]>
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)
31 "unreferenced inline function has been removed": this is not
32 suppressed by push above as it is given at the end of the
35 # pragma warning(disable:4514)
36 #endif /* __WXWINCE__ */
41 #ifndef __GSOCKET_STANDALONE__
42 # include "wx/platform.h"
43 # include "wx/setup.h"
46 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
48 #ifndef __GSOCKET_STANDALONE__
49 # include "wx/msw/gsockmsw.h"
50 # include "wx/gsocket.h"
52 # include "gsockmsw.h"
54 #endif /* __GSOCKET_STANDALONE__ */
61 #define isdigit(x) (x > 47 && x < 58)
63 #include "wx/msw/wince/net.h"
72 /* if we use configure for MSW SOCKLEN_T will be already defined */
74 # define SOCKLEN_T int
77 /* Table of GUI-related functions. We must call them indirectly because
78 * of wxBase and GUI separation: */
80 static struct GSocketGUIFunctionsTable
*gs_gui_functions
;
82 #define USE_GUI() (gs_gui_functions != NULL)
84 /* Define macros to simplify indirection: */
85 #define _GSocket_GUI_Init() \
86 if (gs_gui_functions) gs_gui_functions->GUI_Init()
87 #define _GSocket_GUI_Cleanup() \
88 if (gs_gui_functions) gs_gui_functions->GUI_Cleanup()
89 #define _GSocket_GUI_Init_Socket(socket) \
90 (gs_gui_functions ? gs_gui_functions->GUI_Init_Socket(socket) : 1)
91 #define _GSocket_GUI_Destroy_Socket(socket) \
92 if (gs_gui_functions) gs_gui_functions->GUI_Destroy_Socket(socket)
93 #define _GSocket_Enable_Events(socket) \
94 if (gs_gui_functions) gs_gui_functions->Enable_Events(socket)
95 #define _GSocket_Disable_Events(socket) \
96 if (gs_gui_functions) gs_gui_functions->Disable_Events(socket)
97 #define _GSocket_Install_Callback(socket, event) \
98 if (gs_gui_functions) gs_gui_functions->Install_Callback(socket, event)
99 #define _GSocket_Uninstall_Callback(socket, event) \
100 if (gs_gui_functions) gs_gui_functions->Uninstall_Callback(socket, event)
102 /* Global initialisers */
104 void GSocket_SetGUIFunctions(struct GSocketGUIFunctionsTable
*guifunc
)
106 gs_gui_functions
= guifunc
;
109 int GSocket_Init(void)
113 if (gs_gui_functions
)
115 if ( !gs_gui_functions
->GUI_Init() )
119 /* Initialize WinSocket */
120 return (WSAStartup((1 << 8) | 1, &wsaData
) == 0);
123 void GSocket_Cleanup(void)
125 if (gs_gui_functions
)
127 gs_gui_functions
->GUI_Cleanup();
130 /* Cleanup WinSocket */
134 /* Constructors / Destructors for GSocket */
136 GSocket
*GSocket_new(void)
141 if ((socket
= (GSocket
*) malloc(sizeof(GSocket
))) == NULL
)
144 socket
->m_fd
= INVALID_SOCKET
;
145 for (i
= 0; i
< GSOCK_MAX_EVENT
; i
++)
147 socket
->m_cbacks
[i
] = NULL
;
149 socket
->m_detected
= 0;
150 socket
->m_local
= NULL
;
151 socket
->m_peer
= NULL
;
152 socket
->m_error
= GSOCK_NOERROR
;
153 socket
->m_server
= FALSE
;
154 socket
->m_stream
= TRUE
;
155 socket
->m_non_blocking
= FALSE
;
156 socket
->m_timeout
.tv_sec
= 10 * 60; /* 10 minutes */
157 socket
->m_timeout
.tv_usec
= 0;
158 socket
->m_establishing
= FALSE
;
160 /* Per-socket GUI-specific initialization */
161 success
= _GSocket_GUI_Init_Socket(socket
);
171 void GSocket_close(GSocket
*socket
)
173 _GSocket_Disable_Events(socket
);
174 closesocket(socket
->m_fd
);
175 socket
->m_fd
= INVALID_SOCKET
;
178 void GSocket_destroy(GSocket
*socket
)
180 assert(socket
!= NULL
);
182 /* Per-socket GUI-specific cleanup */
183 _GSocket_GUI_Destroy_Socket(socket
);
185 /* Check that the socket is really shutdowned */
186 if (socket
->m_fd
!= INVALID_SOCKET
)
187 GSocket_Shutdown(socket
);
189 /* Destroy private addresses */
191 GAddress_destroy(socket
->m_local
);
194 GAddress_destroy(socket
->m_peer
);
196 /* Destroy the socket itself */
201 * Disallow further read/write operations on this socket, close
202 * the fd and disable all callbacks.
204 void GSocket_Shutdown(GSocket
*socket
)
208 assert(socket
!= NULL
);
210 /* If socket has been created, shutdown it */
211 if (socket
->m_fd
!= INVALID_SOCKET
)
213 shutdown(socket
->m_fd
, 2);
214 GSocket_close(socket
);
217 /* Disable GUI callbacks */
218 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
219 socket
->m_cbacks
[evt
] = NULL
;
221 socket
->m_detected
= GSOCK_LOST_FLAG
;
224 /* Address handling */
230 * Set or get the local or peer address for this socket. The 'set'
231 * functions return GSOCK_NOERROR on success, an error code otherwise.
232 * The 'get' functions return a pointer to a GAddress object on success,
233 * or NULL otherwise, in which case they set the error code of the
234 * corresponding GSocket.
237 * GSOCK_INVSOCK - the socket is not valid.
238 * GSOCK_INVADDR - the address is not valid.
240 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
242 assert(socket
!= NULL
);
244 /* the socket must be initialized, or it must be a server */
245 if (socket
->m_fd
!= INVALID_SOCKET
&& !socket
->m_server
)
247 socket
->m_error
= GSOCK_INVSOCK
;
248 return GSOCK_INVSOCK
;
252 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
254 socket
->m_error
= GSOCK_INVADDR
;
255 return GSOCK_INVADDR
;
259 GAddress_destroy(socket
->m_local
);
261 socket
->m_local
= GAddress_copy(address
);
263 return GSOCK_NOERROR
;
266 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
268 assert(socket
!= NULL
);
271 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
273 socket
->m_error
= GSOCK_INVADDR
;
274 return GSOCK_INVADDR
;
278 GAddress_destroy(socket
->m_peer
);
280 socket
->m_peer
= GAddress_copy(address
);
282 return GSOCK_NOERROR
;
285 GAddress
*GSocket_GetLocal(GSocket
*socket
)
288 struct sockaddr addr
;
289 SOCKLEN_T size
= sizeof(addr
);
292 assert(socket
!= NULL
);
294 /* try to get it from the m_local var first */
296 return GAddress_copy(socket
->m_local
);
298 /* else, if the socket is initialized, try getsockname */
299 if (socket
->m_fd
== INVALID_SOCKET
)
301 socket
->m_error
= GSOCK_INVSOCK
;
305 if (getsockname(socket
->m_fd
, &addr
, &size
) == SOCKET_ERROR
)
307 socket
->m_error
= GSOCK_IOERR
;
311 /* got a valid address from getsockname, create a GAddress object */
312 if ((address
= GAddress_new()) == NULL
)
314 socket
->m_error
= GSOCK_MEMERR
;
318 if ((err
= _GAddress_translate_from(address
, &addr
, size
)) != GSOCK_NOERROR
)
320 GAddress_destroy(address
);
321 socket
->m_error
= err
;
328 GAddress
*GSocket_GetPeer(GSocket
*socket
)
330 assert(socket
!= NULL
);
332 /* try to get it from the m_peer var */
334 return GAddress_copy(socket
->m_peer
);
339 /* Server specific parts */
341 /* GSocket_SetServer:
342 * Sets up this socket as a server. The local address must have been
343 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
344 * Returns GSOCK_NOERROR on success, one of the following otherwise:
347 * GSOCK_INVSOCK - the socket is in use.
348 * GSOCK_INVADDR - the local address has not been set.
349 * GSOCK_IOERR - low-level error.
351 GSocketError
GSocket_SetServer(GSocket
*sck
)
357 /* must not be in use */
358 if (sck
->m_fd
!= INVALID_SOCKET
)
360 sck
->m_error
= GSOCK_INVSOCK
;
361 return GSOCK_INVSOCK
;
364 /* the local addr must have been set */
367 sck
->m_error
= GSOCK_INVADDR
;
368 return GSOCK_INVADDR
;
371 /* Initialize all fields */
372 sck
->m_server
= TRUE
;
373 sck
->m_stream
= TRUE
;
374 sck
->m_oriented
= TRUE
;
376 /* Create the socket */
377 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
379 if (sck
->m_fd
== INVALID_SOCKET
)
381 sck
->m_error
= GSOCK_IOERR
;
385 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
386 _GSocket_Enable_Events(sck
);
388 /* allow a socket to re-bind if the socket is in the TIME_WAIT
389 state after being previously closed.
391 setsockopt(sck
->m_fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char*)&arg
, sizeof(u_long
));
393 /* Bind to the local address,
394 * retrieve the actual address bound,
395 * and listen up to 5 connections.
397 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
398 (getsockname(sck
->m_fd
,
399 sck
->m_local
->m_addr
,
400 (SOCKLEN_T
*)&sck
->m_local
->m_len
) != 0) ||
401 (listen(sck
->m_fd
, 5) != 0))
404 sck
->m_error
= GSOCK_IOERR
;
408 return GSOCK_NOERROR
;
411 /* GSocket_WaitConnection:
412 * Waits for an incoming client connection. Returns a pointer to
413 * a GSocket object, or NULL if there was an error, in which case
414 * the last error field will be updated for the calling GSocket.
416 * Error codes (set in the calling GSocket)
417 * GSOCK_INVSOCK - the socket is not valid or not a server.
418 * GSOCK_TIMEDOUT - timeout, no incoming connections.
419 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
420 * GSOCK_MEMERR - couldn't allocate memory.
421 * GSOCK_IOERR - low-level error.
423 GSocket
*GSocket_WaitConnection(GSocket
*sck
)
426 struct sockaddr from
;
427 SOCKLEN_T fromlen
= sizeof(from
);
433 /* Reenable CONNECTION events */
434 sck
->m_detected
&= ~GSOCK_CONNECTION_FLAG
;
436 /* If the socket has already been created, we exit immediately */
437 if (sck
->m_fd
== INVALID_SOCKET
|| !sck
->m_server
)
439 sck
->m_error
= GSOCK_INVSOCK
;
443 /* Create a GSocket object for the new connection */
444 connection
= GSocket_new();
448 sck
->m_error
= GSOCK_MEMERR
;
452 /* Wait for a connection (with timeout) */
453 if (_GSocket_Input_Timeout(sck
) == GSOCK_TIMEDOUT
)
455 GSocket_destroy(connection
);
456 /* sck->m_error set by _GSocket_Input_Timeout */
460 connection
->m_fd
= accept(sck
->m_fd
, &from
, &fromlen
);
462 if (connection
->m_fd
== INVALID_SOCKET
)
464 if (WSAGetLastError() == WSAEWOULDBLOCK
)
465 sck
->m_error
= GSOCK_WOULDBLOCK
;
467 sck
->m_error
= GSOCK_IOERR
;
469 GSocket_destroy(connection
);
473 /* Initialize all fields */
474 connection
->m_server
= FALSE
;
475 connection
->m_stream
= TRUE
;
476 connection
->m_oriented
= TRUE
;
478 /* Setup the peer address field */
479 connection
->m_peer
= GAddress_new();
480 if (!connection
->m_peer
)
482 GSocket_destroy(connection
);
483 sck
->m_error
= GSOCK_MEMERR
;
486 err
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
);
487 if (err
!= GSOCK_NOERROR
)
489 GAddress_destroy(connection
->m_peer
);
490 GSocket_destroy(connection
);
495 ioctlsocket(connection
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
496 _GSocket_Enable_Events(connection
);
501 /* Client specific parts */
504 * For stream (connection oriented) sockets, GSocket_Connect() tries
505 * to establish a client connection to a server using the peer address
506 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
507 * connection has been succesfully established, or one of the error
508 * codes listed below. Note that for nonblocking sockets, a return
509 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
510 * request can be completed later; you should use GSocket_Select()
511 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
512 * corresponding asynchronous events.
514 * For datagram (non connection oriented) sockets, GSocket_Connect()
515 * just sets the peer address established with GSocket_SetPeer() as
516 * default destination.
519 * GSOCK_INVSOCK - the socket is in use or not valid.
520 * GSOCK_INVADDR - the peer address has not been established.
521 * GSOCK_TIMEDOUT - timeout, the connection failed.
522 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
523 * GSOCK_MEMERR - couldn't allocate memory.
524 * GSOCK_IOERR - low-level error.
526 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
533 /* Enable CONNECTION events (needed for nonblocking connections) */
534 sck
->m_detected
&= ~GSOCK_CONNECTION_FLAG
;
536 if (sck
->m_fd
!= INVALID_SOCKET
)
538 sck
->m_error
= GSOCK_INVSOCK
;
539 return GSOCK_INVSOCK
;
544 sck
->m_error
= GSOCK_INVADDR
;
545 return GSOCK_INVADDR
;
548 /* Streamed or dgram socket? */
549 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
550 sck
->m_oriented
= TRUE
;
551 sck
->m_server
= FALSE
;
552 sck
->m_establishing
= FALSE
;
554 /* Create the socket */
555 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
,
556 sck
->m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
558 if (sck
->m_fd
== INVALID_SOCKET
)
560 sck
->m_error
= GSOCK_IOERR
;
564 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
565 _GSocket_Enable_Events(sck
);
567 /* Connect it to the peer address, with a timeout (see below) */
568 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
570 if (ret
== SOCKET_ERROR
)
572 err
= WSAGetLastError();
574 /* If connect failed with EWOULDBLOCK and the GSocket object
575 * is in blocking mode, we select() for the specified timeout
576 * checking for writability to see if the connection request
579 if ((err
== WSAEWOULDBLOCK
) && (!sck
->m_non_blocking
))
581 err
= _GSocket_Connect_Timeout(sck
);
583 if (err
!= GSOCK_NOERROR
)
586 /* sck->m_error is set in _GSocket_Connect_Timeout */
589 return (GSocketError
) err
;
592 /* If connect failed with EWOULDBLOCK and the GSocket object
593 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
594 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
595 * this way if the connection completes, a GSOCK_CONNECTION
596 * event will be generated, if enabled.
598 if ((err
== WSAEWOULDBLOCK
) && (sck
->m_non_blocking
))
600 sck
->m_establishing
= TRUE
;
601 sck
->m_error
= GSOCK_WOULDBLOCK
;
602 return GSOCK_WOULDBLOCK
;
605 /* If connect failed with an error other than EWOULDBLOCK,
606 * then the call to GSocket_Connect() has failed.
609 sck
->m_error
= GSOCK_IOERR
;
613 return GSOCK_NOERROR
;
616 /* Datagram sockets */
618 /* GSocket_SetNonOriented:
619 * Sets up this socket as a non-connection oriented (datagram) socket.
620 * Before using this function, the local address must have been set
621 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
622 * on success, or one of the following otherwise.
625 * GSOCK_INVSOCK - the socket is in use.
626 * GSOCK_INVADDR - the local address has not been set.
627 * GSOCK_IOERR - low-level error.
629 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
635 if (sck
->m_fd
!= INVALID_SOCKET
)
637 sck
->m_error
= GSOCK_INVSOCK
;
638 return GSOCK_INVSOCK
;
643 sck
->m_error
= GSOCK_INVADDR
;
644 return GSOCK_INVADDR
;
647 /* Initialize all fields */
648 sck
->m_stream
= FALSE
;
649 sck
->m_server
= FALSE
;
650 sck
->m_oriented
= FALSE
;
652 /* Create the socket */
653 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
655 if (sck
->m_fd
== INVALID_SOCKET
)
657 sck
->m_error
= GSOCK_IOERR
;
661 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
662 _GSocket_Enable_Events(sck
);
664 /* Bind to the local address,
665 * and retrieve the actual address bound.
667 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
668 (getsockname(sck
->m_fd
,
669 sck
->m_local
->m_addr
,
670 (SOCKLEN_T
*)&sck
->m_local
->m_len
) != 0))
673 sck
->m_error
= GSOCK_IOERR
;
677 return GSOCK_NOERROR
;
682 /* Like recv(), send(), ... */
683 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
687 assert(socket
!= NULL
);
689 /* Reenable INPUT events */
690 socket
->m_detected
&= ~GSOCK_INPUT_FLAG
;
692 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
694 socket
->m_error
= GSOCK_INVSOCK
;
698 /* If the socket is blocking, wait for data (with a timeout) */
699 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
703 if (socket
->m_stream
)
704 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
706 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
708 if (ret
== SOCKET_ERROR
)
710 if (WSAGetLastError() != WSAEWOULDBLOCK
)
711 socket
->m_error
= GSOCK_IOERR
;
713 socket
->m_error
= GSOCK_WOULDBLOCK
;
720 int GSocket_Write(GSocket
*socket
, const char *buffer
, int size
)
724 assert(socket
!= NULL
);
726 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
728 socket
->m_error
= GSOCK_INVSOCK
;
732 /* If the socket is blocking, wait for writability (with a timeout) */
733 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
737 if (socket
->m_stream
)
738 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
740 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
742 if (ret
== SOCKET_ERROR
)
744 if (WSAGetLastError() != WSAEWOULDBLOCK
)
745 socket
->m_error
= GSOCK_IOERR
;
747 socket
->m_error
= GSOCK_WOULDBLOCK
;
749 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
750 * does). Once the first OUTPUT event is received, users can assume
751 * that the socket is writable until a read operation fails. Only then
752 * will further OUTPUT events be posted.
754 socket
->m_detected
&= ~GSOCK_OUTPUT_FLAG
;
762 * Polls the socket to determine its status. This function will
763 * check for the events specified in the 'flags' parameter, and
764 * it will return a mask indicating which operations can be
765 * performed. This function won't block, regardless of the
766 * mode (blocking | nonblocking) of the socket.
768 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
772 GSocketEventFlags result
= 0;
777 assert(socket
!= NULL
);
782 FD_SET(socket
->m_fd
, &readfds
);
783 if (flags
& GSOCK_OUTPUT_FLAG
)
784 FD_SET(socket
->m_fd
, &writefds
);
785 FD_SET(socket
->m_fd
, &exceptfds
);
787 /* Check 'sticky' CONNECTION flag first */
788 result
|= (GSOCK_CONNECTION_FLAG
& socket
->m_detected
);
790 /* If we have already detected a LOST event, then don't try
791 * to do any further processing.
793 if ((socket
->m_detected
& GSOCK_LOST_FLAG
) != 0)
795 socket
->m_establishing
= FALSE
;
797 return (GSOCK_LOST_FLAG
& flags
);
801 if (select(socket
->m_fd
+ 1, &readfds
, &writefds
, &exceptfds
,
802 &socket
->m_timeout
) <= 0)
804 /* What to do here? */
805 return (result
& flags
);
808 /* Check for readability */
809 if (FD_ISSET(socket
->m_fd
, &readfds
))
813 if (!socket
->m_stream
|| recv(socket
->m_fd
, &c
, 1, MSG_PEEK
) > 0)
815 result
|= GSOCK_INPUT_FLAG
;
819 if (socket
->m_server
&& socket
->m_stream
)
821 result
|= GSOCK_CONNECTION_FLAG
;
822 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
826 socket
->m_detected
= GSOCK_LOST_FLAG
;
827 socket
->m_establishing
= FALSE
;
829 /* LOST event: Abort any further processing */
830 return (GSOCK_LOST_FLAG
& flags
);
835 /* Check for writability */
836 if (FD_ISSET(socket
->m_fd
, &writefds
))
838 if (socket
->m_establishing
&& !socket
->m_server
)
841 SOCKLEN_T len
= sizeof(error
);
843 socket
->m_establishing
= FALSE
;
845 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*)&error
, &len
);
849 socket
->m_detected
= GSOCK_LOST_FLAG
;
851 /* LOST event: Abort any further processing */
852 return (GSOCK_LOST_FLAG
& flags
);
856 result
|= GSOCK_CONNECTION_FLAG
;
857 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
862 result
|= GSOCK_OUTPUT_FLAG
;
866 /* Check for exceptions and errors (is this useful in Unices?) */
867 if (FD_ISSET(socket
->m_fd
, &exceptfds
))
869 socket
->m_establishing
= FALSE
;
870 socket
->m_detected
= GSOCK_LOST_FLAG
;
872 /* LOST event: Abort any further processing */
873 return (GSOCK_LOST_FLAG
& flags
);
876 return (result
& flags
);
880 assert(socket
!= NULL
);
881 return flags
& socket
->m_detected
;
887 /* GSocket_SetNonBlocking:
888 * Sets the socket to non-blocking mode. All IO calls will return
891 void GSocket_SetNonBlocking(GSocket
*socket
, int non_block
)
893 assert(socket
!= NULL
);
895 socket
->m_non_blocking
= non_block
;
898 /* GSocket_SetTimeout:
899 * Sets the timeout for blocking calls. Time is expressed in
902 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millis
)
904 assert(socket
!= NULL
);
906 socket
->m_timeout
.tv_sec
= (millis
/ 1000);
907 socket
->m_timeout
.tv_usec
= (millis
% 1000) * 1000;
911 * Returns the last error occured for this socket. Note that successful
912 * operations do not clear this back to GSOCK_NOERROR, so use it only
915 GSocketError
GSocket_GetError(GSocket
*socket
)
917 assert(socket
!= NULL
);
919 return socket
->m_error
;
925 * There is data to be read in the input buffer. If, after a read
926 * operation, there is still data available, the callback function will
929 * The socket is available for writing. That is, the next write call
930 * won't block. This event is generated only once, when the connection is
931 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
932 * when the output buffer empties again. This means that the app should
933 * assume that it can write since the first OUTPUT event, and no more
934 * OUTPUT events will be generated unless an error occurs.
936 * Connection succesfully established, for client sockets, or incoming
937 * client connection, for server sockets. Wait for this event (also watch
938 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
940 * The connection is lost (or a connection request failed); this could
941 * be due to a failure, or due to the peer closing it gracefully.
944 /* GSocket_SetCallback:
945 * Enables the callbacks specified by 'flags'. Note that 'flags'
946 * may be a combination of flags OR'ed toghether, so the same
947 * callback function can be made to accept different events.
948 * The callback function must have the following prototype:
950 * void function(GSocket *socket, GSocketEvent event, char *cdata)
952 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
953 GSocketCallback callback
, char *cdata
)
957 assert(socket
!= NULL
);
959 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
961 if ((flags
& (1 << count
)) != 0)
963 socket
->m_cbacks
[count
] = callback
;
964 socket
->m_data
[count
] = cdata
;
969 /* GSocket_UnsetCallback:
970 * Disables all callbacks specified by 'flags', which may be a
971 * combination of flags OR'ed toghether.
973 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
977 assert(socket
!= NULL
);
979 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
981 if ((flags
& (1 << count
)) != 0)
983 socket
->m_cbacks
[count
] = NULL
;
984 socket
->m_data
[count
] = NULL
;
991 /* _GSocket_Input_Timeout:
992 * For blocking sockets, wait until data is available or
993 * until timeout ellapses.
995 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
999 if (!socket
->m_non_blocking
)
1002 FD_SET(socket
->m_fd
, &readfds
);
1003 if (select(0, &readfds
, NULL
, NULL
, &socket
->m_timeout
) == 0)
1005 socket
->m_error
= GSOCK_TIMEDOUT
;
1006 return GSOCK_TIMEDOUT
;
1009 return GSOCK_NOERROR
;
1012 /* _GSocket_Output_Timeout:
1013 * For blocking sockets, wait until data can be sent without
1014 * blocking or until timeout ellapses.
1016 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
1020 if (!socket
->m_non_blocking
)
1023 FD_SET(socket
->m_fd
, &writefds
);
1024 if (select(0, NULL
, &writefds
, NULL
, &socket
->m_timeout
) == 0)
1026 socket
->m_error
= GSOCK_TIMEDOUT
;
1027 return GSOCK_TIMEDOUT
;
1030 return GSOCK_NOERROR
;
1033 /* _GSocket_Connect_Timeout:
1034 * For blocking sockets, wait until the connection is
1035 * established or fails, or until timeout ellapses.
1037 GSocketError
_GSocket_Connect_Timeout(GSocket
*socket
)
1043 FD_ZERO(&exceptfds
);
1044 FD_SET(socket
->m_fd
, &writefds
);
1045 FD_SET(socket
->m_fd
, &exceptfds
);
1046 if (select(0, NULL
, &writefds
, &exceptfds
, &socket
->m_timeout
) == 0)
1048 socket
->m_error
= GSOCK_TIMEDOUT
;
1049 return GSOCK_TIMEDOUT
;
1051 if (!FD_ISSET(socket
->m_fd
, &writefds
))
1053 socket
->m_error
= GSOCK_IOERR
;
1057 return GSOCK_NOERROR
;
1060 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
1062 return recv(socket
->m_fd
, buffer
, size
, 0);
1065 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
1067 struct sockaddr from
;
1068 SOCKLEN_T fromlen
= sizeof(from
);
1072 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
1074 if (ret
== SOCKET_ERROR
)
1075 return SOCKET_ERROR
;
1077 /* Translate a system address into a GSocket address */
1078 if (!socket
->m_peer
)
1080 socket
->m_peer
= GAddress_new();
1081 if (!socket
->m_peer
)
1083 socket
->m_error
= GSOCK_MEMERR
;
1087 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
1088 if (err
!= GSOCK_NOERROR
)
1090 GAddress_destroy(socket
->m_peer
);
1091 socket
->m_peer
= NULL
;
1092 socket
->m_error
= err
;
1099 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
1101 return send(socket
->m_fd
, buffer
, size
, 0);
1104 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
1106 struct sockaddr
*addr
;
1110 if (!socket
->m_peer
)
1112 socket
->m_error
= GSOCK_INVADDR
;
1116 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
1117 if (err
!= GSOCK_NOERROR
)
1119 socket
->m_error
= err
;
1123 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
1125 /* Frees memory allocated by _GAddress_translate_to */
1133 * -------------------------------------------------------------------------
1135 * -------------------------------------------------------------------------
1138 /* CHECK_ADDRESS verifies that the current address family is either
1139 * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it
1140 * initalizes it to be a GSOCK_*family*. In other cases, it returns
1141 * an appropiate error code.
1143 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
1145 #define CHECK_ADDRESS(address, family) \
1147 if (address->m_family == GSOCK_NOFAMILY) \
1148 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1149 return address->m_error; \
1150 if (address->m_family != GSOCK_##family) \
1152 address->m_error = GSOCK_INVADDR; \
1153 return GSOCK_INVADDR; \
1157 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
1159 if (address->m_family == GSOCK_NOFAMILY) \
1160 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1162 if (address->m_family != GSOCK_##family) \
1164 address->m_error = GSOCK_INVADDR; \
1170 GAddress
*GAddress_new(void)
1174 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1177 address
->m_family
= GSOCK_NOFAMILY
;
1178 address
->m_addr
= NULL
;
1184 GAddress
*GAddress_copy(GAddress
*address
)
1188 assert(address
!= NULL
);
1190 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1193 memcpy(addr2
, address
, sizeof(GAddress
));
1195 if (address
->m_addr
)
1197 addr2
->m_addr
= (struct sockaddr
*) malloc(addr2
->m_len
);
1198 if (addr2
->m_addr
== NULL
)
1203 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1209 void GAddress_destroy(GAddress
*address
)
1211 assert(address
!= NULL
);
1213 if (address
->m_addr
)
1214 free(address
->m_addr
);
1219 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1221 assert(address
!= NULL
);
1223 address
->m_family
= type
;
1226 GAddressType
GAddress_GetFamily(GAddress
*address
)
1228 assert(address
!= NULL
);
1230 return address
->m_family
;
1233 GSocketError
_GAddress_translate_from(GAddress
*address
,
1234 struct sockaddr
*addr
, int len
)
1236 address
->m_realfamily
= addr
->sa_family
;
1237 switch (addr
->sa_family
)
1240 address
->m_family
= GSOCK_INET
;
1243 address
->m_family
= GSOCK_UNIX
;
1247 address
->m_family
= GSOCK_INET6
;
1252 address
->m_error
= GSOCK_INVOP
;
1257 if (address
->m_addr
)
1258 free(address
->m_addr
);
1260 address
->m_len
= len
;
1261 address
->m_addr
= (struct sockaddr
*) malloc(len
);
1263 if (address
->m_addr
== NULL
)
1265 address
->m_error
= GSOCK_MEMERR
;
1266 return GSOCK_MEMERR
;
1268 memcpy(address
->m_addr
, addr
, len
);
1270 return GSOCK_NOERROR
;
1273 GSocketError
_GAddress_translate_to(GAddress
*address
,
1274 struct sockaddr
**addr
, int *len
)
1276 if (!address
->m_addr
)
1278 address
->m_error
= GSOCK_INVADDR
;
1279 return GSOCK_INVADDR
;
1282 *len
= address
->m_len
;
1283 *addr
= (struct sockaddr
*) malloc(address
->m_len
);
1286 address
->m_error
= GSOCK_MEMERR
;
1287 return GSOCK_MEMERR
;
1290 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1291 return GSOCK_NOERROR
;
1295 * -------------------------------------------------------------------------
1296 * Internet address family
1297 * -------------------------------------------------------------------------
1300 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1302 address
->m_len
= sizeof(struct sockaddr_in
);
1303 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1304 if (address
->m_addr
== NULL
)
1306 address
->m_error
= GSOCK_MEMERR
;
1307 return GSOCK_MEMERR
;
1310 address
->m_family
= GSOCK_INET
;
1311 address
->m_realfamily
= PF_INET
;
1312 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1313 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1315 return GSOCK_NOERROR
;
1318 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1321 struct in_addr
*addr
;
1323 assert(address
!= NULL
);
1325 CHECK_ADDRESS(address
, INET
);
1327 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1329 addr
->s_addr
= inet_addr(hostname
);
1331 /* If it is a numeric host name, convert it now */
1332 if (addr
->s_addr
== INADDR_NONE
)
1334 struct in_addr
*array_addr
;
1336 /* It is a real name, we solve it */
1337 if ((he
= gethostbyname(hostname
)) == NULL
)
1339 /* addr->s_addr = INADDR_NONE just done by inet_addr() above */
1340 address
->m_error
= GSOCK_NOHOST
;
1341 return GSOCK_NOHOST
;
1343 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1344 addr
->s_addr
= array_addr
[0].s_addr
;
1346 return GSOCK_NOERROR
;
1349 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1351 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1354 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1355 unsigned long hostaddr
)
1357 struct in_addr
*addr
;
1359 assert(address
!= NULL
);
1361 CHECK_ADDRESS(address
, INET
);
1363 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1364 addr
->s_addr
= hostaddr
;
1366 return GSOCK_NOERROR
;
1369 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1370 const char *protocol
)
1373 struct sockaddr_in
*addr
;
1375 assert(address
!= NULL
);
1376 CHECK_ADDRESS(address
, INET
);
1380 address
->m_error
= GSOCK_INVPORT
;
1381 return GSOCK_INVPORT
;
1384 se
= getservbyname(port
, protocol
);
1387 if (isdigit(port
[0]))
1391 port_int
= atoi(port
);
1392 addr
= (struct sockaddr_in
*)address
->m_addr
;
1393 addr
->sin_port
= htons((u_short
) port_int
);
1394 return GSOCK_NOERROR
;
1397 address
->m_error
= GSOCK_INVPORT
;
1398 return GSOCK_INVPORT
;
1401 addr
= (struct sockaddr_in
*)address
->m_addr
;
1402 addr
->sin_port
= se
->s_port
;
1404 return GSOCK_NOERROR
;
1407 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1409 struct sockaddr_in
*addr
;
1411 assert(address
!= NULL
);
1412 CHECK_ADDRESS(address
, INET
);
1414 addr
= (struct sockaddr_in
*)address
->m_addr
;
1415 addr
->sin_port
= htons(port
);
1417 return GSOCK_NOERROR
;
1420 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1424 struct sockaddr_in
*addr
;
1426 assert(address
!= NULL
);
1427 CHECK_ADDRESS(address
, INET
);
1429 addr
= (struct sockaddr_in
*)address
->m_addr
;
1430 addr_buf
= (char *)&(addr
->sin_addr
);
1432 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1435 address
->m_error
= GSOCK_NOHOST
;
1436 return GSOCK_NOHOST
;
1439 strncpy(hostname
, he
->h_name
, sbuf
);
1441 return GSOCK_NOERROR
;
1444 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1446 struct sockaddr_in
*addr
;
1448 assert(address
!= NULL
);
1449 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1451 addr
= (struct sockaddr_in
*)address
->m_addr
;
1453 return addr
->sin_addr
.s_addr
;
1456 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1458 struct sockaddr_in
*addr
;
1460 assert(address
!= NULL
);
1461 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1463 addr
= (struct sockaddr_in
*)address
->m_addr
;
1464 return ntohs(addr
->sin_port
);
1468 * -------------------------------------------------------------------------
1469 * Unix address family
1470 * -------------------------------------------------------------------------
1473 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1475 assert (address
!= NULL
);
1476 address
->m_error
= GSOCK_INVADDR
;
1477 return GSOCK_INVADDR
;
1480 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1482 #if defined(__BORLANDC__)
1483 /* prevents unused variable message in Borland */
1486 assert (address
!= NULL
);
1487 address
->m_error
= GSOCK_INVADDR
;
1488 return GSOCK_INVADDR
;
1491 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1493 #if defined(__BORLANDC__)
1494 /* prevents unused variable message in Borland */
1498 assert (address
!= NULL
);
1499 address
->m_error
= GSOCK_INVADDR
;
1500 return GSOCK_INVADDR
;
1503 #else /* !wxUSE_SOCKETS */
1506 * Translation unit shouldn't be empty, so include this typedef to make the
1507 * compiler (VC++ 6.0, for example) happy
1509 typedef void (*wxDummy
)();
1511 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */