00e5d32ace9988c498406b5ade1dac52c3c91a4b
1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket)
4 * Author: Guillermo Rodriguez Garcia <guille@iies.es>
5 * Purpose: GSocket main MSW file
7 * -------------------------------------------------------------------------
11 * PLEASE don't put C++ comments here - this is a C source file.
14 #ifndef __GSOCKET_STANDALONE__
18 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
20 #ifndef __GSOCKET_STANDALONE__
22 #include "wx/msw/gsockmsw.h"
23 #include "wx/gsocket.h"
30 #endif /* __GSOCKET_STANDALONE__ */
40 /* if we use configure for MSW SOCKLEN_T will be already defined */
46 /* using FD_SET results in this warning */
47 #pragma warning(disable:4127) /* conditional expression is constant */
48 #endif /* Visual C++ */
51 /* Constructors / Destructors for GSocket */
53 GSocket
*GSocket_new()
58 if ((socket
= (GSocket
*) malloc(sizeof(GSocket
))) == NULL
)
61 socket
->m_fd
= INVALID_SOCKET
;
62 for (i
= 0; i
< GSOCK_MAX_EVENT
; i
++)
64 socket
->m_cbacks
[i
] = NULL
;
66 socket
->m_local
= NULL
;
67 socket
->m_peer
= NULL
;
68 socket
->m_error
= GSOCK_NOERROR
;
69 socket
->m_server
= FALSE
;
70 socket
->m_stream
= TRUE
;
71 socket
->m_non_blocking
= FALSE
;
72 socket
->m_timeout
.tv_sec
= 10 * 60; /* 10 minutes */
73 socket
->m_timeout
.tv_usec
= 0;
74 socket
->m_detected
= 0;
76 /* Per-socket GUI-specific initialization */
77 if (!_GSocket_GUI_Init(socket
))
86 void GSocket_destroy(GSocket
*socket
)
88 assert(socket
!= NULL
);
90 /* Per-socket GUI-specific cleanup */
91 _GSocket_GUI_Destroy(socket
);
93 /* Check that the socket is really shutdowned */
94 if (socket
->m_fd
!= INVALID_SOCKET
)
95 GSocket_Shutdown(socket
);
97 /* Destroy private addresses */
99 GAddress_destroy(socket
->m_local
);
102 GAddress_destroy(socket
->m_peer
);
104 /* Destroy the socket itself */
109 * Disallow further read/write operations on this socket, close
110 * the fd and disable all callbacks.
112 void GSocket_Shutdown(GSocket
*socket
)
116 assert(socket
!= NULL
);
118 /* If socket has been created, shutdown it */
119 if (socket
->m_fd
!= INVALID_SOCKET
)
121 shutdown(socket
->m_fd
, 2);
122 closesocket(socket
->m_fd
);
123 socket
->m_fd
= INVALID_SOCKET
;
126 /* Disable GUI callbacks */
127 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
128 socket
->m_cbacks
[evt
] = NULL
;
130 socket
->m_detected
= GSOCK_LOST_FLAG
;
131 _GSocket_Disable_Events(socket
);
134 /* Address handling */
140 * Set or get the local or peer address for this socket. The 'set'
141 * functions return GSOCK_NOERROR on success, an error code otherwise.
142 * The 'get' functions return a pointer to a GAddress object on success,
143 * or NULL otherwise, in which case they set the error code of the
144 * corresponding GSocket.
147 * GSOCK_INVSOCK - the socket is not valid.
148 * GSOCK_INVADDR - the address is not valid.
150 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
152 assert(socket
!= NULL
);
154 /* the socket must be initialized, or it must be a server */
155 if (socket
->m_fd
!= INVALID_SOCKET
&& !socket
->m_server
)
157 socket
->m_error
= GSOCK_INVSOCK
;
158 return GSOCK_INVSOCK
;
162 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
164 socket
->m_error
= GSOCK_INVADDR
;
165 return GSOCK_INVADDR
;
169 GAddress_destroy(socket
->m_local
);
171 socket
->m_local
= GAddress_copy(address
);
173 return GSOCK_NOERROR
;
176 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
178 assert(socket
!= NULL
);
181 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
183 socket
->m_error
= GSOCK_INVADDR
;
184 return GSOCK_INVADDR
;
188 GAddress_destroy(socket
->m_peer
);
190 socket
->m_peer
= GAddress_copy(address
);
192 return GSOCK_NOERROR
;
195 GAddress
*GSocket_GetLocal(GSocket
*socket
)
198 struct sockaddr addr
;
199 SOCKLEN_T size
= sizeof(addr
);
202 assert(socket
!= NULL
);
204 /* try to get it from the m_local var first */
206 return GAddress_copy(socket
->m_local
);
208 /* else, if the socket is initialized, try getsockname */
209 if (socket
->m_fd
== INVALID_SOCKET
)
211 socket
->m_error
= GSOCK_INVSOCK
;
215 if (getsockname(socket
->m_fd
, &addr
, &size
) == SOCKET_ERROR
)
217 socket
->m_error
= GSOCK_IOERR
;
221 /* got a valid address from getsockname, create a GAddress object */
222 if ((address
= GAddress_new()) == NULL
)
224 socket
->m_error
= GSOCK_MEMERR
;
228 if ((err
= _GAddress_translate_from(address
, &addr
, size
)) != GSOCK_NOERROR
)
230 GAddress_destroy(address
);
231 socket
->m_error
= err
;
238 GAddress
*GSocket_GetPeer(GSocket
*socket
)
240 assert(socket
!= NULL
);
242 /* try to get it from the m_peer var */
244 return GAddress_copy(socket
->m_peer
);
249 /* Server specific parts */
251 /* GSocket_SetServer:
252 * Sets up this socket as a server. The local address must have been
253 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
254 * Returns GSOCK_NOERROR on success, one of the following otherwise:
257 * GSOCK_INVSOCK - the socket is in use.
258 * GSOCK_INVADDR - the local address has not been set.
259 * GSOCK_IOERR - low-level error.
261 GSocketError
GSocket_SetServer(GSocket
*sck
)
267 /* must not be in use */
268 if (sck
->m_fd
!= INVALID_SOCKET
)
270 sck
->m_error
= GSOCK_INVSOCK
;
271 return GSOCK_INVSOCK
;
274 /* the local addr must have been set */
277 sck
->m_error
= GSOCK_INVADDR
;
278 return GSOCK_INVADDR
;
281 /* Initialize all fields */
282 sck
->m_server
= TRUE
;
283 sck
->m_stream
= TRUE
;
284 sck
->m_oriented
= TRUE
;
286 /* Create the socket */
287 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
289 if (sck
->m_fd
== INVALID_SOCKET
)
291 sck
->m_error
= GSOCK_IOERR
;
295 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
296 _GSocket_Enable_Events(sck
);
298 /* Bind to the local address,
299 * retrieve the actual address bound,
300 * and listen up to 5 connections.
302 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
303 (getsockname(sck
->m_fd
,
304 sck
->m_local
->m_addr
,
305 (SOCKLEN_T
*)&sck
->m_local
->m_len
) != 0) ||
306 (listen(sck
->m_fd
, 5) != 0))
308 closesocket(sck
->m_fd
);
309 sck
->m_fd
= INVALID_SOCKET
;
310 sck
->m_error
= GSOCK_IOERR
;
314 return GSOCK_NOERROR
;
317 /* GSocket_WaitConnection:
318 * Waits for an incoming client connection. Returns a pointer to
319 * a GSocket object, or NULL if there was an error, in which case
320 * the last error field will be updated for the calling GSocket.
322 * Error codes (set in the calling GSocket)
323 * GSOCK_INVSOCK - the socket is not valid or not a server.
324 * GSOCK_TIMEDOUT - timeout, no incoming connections.
325 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
326 * GSOCK_MEMERR - couldn't allocate memory.
327 * GSOCK_IOERR - low-level error.
329 GSocket
*GSocket_WaitConnection(GSocket
*sck
)
332 struct sockaddr from
;
333 SOCKLEN_T fromlen
= sizeof(from
);
339 /* Reenable CONNECTION events */
340 sck
->m_detected
&= ~GSOCK_CONNECTION_FLAG
;
342 /* If the socket has already been created, we exit immediately */
343 if (sck
->m_fd
== INVALID_SOCKET
|| !sck
->m_server
)
345 sck
->m_error
= GSOCK_INVSOCK
;
349 /* Create a GSocket object for the new connection */
350 connection
= GSocket_new();
354 sck
->m_error
= GSOCK_MEMERR
;
358 /* Wait for a connection (with timeout) */
359 if (_GSocket_Input_Timeout(sck
) == GSOCK_TIMEDOUT
)
361 GSocket_destroy(connection
);
362 /* sck->m_error set by _GSocket_Input_Timeout */
366 connection
->m_fd
= accept(sck
->m_fd
, &from
, &fromlen
);
368 if (connection
->m_fd
== INVALID_SOCKET
)
370 if (WSAGetLastError() == WSAEWOULDBLOCK
)
371 sck
->m_error
= GSOCK_WOULDBLOCK
;
373 sck
->m_error
= GSOCK_IOERR
;
375 GSocket_destroy(connection
);
379 /* Initialize all fields */
380 connection
->m_server
= FALSE
;
381 connection
->m_stream
= TRUE
;
382 connection
->m_oriented
= TRUE
;
384 /* Setup the peer address field */
385 connection
->m_peer
= GAddress_new();
386 if (!connection
->m_peer
)
388 GSocket_destroy(connection
);
389 sck
->m_error
= GSOCK_MEMERR
;
392 err
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
);
393 if (err
!= GSOCK_NOERROR
)
395 GAddress_destroy(connection
->m_peer
);
396 GSocket_destroy(connection
);
401 ioctlsocket(connection
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
402 _GSocket_Enable_Events(connection
);
407 /* Client specific parts */
410 * For stream (connection oriented) sockets, GSocket_Connect() tries
411 * to establish a client connection to a server using the peer address
412 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
413 * connection has been succesfully established, or one of the error
414 * codes listed below. Note that for nonblocking sockets, a return
415 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
416 * request can be completed later; you should use GSocket_Select()
417 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
418 * corresponding asynchronous events.
420 * For datagram (non connection oriented) sockets, GSocket_Connect()
421 * just sets the peer address established with GSocket_SetPeer() as
422 * default destination.
425 * GSOCK_INVSOCK - the socket is in use or not valid.
426 * GSOCK_INVADDR - the peer address has not been established.
427 * GSOCK_TIMEDOUT - timeout, the connection failed.
428 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
429 * GSOCK_MEMERR - couldn't allocate memory.
430 * GSOCK_IOERR - low-level error.
432 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
439 /* Enable CONNECTION events (needed for nonblocking connections) */
440 sck
->m_detected
&= ~GSOCK_CONNECTION_FLAG
;
442 if (sck
->m_fd
!= INVALID_SOCKET
)
444 sck
->m_error
= GSOCK_INVSOCK
;
445 return GSOCK_INVSOCK
;
450 sck
->m_error
= GSOCK_INVADDR
;
451 return GSOCK_INVADDR
;
454 /* Streamed or dgram socket? */
455 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
456 sck
->m_oriented
= TRUE
;
457 sck
->m_server
= FALSE
;
459 /* Create the socket */
460 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
,
461 sck
->m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
463 if (sck
->m_fd
== INVALID_SOCKET
)
465 sck
->m_error
= GSOCK_IOERR
;
469 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
470 _GSocket_Enable_Events(sck
);
472 /* Connect it to the peer address, with a timeout (see below) */
473 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
475 if (ret
== SOCKET_ERROR
)
477 err
= WSAGetLastError();
479 /* If connect failed with EWOULDBLOCK and the GSocket object
480 * is in blocking mode, we select() for the specified timeout
481 * checking for writability to see if the connection request
484 if ((err
== WSAEWOULDBLOCK
) && (!sck
->m_non_blocking
))
486 err
= _GSocket_Connect_Timeout(sck
);
488 if (err
!= GSOCK_NOERROR
)
490 closesocket(sck
->m_fd
);
491 sck
->m_fd
= INVALID_SOCKET
;
492 /* sck->m_error is set in _GSocket_Connect_Timeout */
498 /* If connect failed with EWOULDBLOCK and the GSocket object
499 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
500 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
501 * this way if the connection completes, a GSOCK_CONNECTION
502 * event will be generated, if enabled.
504 if ((err
== WSAEWOULDBLOCK
) && (sck
->m_non_blocking
))
506 sck
->m_error
= GSOCK_WOULDBLOCK
;
507 return GSOCK_WOULDBLOCK
;
510 /* If connect failed with an error other than EWOULDBLOCK,
511 * then the call to GSocket_Connect() has failed.
513 closesocket(sck
->m_fd
);
514 sck
->m_fd
= INVALID_SOCKET
;
515 sck
->m_error
= GSOCK_IOERR
;
519 return GSOCK_NOERROR
;
522 /* Datagram sockets */
524 /* GSocket_SetNonOriented:
525 * Sets up this socket as a non-connection oriented (datagram) socket.
526 * Before using this function, the local address must have been set
527 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
528 * on success, or one of the following otherwise.
531 * GSOCK_INVSOCK - the socket is in use.
532 * GSOCK_INVADDR - the local address has not been set.
533 * GSOCK_IOERR - low-level error.
535 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
541 if (sck
->m_fd
!= INVALID_SOCKET
)
543 sck
->m_error
= GSOCK_INVSOCK
;
544 return GSOCK_INVSOCK
;
549 sck
->m_error
= GSOCK_INVADDR
;
550 return GSOCK_INVADDR
;
553 /* Initialize all fields */
554 sck
->m_stream
= FALSE
;
555 sck
->m_server
= FALSE
;
556 sck
->m_oriented
= FALSE
;
558 /* Create the socket */
559 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
561 if (sck
->m_fd
== INVALID_SOCKET
)
563 sck
->m_error
= GSOCK_IOERR
;
567 ioctlsocket(sck
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
568 _GSocket_Enable_Events(sck
);
570 /* Bind to the local address,
571 * and retrieve the actual address bound.
573 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
574 (getsockname(sck
->m_fd
,
575 sck
->m_local
->m_addr
,
576 (SOCKLEN_T
*)&sck
->m_local
->m_len
) != 0))
578 closesocket(sck
->m_fd
);
579 sck
->m_fd
= INVALID_SOCKET
;
580 sck
->m_error
= GSOCK_IOERR
;
584 return GSOCK_NOERROR
;
589 /* Like recv(), send(), ... */
590 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
594 assert(socket
!= NULL
);
596 /* Reenable INPUT events */
597 socket
->m_detected
&= ~GSOCK_INPUT_FLAG
;
599 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
601 socket
->m_error
= GSOCK_INVSOCK
;
605 /* If the socket is blocking, wait for data (with a timeout) */
606 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
610 if (socket
->m_stream
)
611 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
613 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
615 if (ret
== SOCKET_ERROR
)
617 if (WSAGetLastError() != WSAEWOULDBLOCK
)
618 socket
->m_error
= GSOCK_IOERR
;
620 socket
->m_error
= GSOCK_WOULDBLOCK
;
627 int GSocket_Write(GSocket
*socket
, const char *buffer
, int size
)
631 assert(socket
!= NULL
);
633 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
635 socket
->m_error
= GSOCK_INVSOCK
;
639 /* If the socket is blocking, wait for writability (with a timeout) */
640 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
644 if (socket
->m_stream
)
645 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
647 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
649 if (ret
== SOCKET_ERROR
)
651 if (WSAGetLastError() != WSAEWOULDBLOCK
)
652 socket
->m_error
= GSOCK_IOERR
;
654 socket
->m_error
= GSOCK_WOULDBLOCK
;
656 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
657 * does). Once the first OUTPUT event is received, users can assume
658 * that the socket is writable until a read operation fails. Only then
659 * will further OUTPUT events be posted.
661 socket
->m_detected
&= ~GSOCK_OUTPUT_FLAG
;
669 * Polls the socket to determine its status. This function will
670 * check for the events specified in the 'flags' parameter, and
671 * it will return a mask indicating which operations can be
672 * performed. This function won't block, regardless of the
673 * mode (blocking | nonblocking) of the socket.
675 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
677 assert(socket
!= NULL
);
679 return flags
& socket
->m_detected
;
684 /* GSocket_SetNonBlocking:
685 * Sets the socket to non-blocking mode. All IO calls will return
688 void GSocket_SetNonBlocking(GSocket
*socket
, bool non_block
)
690 assert(socket
!= NULL
);
692 socket
->m_non_blocking
= non_block
;
695 /* GSocket_SetTimeout:
696 * Sets the timeout for blocking calls. Time is expressed in
699 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millis
)
701 assert(socket
!= NULL
);
703 socket
->m_timeout
.tv_sec
= (millis
/ 1000);
704 socket
->m_timeout
.tv_usec
= (millis
% 1000) * 1000;
708 * Returns the last error occured for this socket. Note that successful
709 * operations do not clear this back to GSOCK_NOERROR, so use it only
712 GSocketError
GSocket_GetError(GSocket
*socket
)
714 assert(socket
!= NULL
);
716 return socket
->m_error
;
722 * There is data to be read in the input buffer. If, after a read
723 * operation, there is still data available, the callback function will
726 * The socket is available for writing. That is, the next write call
727 * won't block. This event is generated only once, when the connection is
728 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
729 * when the output buffer empties again. This means that the app should
730 * assume that it can write since the first OUTPUT event, and no more
731 * OUTPUT events will be generated unless an error occurs.
733 * Connection succesfully established, for client sockets, or incoming
734 * client connection, for server sockets. Wait for this event (also watch
735 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
737 * The connection is lost (or a connection request failed); this could
738 * be due to a failure, or due to the peer closing it gracefully.
741 /* GSocket_SetCallback:
742 * Enables the callbacks specified by 'flags'. Note that 'flags'
743 * may be a combination of flags OR'ed toghether, so the same
744 * callback function can be made to accept different events.
745 * The callback function must have the following prototype:
747 * void function(GSocket *socket, GSocketEvent event, char *cdata)
749 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
750 GSocketCallback callback
, char *cdata
)
754 assert(socket
!= NULL
);
756 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
758 if ((flags
& (1 << count
)) != 0)
760 socket
->m_cbacks
[count
] = callback
;
761 socket
->m_data
[count
] = cdata
;
766 /* GSocket_UnsetCallback:
767 * Disables all callbacks specified by 'flags', which may be a
768 * combination of flags OR'ed toghether.
770 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
774 assert(socket
!= NULL
);
776 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
778 if ((flags
& (1 << count
)) != 0)
780 socket
->m_cbacks
[count
] = NULL
;
781 socket
->m_data
[count
] = NULL
;
788 /* _GSocket_Input_Timeout:
789 * For blocking sockets, wait until data is available or
790 * until timeout ellapses.
792 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
796 if (!socket
->m_non_blocking
)
799 FD_SET(socket
->m_fd
, &readfds
);
800 if (select(0, &readfds
, NULL
, NULL
, &socket
->m_timeout
) == 0)
802 socket
->m_error
= GSOCK_TIMEDOUT
;
803 return GSOCK_TIMEDOUT
;
806 return GSOCK_NOERROR
;
809 /* _GSocket_Output_Timeout:
810 * For blocking sockets, wait until data can be sent without
811 * blocking or until timeout ellapses.
813 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
817 if (!socket
->m_non_blocking
)
820 FD_SET(socket
->m_fd
, &writefds
);
821 if (select(0, NULL
, &writefds
, NULL
, &socket
->m_timeout
) == 0)
823 socket
->m_error
= GSOCK_TIMEDOUT
;
824 return GSOCK_TIMEDOUT
;
827 return GSOCK_NOERROR
;
830 /* _GSocket_Connect_Timeout:
831 * For blocking sockets, wait until the connection is
832 * established or fails, or until timeout ellapses.
834 GSocketError
_GSocket_Connect_Timeout(GSocket
*socket
)
841 FD_SET(socket
->m_fd
, &writefds
);
842 FD_SET(socket
->m_fd
, &exceptfds
);
843 if (select(0, NULL
, &writefds
, &exceptfds
, &socket
->m_timeout
) == 0)
845 socket
->m_error
= GSOCK_TIMEDOUT
;
846 return GSOCK_TIMEDOUT
;
848 if (!FD_ISSET(socket
->m_fd
, &writefds
))
850 socket
->m_error
= GSOCK_IOERR
;
854 return GSOCK_NOERROR
;
857 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
859 return recv(socket
->m_fd
, buffer
, size
, 0);
862 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
864 struct sockaddr from
;
865 SOCKLEN_T fromlen
= sizeof(from
);
869 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
871 if (ret
== SOCKET_ERROR
)
874 /* Translate a system address into a GSocket address */
877 socket
->m_peer
= GAddress_new();
880 socket
->m_error
= GSOCK_MEMERR
;
884 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
885 if (err
!= GSOCK_NOERROR
)
887 GAddress_destroy(socket
->m_peer
);
888 socket
->m_peer
= NULL
;
889 socket
->m_error
= err
;
896 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
898 return send(socket
->m_fd
, buffer
, size
, 0);
901 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
903 struct sockaddr
*addr
;
909 socket
->m_error
= GSOCK_INVADDR
;
913 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
914 if (err
!= GSOCK_NOERROR
)
916 socket
->m_error
= err
;
920 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
922 /* Frees memory allocated by _GAddress_translate_to */
930 * -------------------------------------------------------------------------
932 * -------------------------------------------------------------------------
935 /* CHECK_ADDRESS verifies that the current family is either GSOCK_NOFAMILY
936 * or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it initalizes address
937 * to be a GSOCK_*family*. In other cases, it returns GSOCK_INVADDR.
939 #define CHECK_ADDRESS(address, family, retval) \
941 if (address->m_family == GSOCK_NOFAMILY) \
942 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
943 return address->m_error; \
944 if (address->m_family != GSOCK_##family) \
946 address->m_error = GSOCK_INVADDR; \
951 GAddress
*GAddress_new()
955 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
958 address
->m_family
= GSOCK_NOFAMILY
;
959 address
->m_addr
= NULL
;
965 GAddress
*GAddress_copy(GAddress
*address
)
969 assert(address
!= NULL
);
971 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
974 memcpy(addr2
, address
, sizeof(GAddress
));
978 addr2
->m_addr
= (struct sockaddr
*) malloc(addr2
->m_len
);
979 if (addr2
->m_addr
== NULL
)
984 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
990 void GAddress_destroy(GAddress
*address
)
992 assert(address
!= NULL
);
997 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
999 assert(address
!= NULL
);
1001 address
->m_family
= type
;
1004 GAddressType
GAddress_GetFamily(GAddress
*address
)
1006 assert(address
!= NULL
);
1008 return address
->m_family
;
1011 GSocketError
_GAddress_translate_from(GAddress
*address
,
1012 struct sockaddr
*addr
, int len
)
1014 address
->m_realfamily
= addr
->sa_family
;
1015 switch (addr
->sa_family
)
1018 address
->m_family
= GSOCK_INET
;
1021 address
->m_family
= GSOCK_UNIX
;
1025 address
->m_family
= GSOCK_INET6
;
1030 address
->m_error
= GSOCK_INVOP
;
1035 if (address
->m_addr
)
1036 free(address
->m_addr
);
1038 address
->m_len
= len
;
1039 address
->m_addr
= (struct sockaddr
*) malloc(len
);
1041 if (address
->m_addr
== NULL
)
1043 address
->m_error
= GSOCK_MEMERR
;
1044 return GSOCK_MEMERR
;
1046 memcpy(address
->m_addr
, addr
, len
);
1048 return GSOCK_NOERROR
;
1051 GSocketError
_GAddress_translate_to(GAddress
*address
,
1052 struct sockaddr
**addr
, int *len
)
1054 if (!address
->m_addr
)
1056 address
->m_error
= GSOCK_INVADDR
;
1057 return GSOCK_INVADDR
;
1060 *len
= address
->m_len
;
1061 *addr
= (struct sockaddr
*) malloc(address
->m_len
);
1064 address
->m_error
= GSOCK_MEMERR
;
1065 return GSOCK_MEMERR
;
1068 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1069 return GSOCK_NOERROR
;
1073 * -------------------------------------------------------------------------
1074 * Internet address family
1075 * -------------------------------------------------------------------------
1078 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1080 address
->m_len
= sizeof(struct sockaddr_in
);
1081 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1082 if (address
->m_addr
== NULL
)
1084 address
->m_error
= GSOCK_MEMERR
;
1085 return GSOCK_MEMERR
;
1088 address
->m_family
= GSOCK_INET
;
1089 address
->m_realfamily
= PF_INET
;
1090 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1091 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1093 return GSOCK_NOERROR
;
1096 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1099 struct in_addr
*addr
;
1101 assert(address
!= NULL
);
1103 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1105 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1107 addr
->s_addr
= inet_addr(hostname
);
1109 /* If it is a numeric host name, convert it now */
1110 if (addr
->s_addr
== INADDR_NONE
)
1112 struct in_addr
*array_addr
;
1114 /* It is a real name, we solve it */
1115 if ((he
= gethostbyname(hostname
)) == NULL
)
1117 /* addr->s_addr = INADDR_NONE just done by inet_addr() above */
1118 address
->m_error
= GSOCK_NOHOST
;
1119 return GSOCK_NOHOST
;
1121 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1122 addr
->s_addr
= array_addr
[0].s_addr
;
1124 return GSOCK_NOERROR
;
1127 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1129 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1132 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1133 unsigned long hostaddr
)
1135 struct in_addr
*addr
;
1137 assert(address
!= NULL
);
1139 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1141 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1142 addr
->s_addr
= hostaddr
;
1144 return GSOCK_NOERROR
;
1147 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1148 const char *protocol
)
1151 struct sockaddr_in
*addr
;
1153 assert(address
!= NULL
);
1154 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1158 address
->m_error
= GSOCK_INVPORT
;
1159 return GSOCK_INVPORT
;
1162 se
= getservbyname(port
, protocol
);
1165 if (isdigit(port
[0]))
1169 port_int
= atoi(port
);
1170 addr
= (struct sockaddr_in
*)address
->m_addr
;
1171 addr
->sin_port
= htons((u_short
) port_int
);
1172 return GSOCK_NOERROR
;
1175 address
->m_error
= GSOCK_INVPORT
;
1176 return GSOCK_INVPORT
;
1179 addr
= (struct sockaddr_in
*)address
->m_addr
;
1180 addr
->sin_port
= se
->s_port
;
1182 return GSOCK_NOERROR
;
1185 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1187 struct sockaddr_in
*addr
;
1189 assert(address
!= NULL
);
1190 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1192 addr
= (struct sockaddr_in
*)address
->m_addr
;
1193 addr
->sin_port
= htons(port
);
1195 return GSOCK_NOERROR
;
1198 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1202 struct sockaddr_in
*addr
;
1204 assert(address
!= NULL
);
1205 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1207 addr
= (struct sockaddr_in
*)address
->m_addr
;
1208 addr_buf
= (char *)&(addr
->sin_addr
);
1210 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1213 address
->m_error
= GSOCK_NOHOST
;
1214 return GSOCK_NOHOST
;
1217 strncpy(hostname
, he
->h_name
, sbuf
);
1219 return GSOCK_NOERROR
;
1222 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1224 struct sockaddr_in
*addr
;
1226 assert(address
!= NULL
);
1227 CHECK_ADDRESS(address
, INET
, 0);
1229 addr
= (struct sockaddr_in
*)address
->m_addr
;
1231 return addr
->sin_addr
.s_addr
;
1234 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1236 struct sockaddr_in
*addr
;
1238 assert(address
!= NULL
);
1239 CHECK_ADDRESS(address
, INET
, 0);
1241 addr
= (struct sockaddr_in
*)address
->m_addr
;
1242 return ntohs(addr
->sin_port
);
1246 * -------------------------------------------------------------------------
1247 * Unix address family
1248 * -------------------------------------------------------------------------
1252 #pragma warning(disable:4100) /* unreferenced formal parameter */
1253 #endif /* Visual C++ */
1255 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1257 assert (address
!= NULL
);
1258 address
->m_error
= GSOCK_INVADDR
;
1259 return GSOCK_INVADDR
;
1262 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1264 assert (address
!= NULL
);
1265 address
->m_error
= GSOCK_INVADDR
;
1266 return GSOCK_INVADDR
;
1269 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1271 assert (address
!= NULL
);
1272 address
->m_error
= GSOCK_INVADDR
;
1273 return GSOCK_INVADDR
;
1276 #else /* !wxUSE_SOCKETS */
1279 * Translation unit shouldn't be empty, so include this typedef to make the
1280 * compiler (VC++ 6.0, for example) happy
1282 typedef (*wxDummy
)();
1284 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */