1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket) for WX
4 * Purpose: GSocket main MSW file
6 * -------------------------------------------------------------------------
9 #ifndef __GSOCKET_STANDALONE__
13 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
16 #ifndef __GSOCKET_STANDALONE__
18 #include "wx/msw/gsockmsw.h"
19 #include "wx/gsocket.h"
21 #define INSTANCE wxGetInstance()
28 /* If not using wxWindows, a global var called hInst must
29 * be available and it must containt the app's instance
32 #define INSTANCE hInst
34 #endif /* __GSOCKET_STANDALONE__ */
46 #define CLASSNAME "_GSocket_Internal_Window_Class"
47 #define WINDOWNAME "_GSocket_Internal_Window_Name"
49 /* Maximum number of different GSocket objects at a given time.
50 * This value can be modified at will, but it CANNOT be greater
51 * than (0x7FFF - WM_USER + 1)
53 #define MAXSOCKETS 1024
55 #if (MAXSOCKETS > (0x7FFF - WM_USER + 1))
56 #error "MAXSOCKETS is too big!"
60 /* Global variables */
62 extern HINSTANCE INSTANCE
;
64 static CRITICAL_SECTION critical
;
65 static GSocket
* socketList
[MAXSOCKETS
];
66 static int firstAvailable
;
68 /* Global initializers */
76 /* Create internal window for event notifications */
78 winClass
.lpfnWndProc
= _GSocket_Internal_WinProc
;
79 winClass
.cbClsExtra
= 0;
80 winClass
.cbWndExtra
= 0;
81 winClass
.hInstance
= INSTANCE
;
82 winClass
.hIcon
= (HICON
) NULL
;
83 winClass
.hCursor
= (HCURSOR
) NULL
;
84 winClass
.hbrBackground
= (HBRUSH
) NULL
;
85 winClass
.lpszMenuName
= (LPCTSTR
) NULL
;
86 winClass
.lpszClassName
= CLASSNAME
;
88 RegisterClass(&winClass
);
89 hWin
= CreateWindow(CLASSNAME
,
92 (HWND
) NULL
, (HMENU
) NULL
, INSTANCE
, (LPVOID
) NULL
);
94 if (!hWin
) return FALSE
;
96 /* Initialize socket list */
97 InitializeCriticalSection(&critical
);
99 for (i
= 0; i
< MAXSOCKETS
; i
++)
101 socketList
[i
] = NULL
;
105 /* Initialize WinSocket */
106 return (WSAStartup((1 << 8) | 1, &wsaData
) == 0);
109 void GSocket_Cleanup()
111 /* Destroy internal window */
113 UnregisterClass(CLASSNAME
, INSTANCE
);
115 /* Delete critical section */
116 DeleteCriticalSection(&critical
);
118 /* Cleanup WinSocket */
122 /* Constructors / Destructors */
124 GSocket
*GSocket_new()
129 if ((socket
= (GSocket
*) malloc(sizeof(GSocket
))) == NULL
)
132 socket
->m_fd
= INVALID_SOCKET
;
133 for (i
= 0; i
< GSOCK_MAX_EVENT
; i
++)
135 socket
->m_cbacks
[i
] = NULL
;
137 socket
->m_local
= NULL
;
138 socket
->m_peer
= NULL
;
139 socket
->m_error
= GSOCK_NOERROR
;
140 socket
->m_server
= FALSE
;
141 socket
->m_stream
= TRUE
;
142 socket
->m_non_blocking
= FALSE
;
143 socket
->m_timeout
.tv_sec
= 10 * 60; /* 10 minutes */
144 socket
->m_timeout
.tv_usec
= 0;
145 socket
->m_detected
= 0;
147 /* Allocate a new message number for this socket */
148 EnterCriticalSection(&critical
);
151 while (socketList
[i
] != NULL
)
153 i
= (i
+ 1) % MAXSOCKETS
;
155 if (i
== firstAvailable
) /* abort! */
158 LeaveCriticalSection(&critical
);
162 socketList
[i
] = socket
;
163 firstAvailable
= (i
+ 1) % MAXSOCKETS
;
164 socket
->m_msgnumber
= (i
+ WM_USER
);
166 LeaveCriticalSection(&critical
);
171 void GSocket_destroy(GSocket
*socket
)
173 assert(socket
!= NULL
);
175 /* Remove the socket from the list */
176 EnterCriticalSection(&critical
);
177 socketList
[(socket
->m_msgnumber
- WM_USER
)] = NULL
;
178 LeaveCriticalSection(&critical
);
180 /* Check that the socket is really shutdowned */
181 if (socket
->m_fd
!= INVALID_SOCKET
)
182 GSocket_Shutdown(socket
);
184 /* Destroy private addresses */
186 GAddress_destroy(socket
->m_local
);
189 GAddress_destroy(socket
->m_peer
);
191 /* Destroy socket itself */
195 void GSocket_Shutdown(GSocket
*socket
)
199 assert(socket
!= NULL
);
201 /* If socket has been created, shutdown it */
202 if (socket
->m_fd
!= INVALID_SOCKET
)
204 shutdown(socket
->m_fd
, 2);
205 closesocket(socket
->m_fd
);
206 socket
->m_fd
= INVALID_SOCKET
;
209 /* Disable GUI callbacks */
210 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
211 socket
->m_cbacks
[evt
] = NULL
;
213 socket
->m_detected
= 0;
214 _GSocket_Disable_Events(socket
);
217 /* Address handling */
219 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
221 assert(socket
!= NULL
);
223 if (socket
->m_fd
!= INVALID_SOCKET
&& !socket
->m_server
)
225 socket
->m_error
= GSOCK_INVSOCK
;
226 return GSOCK_INVSOCK
;
229 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
231 socket
->m_error
= GSOCK_INVADDR
;
232 return GSOCK_INVADDR
;
236 GAddress_destroy(socket
->m_local
);
238 socket
->m_local
= GAddress_copy(address
);
240 return GSOCK_NOERROR
;
243 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
245 assert(socket
!= NULL
);
247 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
249 socket
->m_error
= GSOCK_INVADDR
;
250 return GSOCK_INVADDR
;
254 GAddress_destroy(socket
->m_peer
);
256 socket
->m_peer
= GAddress_copy(address
);
258 return GSOCK_NOERROR
;
261 GAddress
*GSocket_GetLocal(GSocket
*socket
)
264 struct sockaddr addr
;
267 assert(socket
!= NULL
);
270 return GAddress_copy(socket
->m_local
);
272 if (socket
->m_fd
== INVALID_SOCKET
)
274 socket
->m_error
= GSOCK_INVSOCK
;
280 if (getsockname(socket
->m_fd
, &addr
, &size
) == SOCKET_ERROR
)
282 socket
->m_error
= GSOCK_IOERR
;
286 address
= GAddress_new();
289 socket
->m_error
= GSOCK_MEMERR
;
292 if (_GAddress_translate_from(address
, &addr
, size
) != GSOCK_NOERROR
)
294 socket
->m_error
= GSOCK_MEMERR
;
295 GAddress_destroy(address
);
302 GAddress
*GSocket_GetPeer(GSocket
*socket
)
304 assert(socket
!= NULL
);
307 return GAddress_copy(socket
->m_peer
);
312 /* Server specific parts */
314 /* GSocket_SetServer:
315 * Sets up the socket as a server. It uses the "Local" field of GSocket.
316 * "Local" must be set by GSocket_SetLocal() before GSocket_SetServer()
317 * is called. Possible error codes are: GSOCK_INVSOCK if socket has not
318 * been initialized, GSOCK_INVADDR if the local address has not been
319 * defined and GSOCK_IOERR for other internal errors.
321 GSocketError
GSocket_SetServer(GSocket
*sck
)
327 if (sck
->m_fd
!= INVALID_SOCKET
)
329 sck
->m_error
= GSOCK_INVSOCK
;
330 return GSOCK_INVSOCK
;
335 sck
->m_error
= GSOCK_INVADDR
;
336 return GSOCK_INVADDR
;
339 /* Initialize all fields */
340 sck
->m_server
= TRUE
;
341 sck
->m_stream
= TRUE
;
342 sck
->m_oriented
= TRUE
;
344 /* Create the socket */
345 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
347 if (sck
->m_fd
== INVALID_SOCKET
)
349 sck
->m_error
= GSOCK_IOERR
;
353 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
354 _GSocket_Enable_Events(sck
);
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 sck
->m_detected
&= ~GSOCK_CONNECTION_FLAG
;
389 if (sck
->m_fd
== INVALID_SOCKET
|| !sck
->m_server
)
391 sck
->m_error
= GSOCK_INVSOCK
;
395 /* Create a GSocket object for the new connection */
396 connection
= GSocket_new();
400 sck
->m_error
= GSOCK_MEMERR
;
404 /* Wait for a connection (with timeout) */
405 if (_GSocket_Input_Timeout(sck
) == GSOCK_TIMEDOUT
)
407 GSocket_destroy(connection
);
408 /* sck->m_error set by _GSocket_Input_Timeout */
412 connection
->m_fd
= accept(sck
->m_fd
, NULL
, NULL
);
414 if (connection
->m_fd
== INVALID_SOCKET
)
416 if (WSAGetLastError() == WSAEWOULDBLOCK
)
417 sck
->m_error
= GSOCK_WOULDBLOCK
;
419 sck
->m_error
= GSOCK_IOERR
;
421 GSocket_destroy(connection
);
425 /* Initialize all fields */
426 connection
->m_server
= FALSE
;
427 connection
->m_stream
= TRUE
;
428 connection
->m_oriented
= TRUE
;
430 ioctlsocket(connection
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
431 _GSocket_Enable_Events(connection
);
436 /* Non oriented connections */
438 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
444 if (sck
->m_fd
!= INVALID_SOCKET
)
446 sck
->m_error
= GSOCK_INVSOCK
;
447 return GSOCK_INVSOCK
;
452 sck
->m_error
= GSOCK_INVADDR
;
453 return GSOCK_INVADDR
;
456 /* Initialize all fields */
457 sck
->m_stream
= FALSE
;
458 sck
->m_server
= FALSE
;
459 sck
->m_oriented
= FALSE
;
461 /* Create the socket */
462 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
464 if (sck
->m_fd
== INVALID_SOCKET
)
466 sck
->m_error
= GSOCK_IOERR
;
470 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
471 _GSocket_Enable_Events(sck
);
473 /* Bind it to the LOCAL address */
474 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0)
476 closesocket(sck
->m_fd
);
477 sck
->m_fd
= INVALID_SOCKET
;
478 sck
->m_error
= GSOCK_IOERR
;
482 return GSOCK_NOERROR
;
485 GSocketError
GSocket_SetBroadcast(GSocket
*sck
)
491 if (GSocket_SetNonOriented(sck
) != GSOCK_NOERROR
)
494 setsockopt(sck
->m_fd
, SOL_SOCKET
, SO_BROADCAST
,
495 (const char FAR
*) &b
, sizeof(b
));
497 return GSOCK_NOERROR
;
500 /* Client specific parts */
503 * Establishes a client connection to a server using the "Peer"
504 * field of GSocket. "Peer" must be set by GSocket_SetPeer() before
505 * GSocket_Connect() is called. Possible error codes are GSOCK_INVSOCK,
506 * GSOCK_INVADDR, GSOCK_TIMEDOUT, GSOCK_WOULDBLOCK and GSOCK_IOERR.
507 * If a socket is nonblocking and Connect() returns GSOCK_WOULDBLOCK,
508 * the connection request can be completed later. Use GSocket_Select()
509 * to check or wait for a GSOCK_CONNECTION event.
511 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
518 sck
->m_detected
&= ~GSOCK_CONNECTION_FLAG
;
520 if (sck
->m_fd
!= INVALID_SOCKET
)
522 sck
->m_error
= GSOCK_INVSOCK
;
523 return GSOCK_INVSOCK
;
528 sck
->m_error
= GSOCK_INVADDR
;
529 return GSOCK_INVADDR
;
532 /* Test whether we want the socket to be a stream (e.g. TCP) */
533 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
534 sck
->m_oriented
= TRUE
;
535 sck
->m_server
= FALSE
;
542 /* Create the socket */
543 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
, type
, 0);
545 if (sck
->m_fd
== INVALID_SOCKET
)
547 sck
->m_error
= GSOCK_IOERR
;
551 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
552 _GSocket_Enable_Events(sck
);
554 /* Connect it to the PEER address, with a timeout (see below) */
555 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
557 if (ret
== SOCKET_ERROR
)
559 err
= WSAGetLastError();
561 /* If connect failed with EWOULDBLOCK and the GSocket object
562 * is in blocking mode, we select() for the specified timeout
563 * checking for writability to see if the connection request
566 if ((err
== WSAEWOULDBLOCK
) && (!sck
->m_non_blocking
))
568 if (_GSocket_Output_Timeout(sck
) == GSOCK_TIMEDOUT
)
570 closesocket(sck
->m_fd
);
571 sck
->m_fd
= INVALID_SOCKET
;
572 /* sck->m_error is set in _GSocket_Output_Timeout */
573 return GSOCK_TIMEDOUT
;
576 return GSOCK_NOERROR
;
579 /* If connect failed with EWOULDBLOCK and the GSocket object
580 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
581 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
582 * this way if the connection completes, a GSOCK_CONNECTION
583 * event will be generated, if enabled.
585 if ((err
== WSAEWOULDBLOCK
) && (sck
->m_non_blocking
))
587 sck
->m_error
= GSOCK_WOULDBLOCK
;
588 return GSOCK_WOULDBLOCK
;
591 /* If connect failed with an error other than EWOULDBLOCK,
592 * then the call to GSocket_Connect has failed.
594 closesocket(sck
->m_fd
);
595 sck
->m_fd
= INVALID_SOCKET
;
596 sck
->m_error
= GSOCK_IOERR
;
600 return GSOCK_NOERROR
;
605 /* Like recv(), send(), ... */
606 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
610 assert(socket
!= NULL
);
612 socket
->m_detected
&= ~GSOCK_INPUT_FLAG
;
614 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
616 socket
->m_error
= GSOCK_INVSOCK
;
620 /* If the socket is blocking, wait for data (with a timeout) */
621 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
625 if (socket
->m_stream
)
626 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
628 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
630 if (ret
== SOCKET_ERROR
)
632 /* NOTE: Window sockets exhibit a very strange property;
633 * if the socket is in non-blocking mode (which is always
634 * the case here, no matter the setting of GSocket itself)
635 * a call to send() can fail with EWOULDBLOCK even when
636 * select() says that the socket is readable.
638 * This can break several things because, usually, if
639 * select() says that the socket is writable, it is
640 * assumed that send() won't fail. To avoid this, we
641 * return 0 instead of -1 for this special case.
643 if (WSAGetLastError() != WSAEWOULDBLOCK
)
645 socket
->m_error
= GSOCK_IOERR
;
650 socket
->m_error
= GSOCK_WOULDBLOCK
;
658 int GSocket_Write(GSocket
*socket
, const char *buffer
, int size
)
662 assert(socket
!= NULL
);
664 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
666 socket
->m_error
= GSOCK_INVSOCK
;
670 /* If the socket is blocking, wait for writability (with a timeout) */
671 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
675 if (socket
->m_stream
)
676 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
678 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
680 if (ret
== SOCKET_ERROR
)
682 if (WSAGetLastError() != WSAEWOULDBLOCK
)
683 socket
->m_error
= GSOCK_IOERR
;
685 socket
->m_error
= GSOCK_WOULDBLOCK
;
687 socket
->m_detected
&= ~GSOCK_OUTPUT_FLAG
;
695 * Polls the socket to determine its status. This function will
696 * check for the events specified in the 'flags' parameter, and
697 * it will return a mask indicating which operations can be
698 * performed. This function won't block, regardless of the
699 * mode (blocking|nonblocking) of the socket.
701 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
703 assert(socket
!= NULL
);
705 return (flags
& socket
->m_detected
);
710 /* GSocket_SetNonBlocking:
711 * Sets the socket to non-blocking mode. This is useful if
712 * we don't want to wait.
714 void GSocket_SetNonBlocking(GSocket
*socket
, bool non_block
)
716 assert(socket
!= NULL
);
718 socket
->m_non_blocking
= non_block
;
721 /* GSocket_SetTimeout:
722 * Sets the timeout for blocking calls. Time is
723 * expressed in milliseconds.
725 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millis
)
727 assert(socket
!= NULL
);
729 socket
->m_timeout
.tv_sec
= (millis
/ 1000);
730 socket
->m_timeout
.tv_usec
= (millis
% 1000) * 1000;
734 * Returns the last error occured for this socket.
736 GSocketError
GSocket_GetError(GSocket
*socket
)
738 assert(socket
!= NULL
);
740 return socket
->m_error
;
745 /* Only one callback is possible for each event (INPUT, OUTPUT, CONNECTION
746 * and LOST). The callbacks are called in the following situations:
748 * INPUT: There is at least one byte in the input buffer
749 * OUTPUT: The system is sure that the next write call will not block
750 * CONNECTION: Two cases are possible:
751 * Client socket -> the connection is established
752 * Server socket -> a client requests a connection
753 * LOST: The connection is lost
755 * An event is generated only once and its state is reseted when the
756 * relative IO call is requested.
757 * For example: INPUT -> GSocket_Read()
758 * CONNECTION -> GSocket_Accept()
761 /* GSocket_SetCallback:
762 * Enables the callbacks specified by 'flags'. Note that 'flags'
763 * may be a combination of flags OR'ed toghether, so the same
764 * callback function can be made to accept different events.
765 * The callback function must have the following prototype:
767 * void function(GSocket *socket, GSocketEvent event, char *cdata)
769 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
770 GSocketCallback callback
, char *cdata
)
774 assert (socket
!= NULL
);
776 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
778 if ((flags
& (1 << count
)) != 0)
780 socket
->m_cbacks
[count
] = callback
;
781 socket
->m_data
[count
] = cdata
;
786 /* GSocket_UnsetCallback:
787 * Disables all callbacks specified by 'flags', which may be a
788 * combination of flags OR'ed toghether.
790 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
794 assert(socket
!= NULL
);
796 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
798 if ((flags
& (1 << count
)) != 0)
800 socket
->m_cbacks
[count
] = NULL
;
801 socket
->m_data
[count
] = NULL
;
809 /* _GSocket_Enable_Events:
810 * We enable all event notifications here (we need to be notified
811 * of all events for internal processing) but we will only notify
812 * users when an appropiate callback function has been installed.
814 void _GSocket_Enable_Events(GSocket
*socket
)
816 assert (socket
!= NULL
);
818 if (socket
->m_fd
!= INVALID_SOCKET
)
820 WSAAsyncSelect(socket
->m_fd
, hWin
, socket
->m_msgnumber
,
821 FD_READ
| FD_WRITE
| FD_ACCEPT
| FD_CONNECT
| FD_CLOSE
);
825 /* _GSocket_Disable_Events:
826 * Disable event notifications (when shutdowning the socket)
828 void _GSocket_Disable_Events(GSocket
*socket
)
830 assert (socket
!= NULL
);
832 if (socket
->m_fd
!= INVALID_SOCKET
)
834 WSAAsyncSelect(socket
->m_fd
, hWin
, socket
->m_msgnumber
, 0);
839 LRESULT CALLBACK
_GSocket_Internal_WinProc(HWND hWnd
,
846 GSocketCallback cback
;
849 if (uMsg
>= WM_USER
&& uMsg
<= (WM_USER
+ MAXSOCKETS
- 1))
851 EnterCriticalSection(&critical
);
852 socket
= socketList
[(uMsg
- WM_USER
)];
857 /* Check that the socket still exists (it has not been
858 * destroyed) and for safety, check that the m_fd field
859 * is what we expect it to be.
861 if ((socket
!= NULL
) && (socket
->m_fd
== wParam
))
863 switch WSAGETSELECTEVENT(lParam
)
865 case FD_READ
: event
= GSOCK_INPUT
; break;
866 case FD_WRITE
: event
= GSOCK_OUTPUT
; break;
867 case FD_ACCEPT
: event
= GSOCK_CONNECTION
; break;
870 if (WSAGETSELECTERROR(lParam
) != 0)
873 event
= GSOCK_CONNECTION
;
876 case FD_CLOSE
: event
= GSOCK_LOST
; break;
881 cback
= socket
->m_cbacks
[event
];
882 data
= socket
->m_data
[event
];
884 if (event
== GSOCK_LOST
)
885 socket
->m_detected
= GSOCK_LOST_FLAG
;
887 socket
->m_detected
|= (1 << event
);
891 /* OK, we can now leave the critical section because we have
892 * already obtained the callback address (we make no further
893 * accesses to socket->whatever)
895 LeaveCriticalSection(&critical
);
898 (cback
)(socket
, event
, data
);
903 return DefWindowProc(hWnd
, uMsg
, wParam
, lParam
);
907 /* _GSocket_Input_Timeout:
908 * For blocking sockets, wait until data is available or
909 * until timeout ellapses.
911 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
915 if (!socket
->m_non_blocking
)
918 FD_SET(socket
->m_fd
, &readfds
);
919 if (select(0, &readfds
, NULL
, NULL
, &socket
->m_timeout
) == 0)
921 socket
->m_error
= GSOCK_TIMEDOUT
;
922 return GSOCK_TIMEDOUT
;
925 return GSOCK_NOERROR
;
928 /* _GSocket_Output_Timeout:
929 * For blocking sockets, wait until data can be sent without
930 * blocking or until timeout ellapses.
932 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
936 if (!socket
->m_non_blocking
)
939 FD_SET(socket
->m_fd
, &writefds
);
940 if (select(0, NULL
, &writefds
, NULL
, &socket
->m_timeout
) == 0)
942 socket
->m_error
= GSOCK_TIMEDOUT
;
943 return GSOCK_TIMEDOUT
;
946 return GSOCK_NOERROR
;
949 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
951 return recv(socket
->m_fd
, buffer
, size
, 0);
954 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
956 struct sockaddr from
;
957 SOCKLEN_T fromlen
= sizeof(from
);
960 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
962 if (ret
== SOCKET_ERROR
)
965 /* Translate a system address into a GSocket address */
968 socket
->m_peer
= GAddress_new();
971 socket
->m_error
= GSOCK_MEMERR
;
975 if (_GAddress_translate_from(socket
->m_peer
, &from
, fromlen
) != GSOCK_NOERROR
)
977 socket
->m_error
= GSOCK_MEMERR
;
978 GAddress_destroy(socket
->m_peer
);
985 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
987 return send(socket
->m_fd
, buffer
, size
, 0);
990 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
992 struct sockaddr
*addr
;
997 socket
->m_error
= GSOCK_INVADDR
;
1001 if (!_GAddress_translate_to(socket
->m_peer
, &addr
, &len
))
1003 socket
->m_error
= GSOCK_MEMERR
;
1007 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
1009 /* Frees memory allocated by _GAddress_translate_to */
1017 * -------------------------------------------------------------------------
1019 * -------------------------------------------------------------------------
1022 /* CHECK_ADDRESS verifies that the current family is either GSOCK_NOFAMILY
1023 * or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it initalizes address
1024 * to be a GSOCK_*family*. In other cases, it returns GSOCK_INVADDR.
1026 #define CHECK_ADDRESS(address, family, retval) \
1028 if (address->m_family == GSOCK_NOFAMILY) \
1029 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1030 return address->m_error; \
1031 if (address->m_family != GSOCK_##family) \
1033 address->m_error = GSOCK_INVADDR; \
1038 GAddress
*GAddress_new()
1042 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1045 address
->m_family
= GSOCK_NOFAMILY
;
1046 address
->m_addr
= NULL
;
1052 GAddress
*GAddress_copy(GAddress
*address
)
1056 assert(address
!= NULL
);
1058 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1061 memcpy(addr2
, address
, sizeof(GAddress
));
1063 if (address
->m_addr
)
1065 addr2
->m_addr
= (struct sockaddr
*) malloc(addr2
->m_len
);
1066 if (addr2
->m_addr
== NULL
)
1071 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1077 void GAddress_destroy(GAddress
*address
)
1079 assert(address
!= NULL
);
1084 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1086 assert(address
!= NULL
);
1088 address
->m_family
= type
;
1091 GAddressType
GAddress_GetFamily(GAddress
*address
)
1093 assert(address
!= NULL
);
1095 return address
->m_family
;
1098 GSocketError
_GAddress_translate_from(GAddress
*address
,
1099 struct sockaddr
*addr
, int len
)
1101 address
->m_realfamily
= addr
->sa_family
;
1102 switch (addr
->sa_family
)
1105 address
->m_family
= GSOCK_INET
;
1108 address
->m_family
= GSOCK_UNIX
;
1112 address
->m_family
= GSOCK_INET6
;
1117 address
->m_error
= GSOCK_INVOP
;
1122 if (address
->m_addr
)
1123 free(address
->m_addr
);
1125 address
->m_len
= len
;
1126 address
->m_addr
= (struct sockaddr
*) malloc(len
);
1128 if (address
->m_addr
== NULL
)
1130 address
->m_error
= GSOCK_MEMERR
;
1131 return GSOCK_MEMERR
;
1133 memcpy(address
->m_addr
, addr
, len
);
1135 return GSOCK_NOERROR
;
1138 GSocketError
_GAddress_translate_to(GAddress
*address
,
1139 struct sockaddr
**addr
, int *len
)
1141 if (!address
->m_addr
)
1143 address
->m_error
= GSOCK_INVADDR
;
1144 return GSOCK_INVADDR
;
1147 *len
= address
->m_len
;
1148 *addr
= (struct sockaddr
*) malloc(address
->m_len
);
1151 address
->m_error
= GSOCK_MEMERR
;
1152 return GSOCK_MEMERR
;
1155 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1156 return GSOCK_NOERROR
;
1160 * -------------------------------------------------------------------------
1161 * Internet address family
1162 * -------------------------------------------------------------------------
1165 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1167 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1168 if (address
->m_addr
== NULL
)
1170 address
->m_error
= GSOCK_MEMERR
;
1171 return GSOCK_MEMERR
;
1174 address
->m_len
= sizeof(struct sockaddr_in
);
1175 address
->m_family
= GSOCK_INET
;
1176 address
->m_realfamily
= PF_INET
;
1177 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1178 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1180 return GSOCK_NOERROR
;
1183 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1186 struct in_addr
*addr
;
1188 assert(address
!= NULL
);
1190 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1192 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1194 addr
->s_addr
= inet_addr(hostname
);
1196 /* If it is a numeric host name, convert it now */
1197 if (addr
->s_addr
== INADDR_NONE
)
1199 struct in_addr
*array_addr
;
1201 /* It is a real name, we solve it */
1202 if ((he
= gethostbyname(hostname
)) == NULL
)
1204 address
->m_error
= GSOCK_NOHOST
;
1205 return GSOCK_NOHOST
;
1207 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1208 addr
->s_addr
= array_addr
[0].s_addr
;
1210 return GSOCK_NOERROR
;
1213 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1214 unsigned long hostaddr
)
1216 struct in_addr
*addr
;
1218 assert(address
!= NULL
);
1220 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1222 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1223 addr
->s_addr
= hostaddr
;
1225 return GSOCK_NOERROR
;
1228 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1229 const char *protocol
)
1232 struct sockaddr_in
*addr
;
1234 assert(address
!= NULL
);
1235 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1239 address
->m_error
= GSOCK_INVPORT
;
1243 se
= getservbyname(port
, protocol
);
1246 if (isdigit(port
[0]))
1250 port_int
= atoi(port
);
1251 addr
= (struct sockaddr_in
*)address
->m_addr
;
1252 addr
->sin_port
= htons((u_short
) port_int
);
1253 return GSOCK_NOERROR
;
1256 address
->m_error
= GSOCK_INVPORT
;
1257 return GSOCK_INVPORT
;
1260 addr
= (struct sockaddr_in
*)address
->m_addr
;
1261 addr
->sin_port
= se
->s_port
;
1263 return GSOCK_NOERROR
;
1266 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1268 struct sockaddr_in
*addr
;
1270 assert(address
!= NULL
);
1271 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1273 addr
= (struct sockaddr_in
*)address
->m_addr
;
1274 addr
->sin_port
= htons(port
);
1276 return GSOCK_NOERROR
;
1279 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1283 struct sockaddr_in
*addr
;
1285 assert(address
!= NULL
);
1286 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1288 addr
= (struct sockaddr_in
*)address
->m_addr
;
1289 addr_buf
= (char *)&(addr
->sin_addr
);
1291 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1294 address
->m_error
= GSOCK_NOHOST
;
1295 return GSOCK_NOHOST
;
1298 strncpy(hostname
, he
->h_name
, sbuf
);
1300 return GSOCK_NOERROR
;
1303 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1305 struct sockaddr_in
*addr
;
1307 assert(address
!= NULL
);
1308 CHECK_ADDRESS(address
, INET
, 0);
1310 addr
= (struct sockaddr_in
*)address
->m_addr
;
1312 return addr
->sin_addr
.s_addr
;
1315 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1317 struct sockaddr_in
*addr
;
1319 assert(address
!= NULL
);
1320 CHECK_ADDRESS(address
, INET
, 0);
1322 addr
= (struct sockaddr_in
*)address
->m_addr
;
1323 return ntohs(addr
->sin_port
);
1327 * -------------------------------------------------------------------------
1328 * Unix address family
1329 * -------------------------------------------------------------------------
1332 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1334 assert (address
!= NULL
);
1335 address
->m_error
= GSOCK_INVADDR
;
1336 return GSOCK_INVADDR
;
1339 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1341 assert (address
!= NULL
);
1342 address
->m_error
= GSOCK_INVADDR
;
1343 return GSOCK_INVADDR
;
1346 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1348 assert (address
!= NULL
);
1349 address
->m_error
= GSOCK_INVADDR
;
1350 return GSOCK_INVADDR
;
1354 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */
1359 /* Diferencias con la version Unix:
1360 * - El descriptor es SOCKET y no int
1361 * - Constantes -1 pasan a INVALID_SOCKET
1362 * - Errores en muchas funciones pasan de -1 o <0 a SOCKET_ERROR
1363 * - ioctl y close pasan a ioctlsocket y closesocket
1364 * - inet_addr en lugar de inet_aton
1365 * - Codigo de inicializacion y terminacion para inicializar y
1366 * terminar WinSocket y para la ventana interna.
1367 * - SetTimeout, SetNonBlocking y la implementacion de los
1368 * timeouts eran bastante diferentes, pero ahora se han
1369 * hecho en la version Unix igual que en esta.