]>
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 */
64 /* if we use configure for MSW SOCKLEN_T will be already defined */
66 # define SOCKLEN_T int
70 /* Constructors / Destructors for GSocket */
72 GSocket
*GSocket_new(void)
77 if ((socket
= (GSocket
*) malloc(sizeof(GSocket
))) == NULL
)
80 socket
->m_fd
= INVALID_SOCKET
;
81 for (i
= 0; i
< GSOCK_MAX_EVENT
; i
++)
83 socket
->m_cbacks
[i
] = NULL
;
85 socket
->m_detected
= 0;
86 socket
->m_local
= NULL
;
87 socket
->m_peer
= NULL
;
88 socket
->m_error
= GSOCK_NOERROR
;
89 socket
->m_server
= FALSE
;
90 socket
->m_stream
= TRUE
;
91 socket
->m_non_blocking
= FALSE
;
92 socket
->m_timeout
.tv_sec
= 10 * 60; /* 10 minutes */
93 socket
->m_timeout
.tv_usec
= 0;
94 socket
->m_establishing
= FALSE
;
96 /* Per-socket GUI-specific initialization */
97 success
= _GSocket_GUI_Init(socket
);
107 void GSocket_destroy(GSocket
*socket
)
109 assert(socket
!= NULL
);
111 /* Per-socket GUI-specific cleanup */
112 _GSocket_GUI_Destroy(socket
);
114 /* Check that the socket is really shutdowned */
115 if (socket
->m_fd
!= INVALID_SOCKET
)
116 GSocket_Shutdown(socket
);
118 /* Destroy private addresses */
120 GAddress_destroy(socket
->m_local
);
123 GAddress_destroy(socket
->m_peer
);
125 /* Destroy the socket itself */
130 * Disallow further read/write operations on this socket, close
131 * the fd and disable all callbacks.
133 void GSocket_Shutdown(GSocket
*socket
)
137 assert(socket
!= NULL
);
139 /* If socket has been created, shutdown it */
140 if (socket
->m_fd
!= INVALID_SOCKET
)
142 shutdown(socket
->m_fd
, 2);
143 closesocket(socket
->m_fd
);
144 socket
->m_fd
= INVALID_SOCKET
;
147 /* Disable GUI callbacks */
148 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
149 socket
->m_cbacks
[evt
] = NULL
;
151 socket
->m_detected
= GSOCK_LOST_FLAG
;
152 _GSocket_Disable_Events(socket
);
155 /* Address handling */
161 * Set or get the local or peer address for this socket. The 'set'
162 * functions return GSOCK_NOERROR on success, an error code otherwise.
163 * The 'get' functions return a pointer to a GAddress object on success,
164 * or NULL otherwise, in which case they set the error code of the
165 * corresponding GSocket.
168 * GSOCK_INVSOCK - the socket is not valid.
169 * GSOCK_INVADDR - the address is not valid.
171 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
173 assert(socket
!= NULL
);
175 /* the socket must be initialized, or it must be a server */
176 if (socket
->m_fd
!= INVALID_SOCKET
&& !socket
->m_server
)
178 socket
->m_error
= GSOCK_INVSOCK
;
179 return GSOCK_INVSOCK
;
183 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
185 socket
->m_error
= GSOCK_INVADDR
;
186 return GSOCK_INVADDR
;
190 GAddress_destroy(socket
->m_local
);
192 socket
->m_local
= GAddress_copy(address
);
194 return GSOCK_NOERROR
;
197 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
199 assert(socket
!= NULL
);
202 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
204 socket
->m_error
= GSOCK_INVADDR
;
205 return GSOCK_INVADDR
;
209 GAddress_destroy(socket
->m_peer
);
211 socket
->m_peer
= GAddress_copy(address
);
213 return GSOCK_NOERROR
;
216 GAddress
*GSocket_GetLocal(GSocket
*socket
)
219 struct sockaddr addr
;
220 SOCKLEN_T size
= sizeof(addr
);
223 assert(socket
!= NULL
);
225 /* try to get it from the m_local var first */
227 return GAddress_copy(socket
->m_local
);
229 /* else, if the socket is initialized, try getsockname */
230 if (socket
->m_fd
== INVALID_SOCKET
)
232 socket
->m_error
= GSOCK_INVSOCK
;
236 if (getsockname(socket
->m_fd
, &addr
, &size
) == SOCKET_ERROR
)
238 socket
->m_error
= GSOCK_IOERR
;
242 /* got a valid address from getsockname, create a GAddress object */
243 if ((address
= GAddress_new()) == NULL
)
245 socket
->m_error
= GSOCK_MEMERR
;
249 if ((err
= _GAddress_translate_from(address
, &addr
, size
)) != GSOCK_NOERROR
)
251 GAddress_destroy(address
);
252 socket
->m_error
= err
;
259 GAddress
*GSocket_GetPeer(GSocket
*socket
)
261 assert(socket
!= NULL
);
263 /* try to get it from the m_peer var */
265 return GAddress_copy(socket
->m_peer
);
270 /* Server specific parts */
272 /* GSocket_SetServer:
273 * Sets up this socket as a server. The local address must have been
274 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
275 * Returns GSOCK_NOERROR on success, one of the following otherwise:
278 * GSOCK_INVSOCK - the socket is in use.
279 * GSOCK_INVADDR - the local address has not been set.
280 * GSOCK_IOERR - low-level error.
282 GSocketError
GSocket_SetServer(GSocket
*sck
)
288 /* must not be in use */
289 if (sck
->m_fd
!= INVALID_SOCKET
)
291 sck
->m_error
= GSOCK_INVSOCK
;
292 return GSOCK_INVSOCK
;
295 /* the local addr must have been set */
298 sck
->m_error
= GSOCK_INVADDR
;
299 return GSOCK_INVADDR
;
302 /* Initialize all fields */
303 sck
->m_server
= TRUE
;
304 sck
->m_stream
= TRUE
;
305 sck
->m_oriented
= TRUE
;
307 /* Create the socket */
308 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
310 if (sck
->m_fd
== INVALID_SOCKET
)
312 sck
->m_error
= GSOCK_IOERR
;
316 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
317 _GSocket_Enable_Events(sck
);
319 /* Bind to the local address,
320 * retrieve the actual address bound,
321 * and listen up to 5 connections.
323 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
324 (getsockname(sck
->m_fd
,
325 sck
->m_local
->m_addr
,
326 (SOCKLEN_T
*)&sck
->m_local
->m_len
) != 0) ||
327 (listen(sck
->m_fd
, 5) != 0))
329 closesocket(sck
->m_fd
);
330 sck
->m_fd
= INVALID_SOCKET
;
331 sck
->m_error
= GSOCK_IOERR
;
335 return GSOCK_NOERROR
;
338 /* GSocket_WaitConnection:
339 * Waits for an incoming client connection. Returns a pointer to
340 * a GSocket object, or NULL if there was an error, in which case
341 * the last error field will be updated for the calling GSocket.
343 * Error codes (set in the calling GSocket)
344 * GSOCK_INVSOCK - the socket is not valid or not a server.
345 * GSOCK_TIMEDOUT - timeout, no incoming connections.
346 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
347 * GSOCK_MEMERR - couldn't allocate memory.
348 * GSOCK_IOERR - low-level error.
350 GSocket
*GSocket_WaitConnection(GSocket
*sck
)
353 struct sockaddr from
;
354 SOCKLEN_T fromlen
= sizeof(from
);
360 /* Reenable CONNECTION events */
361 sck
->m_detected
&= ~GSOCK_CONNECTION_FLAG
;
363 /* If the socket has already been created, we exit immediately */
364 if (sck
->m_fd
== INVALID_SOCKET
|| !sck
->m_server
)
366 sck
->m_error
= GSOCK_INVSOCK
;
370 /* Create a GSocket object for the new connection */
371 connection
= GSocket_new();
375 sck
->m_error
= GSOCK_MEMERR
;
379 /* Wait for a connection (with timeout) */
380 if (_GSocket_Input_Timeout(sck
) == GSOCK_TIMEDOUT
)
382 GSocket_destroy(connection
);
383 /* sck->m_error set by _GSocket_Input_Timeout */
387 connection
->m_fd
= accept(sck
->m_fd
, &from
, &fromlen
);
389 if (connection
->m_fd
== INVALID_SOCKET
)
391 if (WSAGetLastError() == WSAEWOULDBLOCK
)
392 sck
->m_error
= GSOCK_WOULDBLOCK
;
394 sck
->m_error
= GSOCK_IOERR
;
396 GSocket_destroy(connection
);
400 /* Initialize all fields */
401 connection
->m_server
= FALSE
;
402 connection
->m_stream
= TRUE
;
403 connection
->m_oriented
= TRUE
;
405 /* Setup the peer address field */
406 connection
->m_peer
= GAddress_new();
407 if (!connection
->m_peer
)
409 GSocket_destroy(connection
);
410 sck
->m_error
= GSOCK_MEMERR
;
413 err
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
);
414 if (err
!= GSOCK_NOERROR
)
416 GAddress_destroy(connection
->m_peer
);
417 GSocket_destroy(connection
);
422 ioctlsocket(connection
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
423 _GSocket_Enable_Events(connection
);
428 /* Client specific parts */
431 * For stream (connection oriented) sockets, GSocket_Connect() tries
432 * to establish a client connection to a server using the peer address
433 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
434 * connection has been succesfully established, or one of the error
435 * codes listed below. Note that for nonblocking sockets, a return
436 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
437 * request can be completed later; you should use GSocket_Select()
438 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
439 * corresponding asynchronous events.
441 * For datagram (non connection oriented) sockets, GSocket_Connect()
442 * just sets the peer address established with GSocket_SetPeer() as
443 * default destination.
446 * GSOCK_INVSOCK - the socket is in use or not valid.
447 * GSOCK_INVADDR - the peer address has not been established.
448 * GSOCK_TIMEDOUT - timeout, the connection failed.
449 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
450 * GSOCK_MEMERR - couldn't allocate memory.
451 * GSOCK_IOERR - low-level error.
453 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
460 /* Enable CONNECTION events (needed for nonblocking connections) */
461 sck
->m_detected
&= ~GSOCK_CONNECTION_FLAG
;
463 if (sck
->m_fd
!= INVALID_SOCKET
)
465 sck
->m_error
= GSOCK_INVSOCK
;
466 return GSOCK_INVSOCK
;
471 sck
->m_error
= GSOCK_INVADDR
;
472 return GSOCK_INVADDR
;
475 /* Streamed or dgram socket? */
476 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
477 sck
->m_oriented
= TRUE
;
478 sck
->m_server
= FALSE
;
479 sck
->m_establishing
= FALSE
;
481 /* Create the socket */
482 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
,
483 sck
->m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
485 if (sck
->m_fd
== INVALID_SOCKET
)
487 sck
->m_error
= GSOCK_IOERR
;
491 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
492 _GSocket_Enable_Events(sck
);
494 /* Connect it to the peer address, with a timeout (see below) */
495 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
497 if (ret
== SOCKET_ERROR
)
499 err
= WSAGetLastError();
501 /* If connect failed with EWOULDBLOCK and the GSocket object
502 * is in blocking mode, we select() for the specified timeout
503 * checking for writability to see if the connection request
506 if ((err
== WSAEWOULDBLOCK
) && (!sck
->m_non_blocking
))
508 err
= _GSocket_Connect_Timeout(sck
);
510 if (err
!= GSOCK_NOERROR
)
512 closesocket(sck
->m_fd
);
513 sck
->m_fd
= INVALID_SOCKET
;
514 /* sck->m_error is set in _GSocket_Connect_Timeout */
520 /* If connect failed with EWOULDBLOCK and the GSocket object
521 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
522 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
523 * this way if the connection completes, a GSOCK_CONNECTION
524 * event will be generated, if enabled.
526 if ((err
== WSAEWOULDBLOCK
) && (sck
->m_non_blocking
))
528 sck
->m_establishing
= TRUE
;
529 sck
->m_error
= GSOCK_WOULDBLOCK
;
530 return GSOCK_WOULDBLOCK
;
533 /* If connect failed with an error other than EWOULDBLOCK,
534 * then the call to GSocket_Connect() has failed.
536 closesocket(sck
->m_fd
);
537 sck
->m_fd
= INVALID_SOCKET
;
538 sck
->m_error
= GSOCK_IOERR
;
542 return GSOCK_NOERROR
;
545 /* Datagram sockets */
547 /* GSocket_SetNonOriented:
548 * Sets up this socket as a non-connection oriented (datagram) socket.
549 * Before using this function, the local address must have been set
550 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
551 * on success, or one of the following otherwise.
554 * GSOCK_INVSOCK - the socket is in use.
555 * GSOCK_INVADDR - the local address has not been set.
556 * GSOCK_IOERR - low-level error.
558 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
564 if (sck
->m_fd
!= INVALID_SOCKET
)
566 sck
->m_error
= GSOCK_INVSOCK
;
567 return GSOCK_INVSOCK
;
572 sck
->m_error
= GSOCK_INVADDR
;
573 return GSOCK_INVADDR
;
576 /* Initialize all fields */
577 sck
->m_stream
= FALSE
;
578 sck
->m_server
= FALSE
;
579 sck
->m_oriented
= FALSE
;
581 /* Create the socket */
582 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
584 if (sck
->m_fd
== INVALID_SOCKET
)
586 sck
->m_error
= GSOCK_IOERR
;
590 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
591 _GSocket_Enable_Events(sck
);
593 /* Bind to the local address,
594 * and retrieve the actual address bound.
596 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
597 (getsockname(sck
->m_fd
,
598 sck
->m_local
->m_addr
,
599 (SOCKLEN_T
*)&sck
->m_local
->m_len
) != 0))
601 closesocket(sck
->m_fd
);
602 sck
->m_fd
= INVALID_SOCKET
;
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__) */