]> git.saurik.com Git - wxWidgets.git/blame - src/common/sckaddr.cpp
Applied #13738: Make a wxRichTextImage's box display around the image (dghart)
[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 {
5fe43b60 147 wxGetservBuf()
ec5756d6
VZ
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)
898c3e88 276 wxUnusedVar(var);
6677f122
VZ
277 *err = gethostbyname_r(hostname, h, &buffer);
278 he = h;
c9bccf23
VZ
279#elif defined(HAVE_GETHOSTBYNAME)
280 wxLOCK_GETBY_MUTEX(name);
281
282 he = gethostbyname(hostname);
283 *err = h_errno;
284
285 if ( he )
286 he = deepCopyHostent(h, he, buffer, size, err);
287#else
288 #error "No gethostbyname[_r]()"
289#endif
290
291 return he;
292}
293
294hostent *wxGethostbyaddr_r(const char *addr_buf,
295 int buf_size,
296 int proto,
297 hostent *h,
298 wxGethostBuf buffer,
299 int size,
300 int *err)
301{
302 hostent *he;
303#if defined(HAVE_FUNC_GETHOSTBYADDR_R_6)
304 gethostbyaddr_r(addr_buf, buf_size, proto, h, buffer, size, &he, err);
305#elif defined(HAVE_FUNC_GETHOSTBYADDR_R_5)
306 he = gethostbyaddr_r(addr_buf, buf_size, proto, h, buffer, size, err);
307#elif defined(HAVE_FUNC_GETHOSTBYADDR_R_3)
898c3e88 308 wxUnusedVar(size);
6677f122
VZ
309 *err = gethostbyaddr_r(addr_buf, buf_size, proto, h, &buffer);
310 he = h;
c9bccf23
VZ
311#elif defined(HAVE_GETHOSTBYADDR)
312 wxLOCK_GETBY_MUTEX(addr);
313
314 he = gethostbyaddr(addr_buf, buf_size, proto);
315 *err = h_errno;
316
317 if ( he )
318 he = deepCopyHostent(h, he, buffer, size, err);
319#else
320 #error "No gethostbyaddr[_r]()"
321#endif
6c0d0845 322
c9bccf23 323 return he;
f4ada568
GL
324}
325
c9bccf23
VZ
326#if defined(HAVE_GETSERVBYNAME)
327servent *deepCopyServent(servent *s,
328 servent *se,
329 char *buffer,
330 int size)
1539c2f5 331{
c9bccf23
VZ
332 /* copy plain old structure */
333 memcpy(s, se, sizeof(servent));
6c0d0845 334
c9bccf23
VZ
335 /* copy name */
336 int len = strlen(s->s_name);
337 if (len >= size)
338 {
339 return NULL;
340 }
341 memcpy(buffer, s->s_name, len);
342 buffer[len] = '\0';
343 s->s_name = buffer;
344
345 /* track position in the buffer */
346 int pos = len + 1;
347
348 /* copy protocol */
349 len = strlen(s->s_proto);
350 if (pos + len >= size)
351 {
352 return NULL;
353 }
354 memcpy(buffer + pos, s->s_proto, len);
355 buffer[pos + len] = '\0';
356 s->s_proto = buffer + pos;
357
358 /* track position in the buffer */
359 pos += len + 1;
360
361 /* ensure pointer alignment */
362 unsigned int misalign = sizeof(char *) - pos%sizeof(char *);
363 if(misalign < sizeof(char *))
364 pos += misalign;
365
366 /* leave space for pointer list */
367 char **p = s->s_aliases, **q;
368 char **s_aliases = (char **)(buffer + pos);
369 while(*(p++) != 0)
370 pos += sizeof(char *);
371
372 /* copy addresses and fill new pointer list */
373 for (p = s->s_aliases, q = s_aliases; *p != 0; p++, q++){
374 len = strlen(*p);
375 if (size <= pos + len)
376 {
377 return NULL;
378 }
379 memcpy(buffer + pos, *p, len); /* copy content */
380 buffer[pos + len] = '\0';
381 *q = buffer + pos; /* set copied pointer to copied content */
382 pos += len + 1;
383 }
384 *++q = 0; /* null terminate the pointer list */
385 s->s_aliases = s_aliases; /* copy pointer to pointers */
386 return s;
387}
388#endif // HAVE_GETSERVBYNAME
389
390servent *wxGetservbyname_r(const char *port,
391 const char *protocol,
392 servent *serv,
58cccb3b 393 wxGetservBuf& buffer,
c9bccf23
VZ
394 int size)
395{
396 servent *se;
397#if defined(HAVE_FUNC_GETSERVBYNAME_R_6)
398 getservbyname_r(port, protocol, serv, buffer, size, &se);
399#elif defined(HAVE_FUNC_GETSERVBYNAME_R_5)
400 se = getservbyname_r(port, protocol, serv, buffer, size);
401#elif defined(HAVE_FUNC_GETSERVBYNAME_R_4)
898c3e88 402 wxUnusedVar(size);
f6a25e08
VZ
403 if ( getservbyname_r(port, protocol, serv, &buffer) != 0 )
404 return NULL;
c9bccf23
VZ
405#elif defined(HAVE_GETSERVBYNAME)
406 wxLOCK_GETBY_MUTEX(serv);
407
408 se = getservbyname(port, protocol);
409 if ( se )
410 se = deepCopyServent(serv, se, buffer, size);
411#else
412 #error "No getservbyname[_r]()"
413#endif
414 return se;
1539c2f5
VZ
415}
416
c9bccf23
VZ
417} // anonymous namespace
418
419// ============================================================================
420// wxSockAddressImpl implementation
421// ============================================================================
422
423// FIXME-VC6: helper macros to call Alloc/Get() hiding the ugly dummy argument
424#define ALLOC(T) Alloc(static_cast<T *>(NULL))
425#define GET(T) Get(static_cast<T *>(NULL))
426
427// ----------------------------------------------------------------------------
428// INET or INET6 address family
429// ----------------------------------------------------------------------------
430
431wxString wxSockAddressImpl::GetHostName() const
f4ada568 432{
c9bccf23
VZ
433 const void *addrbuf;
434 int addrbuflen;
435
436#if wxUSE_IPV6
437 if ( m_family == FAMILY_INET6 )
438 {
439 sockaddr_in6 * const addr6 = GET(sockaddr_in6);
440 addrbuf = &addr6->sin6_addr;
441 addrbuflen = sizeof(addr6->sin6_addr);
442 }
443 else
444#endif // wxUSE_IPV6
445 {
446 sockaddr_in * const addr = GET(sockaddr_in);
447 if ( !addr )
448 return wxString();
449
450 addrbuf = &addr->sin_addr;
451 addrbuflen = sizeof(addr->sin_addr);
452 }
453
454 hostent he;
455 wxGethostBuf buffer;
456 int err;
457 if ( !wxGethostbyaddr_r
458 (
459 static_cast<const char *>(addrbuf),
460 addrbuflen,
461 m_family,
462 &he,
463 buffer,
464 sizeof(buffer),
465 &err
466 ) )
467 {
468 return wxString();
469 }
470
471 return wxString::FromUTF8(he.h_name);
f4ada568
GL
472}
473
c9bccf23 474bool wxSockAddressImpl::SetPortName(const wxString& name, const char *protocol)
f4ada568 475{
c9bccf23
VZ
476 // test whether it's a number first
477 unsigned long port;
478 if ( name.ToULong(&port) )
fccb65a2 479 {
c9bccf23
VZ
480 if ( port > 65535 )
481 return false;
fccb65a2 482 }
c9bccf23
VZ
483 else // it's a service name
484 {
485 wxGetservBuf buffer;
486 servent se;
487 if ( !wxGetservbyname_r(name.utf8_str(), protocol, &se,
488 buffer, sizeof(buffer)) )
489 return false;
490
491 // s_port is in network byte order and SetPort() uses the host byte
492 // order and we prefer to reuse it from here instead of assigning to
493 // sin_port directly
494 port = ntohs(se.s_port);
495 }
496
497 return SetPort(port);
f4ada568
GL
498}
499
c9bccf23
VZ
500// ----------------------------------------------------------------------------
501// INET address family
502// ----------------------------------------------------------------------------
503
504void wxSockAddressImpl::CreateINET()
f4ada568 505{
c9bccf23
VZ
506 wxASSERT_MSG( Is(FAMILY_UNSPEC), "recreating address as different type?" );
507
508 m_family = FAMILY_INET;
509 sockaddr_in * const addr = ALLOC(sockaddr_in);
510 addr->sin_family = FAMILY_INET;
f4ada568
GL
511}
512
c9bccf23 513bool wxSockAddressImpl::SetHostName4(const wxString& name)
acd15a3f 514{
c9bccf23
VZ
515 sockaddr_in * const addr = GET(sockaddr_in);
516 if ( !addr )
517 return false;
518
197380a0 519 const wxScopedCharBuffer namebuf(name.utf8_str());
c9bccf23
VZ
520
521 // first check if this is an address in quad dotted notation
522#if defined(HAVE_INET_ATON)
523 if ( inet_aton(namebuf, &addr->sin_addr) )
524 return true;
525#elif defined(HAVE_INET_ADDR)
526 addr->sin_addr.s_addr = inet_addr(namebuf);
527 if ( addr->sin_addr.s_addr != INADDR_NONE )
528 return true;
529#else
530 #error "Neither inet_aton() nor inet_addr() is available?"
531#endif
532
533 // it's a host name, resolve it
534 hostent he;
535 wxGethostBuf buffer;
536 int err;
537 if ( !wxGethostbyname_r(namebuf, &he, buffer, sizeof(buffer), &err) )
538 return false;
539
540 addr->sin_addr.s_addr = ((in_addr *)he.h_addr)->s_addr;
541 return true;
f4ada568 542}
f4ada568 543
c9bccf23
VZ
544bool wxSockAddressImpl::GetHostAddress(wxUint32 *address) const
545{
546 sockaddr_in * const addr = GET(sockaddr_in);
547 if ( !addr )
548 return false;
be4bd463 549
c9bccf23
VZ
550 *address = ntohl(addr->sin_addr.s_addr);
551
552 return true;
553}
554
555bool wxSockAddressImpl::SetHostAddress(wxUint32 address)
be4bd463 556{
c9bccf23
VZ
557 sockaddr_in * const addr = GET(sockaddr_in);
558 if ( !addr )
559 return false;
560
561 addr->sin_addr.s_addr = htonl(address);
562
563 return true;
be4bd463
JS
564}
565
c9bccf23 566wxUint16 wxSockAddressImpl::GetPort4() const
be4bd463 567{
c9bccf23
VZ
568 sockaddr_in * const addr = GET(sockaddr_in);
569 if ( !addr )
570 return 0;
571
572 return ntohs(addr->sin_port);
be4bd463
JS
573}
574
c9bccf23 575bool wxSockAddressImpl::SetPort4(wxUint16 port)
be4bd463 576{
c9bccf23
VZ
577 sockaddr_in * const addr = GET(sockaddr_in);
578 if ( !addr )
579 return false;
580
581 addr->sin_port = htons(port);
582
583 return true;
be4bd463
JS
584}
585
c9bccf23
VZ
586#if wxUSE_IPV6
587
588// ----------------------------------------------------------------------------
589// INET6 address family
590// ----------------------------------------------------------------------------
a324a7bc 591
c9bccf23 592void wxSockAddressImpl::CreateINET6()
1539c2f5 593{
c9bccf23
VZ
594 wxASSERT_MSG( Is(FAMILY_UNSPEC), "recreating address as different type?" );
595
596 m_family = FAMILY_INET6;
597 sockaddr_in6 * const addr = ALLOC(sockaddr_in6);
598 addr->sin6_family = FAMILY_INET6;
1539c2f5
VZ
599}
600
c9bccf23 601bool wxSockAddressImpl::SetHostName6(const wxString& hostname)
f4ada568 602{
c9bccf23
VZ
603 sockaddr_in6 * const addr = GET(sockaddr_in6);
604 if ( !addr )
605 return false;
606
607 addrinfo hints;
608 memset(&hints, 0, sizeof(hints));
609 hints.ai_family = AF_INET6;
610
611 addrinfo *info = NULL;
612 int rc = getaddrinfo(hostname.utf8_str(), NULL, &hints, &info);
613 if ( rc )
614 {
615 // use gai_strerror()?
616 return false;
617 }
618
619 wxCHECK_MSG( info, false, "should have info on success" );
620
621 wxASSERT_MSG( int(info->ai_addrlen) == m_len, "unexpected address length" );
622
623 memcpy(addr, info->ai_addr, info->ai_addrlen);
624 freeaddrinfo(info);
625
626 return true;
a324a7bc 627}
f4ada568 628
c9bccf23 629bool wxSockAddressImpl::GetHostAddress(in6_addr *address) const
a324a7bc 630{
c9bccf23
VZ
631 sockaddr_in6 * const addr = GET(sockaddr_in6);
632 if ( !addr )
633 return false;
634
635 *address = addr->sin6_addr;
636
637 return true;
a324a7bc 638}
f4ada568 639
c9bccf23 640bool wxSockAddressImpl::SetHostAddress(const in6_addr& address)
a324a7bc 641{
c9bccf23
VZ
642 sockaddr_in6 * const addr = GET(sockaddr_in6);
643 if ( !addr )
644 return false;
645
646 addr->sin6_addr = address;
647
648 return true;
f4ada568
GL
649}
650
c9bccf23 651wxUint16 wxSockAddressImpl::GetPort6() const
f4ada568 652{
c9bccf23
VZ
653 sockaddr_in6 * const addr = GET(sockaddr_in6);
654 if ( !addr )
655 return 0;
656
657 return ntohs(addr->sin6_port);
f4ada568
GL
658}
659
c9bccf23 660bool wxSockAddressImpl::SetPort6(wxUint16 port)
f4ada568 661{
c9bccf23
VZ
662 sockaddr_in6 * const addr = GET(sockaddr_in6);
663 if ( !addr )
664 return false;
665
666 addr->sin6_port = htons(port);
667
668 return true;
f4ada568
GL
669}
670
c9bccf23
VZ
671bool wxSockAddressImpl::SetToAnyAddress6()
672{
673 static const in6_addr any = IN6ADDR_ANY_INIT;
674
675 return SetHostAddress(any);
676}
677
678#endif // wxUSE_IPV6
679
680#ifdef wxHAS_UNIX_DOMAIN_SOCKETS
681
682// ----------------------------------------------------------------------------
683// Unix address family
684// ----------------------------------------------------------------------------
685
686#ifndef UNIX_PATH_MAX
687 #define UNIX_PATH_MAX (WXSIZEOF(((sockaddr_un *)NULL)->sun_path))
688#endif
689
690void wxSockAddressImpl::CreateUnix()
f4ada568 691{
c9bccf23
VZ
692 wxASSERT_MSG( Is(FAMILY_UNSPEC), "recreating address as different type?" );
693
694 m_family = FAMILY_UNIX;
695 sockaddr_un * const addr = ALLOC(sockaddr_un);
696 addr->sun_family = FAMILY_UNIX;
697 addr->sun_path[0] = '\0';
f4ada568
GL
698}
699
c9bccf23 700bool wxSockAddressImpl::SetPath(const wxString& path)
f4ada568 701{
c9bccf23
VZ
702 sockaddr_un * const addr = GET(sockaddr_un);
703 if ( !addr )
704 return false;
705
197380a0 706 const wxScopedCharBuffer buf(path.utf8_str());
c9bccf23
VZ
707 if ( strlen(buf) >= UNIX_PATH_MAX )
708 return false;
709
710 wxStrlcpy(addr->sun_path, buf, UNIX_PATH_MAX);
711
712 return true;
f4ada568
GL
713}
714
c9bccf23 715wxString wxSockAddressImpl::GetPath() const
be4bd463 716{
c9bccf23
VZ
717 sockaddr_un * const addr = GET(sockaddr_un);
718 if ( !addr )
719 return wxString();
720
721 return wxString::FromUTF8(addr->sun_path);
be4bd463
JS
722}
723
c9bccf23
VZ
724#endif // wxHAS_UNIX_DOMAIN_SOCKETS
725
726#undef GET
727#undef ALLOC
728
729// ----------------------------------------------------------------------------
730// wxSockAddress
731// ----------------------------------------------------------------------------
732
5fab0c8d
VZ
733const sockaddr *wxSockAddress::GetAddressData() const
734{
735 return GetAddress().GetAddr();
736}
737
738int wxSockAddress::GetAddressDataLen() const
739{
740 return GetAddress().GetLen();
741}
742
c9bccf23 743void wxSockAddress::Init()
60edcf45 744{
ed2e973e 745 if ( wxIsMainThread() && !wxSocketBase::IsInitialized() )
c9bccf23
VZ
746 {
747 // we must do it before using any socket functions
748 (void)wxSocketBase::Initialize();
749 }
60edcf45
VZ
750}
751
c9bccf23 752wxSockAddress::wxSockAddress()
d3ea6527 753{
c9bccf23
VZ
754 Init();
755
756 m_impl = new wxSockAddressImpl();
d3ea6527
GRG
757}
758
c9bccf23
VZ
759wxSockAddress::wxSockAddress(const wxSockAddress& other)
760 : wxObject()
f4ada568 761{
c9bccf23 762 Init();
f4ada568 763
c9bccf23 764 m_impl = new wxSockAddressImpl(*other.m_impl);
f4ada568
GL
765}
766
c9bccf23 767wxSockAddress::~wxSockAddress()
f4ada568 768{
c9bccf23 769 delete m_impl;
f4ada568
GL
770}
771
c9bccf23 772void wxSockAddress::SetAddress(const wxSockAddressImpl& address)
ce22d615 773{
c9bccf23
VZ
774 if ( &address != m_impl )
775 {
776 delete m_impl;
777 m_impl = new wxSockAddressImpl(address);
778 }
ce22d615
RD
779}
780
c9bccf23 781wxSockAddress& wxSockAddress::operator=(const wxSockAddress& addr)
d775fa82 782{
c9bccf23
VZ
783 SetAddress(addr.GetAddress());
784
785 return *this;
d9552598
VZ
786}
787
c9bccf23 788void wxSockAddress::Clear()
1d6d8b5d 789{
c9bccf23 790 m_impl->Clear();
1d6d8b5d
JS
791}
792
c9bccf23
VZ
793// ----------------------------------------------------------------------------
794// wxIPaddress
795// ----------------------------------------------------------------------------
f4ada568 796
c9bccf23 797wxSockAddressImpl& wxIPaddress::GetImpl()
f4ada568 798{
c9bccf23
VZ
799 if ( m_impl->GetFamily() == wxSockAddressImpl::FAMILY_UNSPEC )
800 m_impl->CreateINET();
801
802 return *m_impl;
f4ada568
GL
803}
804
c9bccf23 805bool wxIPaddress::Hostname(const wxString& name)
1539c2f5 806{
c9bccf23
VZ
807 wxCHECK_MSG( !name.empty(), false, "empty host name is invalid" );
808
809 m_origHostname = name;
810
811 return GetImpl().SetHostName(name);
1539c2f5
VZ
812}
813
c9bccf23 814bool wxIPaddress::Service(const wxString& name)
f4ada568 815{
c9bccf23 816 return GetImpl().SetPortName(name, "tcp");
f4ada568
GL
817}
818
c9bccf23 819bool wxIPaddress::Service(unsigned short port)
f4ada568 820{
c9bccf23 821 return GetImpl().SetPort(port);
f4ada568
GL
822}
823
c9bccf23 824bool wxIPaddress::LocalHost()
f4ada568 825{
c9bccf23 826 return Hostname("localhost");
f4ada568
GL
827}
828
c9bccf23 829wxString wxIPaddress::Hostname() const
f4ada568 830{
c9bccf23 831 return GetImpl().GetHostName();
f4ada568
GL
832}
833
c9bccf23 834unsigned short wxIPaddress::Service() const
f4ada568 835{
c9bccf23 836 return GetImpl().GetPort();
f4ada568
GL
837}
838
c9bccf23 839bool wxIPaddress::operator==(const wxIPaddress& addr) const
f4ada568 840{
c9bccf23
VZ
841 return Hostname().Cmp(addr.Hostname()) == 0 &&
842 Service() == addr.Service();
f4ada568
GL
843}
844
c9bccf23
VZ
845bool wxIPaddress::AnyAddress()
846{
847 return GetImpl().SetToAnyAddress();
848}
849
850// ----------------------------------------------------------------------------
851// wxIPV4address
852// ----------------------------------------------------------------------------
853
854void wxIPV4address::DoInitImpl()
be4bd463 855{
c9bccf23
VZ
856 m_impl->CreateINET();
857}
8575ff50 858
c9bccf23
VZ
859bool wxIPV4address::Hostname(unsigned long addr)
860{
861 if ( !GetImpl().SetHostAddress(addr) )
862 {
863 m_origHostname.clear();
864 return false;
865 }
866
867 m_origHostname = Hostname();
868 return true;
869}
870
871bool wxIPV4address::IsLocalHost() const
872{
873 return Hostname() == "localhost" || IPAddress() == "127.0.0.1";
be4bd463
JS
874}
875
c9bccf23 876wxString wxIPV4address::IPAddress() const
60edcf45 877{
c9bccf23
VZ
878 wxUint32 addr;
879 if ( !GetImpl().GetHostAddress(&addr) )
880 return wxString();
8575ff50 881
c9bccf23
VZ
882 return wxString::Format
883 (
6178debc 884 "%u.%u.%u.%u",
c9bccf23
VZ
885 (addr >> 24) & 0xff,
886 (addr >> 16) & 0xff,
887 (addr >> 8) & 0xff,
888 addr & 0xff
889 );
60edcf45
VZ
890}
891
c9bccf23 892bool wxIPV4address::BroadcastAddress()
be4bd463 893{
c9bccf23
VZ
894 return GetImpl().SetToBroadcastAddress();
895}
896
897#if wxUSE_IPV6
898
899// ---------------------------------------------------------------------------
900// wxIPV6address
901// ---------------------------------------------------------------------------
902
903void wxIPV6address::DoInitImpl()
904{
905 m_impl->CreateINET6();
906}
907
908bool wxIPV6address::Hostname(unsigned char addr[16])
909{
910 unsigned short wk[8];
911 for ( int i = 0; i < 8; ++i )
912 {
913 wk[i] = addr[2*i];
914 wk[i] <<= 8;
915 wk[i] |= addr[2*i+1];
916 }
917
918 return Hostname
919 (
920 wxString::Format
921 (
922 "%x:%x:%x:%x:%x:%x:%x:%x",
923 wk[0], wk[1], wk[2], wk[3], wk[4], wk[5], wk[6], wk[7]
924 )
925 );
926}
927
928bool wxIPV6address::IsLocalHost() const
929{
930 if ( Hostname() == "localhost" )
931 return true;
932
933 wxString addr = IPAddress();
934 return addr == wxT("::1") ||
935 addr == wxT("0:0:0:0:0:0:0:1") ||
936 addr == wxT("::ffff:127.0.0.1");
be4bd463
JS
937}
938
939wxString wxIPV6address::IPAddress() const
d775fa82 940{
c9bccf23
VZ
941 union
942 {
943 in6_addr addr6;
944 wxUint8 bytes[16];
945 } u;
946
947 if ( !GetImpl().GetHostAddress(&u.addr6) )
948 return wxString();
949
950 const wxUint8 * const addr = u.bytes;
8575ff50
VZ
951
952 wxUint16 words[8];
953 int i,
954 prefix_zero_count = 0;
955 for ( i = 0; i < 8; ++i )
956 {
957 words[i] = addr[i*2];
958 words[i] <<= 8;
959 words[i] |= addr[i*2+1];
960 if ( i == prefix_zero_count && words[i] == 0 )
961 ++prefix_zero_count;
962 }
963
964 wxString result;
965 if ( prefix_zero_count == 8 )
966 {
967 result = wxT( "::" );
968 }
969 else if ( prefix_zero_count == 6 && words[5] == 0xFFFF )
970 {
971 // IPv4 mapped
972 result.Printf("::ffff:%d.%d.%d.%d",
973 addr[12], addr[13], addr[14], addr[15]);
974 }
975 else // general case
976 {
977 result = ":";
978 for ( i = prefix_zero_count; i < 8; ++i )
979 {
980 result += wxString::Format(":%x", words[i]);
981 }
982 }
983
984 return result;
be4bd463
JS
985}
986
be4bd463 987#endif // wxUSE_IPV6
f4ada568 988
c9bccf23 989#ifdef wxHAS_UNIX_DOMAIN_SOCKETS
1539c2f5 990
a324a7bc
GL
991// ---------------------------------------------------------------------------
992// wxUNIXaddress
993// ---------------------------------------------------------------------------
f4ada568 994
c9bccf23 995wxSockAddressImpl& wxUNIXaddress::GetUNIX()
f4ada568 996{
c9bccf23
VZ
997 if ( m_impl->GetFamily() == wxSockAddressImpl::FAMILY_UNSPEC )
998 m_impl->CreateUnix();
f4ada568 999
c9bccf23 1000 return *m_impl;
f4ada568
GL
1001}
1002
f4ada568
GL
1003void wxUNIXaddress::Filename(const wxString& fname)
1004{
c9bccf23 1005 GetUNIX().SetPath(fname);
f4ada568
GL
1006}
1007
c9bccf23 1008wxString wxUNIXaddress::Filename() const
f4ada568 1009{
c9bccf23 1010 return GetUNIX().GetPath();
f4ada568
GL
1011}
1012
c9bccf23 1013#endif // wxHAS_UNIX_DOMAIN_SOCKETS
35a4dab7 1014
c9bccf23 1015#endif // wxUSE_SOCKETS