]>
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 /* Bind to the local address,
380 * retrieve the actual address bound,
381 * and listen up to 5 connections.
383 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
384 (getsockname(sck
->m_fd
,
385 sck
->m_local
->m_addr
,
386 (SOCKLEN_T
*)&sck
->m_local
->m_len
) != 0) ||
387 (listen(sck
->m_fd
, 5) != 0))
390 sck
->m_error
= GSOCK_IOERR
;
394 return GSOCK_NOERROR
;
397 /* GSocket_WaitConnection:
398 * Waits for an incoming client connection. Returns a pointer to
399 * a GSocket object, or NULL if there was an error, in which case
400 * the last error field will be updated for the calling GSocket.
402 * Error codes (set in the calling GSocket)
403 * GSOCK_INVSOCK - the socket is not valid or not a server.
404 * GSOCK_TIMEDOUT - timeout, no incoming connections.
405 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
406 * GSOCK_MEMERR - couldn't allocate memory.
407 * GSOCK_IOERR - low-level error.
409 GSocket
*GSocket_WaitConnection(GSocket
*sck
)
412 struct sockaddr from
;
413 SOCKLEN_T fromlen
= sizeof(from
);
419 /* Reenable CONNECTION events */
420 sck
->m_detected
&= ~GSOCK_CONNECTION_FLAG
;
422 /* If the socket has already been created, we exit immediately */
423 if (sck
->m_fd
== INVALID_SOCKET
|| !sck
->m_server
)
425 sck
->m_error
= GSOCK_INVSOCK
;
429 /* Create a GSocket object for the new connection */
430 connection
= GSocket_new();
434 sck
->m_error
= GSOCK_MEMERR
;
438 /* Wait for a connection (with timeout) */
439 if (_GSocket_Input_Timeout(sck
) == GSOCK_TIMEDOUT
)
441 GSocket_destroy(connection
);
442 /* sck->m_error set by _GSocket_Input_Timeout */
446 connection
->m_fd
= accept(sck
->m_fd
, &from
, &fromlen
);
448 if (connection
->m_fd
== INVALID_SOCKET
)
450 if (WSAGetLastError() == WSAEWOULDBLOCK
)
451 sck
->m_error
= GSOCK_WOULDBLOCK
;
453 sck
->m_error
= GSOCK_IOERR
;
455 GSocket_destroy(connection
);
459 /* Initialize all fields */
460 connection
->m_server
= FALSE
;
461 connection
->m_stream
= TRUE
;
462 connection
->m_oriented
= TRUE
;
464 /* Setup the peer address field */
465 connection
->m_peer
= GAddress_new();
466 if (!connection
->m_peer
)
468 GSocket_destroy(connection
);
469 sck
->m_error
= GSOCK_MEMERR
;
472 err
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
);
473 if (err
!= GSOCK_NOERROR
)
475 GAddress_destroy(connection
->m_peer
);
476 GSocket_destroy(connection
);
481 ioctlsocket(connection
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
482 _GSocket_Enable_Events(connection
);
487 /* Client specific parts */
490 * For stream (connection oriented) sockets, GSocket_Connect() tries
491 * to establish a client connection to a server using the peer address
492 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
493 * connection has been succesfully established, or one of the error
494 * codes listed below. Note that for nonblocking sockets, a return
495 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
496 * request can be completed later; you should use GSocket_Select()
497 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
498 * corresponding asynchronous events.
500 * For datagram (non connection oriented) sockets, GSocket_Connect()
501 * just sets the peer address established with GSocket_SetPeer() as
502 * default destination.
505 * GSOCK_INVSOCK - the socket is in use or not valid.
506 * GSOCK_INVADDR - the peer address has not been established.
507 * GSOCK_TIMEDOUT - timeout, the connection failed.
508 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
509 * GSOCK_MEMERR - couldn't allocate memory.
510 * GSOCK_IOERR - low-level error.
512 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
519 /* Enable CONNECTION events (needed for nonblocking connections) */
520 sck
->m_detected
&= ~GSOCK_CONNECTION_FLAG
;
522 if (sck
->m_fd
!= INVALID_SOCKET
)
524 sck
->m_error
= GSOCK_INVSOCK
;
525 return GSOCK_INVSOCK
;
530 sck
->m_error
= GSOCK_INVADDR
;
531 return GSOCK_INVADDR
;
534 /* Streamed or dgram socket? */
535 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
536 sck
->m_oriented
= TRUE
;
537 sck
->m_server
= FALSE
;
538 sck
->m_establishing
= FALSE
;
540 /* Create the socket */
541 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
,
542 sck
->m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
544 if (sck
->m_fd
== INVALID_SOCKET
)
546 sck
->m_error
= GSOCK_IOERR
;
550 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
551 _GSocket_Enable_Events(sck
);
553 /* Connect it to the peer address, with a timeout (see below) */
554 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
556 if (ret
== SOCKET_ERROR
)
558 err
= WSAGetLastError();
560 /* If connect failed with EWOULDBLOCK and the GSocket object
561 * is in blocking mode, we select() for the specified timeout
562 * checking for writability to see if the connection request
565 if ((err
== WSAEWOULDBLOCK
) && (!sck
->m_non_blocking
))
567 err
= _GSocket_Connect_Timeout(sck
);
569 if (err
!= GSOCK_NOERROR
)
572 /* sck->m_error is set in _GSocket_Connect_Timeout */
575 return (GSocketError
) err
;
578 /* If connect failed with EWOULDBLOCK and the GSocket object
579 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
580 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
581 * this way if the connection completes, a GSOCK_CONNECTION
582 * event will be generated, if enabled.
584 if ((err
== WSAEWOULDBLOCK
) && (sck
->m_non_blocking
))
586 sck
->m_establishing
= TRUE
;
587 sck
->m_error
= GSOCK_WOULDBLOCK
;
588 return GSOCK_WOULDBLOCK
;
591 /* If connect failed with an error other than EWOULDBLOCK,
592 * then the call to GSocket_Connect() has failed.
595 sck
->m_error
= GSOCK_IOERR
;
599 return GSOCK_NOERROR
;
602 /* Datagram sockets */
604 /* GSocket_SetNonOriented:
605 * Sets up this socket as a non-connection oriented (datagram) socket.
606 * Before using this function, the local address must have been set
607 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
608 * on success, or one of the following otherwise.
611 * GSOCK_INVSOCK - the socket is in use.
612 * GSOCK_INVADDR - the local address has not been set.
613 * GSOCK_IOERR - low-level error.
615 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
621 if (sck
->m_fd
!= INVALID_SOCKET
)
623 sck
->m_error
= GSOCK_INVSOCK
;
624 return GSOCK_INVSOCK
;
629 sck
->m_error
= GSOCK_INVADDR
;
630 return GSOCK_INVADDR
;
633 /* Initialize all fields */
634 sck
->m_stream
= FALSE
;
635 sck
->m_server
= FALSE
;
636 sck
->m_oriented
= FALSE
;
638 /* Create the socket */
639 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
641 if (sck
->m_fd
== INVALID_SOCKET
)
643 sck
->m_error
= GSOCK_IOERR
;
647 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
648 _GSocket_Enable_Events(sck
);
650 /* Bind to the local address,
651 * and retrieve the actual address bound.
653 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
654 (getsockname(sck
->m_fd
,
655 sck
->m_local
->m_addr
,
656 (SOCKLEN_T
*)&sck
->m_local
->m_len
) != 0))
659 sck
->m_error
= GSOCK_IOERR
;
663 return GSOCK_NOERROR
;
668 /* Like recv(), send(), ... */
669 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
673 assert(socket
!= NULL
);
675 /* Reenable INPUT events */
676 socket
->m_detected
&= ~GSOCK_INPUT_FLAG
;
678 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
680 socket
->m_error
= GSOCK_INVSOCK
;
684 /* If the socket is blocking, wait for data (with a timeout) */
685 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
689 if (socket
->m_stream
)
690 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
692 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
694 if (ret
== SOCKET_ERROR
)
696 if (WSAGetLastError() != WSAEWOULDBLOCK
)
697 socket
->m_error
= GSOCK_IOERR
;
699 socket
->m_error
= GSOCK_WOULDBLOCK
;
706 int GSocket_Write(GSocket
*socket
, const char *buffer
, int size
)
710 assert(socket
!= NULL
);
712 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
714 socket
->m_error
= GSOCK_INVSOCK
;
718 /* If the socket is blocking, wait for writability (with a timeout) */
719 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
723 if (socket
->m_stream
)
724 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
726 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
728 if (ret
== SOCKET_ERROR
)
730 if (WSAGetLastError() != WSAEWOULDBLOCK
)
731 socket
->m_error
= GSOCK_IOERR
;
733 socket
->m_error
= GSOCK_WOULDBLOCK
;
735 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
736 * does). Once the first OUTPUT event is received, users can assume
737 * that the socket is writable until a read operation fails. Only then
738 * will further OUTPUT events be posted.
740 socket
->m_detected
&= ~GSOCK_OUTPUT_FLAG
;
748 * Polls the socket to determine its status. This function will
749 * check for the events specified in the 'flags' parameter, and
750 * it will return a mask indicating which operations can be
751 * performed. This function won't block, regardless of the
752 * mode (blocking | nonblocking) of the socket.
754 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
758 GSocketEventFlags result
= 0;
762 static const struct timeval tv
= { 0, 0 };
764 assert(socket
!= NULL
);
769 FD_SET(socket
->m_fd
, &readfds
);
770 FD_SET(socket
->m_fd
, &writefds
);
771 FD_SET(socket
->m_fd
, &exceptfds
);
773 /* Check 'sticky' CONNECTION flag first */
774 result
|= (GSOCK_CONNECTION_FLAG
& socket
->m_detected
);
776 /* If we have already detected a LOST event, then don't try
777 * to do any further processing.
779 if ((socket
->m_detected
& GSOCK_LOST_FLAG
) != 0)
781 socket
->m_establishing
= FALSE
;
783 return (GSOCK_LOST_FLAG
& flags
);
787 if (select(socket
->m_fd
+ 1, &readfds
, &writefds
, &exceptfds
, &tv
) <= 0)
789 /* What to do here? */
790 return (result
& flags
);
793 /* Check for readability */
794 if (FD_ISSET(socket
->m_fd
, &readfds
))
798 if (recv(socket
->m_fd
, &c
, 1, MSG_PEEK
) > 0)
800 result
|= GSOCK_INPUT_FLAG
;
804 if (socket
->m_server
&& socket
->m_stream
)
806 result
|= GSOCK_CONNECTION_FLAG
;
807 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
811 socket
->m_detected
= GSOCK_LOST_FLAG
;
812 socket
->m_establishing
= FALSE
;
814 /* LOST event: Abort any further processing */
815 return (GSOCK_LOST_FLAG
& flags
);
820 /* Check for writability */
821 if (FD_ISSET(socket
->m_fd
, &writefds
))
823 if (socket
->m_establishing
&& !socket
->m_server
)
826 SOCKLEN_T len
= sizeof(error
);
828 socket
->m_establishing
= FALSE
;
830 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*)&error
, &len
);
834 socket
->m_detected
= GSOCK_LOST_FLAG
;
836 /* LOST event: Abort any further processing */
837 return (GSOCK_LOST_FLAG
& flags
);
841 result
|= GSOCK_CONNECTION_FLAG
;
842 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
847 result
|= GSOCK_OUTPUT_FLAG
;
851 /* Check for exceptions and errors (is this useful in Unices?) */
852 if (FD_ISSET(socket
->m_fd
, &exceptfds
))
854 socket
->m_establishing
= FALSE
;
855 socket
->m_detected
= GSOCK_LOST_FLAG
;
857 /* LOST event: Abort any further processing */
858 return (GSOCK_LOST_FLAG
& flags
);
861 return (result
& flags
);
865 assert(socket
!= NULL
);
866 return flags
& socket
->m_detected
;
872 /* GSocket_SetNonBlocking:
873 * Sets the socket to non-blocking mode. All IO calls will return
876 void GSocket_SetNonBlocking(GSocket
*socket
, int non_block
)
878 assert(socket
!= NULL
);
880 socket
->m_non_blocking
= non_block
;
883 /* GSocket_SetTimeout:
884 * Sets the timeout for blocking calls. Time is expressed in
887 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millis
)
889 assert(socket
!= NULL
);
891 socket
->m_timeout
.tv_sec
= (millis
/ 1000);
892 socket
->m_timeout
.tv_usec
= (millis
% 1000) * 1000;
896 * Returns the last error occured for this socket. Note that successful
897 * operations do not clear this back to GSOCK_NOERROR, so use it only
900 GSocketError
GSocket_GetError(GSocket
*socket
)
902 assert(socket
!= NULL
);
904 return socket
->m_error
;
910 * There is data to be read in the input buffer. If, after a read
911 * operation, there is still data available, the callback function will
914 * The socket is available for writing. That is, the next write call
915 * won't block. This event is generated only once, when the connection is
916 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
917 * when the output buffer empties again. This means that the app should
918 * assume that it can write since the first OUTPUT event, and no more
919 * OUTPUT events will be generated unless an error occurs.
921 * Connection succesfully established, for client sockets, or incoming
922 * client connection, for server sockets. Wait for this event (also watch
923 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
925 * The connection is lost (or a connection request failed); this could
926 * be due to a failure, or due to the peer closing it gracefully.
929 /* GSocket_SetCallback:
930 * Enables the callbacks specified by 'flags'. Note that 'flags'
931 * may be a combination of flags OR'ed toghether, so the same
932 * callback function can be made to accept different events.
933 * The callback function must have the following prototype:
935 * void function(GSocket *socket, GSocketEvent event, char *cdata)
937 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
938 GSocketCallback callback
, char *cdata
)
942 assert(socket
!= NULL
);
944 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
946 if ((flags
& (1 << count
)) != 0)
948 socket
->m_cbacks
[count
] = callback
;
949 socket
->m_data
[count
] = cdata
;
954 /* GSocket_UnsetCallback:
955 * Disables all callbacks specified by 'flags', which may be a
956 * combination of flags OR'ed toghether.
958 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
962 assert(socket
!= NULL
);
964 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
966 if ((flags
& (1 << count
)) != 0)
968 socket
->m_cbacks
[count
] = NULL
;
969 socket
->m_data
[count
] = NULL
;
976 /* _GSocket_Input_Timeout:
977 * For blocking sockets, wait until data is available or
978 * until timeout ellapses.
980 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
984 if (!socket
->m_non_blocking
)
987 FD_SET(socket
->m_fd
, &readfds
);
988 if (select(0, &readfds
, NULL
, NULL
, &socket
->m_timeout
) == 0)
990 socket
->m_error
= GSOCK_TIMEDOUT
;
991 return GSOCK_TIMEDOUT
;
994 return GSOCK_NOERROR
;
997 /* _GSocket_Output_Timeout:
998 * For blocking sockets, wait until data can be sent without
999 * blocking or until timeout ellapses.
1001 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
1005 if (!socket
->m_non_blocking
)
1008 FD_SET(socket
->m_fd
, &writefds
);
1009 if (select(0, NULL
, &writefds
, NULL
, &socket
->m_timeout
) == 0)
1011 socket
->m_error
= GSOCK_TIMEDOUT
;
1012 return GSOCK_TIMEDOUT
;
1015 return GSOCK_NOERROR
;
1018 /* _GSocket_Connect_Timeout:
1019 * For blocking sockets, wait until the connection is
1020 * established or fails, or until timeout ellapses.
1022 GSocketError
_GSocket_Connect_Timeout(GSocket
*socket
)
1028 FD_ZERO(&exceptfds
);
1029 FD_SET(socket
->m_fd
, &writefds
);
1030 FD_SET(socket
->m_fd
, &exceptfds
);
1031 if (select(0, NULL
, &writefds
, &exceptfds
, &socket
->m_timeout
) == 0)
1033 socket
->m_error
= GSOCK_TIMEDOUT
;
1034 return GSOCK_TIMEDOUT
;
1036 if (!FD_ISSET(socket
->m_fd
, &writefds
))
1038 socket
->m_error
= GSOCK_IOERR
;
1042 return GSOCK_NOERROR
;
1045 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
1047 return recv(socket
->m_fd
, buffer
, size
, 0);
1050 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
1052 struct sockaddr from
;
1053 SOCKLEN_T fromlen
= sizeof(from
);
1057 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
1059 if (ret
== SOCKET_ERROR
)
1060 return SOCKET_ERROR
;
1062 /* Translate a system address into a GSocket address */
1063 if (!socket
->m_peer
)
1065 socket
->m_peer
= GAddress_new();
1066 if (!socket
->m_peer
)
1068 socket
->m_error
= GSOCK_MEMERR
;
1072 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
1073 if (err
!= GSOCK_NOERROR
)
1075 GAddress_destroy(socket
->m_peer
);
1076 socket
->m_peer
= NULL
;
1077 socket
->m_error
= err
;
1084 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
1086 return send(socket
->m_fd
, buffer
, size
, 0);
1089 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
1091 struct sockaddr
*addr
;
1095 if (!socket
->m_peer
)
1097 socket
->m_error
= GSOCK_INVADDR
;
1101 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
1102 if (err
!= GSOCK_NOERROR
)
1104 socket
->m_error
= err
;
1108 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
1110 /* Frees memory allocated by _GAddress_translate_to */
1118 * -------------------------------------------------------------------------
1120 * -------------------------------------------------------------------------
1123 /* CHECK_ADDRESS verifies that the current address family is either
1124 * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it
1125 * initalizes it to be a GSOCK_*family*. In other cases, it returns
1126 * an appropiate error code.
1128 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
1130 #define CHECK_ADDRESS(address, family) \
1132 if (address->m_family == GSOCK_NOFAMILY) \
1133 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1134 return address->m_error; \
1135 if (address->m_family != GSOCK_##family) \
1137 address->m_error = GSOCK_INVADDR; \
1138 return GSOCK_INVADDR; \
1142 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
1144 if (address->m_family == GSOCK_NOFAMILY) \
1145 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1147 if (address->m_family != GSOCK_##family) \
1149 address->m_error = GSOCK_INVADDR; \
1155 GAddress
*GAddress_new(void)
1159 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1162 address
->m_family
= GSOCK_NOFAMILY
;
1163 address
->m_addr
= NULL
;
1169 GAddress
*GAddress_copy(GAddress
*address
)
1173 assert(address
!= NULL
);
1175 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1178 memcpy(addr2
, address
, sizeof(GAddress
));
1180 if (address
->m_addr
)
1182 addr2
->m_addr
= (struct sockaddr
*) malloc(addr2
->m_len
);
1183 if (addr2
->m_addr
== NULL
)
1188 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1194 void GAddress_destroy(GAddress
*address
)
1196 assert(address
!= NULL
);
1198 if (address
->m_addr
)
1199 free(address
->m_addr
);
1204 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1206 assert(address
!= NULL
);
1208 address
->m_family
= type
;
1211 GAddressType
GAddress_GetFamily(GAddress
*address
)
1213 assert(address
!= NULL
);
1215 return address
->m_family
;
1218 GSocketError
_GAddress_translate_from(GAddress
*address
,
1219 struct sockaddr
*addr
, int len
)
1221 address
->m_realfamily
= addr
->sa_family
;
1222 switch (addr
->sa_family
)
1225 address
->m_family
= GSOCK_INET
;
1228 address
->m_family
= GSOCK_UNIX
;
1232 address
->m_family
= GSOCK_INET6
;
1237 address
->m_error
= GSOCK_INVOP
;
1242 if (address
->m_addr
)
1243 free(address
->m_addr
);
1245 address
->m_len
= len
;
1246 address
->m_addr
= (struct sockaddr
*) malloc(len
);
1248 if (address
->m_addr
== NULL
)
1250 address
->m_error
= GSOCK_MEMERR
;
1251 return GSOCK_MEMERR
;
1253 memcpy(address
->m_addr
, addr
, len
);
1255 return GSOCK_NOERROR
;
1258 GSocketError
_GAddress_translate_to(GAddress
*address
,
1259 struct sockaddr
**addr
, int *len
)
1261 if (!address
->m_addr
)
1263 address
->m_error
= GSOCK_INVADDR
;
1264 return GSOCK_INVADDR
;
1267 *len
= address
->m_len
;
1268 *addr
= (struct sockaddr
*) malloc(address
->m_len
);
1271 address
->m_error
= GSOCK_MEMERR
;
1272 return GSOCK_MEMERR
;
1275 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1276 return GSOCK_NOERROR
;
1280 * -------------------------------------------------------------------------
1281 * Internet address family
1282 * -------------------------------------------------------------------------
1285 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1287 address
->m_len
= sizeof(struct sockaddr_in
);
1288 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1289 if (address
->m_addr
== NULL
)
1291 address
->m_error
= GSOCK_MEMERR
;
1292 return GSOCK_MEMERR
;
1295 address
->m_family
= GSOCK_INET
;
1296 address
->m_realfamily
= PF_INET
;
1297 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1298 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1300 return GSOCK_NOERROR
;
1303 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1306 struct in_addr
*addr
;
1308 assert(address
!= NULL
);
1310 CHECK_ADDRESS(address
, INET
);
1312 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1314 addr
->s_addr
= inet_addr(hostname
);
1316 /* If it is a numeric host name, convert it now */
1317 if (addr
->s_addr
== INADDR_NONE
)
1319 struct in_addr
*array_addr
;
1321 /* It is a real name, we solve it */
1322 if ((he
= gethostbyname(hostname
)) == NULL
)
1324 /* addr->s_addr = INADDR_NONE just done by inet_addr() above */
1325 address
->m_error
= GSOCK_NOHOST
;
1326 return GSOCK_NOHOST
;
1328 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1329 addr
->s_addr
= array_addr
[0].s_addr
;
1331 return GSOCK_NOERROR
;
1334 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1336 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1339 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1340 unsigned long hostaddr
)
1342 struct in_addr
*addr
;
1344 assert(address
!= NULL
);
1346 CHECK_ADDRESS(address
, INET
);
1348 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1349 addr
->s_addr
= hostaddr
;
1351 return GSOCK_NOERROR
;
1354 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1355 const char *protocol
)
1358 struct sockaddr_in
*addr
;
1360 assert(address
!= NULL
);
1361 CHECK_ADDRESS(address
, INET
);
1365 address
->m_error
= GSOCK_INVPORT
;
1366 return GSOCK_INVPORT
;
1369 se
= getservbyname(port
, protocol
);
1372 if (isdigit(port
[0]))
1376 port_int
= atoi(port
);
1377 addr
= (struct sockaddr_in
*)address
->m_addr
;
1378 addr
->sin_port
= htons((u_short
) port_int
);
1379 return GSOCK_NOERROR
;
1382 address
->m_error
= GSOCK_INVPORT
;
1383 return GSOCK_INVPORT
;
1386 addr
= (struct sockaddr_in
*)address
->m_addr
;
1387 addr
->sin_port
= se
->s_port
;
1389 return GSOCK_NOERROR
;
1392 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1394 struct sockaddr_in
*addr
;
1396 assert(address
!= NULL
);
1397 CHECK_ADDRESS(address
, INET
);
1399 addr
= (struct sockaddr_in
*)address
->m_addr
;
1400 addr
->sin_port
= htons(port
);
1402 return GSOCK_NOERROR
;
1405 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1409 struct sockaddr_in
*addr
;
1411 assert(address
!= NULL
);
1412 CHECK_ADDRESS(address
, INET
);
1414 addr
= (struct sockaddr_in
*)address
->m_addr
;
1415 addr_buf
= (char *)&(addr
->sin_addr
);
1417 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1420 address
->m_error
= GSOCK_NOHOST
;
1421 return GSOCK_NOHOST
;
1424 strncpy(hostname
, he
->h_name
, sbuf
);
1426 return GSOCK_NOERROR
;
1429 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1431 struct sockaddr_in
*addr
;
1433 assert(address
!= NULL
);
1434 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1436 addr
= (struct sockaddr_in
*)address
->m_addr
;
1438 return addr
->sin_addr
.s_addr
;
1441 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1443 struct sockaddr_in
*addr
;
1445 assert(address
!= NULL
);
1446 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1448 addr
= (struct sockaddr_in
*)address
->m_addr
;
1449 return ntohs(addr
->sin_port
);
1453 * -------------------------------------------------------------------------
1454 * Unix address family
1455 * -------------------------------------------------------------------------
1458 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1460 assert (address
!= NULL
);
1461 address
->m_error
= GSOCK_INVADDR
;
1462 return GSOCK_INVADDR
;
1465 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1467 #if defined(__BORLANDC__)
1468 /* prevents unused variable message in Borland */
1471 assert (address
!= NULL
);
1472 address
->m_error
= GSOCK_INVADDR
;
1473 return GSOCK_INVADDR
;
1476 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1478 #if defined(__BORLANDC__)
1479 /* prevents unused variable message in Borland */
1483 assert (address
!= NULL
);
1484 address
->m_error
= GSOCK_INVADDR
;
1485 return GSOCK_INVADDR
;
1488 #else /* !wxUSE_SOCKETS */
1491 * Translation unit shouldn't be empty, so include this typedef to make the
1492 * compiler (VC++ 6.0, for example) happy
1494 typedef void (*wxDummy
)();
1496 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */