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