]>
git.saurik.com Git - wxWidgets.git/blob - src/common/sckaddr.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/sckaddr.cpp
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"
22 #include "wx/object.h"
30 #if !defined(__MWERKS__) && !defined(__SALFORDC__)
35 #include "wx/gsocket.h"
36 #include "wx/socket.h"
37 #include "wx/sckaddr.h"
39 IMPLEMENT_ABSTRACT_CLASS(wxSockAddress
, wxObject
)
40 IMPLEMENT_ABSTRACT_CLASS(wxIPaddress
, wxSockAddress
)
41 IMPLEMENT_DYNAMIC_CLASS(wxIPV4address
, wxIPaddress
)
43 IMPLEMENT_DYNAMIC_CLASS(wxIPV6address
, wxIPaddress
)
45 #if defined(__UNIX__) && !defined(__WINDOWS__) && !defined(__WINE__) && (!defined(__WXMAC__) || defined(__DARWIN__))
46 IMPLEMENT_DYNAMIC_CLASS(wxUNIXaddress
, wxSockAddress
)
49 // ---------------------------------------------------------------------------
51 // ---------------------------------------------------------------------------
53 void wxSockAddress::Init()
55 if ( !wxSocketBase::IsInitialized() )
57 // we must do it before using GAddress_XXX functions
58 (void)wxSocketBase::Initialize();
62 wxSockAddress::wxSockAddress()
66 m_address
= GAddress_new();
69 wxSockAddress::wxSockAddress(const wxSockAddress
& other
)
74 m_address
= GAddress_copy(other
.m_address
);
77 wxSockAddress::~wxSockAddress()
79 GAddress_destroy(m_address
);
82 void wxSockAddress::SetAddress(GAddress
*address
)
84 if ( address
!= m_address
)
86 GAddress_destroy(m_address
);
87 m_address
= GAddress_copy(address
);
91 wxSockAddress
& wxSockAddress::operator=(const wxSockAddress
& addr
)
93 SetAddress(addr
.GetAddress());
97 void wxSockAddress::Clear()
99 GAddress_destroy(m_address
);
100 m_address
= GAddress_new();
103 // ---------------------------------------------------------------------------
105 // ---------------------------------------------------------------------------
107 wxIPaddress::wxIPaddress()
112 wxIPaddress::wxIPaddress(const wxIPaddress
& other
)
113 : wxSockAddress(other
)
117 wxIPaddress::~wxIPaddress()
121 // ---------------------------------------------------------------------------
123 // ---------------------------------------------------------------------------
125 wxIPV4address::wxIPV4address()
130 wxIPV4address::wxIPV4address(const wxIPV4address
& other
)
135 wxIPV4address::~wxIPV4address()
139 bool wxIPV4address::Hostname(const wxString
& name
)
141 // Some people are sometimes fool.
144 wxLogWarning( _("Trying to solve a NULL hostname: giving up") );
147 m_origHostname
= name
;
148 return (GAddress_INET_SetHostName(m_address
, name
.mb_str()) == GSOCK_NOERROR
);
151 bool wxIPV4address::Hostname(unsigned long addr
)
153 bool rv
= (GAddress_INET_SetHostAddress(m_address
, addr
) == GSOCK_NOERROR
);
155 m_origHostname
= Hostname();
157 m_origHostname
= wxEmptyString
;
161 bool wxIPV4address::Service(const wxString
& name
)
163 return (GAddress_INET_SetPortName(m_address
, name
.mb_str(), "tcp") == GSOCK_NOERROR
);
166 bool wxIPV4address::Service(unsigned short port
)
168 return (GAddress_INET_SetPort(m_address
, port
) == GSOCK_NOERROR
);
171 bool wxIPV4address::LocalHost()
173 return (GAddress_INET_SetHostName(m_address
, "localhost") == GSOCK_NOERROR
);
176 bool wxIPV4address::IsLocalHost() const
178 return (Hostname() == wxT("localhost") || IPAddress() == wxT("127.0.0.1"));
181 bool wxIPV4address::AnyAddress()
183 return (GAddress_INET_SetAnyAddress(m_address
) == GSOCK_NOERROR
);
186 wxString
wxIPV4address::Hostname() const
191 GAddress_INET_GetHostName(m_address
, hostname
, 1024);
192 return wxString::FromAscii(hostname
);
195 unsigned short wxIPV4address::Service() const
197 return GAddress_INET_GetPort(m_address
);
200 wxSockAddress
*wxIPV4address::Clone() const
202 wxIPV4address
*addr
= new wxIPV4address(*this);
203 addr
->m_origHostname
= m_origHostname
;
207 wxString
wxIPV4address::IPAddress() const
209 unsigned long raw
= GAddress_INET_GetHostAddress(m_address
);
210 return wxString::Format(_T("%lu.%lu.%lu.%lu"),
218 bool wxIPV4address::operator==(const wxIPV4address
& addr
) const
220 return Hostname().Cmp(addr
.Hostname().c_str()) == 0 &&
221 Service() == addr
.Service();
225 // ---------------------------------------------------------------------------
227 // ---------------------------------------------------------------------------
229 wxIPV6address::wxIPV6address()
234 wxIPV6address::wxIPV6address(const wxIPV6address
& other
)
239 wxIPV6address::~wxIPV6address()
243 bool wxIPV6address::Hostname(const wxString
& name
)
247 wxLogWarning( _("Trying to solve a NULL hostname: giving up") );
250 return (GAddress_INET_SetHostName(m_address
, name
.mb_str()) == GSOCK_NOERROR
);
253 bool wxIPV6address::Hostname(unsigned char[16] WXUNUSED(addr
))
258 bool wxIPV6address::Service(const wxString
& name
)
260 return (GAddress_INET_SetPortName(m_address
, name
.mb_str(), "tcp") == GSOCK_NOERROR
);
263 bool wxIPV6address::Service(unsigned short port
)
265 return (GAddress_INET_SetPort(m_address
, port
) == GSOCK_NOERROR
);
268 bool wxIPV6address::LocalHost()
270 return (GAddress_INET_SetHostName(m_address
, "localhost") == GSOCK_NOERROR
);
273 bool wxIPV6address::IsLocalHost() const
275 return (Hostname() == wxT("localhost") || IPAddress() == wxT("127.0.0.1"));
278 bool wxIPV6address::AnyAddress()
280 return (GAddress_INET_SetAnyAddress(m_address
) == GSOCK_NOERROR
);
283 wxString
wxIPV6address::IPAddress() const
285 unsigned long raw
= GAddress_INET_GetHostAddress(m_address
);
286 return wxString::Format(
288 (unsigned char)((raw
>>24) & 0xff),
289 (unsigned char)((raw
>>16) & 0xff),
290 (unsigned char)((raw
>>8) & 0xff),
291 (unsigned char)(raw
& 0xff)
295 wxString
wxIPV6address::Hostname() const
300 GAddress_INET_GetHostName(m_address
, hostname
, 1024);
301 return wxString::FromAscii(hostname
);
304 unsigned short wxIPV6address::Service() const
306 return GAddress_INET_GetPort(m_address
);
311 #if defined(__UNIX__) && !defined(__WINDOWS__) && !defined(__WINE__) && (!defined(__WXMAC__) || defined(__DARWIN__))
313 // ---------------------------------------------------------------------------
315 // ---------------------------------------------------------------------------
317 wxUNIXaddress::wxUNIXaddress()
322 wxUNIXaddress::wxUNIXaddress(const wxUNIXaddress
& other
)
323 : wxSockAddress(other
)
327 wxUNIXaddress::~wxUNIXaddress()
331 void wxUNIXaddress::Filename(const wxString
& fname
)
333 GAddress_UNIX_SetPath(m_address
, fname
.fn_str());
336 wxString
wxUNIXaddress::Filename()
341 GAddress_UNIX_GetPath(m_address
, path
, 1024);
343 return wxString::FromAscii(path
);