]>
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_close(GSocket
*socket
)
108 _GSocket_Disable_Events(socket
);
109 closesocket(socket
->m_fd
);
110 socket
->m_fd
= INVALID_SOCKET
;
113 void GSocket_destroy(GSocket
*socket
)
115 assert(socket
!= NULL
);
117 /* Per-socket GUI-specific cleanup */
118 _GSocket_GUI_Destroy(socket
);
120 /* Check that the socket is really shutdowned */
121 if (socket
->m_fd
!= INVALID_SOCKET
)
122 GSocket_Shutdown(socket
);
124 /* Destroy private addresses */
126 GAddress_destroy(socket
->m_local
);
129 GAddress_destroy(socket
->m_peer
);
131 /* Destroy the socket itself */
136 * Disallow further read/write operations on this socket, close
137 * the fd and disable all callbacks.
139 void GSocket_Shutdown(GSocket
*socket
)
143 assert(socket
!= NULL
);
145 /* If socket has been created, shutdown it */
146 if (socket
->m_fd
!= INVALID_SOCKET
)
148 shutdown(socket
->m_fd
, 2);
149 GSocket_close(socket
);
152 /* Disable GUI callbacks */
153 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
154 socket
->m_cbacks
[evt
] = NULL
;
156 socket
->m_detected
= GSOCK_LOST_FLAG
;
159 /* Address handling */
165 * Set or get the local or peer address for this socket. The 'set'
166 * functions return GSOCK_NOERROR on success, an error code otherwise.
167 * The 'get' functions return a pointer to a GAddress object on success,
168 * or NULL otherwise, in which case they set the error code of the
169 * corresponding GSocket.
172 * GSOCK_INVSOCK - the socket is not valid.
173 * GSOCK_INVADDR - the address is not valid.
175 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
177 assert(socket
!= NULL
);
179 /* the socket must be initialized, or it must be a server */
180 if (socket
->m_fd
!= INVALID_SOCKET
&& !socket
->m_server
)
182 socket
->m_error
= GSOCK_INVSOCK
;
183 return GSOCK_INVSOCK
;
187 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
189 socket
->m_error
= GSOCK_INVADDR
;
190 return GSOCK_INVADDR
;
194 GAddress_destroy(socket
->m_local
);
196 socket
->m_local
= GAddress_copy(address
);
198 return GSOCK_NOERROR
;
201 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
203 assert(socket
!= NULL
);
206 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
208 socket
->m_error
= GSOCK_INVADDR
;
209 return GSOCK_INVADDR
;
213 GAddress_destroy(socket
->m_peer
);
215 socket
->m_peer
= GAddress_copy(address
);
217 return GSOCK_NOERROR
;
220 GAddress
*GSocket_GetLocal(GSocket
*socket
)
223 struct sockaddr addr
;
224 SOCKLEN_T size
= sizeof(addr
);
227 assert(socket
!= NULL
);
229 /* try to get it from the m_local var first */
231 return GAddress_copy(socket
->m_local
);
233 /* else, if the socket is initialized, try getsockname */
234 if (socket
->m_fd
== INVALID_SOCKET
)
236 socket
->m_error
= GSOCK_INVSOCK
;
240 if (getsockname(socket
->m_fd
, &addr
, &size
) == SOCKET_ERROR
)
242 socket
->m_error
= GSOCK_IOERR
;
246 /* got a valid address from getsockname, create a GAddress object */
247 if ((address
= GAddress_new()) == NULL
)
249 socket
->m_error
= GSOCK_MEMERR
;
253 if ((err
= _GAddress_translate_from(address
, &addr
, size
)) != GSOCK_NOERROR
)
255 GAddress_destroy(address
);
256 socket
->m_error
= err
;
263 GAddress
*GSocket_GetPeer(GSocket
*socket
)
265 assert(socket
!= NULL
);
267 /* try to get it from the m_peer var */
269 return GAddress_copy(socket
->m_peer
);
274 /* Server specific parts */
276 /* GSocket_SetServer:
277 * Sets up this socket as a server. The local address must have been
278 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
279 * Returns GSOCK_NOERROR on success, one of the following otherwise:
282 * GSOCK_INVSOCK - the socket is in use.
283 * GSOCK_INVADDR - the local address has not been set.
284 * GSOCK_IOERR - low-level error.
286 GSocketError
GSocket_SetServer(GSocket
*sck
)
292 /* must not be in use */
293 if (sck
->m_fd
!= INVALID_SOCKET
)
295 sck
->m_error
= GSOCK_INVSOCK
;
296 return GSOCK_INVSOCK
;
299 /* the local addr must have been set */
302 sck
->m_error
= GSOCK_INVADDR
;
303 return GSOCK_INVADDR
;
306 /* Initialize all fields */
307 sck
->m_server
= TRUE
;
308 sck
->m_stream
= TRUE
;
309 sck
->m_oriented
= TRUE
;
311 /* Create the socket */
312 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
314 if (sck
->m_fd
== INVALID_SOCKET
)
316 sck
->m_error
= GSOCK_IOERR
;
320 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
321 _GSocket_Enable_Events(sck
);
323 /* Bind to the local address,
324 * retrieve the actual address bound,
325 * and listen up to 5 connections.
327 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
328 (getsockname(sck
->m_fd
,
329 sck
->m_local
->m_addr
,
330 (SOCKLEN_T
*)&sck
->m_local
->m_len
) != 0) ||
331 (listen(sck
->m_fd
, 5) != 0))
334 sck
->m_error
= GSOCK_IOERR
;
338 return GSOCK_NOERROR
;
341 /* GSocket_WaitConnection:
342 * Waits for an incoming client connection. Returns a pointer to
343 * a GSocket object, or NULL if there was an error, in which case
344 * the last error field will be updated for the calling GSocket.
346 * Error codes (set in the calling GSocket)
347 * GSOCK_INVSOCK - the socket is not valid or not a server.
348 * GSOCK_TIMEDOUT - timeout, no incoming connections.
349 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
350 * GSOCK_MEMERR - couldn't allocate memory.
351 * GSOCK_IOERR - low-level error.
353 GSocket
*GSocket_WaitConnection(GSocket
*sck
)
356 struct sockaddr from
;
357 SOCKLEN_T fromlen
= sizeof(from
);
363 /* Reenable CONNECTION events */
364 sck
->m_detected
&= ~GSOCK_CONNECTION_FLAG
;
366 /* If the socket has already been created, we exit immediately */
367 if (sck
->m_fd
== INVALID_SOCKET
|| !sck
->m_server
)
369 sck
->m_error
= GSOCK_INVSOCK
;
373 /* Create a GSocket object for the new connection */
374 connection
= GSocket_new();
378 sck
->m_error
= GSOCK_MEMERR
;
382 /* Wait for a connection (with timeout) */
383 if (_GSocket_Input_Timeout(sck
) == GSOCK_TIMEDOUT
)
385 GSocket_destroy(connection
);
386 /* sck->m_error set by _GSocket_Input_Timeout */
390 connection
->m_fd
= accept(sck
->m_fd
, &from
, &fromlen
);
392 if (connection
->m_fd
== INVALID_SOCKET
)
394 if (WSAGetLastError() == WSAEWOULDBLOCK
)
395 sck
->m_error
= GSOCK_WOULDBLOCK
;
397 sck
->m_error
= GSOCK_IOERR
;
399 GSocket_destroy(connection
);
403 /* Initialize all fields */
404 connection
->m_server
= FALSE
;
405 connection
->m_stream
= TRUE
;
406 connection
->m_oriented
= TRUE
;
408 /* Setup the peer address field */
409 connection
->m_peer
= GAddress_new();
410 if (!connection
->m_peer
)
412 GSocket_destroy(connection
);
413 sck
->m_error
= GSOCK_MEMERR
;
416 err
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
);
417 if (err
!= GSOCK_NOERROR
)
419 GAddress_destroy(connection
->m_peer
);
420 GSocket_destroy(connection
);
425 ioctlsocket(connection
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
426 _GSocket_Enable_Events(connection
);
431 /* Client specific parts */
434 * For stream (connection oriented) sockets, GSocket_Connect() tries
435 * to establish a client connection to a server using the peer address
436 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
437 * connection has been succesfully established, or one of the error
438 * codes listed below. Note that for nonblocking sockets, a return
439 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
440 * request can be completed later; you should use GSocket_Select()
441 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
442 * corresponding asynchronous events.
444 * For datagram (non connection oriented) sockets, GSocket_Connect()
445 * just sets the peer address established with GSocket_SetPeer() as
446 * default destination.
449 * GSOCK_INVSOCK - the socket is in use or not valid.
450 * GSOCK_INVADDR - the peer address has not been established.
451 * GSOCK_TIMEDOUT - timeout, the connection failed.
452 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
453 * GSOCK_MEMERR - couldn't allocate memory.
454 * GSOCK_IOERR - low-level error.
456 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
463 /* Enable CONNECTION events (needed for nonblocking connections) */
464 sck
->m_detected
&= ~GSOCK_CONNECTION_FLAG
;
466 if (sck
->m_fd
!= INVALID_SOCKET
)
468 sck
->m_error
= GSOCK_INVSOCK
;
469 return GSOCK_INVSOCK
;
474 sck
->m_error
= GSOCK_INVADDR
;
475 return GSOCK_INVADDR
;
478 /* Streamed or dgram socket? */
479 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
480 sck
->m_oriented
= TRUE
;
481 sck
->m_server
= FALSE
;
482 sck
->m_establishing
= FALSE
;
484 /* Create the socket */
485 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
,
486 sck
->m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
488 if (sck
->m_fd
== INVALID_SOCKET
)
490 sck
->m_error
= GSOCK_IOERR
;
494 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
495 _GSocket_Enable_Events(sck
);
497 /* Connect it to the peer address, with a timeout (see below) */
498 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
500 if (ret
== SOCKET_ERROR
)
502 err
= WSAGetLastError();
504 /* If connect failed with EWOULDBLOCK and the GSocket object
505 * is in blocking mode, we select() for the specified timeout
506 * checking for writability to see if the connection request
509 if ((err
== WSAEWOULDBLOCK
) && (!sck
->m_non_blocking
))
511 err
= _GSocket_Connect_Timeout(sck
);
513 if (err
!= GSOCK_NOERROR
)
516 /* sck->m_error is set in _GSocket_Connect_Timeout */
519 return (GSocketError
) err
;
522 /* If connect failed with EWOULDBLOCK and the GSocket object
523 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
524 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
525 * this way if the connection completes, a GSOCK_CONNECTION
526 * event will be generated, if enabled.
528 if ((err
== WSAEWOULDBLOCK
) && (sck
->m_non_blocking
))
530 sck
->m_establishing
= TRUE
;
531 sck
->m_error
= GSOCK_WOULDBLOCK
;
532 return GSOCK_WOULDBLOCK
;
535 /* If connect failed with an error other than EWOULDBLOCK,
536 * then the call to GSocket_Connect() has failed.
539 sck
->m_error
= GSOCK_IOERR
;
543 return GSOCK_NOERROR
;
546 /* Datagram sockets */
548 /* GSocket_SetNonOriented:
549 * Sets up this socket as a non-connection oriented (datagram) socket.
550 * Before using this function, the local address must have been set
551 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
552 * on success, or one of the following otherwise.
555 * GSOCK_INVSOCK - the socket is in use.
556 * GSOCK_INVADDR - the local address has not been set.
557 * GSOCK_IOERR - low-level error.
559 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
565 if (sck
->m_fd
!= INVALID_SOCKET
)
567 sck
->m_error
= GSOCK_INVSOCK
;
568 return GSOCK_INVSOCK
;
573 sck
->m_error
= GSOCK_INVADDR
;
574 return GSOCK_INVADDR
;
577 /* Initialize all fields */
578 sck
->m_stream
= FALSE
;
579 sck
->m_server
= FALSE
;
580 sck
->m_oriented
= FALSE
;
582 /* Create the socket */
583 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
585 if (sck
->m_fd
== INVALID_SOCKET
)
587 sck
->m_error
= GSOCK_IOERR
;
591 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
592 _GSocket_Enable_Events(sck
);
594 /* Bind to the local address,
595 * and retrieve the actual address bound.
597 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
598 (getsockname(sck
->m_fd
,
599 sck
->m_local
->m_addr
,
600 (SOCKLEN_T
*)&sck
->m_local
->m_len
) != 0))
603 sck
->m_error
= GSOCK_IOERR
;
607 return GSOCK_NOERROR
;
612 /* Like recv(), send(), ... */
613 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
617 assert(socket
!= NULL
);
619 /* Reenable INPUT events */
620 socket
->m_detected
&= ~GSOCK_INPUT_FLAG
;
622 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
624 socket
->m_error
= GSOCK_INVSOCK
;
628 /* If the socket is blocking, wait for data (with a timeout) */
629 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
633 if (socket
->m_stream
)
634 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
636 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
638 if (ret
== SOCKET_ERROR
)
640 if (WSAGetLastError() != WSAEWOULDBLOCK
)
641 socket
->m_error
= GSOCK_IOERR
;
643 socket
->m_error
= GSOCK_WOULDBLOCK
;
650 int GSocket_Write(GSocket
*socket
, const char *buffer
, int size
)
654 assert(socket
!= NULL
);
656 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
658 socket
->m_error
= GSOCK_INVSOCK
;
662 /* If the socket is blocking, wait for writability (with a timeout) */
663 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
667 if (socket
->m_stream
)
668 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
670 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
672 if (ret
== SOCKET_ERROR
)
674 if (WSAGetLastError() != WSAEWOULDBLOCK
)
675 socket
->m_error
= GSOCK_IOERR
;
677 socket
->m_error
= GSOCK_WOULDBLOCK
;
679 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
680 * does). Once the first OUTPUT event is received, users can assume
681 * that the socket is writable until a read operation fails. Only then
682 * will further OUTPUT events be posted.
684 socket
->m_detected
&= ~GSOCK_OUTPUT_FLAG
;
692 * Polls the socket to determine its status. This function will
693 * check for the events specified in the 'flags' parameter, and
694 * it will return a mask indicating which operations can be
695 * performed. This function won't block, regardless of the
696 * mode (blocking | nonblocking) of the socket.
698 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
700 #if defined(wxUSE_GUI) && !wxUSE_GUI
702 GSocketEventFlags result
= 0;
706 static const struct timeval tv
= { 0, 0 };
708 assert(socket
!= NULL
);
713 FD_SET(socket
->m_fd
, &readfds
);
714 FD_SET(socket
->m_fd
, &writefds
);
715 FD_SET(socket
->m_fd
, &exceptfds
);
717 /* Check known state first */
718 result
|= (GSOCK_CONNECTION_FLAG
& socket
->m_detected
& flags
);
719 result
|= (GSOCK_LOST_FLAG
& socket
->m_detected
& flags
);
722 if (select(socket
->m_fd
+ 1, &readfds
, &writefds
, &exceptfds
, &tv
) <= 0)
725 /* Check for readability */
726 if (FD_ISSET(socket
->m_fd
, &readfds
))
728 /* Assume that closure of the socket is always reported via exceptfds */
729 if (socket
->m_server
&& socket
->m_stream
)
730 result
|= (GSOCK_CONNECTION_FLAG
& flags
);
732 result
|= (GSOCK_INPUT_FLAG
& flags
);
735 /* Check for writability */
736 if (FD_ISSET(socket
->m_fd
, &writefds
))
738 if (socket
->m_establishing
&& !socket
->m_server
)
740 result
|= (GSOCK_CONNECTION_FLAG
& flags
);
741 socket
->m_establishing
= FALSE
;
742 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
745 result
|= (GSOCK_OUTPUT_FLAG
& flags
);
748 /* Check for exceptions and errors */
749 if (FD_ISSET(socket
->m_fd
, &exceptfds
))
751 result
|= (GSOCK_LOST_FLAG
& flags
);
752 socket
->m_establishing
= FALSE
;
753 socket
->m_detected
= GSOCK_LOST_FLAG
;
760 assert(socket
!= NULL
);
761 return flags
& socket
->m_detected
;
763 #endif /* !wxUSE_GUI */
768 /* GSocket_SetNonBlocking:
769 * Sets the socket to non-blocking mode. All IO calls will return
772 void GSocket_SetNonBlocking(GSocket
*socket
, int non_block
)
774 assert(socket
!= NULL
);
776 socket
->m_non_blocking
= non_block
;
779 /* GSocket_SetTimeout:
780 * Sets the timeout for blocking calls. Time is expressed in
783 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millis
)
785 assert(socket
!= NULL
);
787 socket
->m_timeout
.tv_sec
= (millis
/ 1000);
788 socket
->m_timeout
.tv_usec
= (millis
% 1000) * 1000;
792 * Returns the last error occured for this socket. Note that successful
793 * operations do not clear this back to GSOCK_NOERROR, so use it only
796 GSocketError
GSocket_GetError(GSocket
*socket
)
798 assert(socket
!= NULL
);
800 return socket
->m_error
;
806 * There is data to be read in the input buffer. If, after a read
807 * operation, there is still data available, the callback function will
810 * The socket is available for writing. That is, the next write call
811 * won't block. This event is generated only once, when the connection is
812 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
813 * when the output buffer empties again. This means that the app should
814 * assume that it can write since the first OUTPUT event, and no more
815 * OUTPUT events will be generated unless an error occurs.
817 * Connection succesfully established, for client sockets, or incoming
818 * client connection, for server sockets. Wait for this event (also watch
819 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
821 * The connection is lost (or a connection request failed); this could
822 * be due to a failure, or due to the peer closing it gracefully.
825 /* GSocket_SetCallback:
826 * Enables the callbacks specified by 'flags'. Note that 'flags'
827 * may be a combination of flags OR'ed toghether, so the same
828 * callback function can be made to accept different events.
829 * The callback function must have the following prototype:
831 * void function(GSocket *socket, GSocketEvent event, char *cdata)
833 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
834 GSocketCallback callback
, char *cdata
)
838 assert(socket
!= NULL
);
840 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
842 if ((flags
& (1 << count
)) != 0)
844 socket
->m_cbacks
[count
] = callback
;
845 socket
->m_data
[count
] = cdata
;
850 /* GSocket_UnsetCallback:
851 * Disables all callbacks specified by 'flags', which may be a
852 * combination of flags OR'ed toghether.
854 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
858 assert(socket
!= NULL
);
860 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
862 if ((flags
& (1 << count
)) != 0)
864 socket
->m_cbacks
[count
] = NULL
;
865 socket
->m_data
[count
] = NULL
;
872 /* _GSocket_Input_Timeout:
873 * For blocking sockets, wait until data is available or
874 * until timeout ellapses.
876 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
880 if (!socket
->m_non_blocking
)
883 FD_SET(socket
->m_fd
, &readfds
);
884 if (select(0, &readfds
, NULL
, NULL
, &socket
->m_timeout
) == 0)
886 socket
->m_error
= GSOCK_TIMEDOUT
;
887 return GSOCK_TIMEDOUT
;
890 return GSOCK_NOERROR
;
893 /* _GSocket_Output_Timeout:
894 * For blocking sockets, wait until data can be sent without
895 * blocking or until timeout ellapses.
897 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
901 if (!socket
->m_non_blocking
)
904 FD_SET(socket
->m_fd
, &writefds
);
905 if (select(0, NULL
, &writefds
, NULL
, &socket
->m_timeout
) == 0)
907 socket
->m_error
= GSOCK_TIMEDOUT
;
908 return GSOCK_TIMEDOUT
;
911 return GSOCK_NOERROR
;
914 /* _GSocket_Connect_Timeout:
915 * For blocking sockets, wait until the connection is
916 * established or fails, or until timeout ellapses.
918 GSocketError
_GSocket_Connect_Timeout(GSocket
*socket
)
925 FD_SET(socket
->m_fd
, &writefds
);
926 FD_SET(socket
->m_fd
, &exceptfds
);
927 if (select(0, NULL
, &writefds
, &exceptfds
, &socket
->m_timeout
) == 0)
929 socket
->m_error
= GSOCK_TIMEDOUT
;
930 return GSOCK_TIMEDOUT
;
932 if (!FD_ISSET(socket
->m_fd
, &writefds
))
934 socket
->m_error
= GSOCK_IOERR
;
938 return GSOCK_NOERROR
;
941 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
943 return recv(socket
->m_fd
, buffer
, size
, 0);
946 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
948 struct sockaddr from
;
949 SOCKLEN_T fromlen
= sizeof(from
);
953 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
955 if (ret
== SOCKET_ERROR
)
958 /* Translate a system address into a GSocket address */
961 socket
->m_peer
= GAddress_new();
964 socket
->m_error
= GSOCK_MEMERR
;
968 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
969 if (err
!= GSOCK_NOERROR
)
971 GAddress_destroy(socket
->m_peer
);
972 socket
->m_peer
= NULL
;
973 socket
->m_error
= err
;
980 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
982 return send(socket
->m_fd
, buffer
, size
, 0);
985 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
987 struct sockaddr
*addr
;
993 socket
->m_error
= GSOCK_INVADDR
;
997 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
998 if (err
!= GSOCK_NOERROR
)
1000 socket
->m_error
= err
;
1004 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
1006 /* Frees memory allocated by _GAddress_translate_to */
1014 * -------------------------------------------------------------------------
1016 * -------------------------------------------------------------------------
1019 /* CHECK_ADDRESS verifies that the current address family is either
1020 * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it
1021 * initalizes it to be a GSOCK_*family*. In other cases, it returns
1022 * an appropiate error code.
1024 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
1026 #define CHECK_ADDRESS(address, family) \
1028 if (address->m_family == GSOCK_NOFAMILY) \
1029 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1030 return address->m_error; \
1031 if (address->m_family != GSOCK_##family) \
1033 address->m_error = GSOCK_INVADDR; \
1034 return GSOCK_INVADDR; \
1038 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
1040 if (address->m_family == GSOCK_NOFAMILY) \
1041 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1043 if (address->m_family != GSOCK_##family) \
1045 address->m_error = GSOCK_INVADDR; \
1051 GAddress
*GAddress_new(void)
1055 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1058 address
->m_family
= GSOCK_NOFAMILY
;
1059 address
->m_addr
= NULL
;
1065 GAddress
*GAddress_copy(GAddress
*address
)
1069 assert(address
!= NULL
);
1071 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1074 memcpy(addr2
, address
, sizeof(GAddress
));
1076 if (address
->m_addr
)
1078 addr2
->m_addr
= (struct sockaddr
*) malloc(addr2
->m_len
);
1079 if (addr2
->m_addr
== NULL
)
1084 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1090 void GAddress_destroy(GAddress
*address
)
1092 assert(address
!= NULL
);
1094 if (address
->m_addr
)
1095 free(address
->m_addr
);
1100 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1102 assert(address
!= NULL
);
1104 address
->m_family
= type
;
1107 GAddressType
GAddress_GetFamily(GAddress
*address
)
1109 assert(address
!= NULL
);
1111 return address
->m_family
;
1114 GSocketError
_GAddress_translate_from(GAddress
*address
,
1115 struct sockaddr
*addr
, int len
)
1117 address
->m_realfamily
= addr
->sa_family
;
1118 switch (addr
->sa_family
)
1121 address
->m_family
= GSOCK_INET
;
1124 address
->m_family
= GSOCK_UNIX
;
1128 address
->m_family
= GSOCK_INET6
;
1133 address
->m_error
= GSOCK_INVOP
;
1138 if (address
->m_addr
)
1139 free(address
->m_addr
);
1141 address
->m_len
= len
;
1142 address
->m_addr
= (struct sockaddr
*) malloc(len
);
1144 if (address
->m_addr
== NULL
)
1146 address
->m_error
= GSOCK_MEMERR
;
1147 return GSOCK_MEMERR
;
1149 memcpy(address
->m_addr
, addr
, len
);
1151 return GSOCK_NOERROR
;
1154 GSocketError
_GAddress_translate_to(GAddress
*address
,
1155 struct sockaddr
**addr
, int *len
)
1157 if (!address
->m_addr
)
1159 address
->m_error
= GSOCK_INVADDR
;
1160 return GSOCK_INVADDR
;
1163 *len
= address
->m_len
;
1164 *addr
= (struct sockaddr
*) malloc(address
->m_len
);
1167 address
->m_error
= GSOCK_MEMERR
;
1168 return GSOCK_MEMERR
;
1171 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1172 return GSOCK_NOERROR
;
1176 * -------------------------------------------------------------------------
1177 * Internet address family
1178 * -------------------------------------------------------------------------
1181 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1183 address
->m_len
= sizeof(struct sockaddr_in
);
1184 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1185 if (address
->m_addr
== NULL
)
1187 address
->m_error
= GSOCK_MEMERR
;
1188 return GSOCK_MEMERR
;
1191 address
->m_family
= GSOCK_INET
;
1192 address
->m_realfamily
= PF_INET
;
1193 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1194 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1196 return GSOCK_NOERROR
;
1199 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1202 struct in_addr
*addr
;
1204 assert(address
!= NULL
);
1206 CHECK_ADDRESS(address
, INET
);
1208 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1210 addr
->s_addr
= inet_addr(hostname
);
1212 /* If it is a numeric host name, convert it now */
1213 if (addr
->s_addr
== INADDR_NONE
)
1215 struct in_addr
*array_addr
;
1217 /* It is a real name, we solve it */
1218 if ((he
= gethostbyname(hostname
)) == NULL
)
1220 /* addr->s_addr = INADDR_NONE just done by inet_addr() above */
1221 address
->m_error
= GSOCK_NOHOST
;
1222 return GSOCK_NOHOST
;
1224 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1225 addr
->s_addr
= array_addr
[0].s_addr
;
1227 return GSOCK_NOERROR
;
1230 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1232 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1235 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1236 unsigned long hostaddr
)
1238 struct in_addr
*addr
;
1240 assert(address
!= NULL
);
1242 CHECK_ADDRESS(address
, INET
);
1244 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1245 addr
->s_addr
= hostaddr
;
1247 return GSOCK_NOERROR
;
1250 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1251 const char *protocol
)
1254 struct sockaddr_in
*addr
;
1256 assert(address
!= NULL
);
1257 CHECK_ADDRESS(address
, INET
);
1261 address
->m_error
= GSOCK_INVPORT
;
1262 return GSOCK_INVPORT
;
1265 se
= getservbyname(port
, protocol
);
1268 if (isdigit(port
[0]))
1272 port_int
= atoi(port
);
1273 addr
= (struct sockaddr_in
*)address
->m_addr
;
1274 addr
->sin_port
= htons((u_short
) port_int
);
1275 return GSOCK_NOERROR
;
1278 address
->m_error
= GSOCK_INVPORT
;
1279 return GSOCK_INVPORT
;
1282 addr
= (struct sockaddr_in
*)address
->m_addr
;
1283 addr
->sin_port
= se
->s_port
;
1285 return GSOCK_NOERROR
;
1288 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1290 struct sockaddr_in
*addr
;
1292 assert(address
!= NULL
);
1293 CHECK_ADDRESS(address
, INET
);
1295 addr
= (struct sockaddr_in
*)address
->m_addr
;
1296 addr
->sin_port
= htons(port
);
1298 return GSOCK_NOERROR
;
1301 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1305 struct sockaddr_in
*addr
;
1307 assert(address
!= NULL
);
1308 CHECK_ADDRESS(address
, INET
);
1310 addr
= (struct sockaddr_in
*)address
->m_addr
;
1311 addr_buf
= (char *)&(addr
->sin_addr
);
1313 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1316 address
->m_error
= GSOCK_NOHOST
;
1317 return GSOCK_NOHOST
;
1320 strncpy(hostname
, he
->h_name
, sbuf
);
1322 return GSOCK_NOERROR
;
1325 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1327 struct sockaddr_in
*addr
;
1329 assert(address
!= NULL
);
1330 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1332 addr
= (struct sockaddr_in
*)address
->m_addr
;
1334 return addr
->sin_addr
.s_addr
;
1337 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1339 struct sockaddr_in
*addr
;
1341 assert(address
!= NULL
);
1342 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1344 addr
= (struct sockaddr_in
*)address
->m_addr
;
1345 return ntohs(addr
->sin_port
);
1349 * -------------------------------------------------------------------------
1350 * Unix address family
1351 * -------------------------------------------------------------------------
1354 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1356 assert (address
!= NULL
);
1357 address
->m_error
= GSOCK_INVADDR
;
1358 return GSOCK_INVADDR
;
1361 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1363 assert (address
!= NULL
);
1364 address
->m_error
= GSOCK_INVADDR
;
1365 return GSOCK_INVADDR
;
1368 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1370 assert (address
!= NULL
);
1371 address
->m_error
= GSOCK_INVADDR
;
1372 return GSOCK_INVADDR
;
1375 #else /* !wxUSE_SOCKETS */
1378 * Translation unit shouldn't be empty, so include this typedef to make the
1379 * compiler (VC++ 6.0, for example) happy
1381 typedef void (*wxDummy
)();
1383 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */