]>
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(
210 (unsigned char)((raw
>>24) & 0xff),
211 (unsigned char)((raw
>>16) & 0xff),
212 (unsigned char)((raw
>>8) & 0xff),
213 (unsigned char)(raw
& 0xff)
217 bool wxIPV4address::operator==(const wxIPV4address
& addr
) const
219 if(Hostname().Cmp(addr
.Hostname().c_str()) == 0 && Service() == addr
.Service()) return true;
224 // ---------------------------------------------------------------------------
226 // ---------------------------------------------------------------------------
228 wxIPV6address::wxIPV6address()
233 wxIPV6address::wxIPV6address(const wxIPV6address
& other
)
238 wxIPV6address::~wxIPV6address()
242 bool wxIPV6address::Hostname(const wxString
& name
)
246 wxLogWarning( _("Trying to solve a NULL hostname: giving up") );
249 return (GAddress_INET_SetHostName(m_address
, name
.mb_str()) == GSOCK_NOERROR
);
252 bool wxIPV6address::Hostname(unsigned char[16] WXUNUSED(addr
))
257 bool wxIPV6address::Service(const wxString
& name
)
259 return (GAddress_INET_SetPortName(m_address
, name
.mb_str(), "tcp") == GSOCK_NOERROR
);
262 bool wxIPV6address::Service(unsigned short port
)
264 return (GAddress_INET_SetPort(m_address
, port
) == GSOCK_NOERROR
);
267 bool wxIPV6address::LocalHost()
269 return (GAddress_INET_SetHostName(m_address
, "localhost") == GSOCK_NOERROR
);
272 bool wxIPV6address::IsLocalHost() const
274 return (Hostname() == wxT("localhost") || IPAddress() == wxT("127.0.0.1"));
277 bool wxIPV6address::AnyAddress()
279 return (GAddress_INET_SetAnyAddress(m_address
) == GSOCK_NOERROR
);
282 wxString
wxIPV6address::IPAddress() const
284 unsigned long raw
= GAddress_INET_GetHostAddress(m_address
);
285 return wxString::Format(
287 (unsigned char)((raw
>>24) & 0xff),
288 (unsigned char)((raw
>>16) & 0xff),
289 (unsigned char)((raw
>>8) & 0xff),
290 (unsigned char)(raw
& 0xff)
294 wxString
wxIPV6address::Hostname() const
299 GAddress_INET_GetHostName(m_address
, hostname
, 1024);
300 return wxString::FromAscii(hostname
);
303 unsigned short wxIPV6address::Service() const
305 return GAddress_INET_GetPort(m_address
);
310 #if defined(__UNIX__) && !defined(__WINDOWS__) && !defined(__WINE__) && (!defined(__WXMAC__) || defined(__DARWIN__))
312 // ---------------------------------------------------------------------------
314 // ---------------------------------------------------------------------------
316 wxUNIXaddress::wxUNIXaddress()
321 wxUNIXaddress::wxUNIXaddress(const wxUNIXaddress
& other
)
322 : wxSockAddress(other
)
326 wxUNIXaddress::~wxUNIXaddress()
330 void wxUNIXaddress::Filename(const wxString
& fname
)
332 GAddress_UNIX_SetPath(m_address
, fname
.fn_str());
335 wxString
wxUNIXaddress::Filename()
340 GAddress_UNIX_GetPath(m_address
, path
, 1024);
342 return wxString::FromAscii(path
);