1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket) for WX
4 * Purpose: GSocket main MSW file
6 * -------------------------------------------------------------------------
13 #include "wx/msw/gsockmsw.h"
14 #include "wx/gsocket.h"
16 #define INSTANCE wxhInstance
23 /* If not using wxWindows, a global var called hInst must
24 * be available and it must containt the app's instance
27 #define INSTANCE hInst
29 #endif /* __WXMSW__ */
32 #if !defined(__WXMSW__) || (defined(__WXMSW__) && wxUSE_SOCKETS)
43 #define CLASSNAME "_GSocket_Internal_Window_Class"
44 #define WINDOWNAME "_GSocket_Internal_Window_Name"
46 /* Maximum number of different GSocket objects at a given time.
47 * This value can be modified at will, but it CANNOT be greater
48 * than (0x7FFF - WM_USER + 1)
50 #define MAXSOCKETS 1024
52 #if (MAXSOCKETS > (0x7FFF - WM_USER + 1))
53 #error "MAXSOCKETS is too big!"
57 /* Global variables */
59 extern HINSTANCE INSTANCE
;
61 static CRITICAL_SECTION critical
;
62 static GSocket
* socketList
[MAXSOCKETS
];
63 static int firstAvailable
;
65 /* Global initializers */
73 /* Create internal window for event notifications */
75 winClass
.lpfnWndProc
= _GSocket_Internal_WinProc
;
76 winClass
.cbClsExtra
= 0;
77 winClass
.cbWndExtra
= 0;
78 winClass
.hInstance
= INSTANCE
;
79 winClass
.hIcon
= (HICON
) NULL
;
80 winClass
.hCursor
= (HCURSOR
) NULL
;
81 winClass
.hbrBackground
= (HBRUSH
) NULL
;
82 winClass
.lpszMenuName
= (LPCTSTR
) NULL
;
83 winClass
.lpszClassName
= CLASSNAME
;
85 RegisterClass(&winClass
);
86 hWin
= CreateWindow(CLASSNAME
,
89 (HWND
) NULL
, (HMENU
) NULL
, INSTANCE
, (LPVOID
) NULL
);
91 if (!hWin
) return FALSE
;
93 /* Initialize socket list */
94 InitializeCriticalSection(&critical
);
96 for (i
= 0; i
< MAXSOCKETS
; i
++)
103 /* Initialize WinSocket */
104 return (WSAStartup((1 << 8) | 1, &wsaData
) == 0);
107 void GSocket_Cleanup()
109 /* Destroy internal window */
110 DestroyWindow(WINDOWNAME
);
111 UnregisterClass(CLASSNAME
, INSTANCE
);
113 /* Delete critical section */
114 DeleteCriticalSection(&critical
);
116 /* Cleanup WinSocket */
120 /* Constructors / Destructors */
122 GSocket
*GSocket_new()
127 if ((socket
= (GSocket
*) malloc(sizeof(GSocket
))) == NULL
)
130 socket
->m_fd
= INVALID_SOCKET
;
131 for (i
= 0; i
< GSOCK_MAX_EVENT
; i
++)
133 socket
->m_cbacks
[i
] = NULL
;
135 socket
->m_local
= NULL
;
136 socket
->m_peer
= NULL
;
137 socket
->m_error
= GSOCK_NOERROR
;
138 socket
->m_server
= FALSE
;
139 socket
->m_stream
= TRUE
;
140 socket
->m_non_blocking
= FALSE
;
141 socket
->m_timeout
.tv_sec
= 10 * 60; /* 10 minutes */
142 socket
->m_timeout
.tv_usec
= 0;
144 /* Allocate a new message number for this socket */
145 EnterCriticalSection(&critical
);
148 while (socketList
[i
] != NULL
)
150 i
= (i
+ 1) % MAXSOCKETS
;
152 if (i
== firstAvailable
) /* abort! */
155 LeaveCriticalSection(&critical
);
159 socketList
[i
] = socket
;
160 firstAvailable
= (i
+ 1) % MAXSOCKETS
;
161 socket
->m_msgnumber
= (i
+ WM_USER
);
163 LeaveCriticalSection(&critical
);
168 void GSocket_destroy(GSocket
*socket
)
170 assert(socket
!= NULL
);
172 /* We don't want more event notifications; so first of all we
173 * remove the socket from the list (NOTE: we won't get a CLOSE
174 * event for this socket if it wasn't closed before).
176 EnterCriticalSection(&critical
);
177 socketList
[(socket
->m_msgnumber
- WM_USER
)] = NULL
;
178 LeaveCriticalSection(&critical
);
180 /* We check that the socket is really shutdowned */
181 if (socket
->m_fd
!= INVALID_SOCKET
)
182 GSocket_Shutdown(socket
);
184 /* We destroy private addresses */
186 GAddress_destroy(socket
->m_local
);
189 GAddress_destroy(socket
->m_peer
);
191 /* We destroy socket itself */
195 void GSocket_Shutdown(GSocket
*socket
)
199 assert(socket
!= NULL
);
201 /* If socket has been created, we shutdown it */
202 if (socket
->m_fd
!= INVALID_SOCKET
)
204 /* TODO: Guilhem only does this for connection oriented sockets (?) */
205 shutdown(socket
->m_fd
, 2);
206 closesocket(socket
->m_fd
);
207 socket
->m_fd
= INVALID_SOCKET
;
210 /* We disable GUI callbacks */
211 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
213 socket
->m_cbacks
[evt
] = NULL
;
216 _GSocket_Configure_Callbacks(socket
);
219 /* Address handling */
221 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
223 assert(socket
!= NULL
);
225 if (socket
->m_fd
!= INVALID_SOCKET
&& !socket
->m_server
)
227 socket
->m_error
= GSOCK_INVSOCK
;
228 return GSOCK_INVSOCK
;
231 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
233 socket
->m_error
= GSOCK_INVADDR
;
234 return GSOCK_INVADDR
;
238 GAddress_destroy(socket
->m_local
);
240 socket
->m_local
= GAddress_copy(address
);
242 return GSOCK_NOERROR
;
245 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
247 assert(socket
!= NULL
);
249 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
251 socket
->m_error
= GSOCK_INVADDR
;
252 return GSOCK_INVADDR
;
256 GAddress_destroy(socket
->m_peer
);
258 socket
->m_peer
= GAddress_copy(address
);
260 return GSOCK_NOERROR
;
263 GAddress
*GSocket_GetLocal(GSocket
*socket
)
266 struct sockaddr addr
;
269 assert(socket
!= NULL
);
272 return GAddress_copy(socket
->m_local
);
274 if (socket
->m_fd
== INVALID_SOCKET
)
276 socket
->m_error
= GSOCK_INVSOCK
;
282 if (getsockname(socket
->m_fd
, &addr
, &size
) == SOCKET_ERROR
)
284 socket
->m_error
= GSOCK_IOERR
;
288 address
= GAddress_new();
291 socket
->m_error
= GSOCK_MEMERR
;
294 if (_GAddress_translate_from(address
, &addr
, size
) != GSOCK_NOERROR
)
296 socket
->m_error
= GSOCK_MEMERR
;
297 GAddress_destroy(address
);
304 GAddress
*GSocket_GetPeer(GSocket
*socket
)
306 assert(socket
!= NULL
);
309 return GAddress_copy(socket
->m_peer
);
314 /* Server specific parts */
316 /* GSocket_SetServer:
317 * Sets up the socket as a server. It uses the "Local" field of GSocket.
318 * "Local" must be set by GSocket_SetLocal() before GSocket_SetServer()
319 * is called. Possible error codes are: GSOCK_INVSOCK if socket has not
320 * been initialized, GSOCK_INVADDR if the local address has not been
321 * defined and GSOCK_IOERR for other internal errors.
323 GSocketError
GSocket_SetServer(GSocket
*sck
)
329 if (sck
->m_fd
!= INVALID_SOCKET
)
331 sck
->m_error
= GSOCK_INVSOCK
;
332 return GSOCK_INVSOCK
;
337 sck
->m_error
= GSOCK_INVADDR
;
338 return GSOCK_INVADDR
;
341 /* Initialize all fields */
342 sck
->m_server
= TRUE
;
343 sck
->m_stream
= TRUE
;
344 sck
->m_oriented
= TRUE
;
346 /* Create the socket */
347 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
348 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
350 if (sck
->m_fd
== INVALID_SOCKET
)
352 sck
->m_error
= GSOCK_IOERR
;
356 /* Bind the socket to the LOCAL address */
357 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0)
359 closesocket(sck
->m_fd
);
360 sck
->m_fd
= INVALID_SOCKET
;
361 sck
->m_error
= GSOCK_IOERR
;
365 /* Enable listening up to 5 connections */
366 if (listen(sck
->m_fd
, 5) != 0)
368 closesocket(sck
->m_fd
);
369 sck
->m_fd
= INVALID_SOCKET
;
370 sck
->m_error
= GSOCK_IOERR
;
374 return GSOCK_NOERROR
;
377 /* GSocket_WaitConnection:
378 * Waits for an incoming client connection.
380 GSocket
*GSocket_WaitConnection(GSocket
*sck
)
387 /* If the socket has already been created, we exit immediately */
388 if (sck
->m_fd
== INVALID_SOCKET
|| !sck
->m_server
)
390 sck
->m_error
= GSOCK_INVSOCK
;
394 /* Create a GSocket object for the new connection */
395 connection
= GSocket_new();
399 sck
->m_error
= GSOCK_MEMERR
;
403 /* Wait for a connection (with timeout) */
404 if (_GSocket_Input_Timeout(sck
) == GSOCK_TIMEDOUT
)
406 GSocket_destroy(connection
);
407 /* sck->m_error set by _GSocket_Input_Timeout */
411 connection
->m_fd
= accept(sck
->m_fd
, NULL
, NULL
);
412 ioctlsocket(connection
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
414 if (connection
->m_fd
== INVALID_SOCKET
)
416 GSocket_destroy(connection
);
417 sck
->m_error
= GSOCK_IOERR
;
421 /* Initialize all fields */
422 connection
->m_server
= FALSE
;
423 connection
->m_stream
= TRUE
;
424 connection
->m_oriented
= TRUE
;
429 /* Non oriented connections */
431 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
437 if (sck
->m_fd
!= INVALID_SOCKET
)
439 sck
->m_error
= GSOCK_INVSOCK
;
440 return GSOCK_INVSOCK
;
445 sck
->m_error
= GSOCK_INVADDR
;
446 return GSOCK_INVADDR
;
449 /* Initialize all fields */
450 sck
->m_stream
= FALSE
;
451 sck
->m_server
= FALSE
;
452 sck
->m_oriented
= FALSE
;
454 /* Create the socket */
455 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
456 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
458 if (sck
->m_fd
== INVALID_SOCKET
)
460 sck
->m_error
= GSOCK_IOERR
;
464 /* Bind it to the LOCAL address */
465 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0)
467 closesocket(sck
->m_fd
);
468 sck
->m_fd
= INVALID_SOCKET
;
469 sck
->m_error
= GSOCK_IOERR
;
473 return GSOCK_NOERROR
;
476 GSocketError
GSocket_SetBroadcast(GSocket
*sck
)
482 if (GSocket_SetNonOriented(sck
) != GSOCK_NOERROR
)
486 setsockopt(sck
->m_fd
, SOL_SOCKET
, SO_BROADCAST
, (const char FAR
*) &b
, sizeof(b
));
488 return GSOCK_NOERROR
;
491 /* Client specific parts */
494 * Establishes a client connection to a server using the "Peer"
495 * field of GSocket. "Peer" must be set by GSocket_SetPeer() before
496 * GSocket_Connect() is called. Possible error codes are GSOCK_INVSOCK
497 * if the socket is alredy in use, GSOCK_INVADDR if the peer address
498 * has not been set, or GSOCK_IOERR for other internal errors.
500 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
507 if (sck
->m_fd
!= INVALID_SOCKET
)
509 sck
->m_error
= GSOCK_INVSOCK
;
510 return GSOCK_INVSOCK
;
515 sck
->m_error
= GSOCK_INVADDR
;
516 return GSOCK_INVADDR
;
519 /* Test whether we want the socket to be a stream (e.g. TCP) */
520 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
521 sck
->m_oriented
= TRUE
;
522 sck
->m_server
= FALSE
;
529 /* Create the socket */
530 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
, type
, 0);
531 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
533 if (sck
->m_fd
== INVALID_SOCKET
)
535 sck
->m_error
= GSOCK_IOERR
;
539 /* Connect it to the PEER address, with a timeout */
540 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
542 if (ret
== SOCKET_ERROR
)
544 /* For blocking GSockets, if connect fails with an EWOULDBLOCK
545 * error, then we can select() the socket for the specified
546 * timeout and check for writability to see if the connection
547 * request completes. If the error is different than EWOULDBLOCK,
548 * or if the socket is nonblocking, the call to GSocket_Connect()
551 if ((!sck
->m_non_blocking
) && (WSAGetLastError() == WSAEWOULDBLOCK
))
553 if (_GSocket_Output_Timeout(sck
) == GSOCK_TIMEDOUT
)
555 closesocket(sck
->m_fd
);
556 sck
->m_fd
= INVALID_SOCKET
;
557 /* sck->m_error is set in _GSocket_Input_Timeout */
558 return GSOCK_TIMEDOUT
;
563 closesocket(sck
->m_fd
);
564 sck
->m_fd
= INVALID_SOCKET
;
565 sck
->m_error
= GSOCK_IOERR
;
570 return GSOCK_NOERROR
;
575 /* Like recv(), send(), ... */
576 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
578 assert(socket
!= NULL
);
580 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
582 socket
->m_error
= GSOCK_INVSOCK
;
586 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
589 if (socket
->m_stream
)
590 return _GSocket_Recv_Stream(socket
, buffer
, size
);
592 return _GSocket_Recv_Dgram(socket
, buffer
, size
);
595 int GSocket_Write(GSocket
*socket
, const char *buffer
, int size
)
597 assert(socket
!= NULL
);
599 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
601 socket
->m_error
= GSOCK_INVSOCK
;
605 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
608 if (socket
->m_stream
)
609 return _GSocket_Send_Stream(socket
, buffer
, size
);
611 return _GSocket_Send_Dgram(socket
, buffer
, size
);
614 bool GSocket_DataAvailable(GSocket
*socket
)
619 assert(socket
!= NULL
);
621 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
623 socket
->m_error
= GSOCK_INVSOCK
;
628 FD_SET(socket
->m_fd
, &read_set
);
633 select(socket
->m_fd
+ 1, &read_set
, NULL
, NULL
, &tv
);
635 return FD_ISSET(socket
->m_fd
, &read_set
);
640 /* GSocket_SetNonBlocking:
641 * Sets the socket in non-blocking mode. This is useful if
642 * we don't want to wait.
644 void GSocket_SetNonBlocking(GSocket
*socket
, bool non_block
)
646 assert(socket
!= NULL
);
648 socket
->m_non_blocking
= non_block
;
651 /* GSocket_SetTimeout:
653 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisecs
)
655 assert(socket
!= NULL
);
657 socket
->m_timeout
.tv_sec
= (millisecs
/ 1000);
658 socket
->m_timeout
.tv_usec
= (millisecs
% 1000) * 1000;
662 * Returns the last error occured for this socket.
664 GSocketError
GSocket_GetError(GSocket
*socket
)
666 assert(socket
!= NULL
);
668 return socket
->m_error
;
673 /* Only one callback is possible for each event (INPUT, OUTPUT, CONNECTION
674 * and LOST). The callbacks are called in the following situations:
676 * INPUT: There is at least one byte in the input buffer
677 * OUTPUT: The system is sure that the next write call will not block
678 * CONNECTION: Two cases are possible:
679 * Client socket -> the connection is established
680 * Server socket -> a client requests a connection
681 * LOST: The connection is lost
683 * An event is generated only once and its state is reseted when the
684 * relative IO call is requested.
685 * For example: INPUT -> GSocket_Read()
686 * CONNECTION -> GSocket_Accept()
689 /* GSocket_SetCallback:
690 * Enables the callbacks specified by 'event'. Note that 'event'
691 * may be a combination of flags OR'ed toghether, so the same
692 * callback function can be made to accept different events.
693 * The callback function must have the following prototype:
695 * void function(GSocket *socket, GSocketEvent event, char *cdata)
697 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags event
,
698 GSocketCallback callback
, char *cdata
)
702 assert (socket
!= NULL
);
704 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
706 /* We test each flag and enable the corresponding events */
707 if ((event
& (1 << count
)) != 0)
709 socket
->m_cbacks
[count
] = callback
;
710 socket
->m_data
[count
] = cdata
;
714 _GSocket_Configure_Callbacks(socket
);
717 /* GSocket_UnsetCallback:
718 * Disables all callbacks specified by 'event', which may be a
719 * combination of flags OR'ed toghether.
721 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags event
)
725 assert(socket
!= NULL
);
727 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
729 /* We test each flag and disable the corresponding events */
730 if ((event
& (1 << count
)) != 0)
732 socket
->m_cbacks
[count
] = NULL
;
736 _GSocket_Configure_Callbacks(socket
);
742 void _GSocket_Configure_Callbacks(GSocket
*socket
)
747 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
749 if (socket
->m_cbacks
[count
] != NULL
)
753 case GSOCK_INPUT
: mask
|= FD_READ
; break;
754 case GSOCK_OUTPUT
: mask
|= FD_WRITE
; break;
755 case GSOCK_CONNECTION
: mask
|= (FD_ACCEPT
| FD_CONNECT
); break;
756 case GSOCK_LOST
: mask
|= FD_CLOSE
; break;
761 WSAAsyncSelect(socket
->m_fd
, hWin
, socket
->m_msgnumber
, mask
);
764 LRESULT CALLBACK
_GSocket_Internal_WinProc(HWND hWnd
,
771 GSocketCallback cback
;
773 if (uMsg
>= WM_USER
&& uMsg
<= (WM_USER
+ MAXSOCKETS
- 1))
775 EnterCriticalSection(&critical
);
776 socket
= socketList
[(uMsg
- WM_USER
)];
780 /* Check that the socket still exists (it has not been
781 * destroyed) and for safety, check that the m_fd field
782 * is what we expect it to be.
784 if ((socket
!= NULL
) && (socket
->m_fd
== wParam
))
786 switch WSAGETSELECTEVENT(lParam
)
788 case FD_READ
: event
= GSOCK_INPUT
; break;
789 case FD_WRITE
: event
= GSOCK_OUTPUT
; break;
790 case FD_ACCEPT
: event
= GSOCK_CONNECTION
; break;
791 case FD_CONNECT
: event
= GSOCK_CONNECTION
; break;
792 case FD_CLOSE
: event
= GSOCK_LOST
; break;
796 cback
= socket
->m_cbacks
[event
];
799 /* OK, we can now leave the critical section because we have
800 * already obtained the callback address (we make no further
801 * accesses to socket->whatever)
803 LeaveCriticalSection(&critical
);
806 (cback
)(socket
, event
, socket
->m_data
[event
]);
811 return DefWindowProc(hWnd
, uMsg
, wParam
, lParam
);
815 /* _GSocket_Input_Timeout:
816 * For blocking sockets, wait until data is available or
817 * until timeout ellapses.
819 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
823 if (socket
->m_non_blocking
== FALSE
)
826 FD_SET(socket
->m_fd
, &readfds
);
827 if (select(0, &readfds
, NULL
, NULL
, &socket
->m_timeout
) == 0)
829 socket
->m_error
= GSOCK_TIMEDOUT
;
830 return GSOCK_TIMEDOUT
;
833 return GSOCK_NOERROR
;
836 /* _GSocket_Output_Timeout:
837 * For blocking sockets, wait until data can be sent without
838 * blocking or until timeout ellapses.
840 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
844 if (socket
->m_non_blocking
== FALSE
)
847 FD_SET(socket
->m_fd
, &writefds
);
848 if (select(0, NULL
, &writefds
, NULL
, &socket
->m_timeout
) == 0)
850 socket
->m_error
= GSOCK_TIMEDOUT
;
851 return GSOCK_TIMEDOUT
;
854 return GSOCK_NOERROR
;
857 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
861 ret
= recv(socket
->m_fd
, buffer
, size
, 0);
863 if (ret
== SOCKET_ERROR
)
865 if (WSAGetLastError() != WSAEWOULDBLOCK
)
866 socket
->m_error
= GSOCK_IOERR
;
868 socket
->m_error
= GSOCK_WOULDBLOCK
;
876 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
878 struct sockaddr from
;
882 fromlen
= sizeof(from
);
884 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
886 if (ret
== SOCKET_ERROR
)
888 if (WSAGetLastError() != WSAEWOULDBLOCK
)
889 socket
->m_error
= GSOCK_IOERR
;
891 socket
->m_error
= GSOCK_WOULDBLOCK
;
896 /* Translate a system address into a GSocket address */
899 socket
->m_peer
= GAddress_new();
902 socket
->m_error
= GSOCK_MEMERR
;
906 if (_GAddress_translate_from(socket
->m_peer
, &from
, fromlen
) != GSOCK_NOERROR
)
908 socket
->m_error
= GSOCK_MEMERR
; /* TODO: bug in Unix GSocket! */
909 GAddress_destroy(socket
->m_peer
); /* TODO: bug in Unix GSocket! */
916 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
920 ret
= send(socket
->m_fd
, buffer
, size
, 0);
922 if (ret
== SOCKET_ERROR
)
924 if (WSAGetLastError() != WSAEWOULDBLOCK
)
925 socket
->m_error
= GSOCK_IOERR
;
927 socket
->m_error
= GSOCK_WOULDBLOCK
;
934 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
936 struct sockaddr
*addr
;
941 socket
->m_error
= GSOCK_INVADDR
;
945 if (!_GAddress_translate_to(socket
->m_peer
, &addr
, &len
))
947 socket
->m_error
= GSOCK_MEMERR
;
951 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
953 /* Frees memory allocated by _GAddress_translate_to */
956 if (ret
== SOCKET_ERROR
)
958 if (WSAGetLastError() != WSAEWOULDBLOCK
)
959 socket
->m_error
= GSOCK_IOERR
;
961 socket
->m_error
= GSOCK_WOULDBLOCK
;
970 * -------------------------------------------------------------------------
972 * -------------------------------------------------------------------------
975 /* CHECK_ADDRESS verifies that the current family is either GSOCK_NOFAMILY
976 * or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it initalizes address
977 * to be a GSOCK_*family*. In other cases, it returns GSOCK_INVADDR.
979 #define CHECK_ADDRESS(address, family, retval) \
981 if (address->m_family == GSOCK_NOFAMILY) \
982 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
983 return address->m_error; \
984 if (address->m_family != GSOCK_##family) \
986 address->m_error = GSOCK_INVADDR; \
991 GAddress
*GAddress_new()
995 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
998 address
->m_family
= GSOCK_NOFAMILY
;
999 address
->m_addr
= NULL
;
1005 GAddress
*GAddress_copy(GAddress
*address
)
1009 assert(address
!= NULL
);
1011 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1014 memcpy(addr2
, address
, sizeof(GAddress
));
1016 if (address
->m_addr
)
1018 addr2
->m_addr
= (struct sockaddr
*) malloc(addr2
->m_len
);
1019 if (addr2
->m_addr
== NULL
)
1024 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1030 void GAddress_destroy(GAddress
*address
)
1032 assert(address
!= NULL
);
1037 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1039 assert(address
!= NULL
);
1041 address
->m_family
= type
;
1044 GAddressType
GAddress_GetFamily(GAddress
*address
)
1046 assert(address
!= NULL
);
1048 return address
->m_family
;
1051 GSocketError
_GAddress_translate_from(GAddress
*address
,
1052 struct sockaddr
*addr
, int len
)
1054 address
->m_realfamily
= addr
->sa_family
;
1055 switch (addr
->sa_family
)
1058 address
->m_family
= GSOCK_INET
;
1061 address
->m_family
= GSOCK_UNIX
;
1065 address
->m_family
= GSOCK_INET6
;
1070 address
->m_error
= GSOCK_INVOP
;
1075 if (address
->m_addr
)
1076 free(address
->m_addr
);
1078 address
->m_len
= len
;
1079 address
->m_addr
= (struct sockaddr
*) malloc(len
);
1081 if (address
->m_addr
== NULL
)
1083 address
->m_error
= GSOCK_MEMERR
;
1084 return GSOCK_MEMERR
;
1086 memcpy(address
->m_addr
, addr
, len
);
1088 return GSOCK_NOERROR
;
1091 GSocketError
_GAddress_translate_to(GAddress
*address
,
1092 struct sockaddr
**addr
, int *len
)
1094 if (!address
->m_addr
)
1096 address
->m_error
= GSOCK_INVADDR
;
1097 return GSOCK_INVADDR
;
1100 *len
= address
->m_len
;
1101 *addr
= (struct sockaddr
*) malloc(address
->m_len
);
1104 address
->m_error
= GSOCK_MEMERR
;
1105 return GSOCK_MEMERR
;
1108 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1109 return GSOCK_NOERROR
;
1113 * -------------------------------------------------------------------------
1114 * Internet address family
1115 * -------------------------------------------------------------------------
1118 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1120 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1121 if (address
->m_addr
== NULL
)
1123 address
->m_error
= GSOCK_MEMERR
;
1124 return GSOCK_MEMERR
;
1127 address
->m_len
= sizeof(struct sockaddr_in
);
1128 address
->m_family
= GSOCK_INET
;
1129 address
->m_realfamily
= PF_INET
;
1130 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1131 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1133 return GSOCK_NOERROR
;
1136 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1139 struct in_addr
*addr
;
1141 assert(address
!= NULL
);
1143 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1145 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1147 addr
->s_addr
= inet_addr(hostname
);
1149 /* If it is a numeric host name, convert it now */
1150 if (addr
->s_addr
== INADDR_NONE
)
1152 struct in_addr
*array_addr
;
1154 /* It is a real name, we solve it */
1155 if ((he
= gethostbyname(hostname
)) == NULL
)
1157 address
->m_error
= GSOCK_NOHOST
;
1158 return GSOCK_NOHOST
;
1160 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1161 addr
->s_addr
= array_addr
[0].s_addr
;
1163 return GSOCK_NOERROR
;
1166 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1167 unsigned long hostaddr
)
1169 struct in_addr
*addr
;
1171 assert(address
!= NULL
);
1173 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1175 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1176 addr
->s_addr
= hostaddr
;
1178 return GSOCK_NOERROR
;
1181 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1182 const char *protocol
)
1185 struct sockaddr_in
*addr
;
1187 assert(address
!= NULL
);
1188 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1192 address
->m_error
= GSOCK_INVPORT
;
1196 se
= getservbyname(port
, protocol
);
1199 if (isdigit(port
[0]))
1203 port_int
= atoi(port
);
1204 addr
= (struct sockaddr_in
*)address
->m_addr
;
1205 addr
->sin_port
= htons(port_int
);
1206 return GSOCK_NOERROR
;
1209 address
->m_error
= GSOCK_INVPORT
;
1210 return GSOCK_INVPORT
;
1213 addr
= (struct sockaddr_in
*)address
->m_addr
;
1214 addr
->sin_port
= se
->s_port
;
1216 return GSOCK_NOERROR
;
1219 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1221 struct sockaddr_in
*addr
;
1223 assert(address
!= NULL
);
1224 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1226 addr
= (struct sockaddr_in
*)address
->m_addr
;
1227 addr
->sin_port
= htons(port
);
1229 return GSOCK_NOERROR
;
1232 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1236 struct sockaddr_in
*addr
;
1238 assert(address
!= NULL
);
1239 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1241 addr
= (struct sockaddr_in
*)address
->m_addr
;
1242 addr_buf
= (char *)&(addr
->sin_addr
);
1244 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1247 address
->m_error
= GSOCK_NOHOST
;
1248 return GSOCK_NOHOST
;
1251 strncpy(hostname
, he
->h_name
, sbuf
);
1253 return GSOCK_NOERROR
;
1256 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1258 struct sockaddr_in
*addr
;
1260 assert(address
!= NULL
);
1261 CHECK_ADDRESS(address
, INET
, 0);
1263 addr
= (struct sockaddr_in
*)address
->m_addr
;
1265 return addr
->sin_addr
.s_addr
;
1268 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1270 struct sockaddr_in
*addr
;
1272 assert(address
!= NULL
);
1273 CHECK_ADDRESS(address
, INET
, 0);
1275 addr
= (struct sockaddr_in
*)address
->m_addr
;
1276 return ntohs(addr
->sin_port
);
1280 * -------------------------------------------------------------------------
1281 * Unix address family
1282 * -------------------------------------------------------------------------
1285 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1287 assert (address
!= NULL
);
1288 address
->m_error
= GSOCK_INVADDR
;
1289 return GSOCK_INVADDR
;
1292 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1294 assert (address
!= NULL
);
1295 address
->m_error
= GSOCK_INVADDR
;
1296 return GSOCK_INVADDR
;
1299 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1301 assert (address
!= NULL
);
1302 address
->m_error
= GSOCK_INVADDR
;
1303 return GSOCK_INVADDR
;
1307 #endif /* !defined(__WXMSW__) || (defined(__WXMSW__) && wxUSE_SOCKETS) */
1311 /* Diferencias con la version Unix:
1312 * - El descriptor es SOCKET y no int
1313 * - Constantes -1 pasan a INVALID_SOCKET
1314 * - Errores en muchas funciones pasan de -1 o <0 a SOCKET_ERROR
1315 * - ioctl y close pasan a ioctlsocket y closesocket
1316 * - inet_addr en lugar de inet_aton
1317 * - Codigo de inicializacion y terminacion para inicializar y
1318 * terminar WinSocket y para la ventana interna.
1319 * - SetTimeout en la version MSW simplemente guarda el valor en
1320 * socket.m_timeout, por tanto no hay necesidad de llamar a
1321 * SetTimeout cada vez que se crea realmente un socket (no
1323 * - Lo mismo para SetNonBlocking.