]>
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/setup.h"
36 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
38 #ifndef __GSOCKET_STANDALONE__
39 # include "wx/msw/gsockmsw.h"
40 # include "wx/gsocket.h"
42 # include "gsockmsw.h"
44 #endif /* __GSOCKET_STANDALONE__ */
51 #define isdigit(x) (x > 47 && x < 58)
53 #include "wx/msw/wince/net.h"
62 /* if we use configure for MSW SOCKLEN_T will be already defined */
64 # define SOCKLEN_T int
67 /* Table of GUI-related functions. We must call them indirectly because
68 * of wxBase and GUI separation: */
70 static struct GSocketGUIFunctionsTable
*gs_gui_functions
;
72 #define USE_GUI() (gs_gui_functions != NULL)
74 /* Define macros to simplify indirection: */
75 #define _GSocket_GUI_Init() \
76 if (gs_gui_functions) gs_gui_functions->GUI_Init()
77 #define _GSocket_GUI_Cleanup() \
78 if (gs_gui_functions) gs_gui_functions->GUI_Cleanup()
79 #define _GSocket_GUI_Init_Socket(socket) \
80 (gs_gui_functions ? gs_gui_functions->GUI_Init_Socket(socket) : 1)
81 #define _GSocket_GUI_Destroy_Socket(socket) \
82 if (gs_gui_functions) gs_gui_functions->GUI_Destroy_Socket(socket)
83 #define _GSocket_Enable_Events(socket) \
84 if (gs_gui_functions) gs_gui_functions->Enable_Events(socket)
85 #define _GSocket_Disable_Events(socket) \
86 if (gs_gui_functions) gs_gui_functions->Disable_Events(socket)
87 #define _GSocket_Install_Callback(socket, event) \
88 if (gs_gui_functions) gs_gui_functions->Install_Callback(socket, event)
89 #define _GSocket_Uninstall_Callback(socket, event) \
90 if (gs_gui_functions) gs_gui_functions->Uninstall_Callback(socket, event)
92 /* Global initialisers */
94 void GSocket_SetGUIFunctions(struct GSocketGUIFunctionsTable
*guifunc
)
96 gs_gui_functions
= guifunc
;
99 int GSocket_Init(void)
103 if (gs_gui_functions
)
105 if ( !gs_gui_functions
->GUI_Init() )
109 /* Initialize WinSocket */
110 return (WSAStartup((1 << 8) | 1, &wsaData
) == 0);
113 void GSocket_Cleanup(void)
115 if (gs_gui_functions
)
117 gs_gui_functions
->GUI_Cleanup();
120 /* Cleanup WinSocket */
124 /* Constructors / Destructors for GSocket */
126 GSocket
*GSocket_new(void)
131 if ((socket
= (GSocket
*) malloc(sizeof(GSocket
))) == NULL
)
134 socket
->m_fd
= INVALID_SOCKET
;
135 for (i
= 0; i
< GSOCK_MAX_EVENT
; i
++)
137 socket
->m_cbacks
[i
] = NULL
;
139 socket
->m_detected
= 0;
140 socket
->m_local
= NULL
;
141 socket
->m_peer
= NULL
;
142 socket
->m_error
= GSOCK_NOERROR
;
143 socket
->m_server
= FALSE
;
144 socket
->m_stream
= TRUE
;
145 socket
->m_non_blocking
= FALSE
;
146 socket
->m_timeout
.tv_sec
= 10 * 60; /* 10 minutes */
147 socket
->m_timeout
.tv_usec
= 0;
148 socket
->m_establishing
= FALSE
;
150 /* Per-socket GUI-specific initialization */
151 success
= _GSocket_GUI_Init_Socket(socket
);
161 void GSocket_close(GSocket
*socket
)
163 _GSocket_Disable_Events(socket
);
164 closesocket(socket
->m_fd
);
165 socket
->m_fd
= INVALID_SOCKET
;
168 void GSocket_destroy(GSocket
*socket
)
170 assert(socket
!= NULL
);
172 /* Per-socket GUI-specific cleanup */
173 _GSocket_GUI_Destroy_Socket(socket
);
175 /* Check that the socket is really shutdowned */
176 if (socket
->m_fd
!= INVALID_SOCKET
)
177 GSocket_Shutdown(socket
);
179 /* Destroy private addresses */
181 GAddress_destroy(socket
->m_local
);
184 GAddress_destroy(socket
->m_peer
);
186 /* Destroy the socket itself */
191 * Disallow further read/write operations on this socket, close
192 * the fd and disable all callbacks.
194 void GSocket_Shutdown(GSocket
*socket
)
198 assert(socket
!= NULL
);
200 /* If socket has been created, shutdown it */
201 if (socket
->m_fd
!= INVALID_SOCKET
)
203 shutdown(socket
->m_fd
, 2);
204 GSocket_close(socket
);
207 /* Disable GUI callbacks */
208 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
209 socket
->m_cbacks
[evt
] = NULL
;
211 socket
->m_detected
= GSOCK_LOST_FLAG
;
214 /* Address handling */
220 * Set or get the local or peer address for this socket. The 'set'
221 * functions return GSOCK_NOERROR on success, an error code otherwise.
222 * The 'get' functions return a pointer to a GAddress object on success,
223 * or NULL otherwise, in which case they set the error code of the
224 * corresponding GSocket.
227 * GSOCK_INVSOCK - the socket is not valid.
228 * GSOCK_INVADDR - the address is not valid.
230 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
232 assert(socket
!= NULL
);
234 /* the socket must be initialized, or it must be a server */
235 if (socket
->m_fd
!= INVALID_SOCKET
&& !socket
->m_server
)
237 socket
->m_error
= GSOCK_INVSOCK
;
238 return GSOCK_INVSOCK
;
242 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
244 socket
->m_error
= GSOCK_INVADDR
;
245 return GSOCK_INVADDR
;
249 GAddress_destroy(socket
->m_local
);
251 socket
->m_local
= GAddress_copy(address
);
253 return GSOCK_NOERROR
;
256 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
258 assert(socket
!= NULL
);
261 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
263 socket
->m_error
= GSOCK_INVADDR
;
264 return GSOCK_INVADDR
;
268 GAddress_destroy(socket
->m_peer
);
270 socket
->m_peer
= GAddress_copy(address
);
272 return GSOCK_NOERROR
;
275 GAddress
*GSocket_GetLocal(GSocket
*socket
)
278 struct sockaddr addr
;
279 SOCKLEN_T size
= sizeof(addr
);
282 assert(socket
!= NULL
);
284 /* try to get it from the m_local var first */
286 return GAddress_copy(socket
->m_local
);
288 /* else, if the socket is initialized, try getsockname */
289 if (socket
->m_fd
== INVALID_SOCKET
)
291 socket
->m_error
= GSOCK_INVSOCK
;
295 if (getsockname(socket
->m_fd
, &addr
, &size
) == SOCKET_ERROR
)
297 socket
->m_error
= GSOCK_IOERR
;
301 /* got a valid address from getsockname, create a GAddress object */
302 if ((address
= GAddress_new()) == NULL
)
304 socket
->m_error
= GSOCK_MEMERR
;
308 if ((err
= _GAddress_translate_from(address
, &addr
, size
)) != GSOCK_NOERROR
)
310 GAddress_destroy(address
);
311 socket
->m_error
= err
;
318 GAddress
*GSocket_GetPeer(GSocket
*socket
)
320 assert(socket
!= NULL
);
322 /* try to get it from the m_peer var */
324 return GAddress_copy(socket
->m_peer
);
329 /* Server specific parts */
331 /* GSocket_SetServer:
332 * Sets up this socket as a server. The local address must have been
333 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
334 * Returns GSOCK_NOERROR on success, one of the following otherwise:
337 * GSOCK_INVSOCK - the socket is in use.
338 * GSOCK_INVADDR - the local address has not been set.
339 * GSOCK_IOERR - low-level error.
341 GSocketError
GSocket_SetServer(GSocket
*sck
)
347 /* must not be in use */
348 if (sck
->m_fd
!= INVALID_SOCKET
)
350 sck
->m_error
= GSOCK_INVSOCK
;
351 return GSOCK_INVSOCK
;
354 /* the local addr must have been set */
357 sck
->m_error
= GSOCK_INVADDR
;
358 return GSOCK_INVADDR
;
361 /* Initialize all fields */
362 sck
->m_server
= TRUE
;
363 sck
->m_stream
= TRUE
;
364 sck
->m_oriented
= TRUE
;
366 /* Create the socket */
367 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
369 if (sck
->m_fd
== INVALID_SOCKET
)
371 sck
->m_error
= GSOCK_IOERR
;
375 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
376 _GSocket_Enable_Events(sck
);
378 /* Bind to the local address,
379 * retrieve the actual address bound,
380 * and listen up to 5 connections.
382 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
383 (getsockname(sck
->m_fd
,
384 sck
->m_local
->m_addr
,
385 (SOCKLEN_T
*)&sck
->m_local
->m_len
) != 0) ||
386 (listen(sck
->m_fd
, 5) != 0))
389 sck
->m_error
= GSOCK_IOERR
;
393 return GSOCK_NOERROR
;
396 /* GSocket_WaitConnection:
397 * Waits for an incoming client connection. Returns a pointer to
398 * a GSocket object, or NULL if there was an error, in which case
399 * the last error field will be updated for the calling GSocket.
401 * Error codes (set in the calling GSocket)
402 * GSOCK_INVSOCK - the socket is not valid or not a server.
403 * GSOCK_TIMEDOUT - timeout, no incoming connections.
404 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
405 * GSOCK_MEMERR - couldn't allocate memory.
406 * GSOCK_IOERR - low-level error.
408 GSocket
*GSocket_WaitConnection(GSocket
*sck
)
411 struct sockaddr from
;
412 SOCKLEN_T fromlen
= sizeof(from
);
418 /* Reenable CONNECTION events */
419 sck
->m_detected
&= ~GSOCK_CONNECTION_FLAG
;
421 /* If the socket has already been created, we exit immediately */
422 if (sck
->m_fd
== INVALID_SOCKET
|| !sck
->m_server
)
424 sck
->m_error
= GSOCK_INVSOCK
;
428 /* Create a GSocket object for the new connection */
429 connection
= GSocket_new();
433 sck
->m_error
= GSOCK_MEMERR
;
437 /* Wait for a connection (with timeout) */
438 if (_GSocket_Input_Timeout(sck
) == GSOCK_TIMEDOUT
)
440 GSocket_destroy(connection
);
441 /* sck->m_error set by _GSocket_Input_Timeout */
445 connection
->m_fd
= accept(sck
->m_fd
, &from
, &fromlen
);
447 if (connection
->m_fd
== INVALID_SOCKET
)
449 if (WSAGetLastError() == WSAEWOULDBLOCK
)
450 sck
->m_error
= GSOCK_WOULDBLOCK
;
452 sck
->m_error
= GSOCK_IOERR
;
454 GSocket_destroy(connection
);
458 /* Initialize all fields */
459 connection
->m_server
= FALSE
;
460 connection
->m_stream
= TRUE
;
461 connection
->m_oriented
= TRUE
;
463 /* Setup the peer address field */
464 connection
->m_peer
= GAddress_new();
465 if (!connection
->m_peer
)
467 GSocket_destroy(connection
);
468 sck
->m_error
= GSOCK_MEMERR
;
471 err
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
);
472 if (err
!= GSOCK_NOERROR
)
474 GAddress_destroy(connection
->m_peer
);
475 GSocket_destroy(connection
);
480 ioctlsocket(connection
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
481 _GSocket_Enable_Events(connection
);
486 /* Client specific parts */
489 * For stream (connection oriented) sockets, GSocket_Connect() tries
490 * to establish a client connection to a server using the peer address
491 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
492 * connection has been succesfully established, or one of the error
493 * codes listed below. Note that for nonblocking sockets, a return
494 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
495 * request can be completed later; you should use GSocket_Select()
496 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
497 * corresponding asynchronous events.
499 * For datagram (non connection oriented) sockets, GSocket_Connect()
500 * just sets the peer address established with GSocket_SetPeer() as
501 * default destination.
504 * GSOCK_INVSOCK - the socket is in use or not valid.
505 * GSOCK_INVADDR - the peer address has not been established.
506 * GSOCK_TIMEDOUT - timeout, the connection failed.
507 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
508 * GSOCK_MEMERR - couldn't allocate memory.
509 * GSOCK_IOERR - low-level error.
511 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
518 /* Enable CONNECTION events (needed for nonblocking connections) */
519 sck
->m_detected
&= ~GSOCK_CONNECTION_FLAG
;
521 if (sck
->m_fd
!= INVALID_SOCKET
)
523 sck
->m_error
= GSOCK_INVSOCK
;
524 return GSOCK_INVSOCK
;
529 sck
->m_error
= GSOCK_INVADDR
;
530 return GSOCK_INVADDR
;
533 /* Streamed or dgram socket? */
534 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
535 sck
->m_oriented
= TRUE
;
536 sck
->m_server
= FALSE
;
537 sck
->m_establishing
= FALSE
;
539 /* Create the socket */
540 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
,
541 sck
->m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
543 if (sck
->m_fd
== INVALID_SOCKET
)
545 sck
->m_error
= GSOCK_IOERR
;
549 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
550 _GSocket_Enable_Events(sck
);
552 /* Connect it to the peer address, with a timeout (see below) */
553 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
555 if (ret
== SOCKET_ERROR
)
557 err
= WSAGetLastError();
559 /* If connect failed with EWOULDBLOCK and the GSocket object
560 * is in blocking mode, we select() for the specified timeout
561 * checking for writability to see if the connection request
564 if ((err
== WSAEWOULDBLOCK
) && (!sck
->m_non_blocking
))
566 err
= _GSocket_Connect_Timeout(sck
);
568 if (err
!= GSOCK_NOERROR
)
571 /* sck->m_error is set in _GSocket_Connect_Timeout */
574 return (GSocketError
) err
;
577 /* If connect failed with EWOULDBLOCK and the GSocket object
578 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
579 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
580 * this way if the connection completes, a GSOCK_CONNECTION
581 * event will be generated, if enabled.
583 if ((err
== WSAEWOULDBLOCK
) && (sck
->m_non_blocking
))
585 sck
->m_establishing
= TRUE
;
586 sck
->m_error
= GSOCK_WOULDBLOCK
;
587 return GSOCK_WOULDBLOCK
;
590 /* If connect failed with an error other than EWOULDBLOCK,
591 * then the call to GSocket_Connect() has failed.
594 sck
->m_error
= GSOCK_IOERR
;
598 return GSOCK_NOERROR
;
601 /* Datagram sockets */
603 /* GSocket_SetNonOriented:
604 * Sets up this socket as a non-connection oriented (datagram) socket.
605 * Before using this function, the local address must have been set
606 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
607 * on success, or one of the following otherwise.
610 * GSOCK_INVSOCK - the socket is in use.
611 * GSOCK_INVADDR - the local address has not been set.
612 * GSOCK_IOERR - low-level error.
614 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
620 if (sck
->m_fd
!= INVALID_SOCKET
)
622 sck
->m_error
= GSOCK_INVSOCK
;
623 return GSOCK_INVSOCK
;
628 sck
->m_error
= GSOCK_INVADDR
;
629 return GSOCK_INVADDR
;
632 /* Initialize all fields */
633 sck
->m_stream
= FALSE
;
634 sck
->m_server
= FALSE
;
635 sck
->m_oriented
= FALSE
;
637 /* Create the socket */
638 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
640 if (sck
->m_fd
== INVALID_SOCKET
)
642 sck
->m_error
= GSOCK_IOERR
;
646 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
647 _GSocket_Enable_Events(sck
);
649 /* Bind to the local address,
650 * and retrieve the actual address bound.
652 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
653 (getsockname(sck
->m_fd
,
654 sck
->m_local
->m_addr
,
655 (SOCKLEN_T
*)&sck
->m_local
->m_len
) != 0))
658 sck
->m_error
= GSOCK_IOERR
;
662 return GSOCK_NOERROR
;
667 /* Like recv(), send(), ... */
668 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
672 assert(socket
!= NULL
);
674 /* Reenable INPUT events */
675 socket
->m_detected
&= ~GSOCK_INPUT_FLAG
;
677 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
679 socket
->m_error
= GSOCK_INVSOCK
;
683 /* If the socket is blocking, wait for data (with a timeout) */
684 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
688 if (socket
->m_stream
)
689 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
691 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
693 if (ret
== SOCKET_ERROR
)
695 if (WSAGetLastError() != WSAEWOULDBLOCK
)
696 socket
->m_error
= GSOCK_IOERR
;
698 socket
->m_error
= GSOCK_WOULDBLOCK
;
705 int GSocket_Write(GSocket
*socket
, const char *buffer
, int size
)
709 assert(socket
!= NULL
);
711 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
713 socket
->m_error
= GSOCK_INVSOCK
;
717 /* If the socket is blocking, wait for writability (with a timeout) */
718 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
722 if (socket
->m_stream
)
723 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
725 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
727 if (ret
== SOCKET_ERROR
)
729 if (WSAGetLastError() != WSAEWOULDBLOCK
)
730 socket
->m_error
= GSOCK_IOERR
;
732 socket
->m_error
= GSOCK_WOULDBLOCK
;
734 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
735 * does). Once the first OUTPUT event is received, users can assume
736 * that the socket is writable until a read operation fails. Only then
737 * will further OUTPUT events be posted.
739 socket
->m_detected
&= ~GSOCK_OUTPUT_FLAG
;
747 * Polls the socket to determine its status. This function will
748 * check for the events specified in the 'flags' parameter, and
749 * it will return a mask indicating which operations can be
750 * performed. This function won't block, regardless of the
751 * mode (blocking | nonblocking) of the socket.
753 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
757 GSocketEventFlags result
= 0;
761 static const struct timeval tv
= { 0, 0 };
763 assert(socket
!= NULL
);
768 FD_SET(socket
->m_fd
, &readfds
);
769 FD_SET(socket
->m_fd
, &writefds
);
770 FD_SET(socket
->m_fd
, &exceptfds
);
772 /* Check 'sticky' CONNECTION flag first */
773 result
|= (GSOCK_CONNECTION_FLAG
& socket
->m_detected
);
775 /* If we have already detected a LOST event, then don't try
776 * to do any further processing.
778 if ((socket
->m_detected
& GSOCK_LOST_FLAG
) != 0)
780 socket
->m_establishing
= FALSE
;
782 return (GSOCK_LOST_FLAG
& flags
);
786 if (select(socket
->m_fd
+ 1, &readfds
, &writefds
, &exceptfds
, &tv
) <= 0)
788 /* What to do here? */
789 return (result
& flags
);
792 /* Check for readability */
793 if (FD_ISSET(socket
->m_fd
, &readfds
))
797 if (recv(socket
->m_fd
, &c
, 1, MSG_PEEK
) > 0)
799 result
|= GSOCK_INPUT_FLAG
;
803 if (socket
->m_server
&& socket
->m_stream
)
805 result
|= GSOCK_CONNECTION_FLAG
;
806 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
810 socket
->m_detected
= GSOCK_LOST_FLAG
;
811 socket
->m_establishing
= FALSE
;
813 /* LOST event: Abort any further processing */
814 return (GSOCK_LOST_FLAG
& flags
);
819 /* Check for writability */
820 if (FD_ISSET(socket
->m_fd
, &writefds
))
822 if (socket
->m_establishing
&& !socket
->m_server
)
825 SOCKLEN_T len
= sizeof(error
);
827 socket
->m_establishing
= FALSE
;
829 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*)&error
, &len
);
833 socket
->m_detected
= GSOCK_LOST_FLAG
;
835 /* LOST event: Abort any further processing */
836 return (GSOCK_LOST_FLAG
& flags
);
840 result
|= GSOCK_CONNECTION_FLAG
;
841 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
846 result
|= GSOCK_OUTPUT_FLAG
;
850 /* Check for exceptions and errors (is this useful in Unices?) */
851 if (FD_ISSET(socket
->m_fd
, &exceptfds
))
853 socket
->m_establishing
= FALSE
;
854 socket
->m_detected
= GSOCK_LOST_FLAG
;
856 /* LOST event: Abort any further processing */
857 return (GSOCK_LOST_FLAG
& flags
);
860 return (result
& flags
);
864 assert(socket
!= NULL
);
865 return flags
& socket
->m_detected
;
871 /* GSocket_SetNonBlocking:
872 * Sets the socket to non-blocking mode. All IO calls will return
875 void GSocket_SetNonBlocking(GSocket
*socket
, int non_block
)
877 assert(socket
!= NULL
);
879 socket
->m_non_blocking
= non_block
;
882 /* GSocket_SetTimeout:
883 * Sets the timeout for blocking calls. Time is expressed in
886 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millis
)
888 assert(socket
!= NULL
);
890 socket
->m_timeout
.tv_sec
= (millis
/ 1000);
891 socket
->m_timeout
.tv_usec
= (millis
% 1000) * 1000;
895 * Returns the last error occured for this socket. Note that successful
896 * operations do not clear this back to GSOCK_NOERROR, so use it only
899 GSocketError
GSocket_GetError(GSocket
*socket
)
901 assert(socket
!= NULL
);
903 return socket
->m_error
;
909 * There is data to be read in the input buffer. If, after a read
910 * operation, there is still data available, the callback function will
913 * The socket is available for writing. That is, the next write call
914 * won't block. This event is generated only once, when the connection is
915 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
916 * when the output buffer empties again. This means that the app should
917 * assume that it can write since the first OUTPUT event, and no more
918 * OUTPUT events will be generated unless an error occurs.
920 * Connection succesfully established, for client sockets, or incoming
921 * client connection, for server sockets. Wait for this event (also watch
922 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
924 * The connection is lost (or a connection request failed); this could
925 * be due to a failure, or due to the peer closing it gracefully.
928 /* GSocket_SetCallback:
929 * Enables the callbacks specified by 'flags'. Note that 'flags'
930 * may be a combination of flags OR'ed toghether, so the same
931 * callback function can be made to accept different events.
932 * The callback function must have the following prototype:
934 * void function(GSocket *socket, GSocketEvent event, char *cdata)
936 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
937 GSocketCallback callback
, char *cdata
)
941 assert(socket
!= NULL
);
943 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
945 if ((flags
& (1 << count
)) != 0)
947 socket
->m_cbacks
[count
] = callback
;
948 socket
->m_data
[count
] = cdata
;
953 /* GSocket_UnsetCallback:
954 * Disables all callbacks specified by 'flags', which may be a
955 * combination of flags OR'ed toghether.
957 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
961 assert(socket
!= NULL
);
963 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
965 if ((flags
& (1 << count
)) != 0)
967 socket
->m_cbacks
[count
] = NULL
;
968 socket
->m_data
[count
] = NULL
;
975 /* _GSocket_Input_Timeout:
976 * For blocking sockets, wait until data is available or
977 * until timeout ellapses.
979 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
983 if (!socket
->m_non_blocking
)
986 FD_SET(socket
->m_fd
, &readfds
);
987 if (select(0, &readfds
, NULL
, NULL
, &socket
->m_timeout
) == 0)
989 socket
->m_error
= GSOCK_TIMEDOUT
;
990 return GSOCK_TIMEDOUT
;
993 return GSOCK_NOERROR
;
996 /* _GSocket_Output_Timeout:
997 * For blocking sockets, wait until data can be sent without
998 * blocking or until timeout ellapses.
1000 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
1004 if (!socket
->m_non_blocking
)
1007 FD_SET(socket
->m_fd
, &writefds
);
1008 if (select(0, NULL
, &writefds
, NULL
, &socket
->m_timeout
) == 0)
1010 socket
->m_error
= GSOCK_TIMEDOUT
;
1011 return GSOCK_TIMEDOUT
;
1014 return GSOCK_NOERROR
;
1017 /* _GSocket_Connect_Timeout:
1018 * For blocking sockets, wait until the connection is
1019 * established or fails, or until timeout ellapses.
1021 GSocketError
_GSocket_Connect_Timeout(GSocket
*socket
)
1027 FD_ZERO(&exceptfds
);
1028 FD_SET(socket
->m_fd
, &writefds
);
1029 FD_SET(socket
->m_fd
, &exceptfds
);
1030 if (select(0, NULL
, &writefds
, &exceptfds
, &socket
->m_timeout
) == 0)
1032 socket
->m_error
= GSOCK_TIMEDOUT
;
1033 return GSOCK_TIMEDOUT
;
1035 if (!FD_ISSET(socket
->m_fd
, &writefds
))
1037 socket
->m_error
= GSOCK_IOERR
;
1041 return GSOCK_NOERROR
;
1044 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
1046 return recv(socket
->m_fd
, buffer
, size
, 0);
1049 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
1051 struct sockaddr from
;
1052 SOCKLEN_T fromlen
= sizeof(from
);
1056 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
1058 if (ret
== SOCKET_ERROR
)
1059 return SOCKET_ERROR
;
1061 /* Translate a system address into a GSocket address */
1062 if (!socket
->m_peer
)
1064 socket
->m_peer
= GAddress_new();
1065 if (!socket
->m_peer
)
1067 socket
->m_error
= GSOCK_MEMERR
;
1071 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
1072 if (err
!= GSOCK_NOERROR
)
1074 GAddress_destroy(socket
->m_peer
);
1075 socket
->m_peer
= NULL
;
1076 socket
->m_error
= err
;
1083 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
1085 return send(socket
->m_fd
, buffer
, size
, 0);
1088 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
1090 struct sockaddr
*addr
;
1094 if (!socket
->m_peer
)
1096 socket
->m_error
= GSOCK_INVADDR
;
1100 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
1101 if (err
!= GSOCK_NOERROR
)
1103 socket
->m_error
= err
;
1107 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
1109 /* Frees memory allocated by _GAddress_translate_to */
1117 * -------------------------------------------------------------------------
1119 * -------------------------------------------------------------------------
1122 /* CHECK_ADDRESS verifies that the current address family is either
1123 * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it
1124 * initalizes it to be a GSOCK_*family*. In other cases, it returns
1125 * an appropiate error code.
1127 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
1129 #define CHECK_ADDRESS(address, family) \
1131 if (address->m_family == GSOCK_NOFAMILY) \
1132 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1133 return address->m_error; \
1134 if (address->m_family != GSOCK_##family) \
1136 address->m_error = GSOCK_INVADDR; \
1137 return GSOCK_INVADDR; \
1141 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
1143 if (address->m_family == GSOCK_NOFAMILY) \
1144 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1146 if (address->m_family != GSOCK_##family) \
1148 address->m_error = GSOCK_INVADDR; \
1154 GAddress
*GAddress_new(void)
1158 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1161 address
->m_family
= GSOCK_NOFAMILY
;
1162 address
->m_addr
= NULL
;
1168 GAddress
*GAddress_copy(GAddress
*address
)
1172 assert(address
!= NULL
);
1174 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1177 memcpy(addr2
, address
, sizeof(GAddress
));
1179 if (address
->m_addr
)
1181 addr2
->m_addr
= (struct sockaddr
*) malloc(addr2
->m_len
);
1182 if (addr2
->m_addr
== NULL
)
1187 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1193 void GAddress_destroy(GAddress
*address
)
1195 assert(address
!= NULL
);
1197 if (address
->m_addr
)
1198 free(address
->m_addr
);
1203 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1205 assert(address
!= NULL
);
1207 address
->m_family
= type
;
1210 GAddressType
GAddress_GetFamily(GAddress
*address
)
1212 assert(address
!= NULL
);
1214 return address
->m_family
;
1217 GSocketError
_GAddress_translate_from(GAddress
*address
,
1218 struct sockaddr
*addr
, int len
)
1220 address
->m_realfamily
= addr
->sa_family
;
1221 switch (addr
->sa_family
)
1224 address
->m_family
= GSOCK_INET
;
1227 address
->m_family
= GSOCK_UNIX
;
1231 address
->m_family
= GSOCK_INET6
;
1236 address
->m_error
= GSOCK_INVOP
;
1241 if (address
->m_addr
)
1242 free(address
->m_addr
);
1244 address
->m_len
= len
;
1245 address
->m_addr
= (struct sockaddr
*) malloc(len
);
1247 if (address
->m_addr
== NULL
)
1249 address
->m_error
= GSOCK_MEMERR
;
1250 return GSOCK_MEMERR
;
1252 memcpy(address
->m_addr
, addr
, len
);
1254 return GSOCK_NOERROR
;
1257 GSocketError
_GAddress_translate_to(GAddress
*address
,
1258 struct sockaddr
**addr
, int *len
)
1260 if (!address
->m_addr
)
1262 address
->m_error
= GSOCK_INVADDR
;
1263 return GSOCK_INVADDR
;
1266 *len
= address
->m_len
;
1267 *addr
= (struct sockaddr
*) malloc(address
->m_len
);
1270 address
->m_error
= GSOCK_MEMERR
;
1271 return GSOCK_MEMERR
;
1274 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1275 return GSOCK_NOERROR
;
1279 * -------------------------------------------------------------------------
1280 * Internet address family
1281 * -------------------------------------------------------------------------
1284 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1286 address
->m_len
= sizeof(struct sockaddr_in
);
1287 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1288 if (address
->m_addr
== NULL
)
1290 address
->m_error
= GSOCK_MEMERR
;
1291 return GSOCK_MEMERR
;
1294 address
->m_family
= GSOCK_INET
;
1295 address
->m_realfamily
= PF_INET
;
1296 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1297 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1299 return GSOCK_NOERROR
;
1302 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1305 struct in_addr
*addr
;
1307 assert(address
!= NULL
);
1309 CHECK_ADDRESS(address
, INET
);
1311 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1313 addr
->s_addr
= inet_addr(hostname
);
1315 /* If it is a numeric host name, convert it now */
1316 if (addr
->s_addr
== INADDR_NONE
)
1318 struct in_addr
*array_addr
;
1320 /* It is a real name, we solve it */
1321 if ((he
= gethostbyname(hostname
)) == NULL
)
1323 /* addr->s_addr = INADDR_NONE just done by inet_addr() above */
1324 address
->m_error
= GSOCK_NOHOST
;
1325 return GSOCK_NOHOST
;
1327 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1328 addr
->s_addr
= array_addr
[0].s_addr
;
1330 return GSOCK_NOERROR
;
1333 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1335 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1338 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1339 unsigned long hostaddr
)
1341 struct in_addr
*addr
;
1343 assert(address
!= NULL
);
1345 CHECK_ADDRESS(address
, INET
);
1347 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1348 addr
->s_addr
= hostaddr
;
1350 return GSOCK_NOERROR
;
1353 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1354 const char *protocol
)
1357 struct sockaddr_in
*addr
;
1359 assert(address
!= NULL
);
1360 CHECK_ADDRESS(address
, INET
);
1364 address
->m_error
= GSOCK_INVPORT
;
1365 return GSOCK_INVPORT
;
1368 se
= getservbyname(port
, protocol
);
1371 if (isdigit(port
[0]))
1375 port_int
= atoi(port
);
1376 addr
= (struct sockaddr_in
*)address
->m_addr
;
1377 addr
->sin_port
= htons((u_short
) port_int
);
1378 return GSOCK_NOERROR
;
1381 address
->m_error
= GSOCK_INVPORT
;
1382 return GSOCK_INVPORT
;
1385 addr
= (struct sockaddr_in
*)address
->m_addr
;
1386 addr
->sin_port
= se
->s_port
;
1388 return GSOCK_NOERROR
;
1391 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1393 struct sockaddr_in
*addr
;
1395 assert(address
!= NULL
);
1396 CHECK_ADDRESS(address
, INET
);
1398 addr
= (struct sockaddr_in
*)address
->m_addr
;
1399 addr
->sin_port
= htons(port
);
1401 return GSOCK_NOERROR
;
1404 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1408 struct sockaddr_in
*addr
;
1410 assert(address
!= NULL
);
1411 CHECK_ADDRESS(address
, INET
);
1413 addr
= (struct sockaddr_in
*)address
->m_addr
;
1414 addr_buf
= (char *)&(addr
->sin_addr
);
1416 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1419 address
->m_error
= GSOCK_NOHOST
;
1420 return GSOCK_NOHOST
;
1423 strncpy(hostname
, he
->h_name
, sbuf
);
1425 return GSOCK_NOERROR
;
1428 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1430 struct sockaddr_in
*addr
;
1432 assert(address
!= NULL
);
1433 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1435 addr
= (struct sockaddr_in
*)address
->m_addr
;
1437 return addr
->sin_addr
.s_addr
;
1440 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1442 struct sockaddr_in
*addr
;
1444 assert(address
!= NULL
);
1445 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1447 addr
= (struct sockaddr_in
*)address
->m_addr
;
1448 return ntohs(addr
->sin_port
);
1452 * -------------------------------------------------------------------------
1453 * Unix address family
1454 * -------------------------------------------------------------------------
1457 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1459 assert (address
!= NULL
);
1460 address
->m_error
= GSOCK_INVADDR
;
1461 return GSOCK_INVADDR
;
1464 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1466 #if defined(__BORLANDC__)
1467 /* prevents unused variable message in Borland */
1470 assert (address
!= NULL
);
1471 address
->m_error
= GSOCK_INVADDR
;
1472 return GSOCK_INVADDR
;
1475 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1477 #if defined(__BORLANDC__)
1478 /* prevents unused variable message in Borland */
1482 assert (address
!= NULL
);
1483 address
->m_error
= GSOCK_INVADDR
;
1484 return GSOCK_INVADDR
;
1487 #else /* !wxUSE_SOCKETS */
1490 * Translation unit shouldn't be empty, so include this typedef to make the
1491 * compiler (VC++ 6.0, for example) happy
1493 typedef void (*wxDummy
)();
1495 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */