]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/gsocket.c
7417b834bc4fdbbc90132edb8d338831b3f0d2a0
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.
14 #ifndef __GSOCKET_STANDALONE__
18 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
20 #ifndef __GSOCKET_STANDALONE__
21 # include "wx/msw/gsockmsw.h"
22 # include "wx/gsocket.h"
24 # include "gsockmsw.h"
26 #endif /* __GSOCKET_STANDALONE__ */
28 /* redefine some GUI-only functions to do nothing in console mode */
29 #if defined(wxUSE_GUI) && !wxUSE_GUI
30 # define _GSocket_GUI_Init(socket) 1
31 # define _GSocket_GUI_Destroy(socket)
32 # define _GSocket_Enable_Events(socket)
33 # define _GSocket_Disable_Events(socket)
34 #endif /* wxUSE_GUI */
45 /* if we use configure for MSW SOCKLEN_T will be already defined */
47 # define SOCKLEN_T int
50 /* using FD_SET results in this warning */
52 # pragma warning(disable:4127) /* conditional expression is constant */
56 /* Constructors / Destructors for GSocket */
58 GSocket
*GSocket_new(void)
63 if ((socket
= (GSocket
*) malloc(sizeof(GSocket
))) == NULL
)
66 socket
->m_fd
= INVALID_SOCKET
;
67 for (i
= 0; i
< GSOCK_MAX_EVENT
; i
++)
69 socket
->m_cbacks
[i
] = NULL
;
71 socket
->m_detected
= 0;
72 socket
->m_local
= NULL
;
73 socket
->m_peer
= NULL
;
74 socket
->m_error
= GSOCK_NOERROR
;
75 socket
->m_server
= FALSE
;
76 socket
->m_stream
= TRUE
;
77 socket
->m_non_blocking
= FALSE
;
78 socket
->m_timeout
.tv_sec
= 10 * 60; /* 10 minutes */
79 socket
->m_timeout
.tv_usec
= 0;
80 socket
->m_establishing
= FALSE
;
82 /* Per-socket GUI-specific initialization */
83 success
= _GSocket_GUI_Init(socket
);
93 void GSocket_destroy(GSocket
*socket
)
95 assert(socket
!= NULL
);
97 /* Per-socket GUI-specific cleanup */
98 _GSocket_GUI_Destroy(socket
);
100 /* Check that the socket is really shutdowned */
101 if (socket
->m_fd
!= INVALID_SOCKET
)
102 GSocket_Shutdown(socket
);
104 /* Destroy private addresses */
106 GAddress_destroy(socket
->m_local
);
109 GAddress_destroy(socket
->m_peer
);
111 /* Destroy the socket itself */
116 * Disallow further read/write operations on this socket, close
117 * the fd and disable all callbacks.
119 void GSocket_Shutdown(GSocket
*socket
)
123 assert(socket
!= NULL
);
125 /* If socket has been created, shutdown it */
126 if (socket
->m_fd
!= INVALID_SOCKET
)
128 shutdown(socket
->m_fd
, 2);
129 closesocket(socket
->m_fd
);
130 socket
->m_fd
= INVALID_SOCKET
;
133 /* Disable GUI callbacks */
134 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
135 socket
->m_cbacks
[evt
] = NULL
;
137 socket
->m_detected
= GSOCK_LOST_FLAG
;
138 _GSocket_Disable_Events(socket
);
141 /* Address handling */
147 * Set or get the local or peer address for this socket. The 'set'
148 * functions return GSOCK_NOERROR on success, an error code otherwise.
149 * The 'get' functions return a pointer to a GAddress object on success,
150 * or NULL otherwise, in which case they set the error code of the
151 * corresponding GSocket.
154 * GSOCK_INVSOCK - the socket is not valid.
155 * GSOCK_INVADDR - the address is not valid.
157 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
159 assert(socket
!= NULL
);
161 /* the socket must be initialized, or it must be a server */
162 if (socket
->m_fd
!= INVALID_SOCKET
&& !socket
->m_server
)
164 socket
->m_error
= GSOCK_INVSOCK
;
165 return GSOCK_INVSOCK
;
169 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
171 socket
->m_error
= GSOCK_INVADDR
;
172 return GSOCK_INVADDR
;
176 GAddress_destroy(socket
->m_local
);
178 socket
->m_local
= GAddress_copy(address
);
180 return GSOCK_NOERROR
;
183 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
185 assert(socket
!= NULL
);
188 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
190 socket
->m_error
= GSOCK_INVADDR
;
191 return GSOCK_INVADDR
;
195 GAddress_destroy(socket
->m_peer
);
197 socket
->m_peer
= GAddress_copy(address
);
199 return GSOCK_NOERROR
;
202 GAddress
*GSocket_GetLocal(GSocket
*socket
)
205 struct sockaddr addr
;
206 SOCKLEN_T size
= sizeof(addr
);
209 assert(socket
!= NULL
);
211 /* try to get it from the m_local var first */
213 return GAddress_copy(socket
->m_local
);
215 /* else, if the socket is initialized, try getsockname */
216 if (socket
->m_fd
== INVALID_SOCKET
)
218 socket
->m_error
= GSOCK_INVSOCK
;
222 if (getsockname(socket
->m_fd
, &addr
, &size
) == SOCKET_ERROR
)
224 socket
->m_error
= GSOCK_IOERR
;
228 /* got a valid address from getsockname, create a GAddress object */
229 if ((address
= GAddress_new()) == NULL
)
231 socket
->m_error
= GSOCK_MEMERR
;
235 if ((err
= _GAddress_translate_from(address
, &addr
, size
)) != GSOCK_NOERROR
)
237 GAddress_destroy(address
);
238 socket
->m_error
= err
;
245 GAddress
*GSocket_GetPeer(GSocket
*socket
)
247 assert(socket
!= NULL
);
249 /* try to get it from the m_peer var */
251 return GAddress_copy(socket
->m_peer
);
256 /* Server specific parts */
258 /* GSocket_SetServer:
259 * Sets up this socket as a server. The local address must have been
260 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
261 * Returns GSOCK_NOERROR on success, one of the following otherwise:
264 * GSOCK_INVSOCK - the socket is in use.
265 * GSOCK_INVADDR - the local address has not been set.
266 * GSOCK_IOERR - low-level error.
268 GSocketError
GSocket_SetServer(GSocket
*sck
)
274 /* must not be in use */
275 if (sck
->m_fd
!= INVALID_SOCKET
)
277 sck
->m_error
= GSOCK_INVSOCK
;
278 return GSOCK_INVSOCK
;
281 /* the local addr must have been set */
284 sck
->m_error
= GSOCK_INVADDR
;
285 return GSOCK_INVADDR
;
288 /* Initialize all fields */
289 sck
->m_server
= TRUE
;
290 sck
->m_stream
= TRUE
;
291 sck
->m_oriented
= TRUE
;
293 /* Create the socket */
294 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
296 if (sck
->m_fd
== INVALID_SOCKET
)
298 sck
->m_error
= GSOCK_IOERR
;
302 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
303 _GSocket_Enable_Events(sck
);
305 /* Bind to the local address,
306 * retrieve the actual address bound,
307 * and listen up to 5 connections.
309 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
310 (getsockname(sck
->m_fd
,
311 sck
->m_local
->m_addr
,
312 (SOCKLEN_T
*)&sck
->m_local
->m_len
) != 0) ||
313 (listen(sck
->m_fd
, 5) != 0))
315 closesocket(sck
->m_fd
);
316 sck
->m_fd
= INVALID_SOCKET
;
317 sck
->m_error
= GSOCK_IOERR
;
321 return GSOCK_NOERROR
;
324 /* GSocket_WaitConnection:
325 * Waits for an incoming client connection. Returns a pointer to
326 * a GSocket object, or NULL if there was an error, in which case
327 * the last error field will be updated for the calling GSocket.
329 * Error codes (set in the calling GSocket)
330 * GSOCK_INVSOCK - the socket is not valid or not a server.
331 * GSOCK_TIMEDOUT - timeout, no incoming connections.
332 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
333 * GSOCK_MEMERR - couldn't allocate memory.
334 * GSOCK_IOERR - low-level error.
336 GSocket
*GSocket_WaitConnection(GSocket
*sck
)
339 struct sockaddr from
;
340 SOCKLEN_T fromlen
= sizeof(from
);
346 /* Reenable CONNECTION events */
347 sck
->m_detected
&= ~GSOCK_CONNECTION_FLAG
;
349 /* If the socket has already been created, we exit immediately */
350 if (sck
->m_fd
== INVALID_SOCKET
|| !sck
->m_server
)
352 sck
->m_error
= GSOCK_INVSOCK
;
356 /* Create a GSocket object for the new connection */
357 connection
= GSocket_new();
361 sck
->m_error
= GSOCK_MEMERR
;
365 /* Wait for a connection (with timeout) */
366 if (_GSocket_Input_Timeout(sck
) == GSOCK_TIMEDOUT
)
368 GSocket_destroy(connection
);
369 /* sck->m_error set by _GSocket_Input_Timeout */
373 connection
->m_fd
= accept(sck
->m_fd
, &from
, &fromlen
);
375 if (connection
->m_fd
== INVALID_SOCKET
)
377 if (WSAGetLastError() == WSAEWOULDBLOCK
)
378 sck
->m_error
= GSOCK_WOULDBLOCK
;
380 sck
->m_error
= GSOCK_IOERR
;
382 GSocket_destroy(connection
);
386 /* Initialize all fields */
387 connection
->m_server
= FALSE
;
388 connection
->m_stream
= TRUE
;
389 connection
->m_oriented
= TRUE
;
391 /* Setup the peer address field */
392 connection
->m_peer
= GAddress_new();
393 if (!connection
->m_peer
)
395 GSocket_destroy(connection
);
396 sck
->m_error
= GSOCK_MEMERR
;
399 err
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
);
400 if (err
!= GSOCK_NOERROR
)
402 GAddress_destroy(connection
->m_peer
);
403 GSocket_destroy(connection
);
408 ioctlsocket(connection
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
409 _GSocket_Enable_Events(connection
);
414 /* Client specific parts */
417 * For stream (connection oriented) sockets, GSocket_Connect() tries
418 * to establish a client connection to a server using the peer address
419 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
420 * connection has been succesfully established, or one of the error
421 * codes listed below. Note that for nonblocking sockets, a return
422 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
423 * request can be completed later; you should use GSocket_Select()
424 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
425 * corresponding asynchronous events.
427 * For datagram (non connection oriented) sockets, GSocket_Connect()
428 * just sets the peer address established with GSocket_SetPeer() as
429 * default destination.
432 * GSOCK_INVSOCK - the socket is in use or not valid.
433 * GSOCK_INVADDR - the peer address has not been established.
434 * GSOCK_TIMEDOUT - timeout, the connection failed.
435 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
436 * GSOCK_MEMERR - couldn't allocate memory.
437 * GSOCK_IOERR - low-level error.
439 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
446 /* Enable CONNECTION events (needed for nonblocking connections) */
447 sck
->m_detected
&= ~GSOCK_CONNECTION_FLAG
;
449 if (sck
->m_fd
!= INVALID_SOCKET
)
451 sck
->m_error
= GSOCK_INVSOCK
;
452 return GSOCK_INVSOCK
;
457 sck
->m_error
= GSOCK_INVADDR
;
458 return GSOCK_INVADDR
;
461 /* Streamed or dgram socket? */
462 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
463 sck
->m_oriented
= TRUE
;
464 sck
->m_server
= FALSE
;
465 sck
->m_establishing
= FALSE
;
467 /* Create the socket */
468 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
,
469 sck
->m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
471 if (sck
->m_fd
== INVALID_SOCKET
)
473 sck
->m_error
= GSOCK_IOERR
;
477 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
478 _GSocket_Enable_Events(sck
);
480 /* Connect it to the peer address, with a timeout (see below) */
481 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
483 if (ret
== SOCKET_ERROR
)
485 err
= WSAGetLastError();
487 /* If connect failed with EWOULDBLOCK and the GSocket object
488 * is in blocking mode, we select() for the specified timeout
489 * checking for writability to see if the connection request
492 if ((err
== WSAEWOULDBLOCK
) && (!sck
->m_non_blocking
))
494 err
= _GSocket_Connect_Timeout(sck
);
496 if (err
!= GSOCK_NOERROR
)
498 closesocket(sck
->m_fd
);
499 sck
->m_fd
= INVALID_SOCKET
;
500 /* sck->m_error is set in _GSocket_Connect_Timeout */
506 /* If connect failed with EWOULDBLOCK and the GSocket object
507 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
508 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
509 * this way if the connection completes, a GSOCK_CONNECTION
510 * event will be generated, if enabled.
512 if ((err
== WSAEWOULDBLOCK
) && (sck
->m_non_blocking
))
514 sck
->m_establishing
= TRUE
;
515 sck
->m_error
= GSOCK_WOULDBLOCK
;
516 return GSOCK_WOULDBLOCK
;
519 /* If connect failed with an error other than EWOULDBLOCK,
520 * then the call to GSocket_Connect() has failed.
522 closesocket(sck
->m_fd
);
523 sck
->m_fd
= INVALID_SOCKET
;
524 sck
->m_error
= GSOCK_IOERR
;
528 return GSOCK_NOERROR
;
531 /* Datagram sockets */
533 /* GSocket_SetNonOriented:
534 * Sets up this socket as a non-connection oriented (datagram) socket.
535 * Before using this function, the local address must have been set
536 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
537 * on success, or one of the following otherwise.
540 * GSOCK_INVSOCK - the socket is in use.
541 * GSOCK_INVADDR - the local address has not been set.
542 * GSOCK_IOERR - low-level error.
544 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
550 if (sck
->m_fd
!= INVALID_SOCKET
)
552 sck
->m_error
= GSOCK_INVSOCK
;
553 return GSOCK_INVSOCK
;
558 sck
->m_error
= GSOCK_INVADDR
;
559 return GSOCK_INVADDR
;
562 /* Initialize all fields */
563 sck
->m_stream
= FALSE
;
564 sck
->m_server
= FALSE
;
565 sck
->m_oriented
= FALSE
;
567 /* Create the socket */
568 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
570 if (sck
->m_fd
== INVALID_SOCKET
)
572 sck
->m_error
= GSOCK_IOERR
;
576 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
577 _GSocket_Enable_Events(sck
);
579 /* Bind to the local address,
580 * and retrieve the actual address bound.
582 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
583 (getsockname(sck
->m_fd
,
584 sck
->m_local
->m_addr
,
585 (SOCKLEN_T
*)&sck
->m_local
->m_len
) != 0))
587 closesocket(sck
->m_fd
);
588 sck
->m_fd
= INVALID_SOCKET
;
589 sck
->m_error
= GSOCK_IOERR
;
593 return GSOCK_NOERROR
;
598 /* Like recv(), send(), ... */
599 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
603 assert(socket
!= NULL
);
605 /* Reenable INPUT events */
606 socket
->m_detected
&= ~GSOCK_INPUT_FLAG
;
608 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
610 socket
->m_error
= GSOCK_INVSOCK
;
614 /* If the socket is blocking, wait for data (with a timeout) */
615 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
619 if (socket
->m_stream
)
620 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
622 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
624 if (ret
== SOCKET_ERROR
)
626 if (WSAGetLastError() != WSAEWOULDBLOCK
)
627 socket
->m_error
= GSOCK_IOERR
;
629 socket
->m_error
= GSOCK_WOULDBLOCK
;
636 int GSocket_Write(GSocket
*socket
, const char *buffer
, int size
)
640 assert(socket
!= NULL
);
642 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
644 socket
->m_error
= GSOCK_INVSOCK
;
648 /* If the socket is blocking, wait for writability (with a timeout) */
649 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
653 if (socket
->m_stream
)
654 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
656 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
658 if (ret
== SOCKET_ERROR
)
660 if (WSAGetLastError() != WSAEWOULDBLOCK
)
661 socket
->m_error
= GSOCK_IOERR
;
663 socket
->m_error
= GSOCK_WOULDBLOCK
;
665 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
666 * does). Once the first OUTPUT event is received, users can assume
667 * that the socket is writable until a read operation fails. Only then
668 * will further OUTPUT events be posted.
670 socket
->m_detected
&= ~GSOCK_OUTPUT_FLAG
;
678 * Polls the socket to determine its status. This function will
679 * check for the events specified in the 'flags' parameter, and
680 * it will return a mask indicating which operations can be
681 * performed. This function won't block, regardless of the
682 * mode (blocking | nonblocking) of the socket.
684 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
686 #if defined(wxUSE_GUI) && !wxUSE_GUI
688 GSocketEventFlags result
= 0;
692 static const struct timeval tv
= { 0, 0 };
694 assert(socket
!= NULL
);
699 FD_SET(socket
->m_fd
, &readfds
);
700 FD_SET(socket
->m_fd
, &writefds
);
701 FD_SET(socket
->m_fd
, &exceptfds
);
703 /* Check known state first */
704 result
|= (GSOCK_CONNECTION_FLAG
& socket
->m_detected
& flags
);
705 result
|= (GSOCK_LOST_FLAG
& socket
->m_detected
& flags
);
708 if (select(socket
->m_fd
+ 1, &readfds
, &writefds
, &exceptfds
, &tv
) <= 0)
711 /* Check for readability */
712 if (FD_ISSET(socket
->m_fd
, &readfds
))
714 /* Assume that closure of the socket is always reported via exceptfds */
715 if (socket
->m_server
&& socket
->m_stream
)
716 result
|= (GSOCK_CONNECTION_FLAG
& flags
);
718 result
|= (GSOCK_INPUT_FLAG
& flags
);
721 /* Check for writability */
722 if (FD_ISSET(socket
->m_fd
, &writefds
))
724 if (socket
->m_establishing
&& !socket
->m_server
)
726 result
|= (GSOCK_CONNECTION_FLAG
& flags
);
727 socket
->m_establishing
= FALSE
;
728 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
731 result
|= (GSOCK_OUTPUT_FLAG
& flags
);
734 /* Check for exceptions and errors */
735 if (FD_ISSET(socket
->m_fd
, &exceptfds
))
737 result
|= (GSOCK_LOST_FLAG
& flags
);
738 socket
->m_establishing
= FALSE
;
739 socket
->m_detected
= GSOCK_LOST_FLAG
;
746 assert(socket
!= NULL
);
747 return flags
& socket
->m_detected
;
749 #endif /* !wxUSE_GUI */
754 /* GSocket_SetNonBlocking:
755 * Sets the socket to non-blocking mode. All IO calls will return
758 void GSocket_SetNonBlocking(GSocket
*socket
, bool non_block
)
760 assert(socket
!= NULL
);
762 socket
->m_non_blocking
= non_block
;
765 /* GSocket_SetTimeout:
766 * Sets the timeout for blocking calls. Time is expressed in
769 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millis
)
771 assert(socket
!= NULL
);
773 socket
->m_timeout
.tv_sec
= (millis
/ 1000);
774 socket
->m_timeout
.tv_usec
= (millis
% 1000) * 1000;
778 * Returns the last error occured for this socket. Note that successful
779 * operations do not clear this back to GSOCK_NOERROR, so use it only
782 GSocketError
GSocket_GetError(GSocket
*socket
)
784 assert(socket
!= NULL
);
786 return socket
->m_error
;
792 * There is data to be read in the input buffer. If, after a read
793 * operation, there is still data available, the callback function will
796 * The socket is available for writing. That is, the next write call
797 * won't block. This event is generated only once, when the connection is
798 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
799 * when the output buffer empties again. This means that the app should
800 * assume that it can write since the first OUTPUT event, and no more
801 * OUTPUT events will be generated unless an error occurs.
803 * Connection succesfully established, for client sockets, or incoming
804 * client connection, for server sockets. Wait for this event (also watch
805 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
807 * The connection is lost (or a connection request failed); this could
808 * be due to a failure, or due to the peer closing it gracefully.
811 /* GSocket_SetCallback:
812 * Enables the callbacks specified by 'flags'. Note that 'flags'
813 * may be a combination of flags OR'ed toghether, so the same
814 * callback function can be made to accept different events.
815 * The callback function must have the following prototype:
817 * void function(GSocket *socket, GSocketEvent event, char *cdata)
819 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
820 GSocketCallback callback
, char *cdata
)
824 assert(socket
!= NULL
);
826 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
828 if ((flags
& (1 << count
)) != 0)
830 socket
->m_cbacks
[count
] = callback
;
831 socket
->m_data
[count
] = cdata
;
836 /* GSocket_UnsetCallback:
837 * Disables all callbacks specified by 'flags', which may be a
838 * combination of flags OR'ed toghether.
840 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
844 assert(socket
!= NULL
);
846 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
848 if ((flags
& (1 << count
)) != 0)
850 socket
->m_cbacks
[count
] = NULL
;
851 socket
->m_data
[count
] = NULL
;
858 /* _GSocket_Input_Timeout:
859 * For blocking sockets, wait until data is available or
860 * until timeout ellapses.
862 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
866 if (!socket
->m_non_blocking
)
869 FD_SET(socket
->m_fd
, &readfds
);
870 if (select(0, &readfds
, NULL
, NULL
, &socket
->m_timeout
) == 0)
872 socket
->m_error
= GSOCK_TIMEDOUT
;
873 return GSOCK_TIMEDOUT
;
876 return GSOCK_NOERROR
;
879 /* _GSocket_Output_Timeout:
880 * For blocking sockets, wait until data can be sent without
881 * blocking or until timeout ellapses.
883 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
887 if (!socket
->m_non_blocking
)
890 FD_SET(socket
->m_fd
, &writefds
);
891 if (select(0, NULL
, &writefds
, NULL
, &socket
->m_timeout
) == 0)
893 socket
->m_error
= GSOCK_TIMEDOUT
;
894 return GSOCK_TIMEDOUT
;
897 return GSOCK_NOERROR
;
900 /* _GSocket_Connect_Timeout:
901 * For blocking sockets, wait until the connection is
902 * established or fails, or until timeout ellapses.
904 GSocketError
_GSocket_Connect_Timeout(GSocket
*socket
)
911 FD_SET(socket
->m_fd
, &writefds
);
912 FD_SET(socket
->m_fd
, &exceptfds
);
913 if (select(0, NULL
, &writefds
, &exceptfds
, &socket
->m_timeout
) == 0)
915 socket
->m_error
= GSOCK_TIMEDOUT
;
916 return GSOCK_TIMEDOUT
;
918 if (!FD_ISSET(socket
->m_fd
, &writefds
))
920 socket
->m_error
= GSOCK_IOERR
;
924 return GSOCK_NOERROR
;
927 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
929 return recv(socket
->m_fd
, buffer
, size
, 0);
932 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
934 struct sockaddr from
;
935 SOCKLEN_T fromlen
= sizeof(from
);
939 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
941 if (ret
== SOCKET_ERROR
)
944 /* Translate a system address into a GSocket address */
947 socket
->m_peer
= GAddress_new();
950 socket
->m_error
= GSOCK_MEMERR
;
954 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
955 if (err
!= GSOCK_NOERROR
)
957 GAddress_destroy(socket
->m_peer
);
958 socket
->m_peer
= NULL
;
959 socket
->m_error
= err
;
966 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
968 return send(socket
->m_fd
, buffer
, size
, 0);
971 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
973 struct sockaddr
*addr
;
979 socket
->m_error
= GSOCK_INVADDR
;
983 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
984 if (err
!= GSOCK_NOERROR
)
986 socket
->m_error
= err
;
990 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
992 /* Frees memory allocated by _GAddress_translate_to */
1000 * -------------------------------------------------------------------------
1002 * -------------------------------------------------------------------------
1005 /* CHECK_ADDRESS verifies that the current family is either GSOCK_NOFAMILY
1006 * or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it initalizes address
1007 * to be a GSOCK_*family*. In other cases, it returns GSOCK_INVADDR.
1009 #define CHECK_ADDRESS(address, family, retval) \
1011 if (address->m_family == GSOCK_NOFAMILY) \
1012 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1013 return address->m_error; \
1014 if (address->m_family != GSOCK_##family) \
1016 address->m_error = GSOCK_INVADDR; \
1021 GAddress
*GAddress_new(void)
1025 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1028 address
->m_family
= GSOCK_NOFAMILY
;
1029 address
->m_addr
= NULL
;
1035 GAddress
*GAddress_copy(GAddress
*address
)
1039 assert(address
!= NULL
);
1041 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1044 memcpy(addr2
, address
, sizeof(GAddress
));
1046 if (address
->m_addr
)
1048 addr2
->m_addr
= (struct sockaddr
*) malloc(addr2
->m_len
);
1049 if (addr2
->m_addr
== NULL
)
1054 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1060 void GAddress_destroy(GAddress
*address
)
1062 assert(address
!= NULL
);
1067 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1069 assert(address
!= NULL
);
1071 address
->m_family
= type
;
1074 GAddressType
GAddress_GetFamily(GAddress
*address
)
1076 assert(address
!= NULL
);
1078 return address
->m_family
;
1081 GSocketError
_GAddress_translate_from(GAddress
*address
,
1082 struct sockaddr
*addr
, int len
)
1084 address
->m_realfamily
= addr
->sa_family
;
1085 switch (addr
->sa_family
)
1088 address
->m_family
= GSOCK_INET
;
1091 address
->m_family
= GSOCK_UNIX
;
1095 address
->m_family
= GSOCK_INET6
;
1100 address
->m_error
= GSOCK_INVOP
;
1105 if (address
->m_addr
)
1106 free(address
->m_addr
);
1108 address
->m_len
= len
;
1109 address
->m_addr
= (struct sockaddr
*) malloc(len
);
1111 if (address
->m_addr
== NULL
)
1113 address
->m_error
= GSOCK_MEMERR
;
1114 return GSOCK_MEMERR
;
1116 memcpy(address
->m_addr
, addr
, len
);
1118 return GSOCK_NOERROR
;
1121 GSocketError
_GAddress_translate_to(GAddress
*address
,
1122 struct sockaddr
**addr
, int *len
)
1124 if (!address
->m_addr
)
1126 address
->m_error
= GSOCK_INVADDR
;
1127 return GSOCK_INVADDR
;
1130 *len
= address
->m_len
;
1131 *addr
= (struct sockaddr
*) malloc(address
->m_len
);
1134 address
->m_error
= GSOCK_MEMERR
;
1135 return GSOCK_MEMERR
;
1138 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1139 return GSOCK_NOERROR
;
1143 * -------------------------------------------------------------------------
1144 * Internet address family
1145 * -------------------------------------------------------------------------
1148 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1150 address
->m_len
= sizeof(struct sockaddr_in
);
1151 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1152 if (address
->m_addr
== NULL
)
1154 address
->m_error
= GSOCK_MEMERR
;
1155 return GSOCK_MEMERR
;
1158 address
->m_family
= GSOCK_INET
;
1159 address
->m_realfamily
= PF_INET
;
1160 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1161 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1163 return GSOCK_NOERROR
;
1166 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1169 struct in_addr
*addr
;
1171 assert(address
!= NULL
);
1173 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1175 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1177 addr
->s_addr
= inet_addr(hostname
);
1179 /* If it is a numeric host name, convert it now */
1180 if (addr
->s_addr
== INADDR_NONE
)
1182 struct in_addr
*array_addr
;
1184 /* It is a real name, we solve it */
1185 if ((he
= gethostbyname(hostname
)) == NULL
)
1187 /* addr->s_addr = INADDR_NONE just done by inet_addr() above */
1188 address
->m_error
= GSOCK_NOHOST
;
1189 return GSOCK_NOHOST
;
1191 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1192 addr
->s_addr
= array_addr
[0].s_addr
;
1194 return GSOCK_NOERROR
;
1197 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1199 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1202 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1203 unsigned long hostaddr
)
1205 struct in_addr
*addr
;
1207 assert(address
!= NULL
);
1209 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1211 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1212 addr
->s_addr
= hostaddr
;
1214 return GSOCK_NOERROR
;
1217 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1218 const char *protocol
)
1221 struct sockaddr_in
*addr
;
1223 assert(address
!= NULL
);
1224 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1228 address
->m_error
= GSOCK_INVPORT
;
1229 return GSOCK_INVPORT
;
1232 se
= getservbyname(port
, protocol
);
1235 if (isdigit(port
[0]))
1239 port_int
= atoi(port
);
1240 addr
= (struct sockaddr_in
*)address
->m_addr
;
1241 addr
->sin_port
= htons((u_short
) port_int
);
1242 return GSOCK_NOERROR
;
1245 address
->m_error
= GSOCK_INVPORT
;
1246 return GSOCK_INVPORT
;
1249 addr
= (struct sockaddr_in
*)address
->m_addr
;
1250 addr
->sin_port
= se
->s_port
;
1252 return GSOCK_NOERROR
;
1255 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1257 struct sockaddr_in
*addr
;
1259 assert(address
!= NULL
);
1260 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1262 addr
= (struct sockaddr_in
*)address
->m_addr
;
1263 addr
->sin_port
= htons(port
);
1265 return GSOCK_NOERROR
;
1268 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1272 struct sockaddr_in
*addr
;
1274 assert(address
!= NULL
);
1275 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1277 addr
= (struct sockaddr_in
*)address
->m_addr
;
1278 addr_buf
= (char *)&(addr
->sin_addr
);
1280 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1283 address
->m_error
= GSOCK_NOHOST
;
1284 return GSOCK_NOHOST
;
1287 strncpy(hostname
, he
->h_name
, sbuf
);
1289 return GSOCK_NOERROR
;
1292 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1294 struct sockaddr_in
*addr
;
1296 assert(address
!= NULL
);
1297 CHECK_ADDRESS(address
, INET
, 0);
1299 addr
= (struct sockaddr_in
*)address
->m_addr
;
1301 return addr
->sin_addr
.s_addr
;
1304 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1306 struct sockaddr_in
*addr
;
1308 assert(address
!= NULL
);
1309 CHECK_ADDRESS(address
, INET
, 0);
1311 addr
= (struct sockaddr_in
*)address
->m_addr
;
1312 return ntohs(addr
->sin_port
);
1316 * -------------------------------------------------------------------------
1317 * Unix address family
1318 * -------------------------------------------------------------------------
1322 #pragma warning(disable:4100) /* unreferenced formal parameter */
1323 #endif /* Visual C++ */
1325 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1327 assert (address
!= NULL
);
1328 address
->m_error
= GSOCK_INVADDR
;
1329 return GSOCK_INVADDR
;
1332 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1334 assert (address
!= NULL
);
1335 address
->m_error
= GSOCK_INVADDR
;
1336 return GSOCK_INVADDR
;
1339 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1341 assert (address
!= NULL
);
1342 address
->m_error
= GSOCK_INVADDR
;
1343 return GSOCK_INVADDR
;
1346 #else /* !wxUSE_SOCKETS */
1349 * Translation unit shouldn't be empty, so include this typedef to make the
1350 * compiler (VC++ 6.0, for example) happy
1352 typedef (*wxDummy
)();
1354 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */