]>
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 GAddress_destroy(m_address
);
86 m_address
= GAddress_copy(address
);
89 wxSockAddress
& wxSockAddress::operator=(const wxSockAddress
& addr
)
91 SetAddress(addr
.GetAddress());
95 void wxSockAddress::Clear()
97 GAddress_destroy(m_address
);
98 m_address
= GAddress_new();
101 // ---------------------------------------------------------------------------
103 // ---------------------------------------------------------------------------
105 wxIPaddress::wxIPaddress()
110 wxIPaddress::wxIPaddress(const wxIPaddress
& other
)
111 : wxSockAddress(other
)
115 wxIPaddress::~wxIPaddress()
119 // ---------------------------------------------------------------------------
121 // ---------------------------------------------------------------------------
123 wxIPV4address::wxIPV4address()
128 wxIPV4address::wxIPV4address(const wxIPV4address
& other
)
133 wxIPV4address::~wxIPV4address()
137 bool wxIPV4address::Hostname(const wxString
& name
)
139 // Some people are sometimes fool.
142 wxLogWarning( _("Trying to solve a NULL hostname: giving up") );
145 m_origHostname
= name
;
146 return (GAddress_INET_SetHostName(m_address
, name
.mb_str()) == GSOCK_NOERROR
);
149 bool wxIPV4address::Hostname(unsigned long addr
)
151 bool rv
= (GAddress_INET_SetHostAddress(m_address
, addr
) == GSOCK_NOERROR
);
153 m_origHostname
= Hostname();
155 m_origHostname
= wxEmptyString
;
159 bool wxIPV4address::Service(const wxString
& name
)
161 return (GAddress_INET_SetPortName(m_address
, name
.mb_str(), "tcp") == GSOCK_NOERROR
);
164 bool wxIPV4address::Service(unsigned short port
)
166 return (GAddress_INET_SetPort(m_address
, port
) == GSOCK_NOERROR
);
169 bool wxIPV4address::LocalHost()
171 return (GAddress_INET_SetHostName(m_address
, "localhost") == GSOCK_NOERROR
);
174 bool wxIPV4address::IsLocalHost() const
176 return (Hostname() == wxT("localhost") || IPAddress() == wxT("127.0.0.1"));
179 bool wxIPV4address::AnyAddress()
181 return (GAddress_INET_SetAnyAddress(m_address
) == GSOCK_NOERROR
);
184 wxString
wxIPV4address::Hostname() const
189 GAddress_INET_GetHostName(m_address
, hostname
, 1024);
190 return wxString::FromAscii(hostname
);
193 unsigned short wxIPV4address::Service() const
195 return GAddress_INET_GetPort(m_address
);
198 wxSockAddress
*wxIPV4address::Clone() const
200 wxIPV4address
*addr
= new wxIPV4address(*this);
201 addr
->m_origHostname
= m_origHostname
;
205 wxString
wxIPV4address::IPAddress() const
207 unsigned long raw
= GAddress_INET_GetHostAddress(m_address
);
208 return wxString::Format(_T("%lu.%lu.%lu.%lu"),
216 bool wxIPV4address::operator==(const wxIPV4address
& addr
) const
218 return Hostname().Cmp(addr
.Hostname().c_str()) == 0 &&
219 Service() == addr
.Service();
223 // ---------------------------------------------------------------------------
225 // ---------------------------------------------------------------------------
227 wxIPV6address::wxIPV6address()
232 wxIPV6address::wxIPV6address(const wxIPV6address
& other
)
237 wxIPV6address::~wxIPV6address()
241 bool wxIPV6address::Hostname(const wxString
& name
)
245 wxLogWarning( _("Trying to solve a NULL hostname: giving up") );
248 return (GAddress_INET_SetHostName(m_address
, name
.mb_str()) == GSOCK_NOERROR
);
251 bool wxIPV6address::Hostname(unsigned char[16] WXUNUSED(addr
))
256 bool wxIPV6address::Service(const wxString
& name
)
258 return (GAddress_INET_SetPortName(m_address
, name
.mb_str(), "tcp") == GSOCK_NOERROR
);
261 bool wxIPV6address::Service(unsigned short port
)
263 return (GAddress_INET_SetPort(m_address
, port
) == GSOCK_NOERROR
);
266 bool wxIPV6address::LocalHost()
268 return (GAddress_INET_SetHostName(m_address
, "localhost") == GSOCK_NOERROR
);
271 bool wxIPV6address::IsLocalHost() const
273 return (Hostname() == wxT("localhost") || IPAddress() == wxT("127.0.0.1"));
276 bool wxIPV6address::AnyAddress()
278 return (GAddress_INET_SetAnyAddress(m_address
) == GSOCK_NOERROR
);
281 wxString
wxIPV6address::IPAddress() const
283 unsigned long raw
= GAddress_INET_GetHostAddress(m_address
);
284 return wxString::Format(
286 (unsigned char)((raw
>>24) & 0xff),
287 (unsigned char)((raw
>>16) & 0xff),
288 (unsigned char)((raw
>>8) & 0xff),
289 (unsigned char)(raw
& 0xff)
293 wxString
wxIPV6address::Hostname() const
298 GAddress_INET_GetHostName(m_address
, hostname
, 1024);
299 return wxString::FromAscii(hostname
);
302 unsigned short wxIPV6address::Service() const
304 return GAddress_INET_GetPort(m_address
);
309 #if defined(__UNIX__) && !defined(__WINDOWS__) && !defined(__WINE__) && (!defined(__WXMAC__) || defined(__DARWIN__))
311 // ---------------------------------------------------------------------------
313 // ---------------------------------------------------------------------------
315 wxUNIXaddress::wxUNIXaddress()
320 wxUNIXaddress::wxUNIXaddress(const wxUNIXaddress
& other
)
321 : wxSockAddress(other
)
325 wxUNIXaddress::~wxUNIXaddress()
329 void wxUNIXaddress::Filename(const wxString
& fname
)
331 GAddress_UNIX_SetPath(m_address
, fname
.fn_str());
334 wxString
wxUNIXaddress::Filename()
339 GAddress_UNIX_GetPath(m_address
, path
, 1024);
341 return wxString::FromAscii(path
);