]>
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__
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__ */
47 /* Redefine some GUI-only functions to do nothing in console mode */
48 #if defined(wxUSE_GUI) && !wxUSE_GUI
49 # define _GSocket_GUI_Init(socket) (1)
50 # define _GSocket_GUI_Destroy(socket)
51 # define _GSocket_Enable_Events(socket)
52 # define _GSocket_Disable_Events(socket)
53 #endif /* wxUSE_GUI */
60 #define isdigit(x) (x > 47 && x < 58)
62 #include "wx/msw/wince/net.h"
71 /* if we use configure for MSW SOCKLEN_T will be already defined */
73 # define SOCKLEN_T int
76 /* Table of GUI-related functions. We must call them indirectly because
77 * of wxBase and GUI separation: */
79 static struct GSocketGUIFunctionsTable
*gs_gui_functions
;
81 #define USE_GUI() (gs_gui_functions != NULL)
83 /* Define macros to simplify indirection: */
84 #define _GSocket_GUI_Init() \
85 if (gs_gui_functions) gs_gui_functions->GUI_Init()
86 #define _GSocket_GUI_Cleanup() \
87 if (gs_gui_functions) gs_gui_functions->GUI_Cleanup()
88 #define _GSocket_GUI_Init_Socket(socket) \
89 (gs_gui_functions ? gs_gui_functions->GUI_Init_Socket(socket) : 1)
90 #define _GSocket_GUI_Destroy_Socket(socket) \
91 if (gs_gui_functions) gs_gui_functions->GUI_Destroy_Socket(socket)
92 #define _GSocket_Enable_Events(socket) \
93 if (gs_gui_functions) gs_gui_functions->Enable_Events(socket)
94 #define _GSocket_Disable_Events(socket) \
95 if (gs_gui_functions) gs_gui_functions->Disable_Events(socket)
96 #define _GSocket_Install_Callback(socket, event) \
97 if (gs_gui_functions) gs_gui_functions->Install_Callback(socket, event)
98 #define _GSocket_Uninstall_Callback(socket, event) \
99 if (gs_gui_functions) gs_gui_functions->Uninstall_Callback(socket, event)
101 /* Global initialisers */
103 void GSocket_SetGUIFunctions(struct GSocketGUIFunctionsTable
*guifunc
)
105 gs_gui_functions
= guifunc
;
108 int GSocket_Init(void)
112 if (gs_gui_functions
)
114 if ( !gs_gui_functions
->GUI_Init() )
118 /* Initialize WinSocket */
119 return (WSAStartup((1 << 8) | 1, &wsaData
) == 0);
122 void GSocket_Cleanup(void)
124 if (gs_gui_functions
)
126 gs_gui_functions
->GUI_Cleanup();
129 /* Cleanup WinSocket */
133 /* Constructors / Destructors for GSocket */
135 GSocket
*GSocket_new(void)
140 if ((socket
= (GSocket
*) malloc(sizeof(GSocket
))) == NULL
)
143 socket
->m_fd
= INVALID_SOCKET
;
144 for (i
= 0; i
< GSOCK_MAX_EVENT
; i
++)
146 socket
->m_cbacks
[i
] = NULL
;
148 socket
->m_detected
= 0;
149 socket
->m_local
= NULL
;
150 socket
->m_peer
= NULL
;
151 socket
->m_error
= GSOCK_NOERROR
;
152 socket
->m_server
= FALSE
;
153 socket
->m_stream
= TRUE
;
154 socket
->m_non_blocking
= FALSE
;
155 socket
->m_timeout
.tv_sec
= 10 * 60; /* 10 minutes */
156 socket
->m_timeout
.tv_usec
= 0;
157 socket
->m_establishing
= FALSE
;
159 /* Per-socket GUI-specific initialization */
160 success
= _GSocket_GUI_Init_Socket(socket
);
170 void GSocket_close(GSocket
*socket
)
172 _GSocket_Disable_Events(socket
);
173 closesocket(socket
->m_fd
);
174 socket
->m_fd
= INVALID_SOCKET
;
177 void GSocket_destroy(GSocket
*socket
)
179 assert(socket
!= NULL
);
181 /* Per-socket GUI-specific cleanup */
182 _GSocket_GUI_Destroy_Socket(socket
);
184 /* Check that the socket is really shutdowned */
185 if (socket
->m_fd
!= INVALID_SOCKET
)
186 GSocket_Shutdown(socket
);
188 /* Destroy private addresses */
190 GAddress_destroy(socket
->m_local
);
193 GAddress_destroy(socket
->m_peer
);
195 /* Destroy the socket itself */
200 * Disallow further read/write operations on this socket, close
201 * the fd and disable all callbacks.
203 void GSocket_Shutdown(GSocket
*socket
)
207 assert(socket
!= NULL
);
209 /* If socket has been created, shutdown it */
210 if (socket
->m_fd
!= INVALID_SOCKET
)
212 shutdown(socket
->m_fd
, 2);
213 GSocket_close(socket
);
216 /* Disable GUI callbacks */
217 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
218 socket
->m_cbacks
[evt
] = NULL
;
220 socket
->m_detected
= GSOCK_LOST_FLAG
;
223 /* Address handling */
229 * Set or get the local or peer address for this socket. The 'set'
230 * functions return GSOCK_NOERROR on success, an error code otherwise.
231 * The 'get' functions return a pointer to a GAddress object on success,
232 * or NULL otherwise, in which case they set the error code of the
233 * corresponding GSocket.
236 * GSOCK_INVSOCK - the socket is not valid.
237 * GSOCK_INVADDR - the address is not valid.
239 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
241 assert(socket
!= NULL
);
243 /* the socket must be initialized, or it must be a server */
244 if (socket
->m_fd
!= INVALID_SOCKET
&& !socket
->m_server
)
246 socket
->m_error
= GSOCK_INVSOCK
;
247 return GSOCK_INVSOCK
;
251 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
253 socket
->m_error
= GSOCK_INVADDR
;
254 return GSOCK_INVADDR
;
258 GAddress_destroy(socket
->m_local
);
260 socket
->m_local
= GAddress_copy(address
);
262 return GSOCK_NOERROR
;
265 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
267 assert(socket
!= NULL
);
270 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
272 socket
->m_error
= GSOCK_INVADDR
;
273 return GSOCK_INVADDR
;
277 GAddress_destroy(socket
->m_peer
);
279 socket
->m_peer
= GAddress_copy(address
);
281 return GSOCK_NOERROR
;
284 GAddress
*GSocket_GetLocal(GSocket
*socket
)
287 struct sockaddr addr
;
288 SOCKLEN_T size
= sizeof(addr
);
291 assert(socket
!= NULL
);
293 /* try to get it from the m_local var first */
295 return GAddress_copy(socket
->m_local
);
297 /* else, if the socket is initialized, try getsockname */
298 if (socket
->m_fd
== INVALID_SOCKET
)
300 socket
->m_error
= GSOCK_INVSOCK
;
304 if (getsockname(socket
->m_fd
, &addr
, &size
) == SOCKET_ERROR
)
306 socket
->m_error
= GSOCK_IOERR
;
310 /* got a valid address from getsockname, create a GAddress object */
311 if ((address
= GAddress_new()) == NULL
)
313 socket
->m_error
= GSOCK_MEMERR
;
317 if ((err
= _GAddress_translate_from(address
, &addr
, size
)) != GSOCK_NOERROR
)
319 GAddress_destroy(address
);
320 socket
->m_error
= err
;
327 GAddress
*GSocket_GetPeer(GSocket
*socket
)
329 assert(socket
!= NULL
);
331 /* try to get it from the m_peer var */
333 return GAddress_copy(socket
->m_peer
);
338 /* Server specific parts */
340 /* GSocket_SetServer:
341 * Sets up this socket as a server. The local address must have been
342 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
343 * Returns GSOCK_NOERROR on success, one of the following otherwise:
346 * GSOCK_INVSOCK - the socket is in use.
347 * GSOCK_INVADDR - the local address has not been set.
348 * GSOCK_IOERR - low-level error.
350 GSocketError
GSocket_SetServer(GSocket
*sck
)
356 /* must not be in use */
357 if (sck
->m_fd
!= INVALID_SOCKET
)
359 sck
->m_error
= GSOCK_INVSOCK
;
360 return GSOCK_INVSOCK
;
363 /* the local addr must have been set */
366 sck
->m_error
= GSOCK_INVADDR
;
367 return GSOCK_INVADDR
;
370 /* Initialize all fields */
371 sck
->m_server
= TRUE
;
372 sck
->m_stream
= TRUE
;
373 sck
->m_oriented
= TRUE
;
375 /* Create the socket */
376 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
378 if (sck
->m_fd
== INVALID_SOCKET
)
380 sck
->m_error
= GSOCK_IOERR
;
384 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
385 _GSocket_Enable_Events(sck
);
387 /* Bind to the local address,
388 * retrieve the actual address bound,
389 * and listen up to 5 connections.
391 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
392 (getsockname(sck
->m_fd
,
393 sck
->m_local
->m_addr
,
394 (SOCKLEN_T
*)&sck
->m_local
->m_len
) != 0) ||
395 (listen(sck
->m_fd
, 5) != 0))
398 sck
->m_error
= GSOCK_IOERR
;
402 return GSOCK_NOERROR
;
405 /* GSocket_WaitConnection:
406 * Waits for an incoming client connection. Returns a pointer to
407 * a GSocket object, or NULL if there was an error, in which case
408 * the last error field will be updated for the calling GSocket.
410 * Error codes (set in the calling GSocket)
411 * GSOCK_INVSOCK - the socket is not valid or not a server.
412 * GSOCK_TIMEDOUT - timeout, no incoming connections.
413 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
414 * GSOCK_MEMERR - couldn't allocate memory.
415 * GSOCK_IOERR - low-level error.
417 GSocket
*GSocket_WaitConnection(GSocket
*sck
)
420 struct sockaddr from
;
421 SOCKLEN_T fromlen
= sizeof(from
);
427 /* Reenable CONNECTION events */
428 sck
->m_detected
&= ~GSOCK_CONNECTION_FLAG
;
430 /* If the socket has already been created, we exit immediately */
431 if (sck
->m_fd
== INVALID_SOCKET
|| !sck
->m_server
)
433 sck
->m_error
= GSOCK_INVSOCK
;
437 /* Create a GSocket object for the new connection */
438 connection
= GSocket_new();
442 sck
->m_error
= GSOCK_MEMERR
;
446 /* Wait for a connection (with timeout) */
447 if (_GSocket_Input_Timeout(sck
) == GSOCK_TIMEDOUT
)
449 GSocket_destroy(connection
);
450 /* sck->m_error set by _GSocket_Input_Timeout */
454 connection
->m_fd
= accept(sck
->m_fd
, &from
, &fromlen
);
456 if (connection
->m_fd
== INVALID_SOCKET
)
458 if (WSAGetLastError() == WSAEWOULDBLOCK
)
459 sck
->m_error
= GSOCK_WOULDBLOCK
;
461 sck
->m_error
= GSOCK_IOERR
;
463 GSocket_destroy(connection
);
467 /* Initialize all fields */
468 connection
->m_server
= FALSE
;
469 connection
->m_stream
= TRUE
;
470 connection
->m_oriented
= TRUE
;
472 /* Setup the peer address field */
473 connection
->m_peer
= GAddress_new();
474 if (!connection
->m_peer
)
476 GSocket_destroy(connection
);
477 sck
->m_error
= GSOCK_MEMERR
;
480 err
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
);
481 if (err
!= GSOCK_NOERROR
)
483 GAddress_destroy(connection
->m_peer
);
484 GSocket_destroy(connection
);
489 ioctlsocket(connection
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
490 _GSocket_Enable_Events(connection
);
495 /* Client specific parts */
498 * For stream (connection oriented) sockets, GSocket_Connect() tries
499 * to establish a client connection to a server using the peer address
500 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
501 * connection has been succesfully established, or one of the error
502 * codes listed below. Note that for nonblocking sockets, a return
503 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
504 * request can be completed later; you should use GSocket_Select()
505 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
506 * corresponding asynchronous events.
508 * For datagram (non connection oriented) sockets, GSocket_Connect()
509 * just sets the peer address established with GSocket_SetPeer() as
510 * default destination.
513 * GSOCK_INVSOCK - the socket is in use or not valid.
514 * GSOCK_INVADDR - the peer address has not been established.
515 * GSOCK_TIMEDOUT - timeout, the connection failed.
516 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
517 * GSOCK_MEMERR - couldn't allocate memory.
518 * GSOCK_IOERR - low-level error.
520 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
527 /* Enable CONNECTION events (needed for nonblocking connections) */
528 sck
->m_detected
&= ~GSOCK_CONNECTION_FLAG
;
530 if (sck
->m_fd
!= INVALID_SOCKET
)
532 sck
->m_error
= GSOCK_INVSOCK
;
533 return GSOCK_INVSOCK
;
538 sck
->m_error
= GSOCK_INVADDR
;
539 return GSOCK_INVADDR
;
542 /* Streamed or dgram socket? */
543 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
544 sck
->m_oriented
= TRUE
;
545 sck
->m_server
= FALSE
;
546 sck
->m_establishing
= FALSE
;
548 /* Create the socket */
549 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
,
550 sck
->m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
552 if (sck
->m_fd
== INVALID_SOCKET
)
554 sck
->m_error
= GSOCK_IOERR
;
558 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
559 _GSocket_Enable_Events(sck
);
561 /* Connect it to the peer address, with a timeout (see below) */
562 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
564 if (ret
== SOCKET_ERROR
)
566 err
= WSAGetLastError();
568 /* If connect failed with EWOULDBLOCK and the GSocket object
569 * is in blocking mode, we select() for the specified timeout
570 * checking for writability to see if the connection request
573 if ((err
== WSAEWOULDBLOCK
) && (!sck
->m_non_blocking
))
575 err
= _GSocket_Connect_Timeout(sck
);
577 if (err
!= GSOCK_NOERROR
)
580 /* sck->m_error is set in _GSocket_Connect_Timeout */
583 return (GSocketError
) err
;
586 /* If connect failed with EWOULDBLOCK and the GSocket object
587 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
588 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
589 * this way if the connection completes, a GSOCK_CONNECTION
590 * event will be generated, if enabled.
592 if ((err
== WSAEWOULDBLOCK
) && (sck
->m_non_blocking
))
594 sck
->m_establishing
= TRUE
;
595 sck
->m_error
= GSOCK_WOULDBLOCK
;
596 return GSOCK_WOULDBLOCK
;
599 /* If connect failed with an error other than EWOULDBLOCK,
600 * then the call to GSocket_Connect() has failed.
603 sck
->m_error
= GSOCK_IOERR
;
607 return GSOCK_NOERROR
;
610 /* Datagram sockets */
612 /* GSocket_SetNonOriented:
613 * Sets up this socket as a non-connection oriented (datagram) socket.
614 * Before using this function, the local address must have been set
615 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
616 * on success, or one of the following otherwise.
619 * GSOCK_INVSOCK - the socket is in use.
620 * GSOCK_INVADDR - the local address has not been set.
621 * GSOCK_IOERR - low-level error.
623 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
629 if (sck
->m_fd
!= INVALID_SOCKET
)
631 sck
->m_error
= GSOCK_INVSOCK
;
632 return GSOCK_INVSOCK
;
637 sck
->m_error
= GSOCK_INVADDR
;
638 return GSOCK_INVADDR
;
641 /* Initialize all fields */
642 sck
->m_stream
= FALSE
;
643 sck
->m_server
= FALSE
;
644 sck
->m_oriented
= FALSE
;
646 /* Create the socket */
647 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
649 if (sck
->m_fd
== INVALID_SOCKET
)
651 sck
->m_error
= GSOCK_IOERR
;
655 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
656 _GSocket_Enable_Events(sck
);
658 /* Bind to the local address,
659 * and retrieve the actual address bound.
661 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
662 (getsockname(sck
->m_fd
,
663 sck
->m_local
->m_addr
,
664 (SOCKLEN_T
*)&sck
->m_local
->m_len
) != 0))
667 sck
->m_error
= GSOCK_IOERR
;
671 return GSOCK_NOERROR
;
676 /* Like recv(), send(), ... */
677 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
681 assert(socket
!= NULL
);
683 /* Reenable INPUT events */
684 socket
->m_detected
&= ~GSOCK_INPUT_FLAG
;
686 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
688 socket
->m_error
= GSOCK_INVSOCK
;
692 /* If the socket is blocking, wait for data (with a timeout) */
693 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
697 if (socket
->m_stream
)
698 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
700 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
702 if (ret
== SOCKET_ERROR
)
704 if (WSAGetLastError() != WSAEWOULDBLOCK
)
705 socket
->m_error
= GSOCK_IOERR
;
707 socket
->m_error
= GSOCK_WOULDBLOCK
;
714 int GSocket_Write(GSocket
*socket
, const char *buffer
, int size
)
718 assert(socket
!= NULL
);
720 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
722 socket
->m_error
= GSOCK_INVSOCK
;
726 /* If the socket is blocking, wait for writability (with a timeout) */
727 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
731 if (socket
->m_stream
)
732 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
734 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
736 if (ret
== SOCKET_ERROR
)
738 if (WSAGetLastError() != WSAEWOULDBLOCK
)
739 socket
->m_error
= GSOCK_IOERR
;
741 socket
->m_error
= GSOCK_WOULDBLOCK
;
743 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
744 * does). Once the first OUTPUT event is received, users can assume
745 * that the socket is writable until a read operation fails. Only then
746 * will further OUTPUT events be posted.
748 socket
->m_detected
&= ~GSOCK_OUTPUT_FLAG
;
756 * Polls the socket to determine its status. This function will
757 * check for the events specified in the 'flags' parameter, and
758 * it will return a mask indicating which operations can be
759 * performed. This function won't block, regardless of the
760 * mode (blocking | nonblocking) of the socket.
762 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
764 #if defined(wxUSE_GUI) && !wxUSE_GUI
766 GSocketEventFlags result
= 0;
770 static const struct timeval tv
= { 0, 0 };
772 assert(socket
!= NULL
);
777 FD_SET(socket
->m_fd
, &readfds
);
778 FD_SET(socket
->m_fd
, &writefds
);
779 FD_SET(socket
->m_fd
, &exceptfds
);
781 /* Check 'sticky' CONNECTION flag first */
782 result
|= (GSOCK_CONNECTION_FLAG
& socket
->m_detected
);
784 /* If we have already detected a LOST event, then don't try
785 * to do any further processing.
787 if ((socket
->m_detected
& GSOCK_LOST_FLAG
) != 0)
789 socket
->m_establishing
= FALSE
;
791 return (GSOCK_LOST_FLAG
& flags
);
795 if (select(socket
->m_fd
+ 1, &readfds
, &writefds
, &exceptfds
, &tv
) <= 0)
797 /* What to do here? */
798 return (result
& flags
);
801 /* Check for readability */
802 if (FD_ISSET(socket
->m_fd
, &readfds
))
806 if (recv(socket
->m_fd
, &c
, 1, MSG_PEEK
) > 0)
808 result
|= GSOCK_INPUT_FLAG
;
812 if (socket
->m_server
&& socket
->m_stream
)
814 result
|= GSOCK_CONNECTION_FLAG
;
815 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
819 socket
->m_detected
= GSOCK_LOST_FLAG
;
820 socket
->m_establishing
= FALSE
;
822 /* LOST event: Abort any further processing */
823 return (GSOCK_LOST_FLAG
& flags
);
828 /* Check for writability */
829 if (FD_ISSET(socket
->m_fd
, &writefds
))
831 if (socket
->m_establishing
&& !socket
->m_server
)
834 SOCKLEN_T len
= sizeof(error
);
836 socket
->m_establishing
= FALSE
;
838 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*)&error
, &len
);
842 socket
->m_detected
= GSOCK_LOST_FLAG
;
844 /* LOST event: Abort any further processing */
845 return (GSOCK_LOST_FLAG
& flags
);
849 result
|= GSOCK_CONNECTION_FLAG
;
850 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
855 result
|= GSOCK_OUTPUT_FLAG
;
859 /* Check for exceptions and errors (is this useful in Unices?) */
860 if (FD_ISSET(socket
->m_fd
, &exceptfds
))
862 socket
->m_establishing
= FALSE
;
863 socket
->m_detected
= GSOCK_LOST_FLAG
;
865 /* LOST event: Abort any further processing */
866 return (GSOCK_LOST_FLAG
& flags
);
869 return (result
& flags
);
873 assert(socket
!= NULL
);
874 return flags
& socket
->m_detected
;
876 #endif /* !wxUSE_GUI */
881 /* GSocket_SetNonBlocking:
882 * Sets the socket to non-blocking mode. All IO calls will return
885 void GSocket_SetNonBlocking(GSocket
*socket
, int non_block
)
887 assert(socket
!= NULL
);
889 socket
->m_non_blocking
= non_block
;
892 /* GSocket_SetTimeout:
893 * Sets the timeout for blocking calls. Time is expressed in
896 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millis
)
898 assert(socket
!= NULL
);
900 socket
->m_timeout
.tv_sec
= (millis
/ 1000);
901 socket
->m_timeout
.tv_usec
= (millis
% 1000) * 1000;
905 * Returns the last error occured for this socket. Note that successful
906 * operations do not clear this back to GSOCK_NOERROR, so use it only
909 GSocketError
GSocket_GetError(GSocket
*socket
)
911 assert(socket
!= NULL
);
913 return socket
->m_error
;
919 * There is data to be read in the input buffer. If, after a read
920 * operation, there is still data available, the callback function will
923 * The socket is available for writing. That is, the next write call
924 * won't block. This event is generated only once, when the connection is
925 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
926 * when the output buffer empties again. This means that the app should
927 * assume that it can write since the first OUTPUT event, and no more
928 * OUTPUT events will be generated unless an error occurs.
930 * Connection succesfully established, for client sockets, or incoming
931 * client connection, for server sockets. Wait for this event (also watch
932 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
934 * The connection is lost (or a connection request failed); this could
935 * be due to a failure, or due to the peer closing it gracefully.
938 /* GSocket_SetCallback:
939 * Enables the callbacks specified by 'flags'. Note that 'flags'
940 * may be a combination of flags OR'ed toghether, so the same
941 * callback function can be made to accept different events.
942 * The callback function must have the following prototype:
944 * void function(GSocket *socket, GSocketEvent event, char *cdata)
946 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
947 GSocketCallback callback
, char *cdata
)
951 assert(socket
!= NULL
);
953 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
955 if ((flags
& (1 << count
)) != 0)
957 socket
->m_cbacks
[count
] = callback
;
958 socket
->m_data
[count
] = cdata
;
963 /* GSocket_UnsetCallback:
964 * Disables all callbacks specified by 'flags', which may be a
965 * combination of flags OR'ed toghether.
967 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
971 assert(socket
!= NULL
);
973 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
975 if ((flags
& (1 << count
)) != 0)
977 socket
->m_cbacks
[count
] = NULL
;
978 socket
->m_data
[count
] = NULL
;
985 /* _GSocket_Input_Timeout:
986 * For blocking sockets, wait until data is available or
987 * until timeout ellapses.
989 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
993 if (!socket
->m_non_blocking
)
996 FD_SET(socket
->m_fd
, &readfds
);
997 if (select(0, &readfds
, NULL
, NULL
, &socket
->m_timeout
) == 0)
999 socket
->m_error
= GSOCK_TIMEDOUT
;
1000 return GSOCK_TIMEDOUT
;
1003 return GSOCK_NOERROR
;
1006 /* _GSocket_Output_Timeout:
1007 * For blocking sockets, wait until data can be sent without
1008 * blocking or until timeout ellapses.
1010 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
1014 if (!socket
->m_non_blocking
)
1017 FD_SET(socket
->m_fd
, &writefds
);
1018 if (select(0, NULL
, &writefds
, NULL
, &socket
->m_timeout
) == 0)
1020 socket
->m_error
= GSOCK_TIMEDOUT
;
1021 return GSOCK_TIMEDOUT
;
1024 return GSOCK_NOERROR
;
1027 /* _GSocket_Connect_Timeout:
1028 * For blocking sockets, wait until the connection is
1029 * established or fails, or until timeout ellapses.
1031 GSocketError
_GSocket_Connect_Timeout(GSocket
*socket
)
1037 FD_ZERO(&exceptfds
);
1038 FD_SET(socket
->m_fd
, &writefds
);
1039 FD_SET(socket
->m_fd
, &exceptfds
);
1040 if (select(0, NULL
, &writefds
, &exceptfds
, &socket
->m_timeout
) == 0)
1042 socket
->m_error
= GSOCK_TIMEDOUT
;
1043 return GSOCK_TIMEDOUT
;
1045 if (!FD_ISSET(socket
->m_fd
, &writefds
))
1047 socket
->m_error
= GSOCK_IOERR
;
1051 return GSOCK_NOERROR
;
1054 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
1056 return recv(socket
->m_fd
, buffer
, size
, 0);
1059 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
1061 struct sockaddr from
;
1062 SOCKLEN_T fromlen
= sizeof(from
);
1066 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
1068 if (ret
== SOCKET_ERROR
)
1069 return SOCKET_ERROR
;
1071 /* Translate a system address into a GSocket address */
1072 if (!socket
->m_peer
)
1074 socket
->m_peer
= GAddress_new();
1075 if (!socket
->m_peer
)
1077 socket
->m_error
= GSOCK_MEMERR
;
1081 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
1082 if (err
!= GSOCK_NOERROR
)
1084 GAddress_destroy(socket
->m_peer
);
1085 socket
->m_peer
= NULL
;
1086 socket
->m_error
= err
;
1093 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
1095 return send(socket
->m_fd
, buffer
, size
, 0);
1098 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
1100 struct sockaddr
*addr
;
1104 if (!socket
->m_peer
)
1106 socket
->m_error
= GSOCK_INVADDR
;
1110 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
1111 if (err
!= GSOCK_NOERROR
)
1113 socket
->m_error
= err
;
1117 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
1119 /* Frees memory allocated by _GAddress_translate_to */
1127 * -------------------------------------------------------------------------
1129 * -------------------------------------------------------------------------
1132 /* CHECK_ADDRESS verifies that the current address family is either
1133 * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it
1134 * initalizes it to be a GSOCK_*family*. In other cases, it returns
1135 * an appropiate error code.
1137 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
1139 #define CHECK_ADDRESS(address, family) \
1141 if (address->m_family == GSOCK_NOFAMILY) \
1142 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1143 return address->m_error; \
1144 if (address->m_family != GSOCK_##family) \
1146 address->m_error = GSOCK_INVADDR; \
1147 return GSOCK_INVADDR; \
1151 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
1153 if (address->m_family == GSOCK_NOFAMILY) \
1154 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1156 if (address->m_family != GSOCK_##family) \
1158 address->m_error = GSOCK_INVADDR; \
1164 GAddress
*GAddress_new(void)
1168 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1171 address
->m_family
= GSOCK_NOFAMILY
;
1172 address
->m_addr
= NULL
;
1178 GAddress
*GAddress_copy(GAddress
*address
)
1182 assert(address
!= NULL
);
1184 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1187 memcpy(addr2
, address
, sizeof(GAddress
));
1189 if (address
->m_addr
)
1191 addr2
->m_addr
= (struct sockaddr
*) malloc(addr2
->m_len
);
1192 if (addr2
->m_addr
== NULL
)
1197 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1203 void GAddress_destroy(GAddress
*address
)
1205 assert(address
!= NULL
);
1207 if (address
->m_addr
)
1208 free(address
->m_addr
);
1213 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1215 assert(address
!= NULL
);
1217 address
->m_family
= type
;
1220 GAddressType
GAddress_GetFamily(GAddress
*address
)
1222 assert(address
!= NULL
);
1224 return address
->m_family
;
1227 GSocketError
_GAddress_translate_from(GAddress
*address
,
1228 struct sockaddr
*addr
, int len
)
1230 address
->m_realfamily
= addr
->sa_family
;
1231 switch (addr
->sa_family
)
1234 address
->m_family
= GSOCK_INET
;
1237 address
->m_family
= GSOCK_UNIX
;
1241 address
->m_family
= GSOCK_INET6
;
1246 address
->m_error
= GSOCK_INVOP
;
1251 if (address
->m_addr
)
1252 free(address
->m_addr
);
1254 address
->m_len
= len
;
1255 address
->m_addr
= (struct sockaddr
*) malloc(len
);
1257 if (address
->m_addr
== NULL
)
1259 address
->m_error
= GSOCK_MEMERR
;
1260 return GSOCK_MEMERR
;
1262 memcpy(address
->m_addr
, addr
, len
);
1264 return GSOCK_NOERROR
;
1267 GSocketError
_GAddress_translate_to(GAddress
*address
,
1268 struct sockaddr
**addr
, int *len
)
1270 if (!address
->m_addr
)
1272 address
->m_error
= GSOCK_INVADDR
;
1273 return GSOCK_INVADDR
;
1276 *len
= address
->m_len
;
1277 *addr
= (struct sockaddr
*) malloc(address
->m_len
);
1280 address
->m_error
= GSOCK_MEMERR
;
1281 return GSOCK_MEMERR
;
1284 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1285 return GSOCK_NOERROR
;
1289 * -------------------------------------------------------------------------
1290 * Internet address family
1291 * -------------------------------------------------------------------------
1294 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1296 address
->m_len
= sizeof(struct sockaddr_in
);
1297 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1298 if (address
->m_addr
== NULL
)
1300 address
->m_error
= GSOCK_MEMERR
;
1301 return GSOCK_MEMERR
;
1304 address
->m_family
= GSOCK_INET
;
1305 address
->m_realfamily
= PF_INET
;
1306 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1307 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1309 return GSOCK_NOERROR
;
1312 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1315 struct in_addr
*addr
;
1317 assert(address
!= NULL
);
1319 CHECK_ADDRESS(address
, INET
);
1321 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1323 addr
->s_addr
= inet_addr(hostname
);
1325 /* If it is a numeric host name, convert it now */
1326 if (addr
->s_addr
== INADDR_NONE
)
1328 struct in_addr
*array_addr
;
1330 /* It is a real name, we solve it */
1331 if ((he
= gethostbyname(hostname
)) == NULL
)
1333 /* addr->s_addr = INADDR_NONE just done by inet_addr() above */
1334 address
->m_error
= GSOCK_NOHOST
;
1335 return GSOCK_NOHOST
;
1337 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1338 addr
->s_addr
= array_addr
[0].s_addr
;
1340 return GSOCK_NOERROR
;
1343 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1345 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1348 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1349 unsigned long hostaddr
)
1351 struct in_addr
*addr
;
1353 assert(address
!= NULL
);
1355 CHECK_ADDRESS(address
, INET
);
1357 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1358 addr
->s_addr
= hostaddr
;
1360 return GSOCK_NOERROR
;
1363 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1364 const char *protocol
)
1367 struct sockaddr_in
*addr
;
1369 assert(address
!= NULL
);
1370 CHECK_ADDRESS(address
, INET
);
1374 address
->m_error
= GSOCK_INVPORT
;
1375 return GSOCK_INVPORT
;
1378 se
= getservbyname(port
, protocol
);
1381 if (isdigit(port
[0]))
1385 port_int
= atoi(port
);
1386 addr
= (struct sockaddr_in
*)address
->m_addr
;
1387 addr
->sin_port
= htons((u_short
) port_int
);
1388 return GSOCK_NOERROR
;
1391 address
->m_error
= GSOCK_INVPORT
;
1392 return GSOCK_INVPORT
;
1395 addr
= (struct sockaddr_in
*)address
->m_addr
;
1396 addr
->sin_port
= se
->s_port
;
1398 return GSOCK_NOERROR
;
1401 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1403 struct sockaddr_in
*addr
;
1405 assert(address
!= NULL
);
1406 CHECK_ADDRESS(address
, INET
);
1408 addr
= (struct sockaddr_in
*)address
->m_addr
;
1409 addr
->sin_port
= htons(port
);
1411 return GSOCK_NOERROR
;
1414 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1418 struct sockaddr_in
*addr
;
1420 assert(address
!= NULL
);
1421 CHECK_ADDRESS(address
, INET
);
1423 addr
= (struct sockaddr_in
*)address
->m_addr
;
1424 addr_buf
= (char *)&(addr
->sin_addr
);
1426 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1429 address
->m_error
= GSOCK_NOHOST
;
1430 return GSOCK_NOHOST
;
1433 strncpy(hostname
, he
->h_name
, sbuf
);
1435 return GSOCK_NOERROR
;
1438 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1440 struct sockaddr_in
*addr
;
1442 assert(address
!= NULL
);
1443 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1445 addr
= (struct sockaddr_in
*)address
->m_addr
;
1447 return addr
->sin_addr
.s_addr
;
1450 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1452 struct sockaddr_in
*addr
;
1454 assert(address
!= NULL
);
1455 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1457 addr
= (struct sockaddr_in
*)address
->m_addr
;
1458 return ntohs(addr
->sin_port
);
1462 * -------------------------------------------------------------------------
1463 * Unix address family
1464 * -------------------------------------------------------------------------
1467 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1469 assert (address
!= NULL
);
1470 address
->m_error
= GSOCK_INVADDR
;
1471 return GSOCK_INVADDR
;
1474 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1476 assert (address
!= NULL
);
1477 address
->m_error
= GSOCK_INVADDR
;
1478 return GSOCK_INVADDR
;
1481 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
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__) */