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>
30 u_char sun_len
; /* sockaddr len including null */
31 u_char sun_family
; /* AF_UNIX */
32 char sun_path
[108]; /* path name (gag) */
35 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <arpa/inet.h>
50 # include <sys/filio.h>
60 # define SOCKLEN_T unsigned int
64 # define SOCKLEN_T socklen_t
67 # define SOCKLEN_T int
71 #endif /* SOCKLEN_T */
74 * MSW defines this, Unices don't.
76 #ifndef INVALID_SOCKET
77 #define INVALID_SOCKET -1
81 * INADDR_BROADCAST is identical to INADDR_NONE which is not defined
82 * on all systems. INADDR_BROADCAST should be fine to indicate an error.
85 #define INADDR_NONE INADDR_BROADCAST
88 #define MASK_SIGNAL() \
90 void (*old_handler)(int); \
92 old_handler = signal(SIGPIPE, SIG_IGN);
94 #define UNMASK_SIGNAL() \
95 signal(SIGPIPE, old_handler); \
99 #ifndef __GSOCKET_STANDALONE__
100 # include "wx/unix/gsockunx.h"
101 # include "wx/gsocket.h"
103 # include "gsockunx.h"
104 # include "gsocket.h"
105 #endif /* __GSOCKET_STANDALONE__ */
107 /* redefine some GUI-only functions to do nothing in console mode */
108 #if defined(wxUSE_GUI) && !wxUSE_GUI
109 # define _GSocket_GUI_Init(socket) (1)
110 # define _GSocket_GUI_Destroy(socket)
111 # define _GSocket_Enable_Events(socket)
112 # define _GSocket_Disable_Events(socket)
113 # define _GSocket_Install_Callback(socket, event)
114 # define _GSocket_Uninstall_Callback(socket, event)
115 #endif /* wxUSE_GUI */
117 /* debugging helpers */
118 #ifdef __GSOCKET_DEBUG__
119 # define GSocket_Debug(args) printf args
121 # define GSocket_Debug(args)
122 #endif /* __GSOCKET_DEBUG__ */
124 /* Global initialisers */
126 int GSocket_Init(void)
131 void GSocket_Cleanup(void)
135 /* Constructors / Destructors for GSocket */
137 GSocket
*GSocket_new(void)
142 socket
= (GSocket
*)malloc(sizeof(GSocket
));
147 socket
->m_fd
= INVALID_SOCKET
;
148 for (i
=0;i
<GSOCK_MAX_EVENT
;i
++)
150 socket
->m_cbacks
[i
] = NULL
;
152 socket
->m_detected
= 0;
153 socket
->m_local
= NULL
;
154 socket
->m_peer
= NULL
;
155 socket
->m_error
= GSOCK_NOERROR
;
156 socket
->m_server
= FALSE
;
157 socket
->m_stream
= TRUE
;
158 socket
->m_gui_dependent
= NULL
;
159 socket
->m_non_blocking
= FALSE
;
160 socket
->m_timeout
= 10*60*1000;
161 /* 10 minutes * 60 sec * 1000 millisec */
162 socket
->m_establishing
= FALSE
;
164 /* Per-socket GUI-specific initialization */
165 success
= _GSocket_GUI_Init(socket
);
175 void GSocket_destroy(GSocket
*socket
)
177 assert(socket
!= NULL
);
179 /* Check that the socket is really shutdowned */
180 if (socket
->m_fd
!= INVALID_SOCKET
)
181 GSocket_Shutdown(socket
);
183 /* Per-socket GUI-specific cleanup */
184 _GSocket_GUI_Destroy(socket
);
186 /* Destroy private addresses */
188 GAddress_destroy(socket
->m_local
);
191 GAddress_destroy(socket
->m_peer
);
193 /* Destroy the socket itself */
198 * Disallow further read/write operations on this socket, close
199 * the fd and disable all callbacks.
201 void GSocket_Shutdown(GSocket
*socket
)
205 assert(socket
!= NULL
);
207 /* If socket has been created, shutdown it */
208 if (socket
->m_fd
!= INVALID_SOCKET
)
210 shutdown(socket
->m_fd
, 2);
212 socket
->m_fd
= INVALID_SOCKET
;
215 /* Disable GUI callbacks */
216 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
217 socket
->m_cbacks
[evt
] = NULL
;
219 socket
->m_detected
= GSOCK_LOST_FLAG
;
220 _GSocket_Disable_Events(socket
);
223 /* Address handling */
229 * Set or get the local or peer address for this socket. The 'set'
230 * functions return GSOCK_NOERROR on success, an error code otherwise.
231 * The 'get' functions return a pointer to a GAddress object on success,
232 * or NULL otherwise, in which case they set the error code of the
233 * corresponding GSocket.
236 * GSOCK_INVSOCK - the socket is not valid.
237 * GSOCK_INVADDR - the address is not valid.
239 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
241 assert(socket
!= NULL
);
243 /* the socket must be initialized, or it must be a server */
244 if ((socket
->m_fd
!= INVALID_SOCKET
&& !socket
->m_server
))
246 socket
->m_error
= GSOCK_INVSOCK
;
247 return GSOCK_INVSOCK
;
251 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
253 socket
->m_error
= GSOCK_INVADDR
;
254 return GSOCK_INVADDR
;
258 GAddress_destroy(socket
->m_local
);
260 socket
->m_local
= GAddress_copy(address
);
262 return GSOCK_NOERROR
;
265 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
267 assert(socket
!= NULL
);
270 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
272 socket
->m_error
= GSOCK_INVADDR
;
273 return GSOCK_INVADDR
;
277 GAddress_destroy(socket
->m_peer
);
279 socket
->m_peer
= GAddress_copy(address
);
281 return GSOCK_NOERROR
;
284 GAddress
*GSocket_GetLocal(GSocket
*socket
)
287 struct sockaddr addr
;
288 SOCKLEN_T size
= sizeof(addr
);
291 assert(socket
!= NULL
);
293 /* try to get it from the m_local var first */
295 return GAddress_copy(socket
->m_local
);
297 /* else, if the socket is initialized, try getsockname */
298 if (socket
->m_fd
== INVALID_SOCKET
)
300 socket
->m_error
= GSOCK_INVSOCK
;
304 if (getsockname(socket
->m_fd
, &addr
, (SOCKLEN_T
*) &size
) < 0)
306 socket
->m_error
= GSOCK_IOERR
;
310 /* got a valid address from getsockname, create a GAddress object */
311 address
= GAddress_new();
314 socket
->m_error
= GSOCK_MEMERR
;
318 err
= _GAddress_translate_from(address
, &addr
, size
);
319 if (err
!= GSOCK_NOERROR
)
321 GAddress_destroy(address
);
322 socket
->m_error
= err
;
329 GAddress
*GSocket_GetPeer(GSocket
*socket
)
331 assert(socket
!= NULL
);
333 /* try to get it from the m_peer var */
335 return GAddress_copy(socket
->m_peer
);
340 /* Server specific parts */
342 /* GSocket_SetServer:
343 * Sets up this socket as a server. The local address must have been
344 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
345 * Returns GSOCK_NOERROR on success, one of the following otherwise:
348 * GSOCK_INVSOCK - the socket is in use.
349 * GSOCK_INVADDR - the local address has not been set.
350 * GSOCK_IOERR - low-level error.
352 GSocketError
GSocket_SetServer(GSocket
*sck
)
359 /* must not be in use */
360 if (sck
->m_fd
!= INVALID_SOCKET
)
362 sck
->m_error
= GSOCK_INVSOCK
;
363 return GSOCK_INVSOCK
;
366 /* the local addr must have been set */
369 sck
->m_error
= GSOCK_INVADDR
;
370 return GSOCK_INVADDR
;
373 /* Initialize all fields */
374 sck
->m_stream
= TRUE
;
375 sck
->m_server
= TRUE
;
376 sck
->m_oriented
= TRUE
;
378 /* Create the socket */
379 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
381 if (sck
->m_fd
== INVALID_SOCKET
)
383 sck
->m_error
= GSOCK_IOERR
;
387 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
388 _GSocket_Enable_Events(sck
);
390 /* Bind to the local address,
391 * retrieve the actual address bound,
392 * and listen up to 5 connections.
394 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
395 (getsockname(sck
->m_fd
,
396 sck
->m_local
->m_addr
,
397 (SOCKLEN_T
*) &sck
->m_local
->m_len
) != 0) ||
398 (listen(sck
->m_fd
, 5) != 0))
401 sck
->m_fd
= INVALID_SOCKET
;
402 sck
->m_error
= GSOCK_IOERR
;
406 return GSOCK_NOERROR
;
409 /* GSocket_WaitConnection:
410 * Waits for an incoming client connection. Returns a pointer to
411 * a GSocket object, or NULL if there was an error, in which case
412 * the last error field will be updated for the calling GSocket.
414 * Error codes (set in the calling GSocket)
415 * GSOCK_INVSOCK - the socket is not valid or not a server.
416 * GSOCK_TIMEDOUT - timeout, no incoming connections.
417 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
418 * GSOCK_MEMERR - couldn't allocate memory.
419 * GSOCK_IOERR - low-level error.
421 GSocket
*GSocket_WaitConnection(GSocket
*socket
)
423 struct sockaddr from
;
424 SOCKLEN_T fromlen
= sizeof(from
);
429 assert(socket
!= NULL
);
431 /* Reenable CONNECTION events */
432 _GSocket_Enable(socket
, GSOCK_CONNECTION
);
434 /* If the socket has already been created, we exit immediately */
435 if (socket
->m_fd
== INVALID_SOCKET
|| !socket
->m_server
)
437 socket
->m_error
= GSOCK_INVSOCK
;
441 /* Create a GSocket object for the new connection */
442 connection
= GSocket_new();
446 socket
->m_error
= GSOCK_MEMERR
;
450 /* Wait for a connection (with timeout) */
451 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
453 GSocket_destroy(connection
);
454 /* socket->m_error set by _GSocket_Input_Timeout */
458 connection
->m_fd
= accept(socket
->m_fd
, &from
, (SOCKLEN_T
*) &fromlen
);
460 if (connection
->m_fd
== INVALID_SOCKET
)
462 if (errno
== EWOULDBLOCK
)
463 socket
->m_error
= GSOCK_WOULDBLOCK
;
465 socket
->m_error
= GSOCK_IOERR
;
467 GSocket_destroy(connection
);
471 /* Initialize all fields */
472 connection
->m_server
= FALSE
;
473 connection
->m_stream
= TRUE
;
474 connection
->m_oriented
= TRUE
;
476 /* Setup the peer address field */
477 connection
->m_peer
= GAddress_new();
478 if (!connection
->m_peer
)
480 GSocket_destroy(connection
);
481 socket
->m_error
= GSOCK_MEMERR
;
484 err
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
);
485 if (err
!= GSOCK_NOERROR
)
487 GAddress_destroy(connection
->m_peer
);
488 GSocket_destroy(connection
);
489 socket
->m_error
= err
;
493 ioctl(connection
->m_fd
, FIONBIO
, &arg
);
494 _GSocket_Enable_Events(connection
);
499 /* Client specific parts */
502 * For stream (connection oriented) sockets, GSocket_Connect() tries
503 * to establish a client connection to a server using the peer address
504 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
505 * connection has been succesfully established, or one of the error
506 * codes listed below. Note that for nonblocking sockets, a return
507 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
508 * request can be completed later; you should use GSocket_Select()
509 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
510 * corresponding asynchronous events.
512 * For datagram (non connection oriented) sockets, GSocket_Connect()
513 * just sets the peer address established with GSocket_SetPeer() as
514 * default destination.
517 * GSOCK_INVSOCK - the socket is in use or not valid.
518 * GSOCK_INVADDR - the peer address has not been established.
519 * GSOCK_TIMEDOUT - timeout, the connection failed.
520 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
521 * GSOCK_MEMERR - couldn't allocate memory.
522 * GSOCK_IOERR - low-level error.
524 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
531 /* Enable CONNECTION events (needed for nonblocking connections) */
532 _GSocket_Enable(sck
, GSOCK_CONNECTION
);
534 if (sck
->m_fd
!= INVALID_SOCKET
)
536 sck
->m_error
= GSOCK_INVSOCK
;
537 return GSOCK_INVSOCK
;
542 sck
->m_error
= GSOCK_INVADDR
;
543 return GSOCK_INVADDR
;
546 /* Streamed or dgram socket? */
547 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
548 sck
->m_oriented
= TRUE
;
549 sck
->m_server
= FALSE
;
550 sck
->m_establishing
= FALSE
;
552 /* Create the socket */
553 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
,
554 sck
->m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
556 if (sck
->m_fd
== INVALID_SOCKET
)
558 sck
->m_error
= GSOCK_IOERR
;
562 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
563 _GSocket_Enable_Events(sck
);
565 /* Connect it to the peer address, with a timeout (see below) */
566 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
572 /* If connect failed with EINPROGRESS and the GSocket object
573 * is in blocking mode, we select() for the specified timeout
574 * checking for writability to see if the connection request
577 if ((err
== EINPROGRESS
) && (!sck
->m_non_blocking
))
579 if (_GSocket_Output_Timeout(sck
) == GSOCK_TIMEDOUT
)
582 sck
->m_fd
= INVALID_SOCKET
;
583 /* sck->m_error is set in _GSocket_Output_Timeout */
584 return GSOCK_TIMEDOUT
;
589 SOCKLEN_T len
= sizeof(error
);
591 getsockopt(sck
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*) &error
, &len
);
594 return GSOCK_NOERROR
;
598 /* If connect failed with EINPROGRESS and the GSocket object
599 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
600 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
601 * this way if the connection completes, a GSOCK_CONNECTION
602 * event will be generated, if enabled.
604 if ((err
== EINPROGRESS
) && (sck
->m_non_blocking
))
606 sck
->m_establishing
= TRUE
;
607 sck
->m_error
= GSOCK_WOULDBLOCK
;
608 return GSOCK_WOULDBLOCK
;
611 /* If connect failed with an error other than EINPROGRESS,
612 * then the call to GSocket_Connect has failed.
615 sck
->m_fd
= INVALID_SOCKET
;
616 sck
->m_error
= GSOCK_IOERR
;
620 return GSOCK_NOERROR
;
623 /* Datagram sockets */
625 /* GSocket_SetNonOriented:
626 * Sets up this socket as a non-connection oriented (datagram) socket.
627 * Before using this function, the local address must have been set
628 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
629 * on success, or one of the following otherwise.
632 * GSOCK_INVSOCK - the socket is in use.
633 * GSOCK_INVADDR - the local address has not been set.
634 * GSOCK_IOERR - low-level error.
636 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
642 if (sck
->m_fd
!= INVALID_SOCKET
)
644 sck
->m_error
= GSOCK_INVSOCK
;
645 return GSOCK_INVSOCK
;
650 sck
->m_error
= GSOCK_INVADDR
;
651 return GSOCK_INVADDR
;
654 /* Initialize all fields */
655 sck
->m_stream
= FALSE
;
656 sck
->m_server
= FALSE
;
657 sck
->m_oriented
= FALSE
;
659 /* Create the socket */
660 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
662 if (sck
->m_fd
== INVALID_SOCKET
)
664 sck
->m_error
= GSOCK_IOERR
;
668 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
669 _GSocket_Enable_Events(sck
);
671 /* Bind to the local address,
672 * and retrieve the actual address bound.
674 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
675 (getsockname(sck
->m_fd
,
676 sck
->m_local
->m_addr
,
677 (SOCKLEN_T
*) &sck
->m_local
->m_len
) != 0))
680 sck
->m_fd
= INVALID_SOCKET
;
681 sck
->m_error
= GSOCK_IOERR
;
685 return GSOCK_NOERROR
;
690 /* Like recv(), send(), ... */
691 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
695 assert(socket
!= NULL
);
697 /* Reenable INPUT events */
698 _GSocket_Enable(socket
, GSOCK_INPUT
);
700 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
702 socket
->m_error
= GSOCK_INVSOCK
;
706 /* If the socket is blocking, wait for data (with a timeout) */
707 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
711 if (socket
->m_stream
)
712 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
714 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
718 if (errno
== EWOULDBLOCK
)
719 socket
->m_error
= GSOCK_WOULDBLOCK
;
721 socket
->m_error
= GSOCK_IOERR
;
727 int GSocket_Write(GSocket
*socket
, const char *buffer
, int size
)
731 assert(socket
!= NULL
);
733 GSocket_Debug(( "GSocket_Write #1, size %d\n", size
));
735 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
737 socket
->m_error
= GSOCK_INVSOCK
;
741 GSocket_Debug(( "GSocket_Write #2, size %d\n", size
));
743 /* If the socket is blocking, wait for writability (with a timeout) */
744 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
747 GSocket_Debug(( "GSocket_Write #3, size %d\n", size
));
750 if (socket
->m_stream
)
751 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
753 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
755 GSocket_Debug(( "GSocket_Write #4, size %d\n", size
));
759 if (errno
== EWOULDBLOCK
)
761 socket
->m_error
= GSOCK_WOULDBLOCK
;
762 GSocket_Debug(( "GSocket_Write error WOULDBLOCK\n" ));
766 socket
->m_error
= GSOCK_IOERR
;
767 GSocket_Debug(( "GSocket_Write error IOERR\n" ));
770 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
771 * in MSW). Once the first OUTPUT event is received, users can assume
772 * that the socket is writable until a read operation fails. Only then
773 * will further OUTPUT events be posted.
775 _GSocket_Enable(socket
, GSOCK_OUTPUT
);
779 GSocket_Debug(( "GSocket_Write #5, size %d ret %d\n", size
, ret
));
785 * Polls the socket to determine its status. This function will
786 * check for the events specified in the 'flags' parameter, and
787 * it will return a mask indicating which operations can be
788 * performed. This function won't block, regardless of the
789 * mode (blocking | nonblocking) of the socket.
791 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
793 #if defined(wxUSE_GUI) && !wxUSE_GUI
795 GSocketEventFlags result
= 0;
801 /* Do not use a static struct, Linux can garble it */
805 assert(socket
!= NULL
);
810 FD_SET(socket
->m_fd
, &readfds
);
811 FD_SET(socket
->m_fd
, &writefds
);
812 FD_SET(socket
->m_fd
, &exceptfds
);
814 /* Check known state first */
815 result
|= (GSOCK_CONNECTION_FLAG
& socket
->m_detected
& flags
);
816 result
|= (GSOCK_LOST_FLAG
& socket
->m_detected
& flags
);
819 if (select(socket
->m_fd
+ 1, &readfds
, &writefds
, &exceptfds
, &tv
) <= 0)
822 /* Check for readability */
823 if (FD_ISSET(socket
->m_fd
, &readfds
))
827 if (recv(socket
->m_fd
, &c
, 1, MSG_PEEK
) > 0)
829 result
|= (GSOCK_INPUT_FLAG
& flags
);
833 if (socket
->m_server
&& socket
->m_stream
)
835 result
|= (GSOCK_CONNECTION_FLAG
& flags
);
836 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
840 result
|= (GSOCK_LOST_FLAG
& flags
);
841 socket
->m_detected
= GSOCK_LOST_FLAG
;
846 /* Check for writability */
847 if (FD_ISSET(socket
->m_fd
, &writefds
))
849 if (socket
->m_establishing
&& !socket
->m_server
)
852 SOCKLEN_T len
= sizeof(error
);
854 socket
->m_establishing
= FALSE
;
856 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*)&error
, &len
);
860 result
|= (GSOCK_LOST_FLAG
& flags
);
861 socket
->m_detected
= GSOCK_LOST_FLAG
;
865 result
|= (GSOCK_CONNECTION_FLAG
& flags
);
866 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
871 result
|= (GSOCK_OUTPUT_FLAG
& flags
);
875 /* Check for exceptions and errors (is this useful in Unices?) */
876 if (FD_ISSET(socket
->m_fd
, &exceptfds
))
878 result
|= (GSOCK_LOST_FLAG
& flags
);
879 socket
->m_establishing
= FALSE
;
880 socket
->m_detected
= GSOCK_LOST_FLAG
;
887 assert(socket
!= NULL
);
888 return flags
& socket
->m_detected
;
890 #endif /* !wxUSE_GUI */
895 /* GSocket_SetNonBlocking:
896 * Sets the socket to non-blocking mode. All IO calls will return
899 void GSocket_SetNonBlocking(GSocket
*socket
, int non_block
)
901 assert(socket
!= NULL
);
903 GSocket_Debug( ("GSocket_SetNonBlocking: %d\n", (int)non_block
) );
905 socket
->m_non_blocking
= non_block
;
908 /* GSocket_SetTimeout:
909 * Sets the timeout for blocking calls. Time is expressed in
912 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
)
914 assert(socket
!= NULL
);
916 socket
->m_timeout
= millisec
;
920 * Returns the last error occured for this socket. Note that successful
921 * operations do not clear this back to GSOCK_NOERROR, so use it only
924 GSocketError
GSocket_GetError(GSocket
*socket
)
926 assert(socket
!= NULL
);
928 return socket
->m_error
;
934 * There is data to be read in the input buffer. If, after a read
935 * operation, there is still data available, the callback function will
938 * The socket is available for writing. That is, the next write call
939 * won't block. This event is generated only once, when the connection is
940 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
941 * when the output buffer empties again. This means that the app should
942 * assume that it can write since the first OUTPUT event, and no more
943 * OUTPUT events will be generated unless an error occurs.
945 * Connection succesfully established, for client sockets, or incoming
946 * client connection, for server sockets. Wait for this event (also watch
947 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
949 * The connection is lost (or a connection request failed); this could
950 * be due to a failure, or due to the peer closing it gracefully.
953 /* GSocket_SetCallback:
954 * Enables the callbacks specified by 'flags'. Note that 'flags'
955 * may be a combination of flags OR'ed toghether, so the same
956 * callback function can be made to accept different events.
957 * The callback function must have the following prototype:
959 * void function(GSocket *socket, GSocketEvent event, char *cdata)
961 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
962 GSocketCallback callback
, char *cdata
)
966 assert(socket
!= NULL
);
968 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
970 if ((flags
& (1 << count
)) != 0)
972 socket
->m_cbacks
[count
] = callback
;
973 socket
->m_data
[count
] = cdata
;
978 /* GSocket_UnsetCallback:
979 * Disables all callbacks specified by 'flags', which may be a
980 * combination of flags OR'ed toghether.
982 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
986 assert(socket
!= NULL
);
988 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
990 if ((flags
& (1 << count
)) != 0)
992 socket
->m_cbacks
[count
] = NULL
;
993 socket
->m_data
[count
] = NULL
;
999 #define CALL_CALLBACK(socket, event) { \
1000 _GSocket_Disable(socket, event); \
1001 if (socket->m_cbacks[event]) \
1002 socket->m_cbacks[event](socket, event, socket->m_data[event]); \
1006 void _GSocket_Enable(GSocket
*socket
, GSocketEvent event
)
1008 socket
->m_detected
&= ~(1 << event
);
1009 _GSocket_Install_Callback(socket
, event
);
1012 void _GSocket_Disable(GSocket
*socket
, GSocketEvent event
)
1014 socket
->m_detected
|= (1 << event
);
1015 _GSocket_Uninstall_Callback(socket
, event
);
1018 /* _GSocket_Input_Timeout:
1019 * For blocking sockets, wait until data is available or
1020 * until timeout ellapses.
1022 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
1028 /* Linux select() will overwrite the struct on return */
1029 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
1030 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
1032 if (!socket
->m_non_blocking
)
1035 FD_SET(socket
->m_fd
, &readfds
);
1036 ret
= select(socket
->m_fd
+ 1, &readfds
, NULL
, NULL
, &tv
);
1039 GSocket_Debug(( "GSocket_Input_Timeout, select returned 0\n" ));
1040 socket
->m_error
= GSOCK_TIMEDOUT
;
1041 return GSOCK_TIMEDOUT
;
1045 GSocket_Debug(( "GSocket_Input_Timeout, select returned -1\n" ));
1046 if (errno
== EBADF
) GSocket_Debug(( "Invalid file descriptor\n" ));
1047 if (errno
== EINTR
) GSocket_Debug(( "A non blocked signal was caught\n" ));
1048 if (errno
== EINVAL
) GSocket_Debug(( "The highest number descriptor is negative\n" ));
1049 if (errno
== ENOMEM
) GSocket_Debug(( "Not enough memory\n" ));
1050 socket
->m_error
= GSOCK_TIMEDOUT
;
1051 return GSOCK_TIMEDOUT
;
1054 return GSOCK_NOERROR
;
1057 /* _GSocket_Output_Timeout:
1058 * For blocking sockets, wait until data can be sent without
1059 * blocking or until timeout ellapses.
1061 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
1067 /* Linux select() will overwrite the struct on return */
1068 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
1069 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
1071 GSocket_Debug( ("m_non_blocking has: %d\n", (int)socket
->m_non_blocking
) );
1073 if (!socket
->m_non_blocking
)
1076 FD_SET(socket
->m_fd
, &writefds
);
1077 ret
= select(socket
->m_fd
+ 1, NULL
, &writefds
, NULL
, &tv
);
1080 GSocket_Debug(( "GSocket_Output_Timeout, select returned 0\n" ));
1081 socket
->m_error
= GSOCK_TIMEDOUT
;
1082 return GSOCK_TIMEDOUT
;
1086 GSocket_Debug(( "GSocket_Output_Timeout, select returned -1\n" ));
1087 if (errno
== EBADF
) GSocket_Debug(( "Invalid file descriptor\n" ));
1088 if (errno
== EINTR
) GSocket_Debug(( "A non blocked signal was caught\n" ));
1089 if (errno
== EINVAL
) GSocket_Debug(( "The highest number descriptor is negative\n" ));
1090 if (errno
== ENOMEM
) GSocket_Debug(( "Not enough memory\n" ));
1091 socket
->m_error
= GSOCK_TIMEDOUT
;
1092 return GSOCK_TIMEDOUT
;
1094 if ( ! FD_ISSET(socket
->m_fd
, &writefds
) )
1095 GSocket_Debug(( "GSocket_Output_Timeout is buggy!\n" ));
1097 GSocket_Debug(( "GSocket_Output_Timeout seems correct\n" ));
1101 GSocket_Debug(( "GSocket_Output_Timeout, didn't try select!\n" ));
1104 return GSOCK_NOERROR
;
1107 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
1109 return recv(socket
->m_fd
, buffer
, size
, 0);
1112 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
1114 struct sockaddr from
;
1115 SOCKLEN_T fromlen
= sizeof(from
);
1119 fromlen
= sizeof(from
);
1121 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, (SOCKLEN_T
*) &fromlen
);
1126 /* Translate a system address into a GSocket address */
1127 if (!socket
->m_peer
)
1129 socket
->m_peer
= GAddress_new();
1130 if (!socket
->m_peer
)
1132 socket
->m_error
= GSOCK_MEMERR
;
1136 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
1137 if (err
!= GSOCK_NOERROR
)
1139 GAddress_destroy(socket
->m_peer
);
1140 socket
->m_peer
= NULL
;
1141 socket
->m_error
= err
;
1148 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
1153 ret
= send(socket
->m_fd
, buffer
, size
, 0);
1159 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
1161 struct sockaddr
*addr
;
1165 if (!socket
->m_peer
)
1167 socket
->m_error
= GSOCK_INVADDR
;
1171 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
1172 if (err
!= GSOCK_NOERROR
)
1174 socket
->m_error
= err
;
1179 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
1182 /* Frees memory allocated from _GAddress_translate_to */
1188 void _GSocket_Detected_Read(GSocket
*socket
)
1192 if (recv(socket
->m_fd
, &c
, 1, MSG_PEEK
) > 0)
1194 CALL_CALLBACK(socket
, GSOCK_INPUT
);
1198 if (socket
->m_server
&& socket
->m_stream
)
1200 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
1204 CALL_CALLBACK(socket
, GSOCK_LOST
);
1209 void _GSocket_Detected_Write(GSocket
*socket
)
1211 if (socket
->m_establishing
&& !socket
->m_server
)
1214 SOCKLEN_T len
= sizeof(error
);
1216 socket
->m_establishing
= FALSE
;
1218 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*)&error
, &len
);
1222 CALL_CALLBACK(socket
, GSOCK_LOST
);
1226 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
1227 /* We have to fire this event by hand because CONNECTION (for clients)
1228 * and OUTPUT are internally the same and we just disabled CONNECTION
1229 * events with the above macro.
1231 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
1236 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
1241 * -------------------------------------------------------------------------
1243 * -------------------------------------------------------------------------
1246 /* CHECK_ADDRESS verifies that the current address family is either
1247 * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it
1248 * initalizes it to be a GSOCK_*family*. In other cases, it returns
1249 * an appropiate error code.
1251 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
1253 #define CHECK_ADDRESS(address, family) \
1255 if (address->m_family == GSOCK_NOFAMILY) \
1256 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1257 return address->m_error; \
1258 if (address->m_family != GSOCK_##family) \
1260 address->m_error = GSOCK_INVADDR; \
1261 return GSOCK_INVADDR; \
1265 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
1267 if (address->m_family == GSOCK_NOFAMILY) \
1268 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1270 if (address->m_family != GSOCK_##family) \
1272 address->m_error = GSOCK_INVADDR; \
1278 GAddress
*GAddress_new(void)
1282 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1285 address
->m_family
= GSOCK_NOFAMILY
;
1286 address
->m_addr
= NULL
;
1292 GAddress
*GAddress_copy(GAddress
*address
)
1296 assert(address
!= NULL
);
1298 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1301 memcpy(addr2
, address
, sizeof(GAddress
));
1303 if (address
->m_addr
)
1305 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
1306 if (addr2
->m_addr
== NULL
)
1311 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1317 void GAddress_destroy(GAddress
*address
)
1319 assert(address
!= NULL
);
1321 if (address
->m_addr
)
1322 free(address
->m_addr
);
1327 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1329 assert(address
!= NULL
);
1331 address
->m_family
= type
;
1334 GAddressType
GAddress_GetFamily(GAddress
*address
)
1336 assert(address
!= NULL
);
1338 return address
->m_family
;
1341 GSocketError
_GAddress_translate_from(GAddress
*address
,
1342 struct sockaddr
*addr
, int len
)
1344 address
->m_realfamily
= addr
->sa_family
;
1345 switch (addr
->sa_family
)
1348 address
->m_family
= GSOCK_INET
;
1351 address
->m_family
= GSOCK_UNIX
;
1355 address
->m_family
= GSOCK_INET6
;
1360 address
->m_error
= GSOCK_INVOP
;
1365 if (address
->m_addr
)
1366 free(address
->m_addr
);
1368 address
->m_len
= len
;
1369 address
->m_addr
= (struct sockaddr
*)malloc(len
);
1371 if (address
->m_addr
== NULL
)
1373 address
->m_error
= GSOCK_MEMERR
;
1374 return GSOCK_MEMERR
;
1376 memcpy(address
->m_addr
, addr
, len
);
1378 return GSOCK_NOERROR
;
1381 GSocketError
_GAddress_translate_to(GAddress
*address
,
1382 struct sockaddr
**addr
, int *len
)
1384 if (!address
->m_addr
)
1386 address
->m_error
= GSOCK_INVADDR
;
1387 return GSOCK_INVADDR
;
1390 *len
= address
->m_len
;
1391 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
1394 address
->m_error
= GSOCK_MEMERR
;
1395 return GSOCK_MEMERR
;
1398 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1399 return GSOCK_NOERROR
;
1403 * -------------------------------------------------------------------------
1404 * Internet address family
1405 * -------------------------------------------------------------------------
1408 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1410 address
->m_len
= sizeof(struct sockaddr_in
);
1411 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1412 if (address
->m_addr
== NULL
)
1414 address
->m_error
= GSOCK_MEMERR
;
1415 return GSOCK_MEMERR
;
1418 address
->m_family
= GSOCK_INET
;
1419 address
->m_realfamily
= PF_INET
;
1420 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1421 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1423 return GSOCK_NOERROR
;
1426 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1429 struct in_addr
*addr
;
1431 assert(address
!= NULL
);
1433 CHECK_ADDRESS(address
, INET
);
1435 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1437 /* If it is a numeric host name, convert it now */
1438 #if defined(HAVE_INET_ATON)
1439 if (inet_aton(hostname
, addr
) == 0)
1441 #elif defined(HAVE_INET_ADDR)
1442 if ( (addr
->s_addr
= inet_addr(hostname
)) == -1 )
1445 /* Use gethostbyname by default */
1449 struct in_addr
*array_addr
;
1451 /* It is a real name, we solve it */
1452 if ((he
= gethostbyname(hostname
)) == NULL
)
1454 /* Reset to invalid address */
1455 addr
->s_addr
= INADDR_NONE
;
1456 address
->m_error
= GSOCK_NOHOST
;
1457 return GSOCK_NOHOST
;
1459 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1460 addr
->s_addr
= array_addr
[0].s_addr
;
1462 return GSOCK_NOERROR
;
1465 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1467 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1470 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1471 unsigned long hostaddr
)
1473 struct in_addr
*addr
;
1475 assert(address
!= NULL
);
1477 CHECK_ADDRESS(address
, INET
);
1479 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1480 addr
->s_addr
= hostaddr
;
1482 return GSOCK_NOERROR
;
1485 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1486 const char *protocol
)
1489 struct sockaddr_in
*addr
;
1491 assert(address
!= NULL
);
1492 CHECK_ADDRESS(address
, INET
);
1496 address
->m_error
= GSOCK_INVPORT
;
1497 return GSOCK_INVPORT
;
1500 se
= getservbyname(port
, protocol
);
1503 if (isdigit(port
[0]))
1507 port_int
= atoi(port
);
1508 addr
= (struct sockaddr_in
*)address
->m_addr
;
1509 addr
->sin_port
= htons(port_int
);
1510 return GSOCK_NOERROR
;
1513 address
->m_error
= GSOCK_INVPORT
;
1514 return GSOCK_INVPORT
;
1517 addr
= (struct sockaddr_in
*)address
->m_addr
;
1518 addr
->sin_port
= se
->s_port
;
1520 return GSOCK_NOERROR
;
1523 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1525 struct sockaddr_in
*addr
;
1527 assert(address
!= NULL
);
1528 CHECK_ADDRESS(address
, INET
);
1530 addr
= (struct sockaddr_in
*)address
->m_addr
;
1531 addr
->sin_port
= htons(port
);
1533 return GSOCK_NOERROR
;
1536 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1540 struct sockaddr_in
*addr
;
1542 assert(address
!= NULL
);
1543 CHECK_ADDRESS(address
, INET
);
1545 addr
= (struct sockaddr_in
*)address
->m_addr
;
1546 addr_buf
= (char *)&(addr
->sin_addr
);
1548 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1551 address
->m_error
= GSOCK_NOHOST
;
1552 return GSOCK_NOHOST
;
1555 strncpy(hostname
, he
->h_name
, sbuf
);
1557 return GSOCK_NOERROR
;
1560 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1562 struct sockaddr_in
*addr
;
1564 assert(address
!= NULL
);
1565 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1567 addr
= (struct sockaddr_in
*)address
->m_addr
;
1569 return addr
->sin_addr
.s_addr
;
1572 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1574 struct sockaddr_in
*addr
;
1576 assert(address
!= NULL
);
1577 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1579 addr
= (struct sockaddr_in
*)address
->m_addr
;
1580 return ntohs(addr
->sin_port
);
1584 * -------------------------------------------------------------------------
1585 * Unix address family
1586 * -------------------------------------------------------------------------
1589 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1591 address
->m_len
= sizeof(struct sockaddr_un
);
1592 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
1593 if (address
->m_addr
== NULL
)
1595 address
->m_error
= GSOCK_MEMERR
;
1596 return GSOCK_MEMERR
;
1599 address
->m_family
= GSOCK_UNIX
;
1600 address
->m_realfamily
= PF_UNIX
;
1601 ((struct sockaddr_un
*)address
->m_addr
)->sun_family
= AF_UNIX
;
1602 ((struct sockaddr_un
*)address
->m_addr
)->sun_path
[0] = 0;
1604 return GSOCK_NOERROR
;
1607 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1609 struct sockaddr_un
*addr
;
1611 assert(address
!= NULL
);
1613 CHECK_ADDRESS(address
, UNIX
);
1615 addr
= ((struct sockaddr_un
*)address
->m_addr
);
1616 memcpy(addr
->sun_path
, path
, strlen(path
));
1618 return GSOCK_NOERROR
;
1621 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1623 struct sockaddr_un
*addr
;
1625 assert(address
!= NULL
);
1626 CHECK_ADDRESS(address
, UNIX
);
1628 addr
= (struct sockaddr_un
*)address
->m_addr
;
1630 strncpy(path
, addr
->sun_path
, sbuf
);
1632 return GSOCK_NOERROR
;
1635 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */