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