]>
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 /* don't use C++ TRUE/FALSE definition which we get from wx/defs.h */
69 /* if we use configure for MSW SOCKLEN_T will be already defined */
71 # define SOCKLEN_T int
75 /* Constructors / Destructors for GSocket */
77 GSocket
*GSocket_new(void)
82 if ((socket
= (GSocket
*) malloc(sizeof(GSocket
))) == NULL
)
85 socket
->m_fd
= INVALID_SOCKET
;
86 for (i
= 0; i
< GSOCK_MAX_EVENT
; i
++)
88 socket
->m_cbacks
[i
] = NULL
;
90 socket
->m_detected
= 0;
91 socket
->m_local
= NULL
;
92 socket
->m_peer
= NULL
;
93 socket
->m_error
= GSOCK_NOERROR
;
94 socket
->m_server
= FALSE
;
95 socket
->m_stream
= TRUE
;
96 socket
->m_non_blocking
= FALSE
;
97 socket
->m_timeout
.tv_sec
= 10 * 60; /* 10 minutes */
98 socket
->m_timeout
.tv_usec
= 0;
99 socket
->m_establishing
= FALSE
;
101 /* Per-socket GUI-specific initialization */
102 success
= _GSocket_GUI_Init(socket
);
112 void GSocket_destroy(GSocket
*socket
)
114 assert(socket
!= NULL
);
116 /* Per-socket GUI-specific cleanup */
117 _GSocket_GUI_Destroy(socket
);
119 /* Check that the socket is really shutdowned */
120 if (socket
->m_fd
!= INVALID_SOCKET
)
121 GSocket_Shutdown(socket
);
123 /* Destroy private addresses */
125 GAddress_destroy(socket
->m_local
);
128 GAddress_destroy(socket
->m_peer
);
130 /* Destroy the socket itself */
135 * Disallow further read/write operations on this socket, close
136 * the fd and disable all callbacks.
138 void GSocket_Shutdown(GSocket
*socket
)
142 assert(socket
!= NULL
);
144 /* If socket has been created, shutdown it */
145 if (socket
->m_fd
!= INVALID_SOCKET
)
147 shutdown(socket
->m_fd
, 2);
148 closesocket(socket
->m_fd
);
149 socket
->m_fd
= INVALID_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
;
157 _GSocket_Disable_Events(socket
);
160 /* Address handling */
166 * Set or get the local or peer address for this socket. The 'set'
167 * functions return GSOCK_NOERROR on success, an error code otherwise.
168 * The 'get' functions return a pointer to a GAddress object on success,
169 * or NULL otherwise, in which case they set the error code of the
170 * corresponding GSocket.
173 * GSOCK_INVSOCK - the socket is not valid.
174 * GSOCK_INVADDR - the address is not valid.
176 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
178 assert(socket
!= NULL
);
180 /* the socket must be initialized, or it must be a server */
181 if (socket
->m_fd
!= INVALID_SOCKET
&& !socket
->m_server
)
183 socket
->m_error
= GSOCK_INVSOCK
;
184 return GSOCK_INVSOCK
;
188 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
190 socket
->m_error
= GSOCK_INVADDR
;
191 return GSOCK_INVADDR
;
195 GAddress_destroy(socket
->m_local
);
197 socket
->m_local
= GAddress_copy(address
);
199 return GSOCK_NOERROR
;
202 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
204 assert(socket
!= NULL
);
207 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
209 socket
->m_error
= GSOCK_INVADDR
;
210 return GSOCK_INVADDR
;
214 GAddress_destroy(socket
->m_peer
);
216 socket
->m_peer
= GAddress_copy(address
);
218 return GSOCK_NOERROR
;
221 GAddress
*GSocket_GetLocal(GSocket
*socket
)
224 struct sockaddr addr
;
225 SOCKLEN_T size
= sizeof(addr
);
228 assert(socket
!= NULL
);
230 /* try to get it from the m_local var first */
232 return GAddress_copy(socket
->m_local
);
234 /* else, if the socket is initialized, try getsockname */
235 if (socket
->m_fd
== INVALID_SOCKET
)
237 socket
->m_error
= GSOCK_INVSOCK
;
241 if (getsockname(socket
->m_fd
, &addr
, &size
) == SOCKET_ERROR
)
243 socket
->m_error
= GSOCK_IOERR
;
247 /* got a valid address from getsockname, create a GAddress object */
248 if ((address
= GAddress_new()) == NULL
)
250 socket
->m_error
= GSOCK_MEMERR
;
254 if ((err
= _GAddress_translate_from(address
, &addr
, size
)) != GSOCK_NOERROR
)
256 GAddress_destroy(address
);
257 socket
->m_error
= err
;
264 GAddress
*GSocket_GetPeer(GSocket
*socket
)
266 assert(socket
!= NULL
);
268 /* try to get it from the m_peer var */
270 return GAddress_copy(socket
->m_peer
);
275 /* Server specific parts */
277 /* GSocket_SetServer:
278 * Sets up this socket as a server. The local address must have been
279 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
280 * Returns GSOCK_NOERROR on success, one of the following otherwise:
283 * GSOCK_INVSOCK - the socket is in use.
284 * GSOCK_INVADDR - the local address has not been set.
285 * GSOCK_IOERR - low-level error.
287 GSocketError
GSocket_SetServer(GSocket
*sck
)
293 /* must not be in use */
294 if (sck
->m_fd
!= INVALID_SOCKET
)
296 sck
->m_error
= GSOCK_INVSOCK
;
297 return GSOCK_INVSOCK
;
300 /* the local addr must have been set */
303 sck
->m_error
= GSOCK_INVADDR
;
304 return GSOCK_INVADDR
;
307 /* Initialize all fields */
308 sck
->m_server
= TRUE
;
309 sck
->m_stream
= TRUE
;
310 sck
->m_oriented
= TRUE
;
312 /* Create the socket */
313 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
315 if (sck
->m_fd
== INVALID_SOCKET
)
317 sck
->m_error
= GSOCK_IOERR
;
321 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
322 _GSocket_Enable_Events(sck
);
324 /* Bind to the local address,
325 * retrieve the actual address bound,
326 * and listen up to 5 connections.
328 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
329 (getsockname(sck
->m_fd
,
330 sck
->m_local
->m_addr
,
331 (SOCKLEN_T
*)&sck
->m_local
->m_len
) != 0) ||
332 (listen(sck
->m_fd
, 5) != 0))
334 closesocket(sck
->m_fd
);
335 sck
->m_fd
= INVALID_SOCKET
;
336 sck
->m_error
= GSOCK_IOERR
;
340 return GSOCK_NOERROR
;
343 /* GSocket_WaitConnection:
344 * Waits for an incoming client connection. Returns a pointer to
345 * a GSocket object, or NULL if there was an error, in which case
346 * the last error field will be updated for the calling GSocket.
348 * Error codes (set in the calling GSocket)
349 * GSOCK_INVSOCK - the socket is not valid or not a server.
350 * GSOCK_TIMEDOUT - timeout, no incoming connections.
351 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
352 * GSOCK_MEMERR - couldn't allocate memory.
353 * GSOCK_IOERR - low-level error.
355 GSocket
*GSocket_WaitConnection(GSocket
*sck
)
358 struct sockaddr from
;
359 SOCKLEN_T fromlen
= sizeof(from
);
365 /* Reenable CONNECTION events */
366 sck
->m_detected
&= ~GSOCK_CONNECTION_FLAG
;
368 /* If the socket has already been created, we exit immediately */
369 if (sck
->m_fd
== INVALID_SOCKET
|| !sck
->m_server
)
371 sck
->m_error
= GSOCK_INVSOCK
;
375 /* Create a GSocket object for the new connection */
376 connection
= GSocket_new();
380 sck
->m_error
= GSOCK_MEMERR
;
384 /* Wait for a connection (with timeout) */
385 if (_GSocket_Input_Timeout(sck
) == GSOCK_TIMEDOUT
)
387 GSocket_destroy(connection
);
388 /* sck->m_error set by _GSocket_Input_Timeout */
392 connection
->m_fd
= accept(sck
->m_fd
, &from
, &fromlen
);
394 if (connection
->m_fd
== INVALID_SOCKET
)
396 if (WSAGetLastError() == WSAEWOULDBLOCK
)
397 sck
->m_error
= GSOCK_WOULDBLOCK
;
399 sck
->m_error
= GSOCK_IOERR
;
401 GSocket_destroy(connection
);
405 /* Initialize all fields */
406 connection
->m_server
= FALSE
;
407 connection
->m_stream
= TRUE
;
408 connection
->m_oriented
= TRUE
;
410 /* Setup the peer address field */
411 connection
->m_peer
= GAddress_new();
412 if (!connection
->m_peer
)
414 GSocket_destroy(connection
);
415 sck
->m_error
= GSOCK_MEMERR
;
418 err
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
);
419 if (err
!= GSOCK_NOERROR
)
421 GAddress_destroy(connection
->m_peer
);
422 GSocket_destroy(connection
);
427 ioctlsocket(connection
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
428 _GSocket_Enable_Events(connection
);
433 /* Client specific parts */
436 * For stream (connection oriented) sockets, GSocket_Connect() tries
437 * to establish a client connection to a server using the peer address
438 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
439 * connection has been succesfully established, or one of the error
440 * codes listed below. Note that for nonblocking sockets, a return
441 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
442 * request can be completed later; you should use GSocket_Select()
443 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
444 * corresponding asynchronous events.
446 * For datagram (non connection oriented) sockets, GSocket_Connect()
447 * just sets the peer address established with GSocket_SetPeer() as
448 * default destination.
451 * GSOCK_INVSOCK - the socket is in use or not valid.
452 * GSOCK_INVADDR - the peer address has not been established.
453 * GSOCK_TIMEDOUT - timeout, the connection failed.
454 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
455 * GSOCK_MEMERR - couldn't allocate memory.
456 * GSOCK_IOERR - low-level error.
458 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
465 /* Enable CONNECTION events (needed for nonblocking connections) */
466 sck
->m_detected
&= ~GSOCK_CONNECTION_FLAG
;
468 if (sck
->m_fd
!= INVALID_SOCKET
)
470 sck
->m_error
= GSOCK_INVSOCK
;
471 return GSOCK_INVSOCK
;
476 sck
->m_error
= GSOCK_INVADDR
;
477 return GSOCK_INVADDR
;
480 /* Streamed or dgram socket? */
481 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
482 sck
->m_oriented
= TRUE
;
483 sck
->m_server
= FALSE
;
484 sck
->m_establishing
= FALSE
;
486 /* Create the socket */
487 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
,
488 sck
->m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
490 if (sck
->m_fd
== INVALID_SOCKET
)
492 sck
->m_error
= GSOCK_IOERR
;
496 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
497 _GSocket_Enable_Events(sck
);
499 /* Connect it to the peer address, with a timeout (see below) */
500 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
502 if (ret
== SOCKET_ERROR
)
504 err
= WSAGetLastError();
506 /* If connect failed with EWOULDBLOCK and the GSocket object
507 * is in blocking mode, we select() for the specified timeout
508 * checking for writability to see if the connection request
511 if ((err
== WSAEWOULDBLOCK
) && (!sck
->m_non_blocking
))
513 err
= _GSocket_Connect_Timeout(sck
);
515 if (err
!= GSOCK_NOERROR
)
517 closesocket(sck
->m_fd
);
518 sck
->m_fd
= INVALID_SOCKET
;
519 /* sck->m_error is set in _GSocket_Connect_Timeout */
525 /* If connect failed with EWOULDBLOCK and the GSocket object
526 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
527 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
528 * this way if the connection completes, a GSOCK_CONNECTION
529 * event will be generated, if enabled.
531 if ((err
== WSAEWOULDBLOCK
) && (sck
->m_non_blocking
))
533 sck
->m_establishing
= TRUE
;
534 sck
->m_error
= GSOCK_WOULDBLOCK
;
535 return GSOCK_WOULDBLOCK
;
538 /* If connect failed with an error other than EWOULDBLOCK,
539 * then the call to GSocket_Connect() has failed.
541 closesocket(sck
->m_fd
);
542 sck
->m_fd
= INVALID_SOCKET
;
543 sck
->m_error
= GSOCK_IOERR
;
547 return GSOCK_NOERROR
;
550 /* Datagram sockets */
552 /* GSocket_SetNonOriented:
553 * Sets up this socket as a non-connection oriented (datagram) socket.
554 * Before using this function, the local address must have been set
555 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
556 * on success, or one of the following otherwise.
559 * GSOCK_INVSOCK - the socket is in use.
560 * GSOCK_INVADDR - the local address has not been set.
561 * GSOCK_IOERR - low-level error.
563 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
569 if (sck
->m_fd
!= INVALID_SOCKET
)
571 sck
->m_error
= GSOCK_INVSOCK
;
572 return GSOCK_INVSOCK
;
577 sck
->m_error
= GSOCK_INVADDR
;
578 return GSOCK_INVADDR
;
581 /* Initialize all fields */
582 sck
->m_stream
= FALSE
;
583 sck
->m_server
= FALSE
;
584 sck
->m_oriented
= FALSE
;
586 /* Create the socket */
587 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
589 if (sck
->m_fd
== INVALID_SOCKET
)
591 sck
->m_error
= GSOCK_IOERR
;
595 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
596 _GSocket_Enable_Events(sck
);
598 /* Bind to the local address,
599 * and retrieve the actual address bound.
601 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
602 (getsockname(sck
->m_fd
,
603 sck
->m_local
->m_addr
,
604 (SOCKLEN_T
*)&sck
->m_local
->m_len
) != 0))
606 closesocket(sck
->m_fd
);
607 sck
->m_fd
= INVALID_SOCKET
;
608 sck
->m_error
= GSOCK_IOERR
;
612 return GSOCK_NOERROR
;
617 /* Like recv(), send(), ... */
618 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
622 assert(socket
!= NULL
);
624 /* Reenable INPUT events */
625 socket
->m_detected
&= ~GSOCK_INPUT_FLAG
;
627 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
629 socket
->m_error
= GSOCK_INVSOCK
;
633 /* If the socket is blocking, wait for data (with a timeout) */
634 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
638 if (socket
->m_stream
)
639 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
641 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
643 if (ret
== SOCKET_ERROR
)
645 if (WSAGetLastError() != WSAEWOULDBLOCK
)
646 socket
->m_error
= GSOCK_IOERR
;
648 socket
->m_error
= GSOCK_WOULDBLOCK
;
655 int GSocket_Write(GSocket
*socket
, const char *buffer
, int size
)
659 assert(socket
!= NULL
);
661 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
663 socket
->m_error
= GSOCK_INVSOCK
;
667 /* If the socket is blocking, wait for writability (with a timeout) */
668 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
672 if (socket
->m_stream
)
673 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
675 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
677 if (ret
== SOCKET_ERROR
)
679 if (WSAGetLastError() != WSAEWOULDBLOCK
)
680 socket
->m_error
= GSOCK_IOERR
;
682 socket
->m_error
= GSOCK_WOULDBLOCK
;
684 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
685 * does). Once the first OUTPUT event is received, users can assume
686 * that the socket is writable until a read operation fails. Only then
687 * will further OUTPUT events be posted.
689 socket
->m_detected
&= ~GSOCK_OUTPUT_FLAG
;
697 * Polls the socket to determine its status. This function will
698 * check for the events specified in the 'flags' parameter, and
699 * it will return a mask indicating which operations can be
700 * performed. This function won't block, regardless of the
701 * mode (blocking | nonblocking) of the socket.
703 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
705 #if defined(wxUSE_GUI) && !wxUSE_GUI
707 GSocketEventFlags result
= 0;
711 static const struct timeval tv
= { 0, 0 };
713 assert(socket
!= NULL
);
718 FD_SET(socket
->m_fd
, &readfds
);
719 FD_SET(socket
->m_fd
, &writefds
);
720 FD_SET(socket
->m_fd
, &exceptfds
);
722 /* Check known state first */
723 result
|= (GSOCK_CONNECTION_FLAG
& socket
->m_detected
& flags
);
724 result
|= (GSOCK_LOST_FLAG
& socket
->m_detected
& flags
);
727 if (select(socket
->m_fd
+ 1, &readfds
, &writefds
, &exceptfds
, &tv
) <= 0)
730 /* Check for readability */
731 if (FD_ISSET(socket
->m_fd
, &readfds
))
733 /* Assume that closure of the socket is always reported via exceptfds */
734 if (socket
->m_server
&& socket
->m_stream
)
735 result
|= (GSOCK_CONNECTION_FLAG
& flags
);
737 result
|= (GSOCK_INPUT_FLAG
& flags
);
740 /* Check for writability */
741 if (FD_ISSET(socket
->m_fd
, &writefds
))
743 if (socket
->m_establishing
&& !socket
->m_server
)
745 result
|= (GSOCK_CONNECTION_FLAG
& flags
);
746 socket
->m_establishing
= FALSE
;
747 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
750 result
|= (GSOCK_OUTPUT_FLAG
& flags
);
753 /* Check for exceptions and errors */
754 if (FD_ISSET(socket
->m_fd
, &exceptfds
))
756 result
|= (GSOCK_LOST_FLAG
& flags
);
757 socket
->m_establishing
= FALSE
;
758 socket
->m_detected
= GSOCK_LOST_FLAG
;
765 assert(socket
!= NULL
);
766 return flags
& socket
->m_detected
;
768 #endif /* !wxUSE_GUI */
773 /* GSocket_SetNonBlocking:
774 * Sets the socket to non-blocking mode. All IO calls will return
777 void GSocket_SetNonBlocking(GSocket
*socket
, int non_block
)
779 assert(socket
!= NULL
);
781 socket
->m_non_blocking
= non_block
;
784 /* GSocket_SetTimeout:
785 * Sets the timeout for blocking calls. Time is expressed in
788 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millis
)
790 assert(socket
!= NULL
);
792 socket
->m_timeout
.tv_sec
= (millis
/ 1000);
793 socket
->m_timeout
.tv_usec
= (millis
% 1000) * 1000;
797 * Returns the last error occured for this socket. Note that successful
798 * operations do not clear this back to GSOCK_NOERROR, so use it only
801 GSocketError
GSocket_GetError(GSocket
*socket
)
803 assert(socket
!= NULL
);
805 return socket
->m_error
;
811 * There is data to be read in the input buffer. If, after a read
812 * operation, there is still data available, the callback function will
815 * The socket is available for writing. That is, the next write call
816 * won't block. This event is generated only once, when the connection is
817 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
818 * when the output buffer empties again. This means that the app should
819 * assume that it can write since the first OUTPUT event, and no more
820 * OUTPUT events will be generated unless an error occurs.
822 * Connection succesfully established, for client sockets, or incoming
823 * client connection, for server sockets. Wait for this event (also watch
824 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
826 * The connection is lost (or a connection request failed); this could
827 * be due to a failure, or due to the peer closing it gracefully.
830 /* GSocket_SetCallback:
831 * Enables the callbacks specified by 'flags'. Note that 'flags'
832 * may be a combination of flags OR'ed toghether, so the same
833 * callback function can be made to accept different events.
834 * The callback function must have the following prototype:
836 * void function(GSocket *socket, GSocketEvent event, char *cdata)
838 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
839 GSocketCallback callback
, char *cdata
)
843 assert(socket
!= NULL
);
845 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
847 if ((flags
& (1 << count
)) != 0)
849 socket
->m_cbacks
[count
] = callback
;
850 socket
->m_data
[count
] = cdata
;
855 /* GSocket_UnsetCallback:
856 * Disables all callbacks specified by 'flags', which may be a
857 * combination of flags OR'ed toghether.
859 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
863 assert(socket
!= NULL
);
865 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
867 if ((flags
& (1 << count
)) != 0)
869 socket
->m_cbacks
[count
] = NULL
;
870 socket
->m_data
[count
] = NULL
;
877 /* _GSocket_Input_Timeout:
878 * For blocking sockets, wait until data is available or
879 * until timeout ellapses.
881 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
885 if (!socket
->m_non_blocking
)
888 FD_SET(socket
->m_fd
, &readfds
);
889 if (select(0, &readfds
, NULL
, NULL
, &socket
->m_timeout
) == 0)
891 socket
->m_error
= GSOCK_TIMEDOUT
;
892 return GSOCK_TIMEDOUT
;
895 return GSOCK_NOERROR
;
898 /* _GSocket_Output_Timeout:
899 * For blocking sockets, wait until data can be sent without
900 * blocking or until timeout ellapses.
902 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
906 if (!socket
->m_non_blocking
)
909 FD_SET(socket
->m_fd
, &writefds
);
910 if (select(0, NULL
, &writefds
, NULL
, &socket
->m_timeout
) == 0)
912 socket
->m_error
= GSOCK_TIMEDOUT
;
913 return GSOCK_TIMEDOUT
;
916 return GSOCK_NOERROR
;
919 /* _GSocket_Connect_Timeout:
920 * For blocking sockets, wait until the connection is
921 * established or fails, or until timeout ellapses.
923 GSocketError
_GSocket_Connect_Timeout(GSocket
*socket
)
930 FD_SET(socket
->m_fd
, &writefds
);
931 FD_SET(socket
->m_fd
, &exceptfds
);
932 if (select(0, NULL
, &writefds
, &exceptfds
, &socket
->m_timeout
) == 0)
934 socket
->m_error
= GSOCK_TIMEDOUT
;
935 return GSOCK_TIMEDOUT
;
937 if (!FD_ISSET(socket
->m_fd
, &writefds
))
939 socket
->m_error
= GSOCK_IOERR
;
943 return GSOCK_NOERROR
;
946 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
948 return recv(socket
->m_fd
, buffer
, size
, 0);
951 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
953 struct sockaddr from
;
954 SOCKLEN_T fromlen
= sizeof(from
);
958 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
960 if (ret
== SOCKET_ERROR
)
963 /* Translate a system address into a GSocket address */
966 socket
->m_peer
= GAddress_new();
969 socket
->m_error
= GSOCK_MEMERR
;
973 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
974 if (err
!= GSOCK_NOERROR
)
976 GAddress_destroy(socket
->m_peer
);
977 socket
->m_peer
= NULL
;
978 socket
->m_error
= err
;
985 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
987 return send(socket
->m_fd
, buffer
, size
, 0);
990 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
992 struct sockaddr
*addr
;
998 socket
->m_error
= GSOCK_INVADDR
;
1002 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
1003 if (err
!= GSOCK_NOERROR
)
1005 socket
->m_error
= err
;
1009 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
1011 /* Frees memory allocated by _GAddress_translate_to */
1019 * -------------------------------------------------------------------------
1021 * -------------------------------------------------------------------------
1024 /* CHECK_ADDRESS verifies that the current address family is either
1025 * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it
1026 * initalizes it to be a GSOCK_*family*. In other cases, it returns
1027 * an appropiate error code.
1029 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
1031 #define CHECK_ADDRESS(address, family) \
1033 if (address->m_family == GSOCK_NOFAMILY) \
1034 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1035 return address->m_error; \
1036 if (address->m_family != GSOCK_##family) \
1038 address->m_error = GSOCK_INVADDR; \
1039 return GSOCK_INVADDR; \
1043 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
1045 if (address->m_family == GSOCK_NOFAMILY) \
1046 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1048 if (address->m_family != GSOCK_##family) \
1050 address->m_error = GSOCK_INVADDR; \
1056 GAddress
*GAddress_new(void)
1060 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1063 address
->m_family
= GSOCK_NOFAMILY
;
1064 address
->m_addr
= NULL
;
1070 GAddress
*GAddress_copy(GAddress
*address
)
1074 assert(address
!= NULL
);
1076 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1079 memcpy(addr2
, address
, sizeof(GAddress
));
1081 if (address
->m_addr
)
1083 addr2
->m_addr
= (struct sockaddr
*) malloc(addr2
->m_len
);
1084 if (addr2
->m_addr
== NULL
)
1089 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1095 void GAddress_destroy(GAddress
*address
)
1097 assert(address
!= NULL
);
1099 if (address
->m_addr
)
1100 free(address
->m_addr
);
1105 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1107 assert(address
!= NULL
);
1109 address
->m_family
= type
;
1112 GAddressType
GAddress_GetFamily(GAddress
*address
)
1114 assert(address
!= NULL
);
1116 return address
->m_family
;
1119 GSocketError
_GAddress_translate_from(GAddress
*address
,
1120 struct sockaddr
*addr
, int len
)
1122 address
->m_realfamily
= addr
->sa_family
;
1123 switch (addr
->sa_family
)
1126 address
->m_family
= GSOCK_INET
;
1129 address
->m_family
= GSOCK_UNIX
;
1133 address
->m_family
= GSOCK_INET6
;
1138 address
->m_error
= GSOCK_INVOP
;
1143 if (address
->m_addr
)
1144 free(address
->m_addr
);
1146 address
->m_len
= len
;
1147 address
->m_addr
= (struct sockaddr
*) malloc(len
);
1149 if (address
->m_addr
== NULL
)
1151 address
->m_error
= GSOCK_MEMERR
;
1152 return GSOCK_MEMERR
;
1154 memcpy(address
->m_addr
, addr
, len
);
1156 return GSOCK_NOERROR
;
1159 GSocketError
_GAddress_translate_to(GAddress
*address
,
1160 struct sockaddr
**addr
, int *len
)
1162 if (!address
->m_addr
)
1164 address
->m_error
= GSOCK_INVADDR
;
1165 return GSOCK_INVADDR
;
1168 *len
= address
->m_len
;
1169 *addr
= (struct sockaddr
*) malloc(address
->m_len
);
1172 address
->m_error
= GSOCK_MEMERR
;
1173 return GSOCK_MEMERR
;
1176 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1177 return GSOCK_NOERROR
;
1181 * -------------------------------------------------------------------------
1182 * Internet address family
1183 * -------------------------------------------------------------------------
1186 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1188 address
->m_len
= sizeof(struct sockaddr_in
);
1189 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1190 if (address
->m_addr
== NULL
)
1192 address
->m_error
= GSOCK_MEMERR
;
1193 return GSOCK_MEMERR
;
1196 address
->m_family
= GSOCK_INET
;
1197 address
->m_realfamily
= PF_INET
;
1198 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1199 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1201 return GSOCK_NOERROR
;
1204 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1207 struct in_addr
*addr
;
1209 assert(address
!= NULL
);
1211 CHECK_ADDRESS(address
, INET
);
1213 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1215 addr
->s_addr
= inet_addr(hostname
);
1217 /* If it is a numeric host name, convert it now */
1218 if (addr
->s_addr
== INADDR_NONE
)
1220 struct in_addr
*array_addr
;
1222 /* It is a real name, we solve it */
1223 if ((he
= gethostbyname(hostname
)) == NULL
)
1225 /* addr->s_addr = INADDR_NONE just done by inet_addr() above */
1226 address
->m_error
= GSOCK_NOHOST
;
1227 return GSOCK_NOHOST
;
1229 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1230 addr
->s_addr
= array_addr
[0].s_addr
;
1232 return GSOCK_NOERROR
;
1235 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1237 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1240 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1241 unsigned long hostaddr
)
1243 struct in_addr
*addr
;
1245 assert(address
!= NULL
);
1247 CHECK_ADDRESS(address
, INET
);
1249 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1250 addr
->s_addr
= hostaddr
;
1252 return GSOCK_NOERROR
;
1255 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1256 const char *protocol
)
1259 struct sockaddr_in
*addr
;
1261 assert(address
!= NULL
);
1262 CHECK_ADDRESS(address
, INET
);
1266 address
->m_error
= GSOCK_INVPORT
;
1267 return GSOCK_INVPORT
;
1270 se
= getservbyname(port
, protocol
);
1273 if (isdigit(port
[0]))
1277 port_int
= atoi(port
);
1278 addr
= (struct sockaddr_in
*)address
->m_addr
;
1279 addr
->sin_port
= htons((u_short
) port_int
);
1280 return GSOCK_NOERROR
;
1283 address
->m_error
= GSOCK_INVPORT
;
1284 return GSOCK_INVPORT
;
1287 addr
= (struct sockaddr_in
*)address
->m_addr
;
1288 addr
->sin_port
= se
->s_port
;
1290 return GSOCK_NOERROR
;
1293 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1295 struct sockaddr_in
*addr
;
1297 assert(address
!= NULL
);
1298 CHECK_ADDRESS(address
, INET
);
1300 addr
= (struct sockaddr_in
*)address
->m_addr
;
1301 addr
->sin_port
= htons(port
);
1303 return GSOCK_NOERROR
;
1306 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1310 struct sockaddr_in
*addr
;
1312 assert(address
!= NULL
);
1313 CHECK_ADDRESS(address
, INET
);
1315 addr
= (struct sockaddr_in
*)address
->m_addr
;
1316 addr_buf
= (char *)&(addr
->sin_addr
);
1318 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1321 address
->m_error
= GSOCK_NOHOST
;
1322 return GSOCK_NOHOST
;
1325 strncpy(hostname
, he
->h_name
, sbuf
);
1327 return GSOCK_NOERROR
;
1330 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1332 struct sockaddr_in
*addr
;
1334 assert(address
!= NULL
);
1335 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1337 addr
= (struct sockaddr_in
*)address
->m_addr
;
1339 return addr
->sin_addr
.s_addr
;
1342 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1344 struct sockaddr_in
*addr
;
1346 assert(address
!= NULL
);
1347 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1349 addr
= (struct sockaddr_in
*)address
->m_addr
;
1350 return ntohs(addr
->sin_port
);
1354 * -------------------------------------------------------------------------
1355 * Unix address family
1356 * -------------------------------------------------------------------------
1359 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1361 assert (address
!= NULL
);
1362 address
->m_error
= GSOCK_INVADDR
;
1363 return GSOCK_INVADDR
;
1366 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1368 assert (address
!= NULL
);
1369 address
->m_error
= GSOCK_INVADDR
;
1370 return GSOCK_INVADDR
;
1373 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1375 assert (address
!= NULL
);
1376 address
->m_error
= GSOCK_INVADDR
;
1377 return GSOCK_INVADDR
;
1380 #else /* !wxUSE_SOCKETS */
1383 * Translation unit shouldn't be empty, so include this typedef to make the
1384 * compiler (VC++ 6.0, for example) happy
1386 typedef void (*wxDummy
)();
1388 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */