]>
Commit | Line | Data |
---|---|---|
f4ada568 | 1 | ///////////////////////////////////////////////////////////////////////////// |
8898456d | 2 | // Name: src/common/sckaddr.cpp |
f4ada568 GL |
3 | // Purpose: Network address manager |
4 | // Author: Guilhem Lavaux | |
f4ada568 | 5 | // Created: 26/04/97 |
c9bccf23 | 6 | // Modified by: Vadim Zeitlin to use wxSockAddressImpl on 2008-12-28 |
f4ada568 GL |
7 | // RCS-ID: $Id$ |
8 | // Copyright: (c) 1997, 1998 Guilhem Lavaux | |
c9bccf23 | 9 | // (c) 2008 Vadim Zeitlin |
65571936 | 10 | // Licence: wxWindows licence |
f4ada568 GL |
11 | ///////////////////////////////////////////////////////////////////////////// |
12 | ||
c9bccf23 VZ |
13 | // ============================================================================ |
14 | // declarations | |
15 | // ============================================================================ | |
16 | ||
17 | // ---------------------------------------------------------------------------- | |
18 | // headers | |
19 | // ---------------------------------------------------------------------------- | |
20 | ||
fcc6dddd JS |
21 | // For compilers that support precompilation, includes "wx.h". |
22 | #include "wx/wxprec.h" | |
23 | ||
24 | #ifdef __BORLANDC__ | |
8898456d | 25 | #pragma hdrstop |
fcc6dddd JS |
26 | #endif |
27 | ||
35a4dab7 GL |
28 | #if wxUSE_SOCKETS |
29 | ||
6c0d0845 | 30 | #ifndef WX_PRECOMP |
6c0d0845 VZ |
31 | #include "wx/object.h" |
32 | #include "wx/log.h" | |
33 | #include "wx/intl.h" | |
ce3ed50d | 34 | |
6c0d0845 VZ |
35 | #include <stdio.h> |
36 | #include <stdlib.h> | |
37 | #include <ctype.h> | |
38 | ||
f172cb82 | 39 | #if !defined(__MWERKS__) |
6c0d0845 VZ |
40 | #include <memory.h> |
41 | #endif | |
42 | #endif // !WX_PRECOMP | |
f4ada568 | 43 | |
6c0d0845 | 44 | #include "wx/socket.h" |
3096bd2f | 45 | #include "wx/sckaddr.h" |
60913641 | 46 | #include "wx/private/socket.h" |
c9bccf23 VZ |
47 | #include "wx/private/sckaddr.h" |
48 | ||
169f96cd VZ |
49 | #include <errno.h> |
50 | ||
c9bccf23 VZ |
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 | // ---------------------------------------------------------------------------- | |
f4ada568 | 63 | |
f4ada568 | 64 | IMPLEMENT_ABSTRACT_CLASS(wxSockAddress, wxObject) |
be4bd463 JS |
65 | IMPLEMENT_ABSTRACT_CLASS(wxIPaddress, wxSockAddress) |
66 | IMPLEMENT_DYNAMIC_CLASS(wxIPV4address, wxIPaddress) | |
67 | #if wxUSE_IPV6 | |
68 | IMPLEMENT_DYNAMIC_CLASS(wxIPV6address, wxIPaddress) | |
f4ada568 | 69 | #endif |
c9bccf23 | 70 | #if defined(__UNIX__) && !defined(__WINDOWS__) && !defined(__WINE__) |
f4ada568 GL |
71 | IMPLEMENT_DYNAMIC_CLASS(wxUNIXaddress, wxSockAddress) |
72 | #endif | |
f4ada568 | 73 | |
c9bccf23 VZ |
74 | // ============================================================================ |
75 | // implementation of thread-safe/reentrant functions if they're missing | |
76 | // ============================================================================ | |
a324a7bc | 77 | |
c9bccf23 | 78 | // TODO: use POSIX getaddrinfo() (also available in Winsock 2) for simplicity |
67badd57 | 79 | // and to use the same code for IPv4 and IPv6 support |
c9bccf23 VZ |
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 | |
67badd57 VZ |
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 | |
c9bccf23 VZ |
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 | |
ec5756d6 VZ |
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 | |
c9bccf23 | 127 | #ifdef HAVE_FUNC_GETHOSTBYNAME_R_3 |
ec5756d6 VZ |
128 | struct wxGethostBuf : hostent_data |
129 | { | |
130 | wxGethostBuf() | |
131 | { | |
132 | memset(this, 0, sizeof(hostent_data)); | |
133 | } | |
134 | }; | |
c9bccf23 VZ |
135 | #else |
136 | typedef char wxGethostBuf[1024]; | |
137 | #endif | |
138 | ||
58cccb3b | 139 | #ifdef HAVE_FUNC_GETSERVBYNAME_R_4 |
ec5756d6 VZ |
140 | struct wxGetservBuf : servent_data |
141 | { | |
142 | wxGethostBuf() | |
143 | { | |
144 | memset(this, 0, sizeof(servent_data)); | |
145 | } | |
146 | }; | |
c9bccf23 VZ |
147 | #else |
148 | typedef char wxGetservBuf[1024]; | |
149 | #endif | |
150 | ||
f6a25e08 | 151 | #ifdef wxHAS_MT_SAFE_GETBY_FUNCS |
c9bccf23 VZ |
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 | |
6c0d0845 | 172 | { |
c9bccf23 VZ |
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) | |
6c0d0845 | 187 | { |
c9bccf23 VZ |
188 | *err = ENOMEM; |
189 | return NULL; | |
6c0d0845 | 190 | } |
c9bccf23 VZ |
191 | memcpy(buffer, h->h_name, len); |
192 | buffer[len] = '\0'; | |
193 | h->h_name = buffer; | |
6c0d0845 | 194 | |
c9bccf23 VZ |
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 | |
6c0d0845 | 315 | |
c9bccf23 | 316 | return he; |
f4ada568 GL |
317 | } |
318 | ||
c9bccf23 VZ |
319 | #if defined(HAVE_GETSERVBYNAME) |
320 | servent *deepCopyServent(servent *s, | |
321 | servent *se, | |
322 | char *buffer, | |
323 | int size) | |
1539c2f5 | 324 | { |
c9bccf23 VZ |
325 | /* copy plain old structure */ |
326 | memcpy(s, se, sizeof(servent)); | |
6c0d0845 | 327 | |
c9bccf23 VZ |
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, | |
58cccb3b | 386 | wxGetservBuf& buffer, |
c9bccf23 VZ |
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) | |
f6a25e08 VZ |
395 | if ( getservbyname_r(port, protocol, serv, &buffer) != 0 ) |
396 | return NULL; | |
c9bccf23 VZ |
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; | |
1539c2f5 VZ |
407 | } |
408 | ||
c9bccf23 VZ |
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 | |
f4ada568 | 424 | { |
c9bccf23 VZ |
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); | |
f4ada568 GL |
464 | } |
465 | ||
c9bccf23 | 466 | bool wxSockAddressImpl::SetPortName(const wxString& name, const char *protocol) |
f4ada568 | 467 | { |
c9bccf23 VZ |
468 | // test whether it's a number first |
469 | unsigned long port; | |
470 | if ( name.ToULong(&port) ) | |
fccb65a2 | 471 | { |
c9bccf23 VZ |
472 | if ( port > 65535 ) |
473 | return false; | |
fccb65a2 | 474 | } |
c9bccf23 VZ |
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); | |
f4ada568 GL |
490 | } |
491 | ||
c9bccf23 VZ |
492 | // ---------------------------------------------------------------------------- |
493 | // INET address family | |
494 | // ---------------------------------------------------------------------------- | |
495 | ||
496 | void wxSockAddressImpl::CreateINET() | |
f4ada568 | 497 | { |
c9bccf23 VZ |
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; | |
f4ada568 GL |
503 | } |
504 | ||
c9bccf23 | 505 | bool wxSockAddressImpl::SetHostName4(const wxString& name) |
acd15a3f | 506 | { |
c9bccf23 VZ |
507 | sockaddr_in * const addr = GET(sockaddr_in); |
508 | if ( !addr ) | |
509 | return false; | |
510 | ||
511 | const wxUTF8Buf 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; | |
f4ada568 | 534 | } |
f4ada568 | 535 | |
c9bccf23 VZ |
536 | bool wxSockAddressImpl::GetHostAddress(wxUint32 *address) const |
537 | { | |
538 | sockaddr_in * const addr = GET(sockaddr_in); | |
539 | if ( !addr ) | |
540 | return false; | |
be4bd463 | 541 | |
c9bccf23 VZ |
542 | *address = ntohl(addr->sin_addr.s_addr); |
543 | ||
544 | return true; | |
545 | } | |
546 | ||
547 | bool wxSockAddressImpl::SetHostAddress(wxUint32 address) | |
be4bd463 | 548 | { |
c9bccf23 VZ |
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; | |
be4bd463 JS |
556 | } |
557 | ||
c9bccf23 | 558 | wxUint16 wxSockAddressImpl::GetPort4() const |
be4bd463 | 559 | { |
c9bccf23 VZ |
560 | sockaddr_in * const addr = GET(sockaddr_in); |
561 | if ( !addr ) | |
562 | return 0; | |
563 | ||
564 | return ntohs(addr->sin_port); | |
be4bd463 JS |
565 | } |
566 | ||
c9bccf23 | 567 | bool wxSockAddressImpl::SetPort4(wxUint16 port) |
be4bd463 | 568 | { |
c9bccf23 VZ |
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; | |
be4bd463 JS |
576 | } |
577 | ||
c9bccf23 VZ |
578 | #if wxUSE_IPV6 |
579 | ||
580 | // ---------------------------------------------------------------------------- | |
581 | // INET6 address family | |
582 | // ---------------------------------------------------------------------------- | |
a324a7bc | 583 | |
c9bccf23 | 584 | void wxSockAddressImpl::CreateINET6() |
1539c2f5 | 585 | { |
c9bccf23 VZ |
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; | |
1539c2f5 VZ |
591 | } |
592 | ||
c9bccf23 | 593 | bool wxSockAddressImpl::SetHostName6(const wxString& hostname) |
f4ada568 | 594 | { |
c9bccf23 VZ |
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; | |
a324a7bc | 619 | } |
f4ada568 | 620 | |
c9bccf23 | 621 | bool wxSockAddressImpl::GetHostAddress(in6_addr *address) const |
a324a7bc | 622 | { |
c9bccf23 VZ |
623 | sockaddr_in6 * const addr = GET(sockaddr_in6); |
624 | if ( !addr ) | |
625 | return false; | |
626 | ||
627 | *address = addr->sin6_addr; | |
628 | ||
629 | return true; | |
a324a7bc | 630 | } |
f4ada568 | 631 | |
c9bccf23 | 632 | bool wxSockAddressImpl::SetHostAddress(const in6_addr& address) |
a324a7bc | 633 | { |
c9bccf23 VZ |
634 | sockaddr_in6 * const addr = GET(sockaddr_in6); |
635 | if ( !addr ) | |
636 | return false; | |
637 | ||
638 | addr->sin6_addr = address; | |
639 | ||
640 | return true; | |
f4ada568 GL |
641 | } |
642 | ||
c9bccf23 | 643 | wxUint16 wxSockAddressImpl::GetPort6() const |
f4ada568 | 644 | { |
c9bccf23 VZ |
645 | sockaddr_in6 * const addr = GET(sockaddr_in6); |
646 | if ( !addr ) | |
647 | return 0; | |
648 | ||
649 | return ntohs(addr->sin6_port); | |
f4ada568 GL |
650 | } |
651 | ||
c9bccf23 | 652 | bool wxSockAddressImpl::SetPort6(wxUint16 port) |
f4ada568 | 653 | { |
c9bccf23 VZ |
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; | |
f4ada568 GL |
661 | } |
662 | ||
c9bccf23 VZ |
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() | |
f4ada568 | 683 | { |
c9bccf23 VZ |
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'; | |
f4ada568 GL |
690 | } |
691 | ||
c9bccf23 | 692 | bool wxSockAddressImpl::SetPath(const wxString& path) |
f4ada568 | 693 | { |
c9bccf23 VZ |
694 | sockaddr_un * const addr = GET(sockaddr_un); |
695 | if ( !addr ) | |
696 | return false; | |
697 | ||
698 | const wxUTF8Buf 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; | |
f4ada568 GL |
705 | } |
706 | ||
c9bccf23 | 707 | wxString wxSockAddressImpl::GetPath() const |
be4bd463 | 708 | { |
c9bccf23 VZ |
709 | sockaddr_un * const addr = GET(sockaddr_un); |
710 | if ( !addr ) | |
711 | return wxString(); | |
712 | ||
713 | return wxString::FromUTF8(addr->sun_path); | |
be4bd463 JS |
714 | } |
715 | ||
c9bccf23 VZ |
716 | #endif // wxHAS_UNIX_DOMAIN_SOCKETS |
717 | ||
718 | #undef GET | |
719 | #undef ALLOC | |
720 | ||
721 | // ---------------------------------------------------------------------------- | |
722 | // wxSockAddress | |
723 | // ---------------------------------------------------------------------------- | |
724 | ||
725 | void wxSockAddress::Init() | |
60edcf45 | 726 | { |
c9bccf23 VZ |
727 | if ( !wxSocketBase::IsInitialized() ) |
728 | { | |
729 | // we must do it before using any socket functions | |
730 | (void)wxSocketBase::Initialize(); | |
731 | } | |
60edcf45 VZ |
732 | } |
733 | ||
c9bccf23 | 734 | wxSockAddress::wxSockAddress() |
d3ea6527 | 735 | { |
c9bccf23 VZ |
736 | Init(); |
737 | ||
738 | m_impl = new wxSockAddressImpl(); | |
d3ea6527 GRG |
739 | } |
740 | ||
c9bccf23 VZ |
741 | wxSockAddress::wxSockAddress(const wxSockAddress& other) |
742 | : wxObject() | |
f4ada568 | 743 | { |
c9bccf23 | 744 | Init(); |
f4ada568 | 745 | |
c9bccf23 | 746 | m_impl = new wxSockAddressImpl(*other.m_impl); |
f4ada568 GL |
747 | } |
748 | ||
c9bccf23 | 749 | wxSockAddress::~wxSockAddress() |
f4ada568 | 750 | { |
c9bccf23 | 751 | delete m_impl; |
f4ada568 GL |
752 | } |
753 | ||
c9bccf23 | 754 | void wxSockAddress::SetAddress(const wxSockAddressImpl& address) |
ce22d615 | 755 | { |
c9bccf23 VZ |
756 | if ( &address != m_impl ) |
757 | { | |
758 | delete m_impl; | |
759 | m_impl = new wxSockAddressImpl(address); | |
760 | } | |
ce22d615 RD |
761 | } |
762 | ||
c9bccf23 | 763 | wxSockAddress& wxSockAddress::operator=(const wxSockAddress& addr) |
d775fa82 | 764 | { |
c9bccf23 VZ |
765 | SetAddress(addr.GetAddress()); |
766 | ||
767 | return *this; | |
d9552598 VZ |
768 | } |
769 | ||
c9bccf23 | 770 | void wxSockAddress::Clear() |
1d6d8b5d | 771 | { |
c9bccf23 | 772 | m_impl->Clear(); |
1d6d8b5d JS |
773 | } |
774 | ||
c9bccf23 VZ |
775 | // ---------------------------------------------------------------------------- |
776 | // wxIPaddress | |
777 | // ---------------------------------------------------------------------------- | |
f4ada568 | 778 | |
c9bccf23 | 779 | wxSockAddressImpl& wxIPaddress::GetImpl() |
f4ada568 | 780 | { |
c9bccf23 VZ |
781 | if ( m_impl->GetFamily() == wxSockAddressImpl::FAMILY_UNSPEC ) |
782 | m_impl->CreateINET(); | |
783 | ||
784 | return *m_impl; | |
f4ada568 GL |
785 | } |
786 | ||
c9bccf23 | 787 | bool wxIPaddress::Hostname(const wxString& name) |
1539c2f5 | 788 | { |
c9bccf23 VZ |
789 | wxCHECK_MSG( !name.empty(), false, "empty host name is invalid" ); |
790 | ||
791 | m_origHostname = name; | |
792 | ||
793 | return GetImpl().SetHostName(name); | |
1539c2f5 VZ |
794 | } |
795 | ||
c9bccf23 | 796 | bool wxIPaddress::Service(const wxString& name) |
f4ada568 | 797 | { |
c9bccf23 | 798 | return GetImpl().SetPortName(name, "tcp"); |
f4ada568 GL |
799 | } |
800 | ||
c9bccf23 | 801 | bool wxIPaddress::Service(unsigned short port) |
f4ada568 | 802 | { |
c9bccf23 | 803 | return GetImpl().SetPort(port); |
f4ada568 GL |
804 | } |
805 | ||
c9bccf23 | 806 | bool wxIPaddress::LocalHost() |
f4ada568 | 807 | { |
c9bccf23 | 808 | return Hostname("localhost"); |
f4ada568 GL |
809 | } |
810 | ||
c9bccf23 | 811 | wxString wxIPaddress::Hostname() const |
f4ada568 | 812 | { |
c9bccf23 | 813 | return GetImpl().GetHostName(); |
f4ada568 GL |
814 | } |
815 | ||
c9bccf23 | 816 | unsigned short wxIPaddress::Service() const |
f4ada568 | 817 | { |
c9bccf23 | 818 | return GetImpl().GetPort(); |
f4ada568 GL |
819 | } |
820 | ||
c9bccf23 | 821 | bool wxIPaddress::operator==(const wxIPaddress& addr) const |
f4ada568 | 822 | { |
c9bccf23 VZ |
823 | return Hostname().Cmp(addr.Hostname()) == 0 && |
824 | Service() == addr.Service(); | |
f4ada568 GL |
825 | } |
826 | ||
c9bccf23 VZ |
827 | bool wxIPaddress::AnyAddress() |
828 | { | |
829 | return GetImpl().SetToAnyAddress(); | |
830 | } | |
831 | ||
832 | // ---------------------------------------------------------------------------- | |
833 | // wxIPV4address | |
834 | // ---------------------------------------------------------------------------- | |
835 | ||
836 | void wxIPV4address::DoInitImpl() | |
be4bd463 | 837 | { |
c9bccf23 VZ |
838 | m_impl->CreateINET(); |
839 | } | |
8575ff50 | 840 | |
c9bccf23 VZ |
841 | bool wxIPV4address::Hostname(unsigned long addr) |
842 | { | |
843 | if ( !GetImpl().SetHostAddress(addr) ) | |
844 | { | |
845 | m_origHostname.clear(); | |
846 | return false; | |
847 | } | |
848 | ||
849 | m_origHostname = Hostname(); | |
850 | return true; | |
851 | } | |
852 | ||
853 | bool wxIPV4address::IsLocalHost() const | |
854 | { | |
855 | return Hostname() == "localhost" || IPAddress() == "127.0.0.1"; | |
be4bd463 JS |
856 | } |
857 | ||
c9bccf23 | 858 | wxString wxIPV4address::IPAddress() const |
60edcf45 | 859 | { |
c9bccf23 VZ |
860 | wxUint32 addr; |
861 | if ( !GetImpl().GetHostAddress(&addr) ) | |
862 | return wxString(); | |
8575ff50 | 863 | |
c9bccf23 VZ |
864 | return wxString::Format |
865 | ( | |
866 | "%lu.%lu.%lu.%lu", | |
867 | (addr >> 24) & 0xff, | |
868 | (addr >> 16) & 0xff, | |
869 | (addr >> 8) & 0xff, | |
870 | addr & 0xff | |
871 | ); | |
60edcf45 VZ |
872 | } |
873 | ||
c9bccf23 | 874 | bool wxIPV4address::BroadcastAddress() |
be4bd463 | 875 | { |
c9bccf23 VZ |
876 | return GetImpl().SetToBroadcastAddress(); |
877 | } | |
878 | ||
879 | #if wxUSE_IPV6 | |
880 | ||
881 | // --------------------------------------------------------------------------- | |
882 | // wxIPV6address | |
883 | // --------------------------------------------------------------------------- | |
884 | ||
885 | void wxIPV6address::DoInitImpl() | |
886 | { | |
887 | m_impl->CreateINET6(); | |
888 | } | |
889 | ||
890 | bool wxIPV6address::Hostname(unsigned char addr[16]) | |
891 | { | |
892 | unsigned short wk[8]; | |
893 | for ( int i = 0; i < 8; ++i ) | |
894 | { | |
895 | wk[i] = addr[2*i]; | |
896 | wk[i] <<= 8; | |
897 | wk[i] |= addr[2*i+1]; | |
898 | } | |
899 | ||
900 | return Hostname | |
901 | ( | |
902 | wxString::Format | |
903 | ( | |
904 | "%x:%x:%x:%x:%x:%x:%x:%x", | |
905 | wk[0], wk[1], wk[2], wk[3], wk[4], wk[5], wk[6], wk[7] | |
906 | ) | |
907 | ); | |
908 | } | |
909 | ||
910 | bool wxIPV6address::IsLocalHost() const | |
911 | { | |
912 | if ( Hostname() == "localhost" ) | |
913 | return true; | |
914 | ||
915 | wxString addr = IPAddress(); | |
916 | return addr == wxT("::1") || | |
917 | addr == wxT("0:0:0:0:0:0:0:1") || | |
918 | addr == wxT("::ffff:127.0.0.1"); | |
be4bd463 JS |
919 | } |
920 | ||
921 | wxString wxIPV6address::IPAddress() const | |
d775fa82 | 922 | { |
c9bccf23 VZ |
923 | union |
924 | { | |
925 | in6_addr addr6; | |
926 | wxUint8 bytes[16]; | |
927 | } u; | |
928 | ||
929 | if ( !GetImpl().GetHostAddress(&u.addr6) ) | |
930 | return wxString(); | |
931 | ||
932 | const wxUint8 * const addr = u.bytes; | |
8575ff50 VZ |
933 | |
934 | wxUint16 words[8]; | |
935 | int i, | |
936 | prefix_zero_count = 0; | |
937 | for ( i = 0; i < 8; ++i ) | |
938 | { | |
939 | words[i] = addr[i*2]; | |
940 | words[i] <<= 8; | |
941 | words[i] |= addr[i*2+1]; | |
942 | if ( i == prefix_zero_count && words[i] == 0 ) | |
943 | ++prefix_zero_count; | |
944 | } | |
945 | ||
946 | wxString result; | |
947 | if ( prefix_zero_count == 8 ) | |
948 | { | |
949 | result = wxT( "::" ); | |
950 | } | |
951 | else if ( prefix_zero_count == 6 && words[5] == 0xFFFF ) | |
952 | { | |
953 | // IPv4 mapped | |
954 | result.Printf("::ffff:%d.%d.%d.%d", | |
955 | addr[12], addr[13], addr[14], addr[15]); | |
956 | } | |
957 | else // general case | |
958 | { | |
959 | result = ":"; | |
960 | for ( i = prefix_zero_count; i < 8; ++i ) | |
961 | { | |
962 | result += wxString::Format(":%x", words[i]); | |
963 | } | |
964 | } | |
965 | ||
966 | return result; | |
be4bd463 JS |
967 | } |
968 | ||
be4bd463 | 969 | #endif // wxUSE_IPV6 |
f4ada568 | 970 | |
c9bccf23 | 971 | #ifdef wxHAS_UNIX_DOMAIN_SOCKETS |
1539c2f5 | 972 | |
a324a7bc GL |
973 | // --------------------------------------------------------------------------- |
974 | // wxUNIXaddress | |
975 | // --------------------------------------------------------------------------- | |
f4ada568 | 976 | |
c9bccf23 | 977 | wxSockAddressImpl& wxUNIXaddress::GetUNIX() |
f4ada568 | 978 | { |
c9bccf23 VZ |
979 | if ( m_impl->GetFamily() == wxSockAddressImpl::FAMILY_UNSPEC ) |
980 | m_impl->CreateUnix(); | |
f4ada568 | 981 | |
c9bccf23 | 982 | return *m_impl; |
f4ada568 GL |
983 | } |
984 | ||
f4ada568 GL |
985 | void wxUNIXaddress::Filename(const wxString& fname) |
986 | { | |
c9bccf23 | 987 | GetUNIX().SetPath(fname); |
f4ada568 GL |
988 | } |
989 | ||
c9bccf23 | 990 | wxString wxUNIXaddress::Filename() const |
f4ada568 | 991 | { |
c9bccf23 | 992 | return GetUNIX().GetPath(); |
f4ada568 GL |
993 | } |
994 | ||
c9bccf23 | 995 | #endif // wxHAS_UNIX_DOMAIN_SOCKETS |
35a4dab7 | 996 | |
c9bccf23 | 997 | #endif // wxUSE_SOCKETS |