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