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