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