]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/gsocket.c
9ea70c328efa5d35d33b16bcdea87ecbeb158318
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 */
62 /* if we use configure for MSW SOCKLEN_T will be already defined */
64 # define SOCKLEN_T int
68 /* Constructors / Destructors for GSocket */
70 GSocket
*GSocket_new(void)
75 if ((socket
= (GSocket
*) malloc(sizeof(GSocket
))) == NULL
)
78 socket
->m_fd
= INVALID_SOCKET
;
79 for (i
= 0; i
< GSOCK_MAX_EVENT
; i
++)
81 socket
->m_cbacks
[i
] = NULL
;
83 socket
->m_detected
= 0;
84 socket
->m_local
= NULL
;
85 socket
->m_peer
= NULL
;
86 socket
->m_error
= GSOCK_NOERROR
;
87 socket
->m_server
= FALSE
;
88 socket
->m_stream
= TRUE
;
89 socket
->m_non_blocking
= FALSE
;
90 socket
->m_timeout
.tv_sec
= 10 * 60; /* 10 minutes */
91 socket
->m_timeout
.tv_usec
= 0;
92 socket
->m_establishing
= FALSE
;
94 /* Per-socket GUI-specific initialization */
95 success
= _GSocket_GUI_Init(socket
);
105 void GSocket_close(GSocket
*socket
)
107 _GSocket_Disable_Events(socket
);
108 closesocket(socket
->m_fd
);
109 socket
->m_fd
= INVALID_SOCKET
;
112 void GSocket_destroy(GSocket
*socket
)
114 assert(socket
!= NULL
);
116 /* Per-socket GUI-specific cleanup */
117 _GSocket_GUI_Destroy(socket
);
119 /* Check that the socket is really shutdowned */
120 if (socket
->m_fd
!= INVALID_SOCKET
)
121 GSocket_Shutdown(socket
);
123 /* Destroy private addresses */
125 GAddress_destroy(socket
->m_local
);
128 GAddress_destroy(socket
->m_peer
);
130 /* Destroy the socket itself */
135 * Disallow further read/write operations on this socket, close
136 * the fd and disable all callbacks.
138 void GSocket_Shutdown(GSocket
*socket
)
142 assert(socket
!= NULL
);
144 /* If socket has been created, shutdown it */
145 if (socket
->m_fd
!= INVALID_SOCKET
)
147 shutdown(socket
->m_fd
, 2);
148 GSocket_close(socket
);
151 /* Disable GUI callbacks */
152 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
153 socket
->m_cbacks
[evt
] = NULL
;
155 socket
->m_detected
= GSOCK_LOST_FLAG
;
158 /* Address handling */
164 * Set or get the local or peer address for this socket. The 'set'
165 * functions return GSOCK_NOERROR on success, an error code otherwise.
166 * The 'get' functions return a pointer to a GAddress object on success,
167 * or NULL otherwise, in which case they set the error code of the
168 * corresponding GSocket.
171 * GSOCK_INVSOCK - the socket is not valid.
172 * GSOCK_INVADDR - the address is not valid.
174 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
176 assert(socket
!= NULL
);
178 /* the socket must be initialized, or it must be a server */
179 if (socket
->m_fd
!= INVALID_SOCKET
&& !socket
->m_server
)
181 socket
->m_error
= GSOCK_INVSOCK
;
182 return GSOCK_INVSOCK
;
186 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
188 socket
->m_error
= GSOCK_INVADDR
;
189 return GSOCK_INVADDR
;
193 GAddress_destroy(socket
->m_local
);
195 socket
->m_local
= GAddress_copy(address
);
197 return GSOCK_NOERROR
;
200 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
202 assert(socket
!= NULL
);
205 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
207 socket
->m_error
= GSOCK_INVADDR
;
208 return GSOCK_INVADDR
;
212 GAddress_destroy(socket
->m_peer
);
214 socket
->m_peer
= GAddress_copy(address
);
216 return GSOCK_NOERROR
;
219 GAddress
*GSocket_GetLocal(GSocket
*socket
)
222 struct sockaddr addr
;
223 SOCKLEN_T size
= sizeof(addr
);
226 assert(socket
!= NULL
);
228 /* try to get it from the m_local var first */
230 return GAddress_copy(socket
->m_local
);
232 /* else, if the socket is initialized, try getsockname */
233 if (socket
->m_fd
== INVALID_SOCKET
)
235 socket
->m_error
= GSOCK_INVSOCK
;
239 if (getsockname(socket
->m_fd
, &addr
, &size
) == SOCKET_ERROR
)
241 socket
->m_error
= GSOCK_IOERR
;
245 /* got a valid address from getsockname, create a GAddress object */
246 if ((address
= GAddress_new()) == NULL
)
248 socket
->m_error
= GSOCK_MEMERR
;
252 if ((err
= _GAddress_translate_from(address
, &addr
, size
)) != GSOCK_NOERROR
)
254 GAddress_destroy(address
);
255 socket
->m_error
= err
;
262 GAddress
*GSocket_GetPeer(GSocket
*socket
)
264 assert(socket
!= NULL
);
266 /* try to get it from the m_peer var */
268 return GAddress_copy(socket
->m_peer
);
273 /* Server specific parts */
275 /* GSocket_SetServer:
276 * Sets up this socket as a server. The local address must have been
277 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
278 * Returns GSOCK_NOERROR on success, one of the following otherwise:
281 * GSOCK_INVSOCK - the socket is in use.
282 * GSOCK_INVADDR - the local address has not been set.
283 * GSOCK_IOERR - low-level error.
285 GSocketError
GSocket_SetServer(GSocket
*sck
)
291 /* must not be in use */
292 if (sck
->m_fd
!= INVALID_SOCKET
)
294 sck
->m_error
= GSOCK_INVSOCK
;
295 return GSOCK_INVSOCK
;
298 /* the local addr must have been set */
301 sck
->m_error
= GSOCK_INVADDR
;
302 return GSOCK_INVADDR
;
305 /* Initialize all fields */
306 sck
->m_server
= TRUE
;
307 sck
->m_stream
= TRUE
;
308 sck
->m_oriented
= TRUE
;
310 /* Create the socket */
311 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
313 if (sck
->m_fd
== INVALID_SOCKET
)
315 sck
->m_error
= GSOCK_IOERR
;
319 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
320 _GSocket_Enable_Events(sck
);
322 /* Bind to the local address,
323 * retrieve the actual address bound,
324 * and listen up to 5 connections.
326 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
327 (getsockname(sck
->m_fd
,
328 sck
->m_local
->m_addr
,
329 (SOCKLEN_T
*)&sck
->m_local
->m_len
) != 0) ||
330 (listen(sck
->m_fd
, 5) != 0))
333 sck
->m_error
= GSOCK_IOERR
;
337 return GSOCK_NOERROR
;
340 /* GSocket_WaitConnection:
341 * Waits for an incoming client connection. Returns a pointer to
342 * a GSocket object, or NULL if there was an error, in which case
343 * the last error field will be updated for the calling GSocket.
345 * Error codes (set in the calling GSocket)
346 * GSOCK_INVSOCK - the socket is not valid or not a server.
347 * GSOCK_TIMEDOUT - timeout, no incoming connections.
348 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
349 * GSOCK_MEMERR - couldn't allocate memory.
350 * GSOCK_IOERR - low-level error.
352 GSocket
*GSocket_WaitConnection(GSocket
*sck
)
355 struct sockaddr from
;
356 SOCKLEN_T fromlen
= sizeof(from
);
362 /* Reenable CONNECTION events */
363 sck
->m_detected
&= ~GSOCK_CONNECTION_FLAG
;
365 /* If the socket has already been created, we exit immediately */
366 if (sck
->m_fd
== INVALID_SOCKET
|| !sck
->m_server
)
368 sck
->m_error
= GSOCK_INVSOCK
;
372 /* Create a GSocket object for the new connection */
373 connection
= GSocket_new();
377 sck
->m_error
= GSOCK_MEMERR
;
381 /* Wait for a connection (with timeout) */
382 if (_GSocket_Input_Timeout(sck
) == GSOCK_TIMEDOUT
)
384 GSocket_destroy(connection
);
385 /* sck->m_error set by _GSocket_Input_Timeout */
389 connection
->m_fd
= accept(sck
->m_fd
, &from
, &fromlen
);
391 if (connection
->m_fd
== INVALID_SOCKET
)
393 if (WSAGetLastError() == WSAEWOULDBLOCK
)
394 sck
->m_error
= GSOCK_WOULDBLOCK
;
396 sck
->m_error
= GSOCK_IOERR
;
398 GSocket_destroy(connection
);
402 /* Initialize all fields */
403 connection
->m_server
= FALSE
;
404 connection
->m_stream
= TRUE
;
405 connection
->m_oriented
= TRUE
;
407 /* Setup the peer address field */
408 connection
->m_peer
= GAddress_new();
409 if (!connection
->m_peer
)
411 GSocket_destroy(connection
);
412 sck
->m_error
= GSOCK_MEMERR
;
415 err
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
);
416 if (err
!= GSOCK_NOERROR
)
418 GAddress_destroy(connection
->m_peer
);
419 GSocket_destroy(connection
);
424 ioctlsocket(connection
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
425 _GSocket_Enable_Events(connection
);
430 /* Client specific parts */
433 * For stream (connection oriented) sockets, GSocket_Connect() tries
434 * to establish a client connection to a server using the peer address
435 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
436 * connection has been succesfully established, or one of the error
437 * codes listed below. Note that for nonblocking sockets, a return
438 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
439 * request can be completed later; you should use GSocket_Select()
440 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
441 * corresponding asynchronous events.
443 * For datagram (non connection oriented) sockets, GSocket_Connect()
444 * just sets the peer address established with GSocket_SetPeer() as
445 * default destination.
448 * GSOCK_INVSOCK - the socket is in use or not valid.
449 * GSOCK_INVADDR - the peer address has not been established.
450 * GSOCK_TIMEDOUT - timeout, the connection failed.
451 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
452 * GSOCK_MEMERR - couldn't allocate memory.
453 * GSOCK_IOERR - low-level error.
455 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
462 /* Enable CONNECTION events (needed for nonblocking connections) */
463 sck
->m_detected
&= ~GSOCK_CONNECTION_FLAG
;
465 if (sck
->m_fd
!= INVALID_SOCKET
)
467 sck
->m_error
= GSOCK_INVSOCK
;
468 return GSOCK_INVSOCK
;
473 sck
->m_error
= GSOCK_INVADDR
;
474 return GSOCK_INVADDR
;
477 /* Streamed or dgram socket? */
478 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
479 sck
->m_oriented
= TRUE
;
480 sck
->m_server
= FALSE
;
481 sck
->m_establishing
= FALSE
;
483 /* Create the socket */
484 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
,
485 sck
->m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
487 if (sck
->m_fd
== INVALID_SOCKET
)
489 sck
->m_error
= GSOCK_IOERR
;
493 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
494 _GSocket_Enable_Events(sck
);
496 /* Connect it to the peer address, with a timeout (see below) */
497 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
499 if (ret
== SOCKET_ERROR
)
501 err
= WSAGetLastError();
503 /* If connect failed with EWOULDBLOCK and the GSocket object
504 * is in blocking mode, we select() for the specified timeout
505 * checking for writability to see if the connection request
508 if ((err
== WSAEWOULDBLOCK
) && (!sck
->m_non_blocking
))
510 err
= _GSocket_Connect_Timeout(sck
);
512 if (err
!= GSOCK_NOERROR
)
515 /* sck->m_error is set in _GSocket_Connect_Timeout */
518 return (GSocketError
) err
;
521 /* If connect failed with EWOULDBLOCK and the GSocket object
522 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
523 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
524 * this way if the connection completes, a GSOCK_CONNECTION
525 * event will be generated, if enabled.
527 if ((err
== WSAEWOULDBLOCK
) && (sck
->m_non_blocking
))
529 sck
->m_establishing
= TRUE
;
530 sck
->m_error
= GSOCK_WOULDBLOCK
;
531 return GSOCK_WOULDBLOCK
;
534 /* If connect failed with an error other than EWOULDBLOCK,
535 * then the call to GSocket_Connect() has failed.
538 sck
->m_error
= GSOCK_IOERR
;
542 return GSOCK_NOERROR
;
545 /* Datagram sockets */
547 /* GSocket_SetNonOriented:
548 * Sets up this socket as a non-connection oriented (datagram) socket.
549 * Before using this function, the local address must have been set
550 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
551 * on success, or one of the following otherwise.
554 * GSOCK_INVSOCK - the socket is in use.
555 * GSOCK_INVADDR - the local address has not been set.
556 * GSOCK_IOERR - low-level error.
558 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
564 if (sck
->m_fd
!= INVALID_SOCKET
)
566 sck
->m_error
= GSOCK_INVSOCK
;
567 return GSOCK_INVSOCK
;
572 sck
->m_error
= GSOCK_INVADDR
;
573 return GSOCK_INVADDR
;
576 /* Initialize all fields */
577 sck
->m_stream
= FALSE
;
578 sck
->m_server
= FALSE
;
579 sck
->m_oriented
= FALSE
;
581 /* Create the socket */
582 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
584 if (sck
->m_fd
== INVALID_SOCKET
)
586 sck
->m_error
= GSOCK_IOERR
;
590 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
591 _GSocket_Enable_Events(sck
);
593 /* Bind to the local address,
594 * and retrieve the actual address bound.
596 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
597 (getsockname(sck
->m_fd
,
598 sck
->m_local
->m_addr
,
599 (SOCKLEN_T
*)&sck
->m_local
->m_len
) != 0))
602 sck
->m_error
= GSOCK_IOERR
;
606 return GSOCK_NOERROR
;
611 /* Like recv(), send(), ... */
612 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
616 assert(socket
!= NULL
);
618 /* Reenable INPUT events */
619 socket
->m_detected
&= ~GSOCK_INPUT_FLAG
;
621 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
623 socket
->m_error
= GSOCK_INVSOCK
;
627 /* If the socket is blocking, wait for data (with a timeout) */
628 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
632 if (socket
->m_stream
)
633 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
635 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
637 if (ret
== SOCKET_ERROR
)
639 if (WSAGetLastError() != WSAEWOULDBLOCK
)
640 socket
->m_error
= GSOCK_IOERR
;
642 socket
->m_error
= GSOCK_WOULDBLOCK
;
649 int GSocket_Write(GSocket
*socket
, const char *buffer
, int size
)
653 assert(socket
!= NULL
);
655 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
657 socket
->m_error
= GSOCK_INVSOCK
;
661 /* If the socket is blocking, wait for writability (with a timeout) */
662 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
666 if (socket
->m_stream
)
667 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
669 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
671 if (ret
== SOCKET_ERROR
)
673 if (WSAGetLastError() != WSAEWOULDBLOCK
)
674 socket
->m_error
= GSOCK_IOERR
;
676 socket
->m_error
= GSOCK_WOULDBLOCK
;
678 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
679 * does). Once the first OUTPUT event is received, users can assume
680 * that the socket is writable until a read operation fails. Only then
681 * will further OUTPUT events be posted.
683 socket
->m_detected
&= ~GSOCK_OUTPUT_FLAG
;
691 * Polls the socket to determine its status. This function will
692 * check for the events specified in the 'flags' parameter, and
693 * it will return a mask indicating which operations can be
694 * performed. This function won't block, regardless of the
695 * mode (blocking | nonblocking) of the socket.
697 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
699 #if defined(wxUSE_GUI) && !wxUSE_GUI
701 GSocketEventFlags result
= 0;
705 static const struct timeval tv
= { 0, 0 };
707 assert(socket
!= NULL
);
712 FD_SET(socket
->m_fd
, &readfds
);
713 FD_SET(socket
->m_fd
, &writefds
);
714 FD_SET(socket
->m_fd
, &exceptfds
);
716 /* Check 'sticky' CONNECTION flag first */
717 result
|= (GSOCK_CONNECTION_FLAG
& socket
->m_detected
);
719 /* If we have already detected a LOST event, then don't try
720 * to do any further processing.
722 if ((socket
->m_detected
& GSOCK_LOST_FLAG
) != 0)
724 socket
->m_establishing
= FALSE
;
726 return (GSOCK_LOST_FLAG
& flags
);
730 if (select(socket
->m_fd
+ 1, &readfds
, &writefds
, &exceptfds
, &tv
) <= 0)
732 /* What to do here? */
733 return (result
& flags
);
736 /* Check for readability */
737 if (FD_ISSET(socket
->m_fd
, &readfds
))
741 if (recv(socket
->m_fd
, &c
, 1, MSG_PEEK
) > 0)
743 result
|= GSOCK_INPUT_FLAG
;
747 if (socket
->m_server
&& socket
->m_stream
)
749 result
|= GSOCK_CONNECTION_FLAG
;
750 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
754 socket
->m_detected
= GSOCK_LOST_FLAG
;
755 socket
->m_establishing
= FALSE
;
757 /* LOST event: Abort any further processing */
758 return (GSOCK_LOST_FLAG
& flags
);
763 /* Check for writability */
764 if (FD_ISSET(socket
->m_fd
, &writefds
))
766 if (socket
->m_establishing
&& !socket
->m_server
)
769 SOCKLEN_T len
= sizeof(error
);
771 socket
->m_establishing
= FALSE
;
773 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*)&error
, &len
);
777 socket
->m_detected
= GSOCK_LOST_FLAG
;
779 /* LOST event: Abort any further processing */
780 return (GSOCK_LOST_FLAG
& flags
);
784 result
|= GSOCK_CONNECTION_FLAG
;
785 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
790 result
|= GSOCK_OUTPUT_FLAG
;
794 /* Check for exceptions and errors (is this useful in Unices?) */
795 if (FD_ISSET(socket
->m_fd
, &exceptfds
))
797 socket
->m_establishing
= FALSE
;
798 socket
->m_detected
= GSOCK_LOST_FLAG
;
800 /* LOST event: Abort any further processing */
801 return (GSOCK_LOST_FLAG
& flags
);
804 return (result
& flags
);
808 assert(socket
!= NULL
);
809 return flags
& socket
->m_detected
;
811 #endif /* !wxUSE_GUI */
816 /* GSocket_SetNonBlocking:
817 * Sets the socket to non-blocking mode. All IO calls will return
820 void GSocket_SetNonBlocking(GSocket
*socket
, int non_block
)
822 assert(socket
!= NULL
);
824 socket
->m_non_blocking
= non_block
;
827 /* GSocket_SetTimeout:
828 * Sets the timeout for blocking calls. Time is expressed in
831 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millis
)
833 assert(socket
!= NULL
);
835 socket
->m_timeout
.tv_sec
= (millis
/ 1000);
836 socket
->m_timeout
.tv_usec
= (millis
% 1000) * 1000;
840 * Returns the last error occured for this socket. Note that successful
841 * operations do not clear this back to GSOCK_NOERROR, so use it only
844 GSocketError
GSocket_GetError(GSocket
*socket
)
846 assert(socket
!= NULL
);
848 return socket
->m_error
;
854 * There is data to be read in the input buffer. If, after a read
855 * operation, there is still data available, the callback function will
858 * The socket is available for writing. That is, the next write call
859 * won't block. This event is generated only once, when the connection is
860 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
861 * when the output buffer empties again. This means that the app should
862 * assume that it can write since the first OUTPUT event, and no more
863 * OUTPUT events will be generated unless an error occurs.
865 * Connection succesfully established, for client sockets, or incoming
866 * client connection, for server sockets. Wait for this event (also watch
867 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
869 * The connection is lost (or a connection request failed); this could
870 * be due to a failure, or due to the peer closing it gracefully.
873 /* GSocket_SetCallback:
874 * Enables the callbacks specified by 'flags'. Note that 'flags'
875 * may be a combination of flags OR'ed toghether, so the same
876 * callback function can be made to accept different events.
877 * The callback function must have the following prototype:
879 * void function(GSocket *socket, GSocketEvent event, char *cdata)
881 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
882 GSocketCallback callback
, char *cdata
)
886 assert(socket
!= NULL
);
888 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
890 if ((flags
& (1 << count
)) != 0)
892 socket
->m_cbacks
[count
] = callback
;
893 socket
->m_data
[count
] = cdata
;
898 /* GSocket_UnsetCallback:
899 * Disables all callbacks specified by 'flags', which may be a
900 * combination of flags OR'ed toghether.
902 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
906 assert(socket
!= NULL
);
908 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
910 if ((flags
& (1 << count
)) != 0)
912 socket
->m_cbacks
[count
] = NULL
;
913 socket
->m_data
[count
] = NULL
;
920 /* _GSocket_Input_Timeout:
921 * For blocking sockets, wait until data is available or
922 * until timeout ellapses.
924 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
928 if (!socket
->m_non_blocking
)
931 FD_SET(socket
->m_fd
, &readfds
);
932 if (select(0, &readfds
, NULL
, NULL
, &socket
->m_timeout
) == 0)
934 socket
->m_error
= GSOCK_TIMEDOUT
;
935 return GSOCK_TIMEDOUT
;
938 return GSOCK_NOERROR
;
941 /* _GSocket_Output_Timeout:
942 * For blocking sockets, wait until data can be sent without
943 * blocking or until timeout ellapses.
945 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
949 if (!socket
->m_non_blocking
)
952 FD_SET(socket
->m_fd
, &writefds
);
953 if (select(0, NULL
, &writefds
, NULL
, &socket
->m_timeout
) == 0)
955 socket
->m_error
= GSOCK_TIMEDOUT
;
956 return GSOCK_TIMEDOUT
;
959 return GSOCK_NOERROR
;
962 /* _GSocket_Connect_Timeout:
963 * For blocking sockets, wait until the connection is
964 * established or fails, or until timeout ellapses.
966 GSocketError
_GSocket_Connect_Timeout(GSocket
*socket
)
973 FD_SET(socket
->m_fd
, &writefds
);
974 FD_SET(socket
->m_fd
, &exceptfds
);
975 if (select(0, NULL
, &writefds
, &exceptfds
, &socket
->m_timeout
) == 0)
977 socket
->m_error
= GSOCK_TIMEDOUT
;
978 return GSOCK_TIMEDOUT
;
980 if (!FD_ISSET(socket
->m_fd
, &writefds
))
982 socket
->m_error
= GSOCK_IOERR
;
986 return GSOCK_NOERROR
;
989 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
991 return recv(socket
->m_fd
, buffer
, size
, 0);
994 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
996 struct sockaddr from
;
997 SOCKLEN_T fromlen
= sizeof(from
);
1001 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
1003 if (ret
== SOCKET_ERROR
)
1004 return SOCKET_ERROR
;
1006 /* Translate a system address into a GSocket address */
1007 if (!socket
->m_peer
)
1009 socket
->m_peer
= GAddress_new();
1010 if (!socket
->m_peer
)
1012 socket
->m_error
= GSOCK_MEMERR
;
1016 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
1017 if (err
!= GSOCK_NOERROR
)
1019 GAddress_destroy(socket
->m_peer
);
1020 socket
->m_peer
= NULL
;
1021 socket
->m_error
= err
;
1028 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
1030 return send(socket
->m_fd
, buffer
, size
, 0);
1033 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
1035 struct sockaddr
*addr
;
1039 if (!socket
->m_peer
)
1041 socket
->m_error
= GSOCK_INVADDR
;
1045 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
1046 if (err
!= GSOCK_NOERROR
)
1048 socket
->m_error
= err
;
1052 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
1054 /* Frees memory allocated by _GAddress_translate_to */
1062 * -------------------------------------------------------------------------
1064 * -------------------------------------------------------------------------
1067 /* CHECK_ADDRESS verifies that the current address family is either
1068 * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it
1069 * initalizes it to be a GSOCK_*family*. In other cases, it returns
1070 * an appropiate error code.
1072 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
1074 #define CHECK_ADDRESS(address, family) \
1076 if (address->m_family == GSOCK_NOFAMILY) \
1077 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1078 return address->m_error; \
1079 if (address->m_family != GSOCK_##family) \
1081 address->m_error = GSOCK_INVADDR; \
1082 return GSOCK_INVADDR; \
1086 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
1088 if (address->m_family == GSOCK_NOFAMILY) \
1089 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1091 if (address->m_family != GSOCK_##family) \
1093 address->m_error = GSOCK_INVADDR; \
1099 GAddress
*GAddress_new(void)
1103 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1106 address
->m_family
= GSOCK_NOFAMILY
;
1107 address
->m_addr
= NULL
;
1113 GAddress
*GAddress_copy(GAddress
*address
)
1117 assert(address
!= NULL
);
1119 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1122 memcpy(addr2
, address
, sizeof(GAddress
));
1124 if (address
->m_addr
)
1126 addr2
->m_addr
= (struct sockaddr
*) malloc(addr2
->m_len
);
1127 if (addr2
->m_addr
== NULL
)
1132 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1138 void GAddress_destroy(GAddress
*address
)
1140 assert(address
!= NULL
);
1142 if (address
->m_addr
)
1143 free(address
->m_addr
);
1148 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1150 assert(address
!= NULL
);
1152 address
->m_family
= type
;
1155 GAddressType
GAddress_GetFamily(GAddress
*address
)
1157 assert(address
!= NULL
);
1159 return address
->m_family
;
1162 GSocketError
_GAddress_translate_from(GAddress
*address
,
1163 struct sockaddr
*addr
, int len
)
1165 address
->m_realfamily
= addr
->sa_family
;
1166 switch (addr
->sa_family
)
1169 address
->m_family
= GSOCK_INET
;
1172 address
->m_family
= GSOCK_UNIX
;
1176 address
->m_family
= GSOCK_INET6
;
1181 address
->m_error
= GSOCK_INVOP
;
1186 if (address
->m_addr
)
1187 free(address
->m_addr
);
1189 address
->m_len
= len
;
1190 address
->m_addr
= (struct sockaddr
*) malloc(len
);
1192 if (address
->m_addr
== NULL
)
1194 address
->m_error
= GSOCK_MEMERR
;
1195 return GSOCK_MEMERR
;
1197 memcpy(address
->m_addr
, addr
, len
);
1199 return GSOCK_NOERROR
;
1202 GSocketError
_GAddress_translate_to(GAddress
*address
,
1203 struct sockaddr
**addr
, int *len
)
1205 if (!address
->m_addr
)
1207 address
->m_error
= GSOCK_INVADDR
;
1208 return GSOCK_INVADDR
;
1211 *len
= address
->m_len
;
1212 *addr
= (struct sockaddr
*) malloc(address
->m_len
);
1215 address
->m_error
= GSOCK_MEMERR
;
1216 return GSOCK_MEMERR
;
1219 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1220 return GSOCK_NOERROR
;
1224 * -------------------------------------------------------------------------
1225 * Internet address family
1226 * -------------------------------------------------------------------------
1229 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1231 address
->m_len
= sizeof(struct sockaddr_in
);
1232 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1233 if (address
->m_addr
== NULL
)
1235 address
->m_error
= GSOCK_MEMERR
;
1236 return GSOCK_MEMERR
;
1239 address
->m_family
= GSOCK_INET
;
1240 address
->m_realfamily
= PF_INET
;
1241 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1242 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1244 return GSOCK_NOERROR
;
1247 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1250 struct in_addr
*addr
;
1252 assert(address
!= NULL
);
1254 CHECK_ADDRESS(address
, INET
);
1256 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1258 addr
->s_addr
= inet_addr(hostname
);
1260 /* If it is a numeric host name, convert it now */
1261 if (addr
->s_addr
== INADDR_NONE
)
1263 struct in_addr
*array_addr
;
1265 /* It is a real name, we solve it */
1266 if ((he
= gethostbyname(hostname
)) == NULL
)
1268 /* addr->s_addr = INADDR_NONE just done by inet_addr() above */
1269 address
->m_error
= GSOCK_NOHOST
;
1270 return GSOCK_NOHOST
;
1272 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1273 addr
->s_addr
= array_addr
[0].s_addr
;
1275 return GSOCK_NOERROR
;
1278 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1280 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1283 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1284 unsigned long hostaddr
)
1286 struct in_addr
*addr
;
1288 assert(address
!= NULL
);
1290 CHECK_ADDRESS(address
, INET
);
1292 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1293 addr
->s_addr
= hostaddr
;
1295 return GSOCK_NOERROR
;
1298 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1299 const char *protocol
)
1302 struct sockaddr_in
*addr
;
1304 assert(address
!= NULL
);
1305 CHECK_ADDRESS(address
, INET
);
1309 address
->m_error
= GSOCK_INVPORT
;
1310 return GSOCK_INVPORT
;
1313 se
= getservbyname(port
, protocol
);
1316 if (isdigit(port
[0]))
1320 port_int
= atoi(port
);
1321 addr
= (struct sockaddr_in
*)address
->m_addr
;
1322 addr
->sin_port
= htons((u_short
) port_int
);
1323 return GSOCK_NOERROR
;
1326 address
->m_error
= GSOCK_INVPORT
;
1327 return GSOCK_INVPORT
;
1330 addr
= (struct sockaddr_in
*)address
->m_addr
;
1331 addr
->sin_port
= se
->s_port
;
1333 return GSOCK_NOERROR
;
1336 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1338 struct sockaddr_in
*addr
;
1340 assert(address
!= NULL
);
1341 CHECK_ADDRESS(address
, INET
);
1343 addr
= (struct sockaddr_in
*)address
->m_addr
;
1344 addr
->sin_port
= htons(port
);
1346 return GSOCK_NOERROR
;
1349 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1353 struct sockaddr_in
*addr
;
1355 assert(address
!= NULL
);
1356 CHECK_ADDRESS(address
, INET
);
1358 addr
= (struct sockaddr_in
*)address
->m_addr
;
1359 addr_buf
= (char *)&(addr
->sin_addr
);
1361 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1364 address
->m_error
= GSOCK_NOHOST
;
1365 return GSOCK_NOHOST
;
1368 strncpy(hostname
, he
->h_name
, sbuf
);
1370 return GSOCK_NOERROR
;
1373 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1375 struct sockaddr_in
*addr
;
1377 assert(address
!= NULL
);
1378 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1380 addr
= (struct sockaddr_in
*)address
->m_addr
;
1382 return addr
->sin_addr
.s_addr
;
1385 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1387 struct sockaddr_in
*addr
;
1389 assert(address
!= NULL
);
1390 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1392 addr
= (struct sockaddr_in
*)address
->m_addr
;
1393 return ntohs(addr
->sin_port
);
1397 * -------------------------------------------------------------------------
1398 * Unix address family
1399 * -------------------------------------------------------------------------
1402 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1404 assert (address
!= NULL
);
1405 address
->m_error
= GSOCK_INVADDR
;
1406 return GSOCK_INVADDR
;
1409 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1411 assert (address
!= NULL
);
1412 address
->m_error
= GSOCK_INVADDR
;
1413 return GSOCK_INVADDR
;
1416 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1418 assert (address
!= NULL
);
1419 address
->m_error
= GSOCK_INVADDR
;
1420 return GSOCK_INVADDR
;
1423 #else /* !wxUSE_SOCKETS */
1426 * Translation unit shouldn't be empty, so include this typedef to make the
1427 * compiler (VC++ 6.0, for example) happy
1429 typedef void (*wxDummy
)();
1431 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */