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
++)
102 /* Initialize WinSocket */
103 return (WSAStartup((1 << 8) | 1, &wsaData
) == 0);
106 void GSocket_Cleanup()
108 /* Destroy internal window */
110 UnregisterClass(CLASSNAME
, INSTANCE
);
112 /* Delete critical section */
113 DeleteCriticalSection(&critical
);
115 /* Cleanup WinSocket */
119 /* Constructors / Destructors */
121 GSocket
*GSocket_new()
126 if ((socket
= (GSocket
*) malloc(sizeof(GSocket
))) == NULL
)
129 socket
->m_fd
= INVALID_SOCKET
;
130 for (i
= 0; i
< GSOCK_MAX_EVENT
; i
++)
132 socket
->m_cbacks
[i
] = NULL
;
134 socket
->m_local
= NULL
;
135 socket
->m_peer
= NULL
;
136 socket
->m_error
= GSOCK_NOERROR
;
137 socket
->m_server
= FALSE
;
138 socket
->m_stream
= TRUE
;
139 socket
->m_non_blocking
= FALSE
;
140 socket
->m_timeout
.tv_sec
= 10 * 60; /* 10 minutes */
141 socket
->m_timeout
.tv_usec
= 0;
143 /* Allocate a new message number for this socket */
144 EnterCriticalSection(&critical
);
147 while (socketList
[i
] != NULL
)
149 i
= (i
+ 1) % MAXSOCKETS
;
151 if (i
== firstAvailable
) /* abort! */
154 LeaveCriticalSection(&critical
);
158 socketList
[i
] = socket
;
159 firstAvailable
= (i
+ 1) % MAXSOCKETS
;
160 socket
->m_msgnumber
= (i
+ WM_USER
);
162 LeaveCriticalSection(&critical
);
167 void GSocket_destroy(GSocket
*socket
)
169 assert(socket
!= NULL
);
171 /* We don't want more event notifications; so first of all we
172 * remove the socket from the list (NOTE: we won't get a CLOSE
173 * event for this socket if it wasn't closed before).
175 EnterCriticalSection(&critical
);
176 socketList
[(socket
->m_msgnumber
- WM_USER
)] = NULL
;
177 LeaveCriticalSection(&critical
);
179 /* We check that the socket is really shutdowned */
180 if (socket
->m_fd
!= INVALID_SOCKET
)
181 GSocket_Shutdown(socket
);
183 /* We destroy private addresses */
185 GAddress_destroy(socket
->m_local
);
188 GAddress_destroy(socket
->m_peer
);
190 /* We destroy socket itself */
194 void GSocket_Shutdown(GSocket
*socket
)
198 assert(socket
!= NULL
);
200 /* If socket has been created, we shutdown it */
201 if (socket
->m_fd
!= INVALID_SOCKET
)
203 /* TODO: Guilhem only does this for connection oriented sockets (?) */
204 shutdown(socket
->m_fd
, 2);
205 closesocket(socket
->m_fd
);
206 socket
->m_fd
= INVALID_SOCKET
;
209 /* We disable GUI callbacks */
210 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
212 socket
->m_cbacks
[evt
] = NULL
;
215 _GSocket_Configure_Callbacks(socket
);
218 /* Address handling */
220 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
222 assert(socket
!= NULL
);
224 if (socket
->m_fd
!= INVALID_SOCKET
&& !socket
->m_server
)
226 socket
->m_error
= GSOCK_INVSOCK
;
227 return GSOCK_INVSOCK
;
230 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
232 socket
->m_error
= GSOCK_INVADDR
;
233 return GSOCK_INVADDR
;
237 GAddress_destroy(socket
->m_local
);
239 socket
->m_local
= GAddress_copy(address
);
241 return GSOCK_NOERROR
;
244 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
246 assert(socket
!= NULL
);
248 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
250 socket
->m_error
= GSOCK_INVADDR
;
251 return GSOCK_INVADDR
;
255 GAddress_destroy(socket
->m_peer
);
257 socket
->m_peer
= GAddress_copy(address
);
259 return GSOCK_NOERROR
;
262 GAddress
*GSocket_GetLocal(GSocket
*socket
)
265 struct sockaddr addr
;
268 assert(socket
!= NULL
);
271 return GAddress_copy(socket
->m_local
);
273 if (socket
->m_fd
== INVALID_SOCKET
)
275 socket
->m_error
= GSOCK_INVSOCK
;
281 if (getsockname(socket
->m_fd
, &addr
, &size
) == SOCKET_ERROR
)
283 socket
->m_error
= GSOCK_IOERR
;
287 address
= GAddress_new();
290 socket
->m_error
= GSOCK_MEMERR
;
293 if (_GAddress_translate_from(address
, &addr
, size
) != GSOCK_NOERROR
)
295 socket
->m_error
= GSOCK_MEMERR
;
296 GAddress_destroy(address
);
303 GAddress
*GSocket_GetPeer(GSocket
*socket
)
305 assert(socket
!= NULL
);
308 return GAddress_copy(socket
->m_peer
);
313 /* Server specific parts */
315 /* GSocket_SetServer:
316 * Sets up the socket as a server. It uses the "Local" field of GSocket.
317 * "Local" must be set by GSocket_SetLocal() before GSocket_SetServer()
318 * is called. Possible error codes are: GSOCK_INVSOCK if socket has not
319 * been initialized, GSOCK_INVADDR if the local address has not been
320 * defined and GSOCK_IOERR for other internal errors.
322 GSocketError
GSocket_SetServer(GSocket
*sck
)
328 if (sck
->m_fd
!= INVALID_SOCKET
)
330 sck
->m_error
= GSOCK_INVSOCK
;
331 return GSOCK_INVSOCK
;
336 sck
->m_error
= GSOCK_INVADDR
;
337 return GSOCK_INVADDR
;
340 /* Initialize all fields */
341 sck
->m_server
= TRUE
;
342 sck
->m_stream
= TRUE
;
343 sck
->m_oriented
= TRUE
;
345 /* Create the socket */
346 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
347 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
349 if (sck
->m_fd
== INVALID_SOCKET
)
351 sck
->m_error
= GSOCK_IOERR
;
355 /* Bind the socket to the LOCAL address */
356 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0)
358 closesocket(sck
->m_fd
);
359 sck
->m_fd
= INVALID_SOCKET
;
360 sck
->m_error
= GSOCK_IOERR
;
364 /* Enable listening up to 5 connections */
365 if (listen(sck
->m_fd
, 5) != 0)
367 closesocket(sck
->m_fd
);
368 sck
->m_fd
= INVALID_SOCKET
;
369 sck
->m_error
= GSOCK_IOERR
;
373 return GSOCK_NOERROR
;
376 /* GSocket_WaitConnection:
377 * Waits for an incoming client connection.
379 GSocket
*GSocket_WaitConnection(GSocket
*sck
)
386 /* If the socket has already been created, we exit immediately */
387 if (sck
->m_fd
== INVALID_SOCKET
|| !sck
->m_server
)
389 sck
->m_error
= GSOCK_INVSOCK
;
393 /* Create a GSocket object for the new connection */
394 connection
= GSocket_new();
398 sck
->m_error
= GSOCK_MEMERR
;
402 /* Wait for a connection (with timeout) */
403 if (_GSocket_Input_Timeout(sck
) == GSOCK_TIMEDOUT
)
405 GSocket_destroy(connection
);
406 /* sck->m_error set by _GSocket_Input_Timeout */
410 connection
->m_fd
= accept(sck
->m_fd
, NULL
, NULL
);
412 if (connection
->m_fd
== INVALID_SOCKET
)
414 if (WSAGetLastError() == WSAEWOULDBLOCK
)
415 sck
->m_error
= GSOCK_WOULDBLOCK
;
417 sck
->m_error
= GSOCK_IOERR
;
419 GSocket_destroy(connection
);
423 ioctlsocket(connection
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
425 /* Initialize all fields */
426 connection
->m_server
= FALSE
;
427 connection
->m_stream
= TRUE
;
428 connection
->m_oriented
= TRUE
;
433 /* Non oriented connections */
435 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
441 if (sck
->m_fd
!= INVALID_SOCKET
)
443 sck
->m_error
= GSOCK_INVSOCK
;
444 return GSOCK_INVSOCK
;
449 sck
->m_error
= GSOCK_INVADDR
;
450 return GSOCK_INVADDR
;
453 /* Initialize all fields */
454 sck
->m_stream
= FALSE
;
455 sck
->m_server
= FALSE
;
456 sck
->m_oriented
= FALSE
;
458 /* Create the socket */
459 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
460 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
462 if (sck
->m_fd
== INVALID_SOCKET
)
464 sck
->m_error
= GSOCK_IOERR
;
468 /* Bind it to the LOCAL address */
469 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0)
471 closesocket(sck
->m_fd
);
472 sck
->m_fd
= INVALID_SOCKET
;
473 sck
->m_error
= GSOCK_IOERR
;
477 return GSOCK_NOERROR
;
480 GSocketError
GSocket_SetBroadcast(GSocket
*sck
)
486 if (GSocket_SetNonOriented(sck
) != GSOCK_NOERROR
)
490 setsockopt(sck
->m_fd
, SOL_SOCKET
, SO_BROADCAST
, (const char FAR
*) &b
, sizeof(b
));
492 return GSOCK_NOERROR
;
495 /* Client specific parts */
498 * Establishes a client connection to a server using the "Peer"
499 * field of GSocket. "Peer" must be set by GSocket_SetPeer() before
500 * GSocket_Connect() is called. Possible error codes are GSOCK_INVSOCK
501 * if the socket is alredy in use, GSOCK_INVADDR if the peer address
502 * has not been set, or GSOCK_IOERR for other internal errors.
504 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
511 if (sck
->m_fd
!= INVALID_SOCKET
)
513 sck
->m_error
= GSOCK_INVSOCK
;
514 return GSOCK_INVSOCK
;
519 sck
->m_error
= GSOCK_INVADDR
;
520 return GSOCK_INVADDR
;
523 /* Test whether we want the socket to be a stream (e.g. TCP) */
524 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
525 sck
->m_oriented
= TRUE
;
526 sck
->m_server
= FALSE
;
533 /* Create the socket */
534 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
, type
, 0);
535 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
537 if (sck
->m_fd
== INVALID_SOCKET
)
539 sck
->m_error
= GSOCK_IOERR
;
543 /* Connect it to the PEER address, with a timeout (see below) */
544 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
546 if (ret
== SOCKET_ERROR
)
548 err
= WSAGetLastError();
550 /* If connect failed with EWOULDBLOCK and the GSocket object
551 * is in blocking mode, we select() for the specified timeout
552 * checking for writability to see if the connection request
555 if ((err
== WSAEWOULDBLOCK
) && (sck
->m_non_blocking
== FALSE
))
557 if (_GSocket_Output_Timeout(sck
) == GSOCK_TIMEDOUT
)
559 closesocket(sck
->m_fd
);
560 sck
->m_fd
= INVALID_SOCKET
;
561 /* sck->m_error is set in _GSocket_Input_Timeout */
562 return GSOCK_TIMEDOUT
;
565 return GSOCK_NOERROR
;
568 /* If connect failed with EWOULDBLOCK and the GSocket object
569 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
570 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
571 * this way if the connection completes, a GSOCK_CONNECTION
572 * event will be generated, if enabled.
574 if ((err
== WSAEWOULDBLOCK
) && (sck
->m_non_blocking
== TRUE
))
576 sck
->m_error
= GSOCK_WOULDBLOCK
;
577 return GSOCK_WOULDBLOCK
;
580 /* If connect failed with an error other than EWOULDBLOCK,
581 * then the call to GSocket_Connect has failed.
583 closesocket(sck
->m_fd
);
584 sck
->m_fd
= INVALID_SOCKET
;
585 sck
->m_error
= GSOCK_IOERR
;
589 return GSOCK_NOERROR
;
594 /* Like recv(), send(), ... */
595 int GSocket_Read(GSocket
*socket
, 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_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
608 if (socket
->m_stream
)
609 return _GSocket_Recv_Stream(socket
, buffer
, size
);
611 return _GSocket_Recv_Dgram(socket
, buffer
, size
);
614 int GSocket_Write(GSocket
*socket
, const char *buffer
, int size
)
616 assert(socket
!= NULL
);
618 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
620 socket
->m_error
= GSOCK_INVSOCK
;
624 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
627 if (socket
->m_stream
)
628 return _GSocket_Send_Stream(socket
, buffer
, size
);
630 return _GSocket_Send_Dgram(socket
, buffer
, size
);
633 bool GSocket_DataAvailable(GSocket
*socket
)
638 assert(socket
!= NULL
);
640 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
642 socket
->m_error
= GSOCK_INVSOCK
;
647 FD_SET(socket
->m_fd
, &read_set
);
652 select(socket
->m_fd
+ 1, &read_set
, NULL
, NULL
, &tv
);
654 return FD_ISSET(socket
->m_fd
, &read_set
);
659 /* GSocket_SetNonBlocking:
660 * Sets the socket in non-blocking mode. This is useful if
661 * we don't want to wait.
663 void GSocket_SetNonBlocking(GSocket
*socket
, bool non_block
)
665 assert(socket
!= NULL
);
667 socket
->m_non_blocking
= non_block
;
670 /* GSocket_SetTimeout:
672 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisecs
)
674 assert(socket
!= NULL
);
676 socket
->m_timeout
.tv_sec
= (millisecs
/ 1000);
677 socket
->m_timeout
.tv_usec
= (millisecs
% 1000) * 1000;
681 * Returns the last error occured for this socket.
683 GSocketError
GSocket_GetError(GSocket
*socket
)
685 assert(socket
!= NULL
);
687 return socket
->m_error
;
692 /* Only one callback is possible for each event (INPUT, OUTPUT, CONNECTION
693 * and LOST). The callbacks are called in the following situations:
695 * INPUT: There is at least one byte in the input buffer
696 * OUTPUT: The system is sure that the next write call will not block
697 * CONNECTION: Two cases are possible:
698 * Client socket -> the connection is established
699 * Server socket -> a client requests a connection
700 * LOST: The connection is lost
702 * An event is generated only once and its state is reseted when the
703 * relative IO call is requested.
704 * For example: INPUT -> GSocket_Read()
705 * CONNECTION -> GSocket_Accept()
708 /* GSocket_SetCallback:
709 * Enables the callbacks specified by 'event'. Note that 'event'
710 * may be a combination of flags OR'ed toghether, so the same
711 * callback function can be made to accept different events.
712 * The callback function must have the following prototype:
714 * void function(GSocket *socket, GSocketEvent event, char *cdata)
716 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags event
,
717 GSocketCallback callback
, char *cdata
)
721 assert (socket
!= NULL
);
723 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
725 /* We test each flag and enable the corresponding events */
726 if ((event
& (1 << count
)) != 0)
728 socket
->m_cbacks
[count
] = callback
;
729 socket
->m_data
[count
] = cdata
;
733 _GSocket_Configure_Callbacks(socket
);
736 /* GSocket_UnsetCallback:
737 * Disables all callbacks specified by 'event', which may be a
738 * combination of flags OR'ed toghether.
740 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags event
)
744 assert(socket
!= NULL
);
746 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
748 /* We test each flag and disable the corresponding events */
749 if ((event
& (1 << count
)) != 0)
751 socket
->m_cbacks
[count
] = NULL
;
755 _GSocket_Configure_Callbacks(socket
);
761 void _GSocket_Configure_Callbacks(GSocket
*socket
)
766 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
768 if (socket
->m_cbacks
[count
] != NULL
)
772 case GSOCK_INPUT
: mask
|= FD_READ
; break;
773 case GSOCK_OUTPUT
: mask
|= FD_WRITE
; break;
774 case GSOCK_CONNECTION
: mask
|= (FD_ACCEPT
| FD_CONNECT
); break;
775 case GSOCK_LOST
: mask
|= FD_CLOSE
; break;
780 WSAAsyncSelect(socket
->m_fd
, hWin
, socket
->m_msgnumber
, mask
);
783 LRESULT CALLBACK
_GSocket_Internal_WinProc(HWND hWnd
,
790 GSocketCallback cback
;
792 if (uMsg
>= WM_USER
&& uMsg
<= (WM_USER
+ MAXSOCKETS
- 1))
794 EnterCriticalSection(&critical
);
795 socket
= socketList
[(uMsg
- WM_USER
)];
799 /* Check that the socket still exists (it has not been
800 * destroyed) and for safety, check that the m_fd field
801 * is what we expect it to be.
803 if ((socket
!= NULL
) && (socket
->m_fd
== wParam
))
805 switch WSAGETSELECTEVENT(lParam
)
807 case FD_READ
: event
= GSOCK_INPUT
; break;
808 case FD_WRITE
: event
= GSOCK_OUTPUT
; break;
809 case FD_ACCEPT
: event
= GSOCK_CONNECTION
; break;
810 case FD_CONNECT
: event
= GSOCK_CONNECTION
; break;
811 case FD_CLOSE
: event
= GSOCK_LOST
; break;
815 cback
= socket
->m_cbacks
[event
];
818 /* OK, we can now leave the critical section because we have
819 * already obtained the callback address (we make no further
820 * accesses to socket->whatever)
822 LeaveCriticalSection(&critical
);
825 (cback
)(socket
, event
, socket
->m_data
[event
]);
830 return DefWindowProc(hWnd
, uMsg
, wParam
, lParam
);
834 /* _GSocket_Input_Timeout:
835 * For blocking sockets, wait until data is available or
836 * until timeout ellapses.
838 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
842 if (socket
->m_non_blocking
== FALSE
)
845 FD_SET(socket
->m_fd
, &readfds
);
846 if (select(0, &readfds
, NULL
, NULL
, &socket
->m_timeout
) == 0)
848 socket
->m_error
= GSOCK_TIMEDOUT
;
849 return GSOCK_TIMEDOUT
;
852 return GSOCK_NOERROR
;
855 /* _GSocket_Output_Timeout:
856 * For blocking sockets, wait until data can be sent without
857 * blocking or until timeout ellapses.
859 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
863 if (socket
->m_non_blocking
== FALSE
)
866 FD_SET(socket
->m_fd
, &writefds
);
867 if (select(0, NULL
, &writefds
, NULL
, &socket
->m_timeout
) == 0)
869 socket
->m_error
= GSOCK_TIMEDOUT
;
870 return GSOCK_TIMEDOUT
;
873 return GSOCK_NOERROR
;
876 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
880 ret
= recv(socket
->m_fd
, buffer
, size
, 0);
882 if (ret
== SOCKET_ERROR
)
884 if (WSAGetLastError() != WSAEWOULDBLOCK
)
885 socket
->m_error
= GSOCK_IOERR
;
887 socket
->m_error
= GSOCK_WOULDBLOCK
;
895 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
897 struct sockaddr from
;
901 fromlen
= sizeof(from
);
903 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
905 if (ret
== SOCKET_ERROR
)
907 if (WSAGetLastError() != WSAEWOULDBLOCK
)
908 socket
->m_error
= GSOCK_IOERR
;
910 socket
->m_error
= GSOCK_WOULDBLOCK
;
915 /* Translate a system address into a GSocket address */
918 socket
->m_peer
= GAddress_new();
921 socket
->m_error
= GSOCK_MEMERR
;
925 if (_GAddress_translate_from(socket
->m_peer
, &from
, fromlen
) != GSOCK_NOERROR
)
927 socket
->m_error
= GSOCK_MEMERR
; /* TODO: bug in Unix GSocket! */
928 GAddress_destroy(socket
->m_peer
); /* TODO: bug in Unix GSocket! */
935 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
939 ret
= send(socket
->m_fd
, buffer
, size
, 0);
941 if (ret
== SOCKET_ERROR
)
943 if (WSAGetLastError() != WSAEWOULDBLOCK
)
944 socket
->m_error
= GSOCK_IOERR
;
946 socket
->m_error
= GSOCK_WOULDBLOCK
;
953 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
955 struct sockaddr
*addr
;
960 socket
->m_error
= GSOCK_INVADDR
;
964 if (!_GAddress_translate_to(socket
->m_peer
, &addr
, &len
))
966 socket
->m_error
= GSOCK_MEMERR
;
970 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
972 /* Frees memory allocated by _GAddress_translate_to */
975 if (ret
== SOCKET_ERROR
)
977 if (WSAGetLastError() != WSAEWOULDBLOCK
)
978 socket
->m_error
= GSOCK_IOERR
;
980 socket
->m_error
= GSOCK_WOULDBLOCK
;
989 * -------------------------------------------------------------------------
991 * -------------------------------------------------------------------------
994 /* CHECK_ADDRESS verifies that the current family is either GSOCK_NOFAMILY
995 * or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it initalizes address
996 * to be a GSOCK_*family*. In other cases, it returns GSOCK_INVADDR.
998 #define CHECK_ADDRESS(address, family, retval) \
1000 if (address->m_family == GSOCK_NOFAMILY) \
1001 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1002 return address->m_error; \
1003 if (address->m_family != GSOCK_##family) \
1005 address->m_error = GSOCK_INVADDR; \
1010 GAddress
*GAddress_new()
1014 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1017 address
->m_family
= GSOCK_NOFAMILY
;
1018 address
->m_addr
= NULL
;
1024 GAddress
*GAddress_copy(GAddress
*address
)
1028 assert(address
!= NULL
);
1030 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1033 memcpy(addr2
, address
, sizeof(GAddress
));
1035 if (address
->m_addr
)
1037 addr2
->m_addr
= (struct sockaddr
*) malloc(addr2
->m_len
);
1038 if (addr2
->m_addr
== NULL
)
1043 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1049 void GAddress_destroy(GAddress
*address
)
1051 assert(address
!= NULL
);
1056 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1058 assert(address
!= NULL
);
1060 address
->m_family
= type
;
1063 GAddressType
GAddress_GetFamily(GAddress
*address
)
1065 assert(address
!= NULL
);
1067 return address
->m_family
;
1070 GSocketError
_GAddress_translate_from(GAddress
*address
,
1071 struct sockaddr
*addr
, int len
)
1073 address
->m_realfamily
= addr
->sa_family
;
1074 switch (addr
->sa_family
)
1077 address
->m_family
= GSOCK_INET
;
1080 address
->m_family
= GSOCK_UNIX
;
1084 address
->m_family
= GSOCK_INET6
;
1089 address
->m_error
= GSOCK_INVOP
;
1094 if (address
->m_addr
)
1095 free(address
->m_addr
);
1097 address
->m_len
= len
;
1098 address
->m_addr
= (struct sockaddr
*) malloc(len
);
1100 if (address
->m_addr
== NULL
)
1102 address
->m_error
= GSOCK_MEMERR
;
1103 return GSOCK_MEMERR
;
1105 memcpy(address
->m_addr
, addr
, len
);
1107 return GSOCK_NOERROR
;
1110 GSocketError
_GAddress_translate_to(GAddress
*address
,
1111 struct sockaddr
**addr
, int *len
)
1113 if (!address
->m_addr
)
1115 address
->m_error
= GSOCK_INVADDR
;
1116 return GSOCK_INVADDR
;
1119 *len
= address
->m_len
;
1120 *addr
= (struct sockaddr
*) malloc(address
->m_len
);
1123 address
->m_error
= GSOCK_MEMERR
;
1124 return GSOCK_MEMERR
;
1127 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1128 return GSOCK_NOERROR
;
1132 * -------------------------------------------------------------------------
1133 * Internet address family
1134 * -------------------------------------------------------------------------
1137 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1139 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1140 if (address
->m_addr
== NULL
)
1142 address
->m_error
= GSOCK_MEMERR
;
1143 return GSOCK_MEMERR
;
1146 address
->m_len
= sizeof(struct sockaddr_in
);
1147 address
->m_family
= GSOCK_INET
;
1148 address
->m_realfamily
= PF_INET
;
1149 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1150 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1152 return GSOCK_NOERROR
;
1155 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1158 struct in_addr
*addr
;
1160 assert(address
!= NULL
);
1162 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1164 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1166 addr
->s_addr
= inet_addr(hostname
);
1168 /* If it is a numeric host name, convert it now */
1169 if (addr
->s_addr
== INADDR_NONE
)
1171 struct in_addr
*array_addr
;
1173 /* It is a real name, we solve it */
1174 if ((he
= gethostbyname(hostname
)) == NULL
)
1176 address
->m_error
= GSOCK_NOHOST
;
1177 return GSOCK_NOHOST
;
1179 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1180 addr
->s_addr
= array_addr
[0].s_addr
;
1182 return GSOCK_NOERROR
;
1185 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1186 unsigned long hostaddr
)
1188 struct in_addr
*addr
;
1190 assert(address
!= NULL
);
1192 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1194 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1195 addr
->s_addr
= hostaddr
;
1197 return GSOCK_NOERROR
;
1200 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1201 const char *protocol
)
1204 struct sockaddr_in
*addr
;
1206 assert(address
!= NULL
);
1207 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1211 address
->m_error
= GSOCK_INVPORT
;
1215 se
= getservbyname(port
, protocol
);
1218 if (isdigit(port
[0]))
1222 port_int
= atoi(port
);
1223 addr
= (struct sockaddr_in
*)address
->m_addr
;
1224 addr
->sin_port
= htons((u_short
) port_int
);
1225 return GSOCK_NOERROR
;
1228 address
->m_error
= GSOCK_INVPORT
;
1229 return GSOCK_INVPORT
;
1232 addr
= (struct sockaddr_in
*)address
->m_addr
;
1233 addr
->sin_port
= se
->s_port
;
1235 return GSOCK_NOERROR
;
1238 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1240 struct sockaddr_in
*addr
;
1242 assert(address
!= NULL
);
1243 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1245 addr
= (struct sockaddr_in
*)address
->m_addr
;
1246 addr
->sin_port
= htons(port
);
1248 return GSOCK_NOERROR
;
1251 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1255 struct sockaddr_in
*addr
;
1257 assert(address
!= NULL
);
1258 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1260 addr
= (struct sockaddr_in
*)address
->m_addr
;
1261 addr_buf
= (char *)&(addr
->sin_addr
);
1263 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1266 address
->m_error
= GSOCK_NOHOST
;
1267 return GSOCK_NOHOST
;
1270 strncpy(hostname
, he
->h_name
, sbuf
);
1272 return GSOCK_NOERROR
;
1275 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1277 struct sockaddr_in
*addr
;
1279 assert(address
!= NULL
);
1280 CHECK_ADDRESS(address
, INET
, 0);
1282 addr
= (struct sockaddr_in
*)address
->m_addr
;
1284 return addr
->sin_addr
.s_addr
;
1287 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1289 struct sockaddr_in
*addr
;
1291 assert(address
!= NULL
);
1292 CHECK_ADDRESS(address
, INET
, 0);
1294 addr
= (struct sockaddr_in
*)address
->m_addr
;
1295 return ntohs(addr
->sin_port
);
1299 * -------------------------------------------------------------------------
1300 * Unix address family
1301 * -------------------------------------------------------------------------
1304 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1306 assert (address
!= NULL
);
1307 address
->m_error
= GSOCK_INVADDR
;
1308 return GSOCK_INVADDR
;
1311 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1313 assert (address
!= NULL
);
1314 address
->m_error
= GSOCK_INVADDR
;
1315 return GSOCK_INVADDR
;
1318 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1320 assert (address
!= NULL
);
1321 address
->m_error
= GSOCK_INVADDR
;
1322 return GSOCK_INVADDR
;
1326 #endif /* !defined(__WXMSW__) || (defined(__WXMSW__) && wxUSE_SOCKETS) */
1330 /* Diferencias con la version Unix:
1331 * - El descriptor es SOCKET y no int
1332 * - Constantes -1 pasan a INVALID_SOCKET
1333 * - Errores en muchas funciones pasan de -1 o <0 a SOCKET_ERROR
1334 * - ioctl y close pasan a ioctlsocket y closesocket
1335 * - inet_addr en lugar de inet_aton
1336 * - Codigo de inicializacion y terminacion para inicializar y
1337 * terminar WinSocket y para la ventana interna.
1338 * - SetTimeout en la version MSW simplemente guarda el valor en
1339 * socket.m_timeout, por tanto no hay necesidad de llamar a
1340 * SetTimeout cada vez que se crea realmente un socket (no
1342 * - Lo mismo para SetNonBlocking.