]>
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__
93 # include "wx/unix/gsockunx.h"
94 # include "wx/gsocket.h"
96 # include "gsockunx.h"
98 #endif /* __GSOCKET_STANDALONE__ */
100 /* redefine some GUI-only functions to do nothing in console mode */
101 #if defined(wxUSE_GUI) && !wxUSE_GUI
102 # define _GSocket_GUI_Init(socket) (1)
103 # define _GSocket_GUI_Destroy(socket)
104 # define _GSocket_Enable_Events(socket)
105 # define _GSocket_Disable_Events(socket)
106 # define _GSocket_Install_Callback(socket, event)
107 # define _GSocket_Uninstall_Callback(socket, event)
108 #endif /* wxUSE_GUI */
110 /* debugging helpers */
111 #ifdef __GSOCKET_DEBUG__
112 #define GSocket_Debug(args) printf args
114 #define GSocket_Debug(args)
115 #endif // __GSOCKET_DEBUG__
117 /* Global initialisers */
119 bool GSocket_Init(void)
124 void GSocket_Cleanup(void)
128 /* Constructors / Destructors for GSocket */
130 GSocket
*GSocket_new(void)
135 socket
= (GSocket
*)malloc(sizeof(GSocket
));
141 for (i
=0;i
<GSOCK_MAX_EVENT
;i
++)
143 socket
->m_cbacks
[i
] = NULL
;
145 socket
->m_detected
= 0;
146 socket
->m_local
= NULL
;
147 socket
->m_peer
= NULL
;
148 socket
->m_error
= GSOCK_NOERROR
;
149 socket
->m_server
= FALSE
;
150 socket
->m_stream
= TRUE
;
151 socket
->m_gui_dependent
= NULL
;
152 socket
->m_non_blocking
= FALSE
;
153 socket
->m_timeout
= 10*60*1000;
154 /* 10 minutes * 60 sec * 1000 millisec */
155 socket
->m_establishing
= FALSE
;
157 /* Per-socket GUI-specific initialization */
158 success
= _GSocket_GUI_Init(socket
);
168 void GSocket_destroy(GSocket
*socket
)
170 assert(socket
!= NULL
);
172 /* Check that the socket is really shutdowned */
173 if (socket
->m_fd
!= -1)
174 GSocket_Shutdown(socket
);
176 /* Per-socket GUI-specific cleanup */
177 _GSocket_GUI_Destroy(socket
);
179 /* Destroy private addresses */
181 GAddress_destroy(socket
->m_local
);
184 GAddress_destroy(socket
->m_peer
);
186 /* Destroy the socket itself */
191 * Disallow further read/write operations on this socket, close
192 * the fd and disable all callbacks.
194 void GSocket_Shutdown(GSocket
*socket
)
198 assert(socket
!= NULL
);
200 /* If socket has been created, shutdown it */
201 if (socket
->m_fd
!= -1)
203 shutdown(socket
->m_fd
, 2);
208 /* Disable GUI callbacks */
209 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
210 socket
->m_cbacks
[evt
] = NULL
;
212 socket
->m_detected
= GSOCK_LOST_FLAG
;
213 _GSocket_Disable_Events(socket
);
216 /* Address handling */
222 * Set or get the local or peer address for this socket. The 'set'
223 * functions return GSOCK_NOERROR on success, an error code otherwise.
224 * The 'get' functions return a pointer to a GAddress object on success,
225 * or NULL otherwise, in which case they set the error code of the
226 * corresponding GSocket.
229 * GSOCK_INVSOCK - the socket is not valid.
230 * GSOCK_INVADDR - the address is not valid.
232 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
234 assert(socket
!= NULL
);
236 /* the socket must be initialized, or it must be a server */
237 if ((socket
->m_fd
!= -1 && !socket
->m_server
))
239 socket
->m_error
= GSOCK_INVSOCK
;
240 return GSOCK_INVSOCK
;
244 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
246 socket
->m_error
= GSOCK_INVADDR
;
247 return GSOCK_INVADDR
;
251 GAddress_destroy(socket
->m_local
);
253 socket
->m_local
= GAddress_copy(address
);
255 return GSOCK_NOERROR
;
258 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
260 assert(socket
!= NULL
);
263 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
265 socket
->m_error
= GSOCK_INVADDR
;
266 return GSOCK_INVADDR
;
270 GAddress_destroy(socket
->m_peer
);
272 socket
->m_peer
= GAddress_copy(address
);
274 return GSOCK_NOERROR
;
277 GAddress
*GSocket_GetLocal(GSocket
*socket
)
280 struct sockaddr addr
;
281 SOCKLEN_T size
= sizeof(addr
);
284 assert(socket
!= NULL
);
286 /* try to get it from the m_local var first */
288 return GAddress_copy(socket
->m_local
);
290 /* else, if the socket is initialized, try getsockname */
291 if (socket
->m_fd
== -1)
293 socket
->m_error
= GSOCK_INVSOCK
;
297 if (getsockname(socket
->m_fd
, &addr
, (SOCKLEN_T
*) &size
) < 0)
299 socket
->m_error
= GSOCK_IOERR
;
303 /* got a valid address from getsockname, create a GAddress object */
304 address
= GAddress_new();
307 socket
->m_error
= GSOCK_MEMERR
;
311 err
= _GAddress_translate_from(address
, &addr
, size
);
312 if (err
!= GSOCK_NOERROR
)
314 GAddress_destroy(address
);
315 socket
->m_error
= err
;
322 GAddress
*GSocket_GetPeer(GSocket
*socket
)
324 assert(socket
!= NULL
);
326 /* try to get it from the m_peer var */
328 return GAddress_copy(socket
->m_peer
);
333 /* Server specific parts */
335 /* GSocket_SetServer:
336 * Sets up this socket as a server. The local address must have been
337 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
338 * Returns GSOCK_NOERROR on success, one of the following otherwise:
341 * GSOCK_INVSOCK - the socket is in use.
342 * GSOCK_INVADDR - the local address has not been set.
343 * GSOCK_IOERR - low-level error.
345 GSocketError
GSocket_SetServer(GSocket
*sck
)
352 /* must not be in use */
355 sck
->m_error
= GSOCK_INVSOCK
;
356 return GSOCK_INVSOCK
;
359 /* the local addr must have been set */
362 sck
->m_error
= GSOCK_INVADDR
;
363 return GSOCK_INVADDR
;
366 /* Initialize all fields */
367 sck
->m_stream
= TRUE
;
368 sck
->m_server
= TRUE
;
369 sck
->m_oriented
= TRUE
;
371 /* Create the socket */
372 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
376 sck
->m_error
= GSOCK_IOERR
;
380 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
381 _GSocket_Enable_Events(sck
);
383 /* Bind to the local address,
384 * retrieve the actual address bound,
385 * and listen up to 5 connections.
387 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
388 (getsockname(sck
->m_fd
,
389 sck
->m_local
->m_addr
,
390 (SOCKLEN_T
*) &sck
->m_local
->m_len
) != 0) ||
391 (listen(sck
->m_fd
, 5) != 0))
395 sck
->m_error
= GSOCK_IOERR
;
399 return GSOCK_NOERROR
;
402 /* GSocket_WaitConnection:
403 * Waits for an incoming client connection. Returns a pointer to
404 * a GSocket object, or NULL if there was an error, in which case
405 * the last error field will be updated for the calling GSocket.
407 * Error codes (set in the calling GSocket)
408 * GSOCK_INVSOCK - the socket is not valid or not a server.
409 * GSOCK_TIMEDOUT - timeout, no incoming connections.
410 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
411 * GSOCK_MEMERR - couldn't allocate memory.
412 * GSOCK_IOERR - low-level error.
414 GSocket
*GSocket_WaitConnection(GSocket
*socket
)
416 struct sockaddr from
;
417 SOCKLEN_T fromlen
= sizeof(from
);
422 assert(socket
!= NULL
);
424 /* Reenable CONNECTION events */
425 _GSocket_Enable(socket
, GSOCK_CONNECTION
);
427 /* If the socket has already been created, we exit immediately */
428 if (socket
->m_fd
== -1 || !socket
->m_server
)
430 socket
->m_error
= GSOCK_INVSOCK
;
434 /* Create a GSocket object for the new connection */
435 connection
= GSocket_new();
439 socket
->m_error
= GSOCK_MEMERR
;
443 /* Wait for a connection (with timeout) */
444 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
446 GSocket_destroy(connection
);
447 /* socket->m_error set by _GSocket_Input_Timeout */
451 connection
->m_fd
= accept(socket
->m_fd
, &from
, (SOCKLEN_T
*) &fromlen
);
453 if (connection
->m_fd
== -1)
455 if (errno
== EWOULDBLOCK
)
456 socket
->m_error
= GSOCK_WOULDBLOCK
;
458 socket
->m_error
= GSOCK_IOERR
;
460 GSocket_destroy(connection
);
464 /* Initialize all fields */
465 connection
->m_server
= FALSE
;
466 connection
->m_stream
= TRUE
;
467 connection
->m_oriented
= TRUE
;
469 /* Setup the peer address field */
470 connection
->m_peer
= GAddress_new();
471 if (!connection
->m_peer
)
473 GSocket_destroy(connection
);
474 socket
->m_error
= GSOCK_MEMERR
;
477 err
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
);
478 if (err
!= GSOCK_NOERROR
)
480 GAddress_destroy(connection
->m_peer
);
481 GSocket_destroy(connection
);
482 socket
->m_error
= err
;
486 ioctl(connection
->m_fd
, FIONBIO
, &arg
);
487 _GSocket_Enable_Events(connection
);
492 /* Datagram sockets */
494 /* GSocket_SetNonOriented:
495 * Sets up this socket as a non-connection oriented (datagram) socket.
496 * Before using this function, the local address must have been set
497 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
498 * on success, or one of the following otherwise.
501 * GSOCK_INVSOCK - the socket is in use.
502 * GSOCK_INVADDR - the local address has not been set.
503 * GSOCK_IOERR - low-level error.
505 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
513 sck
->m_error
= GSOCK_INVSOCK
;
514 return GSOCK_INVSOCK
;
519 sck
->m_error
= GSOCK_INVADDR
;
520 return GSOCK_INVADDR
;
523 /* Initialize all fields */
524 sck
->m_stream
= FALSE
;
525 sck
->m_server
= FALSE
;
526 sck
->m_oriented
= FALSE
;
528 /* Create the socket */
529 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
533 sck
->m_error
= GSOCK_IOERR
;
537 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
538 _GSocket_Enable_Events(sck
);
540 /* Bind to the local address,
541 * and retrieve the actual address bound.
543 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
544 (getsockname(sck
->m_fd
,
545 sck
->m_local
->m_addr
,
546 (SOCKLEN_T
*) &sck
->m_local
->m_len
) != 0))
550 sck
->m_error
= GSOCK_IOERR
;
554 return GSOCK_NOERROR
;
557 /* Client specific parts */
560 * For stream (connection oriented) sockets, GSocket_Connect() tries
561 * to establish a client connection to a server using the peer address
562 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
563 * connection has been succesfully established, or one of the error
564 * codes listed below. Note that for nonblocking sockets, a return
565 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
566 * request can be completed later; you should use GSocket_Select()
567 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
568 * corresponding asynchronous events.
570 * For datagram (non connection oriented) sockets, GSocket_Connect()
571 * just sets the peer address established with GSocket_SetPeer() as
572 * default destination.
575 * GSOCK_INVSOCK - the socket is in use or not valid.
576 * GSOCK_INVADDR - the peer address has not been established.
577 * GSOCK_TIMEDOUT - timeout, the connection failed.
578 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
579 * GSOCK_MEMERR - couldn't allocate memory.
580 * GSOCK_IOERR - low-level error.
582 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
589 /* Enable CONNECTION events (needed for nonblocking connections) */
590 _GSocket_Enable(sck
, GSOCK_CONNECTION
);
594 sck
->m_error
= GSOCK_INVSOCK
;
595 return GSOCK_INVSOCK
;
600 sck
->m_error
= GSOCK_INVADDR
;
601 return GSOCK_INVADDR
;
604 /* Streamed or dgram socket? */
605 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
606 sck
->m_oriented
= TRUE
;
607 sck
->m_server
= FALSE
;
608 sck
->m_establishing
= FALSE
;
610 /* Create the socket */
611 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
,
612 sck
->m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
616 sck
->m_error
= GSOCK_IOERR
;
620 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
621 _GSocket_Enable_Events(sck
);
623 /* Connect it to the peer address, with a timeout (see below) */
624 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
630 /* If connect failed with EINPROGRESS and the GSocket object
631 * is in blocking mode, we select() for the specified timeout
632 * checking for writability to see if the connection request
635 if ((err
== EINPROGRESS
) && (!sck
->m_non_blocking
))
637 if (_GSocket_Output_Timeout(sck
) == GSOCK_TIMEDOUT
)
641 /* sck->m_error is set in _GSocket_Output_Timeout */
642 return GSOCK_TIMEDOUT
;
647 SOCKLEN_T len
= sizeof(error
);
649 getsockopt(sck
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*) &error
, &len
);
652 return GSOCK_NOERROR
;
656 /* If connect failed with EINPROGRESS and the GSocket object
657 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
658 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
659 * this way if the connection completes, a GSOCK_CONNECTION
660 * event will be generated, if enabled.
662 if ((err
== EINPROGRESS
) && (sck
->m_non_blocking
))
664 sck
->m_establishing
= TRUE
;
665 sck
->m_error
= GSOCK_WOULDBLOCK
;
666 return GSOCK_WOULDBLOCK
;
669 /* If connect failed with an error other than EINPROGRESS,
670 * then the call to GSocket_Connect has failed.
674 sck
->m_error
= GSOCK_IOERR
;
678 return GSOCK_NOERROR
;
683 /* Like recv(), send(), ... */
684 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
688 assert(socket
!= NULL
);
690 /* Reenable INPUT events */
691 _GSocket_Enable(socket
, GSOCK_INPUT
);
693 if (socket
->m_fd
== -1 || socket
->m_server
)
695 socket
->m_error
= GSOCK_INVSOCK
;
699 /* If the socket is blocking, wait for data (with a timeout) */
700 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
704 if (socket
->m_stream
)
705 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
707 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
711 if (errno
== EWOULDBLOCK
)
712 socket
->m_error
= GSOCK_WOULDBLOCK
;
714 socket
->m_error
= GSOCK_IOERR
;
720 int GSocket_Write(GSocket
*socket
, const char *buffer
, int size
)
724 assert(socket
!= NULL
);
726 GSocket_Debug(( "GSocket_Write #1, size %d\n", size
));
728 if (socket
->m_fd
== -1 || socket
->m_server
)
730 socket
->m_error
= GSOCK_INVSOCK
;
734 GSocket_Debug(( "GSocket_Write #2, size %d\n", size
));
736 /* If the socket is blocking, wait for writability (with a timeout) */
737 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
740 GSocket_Debug(( "GSocket_Write #3, size %d\n", size
));
743 if (socket
->m_stream
)
744 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
746 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
748 GSocket_Debug(( "GSocket_Write #4, size %d\n", size
));
752 if (errno
== EWOULDBLOCK
)
754 socket
->m_error
= GSOCK_WOULDBLOCK
;
755 GSocket_Debug(( "GSocket_Write error WOULDBLOCK\n" ));
759 socket
->m_error
= GSOCK_IOERR
;
760 GSocket_Debug(( "GSocket_Write error IOERR\n" ));
763 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
764 * in MSW). Once the first OUTPUT event is received, users can assume
765 * that the socket is writable until a read operation fails. Only then
766 * will further OUTPUT events be posted.
768 _GSocket_Enable(socket
, GSOCK_OUTPUT
);
772 GSocket_Debug(( "GSocket_Write #5, size %d ret %d\n", size
, ret
));
778 * Polls the socket to determine its status. This function will
779 * check for the events specified in the 'flags' parameter, and
780 * it will return a mask indicating which operations can be
781 * performed. This function won't block, regardless of the
782 * mode (blocking | nonblocking) of the socket.
784 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
786 #if defined(wxUSE_GUI) && !wxUSE_GUI
788 GSocketEventFlags result
= 0;
794 /* Do not use a static struct, Linux can garble it */
798 assert(socket
!= NULL
);
803 FD_SET(socket
->m_fd
, &readfds
);
804 FD_SET(socket
->m_fd
, &writefds
);
805 FD_SET(socket
->m_fd
, &exceptfds
);
807 /* Check known state first */
808 result
|= (GSOCK_CONNECTION_FLAG
& socket
->m_detected
& flags
);
809 result
|= (GSOCK_LOST_FLAG
& socket
->m_detected
& flags
);
812 if (select(socket
->m_fd
+ 1, &readfds
, &writefds
, &exceptfds
, &tv
) <= 0)
815 /* Check for readability */
816 if (FD_ISSET(socket
->m_fd
, &readfds
))
820 if (recv(socket
->m_fd
, &c
, 1, MSG_PEEK
) > 0)
822 result
|= (GSOCK_INPUT_FLAG
& flags
);
826 if (socket
->m_server
&& socket
->m_stream
)
828 result
|= (GSOCK_CONNECTION_FLAG
& flags
);
829 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
833 result
|= (GSOCK_LOST_FLAG
& flags
);
834 socket
->m_detected
= GSOCK_LOST_FLAG
;
839 /* Check for writability */
840 if (FD_ISSET(socket
->m_fd
, &writefds
))
842 if (socket
->m_establishing
&& !socket
->m_server
)
845 SOCKLEN_T len
= sizeof(error
);
847 socket
->m_establishing
= FALSE
;
849 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*)&error
, &len
);
853 result
|= (GSOCK_LOST_FLAG
& flags
);
854 socket
->m_detected
= GSOCK_LOST_FLAG
;
858 result
|= (GSOCK_CONNECTION_FLAG
& flags
);
859 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
864 result
|= (GSOCK_OUTPUT_FLAG
& flags
);
868 /* Check for exceptions and errors (is this useful in Unices?) */
869 if (FD_ISSET(socket
->m_fd
, &exceptfds
))
871 result
|= (GSOCK_LOST_FLAG
& flags
);
872 socket
->m_establishing
= FALSE
;
873 socket
->m_detected
= GSOCK_LOST_FLAG
;
880 assert(socket
!= NULL
);
881 return flags
& socket
->m_detected
;
883 #endif /* !wxUSE_GUI */
888 /* GSocket_SetNonBlocking:
889 * Sets the socket to non-blocking mode. All IO calls will return
892 void GSocket_SetNonBlocking(GSocket
*socket
, bool non_block
)
894 assert(socket
!= NULL
);
896 socket
->m_non_blocking
= non_block
;
899 /* GSocket_SetTimeout:
900 * Sets the timeout for blocking calls. Time is expressed in
903 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
)
905 assert(socket
!= NULL
);
907 socket
->m_timeout
= millisec
;
911 * Returns the last error occured for this socket. Note that successful
912 * operations do not clear this back to GSOCK_NOERROR, so use it only
915 GSocketError
GSocket_GetError(GSocket
*socket
)
917 assert(socket
!= NULL
);
919 return socket
->m_error
;
925 * There is data to be read in the input buffer. If, after a read
926 * operation, there is still data available, the callback function will
929 * The socket is available for writing. That is, the next write call
930 * won't block. This event is generated only once, when the connection is
931 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
932 * when the output buffer empties again. This means that the app should
933 * assume that it can write since the first OUTPUT event, and no more
934 * OUTPUT events will be generated unless an error occurs.
936 * Connection succesfully established, for client sockets, or incoming
937 * client connection, for server sockets. Wait for this event (also watch
938 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
940 * The connection is lost (or a connection request failed); this could
941 * be due to a failure, or due to the peer closing it gracefully.
944 /* GSocket_SetCallback:
945 * Enables the callbacks specified by 'flags'. Note that 'flags'
946 * may be a combination of flags OR'ed toghether, so the same
947 * callback function can be made to accept different events.
948 * The callback function must have the following prototype:
950 * void function(GSocket *socket, GSocketEvent event, char *cdata)
952 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
953 GSocketCallback callback
, char *cdata
)
957 assert(socket
!= NULL
);
959 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
961 if ((flags
& (1 << count
)) != 0)
963 socket
->m_cbacks
[count
] = callback
;
964 socket
->m_data
[count
] = cdata
;
969 /* GSocket_UnsetCallback:
970 * Disables all callbacks specified by 'flags', which may be a
971 * combination of flags OR'ed toghether.
973 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
977 assert(socket
!= NULL
);
979 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
981 if ((flags
& (1 << count
)) != 0)
983 socket
->m_cbacks
[count
] = NULL
;
984 socket
->m_data
[count
] = NULL
;
990 #define CALL_CALLBACK(socket, event) { \
991 _GSocket_Disable(socket, event); \
992 if (socket->m_cbacks[event]) \
993 socket->m_cbacks[event](socket, event, socket->m_data[event]); \
997 void _GSocket_Enable(GSocket
*socket
, GSocketEvent event
)
999 socket
->m_detected
&= ~(1 << event
);
1000 _GSocket_Install_Callback(socket
, event
);
1003 void _GSocket_Disable(GSocket
*socket
, GSocketEvent event
)
1005 socket
->m_detected
|= (1 << event
);
1006 _GSocket_Uninstall_Callback(socket
, event
);
1009 /* _GSocket_Input_Timeout:
1010 * For blocking sockets, wait until data is available or
1011 * until timeout ellapses.
1013 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
1019 /* Linux select() will overwrite the struct on return */
1020 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
1021 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
1023 if (!socket
->m_non_blocking
)
1026 FD_SET(socket
->m_fd
, &readfds
);
1027 ret
= select(socket
->m_fd
+ 1, &readfds
, NULL
, NULL
, &tv
);
1030 GSocket_Debug(( "GSocket_Input_Timeout, select returned 0\n" ));
1031 socket
->m_error
= GSOCK_TIMEDOUT
;
1032 return GSOCK_TIMEDOUT
;
1036 GSocket_Debug(( "GSocket_Input_Timeout, select returned -1\n" ));
1037 if (errno
== EBADF
) GSocket_Debug(( "Invalid file descriptor\n" ));
1038 if (errno
== EINTR
) GSocket_Debug(( "A non blocked signal was caught\n" ));
1039 if (errno
== EINVAL
) GSocket_Debug(( "The highest number descriptor is negative\n" ));
1040 if (errno
== ENOMEM
) GSocket_Debug(( "Not enough memory\n" ));
1041 socket
->m_error
= GSOCK_TIMEDOUT
;
1042 return GSOCK_TIMEDOUT
;
1045 return GSOCK_NOERROR
;
1048 /* _GSocket_Output_Timeout:
1049 * For blocking sockets, wait until data can be sent without
1050 * blocking or until timeout ellapses.
1052 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
1058 /* Linux select() will overwrite the struct on return */
1059 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
1060 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
1062 if (!socket
->m_non_blocking
)
1065 FD_SET(socket
->m_fd
, &writefds
);
1066 ret
= select(socket
->m_fd
+ 1, NULL
, &writefds
, NULL
, &tv
);
1069 GSocket_Debug(( "GSocket_Output_Timeout, select returned 0\n" ));
1070 socket
->m_error
= GSOCK_TIMEDOUT
;
1071 return GSOCK_TIMEDOUT
;
1075 GSocket_Debug(( "GSocket_Output_Timeout, select returned -1\n" ));
1076 if (errno
== EBADF
) GSocket_Debug(( "Invalid file descriptor\n" ));
1077 if (errno
== EINTR
) GSocket_Debug(( "A non blocked signal was caught\n" ));
1078 if (errno
== EINVAL
) GSocket_Debug(( "The highest number descriptor is negative\n" ));
1079 if (errno
== ENOMEM
) GSocket_Debug(( "Not enough memory\n" ));
1080 socket
->m_error
= GSOCK_TIMEDOUT
;
1081 return GSOCK_TIMEDOUT
;
1084 return GSOCK_NOERROR
;
1087 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
1089 return recv(socket
->m_fd
, buffer
, size
, 0);
1092 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
1094 struct sockaddr from
;
1095 SOCKLEN_T fromlen
= sizeof(from
);
1099 fromlen
= sizeof(from
);
1101 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, (SOCKLEN_T
*) &fromlen
);
1106 /* Translate a system address into a GSocket address */
1107 if (!socket
->m_peer
)
1109 socket
->m_peer
= GAddress_new();
1110 if (!socket
->m_peer
)
1112 socket
->m_error
= GSOCK_MEMERR
;
1116 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
1117 if (err
!= GSOCK_NOERROR
)
1119 GAddress_destroy(socket
->m_peer
);
1120 socket
->m_peer
= NULL
;
1121 socket
->m_error
= err
;
1128 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
1133 ret
= send(socket
->m_fd
, buffer
, size
, 0);
1139 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
1141 struct sockaddr
*addr
;
1145 if (!socket
->m_peer
)
1147 socket
->m_error
= GSOCK_INVADDR
;
1151 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
1152 if (err
!= GSOCK_NOERROR
)
1154 socket
->m_error
= err
;
1159 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
1162 /* Frees memory allocated from _GAddress_translate_to */
1168 void _GSocket_Detected_Read(GSocket
*socket
)
1172 if (recv(socket
->m_fd
, &c
, 1, MSG_PEEK
) > 0)
1174 CALL_CALLBACK(socket
, GSOCK_INPUT
);
1178 if (socket
->m_server
&& socket
->m_stream
)
1180 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
1184 CALL_CALLBACK(socket
, GSOCK_LOST
);
1189 void _GSocket_Detected_Write(GSocket
*socket
)
1191 if (socket
->m_establishing
&& !socket
->m_server
)
1194 SOCKLEN_T len
= sizeof(error
);
1196 socket
->m_establishing
= FALSE
;
1198 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*)&error
, &len
);
1202 CALL_CALLBACK(socket
, GSOCK_LOST
);
1206 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
1207 /* We have to fire this event by hand because CONNECTION (for clients)
1208 * and OUTPUT are internally the same and we just disabled CONNECTION
1209 * events with the above macro.
1211 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
1216 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
1221 * -------------------------------------------------------------------------
1223 * -------------------------------------------------------------------------
1226 /* CHECK_ADDRESS verifies that the current family is either GSOCK_NOFAMILY
1227 * or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it initalizes address
1228 * to be a GSOCK_*family*. In other cases, it returns GSOCK_INVADDR.
1230 #define CHECK_ADDRESS(address, family, retval) \
1232 if (address->m_family == GSOCK_NOFAMILY) \
1233 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1234 return address->m_error; \
1235 if (address->m_family != GSOCK_##family) \
1237 address->m_error = GSOCK_INVADDR; \
1242 GAddress
*GAddress_new(void)
1246 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1249 address
->m_family
= GSOCK_NOFAMILY
;
1250 address
->m_addr
= NULL
;
1256 GAddress
*GAddress_copy(GAddress
*address
)
1260 assert(address
!= NULL
);
1262 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1265 memcpy(addr2
, address
, sizeof(GAddress
));
1267 if (address
->m_addr
)
1269 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
1270 if (addr2
->m_addr
== NULL
)
1275 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1281 void GAddress_destroy(GAddress
*address
)
1283 assert(address
!= NULL
);
1288 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1290 assert(address
!= NULL
);
1292 address
->m_family
= type
;
1295 GAddressType
GAddress_GetFamily(GAddress
*address
)
1297 assert(address
!= NULL
);
1299 return address
->m_family
;
1302 GSocketError
_GAddress_translate_from(GAddress
*address
,
1303 struct sockaddr
*addr
, int len
)
1305 address
->m_realfamily
= addr
->sa_family
;
1306 switch (addr
->sa_family
)
1309 address
->m_family
= GSOCK_INET
;
1312 address
->m_family
= GSOCK_UNIX
;
1316 address
->m_family
= GSOCK_INET6
;
1321 address
->m_error
= GSOCK_INVOP
;
1326 if (address
->m_addr
)
1327 free(address
->m_addr
);
1329 address
->m_len
= len
;
1330 address
->m_addr
= (struct sockaddr
*)malloc(len
);
1332 if (address
->m_addr
== NULL
)
1334 address
->m_error
= GSOCK_MEMERR
;
1335 return GSOCK_MEMERR
;
1337 memcpy(address
->m_addr
, addr
, len
);
1339 return GSOCK_NOERROR
;
1342 GSocketError
_GAddress_translate_to(GAddress
*address
,
1343 struct sockaddr
**addr
, int *len
)
1345 if (!address
->m_addr
)
1347 address
->m_error
= GSOCK_INVADDR
;
1348 return GSOCK_INVADDR
;
1351 *len
= address
->m_len
;
1352 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
1353 if (*addr
== NULL
) {
1354 address
->m_error
= GSOCK_MEMERR
;
1355 return GSOCK_MEMERR
;
1358 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1359 return GSOCK_NOERROR
;
1363 * -------------------------------------------------------------------------
1364 * Internet address family
1365 * -------------------------------------------------------------------------
1368 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1370 address
->m_len
= sizeof(struct sockaddr_in
);
1371 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1372 if (address
->m_addr
== NULL
)
1374 address
->m_error
= GSOCK_MEMERR
;
1375 return GSOCK_MEMERR
;
1378 address
->m_family
= GSOCK_INET
;
1379 address
->m_realfamily
= PF_INET
;
1380 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1381 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1383 return GSOCK_NOERROR
;
1386 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1389 struct in_addr
*addr
;
1391 assert(address
!= NULL
);
1393 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1395 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1397 /* If it is a numeric host name, convert it now */
1398 #if defined(HAVE_INET_ATON)
1399 if (inet_aton(hostname
, addr
) == 0)
1401 #elif defined(HAVE_INET_ADDR)
1402 if ( (addr
->s_addr
= inet_addr(hostname
)) == -1 )
1405 /* Use gethostbyname by default */
1409 struct in_addr
*array_addr
;
1411 /* It is a real name, we solve it */
1412 if ((he
= gethostbyname(hostname
)) == NULL
)
1414 /* Reset to invalid address */
1415 addr
->s_addr
= INADDR_NONE
;
1416 address
->m_error
= GSOCK_NOHOST
;
1417 return GSOCK_NOHOST
;
1419 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1420 addr
->s_addr
= array_addr
[0].s_addr
;
1422 return GSOCK_NOERROR
;
1425 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1427 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1430 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1431 unsigned long hostaddr
)
1433 struct in_addr
*addr
;
1435 assert(address
!= NULL
);
1437 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1439 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1440 addr
->s_addr
= hostaddr
;
1442 return GSOCK_NOERROR
;
1445 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1446 const char *protocol
)
1449 struct sockaddr_in
*addr
;
1451 assert(address
!= NULL
);
1452 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1456 address
->m_error
= GSOCK_INVPORT
;
1457 return GSOCK_INVPORT
;
1460 se
= getservbyname(port
, protocol
);
1463 if (isdigit(port
[0]))
1467 port_int
= atoi(port
);
1468 addr
= (struct sockaddr_in
*)address
->m_addr
;
1469 addr
->sin_port
= htons(port_int
);
1470 return GSOCK_NOERROR
;
1473 address
->m_error
= GSOCK_INVPORT
;
1474 return GSOCK_INVPORT
;
1477 addr
= (struct sockaddr_in
*)address
->m_addr
;
1478 addr
->sin_port
= se
->s_port
;
1480 return GSOCK_NOERROR
;
1483 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1485 struct sockaddr_in
*addr
;
1487 assert(address
!= NULL
);
1488 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1490 addr
= (struct sockaddr_in
*)address
->m_addr
;
1491 addr
->sin_port
= htons(port
);
1493 return GSOCK_NOERROR
;
1496 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1500 struct sockaddr_in
*addr
;
1502 assert(address
!= NULL
);
1503 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1505 addr
= (struct sockaddr_in
*)address
->m_addr
;
1506 addr_buf
= (char *)&(addr
->sin_addr
);
1508 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1511 address
->m_error
= GSOCK_NOHOST
;
1512 return GSOCK_NOHOST
;
1515 strncpy(hostname
, he
->h_name
, sbuf
);
1517 return GSOCK_NOERROR
;
1520 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1522 struct sockaddr_in
*addr
;
1524 assert(address
!= NULL
);
1525 CHECK_ADDRESS(address
, INET
, 0);
1527 addr
= (struct sockaddr_in
*)address
->m_addr
;
1529 return addr
->sin_addr
.s_addr
;
1532 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1534 struct sockaddr_in
*addr
;
1536 assert(address
!= NULL
);
1537 CHECK_ADDRESS(address
, INET
, 0);
1539 addr
= (struct sockaddr_in
*)address
->m_addr
;
1540 return ntohs(addr
->sin_port
);
1544 * -------------------------------------------------------------------------
1545 * Unix address family
1546 * -------------------------------------------------------------------------
1549 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1551 address
->m_len
= sizeof(struct sockaddr_un
);
1552 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
1553 if (address
->m_addr
== NULL
)
1555 address
->m_error
= GSOCK_MEMERR
;
1556 return GSOCK_MEMERR
;
1559 address
->m_family
= GSOCK_UNIX
;
1560 address
->m_realfamily
= PF_UNIX
;
1561 ((struct sockaddr_un
*)address
->m_addr
)->sun_family
= AF_UNIX
;
1562 ((struct sockaddr_un
*)address
->m_addr
)->sun_path
[0] = 0;
1567 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1569 struct sockaddr_un
*addr
;
1571 assert(address
!= NULL
);
1573 CHECK_ADDRESS(address
, UNIX
, GSOCK_INVADDR
);
1575 addr
= ((struct sockaddr_un
*)address
->m_addr
);
1576 memcpy(addr
->sun_path
, path
, strlen(path
));
1578 return GSOCK_NOERROR
;
1581 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1583 struct sockaddr_un
*addr
;
1585 assert(address
!= NULL
);
1586 CHECK_ADDRESS(address
, UNIX
, GSOCK_INVADDR
);
1588 addr
= (struct sockaddr_un
*)address
->m_addr
;
1590 strncpy(path
, addr
->sun_path
, sbuf
);
1592 return GSOCK_NOERROR
;
1595 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */