]> git.saurik.com Git - wxWidgets.git/blame - src/common/sckaddr.cpp
no real change: put event handlers together at the end of the file
[wxWidgets.git] / src / common / sckaddr.cpp
CommitLineData
f4ada568 1/////////////////////////////////////////////////////////////////////////////
8898456d 2// Name: src/common/sckaddr.cpp
f4ada568
GL
3// Purpose: Network address manager
4// Author: Guilhem Lavaux
f4ada568 5// Created: 26/04/97
c9bccf23 6// Modified by: Vadim Zeitlin to use wxSockAddressImpl on 2008-12-28
f4ada568
GL
7// RCS-ID: $Id$
8// Copyright: (c) 1997, 1998 Guilhem Lavaux
c9bccf23 9// (c) 2008 Vadim Zeitlin
65571936 10// Licence: wxWindows licence
f4ada568
GL
11/////////////////////////////////////////////////////////////////////////////
12
c9bccf23
VZ
13// ============================================================================
14// declarations
15// ============================================================================
16
17// ----------------------------------------------------------------------------
18// headers
19// ----------------------------------------------------------------------------
20
fcc6dddd
JS
21// For compilers that support precompilation, includes "wx.h".
22#include "wx/wxprec.h"
23
24#ifdef __BORLANDC__
8898456d 25 #pragma hdrstop
fcc6dddd
JS
26#endif
27
35a4dab7
GL
28#if wxUSE_SOCKETS
29
6c0d0845 30#ifndef WX_PRECOMP
6c0d0845
VZ
31 #include "wx/object.h"
32 #include "wx/log.h"
33 #include "wx/intl.h"
ce3ed50d 34
6c0d0845
VZ
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <ctype.h>
38
f172cb82 39 #if !defined(__MWERKS__)
6c0d0845
VZ
40 #include <memory.h>
41 #endif
42#endif // !WX_PRECOMP
f4ada568 43
6c0d0845 44#include "wx/socket.h"
3096bd2f 45#include "wx/sckaddr.h"
60913641 46#include "wx/private/socket.h"
c9bccf23
VZ
47#include "wx/private/sckaddr.h"
48
169f96cd
VZ
49#include <errno.h>
50
c9bccf23
VZ
51#ifdef __UNIX__
52 #include <netdb.h>
53 #include <arpa/inet.h>
54#endif // __UNIX__
55
56#ifndef INADDR_NONE
57 #define INADDR_NONE INADDR_ANY
58#endif
59
60// ----------------------------------------------------------------------------
61// wxRTTI macros
62// ----------------------------------------------------------------------------
f4ada568 63
f4ada568 64IMPLEMENT_ABSTRACT_CLASS(wxSockAddress, wxObject)
be4bd463
JS
65IMPLEMENT_ABSTRACT_CLASS(wxIPaddress, wxSockAddress)
66IMPLEMENT_DYNAMIC_CLASS(wxIPV4address, wxIPaddress)
67#if wxUSE_IPV6
68IMPLEMENT_DYNAMIC_CLASS(wxIPV6address, wxIPaddress)
f4ada568 69#endif
c9bccf23 70#if defined(__UNIX__) && !defined(__WINDOWS__) && !defined(__WINE__)
f4ada568
GL
71IMPLEMENT_DYNAMIC_CLASS(wxUNIXaddress, wxSockAddress)
72#endif
f4ada568 73
c9bccf23
VZ
74// ============================================================================
75// implementation of thread-safe/reentrant functions if they're missing
76// ============================================================================
a324a7bc 77
c9bccf23 78// TODO: use POSIX getaddrinfo() (also available in Winsock 2) for simplicity
67badd57 79// and to use the same code for IPv4 and IPv6 support
c9bccf23
VZ
80
81#ifdef __WXMSW__
82 #define HAVE_INET_ADDR
83
84 #define HAVE_GETHOSTBYNAME
85 #define HAVE_GETSERVBYNAME
86
87 // under MSW getxxxbyname() functions are MT-safe (but not reentrant) so
88 // we don't need to serialize calls to them
89 #define wxHAS_MT_SAFE_GETBY_FUNCS
67badd57
VZ
90
91 #if wxUSE_IPV6
92 // this header does dynamic dispatching of getaddrinfo/freeaddrinfo()
93 // by implementing them in its own code if the system versions are not
94 // available (as is the case for anything < XP)
95 //
96 // NB: if this is not available for the other compilers (so far tested
97 // with MSVC only) we should just use wxDynamicLibrary "manually"
98 #ifdef __VISUALC__
99 // disable a warning occurring in Microsoft own version of this file
100 #pragma warning(disable:4706)
101 #endif
102 #include <wspiapi.h>
103 #ifdef __VISUALC__
104 #pragma warning(default:4706)
105 #endif
106 #endif
c9bccf23
VZ
107#endif // __WXMSW__
108
109// we assume that we have gethostbyaddr_r() if and only if we have
110// gethostbyname_r() and that it uses the similar conventions to it (see
111// comment in configure)
112#define HAVE_GETHOSTBYADDR HAVE_GETHOSTBYNAME
113#ifdef HAVE_FUNC_GETHOSTBYNAME_R_3
114 #define HAVE_FUNC_GETHOSTBYADDR_R_3
115#endif
116#ifdef HAVE_FUNC_GETHOSTBYNAME_R_5
117 #define HAVE_FUNC_GETHOSTBYADDR_R_5
118#endif
119#ifdef HAVE_FUNC_GETHOSTBYNAME_R_6
120 #define HAVE_FUNC_GETHOSTBYADDR_R_6
121#endif
122
123// the _r functions need the extra buffer parameter but unfortunately its type
ec5756d6
VZ
124// differs between different systems and for the systems which use opaque
125// structs for it (at least AIX and OpenBSD) it must be zero-filled before
126// being passed to the system functions
c9bccf23 127#ifdef HAVE_FUNC_GETHOSTBYNAME_R_3
ec5756d6
VZ
128 struct wxGethostBuf : hostent_data
129 {
130 wxGethostBuf()
131 {
132 memset(this, 0, sizeof(hostent_data));
133 }
134 };
c9bccf23
VZ
135#else
136 typedef char wxGethostBuf[1024];
137#endif
138
139#ifdef HAVE_FUNC_GETSERVBYNAME_R_3
ec5756d6
VZ
140 struct wxGetservBuf : servent_data
141 {
142 wxGethostBuf()
143 {
144 memset(this, 0, sizeof(servent_data));
145 }
146 };
c9bccf23
VZ
147#else
148 typedef char wxGetservBuf[1024];
149#endif
150
151#ifdef wxHAS_MT_SAFE_GETBY_FUNCS
152 #define wxLOCK_GETBY_MUTEX(name)
153#else // may need mutexes to protect getxxxbyxxx() calls
154 #if defined(HAVE_GETHOSTBYNAME) || \
155 defined(HAVE_GETHOSTBYADDR) || \
156 defined(HAVE_GETSERVBYNAME)
157 #include "wx/thread.h"
158
159 namespace
160 {
161 // these mutexes are used to serialize
162 wxMutex nameLock, // gethostbyname()
163 addrLock, // gethostbyaddr()
164 servLock; // getservbyname()
165 }
166
167 #define wxLOCK_GETBY_MUTEX(name) wxMutexLocker locker(name ## Lock)
168 #endif // we don't have _r functions
169#endif // wxUSE_THREADS
170
171namespace
6c0d0845 172{
c9bccf23
VZ
173
174#if defined(HAVE_GETHOSTBYNAME)
175hostent *deepCopyHostent(hostent *h,
176 const hostent *he,
177 char *buffer,
178 int size,
179 int *err)
180{
181 /* copy old structure */
182 memcpy(h, he, sizeof(hostent));
183
184 /* copy name */
185 int len = strlen(h->h_name);
186 if (len > size)
6c0d0845 187 {
c9bccf23
VZ
188 *err = ENOMEM;
189 return NULL;
6c0d0845 190 }
c9bccf23
VZ
191 memcpy(buffer, h->h_name, len);
192 buffer[len] = '\0';
193 h->h_name = buffer;
6c0d0845 194
c9bccf23
VZ
195 /* track position in the buffer */
196 int pos = len + 1;
197
198 /* reuse len to store address length */
199 len = h->h_length;
200
201 /* ensure pointer alignment */
202 unsigned int misalign = sizeof(char *) - pos%sizeof(char *);
203 if(misalign < sizeof(char *))
204 pos += misalign;
205
206 /* leave space for pointer list */
207 char **p = h->h_addr_list, **q;
208 char **h_addr_list = (char **)(buffer + pos);
209 while(*(p++) != 0)
210 pos += sizeof(char *);
211
212 /* copy addresses and fill new pointer list */
213 for (p = h->h_addr_list, q = h_addr_list; *p != 0; p++, q++)
214 {
215 if (size < pos + len)
216 {
217 *err = ENOMEM;
218 return NULL;
219 }
220 memcpy(buffer + pos, *p, len); /* copy content */
221 *q = buffer + pos; /* set copied pointer to copied content */
222 pos += len;
223 }
224 *++q = 0; /* null terminate the pointer list */
225 h->h_addr_list = h_addr_list; /* copy pointer to pointers */
226
227 /* ensure word alignment of pointers */
228 misalign = sizeof(char *) - pos%sizeof(char *);
229 if(misalign < sizeof(char *))
230 pos += misalign;
231
232 /* leave space for pointer list */
233 p = h->h_aliases;
234 char **h_aliases = (char **)(buffer + pos);
235 while(*(p++) != 0)
236 pos += sizeof(char *);
237
238 /* copy aliases and fill new pointer list */
239 for (p = h->h_aliases, q = h_aliases; *p != 0; p++, q++)
240 {
241 len = strlen(*p);
242 if (size <= pos + len)
243 {
244 *err = ENOMEM;
245 return NULL;
246 }
247 memcpy(buffer + pos, *p, len); /* copy content */
248 buffer[pos + len] = '\0';
249 *q = buffer + pos; /* set copied pointer to copied content */
250 pos += len + 1;
251 }
252 *++q = 0; /* null terminate the pointer list */
253 h->h_aliases = h_aliases; /* copy pointer to pointers */
254
255 return h;
256}
257#endif // HAVE_GETHOSTBYNAME
258
259hostent *wxGethostbyname_r(const char *hostname,
260 hostent *h,
261 wxGethostBuf buffer,
262 int size,
263 int *err)
264{
265 hostent *he;
266#if defined(HAVE_FUNC_GETHOSTBYNAME_R_6)
267 gethostbyname_r(hostname, h, buffer, size, &he, err);
268#elif defined(HAVE_FUNC_GETHOSTBYNAME_R_5)
269 he = gethostbyname_r(hostname, h, buffer, size, err);
270#elif defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
271 he = gethostbyname_r(hostname, h, &buffer);
272 *err = h_errno;
273#elif defined(HAVE_GETHOSTBYNAME)
274 wxLOCK_GETBY_MUTEX(name);
275
276 he = gethostbyname(hostname);
277 *err = h_errno;
278
279 if ( he )
280 he = deepCopyHostent(h, he, buffer, size, err);
281#else
282 #error "No gethostbyname[_r]()"
283#endif
284
285 return he;
286}
287
288hostent *wxGethostbyaddr_r(const char *addr_buf,
289 int buf_size,
290 int proto,
291 hostent *h,
292 wxGethostBuf buffer,
293 int size,
294 int *err)
295{
296 hostent *he;
297#if defined(HAVE_FUNC_GETHOSTBYADDR_R_6)
298 gethostbyaddr_r(addr_buf, buf_size, proto, h, buffer, size, &he, err);
299#elif defined(HAVE_FUNC_GETHOSTBYADDR_R_5)
300 he = gethostbyaddr_r(addr_buf, buf_size, proto, h, buffer, size, err);
301#elif defined(HAVE_FUNC_GETHOSTBYADDR_R_3)
302 he = gethostbyaddr_r(addr_buf, buf_size, proto, h, buffer);
303 *err = h_errno;
304#elif defined(HAVE_GETHOSTBYADDR)
305 wxLOCK_GETBY_MUTEX(addr);
306
307 he = gethostbyaddr(addr_buf, buf_size, proto);
308 *err = h_errno;
309
310 if ( he )
311 he = deepCopyHostent(h, he, buffer, size, err);
312#else
313 #error "No gethostbyaddr[_r]()"
314#endif
6c0d0845 315
c9bccf23 316 return he;
f4ada568
GL
317}
318
c9bccf23
VZ
319#if defined(HAVE_GETSERVBYNAME)
320servent *deepCopyServent(servent *s,
321 servent *se,
322 char *buffer,
323 int size)
1539c2f5 324{
c9bccf23
VZ
325 /* copy plain old structure */
326 memcpy(s, se, sizeof(servent));
6c0d0845 327
c9bccf23
VZ
328 /* copy name */
329 int len = strlen(s->s_name);
330 if (len >= size)
331 {
332 return NULL;
333 }
334 memcpy(buffer, s->s_name, len);
335 buffer[len] = '\0';
336 s->s_name = buffer;
337
338 /* track position in the buffer */
339 int pos = len + 1;
340
341 /* copy protocol */
342 len = strlen(s->s_proto);
343 if (pos + len >= size)
344 {
345 return NULL;
346 }
347 memcpy(buffer + pos, s->s_proto, len);
348 buffer[pos + len] = '\0';
349 s->s_proto = buffer + pos;
350
351 /* track position in the buffer */
352 pos += len + 1;
353
354 /* ensure pointer alignment */
355 unsigned int misalign = sizeof(char *) - pos%sizeof(char *);
356 if(misalign < sizeof(char *))
357 pos += misalign;
358
359 /* leave space for pointer list */
360 char **p = s->s_aliases, **q;
361 char **s_aliases = (char **)(buffer + pos);
362 while(*(p++) != 0)
363 pos += sizeof(char *);
364
365 /* copy addresses and fill new pointer list */
366 for (p = s->s_aliases, q = s_aliases; *p != 0; p++, q++){
367 len = strlen(*p);
368 if (size <= pos + len)
369 {
370 return NULL;
371 }
372 memcpy(buffer + pos, *p, len); /* copy content */
373 buffer[pos + len] = '\0';
374 *q = buffer + pos; /* set copied pointer to copied content */
375 pos += len + 1;
376 }
377 *++q = 0; /* null terminate the pointer list */
378 s->s_aliases = s_aliases; /* copy pointer to pointers */
379 return s;
380}
381#endif // HAVE_GETSERVBYNAME
382
383servent *wxGetservbyname_r(const char *port,
384 const char *protocol,
385 servent *serv,
386 wxGetservBuf buffer,
387 int size)
388{
389 servent *se;
390#if defined(HAVE_FUNC_GETSERVBYNAME_R_6)
391 getservbyname_r(port, protocol, serv, buffer, size, &se);
392#elif defined(HAVE_FUNC_GETSERVBYNAME_R_5)
393 se = getservbyname_r(port, protocol, serv, buffer, size);
394#elif defined(HAVE_FUNC_GETSERVBYNAME_R_4)
395 se = getservbyname_r(port, protocol, serv, &buffer);
396#elif defined(HAVE_GETSERVBYNAME)
397 wxLOCK_GETBY_MUTEX(serv);
398
399 se = getservbyname(port, protocol);
400 if ( se )
401 se = deepCopyServent(serv, se, buffer, size);
402#else
403 #error "No getservbyname[_r]()"
404#endif
405 return se;
1539c2f5
VZ
406}
407
c9bccf23
VZ
408} // anonymous namespace
409
410// ============================================================================
411// wxSockAddressImpl implementation
412// ============================================================================
413
414// FIXME-VC6: helper macros to call Alloc/Get() hiding the ugly dummy argument
415#define ALLOC(T) Alloc(static_cast<T *>(NULL))
416#define GET(T) Get(static_cast<T *>(NULL))
417
418// ----------------------------------------------------------------------------
419// INET or INET6 address family
420// ----------------------------------------------------------------------------
421
422wxString wxSockAddressImpl::GetHostName() const
f4ada568 423{
c9bccf23
VZ
424 const void *addrbuf;
425 int addrbuflen;
426
427#if wxUSE_IPV6
428 if ( m_family == FAMILY_INET6 )
429 {
430 sockaddr_in6 * const addr6 = GET(sockaddr_in6);
431 addrbuf = &addr6->sin6_addr;
432 addrbuflen = sizeof(addr6->sin6_addr);
433 }
434 else
435#endif // wxUSE_IPV6
436 {
437 sockaddr_in * const addr = GET(sockaddr_in);
438 if ( !addr )
439 return wxString();
440
441 addrbuf = &addr->sin_addr;
442 addrbuflen = sizeof(addr->sin_addr);
443 }
444
445 hostent he;
446 wxGethostBuf buffer;
447 int err;
448 if ( !wxGethostbyaddr_r
449 (
450 static_cast<const char *>(addrbuf),
451 addrbuflen,
452 m_family,
453 &he,
454 buffer,
455 sizeof(buffer),
456 &err
457 ) )
458 {
459 return wxString();
460 }
461
462 return wxString::FromUTF8(he.h_name);
f4ada568
GL
463}
464
c9bccf23 465bool wxSockAddressImpl::SetPortName(const wxString& name, const char *protocol)
f4ada568 466{
c9bccf23
VZ
467 // test whether it's a number first
468 unsigned long port;
469 if ( name.ToULong(&port) )
fccb65a2 470 {
c9bccf23
VZ
471 if ( port > 65535 )
472 return false;
fccb65a2 473 }
c9bccf23
VZ
474 else // it's a service name
475 {
476 wxGetservBuf buffer;
477 servent se;
478 if ( !wxGetservbyname_r(name.utf8_str(), protocol, &se,
479 buffer, sizeof(buffer)) )
480 return false;
481
482 // s_port is in network byte order and SetPort() uses the host byte
483 // order and we prefer to reuse it from here instead of assigning to
484 // sin_port directly
485 port = ntohs(se.s_port);
486 }
487
488 return SetPort(port);
f4ada568
GL
489}
490
c9bccf23
VZ
491// ----------------------------------------------------------------------------
492// INET address family
493// ----------------------------------------------------------------------------
494
495void wxSockAddressImpl::CreateINET()
f4ada568 496{
c9bccf23
VZ
497 wxASSERT_MSG( Is(FAMILY_UNSPEC), "recreating address as different type?" );
498
499 m_family = FAMILY_INET;
500 sockaddr_in * const addr = ALLOC(sockaddr_in);
501 addr->sin_family = FAMILY_INET;
f4ada568
GL
502}
503
c9bccf23 504bool wxSockAddressImpl::SetHostName4(const wxString& name)
acd15a3f 505{
c9bccf23
VZ
506 sockaddr_in * const addr = GET(sockaddr_in);
507 if ( !addr )
508 return false;
509
510 const wxUTF8Buf namebuf(name.utf8_str());
511
512 // first check if this is an address in quad dotted notation
513#if defined(HAVE_INET_ATON)
514 if ( inet_aton(namebuf, &addr->sin_addr) )
515 return true;
516#elif defined(HAVE_INET_ADDR)
517 addr->sin_addr.s_addr = inet_addr(namebuf);
518 if ( addr->sin_addr.s_addr != INADDR_NONE )
519 return true;
520#else
521 #error "Neither inet_aton() nor inet_addr() is available?"
522#endif
523
524 // it's a host name, resolve it
525 hostent he;
526 wxGethostBuf buffer;
527 int err;
528 if ( !wxGethostbyname_r(namebuf, &he, buffer, sizeof(buffer), &err) )
529 return false;
530
531 addr->sin_addr.s_addr = ((in_addr *)he.h_addr)->s_addr;
532 return true;
f4ada568 533}
f4ada568 534
c9bccf23
VZ
535bool wxSockAddressImpl::GetHostAddress(wxUint32 *address) const
536{
537 sockaddr_in * const addr = GET(sockaddr_in);
538 if ( !addr )
539 return false;
be4bd463 540
c9bccf23
VZ
541 *address = ntohl(addr->sin_addr.s_addr);
542
543 return true;
544}
545
546bool wxSockAddressImpl::SetHostAddress(wxUint32 address)
be4bd463 547{
c9bccf23
VZ
548 sockaddr_in * const addr = GET(sockaddr_in);
549 if ( !addr )
550 return false;
551
552 addr->sin_addr.s_addr = htonl(address);
553
554 return true;
be4bd463
JS
555}
556
c9bccf23 557wxUint16 wxSockAddressImpl::GetPort4() const
be4bd463 558{
c9bccf23
VZ
559 sockaddr_in * const addr = GET(sockaddr_in);
560 if ( !addr )
561 return 0;
562
563 return ntohs(addr->sin_port);
be4bd463
JS
564}
565
c9bccf23 566bool wxSockAddressImpl::SetPort4(wxUint16 port)
be4bd463 567{
c9bccf23
VZ
568 sockaddr_in * const addr = GET(sockaddr_in);
569 if ( !addr )
570 return false;
571
572 addr->sin_port = htons(port);
573
574 return true;
be4bd463
JS
575}
576
c9bccf23
VZ
577#if wxUSE_IPV6
578
579// ----------------------------------------------------------------------------
580// INET6 address family
581// ----------------------------------------------------------------------------
a324a7bc 582
c9bccf23 583void wxSockAddressImpl::CreateINET6()
1539c2f5 584{
c9bccf23
VZ
585 wxASSERT_MSG( Is(FAMILY_UNSPEC), "recreating address as different type?" );
586
587 m_family = FAMILY_INET6;
588 sockaddr_in6 * const addr = ALLOC(sockaddr_in6);
589 addr->sin6_family = FAMILY_INET6;
1539c2f5
VZ
590}
591
c9bccf23 592bool wxSockAddressImpl::SetHostName6(const wxString& hostname)
f4ada568 593{
c9bccf23
VZ
594 sockaddr_in6 * const addr = GET(sockaddr_in6);
595 if ( !addr )
596 return false;
597
598 addrinfo hints;
599 memset(&hints, 0, sizeof(hints));
600 hints.ai_family = AF_INET6;
601
602 addrinfo *info = NULL;
603 int rc = getaddrinfo(hostname.utf8_str(), NULL, &hints, &info);
604 if ( rc )
605 {
606 // use gai_strerror()?
607 return false;
608 }
609
610 wxCHECK_MSG( info, false, "should have info on success" );
611
612 wxASSERT_MSG( int(info->ai_addrlen) == m_len, "unexpected address length" );
613
614 memcpy(addr, info->ai_addr, info->ai_addrlen);
615 freeaddrinfo(info);
616
617 return true;
a324a7bc 618}
f4ada568 619
c9bccf23 620bool wxSockAddressImpl::GetHostAddress(in6_addr *address) const
a324a7bc 621{
c9bccf23
VZ
622 sockaddr_in6 * const addr = GET(sockaddr_in6);
623 if ( !addr )
624 return false;
625
626 *address = addr->sin6_addr;
627
628 return true;
a324a7bc 629}
f4ada568 630
c9bccf23 631bool wxSockAddressImpl::SetHostAddress(const in6_addr& address)
a324a7bc 632{
c9bccf23
VZ
633 sockaddr_in6 * const addr = GET(sockaddr_in6);
634 if ( !addr )
635 return false;
636
637 addr->sin6_addr = address;
638
639 return true;
f4ada568
GL
640}
641
c9bccf23 642wxUint16 wxSockAddressImpl::GetPort6() const
f4ada568 643{
c9bccf23
VZ
644 sockaddr_in6 * const addr = GET(sockaddr_in6);
645 if ( !addr )
646 return 0;
647
648 return ntohs(addr->sin6_port);
f4ada568
GL
649}
650
c9bccf23 651bool wxSockAddressImpl::SetPort6(wxUint16 port)
f4ada568 652{
c9bccf23
VZ
653 sockaddr_in6 * const addr = GET(sockaddr_in6);
654 if ( !addr )
655 return false;
656
657 addr->sin6_port = htons(port);
658
659 return true;
f4ada568
GL
660}
661
c9bccf23
VZ
662bool wxSockAddressImpl::SetToAnyAddress6()
663{
664 static const in6_addr any = IN6ADDR_ANY_INIT;
665
666 return SetHostAddress(any);
667}
668
669#endif // wxUSE_IPV6
670
671#ifdef wxHAS_UNIX_DOMAIN_SOCKETS
672
673// ----------------------------------------------------------------------------
674// Unix address family
675// ----------------------------------------------------------------------------
676
677#ifndef UNIX_PATH_MAX
678 #define UNIX_PATH_MAX (WXSIZEOF(((sockaddr_un *)NULL)->sun_path))
679#endif
680
681void wxSockAddressImpl::CreateUnix()
f4ada568 682{
c9bccf23
VZ
683 wxASSERT_MSG( Is(FAMILY_UNSPEC), "recreating address as different type?" );
684
685 m_family = FAMILY_UNIX;
686 sockaddr_un * const addr = ALLOC(sockaddr_un);
687 addr->sun_family = FAMILY_UNIX;
688 addr->sun_path[0] = '\0';
f4ada568
GL
689}
690
c9bccf23 691bool wxSockAddressImpl::SetPath(const wxString& path)
f4ada568 692{
c9bccf23
VZ
693 sockaddr_un * const addr = GET(sockaddr_un);
694 if ( !addr )
695 return false;
696
697 const wxUTF8Buf buf(path.utf8_str());
698 if ( strlen(buf) >= UNIX_PATH_MAX )
699 return false;
700
701 wxStrlcpy(addr->sun_path, buf, UNIX_PATH_MAX);
702
703 return true;
f4ada568
GL
704}
705
c9bccf23 706wxString wxSockAddressImpl::GetPath() const
be4bd463 707{
c9bccf23
VZ
708 sockaddr_un * const addr = GET(sockaddr_un);
709 if ( !addr )
710 return wxString();
711
712 return wxString::FromUTF8(addr->sun_path);
be4bd463
JS
713}
714
c9bccf23
VZ
715#endif // wxHAS_UNIX_DOMAIN_SOCKETS
716
717#undef GET
718#undef ALLOC
719
720// ----------------------------------------------------------------------------
721// wxSockAddress
722// ----------------------------------------------------------------------------
723
724void wxSockAddress::Init()
60edcf45 725{
c9bccf23
VZ
726 if ( !wxSocketBase::IsInitialized() )
727 {
728 // we must do it before using any socket functions
729 (void)wxSocketBase::Initialize();
730 }
60edcf45
VZ
731}
732
c9bccf23 733wxSockAddress::wxSockAddress()
d3ea6527 734{
c9bccf23
VZ
735 Init();
736
737 m_impl = new wxSockAddressImpl();
d3ea6527
GRG
738}
739
c9bccf23
VZ
740wxSockAddress::wxSockAddress(const wxSockAddress& other)
741 : wxObject()
f4ada568 742{
c9bccf23 743 Init();
f4ada568 744
c9bccf23 745 m_impl = new wxSockAddressImpl(*other.m_impl);
f4ada568
GL
746}
747
c9bccf23 748wxSockAddress::~wxSockAddress()
f4ada568 749{
c9bccf23 750 delete m_impl;
f4ada568
GL
751}
752
c9bccf23 753void wxSockAddress::SetAddress(const wxSockAddressImpl& address)
ce22d615 754{
c9bccf23
VZ
755 if ( &address != m_impl )
756 {
757 delete m_impl;
758 m_impl = new wxSockAddressImpl(address);
759 }
ce22d615
RD
760}
761
c9bccf23 762wxSockAddress& wxSockAddress::operator=(const wxSockAddress& addr)
d775fa82 763{
c9bccf23
VZ
764 SetAddress(addr.GetAddress());
765
766 return *this;
d9552598
VZ
767}
768
c9bccf23 769void wxSockAddress::Clear()
1d6d8b5d 770{
c9bccf23 771 m_impl->Clear();
1d6d8b5d
JS
772}
773
c9bccf23
VZ
774// ----------------------------------------------------------------------------
775// wxIPaddress
776// ----------------------------------------------------------------------------
f4ada568 777
c9bccf23 778wxSockAddressImpl& wxIPaddress::GetImpl()
f4ada568 779{
c9bccf23
VZ
780 if ( m_impl->GetFamily() == wxSockAddressImpl::FAMILY_UNSPEC )
781 m_impl->CreateINET();
782
783 return *m_impl;
f4ada568
GL
784}
785
c9bccf23 786bool wxIPaddress::Hostname(const wxString& name)
1539c2f5 787{
c9bccf23
VZ
788 wxCHECK_MSG( !name.empty(), false, "empty host name is invalid" );
789
790 m_origHostname = name;
791
792 return GetImpl().SetHostName(name);
1539c2f5
VZ
793}
794
c9bccf23 795bool wxIPaddress::Service(const wxString& name)
f4ada568 796{
c9bccf23 797 return GetImpl().SetPortName(name, "tcp");
f4ada568
GL
798}
799
c9bccf23 800bool wxIPaddress::Service(unsigned short port)
f4ada568 801{
c9bccf23 802 return GetImpl().SetPort(port);
f4ada568
GL
803}
804
c9bccf23 805bool wxIPaddress::LocalHost()
f4ada568 806{
c9bccf23 807 return Hostname("localhost");
f4ada568
GL
808}
809
c9bccf23 810wxString wxIPaddress::Hostname() const
f4ada568 811{
c9bccf23 812 return GetImpl().GetHostName();
f4ada568
GL
813}
814
c9bccf23 815unsigned short wxIPaddress::Service() const
f4ada568 816{
c9bccf23 817 return GetImpl().GetPort();
f4ada568
GL
818}
819
c9bccf23 820bool wxIPaddress::operator==(const wxIPaddress& addr) const
f4ada568 821{
c9bccf23
VZ
822 return Hostname().Cmp(addr.Hostname()) == 0 &&
823 Service() == addr.Service();
f4ada568
GL
824}
825
c9bccf23
VZ
826bool wxIPaddress::AnyAddress()
827{
828 return GetImpl().SetToAnyAddress();
829}
830
831// ----------------------------------------------------------------------------
832// wxIPV4address
833// ----------------------------------------------------------------------------
834
835void wxIPV4address::DoInitImpl()
be4bd463 836{
c9bccf23
VZ
837 m_impl->CreateINET();
838}
8575ff50 839
c9bccf23
VZ
840bool wxIPV4address::Hostname(unsigned long addr)
841{
842 if ( !GetImpl().SetHostAddress(addr) )
843 {
844 m_origHostname.clear();
845 return false;
846 }
847
848 m_origHostname = Hostname();
849 return true;
850}
851
852bool wxIPV4address::IsLocalHost() const
853{
854 return Hostname() == "localhost" || IPAddress() == "127.0.0.1";
be4bd463
JS
855}
856
c9bccf23 857wxString wxIPV4address::IPAddress() const
60edcf45 858{
c9bccf23
VZ
859 wxUint32 addr;
860 if ( !GetImpl().GetHostAddress(&addr) )
861 return wxString();
8575ff50 862
c9bccf23
VZ
863 return wxString::Format
864 (
865 "%lu.%lu.%lu.%lu",
866 (addr >> 24) & 0xff,
867 (addr >> 16) & 0xff,
868 (addr >> 8) & 0xff,
869 addr & 0xff
870 );
60edcf45
VZ
871}
872
c9bccf23 873bool wxIPV4address::BroadcastAddress()
be4bd463 874{
c9bccf23
VZ
875 return GetImpl().SetToBroadcastAddress();
876}
877
878#if wxUSE_IPV6
879
880// ---------------------------------------------------------------------------
881// wxIPV6address
882// ---------------------------------------------------------------------------
883
884void wxIPV6address::DoInitImpl()
885{
886 m_impl->CreateINET6();
887}
888
889bool wxIPV6address::Hostname(unsigned char addr[16])
890{
891 unsigned short wk[8];
892 for ( int i = 0; i < 8; ++i )
893 {
894 wk[i] = addr[2*i];
895 wk[i] <<= 8;
896 wk[i] |= addr[2*i+1];
897 }
898
899 return Hostname
900 (
901 wxString::Format
902 (
903 "%x:%x:%x:%x:%x:%x:%x:%x",
904 wk[0], wk[1], wk[2], wk[3], wk[4], wk[5], wk[6], wk[7]
905 )
906 );
907}
908
909bool wxIPV6address::IsLocalHost() const
910{
911 if ( Hostname() == "localhost" )
912 return true;
913
914 wxString addr = IPAddress();
915 return addr == wxT("::1") ||
916 addr == wxT("0:0:0:0:0:0:0:1") ||
917 addr == wxT("::ffff:127.0.0.1");
be4bd463
JS
918}
919
920wxString wxIPV6address::IPAddress() const
d775fa82 921{
c9bccf23
VZ
922 union
923 {
924 in6_addr addr6;
925 wxUint8 bytes[16];
926 } u;
927
928 if ( !GetImpl().GetHostAddress(&u.addr6) )
929 return wxString();
930
931 const wxUint8 * const addr = u.bytes;
8575ff50
VZ
932
933 wxUint16 words[8];
934 int i,
935 prefix_zero_count = 0;
936 for ( i = 0; i < 8; ++i )
937 {
938 words[i] = addr[i*2];
939 words[i] <<= 8;
940 words[i] |= addr[i*2+1];
941 if ( i == prefix_zero_count && words[i] == 0 )
942 ++prefix_zero_count;
943 }
944
945 wxString result;
946 if ( prefix_zero_count == 8 )
947 {
948 result = wxT( "::" );
949 }
950 else if ( prefix_zero_count == 6 && words[5] == 0xFFFF )
951 {
952 // IPv4 mapped
953 result.Printf("::ffff:%d.%d.%d.%d",
954 addr[12], addr[13], addr[14], addr[15]);
955 }
956 else // general case
957 {
958 result = ":";
959 for ( i = prefix_zero_count; i < 8; ++i )
960 {
961 result += wxString::Format(":%x", words[i]);
962 }
963 }
964
965 return result;
be4bd463
JS
966}
967
be4bd463 968#endif // wxUSE_IPV6
f4ada568 969
c9bccf23 970#ifdef wxHAS_UNIX_DOMAIN_SOCKETS
1539c2f5 971
a324a7bc
GL
972// ---------------------------------------------------------------------------
973// wxUNIXaddress
974// ---------------------------------------------------------------------------
f4ada568 975
c9bccf23 976wxSockAddressImpl& wxUNIXaddress::GetUNIX()
f4ada568 977{
c9bccf23
VZ
978 if ( m_impl->GetFamily() == wxSockAddressImpl::FAMILY_UNSPEC )
979 m_impl->CreateUnix();
f4ada568 980
c9bccf23 981 return *m_impl;
f4ada568
GL
982}
983
f4ada568
GL
984void wxUNIXaddress::Filename(const wxString& fname)
985{
c9bccf23 986 GetUNIX().SetPath(fname);
f4ada568
GL
987}
988
c9bccf23 989wxString wxUNIXaddress::Filename() const
f4ada568 990{
c9bccf23 991 return GetUNIX().GetPath();
f4ada568
GL
992}
993
c9bccf23 994#endif // wxHAS_UNIX_DOMAIN_SOCKETS
35a4dab7 995
c9bccf23 996#endif // wxUSE_SOCKETS