Allow handling EVT_UPDATE_UI for wxID_UNDO/REDO at wxDocument level.
[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 #if defined(__UNIX__) && !defined(__CYGWIN__)
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 #ifndef HAVE_GETHOSTBYNAME
86 #define HAVE_GETHOSTBYNAME
87 #endif
88 #ifndef HAVE_GETSERVBYNAME
89 #define HAVE_GETSERVBYNAME
90 #endif
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
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
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
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
132 #ifdef HAVE_FUNC_GETHOSTBYNAME_R_3
133 struct wxGethostBuf : hostent_data
134 {
135 wxGethostBuf()
136 {
137 memset(this, 0, sizeof(hostent_data));
138 }
139 };
140 #else
141 typedef char wxGethostBuf[1024];
142 #endif
143
144 #ifdef HAVE_FUNC_GETSERVBYNAME_R_4
145 struct wxGetservBuf : servent_data
146 {
147 wxGetservBuf()
148 {
149 memset(this, 0, sizeof(servent_data));
150 }
151 };
152 #else
153 typedef char wxGetservBuf[1024];
154 #endif
155
156 #if defined(wxHAS_MT_SAFE_GETBY_FUNCS) || !wxUSE_THREADS
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
176 namespace
177 {
178
179 #if defined(HAVE_GETHOSTBYNAME)
180 hostent *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)
192 {
193 *err = ENOMEM;
194 return NULL;
195 }
196 memcpy(buffer, h->h_name, len);
197 buffer[len] = '\0';
198 h->h_name = buffer;
199
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
264 hostent *wxGethostbyname_r(const char *hostname,
265 hostent *h,
266 wxGethostBuf buffer,
267 int size,
268 int *err)
269 {
270 hostent *he;
271 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_6)
272 gethostbyname_r(hostname, h, buffer, size, &he, err);
273 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_5)
274 he = gethostbyname_r(hostname, h, buffer, size, err);
275 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
276 wxUnusedVar(var);
277 *err = gethostbyname_r(hostname, h, &buffer);
278 he = h;
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
294 hostent *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)
308 wxUnusedVar(size);
309 *err = gethostbyaddr_r(addr_buf, buf_size, proto, h, &buffer);
310 he = h;
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
322
323 return he;
324 }
325
326 #if defined(HAVE_GETSERVBYNAME)
327 servent *deepCopyServent(servent *s,
328 servent *se,
329 char *buffer,
330 int size)
331 {
332 /* copy plain old structure */
333 memcpy(s, se, sizeof(servent));
334
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
390 servent *wxGetservbyname_r(const char *port,
391 const char *protocol,
392 servent *serv,
393 wxGetservBuf& buffer,
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)
402 wxUnusedVar(size);
403 if ( getservbyname_r(port, protocol, serv, &buffer) != 0 )
404 return NULL;
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;
415 }
416
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
431 wxString wxSockAddressImpl::GetHostName() const
432 {
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);
472 }
473
474 bool wxSockAddressImpl::SetPortName(const wxString& name, const char *protocol)
475 {
476 // test whether it's a number first
477 unsigned long port;
478 if ( name.ToULong(&port) )
479 {
480 if ( port > 65535 )
481 return false;
482 }
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);
498 }
499
500 // ----------------------------------------------------------------------------
501 // INET address family
502 // ----------------------------------------------------------------------------
503
504 void wxSockAddressImpl::CreateINET()
505 {
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;
511 }
512
513 bool wxSockAddressImpl::SetHostName4(const wxString& name)
514 {
515 sockaddr_in * const addr = GET(sockaddr_in);
516 if ( !addr )
517 return false;
518
519 const wxScopedCharBuffer namebuf(name.utf8_str());
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;
542 }
543
544 bool wxSockAddressImpl::GetHostAddress(wxUint32 *address) const
545 {
546 sockaddr_in * const addr = GET(sockaddr_in);
547 if ( !addr )
548 return false;
549
550 *address = ntohl(addr->sin_addr.s_addr);
551
552 return true;
553 }
554
555 bool wxSockAddressImpl::SetHostAddress(wxUint32 address)
556 {
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;
564 }
565
566 wxUint16 wxSockAddressImpl::GetPort4() const
567 {
568 sockaddr_in * const addr = GET(sockaddr_in);
569 if ( !addr )
570 return 0;
571
572 return ntohs(addr->sin_port);
573 }
574
575 bool wxSockAddressImpl::SetPort4(wxUint16 port)
576 {
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;
584 }
585
586 #if wxUSE_IPV6
587
588 // ----------------------------------------------------------------------------
589 // INET6 address family
590 // ----------------------------------------------------------------------------
591
592 void wxSockAddressImpl::CreateINET6()
593 {
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;
599 }
600
601 bool wxSockAddressImpl::SetHostName6(const wxString& hostname)
602 {
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;
627 }
628
629 bool wxSockAddressImpl::GetHostAddress(in6_addr *address) const
630 {
631 sockaddr_in6 * const addr = GET(sockaddr_in6);
632 if ( !addr )
633 return false;
634
635 *address = addr->sin6_addr;
636
637 return true;
638 }
639
640 bool wxSockAddressImpl::SetHostAddress(const in6_addr& address)
641 {
642 sockaddr_in6 * const addr = GET(sockaddr_in6);
643 if ( !addr )
644 return false;
645
646 addr->sin6_addr = address;
647
648 return true;
649 }
650
651 wxUint16 wxSockAddressImpl::GetPort6() const
652 {
653 sockaddr_in6 * const addr = GET(sockaddr_in6);
654 if ( !addr )
655 return 0;
656
657 return ntohs(addr->sin6_port);
658 }
659
660 bool wxSockAddressImpl::SetPort6(wxUint16 port)
661 {
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;
669 }
670
671 bool 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
690 void wxSockAddressImpl::CreateUnix()
691 {
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';
698 }
699
700 bool wxSockAddressImpl::SetPath(const wxString& path)
701 {
702 sockaddr_un * const addr = GET(sockaddr_un);
703 if ( !addr )
704 return false;
705
706 const wxScopedCharBuffer buf(path.utf8_str());
707 if ( strlen(buf) >= UNIX_PATH_MAX )
708 return false;
709
710 wxStrlcpy(addr->sun_path, buf, UNIX_PATH_MAX);
711
712 return true;
713 }
714
715 wxString wxSockAddressImpl::GetPath() const
716 {
717 sockaddr_un * const addr = GET(sockaddr_un);
718 if ( !addr )
719 return wxString();
720
721 return wxString::FromUTF8(addr->sun_path);
722 }
723
724 #endif // wxHAS_UNIX_DOMAIN_SOCKETS
725
726 #undef GET
727 #undef ALLOC
728
729 // ----------------------------------------------------------------------------
730 // wxSockAddress
731 // ----------------------------------------------------------------------------
732
733 const sockaddr *wxSockAddress::GetAddressData() const
734 {
735 return GetAddress().GetAddr();
736 }
737
738 int wxSockAddress::GetAddressDataLen() const
739 {
740 return GetAddress().GetLen();
741 }
742
743 void wxSockAddress::Init()
744 {
745 if ( wxIsMainThread() && !wxSocketBase::IsInitialized() )
746 {
747 // we must do it before using any socket functions
748 (void)wxSocketBase::Initialize();
749 }
750 }
751
752 wxSockAddress::wxSockAddress()
753 {
754 Init();
755
756 m_impl = new wxSockAddressImpl();
757 }
758
759 wxSockAddress::wxSockAddress(const wxSockAddress& other)
760 : wxObject()
761 {
762 Init();
763
764 m_impl = new wxSockAddressImpl(*other.m_impl);
765 }
766
767 wxSockAddress::~wxSockAddress()
768 {
769 delete m_impl;
770 }
771
772 void wxSockAddress::SetAddress(const wxSockAddressImpl& address)
773 {
774 if ( &address != m_impl )
775 {
776 delete m_impl;
777 m_impl = new wxSockAddressImpl(address);
778 }
779 }
780
781 wxSockAddress& wxSockAddress::operator=(const wxSockAddress& addr)
782 {
783 SetAddress(addr.GetAddress());
784
785 return *this;
786 }
787
788 void wxSockAddress::Clear()
789 {
790 m_impl->Clear();
791 }
792
793 // ----------------------------------------------------------------------------
794 // wxIPaddress
795 // ----------------------------------------------------------------------------
796
797 wxSockAddressImpl& wxIPaddress::GetImpl()
798 {
799 if ( m_impl->GetFamily() == wxSockAddressImpl::FAMILY_UNSPEC )
800 m_impl->CreateINET();
801
802 return *m_impl;
803 }
804
805 bool wxIPaddress::Hostname(const wxString& name)
806 {
807 wxCHECK_MSG( !name.empty(), false, "empty host name is invalid" );
808
809 m_origHostname = name;
810
811 return GetImpl().SetHostName(name);
812 }
813
814 bool wxIPaddress::Service(const wxString& name)
815 {
816 return GetImpl().SetPortName(name, "tcp");
817 }
818
819 bool wxIPaddress::Service(unsigned short port)
820 {
821 return GetImpl().SetPort(port);
822 }
823
824 bool wxIPaddress::LocalHost()
825 {
826 return Hostname("localhost");
827 }
828
829 wxString wxIPaddress::Hostname() const
830 {
831 return GetImpl().GetHostName();
832 }
833
834 unsigned short wxIPaddress::Service() const
835 {
836 return GetImpl().GetPort();
837 }
838
839 bool wxIPaddress::operator==(const wxIPaddress& addr) const
840 {
841 return Hostname().Cmp(addr.Hostname()) == 0 &&
842 Service() == addr.Service();
843 }
844
845 bool wxIPaddress::AnyAddress()
846 {
847 return GetImpl().SetToAnyAddress();
848 }
849
850 // ----------------------------------------------------------------------------
851 // wxIPV4address
852 // ----------------------------------------------------------------------------
853
854 void wxIPV4address::DoInitImpl()
855 {
856 m_impl->CreateINET();
857 }
858
859 bool 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
871 bool wxIPV4address::IsLocalHost() const
872 {
873 return Hostname() == "localhost" || IPAddress() == "127.0.0.1";
874 }
875
876 wxString wxIPV4address::IPAddress() const
877 {
878 wxUint32 addr;
879 if ( !GetImpl().GetHostAddress(&addr) )
880 return wxString();
881
882 return wxString::Format
883 (
884 "%u.%u.%u.%u",
885 (addr >> 24) & 0xff,
886 (addr >> 16) & 0xff,
887 (addr >> 8) & 0xff,
888 addr & 0xff
889 );
890 }
891
892 bool wxIPV4address::BroadcastAddress()
893 {
894 return GetImpl().SetToBroadcastAddress();
895 }
896
897 #if wxUSE_IPV6
898
899 // ---------------------------------------------------------------------------
900 // wxIPV6address
901 // ---------------------------------------------------------------------------
902
903 void wxIPV6address::DoInitImpl()
904 {
905 m_impl->CreateINET6();
906 }
907
908 bool 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
928 bool 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");
937 }
938
939 wxString wxIPV6address::IPAddress() const
940 {
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;
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;
985 }
986
987 #endif // wxUSE_IPV6
988
989 #ifdef wxHAS_UNIX_DOMAIN_SOCKETS
990
991 // ---------------------------------------------------------------------------
992 // wxUNIXaddress
993 // ---------------------------------------------------------------------------
994
995 wxSockAddressImpl& wxUNIXaddress::GetUNIX()
996 {
997 if ( m_impl->GetFamily() == wxSockAddressImpl::FAMILY_UNSPEC )
998 m_impl->CreateUnix();
999
1000 return *m_impl;
1001 }
1002
1003 void wxUNIXaddress::Filename(const wxString& fname)
1004 {
1005 GetUNIX().SetPath(fname);
1006 }
1007
1008 wxString wxUNIXaddress::Filename() const
1009 {
1010 return GetUNIX().GetPath();
1011 }
1012
1013 #endif // wxHAS_UNIX_DOMAIN_SOCKETS
1014
1015 #endif // wxUSE_SOCKETS