]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/gsocket.c
1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket)
4 * Author: Guillermo Rodriguez Garcia <guille@iies.es>
5 * Purpose: GSocket main MSW file
6 * Licence: The wxWidgets licence
8 * -------------------------------------------------------------------------
12 * PLEASE don't put C++ comments here - this is a C source file.
16 /* RPCNOTIFICATION_ROUTINE in rasasync.h (included from winsock.h),
17 * warning: conditional expression is constant.
19 # pragma warning(disable:4115)
21 * warning: named type definition in parentheses.
23 # pragma warning(disable:4127)
24 /* GAddress_UNIX_GetPath,
25 * warning: unreferenced formal parameter.
27 # pragma warning(disable:4100)
30 /* windows.h results in tons of warnings at max warning level */
32 # pragma warning(push, 1)
37 # pragma warning(disable:4514)
45 #ifndef __GSOCKET_STANDALONE__
46 # include "wx/platform.h"
47 # include "wx/setup.h"
50 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
52 #ifndef __GSOCKET_STANDALONE__
53 # include "wx/msw/gsockmsw.h"
54 # include "wx/gsocket.h"
56 # include "gsockmsw.h"
58 #endif /* __GSOCKET_STANDALONE__ */
65 #define isdigit(x) (x > 47 && x < 58)
67 #include "wx/msw/wince/net.h"
76 /* if we use configure for MSW SOCKLEN_T will be already defined */
78 # define SOCKLEN_T int
81 /* Table of GUI-related functions. We must call them indirectly because
82 * of wxBase and GUI separation: */
84 static struct GSocketGUIFunctionsTable
*gs_gui_functions
;
86 #define USE_GUI() (gs_gui_functions != NULL)
88 /* Define macros to simplify indirection: */
89 #define _GSocket_GUI_Init() \
90 if (gs_gui_functions) gs_gui_functions->GUI_Init()
91 #define _GSocket_GUI_Cleanup() \
92 if (gs_gui_functions) gs_gui_functions->GUI_Cleanup()
93 #define _GSocket_GUI_Init_Socket(socket) \
94 (gs_gui_functions ? gs_gui_functions->GUI_Init_Socket(socket) : 1)
95 #define _GSocket_GUI_Destroy_Socket(socket) \
96 if (gs_gui_functions) gs_gui_functions->GUI_Destroy_Socket(socket)
97 #define _GSocket_Enable_Events(socket) \
98 if (gs_gui_functions) gs_gui_functions->Enable_Events(socket)
99 #define _GSocket_Disable_Events(socket) \
100 if (gs_gui_functions) gs_gui_functions->Disable_Events(socket)
101 #define _GSocket_Install_Callback(socket, event) \
102 if (gs_gui_functions) gs_gui_functions->Install_Callback(socket, event)
103 #define _GSocket_Uninstall_Callback(socket, event) \
104 if (gs_gui_functions) gs_gui_functions->Uninstall_Callback(socket, event)
106 /* Global initialisers */
108 void GSocket_SetGUIFunctions(struct GSocketGUIFunctionsTable
*guifunc
)
110 gs_gui_functions
= guifunc
;
113 int GSocket_Init(void)
117 if (gs_gui_functions
)
119 if ( !gs_gui_functions
->GUI_Init() )
123 /* Initialize WinSocket */
124 return (WSAStartup((1 << 8) | 1, &wsaData
) == 0);
127 void GSocket_Cleanup(void)
129 if (gs_gui_functions
)
131 gs_gui_functions
->GUI_Cleanup();
134 /* Cleanup WinSocket */
138 /* Constructors / Destructors for GSocket */
140 GSocket
*GSocket_new(void)
145 if ((socket
= (GSocket
*) malloc(sizeof(GSocket
))) == NULL
)
148 socket
->m_fd
= INVALID_SOCKET
;
149 for (i
= 0; i
< GSOCK_MAX_EVENT
; i
++)
151 socket
->m_cbacks
[i
] = NULL
;
153 socket
->m_detected
= 0;
154 socket
->m_local
= NULL
;
155 socket
->m_peer
= NULL
;
156 socket
->m_error
= GSOCK_NOERROR
;
157 socket
->m_server
= FALSE
;
158 socket
->m_stream
= TRUE
;
159 socket
->m_non_blocking
= FALSE
;
160 socket
->m_timeout
.tv_sec
= 10 * 60; /* 10 minutes */
161 socket
->m_timeout
.tv_usec
= 0;
162 socket
->m_establishing
= FALSE
;
164 /* Per-socket GUI-specific initialization */
165 success
= _GSocket_GUI_Init_Socket(socket
);
175 void GSocket_close(GSocket
*socket
)
177 _GSocket_Disable_Events(socket
);
178 closesocket(socket
->m_fd
);
179 socket
->m_fd
= INVALID_SOCKET
;
182 void GSocket_destroy(GSocket
*socket
)
184 assert(socket
!= NULL
);
186 /* Per-socket GUI-specific cleanup */
187 _GSocket_GUI_Destroy_Socket(socket
);
189 /* Check that the socket is really shutdowned */
190 if (socket
->m_fd
!= INVALID_SOCKET
)
191 GSocket_Shutdown(socket
);
193 /* Destroy private addresses */
195 GAddress_destroy(socket
->m_local
);
198 GAddress_destroy(socket
->m_peer
);
200 /* Destroy the socket itself */
205 * Disallow further read/write operations on this socket, close
206 * the fd and disable all callbacks.
208 void GSocket_Shutdown(GSocket
*socket
)
212 assert(socket
!= NULL
);
214 /* If socket has been created, shutdown it */
215 if (socket
->m_fd
!= INVALID_SOCKET
)
217 shutdown(socket
->m_fd
, 2);
218 GSocket_close(socket
);
221 /* Disable GUI callbacks */
222 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
223 socket
->m_cbacks
[evt
] = NULL
;
225 socket
->m_detected
= GSOCK_LOST_FLAG
;
228 /* Address handling */
234 * Set or get the local or peer address for this socket. The 'set'
235 * functions return GSOCK_NOERROR on success, an error code otherwise.
236 * The 'get' functions return a pointer to a GAddress object on success,
237 * or NULL otherwise, in which case they set the error code of the
238 * corresponding GSocket.
241 * GSOCK_INVSOCK - the socket is not valid.
242 * GSOCK_INVADDR - the address is not valid.
244 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
246 assert(socket
!= NULL
);
248 /* the socket must be initialized, or it must be a server */
249 if (socket
->m_fd
!= INVALID_SOCKET
&& !socket
->m_server
)
251 socket
->m_error
= GSOCK_INVSOCK
;
252 return GSOCK_INVSOCK
;
256 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
258 socket
->m_error
= GSOCK_INVADDR
;
259 return GSOCK_INVADDR
;
263 GAddress_destroy(socket
->m_local
);
265 socket
->m_local
= GAddress_copy(address
);
267 return GSOCK_NOERROR
;
270 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
272 assert(socket
!= NULL
);
275 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
277 socket
->m_error
= GSOCK_INVADDR
;
278 return GSOCK_INVADDR
;
282 GAddress_destroy(socket
->m_peer
);
284 socket
->m_peer
= GAddress_copy(address
);
286 return GSOCK_NOERROR
;
289 GAddress
*GSocket_GetLocal(GSocket
*socket
)
292 struct sockaddr addr
;
293 SOCKLEN_T size
= sizeof(addr
);
296 assert(socket
!= NULL
);
298 /* try to get it from the m_local var first */
300 return GAddress_copy(socket
->m_local
);
302 /* else, if the socket is initialized, try getsockname */
303 if (socket
->m_fd
== INVALID_SOCKET
)
305 socket
->m_error
= GSOCK_INVSOCK
;
309 if (getsockname(socket
->m_fd
, &addr
, &size
) == SOCKET_ERROR
)
311 socket
->m_error
= GSOCK_IOERR
;
315 /* got a valid address from getsockname, create a GAddress object */
316 if ((address
= GAddress_new()) == NULL
)
318 socket
->m_error
= GSOCK_MEMERR
;
322 if ((err
= _GAddress_translate_from(address
, &addr
, size
)) != GSOCK_NOERROR
)
324 GAddress_destroy(address
);
325 socket
->m_error
= err
;
332 GAddress
*GSocket_GetPeer(GSocket
*socket
)
334 assert(socket
!= NULL
);
336 /* try to get it from the m_peer var */
338 return GAddress_copy(socket
->m_peer
);
343 /* Server specific parts */
345 /* GSocket_SetServer:
346 * Sets up this socket as a server. The local address must have been
347 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
348 * Returns GSOCK_NOERROR on success, one of the following otherwise:
351 * GSOCK_INVSOCK - the socket is in use.
352 * GSOCK_INVADDR - the local address has not been set.
353 * GSOCK_IOERR - low-level error.
355 GSocketError
GSocket_SetServer(GSocket
*sck
)
361 /* must not be in use */
362 if (sck
->m_fd
!= INVALID_SOCKET
)
364 sck
->m_error
= GSOCK_INVSOCK
;
365 return GSOCK_INVSOCK
;
368 /* the local addr must have been set */
371 sck
->m_error
= GSOCK_INVADDR
;
372 return GSOCK_INVADDR
;
375 /* Initialize all fields */
376 sck
->m_server
= TRUE
;
377 sck
->m_stream
= TRUE
;
378 sck
->m_oriented
= TRUE
;
380 /* Create the socket */
381 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
383 if (sck
->m_fd
== INVALID_SOCKET
)
385 sck
->m_error
= GSOCK_IOERR
;
389 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
390 _GSocket_Enable_Events(sck
);
392 /* allow a socket to re-bind if the socket is in the TIME_WAIT
393 state after being previously closed.
395 setsockopt(sck
->m_fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char*)&arg
, sizeof(u_long
));
397 /* Bind to the local address,
398 * retrieve the actual address bound,
399 * and listen up to 5 connections.
401 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
402 (getsockname(sck
->m_fd
,
403 sck
->m_local
->m_addr
,
404 (SOCKLEN_T
*)&sck
->m_local
->m_len
) != 0) ||
405 (listen(sck
->m_fd
, 5) != 0))
408 sck
->m_error
= GSOCK_IOERR
;
412 return GSOCK_NOERROR
;
415 /* GSocket_WaitConnection:
416 * Waits for an incoming client connection. Returns a pointer to
417 * a GSocket object, or NULL if there was an error, in which case
418 * the last error field will be updated for the calling GSocket.
420 * Error codes (set in the calling GSocket)
421 * GSOCK_INVSOCK - the socket is not valid or not a server.
422 * GSOCK_TIMEDOUT - timeout, no incoming connections.
423 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
424 * GSOCK_MEMERR - couldn't allocate memory.
425 * GSOCK_IOERR - low-level error.
427 GSocket
*GSocket_WaitConnection(GSocket
*sck
)
430 struct sockaddr from
;
431 SOCKLEN_T fromlen
= sizeof(from
);
437 /* Reenable CONNECTION events */
438 sck
->m_detected
&= ~GSOCK_CONNECTION_FLAG
;
440 /* If the socket has already been created, we exit immediately */
441 if (sck
->m_fd
== INVALID_SOCKET
|| !sck
->m_server
)
443 sck
->m_error
= GSOCK_INVSOCK
;
447 /* Create a GSocket object for the new connection */
448 connection
= GSocket_new();
452 sck
->m_error
= GSOCK_MEMERR
;
456 /* Wait for a connection (with timeout) */
457 if (_GSocket_Input_Timeout(sck
) == GSOCK_TIMEDOUT
)
459 GSocket_destroy(connection
);
460 /* sck->m_error set by _GSocket_Input_Timeout */
464 connection
->m_fd
= accept(sck
->m_fd
, &from
, &fromlen
);
466 if (connection
->m_fd
== INVALID_SOCKET
)
468 if (WSAGetLastError() == WSAEWOULDBLOCK
)
469 sck
->m_error
= GSOCK_WOULDBLOCK
;
471 sck
->m_error
= GSOCK_IOERR
;
473 GSocket_destroy(connection
);
477 /* Initialize all fields */
478 connection
->m_server
= FALSE
;
479 connection
->m_stream
= TRUE
;
480 connection
->m_oriented
= TRUE
;
482 /* Setup the peer address field */
483 connection
->m_peer
= GAddress_new();
484 if (!connection
->m_peer
)
486 GSocket_destroy(connection
);
487 sck
->m_error
= GSOCK_MEMERR
;
490 err
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
);
491 if (err
!= GSOCK_NOERROR
)
493 GAddress_destroy(connection
->m_peer
);
494 GSocket_destroy(connection
);
499 ioctlsocket(connection
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
500 _GSocket_Enable_Events(connection
);
505 /* Client specific parts */
508 * For stream (connection oriented) sockets, GSocket_Connect() tries
509 * to establish a client connection to a server using the peer address
510 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
511 * connection has been succesfully established, or one of the error
512 * codes listed below. Note that for nonblocking sockets, a return
513 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
514 * request can be completed later; you should use GSocket_Select()
515 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
516 * corresponding asynchronous events.
518 * For datagram (non connection oriented) sockets, GSocket_Connect()
519 * just sets the peer address established with GSocket_SetPeer() as
520 * default destination.
523 * GSOCK_INVSOCK - the socket is in use or not valid.
524 * GSOCK_INVADDR - the peer address has not been established.
525 * GSOCK_TIMEDOUT - timeout, the connection failed.
526 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
527 * GSOCK_MEMERR - couldn't allocate memory.
528 * GSOCK_IOERR - low-level error.
530 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
537 /* Enable CONNECTION events (needed for nonblocking connections) */
538 sck
->m_detected
&= ~GSOCK_CONNECTION_FLAG
;
540 if (sck
->m_fd
!= INVALID_SOCKET
)
542 sck
->m_error
= GSOCK_INVSOCK
;
543 return GSOCK_INVSOCK
;
548 sck
->m_error
= GSOCK_INVADDR
;
549 return GSOCK_INVADDR
;
552 /* Streamed or dgram socket? */
553 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
554 sck
->m_oriented
= TRUE
;
555 sck
->m_server
= FALSE
;
556 sck
->m_establishing
= FALSE
;
558 /* Create the socket */
559 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
,
560 sck
->m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
562 if (sck
->m_fd
== INVALID_SOCKET
)
564 sck
->m_error
= GSOCK_IOERR
;
568 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
569 _GSocket_Enable_Events(sck
);
571 /* Connect it to the peer address, with a timeout (see below) */
572 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
574 if (ret
== SOCKET_ERROR
)
576 err
= WSAGetLastError();
578 /* If connect failed with EWOULDBLOCK and the GSocket object
579 * is in blocking mode, we select() for the specified timeout
580 * checking for writability to see if the connection request
583 if ((err
== WSAEWOULDBLOCK
) && (!sck
->m_non_blocking
))
585 err
= _GSocket_Connect_Timeout(sck
);
587 if (err
!= GSOCK_NOERROR
)
590 /* sck->m_error is set in _GSocket_Connect_Timeout */
593 return (GSocketError
) err
;
596 /* If connect failed with EWOULDBLOCK and the GSocket object
597 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
598 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
599 * this way if the connection completes, a GSOCK_CONNECTION
600 * event will be generated, if enabled.
602 if ((err
== WSAEWOULDBLOCK
) && (sck
->m_non_blocking
))
604 sck
->m_establishing
= TRUE
;
605 sck
->m_error
= GSOCK_WOULDBLOCK
;
606 return GSOCK_WOULDBLOCK
;
609 /* If connect failed with an error other than EWOULDBLOCK,
610 * then the call to GSocket_Connect() has failed.
613 sck
->m_error
= GSOCK_IOERR
;
617 return GSOCK_NOERROR
;
620 /* Datagram sockets */
622 /* GSocket_SetNonOriented:
623 * Sets up this socket as a non-connection oriented (datagram) socket.
624 * Before using this function, the local address must have been set
625 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
626 * on success, or one of the following otherwise.
629 * GSOCK_INVSOCK - the socket is in use.
630 * GSOCK_INVADDR - the local address has not been set.
631 * GSOCK_IOERR - low-level error.
633 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
639 if (sck
->m_fd
!= INVALID_SOCKET
)
641 sck
->m_error
= GSOCK_INVSOCK
;
642 return GSOCK_INVSOCK
;
647 sck
->m_error
= GSOCK_INVADDR
;
648 return GSOCK_INVADDR
;
651 /* Initialize all fields */
652 sck
->m_stream
= FALSE
;
653 sck
->m_server
= FALSE
;
654 sck
->m_oriented
= FALSE
;
656 /* Create the socket */
657 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
659 if (sck
->m_fd
== INVALID_SOCKET
)
661 sck
->m_error
= GSOCK_IOERR
;
665 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
666 _GSocket_Enable_Events(sck
);
668 /* Bind to the local address,
669 * and retrieve the actual address bound.
671 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
672 (getsockname(sck
->m_fd
,
673 sck
->m_local
->m_addr
,
674 (SOCKLEN_T
*)&sck
->m_local
->m_len
) != 0))
677 sck
->m_error
= GSOCK_IOERR
;
681 return GSOCK_NOERROR
;
686 /* Like recv(), send(), ... */
687 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
691 assert(socket
!= NULL
);
693 /* Reenable INPUT events */
694 socket
->m_detected
&= ~GSOCK_INPUT_FLAG
;
696 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
698 socket
->m_error
= GSOCK_INVSOCK
;
702 /* If the socket is blocking, wait for data (with a timeout) */
703 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
707 if (socket
->m_stream
)
708 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
710 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
712 if (ret
== SOCKET_ERROR
)
714 if (WSAGetLastError() != WSAEWOULDBLOCK
)
715 socket
->m_error
= GSOCK_IOERR
;
717 socket
->m_error
= GSOCK_WOULDBLOCK
;
724 int GSocket_Write(GSocket
*socket
, const char *buffer
, int size
)
728 assert(socket
!= NULL
);
730 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
732 socket
->m_error
= GSOCK_INVSOCK
;
736 /* If the socket is blocking, wait for writability (with a timeout) */
737 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
741 if (socket
->m_stream
)
742 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
744 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
746 if (ret
== SOCKET_ERROR
)
748 if (WSAGetLastError() != WSAEWOULDBLOCK
)
749 socket
->m_error
= GSOCK_IOERR
;
751 socket
->m_error
= GSOCK_WOULDBLOCK
;
753 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
754 * does). Once the first OUTPUT event is received, users can assume
755 * that the socket is writable until a read operation fails. Only then
756 * will further OUTPUT events be posted.
758 socket
->m_detected
&= ~GSOCK_OUTPUT_FLAG
;
766 * Polls the socket to determine its status. This function will
767 * check for the events specified in the 'flags' parameter, and
768 * it will return a mask indicating which operations can be
769 * performed. This function won't block, regardless of the
770 * mode (blocking | nonblocking) of the socket.
772 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
776 GSocketEventFlags result
= 0;
781 assert(socket
!= NULL
);
786 FD_SET(socket
->m_fd
, &readfds
);
787 if (flags
& GSOCK_OUTPUT_FLAG
|| flags
& GSOCK_CONNECTION_FLAG
)
788 FD_SET(socket
->m_fd
, &writefds
);
789 FD_SET(socket
->m_fd
, &exceptfds
);
791 /* Check 'sticky' CONNECTION flag first */
792 result
|= (GSOCK_CONNECTION_FLAG
& socket
->m_detected
);
794 /* If we have already detected a LOST event, then don't try
795 * to do any further processing.
797 if ((socket
->m_detected
& GSOCK_LOST_FLAG
) != 0)
799 socket
->m_establishing
= FALSE
;
801 return (GSOCK_LOST_FLAG
& flags
);
805 if (select(socket
->m_fd
+ 1, &readfds
, &writefds
, &exceptfds
,
806 &socket
->m_timeout
) <= 0)
808 /* What to do here? */
809 return (result
& flags
);
812 /* Check for readability */
813 if (FD_ISSET(socket
->m_fd
, &readfds
))
817 if (!socket
->m_stream
|| recv(socket
->m_fd
, &c
, 1, MSG_PEEK
) > 0)
819 result
|= GSOCK_INPUT_FLAG
;
823 if (socket
->m_server
&& socket
->m_stream
)
825 result
|= GSOCK_CONNECTION_FLAG
;
826 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
830 socket
->m_detected
= GSOCK_LOST_FLAG
;
831 socket
->m_establishing
= FALSE
;
833 /* LOST event: Abort any further processing */
834 return (GSOCK_LOST_FLAG
& flags
);
839 /* Check for writability */
840 if (FD_ISSET(socket
->m_fd
, &writefds
))
842 if (socket
->m_establishing
&& !socket
->m_server
)
845 SOCKLEN_T len
= sizeof(error
);
847 socket
->m_establishing
= FALSE
;
849 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*)&error
, &len
);
853 socket
->m_detected
= GSOCK_LOST_FLAG
;
855 /* LOST event: Abort any further processing */
856 return (GSOCK_LOST_FLAG
& flags
);
860 result
|= GSOCK_CONNECTION_FLAG
;
861 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
866 result
|= GSOCK_OUTPUT_FLAG
;
870 /* Check for exceptions and errors (is this useful in Unices?) */
871 if (FD_ISSET(socket
->m_fd
, &exceptfds
))
873 socket
->m_establishing
= FALSE
;
874 socket
->m_detected
= GSOCK_LOST_FLAG
;
876 /* LOST event: Abort any further processing */
877 return (GSOCK_LOST_FLAG
& flags
);
880 return (result
& flags
);
884 assert(socket
!= NULL
);
885 return flags
& socket
->m_detected
;
891 /* GSocket_SetNonBlocking:
892 * Sets the socket to non-blocking mode. All IO calls will return
895 void GSocket_SetNonBlocking(GSocket
*socket
, int non_block
)
897 assert(socket
!= NULL
);
899 socket
->m_non_blocking
= non_block
;
902 /* GSocket_SetTimeout:
903 * Sets the timeout for blocking calls. Time is expressed in
906 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millis
)
908 assert(socket
!= NULL
);
910 socket
->m_timeout
.tv_sec
= (millis
/ 1000);
911 socket
->m_timeout
.tv_usec
= (millis
% 1000) * 1000;
915 * Returns the last error occured for this socket. Note that successful
916 * operations do not clear this back to GSOCK_NOERROR, so use it only
919 GSocketError
GSocket_GetError(GSocket
*socket
)
921 assert(socket
!= NULL
);
923 return socket
->m_error
;
929 * There is data to be read in the input buffer. If, after a read
930 * operation, there is still data available, the callback function will
933 * The socket is available for writing. That is, the next write call
934 * won't block. This event is generated only once, when the connection is
935 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
936 * when the output buffer empties again. This means that the app should
937 * assume that it can write since the first OUTPUT event, and no more
938 * OUTPUT events will be generated unless an error occurs.
940 * Connection succesfully established, for client sockets, or incoming
941 * client connection, for server sockets. Wait for this event (also watch
942 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
944 * The connection is lost (or a connection request failed); this could
945 * be due to a failure, or due to the peer closing it gracefully.
948 /* GSocket_SetCallback:
949 * Enables the callbacks specified by 'flags'. Note that 'flags'
950 * may be a combination of flags OR'ed toghether, so the same
951 * callback function can be made to accept different events.
952 * The callback function must have the following prototype:
954 * void function(GSocket *socket, GSocketEvent event, char *cdata)
956 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
957 GSocketCallback callback
, char *cdata
)
961 assert(socket
!= NULL
);
963 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
965 if ((flags
& (1 << count
)) != 0)
967 socket
->m_cbacks
[count
] = callback
;
968 socket
->m_data
[count
] = cdata
;
973 /* GSocket_UnsetCallback:
974 * Disables all callbacks specified by 'flags', which may be a
975 * combination of flags OR'ed toghether.
977 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
981 assert(socket
!= NULL
);
983 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
985 if ((flags
& (1 << count
)) != 0)
987 socket
->m_cbacks
[count
] = NULL
;
988 socket
->m_data
[count
] = NULL
;
995 /* _GSocket_Input_Timeout:
996 * For blocking sockets, wait until data is available or
997 * until timeout ellapses.
999 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
1003 if (!socket
->m_non_blocking
)
1006 FD_SET(socket
->m_fd
, &readfds
);
1007 if (select(0, &readfds
, NULL
, NULL
, &socket
->m_timeout
) == 0)
1009 socket
->m_error
= GSOCK_TIMEDOUT
;
1010 return GSOCK_TIMEDOUT
;
1013 return GSOCK_NOERROR
;
1016 /* _GSocket_Output_Timeout:
1017 * For blocking sockets, wait until data can be sent without
1018 * blocking or until timeout ellapses.
1020 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
1024 if (!socket
->m_non_blocking
)
1027 FD_SET(socket
->m_fd
, &writefds
);
1028 if (select(0, NULL
, &writefds
, NULL
, &socket
->m_timeout
) == 0)
1030 socket
->m_error
= GSOCK_TIMEDOUT
;
1031 return GSOCK_TIMEDOUT
;
1034 return GSOCK_NOERROR
;
1037 /* _GSocket_Connect_Timeout:
1038 * For blocking sockets, wait until the connection is
1039 * established or fails, or until timeout ellapses.
1041 GSocketError
_GSocket_Connect_Timeout(GSocket
*socket
)
1047 FD_ZERO(&exceptfds
);
1048 FD_SET(socket
->m_fd
, &writefds
);
1049 FD_SET(socket
->m_fd
, &exceptfds
);
1050 if (select(0, NULL
, &writefds
, &exceptfds
, &socket
->m_timeout
) == 0)
1052 socket
->m_error
= GSOCK_TIMEDOUT
;
1053 return GSOCK_TIMEDOUT
;
1055 if (!FD_ISSET(socket
->m_fd
, &writefds
))
1057 socket
->m_error
= GSOCK_IOERR
;
1061 return GSOCK_NOERROR
;
1064 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
1066 return recv(socket
->m_fd
, buffer
, size
, 0);
1069 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
1071 struct sockaddr from
;
1072 SOCKLEN_T fromlen
= sizeof(from
);
1076 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
1078 if (ret
== SOCKET_ERROR
)
1079 return SOCKET_ERROR
;
1081 /* Translate a system address into a GSocket address */
1082 if (!socket
->m_peer
)
1084 socket
->m_peer
= GAddress_new();
1085 if (!socket
->m_peer
)
1087 socket
->m_error
= GSOCK_MEMERR
;
1091 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
1092 if (err
!= GSOCK_NOERROR
)
1094 GAddress_destroy(socket
->m_peer
);
1095 socket
->m_peer
= NULL
;
1096 socket
->m_error
= err
;
1103 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
1105 return send(socket
->m_fd
, buffer
, size
, 0);
1108 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
1110 struct sockaddr
*addr
;
1114 if (!socket
->m_peer
)
1116 socket
->m_error
= GSOCK_INVADDR
;
1120 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
1121 if (err
!= GSOCK_NOERROR
)
1123 socket
->m_error
= err
;
1127 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
1129 /* Frees memory allocated by _GAddress_translate_to */
1137 * -------------------------------------------------------------------------
1139 * -------------------------------------------------------------------------
1142 /* CHECK_ADDRESS verifies that the current address family is either
1143 * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it
1144 * initalizes it to be a GSOCK_*family*. In other cases, it returns
1145 * an appropiate error code.
1147 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
1149 #define CHECK_ADDRESS(address, family) \
1151 if (address->m_family == GSOCK_NOFAMILY) \
1152 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1153 return address->m_error; \
1154 if (address->m_family != GSOCK_##family) \
1156 address->m_error = GSOCK_INVADDR; \
1157 return GSOCK_INVADDR; \
1161 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
1163 if (address->m_family == GSOCK_NOFAMILY) \
1164 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1166 if (address->m_family != GSOCK_##family) \
1168 address->m_error = GSOCK_INVADDR; \
1174 GAddress
*GAddress_new(void)
1178 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1181 address
->m_family
= GSOCK_NOFAMILY
;
1182 address
->m_addr
= NULL
;
1188 GAddress
*GAddress_copy(GAddress
*address
)
1192 assert(address
!= NULL
);
1194 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1197 memcpy(addr2
, address
, sizeof(GAddress
));
1199 if (address
->m_addr
)
1201 addr2
->m_addr
= (struct sockaddr
*) malloc(addr2
->m_len
);
1202 if (addr2
->m_addr
== NULL
)
1207 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1213 void GAddress_destroy(GAddress
*address
)
1215 assert(address
!= NULL
);
1217 if (address
->m_addr
)
1218 free(address
->m_addr
);
1223 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1225 assert(address
!= NULL
);
1227 address
->m_family
= type
;
1230 GAddressType
GAddress_GetFamily(GAddress
*address
)
1232 assert(address
!= NULL
);
1234 return address
->m_family
;
1237 GSocketError
_GAddress_translate_from(GAddress
*address
,
1238 struct sockaddr
*addr
, int len
)
1240 address
->m_realfamily
= addr
->sa_family
;
1241 switch (addr
->sa_family
)
1244 address
->m_family
= GSOCK_INET
;
1247 address
->m_family
= GSOCK_UNIX
;
1251 address
->m_family
= GSOCK_INET6
;
1256 address
->m_error
= GSOCK_INVOP
;
1261 if (address
->m_addr
)
1262 free(address
->m_addr
);
1264 address
->m_len
= len
;
1265 address
->m_addr
= (struct sockaddr
*) malloc(len
);
1267 if (address
->m_addr
== NULL
)
1269 address
->m_error
= GSOCK_MEMERR
;
1270 return GSOCK_MEMERR
;
1272 memcpy(address
->m_addr
, addr
, len
);
1274 return GSOCK_NOERROR
;
1277 GSocketError
_GAddress_translate_to(GAddress
*address
,
1278 struct sockaddr
**addr
, int *len
)
1280 if (!address
->m_addr
)
1282 address
->m_error
= GSOCK_INVADDR
;
1283 return GSOCK_INVADDR
;
1286 *len
= address
->m_len
;
1287 *addr
= (struct sockaddr
*) malloc(address
->m_len
);
1290 address
->m_error
= GSOCK_MEMERR
;
1291 return GSOCK_MEMERR
;
1294 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1295 return GSOCK_NOERROR
;
1299 * -------------------------------------------------------------------------
1300 * Internet address family
1301 * -------------------------------------------------------------------------
1304 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1306 address
->m_len
= sizeof(struct sockaddr_in
);
1307 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1308 if (address
->m_addr
== NULL
)
1310 address
->m_error
= GSOCK_MEMERR
;
1311 return GSOCK_MEMERR
;
1314 address
->m_family
= GSOCK_INET
;
1315 address
->m_realfamily
= PF_INET
;
1316 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1317 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1319 return GSOCK_NOERROR
;
1322 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1325 struct in_addr
*addr
;
1327 assert(address
!= NULL
);
1329 CHECK_ADDRESS(address
, INET
);
1331 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1333 addr
->s_addr
= inet_addr(hostname
);
1335 /* If it is a numeric host name, convert it now */
1336 if (addr
->s_addr
== INADDR_NONE
)
1338 struct in_addr
*array_addr
;
1340 /* It is a real name, we solve it */
1341 if ((he
= gethostbyname(hostname
)) == NULL
)
1343 /* addr->s_addr = INADDR_NONE just done by inet_addr() above */
1344 address
->m_error
= GSOCK_NOHOST
;
1345 return GSOCK_NOHOST
;
1347 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1348 addr
->s_addr
= array_addr
[0].s_addr
;
1350 return GSOCK_NOERROR
;
1353 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1355 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1358 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1359 unsigned long hostaddr
)
1361 struct in_addr
*addr
;
1363 assert(address
!= NULL
);
1365 CHECK_ADDRESS(address
, INET
);
1367 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1368 addr
->s_addr
= htonl(hostaddr
);;
1370 return GSOCK_NOERROR
;
1373 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1374 const char *protocol
)
1377 struct sockaddr_in
*addr
;
1379 assert(address
!= NULL
);
1380 CHECK_ADDRESS(address
, INET
);
1384 address
->m_error
= GSOCK_INVPORT
;
1385 return GSOCK_INVPORT
;
1388 se
= getservbyname(port
, protocol
);
1391 if (isdigit(port
[0]))
1395 port_int
= atoi(port
);
1396 addr
= (struct sockaddr_in
*)address
->m_addr
;
1397 addr
->sin_port
= htons((u_short
) port_int
);
1398 return GSOCK_NOERROR
;
1401 address
->m_error
= GSOCK_INVPORT
;
1402 return GSOCK_INVPORT
;
1405 addr
= (struct sockaddr_in
*)address
->m_addr
;
1406 addr
->sin_port
= se
->s_port
;
1408 return GSOCK_NOERROR
;
1411 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1413 struct sockaddr_in
*addr
;
1415 assert(address
!= NULL
);
1416 CHECK_ADDRESS(address
, INET
);
1418 addr
= (struct sockaddr_in
*)address
->m_addr
;
1419 addr
->sin_port
= htons(port
);
1421 return GSOCK_NOERROR
;
1424 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1428 struct sockaddr_in
*addr
;
1430 assert(address
!= NULL
);
1431 CHECK_ADDRESS(address
, INET
);
1433 addr
= (struct sockaddr_in
*)address
->m_addr
;
1434 addr_buf
= (char *)&(addr
->sin_addr
);
1436 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1439 address
->m_error
= GSOCK_NOHOST
;
1440 return GSOCK_NOHOST
;
1443 strncpy(hostname
, he
->h_name
, sbuf
);
1445 return GSOCK_NOERROR
;
1448 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1450 struct sockaddr_in
*addr
;
1452 assert(address
!= NULL
);
1453 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1455 addr
= (struct sockaddr_in
*)address
->m_addr
;
1457 return ntohl(addr
->sin_addr
.s_addr
);
1460 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1462 struct sockaddr_in
*addr
;
1464 assert(address
!= NULL
);
1465 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1467 addr
= (struct sockaddr_in
*)address
->m_addr
;
1468 return ntohs(addr
->sin_port
);
1472 * -------------------------------------------------------------------------
1473 * Unix address family
1474 * -------------------------------------------------------------------------
1477 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1479 assert (address
!= NULL
);
1480 address
->m_error
= GSOCK_INVADDR
;
1481 return GSOCK_INVADDR
;
1484 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1486 #if defined(__BORLANDC__)
1487 /* prevents unused variable message in Borland */
1490 assert (address
!= NULL
);
1491 address
->m_error
= GSOCK_INVADDR
;
1492 return GSOCK_INVADDR
;
1495 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1497 #if defined(__BORLANDC__)
1498 /* prevents unused variable message in Borland */
1502 assert (address
!= NULL
);
1503 address
->m_error
= GSOCK_INVADDR
;
1504 return GSOCK_INVADDR
;
1507 #else /* !wxUSE_SOCKETS */
1510 * Translation unit shouldn't be empty, so include this typedef to make the
1511 * compiler (VC++ 6.0, for example) happy
1513 typedef void (*wxDummy
)();
1515 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */