]>
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 */
116 bool GSocket_Init(void)
121 void GSocket_Cleanup(void)
125 /* Constructors / Destructors for GSocket */
127 GSocket
*GSocket_new(void)
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 /* Per-socket GUI-specific initialization */
155 if (!_GSocket_GUI_Init(socket
))
164 void GSocket_destroy(GSocket
*socket
)
166 assert(socket
!= NULL
);
168 /* Per-socket GUI-specific cleanup */
169 _GSocket_GUI_Destroy(socket
);
171 /* Check that the socket is really shutdowned */
172 if (socket
->m_fd
!= -1)
173 GSocket_Shutdown(socket
);
175 /* Destroy private addresses */
177 GAddress_destroy(socket
->m_local
);
180 GAddress_destroy(socket
->m_peer
);
182 /* Destroy the socket itself */
187 * Disallow further read/write operations on this socket, close
188 * the fd and disable all callbacks.
190 void GSocket_Shutdown(GSocket
*socket
)
194 assert(socket
!= NULL
);
196 /* If socket has been created, shutdown it */
197 if (socket
->m_fd
!= -1)
199 shutdown(socket
->m_fd
, 2);
204 /* Disable GUI callbacks */
205 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
206 socket
->m_cbacks
[evt
] = NULL
;
208 socket
->m_detected
= GSOCK_LOST_FLAG
;
209 _GSocket_Disable_Events(socket
);
212 /* Address handling */
218 * Set or get the local or peer address for this socket. The 'set'
219 * functions return GSOCK_NOERROR on success, an error code otherwise.
220 * The 'get' functions return a pointer to a GAddress object on success,
221 * or NULL otherwise, in which case they set the error code of the
222 * corresponding GSocket.
225 * GSOCK_INVSOCK - the socket is not valid.
226 * GSOCK_INVADDR - the address is not valid.
228 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
230 assert(socket
!= NULL
);
232 /* the socket must be initialized, or it must be a server */
233 if ((socket
->m_fd
!= -1 && !socket
->m_server
))
235 socket
->m_error
= GSOCK_INVSOCK
;
236 return GSOCK_INVSOCK
;
240 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
242 socket
->m_error
= GSOCK_INVADDR
;
243 return GSOCK_INVADDR
;
247 GAddress_destroy(socket
->m_local
);
249 socket
->m_local
= GAddress_copy(address
);
251 return GSOCK_NOERROR
;
254 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
256 assert(socket
!= NULL
);
259 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
261 socket
->m_error
= GSOCK_INVADDR
;
262 return GSOCK_INVADDR
;
266 GAddress_destroy(socket
->m_peer
);
268 socket
->m_peer
= GAddress_copy(address
);
270 return GSOCK_NOERROR
;
273 GAddress
*GSocket_GetLocal(GSocket
*socket
)
276 struct sockaddr addr
;
277 SOCKLEN_T size
= sizeof(addr
);
280 assert(socket
!= NULL
);
282 /* try to get it from the m_local var first */
284 return GAddress_copy(socket
->m_local
);
286 /* else, if the socket is initialized, try getsockname */
287 if (socket
->m_fd
== -1)
289 socket
->m_error
= GSOCK_INVSOCK
;
293 if (getsockname(socket
->m_fd
, &addr
, (SOCKLEN_T
*) &size
) < 0)
295 socket
->m_error
= GSOCK_IOERR
;
299 /* got a valid address from getsockname, create a GAddress object */
300 address
= GAddress_new();
303 socket
->m_error
= GSOCK_MEMERR
;
307 err
= _GAddress_translate_from(address
, &addr
, size
);
308 if (err
!= GSOCK_NOERROR
)
310 GAddress_destroy(address
);
311 socket
->m_error
= err
;
318 GAddress
*GSocket_GetPeer(GSocket
*socket
)
320 assert(socket
!= NULL
);
322 /* try to get it from the m_peer var */
324 return GAddress_copy(socket
->m_peer
);
329 /* Server specific parts */
331 /* GSocket_SetServer:
332 * Sets up this socket as a server. The local address must have been
333 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
334 * Returns GSOCK_NOERROR on success, one of the following otherwise:
337 * GSOCK_INVSOCK - the socket is in use.
338 * GSOCK_INVADDR - the local address has not been set.
339 * GSOCK_IOERR - low-level error.
341 GSocketError
GSocket_SetServer(GSocket
*sck
)
348 /* must not be in use */
351 sck
->m_error
= GSOCK_INVSOCK
;
352 return GSOCK_INVSOCK
;
355 /* the local addr must have been set */
358 sck
->m_error
= GSOCK_INVADDR
;
359 return GSOCK_INVADDR
;
362 /* Initialize all fields */
363 sck
->m_stream
= TRUE
;
364 sck
->m_server
= TRUE
;
365 sck
->m_oriented
= TRUE
;
367 /* Create the socket */
368 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
372 sck
->m_error
= GSOCK_IOERR
;
376 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
377 _GSocket_Enable_Events(sck
);
379 /* Bind to the local address,
380 * retrieve the actual address bound,
381 * and listen up to 5 connections.
383 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
384 (getsockname(sck
->m_fd
,
385 sck
->m_local
->m_addr
,
386 (SOCKLEN_T
*) &sck
->m_local
->m_len
) != 0) ||
387 (listen(sck
->m_fd
, 5) != 0))
391 sck
->m_error
= GSOCK_IOERR
;
395 return GSOCK_NOERROR
;
398 /* GSocket_WaitConnection:
399 * Waits for an incoming client connection. Returns a pointer to
400 * a GSocket object, or NULL if there was an error, in which case
401 * the last error field will be updated for the calling GSocket.
403 * Error codes (set in the calling GSocket)
404 * GSOCK_INVSOCK - the socket is not valid or not a server.
405 * GSOCK_TIMEDOUT - timeout, no incoming connections.
406 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
407 * GSOCK_MEMERR - couldn't allocate memory.
408 * GSOCK_IOERR - low-level error.
410 GSocket
*GSocket_WaitConnection(GSocket
*socket
)
412 struct sockaddr from
;
413 SOCKLEN_T fromlen
= sizeof(from
);
418 assert(socket
!= NULL
);
420 /* Reenable CONNECTION events */
421 _GSocket_Enable(socket
, GSOCK_CONNECTION
);
423 /* If the socket has already been created, we exit immediately */
424 if (socket
->m_fd
== -1 || !socket
->m_server
)
426 socket
->m_error
= GSOCK_INVSOCK
;
430 /* Create a GSocket object for the new connection */
431 connection
= GSocket_new();
435 socket
->m_error
= GSOCK_MEMERR
;
439 /* Wait for a connection (with timeout) */
440 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
442 GSocket_destroy(connection
);
443 /* socket->m_error set by _GSocket_Input_Timeout */
447 connection
->m_fd
= accept(socket
->m_fd
, &from
, (SOCKLEN_T
*) &fromlen
);
449 if (connection
->m_fd
== -1)
451 if (errno
== EWOULDBLOCK
)
452 socket
->m_error
= GSOCK_WOULDBLOCK
;
454 socket
->m_error
= GSOCK_IOERR
;
456 GSocket_destroy(connection
);
460 /* Initialize all fields */
461 connection
->m_server
= FALSE
;
462 connection
->m_stream
= TRUE
;
463 connection
->m_oriented
= TRUE
;
465 /* Setup the peer address field */
466 connection
->m_peer
= GAddress_new();
467 if (!connection
->m_peer
)
469 GSocket_destroy(connection
);
470 socket
->m_error
= GSOCK_MEMERR
;
473 err
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
);
474 if (err
!= GSOCK_NOERROR
)
476 GAddress_destroy(connection
->m_peer
);
477 GSocket_destroy(connection
);
478 socket
->m_error
= err
;
482 ioctl(connection
->m_fd
, FIONBIO
, &arg
);
483 _GSocket_Enable_Events(connection
);
488 /* Datagram sockets */
490 /* GSocket_SetNonOriented:
491 * Sets up this socket as a non-connection oriented (datagram) socket.
492 * Before using this function, the local address must have been set
493 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
494 * on success, or one of the following otherwise.
497 * GSOCK_INVSOCK - the socket is in use.
498 * GSOCK_INVADDR - the local address has not been set.
499 * GSOCK_IOERR - low-level error.
501 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
509 sck
->m_error
= GSOCK_INVSOCK
;
510 return GSOCK_INVSOCK
;
515 sck
->m_error
= GSOCK_INVADDR
;
516 return GSOCK_INVADDR
;
519 /* Initialize all fields */
520 sck
->m_stream
= FALSE
;
521 sck
->m_server
= FALSE
;
522 sck
->m_oriented
= FALSE
;
524 /* Create the socket */
525 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
529 sck
->m_error
= GSOCK_IOERR
;
533 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
534 _GSocket_Enable_Events(sck
);
536 /* Bind to the local address,
537 * and retrieve the actual address bound.
539 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
540 (getsockname(sck
->m_fd
,
541 sck
->m_local
->m_addr
,
542 (SOCKLEN_T
*) &sck
->m_local
->m_len
) != 0))
546 sck
->m_error
= GSOCK_IOERR
;
550 return GSOCK_NOERROR
;
553 /* Client specific parts */
556 * For stream (connection oriented) sockets, GSocket_Connect() tries
557 * to establish a client connection to a server using the peer address
558 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
559 * connection has been succesfully established, or one of the error
560 * codes listed below. Note that for nonblocking sockets, a return
561 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
562 * request can be completed later; you should use GSocket_Select()
563 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
564 * corresponding asynchronous events.
566 * For datagram (non connection oriented) sockets, GSocket_Connect()
567 * just sets the peer address established with GSocket_SetPeer() as
568 * default destination.
571 * GSOCK_INVSOCK - the socket is in use or not valid.
572 * GSOCK_INVADDR - the peer address has not been established.
573 * GSOCK_TIMEDOUT - timeout, the connection failed.
574 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
575 * GSOCK_MEMERR - couldn't allocate memory.
576 * GSOCK_IOERR - low-level error.
578 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
585 /* Enable CONNECTION events (needed for nonblocking connections) */
586 _GSocket_Enable(sck
, GSOCK_CONNECTION
);
590 sck
->m_error
= GSOCK_INVSOCK
;
591 return GSOCK_INVSOCK
;
596 sck
->m_error
= GSOCK_INVADDR
;
597 return GSOCK_INVADDR
;
600 /* Streamed or dgram socket? */
601 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
602 sck
->m_oriented
= TRUE
;
603 sck
->m_server
= FALSE
;
604 sck
->m_establishing
= FALSE
;
606 /* Create the socket */
607 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
,
608 sck
->m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
612 sck
->m_error
= GSOCK_IOERR
;
616 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
617 _GSocket_Enable_Events(sck
);
619 /* Connect it to the peer address, with a timeout (see below) */
620 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
626 /* If connect failed with EINPROGRESS and the GSocket object
627 * is in blocking mode, we select() for the specified timeout
628 * checking for writability to see if the connection request
631 if ((err
== EINPROGRESS
) && (!sck
->m_non_blocking
))
633 if (_GSocket_Output_Timeout(sck
) == GSOCK_TIMEDOUT
)
637 /* sck->m_error is set in _GSocket_Output_Timeout */
638 return GSOCK_TIMEDOUT
;
643 SOCKLEN_T len
= sizeof(error
);
645 getsockopt(sck
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*) &error
, &len
);
648 return GSOCK_NOERROR
;
652 /* If connect failed with EINPROGRESS and the GSocket object
653 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
654 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
655 * this way if the connection completes, a GSOCK_CONNECTION
656 * event will be generated, if enabled.
658 if ((err
== EINPROGRESS
) && (sck
->m_non_blocking
))
660 sck
->m_establishing
= TRUE
;
661 sck
->m_error
= GSOCK_WOULDBLOCK
;
662 return GSOCK_WOULDBLOCK
;
665 /* If connect failed with an error other than EINPROGRESS,
666 * then the call to GSocket_Connect has failed.
670 sck
->m_error
= GSOCK_IOERR
;
674 return GSOCK_NOERROR
;
679 /* Like recv(), send(), ... */
680 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
684 assert(socket
!= NULL
);
686 /* Reenable INPUT events */
687 _GSocket_Enable(socket
, GSOCK_INPUT
);
689 if (socket
->m_fd
== -1 || socket
->m_server
)
691 socket
->m_error
= GSOCK_INVSOCK
;
695 /* If the socket is blocking, wait for data (with a timeout) */
696 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
700 if (socket
->m_stream
)
701 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
703 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
707 if (errno
== EWOULDBLOCK
)
708 socket
->m_error
= GSOCK_WOULDBLOCK
;
710 socket
->m_error
= GSOCK_IOERR
;
716 int GSocket_Write(GSocket
*socket
, const char *buffer
, int size
)
720 assert(socket
!= NULL
);
722 printf( "GSocket_Write #1, size %d\n", size
);
724 if (socket
->m_fd
== -1 || socket
->m_server
)
726 socket
->m_error
= GSOCK_INVSOCK
;
730 printf( "GSocket_Write #2, size %d\n", size
);
732 /* If the socket is blocking, wait for writability (with a timeout) */
733 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
736 printf( "GSocket_Write #3, size %d\n", size
);
739 if (socket
->m_stream
)
740 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
742 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
744 printf( "GSocket_Write #4, size %d\n", size
);
748 if (errno
== EWOULDBLOCK
)
750 socket
->m_error
= GSOCK_WOULDBLOCK
;
751 printf( "GSocket_Write error WOULDBLOCK\n" );
755 socket
->m_error
= GSOCK_IOERR
;
756 printf( "GSocket_Write error IOERR\n" );
759 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
760 * in MSW). Once the first OUTPUT event is received, users can assume
761 * that the socket is writable until a read operation fails. Only then
762 * will further OUTPUT events be posted.
764 _GSocket_Enable(socket
, GSOCK_OUTPUT
);
768 printf( "GSocket_Write #5, size %d ret %d\n", size
, ret
);
774 * Polls the socket to determine its status. This function will
775 * check for the events specified in the 'flags' parameter, and
776 * it will return a mask indicating which operations can be
777 * performed. This function won't block, regardless of the
778 * mode (blocking | nonblocking) of the socket.
780 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
782 assert(socket
!= NULL
);
784 return flags
& socket
->m_detected
;
789 /* GSocket_SetNonBlocking:
790 * Sets the socket to non-blocking mode. All IO calls will return
793 void GSocket_SetNonBlocking(GSocket
*socket
, bool non_block
)
795 assert(socket
!= NULL
);
797 socket
->m_non_blocking
= non_block
;
800 /* GSocket_SetTimeout:
801 * Sets the timeout for blocking calls. Time is expressed in
804 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
)
806 assert(socket
!= NULL
);
808 socket
->m_timeout
= millisec
;
812 * Returns the last error occured for this socket. Note that successful
813 * operations do not clear this back to GSOCK_NOERROR, so use it only
816 GSocketError
GSocket_GetError(GSocket
*socket
)
818 assert(socket
!= NULL
);
820 return socket
->m_error
;
826 * There is data to be read in the input buffer. If, after a read
827 * operation, there is still data available, the callback function will
830 * The socket is available for writing. That is, the next write call
831 * won't block. This event is generated only once, when the connection is
832 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
833 * when the output buffer empties again. This means that the app should
834 * assume that it can write since the first OUTPUT event, and no more
835 * OUTPUT events will be generated unless an error occurs.
837 * Connection succesfully established, for client sockets, or incoming
838 * client connection, for server sockets. Wait for this event (also watch
839 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
841 * The connection is lost (or a connection request failed); this could
842 * be due to a failure, or due to the peer closing it gracefully.
845 /* GSocket_SetCallback:
846 * Enables the callbacks specified by 'flags'. Note that 'flags'
847 * may be a combination of flags OR'ed toghether, so the same
848 * callback function can be made to accept different events.
849 * The callback function must have the following prototype:
851 * void function(GSocket *socket, GSocketEvent event, char *cdata)
853 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
854 GSocketCallback callback
, char *cdata
)
858 assert(socket
!= NULL
);
860 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
862 if ((flags
& (1 << count
)) != 0)
864 socket
->m_cbacks
[count
] = callback
;
865 socket
->m_data
[count
] = cdata
;
870 /* GSocket_UnsetCallback:
871 * Disables all callbacks specified by 'flags', which may be a
872 * combination of flags OR'ed toghether.
874 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
878 assert(socket
!= NULL
);
880 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
882 if ((flags
& (1 << count
)) != 0)
884 socket
->m_cbacks
[count
] = NULL
;
885 socket
->m_data
[count
] = NULL
;
891 #define CALL_CALLBACK(socket, event) { \
892 _GSocket_Disable(socket, event); \
893 if (socket->m_cbacks[event]) \
894 socket->m_cbacks[event](socket, event, socket->m_data[event]); \
898 void _GSocket_Enable(GSocket
*socket
, GSocketEvent event
)
900 socket
->m_detected
&= ~(1 << event
);
901 _GSocket_Install_Callback(socket
, event
);
904 void _GSocket_Disable(GSocket
*socket
, GSocketEvent event
)
906 socket
->m_detected
|= (1 << event
);
907 _GSocket_Uninstall_Callback(socket
, event
);
910 /* _GSocket_Input_Timeout:
911 * For blocking sockets, wait until data is available or
912 * until timeout ellapses.
914 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
920 /* Linux select() will overwrite the struct on return */
921 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
922 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
924 if (!socket
->m_non_blocking
)
927 FD_SET(socket
->m_fd
, &readfds
);
928 ret
= select(socket
->m_fd
+ 1, &readfds
, NULL
, NULL
, &tv
);
931 printf( "GSocket_Input_Timeout, select returned 0\n" );
932 socket
->m_error
= GSOCK_TIMEDOUT
;
933 return GSOCK_TIMEDOUT
;
937 printf( "GSocket_Input_Timeout, select returned -1\n" );
938 if (errno
== EBADF
) printf( "Invalid file descriptor\n" );
939 if (errno
== EINTR
) printf( "A non blocked signal was caught\n" );
940 if (errno
== EINVAL
) printf( "The highest number descriptor is negative\n" );
941 if (errno
== ENOMEM
) printf( "Not enough memory\n" );
942 socket
->m_error
= GSOCK_TIMEDOUT
;
943 return GSOCK_TIMEDOUT
;
946 return GSOCK_NOERROR
;
949 /* _GSocket_Output_Timeout:
950 * For blocking sockets, wait until data can be sent without
951 * blocking or until timeout ellapses.
953 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
959 /* Linux select() will overwrite the struct on return */
960 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
961 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
963 if (!socket
->m_non_blocking
)
966 FD_SET(socket
->m_fd
, &writefds
);
967 ret
= select(socket
->m_fd
+ 1, NULL
, &writefds
, NULL
, &tv
);
970 printf( "GSocket_Output_Timeout, select returned 0\n" );
971 socket
->m_error
= GSOCK_TIMEDOUT
;
972 return GSOCK_TIMEDOUT
;
976 printf( "GSocket_Output_Timeout, select returned -1\n" );
977 if (errno
== EBADF
) printf( "Invalid file descriptor\n" );
978 if (errno
== EINTR
) printf( "A non blocked signal was caught\n" );
979 if (errno
== EINVAL
) printf( "The highest number descriptor is negative\n" );
980 if (errno
== ENOMEM
) printf( "Not enough memory\n" );
981 socket
->m_error
= GSOCK_TIMEDOUT
;
982 return GSOCK_TIMEDOUT
;
985 return GSOCK_NOERROR
;
988 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
990 return recv(socket
->m_fd
, buffer
, size
, 0);
993 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
995 struct sockaddr from
;
996 SOCKLEN_T fromlen
= sizeof(from
);
1000 fromlen
= sizeof(from
);
1002 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, (SOCKLEN_T
*) &fromlen
);
1007 /* Translate a system address into a GSocket address */
1008 if (!socket
->m_peer
)
1010 socket
->m_peer
= GAddress_new();
1011 if (!socket
->m_peer
)
1013 socket
->m_error
= GSOCK_MEMERR
;
1017 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
1018 if (err
!= GSOCK_NOERROR
)
1020 GAddress_destroy(socket
->m_peer
);
1021 socket
->m_peer
= NULL
;
1022 socket
->m_error
= err
;
1029 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
1034 ret
= send(socket
->m_fd
, buffer
, size
, 0);
1040 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
1042 struct sockaddr
*addr
;
1046 if (!socket
->m_peer
)
1048 socket
->m_error
= GSOCK_INVADDR
;
1052 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
1053 if (err
!= GSOCK_NOERROR
)
1055 socket
->m_error
= err
;
1060 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
1063 /* Frees memory allocated from _GAddress_translate_to */
1069 void _GSocket_Detected_Read(GSocket
*socket
)
1074 ret
= recv(socket
->m_fd
, &c
, 1, MSG_PEEK
);
1076 if (socket
->m_stream
)
1078 if (ret
< 0 && socket
->m_server
)
1080 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
1087 CALL_CALLBACK(socket
, GSOCK_INPUT
);
1091 CALL_CALLBACK(socket
, GSOCK_LOST
);
1095 void _GSocket_Detected_Write(GSocket
*socket
)
1097 if (socket
->m_establishing
&& !socket
->m_server
)
1100 SOCKLEN_T len
= sizeof(error
);
1102 socket
->m_establishing
= FALSE
;
1104 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*) &error
, &len
);
1108 CALL_CALLBACK(socket
, GSOCK_LOST
);
1112 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
1113 /* We have to fire this event by hand because CONNECTION (for clients)
1114 * and OUTPUT are internally the same and we just disabled CONNECTION
1115 * events with the above macro.
1117 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
1122 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
1127 * -------------------------------------------------------------------------
1129 * -------------------------------------------------------------------------
1132 /* CHECK_ADDRESS verifies that the current family is either GSOCK_NOFAMILY
1133 * or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it initalizes address
1134 * to be a GSOCK_*family*. In other cases, it returns GSOCK_INVADDR.
1136 #define CHECK_ADDRESS(address, family, retval) \
1138 if (address->m_family == GSOCK_NOFAMILY) \
1139 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1140 return address->m_error; \
1141 if (address->m_family != GSOCK_##family) \
1143 address->m_error = GSOCK_INVADDR; \
1148 GAddress
*GAddress_new(void)
1152 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1155 address
->m_family
= GSOCK_NOFAMILY
;
1156 address
->m_addr
= NULL
;
1162 GAddress
*GAddress_copy(GAddress
*address
)
1166 assert(address
!= NULL
);
1168 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1171 memcpy(addr2
, address
, sizeof(GAddress
));
1173 if (address
->m_addr
)
1175 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
1176 if (addr2
->m_addr
== NULL
)
1181 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1187 void GAddress_destroy(GAddress
*address
)
1189 assert(address
!= NULL
);
1194 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1196 assert(address
!= NULL
);
1198 address
->m_family
= type
;
1201 GAddressType
GAddress_GetFamily(GAddress
*address
)
1203 assert(address
!= NULL
);
1205 return address
->m_family
;
1208 GSocketError
_GAddress_translate_from(GAddress
*address
,
1209 struct sockaddr
*addr
, int len
)
1211 address
->m_realfamily
= addr
->sa_family
;
1212 switch (addr
->sa_family
)
1215 address
->m_family
= GSOCK_INET
;
1218 address
->m_family
= GSOCK_UNIX
;
1222 address
->m_family
= GSOCK_INET6
;
1227 address
->m_error
= GSOCK_INVOP
;
1232 if (address
->m_addr
)
1233 free(address
->m_addr
);
1235 address
->m_len
= len
;
1236 address
->m_addr
= (struct sockaddr
*)malloc(len
);
1238 if (address
->m_addr
== NULL
)
1240 address
->m_error
= GSOCK_MEMERR
;
1241 return GSOCK_MEMERR
;
1243 memcpy(address
->m_addr
, addr
, len
);
1245 return GSOCK_NOERROR
;
1248 GSocketError
_GAddress_translate_to(GAddress
*address
,
1249 struct sockaddr
**addr
, int *len
)
1251 if (!address
->m_addr
)
1253 address
->m_error
= GSOCK_INVADDR
;
1254 return GSOCK_INVADDR
;
1257 *len
= address
->m_len
;
1258 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
1259 if (*addr
== NULL
) {
1260 address
->m_error
= GSOCK_MEMERR
;
1261 return GSOCK_MEMERR
;
1264 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1265 return GSOCK_NOERROR
;
1269 * -------------------------------------------------------------------------
1270 * Internet address family
1271 * -------------------------------------------------------------------------
1274 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1276 address
->m_len
= sizeof(struct sockaddr_in
);
1277 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1278 if (address
->m_addr
== NULL
)
1280 address
->m_error
= GSOCK_MEMERR
;
1281 return GSOCK_MEMERR
;
1284 address
->m_family
= GSOCK_INET
;
1285 address
->m_realfamily
= PF_INET
;
1286 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1287 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1289 return GSOCK_NOERROR
;
1292 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1295 struct in_addr
*addr
;
1297 assert(address
!= NULL
);
1299 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1301 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1303 /* If it is a numeric host name, convert it now */
1304 #if defined(HAVE_INET_ATON)
1305 if (inet_aton(hostname
, addr
) == 0)
1307 #elif defined(HAVE_INET_ADDR)
1308 if ( (addr
->s_addr
= inet_addr(hostname
)) == -1 )
1311 /* Use gethostbyname by default */
1315 struct in_addr
*array_addr
;
1317 /* It is a real name, we solve it */
1318 if ((he
= gethostbyname(hostname
)) == NULL
)
1320 /* Reset to invalid address */
1321 addr
->s_addr
= INADDR_NONE
;
1322 address
->m_error
= GSOCK_NOHOST
;
1323 return GSOCK_NOHOST
;
1325 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1326 addr
->s_addr
= array_addr
[0].s_addr
;
1328 return GSOCK_NOERROR
;
1331 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1333 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1336 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1337 unsigned long hostaddr
)
1339 struct in_addr
*addr
;
1341 assert(address
!= NULL
);
1343 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1345 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1346 addr
->s_addr
= hostaddr
;
1348 return GSOCK_NOERROR
;
1351 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1352 const char *protocol
)
1355 struct sockaddr_in
*addr
;
1357 assert(address
!= NULL
);
1358 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1362 address
->m_error
= GSOCK_INVPORT
;
1363 return GSOCK_INVPORT
;
1366 se
= getservbyname(port
, protocol
);
1369 if (isdigit(port
[0]))
1373 port_int
= atoi(port
);
1374 addr
= (struct sockaddr_in
*)address
->m_addr
;
1375 addr
->sin_port
= htons(port_int
);
1376 return GSOCK_NOERROR
;
1379 address
->m_error
= GSOCK_INVPORT
;
1380 return GSOCK_INVPORT
;
1383 addr
= (struct sockaddr_in
*)address
->m_addr
;
1384 addr
->sin_port
= se
->s_port
;
1386 return GSOCK_NOERROR
;
1389 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1391 struct sockaddr_in
*addr
;
1393 assert(address
!= NULL
);
1394 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1396 addr
= (struct sockaddr_in
*)address
->m_addr
;
1397 addr
->sin_port
= htons(port
);
1399 return GSOCK_NOERROR
;
1402 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1406 struct sockaddr_in
*addr
;
1408 assert(address
!= NULL
);
1409 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1411 addr
= (struct sockaddr_in
*)address
->m_addr
;
1412 addr_buf
= (char *)&(addr
->sin_addr
);
1414 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1417 address
->m_error
= GSOCK_NOHOST
;
1418 return GSOCK_NOHOST
;
1421 strncpy(hostname
, he
->h_name
, sbuf
);
1423 return GSOCK_NOERROR
;
1426 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1428 struct sockaddr_in
*addr
;
1430 assert(address
!= NULL
);
1431 CHECK_ADDRESS(address
, INET
, 0);
1433 addr
= (struct sockaddr_in
*)address
->m_addr
;
1435 return addr
->sin_addr
.s_addr
;
1438 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1440 struct sockaddr_in
*addr
;
1442 assert(address
!= NULL
);
1443 CHECK_ADDRESS(address
, INET
, 0);
1445 addr
= (struct sockaddr_in
*)address
->m_addr
;
1446 return ntohs(addr
->sin_port
);
1450 * -------------------------------------------------------------------------
1451 * Unix address family
1452 * -------------------------------------------------------------------------
1455 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1457 address
->m_len
= sizeof(struct sockaddr_un
);
1458 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
1459 if (address
->m_addr
== NULL
)
1461 address
->m_error
= GSOCK_MEMERR
;
1462 return GSOCK_MEMERR
;
1465 address
->m_family
= GSOCK_UNIX
;
1466 address
->m_realfamily
= PF_UNIX
;
1467 ((struct sockaddr_un
*)address
->m_addr
)->sun_family
= AF_UNIX
;
1468 ((struct sockaddr_un
*)address
->m_addr
)->sun_path
[0] = 0;
1473 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1475 struct sockaddr_un
*addr
;
1477 assert(address
!= NULL
);
1479 CHECK_ADDRESS(address
, UNIX
, GSOCK_INVADDR
);
1481 addr
= ((struct sockaddr_un
*)address
->m_addr
);
1482 memcpy(addr
->sun_path
, path
, strlen(path
));
1484 return GSOCK_NOERROR
;
1487 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1489 struct sockaddr_un
*addr
;
1491 assert(address
!= NULL
);
1492 CHECK_ADDRESS(address
, UNIX
, GSOCK_INVADDR
);
1494 addr
= (struct sockaddr_un
*)address
->m_addr
;
1496 strncpy(path
, addr
->sun_path
, sbuf
);
1498 return GSOCK_NOERROR
;
1501 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */