]>
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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "sckaddr.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
27 #include "wx/object.h"
35 #if !defined(__MWERKS__) && !defined(__SALFORDC__)
40 #include "wx/gsocket.h"
41 #include "wx/socket.h"
42 #include "wx/sckaddr.h"
44 IMPLEMENT_ABSTRACT_CLASS(wxSockAddress
, wxObject
)
45 IMPLEMENT_ABSTRACT_CLASS(wxIPaddress
, wxSockAddress
)
46 IMPLEMENT_DYNAMIC_CLASS(wxIPV4address
, wxIPaddress
)
48 IMPLEMENT_DYNAMIC_CLASS(wxIPV6address
, wxIPaddress
)
50 #if defined(__UNIX__) && !defined(__WINDOWS__) && !defined(__WINE__) && (!defined(__WXMAC__) || defined(__DARWIN__))
51 IMPLEMENT_DYNAMIC_CLASS(wxUNIXaddress
, wxSockAddress
)
54 // ---------------------------------------------------------------------------
56 // ---------------------------------------------------------------------------
58 void wxSockAddress::Init()
60 if ( !wxSocketBase::IsInitialized() )
62 // we must do it before using GAddress_XXX functions
63 (void)wxSocketBase::Initialize();
67 wxSockAddress::wxSockAddress()
71 m_address
= GAddress_new();
74 wxSockAddress::wxSockAddress(const wxSockAddress
& other
)
79 m_address
= GAddress_copy(other
.m_address
);
82 wxSockAddress::~wxSockAddress()
84 GAddress_destroy(m_address
);
87 void wxSockAddress::SetAddress(GAddress
*address
)
89 GAddress_destroy(m_address
);
90 m_address
= GAddress_copy(address
);
93 wxSockAddress
& wxSockAddress::operator=(const wxSockAddress
& addr
)
95 SetAddress(addr
.GetAddress());
99 void wxSockAddress::Clear()
101 GAddress_destroy(m_address
);
102 m_address
= GAddress_new();
105 // ---------------------------------------------------------------------------
107 // ---------------------------------------------------------------------------
109 wxIPaddress::wxIPaddress()
114 wxIPaddress::wxIPaddress(const wxIPaddress
& other
)
115 : wxSockAddress(other
)
119 wxIPaddress::~wxIPaddress()
123 // ---------------------------------------------------------------------------
125 // ---------------------------------------------------------------------------
127 wxIPV4address::wxIPV4address()
132 wxIPV4address::wxIPV4address(const wxIPV4address
& other
)
137 wxIPV4address::~wxIPV4address()
141 bool wxIPV4address::Hostname(const wxString
& name
)
143 // Some people are sometimes fool.
146 wxLogWarning( _("Trying to solve a NULL hostname: giving up") );
149 m_origHostname
= name
;
150 return (GAddress_INET_SetHostName(m_address
, name
.mb_str()) == GSOCK_NOERROR
);
153 bool wxIPV4address::Hostname(unsigned long addr
)
155 bool rv
= (GAddress_INET_SetHostAddress(m_address
, addr
) == GSOCK_NOERROR
);
157 m_origHostname
= Hostname();
159 m_origHostname
= wxEmptyString
;
163 bool wxIPV4address::Service(const wxString
& name
)
165 return (GAddress_INET_SetPortName(m_address
, name
.mb_str(), "tcp") == GSOCK_NOERROR
);
168 bool wxIPV4address::Service(unsigned short port
)
170 return (GAddress_INET_SetPort(m_address
, port
) == GSOCK_NOERROR
);
173 bool wxIPV4address::LocalHost()
175 return (GAddress_INET_SetHostName(m_address
, "localhost") == GSOCK_NOERROR
);
178 bool wxIPV4address::IsLocalHost() const
180 return (Hostname() == wxT("localhost") || IPAddress() == wxT("127.0.0.1"));
183 bool wxIPV4address::AnyAddress()
185 return (GAddress_INET_SetAnyAddress(m_address
) == GSOCK_NOERROR
);
188 wxString
wxIPV4address::Hostname() const
193 GAddress_INET_GetHostName(m_address
, hostname
, 1024);
194 return wxString::FromAscii(hostname
);
197 unsigned short wxIPV4address::Service() const
199 return GAddress_INET_GetPort(m_address
);
202 wxSockAddress
*wxIPV4address::Clone() const
204 wxIPV4address
*addr
= new wxIPV4address(*this);
205 addr
->m_origHostname
= m_origHostname
;
209 wxString
wxIPV4address::IPAddress() const
211 unsigned long raw
= GAddress_INET_GetHostAddress(m_address
);
212 return wxString::Format(
214 (unsigned char)((raw
>>24) & 0xff),
215 (unsigned char)((raw
>>16) & 0xff),
216 (unsigned char)((raw
>>8) & 0xff),
217 (unsigned char)(raw
& 0xff)
221 bool wxIPV4address::operator==(wxIPV4address
& addr
)
223 if(Hostname().Cmp(addr
.Hostname().c_str()) == 0 && Service() == addr
.Service()) return true;
228 // ---------------------------------------------------------------------------
230 // ---------------------------------------------------------------------------
232 wxIPV6address::wxIPV6address()
237 wxIPV6address::wxIPV6address(const wxIPV6address
& other
)
242 wxIPV6address::~wxIPV6address()
246 bool wxIPV6address::Hostname(const wxString
& name
)
250 wxLogWarning( _("Trying to solve a NULL hostname: giving up") );
253 return (GAddress_INET_SetHostName(m_address
, name
.mb_str()) == GSOCK_NOERROR
);
256 bool wxIPV6address::Hostname(unsigned char[16] WXUNUSED(addr
))
261 bool wxIPV6address::Service(const wxString
& name
)
263 return (GAddress_INET_SetPortName(m_address
, name
.mb_str(), "tcp") == GSOCK_NOERROR
);
266 bool wxIPV6address::Service(unsigned short port
)
268 return (GAddress_INET_SetPort(m_address
, port
) == GSOCK_NOERROR
);
271 bool wxIPV6address::LocalHost()
273 return (GAddress_INET_SetHostName(m_address
, "localhost") == GSOCK_NOERROR
);
276 bool wxIPV6address::IsLocalHost() const
278 return (Hostname() == wxT("localhost") || IPAddress() == wxT("127.0.0.1"));
281 bool wxIPV6address::AnyAddress()
283 return (GAddress_INET_SetAnyAddress(m_address
) == GSOCK_NOERROR
);
286 wxString
wxIPV6address::IPAddress() const
288 unsigned long raw
= GAddress_INET_GetHostAddress(m_address
);
289 return wxString::Format(
291 (unsigned char)((raw
>>24) & 0xff),
292 (unsigned char)((raw
>>16) & 0xff),
293 (unsigned char)((raw
>>8) & 0xff),
294 (unsigned char)(raw
& 0xff)
298 wxString
wxIPV6address::Hostname() const
303 GAddress_INET_GetHostName(m_address
, hostname
, 1024);
304 return wxString::FromAscii(hostname
);
307 unsigned short wxIPV6address::Service() const
309 return GAddress_INET_GetPort(m_address
);
314 #if defined(__UNIX__) && !defined(__WINDOWS__) && !defined(__WINE__) && (!defined(__WXMAC__) || defined(__DARWIN__))
316 // ---------------------------------------------------------------------------
318 // ---------------------------------------------------------------------------
320 wxUNIXaddress::wxUNIXaddress()
325 wxUNIXaddress::wxUNIXaddress(const wxUNIXaddress
& other
)
326 : wxSockAddress(other
)
330 wxUNIXaddress::~wxUNIXaddress()
334 void wxUNIXaddress::Filename(const wxString
& fname
)
336 GAddress_UNIX_SetPath(m_address
, fname
.fn_str());
339 wxString
wxUNIXaddress::Filename()
344 GAddress_UNIX_GetPath(m_address
, path
, 1024);
346 return wxString::FromAscii(path
);