]>
git.saurik.com Git - wxWidgets.git/blob - src/unix/gsocket.c
1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket) for WX
4 * Authors: Guilhem Lavaux,
5 * Guillermo Rodriguez Garcia <guille@iies.es> (maintainer)
6 * Purpose: GSocket main Unix file
8 * -------------------------------------------------------------------------
12 * PLEASE don't put C++ comments here - this is a C source file.
15 #ifndef __GSOCKET_STANDALONE__
19 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
22 #include <sys/types.h>
24 #include <sys/ioctl.h>
29 u_char sun_len
; /* sockaddr len including null */
30 u_char sun_family
; /* AF_UNIX */
31 char sun_path
[108]; /* path name (gag) */
34 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
49 # include <sys/filio.h>
59 # define SOCKLEN_T unsigned int
63 # define SOCKLEN_T socklen_t
66 # define SOCKLEN_T int
70 #endif /* SOCKLEN_T */
74 * INADDR_BROADCAST is identical to INADDR_NONE which is not defined
75 * on all unices. INADDR_BROADCAST should be fine to indicate an error.
78 #define INADDR_NONE INADDR_BROADCAST
81 #define MASK_SIGNAL() \
83 void (*old_handler)(int); \
85 old_handler = signal(SIGPIPE, SIG_IGN);
87 #define UNMASK_SIGNAL() \
88 signal(SIGPIPE, old_handler); \
92 #ifndef __GSOCKET_STANDALONE__
94 #include "wx/unix/gsockunx.h"
95 #include "wx/gsocket.h"
102 #endif /* __GSOCKET_STANDALONE__ */
104 /* redefine some GUI-only functions to do nothing in console mode */
105 #if defined(wxUSE_GUI) && !wxUSE_GUI
106 #define _GSocket_GUI_Init(socket)
107 #define _GSocket_GUI_Destroy(socket)
108 #define _GSocket_Enable_Events(socket)
109 #define _GSocket_Disable_Events(socket)
110 #define _GSocket_Install_Callback(socket, event)
111 #define _GSocket_Uninstall_Callback(socket, event)
112 #endif /* wxUSE_GUI */
114 /* Global initialisers */
121 void GSocket_Cleanup()
125 /* Constructors / Destructors for GSocket */
127 GSocket
*GSocket_new()
132 socket
= (GSocket
*)malloc(sizeof(GSocket
));
138 for (i
=0;i
<GSOCK_MAX_EVENT
;i
++)
140 socket
->m_cbacks
[i
] = NULL
;
142 socket
->m_detected
= 0;
143 socket
->m_local
= NULL
;
144 socket
->m_peer
= NULL
;
145 socket
->m_error
= GSOCK_NOERROR
;
146 socket
->m_server
= FALSE
;
147 socket
->m_stream
= TRUE
;
148 socket
->m_gui_dependent
= NULL
;
149 socket
->m_non_blocking
= FALSE
;
150 socket
->m_timeout
= 10*60*1000;
151 /* 10 minutes * 60 sec * 1000 millisec */
152 socket
->m_establishing
= FALSE
;
154 /* We initialize the GUI specific entries here */
155 _GSocket_GUI_Init(socket
);
160 void GSocket_destroy(GSocket
*socket
)
162 assert(socket
!= NULL
);
164 /* Check that the socket is really shutdowned */
165 if (socket
->m_fd
!= -1)
166 GSocket_Shutdown(socket
);
168 /* We destroy GUI specific variables */
169 _GSocket_GUI_Destroy(socket
);
171 /* Destroy private addresses */
173 GAddress_destroy(socket
->m_local
);
176 GAddress_destroy(socket
->m_peer
);
178 /* Destroy the socket itself */
183 * Disallow further read/write operations on this socket, close
184 * the fd and disable all callbacks.
186 void GSocket_Shutdown(GSocket
*socket
)
190 assert(socket
!= NULL
);
192 /* If socket has been created, shutdown it */
193 if (socket
->m_fd
!= -1)
195 shutdown(socket
->m_fd
, 2);
200 /* Disable GUI callbacks */
201 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
202 socket
->m_cbacks
[evt
] = NULL
;
204 socket
->m_detected
= GSOCK_LOST_FLAG
;
205 _GSocket_Disable_Events(socket
);
208 /* Address handling */
214 * Set or get the local or peer address for this socket. The 'set'
215 * functions return GSOCK_NOERROR on success, an error code otherwise.
216 * The 'get' functions return a pointer to a GAddress object on success,
217 * or NULL otherwise, in which case they set the error code of the
218 * corresponding GSocket.
221 * GSOCK_INVSOCK - the socket is not valid.
222 * GSOCK_INVADDR - the address is not valid.
224 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
226 assert(socket
!= NULL
);
228 /* the socket must be initialized, or it must be a server */
229 if ((socket
->m_fd
!= -1 && !socket
->m_server
))
231 socket
->m_error
= GSOCK_INVSOCK
;
232 return GSOCK_INVSOCK
;
236 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
238 socket
->m_error
= GSOCK_INVADDR
;
239 return GSOCK_INVADDR
;
243 GAddress_destroy(socket
->m_local
);
245 socket
->m_local
= GAddress_copy(address
);
247 return GSOCK_NOERROR
;
250 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
252 assert(socket
!= NULL
);
255 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
257 socket
->m_error
= GSOCK_INVADDR
;
258 return GSOCK_INVADDR
;
262 GAddress_destroy(socket
->m_peer
);
264 socket
->m_peer
= GAddress_copy(address
);
266 return GSOCK_NOERROR
;
269 GAddress
*GSocket_GetLocal(GSocket
*socket
)
272 struct sockaddr addr
;
273 SOCKLEN_T size
= sizeof(addr
);
276 assert(socket
!= NULL
);
278 /* try to get it from the m_local var first */
280 return GAddress_copy(socket
->m_local
);
282 /* else, if the socket is initialized, try getsockname */
283 if (socket
->m_fd
== -1)
285 socket
->m_error
= GSOCK_INVSOCK
;
289 if (getsockname(socket
->m_fd
, &addr
, (SOCKLEN_T
*) &size
) < 0)
291 socket
->m_error
= GSOCK_IOERR
;
295 /* got a valid address from getsockname, create a GAddress object */
296 address
= GAddress_new();
299 socket
->m_error
= GSOCK_MEMERR
;
303 err
= _GAddress_translate_from(address
, &addr
, size
);
304 if (err
!= GSOCK_NOERROR
)
306 GAddress_destroy(address
);
307 socket
->m_error
= err
;
314 GAddress
*GSocket_GetPeer(GSocket
*socket
)
316 assert(socket
!= NULL
);
318 /* try to get it from the m_peer var */
320 return GAddress_copy(socket
->m_peer
);
325 /* Server specific parts */
327 /* GSocket_SetServer:
328 * Sets up this socket as a server. The local address must have been
329 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
330 * Returns GSOCK_NOERROR on success, one of the following otherwise:
333 * GSOCK_INVSOCK - the socket is in use.
334 * GSOCK_INVADDR - the local address has not been set.
335 * GSOCK_IOERR - low-level error.
337 GSocketError
GSocket_SetServer(GSocket
*sck
)
344 /* must not be in use */
347 sck
->m_error
= GSOCK_INVSOCK
;
348 return GSOCK_INVSOCK
;
351 /* the local addr must have been set */
354 sck
->m_error
= GSOCK_INVADDR
;
355 return GSOCK_INVADDR
;
358 /* Initialize all fields */
359 sck
->m_stream
= TRUE
;
360 sck
->m_server
= TRUE
;
361 sck
->m_oriented
= TRUE
;
363 /* Create the socket */
364 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
368 sck
->m_error
= GSOCK_IOERR
;
372 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
373 _GSocket_Enable_Events(sck
);
375 /* Bind to the local address,
376 * retrieve the actual address bound,
377 * and listen up to 5 connections.
379 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
380 (getsockname(sck
->m_fd
,
381 sck
->m_local
->m_addr
,
382 (SOCKLEN_T
*) &sck
->m_local
->m_len
) != 0) ||
383 (listen(sck
->m_fd
, 5) != 0))
387 sck
->m_error
= GSOCK_IOERR
;
391 return GSOCK_NOERROR
;
394 /* GSocket_WaitConnection:
395 * Waits for an incoming client connection. Returns a pointer to
396 * a GSocket object, or NULL if there was an error, in which case
397 * the last error field will be updated for the calling GSocket.
399 * Error codes (set in the calling GSocket)
400 * GSOCK_INVSOCK - the socket is not valid or not a server.
401 * GSOCK_TIMEDOUT - timeout, no incoming connections.
402 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
403 * GSOCK_MEMERR - couldn't allocate memory.
404 * GSOCK_IOERR - low-level error.
406 GSocket
*GSocket_WaitConnection(GSocket
*socket
)
408 struct sockaddr from
;
409 SOCKLEN_T fromlen
= sizeof(from
);
414 assert(socket
!= NULL
);
416 /* Reenable CONNECTION events */
417 _GSocket_Enable(socket
, GSOCK_CONNECTION
);
419 /* If the socket has already been created, we exit immediately */
420 if (socket
->m_fd
== -1 || !socket
->m_server
)
422 socket
->m_error
= GSOCK_INVSOCK
;
426 /* Create a GSocket object for the new connection */
427 connection
= GSocket_new();
431 socket
->m_error
= GSOCK_MEMERR
;
435 /* Wait for a connection (with timeout) */
436 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
438 GSocket_destroy(connection
);
439 /* socket->m_error set by _GSocket_Input_Timeout */
443 connection
->m_fd
= accept(socket
->m_fd
, &from
, (SOCKLEN_T
*) &fromlen
);
445 if (connection
->m_fd
== -1)
447 if (errno
== EWOULDBLOCK
)
448 socket
->m_error
= GSOCK_WOULDBLOCK
;
450 socket
->m_error
= GSOCK_IOERR
;
452 GSocket_destroy(connection
);
456 /* Initialize all fields */
457 connection
->m_server
= FALSE
;
458 connection
->m_stream
= TRUE
;
459 connection
->m_oriented
= TRUE
;
461 /* Setup the peer address field */
462 connection
->m_peer
= GAddress_new();
463 if (!connection
->m_peer
)
465 GSocket_destroy(connection
);
466 socket
->m_error
= GSOCK_MEMERR
;
469 err
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
);
470 if (err
!= GSOCK_NOERROR
)
472 GAddress_destroy(connection
->m_peer
);
473 GSocket_destroy(connection
);
474 socket
->m_error
= err
;
478 ioctl(connection
->m_fd
, FIONBIO
, &arg
);
479 _GSocket_Enable_Events(connection
);
484 /* Datagram sockets */
486 /* GSocket_SetNonOriented:
487 * Sets up this socket as a non-connection oriented (datagram) socket.
488 * Before using this function, the local address must have been set
489 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
490 * on success, or one of the following otherwise.
493 * GSOCK_INVSOCK - the socket is in use.
494 * GSOCK_INVADDR - the local address has not been set.
495 * GSOCK_IOERR - low-level error.
497 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
505 sck
->m_error
= GSOCK_INVSOCK
;
506 return GSOCK_INVSOCK
;
511 sck
->m_error
= GSOCK_INVADDR
;
512 return GSOCK_INVADDR
;
515 /* Initialize all fields */
516 sck
->m_stream
= FALSE
;
517 sck
->m_server
= FALSE
;
518 sck
->m_oriented
= FALSE
;
520 /* Create the socket */
521 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
525 sck
->m_error
= GSOCK_IOERR
;
529 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
530 _GSocket_Enable_Events(sck
);
532 /* Bind to the local address,
533 * and retrieve the actual address bound.
535 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
536 (getsockname(sck
->m_fd
,
537 sck
->m_local
->m_addr
,
538 (SOCKLEN_T
*) &sck
->m_local
->m_len
) != 0))
542 sck
->m_error
= GSOCK_IOERR
;
546 return GSOCK_NOERROR
;
549 /* Client specific parts */
552 * For stream (connection oriented) sockets, GSocket_Connect() tries
553 * to establish a client connection to a server using the peer address
554 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
555 * connection has been succesfully established, or one of the error
556 * codes listed below. Note that for nonblocking sockets, a return
557 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
558 * request can be completed later; you should use GSocket_Select()
559 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
560 * corresponding asynchronous events.
562 * For datagram (non connection oriented) sockets, GSocket_Connect()
563 * just sets the peer address established with GSocket_SetPeer() as
564 * default destination.
567 * GSOCK_INVSOCK - the socket is in use or not valid.
568 * GSOCK_INVADDR - the peer address has not been established.
569 * GSOCK_TIMEDOUT - timeout, the connection failed.
570 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
571 * GSOCK_MEMERR - couldn't allocate memory.
572 * GSOCK_IOERR - low-level error.
574 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
581 /* Enable CONNECTION events (needed for nonblocking connections) */
582 _GSocket_Enable(sck
, GSOCK_CONNECTION
);
586 sck
->m_error
= GSOCK_INVSOCK
;
587 return GSOCK_INVSOCK
;
592 sck
->m_error
= GSOCK_INVADDR
;
593 return GSOCK_INVADDR
;
596 /* Streamed or dgram socket? */
597 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
598 sck
->m_oriented
= TRUE
;
599 sck
->m_server
= FALSE
;
600 sck
->m_establishing
= FALSE
;
602 /* Create the socket */
603 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
,
604 sck
->m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
608 sck
->m_error
= GSOCK_IOERR
;
612 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
613 _GSocket_Enable_Events(sck
);
615 /* Connect it to the peer address, with a timeout (see below) */
616 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
622 /* If connect failed with EINPROGRESS and the GSocket object
623 * is in blocking mode, we select() for the specified timeout
624 * checking for writability to see if the connection request
627 if ((err
== EINPROGRESS
) && (!sck
->m_non_blocking
))
629 if (_GSocket_Output_Timeout(sck
) == GSOCK_TIMEDOUT
)
633 /* sck->m_error is set in _GSocket_Output_Timeout */
634 return GSOCK_TIMEDOUT
;
639 SOCKLEN_T len
= sizeof(error
);
641 getsockopt(sck
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*) &error
, &len
);
644 return GSOCK_NOERROR
;
648 /* If connect failed with EINPROGRESS and the GSocket object
649 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
650 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
651 * this way if the connection completes, a GSOCK_CONNECTION
652 * event will be generated, if enabled.
654 if ((err
== EINPROGRESS
) && (sck
->m_non_blocking
))
656 sck
->m_establishing
= TRUE
;
657 sck
->m_error
= GSOCK_WOULDBLOCK
;
658 return GSOCK_WOULDBLOCK
;
661 /* If connect failed with an error other than EINPROGRESS,
662 * then the call to GSocket_Connect has failed.
666 sck
->m_error
= GSOCK_IOERR
;
670 return GSOCK_NOERROR
;
675 /* Like recv(), send(), ... */
676 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
680 assert(socket
!= NULL
);
682 /* Reenable INPUT events */
683 _GSocket_Enable(socket
, GSOCK_INPUT
);
685 if (socket
->m_fd
== -1 || socket
->m_server
)
687 socket
->m_error
= GSOCK_INVSOCK
;
691 /* If the socket is blocking, wait for data (with a timeout) */
692 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
696 if (socket
->m_stream
)
697 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
699 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
703 if (errno
== EWOULDBLOCK
)
704 socket
->m_error
= GSOCK_WOULDBLOCK
;
706 socket
->m_error
= GSOCK_IOERR
;
712 int GSocket_Write(GSocket
*socket
, const char *buffer
, int size
)
716 assert(socket
!= NULL
);
718 printf( "GSocket_Write #1, size %d\n", size
);
720 if (socket
->m_fd
== -1 || socket
->m_server
)
722 socket
->m_error
= GSOCK_INVSOCK
;
726 printf( "GSocket_Write #2, size %d\n", size
);
728 /* If the socket is blocking, wait for writability (with a timeout) */
729 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
732 printf( "GSocket_Write #3, size %d\n", size
);
735 if (socket
->m_stream
)
736 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
738 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
740 printf( "GSocket_Write #4, size %d\n", size
);
744 if (errno
== EWOULDBLOCK
)
745 socket
->m_error
= GSOCK_WOULDBLOCK
;
747 socket
->m_error
= GSOCK_IOERR
;
749 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
750 * in MSW). Once the first OUTPUT event is received, users can assume
751 * that the socket is writable until a read operation fails. Only then
752 * will further OUTPUT events be posted.
754 _GSocket_Enable(socket
, GSOCK_OUTPUT
);
758 printf( "GSocket_Write #5, size %d ret %d\n", size
, ret
);
764 * Polls the socket to determine its status. This function will
765 * check for the events specified in the 'flags' parameter, and
766 * it will return a mask indicating which operations can be
767 * performed. This function won't block, regardless of the
768 * mode (blocking | nonblocking) of the socket.
770 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
772 assert(socket
!= NULL
);
774 return flags
& socket
->m_detected
;
779 /* GSocket_SetNonBlocking:
780 * Sets the socket to non-blocking mode. All IO calls will return
783 void GSocket_SetNonBlocking(GSocket
*socket
, bool non_block
)
785 assert(socket
!= NULL
);
787 socket
->m_non_blocking
= non_block
;
790 /* GSocket_SetTimeout:
791 * Sets the timeout for blocking calls. Time is expressed in
794 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
)
796 assert(socket
!= NULL
);
798 socket
->m_timeout
= millisec
;
802 * Returns the last error occured for this socket. Note that successful
803 * operations do not clear this back to GSOCK_NOERROR, so use it only
806 GSocketError
GSocket_GetError(GSocket
*socket
)
808 assert(socket
!= NULL
);
810 return socket
->m_error
;
816 * There is data to be read in the input buffer. If, after a read
817 * operation, there is still data available, the callback function will
820 * The socket is available for writing. That is, the next write call
821 * won't block. This event is generated only once, when the connection is
822 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
823 * when the output buffer empties again. This means that the app should
824 * assume that it can write since the first OUTPUT event, and no more
825 * OUTPUT events will be generated unless an error occurs.
827 * Connection succesfully established, for client sockets, or incoming
828 * client connection, for server sockets. Wait for this event (also watch
829 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
831 * The connection is lost (or a connection request failed); this could
832 * be due to a failure, or due to the peer closing it gracefully.
835 /* GSocket_SetCallback:
836 * Enables the callbacks specified by 'flags'. Note that 'flags'
837 * may be a combination of flags OR'ed toghether, so the same
838 * callback function can be made to accept different events.
839 * The callback function must have the following prototype:
841 * void function(GSocket *socket, GSocketEvent event, char *cdata)
843 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
844 GSocketCallback callback
, char *cdata
)
848 assert(socket
!= NULL
);
850 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
852 if ((flags
& (1 << count
)) != 0)
854 socket
->m_cbacks
[count
] = callback
;
855 socket
->m_data
[count
] = cdata
;
860 /* GSocket_UnsetCallback:
861 * Disables all callbacks specified by 'flags', which may be a
862 * combination of flags OR'ed toghether.
864 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
868 assert(socket
!= NULL
);
870 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
872 if ((flags
& (1 << count
)) != 0)
874 socket
->m_cbacks
[count
] = NULL
;
875 socket
->m_data
[count
] = NULL
;
881 #define CALL_CALLBACK(socket, event) { \
882 _GSocket_Disable(socket, event); \
883 if (socket->m_cbacks[event]) \
884 socket->m_cbacks[event](socket, event, socket->m_data[event]); \
888 void _GSocket_Enable(GSocket
*socket
, GSocketEvent event
)
890 socket
->m_detected
&= ~(1 << event
);
891 _GSocket_Install_Callback(socket
, event
);
894 void _GSocket_Disable(GSocket
*socket
, GSocketEvent event
)
896 socket
->m_detected
|= (1 << event
);
897 _GSocket_Uninstall_Callback(socket
, event
);
900 /* _GSocket_Input_Timeout:
901 * For blocking sockets, wait until data is available or
902 * until timeout ellapses.
904 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
909 /* Linux select() will overwrite the struct on return */
910 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
911 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
913 if (!socket
->m_non_blocking
)
916 FD_SET(socket
->m_fd
, &readfds
);
917 if (select(socket
->m_fd
+ 1, &readfds
, NULL
, NULL
, &tv
) == 0)
919 socket
->m_error
= GSOCK_TIMEDOUT
;
920 return GSOCK_TIMEDOUT
;
923 return GSOCK_NOERROR
;
926 /* _GSocket_Output_Timeout:
927 * For blocking sockets, wait until data can be sent without
928 * blocking or until timeout ellapses.
930 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
935 /* Linux select() will overwrite the struct on return */
936 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
937 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
939 if (!socket
->m_non_blocking
)
942 FD_SET(socket
->m_fd
, &writefds
);
943 if (select(socket
->m_fd
+ 1, NULL
, &writefds
, NULL
, &tv
) == 0)
945 socket
->m_error
= GSOCK_TIMEDOUT
;
946 return GSOCK_TIMEDOUT
;
949 return GSOCK_NOERROR
;
952 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
954 return recv(socket
->m_fd
, buffer
, size
, 0);
957 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
959 struct sockaddr from
;
960 SOCKLEN_T fromlen
= sizeof(from
);
964 fromlen
= sizeof(from
);
966 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, (SOCKLEN_T
*) &fromlen
);
971 /* Translate a system address into a GSocket address */
974 socket
->m_peer
= GAddress_new();
977 socket
->m_error
= GSOCK_MEMERR
;
981 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
982 if (err
!= GSOCK_NOERROR
)
984 GAddress_destroy(socket
->m_peer
);
985 socket
->m_peer
= NULL
;
986 socket
->m_error
= err
;
993 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
998 ret
= send(socket
->m_fd
, buffer
, size
, 0);
1004 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
1006 struct sockaddr
*addr
;
1010 if (!socket
->m_peer
)
1012 socket
->m_error
= GSOCK_INVADDR
;
1016 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
1017 if (err
!= GSOCK_NOERROR
)
1019 socket
->m_error
= err
;
1024 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
1027 /* Frees memory allocated from _GAddress_translate_to */
1033 void _GSocket_Detected_Read(GSocket
*socket
)
1038 ret
= recv(socket
->m_fd
, &c
, 1, MSG_PEEK
);
1040 if (socket
->m_stream
)
1042 if (ret
< 0 && socket
->m_server
)
1044 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
1051 CALL_CALLBACK(socket
, GSOCK_INPUT
);
1055 CALL_CALLBACK(socket
, GSOCK_LOST
);
1059 void _GSocket_Detected_Write(GSocket
*socket
)
1061 if (socket
->m_establishing
&& !socket
->m_server
)
1064 SOCKLEN_T len
= sizeof(error
);
1066 socket
->m_establishing
= FALSE
;
1068 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*) &error
, &len
);
1072 CALL_CALLBACK(socket
, GSOCK_LOST
);
1076 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
1077 /* We have to fire this event by hand because CONNECTION (for clients)
1078 * and OUTPUT are internally the same and we just disabled CONNECTION
1079 * events with the above macro.
1081 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
1086 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
1091 * -------------------------------------------------------------------------
1093 * -------------------------------------------------------------------------
1096 /* CHECK_ADDRESS verifies that the current family is either GSOCK_NOFAMILY
1097 * or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it initalizes address
1098 * to be a GSOCK_*family*. In other cases, it returns GSOCK_INVADDR.
1100 #define CHECK_ADDRESS(address, family, retval) \
1102 if (address->m_family == GSOCK_NOFAMILY) \
1103 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1104 return address->m_error; \
1105 if (address->m_family != GSOCK_##family) \
1107 address->m_error = GSOCK_INVADDR; \
1112 GAddress
*GAddress_new()
1116 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1119 address
->m_family
= GSOCK_NOFAMILY
;
1120 address
->m_addr
= NULL
;
1126 GAddress
*GAddress_copy(GAddress
*address
)
1130 assert(address
!= NULL
);
1132 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1135 memcpy(addr2
, address
, sizeof(GAddress
));
1137 if (address
->m_addr
)
1139 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
1140 if (addr2
->m_addr
== NULL
)
1145 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1151 void GAddress_destroy(GAddress
*address
)
1153 assert(address
!= NULL
);
1158 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1160 assert(address
!= NULL
);
1162 address
->m_family
= type
;
1165 GAddressType
GAddress_GetFamily(GAddress
*address
)
1167 assert(address
!= NULL
);
1169 return address
->m_family
;
1172 GSocketError
_GAddress_translate_from(GAddress
*address
,
1173 struct sockaddr
*addr
, int len
)
1175 address
->m_realfamily
= addr
->sa_family
;
1176 switch (addr
->sa_family
)
1179 address
->m_family
= GSOCK_INET
;
1182 address
->m_family
= GSOCK_UNIX
;
1186 address
->m_family
= GSOCK_INET6
;
1191 address
->m_error
= GSOCK_INVOP
;
1196 if (address
->m_addr
)
1197 free(address
->m_addr
);
1199 address
->m_len
= len
;
1200 address
->m_addr
= (struct sockaddr
*)malloc(len
);
1202 if (address
->m_addr
== NULL
)
1204 address
->m_error
= GSOCK_MEMERR
;
1205 return GSOCK_MEMERR
;
1207 memcpy(address
->m_addr
, addr
, len
);
1209 return GSOCK_NOERROR
;
1212 GSocketError
_GAddress_translate_to(GAddress
*address
,
1213 struct sockaddr
**addr
, int *len
)
1215 if (!address
->m_addr
)
1217 address
->m_error
= GSOCK_INVADDR
;
1218 return GSOCK_INVADDR
;
1221 *len
= address
->m_len
;
1222 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
1223 if (*addr
== NULL
) {
1224 address
->m_error
= GSOCK_MEMERR
;
1225 return GSOCK_MEMERR
;
1228 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1229 return GSOCK_NOERROR
;
1233 * -------------------------------------------------------------------------
1234 * Internet address family
1235 * -------------------------------------------------------------------------
1238 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1240 address
->m_len
= sizeof(struct sockaddr_in
);
1241 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1242 if (address
->m_addr
== NULL
)
1244 address
->m_error
= GSOCK_MEMERR
;
1245 return GSOCK_MEMERR
;
1248 address
->m_family
= GSOCK_INET
;
1249 address
->m_realfamily
= PF_INET
;
1250 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1251 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1253 return GSOCK_NOERROR
;
1256 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1259 struct in_addr
*addr
;
1261 assert(address
!= NULL
);
1263 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1265 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1267 /* If it is a numeric host name, convert it now */
1268 #if defined(HAVE_INET_ATON)
1269 if (inet_aton(hostname
, addr
) == 0)
1271 #elif defined(HAVE_INET_ADDR)
1272 if ( (addr
->s_addr
= inet_addr(hostname
)) == -1 )
1275 /* Use gethostbyname by default */
1279 struct in_addr
*array_addr
;
1281 /* It is a real name, we solve it */
1282 if ((he
= gethostbyname(hostname
)) == NULL
)
1284 /* Reset to invalid address */
1285 addr
->s_addr
= INADDR_NONE
;
1286 address
->m_error
= GSOCK_NOHOST
;
1287 return GSOCK_NOHOST
;
1289 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1290 addr
->s_addr
= array_addr
[0].s_addr
;
1292 return GSOCK_NOERROR
;
1295 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1297 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1300 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1301 unsigned long hostaddr
)
1303 struct in_addr
*addr
;
1305 assert(address
!= NULL
);
1307 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1309 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1310 addr
->s_addr
= hostaddr
;
1312 return GSOCK_NOERROR
;
1315 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1316 const char *protocol
)
1319 struct sockaddr_in
*addr
;
1321 assert(address
!= NULL
);
1322 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1326 address
->m_error
= GSOCK_INVPORT
;
1327 return GSOCK_INVPORT
;
1330 se
= getservbyname(port
, protocol
);
1333 if (isdigit(port
[0]))
1337 port_int
= atoi(port
);
1338 addr
= (struct sockaddr_in
*)address
->m_addr
;
1339 addr
->sin_port
= htons(port_int
);
1340 return GSOCK_NOERROR
;
1343 address
->m_error
= GSOCK_INVPORT
;
1344 return GSOCK_INVPORT
;
1347 addr
= (struct sockaddr_in
*)address
->m_addr
;
1348 addr
->sin_port
= se
->s_port
;
1350 return GSOCK_NOERROR
;
1353 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1355 struct sockaddr_in
*addr
;
1357 assert(address
!= NULL
);
1358 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1360 addr
= (struct sockaddr_in
*)address
->m_addr
;
1361 addr
->sin_port
= htons(port
);
1363 return GSOCK_NOERROR
;
1366 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1370 struct sockaddr_in
*addr
;
1372 assert(address
!= NULL
);
1373 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1375 addr
= (struct sockaddr_in
*)address
->m_addr
;
1376 addr_buf
= (char *)&(addr
->sin_addr
);
1378 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1381 address
->m_error
= GSOCK_NOHOST
;
1382 return GSOCK_NOHOST
;
1385 strncpy(hostname
, he
->h_name
, sbuf
);
1387 return GSOCK_NOERROR
;
1390 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1392 struct sockaddr_in
*addr
;
1394 assert(address
!= NULL
);
1395 CHECK_ADDRESS(address
, INET
, 0);
1397 addr
= (struct sockaddr_in
*)address
->m_addr
;
1399 return addr
->sin_addr
.s_addr
;
1402 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1404 struct sockaddr_in
*addr
;
1406 assert(address
!= NULL
);
1407 CHECK_ADDRESS(address
, INET
, 0);
1409 addr
= (struct sockaddr_in
*)address
->m_addr
;
1410 return ntohs(addr
->sin_port
);
1414 * -------------------------------------------------------------------------
1415 * Unix address family
1416 * -------------------------------------------------------------------------
1419 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1421 address
->m_len
= sizeof(struct sockaddr_un
);
1422 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
1423 if (address
->m_addr
== NULL
)
1425 address
->m_error
= GSOCK_MEMERR
;
1426 return GSOCK_MEMERR
;
1429 address
->m_family
= GSOCK_UNIX
;
1430 address
->m_realfamily
= PF_UNIX
;
1431 ((struct sockaddr_un
*)address
->m_addr
)->sun_family
= AF_UNIX
;
1432 ((struct sockaddr_un
*)address
->m_addr
)->sun_path
[0] = 0;
1437 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1439 struct sockaddr_un
*addr
;
1441 assert(address
!= NULL
);
1443 CHECK_ADDRESS(address
, UNIX
, GSOCK_INVADDR
);
1445 addr
= ((struct sockaddr_un
*)address
->m_addr
);
1446 memcpy(addr
->sun_path
, path
, strlen(path
));
1448 return GSOCK_NOERROR
;
1451 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1453 struct sockaddr_un
*addr
;
1455 assert(address
!= NULL
);
1456 CHECK_ADDRESS(address
, UNIX
, GSOCK_INVADDR
);
1458 addr
= (struct sockaddr_un
*)address
->m_addr
;
1460 strncpy(path
, addr
->sun_path
, sbuf
);
1462 return GSOCK_NOERROR
;
1465 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */