]>
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
7 * -------------------------------------------------------------------------
11 * PLEASE don't put C++ comments here - this is a C source file.
15 /* RPCNOTIFICATION_ROUTINE in rasasync.h (included from winsock.h),
16 * warning: conditional expression is constant.
18 # pragma warning(disable:4115)
20 * warning: named type definition in parentheses.
22 # pragma warning(disable:4127)
23 /* GAddress_UNIX_GetPath,
24 * warning: unreferenced formal parameter.
26 # pragma warning(disable:4100)
30 #ifndef __GSOCKET_STANDALONE__
32 # include "wx/setup.h"
35 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
37 #ifndef __GSOCKET_STANDALONE__
38 # include "wx/msw/gsockmsw.h"
39 # include "wx/gsocket.h"
41 # include "gsockmsw.h"
43 #endif /* __GSOCKET_STANDALONE__ */
45 /* Redefine some GUI-only functions to do nothing in console mode */
46 #if defined(wxUSE_GUI) && !wxUSE_GUI
47 # define _GSocket_GUI_Init(socket) (1)
48 # define _GSocket_GUI_Destroy(socket)
49 # define _GSocket_Enable_Events(socket)
50 # define _GSocket_Disable_Events(socket)
51 #endif /* wxUSE_GUI */
63 /* if we use configure for MSW SOCKLEN_T will be already defined */
65 # define SOCKLEN_T int
69 /* Constructors / Destructors for GSocket */
71 GSocket
*GSocket_new(void)
76 if ((socket
= (GSocket
*) malloc(sizeof(GSocket
))) == NULL
)
79 socket
->m_fd
= INVALID_SOCKET
;
80 for (i
= 0; i
< GSOCK_MAX_EVENT
; i
++)
82 socket
->m_cbacks
[i
] = NULL
;
84 socket
->m_detected
= 0;
85 socket
->m_local
= NULL
;
86 socket
->m_peer
= NULL
;
87 socket
->m_error
= GSOCK_NOERROR
;
88 socket
->m_server
= FALSE
;
89 socket
->m_stream
= TRUE
;
90 socket
->m_non_blocking
= FALSE
;
91 socket
->m_timeout
.tv_sec
= 10 * 60; /* 10 minutes */
92 socket
->m_timeout
.tv_usec
= 0;
93 socket
->m_establishing
= FALSE
;
95 /* Per-socket GUI-specific initialization */
96 success
= _GSocket_GUI_Init(socket
);
106 void GSocket_destroy(GSocket
*socket
)
108 assert(socket
!= NULL
);
110 /* Per-socket GUI-specific cleanup */
111 _GSocket_GUI_Destroy(socket
);
113 /* Check that the socket is really shutdowned */
114 if (socket
->m_fd
!= INVALID_SOCKET
)
115 GSocket_Shutdown(socket
);
117 /* Destroy private addresses */
119 GAddress_destroy(socket
->m_local
);
122 GAddress_destroy(socket
->m_peer
);
124 /* Destroy the socket itself */
129 * Disallow further read/write operations on this socket, close
130 * the fd and disable all callbacks.
132 void GSocket_Shutdown(GSocket
*socket
)
136 assert(socket
!= NULL
);
138 /* If socket has been created, shutdown it */
139 if (socket
->m_fd
!= INVALID_SOCKET
)
141 shutdown(socket
->m_fd
, 2);
142 closesocket(socket
->m_fd
);
143 socket
->m_fd
= INVALID_SOCKET
;
146 /* Disable GUI callbacks */
147 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
148 socket
->m_cbacks
[evt
] = NULL
;
150 socket
->m_detected
= GSOCK_LOST_FLAG
;
151 _GSocket_Disable_Events(socket
);
154 /* Address handling */
160 * Set or get the local or peer address for this socket. The 'set'
161 * functions return GSOCK_NOERROR on success, an error code otherwise.
162 * The 'get' functions return a pointer to a GAddress object on success,
163 * or NULL otherwise, in which case they set the error code of the
164 * corresponding GSocket.
167 * GSOCK_INVSOCK - the socket is not valid.
168 * GSOCK_INVADDR - the address is not valid.
170 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
172 assert(socket
!= NULL
);
174 /* the socket must be initialized, or it must be a server */
175 if (socket
->m_fd
!= INVALID_SOCKET
&& !socket
->m_server
)
177 socket
->m_error
= GSOCK_INVSOCK
;
178 return GSOCK_INVSOCK
;
182 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
184 socket
->m_error
= GSOCK_INVADDR
;
185 return GSOCK_INVADDR
;
189 GAddress_destroy(socket
->m_local
);
191 socket
->m_local
= GAddress_copy(address
);
193 return GSOCK_NOERROR
;
196 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
198 assert(socket
!= NULL
);
201 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
203 socket
->m_error
= GSOCK_INVADDR
;
204 return GSOCK_INVADDR
;
208 GAddress_destroy(socket
->m_peer
);
210 socket
->m_peer
= GAddress_copy(address
);
212 return GSOCK_NOERROR
;
215 GAddress
*GSocket_GetLocal(GSocket
*socket
)
218 struct sockaddr addr
;
219 SOCKLEN_T size
= sizeof(addr
);
222 assert(socket
!= NULL
);
224 /* try to get it from the m_local var first */
226 return GAddress_copy(socket
->m_local
);
228 /* else, if the socket is initialized, try getsockname */
229 if (socket
->m_fd
== INVALID_SOCKET
)
231 socket
->m_error
= GSOCK_INVSOCK
;
235 if (getsockname(socket
->m_fd
, &addr
, &size
) == SOCKET_ERROR
)
237 socket
->m_error
= GSOCK_IOERR
;
241 /* got a valid address from getsockname, create a GAddress object */
242 if ((address
= GAddress_new()) == NULL
)
244 socket
->m_error
= GSOCK_MEMERR
;
248 if ((err
= _GAddress_translate_from(address
, &addr
, size
)) != GSOCK_NOERROR
)
250 GAddress_destroy(address
);
251 socket
->m_error
= err
;
258 GAddress
*GSocket_GetPeer(GSocket
*socket
)
260 assert(socket
!= NULL
);
262 /* try to get it from the m_peer var */
264 return GAddress_copy(socket
->m_peer
);
269 /* Server specific parts */
271 /* GSocket_SetServer:
272 * Sets up this socket as a server. The local address must have been
273 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
274 * Returns GSOCK_NOERROR on success, one of the following otherwise:
277 * GSOCK_INVSOCK - the socket is in use.
278 * GSOCK_INVADDR - the local address has not been set.
279 * GSOCK_IOERR - low-level error.
281 GSocketError
GSocket_SetServer(GSocket
*sck
)
287 /* must not be in use */
288 if (sck
->m_fd
!= INVALID_SOCKET
)
290 sck
->m_error
= GSOCK_INVSOCK
;
291 return GSOCK_INVSOCK
;
294 /* the local addr must have been set */
297 sck
->m_error
= GSOCK_INVADDR
;
298 return GSOCK_INVADDR
;
301 /* Initialize all fields */
302 sck
->m_server
= TRUE
;
303 sck
->m_stream
= TRUE
;
304 sck
->m_oriented
= TRUE
;
306 /* Create the socket */
307 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
309 if (sck
->m_fd
== INVALID_SOCKET
)
311 sck
->m_error
= GSOCK_IOERR
;
315 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
316 _GSocket_Enable_Events(sck
);
318 /* Bind to the local address,
319 * retrieve the actual address bound,
320 * and listen up to 5 connections.
322 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
323 (getsockname(sck
->m_fd
,
324 sck
->m_local
->m_addr
,
325 (SOCKLEN_T
*)&sck
->m_local
->m_len
) != 0) ||
326 (listen(sck
->m_fd
, 5) != 0))
328 closesocket(sck
->m_fd
);
329 sck
->m_fd
= INVALID_SOCKET
;
330 sck
->m_error
= GSOCK_IOERR
;
334 return GSOCK_NOERROR
;
337 /* GSocket_WaitConnection:
338 * Waits for an incoming client connection. Returns a pointer to
339 * a GSocket object, or NULL if there was an error, in which case
340 * the last error field will be updated for the calling GSocket.
342 * Error codes (set in the calling GSocket)
343 * GSOCK_INVSOCK - the socket is not valid or not a server.
344 * GSOCK_TIMEDOUT - timeout, no incoming connections.
345 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
346 * GSOCK_MEMERR - couldn't allocate memory.
347 * GSOCK_IOERR - low-level error.
349 GSocket
*GSocket_WaitConnection(GSocket
*sck
)
352 struct sockaddr from
;
353 SOCKLEN_T fromlen
= sizeof(from
);
359 /* Reenable CONNECTION events */
360 sck
->m_detected
&= ~GSOCK_CONNECTION_FLAG
;
362 /* If the socket has already been created, we exit immediately */
363 if (sck
->m_fd
== INVALID_SOCKET
|| !sck
->m_server
)
365 sck
->m_error
= GSOCK_INVSOCK
;
369 /* Create a GSocket object for the new connection */
370 connection
= GSocket_new();
374 sck
->m_error
= GSOCK_MEMERR
;
378 /* Wait for a connection (with timeout) */
379 if (_GSocket_Input_Timeout(sck
) == GSOCK_TIMEDOUT
)
381 GSocket_destroy(connection
);
382 /* sck->m_error set by _GSocket_Input_Timeout */
386 connection
->m_fd
= accept(sck
->m_fd
, &from
, &fromlen
);
388 if (connection
->m_fd
== INVALID_SOCKET
)
390 if (WSAGetLastError() == WSAEWOULDBLOCK
)
391 sck
->m_error
= GSOCK_WOULDBLOCK
;
393 sck
->m_error
= GSOCK_IOERR
;
395 GSocket_destroy(connection
);
399 /* Initialize all fields */
400 connection
->m_server
= FALSE
;
401 connection
->m_stream
= TRUE
;
402 connection
->m_oriented
= TRUE
;
404 /* Setup the peer address field */
405 connection
->m_peer
= GAddress_new();
406 if (!connection
->m_peer
)
408 GSocket_destroy(connection
);
409 sck
->m_error
= GSOCK_MEMERR
;
412 err
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
);
413 if (err
!= GSOCK_NOERROR
)
415 GAddress_destroy(connection
->m_peer
);
416 GSocket_destroy(connection
);
421 ioctlsocket(connection
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
422 _GSocket_Enable_Events(connection
);
427 /* Client specific parts */
430 * For stream (connection oriented) sockets, GSocket_Connect() tries
431 * to establish a client connection to a server using the peer address
432 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
433 * connection has been succesfully established, or one of the error
434 * codes listed below. Note that for nonblocking sockets, a return
435 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
436 * request can be completed later; you should use GSocket_Select()
437 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
438 * corresponding asynchronous events.
440 * For datagram (non connection oriented) sockets, GSocket_Connect()
441 * just sets the peer address established with GSocket_SetPeer() as
442 * default destination.
445 * GSOCK_INVSOCK - the socket is in use or not valid.
446 * GSOCK_INVADDR - the peer address has not been established.
447 * GSOCK_TIMEDOUT - timeout, the connection failed.
448 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
449 * GSOCK_MEMERR - couldn't allocate memory.
450 * GSOCK_IOERR - low-level error.
452 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
459 /* Enable CONNECTION events (needed for nonblocking connections) */
460 sck
->m_detected
&= ~GSOCK_CONNECTION_FLAG
;
462 if (sck
->m_fd
!= INVALID_SOCKET
)
464 sck
->m_error
= GSOCK_INVSOCK
;
465 return GSOCK_INVSOCK
;
470 sck
->m_error
= GSOCK_INVADDR
;
471 return GSOCK_INVADDR
;
474 /* Streamed or dgram socket? */
475 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
476 sck
->m_oriented
= TRUE
;
477 sck
->m_server
= FALSE
;
478 sck
->m_establishing
= FALSE
;
480 /* Create the socket */
481 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
,
482 sck
->m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
484 if (sck
->m_fd
== INVALID_SOCKET
)
486 sck
->m_error
= GSOCK_IOERR
;
490 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
491 _GSocket_Enable_Events(sck
);
493 /* Connect it to the peer address, with a timeout (see below) */
494 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
496 if (ret
== SOCKET_ERROR
)
498 err
= WSAGetLastError();
500 /* If connect failed with EWOULDBLOCK and the GSocket object
501 * is in blocking mode, we select() for the specified timeout
502 * checking for writability to see if the connection request
505 if ((err
== WSAEWOULDBLOCK
) && (!sck
->m_non_blocking
))
507 err
= _GSocket_Connect_Timeout(sck
);
509 if (err
!= GSOCK_NOERROR
)
511 closesocket(sck
->m_fd
);
512 sck
->m_fd
= INVALID_SOCKET
;
513 /* sck->m_error is set in _GSocket_Connect_Timeout */
516 return (GSocketError
) err
;
519 /* If connect failed with EWOULDBLOCK and the GSocket object
520 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
521 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
522 * this way if the connection completes, a GSOCK_CONNECTION
523 * event will be generated, if enabled.
525 if ((err
== WSAEWOULDBLOCK
) && (sck
->m_non_blocking
))
527 sck
->m_establishing
= TRUE
;
528 sck
->m_error
= GSOCK_WOULDBLOCK
;
529 return GSOCK_WOULDBLOCK
;
532 /* If connect failed with an error other than EWOULDBLOCK,
533 * then the call to GSocket_Connect() has failed.
535 closesocket(sck
->m_fd
);
536 sck
->m_fd
= INVALID_SOCKET
;
537 sck
->m_error
= GSOCK_IOERR
;
541 return GSOCK_NOERROR
;
544 /* Datagram sockets */
546 /* GSocket_SetNonOriented:
547 * Sets up this socket as a non-connection oriented (datagram) socket.
548 * Before using this function, the local address must have been set
549 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
550 * on success, or one of the following otherwise.
553 * GSOCK_INVSOCK - the socket is in use.
554 * GSOCK_INVADDR - the local address has not been set.
555 * GSOCK_IOERR - low-level error.
557 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
563 if (sck
->m_fd
!= INVALID_SOCKET
)
565 sck
->m_error
= GSOCK_INVSOCK
;
566 return GSOCK_INVSOCK
;
571 sck
->m_error
= GSOCK_INVADDR
;
572 return GSOCK_INVADDR
;
575 /* Initialize all fields */
576 sck
->m_stream
= FALSE
;
577 sck
->m_server
= FALSE
;
578 sck
->m_oriented
= FALSE
;
580 /* Create the socket */
581 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
583 if (sck
->m_fd
== INVALID_SOCKET
)
585 sck
->m_error
= GSOCK_IOERR
;
589 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
590 _GSocket_Enable_Events(sck
);
592 /* Bind to the local address,
593 * and retrieve the actual address bound.
595 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
596 (getsockname(sck
->m_fd
,
597 sck
->m_local
->m_addr
,
598 (SOCKLEN_T
*)&sck
->m_local
->m_len
) != 0))
600 closesocket(sck
->m_fd
);
601 sck
->m_fd
= INVALID_SOCKET
;
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 known state first */
717 result
|= (GSOCK_CONNECTION_FLAG
& socket
->m_detected
& flags
);
718 result
|= (GSOCK_LOST_FLAG
& socket
->m_detected
& flags
);
721 if (select(socket
->m_fd
+ 1, &readfds
, &writefds
, &exceptfds
, &tv
) <= 0)
724 /* Check for readability */
725 if (FD_ISSET(socket
->m_fd
, &readfds
))
727 /* Assume that closure of the socket is always reported via exceptfds */
728 if (socket
->m_server
&& socket
->m_stream
)
729 result
|= (GSOCK_CONNECTION_FLAG
& flags
);
731 result
|= (GSOCK_INPUT_FLAG
& flags
);
734 /* Check for writability */
735 if (FD_ISSET(socket
->m_fd
, &writefds
))
737 if (socket
->m_establishing
&& !socket
->m_server
)
739 result
|= (GSOCK_CONNECTION_FLAG
& flags
);
740 socket
->m_establishing
= FALSE
;
741 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
744 result
|= (GSOCK_OUTPUT_FLAG
& flags
);
747 /* Check for exceptions and errors */
748 if (FD_ISSET(socket
->m_fd
, &exceptfds
))
750 result
|= (GSOCK_LOST_FLAG
& flags
);
751 socket
->m_establishing
= FALSE
;
752 socket
->m_detected
= GSOCK_LOST_FLAG
;
759 assert(socket
!= NULL
);
760 return flags
& socket
->m_detected
;
762 #endif /* !wxUSE_GUI */
767 /* GSocket_SetNonBlocking:
768 * Sets the socket to non-blocking mode. All IO calls will return
771 void GSocket_SetNonBlocking(GSocket
*socket
, int non_block
)
773 assert(socket
!= NULL
);
775 socket
->m_non_blocking
= non_block
;
778 /* GSocket_SetTimeout:
779 * Sets the timeout for blocking calls. Time is expressed in
782 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millis
)
784 assert(socket
!= NULL
);
786 socket
->m_timeout
.tv_sec
= (millis
/ 1000);
787 socket
->m_timeout
.tv_usec
= (millis
% 1000) * 1000;
791 * Returns the last error occured for this socket. Note that successful
792 * operations do not clear this back to GSOCK_NOERROR, so use it only
795 GSocketError
GSocket_GetError(GSocket
*socket
)
797 assert(socket
!= NULL
);
799 return socket
->m_error
;
805 * There is data to be read in the input buffer. If, after a read
806 * operation, there is still data available, the callback function will
809 * The socket is available for writing. That is, the next write call
810 * won't block. This event is generated only once, when the connection is
811 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
812 * when the output buffer empties again. This means that the app should
813 * assume that it can write since the first OUTPUT event, and no more
814 * OUTPUT events will be generated unless an error occurs.
816 * Connection succesfully established, for client sockets, or incoming
817 * client connection, for server sockets. Wait for this event (also watch
818 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
820 * The connection is lost (or a connection request failed); this could
821 * be due to a failure, or due to the peer closing it gracefully.
824 /* GSocket_SetCallback:
825 * Enables the callbacks specified by 'flags'. Note that 'flags'
826 * may be a combination of flags OR'ed toghether, so the same
827 * callback function can be made to accept different events.
828 * The callback function must have the following prototype:
830 * void function(GSocket *socket, GSocketEvent event, char *cdata)
832 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
833 GSocketCallback callback
, char *cdata
)
837 assert(socket
!= NULL
);
839 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
841 if ((flags
& (1 << count
)) != 0)
843 socket
->m_cbacks
[count
] = callback
;
844 socket
->m_data
[count
] = cdata
;
849 /* GSocket_UnsetCallback:
850 * Disables all callbacks specified by 'flags', which may be a
851 * combination of flags OR'ed toghether.
853 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
857 assert(socket
!= NULL
);
859 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
861 if ((flags
& (1 << count
)) != 0)
863 socket
->m_cbacks
[count
] = NULL
;
864 socket
->m_data
[count
] = NULL
;
871 /* _GSocket_Input_Timeout:
872 * For blocking sockets, wait until data is available or
873 * until timeout ellapses.
875 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
879 if (!socket
->m_non_blocking
)
882 FD_SET(socket
->m_fd
, &readfds
);
883 if (select(0, &readfds
, NULL
, NULL
, &socket
->m_timeout
) == 0)
885 socket
->m_error
= GSOCK_TIMEDOUT
;
886 return GSOCK_TIMEDOUT
;
889 return GSOCK_NOERROR
;
892 /* _GSocket_Output_Timeout:
893 * For blocking sockets, wait until data can be sent without
894 * blocking or until timeout ellapses.
896 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
900 if (!socket
->m_non_blocking
)
903 FD_SET(socket
->m_fd
, &writefds
);
904 if (select(0, NULL
, &writefds
, NULL
, &socket
->m_timeout
) == 0)
906 socket
->m_error
= GSOCK_TIMEDOUT
;
907 return GSOCK_TIMEDOUT
;
910 return GSOCK_NOERROR
;
913 /* _GSocket_Connect_Timeout:
914 * For blocking sockets, wait until the connection is
915 * established or fails, or until timeout ellapses.
917 GSocketError
_GSocket_Connect_Timeout(GSocket
*socket
)
924 FD_SET(socket
->m_fd
, &writefds
);
925 FD_SET(socket
->m_fd
, &exceptfds
);
926 if (select(0, NULL
, &writefds
, &exceptfds
, &socket
->m_timeout
) == 0)
928 socket
->m_error
= GSOCK_TIMEDOUT
;
929 return GSOCK_TIMEDOUT
;
931 if (!FD_ISSET(socket
->m_fd
, &writefds
))
933 socket
->m_error
= GSOCK_IOERR
;
937 return GSOCK_NOERROR
;
940 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
942 return recv(socket
->m_fd
, buffer
, size
, 0);
945 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
947 struct sockaddr from
;
948 SOCKLEN_T fromlen
= sizeof(from
);
952 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
954 if (ret
== SOCKET_ERROR
)
957 /* Translate a system address into a GSocket address */
960 socket
->m_peer
= GAddress_new();
963 socket
->m_error
= GSOCK_MEMERR
;
967 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
968 if (err
!= GSOCK_NOERROR
)
970 GAddress_destroy(socket
->m_peer
);
971 socket
->m_peer
= NULL
;
972 socket
->m_error
= err
;
979 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
981 return send(socket
->m_fd
, buffer
, size
, 0);
984 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
986 struct sockaddr
*addr
;
992 socket
->m_error
= GSOCK_INVADDR
;
996 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
997 if (err
!= GSOCK_NOERROR
)
999 socket
->m_error
= err
;
1003 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
1005 /* Frees memory allocated by _GAddress_translate_to */
1013 * -------------------------------------------------------------------------
1015 * -------------------------------------------------------------------------
1018 /* CHECK_ADDRESS verifies that the current address family is either
1019 * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it
1020 * initalizes it to be a GSOCK_*family*. In other cases, it returns
1021 * an appropiate error code.
1023 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
1025 #define CHECK_ADDRESS(address, family) \
1027 if (address->m_family == GSOCK_NOFAMILY) \
1028 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1029 return address->m_error; \
1030 if (address->m_family != GSOCK_##family) \
1032 address->m_error = GSOCK_INVADDR; \
1033 return GSOCK_INVADDR; \
1037 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
1039 if (address->m_family == GSOCK_NOFAMILY) \
1040 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1042 if (address->m_family != GSOCK_##family) \
1044 address->m_error = GSOCK_INVADDR; \
1050 GAddress
*GAddress_new(void)
1054 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1057 address
->m_family
= GSOCK_NOFAMILY
;
1058 address
->m_addr
= NULL
;
1064 GAddress
*GAddress_copy(GAddress
*address
)
1068 assert(address
!= NULL
);
1070 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1073 memcpy(addr2
, address
, sizeof(GAddress
));
1075 if (address
->m_addr
)
1077 addr2
->m_addr
= (struct sockaddr
*) malloc(addr2
->m_len
);
1078 if (addr2
->m_addr
== NULL
)
1083 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1089 void GAddress_destroy(GAddress
*address
)
1091 assert(address
!= NULL
);
1093 if (address
->m_addr
)
1094 free(address
->m_addr
);
1099 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1101 assert(address
!= NULL
);
1103 address
->m_family
= type
;
1106 GAddressType
GAddress_GetFamily(GAddress
*address
)
1108 assert(address
!= NULL
);
1110 return address
->m_family
;
1113 GSocketError
_GAddress_translate_from(GAddress
*address
,
1114 struct sockaddr
*addr
, int len
)
1116 address
->m_realfamily
= addr
->sa_family
;
1117 switch (addr
->sa_family
)
1120 address
->m_family
= GSOCK_INET
;
1123 address
->m_family
= GSOCK_UNIX
;
1127 address
->m_family
= GSOCK_INET6
;
1132 address
->m_error
= GSOCK_INVOP
;
1137 if (address
->m_addr
)
1138 free(address
->m_addr
);
1140 address
->m_len
= len
;
1141 address
->m_addr
= (struct sockaddr
*) malloc(len
);
1143 if (address
->m_addr
== NULL
)
1145 address
->m_error
= GSOCK_MEMERR
;
1146 return GSOCK_MEMERR
;
1148 memcpy(address
->m_addr
, addr
, len
);
1150 return GSOCK_NOERROR
;
1153 GSocketError
_GAddress_translate_to(GAddress
*address
,
1154 struct sockaddr
**addr
, int *len
)
1156 if (!address
->m_addr
)
1158 address
->m_error
= GSOCK_INVADDR
;
1159 return GSOCK_INVADDR
;
1162 *len
= address
->m_len
;
1163 *addr
= (struct sockaddr
*) malloc(address
->m_len
);
1166 address
->m_error
= GSOCK_MEMERR
;
1167 return GSOCK_MEMERR
;
1170 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1171 return GSOCK_NOERROR
;
1175 * -------------------------------------------------------------------------
1176 * Internet address family
1177 * -------------------------------------------------------------------------
1180 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1182 address
->m_len
= sizeof(struct sockaddr_in
);
1183 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1184 if (address
->m_addr
== NULL
)
1186 address
->m_error
= GSOCK_MEMERR
;
1187 return GSOCK_MEMERR
;
1190 address
->m_family
= GSOCK_INET
;
1191 address
->m_realfamily
= PF_INET
;
1192 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1193 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1195 return GSOCK_NOERROR
;
1198 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1201 struct in_addr
*addr
;
1203 assert(address
!= NULL
);
1205 CHECK_ADDRESS(address
, INET
);
1207 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1209 addr
->s_addr
= inet_addr(hostname
);
1211 /* If it is a numeric host name, convert it now */
1212 if (addr
->s_addr
== INADDR_NONE
)
1214 struct in_addr
*array_addr
;
1216 /* It is a real name, we solve it */
1217 if ((he
= gethostbyname(hostname
)) == NULL
)
1219 /* addr->s_addr = INADDR_NONE just done by inet_addr() above */
1220 address
->m_error
= GSOCK_NOHOST
;
1221 return GSOCK_NOHOST
;
1223 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1224 addr
->s_addr
= array_addr
[0].s_addr
;
1226 return GSOCK_NOERROR
;
1229 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1231 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1234 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1235 unsigned long hostaddr
)
1237 struct in_addr
*addr
;
1239 assert(address
!= NULL
);
1241 CHECK_ADDRESS(address
, INET
);
1243 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1244 addr
->s_addr
= hostaddr
;
1246 return GSOCK_NOERROR
;
1249 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1250 const char *protocol
)
1253 struct sockaddr_in
*addr
;
1255 assert(address
!= NULL
);
1256 CHECK_ADDRESS(address
, INET
);
1260 address
->m_error
= GSOCK_INVPORT
;
1261 return GSOCK_INVPORT
;
1264 se
= getservbyname(port
, protocol
);
1267 if (isdigit(port
[0]))
1271 port_int
= atoi(port
);
1272 addr
= (struct sockaddr_in
*)address
->m_addr
;
1273 addr
->sin_port
= htons((u_short
) port_int
);
1274 return GSOCK_NOERROR
;
1277 address
->m_error
= GSOCK_INVPORT
;
1278 return GSOCK_INVPORT
;
1281 addr
= (struct sockaddr_in
*)address
->m_addr
;
1282 addr
->sin_port
= se
->s_port
;
1284 return GSOCK_NOERROR
;
1287 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1289 struct sockaddr_in
*addr
;
1291 assert(address
!= NULL
);
1292 CHECK_ADDRESS(address
, INET
);
1294 addr
= (struct sockaddr_in
*)address
->m_addr
;
1295 addr
->sin_port
= htons(port
);
1297 return GSOCK_NOERROR
;
1300 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1304 struct sockaddr_in
*addr
;
1306 assert(address
!= NULL
);
1307 CHECK_ADDRESS(address
, INET
);
1309 addr
= (struct sockaddr_in
*)address
->m_addr
;
1310 addr_buf
= (char *)&(addr
->sin_addr
);
1312 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1315 address
->m_error
= GSOCK_NOHOST
;
1316 return GSOCK_NOHOST
;
1319 strncpy(hostname
, he
->h_name
, sbuf
);
1321 return GSOCK_NOERROR
;
1324 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1326 struct sockaddr_in
*addr
;
1328 assert(address
!= NULL
);
1329 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1331 addr
= (struct sockaddr_in
*)address
->m_addr
;
1333 return addr
->sin_addr
.s_addr
;
1336 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1338 struct sockaddr_in
*addr
;
1340 assert(address
!= NULL
);
1341 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1343 addr
= (struct sockaddr_in
*)address
->m_addr
;
1344 return ntohs(addr
->sin_port
);
1348 * -------------------------------------------------------------------------
1349 * Unix address family
1350 * -------------------------------------------------------------------------
1353 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1355 assert (address
!= NULL
);
1356 address
->m_error
= GSOCK_INVADDR
;
1357 return GSOCK_INVADDR
;
1360 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1362 assert (address
!= NULL
);
1363 address
->m_error
= GSOCK_INVADDR
;
1364 return GSOCK_INVADDR
;
1367 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1369 assert (address
!= NULL
);
1370 address
->m_error
= GSOCK_INVADDR
;
1371 return GSOCK_INVADDR
;
1374 #else /* !wxUSE_SOCKETS */
1377 * Translation unit shouldn't be empty, so include this typedef to make the
1378 * compiler (VC++ 6.0, for example) happy
1380 typedef void (*wxDummy
)();
1382 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */