]> git.saurik.com Git - wxWidgets.git/blob - src/unix/gsocket.cpp
56cd0661191fbc16b7718994a7ec399540a4d646
[wxWidgets.git] / src / unix / gsocket.cpp
1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket) for WX
3 * Name: gsocket.c
4 * Copyright: (c) Guilhem Lavaux
5 * Licence: wxWindows Licence
6 * Authors: David Elliott (C++ conversion, maintainer)
7 * Guilhem Lavaux,
8 * Guillermo Rodriguez Garcia <guille@iies.es>
9 * Purpose: GSocket main Unix and OS/2 file
10 * Licence: The wxWindows licence
11 * CVSID: $Id$
12 * -------------------------------------------------------------------------
13 */
14
15 #if defined(__WATCOMC__)
16 #include "wx/wxprec.h"
17 #include <errno.h>
18 #include <nerrno.h>
19 #endif
20
21 #ifndef __GSOCKET_STANDALONE__
22 #include "wx/defs.h"
23 #include "wx/private/gsocketiohandler.h"
24 #include "wx/thread.h"
25 #endif
26
27 #if defined(__VISAGECPP__)
28 #define BSD_SELECT /* use Berkeley Sockets select */
29 #endif
30
31 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
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 /*
139 * MSW defines this, Unices don't.
140 */
141 #ifndef INVALID_SOCKET
142 #define INVALID_SOCKET -1
143 #endif
144
145 /* UnixWare reportedly needs this for FIONBIO definition */
146 #ifdef __UNIXWARE__
147 #include <sys/filio.h>
148 #endif
149
150 /*
151 * INADDR_BROADCAST is identical to INADDR_NONE which is not defined
152 * on all systems. INADDR_BROADCAST should be fine to indicate an error.
153 */
154 #ifndef INADDR_NONE
155 #define INADDR_NONE INADDR_BROADCAST
156 #endif
157
158 #if defined(__VISAGECPP__) || defined(__WATCOMC__)
159
160 #define MASK_SIGNAL() {
161 #define UNMASK_SIGNAL() }
162
163 #else
164 extern "C" { typedef void (*wxSigHandler)(int); }
165
166 #define MASK_SIGNAL() \
167 { \
168 wxSigHandler old_handler = signal(SIGPIPE, SIG_IGN);
169
170 #define UNMASK_SIGNAL() \
171 signal(SIGPIPE, old_handler); \
172 }
173
174 #endif
175
176 /* If a SIGPIPE is issued by a socket call on a remotely closed socket,
177 the program will "crash" unless it explicitly handles the SIGPIPE.
178 By using MSG_NOSIGNAL, the SIGPIPE is suppressed. Later, we will
179 use SO_NOSIGPIPE (if available), the BSD equivalent. */
180 #ifdef MSG_NOSIGNAL
181 # define GSOCKET_MSG_NOSIGNAL MSG_NOSIGNAL
182 #else /* MSG_NOSIGNAL not available (FreeBSD including OS X) */
183 # define GSOCKET_MSG_NOSIGNAL 0
184 #endif /* MSG_NOSIGNAL */
185
186 #ifndef __GSOCKET_STANDALONE__
187 # include "wx/unix/gsockunx.h"
188 # include "wx/unix/private.h"
189 # include "wx/gsocket.h"
190 #if wxUSE_THREADS && (defined(HAVE_GETHOSTBYNAME) || defined(HAVE_GETSERVBYNAME))
191 # include "wx/thread.h"
192 #endif
193 #else
194 # include "gsockunx.h"
195 # include "gsocket.h"
196 # ifndef WXUNUSED
197 # define WXUNUSED(x)
198 # endif
199 #endif /* __GSOCKET_STANDALONE__ */
200
201 #if defined(HAVE_GETHOSTBYNAME)
202 static struct hostent * deepCopyHostent(struct hostent *h,
203 const struct hostent *he,
204 char *buffer, int size, int *err)
205 {
206 /* copy old structure */
207 memcpy(h, he, sizeof(struct hostent));
208
209 /* copy name */
210 int len = strlen(h->h_name);
211 if (len > size)
212 {
213 *err = ENOMEM;
214 return NULL;
215 }
216 memcpy(buffer, h->h_name, len);
217 buffer[len] = '\0';
218 h->h_name = buffer;
219
220 /* track position in the buffer */
221 int pos = len + 1;
222
223 /* reuse len to store address length */
224 len = h->h_length;
225
226 /* ensure pointer alignment */
227 unsigned int misalign = sizeof(char *) - pos%sizeof(char *);
228 if(misalign < sizeof(char *))
229 pos += misalign;
230
231 /* leave space for pointer list */
232 char **p = h->h_addr_list, **q;
233 char **h_addr_list = (char **)(buffer + pos);
234 while(*(p++) != 0)
235 pos += sizeof(char *);
236
237 /* copy addresses and fill new pointer list */
238 for (p = h->h_addr_list, q = h_addr_list; *p != 0; p++, q++)
239 {
240 if (size < pos + len)
241 {
242 *err = ENOMEM;
243 return NULL;
244 }
245 memcpy(buffer + pos, *p, len); /* copy content */
246 *q = buffer + pos; /* set copied pointer to copied content */
247 pos += len;
248 }
249 *++q = 0; /* null terminate the pointer list */
250 h->h_addr_list = h_addr_list; /* copy pointer to pointers */
251
252 /* ensure word alignment of pointers */
253 misalign = sizeof(char *) - pos%sizeof(char *);
254 if(misalign < sizeof(char *))
255 pos += misalign;
256
257 /* leave space for pointer list */
258 p = h->h_aliases;
259 char **h_aliases = (char **)(buffer + pos);
260 while(*(p++) != 0)
261 pos += sizeof(char *);
262
263 /* copy aliases and fill new pointer list */
264 for (p = h->h_aliases, q = h_aliases; *p != 0; p++, q++)
265 {
266 len = strlen(*p);
267 if (size <= pos + len)
268 {
269 *err = ENOMEM;
270 return NULL;
271 }
272 memcpy(buffer + pos, *p, len); /* copy content */
273 buffer[pos + len] = '\0';
274 *q = buffer + pos; /* set copied pointer to copied content */
275 pos += len + 1;
276 }
277 *++q = 0; /* null terminate the pointer list */
278 h->h_aliases = h_aliases; /* copy pointer to pointers */
279
280 return h;
281 }
282 #endif
283
284 #if defined(HAVE_GETHOSTBYNAME) && wxUSE_THREADS
285 static wxMutex nameLock;
286 #endif
287 struct hostent * wxGethostbyname_r(const char *hostname, struct hostent *h,
288 void *buffer, int size, int *err)
289
290 {
291 struct hostent *he = NULL;
292 *err = 0;
293 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_6)
294 if (gethostbyname_r(hostname, h, (char*)buffer, size, &he, err))
295 he = NULL;
296 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_5)
297 he = gethostbyname_r(hostname, h, (char*)buffer, size, err);
298 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
299 if (gethostbyname_r(hostname, h, (struct hostent_data*) buffer))
300 {
301 he = NULL;
302 *err = h_errno;
303 }
304 else
305 he = h;
306 #elif defined(HAVE_GETHOSTBYNAME)
307 #if wxUSE_THREADS
308 wxMutexLocker locker(nameLock);
309 #endif
310 he = gethostbyname(hostname);
311 if (!he)
312 *err = h_errno;
313 else
314 he = deepCopyHostent(h, he, (char*)buffer, size, err);
315 #endif
316 return he;
317 }
318
319 #if defined(HAVE_GETHOSTBYNAME) && wxUSE_THREADS
320 static wxMutex addrLock;
321 #endif
322 struct hostent * wxGethostbyaddr_r(const char *addr_buf, int buf_size,
323 int proto, struct hostent *h,
324 void *buffer, int size, int *err)
325 {
326 struct hostent *he = NULL;
327 *err = 0;
328 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_6)
329 if (gethostbyaddr_r(addr_buf, buf_size, proto, h,
330 (char*)buffer, size, &he, err))
331 he = NULL;
332 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_5)
333 he = gethostbyaddr_r(addr_buf, buf_size, proto, h, (char*)buffer, size, err);
334 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
335 if (gethostbyaddr_r(addr_buf, buf_size, proto, h,
336 (struct hostent_data*) buffer))
337 {
338 he = NULL;
339 *err = h_errno;
340 }
341 else
342 he = h;
343 #elif defined(HAVE_GETHOSTBYNAME)
344 #if wxUSE_THREADS
345 wxMutexLocker locker(addrLock);
346 #endif
347 he = gethostbyaddr(addr_buf, buf_size, proto);
348 if (!he)
349 *err = h_errno;
350 else
351 he = deepCopyHostent(h, he, (char*)buffer, size, err);
352 #endif
353 return he;
354 }
355
356 #if defined(HAVE_GETSERVBYNAME)
357 static struct servent * deepCopyServent(struct servent *s,
358 const struct servent *se,
359 char *buffer, int size)
360 {
361 /* copy plain old structure */
362 memcpy(s, se, sizeof(struct servent));
363
364 /* copy name */
365 int len = strlen(s->s_name);
366 if (len >= size)
367 {
368 return NULL;
369 }
370 memcpy(buffer, s->s_name, len);
371 buffer[len] = '\0';
372 s->s_name = buffer;
373
374 /* track position in the buffer */
375 int pos = len + 1;
376
377 /* copy protocol */
378 len = strlen(s->s_proto);
379 if (pos + len >= size)
380 {
381 return NULL;
382 }
383 memcpy(buffer + pos, s->s_proto, len);
384 buffer[pos + len] = '\0';
385 s->s_proto = buffer + pos;
386
387 /* track position in the buffer */
388 pos += len + 1;
389
390 /* ensure pointer alignment */
391 unsigned int misalign = sizeof(char *) - pos%sizeof(char *);
392 if(misalign < sizeof(char *))
393 pos += misalign;
394
395 /* leave space for pointer list */
396 char **p = s->s_aliases, **q;
397 char **s_aliases = (char **)(buffer + pos);
398 while(*(p++) != 0)
399 pos += sizeof(char *);
400
401 /* copy addresses and fill new pointer list */
402 for (p = s->s_aliases, q = s_aliases; *p != 0; p++, q++){
403 len = strlen(*p);
404 if (size <= pos + len)
405 {
406 return NULL;
407 }
408 memcpy(buffer + pos, *p, len); /* copy content */
409 buffer[pos + len] = '\0';
410 *q = buffer + pos; /* set copied pointer to copied content */
411 pos += len + 1;
412 }
413 *++q = 0; /* null terminate the pointer list */
414 s->s_aliases = s_aliases; /* copy pointer to pointers */
415 return s;
416 }
417 #endif
418
419 #if defined(HAVE_GETSERVBYNAME) && wxUSE_THREADS
420 static wxMutex servLock;
421 #endif
422 struct servent *wxGetservbyname_r(const char *port, const char *protocol,
423 struct servent *serv, void *buffer, int size)
424 {
425 struct servent *se = NULL;
426 #if defined(HAVE_FUNC_GETSERVBYNAME_R_6)
427 if (getservbyname_r(port, protocol, serv, (char*)buffer, size, &se))
428 se = NULL;
429 #elif defined(HAVE_FUNC_GETSERVBYNAME_R_5)
430 se = getservbyname_r(port, protocol, serv, (char*)buffer, size);
431 #elif defined(HAVE_FUNC_GETSERVBYNAME_R_4)
432 if (getservbyname_r(port, protocol, serv, (struct servent_data*) buffer))
433 se = NULL;
434 else
435 se = serv;
436 #elif defined(HAVE_GETSERVBYNAME)
437 #if wxUSE_THREADS
438 wxMutexLocker locker(servLock);
439 #endif
440 se = getservbyname(port, protocol);
441 if (se)
442 se = deepCopyServent(serv, se, (char*)buffer, size);
443 #endif
444 return se;
445 }
446
447 /* debugging helpers */
448 #ifdef __GSOCKET_DEBUG__
449 # define GSocket_Debug(args) printf args
450 #else
451 # define GSocket_Debug(args)
452 #endif /* __GSOCKET_DEBUG__ */
453
454 /* Table of GUI-related functions. We must call them indirectly because
455 * of wxBase and GUI separation: */
456
457 static GSocketGUIFunctionsTable *gs_gui_functions;
458
459 /* Global initialisers */
460
461 void GSocket_SetGUIFunctions(GSocketGUIFunctionsTable *guifunc)
462 {
463 gs_gui_functions = guifunc;
464 }
465
466 int GSocket_Init(void)
467 {
468 if (!gs_gui_functions)
469 {
470 static GSocketGUIFunctionsTableConcrete table;
471 gs_gui_functions = &table;
472 }
473 if ( !gs_gui_functions->OnInit() )
474 return 0;
475 return 1;
476 }
477
478 void GSocket_Cleanup(void)
479 {
480 if (gs_gui_functions)
481 {
482 gs_gui_functions->OnExit();
483 }
484 }
485
486 /* Constructors / Destructors for GSocket */
487
488 GSocket::GSocket()
489 {
490 int i;
491
492 m_fd = INVALID_SOCKET;
493 m_handler = NULL;
494
495 for (i=0;i<GSOCK_MAX_EVENT;i++)
496 {
497 m_cbacks[i] = NULL;
498 }
499 m_detected = 0;
500 m_local = NULL;
501 m_peer = NULL;
502 m_error = GSOCK_NOERROR;
503 m_server = false;
504 m_stream = true;
505 m_gui_dependent = NULL;
506 m_non_blocking = false;
507 m_reusable = false;
508 m_broadcast = false;
509 m_dobind = true;
510 m_timeout = 10*60*1000;
511 /* 10 minutes * 60 sec * 1000 millisec */
512 m_establishing = false;
513 m_use_events = false;
514
515 assert(gs_gui_functions);
516 /* Per-socket GUI-specific initialization */
517 m_ok = gs_gui_functions->Init_Socket(this);
518 }
519
520 void GSocket::Close()
521 {
522 if (m_use_events)
523 DisableEvents();
524 /* gsockosx.c calls CFSocketInvalidate which closes the socket for us */
525 #if !(defined(__DARWIN__) && (defined(__WXMAC__) || defined(__WXCOCOA__)))
526 close(m_fd);
527 #endif
528 m_fd = INVALID_SOCKET;
529 }
530
531 GSocket::~GSocket()
532 {
533 assert(this);
534
535 /* Check that the socket is really shutdowned */
536 if (m_fd != INVALID_SOCKET)
537 Shutdown();
538
539 /* Per-socket GUI-specific cleanup */
540 if (m_use_events)
541 gs_gui_functions->Destroy_Socket(this);
542
543 delete m_handler;
544
545 /* Destroy private addresses */
546 if (m_local)
547 GAddress_destroy(m_local);
548
549 if (m_peer)
550 GAddress_destroy(m_peer);
551
552 }
553
554 /* GSocket_Shutdown:
555 * Disallow further read/write operations on this socket, close
556 * the fd and disable all callbacks.
557 */
558 void GSocket::Shutdown()
559 {
560 int evt;
561
562 assert(this);
563
564 /* Don't allow events to fire after socket has been closed */
565 if (m_use_events)
566 DisableEvents();
567
568 /* If socket has been created, shutdown it */
569 if (m_fd != INVALID_SOCKET)
570 {
571 shutdown(m_fd, 1);
572 Close();
573 }
574
575 /* Disable GUI callbacks */
576 for (evt = 0; evt < GSOCK_MAX_EVENT; evt++)
577 m_cbacks[evt] = NULL;
578
579 m_detected = GSOCK_LOST_FLAG;
580 }
581
582 /* Address handling */
583
584 /* GSocket_SetLocal:
585 * GSocket_GetLocal:
586 * GSocket_SetPeer:
587 * GSocket_GetPeer:
588 * Set or get the local or peer address for this socket. The 'set'
589 * functions return GSOCK_NOERROR on success, an error code otherwise.
590 * The 'get' functions return a pointer to a GAddress object on success,
591 * or NULL otherwise, in which case they set the error code of the
592 * corresponding GSocket.
593 *
594 * Error codes:
595 * GSOCK_INVSOCK - the socket is not valid.
596 * GSOCK_INVADDR - the address is not valid.
597 */
598 GSocketError GSocket::SetLocal(GAddress *address)
599 {
600 assert(this);
601
602 /* the socket must be initialized, or it must be a server */
603 if ((m_fd != INVALID_SOCKET && !m_server))
604 {
605 m_error = GSOCK_INVSOCK;
606 return GSOCK_INVSOCK;
607 }
608
609 /* check address */
610 if (address == NULL || address->m_family == GSOCK_NOFAMILY)
611 {
612 m_error = GSOCK_INVADDR;
613 return GSOCK_INVADDR;
614 }
615
616 if (m_local)
617 GAddress_destroy(m_local);
618
619 m_local = GAddress_copy(address);
620
621 return GSOCK_NOERROR;
622 }
623
624 GSocketError GSocket::SetPeer(GAddress *address)
625 {
626 assert(this);
627
628 /* check address */
629 if (address == NULL || address->m_family == GSOCK_NOFAMILY)
630 {
631 m_error = GSOCK_INVADDR;
632 return GSOCK_INVADDR;
633 }
634
635 if (m_peer)
636 GAddress_destroy(m_peer);
637
638 m_peer = GAddress_copy(address);
639
640 return GSOCK_NOERROR;
641 }
642
643 GAddress *GSocket::GetLocal()
644 {
645 GAddress *address;
646 struct sockaddr addr;
647 WX_SOCKLEN_T size = sizeof(addr);
648 GSocketError err;
649
650 assert(this);
651
652 /* try to get it from the m_local var first */
653 if (m_local)
654 return GAddress_copy(m_local);
655
656 /* else, if the socket is initialized, try getsockname */
657 if (m_fd == INVALID_SOCKET)
658 {
659 m_error = GSOCK_INVSOCK;
660 return NULL;
661 }
662
663 if (getsockname(m_fd, &addr, (WX_SOCKLEN_T *) &size) < 0)
664 {
665 m_error = GSOCK_IOERR;
666 return NULL;
667 }
668
669 /* got a valid address from getsockname, create a GAddress object */
670 address = GAddress_new();
671 if (address == NULL)
672 {
673 m_error = GSOCK_MEMERR;
674 return NULL;
675 }
676
677 err = _GAddress_translate_from(address, &addr, size);
678 if (err != GSOCK_NOERROR)
679 {
680 GAddress_destroy(address);
681 m_error = err;
682 return NULL;
683 }
684
685 return address;
686 }
687
688 GAddress *GSocket::GetPeer()
689 {
690 assert(this);
691
692 /* try to get it from the m_peer var */
693 if (m_peer)
694 return GAddress_copy(m_peer);
695
696 return NULL;
697 }
698
699 /* Server specific parts */
700
701 /* GSocket_SetServer:
702 * Sets up this socket as a server. The local address must have been
703 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
704 * Returns GSOCK_NOERROR on success, one of the following otherwise:
705 *
706 * Error codes:
707 * GSOCK_INVSOCK - the socket is in use.
708 * GSOCK_INVADDR - the local address has not been set.
709 * GSOCK_IOERR - low-level error.
710 */
711 GSocketError GSocket::SetServer()
712 {
713 int arg = 1;
714
715 assert(this);
716
717 /* must not be in use */
718 if (m_fd != INVALID_SOCKET)
719 {
720 m_error = GSOCK_INVSOCK;
721 return GSOCK_INVSOCK;
722 }
723
724 /* the local addr must have been set */
725 if (!m_local)
726 {
727 m_error = GSOCK_INVADDR;
728 return GSOCK_INVADDR;
729 }
730
731 /* Initialize all fields */
732 m_stream = true;
733 m_server = true;
734
735 /* Create the socket */
736 m_fd = socket(m_local->m_realfamily, SOCK_STREAM, 0);
737
738 if (m_fd == INVALID_SOCKET)
739 {
740 m_error = GSOCK_IOERR;
741 return GSOCK_IOERR;
742 }
743
744 /* FreeBSD variants can't use MSG_NOSIGNAL, and instead use a socket option */
745 #ifdef SO_NOSIGPIPE
746 setsockopt(m_fd, SOL_SOCKET, SO_NOSIGPIPE, (const char*)&arg, sizeof(arg));
747 #endif
748
749 ioctl(m_fd, FIONBIO, &arg);
750 if (m_use_events)
751 EnableEvents();
752
753 /* allow a socket to re-bind if the socket is in the TIME_WAIT
754 state after being previously closed.
755 */
756 if (m_reusable)
757 {
758 setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&arg, sizeof(arg));
759 #ifdef SO_REUSEPORT
760 setsockopt(m_fd, SOL_SOCKET, SO_REUSEPORT, (const char*)&arg, sizeof(arg));
761 #endif
762 }
763
764 /* Bind to the local address,
765 * retrieve the actual address bound,
766 * and listen up to 5 connections.
767 */
768 if ((bind(m_fd, m_local->m_addr, m_local->m_len) != 0) ||
769 (getsockname(m_fd,
770 m_local->m_addr,
771 (WX_SOCKLEN_T *) &m_local->m_len) != 0) ||
772 (listen(m_fd, 5) != 0))
773 {
774 Close();
775 m_error = GSOCK_IOERR;
776 return GSOCK_IOERR;
777 }
778
779 return GSOCK_NOERROR;
780 }
781
782 /* GSocket_WaitConnection:
783 * Waits for an incoming client connection. Returns a pointer to
784 * a GSocket object, or NULL if there was an error, in which case
785 * the last error field will be updated for the calling GSocket.
786 *
787 * Error codes (set in the calling GSocket)
788 * GSOCK_INVSOCK - the socket is not valid or not a server.
789 * GSOCK_TIMEDOUT - timeout, no incoming connections.
790 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
791 * GSOCK_MEMERR - couldn't allocate memory.
792 * GSOCK_IOERR - low-level error.
793 */
794 GSocket *GSocket::WaitConnection()
795 {
796 struct sockaddr from;
797 WX_SOCKLEN_T fromlen = sizeof(from);
798 GSocket *connection;
799 GSocketError err;
800 int arg = 1;
801
802 assert(this);
803
804 /* If the socket has already been created, we exit immediately */
805 if (m_fd == INVALID_SOCKET || !m_server)
806 {
807 m_error = GSOCK_INVSOCK;
808 return NULL;
809 }
810
811 /* Create a GSocket object for the new connection */
812 connection = GSocket_new();
813
814 if (!connection)
815 {
816 m_error = GSOCK_MEMERR;
817 return NULL;
818 }
819
820 /* Wait for a connection (with timeout) */
821 if (Input_Timeout() == GSOCK_TIMEDOUT)
822 {
823 delete connection;
824 /* m_error set by _GSocket_Input_Timeout */
825 return NULL;
826 }
827
828 connection->m_fd = accept(m_fd, &from, (WX_SOCKLEN_T *) &fromlen);
829
830 /* Reenable CONNECTION events */
831 Enable(GSOCK_CONNECTION);
832
833 if (connection->m_fd == INVALID_SOCKET)
834 {
835 if (errno == EWOULDBLOCK)
836 m_error = GSOCK_WOULDBLOCK;
837 else
838 m_error = GSOCK_IOERR;
839
840 delete connection;
841 return NULL;
842 }
843
844 /* Initialize all fields */
845 connection->m_server = false;
846 connection->m_stream = true;
847
848 /* Setup the peer address field */
849 connection->m_peer = GAddress_new();
850 if (!connection->m_peer)
851 {
852 delete connection;
853 m_error = GSOCK_MEMERR;
854 return NULL;
855 }
856
857 err = _GAddress_translate_from(connection->m_peer, &from, fromlen);
858 if (err != GSOCK_NOERROR)
859 {
860 delete connection;
861 m_error = err;
862 return NULL;
863 }
864
865 #if defined(__EMX__) || defined(__VISAGECPP__)
866 ioctl(connection->m_fd, FIONBIO, (char*)&arg, sizeof(arg));
867 #else
868 ioctl(connection->m_fd, FIONBIO, &arg);
869 #endif
870 if (m_use_events)
871 connection->Notify(true);
872
873 return connection;
874 }
875
876 void GSocket::Notify(bool flag)
877 {
878 if ( flag == m_use_events )
879 return;
880 // it is not safe to attach or detach i/o descriptor in child thread
881 wxASSERT_MSG(wxThread::IsMain(),wxT("wxSocketBase::Notify can be called in main thread only"));
882 m_use_events = flag;
883 EnableEvents(flag);
884 }
885
886 void GSocket::EnableEvents(bool flag)
887 {
888 if ( flag )
889 gs_gui_functions->Enable_Events(this);
890 else
891 gs_gui_functions->Disable_Events(this);
892 }
893
894 bool GSocket::SetReusable()
895 {
896 /* socket must not be null, and must not be in use/already bound */
897 if (this && m_fd == INVALID_SOCKET)
898 {
899 m_reusable = true;
900
901 return true;
902 }
903
904 return false;
905 }
906
907 bool GSocket::SetBroadcast()
908 {
909 /* socket must not be in use/already bound */
910 if (m_fd == INVALID_SOCKET) {
911 m_broadcast = true;
912 return true;
913 }
914 return false;
915 }
916
917 bool GSocket::DontDoBind()
918 {
919 /* socket must not be in use/already bound */
920 if (m_fd == INVALID_SOCKET) {
921 m_dobind = false;
922 return true;
923 }
924 return false;
925 }
926
927 /* Client specific parts */
928
929 /* GSocket_Connect:
930 * For stream (connection oriented) sockets, GSocket_Connect() tries
931 * to establish a client connection to a server using the peer address
932 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
933 * connection has been successfully established, or one of the error
934 * codes listed below. Note that for nonblocking sockets, a return
935 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
936 * request can be completed later; you should use GSocket_Select()
937 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
938 * corresponding asynchronous events.
939 *
940 * For datagram (non connection oriented) sockets, GSocket_Connect()
941 * just sets the peer address established with GSocket_SetPeer() as
942 * default destination.
943 *
944 * Error codes:
945 * GSOCK_INVSOCK - the socket is in use or not valid.
946 * GSOCK_INVADDR - the peer address has not been established.
947 * GSOCK_TIMEDOUT - timeout, the connection failed.
948 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
949 * GSOCK_MEMERR - couldn't allocate memory.
950 * GSOCK_IOERR - low-level error.
951 */
952 GSocketError GSocket::Connect(GSocketStream stream)
953 {
954 int err, ret;
955 int arg = 1;
956
957 assert(this);
958
959 /* Enable CONNECTION events (needed for nonblocking connections) */
960 Enable(GSOCK_CONNECTION);
961
962 if (m_fd != INVALID_SOCKET)
963 {
964 m_error = GSOCK_INVSOCK;
965 return GSOCK_INVSOCK;
966 }
967
968 if (!m_peer)
969 {
970 m_error = GSOCK_INVADDR;
971 return GSOCK_INVADDR;
972 }
973
974 /* Streamed or dgram socket? */
975 m_stream = (stream == GSOCK_STREAMED);
976 m_server = false;
977 m_establishing = false;
978
979 /* Create the socket */
980 m_fd = socket(m_peer->m_realfamily,
981 m_stream? SOCK_STREAM : SOCK_DGRAM, 0);
982
983 if (m_fd == INVALID_SOCKET)
984 {
985 m_error = GSOCK_IOERR;
986 return GSOCK_IOERR;
987 }
988
989 /* FreeBSD variants can't use MSG_NOSIGNAL, and instead use a socket option */
990 #ifdef SO_NOSIGPIPE
991 setsockopt(m_fd, SOL_SOCKET, SO_NOSIGPIPE, (const char*)&arg, sizeof(arg));
992 #endif
993
994 #if defined(__EMX__) || defined(__VISAGECPP__)
995 ioctl(m_fd, FIONBIO, (char*)&arg, sizeof(arg));
996 #else
997 ioctl(m_fd, FIONBIO, &arg);
998 #endif
999
1000 // If the reuse flag is set, use the applicable socket reuse flags(s)
1001 if (m_reusable)
1002 {
1003 setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&arg, sizeof(arg));
1004 #ifdef SO_REUSEPORT
1005 setsockopt(m_fd, SOL_SOCKET, SO_REUSEPORT, (const char*)&arg, sizeof(arg));
1006 #endif
1007 }
1008
1009 // If a local address has been set, then we need to bind to it before calling connect
1010 if (m_local && m_local->m_addr)
1011 {
1012 bind(m_fd, m_local->m_addr, m_local->m_len);
1013 }
1014
1015 /* Connect it to the peer address, with a timeout (see below) */
1016 ret = connect(m_fd, m_peer->m_addr, m_peer->m_len);
1017
1018 /* We only call Enable_Events if we know we aren't shutting down the socket.
1019 * NB: Enable_Events needs to be called whether the socket is blocking or
1020 * non-blocking, it just shouldn't be called prior to knowing there is a
1021 * connection _if_ blocking sockets are being used.
1022 * If connect above returns 0, we are already connected and need to make the
1023 * call to Enable_Events now.
1024 */
1025
1026 if ( m_use_events && (m_non_blocking || ret == 0) )
1027 EnableEvents();
1028
1029 if (ret == -1)
1030 {
1031 err = errno;
1032
1033 /* If connect failed with EINPROGRESS and the GSocket object
1034 * is in blocking mode, we select() for the specified timeout
1035 * checking for writability to see if the connection request
1036 * completes.
1037 */
1038 if ((err == EINPROGRESS) && (!m_non_blocking))
1039 {
1040 if (Output_Timeout() == GSOCK_TIMEDOUT)
1041 {
1042 Close();
1043 /* m_error is set in _GSocket_Output_Timeout */
1044 return GSOCK_TIMEDOUT;
1045 }
1046 else
1047 {
1048 int error;
1049 SOCKOPTLEN_T len = sizeof(error);
1050
1051 getsockopt(m_fd, SOL_SOCKET, SO_ERROR, (char*) &error, &len);
1052 if ( m_use_events )
1053 EnableEvents();
1054
1055 if (!error)
1056 return GSOCK_NOERROR;
1057 }
1058 }
1059
1060 /* If connect failed with EINPROGRESS and the GSocket object
1061 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
1062 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
1063 * this way if the connection completes, a GSOCK_CONNECTION
1064 * event will be generated, if enabled.
1065 */
1066 if ((err == EINPROGRESS) && (m_non_blocking))
1067 {
1068 m_establishing = true;
1069 m_error = GSOCK_WOULDBLOCK;
1070 return GSOCK_WOULDBLOCK;
1071 }
1072
1073 /* If connect failed with an error other than EINPROGRESS,
1074 * then the call to GSocket_Connect has failed.
1075 */
1076 Close();
1077 m_error = GSOCK_IOERR;
1078
1079 return GSOCK_IOERR;
1080 }
1081
1082 return GSOCK_NOERROR;
1083 }
1084
1085 /* Datagram sockets */
1086
1087 /* GSocket_SetNonOriented:
1088 * Sets up this socket as a non-connection oriented (datagram) socket.
1089 * Before using this function, the local address must have been set
1090 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
1091 * on success, or one of the following otherwise.
1092 *
1093 * Error codes:
1094 * GSOCK_INVSOCK - the socket is in use.
1095 * GSOCK_INVADDR - the local address has not been set.
1096 * GSOCK_IOERR - low-level error.
1097 */
1098 GSocketError GSocket::SetNonOriented()
1099 {
1100 int arg = 1;
1101
1102 assert(this);
1103
1104 if (m_fd != INVALID_SOCKET)
1105 {
1106 m_error = GSOCK_INVSOCK;
1107 return GSOCK_INVSOCK;
1108 }
1109
1110 if (!m_local)
1111 {
1112 m_error = GSOCK_INVADDR;
1113 return GSOCK_INVADDR;
1114 }
1115
1116 /* Initialize all fields */
1117 m_stream = false;
1118 m_server = false;
1119
1120 /* Create the socket */
1121 m_fd = socket(m_local->m_realfamily, SOCK_DGRAM, 0);
1122
1123 if (m_fd == INVALID_SOCKET)
1124 {
1125 m_error = GSOCK_IOERR;
1126 return GSOCK_IOERR;
1127 }
1128 #if defined(__EMX__) || defined(__VISAGECPP__)
1129 ioctl(m_fd, FIONBIO, (char*)&arg, sizeof(arg));
1130 #else
1131 ioctl(m_fd, FIONBIO, &arg);
1132 #endif
1133 if ( m_use_events )
1134 EnableEvents();
1135
1136 if (m_reusable)
1137 {
1138 setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&arg, sizeof(arg));
1139 #ifdef SO_REUSEPORT
1140 setsockopt(m_fd, SOL_SOCKET, SO_REUSEPORT, (const char*)&arg, sizeof(arg));
1141 #endif
1142 }
1143
1144 if (m_broadcast)
1145 {
1146 setsockopt(m_fd, SOL_SOCKET, SO_BROADCAST, (const char*)&arg, sizeof(arg));
1147 }
1148 if (m_dobind)
1149 {
1150 /* Bind to the local address,
1151 * and retrieve the actual address bound.
1152 */
1153 if ((bind(m_fd, m_local->m_addr, m_local->m_len) != 0) ||
1154 (getsockname(m_fd,
1155 m_local->m_addr,
1156 (WX_SOCKLEN_T *) &m_local->m_len) != 0))
1157 {
1158 Close();
1159 m_error = GSOCK_IOERR;
1160 return GSOCK_IOERR;
1161 }
1162 }
1163 return GSOCK_NOERROR;
1164 }
1165
1166 /* Generic IO */
1167
1168 /* Like recv(), send(), ... */
1169 int GSocket::Read(char *buffer, int size)
1170 {
1171 int ret;
1172
1173 assert(this);
1174
1175 if (m_fd == INVALID_SOCKET || m_server)
1176 {
1177 m_error = GSOCK_INVSOCK;
1178 return -1;
1179 }
1180
1181 /* Disable events during query of socket status */
1182 Disable(GSOCK_INPUT);
1183
1184 /* If the socket is blocking, wait for data (with a timeout) */
1185 if (Input_Timeout() == GSOCK_TIMEDOUT) {
1186 m_error = GSOCK_TIMEDOUT;
1187 /* Don't return here immediately, otherwise socket events would not be
1188 * re-enabled! */
1189 ret = -1;
1190 }
1191 else
1192 {
1193 /* Read the data */
1194 if (m_stream)
1195 ret = Recv_Stream(buffer, size);
1196 else
1197 ret = Recv_Dgram(buffer, size);
1198
1199 /* If recv returned zero, then the connection has been gracefully closed.
1200 * Otherwise, recv has returned an error (-1), in which case we have lost the
1201 * socket only if errno does _not_ indicate that there may be more data to read.
1202 */
1203 if (ret == 0)
1204 {
1205 /* Make sure wxSOCKET_LOST event gets sent and shut down the socket */
1206 if (m_use_events)
1207 {
1208 m_detected = GSOCK_LOST_FLAG;
1209 Detected_Read();
1210 return 0;
1211 }
1212 }
1213 else if (ret == -1)
1214 {
1215 if ((errno == EWOULDBLOCK) || (errno == EAGAIN))
1216 m_error = GSOCK_WOULDBLOCK;
1217 else
1218 m_error = GSOCK_IOERR;
1219 }
1220 }
1221
1222 /* Enable events again now that we are done processing */
1223 Enable(GSOCK_INPUT);
1224
1225 return ret;
1226 }
1227
1228 int GSocket::Write(const char *buffer, int size)
1229 {
1230 int ret;
1231
1232 assert(this);
1233
1234 GSocket_Debug(( "GSocket_Write #1, size %d\n", size ));
1235
1236 if (m_fd == INVALID_SOCKET || m_server)
1237 {
1238 m_error = GSOCK_INVSOCK;
1239 return -1;
1240 }
1241
1242 GSocket_Debug(( "GSocket_Write #2, size %d\n", size ));
1243
1244 /* If the socket is blocking, wait for writability (with a timeout) */
1245 if (Output_Timeout() == GSOCK_TIMEDOUT)
1246 return -1;
1247
1248 GSocket_Debug(( "GSocket_Write #3, size %d\n", size ));
1249
1250 /* Write the data */
1251 if (m_stream)
1252 ret = Send_Stream(buffer, size);
1253 else
1254 ret = Send_Dgram(buffer, size);
1255
1256 GSocket_Debug(( "GSocket_Write #4, size %d\n", size ));
1257
1258 if (ret == -1)
1259 {
1260 if ((errno == EWOULDBLOCK) || (errno == EAGAIN))
1261 {
1262 m_error = GSOCK_WOULDBLOCK;
1263 GSocket_Debug(( "GSocket_Write error WOULDBLOCK\n" ));
1264 }
1265 else
1266 {
1267 m_error = GSOCK_IOERR;
1268 GSocket_Debug(( "GSocket_Write error IOERR\n" ));
1269 }
1270
1271 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
1272 * in MSW). Once the first OUTPUT event is received, users can assume
1273 * that the socket is writable until a read operation fails. Only then
1274 * will further OUTPUT events be posted.
1275 */
1276 Enable(GSOCK_OUTPUT);
1277
1278 return -1;
1279 }
1280
1281 GSocket_Debug(( "GSocket_Write #5, size %d ret %d\n", size, ret ));
1282
1283 return ret;
1284 }
1285
1286 /* GSocket_Select:
1287 * Polls the socket to determine its status. This function will
1288 * check for the events specified in the 'flags' parameter, and
1289 * it will return a mask indicating which operations can be
1290 * performed. This function won't block, regardless of the
1291 * mode (blocking | nonblocking) of the socket.
1292 */
1293 GSocketEventFlags GSocket::Select(GSocketEventFlags flags)
1294 {
1295 if (!gs_gui_functions->CanUseEventLoop())
1296 {
1297
1298 GSocketEventFlags result = 0;
1299 fd_set readfds;
1300 fd_set writefds;
1301 fd_set exceptfds;
1302 struct timeval tv;
1303
1304 assert(this);
1305
1306 if (m_fd == -1)
1307 return (GSOCK_LOST_FLAG & flags);
1308
1309 /* Do not use a static struct, Linux can garble it */
1310 tv.tv_sec = m_timeout / 1000;
1311 tv.tv_usec = (m_timeout % 1000) * 1000;
1312
1313 wxFD_ZERO(&readfds);
1314 wxFD_ZERO(&writefds);
1315 wxFD_ZERO(&exceptfds);
1316 wxFD_SET(m_fd, &readfds);
1317 if (flags & GSOCK_OUTPUT_FLAG || flags & GSOCK_CONNECTION_FLAG)
1318 wxFD_SET(m_fd, &writefds);
1319 wxFD_SET(m_fd, &exceptfds);
1320
1321 /* Check 'sticky' CONNECTION flag first */
1322 result |= (GSOCK_CONNECTION_FLAG & m_detected);
1323
1324 /* If we have already detected a LOST event, then don't try
1325 * to do any further processing.
1326 */
1327 if ((m_detected & GSOCK_LOST_FLAG) != 0)
1328 {
1329 m_establishing = false;
1330
1331 return (GSOCK_LOST_FLAG & flags);
1332 }
1333
1334 /* Try select now */
1335 if (select(m_fd + 1, &readfds, &writefds, &exceptfds, &tv) <= 0)
1336 {
1337 /* What to do here? */
1338 return (result & flags);
1339 }
1340
1341 /* Check for exceptions and errors */
1342 if (wxFD_ISSET(m_fd, &exceptfds))
1343 {
1344 m_establishing = false;
1345 m_detected = GSOCK_LOST_FLAG;
1346
1347 /* LOST event: Abort any further processing */
1348 return (GSOCK_LOST_FLAG & flags);
1349 }
1350
1351 /* Check for readability */
1352 if (wxFD_ISSET(m_fd, &readfds))
1353 {
1354 result |= GSOCK_INPUT_FLAG;
1355
1356 if (m_server && m_stream)
1357 {
1358 /* This is a TCP server socket that detected a connection.
1359 While the INPUT_FLAG is also set, it doesn't matter on
1360 this kind of sockets, as we can only Accept() from them. */
1361 result |= GSOCK_CONNECTION_FLAG;
1362 m_detected |= GSOCK_CONNECTION_FLAG;
1363 }
1364 }
1365
1366 /* Check for writability */
1367 if (wxFD_ISSET(m_fd, &writefds))
1368 {
1369 if (m_establishing && !m_server)
1370 {
1371 int error;
1372 SOCKOPTLEN_T len = sizeof(error);
1373
1374 m_establishing = false;
1375
1376 getsockopt(m_fd, SOL_SOCKET, SO_ERROR, (char*)&error, &len);
1377
1378 if (error)
1379 {
1380 m_detected = GSOCK_LOST_FLAG;
1381
1382 /* LOST event: Abort any further processing */
1383 return (GSOCK_LOST_FLAG & flags);
1384 }
1385 else
1386 {
1387 result |= GSOCK_CONNECTION_FLAG;
1388 m_detected |= GSOCK_CONNECTION_FLAG;
1389 }
1390 }
1391 else
1392 {
1393 result |= GSOCK_OUTPUT_FLAG;
1394 }
1395 }
1396
1397 return (result & flags);
1398
1399 }
1400 else
1401 {
1402 assert(this);
1403 return flags & m_detected;
1404 }
1405 }
1406
1407 /* Flags */
1408
1409 /* GSocket_SetNonBlocking:
1410 * Sets the socket to non-blocking mode. All IO calls will return
1411 * immediately.
1412 */
1413 void GSocket::SetNonBlocking(bool non_block)
1414 {
1415 assert(this);
1416
1417 GSocket_Debug( ("GSocket_SetNonBlocking: %d\n", (int)non_block) );
1418
1419 m_non_blocking = non_block;
1420 }
1421
1422 /* GSocket_SetTimeout:
1423 * Sets the timeout for blocking calls. Time is expressed in
1424 * milliseconds.
1425 */
1426 void GSocket::SetTimeout(unsigned long millisec)
1427 {
1428 assert(this);
1429
1430 m_timeout = millisec;
1431 }
1432
1433 /* GSocket_GetError:
1434 * Returns the last error occurred for this socket. Note that successful
1435 * operations do not clear this back to GSOCK_NOERROR, so use it only
1436 * after an error.
1437 */
1438 GSocketError WXDLLIMPEXP_NET GSocket::GetError()
1439 {
1440 assert(this);
1441
1442 return m_error;
1443 }
1444
1445 /* Callbacks */
1446
1447 /* GSOCK_INPUT:
1448 * There is data to be read in the input buffer. If, after a read
1449 * operation, there is still data available, the callback function will
1450 * be called again.
1451 * GSOCK_OUTPUT:
1452 * The socket is available for writing. That is, the next write call
1453 * won't block. This event is generated only once, when the connection is
1454 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
1455 * when the output buffer empties again. This means that the app should
1456 * assume that it can write since the first OUTPUT event, and no more
1457 * OUTPUT events will be generated unless an error occurs.
1458 * GSOCK_CONNECTION:
1459 * Connection successfully established, for client sockets, or incoming
1460 * client connection, for server sockets. Wait for this event (also watch
1461 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
1462 * GSOCK_LOST:
1463 * The connection is lost (or a connection request failed); this could
1464 * be due to a failure, or due to the peer closing it gracefully.
1465 */
1466
1467 /* GSocket_SetCallback:
1468 * Enables the callbacks specified by 'flags'. Note that 'flags'
1469 * may be a combination of flags OR'ed toghether, so the same
1470 * callback function can be made to accept different events.
1471 * The callback function must have the following prototype:
1472 *
1473 * void function(GSocket *socket, GSocketEvent event, char *cdata)
1474 */
1475 void GSocket::SetCallback(GSocketEventFlags flags,
1476 GSocketCallback callback, char *cdata)
1477 {
1478 int count;
1479
1480 assert(this);
1481
1482 for (count = 0; count < GSOCK_MAX_EVENT; count++)
1483 {
1484 if ((flags & (1 << count)) != 0)
1485 {
1486 m_cbacks[count] = callback;
1487 m_data[count] = cdata;
1488 }
1489 }
1490 }
1491
1492 /* GSocket_UnsetCallback:
1493 * Disables all callbacks specified by 'flags', which may be a
1494 * combination of flags OR'ed toghether.
1495 */
1496 void GSocket::UnsetCallback(GSocketEventFlags flags)
1497 {
1498 int count;
1499
1500 assert(this);
1501
1502 for (count = 0; count < GSOCK_MAX_EVENT; count++)
1503 {
1504 if ((flags & (1 << count)) != 0)
1505 {
1506 m_cbacks[count] = NULL;
1507 m_data[count] = NULL;
1508 }
1509 }
1510 }
1511
1512 GSocketError GSocket::GetSockOpt(int level, int optname,
1513 void *optval, int *optlen)
1514 {
1515 if (getsockopt(m_fd, level, optname, (char*)optval, (SOCKOPTLEN_T*)optlen) == 0)
1516 return GSOCK_NOERROR;
1517
1518 return GSOCK_OPTERR;
1519 }
1520
1521 GSocketError GSocket::SetSockOpt(int level, int optname,
1522 const void *optval, int optlen)
1523 {
1524 if (setsockopt(m_fd, level, optname, (const char*)optval, optlen) == 0)
1525 return GSOCK_NOERROR;
1526
1527 return GSOCK_OPTERR;
1528 }
1529
1530 #define CALL_CALLBACK(socket, event) { \
1531 socket->Disable(event); \
1532 if (socket->m_cbacks[event]) \
1533 socket->m_cbacks[event](socket, event, socket->m_data[event]); \
1534 }
1535
1536
1537 void GSocket::Enable(GSocketEvent event)
1538 {
1539 if ( m_use_events )
1540 {
1541 m_detected &= ~(1 << event);
1542 gs_gui_functions->Install_Callback(this, event);
1543 }
1544 }
1545
1546 void GSocket::Disable(GSocketEvent event)
1547 {
1548 if ( m_use_events )
1549 {
1550 m_detected |= (1 << event);
1551 gs_gui_functions->Uninstall_Callback(this, event);
1552 }
1553 }
1554
1555 /* _GSocket_Input_Timeout:
1556 * For blocking sockets, wait until data is available or
1557 * until timeout ellapses.
1558 */
1559 GSocketError GSocket::Input_Timeout()
1560 {
1561 struct timeval tv;
1562 fd_set readfds;
1563 int ret;
1564
1565 /* Linux select() will overwrite the struct on return */
1566 tv.tv_sec = (m_timeout / 1000);
1567 tv.tv_usec = (m_timeout % 1000) * 1000;
1568
1569 if (!m_non_blocking)
1570 {
1571 wxFD_ZERO(&readfds);
1572 wxFD_SET(m_fd, &readfds);
1573 ret = select(m_fd + 1, &readfds, NULL, NULL, &tv);
1574 if (ret == 0)
1575 {
1576 GSocket_Debug(( "GSocket_Input_Timeout, select returned 0\n" ));
1577 m_error = GSOCK_TIMEDOUT;
1578 return GSOCK_TIMEDOUT;
1579 }
1580
1581 if (ret == -1)
1582 {
1583 GSocket_Debug(( "GSocket_Input_Timeout, select returned -1\n" ));
1584 if (errno == EBADF) { GSocket_Debug(( "Invalid file descriptor\n" )); }
1585 if (errno == EINTR) { GSocket_Debug(( "A non blocked signal was caught\n" )); }
1586 if (errno == EINVAL) { GSocket_Debug(( "The highest number descriptor is negative\n" )); }
1587 if (errno == ENOMEM) { GSocket_Debug(( "Not enough memory\n" )); }
1588 m_error = GSOCK_TIMEDOUT;
1589 return GSOCK_TIMEDOUT;
1590 }
1591 }
1592
1593 return GSOCK_NOERROR;
1594 }
1595
1596 /* _GSocket_Output_Timeout:
1597 * For blocking sockets, wait until data can be sent without
1598 * blocking or until timeout ellapses.
1599 */
1600 GSocketError GSocket::Output_Timeout()
1601 {
1602 struct timeval tv;
1603 fd_set writefds;
1604 int ret;
1605
1606 /* Linux select() will overwrite the struct on return */
1607 tv.tv_sec = (m_timeout / 1000);
1608 tv.tv_usec = (m_timeout % 1000) * 1000;
1609
1610 GSocket_Debug( ("m_non_blocking has: %d\n", (int)m_non_blocking) );
1611
1612 if (!m_non_blocking)
1613 {
1614 wxFD_ZERO(&writefds);
1615 wxFD_SET(m_fd, &writefds);
1616 ret = select(m_fd + 1, NULL, &writefds, NULL, &tv);
1617 if (ret == 0)
1618 {
1619 GSocket_Debug(( "GSocket_Output_Timeout, select returned 0\n" ));
1620 m_error = GSOCK_TIMEDOUT;
1621 return GSOCK_TIMEDOUT;
1622 }
1623
1624 if (ret == -1)
1625 {
1626 GSocket_Debug(( "GSocket_Output_Timeout, select returned -1\n" ));
1627 if (errno == EBADF) { GSocket_Debug(( "Invalid file descriptor\n" )); }
1628 if (errno == EINTR) { GSocket_Debug(( "A non blocked signal was caught\n" )); }
1629 if (errno == EINVAL) { GSocket_Debug(( "The highest number descriptor is negative\n" )); }
1630 if (errno == ENOMEM) { GSocket_Debug(( "Not enough memory\n" )); }
1631 m_error = GSOCK_TIMEDOUT;
1632 return GSOCK_TIMEDOUT;
1633 }
1634
1635 if ( ! wxFD_ISSET(m_fd, &writefds) )
1636 {
1637 GSocket_Debug(( "GSocket_Output_Timeout is buggy!\n" ));
1638 }
1639 else
1640 {
1641 GSocket_Debug(( "GSocket_Output_Timeout seems correct\n" ));
1642 }
1643 }
1644 else
1645 {
1646 GSocket_Debug(( "GSocket_Output_Timeout, didn't try select!\n" ));
1647 }
1648
1649 return GSOCK_NOERROR;
1650 }
1651
1652 int GSocket::Recv_Stream(char *buffer, int size)
1653 {
1654 int ret;
1655 do
1656 {
1657 ret = recv(m_fd, buffer, size, GSOCKET_MSG_NOSIGNAL);
1658 }
1659 while (ret == -1 && errno == EINTR); /* Loop until not interrupted */
1660
1661 return ret;
1662 }
1663
1664 int GSocket::Recv_Dgram(char *buffer, int size)
1665 {
1666 struct sockaddr from;
1667 WX_SOCKLEN_T fromlen = sizeof(from);
1668 int ret;
1669 GSocketError err;
1670
1671 fromlen = sizeof(from);
1672
1673 do
1674 {
1675 ret = recvfrom(m_fd, buffer, size, 0, &from, (WX_SOCKLEN_T *) &fromlen);
1676 }
1677 while (ret == -1 && errno == EINTR); /* Loop until not interrupted */
1678
1679 if (ret == -1)
1680 return -1;
1681
1682 /* Translate a system address into a GSocket address */
1683 if (!m_peer)
1684 {
1685 m_peer = GAddress_new();
1686 if (!m_peer)
1687 {
1688 m_error = GSOCK_MEMERR;
1689 return -1;
1690 }
1691 }
1692
1693 err = _GAddress_translate_from(m_peer, &from, fromlen);
1694 if (err != GSOCK_NOERROR)
1695 {
1696 GAddress_destroy(m_peer);
1697 m_peer = NULL;
1698 m_error = err;
1699 return -1;
1700 }
1701
1702 return ret;
1703 }
1704
1705 int GSocket::Send_Stream(const char *buffer, int size)
1706 {
1707 int ret;
1708
1709 MASK_SIGNAL();
1710
1711 do
1712 {
1713 ret = send(m_fd, (char *)buffer, size, GSOCKET_MSG_NOSIGNAL);
1714 }
1715 while (ret == -1 && errno == EINTR); /* Loop until not interrupted */
1716
1717 UNMASK_SIGNAL();
1718
1719 return ret;
1720 }
1721
1722 int GSocket::Send_Dgram(const char *buffer, int size)
1723 {
1724 struct sockaddr *addr;
1725 int len, ret;
1726 GSocketError err;
1727
1728 if (!m_peer)
1729 {
1730 m_error = GSOCK_INVADDR;
1731 return -1;
1732 }
1733
1734 err = _GAddress_translate_to(m_peer, &addr, &len);
1735 if (err != GSOCK_NOERROR)
1736 {
1737 m_error = err;
1738 return -1;
1739 }
1740
1741 MASK_SIGNAL();
1742
1743 do
1744 {
1745 ret = sendto(m_fd, (char *)buffer, size, 0, addr, len);
1746 }
1747 while (ret == -1 && errno == EINTR); /* Loop until not interrupted */
1748
1749 UNMASK_SIGNAL();
1750
1751 /* Frees memory allocated from _GAddress_translate_to */
1752 free(addr);
1753
1754 return ret;
1755 }
1756
1757 void GSocket::Detected_Read()
1758 {
1759 char c;
1760
1761 /* Safeguard against straggling call to Detected_Read */
1762 if (m_fd == INVALID_SOCKET)
1763 {
1764 return;
1765 }
1766
1767 /* If we have already detected a LOST event, then don't try
1768 * to do any further processing.
1769 */
1770 if ((m_detected & GSOCK_LOST_FLAG) != 0)
1771 {
1772 m_establishing = false;
1773
1774 CALL_CALLBACK(this, GSOCK_LOST);
1775 Shutdown();
1776 return;
1777 }
1778
1779 int num = recv(m_fd, &c, 1, MSG_PEEK | GSOCKET_MSG_NOSIGNAL);
1780
1781 if (num > 0)
1782 {
1783 CALL_CALLBACK(this, GSOCK_INPUT);
1784 }
1785 else
1786 {
1787 if (m_server && m_stream)
1788 {
1789 CALL_CALLBACK(this, GSOCK_CONNECTION);
1790 }
1791 else if (num == 0)
1792 {
1793 /* graceful shutdown */
1794 CALL_CALLBACK(this, GSOCK_LOST);
1795 Shutdown();
1796 }
1797 else
1798 {
1799 /* Do not throw a lost event in cases where the socket isn't really lost */
1800 if ((errno == EWOULDBLOCK) || (errno == EAGAIN) || (errno == EINTR))
1801 {
1802 CALL_CALLBACK(this, GSOCK_INPUT);
1803 }
1804 else
1805 {
1806 CALL_CALLBACK(this, GSOCK_LOST);
1807 Shutdown();
1808 }
1809 }
1810 }
1811 }
1812
1813 void GSocket::Detected_Write()
1814 {
1815 /* If we have already detected a LOST event, then don't try
1816 * to do any further processing.
1817 */
1818 if ((m_detected & GSOCK_LOST_FLAG) != 0)
1819 {
1820 m_establishing = false;
1821
1822 CALL_CALLBACK(this, GSOCK_LOST);
1823 Shutdown();
1824 return;
1825 }
1826
1827 if (m_establishing && !m_server)
1828 {
1829 int error;
1830 SOCKOPTLEN_T len = sizeof(error);
1831
1832 m_establishing = false;
1833
1834 getsockopt(m_fd, SOL_SOCKET, SO_ERROR, (char*)&error, &len);
1835
1836 if (error)
1837 {
1838 CALL_CALLBACK(this, GSOCK_LOST);
1839 Shutdown();
1840 }
1841 else
1842 {
1843 CALL_CALLBACK(this, GSOCK_CONNECTION);
1844 /* We have to fire this event by hand because CONNECTION (for clients)
1845 * and OUTPUT are internally the same and we just disabled CONNECTION
1846 * events with the above macro.
1847 */
1848 CALL_CALLBACK(this, GSOCK_OUTPUT);
1849 }
1850 }
1851 else
1852 {
1853 CALL_CALLBACK(this, GSOCK_OUTPUT);
1854 }
1855 }
1856
1857 /* Compatibility functions for GSocket */
1858 GSocket *GSocket_new(void)
1859 {
1860 GSocket *newsocket = new GSocket();
1861 if (newsocket->IsOk())
1862 return newsocket;
1863
1864 delete newsocket;
1865
1866 return NULL;
1867 }
1868
1869 /*
1870 * -------------------------------------------------------------------------
1871 * GAddress
1872 * -------------------------------------------------------------------------
1873 */
1874
1875 /* CHECK_ADDRESS verifies that the current address family is either
1876 * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it
1877 * initalizes it to be a GSOCK_*family*. In other cases, it returns
1878 * an appropiate error code.
1879 *
1880 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
1881 */
1882 #define CHECK_ADDRESS(address, family) \
1883 { \
1884 if (address->m_family == GSOCK_NOFAMILY) \
1885 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1886 return address->m_error; \
1887 if (address->m_family != GSOCK_##family) \
1888 { \
1889 address->m_error = GSOCK_INVADDR; \
1890 return GSOCK_INVADDR; \
1891 } \
1892 }
1893
1894 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
1895 { \
1896 if (address->m_family == GSOCK_NOFAMILY) \
1897 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1898 return retval; \
1899 if (address->m_family != GSOCK_##family) \
1900 { \
1901 address->m_error = GSOCK_INVADDR; \
1902 return retval; \
1903 } \
1904 }
1905
1906
1907 GAddress *GAddress_new(void)
1908 {
1909 GAddress *address;
1910
1911 if ((address = (GAddress *) malloc(sizeof(GAddress))) == NULL)
1912 return NULL;
1913
1914 address->m_family = GSOCK_NOFAMILY;
1915 address->m_addr = NULL;
1916 address->m_len = 0;
1917
1918 return address;
1919 }
1920
1921 GAddress *GAddress_copy(GAddress *address)
1922 {
1923 GAddress *addr2;
1924
1925 assert(address != NULL);
1926
1927 if ((addr2 = (GAddress *) malloc(sizeof(GAddress))) == NULL)
1928 return NULL;
1929
1930 memcpy(addr2, address, sizeof(GAddress));
1931
1932 if (address->m_addr && address->m_len > 0)
1933 {
1934 addr2->m_addr = (struct sockaddr *)malloc(addr2->m_len);
1935 if (addr2->m_addr == NULL)
1936 {
1937 free(addr2);
1938 return NULL;
1939 }
1940 memcpy(addr2->m_addr, address->m_addr, addr2->m_len);
1941 }
1942
1943 return addr2;
1944 }
1945
1946 void GAddress_destroy(GAddress *address)
1947 {
1948 assert(address != NULL);
1949
1950 if (address->m_addr)
1951 free(address->m_addr);
1952
1953 free(address);
1954 }
1955
1956 void GAddress_SetFamily(GAddress *address, GAddressType type)
1957 {
1958 assert(address != NULL);
1959
1960 address->m_family = type;
1961 }
1962
1963 GAddressType GAddress_GetFamily(GAddress *address)
1964 {
1965 assert(address != NULL);
1966
1967 return address->m_family;
1968 }
1969
1970 GSocketError _GAddress_translate_from(GAddress *address,
1971 struct sockaddr *addr, int len)
1972 {
1973 address->m_realfamily = addr->sa_family;
1974 switch (addr->sa_family)
1975 {
1976 case AF_INET:
1977 address->m_family = GSOCK_INET;
1978 break;
1979 case AF_UNIX:
1980 address->m_family = GSOCK_UNIX;
1981 break;
1982 #ifdef AF_INET6
1983 case AF_INET6:
1984 address->m_family = GSOCK_INET6;
1985 break;
1986 #endif
1987 default:
1988 {
1989 address->m_error = GSOCK_INVOP;
1990 return GSOCK_INVOP;
1991 }
1992 }
1993
1994 if (address->m_addr)
1995 free(address->m_addr);
1996
1997 address->m_len = len;
1998 address->m_addr = (struct sockaddr *)malloc(len);
1999
2000 if (address->m_addr == NULL)
2001 {
2002 address->m_error = GSOCK_MEMERR;
2003 return GSOCK_MEMERR;
2004 }
2005
2006 memcpy(address->m_addr, addr, len);
2007
2008 return GSOCK_NOERROR;
2009 }
2010
2011 GSocketError _GAddress_translate_to(GAddress *address,
2012 struct sockaddr **addr, int *len)
2013 {
2014 if (!address->m_addr)
2015 {
2016 address->m_error = GSOCK_INVADDR;
2017 return GSOCK_INVADDR;
2018 }
2019
2020 *len = address->m_len;
2021 *addr = (struct sockaddr *)malloc(address->m_len);
2022 if (*addr == NULL)
2023 {
2024 address->m_error = GSOCK_MEMERR;
2025 return GSOCK_MEMERR;
2026 }
2027
2028 memcpy(*addr, address->m_addr, address->m_len);
2029 return GSOCK_NOERROR;
2030 }
2031
2032 /*
2033 * -------------------------------------------------------------------------
2034 * Internet address family
2035 * -------------------------------------------------------------------------
2036 */
2037
2038 GSocketError _GAddress_Init_INET(GAddress *address)
2039 {
2040 address->m_len = sizeof(struct sockaddr_in);
2041 address->m_addr = (struct sockaddr *) malloc(address->m_len);
2042 if (address->m_addr == NULL)
2043 {
2044 address->m_error = GSOCK_MEMERR;
2045 return GSOCK_MEMERR;
2046 }
2047
2048 address->m_family = GSOCK_INET;
2049 address->m_realfamily = PF_INET;
2050 ((struct sockaddr_in *)address->m_addr)->sin_family = AF_INET;
2051 ((struct sockaddr_in *)address->m_addr)->sin_addr.s_addr = INADDR_ANY;
2052
2053 return GSOCK_NOERROR;
2054 }
2055
2056 GSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname)
2057 {
2058 struct hostent *he;
2059 struct in_addr *addr;
2060
2061 assert(address != NULL);
2062
2063 CHECK_ADDRESS(address, INET);
2064
2065 addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr);
2066
2067 /* If it is a numeric host name, convert it now */
2068 #if defined(HAVE_INET_ATON)
2069 if (inet_aton(hostname, addr) == 0)
2070 {
2071 #elif defined(HAVE_INET_ADDR)
2072 if ( (addr->s_addr = inet_addr(hostname)) == (unsigned)-1 )
2073 {
2074 #else
2075 /* Use gethostbyname by default */
2076 #ifndef __WXMAC__
2077 int val = 1; /* VA doesn't like constants in conditional expressions */
2078 if (val)
2079 #endif
2080 {
2081 #endif
2082 struct in_addr *array_addr;
2083
2084 /* It is a real name, we solve it */
2085 struct hostent h;
2086 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
2087 struct hostent_data buffer;
2088 #else
2089 char buffer[1024];
2090 #endif
2091 int err;
2092 he = wxGethostbyname_r(hostname, &h, (void*)&buffer, sizeof(buffer), &err);
2093 if (he == NULL)
2094 {
2095 /* Reset to invalid address */
2096 addr->s_addr = INADDR_NONE;
2097 address->m_error = GSOCK_NOHOST;
2098 return GSOCK_NOHOST;
2099 }
2100
2101 array_addr = (struct in_addr *) *(he->h_addr_list);
2102 addr->s_addr = array_addr[0].s_addr;
2103 }
2104
2105 return GSOCK_NOERROR;
2106 }
2107
2108
2109 GSocketError GAddress_INET_SetBroadcastAddress(GAddress *address)
2110 {
2111 return GAddress_INET_SetHostAddress(address, INADDR_BROADCAST);
2112 }
2113
2114 GSocketError GAddress_INET_SetAnyAddress(GAddress *address)
2115 {
2116 return GAddress_INET_SetHostAddress(address, INADDR_ANY);
2117 }
2118
2119 GSocketError GAddress_INET_SetHostAddress(GAddress *address,
2120 unsigned long hostaddr)
2121 {
2122 struct in_addr *addr;
2123
2124 assert(address != NULL);
2125
2126 CHECK_ADDRESS(address, INET);
2127
2128 addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr);
2129 addr->s_addr = htonl(hostaddr);
2130
2131 return GSOCK_NOERROR;
2132 }
2133
2134 GSocketError GAddress_INET_SetPortName(GAddress *address, const char *port,
2135 const char *protocol)
2136 {
2137 struct servent *se;
2138 struct sockaddr_in *addr;
2139
2140 assert(address != NULL);
2141 CHECK_ADDRESS(address, INET);
2142
2143 if (!port)
2144 {
2145 address->m_error = GSOCK_INVPORT;
2146 return GSOCK_INVPORT;
2147 }
2148
2149 #if defined(HAVE_FUNC_GETSERVBYNAME_R_4)
2150 struct servent_data buffer;
2151 #else
2152 char buffer[1024];
2153 #endif
2154 struct servent serv;
2155 se = wxGetservbyname_r(port, protocol, &serv,
2156 (void*)&buffer, sizeof(buffer));
2157 if (!se)
2158 {
2159 /* the cast to int suppresses compiler warnings about subscript having the
2160 type char */
2161 if (isdigit((int)port[0]))
2162 {
2163 int port_int;
2164
2165 port_int = atoi(port);
2166 addr = (struct sockaddr_in *)address->m_addr;
2167 addr->sin_port = htons(port_int);
2168 return GSOCK_NOERROR;
2169 }
2170
2171 address->m_error = GSOCK_INVPORT;
2172 return GSOCK_INVPORT;
2173 }
2174
2175 addr = (struct sockaddr_in *)address->m_addr;
2176 addr->sin_port = se->s_port;
2177
2178 return GSOCK_NOERROR;
2179 }
2180
2181 GSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port)
2182 {
2183 struct sockaddr_in *addr;
2184
2185 assert(address != NULL);
2186 CHECK_ADDRESS(address, INET);
2187
2188 addr = (struct sockaddr_in *)address->m_addr;
2189 addr->sin_port = htons(port);
2190
2191 return GSOCK_NOERROR;
2192 }
2193
2194 GSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname, size_t sbuf)
2195 {
2196 struct hostent *he;
2197 char *addr_buf;
2198 struct sockaddr_in *addr;
2199
2200 assert(address != NULL);
2201 CHECK_ADDRESS(address, INET);
2202
2203 addr = (struct sockaddr_in *)address->m_addr;
2204 addr_buf = (char *)&(addr->sin_addr);
2205
2206 struct hostent temphost;
2207 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
2208 struct hostent_data buffer;
2209 #else
2210 char buffer[1024];
2211 #endif
2212 int err;
2213 he = wxGethostbyaddr_r(addr_buf, sizeof(addr->sin_addr), AF_INET, &temphost,
2214 (void*)&buffer, sizeof(buffer), &err);
2215 if (he == NULL)
2216 {
2217 address->m_error = GSOCK_NOHOST;
2218 return GSOCK_NOHOST;
2219 }
2220
2221 strncpy(hostname, he->h_name, sbuf);
2222
2223 return GSOCK_NOERROR;
2224 }
2225
2226 unsigned long GAddress_INET_GetHostAddress(GAddress *address)
2227 {
2228 struct sockaddr_in *addr;
2229
2230 assert(address != NULL);
2231 CHECK_ADDRESS_RETVAL(address, INET, 0);
2232
2233 addr = (struct sockaddr_in *)address->m_addr;
2234
2235 return ntohl(addr->sin_addr.s_addr);
2236 }
2237
2238 unsigned short GAddress_INET_GetPort(GAddress *address)
2239 {
2240 struct sockaddr_in *addr;
2241
2242 assert(address != NULL);
2243 CHECK_ADDRESS_RETVAL(address, INET, 0);
2244
2245 addr = (struct sockaddr_in *)address->m_addr;
2246 return ntohs(addr->sin_port);
2247 }
2248
2249 /*
2250 * -------------------------------------------------------------------------
2251 * Unix address family
2252 * -------------------------------------------------------------------------
2253 */
2254
2255 #ifndef __VISAGECPP__
2256 GSocketError _GAddress_Init_UNIX(GAddress *address)
2257 {
2258 address->m_len = sizeof(struct sockaddr_un);
2259 address->m_addr = (struct sockaddr *)malloc(address->m_len);
2260 if (address->m_addr == NULL)
2261 {
2262 address->m_error = GSOCK_MEMERR;
2263 return GSOCK_MEMERR;
2264 }
2265
2266 address->m_family = GSOCK_UNIX;
2267 address->m_realfamily = PF_UNIX;
2268 ((struct sockaddr_un *)address->m_addr)->sun_family = AF_UNIX;
2269 ((struct sockaddr_un *)address->m_addr)->sun_path[0] = 0;
2270
2271 return GSOCK_NOERROR;
2272 }
2273
2274 #define UNIX_SOCK_PATHLEN (sizeof(addr->sun_path)/sizeof(addr->sun_path[0]))
2275
2276 GSocketError GAddress_UNIX_SetPath(GAddress *address, const char *path)
2277 {
2278 struct sockaddr_un *addr;
2279
2280 assert(address != NULL);
2281
2282 CHECK_ADDRESS(address, UNIX);
2283
2284 addr = ((struct sockaddr_un *)address->m_addr);
2285 strncpy(addr->sun_path, path, UNIX_SOCK_PATHLEN);
2286 addr->sun_path[UNIX_SOCK_PATHLEN - 1] = '\0';
2287
2288 return GSOCK_NOERROR;
2289 }
2290
2291 GSocketError GAddress_UNIX_GetPath(GAddress *address, char *path, size_t sbuf)
2292 {
2293 struct sockaddr_un *addr;
2294
2295 assert(address != NULL);
2296 CHECK_ADDRESS(address, UNIX);
2297
2298 addr = (struct sockaddr_un *)address->m_addr;
2299
2300 strncpy(path, addr->sun_path, sbuf);
2301
2302 return GSOCK_NOERROR;
2303 }
2304 #endif /* !defined(__VISAGECPP__) */
2305 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */