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__
22 #include "wx/msw/gsockmsw.h"
23 #include "wx/gsocket.h"
25 #define INSTANCE wxGetInstance()
32 /* If not using wxWindows, a global var called hInst must
33 * be available and it must containt the app's instance
36 #define INSTANCE hInst
38 #endif /* __GSOCKET_STANDALONE__ */
49 /* if we use configure for MSW SOCKLEN_T will be already defined */
54 #define CLASSNAME "_GSocket_Internal_Window_Class"
55 #define WINDOWNAME "_GSocket_Internal_Window_Name"
57 /* Maximum number of different GSocket objects at a given time.
58 * This value can be modified at will, but it CANNOT be greater
59 * than (0x7FFF - WM_USER + 1)
61 #define MAXSOCKETS 1024
63 #if (MAXSOCKETS > (0x7FFF - WM_USER + 1))
64 #error "MAXSOCKETS is too big!"
68 /* Global variables */
70 extern HINSTANCE INSTANCE
;
72 static CRITICAL_SECTION critical
;
73 static GSocket
* socketList
[MAXSOCKETS
];
74 static int firstAvailable
;
76 /* Global initializers */
84 /* Create internal window for event notifications */
86 winClass
.lpfnWndProc
= _GSocket_Internal_WinProc
;
87 winClass
.cbClsExtra
= 0;
88 winClass
.cbWndExtra
= 0;
89 winClass
.hInstance
= INSTANCE
;
90 winClass
.hIcon
= (HICON
) NULL
;
91 winClass
.hCursor
= (HCURSOR
) NULL
;
92 winClass
.hbrBackground
= (HBRUSH
) NULL
;
93 winClass
.lpszMenuName
= (LPCTSTR
) NULL
;
94 winClass
.lpszClassName
= CLASSNAME
;
96 RegisterClass(&winClass
);
97 hWin
= CreateWindow(CLASSNAME
,
100 (HWND
) NULL
, (HMENU
) NULL
, INSTANCE
, (LPVOID
) NULL
);
102 if (!hWin
) return FALSE
;
104 /* Initialize socket list */
105 InitializeCriticalSection(&critical
);
107 for (i
= 0; i
< MAXSOCKETS
; i
++)
109 socketList
[i
] = NULL
;
113 /* Initialize WinSocket */
114 return (WSAStartup((1 << 8) | 1, &wsaData
) == 0);
117 void GSocket_Cleanup()
119 /* Destroy internal window */
121 UnregisterClass(CLASSNAME
, INSTANCE
);
123 /* Delete critical section */
124 DeleteCriticalSection(&critical
);
126 /* Cleanup WinSocket */
130 /* Constructors / Destructors for GSocket */
132 GSocket
*GSocket_new()
137 if ((socket
= (GSocket
*) malloc(sizeof(GSocket
))) == NULL
)
140 socket
->m_fd
= INVALID_SOCKET
;
141 for (i
= 0; i
< GSOCK_MAX_EVENT
; i
++)
143 socket
->m_cbacks
[i
] = NULL
;
145 socket
->m_local
= NULL
;
146 socket
->m_peer
= NULL
;
147 socket
->m_error
= GSOCK_NOERROR
;
148 socket
->m_server
= FALSE
;
149 socket
->m_stream
= TRUE
;
150 socket
->m_non_blocking
= FALSE
;
151 socket
->m_timeout
.tv_sec
= 10 * 60; /* 10 minutes */
152 socket
->m_timeout
.tv_usec
= 0;
153 socket
->m_detected
= 0;
155 /* Allocate a new message number for this socket */
156 EnterCriticalSection(&critical
);
159 while (socketList
[i
] != NULL
)
161 i
= (i
+ 1) % MAXSOCKETS
;
163 if (i
== firstAvailable
) /* abort! */
166 LeaveCriticalSection(&critical
);
170 socketList
[i
] = socket
;
171 firstAvailable
= (i
+ 1) % MAXSOCKETS
;
172 socket
->m_msgnumber
= (i
+ WM_USER
);
174 LeaveCriticalSection(&critical
);
179 void GSocket_destroy(GSocket
*socket
)
181 assert(socket
!= NULL
);
183 /* Remove the socket from the list */
184 EnterCriticalSection(&critical
);
185 socketList
[(socket
->m_msgnumber
- WM_USER
)] = NULL
;
186 LeaveCriticalSection(&critical
);
188 /* Check that the socket is really shutdowned */
189 if (socket
->m_fd
!= INVALID_SOCKET
)
190 GSocket_Shutdown(socket
);
192 /* Destroy private addresses */
194 GAddress_destroy(socket
->m_local
);
197 GAddress_destroy(socket
->m_peer
);
199 /* Destroy the socket itself */
204 * Disallow further read/write operations on this socket, close
205 * the fd and disable all callbacks.
207 void GSocket_Shutdown(GSocket
*socket
)
211 assert(socket
!= NULL
);
213 /* If socket has been created, shutdown it */
214 if (socket
->m_fd
!= INVALID_SOCKET
)
216 shutdown(socket
->m_fd
, 2);
217 closesocket(socket
->m_fd
);
218 socket
->m_fd
= INVALID_SOCKET
;
221 /* Disable GUI callbacks */
222 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
223 socket
->m_cbacks
[evt
] = NULL
;
225 socket
->m_detected
= 0;
226 _GSocket_Disable_Events(socket
);
229 /* Address handling */
235 * Set or get the local or peer address for this socket. The 'set'
236 * functions return GSOCK_NOERROR on success, an error code otherwise.
237 * The 'get' functions return a pointer to a GAddress object on success,
238 * or NULL otherwise, in which case they set the error code of the
239 * corresponding GSocket.
242 * GSOCK_INVSOCK - the socket is not valid.
243 * GSOCK_INVADDR - the address is not valid.
245 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
247 assert(socket
!= NULL
);
249 /* the socket must be initialized, or it must be a server */
250 if (socket
->m_fd
!= INVALID_SOCKET
&& !socket
->m_server
)
252 socket
->m_error
= GSOCK_INVSOCK
;
253 return GSOCK_INVSOCK
;
257 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
259 socket
->m_error
= GSOCK_INVADDR
;
260 return GSOCK_INVADDR
;
264 GAddress_destroy(socket
->m_local
);
266 socket
->m_local
= GAddress_copy(address
);
268 return GSOCK_NOERROR
;
271 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
273 assert(socket
!= NULL
);
276 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
278 socket
->m_error
= GSOCK_INVADDR
;
279 return GSOCK_INVADDR
;
283 GAddress_destroy(socket
->m_peer
);
285 socket
->m_peer
= GAddress_copy(address
);
287 return GSOCK_NOERROR
;
290 GAddress
*GSocket_GetLocal(GSocket
*socket
)
293 struct sockaddr addr
;
294 SOCKLEN_T size
= sizeof(addr
);
297 assert(socket
!= NULL
);
299 /* try to get it from the m_local var first */
301 return GAddress_copy(socket
->m_local
);
303 /* else, if the socket is initialized, try getsockname */
304 if (socket
->m_fd
== INVALID_SOCKET
)
306 socket
->m_error
= GSOCK_INVSOCK
;
310 if (getsockname(socket
->m_fd
, &addr
, &size
) == SOCKET_ERROR
)
312 socket
->m_error
= GSOCK_IOERR
;
316 /* got a valid address from getsockname, create a GAddress object */
317 if ((address
= GAddress_new()) == NULL
)
319 socket
->m_error
= GSOCK_MEMERR
;
323 if ((err
= _GAddress_translate_from(address
, &addr
, size
)) != GSOCK_NOERROR
)
325 GAddress_destroy(address
);
326 socket
->m_error
= err
;
333 GAddress
*GSocket_GetPeer(GSocket
*socket
)
335 assert(socket
!= NULL
);
337 /* try to get it from the m_peer var */
339 return GAddress_copy(socket
->m_peer
);
344 /* Server specific parts */
346 /* GSocket_SetServer:
347 * Sets up this socket as a server. The local address must have been
348 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
349 * Returns GSOCK_NOERROR on success, one of the following otherwise:
352 * GSOCK_INVSOCK - the socket is in use.
353 * GSOCK_INVADDR - the local address has not been set.
354 * GSOCK_IOERR - low-level error.
356 GSocketError
GSocket_SetServer(GSocket
*sck
)
362 /* must not be in use */
363 if (sck
->m_fd
!= INVALID_SOCKET
)
365 sck
->m_error
= GSOCK_INVSOCK
;
366 return GSOCK_INVSOCK
;
369 /* the local addr must have been set */
372 sck
->m_error
= GSOCK_INVADDR
;
373 return GSOCK_INVADDR
;
376 /* Initialize all fields */
377 sck
->m_server
= TRUE
;
378 sck
->m_stream
= TRUE
;
379 sck
->m_oriented
= TRUE
;
381 /* Create the socket */
382 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
384 if (sck
->m_fd
== INVALID_SOCKET
)
386 sck
->m_error
= GSOCK_IOERR
;
390 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
391 _GSocket_Enable_Events(sck
);
393 /* Bind to the local address,
394 * retrieve the actual address bound,
395 * and listen up to 5 connections.
397 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
398 (getsockname(sck
->m_fd
,
399 sck
->m_local
->m_addr
,
400 &sck
->m_local
->m_len
) != 0) ||
401 (listen(sck
->m_fd
, 5) != 0))
403 closesocket(sck
->m_fd
);
404 sck
->m_fd
= INVALID_SOCKET
;
405 sck
->m_error
= GSOCK_IOERR
;
409 return GSOCK_NOERROR
;
412 /* GSocket_WaitConnection:
413 * Waits for an incoming client connection. Returns a pointer to
414 * a GSocket object, or NULL if there was an error, in which case
415 * the last error field will be updated for the calling GSocket.
417 * Error codes (set in the calling GSocket)
418 * GSOCK_INVSOCK - the socket is not valid or not a server.
419 * GSOCK_TIMEDOUT - timeout, no incoming connections.
420 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
421 * GSOCK_MEMERR - couldn't allocate memory.
422 * GSOCK_IOERR - low-level error.
424 GSocket
*GSocket_WaitConnection(GSocket
*sck
)
427 struct sockaddr from
;
428 SOCKLEN_T fromlen
= sizeof(from
);
434 /* Reenable CONNECTION events */
435 sck
->m_detected
&= ~GSOCK_CONNECTION_FLAG
;
437 /* If the socket has already been created, we exit immediately */
438 if (sck
->m_fd
== INVALID_SOCKET
|| !sck
->m_server
)
440 sck
->m_error
= GSOCK_INVSOCK
;
444 /* Create a GSocket object for the new connection */
445 connection
= GSocket_new();
449 sck
->m_error
= GSOCK_MEMERR
;
453 /* Wait for a connection (with timeout) */
454 if (_GSocket_Input_Timeout(sck
) == GSOCK_TIMEDOUT
)
456 GSocket_destroy(connection
);
457 /* sck->m_error set by _GSocket_Input_Timeout */
461 connection
->m_fd
= accept(sck
->m_fd
, &from
, &fromlen
);
463 if (connection
->m_fd
== INVALID_SOCKET
)
465 if (WSAGetLastError() == WSAEWOULDBLOCK
)
466 sck
->m_error
= GSOCK_WOULDBLOCK
;
468 sck
->m_error
= GSOCK_IOERR
;
470 GSocket_destroy(connection
);
474 /* Initialize all fields */
475 connection
->m_server
= FALSE
;
476 connection
->m_stream
= TRUE
;
477 connection
->m_oriented
= TRUE
;
479 /* Setup the peer address field */
480 connection
->m_peer
= GAddress_new();
481 if (!connection
->m_peer
)
483 GSocket_destroy(connection
);
484 sck
->m_error
= GSOCK_MEMERR
;
487 err
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
);
488 if (err
!= GSOCK_NOERROR
)
490 GAddress_destroy(connection
->m_peer
);
491 GSocket_destroy(connection
);
496 ioctlsocket(connection
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
497 _GSocket_Enable_Events(connection
);
502 /* Client specific parts */
505 * For stream (connection oriented) sockets, GSocket_Connect() tries
506 * to establish a client connection to a server using the peer address
507 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
508 * connection has been succesfully established, or one of the error
509 * codes listed below. Note that for nonblocking sockets, a return
510 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
511 * request can be completed later; you should use GSocket_Select()
512 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
513 * corresponding asynchronous events.
515 * For datagram (non connection oriented) sockets, GSocket_Connect()
516 * just sets the peer address established with GSocket_SetPeer() as
517 * default destination.
520 * GSOCK_INVSOCK - the socket is in use or not valid.
521 * GSOCK_INVADDR - the peer address has not been established.
522 * GSOCK_TIMEDOUT - timeout, the connection failed.
523 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
524 * GSOCK_MEMERR - couldn't allocate memory.
525 * GSOCK_IOERR - low-level error.
527 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
534 /* Enable CONNECTION events (needed for nonblocking connections) */
535 sck
->m_detected
&= ~GSOCK_CONNECTION_FLAG
;
537 if (sck
->m_fd
!= INVALID_SOCKET
)
539 sck
->m_error
= GSOCK_INVSOCK
;
540 return GSOCK_INVSOCK
;
545 sck
->m_error
= GSOCK_INVADDR
;
546 return GSOCK_INVADDR
;
549 /* Streamed or dgram socket? */
550 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
551 sck
->m_oriented
= TRUE
;
552 sck
->m_server
= FALSE
;
554 /* Create the socket */
555 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
,
556 sck
->m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
558 if (sck
->m_fd
== INVALID_SOCKET
)
560 sck
->m_error
= GSOCK_IOERR
;
564 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
565 _GSocket_Enable_Events(sck
);
567 /* Connect it to the peer address, with a timeout (see below) */
568 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
570 if (ret
== SOCKET_ERROR
)
572 err
= WSAGetLastError();
574 /* If connect failed with EWOULDBLOCK and the GSocket object
575 * is in blocking mode, we select() for the specified timeout
576 * checking for writability to see if the connection request
579 if ((err
== WSAEWOULDBLOCK
) && (!sck
->m_non_blocking
))
581 err
= _GSocket_Connect_Timeout(sck
);
583 if (err
!= GSOCK_NOERROR
)
585 closesocket(sck
->m_fd
);
586 sck
->m_fd
= INVALID_SOCKET
;
587 /* sck->m_error is set in _GSocket_Connect_Timeout */
593 /* If connect failed with EWOULDBLOCK and the GSocket object
594 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
595 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
596 * this way if the connection completes, a GSOCK_CONNECTION
597 * event will be generated, if enabled.
599 if ((err
== WSAEWOULDBLOCK
) && (sck
->m_non_blocking
))
601 sck
->m_error
= GSOCK_WOULDBLOCK
;
602 return GSOCK_WOULDBLOCK
;
605 /* If connect failed with an error other than EWOULDBLOCK,
606 * then the call to GSocket_Connect() has failed.
608 closesocket(sck
->m_fd
);
609 sck
->m_fd
= INVALID_SOCKET
;
610 sck
->m_error
= GSOCK_IOERR
;
614 return GSOCK_NOERROR
;
617 /* Datagram sockets */
619 /* GSocket_SetNonOriented:
620 * Sets up this socket as a non-connection oriented (datagram) socket.
621 * Before using this function, the local address must have been set
622 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
623 * on success, or one of the following otherwise.
626 * GSOCK_INVSOCK - the socket is in use.
627 * GSOCK_INVADDR - the local address has not been set.
628 * GSOCK_IOERR - low-level error.
630 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
636 if (sck
->m_fd
!= INVALID_SOCKET
)
638 sck
->m_error
= GSOCK_INVSOCK
;
639 return GSOCK_INVSOCK
;
644 sck
->m_error
= GSOCK_INVADDR
;
645 return GSOCK_INVADDR
;
648 /* Initialize all fields */
649 sck
->m_stream
= FALSE
;
650 sck
->m_server
= FALSE
;
651 sck
->m_oriented
= FALSE
;
653 /* Create the socket */
654 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
656 if (sck
->m_fd
== INVALID_SOCKET
)
658 sck
->m_error
= GSOCK_IOERR
;
662 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
663 _GSocket_Enable_Events(sck
);
665 /* Bind to the local address,
666 * and retrieve the actual address bound.
668 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
669 (getsockname(sck
->m_fd
,
670 sck
->m_local
->m_addr
,
671 &sck
->m_local
->m_len
) != 0))
673 closesocket(sck
->m_fd
);
674 sck
->m_fd
= INVALID_SOCKET
;
675 sck
->m_error
= GSOCK_IOERR
;
679 return GSOCK_NOERROR
;
685 /* Like recv(), send(), ... */
686 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
690 assert(socket
!= NULL
);
692 /* Reenable INPUT events */
693 socket
->m_detected
&= ~GSOCK_INPUT_FLAG
;
695 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
697 socket
->m_error
= GSOCK_INVSOCK
;
701 /* If the socket is blocking, wait for data (with a timeout) */
702 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
706 if (socket
->m_stream
)
707 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
709 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
711 if (ret
== SOCKET_ERROR
)
713 if (WSAGetLastError() != WSAEWOULDBLOCK
)
714 socket
->m_error
= GSOCK_IOERR
;
716 socket
->m_error
= GSOCK_WOULDBLOCK
;
723 int GSocket_Write(GSocket
*socket
, const char *buffer
, int size
)
727 assert(socket
!= NULL
);
729 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
731 socket
->m_error
= GSOCK_INVSOCK
;
735 /* If the socket is blocking, wait for writability (with a timeout) */
736 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
740 if (socket
->m_stream
)
741 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
743 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
745 if (ret
== SOCKET_ERROR
)
747 if (WSAGetLastError() != WSAEWOULDBLOCK
)
748 socket
->m_error
= GSOCK_IOERR
;
750 socket
->m_error
= GSOCK_WOULDBLOCK
;
752 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
753 * does). Once the first OUTPUT event is received, users can assume
754 * that the socket is writable until a read operation fails. Only then
755 * will further OUTPUT events be posted.
757 socket
->m_detected
&= ~GSOCK_OUTPUT_FLAG
;
765 * Polls the socket to determine its status. This function will
766 * check for the events specified in the 'flags' parameter, and
767 * it will return a mask indicating which operations can be
768 * performed. This function won't block, regardless of the
769 * mode (blocking | nonblocking) of the socket.
771 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
773 assert(socket
!= NULL
);
775 return flags
& socket
->m_detected
;
780 /* GSocket_SetNonBlocking:
781 * Sets the socket to non-blocking mode. All IO calls will return
784 void GSocket_SetNonBlocking(GSocket
*socket
, bool non_block
)
786 assert(socket
!= NULL
);
788 socket
->m_non_blocking
= non_block
;
791 /* GSocket_SetTimeout:
792 * Sets the timeout for blocking calls. Time is expressed in
795 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millis
)
797 assert(socket
!= NULL
);
799 socket
->m_timeout
.tv_sec
= (millis
/ 1000);
800 socket
->m_timeout
.tv_usec
= (millis
% 1000) * 1000;
804 * Returns the last error occured for this socket. Note that successful
805 * operations do not clear this back to GSOCK_NOERROR, so use it only
808 GSocketError
GSocket_GetError(GSocket
*socket
)
810 assert(socket
!= NULL
);
812 return socket
->m_error
;
819 * There is data to be read in the input buffer. If, after a read
820 * operation, there is still data available, the callback function will
823 * The socket is available for writing. That is, the next write call
824 * won't block. This event is generated only once, when the connection is
825 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
826 * when the output buffer empties again. This means that the app should
827 * assume that it can write since the first OUTPUT event, and no more
828 * OUTPUT events will be generated unless an error occurs.
830 * Connection succesfully established, for client sockets, or incoming
831 * client connection, for server sockets. Wait for this event (also watch
832 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
834 * The connection is lost (or a connection request failed); this could
835 * be due to a failure, or due to the peer closing it gracefully.
838 /* GSocket_SetCallback:
839 * Enables the callbacks specified by 'flags'. Note that 'flags'
840 * may be a combination of flags OR'ed toghether, so the same
841 * callback function can be made to accept different events.
842 * The callback function must have the following prototype:
844 * void function(GSocket *socket, GSocketEvent event, char *cdata)
846 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
847 GSocketCallback callback
, char *cdata
)
851 assert(socket
!= NULL
);
853 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
855 if ((flags
& (1 << count
)) != 0)
857 socket
->m_cbacks
[count
] = callback
;
858 socket
->m_data
[count
] = cdata
;
863 /* GSocket_UnsetCallback:
864 * Disables all callbacks specified by 'flags', which may be a
865 * combination of flags OR'ed toghether.
867 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
871 assert(socket
!= NULL
);
873 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
875 if ((flags
& (1 << count
)) != 0)
877 socket
->m_cbacks
[count
] = NULL
;
878 socket
->m_data
[count
] = NULL
;
886 /* _GSocket_Enable_Events:
887 * Enable all event notifications; we need to be notified of all
888 * events for internal processing, but we will only notify users
889 * when an appropiate callback function has been installed.
891 void _GSocket_Enable_Events(GSocket
*socket
)
893 assert (socket
!= NULL
);
895 if (socket
->m_fd
!= INVALID_SOCKET
)
897 WSAAsyncSelect(socket
->m_fd
, hWin
, socket
->m_msgnumber
,
898 FD_READ
| FD_WRITE
| FD_ACCEPT
| FD_CONNECT
| FD_CLOSE
);
902 /* _GSocket_Disable_Events:
903 * Disable event notifications (when shutdowning the socket)
905 void _GSocket_Disable_Events(GSocket
*socket
)
907 assert (socket
!= NULL
);
909 if (socket
->m_fd
!= INVALID_SOCKET
)
911 WSAAsyncSelect(socket
->m_fd
, hWin
, socket
->m_msgnumber
, 0);
916 LRESULT CALLBACK
_GSocket_Internal_WinProc(HWND hWnd
,
923 GSocketCallback cback
;
926 if (uMsg
>= WM_USER
&& uMsg
<= (WM_USER
+ MAXSOCKETS
- 1))
928 EnterCriticalSection(&critical
);
929 socket
= socketList
[(uMsg
- WM_USER
)];
934 /* Check that the socket still exists (it has not been
935 * destroyed) and for safety, check that the m_fd field
936 * is what we expect it to be.
938 if ((socket
!= NULL
) && (socket
->m_fd
== wParam
))
940 switch WSAGETSELECTEVENT(lParam
)
942 case FD_READ
: event
= GSOCK_INPUT
; break;
943 case FD_WRITE
: event
= GSOCK_OUTPUT
; break;
944 case FD_ACCEPT
: event
= GSOCK_CONNECTION
; break;
947 if (WSAGETSELECTERROR(lParam
) != 0)
950 event
= GSOCK_CONNECTION
;
953 case FD_CLOSE
: event
= GSOCK_LOST
; break;
958 cback
= socket
->m_cbacks
[event
];
959 data
= socket
->m_data
[event
];
961 if (event
== GSOCK_LOST
)
962 socket
->m_detected
= GSOCK_LOST_FLAG
;
964 socket
->m_detected
|= (1 << event
);
968 /* OK, we can now leave the critical section because we have
969 * already obtained the callback address (we make no further
970 * accesses to socket->whatever). However, the app should
971 * be prepared to handle events from a socket that has just
974 LeaveCriticalSection(&critical
);
977 (cback
)(socket
, event
, data
);
982 return DefWindowProc(hWnd
, uMsg
, wParam
, lParam
);
986 /* _GSocket_Input_Timeout:
987 * For blocking sockets, wait until data is available or
988 * until timeout ellapses.
990 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
994 if (!socket
->m_non_blocking
)
997 FD_SET(socket
->m_fd
, &readfds
);
998 if (select(0, &readfds
, NULL
, NULL
, &socket
->m_timeout
) == 0)
1000 socket
->m_error
= GSOCK_TIMEDOUT
;
1001 return GSOCK_TIMEDOUT
;
1004 return GSOCK_NOERROR
;
1007 /* _GSocket_Output_Timeout:
1008 * For blocking sockets, wait until data can be sent without
1009 * blocking or until timeout ellapses.
1011 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
1015 if (!socket
->m_non_blocking
)
1018 FD_SET(socket
->m_fd
, &writefds
);
1019 if (select(0, NULL
, &writefds
, NULL
, &socket
->m_timeout
) == 0)
1021 socket
->m_error
= GSOCK_TIMEDOUT
;
1022 return GSOCK_TIMEDOUT
;
1025 return GSOCK_NOERROR
;
1028 /* _GSocket_Connect_Timeout:
1029 * For blocking sockets, wait until the connection is
1030 * established or fails, or until timeout ellapses.
1032 GSocketError
_GSocket_Connect_Timeout(GSocket
*socket
)
1038 FD_ZERO(&exceptfds
);
1039 FD_SET(socket
->m_fd
, &writefds
);
1040 FD_SET(socket
->m_fd
, &exceptfds
);
1041 if (select(0, NULL
, &writefds
, &exceptfds
, &socket
->m_timeout
) == 0)
1043 socket
->m_error
= GSOCK_TIMEDOUT
;
1044 return GSOCK_TIMEDOUT
;
1046 if (!FD_ISSET(socket
->m_fd
, &writefds
))
1048 socket
->m_error
= GSOCK_IOERR
;
1052 return GSOCK_NOERROR
;
1055 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
1057 return recv(socket
->m_fd
, buffer
, size
, 0);
1060 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
1062 struct sockaddr from
;
1063 SOCKLEN_T fromlen
= sizeof(from
);
1067 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
1069 if (ret
== SOCKET_ERROR
)
1070 return SOCKET_ERROR
;
1072 /* Translate a system address into a GSocket address */
1073 if (!socket
->m_peer
)
1075 socket
->m_peer
= GAddress_new();
1076 if (!socket
->m_peer
)
1078 socket
->m_error
= GSOCK_MEMERR
;
1082 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
1083 if (err
!= GSOCK_NOERROR
)
1085 GAddress_destroy(socket
->m_peer
);
1086 socket
->m_peer
= NULL
;
1087 socket
->m_error
= err
;
1094 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
1096 return send(socket
->m_fd
, buffer
, size
, 0);
1099 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
1101 struct sockaddr
*addr
;
1105 if (!socket
->m_peer
)
1107 socket
->m_error
= GSOCK_INVADDR
;
1111 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
1112 if (err
!= GSOCK_NOERROR
)
1114 socket
->m_error
= err
;
1118 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
1120 /* Frees memory allocated by _GAddress_translate_to */
1128 * -------------------------------------------------------------------------
1130 * -------------------------------------------------------------------------
1133 /* CHECK_ADDRESS verifies that the current family is either GSOCK_NOFAMILY
1134 * or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it initalizes address
1135 * to be a GSOCK_*family*. In other cases, it returns GSOCK_INVADDR.
1137 #define CHECK_ADDRESS(address, family, retval) \
1139 if (address->m_family == GSOCK_NOFAMILY) \
1140 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1141 return address->m_error; \
1142 if (address->m_family != GSOCK_##family) \
1144 address->m_error = GSOCK_INVADDR; \
1149 GAddress
*GAddress_new()
1153 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1156 address
->m_family
= GSOCK_NOFAMILY
;
1157 address
->m_addr
= NULL
;
1163 GAddress
*GAddress_copy(GAddress
*address
)
1167 assert(address
!= NULL
);
1169 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1172 memcpy(addr2
, address
, sizeof(GAddress
));
1174 if (address
->m_addr
)
1176 addr2
->m_addr
= (struct sockaddr
*) malloc(addr2
->m_len
);
1177 if (addr2
->m_addr
== NULL
)
1182 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1188 void GAddress_destroy(GAddress
*address
)
1190 assert(address
!= NULL
);
1195 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1197 assert(address
!= NULL
);
1199 address
->m_family
= type
;
1202 GAddressType
GAddress_GetFamily(GAddress
*address
)
1204 assert(address
!= NULL
);
1206 return address
->m_family
;
1209 GSocketError
_GAddress_translate_from(GAddress
*address
,
1210 struct sockaddr
*addr
, int len
)
1212 address
->m_realfamily
= addr
->sa_family
;
1213 switch (addr
->sa_family
)
1216 address
->m_family
= GSOCK_INET
;
1219 address
->m_family
= GSOCK_UNIX
;
1223 address
->m_family
= GSOCK_INET6
;
1228 address
->m_error
= GSOCK_INVOP
;
1233 if (address
->m_addr
)
1234 free(address
->m_addr
);
1236 address
->m_len
= len
;
1237 address
->m_addr
= (struct sockaddr
*) malloc(len
);
1239 if (address
->m_addr
== NULL
)
1241 address
->m_error
= GSOCK_MEMERR
;
1242 return GSOCK_MEMERR
;
1244 memcpy(address
->m_addr
, addr
, len
);
1246 return GSOCK_NOERROR
;
1249 GSocketError
_GAddress_translate_to(GAddress
*address
,
1250 struct sockaddr
**addr
, int *len
)
1252 if (!address
->m_addr
)
1254 address
->m_error
= GSOCK_INVADDR
;
1255 return GSOCK_INVADDR
;
1258 *len
= address
->m_len
;
1259 *addr
= (struct sockaddr
*) malloc(address
->m_len
);
1262 address
->m_error
= GSOCK_MEMERR
;
1263 return GSOCK_MEMERR
;
1266 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1267 return GSOCK_NOERROR
;
1271 * -------------------------------------------------------------------------
1272 * Internet address family
1273 * -------------------------------------------------------------------------
1276 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1278 address
->m_len
= sizeof(struct sockaddr_in
);
1279 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1280 if (address
->m_addr
== NULL
)
1282 address
->m_error
= GSOCK_MEMERR
;
1283 return GSOCK_MEMERR
;
1286 address
->m_family
= GSOCK_INET
;
1287 address
->m_realfamily
= PF_INET
;
1288 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1289 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1291 return GSOCK_NOERROR
;
1294 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1297 struct in_addr
*addr
;
1299 assert(address
!= NULL
);
1301 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1303 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1305 addr
->s_addr
= inet_addr(hostname
);
1307 /* If it is a numeric host name, convert it now */
1308 if (addr
->s_addr
== INADDR_NONE
)
1310 struct in_addr
*array_addr
;
1312 /* It is a real name, we solve it */
1313 if ((he
= gethostbyname(hostname
)) == NULL
)
1315 /* addr->s_addr = INADDR_NONE just done by inet_addr() above */
1316 address
->m_error
= GSOCK_NOHOST
;
1317 return GSOCK_NOHOST
;
1319 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1320 addr
->s_addr
= array_addr
[0].s_addr
;
1322 return GSOCK_NOERROR
;
1325 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1327 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1330 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1331 unsigned long hostaddr
)
1333 struct in_addr
*addr
;
1335 assert(address
!= NULL
);
1337 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1339 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1340 addr
->s_addr
= hostaddr
;
1342 return GSOCK_NOERROR
;
1345 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1346 const char *protocol
)
1349 struct sockaddr_in
*addr
;
1351 assert(address
!= NULL
);
1352 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1356 address
->m_error
= GSOCK_INVPORT
;
1357 return GSOCK_INVPORT
;
1360 se
= getservbyname(port
, protocol
);
1363 if (isdigit(port
[0]))
1367 port_int
= atoi(port
);
1368 addr
= (struct sockaddr_in
*)address
->m_addr
;
1369 addr
->sin_port
= htons((u_short
) port_int
);
1370 return GSOCK_NOERROR
;
1373 address
->m_error
= GSOCK_INVPORT
;
1374 return GSOCK_INVPORT
;
1377 addr
= (struct sockaddr_in
*)address
->m_addr
;
1378 addr
->sin_port
= se
->s_port
;
1380 return GSOCK_NOERROR
;
1383 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1385 struct sockaddr_in
*addr
;
1387 assert(address
!= NULL
);
1388 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1390 addr
= (struct sockaddr_in
*)address
->m_addr
;
1391 addr
->sin_port
= htons(port
);
1393 return GSOCK_NOERROR
;
1396 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1400 struct sockaddr_in
*addr
;
1402 assert(address
!= NULL
);
1403 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1405 addr
= (struct sockaddr_in
*)address
->m_addr
;
1406 addr_buf
= (char *)&(addr
->sin_addr
);
1408 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1411 address
->m_error
= GSOCK_NOHOST
;
1412 return GSOCK_NOHOST
;
1415 strncpy(hostname
, he
->h_name
, sbuf
);
1417 return GSOCK_NOERROR
;
1420 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1422 struct sockaddr_in
*addr
;
1424 assert(address
!= NULL
);
1425 CHECK_ADDRESS(address
, INET
, 0);
1427 addr
= (struct sockaddr_in
*)address
->m_addr
;
1429 return addr
->sin_addr
.s_addr
;
1432 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1434 struct sockaddr_in
*addr
;
1436 assert(address
!= NULL
);
1437 CHECK_ADDRESS(address
, INET
, 0);
1439 addr
= (struct sockaddr_in
*)address
->m_addr
;
1440 return ntohs(addr
->sin_port
);
1444 * -------------------------------------------------------------------------
1445 * Unix address family
1446 * -------------------------------------------------------------------------
1449 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1451 assert (address
!= NULL
);
1452 address
->m_error
= GSOCK_INVADDR
;
1453 return GSOCK_INVADDR
;
1456 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1458 assert (address
!= NULL
);
1459 address
->m_error
= GSOCK_INVADDR
;
1460 return GSOCK_INVADDR
;
1463 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1465 assert (address
!= NULL
);
1466 address
->m_error
= GSOCK_INVADDR
;
1467 return GSOCK_INVADDR
;
1470 #else /* !wxUSE_SOCKETS */
1473 * Translation unit shouldn't be empty, so include this typedef to make the
1474 * compiler (VC++ 6.0, for example) happy
1476 typedef (*wxDummy
)();
1478 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */