rename various gsock* files to sock* (except for MSW where this will be done later)
[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 #include "wx/private/gsocketiohandler.h"
22
23 #if defined(__VISAGECPP__)
24 #define BSD_SELECT /* use Berkeley Sockets select */
25 #endif
26
27 #if defined(__WATCOMC__)
28 #include <errno.h>
29 #include <nerrno.h>
30 #endif
31
32 #include <assert.h>
33 #include <sys/types.h>
34 #ifdef __VISAGECPP__
35 #include <string.h>
36 #include <sys/time.h>
37 #include <types.h>
38 #include <netinet/in.h>
39 #endif
40 #include <netdb.h>
41 #include <sys/ioctl.h>
42
43 #ifdef HAVE_SYS_SELECT_H
44 # include <sys/select.h>
45 #endif
46
47 #ifdef __VMS__
48 #include <socket.h>
49 struct sockaddr_un
50 {
51 u_char sun_len; /* sockaddr len including null */
52 u_char sun_family; /* AF_UNIX */
53 char sun_path[108]; /* path name (gag) */
54 };
55 #else
56 #include <sys/socket.h>
57 #include <sys/un.h>
58 #endif
59
60 #ifndef __VISAGECPP__
61 #include <sys/time.h>
62 #include <netinet/in.h>
63 #include <arpa/inet.h>
64 #include <errno.h>
65 #include <string.h>
66 #include <unistd.h>
67 #else
68 #include <nerrno.h>
69 # if __IBMCPP__ < 400
70 #include <machine/endian.h>
71 #include <socket.h>
72 #include <ioctl.h>
73 #include <select.h>
74 #include <unistd.h>
75
76 #define EBADF SOCEBADF
77
78 # ifdef min
79 # undef min
80 # endif
81 # else
82 #include <sys/socket.h>
83 #include <sys/ioctl.h>
84 #include <sys/select.h>
85
86 #define close(a) soclose(a)
87 #define select(a,b,c,d,e) bsdselect(a,b,c,d,e)
88 int _System bsdselect(int,
89 struct fd_set *,
90 struct fd_set *,
91 struct fd_set *,
92 struct timeval *);
93 int _System soclose(int);
94 # endif
95 #endif
96 #ifdef __EMX__
97 #include <sys/select.h>
98 #endif
99
100 #include <stdio.h>
101 #include <stdlib.h>
102 #include <stddef.h>
103 #include <ctype.h>
104 #ifdef sun
105 # include <sys/filio.h>
106 #endif
107 #ifdef sgi
108 # include <bstring.h>
109 #endif
110 #ifdef _AIX
111 # include <strings.h>
112 #endif
113 #include <signal.h>
114
115 #ifndef WX_SOCKLEN_T
116
117 #ifdef VMS
118 # define WX_SOCKLEN_T unsigned int
119 #else
120 # ifdef __GLIBC__
121 # if __GLIBC__ == 2
122 # define WX_SOCKLEN_T socklen_t
123 # endif
124 # elif defined(__WXMAC__)
125 # define WX_SOCKLEN_T socklen_t
126 # else
127 # define WX_SOCKLEN_T int
128 # endif
129 #endif
130
131 #endif /* SOCKLEN_T */
132
133 #ifndef SOCKOPTLEN_T
134 #define SOCKOPTLEN_T WX_SOCKLEN_T
135 #endif
136
137 /* UnixWare reportedly needs this for FIONBIO definition */
138 #ifdef __UNIXWARE__
139 #include <sys/filio.h>
140 #endif
141
142 /*
143 * INADDR_BROADCAST is identical to INADDR_NONE which is not defined
144 * on all systems. INADDR_BROADCAST should be fine to indicate an error.
145 */
146 #ifndef INADDR_NONE
147 #define INADDR_NONE INADDR_BROADCAST
148 #endif
149
150 #if defined(__VISAGECPP__) || defined(__WATCOMC__)
151
152 #define MASK_SIGNAL() {
153 #define UNMASK_SIGNAL() }
154
155 #else
156 extern "C" { typedef void (*wxSigHandler)(int); }
157
158 #define MASK_SIGNAL() \
159 { \
160 wxSigHandler old_handler = signal(SIGPIPE, SIG_IGN);
161
162 #define UNMASK_SIGNAL() \
163 signal(SIGPIPE, old_handler); \
164 }
165
166 #endif
167
168 /* If a SIGPIPE is issued by a socket call on a remotely closed socket,
169 the program will "crash" unless it explicitly handles the SIGPIPE.
170 By using MSG_NOSIGNAL, the SIGPIPE is suppressed. Later, we will
171 use SO_NOSIGPIPE (if available), the BSD equivalent. */
172 #ifdef MSG_NOSIGNAL
173 # define GSOCKET_MSG_NOSIGNAL MSG_NOSIGNAL
174 #else /* MSG_NOSIGNAL not available (FreeBSD including OS X) */
175 # define GSOCKET_MSG_NOSIGNAL 0
176 #endif /* MSG_NOSIGNAL */
177
178 #if wxUSE_THREADS && (defined(HAVE_GETHOSTBYNAME) || defined(HAVE_GETSERVBYNAME))
179 # include "wx/thread.h"
180 #endif
181
182 #if defined(HAVE_GETHOSTBYNAME)
183 static struct hostent * deepCopyHostent(struct hostent *h,
184 const struct hostent *he,
185 char *buffer, int size, int *err)
186 {
187 /* copy old structure */
188 memcpy(h, he, sizeof(struct hostent));
189
190 /* copy name */
191 int len = strlen(h->h_name);
192 if (len > size)
193 {
194 *err = ENOMEM;
195 return NULL;
196 }
197 memcpy(buffer, h->h_name, len);
198 buffer[len] = '\0';
199 h->h_name = buffer;
200
201 /* track position in the buffer */
202 int pos = len + 1;
203
204 /* reuse len to store address length */
205 len = h->h_length;
206
207 /* ensure pointer alignment */
208 unsigned int misalign = sizeof(char *) - pos%sizeof(char *);
209 if(misalign < sizeof(char *))
210 pos += misalign;
211
212 /* leave space for pointer list */
213 char **p = h->h_addr_list, **q;
214 char **h_addr_list = (char **)(buffer + pos);
215 while(*(p++) != 0)
216 pos += sizeof(char *);
217
218 /* copy addresses and fill new pointer list */
219 for (p = h->h_addr_list, q = h_addr_list; *p != 0; p++, q++)
220 {
221 if (size < pos + len)
222 {
223 *err = ENOMEM;
224 return NULL;
225 }
226 memcpy(buffer + pos, *p, len); /* copy content */
227 *q = buffer + pos; /* set copied pointer to copied content */
228 pos += len;
229 }
230 *++q = 0; /* null terminate the pointer list */
231 h->h_addr_list = h_addr_list; /* copy pointer to pointers */
232
233 /* ensure word alignment of pointers */
234 misalign = sizeof(char *) - pos%sizeof(char *);
235 if(misalign < sizeof(char *))
236 pos += misalign;
237
238 /* leave space for pointer list */
239 p = h->h_aliases;
240 char **h_aliases = (char **)(buffer + pos);
241 while(*(p++) != 0)
242 pos += sizeof(char *);
243
244 /* copy aliases and fill new pointer list */
245 for (p = h->h_aliases, q = h_aliases; *p != 0; p++, q++)
246 {
247 len = strlen(*p);
248 if (size <= pos + len)
249 {
250 *err = ENOMEM;
251 return NULL;
252 }
253 memcpy(buffer + pos, *p, len); /* copy content */
254 buffer[pos + len] = '\0';
255 *q = buffer + pos; /* set copied pointer to copied content */
256 pos += len + 1;
257 }
258 *++q = 0; /* null terminate the pointer list */
259 h->h_aliases = h_aliases; /* copy pointer to pointers */
260
261 return h;
262 }
263 #endif
264
265 #if defined(HAVE_GETHOSTBYNAME) && wxUSE_THREADS
266 static wxMutex nameLock;
267 #endif
268 struct hostent * wxGethostbyname_r(const char *hostname, struct hostent *h,
269 void *buffer, int size, int *err)
270
271 {
272 struct hostent *he = NULL;
273 *err = 0;
274 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_6)
275 if (gethostbyname_r(hostname, h, (char*)buffer, size, &he, err))
276 he = NULL;
277 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_5)
278 he = gethostbyname_r(hostname, h, (char*)buffer, size, err);
279 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
280 if (gethostbyname_r(hostname, h, (struct hostent_data*) buffer))
281 {
282 he = NULL;
283 *err = h_errno;
284 }
285 else
286 he = h;
287 #elif defined(HAVE_GETHOSTBYNAME)
288 #if wxUSE_THREADS
289 wxMutexLocker locker(nameLock);
290 #endif
291 he = gethostbyname(hostname);
292 if (!he)
293 *err = h_errno;
294 else
295 he = deepCopyHostent(h, he, (char*)buffer, size, err);
296 #endif
297 return he;
298 }
299
300 #if defined(HAVE_GETHOSTBYNAME) && wxUSE_THREADS
301 static wxMutex addrLock;
302 #endif
303 struct hostent * wxGethostbyaddr_r(const char *addr_buf, int buf_size,
304 int proto, struct hostent *h,
305 void *buffer, int size, int *err)
306 {
307 struct hostent *he = NULL;
308 *err = 0;
309 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_6)
310 if (gethostbyaddr_r(addr_buf, buf_size, proto, h,
311 (char*)buffer, size, &he, err))
312 he = NULL;
313 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_5)
314 he = gethostbyaddr_r(addr_buf, buf_size, proto, h, (char*)buffer, size, err);
315 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
316 if (gethostbyaddr_r(addr_buf, buf_size, proto, h,
317 (struct hostent_data*) buffer))
318 {
319 he = NULL;
320 *err = h_errno;
321 }
322 else
323 he = h;
324 #elif defined(HAVE_GETHOSTBYNAME)
325 #if wxUSE_THREADS
326 wxMutexLocker locker(addrLock);
327 #endif
328 he = gethostbyaddr(addr_buf, buf_size, proto);
329 if (!he)
330 *err = h_errno;
331 else
332 he = deepCopyHostent(h, he, (char*)buffer, size, err);
333 #endif
334 return he;
335 }
336
337 #if defined(HAVE_GETSERVBYNAME)
338 static struct servent * deepCopyServent(struct servent *s,
339 const struct servent *se,
340 char *buffer, int size)
341 {
342 /* copy plain old structure */
343 memcpy(s, se, sizeof(struct servent));
344
345 /* copy name */
346 int len = strlen(s->s_name);
347 if (len >= size)
348 {
349 return NULL;
350 }
351 memcpy(buffer, s->s_name, len);
352 buffer[len] = '\0';
353 s->s_name = buffer;
354
355 /* track position in the buffer */
356 int pos = len + 1;
357
358 /* copy protocol */
359 len = strlen(s->s_proto);
360 if (pos + len >= size)
361 {
362 return NULL;
363 }
364 memcpy(buffer + pos, s->s_proto, len);
365 buffer[pos + len] = '\0';
366 s->s_proto = buffer + pos;
367
368 /* track position in the buffer */
369 pos += len + 1;
370
371 /* ensure pointer alignment */
372 unsigned int misalign = sizeof(char *) - pos%sizeof(char *);
373 if(misalign < sizeof(char *))
374 pos += misalign;
375
376 /* leave space for pointer list */
377 char **p = s->s_aliases, **q;
378 char **s_aliases = (char **)(buffer + pos);
379 while(*(p++) != 0)
380 pos += sizeof(char *);
381
382 /* copy addresses and fill new pointer list */
383 for (p = s->s_aliases, q = s_aliases; *p != 0; p++, q++){
384 len = strlen(*p);
385 if (size <= pos + len)
386 {
387 return NULL;
388 }
389 memcpy(buffer + pos, *p, len); /* copy content */
390 buffer[pos + len] = '\0';
391 *q = buffer + pos; /* set copied pointer to copied content */
392 pos += len + 1;
393 }
394 *++q = 0; /* null terminate the pointer list */
395 s->s_aliases = s_aliases; /* copy pointer to pointers */
396 return s;
397 }
398 #endif
399
400 #if defined(HAVE_GETSERVBYNAME) && wxUSE_THREADS
401 static wxMutex servLock;
402 #endif
403 struct servent *wxGetservbyname_r(const char *port, const char *protocol,
404 struct servent *serv, void *buffer, int size)
405 {
406 struct servent *se = NULL;
407 #if defined(HAVE_FUNC_GETSERVBYNAME_R_6)
408 if (getservbyname_r(port, protocol, serv, (char*)buffer, size, &se))
409 se = NULL;
410 #elif defined(HAVE_FUNC_GETSERVBYNAME_R_5)
411 se = getservbyname_r(port, protocol, serv, (char*)buffer, size);
412 #elif defined(HAVE_FUNC_GETSERVBYNAME_R_4)
413 if (getservbyname_r(port, protocol, serv, (struct servent_data*) buffer))
414 se = NULL;
415 else
416 se = serv;
417 #elif defined(HAVE_GETSERVBYNAME)
418 #if wxUSE_THREADS
419 wxMutexLocker locker(servLock);
420 #endif
421 se = getservbyname(port, protocol);
422 if (se)
423 se = deepCopyServent(serv, se, (char*)buffer, size);
424 #endif
425 return se;
426 }
427
428 /* debugging helpers */
429 #ifdef __GSOCKET_DEBUG__
430 # define SOCKET_DEBUG(args) printf args
431 #else
432 # define SOCKET_DEBUG(args)
433 #endif /* __GSOCKET_DEBUG__ */
434
435 /* Constructors / Destructors for wxSocketImplUnix */
436
437 wxSocketImplUnix::wxSocketImplUnix(wxSocketBase& wxsocket)
438 : wxSocketImpl(wxsocket)
439 {
440 m_fds[0] =
441 m_fds[1] = -1;
442
443 m_use_events = false;
444 }
445
446 /*
447 * Disallow further read/write operations on this socket, close
448 * the fd and disable all callbacks.
449 */
450 void wxSocketImplUnix::Shutdown()
451 {
452 /* Don't allow events to fire after socket has been closed */
453 DisableEvents();
454
455 wxSocketImpl::Shutdown();
456 }
457
458 /*
459 * Waits for an incoming client connection. Returns a pointer to
460 * a wxSocketImplUnix object, or NULL if there was an error, in which case
461 * the last error field will be updated for the calling wxSocketImplUnix.
462 *
463 * Error codes (set in the calling wxSocketImplUnix)
464 * wxSOCKET_INVSOCK - the socket is not valid or not a server.
465 * wxSOCKET_TIMEDOUT - timeout, no incoming connections.
466 * wxSOCKET_WOULDBLOCK - the call would block and the socket is nonblocking.
467 * wxSOCKET_MEMERR - couldn't allocate memory.
468 * wxSOCKET_IOERR - low-level error.
469 */
470 wxSocketImpl *wxSocketImplUnix::WaitConnection(wxSocketBase& wxsocket)
471 {
472 wxSockAddr from;
473 WX_SOCKLEN_T fromlen = sizeof(from);
474 wxSocketImpl *connection;
475 wxSocketError err;
476 int arg = 1;
477
478 /* If the socket has already been created, we exit immediately */
479 if (m_fd == INVALID_SOCKET || !m_server)
480 {
481 m_error = wxSOCKET_INVSOCK;
482 return NULL;
483 }
484
485 /* Create a wxSocketImplUnix object for the new connection */
486 connection = wxSocketImplUnix::Create(wxsocket);
487
488 if (!connection)
489 {
490 m_error = wxSOCKET_MEMERR;
491 return NULL;
492 }
493
494 /* Wait for a connection (with timeout) */
495 if (Input_Timeout() == wxSOCKET_TIMEDOUT)
496 {
497 delete connection;
498 /* m_error set by Input_Timeout */
499 return NULL;
500 }
501
502 connection->m_fd = accept(m_fd, (sockaddr*)&from, (WX_SOCKLEN_T *) &fromlen);
503
504 /* Reenable CONNECTION events */
505 EnableEvent(wxSOCKET_CONNECTION);
506
507 if (connection->m_fd == INVALID_SOCKET)
508 {
509 if (errno == EWOULDBLOCK)
510 m_error = wxSOCKET_WOULDBLOCK;
511 else
512 m_error = wxSOCKET_IOERR;
513
514 delete connection;
515 return NULL;
516 }
517
518 /* Initialize all fields */
519 connection->m_server = false;
520 connection->m_stream = true;
521
522 /* Setup the peer address field */
523 connection->m_peer = GAddress_new();
524 if (!connection->m_peer)
525 {
526 delete connection;
527 m_error = wxSOCKET_MEMERR;
528 return NULL;
529 }
530
531 err = _GAddress_translate_from(connection->m_peer, (sockaddr*)&from, fromlen);
532 if (err != wxSOCKET_NOERROR)
533 {
534 delete connection;
535 m_error = err;
536 return NULL;
537 }
538
539 #if defined(__EMX__) || defined(__VISAGECPP__)
540 ioctl(connection->m_fd, FIONBIO, (char*)&arg, sizeof(arg));
541 #else
542 ioctl(connection->m_fd, FIONBIO, &arg);
543 #endif
544 if (m_use_events)
545 connection->Notify(true);
546
547 return connection;
548 }
549
550 void wxSocketImplUnix::Notify(bool flag)
551 {
552 if (flag == m_use_events)
553 return;
554 m_use_events = flag;
555 DoEnableEvents(flag);
556 }
557
558 void wxSocketImplUnix::DoEnableEvents(bool flag)
559 {
560 wxSocketManager * const manager = wxSocketManager::Get();
561 if ( flag )
562 {
563 manager->Install_Callback(this, wxSOCKET_INPUT);
564 manager->Install_Callback(this, wxSOCKET_OUTPUT);
565 }
566 else // off
567 {
568 manager->Uninstall_Callback(this, wxSOCKET_INPUT);
569 manager->Uninstall_Callback(this, wxSOCKET_OUTPUT);
570 }
571 }
572
573 wxSocketError wxSocketImplUnix::DoHandleConnect(int ret)
574 {
575 /* We only call EnableEvents() if we know we aren't shutting down the socket.
576 * NB: EnableEvents() needs to be called whether the socket is blocking or
577 * non-blocking, it just shouldn't be called prior to knowing there is a
578 * connection _if_ blocking sockets are being used.
579 * If connect above returns 0, we are already connected and need to make the
580 * call to EnableEvents() now.
581 */
582 if ( m_non_blocking || (ret == 0) )
583 EnableEvents();
584
585 if (ret == -1)
586 {
587 const int err = errno;
588
589 /* If connect failed with EINPROGRESS and the wxSocketImplUnix object
590 * is in blocking mode, we select() for the specified timeout
591 * checking for writability to see if the connection request
592 * completes.
593 */
594 if ((err == EINPROGRESS) && (!m_non_blocking))
595 {
596 if (Output_Timeout() == wxSOCKET_TIMEDOUT)
597 {
598 Close();
599 /* m_error is set in Output_Timeout */
600 return wxSOCKET_TIMEDOUT;
601 }
602 else
603 {
604 int error;
605 SOCKOPTLEN_T len = sizeof(error);
606
607 getsockopt(m_fd, SOL_SOCKET, SO_ERROR, (char*) &error, &len);
608 EnableEvents();
609
610 if (!error)
611 return wxSOCKET_NOERROR;
612 }
613 }
614
615 /* If connect failed with EINPROGRESS and the wxSocketImplUnix object
616 * is set to nonblocking, we set m_error to wxSOCKET_WOULDBLOCK
617 * (and return wxSOCKET_WOULDBLOCK) but we don't close the socket;
618 * this way if the connection completes, a wxSOCKET_CONNECTION
619 * event will be generated, if enabled.
620 */
621 if ((err == EINPROGRESS) && (m_non_blocking))
622 {
623 m_establishing = true;
624 m_error = wxSOCKET_WOULDBLOCK;
625 return wxSOCKET_WOULDBLOCK;
626 }
627
628 /* If connect failed with an error other than EINPROGRESS,
629 * then the call to Connect has failed.
630 */
631 Close();
632 m_error = wxSOCKET_IOERR;
633
634 return wxSOCKET_IOERR;
635 }
636
637 return wxSOCKET_NOERROR;
638 }
639
640 /* Generic IO */
641
642 /* Like recv(), send(), ... */
643 int wxSocketImplUnix::Read(char *buffer, int size)
644 {
645 int ret;
646
647 if (m_fd == INVALID_SOCKET || m_server)
648 {
649 m_error = wxSOCKET_INVSOCK;
650 return -1;
651 }
652
653 /* Disable events during query of socket status */
654 DisableEvent(wxSOCKET_INPUT);
655
656 /* If the socket is blocking, wait for data (with a timeout) */
657 if (Input_Timeout() == wxSOCKET_TIMEDOUT) {
658 m_error = wxSOCKET_TIMEDOUT;
659 /* Don't return here immediately, otherwise socket events would not be
660 * re-enabled! */
661 ret = -1;
662 }
663 else
664 {
665 /* Read the data */
666 if (m_stream)
667 ret = Recv_Stream(buffer, size);
668 else
669 ret = Recv_Dgram(buffer, size);
670
671 /*
672 * If recv returned zero for a TCP socket (if m_stream == NULL, it's an UDP
673 * socket and empty datagrams are possible), then the connection has been
674 * gracefully closed.
675 *
676 * Otherwise, recv has returned an error (-1), in which case we have lost
677 * the socket only if errno does _not_ indicate that there may be more data
678 * to read.
679 */
680 if ((ret == 0) && m_stream)
681 {
682 /* Make sure wxSOCKET_LOST event gets sent and shut down the socket */
683 if (m_use_events)
684 {
685 m_detected = wxSOCKET_LOST_FLAG;
686 Detected_Read();
687 return 0;
688 }
689 }
690 else if (ret == -1)
691 {
692 if ((errno == EWOULDBLOCK) || (errno == EAGAIN))
693 m_error = wxSOCKET_WOULDBLOCK;
694 else
695 m_error = wxSOCKET_IOERR;
696 }
697 }
698
699 /* Enable events again now that we are done processing */
700 EnableEvent(wxSOCKET_INPUT);
701
702 return ret;
703 }
704
705 int wxSocketImplUnix::Write(const char *buffer, int size)
706 {
707 int ret;
708
709 SOCKET_DEBUG(( "Write #1, size %d\n", size ));
710
711 if (m_fd == INVALID_SOCKET || m_server)
712 {
713 m_error = wxSOCKET_INVSOCK;
714 return -1;
715 }
716
717 SOCKET_DEBUG(( "Write #2, size %d\n", size ));
718
719 /* If the socket is blocking, wait for writability (with a timeout) */
720 if (Output_Timeout() == wxSOCKET_TIMEDOUT)
721 return -1;
722
723 SOCKET_DEBUG(( "Write #3, size %d\n", size ));
724
725 /* Write the data */
726 if (m_stream)
727 ret = Send_Stream(buffer, size);
728 else
729 ret = Send_Dgram(buffer, size);
730
731 SOCKET_DEBUG(( "Write #4, size %d\n", size ));
732
733 if (ret == -1)
734 {
735 if ((errno == EWOULDBLOCK) || (errno == EAGAIN))
736 {
737 m_error = wxSOCKET_WOULDBLOCK;
738 SOCKET_DEBUG(( "Write error WOULDBLOCK\n" ));
739 }
740 else
741 {
742 m_error = wxSOCKET_IOERR;
743 SOCKET_DEBUG(( "Write error IOERR\n" ));
744 }
745
746 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
747 * in MSW). Once the first OUTPUT event is received, users can assume
748 * that the socket is writable until a read operation fails. Only then
749 * will further OUTPUT events be posted.
750 */
751 EnableEvent(wxSOCKET_OUTPUT);
752
753 return -1;
754 }
755
756 SOCKET_DEBUG(( "Write #5, size %d ret %d\n", size, ret ));
757
758 return ret;
759 }
760
761 /* Flags */
762
763 void wxSocketImplUnix::EnableEvent(wxSocketNotify event)
764 {
765 if (m_use_events)
766 {
767 m_detected &= ~(1 << event);
768 wxSocketManager::Get()->Install_Callback(this, event);
769 }
770 }
771
772 void wxSocketImplUnix::DisableEvent(wxSocketNotify event)
773 {
774 if (m_use_events)
775 {
776 m_detected |= (1 << event);
777 wxSocketManager::Get()->Uninstall_Callback(this, event);
778 }
779 }
780
781 /*
782 * For blocking sockets, wait until data is available or
783 * until timeout ellapses.
784 */
785 wxSocketError wxSocketImplUnix::Input_Timeout()
786 {
787 fd_set readfds;
788 int ret;
789
790 // Linux select() will overwrite the struct on return so make a copy
791 struct timeval tv = m_timeout;
792
793 if (!m_non_blocking)
794 {
795 wxFD_ZERO(&readfds);
796 wxFD_SET(m_fd, &readfds);
797 ret = select(m_fd + 1, &readfds, NULL, NULL, &tv);
798 if (ret == 0)
799 {
800 SOCKET_DEBUG(( "Input_Timeout, select returned 0\n" ));
801 m_error = wxSOCKET_TIMEDOUT;
802 return wxSOCKET_TIMEDOUT;
803 }
804
805 if (ret == -1)
806 {
807 SOCKET_DEBUG(( "Input_Timeout, select returned -1\n" ));
808 if (errno == EBADF) { SOCKET_DEBUG(( "Invalid file descriptor\n" )); }
809 if (errno == EINTR) { SOCKET_DEBUG(( "A non blocked signal was caught\n" )); }
810 if (errno == EINVAL) { SOCKET_DEBUG(( "The highest number descriptor is negative\n" )); }
811 if (errno == ENOMEM) { SOCKET_DEBUG(( "Not enough memory\n" )); }
812 m_error = wxSOCKET_TIMEDOUT;
813 return wxSOCKET_TIMEDOUT;
814 }
815 }
816
817 return wxSOCKET_NOERROR;
818 }
819
820 /*
821 * For blocking sockets, wait until data can be sent without
822 * blocking or until timeout ellapses.
823 */
824 wxSocketError wxSocketImplUnix::Output_Timeout()
825 {
826 fd_set writefds;
827 int ret;
828
829 // Linux select() will overwrite the struct on return so make a copy
830 struct timeval tv = m_timeout;
831
832 SOCKET_DEBUG( ("m_non_blocking has: %d\n", (int)m_non_blocking) );
833
834 if (!m_non_blocking)
835 {
836 wxFD_ZERO(&writefds);
837 wxFD_SET(m_fd, &writefds);
838 ret = select(m_fd + 1, NULL, &writefds, NULL, &tv);
839 if (ret == 0)
840 {
841 SOCKET_DEBUG(( "Output_Timeout, select returned 0\n" ));
842 m_error = wxSOCKET_TIMEDOUT;
843 return wxSOCKET_TIMEDOUT;
844 }
845
846 if (ret == -1)
847 {
848 SOCKET_DEBUG(( "Output_Timeout, select returned -1\n" ));
849 if (errno == EBADF) { SOCKET_DEBUG(( "Invalid file descriptor\n" )); }
850 if (errno == EINTR) { SOCKET_DEBUG(( "A non blocked signal was caught\n" )); }
851 if (errno == EINVAL) { SOCKET_DEBUG(( "The highest number descriptor is negative\n" )); }
852 if (errno == ENOMEM) { SOCKET_DEBUG(( "Not enough memory\n" )); }
853 m_error = wxSOCKET_TIMEDOUT;
854 return wxSOCKET_TIMEDOUT;
855 }
856
857 if ( ! wxFD_ISSET(m_fd, &writefds) )
858 {
859 SOCKET_DEBUG(( "Output_Timeout is buggy!\n" ));
860 }
861 else
862 {
863 SOCKET_DEBUG(( "Output_Timeout seems correct\n" ));
864 }
865 }
866 else
867 {
868 SOCKET_DEBUG(( "Output_Timeout, didn't try select!\n" ));
869 }
870
871 return wxSOCKET_NOERROR;
872 }
873
874 int wxSocketImplUnix::Recv_Stream(char *buffer, int size)
875 {
876 int ret;
877 do
878 {
879 ret = recv(m_fd, buffer, size, GSOCKET_MSG_NOSIGNAL);
880 }
881 while (ret == -1 && errno == EINTR); /* Loop until not interrupted */
882
883 return ret;
884 }
885
886 int wxSocketImplUnix::Recv_Dgram(char *buffer, int size)
887 {
888 wxSockAddr from;
889 WX_SOCKLEN_T fromlen = sizeof(from);
890 int ret;
891 wxSocketError err;
892
893 fromlen = sizeof(from);
894
895 do
896 {
897 ret = recvfrom(m_fd, buffer, size, 0, (sockaddr*)&from, (WX_SOCKLEN_T *) &fromlen);
898 }
899 while (ret == -1 && errno == EINTR); /* Loop until not interrupted */
900
901 if (ret == -1)
902 return -1;
903
904 /* Translate a system address into a wxSocketImplUnix address */
905 if (!m_peer)
906 {
907 m_peer = GAddress_new();
908 if (!m_peer)
909 {
910 m_error = wxSOCKET_MEMERR;
911 return -1;
912 }
913 }
914
915 err = _GAddress_translate_from(m_peer, (sockaddr*)&from, fromlen);
916 if (err != wxSOCKET_NOERROR)
917 {
918 GAddress_destroy(m_peer);
919 m_peer = NULL;
920 m_error = err;
921 return -1;
922 }
923
924 return ret;
925 }
926
927 int wxSocketImplUnix::Send_Stream(const char *buffer, int size)
928 {
929 int ret;
930
931 MASK_SIGNAL();
932
933 do
934 {
935 ret = send(m_fd, (char *)buffer, size, GSOCKET_MSG_NOSIGNAL);
936 }
937 while (ret == -1 && errno == EINTR); /* Loop until not interrupted */
938
939 UNMASK_SIGNAL();
940
941 return ret;
942 }
943
944 int wxSocketImplUnix::Send_Dgram(const char *buffer, int size)
945 {
946 struct sockaddr *addr;
947 int len, ret;
948 wxSocketError err;
949
950 if (!m_peer)
951 {
952 m_error = wxSOCKET_INVADDR;
953 return -1;
954 }
955
956 err = _GAddress_translate_to(m_peer, &addr, &len);
957 if (err != wxSOCKET_NOERROR)
958 {
959 m_error = err;
960 return -1;
961 }
962
963 MASK_SIGNAL();
964
965 do
966 {
967 ret = sendto(m_fd, (char *)buffer, size, 0, addr, len);
968 }
969 while (ret == -1 && errno == EINTR); /* Loop until not interrupted */
970
971 UNMASK_SIGNAL();
972
973 /* Frees memory allocated from _GAddress_translate_to */
974 free(addr);
975
976 return ret;
977 }
978
979 void wxSocketImplUnix::OnStateChange(wxSocketNotify event)
980 {
981 DisableEvent(event);
982 NotifyOnStateChange(event);
983
984 if ( event == wxSOCKET_LOST )
985 Shutdown();
986 }
987
988 void wxSocketImplUnix::Detected_Read()
989 {
990 char c;
991
992 /* Safeguard against straggling call to Detected_Read */
993 if (m_fd == INVALID_SOCKET)
994 {
995 return;
996 }
997
998 /* If we have already detected a LOST event, then don't try
999 * to do any further processing.
1000 */
1001 if ((m_detected & wxSOCKET_LOST_FLAG) != 0)
1002 {
1003 m_establishing = false;
1004
1005 OnStateChange(wxSOCKET_LOST);
1006 return;
1007 }
1008
1009 int num = recv(m_fd, &c, 1, MSG_PEEK | GSOCKET_MSG_NOSIGNAL);
1010
1011 if (num > 0)
1012 {
1013 OnStateChange(wxSOCKET_INPUT);
1014 }
1015 else
1016 {
1017 if (m_server && m_stream)
1018 {
1019 OnStateChange(wxSOCKET_CONNECTION);
1020 }
1021 else if (num == 0)
1022 {
1023 if (m_stream)
1024 {
1025 /* graceful shutdown */
1026 OnStateChange(wxSOCKET_LOST);
1027 }
1028 else
1029 {
1030 /* Empty datagram received */
1031 OnStateChange(wxSOCKET_INPUT);
1032 }
1033 }
1034 else
1035 {
1036 /* Do not throw a lost event in cases where the socket isn't really lost */
1037 if ((errno == EWOULDBLOCK) || (errno == EAGAIN) || (errno == EINTR))
1038 {
1039 OnStateChange(wxSOCKET_INPUT);
1040 }
1041 else
1042 {
1043 OnStateChange(wxSOCKET_LOST);
1044 }
1045 }
1046 }
1047 }
1048
1049 void wxSocketImplUnix::Detected_Write()
1050 {
1051 /* If we have already detected a LOST event, then don't try
1052 * to do any further processing.
1053 */
1054 if ((m_detected & wxSOCKET_LOST_FLAG) != 0)
1055 {
1056 m_establishing = false;
1057
1058 OnStateChange(wxSOCKET_LOST);
1059 return;
1060 }
1061
1062 if (m_establishing && !m_server)
1063 {
1064 int error;
1065 SOCKOPTLEN_T len = sizeof(error);
1066
1067 m_establishing = false;
1068
1069 getsockopt(m_fd, SOL_SOCKET, SO_ERROR, (char*)&error, &len);
1070
1071 if (error)
1072 {
1073 OnStateChange(wxSOCKET_LOST);
1074 }
1075 else
1076 {
1077 OnStateChange(wxSOCKET_CONNECTION);
1078 /* We have to fire this event by hand because CONNECTION (for clients)
1079 * and OUTPUT are internally the same and we just disabled CONNECTION
1080 * events with the above macro.
1081 */
1082 OnStateChange(wxSOCKET_OUTPUT);
1083 }
1084 }
1085 else
1086 {
1087 OnStateChange(wxSOCKET_OUTPUT);
1088 }
1089 }
1090
1091 /*
1092 * -------------------------------------------------------------------------
1093 * GAddress
1094 * -------------------------------------------------------------------------
1095 */
1096
1097 /* CHECK_ADDRESS verifies that the current address family is either
1098 * wxSOCKET_NOFAMILY or wxSOCKET_*family*, and if it is wxSOCKET_NOFAMILY, it
1099 * initalizes it to be a wxSOCKET_*family*. In other cases, it returns
1100 * an appropiate error code.
1101 *
1102 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
1103 */
1104 #define CHECK_ADDRESS(address, family) \
1105 { \
1106 if (address->m_family == wxSOCKET_NOFAMILY) \
1107 if (_GAddress_Init_##family(address) != wxSOCKET_NOERROR) \
1108 return address->m_error; \
1109 if (address->m_family != wxSOCKET_##family) \
1110 { \
1111 address->m_error = wxSOCKET_INVADDR; \
1112 return wxSOCKET_INVADDR; \
1113 } \
1114 }
1115
1116 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
1117 { \
1118 if (address->m_family == wxSOCKET_NOFAMILY) \
1119 if (_GAddress_Init_##family(address) != wxSOCKET_NOERROR) \
1120 return retval; \
1121 if (address->m_family != wxSOCKET_##family) \
1122 { \
1123 address->m_error = wxSOCKET_INVADDR; \
1124 return retval; \
1125 } \
1126 }
1127
1128
1129 GAddress *GAddress_new(void)
1130 {
1131 GAddress *address;
1132
1133 if ((address = (GAddress *) malloc(sizeof(GAddress))) == NULL)
1134 return NULL;
1135
1136 address->m_family = wxSOCKET_NOFAMILY;
1137 address->m_addr = NULL;
1138 address->m_len = 0;
1139
1140 return address;
1141 }
1142
1143 GAddress *GAddress_copy(GAddress *address)
1144 {
1145 GAddress *addr2;
1146
1147 assert(address != NULL);
1148
1149 if ((addr2 = (GAddress *) malloc(sizeof(GAddress))) == NULL)
1150 return NULL;
1151
1152 memcpy(addr2, address, sizeof(GAddress));
1153
1154 if (address->m_addr && address->m_len > 0)
1155 {
1156 addr2->m_addr = (struct sockaddr *)malloc(addr2->m_len);
1157 if (addr2->m_addr == NULL)
1158 {
1159 free(addr2);
1160 return NULL;
1161 }
1162 memcpy(addr2->m_addr, address->m_addr, addr2->m_len);
1163 }
1164
1165 return addr2;
1166 }
1167
1168 void GAddress_destroy(GAddress *address)
1169 {
1170 assert(address != NULL);
1171
1172 if (address->m_addr)
1173 free(address->m_addr);
1174
1175 free(address);
1176 }
1177
1178 void GAddress_SetFamily(GAddress *address, GAddressType type)
1179 {
1180 assert(address != NULL);
1181
1182 address->m_family = type;
1183 }
1184
1185 GAddressType GAddress_GetFamily(GAddress *address)
1186 {
1187 assert(address != NULL);
1188
1189 return address->m_family;
1190 }
1191
1192 wxSocketError _GAddress_translate_from(GAddress *address,
1193 struct sockaddr *addr, int len)
1194 {
1195 address->m_realfamily = addr->sa_family;
1196 switch (addr->sa_family)
1197 {
1198 case AF_INET:
1199 address->m_family = wxSOCKET_INET;
1200 break;
1201 case AF_UNIX:
1202 address->m_family = wxSOCKET_UNIX;
1203 break;
1204 #if wxUSE_IPV6
1205 case AF_INET6:
1206 address->m_family = wxSOCKET_INET6;
1207 break;
1208 #endif // wxUSE_IPV6
1209 default:
1210 {
1211 address->m_error = wxSOCKET_INVOP;
1212 return wxSOCKET_INVOP;
1213 }
1214 }
1215
1216 if (address->m_addr)
1217 free(address->m_addr);
1218
1219 address->m_len = len;
1220 address->m_addr = (struct sockaddr *)malloc(len);
1221
1222 if (address->m_addr == NULL)
1223 {
1224 address->m_error = wxSOCKET_MEMERR;
1225 return wxSOCKET_MEMERR;
1226 }
1227
1228 memcpy(address->m_addr, addr, len);
1229
1230 return wxSOCKET_NOERROR;
1231 }
1232
1233 wxSocketError _GAddress_translate_to(GAddress *address,
1234 struct sockaddr **addr, int *len)
1235 {
1236 if (!address->m_addr)
1237 {
1238 address->m_error = wxSOCKET_INVADDR;
1239 return wxSOCKET_INVADDR;
1240 }
1241
1242 *len = address->m_len;
1243 *addr = (struct sockaddr *)malloc(address->m_len);
1244 if (*addr == NULL)
1245 {
1246 address->m_error = wxSOCKET_MEMERR;
1247 return wxSOCKET_MEMERR;
1248 }
1249
1250 memcpy(*addr, address->m_addr, address->m_len);
1251 return wxSOCKET_NOERROR;
1252 }
1253
1254 /*
1255 * -------------------------------------------------------------------------
1256 * Internet address family
1257 * -------------------------------------------------------------------------
1258 */
1259
1260 wxSocketError _GAddress_Init_INET(GAddress *address)
1261 {
1262 address->m_len = sizeof(struct sockaddr_in);
1263 address->m_addr = (struct sockaddr *) malloc(address->m_len);
1264 if (address->m_addr == NULL)
1265 {
1266 address->m_error = wxSOCKET_MEMERR;
1267 return wxSOCKET_MEMERR;
1268 }
1269
1270 address->m_family = wxSOCKET_INET;
1271 address->m_realfamily = PF_INET;
1272 ((struct sockaddr_in *)address->m_addr)->sin_family = AF_INET;
1273 ((struct sockaddr_in *)address->m_addr)->sin_addr.s_addr = INADDR_ANY;
1274
1275 return wxSOCKET_NOERROR;
1276 }
1277
1278 wxSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname)
1279 {
1280 struct hostent *he;
1281 struct in_addr *addr;
1282
1283 assert(address != NULL);
1284
1285 CHECK_ADDRESS(address, INET);
1286
1287 addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr);
1288
1289 /* If it is a numeric host name, convert it now */
1290 #if defined(HAVE_INET_ATON)
1291 if (inet_aton(hostname, addr) == 0)
1292 {
1293 #elif defined(HAVE_INET_ADDR)
1294 if ( (addr->s_addr = inet_addr(hostname)) == (unsigned)-1 )
1295 {
1296 #else
1297 /* Use gethostbyname by default */
1298 #ifndef __WXMAC__
1299 int val = 1; /* VA doesn't like constants in conditional expressions */
1300 if (val)
1301 #endif
1302 {
1303 #endif
1304 struct in_addr *array_addr;
1305
1306 /* It is a real name, we solve it */
1307 struct hostent h;
1308 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
1309 struct hostent_data buffer;
1310 #else
1311 char buffer[1024];
1312 #endif
1313 int err;
1314 he = wxGethostbyname_r(hostname, &h, (void*)&buffer, sizeof(buffer), &err);
1315 if (he == NULL)
1316 {
1317 /* Reset to invalid address */
1318 addr->s_addr = INADDR_NONE;
1319 address->m_error = wxSOCKET_NOHOST;
1320 return wxSOCKET_NOHOST;
1321 }
1322
1323 array_addr = (struct in_addr *) *(he->h_addr_list);
1324 addr->s_addr = array_addr[0].s_addr;
1325 }
1326
1327 return wxSOCKET_NOERROR;
1328 }
1329
1330
1331 wxSocketError GAddress_INET_SetBroadcastAddress(GAddress *address)
1332 {
1333 return GAddress_INET_SetHostAddress(address, INADDR_BROADCAST);
1334 }
1335
1336 wxSocketError GAddress_INET_SetAnyAddress(GAddress *address)
1337 {
1338 return GAddress_INET_SetHostAddress(address, INADDR_ANY);
1339 }
1340
1341 wxSocketError GAddress_INET_SetHostAddress(GAddress *address,
1342 unsigned long hostaddr)
1343 {
1344 struct in_addr *addr;
1345
1346 assert(address != NULL);
1347
1348 CHECK_ADDRESS(address, INET);
1349
1350 addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr);
1351 addr->s_addr = htonl(hostaddr);
1352
1353 return wxSOCKET_NOERROR;
1354 }
1355
1356 wxSocketError GAddress_INET_SetPortName(GAddress *address, const char *port,
1357 const char *protocol)
1358 {
1359 struct servent *se;
1360 struct sockaddr_in *addr;
1361
1362 assert(address != NULL);
1363 CHECK_ADDRESS(address, INET);
1364
1365 if (!port)
1366 {
1367 address->m_error = wxSOCKET_INVPORT;
1368 return wxSOCKET_INVPORT;
1369 }
1370
1371 #if defined(HAVE_FUNC_GETSERVBYNAME_R_4)
1372 struct servent_data buffer;
1373 #else
1374 char buffer[1024];
1375 #endif
1376 struct servent serv;
1377 se = wxGetservbyname_r(port, protocol, &serv,
1378 (void*)&buffer, sizeof(buffer));
1379 if (!se)
1380 {
1381 /* the cast to int suppresses compiler warnings about subscript having the
1382 type char */
1383 if (isdigit((int)port[0]))
1384 {
1385 int port_int;
1386
1387 port_int = atoi(port);
1388 addr = (struct sockaddr_in *)address->m_addr;
1389 addr->sin_port = htons(port_int);
1390 return wxSOCKET_NOERROR;
1391 }
1392
1393 address->m_error = wxSOCKET_INVPORT;
1394 return wxSOCKET_INVPORT;
1395 }
1396
1397 addr = (struct sockaddr_in *)address->m_addr;
1398 addr->sin_port = se->s_port;
1399
1400 return wxSOCKET_NOERROR;
1401 }
1402
1403 wxSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port)
1404 {
1405 struct sockaddr_in *addr;
1406
1407 assert(address != NULL);
1408 CHECK_ADDRESS(address, INET);
1409
1410 addr = (struct sockaddr_in *)address->m_addr;
1411 addr->sin_port = htons(port);
1412
1413 return wxSOCKET_NOERROR;
1414 }
1415
1416 wxSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname, size_t sbuf)
1417 {
1418 struct hostent *he;
1419 char *addr_buf;
1420 struct sockaddr_in *addr;
1421
1422 assert(address != NULL);
1423 CHECK_ADDRESS(address, INET);
1424
1425 addr = (struct sockaddr_in *)address->m_addr;
1426 addr_buf = (char *)&(addr->sin_addr);
1427
1428 struct hostent temphost;
1429 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
1430 struct hostent_data buffer;
1431 #else
1432 char buffer[1024];
1433 #endif
1434 int err;
1435 he = wxGethostbyaddr_r(addr_buf, sizeof(addr->sin_addr), AF_INET, &temphost,
1436 (void*)&buffer, sizeof(buffer), &err);
1437 if (he == NULL)
1438 {
1439 address->m_error = wxSOCKET_NOHOST;
1440 return wxSOCKET_NOHOST;
1441 }
1442
1443 strncpy(hostname, he->h_name, sbuf);
1444
1445 return wxSOCKET_NOERROR;
1446 }
1447
1448 unsigned long GAddress_INET_GetHostAddress(GAddress *address)
1449 {
1450 struct sockaddr_in *addr;
1451
1452 assert(address != NULL);
1453 CHECK_ADDRESS_RETVAL(address, INET, 0);
1454
1455 addr = (struct sockaddr_in *)address->m_addr;
1456
1457 return ntohl(addr->sin_addr.s_addr);
1458 }
1459
1460 unsigned short GAddress_INET_GetPort(GAddress *address)
1461 {
1462 struct sockaddr_in *addr;
1463
1464 assert(address != NULL);
1465 CHECK_ADDRESS_RETVAL(address, INET, 0);
1466
1467 addr = (struct sockaddr_in *)address->m_addr;
1468 return ntohs(addr->sin_port);
1469 }
1470
1471 #if wxUSE_IPV6
1472 /*
1473 * -------------------------------------------------------------------------
1474 * Internet IPv6 address family
1475 * -------------------------------------------------------------------------
1476 */
1477
1478 wxSocketError _GAddress_Init_INET6(GAddress *address)
1479 {
1480 struct in6_addr any_address = IN6ADDR_ANY_INIT;
1481 address->m_len = sizeof(struct sockaddr_in6);
1482 address->m_addr = (struct sockaddr *) malloc(address->m_len);
1483 if (address->m_addr == NULL)
1484 {
1485 address->m_error = wxSOCKET_MEMERR;
1486 return wxSOCKET_MEMERR;
1487 }
1488 memset(address->m_addr,0,address->m_len);
1489
1490 address->m_family = wxSOCKET_INET6;
1491 address->m_realfamily = AF_INET6;
1492 ((struct sockaddr_in6 *)address->m_addr)->sin6_family = AF_INET6;
1493 ((struct sockaddr_in6 *)address->m_addr)->sin6_addr = any_address;
1494
1495 return wxSOCKET_NOERROR;
1496 }
1497
1498 wxSocketError GAddress_INET6_SetHostName(GAddress *address, const char *hostname)
1499 {
1500 assert(address != NULL);
1501 CHECK_ADDRESS(address, INET6);
1502
1503 addrinfo hints;
1504 memset( & hints, 0, sizeof( hints ) );
1505 hints.ai_family = AF_INET6;
1506 addrinfo * info = 0;
1507 if ( getaddrinfo( hostname, "0", & hints, & info ) || ! info )
1508 {
1509 address->m_error = wxSOCKET_NOHOST;
1510 return wxSOCKET_NOHOST;
1511 }
1512
1513 memcpy( address->m_addr, info->ai_addr, info->ai_addrlen );
1514 freeaddrinfo( info );
1515 return wxSOCKET_NOERROR;
1516 }
1517
1518 wxSocketError GAddress_INET6_SetAnyAddress(GAddress *address)
1519 {
1520 assert(address != NULL);
1521
1522 CHECK_ADDRESS(address, INET6);
1523
1524 struct in6_addr addr;
1525 memset( & addr, 0, sizeof( addr ) );
1526 return GAddress_INET6_SetHostAddress(address, addr);
1527 }
1528 wxSocketError GAddress_INET6_SetHostAddress(GAddress *address,
1529 struct in6_addr hostaddr)
1530 {
1531 assert(address != NULL);
1532
1533 CHECK_ADDRESS(address, INET6);
1534
1535 ((struct sockaddr_in6 *)address->m_addr)->sin6_addr = hostaddr;
1536
1537 return wxSOCKET_NOERROR;
1538 }
1539
1540 wxSocketError GAddress_INET6_SetPortName(GAddress *address, const char *port,
1541 const char *protocol)
1542 {
1543 struct servent *se;
1544 struct sockaddr_in6 *addr;
1545
1546 assert(address != NULL);
1547 CHECK_ADDRESS(address, INET6);
1548
1549 if (!port)
1550 {
1551 address->m_error = wxSOCKET_INVPORT;
1552 return wxSOCKET_INVPORT;
1553 }
1554
1555 se = getservbyname(port, protocol);
1556 if (!se)
1557 {
1558 if (isdigit(port[0]))
1559 {
1560 int port_int;
1561
1562 port_int = atoi(port);
1563 addr = (struct sockaddr_in6 *)address->m_addr;
1564 addr->sin6_port = htons((u_short) port_int);
1565 return wxSOCKET_NOERROR;
1566 }
1567
1568 address->m_error = wxSOCKET_INVPORT;
1569 return wxSOCKET_INVPORT;
1570 }
1571
1572 addr = (struct sockaddr_in6 *)address->m_addr;
1573 addr->sin6_port = se->s_port;
1574
1575 return wxSOCKET_NOERROR;
1576 }
1577
1578 wxSocketError GAddress_INET6_SetPort(GAddress *address, unsigned short port)
1579 {
1580 struct sockaddr_in6 *addr;
1581
1582 assert(address != NULL);
1583 CHECK_ADDRESS(address, INET6);
1584
1585 addr = (struct sockaddr_in6 *)address->m_addr;
1586 addr->sin6_port = htons(port);
1587
1588 return wxSOCKET_NOERROR;
1589 }
1590
1591 wxSocketError GAddress_INET6_GetHostName(GAddress *address, char *hostname, size_t sbuf)
1592 {
1593 struct hostent *he;
1594 char *addr_buf;
1595 struct sockaddr_in6 *addr;
1596
1597 assert(address != NULL);
1598 CHECK_ADDRESS(address, INET6);
1599
1600 addr = (struct sockaddr_in6 *)address->m_addr;
1601 addr_buf = (char *)&(addr->sin6_addr);
1602
1603 he = gethostbyaddr(addr_buf, sizeof(addr->sin6_addr), AF_INET6);
1604 if (he == NULL)
1605 {
1606 address->m_error = wxSOCKET_NOHOST;
1607 return wxSOCKET_NOHOST;
1608 }
1609
1610 strncpy(hostname, he->h_name, sbuf);
1611
1612 return wxSOCKET_NOERROR;
1613 }
1614
1615 wxSocketError GAddress_INET6_GetHostAddress(GAddress *address,struct in6_addr *hostaddr)
1616 {
1617 assert(address != NULL);
1618 assert(hostaddr != NULL);
1619 CHECK_ADDRESS_RETVAL(address, INET6, wxSOCKET_INVADDR);
1620 *hostaddr = ( (struct sockaddr_in6 *)address->m_addr )->sin6_addr;
1621 return wxSOCKET_NOERROR;
1622 }
1623
1624 unsigned short GAddress_INET6_GetPort(GAddress *address)
1625 {
1626 assert(address != NULL);
1627 CHECK_ADDRESS_RETVAL(address, INET6, 0);
1628
1629 return ntohs( ((struct sockaddr_in6 *)address->m_addr)->sin6_port );
1630 }
1631
1632 #endif // wxUSE_IPV6
1633
1634 /*
1635 * -------------------------------------------------------------------------
1636 * Unix address family
1637 * -------------------------------------------------------------------------
1638 */
1639
1640 #ifndef __VISAGECPP__
1641 wxSocketError _GAddress_Init_UNIX(GAddress *address)
1642 {
1643 address->m_len = sizeof(struct sockaddr_un);
1644 address->m_addr = (struct sockaddr *)malloc(address->m_len);
1645 if (address->m_addr == NULL)
1646 {
1647 address->m_error = wxSOCKET_MEMERR;
1648 return wxSOCKET_MEMERR;
1649 }
1650
1651 address->m_family = wxSOCKET_UNIX;
1652 address->m_realfamily = PF_UNIX;
1653 ((struct sockaddr_un *)address->m_addr)->sun_family = AF_UNIX;
1654 ((struct sockaddr_un *)address->m_addr)->sun_path[0] = 0;
1655
1656 return wxSOCKET_NOERROR;
1657 }
1658
1659 #define UNIX_SOCK_PATHLEN (sizeof(addr->sun_path)/sizeof(addr->sun_path[0]))
1660
1661 wxSocketError GAddress_UNIX_SetPath(GAddress *address, const char *path)
1662 {
1663 struct sockaddr_un *addr;
1664
1665 assert(address != NULL);
1666
1667 CHECK_ADDRESS(address, UNIX);
1668
1669 addr = ((struct sockaddr_un *)address->m_addr);
1670 strncpy(addr->sun_path, path, UNIX_SOCK_PATHLEN);
1671 addr->sun_path[UNIX_SOCK_PATHLEN - 1] = '\0';
1672
1673 return wxSOCKET_NOERROR;
1674 }
1675
1676 wxSocketError GAddress_UNIX_GetPath(GAddress *address, char *path, size_t sbuf)
1677 {
1678 struct sockaddr_un *addr;
1679
1680 assert(address != NULL);
1681 CHECK_ADDRESS(address, UNIX);
1682
1683 addr = (struct sockaddr_un *)address->m_addr;
1684
1685 strncpy(path, addr->sun_path, sbuf);
1686
1687 return wxSOCKET_NOERROR;
1688 }
1689 #endif /* !defined(__VISAGECPP__) */
1690 #endif /* wxUSE_SOCKETS */