1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket) for WX
4 * Purpose: GSocket main MSW file
6 * -------------------------------------------------------------------------
12 #include "wx/msw/gsockmsw.h"
13 #include "wx/gsocket.h"
15 #define INSTANCE wxhInstance
22 /* If not using wxWindows, a global var called hInst must
23 * be available and it must containt the app's instance
26 #define INSTANCE hInst
28 #endif /* __WXMSW__ */
31 #if !defined(__WXMSW__) || (defined(__WXMSW__) && wxUSE_SOCKETS)
42 #define CLASSNAME "_GSocket_Internal_Window_Class"
43 #define WINDOWNAME "_GSocket_Internal_Window_Name"
45 /* Maximum number of different GSocket objects at a given time.
46 * This value can be modified at will, but it CANNOT be greater
47 * than (0x7FFF - WM_USER + 1)
49 #define MAXSOCKETS 1024
51 #if (MAXSOCKETS > (0x7FFF - WM_USER + 1))
52 #error "MAXSOCKETS is too big!"
56 /* Global variables */
58 extern HINSTANCE INSTANCE
;
60 static CRITICAL_SECTION critical
;
61 static GSocket
* socketList
[MAXSOCKETS
];
62 static int firstAvailable
;
64 /* Global initializers */
72 /* Create internal window for event notifications */
74 winClass
.lpfnWndProc
= _GSocket_Internal_WinProc
;
75 winClass
.cbClsExtra
= 0;
76 winClass
.cbWndExtra
= 0;
77 winClass
.hInstance
= INSTANCE
;
78 winClass
.hIcon
= (HICON
) NULL
;
79 winClass
.hCursor
= (HCURSOR
) NULL
;
80 winClass
.hbrBackground
= (HBRUSH
) NULL
;
81 winClass
.lpszMenuName
= (LPCTSTR
) NULL
;
82 winClass
.lpszClassName
= CLASSNAME
;
84 RegisterClass(&winClass
);
85 hWin
= CreateWindow(CLASSNAME
,
88 (HWND
) NULL
, (HMENU
) NULL
, INSTANCE
, (LPVOID
) NULL
);
90 if (!hWin
) return FALSE
;
92 /* Initialize socket list */
93 InitializeCriticalSection(&critical
);
95 for (i
= 0; i
< MAXSOCKETS
; i
++)
101 /* Initialize WinSocket */
102 return (WSAStartup((1 << 8) | 1, &wsaData
) == 0);
105 void GSocket_Cleanup()
107 /* Destroy internal window */
109 UnregisterClass(CLASSNAME
, INSTANCE
);
111 /* Delete critical section */
112 DeleteCriticalSection(&critical
);
114 /* Cleanup WinSocket */
118 /* Constructors / Destructors */
120 GSocket
*GSocket_new()
125 if ((socket
= (GSocket
*) malloc(sizeof(GSocket
))) == NULL
)
128 socket
->m_fd
= INVALID_SOCKET
;
129 for (i
= 0; i
< GSOCK_MAX_EVENT
; i
++)
131 socket
->m_cbacks
[i
] = NULL
;
133 socket
->m_local
= NULL
;
134 socket
->m_peer
= NULL
;
135 socket
->m_error
= GSOCK_NOERROR
;
136 socket
->m_server
= FALSE
;
137 socket
->m_stream
= TRUE
;
138 socket
->m_non_blocking
= FALSE
;
139 socket
->m_timeout
.tv_sec
= 10 * 60; /* 10 minutes */
140 socket
->m_timeout
.tv_usec
= 0;
142 /* Allocate a new message number for this socket */
143 EnterCriticalSection(&critical
);
146 while (socketList
[i
] != NULL
)
148 i
= (i
+ 1) % MAXSOCKETS
;
150 if (i
== firstAvailable
) /* abort! */
153 LeaveCriticalSection(&critical
);
157 socketList
[i
] = socket
;
158 firstAvailable
= (i
+ 1) % MAXSOCKETS
;
159 socket
->m_msgnumber
= (i
+ WM_USER
);
161 LeaveCriticalSection(&critical
);
166 void GSocket_destroy(GSocket
*socket
)
168 assert(socket
!= NULL
);
170 /* We don't want more event notifications; so first of all we
171 * remove the socket from the list (NOTE: we won't get a CLOSE
172 * event for this socket if it wasn't closed before).
174 EnterCriticalSection(&critical
);
175 socketList
[(socket
->m_msgnumber
- WM_USER
)] = NULL
;
176 LeaveCriticalSection(&critical
);
178 /* We check that the socket is really shutdowned */
179 if (socket
->m_fd
!= INVALID_SOCKET
)
180 GSocket_Shutdown(socket
);
182 /* We destroy private addresses */
184 GAddress_destroy(socket
->m_local
);
187 GAddress_destroy(socket
->m_peer
);
189 /* We destroy socket itself */
193 void GSocket_Shutdown(GSocket
*socket
)
197 assert(socket
!= NULL
);
199 /* If socket has been created, we shutdown it */
200 if (socket
->m_fd
!= INVALID_SOCKET
)
202 shutdown(socket
->m_fd
, 2);
203 closesocket(socket
->m_fd
);
204 socket
->m_fd
= INVALID_SOCKET
;
207 /* We disable GUI callbacks */
208 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
210 socket
->m_cbacks
[evt
] = NULL
;
213 _GSocket_Configure_Callbacks(socket
);
216 /* Address handling */
218 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
220 assert(socket
!= NULL
);
222 if (socket
->m_fd
!= INVALID_SOCKET
&& !socket
->m_server
)
224 socket
->m_error
= GSOCK_INVSOCK
;
225 return GSOCK_INVSOCK
;
228 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
230 socket
->m_error
= GSOCK_INVADDR
;
231 return GSOCK_INVADDR
;
235 GAddress_destroy(socket
->m_local
);
237 socket
->m_local
= GAddress_copy(address
);
239 return GSOCK_NOERROR
;
242 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
244 assert(socket
!= NULL
);
246 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
248 socket
->m_error
= GSOCK_INVADDR
;
249 return GSOCK_INVADDR
;
253 GAddress_destroy(socket
->m_peer
);
255 socket
->m_peer
= GAddress_copy(address
);
257 return GSOCK_NOERROR
;
260 GAddress
*GSocket_GetLocal(GSocket
*socket
)
263 struct sockaddr addr
;
266 assert(socket
!= NULL
);
269 return GAddress_copy(socket
->m_local
);
271 if (socket
->m_fd
== INVALID_SOCKET
)
273 socket
->m_error
= GSOCK_INVSOCK
;
279 if (getsockname(socket
->m_fd
, &addr
, &size
) == SOCKET_ERROR
)
281 socket
->m_error
= GSOCK_IOERR
;
285 address
= GAddress_new();
288 socket
->m_error
= GSOCK_MEMERR
;
291 if (_GAddress_translate_from(address
, &addr
, size
) != GSOCK_NOERROR
)
293 socket
->m_error
= GSOCK_MEMERR
;
294 GAddress_destroy(address
);
301 GAddress
*GSocket_GetPeer(GSocket
*socket
)
303 assert(socket
!= NULL
);
306 return GAddress_copy(socket
->m_peer
);
311 /* Server specific parts */
313 /* GSocket_SetServer:
314 * Sets up the socket as a server. It uses the "Local" field of GSocket.
315 * "Local" must be set by GSocket_SetLocal() before GSocket_SetServer()
316 * is called. Possible error codes are: GSOCK_INVSOCK if socket has not
317 * been initialized, GSOCK_INVADDR if the local address has not been
318 * defined and GSOCK_IOERR for other internal errors.
320 GSocketError
GSocket_SetServer(GSocket
*sck
)
326 if (sck
->m_fd
!= INVALID_SOCKET
)
328 sck
->m_error
= GSOCK_INVSOCK
;
329 return GSOCK_INVSOCK
;
334 sck
->m_error
= GSOCK_INVADDR
;
335 return GSOCK_INVADDR
;
338 /* Initialize all fields */
339 sck
->m_server
= TRUE
;
340 sck
->m_stream
= TRUE
;
341 sck
->m_oriented
= TRUE
;
343 /* Create the socket */
344 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
345 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
347 if (sck
->m_fd
== INVALID_SOCKET
)
349 sck
->m_error
= GSOCK_IOERR
;
353 /* Bind the socket to the LOCAL address */
354 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0)
356 closesocket(sck
->m_fd
);
357 sck
->m_fd
= INVALID_SOCKET
;
358 sck
->m_error
= GSOCK_IOERR
;
362 /* Enable listening up to 5 connections */
363 if (listen(sck
->m_fd
, 5) != 0)
365 closesocket(sck
->m_fd
);
366 sck
->m_fd
= INVALID_SOCKET
;
367 sck
->m_error
= GSOCK_IOERR
;
371 return GSOCK_NOERROR
;
374 /* GSocket_WaitConnection:
375 * Waits for an incoming client connection.
377 GSocket
*GSocket_WaitConnection(GSocket
*sck
)
384 /* If the socket has already been created, we exit immediately */
385 if (sck
->m_fd
== INVALID_SOCKET
|| !sck
->m_server
)
387 sck
->m_error
= GSOCK_INVSOCK
;
391 /* Create a GSocket object for the new connection */
392 connection
= GSocket_new();
396 sck
->m_error
= GSOCK_MEMERR
;
400 /* Wait for a connection (with timeout) */
401 if (_GSocket_Input_Timeout(sck
) == GSOCK_TIMEDOUT
)
403 GSocket_destroy(connection
);
404 /* sck->m_error set by _GSocket_Input_Timeout */
408 connection
->m_fd
= accept(sck
->m_fd
, NULL
, NULL
);
410 if (connection
->m_fd
== INVALID_SOCKET
)
412 if (WSAGetLastError() == WSAEWOULDBLOCK
)
413 sck
->m_error
= GSOCK_WOULDBLOCK
;
415 sck
->m_error
= GSOCK_IOERR
;
417 GSocket_destroy(connection
);
421 /* Initialize all fields */
422 connection
->m_server
= FALSE
;
423 connection
->m_stream
= TRUE
;
424 connection
->m_oriented
= TRUE
;
426 ioctlsocket(connection
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
431 /* Non oriented connections */
433 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
439 if (sck
->m_fd
!= INVALID_SOCKET
)
441 sck
->m_error
= GSOCK_INVSOCK
;
442 return GSOCK_INVSOCK
;
447 sck
->m_error
= GSOCK_INVADDR
;
448 return GSOCK_INVADDR
;
451 /* Initialize all fields */
452 sck
->m_stream
= FALSE
;
453 sck
->m_server
= FALSE
;
454 sck
->m_oriented
= FALSE
;
456 /* Create the socket */
457 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
458 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
460 if (sck
->m_fd
== INVALID_SOCKET
)
462 sck
->m_error
= GSOCK_IOERR
;
466 /* Bind it to the LOCAL address */
467 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0)
469 closesocket(sck
->m_fd
);
470 sck
->m_fd
= INVALID_SOCKET
;
471 sck
->m_error
= GSOCK_IOERR
;
475 return GSOCK_NOERROR
;
478 GSocketError
GSocket_SetBroadcast(GSocket
*sck
)
484 if (GSocket_SetNonOriented(sck
) != GSOCK_NOERROR
)
488 setsockopt(sck
->m_fd
, SOL_SOCKET
, SO_BROADCAST
, (const char FAR
*) &b
, sizeof(b
));
490 return GSOCK_NOERROR
;
493 /* Client specific parts */
496 * Establishes a client connection to a server using the "Peer"
497 * field of GSocket. "Peer" must be set by GSocket_SetPeer() before
498 * GSocket_Connect() is called. Possible error codes are GSOCK_INVSOCK,
499 * GSOCK_INVADDR, GSOCK_TIMEDOUT, GSOCK_WOULDBLOCK and GSOCK_IOERR.
501 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
508 if (sck
->m_fd
!= INVALID_SOCKET
)
510 sck
->m_error
= GSOCK_INVSOCK
;
511 return GSOCK_INVSOCK
;
516 sck
->m_error
= GSOCK_INVADDR
;
517 return GSOCK_INVADDR
;
520 /* Test whether we want the socket to be a stream (e.g. TCP) */
521 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
522 sck
->m_oriented
= TRUE
;
523 sck
->m_server
= FALSE
;
530 /* Create the socket */
531 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
, type
, 0);
532 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
534 if (sck
->m_fd
== INVALID_SOCKET
)
536 sck
->m_error
= GSOCK_IOERR
;
540 /* Connect it to the PEER address, with a timeout (see below) */
541 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
543 if (ret
== SOCKET_ERROR
)
545 err
= WSAGetLastError();
547 /* If connect failed with EWOULDBLOCK and the GSocket object
548 * is in blocking mode, we select() for the specified timeout
549 * checking for writability to see if the connection request
552 if ((err
== WSAEWOULDBLOCK
) && (!sck
->m_non_blocking
))
554 if (_GSocket_Output_Timeout(sck
) == GSOCK_TIMEDOUT
)
556 closesocket(sck
->m_fd
);
557 sck
->m_fd
= INVALID_SOCKET
;
558 /* sck->m_error is set in _GSocket_Output_Timeout */
559 return GSOCK_TIMEDOUT
;
562 return GSOCK_NOERROR
;
565 /* If connect failed with EWOULDBLOCK and the GSocket object
566 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
567 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
568 * this way if the connection completes, a GSOCK_CONNECTION
569 * event will be generated, if enabled.
571 if ((err
== WSAEWOULDBLOCK
) && (sck
->m_non_blocking
))
573 sck
->m_error
= GSOCK_WOULDBLOCK
;
574 return GSOCK_WOULDBLOCK
;
577 /* If connect failed with an error other than EWOULDBLOCK,
578 * then the call to GSocket_Connect has failed.
580 closesocket(sck
->m_fd
);
581 sck
->m_fd
= INVALID_SOCKET
;
582 sck
->m_error
= GSOCK_IOERR
;
586 return GSOCK_NOERROR
;
591 /* Like recv(), send(), ... */
592 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
594 assert(socket
!= NULL
);
596 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
598 socket
->m_error
= GSOCK_INVSOCK
;
602 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
605 if (socket
->m_stream
)
606 return _GSocket_Recv_Stream(socket
, buffer
, size
);
608 return _GSocket_Recv_Dgram(socket
, buffer
, size
);
611 int GSocket_Write(GSocket
*socket
, const char *buffer
, int size
)
613 assert(socket
!= NULL
);
615 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
617 socket
->m_error
= GSOCK_INVSOCK
;
621 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
624 if (socket
->m_stream
)
625 return _GSocket_Send_Stream(socket
, buffer
, size
);
627 return _GSocket_Send_Dgram(socket
, buffer
, size
);
630 bool GSocket_DataAvailable(GSocket
*socket
)
635 assert(socket
!= NULL
);
637 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
639 socket
->m_error
= GSOCK_INVSOCK
;
644 FD_SET(socket
->m_fd
, &read_set
);
649 select(socket
->m_fd
+ 1, &read_set
, NULL
, NULL
, &tv
);
651 return FD_ISSET(socket
->m_fd
, &read_set
);
656 /* GSocket_SetNonBlocking:
657 * Sets the socket in non-blocking mode. This is useful if
658 * we don't want to wait.
660 void GSocket_SetNonBlocking(GSocket
*socket
, bool non_block
)
662 assert(socket
!= NULL
);
664 socket
->m_non_blocking
= non_block
;
667 /* GSocket_SetTimeout:
669 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisecs
)
671 assert(socket
!= NULL
);
673 socket
->m_timeout
.tv_sec
= (millisecs
/ 1000);
674 socket
->m_timeout
.tv_usec
= (millisecs
% 1000) * 1000;
678 * Returns the last error occured for this socket.
680 GSocketError
GSocket_GetError(GSocket
*socket
)
682 assert(socket
!= NULL
);
684 return socket
->m_error
;
689 /* Only one callback is possible for each event (INPUT, OUTPUT, CONNECTION
690 * and LOST). The callbacks are called in the following situations:
692 * INPUT: There is at least one byte in the input buffer
693 * OUTPUT: The system is sure that the next write call will not block
694 * CONNECTION: Two cases are possible:
695 * Client socket -> the connection is established
696 * Server socket -> a client requests a connection
697 * LOST: The connection is lost
699 * An event is generated only once and its state is reseted when the
700 * relative IO call is requested.
701 * For example: INPUT -> GSocket_Read()
702 * CONNECTION -> GSocket_Accept()
705 /* GSocket_SetCallback:
706 * Enables the callbacks specified by 'event'. Note that 'event'
707 * may be a combination of flags OR'ed toghether, so the same
708 * callback function can be made to accept different events.
709 * The callback function must have the following prototype:
711 * void function(GSocket *socket, GSocketEvent event, char *cdata)
713 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags event
,
714 GSocketCallback callback
, char *cdata
)
718 assert (socket
!= NULL
);
720 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
722 /* We test each flag and enable the corresponding events */
723 if ((event
& (1 << count
)) != 0)
725 socket
->m_cbacks
[count
] = callback
;
726 socket
->m_data
[count
] = cdata
;
730 _GSocket_Configure_Callbacks(socket
);
733 /* GSocket_UnsetCallback:
734 * Disables all callbacks specified by 'event', which may be a
735 * combination of flags OR'ed toghether.
737 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags event
)
741 assert(socket
!= NULL
);
743 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
745 /* We test each flag and disable the corresponding events */
746 if ((event
& (1 << count
)) != 0)
748 socket
->m_cbacks
[count
] = NULL
;
752 _GSocket_Configure_Callbacks(socket
);
758 void _GSocket_Configure_Callbacks(GSocket
*socket
)
763 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
765 if (socket
->m_cbacks
[count
] != NULL
)
769 case GSOCK_INPUT
: mask
|= FD_READ
; break;
770 case GSOCK_OUTPUT
: mask
|= FD_WRITE
; break;
771 case GSOCK_CONNECTION
: mask
|= (FD_ACCEPT
| FD_CONNECT
); break;
772 case GSOCK_LOST
: mask
|= FD_CLOSE
; break;
777 WSAAsyncSelect(socket
->m_fd
, hWin
, socket
->m_msgnumber
, mask
);
780 LRESULT CALLBACK
_GSocket_Internal_WinProc(HWND hWnd
,
787 GSocketCallback cback
;
789 if (uMsg
>= WM_USER
&& uMsg
<= (WM_USER
+ MAXSOCKETS
- 1))
791 EnterCriticalSection(&critical
);
792 socket
= socketList
[(uMsg
- WM_USER
)];
796 /* Check that the socket still exists (it has not been
797 * destroyed) and for safety, check that the m_fd field
798 * is what we expect it to be.
800 if ((socket
!= NULL
) && (socket
->m_fd
== wParam
))
802 switch WSAGETSELECTEVENT(lParam
)
804 case FD_READ
: event
= GSOCK_INPUT
; break;
805 case FD_WRITE
: event
= GSOCK_OUTPUT
; break;
806 case FD_ACCEPT
: event
= GSOCK_CONNECTION
; break;
809 if (WSAGETSELECTERROR(lParam
) != 0)
812 event
= GSOCK_CONNECTION
;
815 case FD_CLOSE
: event
= GSOCK_LOST
; break;
819 cback
= socket
->m_cbacks
[event
];
822 /* OK, we can now leave the critical section because we have
823 * already obtained the callback address (we make no further
824 * accesses to socket->whatever)
826 LeaveCriticalSection(&critical
);
829 (cback
)(socket
, event
, socket
->m_data
[event
]);
834 return DefWindowProc(hWnd
, uMsg
, wParam
, lParam
);
838 /* _GSocket_Input_Timeout:
839 * For blocking sockets, wait until data is available or
840 * until timeout ellapses.
842 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
846 if (!socket
->m_non_blocking
)
849 FD_SET(socket
->m_fd
, &readfds
);
850 if (select(0, &readfds
, NULL
, NULL
, &socket
->m_timeout
) == 0)
852 socket
->m_error
= GSOCK_TIMEDOUT
;
853 return GSOCK_TIMEDOUT
;
856 return GSOCK_NOERROR
;
859 /* _GSocket_Output_Timeout:
860 * For blocking sockets, wait until data can be sent without
861 * blocking or until timeout ellapses.
863 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
867 if (!socket
->m_non_blocking
)
870 FD_SET(socket
->m_fd
, &writefds
);
871 if (select(0, NULL
, &writefds
, NULL
, &socket
->m_timeout
) == 0)
873 socket
->m_error
= GSOCK_TIMEDOUT
;
874 return GSOCK_TIMEDOUT
;
877 return GSOCK_NOERROR
;
880 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
884 ret
= recv(socket
->m_fd
, buffer
, size
, 0);
886 if (ret
== SOCKET_ERROR
)
888 if (WSAGetLastError() != WSAEWOULDBLOCK
)
889 socket
->m_error
= GSOCK_IOERR
;
891 socket
->m_error
= GSOCK_WOULDBLOCK
;
899 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
901 struct sockaddr from
;
905 fromlen
= sizeof(from
);
907 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
909 if (ret
== SOCKET_ERROR
)
911 if (WSAGetLastError() != WSAEWOULDBLOCK
)
912 socket
->m_error
= GSOCK_IOERR
;
914 socket
->m_error
= GSOCK_WOULDBLOCK
;
919 /* Translate a system address into a GSocket address */
922 socket
->m_peer
= GAddress_new();
925 socket
->m_error
= GSOCK_MEMERR
;
929 if (_GAddress_translate_from(socket
->m_peer
, &from
, fromlen
) != GSOCK_NOERROR
)
931 socket
->m_error
= GSOCK_MEMERR
;
932 GAddress_destroy(socket
->m_peer
);
939 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
943 ret
= send(socket
->m_fd
, buffer
, size
, 0);
945 if (ret
== SOCKET_ERROR
)
947 if (WSAGetLastError() != WSAEWOULDBLOCK
)
948 socket
->m_error
= GSOCK_IOERR
;
950 socket
->m_error
= GSOCK_WOULDBLOCK
;
957 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
959 struct sockaddr
*addr
;
964 socket
->m_error
= GSOCK_INVADDR
;
968 if (!_GAddress_translate_to(socket
->m_peer
, &addr
, &len
))
970 socket
->m_error
= GSOCK_MEMERR
;
974 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
976 /* Frees memory allocated by _GAddress_translate_to */
979 if (ret
== SOCKET_ERROR
)
981 if (WSAGetLastError() != WSAEWOULDBLOCK
)
982 socket
->m_error
= GSOCK_IOERR
;
984 socket
->m_error
= GSOCK_WOULDBLOCK
;
993 * -------------------------------------------------------------------------
995 * -------------------------------------------------------------------------
998 /* CHECK_ADDRESS verifies that the current family is either GSOCK_NOFAMILY
999 * or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it initalizes address
1000 * to be a GSOCK_*family*. In other cases, it returns GSOCK_INVADDR.
1002 #define CHECK_ADDRESS(address, family, retval) \
1004 if (address->m_family == GSOCK_NOFAMILY) \
1005 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1006 return address->m_error; \
1007 if (address->m_family != GSOCK_##family) \
1009 address->m_error = GSOCK_INVADDR; \
1014 GAddress
*GAddress_new()
1018 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1021 address
->m_family
= GSOCK_NOFAMILY
;
1022 address
->m_addr
= NULL
;
1028 GAddress
*GAddress_copy(GAddress
*address
)
1032 assert(address
!= NULL
);
1034 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1037 memcpy(addr2
, address
, sizeof(GAddress
));
1039 if (address
->m_addr
)
1041 addr2
->m_addr
= (struct sockaddr
*) malloc(addr2
->m_len
);
1042 if (addr2
->m_addr
== NULL
)
1047 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1053 void GAddress_destroy(GAddress
*address
)
1055 assert(address
!= NULL
);
1060 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1062 assert(address
!= NULL
);
1064 address
->m_family
= type
;
1067 GAddressType
GAddress_GetFamily(GAddress
*address
)
1069 assert(address
!= NULL
);
1071 return address
->m_family
;
1074 GSocketError
_GAddress_translate_from(GAddress
*address
,
1075 struct sockaddr
*addr
, int len
)
1077 address
->m_realfamily
= addr
->sa_family
;
1078 switch (addr
->sa_family
)
1081 address
->m_family
= GSOCK_INET
;
1084 address
->m_family
= GSOCK_UNIX
;
1088 address
->m_family
= GSOCK_INET6
;
1093 address
->m_error
= GSOCK_INVOP
;
1098 if (address
->m_addr
)
1099 free(address
->m_addr
);
1101 address
->m_len
= len
;
1102 address
->m_addr
= (struct sockaddr
*) malloc(len
);
1104 if (address
->m_addr
== NULL
)
1106 address
->m_error
= GSOCK_MEMERR
;
1107 return GSOCK_MEMERR
;
1109 memcpy(address
->m_addr
, addr
, len
);
1111 return GSOCK_NOERROR
;
1114 GSocketError
_GAddress_translate_to(GAddress
*address
,
1115 struct sockaddr
**addr
, int *len
)
1117 if (!address
->m_addr
)
1119 address
->m_error
= GSOCK_INVADDR
;
1120 return GSOCK_INVADDR
;
1123 *len
= address
->m_len
;
1124 *addr
= (struct sockaddr
*) malloc(address
->m_len
);
1127 address
->m_error
= GSOCK_MEMERR
;
1128 return GSOCK_MEMERR
;
1131 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1132 return GSOCK_NOERROR
;
1136 * -------------------------------------------------------------------------
1137 * Internet address family
1138 * -------------------------------------------------------------------------
1141 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1143 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1144 if (address
->m_addr
== NULL
)
1146 address
->m_error
= GSOCK_MEMERR
;
1147 return GSOCK_MEMERR
;
1150 address
->m_len
= sizeof(struct sockaddr_in
);
1151 address
->m_family
= GSOCK_INET
;
1152 address
->m_realfamily
= PF_INET
;
1153 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1154 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1156 return GSOCK_NOERROR
;
1159 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1162 struct in_addr
*addr
;
1164 assert(address
!= NULL
);
1166 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1168 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1170 addr
->s_addr
= inet_addr(hostname
);
1172 /* If it is a numeric host name, convert it now */
1173 if (addr
->s_addr
== INADDR_NONE
)
1175 struct in_addr
*array_addr
;
1177 /* It is a real name, we solve it */
1178 if ((he
= gethostbyname(hostname
)) == NULL
)
1180 address
->m_error
= GSOCK_NOHOST
;
1181 return GSOCK_NOHOST
;
1183 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1184 addr
->s_addr
= array_addr
[0].s_addr
;
1186 return GSOCK_NOERROR
;
1189 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1190 unsigned long hostaddr
)
1192 struct in_addr
*addr
;
1194 assert(address
!= NULL
);
1196 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1198 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1199 addr
->s_addr
= hostaddr
;
1201 return GSOCK_NOERROR
;
1204 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1205 const char *protocol
)
1208 struct sockaddr_in
*addr
;
1210 assert(address
!= NULL
);
1211 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1215 address
->m_error
= GSOCK_INVPORT
;
1219 se
= getservbyname(port
, protocol
);
1222 if (isdigit(port
[0]))
1226 port_int
= atoi(port
);
1227 addr
= (struct sockaddr_in
*)address
->m_addr
;
1228 addr
->sin_port
= htons((u_short
) port_int
);
1229 return GSOCK_NOERROR
;
1232 address
->m_error
= GSOCK_INVPORT
;
1233 return GSOCK_INVPORT
;
1236 addr
= (struct sockaddr_in
*)address
->m_addr
;
1237 addr
->sin_port
= se
->s_port
;
1239 return GSOCK_NOERROR
;
1242 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1244 struct sockaddr_in
*addr
;
1246 assert(address
!= NULL
);
1247 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1249 addr
= (struct sockaddr_in
*)address
->m_addr
;
1250 addr
->sin_port
= htons(port
);
1252 return GSOCK_NOERROR
;
1255 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1259 struct sockaddr_in
*addr
;
1261 assert(address
!= NULL
);
1262 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1264 addr
= (struct sockaddr_in
*)address
->m_addr
;
1265 addr_buf
= (char *)&(addr
->sin_addr
);
1267 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1270 address
->m_error
= GSOCK_NOHOST
;
1271 return GSOCK_NOHOST
;
1274 strncpy(hostname
, he
->h_name
, sbuf
);
1276 return GSOCK_NOERROR
;
1279 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1281 struct sockaddr_in
*addr
;
1283 assert(address
!= NULL
);
1284 CHECK_ADDRESS(address
, INET
, 0);
1286 addr
= (struct sockaddr_in
*)address
->m_addr
;
1288 return addr
->sin_addr
.s_addr
;
1291 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1293 struct sockaddr_in
*addr
;
1295 assert(address
!= NULL
);
1296 CHECK_ADDRESS(address
, INET
, 0);
1298 addr
= (struct sockaddr_in
*)address
->m_addr
;
1299 return ntohs(addr
->sin_port
);
1303 * -------------------------------------------------------------------------
1304 * Unix address family
1305 * -------------------------------------------------------------------------
1308 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1310 assert (address
!= NULL
);
1311 address
->m_error
= GSOCK_INVADDR
;
1312 return GSOCK_INVADDR
;
1315 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1317 assert (address
!= NULL
);
1318 address
->m_error
= GSOCK_INVADDR
;
1319 return GSOCK_INVADDR
;
1322 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1324 assert (address
!= NULL
);
1325 address
->m_error
= GSOCK_INVADDR
;
1326 return GSOCK_INVADDR
;
1330 #endif /* !defined(__WXMSW__) || (defined(__WXMSW__) && wxUSE_SOCKETS) */
1334 /* Diferencias con la version Unix:
1335 * - El descriptor es SOCKET y no int
1336 * - Constantes -1 pasan a INVALID_SOCKET
1337 * - Errores en muchas funciones pasan de -1 o <0 a SOCKET_ERROR
1338 * - ioctl y close pasan a ioctlsocket y closesocket
1339 * - inet_addr en lugar de inet_aton
1340 * - Codigo de inicializacion y terminacion para inicializar y
1341 * terminar WinSocket y para la ventana interna.
1342 * - SetTimeout en la version MSW simplemente guarda el valor en
1343 * socket.m_timeout, por tanto no hay necesidad de llamar a
1344 * SetTimeout cada vez que se crea realmente un socket (no
1346 * - Lo mismo para SetNonBlocking.