]>
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 wxWindows 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)
32 #ifndef __GSOCKET_STANDALONE__
33 # include "wx/platform.h"
34 # include "wx/setup.h"
37 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
39 #ifndef __GSOCKET_STANDALONE__
40 # include "wx/msw/gsockmsw.h"
41 # include "wx/gsocket.h"
43 # include "gsockmsw.h"
45 #endif /* __GSOCKET_STANDALONE__ */
52 #define isdigit(x) (x > 47 && x < 58)
54 #include "wx/msw/wince/net.h"
63 /* if we use configure for MSW SOCKLEN_T will be already defined */
65 # define SOCKLEN_T int
68 /* Table of GUI-related functions. We must call them indirectly because
69 * of wxBase and GUI separation: */
71 static struct GSocketGUIFunctionsTable
*gs_gui_functions
;
73 #define USE_GUI() (gs_gui_functions != NULL)
75 /* Define macros to simplify indirection: */
76 #define _GSocket_GUI_Init() \
77 if (gs_gui_functions) gs_gui_functions->GUI_Init()
78 #define _GSocket_GUI_Cleanup() \
79 if (gs_gui_functions) gs_gui_functions->GUI_Cleanup()
80 #define _GSocket_GUI_Init_Socket(socket) \
81 (gs_gui_functions ? gs_gui_functions->GUI_Init_Socket(socket) : 1)
82 #define _GSocket_GUI_Destroy_Socket(socket) \
83 if (gs_gui_functions) gs_gui_functions->GUI_Destroy_Socket(socket)
84 #define _GSocket_Enable_Events(socket) \
85 if (gs_gui_functions) gs_gui_functions->Enable_Events(socket)
86 #define _GSocket_Disable_Events(socket) \
87 if (gs_gui_functions) gs_gui_functions->Disable_Events(socket)
88 #define _GSocket_Install_Callback(socket, event) \
89 if (gs_gui_functions) gs_gui_functions->Install_Callback(socket, event)
90 #define _GSocket_Uninstall_Callback(socket, event) \
91 if (gs_gui_functions) gs_gui_functions->Uninstall_Callback(socket, event)
93 /* Global initialisers */
95 void GSocket_SetGUIFunctions(struct GSocketGUIFunctionsTable
*guifunc
)
97 gs_gui_functions
= guifunc
;
100 int GSocket_Init(void)
104 if (gs_gui_functions
)
106 if ( !gs_gui_functions
->GUI_Init() )
110 /* Initialize WinSocket */
111 return (WSAStartup((1 << 8) | 1, &wsaData
) == 0);
114 void GSocket_Cleanup(void)
116 if (gs_gui_functions
)
118 gs_gui_functions
->GUI_Cleanup();
121 /* Cleanup WinSocket */
125 /* Constructors / Destructors for GSocket */
127 GSocket
*GSocket_new(void)
132 if ((socket
= (GSocket
*) malloc(sizeof(GSocket
))) == NULL
)
135 socket
->m_fd
= INVALID_SOCKET
;
136 for (i
= 0; i
< GSOCK_MAX_EVENT
; i
++)
138 socket
->m_cbacks
[i
] = NULL
;
140 socket
->m_detected
= 0;
141 socket
->m_local
= NULL
;
142 socket
->m_peer
= NULL
;
143 socket
->m_error
= GSOCK_NOERROR
;
144 socket
->m_server
= FALSE
;
145 socket
->m_stream
= TRUE
;
146 socket
->m_non_blocking
= FALSE
;
147 socket
->m_timeout
.tv_sec
= 10 * 60; /* 10 minutes */
148 socket
->m_timeout
.tv_usec
= 0;
149 socket
->m_establishing
= FALSE
;
151 /* Per-socket GUI-specific initialization */
152 success
= _GSocket_GUI_Init_Socket(socket
);
162 void GSocket_close(GSocket
*socket
)
164 _GSocket_Disable_Events(socket
);
165 closesocket(socket
->m_fd
);
166 socket
->m_fd
= INVALID_SOCKET
;
169 void GSocket_destroy(GSocket
*socket
)
171 assert(socket
!= NULL
);
173 /* Per-socket GUI-specific cleanup */
174 _GSocket_GUI_Destroy_Socket(socket
);
176 /* Check that the socket is really shutdowned */
177 if (socket
->m_fd
!= INVALID_SOCKET
)
178 GSocket_Shutdown(socket
);
180 /* Destroy private addresses */
182 GAddress_destroy(socket
->m_local
);
185 GAddress_destroy(socket
->m_peer
);
187 /* Destroy the socket itself */
192 * Disallow further read/write operations on this socket, close
193 * the fd and disable all callbacks.
195 void GSocket_Shutdown(GSocket
*socket
)
199 assert(socket
!= NULL
);
201 /* If socket has been created, shutdown it */
202 if (socket
->m_fd
!= INVALID_SOCKET
)
204 shutdown(socket
->m_fd
, 2);
205 GSocket_close(socket
);
208 /* Disable GUI callbacks */
209 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
210 socket
->m_cbacks
[evt
] = NULL
;
212 socket
->m_detected
= GSOCK_LOST_FLAG
;
215 /* Address handling */
221 * Set or get the local or peer address for this socket. The 'set'
222 * functions return GSOCK_NOERROR on success, an error code otherwise.
223 * The 'get' functions return a pointer to a GAddress object on success,
224 * or NULL otherwise, in which case they set the error code of the
225 * corresponding GSocket.
228 * GSOCK_INVSOCK - the socket is not valid.
229 * GSOCK_INVADDR - the address is not valid.
231 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
233 assert(socket
!= NULL
);
235 /* the socket must be initialized, or it must be a server */
236 if (socket
->m_fd
!= INVALID_SOCKET
&& !socket
->m_server
)
238 socket
->m_error
= GSOCK_INVSOCK
;
239 return GSOCK_INVSOCK
;
243 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
245 socket
->m_error
= GSOCK_INVADDR
;
246 return GSOCK_INVADDR
;
250 GAddress_destroy(socket
->m_local
);
252 socket
->m_local
= GAddress_copy(address
);
254 return GSOCK_NOERROR
;
257 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
259 assert(socket
!= NULL
);
262 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
264 socket
->m_error
= GSOCK_INVADDR
;
265 return GSOCK_INVADDR
;
269 GAddress_destroy(socket
->m_peer
);
271 socket
->m_peer
= GAddress_copy(address
);
273 return GSOCK_NOERROR
;
276 GAddress
*GSocket_GetLocal(GSocket
*socket
)
279 struct sockaddr addr
;
280 SOCKLEN_T size
= sizeof(addr
);
283 assert(socket
!= NULL
);
285 /* try to get it from the m_local var first */
287 return GAddress_copy(socket
->m_local
);
289 /* else, if the socket is initialized, try getsockname */
290 if (socket
->m_fd
== INVALID_SOCKET
)
292 socket
->m_error
= GSOCK_INVSOCK
;
296 if (getsockname(socket
->m_fd
, &addr
, &size
) == SOCKET_ERROR
)
298 socket
->m_error
= GSOCK_IOERR
;
302 /* got a valid address from getsockname, create a GAddress object */
303 if ((address
= GAddress_new()) == NULL
)
305 socket
->m_error
= GSOCK_MEMERR
;
309 if ((err
= _GAddress_translate_from(address
, &addr
, size
)) != GSOCK_NOERROR
)
311 GAddress_destroy(address
);
312 socket
->m_error
= err
;
319 GAddress
*GSocket_GetPeer(GSocket
*socket
)
321 assert(socket
!= NULL
);
323 /* try to get it from the m_peer var */
325 return GAddress_copy(socket
->m_peer
);
330 /* Server specific parts */
332 /* GSocket_SetServer:
333 * Sets up this socket as a server. The local address must have been
334 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
335 * Returns GSOCK_NOERROR on success, one of the following otherwise:
338 * GSOCK_INVSOCK - the socket is in use.
339 * GSOCK_INVADDR - the local address has not been set.
340 * GSOCK_IOERR - low-level error.
342 GSocketError
GSocket_SetServer(GSocket
*sck
)
348 /* must not be in use */
349 if (sck
->m_fd
!= INVALID_SOCKET
)
351 sck
->m_error
= GSOCK_INVSOCK
;
352 return GSOCK_INVSOCK
;
355 /* the local addr must have been set */
358 sck
->m_error
= GSOCK_INVADDR
;
359 return GSOCK_INVADDR
;
362 /* Initialize all fields */
363 sck
->m_server
= TRUE
;
364 sck
->m_stream
= TRUE
;
365 sck
->m_oriented
= TRUE
;
367 /* Create the socket */
368 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
370 if (sck
->m_fd
== INVALID_SOCKET
)
372 sck
->m_error
= GSOCK_IOERR
;
376 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
377 _GSocket_Enable_Events(sck
);
379 /* allow a socket to re-bind if the socket is in the TIME_WAIT
380 state after being previously closed.
382 setsockopt(sck
->m_fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char*)&arg
, sizeof(u_long
));
384 /* Bind to the local address,
385 * retrieve the actual address bound,
386 * and listen up to 5 connections.
388 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
389 (getsockname(sck
->m_fd
,
390 sck
->m_local
->m_addr
,
391 (SOCKLEN_T
*)&sck
->m_local
->m_len
) != 0) ||
392 (listen(sck
->m_fd
, 5) != 0))
395 sck
->m_error
= GSOCK_IOERR
;
399 return GSOCK_NOERROR
;
402 /* GSocket_WaitConnection:
403 * Waits for an incoming client connection. Returns a pointer to
404 * a GSocket object, or NULL if there was an error, in which case
405 * the last error field will be updated for the calling GSocket.
407 * Error codes (set in the calling GSocket)
408 * GSOCK_INVSOCK - the socket is not valid or not a server.
409 * GSOCK_TIMEDOUT - timeout, no incoming connections.
410 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
411 * GSOCK_MEMERR - couldn't allocate memory.
412 * GSOCK_IOERR - low-level error.
414 GSocket
*GSocket_WaitConnection(GSocket
*sck
)
417 struct sockaddr from
;
418 SOCKLEN_T fromlen
= sizeof(from
);
424 /* Reenable CONNECTION events */
425 sck
->m_detected
&= ~GSOCK_CONNECTION_FLAG
;
427 /* If the socket has already been created, we exit immediately */
428 if (sck
->m_fd
== INVALID_SOCKET
|| !sck
->m_server
)
430 sck
->m_error
= GSOCK_INVSOCK
;
434 /* Create a GSocket object for the new connection */
435 connection
= GSocket_new();
439 sck
->m_error
= GSOCK_MEMERR
;
443 /* Wait for a connection (with timeout) */
444 if (_GSocket_Input_Timeout(sck
) == GSOCK_TIMEDOUT
)
446 GSocket_destroy(connection
);
447 /* sck->m_error set by _GSocket_Input_Timeout */
451 connection
->m_fd
= accept(sck
->m_fd
, &from
, &fromlen
);
453 if (connection
->m_fd
== INVALID_SOCKET
)
455 if (WSAGetLastError() == WSAEWOULDBLOCK
)
456 sck
->m_error
= GSOCK_WOULDBLOCK
;
458 sck
->m_error
= GSOCK_IOERR
;
460 GSocket_destroy(connection
);
464 /* Initialize all fields */
465 connection
->m_server
= FALSE
;
466 connection
->m_stream
= TRUE
;
467 connection
->m_oriented
= TRUE
;
469 /* Setup the peer address field */
470 connection
->m_peer
= GAddress_new();
471 if (!connection
->m_peer
)
473 GSocket_destroy(connection
);
474 sck
->m_error
= GSOCK_MEMERR
;
477 err
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
);
478 if (err
!= GSOCK_NOERROR
)
480 GAddress_destroy(connection
->m_peer
);
481 GSocket_destroy(connection
);
486 ioctlsocket(connection
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
487 _GSocket_Enable_Events(connection
);
492 /* Client specific parts */
495 * For stream (connection oriented) sockets, GSocket_Connect() tries
496 * to establish a client connection to a server using the peer address
497 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
498 * connection has been succesfully established, or one of the error
499 * codes listed below. Note that for nonblocking sockets, a return
500 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
501 * request can be completed later; you should use GSocket_Select()
502 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
503 * corresponding asynchronous events.
505 * For datagram (non connection oriented) sockets, GSocket_Connect()
506 * just sets the peer address established with GSocket_SetPeer() as
507 * default destination.
510 * GSOCK_INVSOCK - the socket is in use or not valid.
511 * GSOCK_INVADDR - the peer address has not been established.
512 * GSOCK_TIMEDOUT - timeout, the connection failed.
513 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
514 * GSOCK_MEMERR - couldn't allocate memory.
515 * GSOCK_IOERR - low-level error.
517 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
524 /* Enable CONNECTION events (needed for nonblocking connections) */
525 sck
->m_detected
&= ~GSOCK_CONNECTION_FLAG
;
527 if (sck
->m_fd
!= INVALID_SOCKET
)
529 sck
->m_error
= GSOCK_INVSOCK
;
530 return GSOCK_INVSOCK
;
535 sck
->m_error
= GSOCK_INVADDR
;
536 return GSOCK_INVADDR
;
539 /* Streamed or dgram socket? */
540 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
541 sck
->m_oriented
= TRUE
;
542 sck
->m_server
= FALSE
;
543 sck
->m_establishing
= FALSE
;
545 /* Create the socket */
546 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
,
547 sck
->m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
549 if (sck
->m_fd
== INVALID_SOCKET
)
551 sck
->m_error
= GSOCK_IOERR
;
555 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
556 _GSocket_Enable_Events(sck
);
558 /* Connect it to the peer address, with a timeout (see below) */
559 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
561 if (ret
== SOCKET_ERROR
)
563 err
= WSAGetLastError();
565 /* If connect failed with EWOULDBLOCK and the GSocket object
566 * is in blocking mode, we select() for the specified timeout
567 * checking for writability to see if the connection request
570 if ((err
== WSAEWOULDBLOCK
) && (!sck
->m_non_blocking
))
572 err
= _GSocket_Connect_Timeout(sck
);
574 if (err
!= GSOCK_NOERROR
)
577 /* sck->m_error is set in _GSocket_Connect_Timeout */
580 return (GSocketError
) err
;
583 /* If connect failed with EWOULDBLOCK and the GSocket object
584 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
585 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
586 * this way if the connection completes, a GSOCK_CONNECTION
587 * event will be generated, if enabled.
589 if ((err
== WSAEWOULDBLOCK
) && (sck
->m_non_blocking
))
591 sck
->m_establishing
= TRUE
;
592 sck
->m_error
= GSOCK_WOULDBLOCK
;
593 return GSOCK_WOULDBLOCK
;
596 /* If connect failed with an error other than EWOULDBLOCK,
597 * then the call to GSocket_Connect() has failed.
600 sck
->m_error
= GSOCK_IOERR
;
604 return GSOCK_NOERROR
;
607 /* Datagram sockets */
609 /* GSocket_SetNonOriented:
610 * Sets up this socket as a non-connection oriented (datagram) socket.
611 * Before using this function, the local address must have been set
612 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
613 * on success, or one of the following otherwise.
616 * GSOCK_INVSOCK - the socket is in use.
617 * GSOCK_INVADDR - the local address has not been set.
618 * GSOCK_IOERR - low-level error.
620 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
626 if (sck
->m_fd
!= INVALID_SOCKET
)
628 sck
->m_error
= GSOCK_INVSOCK
;
629 return GSOCK_INVSOCK
;
634 sck
->m_error
= GSOCK_INVADDR
;
635 return GSOCK_INVADDR
;
638 /* Initialize all fields */
639 sck
->m_stream
= FALSE
;
640 sck
->m_server
= FALSE
;
641 sck
->m_oriented
= FALSE
;
643 /* Create the socket */
644 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
646 if (sck
->m_fd
== INVALID_SOCKET
)
648 sck
->m_error
= GSOCK_IOERR
;
652 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
653 _GSocket_Enable_Events(sck
);
655 /* Bind to the local address,
656 * and retrieve the actual address bound.
658 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
659 (getsockname(sck
->m_fd
,
660 sck
->m_local
->m_addr
,
661 (SOCKLEN_T
*)&sck
->m_local
->m_len
) != 0))
664 sck
->m_error
= GSOCK_IOERR
;
668 return GSOCK_NOERROR
;
673 /* Like recv(), send(), ... */
674 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
678 assert(socket
!= NULL
);
680 /* Reenable INPUT events */
681 socket
->m_detected
&= ~GSOCK_INPUT_FLAG
;
683 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
685 socket
->m_error
= GSOCK_INVSOCK
;
689 /* If the socket is blocking, wait for data (with a timeout) */
690 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
694 if (socket
->m_stream
)
695 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
697 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
699 if (ret
== SOCKET_ERROR
)
701 if (WSAGetLastError() != WSAEWOULDBLOCK
)
702 socket
->m_error
= GSOCK_IOERR
;
704 socket
->m_error
= GSOCK_WOULDBLOCK
;
711 int GSocket_Write(GSocket
*socket
, const char *buffer
, int size
)
715 assert(socket
!= NULL
);
717 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
719 socket
->m_error
= GSOCK_INVSOCK
;
723 /* If the socket is blocking, wait for writability (with a timeout) */
724 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
728 if (socket
->m_stream
)
729 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
731 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
733 if (ret
== SOCKET_ERROR
)
735 if (WSAGetLastError() != WSAEWOULDBLOCK
)
736 socket
->m_error
= GSOCK_IOERR
;
738 socket
->m_error
= GSOCK_WOULDBLOCK
;
740 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
741 * does). Once the first OUTPUT event is received, users can assume
742 * that the socket is writable until a read operation fails. Only then
743 * will further OUTPUT events be posted.
745 socket
->m_detected
&= ~GSOCK_OUTPUT_FLAG
;
753 * Polls the socket to determine its status. This function will
754 * check for the events specified in the 'flags' parameter, and
755 * it will return a mask indicating which operations can be
756 * performed. This function won't block, regardless of the
757 * mode (blocking | nonblocking) of the socket.
759 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
763 GSocketEventFlags result
= 0;
768 assert(socket
!= NULL
);
773 FD_SET(socket
->m_fd
, &readfds
);
774 if (flags
& GSOCK_OUTPUT_FLAG
)
775 FD_SET(socket
->m_fd
, &writefds
);
776 FD_SET(socket
->m_fd
, &exceptfds
);
778 /* Check 'sticky' CONNECTION flag first */
779 result
|= (GSOCK_CONNECTION_FLAG
& socket
->m_detected
);
781 /* If we have already detected a LOST event, then don't try
782 * to do any further processing.
784 if ((socket
->m_detected
& GSOCK_LOST_FLAG
) != 0)
786 socket
->m_establishing
= FALSE
;
788 return (GSOCK_LOST_FLAG
& flags
);
792 if (select(socket
->m_fd
+ 1, &readfds
, &writefds
, &exceptfds
,
793 &socket
->m_timeout
) <= 0)
795 /* What to do here? */
796 return (result
& flags
);
799 /* Check for readability */
800 if (FD_ISSET(socket
->m_fd
, &readfds
))
804 if (!socket
->m_stream
|| recv(socket
->m_fd
, &c
, 1, MSG_PEEK
) > 0)
806 result
|= GSOCK_INPUT_FLAG
;
810 if (socket
->m_server
&& socket
->m_stream
)
812 result
|= GSOCK_CONNECTION_FLAG
;
813 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
817 socket
->m_detected
= GSOCK_LOST_FLAG
;
818 socket
->m_establishing
= FALSE
;
820 /* LOST event: Abort any further processing */
821 return (GSOCK_LOST_FLAG
& flags
);
826 /* Check for writability */
827 if (FD_ISSET(socket
->m_fd
, &writefds
))
829 if (socket
->m_establishing
&& !socket
->m_server
)
832 SOCKLEN_T len
= sizeof(error
);
834 socket
->m_establishing
= FALSE
;
836 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*)&error
, &len
);
840 socket
->m_detected
= GSOCK_LOST_FLAG
;
842 /* LOST event: Abort any further processing */
843 return (GSOCK_LOST_FLAG
& flags
);
847 result
|= GSOCK_CONNECTION_FLAG
;
848 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
853 result
|= GSOCK_OUTPUT_FLAG
;
857 /* Check for exceptions and errors (is this useful in Unices?) */
858 if (FD_ISSET(socket
->m_fd
, &exceptfds
))
860 socket
->m_establishing
= FALSE
;
861 socket
->m_detected
= GSOCK_LOST_FLAG
;
863 /* LOST event: Abort any further processing */
864 return (GSOCK_LOST_FLAG
& flags
);
867 return (result
& flags
);
871 assert(socket
!= NULL
);
872 return flags
& socket
->m_detected
;
878 /* GSocket_SetNonBlocking:
879 * Sets the socket to non-blocking mode. All IO calls will return
882 void GSocket_SetNonBlocking(GSocket
*socket
, int non_block
)
884 assert(socket
!= NULL
);
886 socket
->m_non_blocking
= non_block
;
889 /* GSocket_SetTimeout:
890 * Sets the timeout for blocking calls. Time is expressed in
893 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millis
)
895 assert(socket
!= NULL
);
897 socket
->m_timeout
.tv_sec
= (millis
/ 1000);
898 socket
->m_timeout
.tv_usec
= (millis
% 1000) * 1000;
902 * Returns the last error occured for this socket. Note that successful
903 * operations do not clear this back to GSOCK_NOERROR, so use it only
906 GSocketError
GSocket_GetError(GSocket
*socket
)
908 assert(socket
!= NULL
);
910 return socket
->m_error
;
916 * There is data to be read in the input buffer. If, after a read
917 * operation, there is still data available, the callback function will
920 * The socket is available for writing. That is, the next write call
921 * won't block. This event is generated only once, when the connection is
922 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
923 * when the output buffer empties again. This means that the app should
924 * assume that it can write since the first OUTPUT event, and no more
925 * OUTPUT events will be generated unless an error occurs.
927 * Connection succesfully established, for client sockets, or incoming
928 * client connection, for server sockets. Wait for this event (also watch
929 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
931 * The connection is lost (or a connection request failed); this could
932 * be due to a failure, or due to the peer closing it gracefully.
935 /* GSocket_SetCallback:
936 * Enables the callbacks specified by 'flags'. Note that 'flags'
937 * may be a combination of flags OR'ed toghether, so the same
938 * callback function can be made to accept different events.
939 * The callback function must have the following prototype:
941 * void function(GSocket *socket, GSocketEvent event, char *cdata)
943 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
944 GSocketCallback callback
, char *cdata
)
948 assert(socket
!= NULL
);
950 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
952 if ((flags
& (1 << count
)) != 0)
954 socket
->m_cbacks
[count
] = callback
;
955 socket
->m_data
[count
] = cdata
;
960 /* GSocket_UnsetCallback:
961 * Disables all callbacks specified by 'flags', which may be a
962 * combination of flags OR'ed toghether.
964 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
968 assert(socket
!= NULL
);
970 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
972 if ((flags
& (1 << count
)) != 0)
974 socket
->m_cbacks
[count
] = NULL
;
975 socket
->m_data
[count
] = NULL
;
982 /* _GSocket_Input_Timeout:
983 * For blocking sockets, wait until data is available or
984 * until timeout ellapses.
986 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
990 if (!socket
->m_non_blocking
)
993 FD_SET(socket
->m_fd
, &readfds
);
994 if (select(0, &readfds
, NULL
, NULL
, &socket
->m_timeout
) == 0)
996 socket
->m_error
= GSOCK_TIMEDOUT
;
997 return GSOCK_TIMEDOUT
;
1000 return GSOCK_NOERROR
;
1003 /* _GSocket_Output_Timeout:
1004 * For blocking sockets, wait until data can be sent without
1005 * blocking or until timeout ellapses.
1007 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
1011 if (!socket
->m_non_blocking
)
1014 FD_SET(socket
->m_fd
, &writefds
);
1015 if (select(0, NULL
, &writefds
, NULL
, &socket
->m_timeout
) == 0)
1017 socket
->m_error
= GSOCK_TIMEDOUT
;
1018 return GSOCK_TIMEDOUT
;
1021 return GSOCK_NOERROR
;
1024 /* _GSocket_Connect_Timeout:
1025 * For blocking sockets, wait until the connection is
1026 * established or fails, or until timeout ellapses.
1028 GSocketError
_GSocket_Connect_Timeout(GSocket
*socket
)
1034 FD_ZERO(&exceptfds
);
1035 FD_SET(socket
->m_fd
, &writefds
);
1036 FD_SET(socket
->m_fd
, &exceptfds
);
1037 if (select(0, NULL
, &writefds
, &exceptfds
, &socket
->m_timeout
) == 0)
1039 socket
->m_error
= GSOCK_TIMEDOUT
;
1040 return GSOCK_TIMEDOUT
;
1042 if (!FD_ISSET(socket
->m_fd
, &writefds
))
1044 socket
->m_error
= GSOCK_IOERR
;
1048 return GSOCK_NOERROR
;
1051 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
1053 return recv(socket
->m_fd
, buffer
, size
, 0);
1056 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
1058 struct sockaddr from
;
1059 SOCKLEN_T fromlen
= sizeof(from
);
1063 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
1065 if (ret
== SOCKET_ERROR
)
1066 return SOCKET_ERROR
;
1068 /* Translate a system address into a GSocket address */
1069 if (!socket
->m_peer
)
1071 socket
->m_peer
= GAddress_new();
1072 if (!socket
->m_peer
)
1074 socket
->m_error
= GSOCK_MEMERR
;
1078 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
1079 if (err
!= GSOCK_NOERROR
)
1081 GAddress_destroy(socket
->m_peer
);
1082 socket
->m_peer
= NULL
;
1083 socket
->m_error
= err
;
1090 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
1092 return send(socket
->m_fd
, buffer
, size
, 0);
1095 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
1097 struct sockaddr
*addr
;
1101 if (!socket
->m_peer
)
1103 socket
->m_error
= GSOCK_INVADDR
;
1107 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
1108 if (err
!= GSOCK_NOERROR
)
1110 socket
->m_error
= err
;
1114 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
1116 /* Frees memory allocated by _GAddress_translate_to */
1124 * -------------------------------------------------------------------------
1126 * -------------------------------------------------------------------------
1129 /* CHECK_ADDRESS verifies that the current address family is either
1130 * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it
1131 * initalizes it to be a GSOCK_*family*. In other cases, it returns
1132 * an appropiate error code.
1134 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
1136 #define CHECK_ADDRESS(address, family) \
1138 if (address->m_family == GSOCK_NOFAMILY) \
1139 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1140 return address->m_error; \
1141 if (address->m_family != GSOCK_##family) \
1143 address->m_error = GSOCK_INVADDR; \
1144 return GSOCK_INVADDR; \
1148 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
1150 if (address->m_family == GSOCK_NOFAMILY) \
1151 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1153 if (address->m_family != GSOCK_##family) \
1155 address->m_error = GSOCK_INVADDR; \
1161 GAddress
*GAddress_new(void)
1165 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1168 address
->m_family
= GSOCK_NOFAMILY
;
1169 address
->m_addr
= NULL
;
1175 GAddress
*GAddress_copy(GAddress
*address
)
1179 assert(address
!= NULL
);
1181 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1184 memcpy(addr2
, address
, sizeof(GAddress
));
1186 if (address
->m_addr
)
1188 addr2
->m_addr
= (struct sockaddr
*) malloc(addr2
->m_len
);
1189 if (addr2
->m_addr
== NULL
)
1194 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1200 void GAddress_destroy(GAddress
*address
)
1202 assert(address
!= NULL
);
1204 if (address
->m_addr
)
1205 free(address
->m_addr
);
1210 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1212 assert(address
!= NULL
);
1214 address
->m_family
= type
;
1217 GAddressType
GAddress_GetFamily(GAddress
*address
)
1219 assert(address
!= NULL
);
1221 return address
->m_family
;
1224 GSocketError
_GAddress_translate_from(GAddress
*address
,
1225 struct sockaddr
*addr
, int len
)
1227 address
->m_realfamily
= addr
->sa_family
;
1228 switch (addr
->sa_family
)
1231 address
->m_family
= GSOCK_INET
;
1234 address
->m_family
= GSOCK_UNIX
;
1238 address
->m_family
= GSOCK_INET6
;
1243 address
->m_error
= GSOCK_INVOP
;
1248 if (address
->m_addr
)
1249 free(address
->m_addr
);
1251 address
->m_len
= len
;
1252 address
->m_addr
= (struct sockaddr
*) malloc(len
);
1254 if (address
->m_addr
== NULL
)
1256 address
->m_error
= GSOCK_MEMERR
;
1257 return GSOCK_MEMERR
;
1259 memcpy(address
->m_addr
, addr
, len
);
1261 return GSOCK_NOERROR
;
1264 GSocketError
_GAddress_translate_to(GAddress
*address
,
1265 struct sockaddr
**addr
, int *len
)
1267 if (!address
->m_addr
)
1269 address
->m_error
= GSOCK_INVADDR
;
1270 return GSOCK_INVADDR
;
1273 *len
= address
->m_len
;
1274 *addr
= (struct sockaddr
*) malloc(address
->m_len
);
1277 address
->m_error
= GSOCK_MEMERR
;
1278 return GSOCK_MEMERR
;
1281 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1282 return GSOCK_NOERROR
;
1286 * -------------------------------------------------------------------------
1287 * Internet address family
1288 * -------------------------------------------------------------------------
1291 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1293 address
->m_len
= sizeof(struct sockaddr_in
);
1294 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1295 if (address
->m_addr
== NULL
)
1297 address
->m_error
= GSOCK_MEMERR
;
1298 return GSOCK_MEMERR
;
1301 address
->m_family
= GSOCK_INET
;
1302 address
->m_realfamily
= PF_INET
;
1303 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1304 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1306 return GSOCK_NOERROR
;
1309 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1312 struct in_addr
*addr
;
1314 assert(address
!= NULL
);
1316 CHECK_ADDRESS(address
, INET
);
1318 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1320 addr
->s_addr
= inet_addr(hostname
);
1322 /* If it is a numeric host name, convert it now */
1323 if (addr
->s_addr
== INADDR_NONE
)
1325 struct in_addr
*array_addr
;
1327 /* It is a real name, we solve it */
1328 if ((he
= gethostbyname(hostname
)) == NULL
)
1330 /* addr->s_addr = INADDR_NONE just done by inet_addr() above */
1331 address
->m_error
= GSOCK_NOHOST
;
1332 return GSOCK_NOHOST
;
1334 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1335 addr
->s_addr
= array_addr
[0].s_addr
;
1337 return GSOCK_NOERROR
;
1340 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1342 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1345 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1346 unsigned long hostaddr
)
1348 struct in_addr
*addr
;
1350 assert(address
!= NULL
);
1352 CHECK_ADDRESS(address
, INET
);
1354 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1355 addr
->s_addr
= hostaddr
;
1357 return GSOCK_NOERROR
;
1360 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1361 const char *protocol
)
1364 struct sockaddr_in
*addr
;
1366 assert(address
!= NULL
);
1367 CHECK_ADDRESS(address
, INET
);
1371 address
->m_error
= GSOCK_INVPORT
;
1372 return GSOCK_INVPORT
;
1375 se
= getservbyname(port
, protocol
);
1378 if (isdigit(port
[0]))
1382 port_int
= atoi(port
);
1383 addr
= (struct sockaddr_in
*)address
->m_addr
;
1384 addr
->sin_port
= htons((u_short
) port_int
);
1385 return GSOCK_NOERROR
;
1388 address
->m_error
= GSOCK_INVPORT
;
1389 return GSOCK_INVPORT
;
1392 addr
= (struct sockaddr_in
*)address
->m_addr
;
1393 addr
->sin_port
= se
->s_port
;
1395 return GSOCK_NOERROR
;
1398 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1400 struct sockaddr_in
*addr
;
1402 assert(address
!= NULL
);
1403 CHECK_ADDRESS(address
, INET
);
1405 addr
= (struct sockaddr_in
*)address
->m_addr
;
1406 addr
->sin_port
= htons(port
);
1408 return GSOCK_NOERROR
;
1411 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1415 struct sockaddr_in
*addr
;
1417 assert(address
!= NULL
);
1418 CHECK_ADDRESS(address
, INET
);
1420 addr
= (struct sockaddr_in
*)address
->m_addr
;
1421 addr_buf
= (char *)&(addr
->sin_addr
);
1423 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1426 address
->m_error
= GSOCK_NOHOST
;
1427 return GSOCK_NOHOST
;
1430 strncpy(hostname
, he
->h_name
, sbuf
);
1432 return GSOCK_NOERROR
;
1435 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1437 struct sockaddr_in
*addr
;
1439 assert(address
!= NULL
);
1440 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1442 addr
= (struct sockaddr_in
*)address
->m_addr
;
1444 return addr
->sin_addr
.s_addr
;
1447 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1449 struct sockaddr_in
*addr
;
1451 assert(address
!= NULL
);
1452 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1454 addr
= (struct sockaddr_in
*)address
->m_addr
;
1455 return ntohs(addr
->sin_port
);
1459 * -------------------------------------------------------------------------
1460 * Unix address family
1461 * -------------------------------------------------------------------------
1464 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1466 assert (address
!= NULL
);
1467 address
->m_error
= GSOCK_INVADDR
;
1468 return GSOCK_INVADDR
;
1471 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1473 #if defined(__BORLANDC__)
1474 /* prevents unused variable message in Borland */
1477 assert (address
!= NULL
);
1478 address
->m_error
= GSOCK_INVADDR
;
1479 return GSOCK_INVADDR
;
1482 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1484 #if defined(__BORLANDC__)
1485 /* prevents unused variable message in Borland */
1489 assert (address
!= NULL
);
1490 address
->m_error
= GSOCK_INVADDR
;
1491 return GSOCK_INVADDR
;
1494 #else /* !wxUSE_SOCKETS */
1497 * Translation unit shouldn't be empty, so include this typedef to make the
1498 * compiler (VC++ 6.0, for example) happy
1500 typedef void (*wxDummy
)();
1502 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */