move Read/Write() to common code, there was almost nothing platform-specific in it
[wxWidgets.git] / src / unix / sockunix.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/unix/sockunix.cpp
3 // Purpose: wxSocketImpl implementation for Unix systems
4 // Authors: Guilhem Lavaux, Guillermo Rodriguez Garcia, David Elliott,
5 // Vadim Zeitlin
6 // Created: April 1997
7 // RCS-ID: $Id$
8 // Copyright: (c) 1997 Guilhem Lavaux
9 // (c) 2008 Vadim Zeitlin
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13
14 #include "wx/wxprec.h"
15
16 #if wxUSE_SOCKETS
17
18 #include "wx/private/fd.h"
19 #include "wx/private/socket.h"
20 #include "wx/unix/private/sockunix.h"
21
22 #if defined(__VISAGECPP__)
23 #define BSD_SELECT /* use Berkeley Sockets select */
24 #endif
25
26 #if defined(__WATCOMC__)
27 #include <errno.h>
28 #include <nerrno.h>
29 #endif
30
31 #include <assert.h>
32 #include <sys/types.h>
33 #ifdef __VISAGECPP__
34 #include <string.h>
35 #include <sys/time.h>
36 #include <types.h>
37 #include <netinet/in.h>
38 #endif
39 #include <netdb.h>
40 #include <sys/ioctl.h>
41
42 #ifdef HAVE_SYS_SELECT_H
43 # include <sys/select.h>
44 #endif
45
46 #ifdef __VMS__
47 #include <socket.h>
48 struct sockaddr_un
49 {
50 u_char sun_len; /* sockaddr len including null */
51 u_char sun_family; /* AF_UNIX */
52 char sun_path[108]; /* path name (gag) */
53 };
54 #else
55 #include <sys/socket.h>
56 #include <sys/un.h>
57 #endif
58
59 #ifndef __VISAGECPP__
60 #include <sys/time.h>
61 #include <netinet/in.h>
62 #include <arpa/inet.h>
63 #include <errno.h>
64 #include <string.h>
65 #include <unistd.h>
66 #else
67 #include <nerrno.h>
68 # if __IBMCPP__ < 400
69 #include <machine/endian.h>
70 #include <socket.h>
71 #include <ioctl.h>
72 #include <select.h>
73 #include <unistd.h>
74
75 #define EBADF SOCEBADF
76
77 # ifdef min
78 # undef min
79 # endif
80 # else
81 #include <sys/socket.h>
82 #include <sys/ioctl.h>
83 #include <sys/select.h>
84
85 #define close(a) soclose(a)
86 #define select(a,b,c,d,e) bsdselect(a,b,c,d,e)
87 int _System bsdselect(int,
88 struct fd_set *,
89 struct fd_set *,
90 struct fd_set *,
91 struct timeval *);
92 int _System soclose(int);
93 # endif
94 #endif
95 #ifdef __EMX__
96 #include <sys/select.h>
97 #endif
98
99 #include <stdio.h>
100 #include <stdlib.h>
101 #include <stddef.h>
102 #include <ctype.h>
103 #ifdef sun
104 # include <sys/filio.h>
105 #endif
106 #ifdef sgi
107 # include <bstring.h>
108 #endif
109 #ifdef _AIX
110 # include <strings.h>
111 #endif
112 #include <signal.h>
113
114 #ifndef WX_SOCKLEN_T
115
116 #ifdef VMS
117 # define WX_SOCKLEN_T unsigned int
118 #else
119 # ifdef __GLIBC__
120 # if __GLIBC__ == 2
121 # define WX_SOCKLEN_T socklen_t
122 # endif
123 # elif defined(__WXMAC__)
124 # define WX_SOCKLEN_T socklen_t
125 # else
126 # define WX_SOCKLEN_T int
127 # endif
128 #endif
129
130 #endif /* SOCKLEN_T */
131
132 #ifndef SOCKOPTLEN_T
133 #define SOCKOPTLEN_T WX_SOCKLEN_T
134 #endif
135
136 /* UnixWare reportedly needs this for FIONBIO definition */
137 #ifdef __UNIXWARE__
138 #include <sys/filio.h>
139 #endif
140
141 /*
142 * INADDR_BROADCAST is identical to INADDR_NONE which is not defined
143 * on all systems. INADDR_BROADCAST should be fine to indicate an error.
144 */
145 #ifndef INADDR_NONE
146 #define INADDR_NONE INADDR_BROADCAST
147 #endif
148
149 // ----------------------------------------------------------------------------
150 // implementation of thread-safe/reentrant functions if they're missing
151 // ----------------------------------------------------------------------------
152
153 #if wxUSE_THREADS && (defined(HAVE_GETHOSTBYNAME) || defined(HAVE_GETSERVBYNAME))
154 # include "wx/thread.h"
155 #endif
156
157 #if defined(HAVE_GETHOSTBYNAME)
158 static struct hostent * deepCopyHostent(struct hostent *h,
159 const struct hostent *he,
160 char *buffer, int size, int *err)
161 {
162 /* copy old structure */
163 memcpy(h, he, sizeof(struct hostent));
164
165 /* copy name */
166 int len = strlen(h->h_name);
167 if (len > size)
168 {
169 *err = ENOMEM;
170 return NULL;
171 }
172 memcpy(buffer, h->h_name, len);
173 buffer[len] = '\0';
174 h->h_name = buffer;
175
176 /* track position in the buffer */
177 int pos = len + 1;
178
179 /* reuse len to store address length */
180 len = h->h_length;
181
182 /* ensure pointer alignment */
183 unsigned int misalign = sizeof(char *) - pos%sizeof(char *);
184 if(misalign < sizeof(char *))
185 pos += misalign;
186
187 /* leave space for pointer list */
188 char **p = h->h_addr_list, **q;
189 char **h_addr_list = (char **)(buffer + pos);
190 while(*(p++) != 0)
191 pos += sizeof(char *);
192
193 /* copy addresses and fill new pointer list */
194 for (p = h->h_addr_list, q = h_addr_list; *p != 0; p++, q++)
195 {
196 if (size < pos + len)
197 {
198 *err = ENOMEM;
199 return NULL;
200 }
201 memcpy(buffer + pos, *p, len); /* copy content */
202 *q = buffer + pos; /* set copied pointer to copied content */
203 pos += len;
204 }
205 *++q = 0; /* null terminate the pointer list */
206 h->h_addr_list = h_addr_list; /* copy pointer to pointers */
207
208 /* ensure word alignment of pointers */
209 misalign = sizeof(char *) - pos%sizeof(char *);
210 if(misalign < sizeof(char *))
211 pos += misalign;
212
213 /* leave space for pointer list */
214 p = h->h_aliases;
215 char **h_aliases = (char **)(buffer + pos);
216 while(*(p++) != 0)
217 pos += sizeof(char *);
218
219 /* copy aliases and fill new pointer list */
220 for (p = h->h_aliases, q = h_aliases; *p != 0; p++, q++)
221 {
222 len = strlen(*p);
223 if (size <= pos + len)
224 {
225 *err = ENOMEM;
226 return NULL;
227 }
228 memcpy(buffer + pos, *p, len); /* copy content */
229 buffer[pos + len] = '\0';
230 *q = buffer + pos; /* set copied pointer to copied content */
231 pos += len + 1;
232 }
233 *++q = 0; /* null terminate the pointer list */
234 h->h_aliases = h_aliases; /* copy pointer to pointers */
235
236 return h;
237 }
238 #endif
239
240 #if defined(HAVE_GETHOSTBYNAME) && wxUSE_THREADS
241 static wxMutex nameLock;
242 #endif
243 struct hostent * wxGethostbyname_r(const char *hostname, struct hostent *h,
244 void *buffer, int size, int *err)
245
246 {
247 struct hostent *he = NULL;
248 *err = 0;
249 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_6)
250 if (gethostbyname_r(hostname, h, (char*)buffer, size, &he, err))
251 he = NULL;
252 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_5)
253 he = gethostbyname_r(hostname, h, (char*)buffer, size, err);
254 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
255 if (gethostbyname_r(hostname, h, (struct hostent_data*) buffer))
256 {
257 he = NULL;
258 *err = h_errno;
259 }
260 else
261 he = h;
262 #elif defined(HAVE_GETHOSTBYNAME)
263 #if wxUSE_THREADS
264 wxMutexLocker locker(nameLock);
265 #endif
266 he = gethostbyname(hostname);
267 if (!he)
268 *err = h_errno;
269 else
270 he = deepCopyHostent(h, he, (char*)buffer, size, err);
271 #endif
272 return he;
273 }
274
275 #if defined(HAVE_GETHOSTBYNAME) && wxUSE_THREADS
276 static wxMutex addrLock;
277 #endif
278 struct hostent * wxGethostbyaddr_r(const char *addr_buf, int buf_size,
279 int proto, struct hostent *h,
280 void *buffer, int size, int *err)
281 {
282 struct hostent *he = NULL;
283 *err = 0;
284 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_6)
285 if (gethostbyaddr_r(addr_buf, buf_size, proto, h,
286 (char*)buffer, size, &he, err))
287 he = NULL;
288 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_5)
289 he = gethostbyaddr_r(addr_buf, buf_size, proto, h, (char*)buffer, size, err);
290 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
291 if (gethostbyaddr_r(addr_buf, buf_size, proto, h,
292 (struct hostent_data*) buffer))
293 {
294 he = NULL;
295 *err = h_errno;
296 }
297 else
298 he = h;
299 #elif defined(HAVE_GETHOSTBYNAME)
300 #if wxUSE_THREADS
301 wxMutexLocker locker(addrLock);
302 #endif
303 he = gethostbyaddr(addr_buf, buf_size, proto);
304 if (!he)
305 *err = h_errno;
306 else
307 he = deepCopyHostent(h, he, (char*)buffer, size, err);
308 #endif
309 return he;
310 }
311
312 #if defined(HAVE_GETSERVBYNAME)
313 static struct servent * deepCopyServent(struct servent *s,
314 const struct servent *se,
315 char *buffer, int size)
316 {
317 /* copy plain old structure */
318 memcpy(s, se, sizeof(struct servent));
319
320 /* copy name */
321 int len = strlen(s->s_name);
322 if (len >= size)
323 {
324 return NULL;
325 }
326 memcpy(buffer, s->s_name, len);
327 buffer[len] = '\0';
328 s->s_name = buffer;
329
330 /* track position in the buffer */
331 int pos = len + 1;
332
333 /* copy protocol */
334 len = strlen(s->s_proto);
335 if (pos + len >= size)
336 {
337 return NULL;
338 }
339 memcpy(buffer + pos, s->s_proto, len);
340 buffer[pos + len] = '\0';
341 s->s_proto = buffer + pos;
342
343 /* track position in the buffer */
344 pos += len + 1;
345
346 /* ensure pointer alignment */
347 unsigned int misalign = sizeof(char *) - pos%sizeof(char *);
348 if(misalign < sizeof(char *))
349 pos += misalign;
350
351 /* leave space for pointer list */
352 char **p = s->s_aliases, **q;
353 char **s_aliases = (char **)(buffer + pos);
354 while(*(p++) != 0)
355 pos += sizeof(char *);
356
357 /* copy addresses and fill new pointer list */
358 for (p = s->s_aliases, q = s_aliases; *p != 0; p++, q++){
359 len = strlen(*p);
360 if (size <= pos + len)
361 {
362 return NULL;
363 }
364 memcpy(buffer + pos, *p, len); /* copy content */
365 buffer[pos + len] = '\0';
366 *q = buffer + pos; /* set copied pointer to copied content */
367 pos += len + 1;
368 }
369 *++q = 0; /* null terminate the pointer list */
370 s->s_aliases = s_aliases; /* copy pointer to pointers */
371 return s;
372 }
373 #endif
374
375 #if defined(HAVE_GETSERVBYNAME) && wxUSE_THREADS
376 static wxMutex servLock;
377 #endif
378 struct servent *wxGetservbyname_r(const char *port, const char *protocol,
379 struct servent *serv, void *buffer, int size)
380 {
381 struct servent *se = NULL;
382 #if defined(HAVE_FUNC_GETSERVBYNAME_R_6)
383 if (getservbyname_r(port, protocol, serv, (char*)buffer, size, &se))
384 se = NULL;
385 #elif defined(HAVE_FUNC_GETSERVBYNAME_R_5)
386 se = getservbyname_r(port, protocol, serv, (char*)buffer, size);
387 #elif defined(HAVE_FUNC_GETSERVBYNAME_R_4)
388 if (getservbyname_r(port, protocol, serv, (struct servent_data*) buffer))
389 se = NULL;
390 else
391 se = serv;
392 #elif defined(HAVE_GETSERVBYNAME)
393 #if wxUSE_THREADS
394 wxMutexLocker locker(servLock);
395 #endif
396 se = getservbyname(port, protocol);
397 if (se)
398 se = deepCopyServent(serv, se, (char*)buffer, size);
399 #endif
400 return se;
401 }
402
403 // ============================================================================
404 // wxSocketImpl implementation
405 // ============================================================================
406
407 /* static */
408 wxSocketImpl *wxSocketImpl::Create(wxSocketBase& wxsocket)
409 {
410 return new wxSocketImplUnix(wxsocket);
411 }
412
413
414 wxSocketError wxSocketImplUnix::GetLastError() const
415 {
416 switch ( errno )
417 {
418 case 0:
419 return wxSOCKET_NOERROR;
420
421 case ENOTSOCK:
422 return wxSOCKET_INVSOCK;
423
424 // unfortunately EAGAIN only has the "would block" meaning for read(),
425 // not for connect() for which it means something rather different but
426 // we can't distinguish between these two situations currently...
427 //
428 // also notice that EWOULDBLOCK can be different from EAGAIN on some
429 // systems (HP-UX being the only known example) while it's defined as
430 // EAGAIN on most others (e.g. Linux)
431 case EAGAIN:
432 #ifdef EWOULDBLOCK
433 #if EWOULDBLOCK != EAGAIN
434 case EWOULDBLOCK:
435 #endif
436 #endif // EWOULDBLOCK
437 case EINPROGRESS:
438 return wxSOCKET_WOULDBLOCK;
439
440 default:
441 return wxSOCKET_IOERR;
442 }
443 }
444
445 void wxSocketImplUnix::DoEnableEvents(bool flag)
446 {
447 wxSocketManager * const manager = wxSocketManager::Get();
448 if ( flag )
449 {
450 manager->Install_Callback(this, wxSOCKET_INPUT);
451 manager->Install_Callback(this, wxSOCKET_OUTPUT);
452 }
453 else // off
454 {
455 manager->Uninstall_Callback(this, wxSOCKET_INPUT);
456 manager->Uninstall_Callback(this, wxSOCKET_OUTPUT);
457 }
458 }
459
460
461 void wxSocketImplUnix::OnStateChange(wxSocketNotify event)
462 {
463 NotifyOnStateChange(event);
464
465 if ( event == wxSOCKET_LOST )
466 Shutdown();
467 }
468
469 void wxSocketImplUnix::OnReadWaiting()
470 {
471 char c;
472
473 if (m_fd == INVALID_SOCKET)
474 {
475 return;
476 }
477
478 int num = recv(m_fd, &c, 1, MSG_PEEK);
479
480 if (num > 0)
481 {
482 OnStateChange(wxSOCKET_INPUT);
483 }
484 else
485 {
486 if (m_server && m_stream)
487 {
488 OnStateChange(wxSOCKET_CONNECTION);
489 }
490 else if (num == 0)
491 {
492 if (m_stream)
493 {
494 /* graceful shutdown */
495 OnStateChange(wxSOCKET_LOST);
496 }
497 else
498 {
499 /* Empty datagram received */
500 OnStateChange(wxSOCKET_INPUT);
501 }
502 }
503 else
504 {
505 /* Do not throw a lost event in cases where the socket isn't really lost */
506 if ((errno == EWOULDBLOCK) || (errno == EAGAIN) || (errno == EINTR))
507 {
508 OnStateChange(wxSOCKET_INPUT);
509 }
510 else
511 {
512 OnStateChange(wxSOCKET_LOST);
513 }
514 }
515 }
516 }
517
518 void wxSocketImplUnix::OnWriteWaiting()
519 {
520 if (m_establishing && !m_server)
521 {
522 int error;
523 SOCKOPTLEN_T len = sizeof(error);
524
525 m_establishing = false;
526
527 getsockopt(m_fd, SOL_SOCKET, SO_ERROR, (char*)&error, &len);
528
529 if (error)
530 {
531 OnStateChange(wxSOCKET_LOST);
532 }
533 else
534 {
535 OnStateChange(wxSOCKET_CONNECTION);
536 /* We have to fire this event by hand because CONNECTION (for clients)
537 * and OUTPUT are internally the same and we just disabled CONNECTION
538 * events with the above macro.
539 */
540 OnStateChange(wxSOCKET_OUTPUT);
541 }
542 }
543 else
544 {
545 OnStateChange(wxSOCKET_OUTPUT);
546 }
547 }
548
549 void wxSocketImplUnix::OnExceptionWaiting()
550 {
551 wxFAIL_MSG( "not supposed to be called" );
552 }
553
554 /*
555 * -------------------------------------------------------------------------
556 * GAddress
557 * -------------------------------------------------------------------------
558 */
559
560 /* CHECK_ADDRESS verifies that the current address family is either
561 * wxSOCKET_NOFAMILY or wxSOCKET_*family*, and if it is wxSOCKET_NOFAMILY, it
562 * initalizes it to be a wxSOCKET_*family*. In other cases, it returns
563 * an appropiate error code.
564 *
565 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
566 */
567 #define CHECK_ADDRESS(address, family) \
568 { \
569 if (address->m_family == wxSOCKET_NOFAMILY) \
570 if (_GAddress_Init_##family(address) != wxSOCKET_NOERROR) \
571 return address->m_error; \
572 if (address->m_family != wxSOCKET_##family) \
573 { \
574 address->m_error = wxSOCKET_INVADDR; \
575 return wxSOCKET_INVADDR; \
576 } \
577 }
578
579 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
580 { \
581 if (address->m_family == wxSOCKET_NOFAMILY) \
582 if (_GAddress_Init_##family(address) != wxSOCKET_NOERROR) \
583 return retval; \
584 if (address->m_family != wxSOCKET_##family) \
585 { \
586 address->m_error = wxSOCKET_INVADDR; \
587 return retval; \
588 } \
589 }
590
591
592 GAddress *GAddress_new(void)
593 {
594 GAddress *address;
595
596 if ((address = (GAddress *) malloc(sizeof(GAddress))) == NULL)
597 return NULL;
598
599 address->m_family = wxSOCKET_NOFAMILY;
600 address->m_addr = NULL;
601 address->m_len = 0;
602
603 return address;
604 }
605
606 GAddress *GAddress_copy(GAddress *address)
607 {
608 GAddress *addr2;
609
610 assert(address != NULL);
611
612 if ((addr2 = (GAddress *) malloc(sizeof(GAddress))) == NULL)
613 return NULL;
614
615 memcpy(addr2, address, sizeof(GAddress));
616
617 if (address->m_addr && address->m_len > 0)
618 {
619 addr2->m_addr = (struct sockaddr *)malloc(addr2->m_len);
620 if (addr2->m_addr == NULL)
621 {
622 free(addr2);
623 return NULL;
624 }
625 memcpy(addr2->m_addr, address->m_addr, addr2->m_len);
626 }
627
628 return addr2;
629 }
630
631 void GAddress_destroy(GAddress *address)
632 {
633 assert(address != NULL);
634
635 if (address->m_addr)
636 free(address->m_addr);
637
638 free(address);
639 }
640
641 void GAddress_SetFamily(GAddress *address, GAddressType type)
642 {
643 assert(address != NULL);
644
645 address->m_family = type;
646 }
647
648 GAddressType GAddress_GetFamily(GAddress *address)
649 {
650 assert(address != NULL);
651
652 return address->m_family;
653 }
654
655 wxSocketError _GAddress_translate_from(GAddress *address,
656 struct sockaddr *addr, int len)
657 {
658 address->m_realfamily = addr->sa_family;
659 switch (addr->sa_family)
660 {
661 case AF_INET:
662 address->m_family = wxSOCKET_INET;
663 break;
664 case AF_UNIX:
665 address->m_family = wxSOCKET_UNIX;
666 break;
667 #if wxUSE_IPV6
668 case AF_INET6:
669 address->m_family = wxSOCKET_INET6;
670 break;
671 #endif // wxUSE_IPV6
672 default:
673 {
674 address->m_error = wxSOCKET_INVOP;
675 return wxSOCKET_INVOP;
676 }
677 }
678
679 if (address->m_addr)
680 free(address->m_addr);
681
682 address->m_len = len;
683 address->m_addr = (struct sockaddr *)malloc(len);
684
685 if (address->m_addr == NULL)
686 {
687 address->m_error = wxSOCKET_MEMERR;
688 return wxSOCKET_MEMERR;
689 }
690
691 memcpy(address->m_addr, addr, len);
692
693 return wxSOCKET_NOERROR;
694 }
695
696 wxSocketError _GAddress_translate_to(GAddress *address,
697 struct sockaddr **addr, int *len)
698 {
699 if (!address->m_addr)
700 {
701 address->m_error = wxSOCKET_INVADDR;
702 return wxSOCKET_INVADDR;
703 }
704
705 *len = address->m_len;
706 *addr = (struct sockaddr *)malloc(address->m_len);
707 if (*addr == NULL)
708 {
709 address->m_error = wxSOCKET_MEMERR;
710 return wxSOCKET_MEMERR;
711 }
712
713 memcpy(*addr, address->m_addr, address->m_len);
714 return wxSOCKET_NOERROR;
715 }
716
717 /*
718 * -------------------------------------------------------------------------
719 * Internet address family
720 * -------------------------------------------------------------------------
721 */
722
723 wxSocketError _GAddress_Init_INET(GAddress *address)
724 {
725 address->m_len = sizeof(struct sockaddr_in);
726 address->m_addr = (struct sockaddr *) malloc(address->m_len);
727 if (address->m_addr == NULL)
728 {
729 address->m_error = wxSOCKET_MEMERR;
730 return wxSOCKET_MEMERR;
731 }
732
733 address->m_family = wxSOCKET_INET;
734 address->m_realfamily = PF_INET;
735 ((struct sockaddr_in *)address->m_addr)->sin_family = AF_INET;
736 ((struct sockaddr_in *)address->m_addr)->sin_addr.s_addr = INADDR_ANY;
737
738 return wxSOCKET_NOERROR;
739 }
740
741 wxSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname)
742 {
743 struct hostent *he;
744 struct in_addr *addr;
745
746 assert(address != NULL);
747
748 CHECK_ADDRESS(address, INET);
749
750 addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr);
751
752 /* If it is a numeric host name, convert it now */
753 #if defined(HAVE_INET_ATON)
754 if (inet_aton(hostname, addr) == 0)
755 {
756 #elif defined(HAVE_INET_ADDR)
757 if ( (addr->s_addr = inet_addr(hostname)) == (unsigned)-1 )
758 {
759 #else
760 /* Use gethostbyname by default */
761 #ifndef __WXMAC__
762 int val = 1; /* VA doesn't like constants in conditional expressions */
763 if (val)
764 #endif
765 {
766 #endif
767 struct in_addr *array_addr;
768
769 /* It is a real name, we solve it */
770 struct hostent h;
771 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
772 struct hostent_data buffer;
773 #else
774 char buffer[1024];
775 #endif
776 int err;
777 he = wxGethostbyname_r(hostname, &h, (void*)&buffer, sizeof(buffer), &err);
778 if (he == NULL)
779 {
780 /* Reset to invalid address */
781 addr->s_addr = INADDR_NONE;
782 address->m_error = wxSOCKET_NOHOST;
783 return wxSOCKET_NOHOST;
784 }
785
786 array_addr = (struct in_addr *) *(he->h_addr_list);
787 addr->s_addr = array_addr[0].s_addr;
788 }
789
790 return wxSOCKET_NOERROR;
791 }
792
793
794 wxSocketError GAddress_INET_SetBroadcastAddress(GAddress *address)
795 {
796 return GAddress_INET_SetHostAddress(address, INADDR_BROADCAST);
797 }
798
799 wxSocketError GAddress_INET_SetAnyAddress(GAddress *address)
800 {
801 return GAddress_INET_SetHostAddress(address, INADDR_ANY);
802 }
803
804 wxSocketError GAddress_INET_SetHostAddress(GAddress *address,
805 unsigned long hostaddr)
806 {
807 struct in_addr *addr;
808
809 assert(address != NULL);
810
811 CHECK_ADDRESS(address, INET);
812
813 addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr);
814 addr->s_addr = htonl(hostaddr);
815
816 return wxSOCKET_NOERROR;
817 }
818
819 wxSocketError GAddress_INET_SetPortName(GAddress *address, const char *port,
820 const char *protocol)
821 {
822 struct servent *se;
823 struct sockaddr_in *addr;
824
825 assert(address != NULL);
826 CHECK_ADDRESS(address, INET);
827
828 if (!port)
829 {
830 address->m_error = wxSOCKET_INVPORT;
831 return wxSOCKET_INVPORT;
832 }
833
834 #if defined(HAVE_FUNC_GETSERVBYNAME_R_4)
835 struct servent_data buffer;
836 #else
837 char buffer[1024];
838 #endif
839 struct servent serv;
840 se = wxGetservbyname_r(port, protocol, &serv,
841 (void*)&buffer, sizeof(buffer));
842 if (!se)
843 {
844 /* the cast to int suppresses compiler warnings about subscript having the
845 type char */
846 if (isdigit((int)port[0]))
847 {
848 int port_int;
849
850 port_int = atoi(port);
851 addr = (struct sockaddr_in *)address->m_addr;
852 addr->sin_port = htons(port_int);
853 return wxSOCKET_NOERROR;
854 }
855
856 address->m_error = wxSOCKET_INVPORT;
857 return wxSOCKET_INVPORT;
858 }
859
860 addr = (struct sockaddr_in *)address->m_addr;
861 addr->sin_port = se->s_port;
862
863 return wxSOCKET_NOERROR;
864 }
865
866 wxSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port)
867 {
868 struct sockaddr_in *addr;
869
870 assert(address != NULL);
871 CHECK_ADDRESS(address, INET);
872
873 addr = (struct sockaddr_in *)address->m_addr;
874 addr->sin_port = htons(port);
875
876 return wxSOCKET_NOERROR;
877 }
878
879 wxSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname, size_t sbuf)
880 {
881 struct hostent *he;
882 char *addr_buf;
883 struct sockaddr_in *addr;
884
885 assert(address != NULL);
886 CHECK_ADDRESS(address, INET);
887
888 addr = (struct sockaddr_in *)address->m_addr;
889 addr_buf = (char *)&(addr->sin_addr);
890
891 struct hostent temphost;
892 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
893 struct hostent_data buffer;
894 #else
895 char buffer[1024];
896 #endif
897 int err;
898 he = wxGethostbyaddr_r(addr_buf, sizeof(addr->sin_addr), AF_INET, &temphost,
899 (void*)&buffer, sizeof(buffer), &err);
900 if (he == NULL)
901 {
902 address->m_error = wxSOCKET_NOHOST;
903 return wxSOCKET_NOHOST;
904 }
905
906 strncpy(hostname, he->h_name, sbuf);
907
908 return wxSOCKET_NOERROR;
909 }
910
911 unsigned long GAddress_INET_GetHostAddress(GAddress *address)
912 {
913 struct sockaddr_in *addr;
914
915 assert(address != NULL);
916 CHECK_ADDRESS_RETVAL(address, INET, 0);
917
918 addr = (struct sockaddr_in *)address->m_addr;
919
920 return ntohl(addr->sin_addr.s_addr);
921 }
922
923 unsigned short GAddress_INET_GetPort(GAddress *address)
924 {
925 struct sockaddr_in *addr;
926
927 assert(address != NULL);
928 CHECK_ADDRESS_RETVAL(address, INET, 0);
929
930 addr = (struct sockaddr_in *)address->m_addr;
931 return ntohs(addr->sin_port);
932 }
933
934 #if wxUSE_IPV6
935 /*
936 * -------------------------------------------------------------------------
937 * Internet IPv6 address family
938 * -------------------------------------------------------------------------
939 */
940
941 wxSocketError _GAddress_Init_INET6(GAddress *address)
942 {
943 struct in6_addr any_address = IN6ADDR_ANY_INIT;
944 address->m_len = sizeof(struct sockaddr_in6);
945 address->m_addr = (struct sockaddr *) malloc(address->m_len);
946 if (address->m_addr == NULL)
947 {
948 address->m_error = wxSOCKET_MEMERR;
949 return wxSOCKET_MEMERR;
950 }
951 memset(address->m_addr,0,address->m_len);
952
953 address->m_family = wxSOCKET_INET6;
954 address->m_realfamily = AF_INET6;
955 ((struct sockaddr_in6 *)address->m_addr)->sin6_family = AF_INET6;
956 ((struct sockaddr_in6 *)address->m_addr)->sin6_addr = any_address;
957
958 return wxSOCKET_NOERROR;
959 }
960
961 wxSocketError GAddress_INET6_SetHostName(GAddress *address, const char *hostname)
962 {
963 assert(address != NULL);
964 CHECK_ADDRESS(address, INET6);
965
966 addrinfo hints;
967 memset( & hints, 0, sizeof( hints ) );
968 hints.ai_family = AF_INET6;
969 addrinfo * info = 0;
970 if ( getaddrinfo( hostname, "0", & hints, & info ) || ! info )
971 {
972 address->m_error = wxSOCKET_NOHOST;
973 return wxSOCKET_NOHOST;
974 }
975
976 memcpy( address->m_addr, info->ai_addr, info->ai_addrlen );
977 freeaddrinfo( info );
978 return wxSOCKET_NOERROR;
979 }
980
981 wxSocketError GAddress_INET6_SetAnyAddress(GAddress *address)
982 {
983 assert(address != NULL);
984
985 CHECK_ADDRESS(address, INET6);
986
987 struct in6_addr addr;
988 memset( & addr, 0, sizeof( addr ) );
989 return GAddress_INET6_SetHostAddress(address, addr);
990 }
991 wxSocketError GAddress_INET6_SetHostAddress(GAddress *address,
992 struct in6_addr hostaddr)
993 {
994 assert(address != NULL);
995
996 CHECK_ADDRESS(address, INET6);
997
998 ((struct sockaddr_in6 *)address->m_addr)->sin6_addr = hostaddr;
999
1000 return wxSOCKET_NOERROR;
1001 }
1002
1003 wxSocketError GAddress_INET6_SetPortName(GAddress *address, const char *port,
1004 const char *protocol)
1005 {
1006 struct servent *se;
1007 struct sockaddr_in6 *addr;
1008
1009 assert(address != NULL);
1010 CHECK_ADDRESS(address, INET6);
1011
1012 if (!port)
1013 {
1014 address->m_error = wxSOCKET_INVPORT;
1015 return wxSOCKET_INVPORT;
1016 }
1017
1018 se = getservbyname(port, protocol);
1019 if (!se)
1020 {
1021 if (isdigit(port[0]))
1022 {
1023 int port_int;
1024
1025 port_int = atoi(port);
1026 addr = (struct sockaddr_in6 *)address->m_addr;
1027 addr->sin6_port = htons((u_short) port_int);
1028 return wxSOCKET_NOERROR;
1029 }
1030
1031 address->m_error = wxSOCKET_INVPORT;
1032 return wxSOCKET_INVPORT;
1033 }
1034
1035 addr = (struct sockaddr_in6 *)address->m_addr;
1036 addr->sin6_port = se->s_port;
1037
1038 return wxSOCKET_NOERROR;
1039 }
1040
1041 wxSocketError GAddress_INET6_SetPort(GAddress *address, unsigned short port)
1042 {
1043 struct sockaddr_in6 *addr;
1044
1045 assert(address != NULL);
1046 CHECK_ADDRESS(address, INET6);
1047
1048 addr = (struct sockaddr_in6 *)address->m_addr;
1049 addr->sin6_port = htons(port);
1050
1051 return wxSOCKET_NOERROR;
1052 }
1053
1054 wxSocketError GAddress_INET6_GetHostName(GAddress *address, char *hostname, size_t sbuf)
1055 {
1056 struct hostent *he;
1057 char *addr_buf;
1058 struct sockaddr_in6 *addr;
1059
1060 assert(address != NULL);
1061 CHECK_ADDRESS(address, INET6);
1062
1063 addr = (struct sockaddr_in6 *)address->m_addr;
1064 addr_buf = (char *)&(addr->sin6_addr);
1065
1066 he = gethostbyaddr(addr_buf, sizeof(addr->sin6_addr), AF_INET6);
1067 if (he == NULL)
1068 {
1069 address->m_error = wxSOCKET_NOHOST;
1070 return wxSOCKET_NOHOST;
1071 }
1072
1073 strncpy(hostname, he->h_name, sbuf);
1074
1075 return wxSOCKET_NOERROR;
1076 }
1077
1078 wxSocketError GAddress_INET6_GetHostAddress(GAddress *address,struct in6_addr *hostaddr)
1079 {
1080 assert(address != NULL);
1081 assert(hostaddr != NULL);
1082 CHECK_ADDRESS_RETVAL(address, INET6, wxSOCKET_INVADDR);
1083 *hostaddr = ( (struct sockaddr_in6 *)address->m_addr )->sin6_addr;
1084 return wxSOCKET_NOERROR;
1085 }
1086
1087 unsigned short GAddress_INET6_GetPort(GAddress *address)
1088 {
1089 assert(address != NULL);
1090 CHECK_ADDRESS_RETVAL(address, INET6, 0);
1091
1092 return ntohs( ((struct sockaddr_in6 *)address->m_addr)->sin6_port );
1093 }
1094
1095 #endif // wxUSE_IPV6
1096
1097 /*
1098 * -------------------------------------------------------------------------
1099 * Unix address family
1100 * -------------------------------------------------------------------------
1101 */
1102
1103 #ifndef __VISAGECPP__
1104 wxSocketError _GAddress_Init_UNIX(GAddress *address)
1105 {
1106 address->m_len = sizeof(struct sockaddr_un);
1107 address->m_addr = (struct sockaddr *)malloc(address->m_len);
1108 if (address->m_addr == NULL)
1109 {
1110 address->m_error = wxSOCKET_MEMERR;
1111 return wxSOCKET_MEMERR;
1112 }
1113
1114 address->m_family = wxSOCKET_UNIX;
1115 address->m_realfamily = PF_UNIX;
1116 ((struct sockaddr_un *)address->m_addr)->sun_family = AF_UNIX;
1117 ((struct sockaddr_un *)address->m_addr)->sun_path[0] = 0;
1118
1119 return wxSOCKET_NOERROR;
1120 }
1121
1122 #define UNIX_SOCK_PATHLEN (sizeof(addr->sun_path)/sizeof(addr->sun_path[0]))
1123
1124 wxSocketError GAddress_UNIX_SetPath(GAddress *address, const char *path)
1125 {
1126 struct sockaddr_un *addr;
1127
1128 assert(address != NULL);
1129
1130 CHECK_ADDRESS(address, UNIX);
1131
1132 addr = ((struct sockaddr_un *)address->m_addr);
1133 strncpy(addr->sun_path, path, UNIX_SOCK_PATHLEN);
1134 addr->sun_path[UNIX_SOCK_PATHLEN - 1] = '\0';
1135
1136 return wxSOCKET_NOERROR;
1137 }
1138
1139 wxSocketError GAddress_UNIX_GetPath(GAddress *address, char *path, size_t sbuf)
1140 {
1141 struct sockaddr_un *addr;
1142
1143 assert(address != NULL);
1144 CHECK_ADDRESS(address, UNIX);
1145
1146 addr = (struct sockaddr_un *)address->m_addr;
1147
1148 strncpy(path, addr->sun_path, sbuf);
1149
1150 return wxSOCKET_NOERROR;
1151 }
1152 #endif /* !defined(__VISAGECPP__) */
1153
1154 #endif /* wxUSE_SOCKETS */