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