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