]>
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__)
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::BroadcastAddress()
183 return (GAddress_INET_SetBroadcastAddress(m_address
) == GSOCK_NOERROR
);
186 bool wxIPV4address::AnyAddress()
188 return (GAddress_INET_SetAnyAddress(m_address
) == GSOCK_NOERROR
);
191 wxString
wxIPV4address::Hostname() const
196 GAddress_INET_GetHostName(m_address
, hostname
, 1024);
197 return wxString::FromAscii(hostname
);
200 unsigned short wxIPV4address::Service() const
202 return GAddress_INET_GetPort(m_address
);
205 wxSockAddress
*wxIPV4address::Clone() const
207 wxIPV4address
*addr
= new wxIPV4address(*this);
208 addr
->m_origHostname
= m_origHostname
;
212 wxString
wxIPV4address::IPAddress() const
214 unsigned long raw
= GAddress_INET_GetHostAddress(m_address
);
215 return wxString::Format(_T("%lu.%lu.%lu.%lu"),
223 bool wxIPV4address::operator==(const wxIPV4address
& addr
) const
225 return Hostname().Cmp(addr
.Hostname()) == 0 &&
226 Service() == addr
.Service();
230 // ---------------------------------------------------------------------------
232 // ---------------------------------------------------------------------------
234 wxIPV6address::wxIPV6address()
239 wxIPV6address::wxIPV6address(const wxIPV6address
& other
)
240 : wxIPaddress(other
), m_origHostname(other
.m_origHostname
)
244 wxIPV6address::~wxIPV6address()
248 bool wxIPV6address::Hostname(const wxString
& name
)
252 wxLogWarning( _("Trying to solve a NULL hostname: giving up") );
255 m_origHostname
= name
;
256 return (GAddress_INET6_SetHostName(m_address
, name
.mb_str()) == GSOCK_NOERROR
);
259 bool wxIPV6address::Hostname(unsigned char addr
[16])
262 unsigned short wk
[8];
263 for ( int i
= 0; i
< 8; ++i
)
267 wk
[i
] |= addr
[2*i
+1];
269 name
.Printf("%x:%x:%x:%x:%x:%x:%x:%x",
270 wk
[0], wk
[1], wk
[2], wk
[3], wk
[4], wk
[5], wk
[6], wk
[7]);
271 return Hostname(name
);
274 bool wxIPV6address::Service(const wxString
& name
)
276 return (GAddress_INET6_SetPortName(m_address
, name
.mb_str(), "tcp") == GSOCK_NOERROR
);
279 bool wxIPV6address::Service(unsigned short port
)
281 return (GAddress_INET6_SetPort(m_address
, port
) == GSOCK_NOERROR
);
284 bool wxIPV6address::LocalHost()
286 return (GAddress_INET6_SetHostName(m_address
, "localhost") == GSOCK_NOERROR
);
289 bool wxIPV6address::IsLocalHost() const
291 if ( Hostname() == "localhost" )
294 wxString addr
= IPAddress();
295 return addr
== wxT("::1") ||
296 addr
== wxT("0:0:0:0:0:0:0:1") ||
297 addr
== wxT("::ffff:127.0.0.1");
300 bool wxIPV6address::BroadcastAddress()
302 wxFAIL_MSG( "not implemented" );
307 bool wxIPV6address::AnyAddress()
309 return (GAddress_INET6_SetAnyAddress(m_address
) == GSOCK_NOERROR
);
312 wxString
wxIPV6address::IPAddress() const
314 unsigned char addr
[16];
315 GAddress_INET6_GetHostAddress(m_address
,(in6_addr
*)addr
);
319 prefix_zero_count
= 0;
320 for ( i
= 0; i
< 8; ++i
)
322 words
[i
] = addr
[i
*2];
324 words
[i
] |= addr
[i
*2+1];
325 if ( i
== prefix_zero_count
&& words
[i
] == 0 )
330 if ( prefix_zero_count
== 8 )
332 result
= wxT( "::" );
334 else if ( prefix_zero_count
== 6 && words
[5] == 0xFFFF )
337 result
.Printf("::ffff:%d.%d.%d.%d",
338 addr
[12], addr
[13], addr
[14], addr
[15]);
343 for ( i
= prefix_zero_count
; i
< 8; ++i
)
345 result
+= wxString::Format(":%x", words
[i
]);
352 wxString
wxIPV6address::Hostname() const
357 if ( GAddress_INET6_GetHostName(m_address
,
359 WXSIZEOF(hostname
)) != GSOCK_NOERROR
)
360 return wxEmptyString
;
362 return wxString::FromAscii(hostname
);
365 unsigned short wxIPV6address::Service() const
367 return GAddress_INET6_GetPort(m_address
);
372 #if defined(__UNIX__) && !defined(__WINDOWS__) && !defined(__WINE__)
374 // ---------------------------------------------------------------------------
376 // ---------------------------------------------------------------------------
378 wxUNIXaddress::wxUNIXaddress()
383 wxUNIXaddress::wxUNIXaddress(const wxUNIXaddress
& other
)
384 : wxSockAddress(other
)
388 wxUNIXaddress::~wxUNIXaddress()
392 void wxUNIXaddress::Filename(const wxString
& fname
)
394 GAddress_UNIX_SetPath(m_address
, fname
.fn_str());
397 wxString
wxUNIXaddress::Filename()
402 GAddress_UNIX_GetPath(m_address
, path
, 1024);
404 return wxString::FromAscii(path
);