]>
git.saurik.com Git - wxWidgets.git/blob - src/common/sckaddr.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Network address manager
4 // Author: Guilhem Lavaux
8 // Copyright: (c) 1997, 1998 Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
23 #include "wx/object.h"
31 #if !defined(__MWERKS__) && !defined(__SALFORDC__)
36 #include "wx/gsocket.h"
37 #include "wx/socket.h"
38 #include "wx/sckaddr.h"
40 IMPLEMENT_ABSTRACT_CLASS(wxSockAddress
, wxObject
)
41 IMPLEMENT_ABSTRACT_CLASS(wxIPaddress
, wxSockAddress
)
42 IMPLEMENT_DYNAMIC_CLASS(wxIPV4address
, wxIPaddress
)
44 IMPLEMENT_DYNAMIC_CLASS(wxIPV6address
, wxIPaddress
)
46 #if defined(__UNIX__) && !defined(__WINDOWS__) && !defined(__WINE__) && (!defined(__WXMAC__) || defined(__DARWIN__))
47 IMPLEMENT_DYNAMIC_CLASS(wxUNIXaddress
, wxSockAddress
)
50 // ---------------------------------------------------------------------------
52 // ---------------------------------------------------------------------------
54 void wxSockAddress::Init()
56 if ( !wxSocketBase::IsInitialized() )
58 // we must do it before using GAddress_XXX functions
59 (void)wxSocketBase::Initialize();
63 wxSockAddress::wxSockAddress()
67 m_address
= GAddress_new();
70 wxSockAddress::wxSockAddress(const wxSockAddress
& other
)
75 m_address
= GAddress_copy(other
.m_address
);
78 wxSockAddress::~wxSockAddress()
80 GAddress_destroy(m_address
);
83 void wxSockAddress::SetAddress(GAddress
*address
)
85 if ( address
!= m_address
)
87 GAddress_destroy(m_address
);
88 m_address
= GAddress_copy(address
);
92 wxSockAddress
& wxSockAddress::operator=(const wxSockAddress
& addr
)
94 SetAddress(addr
.GetAddress());
98 void wxSockAddress::Clear()
100 GAddress_destroy(m_address
);
101 m_address
= GAddress_new();
104 // ---------------------------------------------------------------------------
106 // ---------------------------------------------------------------------------
108 wxIPaddress::wxIPaddress()
113 wxIPaddress::wxIPaddress(const wxIPaddress
& other
)
114 : wxSockAddress(other
)
118 wxIPaddress::~wxIPaddress()
122 // ---------------------------------------------------------------------------
124 // ---------------------------------------------------------------------------
126 wxIPV4address::wxIPV4address()
131 wxIPV4address::wxIPV4address(const wxIPV4address
& other
)
136 wxIPV4address::~wxIPV4address()
140 bool wxIPV4address::Hostname(const wxString
& name
)
142 // Some people are sometimes fool.
145 wxLogWarning( _("Trying to solve a NULL hostname: giving up") );
148 m_origHostname
= name
;
149 return (GAddress_INET_SetHostName(m_address
, name
.mb_str()) == GSOCK_NOERROR
);
152 bool wxIPV4address::Hostname(unsigned long addr
)
154 bool rv
= (GAddress_INET_SetHostAddress(m_address
, addr
) == GSOCK_NOERROR
);
156 m_origHostname
= Hostname();
158 m_origHostname
= wxEmptyString
;
162 bool wxIPV4address::Service(const wxString
& name
)
164 return (GAddress_INET_SetPortName(m_address
, name
.mb_str(), "tcp") == GSOCK_NOERROR
);
167 bool wxIPV4address::Service(unsigned short port
)
169 return (GAddress_INET_SetPort(m_address
, port
) == GSOCK_NOERROR
);
172 bool wxIPV4address::LocalHost()
174 return (GAddress_INET_SetHostName(m_address
, "localhost") == GSOCK_NOERROR
);
177 bool wxIPV4address::IsLocalHost() const
179 return (Hostname() == wxT("localhost") || IPAddress() == wxT("127.0.0.1"));
182 bool wxIPV4address::AnyAddress()
184 return (GAddress_INET_SetAnyAddress(m_address
) == GSOCK_NOERROR
);
187 wxString
wxIPV4address::Hostname() const
192 GAddress_INET_GetHostName(m_address
, hostname
, 1024);
193 return wxString::FromAscii(hostname
);
196 unsigned short wxIPV4address::Service() const
198 return GAddress_INET_GetPort(m_address
);
201 wxSockAddress
*wxIPV4address::Clone() const
203 wxIPV4address
*addr
= new wxIPV4address(*this);
204 addr
->m_origHostname
= m_origHostname
;
208 wxString
wxIPV4address::IPAddress() const
210 unsigned long raw
= GAddress_INET_GetHostAddress(m_address
);
211 return wxString::Format(_T("%lu.%lu.%lu.%lu"),
219 bool wxIPV4address::operator==(const wxIPV4address
& addr
) const
221 return Hostname().Cmp(addr
.Hostname().c_str()) == 0 &&
222 Service() == addr
.Service();
226 // ---------------------------------------------------------------------------
228 // ---------------------------------------------------------------------------
230 wxIPV6address::wxIPV6address()
235 wxIPV6address::wxIPV6address(const wxIPV6address
& other
)
240 wxIPV6address::~wxIPV6address()
244 bool wxIPV6address::Hostname(const wxString
& name
)
248 wxLogWarning( _("Trying to solve a NULL hostname: giving up") );
251 return (GAddress_INET_SetHostName(m_address
, name
.mb_str()) == GSOCK_NOERROR
);
254 bool wxIPV6address::Hostname(unsigned char[16] WXUNUSED(addr
))
259 bool wxIPV6address::Service(const wxString
& name
)
261 return (GAddress_INET_SetPortName(m_address
, name
.mb_str(), "tcp") == GSOCK_NOERROR
);
264 bool wxIPV6address::Service(unsigned short port
)
266 return (GAddress_INET_SetPort(m_address
, port
) == GSOCK_NOERROR
);
269 bool wxIPV6address::LocalHost()
271 return (GAddress_INET_SetHostName(m_address
, "localhost") == GSOCK_NOERROR
);
274 bool wxIPV6address::IsLocalHost() const
276 return (Hostname() == wxT("localhost") || IPAddress() == wxT("127.0.0.1"));
279 bool wxIPV6address::AnyAddress()
281 return (GAddress_INET_SetAnyAddress(m_address
) == GSOCK_NOERROR
);
284 wxString
wxIPV6address::IPAddress() const
286 unsigned long raw
= GAddress_INET_GetHostAddress(m_address
);
287 return wxString::Format(
289 (unsigned char)((raw
>>24) & 0xff),
290 (unsigned char)((raw
>>16) & 0xff),
291 (unsigned char)((raw
>>8) & 0xff),
292 (unsigned char)(raw
& 0xff)
296 wxString
wxIPV6address::Hostname() const
301 GAddress_INET_GetHostName(m_address
, hostname
, 1024);
302 return wxString::FromAscii(hostname
);
305 unsigned short wxIPV6address::Service() const
307 return GAddress_INET_GetPort(m_address
);
312 #if defined(__UNIX__) && !defined(__WINDOWS__) && !defined(__WINE__) && (!defined(__WXMAC__) || defined(__DARWIN__))
314 // ---------------------------------------------------------------------------
316 // ---------------------------------------------------------------------------
318 wxUNIXaddress::wxUNIXaddress()
323 wxUNIXaddress::wxUNIXaddress(const wxUNIXaddress
& other
)
324 : wxSockAddress(other
)
328 wxUNIXaddress::~wxUNIXaddress()
332 void wxUNIXaddress::Filename(const wxString
& fname
)
334 GAddress_UNIX_SetPath(m_address
, fname
.fn_str());
337 wxString
wxUNIXaddress::Filename()
342 GAddress_UNIX_GetPath(m_address
, path
, 1024);
344 return wxString::FromAscii(path
);