]>
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 /* Bind to the local address,
393 * retrieve the actual address bound,
394 * and listen up to 5 connections.
396 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
397 (getsockname(sck
->m_fd
,
398 sck
->m_local
->m_addr
,
399 (SOCKLEN_T
*)&sck
->m_local
->m_len
) != 0) ||
400 (listen(sck
->m_fd
, 5) != 0))
403 sck
->m_error
= GSOCK_IOERR
;
407 return GSOCK_NOERROR
;
410 /* GSocket_WaitConnection:
411 * Waits for an incoming client connection. Returns a pointer to
412 * a GSocket object, or NULL if there was an error, in which case
413 * the last error field will be updated for the calling GSocket.
415 * Error codes (set in the calling GSocket)
416 * GSOCK_INVSOCK - the socket is not valid or not a server.
417 * GSOCK_TIMEDOUT - timeout, no incoming connections.
418 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
419 * GSOCK_MEMERR - couldn't allocate memory.
420 * GSOCK_IOERR - low-level error.
422 GSocket
*GSocket_WaitConnection(GSocket
*sck
)
425 struct sockaddr from
;
426 SOCKLEN_T fromlen
= sizeof(from
);
432 /* Reenable CONNECTION events */
433 sck
->m_detected
&= ~GSOCK_CONNECTION_FLAG
;
435 /* If the socket has already been created, we exit immediately */
436 if (sck
->m_fd
== INVALID_SOCKET
|| !sck
->m_server
)
438 sck
->m_error
= GSOCK_INVSOCK
;
442 /* Create a GSocket object for the new connection */
443 connection
= GSocket_new();
447 sck
->m_error
= GSOCK_MEMERR
;
451 /* Wait for a connection (with timeout) */
452 if (_GSocket_Input_Timeout(sck
) == GSOCK_TIMEDOUT
)
454 GSocket_destroy(connection
);
455 /* sck->m_error set by _GSocket_Input_Timeout */
459 connection
->m_fd
= accept(sck
->m_fd
, &from
, &fromlen
);
461 if (connection
->m_fd
== INVALID_SOCKET
)
463 if (WSAGetLastError() == WSAEWOULDBLOCK
)
464 sck
->m_error
= GSOCK_WOULDBLOCK
;
466 sck
->m_error
= GSOCK_IOERR
;
468 GSocket_destroy(connection
);
472 /* Initialize all fields */
473 connection
->m_server
= FALSE
;
474 connection
->m_stream
= TRUE
;
475 connection
->m_oriented
= TRUE
;
477 /* Setup the peer address field */
478 connection
->m_peer
= GAddress_new();
479 if (!connection
->m_peer
)
481 GSocket_destroy(connection
);
482 sck
->m_error
= GSOCK_MEMERR
;
485 err
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
);
486 if (err
!= GSOCK_NOERROR
)
488 GAddress_destroy(connection
->m_peer
);
489 GSocket_destroy(connection
);
494 ioctlsocket(connection
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
495 _GSocket_Enable_Events(connection
);
500 /* Client specific parts */
503 * For stream (connection oriented) sockets, GSocket_Connect() tries
504 * to establish a client connection to a server using the peer address
505 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
506 * connection has been succesfully established, or one of the error
507 * codes listed below. Note that for nonblocking sockets, a return
508 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
509 * request can be completed later; you should use GSocket_Select()
510 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
511 * corresponding asynchronous events.
513 * For datagram (non connection oriented) sockets, GSocket_Connect()
514 * just sets the peer address established with GSocket_SetPeer() as
515 * default destination.
518 * GSOCK_INVSOCK - the socket is in use or not valid.
519 * GSOCK_INVADDR - the peer address has not been established.
520 * GSOCK_TIMEDOUT - timeout, the connection failed.
521 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
522 * GSOCK_MEMERR - couldn't allocate memory.
523 * GSOCK_IOERR - low-level error.
525 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
532 /* Enable CONNECTION events (needed for nonblocking connections) */
533 sck
->m_detected
&= ~GSOCK_CONNECTION_FLAG
;
535 if (sck
->m_fd
!= INVALID_SOCKET
)
537 sck
->m_error
= GSOCK_INVSOCK
;
538 return GSOCK_INVSOCK
;
543 sck
->m_error
= GSOCK_INVADDR
;
544 return GSOCK_INVADDR
;
547 /* Streamed or dgram socket? */
548 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
549 sck
->m_oriented
= TRUE
;
550 sck
->m_server
= FALSE
;
551 sck
->m_establishing
= FALSE
;
553 /* Create the socket */
554 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
,
555 sck
->m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
557 if (sck
->m_fd
== INVALID_SOCKET
)
559 sck
->m_error
= GSOCK_IOERR
;
563 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
564 _GSocket_Enable_Events(sck
);
566 /* Connect it to the peer address, with a timeout (see below) */
567 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
569 if (ret
== SOCKET_ERROR
)
571 err
= WSAGetLastError();
573 /* If connect failed with EWOULDBLOCK and the GSocket object
574 * is in blocking mode, we select() for the specified timeout
575 * checking for writability to see if the connection request
578 if ((err
== WSAEWOULDBLOCK
) && (!sck
->m_non_blocking
))
580 err
= _GSocket_Connect_Timeout(sck
);
582 if (err
!= GSOCK_NOERROR
)
585 /* sck->m_error is set in _GSocket_Connect_Timeout */
588 return (GSocketError
) err
;
591 /* If connect failed with EWOULDBLOCK and the GSocket object
592 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
593 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
594 * this way if the connection completes, a GSOCK_CONNECTION
595 * event will be generated, if enabled.
597 if ((err
== WSAEWOULDBLOCK
) && (sck
->m_non_blocking
))
599 sck
->m_establishing
= TRUE
;
600 sck
->m_error
= GSOCK_WOULDBLOCK
;
601 return GSOCK_WOULDBLOCK
;
604 /* If connect failed with an error other than EWOULDBLOCK,
605 * then the call to GSocket_Connect() has failed.
608 sck
->m_error
= GSOCK_IOERR
;
612 return GSOCK_NOERROR
;
615 /* Datagram sockets */
617 /* GSocket_SetNonOriented:
618 * Sets up this socket as a non-connection oriented (datagram) socket.
619 * Before using this function, the local address must have been set
620 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
621 * on success, or one of the following otherwise.
624 * GSOCK_INVSOCK - the socket is in use.
625 * GSOCK_INVADDR - the local address has not been set.
626 * GSOCK_IOERR - low-level error.
628 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
634 if (sck
->m_fd
!= INVALID_SOCKET
)
636 sck
->m_error
= GSOCK_INVSOCK
;
637 return GSOCK_INVSOCK
;
642 sck
->m_error
= GSOCK_INVADDR
;
643 return GSOCK_INVADDR
;
646 /* Initialize all fields */
647 sck
->m_stream
= FALSE
;
648 sck
->m_server
= FALSE
;
649 sck
->m_oriented
= FALSE
;
651 /* Create the socket */
652 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
654 if (sck
->m_fd
== INVALID_SOCKET
)
656 sck
->m_error
= GSOCK_IOERR
;
660 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
661 _GSocket_Enable_Events(sck
);
663 /* Bind to the local address,
664 * and retrieve the actual address bound.
666 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
667 (getsockname(sck
->m_fd
,
668 sck
->m_local
->m_addr
,
669 (SOCKLEN_T
*)&sck
->m_local
->m_len
) != 0))
672 sck
->m_error
= GSOCK_IOERR
;
676 return GSOCK_NOERROR
;
681 /* Like recv(), send(), ... */
682 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
686 assert(socket
!= NULL
);
688 /* Reenable INPUT events */
689 socket
->m_detected
&= ~GSOCK_INPUT_FLAG
;
691 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
693 socket
->m_error
= GSOCK_INVSOCK
;
697 /* If the socket is blocking, wait for data (with a timeout) */
698 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
702 if (socket
->m_stream
)
703 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
705 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
707 if (ret
== SOCKET_ERROR
)
709 if (WSAGetLastError() != WSAEWOULDBLOCK
)
710 socket
->m_error
= GSOCK_IOERR
;
712 socket
->m_error
= GSOCK_WOULDBLOCK
;
719 int GSocket_Write(GSocket
*socket
, const char *buffer
, int size
)
723 assert(socket
!= NULL
);
725 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
727 socket
->m_error
= GSOCK_INVSOCK
;
731 /* If the socket is blocking, wait for writability (with a timeout) */
732 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
736 if (socket
->m_stream
)
737 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
739 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
741 if (ret
== SOCKET_ERROR
)
743 if (WSAGetLastError() != WSAEWOULDBLOCK
)
744 socket
->m_error
= GSOCK_IOERR
;
746 socket
->m_error
= GSOCK_WOULDBLOCK
;
748 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
749 * does). Once the first OUTPUT event is received, users can assume
750 * that the socket is writable until a read operation fails. Only then
751 * will further OUTPUT events be posted.
753 socket
->m_detected
&= ~GSOCK_OUTPUT_FLAG
;
761 * Polls the socket to determine its status. This function will
762 * check for the events specified in the 'flags' parameter, and
763 * it will return a mask indicating which operations can be
764 * performed. This function won't block, regardless of the
765 * mode (blocking | nonblocking) of the socket.
767 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
771 GSocketEventFlags result
= 0;
776 assert(socket
!= NULL
);
781 FD_SET(socket
->m_fd
, &readfds
);
782 if (flags
& GSOCK_OUTPUT_FLAG
|| flags
& GSOCK_CONNECTION_FLAG
)
783 FD_SET(socket
->m_fd
, &writefds
);
784 FD_SET(socket
->m_fd
, &exceptfds
);
786 /* Check 'sticky' CONNECTION flag first */
787 result
|= (GSOCK_CONNECTION_FLAG
& socket
->m_detected
);
789 /* If we have already detected a LOST event, then don't try
790 * to do any further processing.
792 if ((socket
->m_detected
& GSOCK_LOST_FLAG
) != 0)
794 socket
->m_establishing
= FALSE
;
796 return (GSOCK_LOST_FLAG
& flags
);
800 if (select(socket
->m_fd
+ 1, &readfds
, &writefds
, &exceptfds
,
801 &socket
->m_timeout
) <= 0)
803 /* What to do here? */
804 return (result
& flags
);
807 /* Check for readability */
808 if (FD_ISSET(socket
->m_fd
, &readfds
))
812 if (!socket
->m_stream
|| recv(socket
->m_fd
, &c
, 1, MSG_PEEK
) > 0)
814 result
|= GSOCK_INPUT_FLAG
;
818 if (socket
->m_server
&& socket
->m_stream
)
820 result
|= GSOCK_CONNECTION_FLAG
;
821 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
825 socket
->m_detected
= GSOCK_LOST_FLAG
;
826 socket
->m_establishing
= FALSE
;
828 /* LOST event: Abort any further processing */
829 return (GSOCK_LOST_FLAG
& flags
);
834 /* Check for writability */
835 if (FD_ISSET(socket
->m_fd
, &writefds
))
837 if (socket
->m_establishing
&& !socket
->m_server
)
840 SOCKLEN_T len
= sizeof(error
);
842 socket
->m_establishing
= FALSE
;
844 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*)&error
, &len
);
848 socket
->m_detected
= GSOCK_LOST_FLAG
;
850 /* LOST event: Abort any further processing */
851 return (GSOCK_LOST_FLAG
& flags
);
855 result
|= GSOCK_CONNECTION_FLAG
;
856 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
861 result
|= GSOCK_OUTPUT_FLAG
;
865 /* Check for exceptions and errors (is this useful in Unices?) */
866 if (FD_ISSET(socket
->m_fd
, &exceptfds
))
868 socket
->m_establishing
= FALSE
;
869 socket
->m_detected
= GSOCK_LOST_FLAG
;
871 /* LOST event: Abort any further processing */
872 return (GSOCK_LOST_FLAG
& flags
);
875 return (result
& flags
);
879 assert(socket
!= NULL
);
880 return flags
& socket
->m_detected
;
886 /* GSocket_SetNonBlocking:
887 * Sets the socket to non-blocking mode. All IO calls will return
890 void GSocket_SetNonBlocking(GSocket
*socket
, int non_block
)
892 assert(socket
!= NULL
);
894 socket
->m_non_blocking
= non_block
;
897 /* GSocket_SetTimeout:
898 * Sets the timeout for blocking calls. Time is expressed in
901 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millis
)
903 assert(socket
!= NULL
);
905 socket
->m_timeout
.tv_sec
= (millis
/ 1000);
906 socket
->m_timeout
.tv_usec
= (millis
% 1000) * 1000;
910 * Returns the last error occured for this socket. Note that successful
911 * operations do not clear this back to GSOCK_NOERROR, so use it only
914 GSocketError
GSocket_GetError(GSocket
*socket
)
916 assert(socket
!= NULL
);
918 return socket
->m_error
;
924 * There is data to be read in the input buffer. If, after a read
925 * operation, there is still data available, the callback function will
928 * The socket is available for writing. That is, the next write call
929 * won't block. This event is generated only once, when the connection is
930 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
931 * when the output buffer empties again. This means that the app should
932 * assume that it can write since the first OUTPUT event, and no more
933 * OUTPUT events will be generated unless an error occurs.
935 * Connection succesfully established, for client sockets, or incoming
936 * client connection, for server sockets. Wait for this event (also watch
937 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
939 * The connection is lost (or a connection request failed); this could
940 * be due to a failure, or due to the peer closing it gracefully.
943 /* GSocket_SetCallback:
944 * Enables the callbacks specified by 'flags'. Note that 'flags'
945 * may be a combination of flags OR'ed toghether, so the same
946 * callback function can be made to accept different events.
947 * The callback function must have the following prototype:
949 * void function(GSocket *socket, GSocketEvent event, char *cdata)
951 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
952 GSocketCallback callback
, char *cdata
)
956 assert(socket
!= NULL
);
958 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
960 if ((flags
& (1 << count
)) != 0)
962 socket
->m_cbacks
[count
] = callback
;
963 socket
->m_data
[count
] = cdata
;
968 /* GSocket_UnsetCallback:
969 * Disables all callbacks specified by 'flags', which may be a
970 * combination of flags OR'ed toghether.
972 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
976 assert(socket
!= NULL
);
978 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
980 if ((flags
& (1 << count
)) != 0)
982 socket
->m_cbacks
[count
] = NULL
;
983 socket
->m_data
[count
] = NULL
;
988 GSocketError
GSocket_GetSockOpt(GSocket
*socket
, int level
, int optname
,
989 void *optval
, int *optlen
)
991 if (getsockopt(socket
->m_fd
, level
, optname
, optval
, optlen
) == 0)
993 return GSOCK_NOERROR
;
998 GSocketError
GSocket_SetSockOpt(GSocket
*socket
, int level
, int optname
,
999 const void *optval
, int optlen
)
1001 if (setsockopt(socket
->m_fd
, level
, optname
, optval
, optlen
) == 0)
1003 return GSOCK_NOERROR
;
1005 return GSOCK_OPTERR
;
1008 GSocketError
GSocket_SetReuseAddr(GSocket
*socket
)
1010 /* allow a socket to re-bind if the socket is in the TIME_WAIT
1011 state after being previously closed.
1014 setsockopt(socket
->m_fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char*)&arg
, sizeof(u_long
));
1017 void GSocket_Streamed(GSocket
*socket
)
1019 socket
->m_stream
= TRUE
;
1022 void GSocket_Unstreamed(GSocket
*socket
)
1024 socket
->m_stream
= FALSE
;
1027 /* Internals (IO) */
1029 /* _GSocket_Input_Timeout:
1030 * For blocking sockets, wait until data is available or
1031 * until timeout ellapses.
1033 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
1037 if (!socket
->m_non_blocking
)
1040 FD_SET(socket
->m_fd
, &readfds
);
1041 if (select(0, &readfds
, NULL
, NULL
, &socket
->m_timeout
) == 0)
1043 socket
->m_error
= GSOCK_TIMEDOUT
;
1044 return GSOCK_TIMEDOUT
;
1047 return GSOCK_NOERROR
;
1050 /* _GSocket_Output_Timeout:
1051 * For blocking sockets, wait until data can be sent without
1052 * blocking or until timeout ellapses.
1054 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
1058 if (!socket
->m_non_blocking
)
1061 FD_SET(socket
->m_fd
, &writefds
);
1062 if (select(0, NULL
, &writefds
, NULL
, &socket
->m_timeout
) == 0)
1064 socket
->m_error
= GSOCK_TIMEDOUT
;
1065 return GSOCK_TIMEDOUT
;
1068 return GSOCK_NOERROR
;
1071 /* _GSocket_Connect_Timeout:
1072 * For blocking sockets, wait until the connection is
1073 * established or fails, or until timeout ellapses.
1075 GSocketError
_GSocket_Connect_Timeout(GSocket
*socket
)
1081 FD_ZERO(&exceptfds
);
1082 FD_SET(socket
->m_fd
, &writefds
);
1083 FD_SET(socket
->m_fd
, &exceptfds
);
1084 if (select(0, NULL
, &writefds
, &exceptfds
, &socket
->m_timeout
) == 0)
1086 socket
->m_error
= GSOCK_TIMEDOUT
;
1087 return GSOCK_TIMEDOUT
;
1089 if (!FD_ISSET(socket
->m_fd
, &writefds
))
1091 socket
->m_error
= GSOCK_IOERR
;
1095 return GSOCK_NOERROR
;
1098 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
1100 return recv(socket
->m_fd
, buffer
, size
, 0);
1103 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
1105 struct sockaddr from
;
1106 SOCKLEN_T fromlen
= sizeof(from
);
1110 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
1112 if (ret
== SOCKET_ERROR
)
1113 return SOCKET_ERROR
;
1115 /* Translate a system address into a GSocket address */
1116 if (!socket
->m_peer
)
1118 socket
->m_peer
= GAddress_new();
1119 if (!socket
->m_peer
)
1121 socket
->m_error
= GSOCK_MEMERR
;
1125 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
1126 if (err
!= GSOCK_NOERROR
)
1128 GAddress_destroy(socket
->m_peer
);
1129 socket
->m_peer
= NULL
;
1130 socket
->m_error
= err
;
1137 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
1139 return send(socket
->m_fd
, buffer
, size
, 0);
1142 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
1144 struct sockaddr
*addr
;
1148 if (!socket
->m_peer
)
1150 socket
->m_error
= GSOCK_INVADDR
;
1154 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
1155 if (err
!= GSOCK_NOERROR
)
1157 socket
->m_error
= err
;
1161 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
1163 /* Frees memory allocated by _GAddress_translate_to */
1171 * -------------------------------------------------------------------------
1173 * -------------------------------------------------------------------------
1176 /* CHECK_ADDRESS verifies that the current address family is either
1177 * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it
1178 * initalizes it to be a GSOCK_*family*. In other cases, it returns
1179 * an appropiate error code.
1181 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
1183 #define CHECK_ADDRESS(address, family) \
1185 if (address->m_family == GSOCK_NOFAMILY) \
1186 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1187 return address->m_error; \
1188 if (address->m_family != GSOCK_##family) \
1190 address->m_error = GSOCK_INVADDR; \
1191 return GSOCK_INVADDR; \
1195 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
1197 if (address->m_family == GSOCK_NOFAMILY) \
1198 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1200 if (address->m_family != GSOCK_##family) \
1202 address->m_error = GSOCK_INVADDR; \
1208 GAddress
*GAddress_new(void)
1212 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1215 address
->m_family
= GSOCK_NOFAMILY
;
1216 address
->m_addr
= NULL
;
1222 GAddress
*GAddress_copy(GAddress
*address
)
1226 assert(address
!= NULL
);
1228 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1231 memcpy(addr2
, address
, sizeof(GAddress
));
1233 if (address
->m_addr
)
1235 addr2
->m_addr
= (struct sockaddr
*) malloc(addr2
->m_len
);
1236 if (addr2
->m_addr
== NULL
)
1241 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1247 void GAddress_destroy(GAddress
*address
)
1249 assert(address
!= NULL
);
1251 if (address
->m_addr
)
1252 free(address
->m_addr
);
1257 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1259 assert(address
!= NULL
);
1261 address
->m_family
= type
;
1264 GAddressType
GAddress_GetFamily(GAddress
*address
)
1266 assert(address
!= NULL
);
1268 return address
->m_family
;
1271 GSocketError
_GAddress_translate_from(GAddress
*address
,
1272 struct sockaddr
*addr
, int len
)
1274 address
->m_realfamily
= addr
->sa_family
;
1275 switch (addr
->sa_family
)
1278 address
->m_family
= GSOCK_INET
;
1281 address
->m_family
= GSOCK_UNIX
;
1285 address
->m_family
= GSOCK_INET6
;
1290 address
->m_error
= GSOCK_INVOP
;
1295 if (address
->m_addr
)
1296 free(address
->m_addr
);
1298 address
->m_len
= len
;
1299 address
->m_addr
= (struct sockaddr
*) malloc(len
);
1301 if (address
->m_addr
== NULL
)
1303 address
->m_error
= GSOCK_MEMERR
;
1304 return GSOCK_MEMERR
;
1306 memcpy(address
->m_addr
, addr
, len
);
1308 return GSOCK_NOERROR
;
1311 GSocketError
_GAddress_translate_to(GAddress
*address
,
1312 struct sockaddr
**addr
, int *len
)
1314 if (!address
->m_addr
)
1316 address
->m_error
= GSOCK_INVADDR
;
1317 return GSOCK_INVADDR
;
1320 *len
= address
->m_len
;
1321 *addr
= (struct sockaddr
*) malloc(address
->m_len
);
1324 address
->m_error
= GSOCK_MEMERR
;
1325 return GSOCK_MEMERR
;
1328 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1329 return GSOCK_NOERROR
;
1333 * -------------------------------------------------------------------------
1334 * Internet address family
1335 * -------------------------------------------------------------------------
1338 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1340 address
->m_len
= sizeof(struct sockaddr_in
);
1341 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1342 if (address
->m_addr
== NULL
)
1344 address
->m_error
= GSOCK_MEMERR
;
1345 return GSOCK_MEMERR
;
1348 address
->m_family
= GSOCK_INET
;
1349 address
->m_realfamily
= AF_INET
;
1350 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1351 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1353 return GSOCK_NOERROR
;
1356 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1359 struct in_addr
*addr
;
1361 assert(address
!= NULL
);
1363 CHECK_ADDRESS(address
, INET
);
1365 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1367 addr
->s_addr
= inet_addr(hostname
);
1369 /* If it is a numeric host name, convert it now */
1370 if (addr
->s_addr
== INADDR_NONE
)
1372 struct in_addr
*array_addr
;
1374 /* It is a real name, we solve it */
1375 if ((he
= gethostbyname(hostname
)) == NULL
)
1377 /* addr->s_addr = INADDR_NONE just done by inet_addr() above */
1378 address
->m_error
= GSOCK_NOHOST
;
1379 return GSOCK_NOHOST
;
1381 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1382 addr
->s_addr
= array_addr
[0].s_addr
;
1384 return GSOCK_NOERROR
;
1387 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1389 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1392 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1393 unsigned long hostaddr
)
1395 struct in_addr
*addr
;
1397 assert(address
!= NULL
);
1399 CHECK_ADDRESS(address
, INET
);
1401 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1402 addr
->s_addr
= htonl(hostaddr
);;
1404 return GSOCK_NOERROR
;
1407 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1408 const char *protocol
)
1411 struct sockaddr_in
*addr
;
1413 assert(address
!= NULL
);
1414 CHECK_ADDRESS(address
, INET
);
1418 address
->m_error
= GSOCK_INVPORT
;
1419 return GSOCK_INVPORT
;
1422 se
= getservbyname(port
, protocol
);
1425 if (isdigit(port
[0]))
1429 port_int
= atoi(port
);
1430 addr
= (struct sockaddr_in
*)address
->m_addr
;
1431 addr
->sin_port
= htons((u_short
) port_int
);
1432 return GSOCK_NOERROR
;
1435 address
->m_error
= GSOCK_INVPORT
;
1436 return GSOCK_INVPORT
;
1439 addr
= (struct sockaddr_in
*)address
->m_addr
;
1440 addr
->sin_port
= se
->s_port
;
1442 return GSOCK_NOERROR
;
1445 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1447 struct sockaddr_in
*addr
;
1449 assert(address
!= NULL
);
1450 CHECK_ADDRESS(address
, INET
);
1452 addr
= (struct sockaddr_in
*)address
->m_addr
;
1453 addr
->sin_port
= htons(port
);
1455 return GSOCK_NOERROR
;
1458 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1462 struct sockaddr_in
*addr
;
1464 assert(address
!= NULL
);
1465 CHECK_ADDRESS(address
, INET
);
1467 addr
= (struct sockaddr_in
*)address
->m_addr
;
1468 addr_buf
= (char *)&(addr
->sin_addr
);
1470 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1473 address
->m_error
= GSOCK_NOHOST
;
1474 return GSOCK_NOHOST
;
1477 strncpy(hostname
, he
->h_name
, sbuf
);
1479 return GSOCK_NOERROR
;
1482 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1484 struct sockaddr_in
*addr
;
1486 assert(address
!= NULL
);
1487 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1489 addr
= (struct sockaddr_in
*)address
->m_addr
;
1491 return ntohl(addr
->sin_addr
.s_addr
);
1494 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1496 struct sockaddr_in
*addr
;
1498 assert(address
!= NULL
);
1499 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1501 addr
= (struct sockaddr_in
*)address
->m_addr
;
1502 return ntohs(addr
->sin_port
);
1506 * -------------------------------------------------------------------------
1507 * Unix address family
1508 * -------------------------------------------------------------------------
1511 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1513 assert (address
!= NULL
);
1514 address
->m_error
= GSOCK_INVADDR
;
1515 return GSOCK_INVADDR
;
1518 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1520 #if defined(__BORLANDC__)
1521 /* prevents unused variable message in Borland */
1524 assert (address
!= NULL
);
1525 address
->m_error
= GSOCK_INVADDR
;
1526 return GSOCK_INVADDR
;
1529 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1531 #if defined(__BORLANDC__)
1532 /* prevents unused variable message in Borland */
1536 assert (address
!= NULL
);
1537 address
->m_error
= GSOCK_INVADDR
;
1538 return GSOCK_INVADDR
;
1541 #else /* !wxUSE_SOCKETS */
1544 * Translation unit shouldn't be empty, so include this typedef to make the
1545 * compiler (VC++ 6.0, for example) happy
1547 typedef void (*wxDummy
)();
1549 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */