]> git.saurik.com Git - wxWidgets.git/blob - src/unix/sockunix.cpp
handle correctly never/always shown scrollbars in GetClientSize()
[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 /* static */
435 wxSocketImpl *wxSocketImpl::Create(wxSocketBase& wxsocket)
436 {
437 return new wxSocketImplUnix(wxsocket);
438 }
439
440
441 /*
442 * Disallow further read/write operations on this socket, close
443 * the fd and disable all callbacks.
444 */
445 void wxSocketImplUnix::Shutdown()
446 {
447 /* Don't allow events to fire after socket has been closed */
448 DisableEvents();
449
450 wxSocketImpl::Shutdown();
451 }
452
453 /*
454 * Waits for an incoming client connection. Returns a pointer to
455 * a wxSocketImplUnix object, or NULL if there was an error, in which case
456 * the last error field will be updated for the calling wxSocketImplUnix.
457 *
458 * Error codes (set in the calling wxSocketImplUnix)
459 * wxSOCKET_INVSOCK - the socket is not valid or not a server.
460 * wxSOCKET_TIMEDOUT - timeout, no incoming connections.
461 * wxSOCKET_WOULDBLOCK - the call would block and the socket is nonblocking.
462 * wxSOCKET_MEMERR - couldn't allocate memory.
463 * wxSOCKET_IOERR - low-level error.
464 */
465 wxSocketImpl *wxSocketImplUnix::WaitConnection(wxSocketBase& wxsocket)
466 {
467 wxSockAddr from;
468 WX_SOCKLEN_T fromlen = sizeof(from);
469 wxSocketImpl *connection;
470 wxSocketError err;
471 int arg = 1;
472
473 /* If the socket has already been created, we exit immediately */
474 if (m_fd == INVALID_SOCKET || !m_server)
475 {
476 m_error = wxSOCKET_INVSOCK;
477 return NULL;
478 }
479
480 /* Create a wxSocketImplUnix object for the new connection */
481 connection = wxSocketImplUnix::Create(wxsocket);
482
483 if (!connection)
484 {
485 m_error = wxSOCKET_MEMERR;
486 return NULL;
487 }
488
489 /* Wait for a connection (with timeout) */
490 if (Input_Timeout() == wxSOCKET_TIMEDOUT)
491 {
492 delete connection;
493 /* m_error set by Input_Timeout */
494 return NULL;
495 }
496
497 connection->m_fd = accept(m_fd, (sockaddr*)&from, (WX_SOCKLEN_T *) &fromlen);
498
499 /* Reenable CONNECTION events */
500 EnableEvent(wxSOCKET_CONNECTION);
501
502 if (connection->m_fd == INVALID_SOCKET)
503 {
504 if (errno == EWOULDBLOCK)
505 m_error = wxSOCKET_WOULDBLOCK;
506 else
507 m_error = wxSOCKET_IOERR;
508
509 delete connection;
510 return NULL;
511 }
512
513 /* Initialize all fields */
514 connection->m_server = false;
515 connection->m_stream = true;
516
517 /* Setup the peer address field */
518 connection->m_peer = GAddress_new();
519 if (!connection->m_peer)
520 {
521 delete connection;
522 m_error = wxSOCKET_MEMERR;
523 return NULL;
524 }
525
526 err = _GAddress_translate_from(connection->m_peer, (sockaddr*)&from, fromlen);
527 if (err != wxSOCKET_NOERROR)
528 {
529 delete connection;
530 m_error = err;
531 return NULL;
532 }
533
534 #if defined(__EMX__) || defined(__VISAGECPP__)
535 ioctl(connection->m_fd, FIONBIO, (char*)&arg, sizeof(arg));
536 #else
537 ioctl(connection->m_fd, FIONBIO, &arg);
538 #endif
539 if (m_use_events)
540 connection->Notify(true);
541
542 return connection;
543 }
544
545 void wxSocketImplUnix::Notify(bool flag)
546 {
547 if (flag == m_use_events)
548 return;
549 m_use_events = flag;
550 DoEnableEvents(flag);
551 }
552
553 void wxSocketImplUnix::DoEnableEvents(bool flag)
554 {
555 wxSocketManager * const manager = wxSocketManager::Get();
556 if ( flag )
557 {
558 manager->Install_Callback(this, wxSOCKET_INPUT);
559 manager->Install_Callback(this, wxSOCKET_OUTPUT);
560 }
561 else // off
562 {
563 manager->Uninstall_Callback(this, wxSOCKET_INPUT);
564 manager->Uninstall_Callback(this, wxSOCKET_OUTPUT);
565 }
566 }
567
568 wxSocketError wxSocketImplUnix::DoHandleConnect(int ret)
569 {
570 /* We only call EnableEvents() if we know we aren't shutting down the socket.
571 * NB: EnableEvents() needs to be called whether the socket is blocking or
572 * non-blocking, it just shouldn't be called prior to knowing there is a
573 * connection _if_ blocking sockets are being used.
574 * If connect above returns 0, we are already connected and need to make the
575 * call to EnableEvents() now.
576 */
577 if ( m_non_blocking || (ret == 0) )
578 EnableEvents();
579
580 if (ret == -1)
581 {
582 const int err = errno;
583
584 /* If connect failed with EINPROGRESS and the wxSocketImplUnix object
585 * is in blocking mode, we select() for the specified timeout
586 * checking for writability to see if the connection request
587 * completes.
588 */
589 if ((err == EINPROGRESS) && (!m_non_blocking))
590 {
591 if (Output_Timeout() == wxSOCKET_TIMEDOUT)
592 {
593 Close();
594 /* m_error is set in Output_Timeout */
595 return wxSOCKET_TIMEDOUT;
596 }
597 else
598 {
599 int error;
600 SOCKOPTLEN_T len = sizeof(error);
601
602 getsockopt(m_fd, SOL_SOCKET, SO_ERROR, (char*) &error, &len);
603 EnableEvents();
604
605 if (!error)
606 return wxSOCKET_NOERROR;
607 }
608 }
609
610 /* If connect failed with EINPROGRESS and the wxSocketImplUnix object
611 * is set to nonblocking, we set m_error to wxSOCKET_WOULDBLOCK
612 * (and return wxSOCKET_WOULDBLOCK) but we don't close the socket;
613 * this way if the connection completes, a wxSOCKET_CONNECTION
614 * event will be generated, if enabled.
615 */
616 if ((err == EINPROGRESS) && (m_non_blocking))
617 {
618 m_establishing = true;
619 m_error = wxSOCKET_WOULDBLOCK;
620 return wxSOCKET_WOULDBLOCK;
621 }
622
623 /* If connect failed with an error other than EINPROGRESS,
624 * then the call to Connect has failed.
625 */
626 Close();
627 m_error = wxSOCKET_IOERR;
628
629 return wxSOCKET_IOERR;
630 }
631
632 return wxSOCKET_NOERROR;
633 }
634
635 /* Generic IO */
636
637 /* Like recv(), send(), ... */
638 int wxSocketImplUnix::Read(char *buffer, int size)
639 {
640 int ret;
641
642 if (m_fd == INVALID_SOCKET || m_server)
643 {
644 m_error = wxSOCKET_INVSOCK;
645 return -1;
646 }
647
648 /* Disable events during query of socket status */
649 DisableEvent(wxSOCKET_INPUT);
650
651 /* If the socket is blocking, wait for data (with a timeout) */
652 if (Input_Timeout() == wxSOCKET_TIMEDOUT) {
653 m_error = wxSOCKET_TIMEDOUT;
654 /* Don't return here immediately, otherwise socket events would not be
655 * re-enabled! */
656 ret = -1;
657 }
658 else
659 {
660 /* Read the data */
661 if (m_stream)
662 ret = Recv_Stream(buffer, size);
663 else
664 ret = Recv_Dgram(buffer, size);
665
666 /*
667 * If recv returned zero for a TCP socket (if m_stream == NULL, it's an UDP
668 * socket and empty datagrams are possible), then the connection has been
669 * gracefully closed.
670 *
671 * Otherwise, recv has returned an error (-1), in which case we have lost
672 * the socket only if errno does _not_ indicate that there may be more data
673 * to read.
674 */
675 if ((ret == 0) && m_stream)
676 {
677 /* Make sure wxSOCKET_LOST event gets sent and shut down the socket */
678 if (m_use_events)
679 {
680 m_detected = wxSOCKET_LOST_FLAG;
681 OnReadWaiting();
682 return 0;
683 }
684 }
685 else if (ret == -1)
686 {
687 if ((errno == EWOULDBLOCK) || (errno == EAGAIN))
688 m_error = wxSOCKET_WOULDBLOCK;
689 else
690 m_error = wxSOCKET_IOERR;
691 }
692 }
693
694 /* Enable events again now that we are done processing */
695 EnableEvent(wxSOCKET_INPUT);
696
697 return ret;
698 }
699
700 int wxSocketImplUnix::Write(const char *buffer, int size)
701 {
702 int ret;
703
704 SOCKET_DEBUG(( "Write #1, size %d\n", size ));
705
706 if (m_fd == INVALID_SOCKET || m_server)
707 {
708 m_error = wxSOCKET_INVSOCK;
709 return -1;
710 }
711
712 SOCKET_DEBUG(( "Write #2, size %d\n", size ));
713
714 /* If the socket is blocking, wait for writability (with a timeout) */
715 if (Output_Timeout() == wxSOCKET_TIMEDOUT)
716 return -1;
717
718 SOCKET_DEBUG(( "Write #3, size %d\n", size ));
719
720 /* Write the data */
721 if (m_stream)
722 ret = Send_Stream(buffer, size);
723 else
724 ret = Send_Dgram(buffer, size);
725
726 SOCKET_DEBUG(( "Write #4, size %d\n", size ));
727
728 if (ret == -1)
729 {
730 if ((errno == EWOULDBLOCK) || (errno == EAGAIN))
731 {
732 m_error = wxSOCKET_WOULDBLOCK;
733 SOCKET_DEBUG(( "Write error WOULDBLOCK\n" ));
734 }
735 else
736 {
737 m_error = wxSOCKET_IOERR;
738 SOCKET_DEBUG(( "Write error IOERR\n" ));
739 }
740
741 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
742 * in MSW). Once the first OUTPUT event is received, users can assume
743 * that the socket is writable until a read operation fails. Only then
744 * will further OUTPUT events be posted.
745 */
746 EnableEvent(wxSOCKET_OUTPUT);
747
748 return -1;
749 }
750
751 SOCKET_DEBUG(( "Write #5, size %d ret %d\n", size, ret ));
752
753 return ret;
754 }
755
756 /* Flags */
757
758 void wxSocketImplUnix::EnableEvent(wxSocketNotify event)
759 {
760 if (m_use_events)
761 {
762 m_detected &= ~(1 << event);
763 wxSocketManager::Get()->Install_Callback(this, event);
764 }
765 }
766
767 void wxSocketImplUnix::DisableEvent(wxSocketNotify event)
768 {
769 if (m_use_events)
770 {
771 m_detected |= (1 << event);
772 wxSocketManager::Get()->Uninstall_Callback(this, event);
773 }
774 }
775
776 /*
777 * For blocking sockets, wait until data is available or
778 * until timeout ellapses.
779 */
780 wxSocketError wxSocketImplUnix::Input_Timeout()
781 {
782 fd_set readfds;
783 int ret;
784
785 // Linux select() will overwrite the struct on return so make a copy
786 struct timeval tv = m_timeout;
787
788 if (!m_non_blocking)
789 {
790 wxFD_ZERO(&readfds);
791 wxFD_SET(m_fd, &readfds);
792 ret = select(m_fd + 1, &readfds, NULL, NULL, &tv);
793 if (ret == 0)
794 {
795 SOCKET_DEBUG(( "Input_Timeout, select returned 0\n" ));
796 m_error = wxSOCKET_TIMEDOUT;
797 return wxSOCKET_TIMEDOUT;
798 }
799
800 if (ret == -1)
801 {
802 SOCKET_DEBUG(( "Input_Timeout, select returned -1\n" ));
803 if (errno == EBADF) { SOCKET_DEBUG(( "Invalid file descriptor\n" )); }
804 if (errno == EINTR) { SOCKET_DEBUG(( "A non blocked signal was caught\n" )); }
805 if (errno == EINVAL) { SOCKET_DEBUG(( "The highest number descriptor is negative\n" )); }
806 if (errno == ENOMEM) { SOCKET_DEBUG(( "Not enough memory\n" )); }
807 m_error = wxSOCKET_TIMEDOUT;
808 return wxSOCKET_TIMEDOUT;
809 }
810 }
811
812 return wxSOCKET_NOERROR;
813 }
814
815 /*
816 * For blocking sockets, wait until data can be sent without
817 * blocking or until timeout ellapses.
818 */
819 wxSocketError wxSocketImplUnix::Output_Timeout()
820 {
821 fd_set writefds;
822 int ret;
823
824 // Linux select() will overwrite the struct on return so make a copy
825 struct timeval tv = m_timeout;
826
827 SOCKET_DEBUG( ("m_non_blocking has: %d\n", (int)m_non_blocking) );
828
829 if (!m_non_blocking)
830 {
831 wxFD_ZERO(&writefds);
832 wxFD_SET(m_fd, &writefds);
833 ret = select(m_fd + 1, NULL, &writefds, NULL, &tv);
834 if (ret == 0)
835 {
836 SOCKET_DEBUG(( "Output_Timeout, select returned 0\n" ));
837 m_error = wxSOCKET_TIMEDOUT;
838 return wxSOCKET_TIMEDOUT;
839 }
840
841 if (ret == -1)
842 {
843 SOCKET_DEBUG(( "Output_Timeout, select returned -1\n" ));
844 if (errno == EBADF) { SOCKET_DEBUG(( "Invalid file descriptor\n" )); }
845 if (errno == EINTR) { SOCKET_DEBUG(( "A non blocked signal was caught\n" )); }
846 if (errno == EINVAL) { SOCKET_DEBUG(( "The highest number descriptor is negative\n" )); }
847 if (errno == ENOMEM) { SOCKET_DEBUG(( "Not enough memory\n" )); }
848 m_error = wxSOCKET_TIMEDOUT;
849 return wxSOCKET_TIMEDOUT;
850 }
851
852 if ( ! wxFD_ISSET(m_fd, &writefds) )
853 {
854 SOCKET_DEBUG(( "Output_Timeout is buggy!\n" ));
855 }
856 else
857 {
858 SOCKET_DEBUG(( "Output_Timeout seems correct\n" ));
859 }
860 }
861 else
862 {
863 SOCKET_DEBUG(( "Output_Timeout, didn't try select!\n" ));
864 }
865
866 return wxSOCKET_NOERROR;
867 }
868
869 int wxSocketImplUnix::Recv_Stream(char *buffer, int size)
870 {
871 int ret;
872 do
873 {
874 ret = recv(m_fd, buffer, size, GSOCKET_MSG_NOSIGNAL);
875 }
876 while (ret == -1 && errno == EINTR); /* Loop until not interrupted */
877
878 return ret;
879 }
880
881 int wxSocketImplUnix::Recv_Dgram(char *buffer, int size)
882 {
883 wxSockAddr from;
884 WX_SOCKLEN_T fromlen = sizeof(from);
885 int ret;
886 wxSocketError err;
887
888 fromlen = sizeof(from);
889
890 do
891 {
892 ret = recvfrom(m_fd, buffer, size, 0, (sockaddr*)&from, (WX_SOCKLEN_T *) &fromlen);
893 }
894 while (ret == -1 && errno == EINTR); /* Loop until not interrupted */
895
896 if (ret == -1)
897 return -1;
898
899 /* Translate a system address into a wxSocketImplUnix address */
900 if (!m_peer)
901 {
902 m_peer = GAddress_new();
903 if (!m_peer)
904 {
905 m_error = wxSOCKET_MEMERR;
906 return -1;
907 }
908 }
909
910 err = _GAddress_translate_from(m_peer, (sockaddr*)&from, fromlen);
911 if (err != wxSOCKET_NOERROR)
912 {
913 GAddress_destroy(m_peer);
914 m_peer = NULL;
915 m_error = err;
916 return -1;
917 }
918
919 return ret;
920 }
921
922 int wxSocketImplUnix::Send_Stream(const char *buffer, int size)
923 {
924 int ret;
925
926 MASK_SIGNAL();
927
928 do
929 {
930 ret = send(m_fd, (char *)buffer, size, GSOCKET_MSG_NOSIGNAL);
931 }
932 while (ret == -1 && errno == EINTR); /* Loop until not interrupted */
933
934 UNMASK_SIGNAL();
935
936 return ret;
937 }
938
939 int wxSocketImplUnix::Send_Dgram(const char *buffer, int size)
940 {
941 struct sockaddr *addr;
942 int len, ret;
943 wxSocketError err;
944
945 if (!m_peer)
946 {
947 m_error = wxSOCKET_INVADDR;
948 return -1;
949 }
950
951 err = _GAddress_translate_to(m_peer, &addr, &len);
952 if (err != wxSOCKET_NOERROR)
953 {
954 m_error = err;
955 return -1;
956 }
957
958 MASK_SIGNAL();
959
960 do
961 {
962 ret = sendto(m_fd, (char *)buffer, size, 0, addr, len);
963 }
964 while (ret == -1 && errno == EINTR); /* Loop until not interrupted */
965
966 UNMASK_SIGNAL();
967
968 /* Frees memory allocated from _GAddress_translate_to */
969 free(addr);
970
971 return ret;
972 }
973
974 void wxSocketImplUnix::OnStateChange(wxSocketNotify event)
975 {
976 DisableEvent(event);
977 NotifyOnStateChange(event);
978
979 if ( event == wxSOCKET_LOST )
980 Shutdown();
981 }
982
983 void wxSocketImplUnix::OnReadWaiting()
984 {
985 char c;
986
987 if (m_fd == INVALID_SOCKET)
988 {
989 return;
990 }
991
992 /* If we have already detected a LOST event, then don't try
993 * to do any further processing.
994 */
995 if ((m_detected & wxSOCKET_LOST_FLAG) != 0)
996 {
997 m_establishing = false;
998
999 OnStateChange(wxSOCKET_LOST);
1000 return;
1001 }
1002
1003 int num = recv(m_fd, &c, 1, MSG_PEEK | GSOCKET_MSG_NOSIGNAL);
1004
1005 if (num > 0)
1006 {
1007 OnStateChange(wxSOCKET_INPUT);
1008 }
1009 else
1010 {
1011 if (m_server && m_stream)
1012 {
1013 OnStateChange(wxSOCKET_CONNECTION);
1014 }
1015 else if (num == 0)
1016 {
1017 if (m_stream)
1018 {
1019 /* graceful shutdown */
1020 OnStateChange(wxSOCKET_LOST);
1021 }
1022 else
1023 {
1024 /* Empty datagram received */
1025 OnStateChange(wxSOCKET_INPUT);
1026 }
1027 }
1028 else
1029 {
1030 /* Do not throw a lost event in cases where the socket isn't really lost */
1031 if ((errno == EWOULDBLOCK) || (errno == EAGAIN) || (errno == EINTR))
1032 {
1033 OnStateChange(wxSOCKET_INPUT);
1034 }
1035 else
1036 {
1037 OnStateChange(wxSOCKET_LOST);
1038 }
1039 }
1040 }
1041 }
1042
1043 void wxSocketImplUnix::OnWriteWaiting()
1044 {
1045 /* If we have already detected a LOST event, then don't try
1046 * to do any further processing.
1047 */
1048 if ((m_detected & wxSOCKET_LOST_FLAG) != 0)
1049 {
1050 m_establishing = false;
1051
1052 OnStateChange(wxSOCKET_LOST);
1053 return;
1054 }
1055
1056 if (m_establishing && !m_server)
1057 {
1058 int error;
1059 SOCKOPTLEN_T len = sizeof(error);
1060
1061 m_establishing = false;
1062
1063 getsockopt(m_fd, SOL_SOCKET, SO_ERROR, (char*)&error, &len);
1064
1065 if (error)
1066 {
1067 OnStateChange(wxSOCKET_LOST);
1068 }
1069 else
1070 {
1071 OnStateChange(wxSOCKET_CONNECTION);
1072 /* We have to fire this event by hand because CONNECTION (for clients)
1073 * and OUTPUT are internally the same and we just disabled CONNECTION
1074 * events with the above macro.
1075 */
1076 OnStateChange(wxSOCKET_OUTPUT);
1077 }
1078 }
1079 else
1080 {
1081 OnStateChange(wxSOCKET_OUTPUT);
1082 }
1083 }
1084
1085 void wxSocketImplUnix::OnExceptionWaiting()
1086 {
1087 wxFAIL_MSG( "not supposed to be called" );
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
1690 #endif /* wxUSE_SOCKETS */