1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket) for WX
4 * Copyright: (c) Guilhem Lavaux
5 * Licence: wxWindows Licence
6 * Authors: David Elliott (C++ conversion, maintainer)
8 * Guillermo Rodriguez Garcia <guille@iies.es>
9 * Purpose: GSocket main Unix and OS/2 file
10 * Licence: The wxWindows licence
12 * -------------------------------------------------------------------------
16 * PLEASE don't put C++ comments here - this is a C source file.
19 #ifndef __GSOCKET_STANDALONE__
23 #if defined(__VISAGECPP__)
24 /* Seems to be needed by Visual Age C++, though I don't see how it manages
25 to not break on including a C++ header into a plain C source file */
27 #define BSD_SELECT /* use Berkley Sockets select */
30 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
33 #include <sys/types.h>
38 #include <netinet/in.h>
41 #include <sys/ioctl.h>
47 u_char sun_len
; /* sockaddr len including null */
48 u_char sun_family
; /* AF_UNIX */
49 char sun_path
[108]; /* path name (gag) */
52 #include <sys/socket.h>
58 #include <netinet/in.h>
59 #include <arpa/inet.h>
66 #include <machine/endian.h>
72 #define EBADF SOCEBADF
78 #include <sys/socket.h>
79 #include <sys/ioctl.h>
80 #include <sys/select.h>
82 #define close(a) soclose(a)
83 #define select(a,b,c,d,e) bsdselect(a,b,c,d,e)
84 int _System
bsdselect(int,
89 int _System
soclose(int);
93 #include <sys/select.h>
101 # include <sys/filio.h>
104 # include <bstring.h>
107 # include <strings.h>
114 # define SOCKLEN_T unsigned int
118 # define SOCKLEN_T socklen_t
121 # define SOCKLEN_T int
125 #endif /* SOCKLEN_T */
128 #define SOCKOPTLEN_T SOCKLEN_T
132 * MSW defines this, Unices don't.
134 #ifndef INVALID_SOCKET
135 #define INVALID_SOCKET -1
138 /* UnixWare reportedly needs this for FIONBIO definition */
140 #include <sys/filio.h>
144 * INADDR_BROADCAST is identical to INADDR_NONE which is not defined
145 * on all systems. INADDR_BROADCAST should be fine to indicate an error.
148 #define INADDR_NONE INADDR_BROADCAST
151 #define MASK_SIGNAL() \
153 void (*old_handler)(int); \
155 old_handler = signal(SIGPIPE, SIG_IGN);
157 #define UNMASK_SIGNAL() \
158 signal(SIGPIPE, old_handler); \
161 /* If a SIGPIPE is issued by a socket call on a remotely closed socket,
162 the program will "crash" unless it explicitly handles the SIGPIPE.
163 By using MSG_NOSIGNAL, the SIGPIPE is suppressed. Later, we will
164 use SO_NOSIGPIPE (if available), the BSD equivalent. */
166 # define GSOCKET_MSG_NOSIGNAL MSG_NOSIGNAL
167 #else /* MSG_NOSIGNAL not available (FreeBSD including OS X) */
168 # define GSOCKET_MSG_NOSIGNAL 0
169 #endif /* MSG_NOSIGNAL */
171 #ifndef __GSOCKET_STANDALONE__
172 # include "wx/unix/gsockunx.h"
173 # include "wx/gsocket.h"
175 # include "gsockunx.h"
176 # include "gsocket.h"
177 #endif /* __GSOCKET_STANDALONE__ */
179 /* debugging helpers */
180 #ifdef __GSOCKET_DEBUG__
181 # define GSocket_Debug(args) printf args
183 # define GSocket_Debug(args)
184 #endif /* __GSOCKET_DEBUG__ */
186 /* Table of GUI-related functions. We must call them indirectly because
187 * of wxBase and GUI separation: */
189 static GSocketGUIFunctionsTable
*gs_gui_functions
;
191 class GSocketGUIFunctionsTableNull
: public GSocketGUIFunctionsTable
194 virtual bool OnInit();
195 virtual void OnExit();
196 virtual bool CanUseEventLoop();
197 virtual bool Init_Socket(GSocket
*socket
);
198 virtual void Destroy_Socket(GSocket
*socket
);
199 virtual void Install_Callback(GSocket
*socket
, GSocketEvent event
);
200 virtual void Uninstall_Callback(GSocket
*socket
, GSocketEvent event
);
201 virtual void Enable_Events(GSocket
*socket
);
202 virtual void Disable_Events(GSocket
*socket
);
205 bool GSocketGUIFunctionsTableNull::OnInit()
207 void GSocketGUIFunctionsTableNull::OnExit()
209 bool GSocketGUIFunctionsTableNull::CanUseEventLoop()
211 bool GSocketGUIFunctionsTableNull::Init_Socket(GSocket
*socket
)
213 void GSocketGUIFunctionsTableNull::Destroy_Socket(GSocket
*socket
)
215 void GSocketGUIFunctionsTableNull::Install_Callback(GSocket
*socket
, GSocketEvent event
)
217 void GSocketGUIFunctionsTableNull::Uninstall_Callback(GSocket
*socket
, GSocketEvent event
)
219 void GSocketGUIFunctionsTableNull::Enable_Events(GSocket
*socket
)
221 void GSocketGUIFunctionsTableNull::Disable_Events(GSocket
*socket
)
223 /* Global initialisers */
225 void GSocket_SetGUIFunctions(GSocketGUIFunctionsTable
*guifunc
)
227 gs_gui_functions
= guifunc
;
230 int GSocket_Init(void)
232 if (!gs_gui_functions
)
234 static GSocketGUIFunctionsTableNull table
;
235 gs_gui_functions
= &table
;
237 if ( !gs_gui_functions
->OnInit() )
242 void GSocket_Cleanup(void)
244 if (gs_gui_functions
)
246 gs_gui_functions
->OnExit();
250 /* Constructors / Destructors for GSocket */
256 m_fd
= INVALID_SOCKET
;
257 for (i
=0;i
<GSOCK_MAX_EVENT
;i
++)
264 m_error
= GSOCK_NOERROR
;
267 m_gui_dependent
= NULL
;
268 m_non_blocking
= false;
270 m_timeout
= 10*60*1000;
271 /* 10 minutes * 60 sec * 1000 millisec */
272 m_establishing
= false;
274 assert(gs_gui_functions
);
275 /* Per-socket GUI-specific initialization */
276 m_ok
= gs_gui_functions
->Init_Socket(this);
279 void GSocket::Close()
281 gs_gui_functions
->Disable_Events(this);
282 /* gsockosx.c calls CFSocketInvalidate which closes the socket for us */
283 #if !(defined(__DARWIN__) && (defined(__WXMAC__) || defined(__WXCOCOA__)))
286 m_fd
= INVALID_SOCKET
;
293 /* Check that the socket is really shutdowned */
294 if (m_fd
!= INVALID_SOCKET
)
297 /* Per-socket GUI-specific cleanup */
298 gs_gui_functions
->Destroy_Socket(this);
300 /* Destroy private addresses */
302 GAddress_destroy(m_local
);
305 GAddress_destroy(m_peer
);
309 * Disallow further read/write operations on this socket, close
310 * the fd and disable all callbacks.
312 void GSocket::Shutdown()
318 /* If socket has been created, shutdown it */
319 if (m_fd
!= INVALID_SOCKET
)
325 /* Disable GUI callbacks */
326 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
327 m_cbacks
[evt
] = NULL
;
329 m_detected
= GSOCK_LOST_FLAG
;
332 /* Address handling */
338 * Set or get the local or peer address for this socket. The 'set'
339 * functions return GSOCK_NOERROR on success, an error code otherwise.
340 * The 'get' functions return a pointer to a GAddress object on success,
341 * or NULL otherwise, in which case they set the error code of the
342 * corresponding GSocket.
345 * GSOCK_INVSOCK - the socket is not valid.
346 * GSOCK_INVADDR - the address is not valid.
348 GSocketError
GSocket::SetLocal(GAddress
*address
)
352 /* the socket must be initialized, or it must be a server */
353 if ((m_fd
!= INVALID_SOCKET
&& !m_server
))
355 m_error
= GSOCK_INVSOCK
;
356 return GSOCK_INVSOCK
;
360 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
362 m_error
= GSOCK_INVADDR
;
363 return GSOCK_INVADDR
;
367 GAddress_destroy(m_local
);
369 m_local
= GAddress_copy(address
);
371 return GSOCK_NOERROR
;
374 GSocketError
GSocket::SetPeer(GAddress
*address
)
379 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
381 m_error
= GSOCK_INVADDR
;
382 return GSOCK_INVADDR
;
386 GAddress_destroy(m_peer
);
388 m_peer
= GAddress_copy(address
);
390 return GSOCK_NOERROR
;
393 GAddress
*GSocket::GetLocal()
396 struct sockaddr addr
;
397 SOCKLEN_T size
= sizeof(addr
);
402 /* try to get it from the m_local var first */
404 return GAddress_copy(m_local
);
406 /* else, if the socket is initialized, try getsockname */
407 if (m_fd
== INVALID_SOCKET
)
409 m_error
= GSOCK_INVSOCK
;
413 if (getsockname(m_fd
, &addr
, (SOCKLEN_T
*) &size
) < 0)
415 m_error
= GSOCK_IOERR
;
419 /* got a valid address from getsockname, create a GAddress object */
420 address
= GAddress_new();
423 m_error
= GSOCK_MEMERR
;
427 err
= _GAddress_translate_from(address
, &addr
, size
);
428 if (err
!= GSOCK_NOERROR
)
430 GAddress_destroy(address
);
438 GAddress
*GSocket::GetPeer()
442 /* try to get it from the m_peer var */
444 return GAddress_copy(m_peer
);
449 /* Server specific parts */
451 /* GSocket_SetServer:
452 * Sets up this socket as a server. The local address must have been
453 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
454 * Returns GSOCK_NOERROR on success, one of the following otherwise:
457 * GSOCK_INVSOCK - the socket is in use.
458 * GSOCK_INVADDR - the local address has not been set.
459 * GSOCK_IOERR - low-level error.
461 GSocketError
GSocket::SetServer()
467 /* must not be in use */
468 if (m_fd
!= INVALID_SOCKET
)
470 m_error
= GSOCK_INVSOCK
;
471 return GSOCK_INVSOCK
;
474 /* the local addr must have been set */
477 m_error
= GSOCK_INVADDR
;
478 return GSOCK_INVADDR
;
481 /* Initialize all fields */
485 /* Create the socket */
486 m_fd
= socket(m_local
->m_realfamily
, SOCK_STREAM
, 0);
488 if (m_fd
== INVALID_SOCKET
)
490 m_error
= GSOCK_IOERR
;
494 /* FreeBSD variants can't use MSG_NOSIGNAL, and instead use a socket option */
496 setsockopt(m_fd
, SOL_SOCKET
, SO_NOSIGPIPE
, (const char*)&arg
, sizeof(u_long
));
499 ioctl(m_fd
, FIONBIO
, &arg
);
500 gs_gui_functions
->Enable_Events(this);
502 /* allow a socket to re-bind if the socket is in the TIME_WAIT
503 state after being previously closed.
506 setsockopt(m_fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char*)&arg
, sizeof(u_long
));
508 /* Bind to the local address,
509 * retrieve the actual address bound,
510 * and listen up to 5 connections.
512 if ((bind(m_fd
, m_local
->m_addr
, m_local
->m_len
) != 0) ||
515 (SOCKLEN_T
*) &m_local
->m_len
) != 0) ||
516 (listen(m_fd
, 5) != 0))
519 m_error
= GSOCK_IOERR
;
523 return GSOCK_NOERROR
;
526 /* GSocket_WaitConnection:
527 * Waits for an incoming client connection. Returns a pointer to
528 * a GSocket object, or NULL if there was an error, in which case
529 * the last error field will be updated for the calling GSocket.
531 * Error codes (set in the calling GSocket)
532 * GSOCK_INVSOCK - the socket is not valid or not a server.
533 * GSOCK_TIMEDOUT - timeout, no incoming connections.
534 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
535 * GSOCK_MEMERR - couldn't allocate memory.
536 * GSOCK_IOERR - low-level error.
538 GSocket
*GSocket::WaitConnection()
540 struct sockaddr from
;
541 SOCKLEN_T fromlen
= sizeof(from
);
548 /* If the socket has already been created, we exit immediately */
549 if (m_fd
== INVALID_SOCKET
|| !m_server
)
551 m_error
= GSOCK_INVSOCK
;
555 /* Create a GSocket object for the new connection */
556 connection
= GSocket_new();
560 m_error
= GSOCK_MEMERR
;
564 /* Wait for a connection (with timeout) */
565 if (Input_Timeout() == GSOCK_TIMEDOUT
)
568 /* m_error set by _GSocket_Input_Timeout */
572 connection
->m_fd
= accept(m_fd
, &from
, (SOCKLEN_T
*) &fromlen
);
574 /* Reenable CONNECTION events */
575 Enable(GSOCK_CONNECTION
);
577 if (connection
->m_fd
== INVALID_SOCKET
)
579 if (errno
== EWOULDBLOCK
)
580 m_error
= GSOCK_WOULDBLOCK
;
582 m_error
= GSOCK_IOERR
;
588 /* Initialize all fields */
589 connection
->m_server
= false;
590 connection
->m_stream
= true;
592 /* Setup the peer address field */
593 connection
->m_peer
= GAddress_new();
594 if (!connection
->m_peer
)
597 m_error
= GSOCK_MEMERR
;
600 err
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
);
601 if (err
!= GSOCK_NOERROR
)
607 #if defined(__EMX__) || defined(__VISAGECPP__)
608 ioctl(connection
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
610 ioctl(connection
->m_fd
, FIONBIO
, &arg
);
612 gs_gui_functions
->Enable_Events(connection
);
617 bool GSocket::SetReusable()
619 /* socket must not be null, and must not be in use/already bound */
620 if (this && m_fd
== INVALID_SOCKET
) {
627 /* Client specific parts */
630 * For stream (connection oriented) sockets, GSocket_Connect() tries
631 * to establish a client connection to a server using the peer address
632 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
633 * connection has been succesfully established, or one of the error
634 * codes listed below. Note that for nonblocking sockets, a return
635 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
636 * request can be completed later; you should use GSocket_Select()
637 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
638 * corresponding asynchronous events.
640 * For datagram (non connection oriented) sockets, GSocket_Connect()
641 * just sets the peer address established with GSocket_SetPeer() as
642 * default destination.
645 * GSOCK_INVSOCK - the socket is in use or not valid.
646 * GSOCK_INVADDR - the peer address has not been established.
647 * GSOCK_TIMEDOUT - timeout, the connection failed.
648 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
649 * GSOCK_MEMERR - couldn't allocate memory.
650 * GSOCK_IOERR - low-level error.
652 GSocketError
GSocket::Connect(GSocketStream stream
)
659 /* Enable CONNECTION events (needed for nonblocking connections) */
660 Enable(GSOCK_CONNECTION
);
662 if (m_fd
!= INVALID_SOCKET
)
664 m_error
= GSOCK_INVSOCK
;
665 return GSOCK_INVSOCK
;
670 m_error
= GSOCK_INVADDR
;
671 return GSOCK_INVADDR
;
674 /* Streamed or dgram socket? */
675 m_stream
= (stream
== GSOCK_STREAMED
);
677 m_establishing
= false;
679 /* Create the socket */
680 m_fd
= socket(m_peer
->m_realfamily
,
681 m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
683 if (m_fd
== INVALID_SOCKET
)
685 m_error
= GSOCK_IOERR
;
689 /* FreeBSD variants can't use MSG_NOSIGNAL, and instead use a socket option */
691 setsockopt(m_fd
, SOL_SOCKET
, SO_NOSIGPIPE
, (const char*)&arg
, sizeof(u_long
));
694 #if defined(__EMX__) || defined(__VISAGECPP__)
695 ioctl(m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
697 ioctl(m_fd
, FIONBIO
, &arg
);
700 /* Connect it to the peer address, with a timeout (see below) */
701 ret
= connect(m_fd
, m_peer
->m_addr
, m_peer
->m_len
);
703 /* We only call Enable_Events if we know e aren't shutting down the socket */
707 gs_gui_functions
->Enable_Events(this);
714 /* If connect failed with EINPROGRESS and the GSocket object
715 * is in blocking mode, we select() for the specified timeout
716 * checking for writability to see if the connection request
719 if ((err
== EINPROGRESS
) && (!m_non_blocking
))
721 if (Output_Timeout() == GSOCK_TIMEDOUT
)
724 /* m_error is set in _GSocket_Output_Timeout */
725 return GSOCK_TIMEDOUT
;
730 SOCKOPTLEN_T len
= sizeof(error
);
732 getsockopt(m_fd
, SOL_SOCKET
, SO_ERROR
, (char*) &error
, &len
);
734 gs_gui_functions
->Enable_Events(this);
737 return GSOCK_NOERROR
;
741 /* If connect failed with EINPROGRESS and the GSocket object
742 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
743 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
744 * this way if the connection completes, a GSOCK_CONNECTION
745 * event will be generated, if enabled.
747 if ((err
== EINPROGRESS
) && (m_non_blocking
))
749 m_establishing
= true;
750 m_error
= GSOCK_WOULDBLOCK
;
751 return GSOCK_WOULDBLOCK
;
754 /* If connect failed with an error other than EINPROGRESS,
755 * then the call to GSocket_Connect has failed.
758 m_error
= GSOCK_IOERR
;
762 return GSOCK_NOERROR
;
765 /* Datagram sockets */
767 /* GSocket_SetNonOriented:
768 * Sets up this socket as a non-connection oriented (datagram) socket.
769 * Before using this function, the local address must have been set
770 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
771 * on success, or one of the following otherwise.
774 * GSOCK_INVSOCK - the socket is in use.
775 * GSOCK_INVADDR - the local address has not been set.
776 * GSOCK_IOERR - low-level error.
778 GSocketError
GSocket::SetNonOriented()
784 if (m_fd
!= INVALID_SOCKET
)
786 m_error
= GSOCK_INVSOCK
;
787 return GSOCK_INVSOCK
;
792 m_error
= GSOCK_INVADDR
;
793 return GSOCK_INVADDR
;
796 /* Initialize all fields */
800 /* Create the socket */
801 m_fd
= socket(m_local
->m_realfamily
, SOCK_DGRAM
, 0);
803 if (m_fd
== INVALID_SOCKET
)
805 m_error
= GSOCK_IOERR
;
808 #if defined(__EMX__) || defined(__VISAGECPP__)
809 ioctl(m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
811 ioctl(m_fd
, FIONBIO
, &arg
);
813 gs_gui_functions
->Enable_Events(this);
815 /* Bind to the local address,
816 * and retrieve the actual address bound.
818 if ((bind(m_fd
, m_local
->m_addr
, m_local
->m_len
) != 0) ||
821 (SOCKLEN_T
*) &m_local
->m_len
) != 0))
824 m_error
= GSOCK_IOERR
;
828 return GSOCK_NOERROR
;
833 /* Like recv(), send(), ... */
834 int GSocket::Read(char *buffer
, int size
)
840 if (m_fd
== INVALID_SOCKET
|| m_server
)
842 m_error
= GSOCK_INVSOCK
;
846 /* Disable events during query of socket status */
847 Disable(GSOCK_INPUT
);
849 /* If the socket is blocking, wait for data (with a timeout) */
850 if (Input_Timeout() == GSOCK_TIMEDOUT
)
851 /* We no longer return here immediately, otherwise socket events would not be re-enabled! */
856 ret
= Recv_Stream(buffer
, size
);
858 ret
= Recv_Dgram(buffer
, size
);
863 if ((errno
== EWOULDBLOCK
) || (errno
== EAGAIN
))
864 m_error
= GSOCK_WOULDBLOCK
;
866 m_error
= GSOCK_IOERR
;
869 /* Enable events again now that we are done processing */
875 int GSocket::Write(const char *buffer
, int size
)
881 GSocket_Debug(( "GSocket_Write #1, size %d\n", size
));
883 if (m_fd
== INVALID_SOCKET
|| m_server
)
885 m_error
= GSOCK_INVSOCK
;
889 GSocket_Debug(( "GSocket_Write #2, size %d\n", size
));
891 /* If the socket is blocking, wait for writability (with a timeout) */
892 if (Output_Timeout() == GSOCK_TIMEDOUT
)
895 GSocket_Debug(( "GSocket_Write #3, size %d\n", size
));
899 ret
= Send_Stream(buffer
, size
);
901 ret
= Send_Dgram(buffer
, size
);
903 GSocket_Debug(( "GSocket_Write #4, size %d\n", size
));
907 if ((errno
== EWOULDBLOCK
) || (errno
== EAGAIN
))
909 m_error
= GSOCK_WOULDBLOCK
;
910 GSocket_Debug(( "GSocket_Write error WOULDBLOCK\n" ));
914 m_error
= GSOCK_IOERR
;
915 GSocket_Debug(( "GSocket_Write error IOERR\n" ));
918 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
919 * in MSW). Once the first OUTPUT event is received, users can assume
920 * that the socket is writable until a read operation fails. Only then
921 * will further OUTPUT events be posted.
923 Enable(GSOCK_OUTPUT
);
927 GSocket_Debug(( "GSocket_Write #5, size %d ret %d\n", size
, ret
));
933 * Polls the socket to determine its status. This function will
934 * check for the events specified in the 'flags' parameter, and
935 * it will return a mask indicating which operations can be
936 * performed. This function won't block, regardless of the
937 * mode (blocking | nonblocking) of the socket.
939 GSocketEventFlags
GSocket::Select(GSocketEventFlags flags
)
941 if (!gs_gui_functions
->CanUseEventLoop())
944 GSocketEventFlags result
= 0;
952 /* Do not use a static struct, Linux can garble it */
953 tv
.tv_sec
= m_timeout
/ 1000;
954 tv
.tv_usec
= (m_timeout
% 1000) * 1000;
959 FD_SET(m_fd
, &readfds
);
960 if (flags
& GSOCK_OUTPUT_FLAG
|| flags
& GSOCK_CONNECTION_FLAG
)
961 FD_SET(m_fd
, &writefds
);
962 FD_SET(m_fd
, &exceptfds
);
964 /* Check 'sticky' CONNECTION flag first */
965 result
|= (GSOCK_CONNECTION_FLAG
& m_detected
);
967 /* If we have already detected a LOST event, then don't try
968 * to do any further processing.
970 if ((m_detected
& GSOCK_LOST_FLAG
) != 0)
972 m_establishing
= false;
974 return (GSOCK_LOST_FLAG
& flags
);
978 if (select(m_fd
+ 1, &readfds
, &writefds
, &exceptfds
, &tv
) <= 0)
980 /* What to do here? */
981 return (result
& flags
);
984 /* Check for readability */
985 if (FD_ISSET(m_fd
, &readfds
))
989 int num
= recv(m_fd
, &c
, 1, MSG_PEEK
| GSOCKET_MSG_NOSIGNAL
);
993 result
|= GSOCK_INPUT_FLAG
;
997 if (m_server
&& m_stream
)
999 result
|= GSOCK_CONNECTION_FLAG
;
1000 m_detected
|= GSOCK_CONNECTION_FLAG
;
1002 else if ((errno
!= EWOULDBLOCK
) && (errno
!= EAGAIN
) && (errno
!= EINTR
))
1004 m_detected
= GSOCK_LOST_FLAG
;
1005 m_establishing
= false;
1007 /* LOST event: Abort any further processing */
1008 return (GSOCK_LOST_FLAG
& flags
);
1013 /* Check for writability */
1014 if (FD_ISSET(m_fd
, &writefds
))
1016 if (m_establishing
&& !m_server
)
1019 SOCKOPTLEN_T len
= sizeof(error
);
1021 m_establishing
= false;
1023 getsockopt(m_fd
, SOL_SOCKET
, SO_ERROR
, (char*)&error
, &len
);
1027 m_detected
= GSOCK_LOST_FLAG
;
1029 /* LOST event: Abort any further processing */
1030 return (GSOCK_LOST_FLAG
& flags
);
1034 result
|= GSOCK_CONNECTION_FLAG
;
1035 m_detected
|= GSOCK_CONNECTION_FLAG
;
1040 result
|= GSOCK_OUTPUT_FLAG
;
1044 /* Check for exceptions and errors (is this useful in Unices?) */
1045 if (FD_ISSET(m_fd
, &exceptfds
))
1047 m_establishing
= false;
1048 m_detected
= GSOCK_LOST_FLAG
;
1050 /* LOST event: Abort any further processing */
1051 return (GSOCK_LOST_FLAG
& flags
);
1054 return (result
& flags
);
1061 return flags
& m_detected
;
1068 /* GSocket_SetNonBlocking:
1069 * Sets the socket to non-blocking mode. All IO calls will return
1072 void GSocket::SetNonBlocking(bool non_block
)
1076 GSocket_Debug( ("GSocket_SetNonBlocking: %d\n", (int)non_block
) );
1078 m_non_blocking
= non_block
;
1081 /* GSocket_SetTimeout:
1082 * Sets the timeout for blocking calls. Time is expressed in
1085 void GSocket::SetTimeout(unsigned long millisec
)
1089 m_timeout
= millisec
;
1092 /* GSocket_GetError:
1093 * Returns the last error occured for this socket. Note that successful
1094 * operations do not clear this back to GSOCK_NOERROR, so use it only
1097 GSocketError WXDLLIMPEXP_NET
GSocket::GetError()
1107 * There is data to be read in the input buffer. If, after a read
1108 * operation, there is still data available, the callback function will
1111 * The socket is available for writing. That is, the next write call
1112 * won't block. This event is generated only once, when the connection is
1113 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
1114 * when the output buffer empties again. This means that the app should
1115 * assume that it can write since the first OUTPUT event, and no more
1116 * OUTPUT events will be generated unless an error occurs.
1118 * Connection succesfully established, for client sockets, or incoming
1119 * client connection, for server sockets. Wait for this event (also watch
1120 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
1122 * The connection is lost (or a connection request failed); this could
1123 * be due to a failure, or due to the peer closing it gracefully.
1126 /* GSocket_SetCallback:
1127 * Enables the callbacks specified by 'flags'. Note that 'flags'
1128 * may be a combination of flags OR'ed toghether, so the same
1129 * callback function can be made to accept different events.
1130 * The callback function must have the following prototype:
1132 * void function(GSocket *socket, GSocketEvent event, char *cdata)
1134 void GSocket::SetCallback(GSocketEventFlags flags
,
1135 GSocketCallback callback
, char *cdata
)
1141 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
1143 if ((flags
& (1 << count
)) != 0)
1145 m_cbacks
[count
] = callback
;
1146 m_data
[count
] = cdata
;
1151 /* GSocket_UnsetCallback:
1152 * Disables all callbacks specified by 'flags', which may be a
1153 * combination of flags OR'ed toghether.
1155 void GSocket::UnsetCallback(GSocketEventFlags flags
)
1161 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
1163 if ((flags
& (1 << count
)) != 0)
1165 m_cbacks
[count
] = NULL
;
1166 m_data
[count
] = NULL
;
1171 GSocketError
GSocket::GetSockOpt(int level
, int optname
,
1172 void *optval
, int *optlen
)
1174 if (getsockopt(m_fd
, level
, optname
, (char*)optval
, (SOCKOPTLEN_T
*)optlen
) == 0)
1176 return GSOCK_NOERROR
;
1178 return GSOCK_OPTERR
;
1181 GSocketError
GSocket::SetSockOpt(int level
, int optname
,
1182 const void *optval
, int optlen
)
1184 if (setsockopt(m_fd
, level
, optname
, (const char*)optval
, optlen
) == 0)
1186 return GSOCK_NOERROR
;
1188 return GSOCK_OPTERR
;
1191 #define CALL_CALLBACK(socket, event) { \
1192 socket->Disable(event); \
1193 if (socket->m_cbacks[event]) \
1194 socket->m_cbacks[event](socket, event, socket->m_data[event]); \
1198 void GSocket::Enable(GSocketEvent event
)
1200 m_detected
&= ~(1 << event
);
1201 gs_gui_functions
->Install_Callback(this, event
);
1204 void GSocket::Disable(GSocketEvent event
)
1206 m_detected
|= (1 << event
);
1207 gs_gui_functions
->Uninstall_Callback(this, event
);
1210 /* _GSocket_Input_Timeout:
1211 * For blocking sockets, wait until data is available or
1212 * until timeout ellapses.
1214 GSocketError
GSocket::Input_Timeout()
1220 /* Linux select() will overwrite the struct on return */
1221 tv
.tv_sec
= (m_timeout
/ 1000);
1222 tv
.tv_usec
= (m_timeout
% 1000) * 1000;
1224 if (!m_non_blocking
)
1227 FD_SET(m_fd
, &readfds
);
1228 ret
= select(m_fd
+ 1, &readfds
, NULL
, NULL
, &tv
);
1231 GSocket_Debug(( "GSocket_Input_Timeout, select returned 0\n" ));
1232 m_error
= GSOCK_TIMEDOUT
;
1233 return GSOCK_TIMEDOUT
;
1237 GSocket_Debug(( "GSocket_Input_Timeout, select returned -1\n" ));
1238 if (errno
== EBADF
) { GSocket_Debug(( "Invalid file descriptor\n" )); }
1239 if (errno
== EINTR
) { GSocket_Debug(( "A non blocked signal was caught\n" )); }
1240 if (errno
== EINVAL
) { GSocket_Debug(( "The highest number descriptor is negative\n" )); }
1241 if (errno
== ENOMEM
) { GSocket_Debug(( "Not enough memory\n" )); }
1242 m_error
= GSOCK_TIMEDOUT
;
1243 return GSOCK_TIMEDOUT
;
1246 return GSOCK_NOERROR
;
1249 /* _GSocket_Output_Timeout:
1250 * For blocking sockets, wait until data can be sent without
1251 * blocking or until timeout ellapses.
1253 GSocketError
GSocket::Output_Timeout()
1259 /* Linux select() will overwrite the struct on return */
1260 tv
.tv_sec
= (m_timeout
/ 1000);
1261 tv
.tv_usec
= (m_timeout
% 1000) * 1000;
1263 GSocket_Debug( ("m_non_blocking has: %d\n", (int)m_non_blocking
) );
1265 if (!m_non_blocking
)
1268 FD_SET(m_fd
, &writefds
);
1269 ret
= select(m_fd
+ 1, NULL
, &writefds
, NULL
, &tv
);
1272 GSocket_Debug(( "GSocket_Output_Timeout, select returned 0\n" ));
1273 m_error
= GSOCK_TIMEDOUT
;
1274 return GSOCK_TIMEDOUT
;
1278 GSocket_Debug(( "GSocket_Output_Timeout, select returned -1\n" ));
1279 if (errno
== EBADF
) { GSocket_Debug(( "Invalid file descriptor\n" )); }
1280 if (errno
== EINTR
) { GSocket_Debug(( "A non blocked signal was caught\n" )); }
1281 if (errno
== EINVAL
) { GSocket_Debug(( "The highest number descriptor is negative\n" )); }
1282 if (errno
== ENOMEM
) { GSocket_Debug(( "Not enough memory\n" )); }
1283 m_error
= GSOCK_TIMEDOUT
;
1284 return GSOCK_TIMEDOUT
;
1286 if ( ! FD_ISSET(m_fd
, &writefds
) ) {
1287 GSocket_Debug(( "GSocket_Output_Timeout is buggy!\n" ));
1290 GSocket_Debug(( "GSocket_Output_Timeout seems correct\n" ));
1295 GSocket_Debug(( "GSocket_Output_Timeout, didn't try select!\n" ));
1298 return GSOCK_NOERROR
;
1301 int GSocket::Recv_Stream(char *buffer
, int size
)
1306 ret
= recv(m_fd
, buffer
, size
, GSOCKET_MSG_NOSIGNAL
);
1307 } while (ret
== -1 && errno
== EINTR
); /* Loop until not interrupted */
1311 int GSocket::Recv_Dgram(char *buffer
, int size
)
1313 struct sockaddr from
;
1314 SOCKLEN_T fromlen
= sizeof(from
);
1318 fromlen
= sizeof(from
);
1322 ret
= recvfrom(m_fd
, buffer
, size
, 0, &from
, (SOCKLEN_T
*) &fromlen
);
1323 } while (ret
== -1 && errno
== EINTR
); /* Loop until not interrupted */
1328 /* Translate a system address into a GSocket address */
1331 m_peer
= GAddress_new();
1334 m_error
= GSOCK_MEMERR
;
1338 err
= _GAddress_translate_from(m_peer
, &from
, fromlen
);
1339 if (err
!= GSOCK_NOERROR
)
1341 GAddress_destroy(m_peer
);
1350 int GSocket::Send_Stream(const char *buffer
, int size
)
1354 #ifndef __VISAGECPP__
1359 ret
= send(m_fd
, (char *)buffer
, size
, GSOCKET_MSG_NOSIGNAL
);
1360 } while (ret
== -1 && errno
== EINTR
); /* Loop until not interrupted */
1361 #ifndef __VISAGECPP__
1368 int GSocket::Send_Dgram(const char *buffer
, int size
)
1370 struct sockaddr
*addr
;
1376 m_error
= GSOCK_INVADDR
;
1380 err
= _GAddress_translate_to(m_peer
, &addr
, &len
);
1381 if (err
!= GSOCK_NOERROR
)
1387 #ifndef __VISAGECPP__
1392 ret
= sendto(m_fd
, (char *)buffer
, size
, 0, addr
, len
);
1393 } while (ret
== -1 && errno
== EINTR
); /* Loop until not interrupted */
1394 #ifndef __VISAGECPP__
1398 /* Frees memory allocated from _GAddress_translate_to */
1404 void GSocket::Detected_Read()
1408 /* Safeguard against straggling call to Detected_Read */
1409 if (m_fd
== INVALID_SOCKET
)
1414 /* If we have already detected a LOST event, then don't try
1415 * to do any further processing.
1417 if ((m_detected
& GSOCK_LOST_FLAG
) != 0)
1419 m_establishing
= false;
1421 CALL_CALLBACK(this, GSOCK_LOST
);
1426 int num
= recv(m_fd
, &c
, 1, MSG_PEEK
| GSOCKET_MSG_NOSIGNAL
);
1430 CALL_CALLBACK(this, GSOCK_INPUT
);
1434 if (m_server
&& m_stream
)
1436 CALL_CALLBACK(this, GSOCK_CONNECTION
);
1440 /* Do not throw a lost event in cases where the socket isn't really lost */
1441 if ((errno
== EWOULDBLOCK
) || (errno
== EAGAIN
) || (errno
== EINTR
))
1443 CALL_CALLBACK(this, GSOCK_INPUT
);
1447 CALL_CALLBACK(this, GSOCK_LOST
);
1454 void GSocket::Detected_Write()
1456 /* If we have already detected a LOST event, then don't try
1457 * to do any further processing.
1459 if ((m_detected
& GSOCK_LOST_FLAG
) != 0)
1461 m_establishing
= false;
1463 CALL_CALLBACK(this, GSOCK_LOST
);
1468 if (m_establishing
&& !m_server
)
1471 SOCKOPTLEN_T len
= sizeof(error
);
1473 m_establishing
= false;
1475 getsockopt(m_fd
, SOL_SOCKET
, SO_ERROR
, (char*)&error
, &len
);
1479 CALL_CALLBACK(this, GSOCK_LOST
);
1484 CALL_CALLBACK(this, GSOCK_CONNECTION
);
1485 /* We have to fire this event by hand because CONNECTION (for clients)
1486 * and OUTPUT are internally the same and we just disabled CONNECTION
1487 * events with the above macro.
1489 CALL_CALLBACK(this, GSOCK_OUTPUT
);
1494 CALL_CALLBACK(this, GSOCK_OUTPUT
);
1498 /* Compatibility functions for GSocket */
1499 GSocket
*GSocket_new(void)
1501 GSocket
*newsocket
= new GSocket();
1502 if(newsocket
->IsOk())
1509 * -------------------------------------------------------------------------
1511 * -------------------------------------------------------------------------
1514 /* CHECK_ADDRESS verifies that the current address family is either
1515 * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it
1516 * initalizes it to be a GSOCK_*family*. In other cases, it returns
1517 * an appropiate error code.
1519 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
1521 #define CHECK_ADDRESS(address, family) \
1523 if (address->m_family == GSOCK_NOFAMILY) \
1524 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1525 return address->m_error; \
1526 if (address->m_family != GSOCK_##family) \
1528 address->m_error = GSOCK_INVADDR; \
1529 return GSOCK_INVADDR; \
1533 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
1535 if (address->m_family == GSOCK_NOFAMILY) \
1536 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1538 if (address->m_family != GSOCK_##family) \
1540 address->m_error = GSOCK_INVADDR; \
1546 GAddress
*GAddress_new(void)
1550 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1553 address
->m_family
= GSOCK_NOFAMILY
;
1554 address
->m_addr
= NULL
;
1560 GAddress
*GAddress_copy(GAddress
*address
)
1564 assert(address
!= NULL
);
1566 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1569 memcpy(addr2
, address
, sizeof(GAddress
));
1571 if (address
->m_addr
&& address
->m_len
> 0)
1573 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
1574 if (addr2
->m_addr
== NULL
)
1579 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1585 void GAddress_destroy(GAddress
*address
)
1587 assert(address
!= NULL
);
1589 if (address
->m_addr
)
1590 free(address
->m_addr
);
1595 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1597 assert(address
!= NULL
);
1599 address
->m_family
= type
;
1602 GAddressType
GAddress_GetFamily(GAddress
*address
)
1604 assert(address
!= NULL
);
1606 return address
->m_family
;
1609 GSocketError
_GAddress_translate_from(GAddress
*address
,
1610 struct sockaddr
*addr
, int len
)
1612 address
->m_realfamily
= addr
->sa_family
;
1613 switch (addr
->sa_family
)
1616 address
->m_family
= GSOCK_INET
;
1619 address
->m_family
= GSOCK_UNIX
;
1623 address
->m_family
= GSOCK_INET6
;
1628 address
->m_error
= GSOCK_INVOP
;
1633 if (address
->m_addr
)
1634 free(address
->m_addr
);
1636 address
->m_len
= len
;
1637 address
->m_addr
= (struct sockaddr
*)malloc(len
);
1639 if (address
->m_addr
== NULL
)
1641 address
->m_error
= GSOCK_MEMERR
;
1642 return GSOCK_MEMERR
;
1644 memcpy(address
->m_addr
, addr
, len
);
1646 return GSOCK_NOERROR
;
1649 GSocketError
_GAddress_translate_to(GAddress
*address
,
1650 struct sockaddr
**addr
, int *len
)
1652 if (!address
->m_addr
)
1654 address
->m_error
= GSOCK_INVADDR
;
1655 return GSOCK_INVADDR
;
1658 *len
= address
->m_len
;
1659 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
1662 address
->m_error
= GSOCK_MEMERR
;
1663 return GSOCK_MEMERR
;
1666 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1667 return GSOCK_NOERROR
;
1671 * -------------------------------------------------------------------------
1672 * Internet address family
1673 * -------------------------------------------------------------------------
1676 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1678 address
->m_len
= sizeof(struct sockaddr_in
);
1679 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1680 if (address
->m_addr
== NULL
)
1682 address
->m_error
= GSOCK_MEMERR
;
1683 return GSOCK_MEMERR
;
1686 address
->m_family
= GSOCK_INET
;
1687 address
->m_realfamily
= PF_INET
;
1688 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1689 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1691 return GSOCK_NOERROR
;
1694 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1697 struct in_addr
*addr
;
1699 assert(address
!= NULL
);
1701 CHECK_ADDRESS(address
, INET
);
1703 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1705 /* If it is a numeric host name, convert it now */
1706 #if defined(HAVE_INET_ATON)
1707 if (inet_aton(hostname
, addr
) == 0)
1709 #elif defined(HAVE_INET_ADDR)
1710 if ( (addr
->s_addr
= inet_addr(hostname
)) == (unsigned)-1 )
1713 /* Use gethostbyname by default */
1715 int val
= 1; /* VA doesn't like constants in conditional expressions */
1720 struct in_addr
*array_addr
;
1722 /* It is a real name, we solve it */
1723 if ((he
= gethostbyname(hostname
)) == NULL
)
1725 /* Reset to invalid address */
1726 addr
->s_addr
= INADDR_NONE
;
1727 address
->m_error
= GSOCK_NOHOST
;
1728 return GSOCK_NOHOST
;
1730 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1731 addr
->s_addr
= array_addr
[0].s_addr
;
1733 return GSOCK_NOERROR
;
1736 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1738 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1741 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1742 unsigned long hostaddr
)
1744 struct in_addr
*addr
;
1746 assert(address
!= NULL
);
1748 CHECK_ADDRESS(address
, INET
);
1750 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1751 addr
->s_addr
= htonl(hostaddr
);
1753 return GSOCK_NOERROR
;
1756 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1757 const char *protocol
)
1760 struct sockaddr_in
*addr
;
1762 assert(address
!= NULL
);
1763 CHECK_ADDRESS(address
, INET
);
1767 address
->m_error
= GSOCK_INVPORT
;
1768 return GSOCK_INVPORT
;
1771 #if defined(__WXPM__) && defined(__EMX__)
1772 se
= getservbyname(port
, (char*)protocol
);
1774 se
= getservbyname(port
, protocol
);
1778 /* the cast to int suppresses compiler warnings about subscript having the
1780 if (isdigit((int)port
[0]))
1784 port_int
= atoi(port
);
1785 addr
= (struct sockaddr_in
*)address
->m_addr
;
1786 addr
->sin_port
= htons(port_int
);
1787 return GSOCK_NOERROR
;
1790 address
->m_error
= GSOCK_INVPORT
;
1791 return GSOCK_INVPORT
;
1794 addr
= (struct sockaddr_in
*)address
->m_addr
;
1795 addr
->sin_port
= se
->s_port
;
1797 return GSOCK_NOERROR
;
1800 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1802 struct sockaddr_in
*addr
;
1804 assert(address
!= NULL
);
1805 CHECK_ADDRESS(address
, INET
);
1807 addr
= (struct sockaddr_in
*)address
->m_addr
;
1808 addr
->sin_port
= htons(port
);
1810 return GSOCK_NOERROR
;
1813 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1817 struct sockaddr_in
*addr
;
1819 assert(address
!= NULL
);
1820 CHECK_ADDRESS(address
, INET
);
1822 addr
= (struct sockaddr_in
*)address
->m_addr
;
1823 addr_buf
= (char *)&(addr
->sin_addr
);
1825 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1828 address
->m_error
= GSOCK_NOHOST
;
1829 return GSOCK_NOHOST
;
1832 strncpy(hostname
, he
->h_name
, sbuf
);
1834 return GSOCK_NOERROR
;
1837 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1839 struct sockaddr_in
*addr
;
1841 assert(address
!= NULL
);
1842 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1844 addr
= (struct sockaddr_in
*)address
->m_addr
;
1846 return ntohl(addr
->sin_addr
.s_addr
);
1849 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1851 struct sockaddr_in
*addr
;
1853 assert(address
!= NULL
);
1854 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1856 addr
= (struct sockaddr_in
*)address
->m_addr
;
1857 return ntohs(addr
->sin_port
);
1861 * -------------------------------------------------------------------------
1862 * Unix address family
1863 * -------------------------------------------------------------------------
1866 #ifndef __VISAGECPP__
1867 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1869 address
->m_len
= sizeof(struct sockaddr_un
);
1870 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
1871 if (address
->m_addr
== NULL
)
1873 address
->m_error
= GSOCK_MEMERR
;
1874 return GSOCK_MEMERR
;
1877 address
->m_family
= GSOCK_UNIX
;
1878 address
->m_realfamily
= PF_UNIX
;
1879 ((struct sockaddr_un
*)address
->m_addr
)->sun_family
= AF_UNIX
;
1880 ((struct sockaddr_un
*)address
->m_addr
)->sun_path
[0] = 0;
1882 return GSOCK_NOERROR
;
1885 #define UNIX_SOCK_PATHLEN (sizeof(addr->sun_path)/sizeof(addr->sun_path[0]))
1887 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1889 struct sockaddr_un
*addr
;
1891 assert(address
!= NULL
);
1893 CHECK_ADDRESS(address
, UNIX
);
1895 addr
= ((struct sockaddr_un
*)address
->m_addr
);
1896 strncpy(addr
->sun_path
, path
, UNIX_SOCK_PATHLEN
);
1897 addr
->sun_path
[UNIX_SOCK_PATHLEN
- 1] = '\0';
1899 return GSOCK_NOERROR
;
1902 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1904 struct sockaddr_un
*addr
;
1906 assert(address
!= NULL
);
1907 CHECK_ADDRESS(address
, UNIX
);
1909 addr
= (struct sockaddr_un
*)address
->m_addr
;
1911 strncpy(path
, addr
->sun_path
, sbuf
);
1913 return GSOCK_NOERROR
;
1915 #endif /* !defined(__VISAGECPP__) */
1916 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */