]>
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
;
993 GSocketError
GSocket_GetSockOpt(GSocket
*socket
, int level
, int optname
,
994 void *optval
, int *optlen
)
996 if (getsockopt(socket
->m_fd
, level
, optname
, optval
, optlen
) == 0)
998 return GSOCK_NOERROR
;
1000 return GSOCK_OPTERR
;
1003 GSocketError
GSocket_SetSockOpt(GSocket
*socket
, int level
, int optname
,
1004 const void *optval
, int optlen
)
1006 if (setsockopt(socket
->m_fd
, level
, optname
, optval
, optlen
) == 0)
1008 return GSOCK_NOERROR
;
1010 return GSOCK_OPTERR
;
1013 void GSocket_Streamed(GSocket
*socket
)
1015 socket
->m_stream
= TRUE
;
1018 void GSocket_Unstreamed(GSocket
*socket
)
1020 socket
->m_stream
= FALSE
;
1023 /* Internals (IO) */
1025 /* _GSocket_Input_Timeout:
1026 * For blocking sockets, wait until data is available or
1027 * until timeout ellapses.
1029 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
1033 if (!socket
->m_non_blocking
)
1036 FD_SET(socket
->m_fd
, &readfds
);
1037 if (select(0, &readfds
, NULL
, NULL
, &socket
->m_timeout
) == 0)
1039 socket
->m_error
= GSOCK_TIMEDOUT
;
1040 return GSOCK_TIMEDOUT
;
1043 return GSOCK_NOERROR
;
1046 /* _GSocket_Output_Timeout:
1047 * For blocking sockets, wait until data can be sent without
1048 * blocking or until timeout ellapses.
1050 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
1054 if (!socket
->m_non_blocking
)
1057 FD_SET(socket
->m_fd
, &writefds
);
1058 if (select(0, NULL
, &writefds
, NULL
, &socket
->m_timeout
) == 0)
1060 socket
->m_error
= GSOCK_TIMEDOUT
;
1061 return GSOCK_TIMEDOUT
;
1064 return GSOCK_NOERROR
;
1067 /* _GSocket_Connect_Timeout:
1068 * For blocking sockets, wait until the connection is
1069 * established or fails, or until timeout ellapses.
1071 GSocketError
_GSocket_Connect_Timeout(GSocket
*socket
)
1077 FD_ZERO(&exceptfds
);
1078 FD_SET(socket
->m_fd
, &writefds
);
1079 FD_SET(socket
->m_fd
, &exceptfds
);
1080 if (select(0, NULL
, &writefds
, &exceptfds
, &socket
->m_timeout
) == 0)
1082 socket
->m_error
= GSOCK_TIMEDOUT
;
1083 return GSOCK_TIMEDOUT
;
1085 if (!FD_ISSET(socket
->m_fd
, &writefds
))
1087 socket
->m_error
= GSOCK_IOERR
;
1091 return GSOCK_NOERROR
;
1094 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
1096 return recv(socket
->m_fd
, buffer
, size
, 0);
1099 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
1101 struct sockaddr from
;
1102 SOCKLEN_T fromlen
= sizeof(from
);
1106 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
1108 if (ret
== SOCKET_ERROR
)
1109 return SOCKET_ERROR
;
1111 /* Translate a system address into a GSocket address */
1112 if (!socket
->m_peer
)
1114 socket
->m_peer
= GAddress_new();
1115 if (!socket
->m_peer
)
1117 socket
->m_error
= GSOCK_MEMERR
;
1121 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
1122 if (err
!= GSOCK_NOERROR
)
1124 GAddress_destroy(socket
->m_peer
);
1125 socket
->m_peer
= NULL
;
1126 socket
->m_error
= err
;
1133 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
1135 return send(socket
->m_fd
, buffer
, size
, 0);
1138 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
1140 struct sockaddr
*addr
;
1144 if (!socket
->m_peer
)
1146 socket
->m_error
= GSOCK_INVADDR
;
1150 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
1151 if (err
!= GSOCK_NOERROR
)
1153 socket
->m_error
= err
;
1157 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
1159 /* Frees memory allocated by _GAddress_translate_to */
1167 * -------------------------------------------------------------------------
1169 * -------------------------------------------------------------------------
1172 /* CHECK_ADDRESS verifies that the current address family is either
1173 * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it
1174 * initalizes it to be a GSOCK_*family*. In other cases, it returns
1175 * an appropiate error code.
1177 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
1179 #define CHECK_ADDRESS(address, family) \
1181 if (address->m_family == GSOCK_NOFAMILY) \
1182 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1183 return address->m_error; \
1184 if (address->m_family != GSOCK_##family) \
1186 address->m_error = GSOCK_INVADDR; \
1187 return GSOCK_INVADDR; \
1191 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
1193 if (address->m_family == GSOCK_NOFAMILY) \
1194 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1196 if (address->m_family != GSOCK_##family) \
1198 address->m_error = GSOCK_INVADDR; \
1204 GAddress
*GAddress_new(void)
1208 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1211 address
->m_family
= GSOCK_NOFAMILY
;
1212 address
->m_addr
= NULL
;
1218 GAddress
*GAddress_copy(GAddress
*address
)
1222 assert(address
!= NULL
);
1224 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1227 memcpy(addr2
, address
, sizeof(GAddress
));
1229 if (address
->m_addr
)
1231 addr2
->m_addr
= (struct sockaddr
*) malloc(addr2
->m_len
);
1232 if (addr2
->m_addr
== NULL
)
1237 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1243 void GAddress_destroy(GAddress
*address
)
1245 assert(address
!= NULL
);
1247 if (address
->m_addr
)
1248 free(address
->m_addr
);
1253 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1255 assert(address
!= NULL
);
1257 address
->m_family
= type
;
1260 GAddressType
GAddress_GetFamily(GAddress
*address
)
1262 assert(address
!= NULL
);
1264 return address
->m_family
;
1267 GSocketError
_GAddress_translate_from(GAddress
*address
,
1268 struct sockaddr
*addr
, int len
)
1270 address
->m_realfamily
= addr
->sa_family
;
1271 switch (addr
->sa_family
)
1274 address
->m_family
= GSOCK_INET
;
1277 address
->m_family
= GSOCK_UNIX
;
1281 address
->m_family
= GSOCK_INET6
;
1286 address
->m_error
= GSOCK_INVOP
;
1291 if (address
->m_addr
)
1292 free(address
->m_addr
);
1294 address
->m_len
= len
;
1295 address
->m_addr
= (struct sockaddr
*) malloc(len
);
1297 if (address
->m_addr
== NULL
)
1299 address
->m_error
= GSOCK_MEMERR
;
1300 return GSOCK_MEMERR
;
1302 memcpy(address
->m_addr
, addr
, len
);
1304 return GSOCK_NOERROR
;
1307 GSocketError
_GAddress_translate_to(GAddress
*address
,
1308 struct sockaddr
**addr
, int *len
)
1310 if (!address
->m_addr
)
1312 address
->m_error
= GSOCK_INVADDR
;
1313 return GSOCK_INVADDR
;
1316 *len
= address
->m_len
;
1317 *addr
= (struct sockaddr
*) malloc(address
->m_len
);
1320 address
->m_error
= GSOCK_MEMERR
;
1321 return GSOCK_MEMERR
;
1324 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1325 return GSOCK_NOERROR
;
1329 * -------------------------------------------------------------------------
1330 * Internet address family
1331 * -------------------------------------------------------------------------
1334 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1336 address
->m_len
= sizeof(struct sockaddr_in
);
1337 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1338 if (address
->m_addr
== NULL
)
1340 address
->m_error
= GSOCK_MEMERR
;
1341 return GSOCK_MEMERR
;
1344 address
->m_family
= GSOCK_INET
;
1345 address
->m_realfamily
= AF_INET
;
1346 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1347 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1349 return GSOCK_NOERROR
;
1352 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1355 struct in_addr
*addr
;
1357 assert(address
!= NULL
);
1359 CHECK_ADDRESS(address
, INET
);
1361 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1363 addr
->s_addr
= inet_addr(hostname
);
1365 /* If it is a numeric host name, convert it now */
1366 if (addr
->s_addr
== INADDR_NONE
)
1368 struct in_addr
*array_addr
;
1370 /* It is a real name, we solve it */
1371 if ((he
= gethostbyname(hostname
)) == NULL
)
1373 /* addr->s_addr = INADDR_NONE just done by inet_addr() above */
1374 address
->m_error
= GSOCK_NOHOST
;
1375 return GSOCK_NOHOST
;
1377 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1378 addr
->s_addr
= array_addr
[0].s_addr
;
1380 return GSOCK_NOERROR
;
1383 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1385 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1388 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1389 unsigned long hostaddr
)
1391 struct in_addr
*addr
;
1393 assert(address
!= NULL
);
1395 CHECK_ADDRESS(address
, INET
);
1397 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1398 addr
->s_addr
= htonl(hostaddr
);;
1400 return GSOCK_NOERROR
;
1403 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1404 const char *protocol
)
1407 struct sockaddr_in
*addr
;
1409 assert(address
!= NULL
);
1410 CHECK_ADDRESS(address
, INET
);
1414 address
->m_error
= GSOCK_INVPORT
;
1415 return GSOCK_INVPORT
;
1418 se
= getservbyname(port
, protocol
);
1421 if (isdigit(port
[0]))
1425 port_int
= atoi(port
);
1426 addr
= (struct sockaddr_in
*)address
->m_addr
;
1427 addr
->sin_port
= htons((u_short
) port_int
);
1428 return GSOCK_NOERROR
;
1431 address
->m_error
= GSOCK_INVPORT
;
1432 return GSOCK_INVPORT
;
1435 addr
= (struct sockaddr_in
*)address
->m_addr
;
1436 addr
->sin_port
= se
->s_port
;
1438 return GSOCK_NOERROR
;
1441 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1443 struct sockaddr_in
*addr
;
1445 assert(address
!= NULL
);
1446 CHECK_ADDRESS(address
, INET
);
1448 addr
= (struct sockaddr_in
*)address
->m_addr
;
1449 addr
->sin_port
= htons(port
);
1451 return GSOCK_NOERROR
;
1454 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1458 struct sockaddr_in
*addr
;
1460 assert(address
!= NULL
);
1461 CHECK_ADDRESS(address
, INET
);
1463 addr
= (struct sockaddr_in
*)address
->m_addr
;
1464 addr_buf
= (char *)&(addr
->sin_addr
);
1466 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1469 address
->m_error
= GSOCK_NOHOST
;
1470 return GSOCK_NOHOST
;
1473 strncpy(hostname
, he
->h_name
, sbuf
);
1475 return GSOCK_NOERROR
;
1478 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1480 struct sockaddr_in
*addr
;
1482 assert(address
!= NULL
);
1483 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1485 addr
= (struct sockaddr_in
*)address
->m_addr
;
1487 return ntohl(addr
->sin_addr
.s_addr
);
1490 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1492 struct sockaddr_in
*addr
;
1494 assert(address
!= NULL
);
1495 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1497 addr
= (struct sockaddr_in
*)address
->m_addr
;
1498 return ntohs(addr
->sin_port
);
1502 * -------------------------------------------------------------------------
1503 * Unix address family
1504 * -------------------------------------------------------------------------
1507 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1509 assert (address
!= NULL
);
1510 address
->m_error
= GSOCK_INVADDR
;
1511 return GSOCK_INVADDR
;
1514 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1516 #if defined(__BORLANDC__)
1517 /* prevents unused variable message in Borland */
1520 assert (address
!= NULL
);
1521 address
->m_error
= GSOCK_INVADDR
;
1522 return GSOCK_INVADDR
;
1525 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1527 #if defined(__BORLANDC__)
1528 /* prevents unused variable message in Borland */
1532 assert (address
!= NULL
);
1533 address
->m_error
= GSOCK_INVADDR
;
1534 return GSOCK_INVADDR
;
1537 #else /* !wxUSE_SOCKETS */
1540 * Translation unit shouldn't be empty, so include this typedef to make the
1541 * compiler (VC++ 6.0, for example) happy
1543 typedef void (*wxDummy
)();
1545 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */