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