]>
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 */
73 * MSW defines this, Unices don't.
75 #ifndef INVALID_SOCKET
76 #define INVALID_SOCKET -1
80 * INADDR_BROADCAST is identical to INADDR_NONE which is not defined
81 * on all systems. INADDR_BROADCAST should be fine to indicate an error.
84 #define INADDR_NONE INADDR_BROADCAST
87 #define MASK_SIGNAL() \
89 void (*old_handler)(int); \
91 old_handler = signal(SIGPIPE, SIG_IGN);
93 #define UNMASK_SIGNAL() \
94 signal(SIGPIPE, old_handler); \
98 #ifndef __GSOCKET_STANDALONE__
99 # include "wx/unix/gsockunx.h"
100 # include "wx/gsocket.h"
102 # include "gsockunx.h"
103 # include "gsocket.h"
104 #endif /* __GSOCKET_STANDALONE__ */
106 /* redefine some GUI-only functions to do nothing in console mode */
107 #if defined(wxUSE_GUI) && !wxUSE_GUI
108 # define _GSocket_GUI_Init(socket) (1)
109 # define _GSocket_GUI_Destroy(socket)
110 # define _GSocket_Enable_Events(socket)
111 # define _GSocket_Disable_Events(socket)
112 # define _GSocket_Install_Callback(socket, event)
113 # define _GSocket_Uninstall_Callback(socket, event)
114 #endif /* wxUSE_GUI */
116 /* debugging helpers */
117 #ifdef __GSOCKET_DEBUG__
118 #define GSocket_Debug(args) printf args
120 #define GSocket_Debug(args)
121 #endif // __GSOCKET_DEBUG__
123 /* Global initialisers */
125 bool GSocket_Init(void)
130 void GSocket_Cleanup(void)
134 /* Constructors / Destructors for GSocket */
136 GSocket
*GSocket_new(void)
141 socket
= (GSocket
*)malloc(sizeof(GSocket
));
146 socket
->m_fd
= INVALID_SOCKET
;
147 for (i
=0;i
<GSOCK_MAX_EVENT
;i
++)
149 socket
->m_cbacks
[i
] = NULL
;
151 socket
->m_detected
= 0;
152 socket
->m_local
= NULL
;
153 socket
->m_peer
= NULL
;
154 socket
->m_error
= GSOCK_NOERROR
;
155 socket
->m_server
= FALSE
;
156 socket
->m_stream
= TRUE
;
157 socket
->m_gui_dependent
= NULL
;
158 socket
->m_non_blocking
= FALSE
;
159 socket
->m_timeout
= 10*60*1000;
160 /* 10 minutes * 60 sec * 1000 millisec */
161 socket
->m_establishing
= FALSE
;
163 /* Per-socket GUI-specific initialization */
164 success
= _GSocket_GUI_Init(socket
);
174 void GSocket_destroy(GSocket
*socket
)
176 assert(socket
!= NULL
);
178 /* Check that the socket is really shutdowned */
179 if (socket
->m_fd
!= INVALID_SOCKET
)
180 GSocket_Shutdown(socket
);
182 /* Per-socket GUI-specific cleanup */
183 _GSocket_GUI_Destroy(socket
);
185 /* Destroy private addresses */
187 GAddress_destroy(socket
->m_local
);
190 GAddress_destroy(socket
->m_peer
);
192 /* Destroy the socket itself */
197 * Disallow further read/write operations on this socket, close
198 * the fd and disable all callbacks.
200 void GSocket_Shutdown(GSocket
*socket
)
204 assert(socket
!= NULL
);
206 /* If socket has been created, shutdown it */
207 if (socket
->m_fd
!= INVALID_SOCKET
)
209 shutdown(socket
->m_fd
, 2);
211 socket
->m_fd
= INVALID_SOCKET
;
214 /* Disable GUI callbacks */
215 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
216 socket
->m_cbacks
[evt
] = NULL
;
218 socket
->m_detected
= GSOCK_LOST_FLAG
;
219 _GSocket_Disable_Events(socket
);
222 /* Address handling */
228 * Set or get the local or peer address for this socket. The 'set'
229 * functions return GSOCK_NOERROR on success, an error code otherwise.
230 * The 'get' functions return a pointer to a GAddress object on success,
231 * or NULL otherwise, in which case they set the error code of the
232 * corresponding GSocket.
235 * GSOCK_INVSOCK - the socket is not valid.
236 * GSOCK_INVADDR - the address is not valid.
238 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
240 assert(socket
!= NULL
);
242 /* the socket must be initialized, or it must be a server */
243 if ((socket
->m_fd
!= INVALID_SOCKET
&& !socket
->m_server
))
245 socket
->m_error
= GSOCK_INVSOCK
;
246 return GSOCK_INVSOCK
;
250 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
252 socket
->m_error
= GSOCK_INVADDR
;
253 return GSOCK_INVADDR
;
257 GAddress_destroy(socket
->m_local
);
259 socket
->m_local
= GAddress_copy(address
);
261 return GSOCK_NOERROR
;
264 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
266 assert(socket
!= NULL
);
269 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
271 socket
->m_error
= GSOCK_INVADDR
;
272 return GSOCK_INVADDR
;
276 GAddress_destroy(socket
->m_peer
);
278 socket
->m_peer
= GAddress_copy(address
);
280 return GSOCK_NOERROR
;
283 GAddress
*GSocket_GetLocal(GSocket
*socket
)
286 struct sockaddr addr
;
287 SOCKLEN_T size
= sizeof(addr
);
290 assert(socket
!= NULL
);
292 /* try to get it from the m_local var first */
294 return GAddress_copy(socket
->m_local
);
296 /* else, if the socket is initialized, try getsockname */
297 if (socket
->m_fd
== INVALID_SOCKET
)
299 socket
->m_error
= GSOCK_INVSOCK
;
303 if (getsockname(socket
->m_fd
, &addr
, (SOCKLEN_T
*) &size
) < 0)
305 socket
->m_error
= GSOCK_IOERR
;
309 /* got a valid address from getsockname, create a GAddress object */
310 address
= GAddress_new();
313 socket
->m_error
= GSOCK_MEMERR
;
317 err
= _GAddress_translate_from(address
, &addr
, size
);
318 if (err
!= GSOCK_NOERROR
)
320 GAddress_destroy(address
);
321 socket
->m_error
= err
;
328 GAddress
*GSocket_GetPeer(GSocket
*socket
)
330 assert(socket
!= NULL
);
332 /* try to get it from the m_peer var */
334 return GAddress_copy(socket
->m_peer
);
339 /* Server specific parts */
341 /* GSocket_SetServer:
342 * Sets up this socket as a server. The local address must have been
343 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
344 * Returns GSOCK_NOERROR on success, one of the following otherwise:
347 * GSOCK_INVSOCK - the socket is in use.
348 * GSOCK_INVADDR - the local address has not been set.
349 * GSOCK_IOERR - low-level error.
351 GSocketError
GSocket_SetServer(GSocket
*sck
)
358 /* must not be in use */
359 if (sck
->m_fd
!= INVALID_SOCKET
)
361 sck
->m_error
= GSOCK_INVSOCK
;
362 return GSOCK_INVSOCK
;
365 /* the local addr must have been set */
368 sck
->m_error
= GSOCK_INVADDR
;
369 return GSOCK_INVADDR
;
372 /* Initialize all fields */
373 sck
->m_stream
= TRUE
;
374 sck
->m_server
= TRUE
;
375 sck
->m_oriented
= TRUE
;
377 /* Create the socket */
378 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
380 if (sck
->m_fd
== INVALID_SOCKET
)
382 sck
->m_error
= GSOCK_IOERR
;
386 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
387 _GSocket_Enable_Events(sck
);
389 /* Bind to the local address,
390 * retrieve the actual address bound,
391 * and listen up to 5 connections.
393 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
394 (getsockname(sck
->m_fd
,
395 sck
->m_local
->m_addr
,
396 (SOCKLEN_T
*) &sck
->m_local
->m_len
) != 0) ||
397 (listen(sck
->m_fd
, 5) != 0))
400 sck
->m_fd
= INVALID_SOCKET
;
401 sck
->m_error
= GSOCK_IOERR
;
405 return GSOCK_NOERROR
;
408 /* GSocket_WaitConnection:
409 * Waits for an incoming client connection. Returns a pointer to
410 * a GSocket object, or NULL if there was an error, in which case
411 * the last error field will be updated for the calling GSocket.
413 * Error codes (set in the calling GSocket)
414 * GSOCK_INVSOCK - the socket is not valid or not a server.
415 * GSOCK_TIMEDOUT - timeout, no incoming connections.
416 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
417 * GSOCK_MEMERR - couldn't allocate memory.
418 * GSOCK_IOERR - low-level error.
420 GSocket
*GSocket_WaitConnection(GSocket
*socket
)
422 struct sockaddr from
;
423 SOCKLEN_T fromlen
= sizeof(from
);
428 assert(socket
!= NULL
);
430 /* Reenable CONNECTION events */
431 _GSocket_Enable(socket
, GSOCK_CONNECTION
);
433 /* If the socket has already been created, we exit immediately */
434 if (socket
->m_fd
== INVALID_SOCKET
|| !socket
->m_server
)
436 socket
->m_error
= GSOCK_INVSOCK
;
440 /* Create a GSocket object for the new connection */
441 connection
= GSocket_new();
445 socket
->m_error
= GSOCK_MEMERR
;
449 /* Wait for a connection (with timeout) */
450 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
452 GSocket_destroy(connection
);
453 /* socket->m_error set by _GSocket_Input_Timeout */
457 connection
->m_fd
= accept(socket
->m_fd
, &from
, (SOCKLEN_T
*) &fromlen
);
459 if (connection
->m_fd
== INVALID_SOCKET
)
461 if (errno
== EWOULDBLOCK
)
462 socket
->m_error
= GSOCK_WOULDBLOCK
;
464 socket
->m_error
= GSOCK_IOERR
;
466 GSocket_destroy(connection
);
470 /* Initialize all fields */
471 connection
->m_server
= FALSE
;
472 connection
->m_stream
= TRUE
;
473 connection
->m_oriented
= TRUE
;
475 /* Setup the peer address field */
476 connection
->m_peer
= GAddress_new();
477 if (!connection
->m_peer
)
479 GSocket_destroy(connection
);
480 socket
->m_error
= GSOCK_MEMERR
;
483 err
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
);
484 if (err
!= GSOCK_NOERROR
)
486 GAddress_destroy(connection
->m_peer
);
487 GSocket_destroy(connection
);
488 socket
->m_error
= err
;
492 ioctl(connection
->m_fd
, FIONBIO
, &arg
);
493 _GSocket_Enable_Events(connection
);
498 /* Client specific parts */
501 * For stream (connection oriented) sockets, GSocket_Connect() tries
502 * to establish a client connection to a server using the peer address
503 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
504 * connection has been succesfully established, or one of the error
505 * codes listed below. Note that for nonblocking sockets, a return
506 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
507 * request can be completed later; you should use GSocket_Select()
508 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
509 * corresponding asynchronous events.
511 * For datagram (non connection oriented) sockets, GSocket_Connect()
512 * just sets the peer address established with GSocket_SetPeer() as
513 * default destination.
516 * GSOCK_INVSOCK - the socket is in use or not valid.
517 * GSOCK_INVADDR - the peer address has not been established.
518 * GSOCK_TIMEDOUT - timeout, the connection failed.
519 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
520 * GSOCK_MEMERR - couldn't allocate memory.
521 * GSOCK_IOERR - low-level error.
523 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
530 /* Enable CONNECTION events (needed for nonblocking connections) */
531 _GSocket_Enable(sck
, GSOCK_CONNECTION
);
533 if (sck
->m_fd
!= INVALID_SOCKET
)
535 sck
->m_error
= GSOCK_INVSOCK
;
536 return GSOCK_INVSOCK
;
541 sck
->m_error
= GSOCK_INVADDR
;
542 return GSOCK_INVADDR
;
545 /* Streamed or dgram socket? */
546 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
547 sck
->m_oriented
= TRUE
;
548 sck
->m_server
= FALSE
;
549 sck
->m_establishing
= FALSE
;
551 /* Create the socket */
552 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
,
553 sck
->m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
555 if (sck
->m_fd
== INVALID_SOCKET
)
557 sck
->m_error
= GSOCK_IOERR
;
561 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
562 _GSocket_Enable_Events(sck
);
564 /* Connect it to the peer address, with a timeout (see below) */
565 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
571 /* If connect failed with EINPROGRESS and the GSocket object
572 * is in blocking mode, we select() for the specified timeout
573 * checking for writability to see if the connection request
576 if ((err
== EINPROGRESS
) && (!sck
->m_non_blocking
))
578 if (_GSocket_Output_Timeout(sck
) == GSOCK_TIMEDOUT
)
581 sck
->m_fd
= INVALID_SOCKET
;
582 /* sck->m_error is set in _GSocket_Output_Timeout */
583 return GSOCK_TIMEDOUT
;
588 SOCKLEN_T len
= sizeof(error
);
590 getsockopt(sck
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*) &error
, &len
);
593 return GSOCK_NOERROR
;
597 /* If connect failed with EINPROGRESS and the GSocket object
598 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
599 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
600 * this way if the connection completes, a GSOCK_CONNECTION
601 * event will be generated, if enabled.
603 if ((err
== EINPROGRESS
) && (sck
->m_non_blocking
))
605 sck
->m_establishing
= TRUE
;
606 sck
->m_error
= GSOCK_WOULDBLOCK
;
607 return GSOCK_WOULDBLOCK
;
610 /* If connect failed with an error other than EINPROGRESS,
611 * then the call to GSocket_Connect has failed.
614 sck
->m_fd
= INVALID_SOCKET
;
615 sck
->m_error
= GSOCK_IOERR
;
619 return GSOCK_NOERROR
;
622 /* Datagram sockets */
624 /* GSocket_SetNonOriented:
625 * Sets up this socket as a non-connection oriented (datagram) socket.
626 * Before using this function, the local address must have been set
627 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
628 * on success, or one of the following otherwise.
631 * GSOCK_INVSOCK - the socket is in use.
632 * GSOCK_INVADDR - the local address has not been set.
633 * GSOCK_IOERR - low-level error.
635 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
641 if (sck
->m_fd
!= INVALID_SOCKET
)
643 sck
->m_error
= GSOCK_INVSOCK
;
644 return GSOCK_INVSOCK
;
649 sck
->m_error
= GSOCK_INVADDR
;
650 return GSOCK_INVADDR
;
653 /* Initialize all fields */
654 sck
->m_stream
= FALSE
;
655 sck
->m_server
= FALSE
;
656 sck
->m_oriented
= FALSE
;
658 /* Create the socket */
659 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
661 if (sck
->m_fd
== INVALID_SOCKET
)
663 sck
->m_error
= GSOCK_IOERR
;
667 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
668 _GSocket_Enable_Events(sck
);
670 /* Bind to the local address,
671 * and retrieve the actual address bound.
673 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
674 (getsockname(sck
->m_fd
,
675 sck
->m_local
->m_addr
,
676 (SOCKLEN_T
*) &sck
->m_local
->m_len
) != 0))
679 sck
->m_fd
= INVALID_SOCKET
;
680 sck
->m_error
= GSOCK_IOERR
;
684 return GSOCK_NOERROR
;
689 /* Like recv(), send(), ... */
690 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
694 assert(socket
!= NULL
);
696 /* Reenable INPUT events */
697 _GSocket_Enable(socket
, GSOCK_INPUT
);
699 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
701 socket
->m_error
= GSOCK_INVSOCK
;
705 /* If the socket is blocking, wait for data (with a timeout) */
706 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
710 if (socket
->m_stream
)
711 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
713 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
717 if (errno
== EWOULDBLOCK
)
718 socket
->m_error
= GSOCK_WOULDBLOCK
;
720 socket
->m_error
= GSOCK_IOERR
;
726 int GSocket_Write(GSocket
*socket
, const char *buffer
, int size
)
730 assert(socket
!= NULL
);
732 GSocket_Debug(( "GSocket_Write #1, size %d\n", size
));
734 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
736 socket
->m_error
= GSOCK_INVSOCK
;
740 GSocket_Debug(( "GSocket_Write #2, size %d\n", size
));
742 /* If the socket is blocking, wait for writability (with a timeout) */
743 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
746 GSocket_Debug(( "GSocket_Write #3, size %d\n", size
));
749 if (socket
->m_stream
)
750 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
752 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
754 GSocket_Debug(( "GSocket_Write #4, size %d\n", size
));
758 if (errno
== EWOULDBLOCK
)
760 socket
->m_error
= GSOCK_WOULDBLOCK
;
761 GSocket_Debug(( "GSocket_Write error WOULDBLOCK\n" ));
765 socket
->m_error
= GSOCK_IOERR
;
766 GSocket_Debug(( "GSocket_Write error IOERR\n" ));
769 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
770 * in MSW). Once the first OUTPUT event is received, users can assume
771 * that the socket is writable until a read operation fails. Only then
772 * will further OUTPUT events be posted.
774 _GSocket_Enable(socket
, GSOCK_OUTPUT
);
778 GSocket_Debug(( "GSocket_Write #5, size %d ret %d\n", size
, ret
));
784 * Polls the socket to determine its status. This function will
785 * check for the events specified in the 'flags' parameter, and
786 * it will return a mask indicating which operations can be
787 * performed. This function won't block, regardless of the
788 * mode (blocking | nonblocking) of the socket.
790 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
792 #if defined(wxUSE_GUI) && !wxUSE_GUI
794 GSocketEventFlags result
= 0;
800 /* Do not use a static struct, Linux can garble it */
804 assert(socket
!= NULL
);
809 FD_SET(socket
->m_fd
, &readfds
);
810 FD_SET(socket
->m_fd
, &writefds
);
811 FD_SET(socket
->m_fd
, &exceptfds
);
813 /* Check known state first */
814 result
|= (GSOCK_CONNECTION_FLAG
& socket
->m_detected
& flags
);
815 result
|= (GSOCK_LOST_FLAG
& socket
->m_detected
& flags
);
818 if (select(socket
->m_fd
+ 1, &readfds
, &writefds
, &exceptfds
, &tv
) <= 0)
821 /* Check for readability */
822 if (FD_ISSET(socket
->m_fd
, &readfds
))
826 if (recv(socket
->m_fd
, &c
, 1, MSG_PEEK
) > 0)
828 result
|= (GSOCK_INPUT_FLAG
& flags
);
832 if (socket
->m_server
&& socket
->m_stream
)
834 result
|= (GSOCK_CONNECTION_FLAG
& flags
);
835 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
839 result
|= (GSOCK_LOST_FLAG
& flags
);
840 socket
->m_detected
= GSOCK_LOST_FLAG
;
845 /* Check for writability */
846 if (FD_ISSET(socket
->m_fd
, &writefds
))
848 if (socket
->m_establishing
&& !socket
->m_server
)
851 SOCKLEN_T len
= sizeof(error
);
853 socket
->m_establishing
= FALSE
;
855 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*)&error
, &len
);
859 result
|= (GSOCK_LOST_FLAG
& flags
);
860 socket
->m_detected
= GSOCK_LOST_FLAG
;
864 result
|= (GSOCK_CONNECTION_FLAG
& flags
);
865 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
870 result
|= (GSOCK_OUTPUT_FLAG
& flags
);
874 /* Check for exceptions and errors (is this useful in Unices?) */
875 if (FD_ISSET(socket
->m_fd
, &exceptfds
))
877 result
|= (GSOCK_LOST_FLAG
& flags
);
878 socket
->m_establishing
= FALSE
;
879 socket
->m_detected
= GSOCK_LOST_FLAG
;
886 assert(socket
!= NULL
);
887 return flags
& socket
->m_detected
;
889 #endif /* !wxUSE_GUI */
894 /* GSocket_SetNonBlocking:
895 * Sets the socket to non-blocking mode. All IO calls will return
898 void GSocket_SetNonBlocking(GSocket
*socket
, bool non_block
)
900 assert(socket
!= NULL
);
902 GSocket_Debug( ("GSocket_SetNonBlocking: %d\n", (int)non_block
) );
904 socket
->m_non_blocking
= non_block
;
907 /* GSocket_SetTimeout:
908 * Sets the timeout for blocking calls. Time is expressed in
911 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
)
913 assert(socket
!= NULL
);
915 socket
->m_timeout
= millisec
;
919 * Returns the last error occured for this socket. Note that successful
920 * operations do not clear this back to GSOCK_NOERROR, so use it only
923 GSocketError
GSocket_GetError(GSocket
*socket
)
925 assert(socket
!= NULL
);
927 return socket
->m_error
;
933 * There is data to be read in the input buffer. If, after a read
934 * operation, there is still data available, the callback function will
937 * The socket is available for writing. That is, the next write call
938 * won't block. This event is generated only once, when the connection is
939 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
940 * when the output buffer empties again. This means that the app should
941 * assume that it can write since the first OUTPUT event, and no more
942 * OUTPUT events will be generated unless an error occurs.
944 * Connection succesfully established, for client sockets, or incoming
945 * client connection, for server sockets. Wait for this event (also watch
946 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
948 * The connection is lost (or a connection request failed); this could
949 * be due to a failure, or due to the peer closing it gracefully.
952 /* GSocket_SetCallback:
953 * Enables the callbacks specified by 'flags'. Note that 'flags'
954 * may be a combination of flags OR'ed toghether, so the same
955 * callback function can be made to accept different events.
956 * The callback function must have the following prototype:
958 * void function(GSocket *socket, GSocketEvent event, char *cdata)
960 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
961 GSocketCallback callback
, char *cdata
)
965 assert(socket
!= NULL
);
967 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
969 if ((flags
& (1 << count
)) != 0)
971 socket
->m_cbacks
[count
] = callback
;
972 socket
->m_data
[count
] = cdata
;
977 /* GSocket_UnsetCallback:
978 * Disables all callbacks specified by 'flags', which may be a
979 * combination of flags OR'ed toghether.
981 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
985 assert(socket
!= NULL
);
987 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
989 if ((flags
& (1 << count
)) != 0)
991 socket
->m_cbacks
[count
] = NULL
;
992 socket
->m_data
[count
] = NULL
;
998 #define CALL_CALLBACK(socket, event) { \
999 _GSocket_Disable(socket, event); \
1000 if (socket->m_cbacks[event]) \
1001 socket->m_cbacks[event](socket, event, socket->m_data[event]); \
1005 void _GSocket_Enable(GSocket
*socket
, GSocketEvent event
)
1007 socket
->m_detected
&= ~(1 << event
);
1008 _GSocket_Install_Callback(socket
, event
);
1011 void _GSocket_Disable(GSocket
*socket
, GSocketEvent event
)
1013 socket
->m_detected
|= (1 << event
);
1014 _GSocket_Uninstall_Callback(socket
, event
);
1017 /* _GSocket_Input_Timeout:
1018 * For blocking sockets, wait until data is available or
1019 * until timeout ellapses.
1021 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
1027 /* Linux select() will overwrite the struct on return */
1028 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
1029 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
1031 if (!socket
->m_non_blocking
)
1034 FD_SET(socket
->m_fd
, &readfds
);
1035 ret
= select(socket
->m_fd
+ 1, &readfds
, NULL
, NULL
, &tv
);
1038 GSocket_Debug(( "GSocket_Input_Timeout, select returned 0\n" ));
1039 socket
->m_error
= GSOCK_TIMEDOUT
;
1040 return GSOCK_TIMEDOUT
;
1044 GSocket_Debug(( "GSocket_Input_Timeout, select returned -1\n" ));
1045 if (errno
== EBADF
) GSocket_Debug(( "Invalid file descriptor\n" ));
1046 if (errno
== EINTR
) GSocket_Debug(( "A non blocked signal was caught\n" ));
1047 if (errno
== EINVAL
) GSocket_Debug(( "The highest number descriptor is negative\n" ));
1048 if (errno
== ENOMEM
) GSocket_Debug(( "Not enough memory\n" ));
1049 socket
->m_error
= GSOCK_TIMEDOUT
;
1050 return GSOCK_TIMEDOUT
;
1053 return GSOCK_NOERROR
;
1056 /* _GSocket_Output_Timeout:
1057 * For blocking sockets, wait until data can be sent without
1058 * blocking or until timeout ellapses.
1060 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
1066 /* Linux select() will overwrite the struct on return */
1067 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
1068 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
1070 GSocket_Debug( ("m_non_blocking has: %d\n", (int)socket
->m_non_blocking
) );
1072 if (!socket
->m_non_blocking
)
1075 FD_SET(socket
->m_fd
, &writefds
);
1076 ret
= select(socket
->m_fd
+ 1, NULL
, &writefds
, NULL
, &tv
);
1079 GSocket_Debug(( "GSocket_Output_Timeout, select returned 0\n" ));
1080 socket
->m_error
= GSOCK_TIMEDOUT
;
1081 return GSOCK_TIMEDOUT
;
1085 GSocket_Debug(( "GSocket_Output_Timeout, select returned -1\n" ));
1086 if (errno
== EBADF
) GSocket_Debug(( "Invalid file descriptor\n" ));
1087 if (errno
== EINTR
) GSocket_Debug(( "A non blocked signal was caught\n" ));
1088 if (errno
== EINVAL
) GSocket_Debug(( "The highest number descriptor is negative\n" ));
1089 if (errno
== ENOMEM
) GSocket_Debug(( "Not enough memory\n" ));
1090 socket
->m_error
= GSOCK_TIMEDOUT
;
1091 return GSOCK_TIMEDOUT
;
1093 if ( ! FD_ISSET(socket
->m_fd
, &writefds
) )
1094 GSocket_Debug(( "GSocket_Output_Timeout is buggy!\n" ));
1096 GSocket_Debug(( "GSocket_Output_Timeout seems correct\n" ));
1100 GSocket_Debug(( "GSocket_Output_Timeout, didn't try select!\n" ));
1103 return GSOCK_NOERROR
;
1106 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
1108 return recv(socket
->m_fd
, buffer
, size
, 0);
1111 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
1113 struct sockaddr from
;
1114 SOCKLEN_T fromlen
= sizeof(from
);
1118 fromlen
= sizeof(from
);
1120 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, (SOCKLEN_T
*) &fromlen
);
1125 /* Translate a system address into a GSocket address */
1126 if (!socket
->m_peer
)
1128 socket
->m_peer
= GAddress_new();
1129 if (!socket
->m_peer
)
1131 socket
->m_error
= GSOCK_MEMERR
;
1135 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
1136 if (err
!= GSOCK_NOERROR
)
1138 GAddress_destroy(socket
->m_peer
);
1139 socket
->m_peer
= NULL
;
1140 socket
->m_error
= err
;
1147 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
1152 ret
= send(socket
->m_fd
, buffer
, size
, 0);
1158 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
1160 struct sockaddr
*addr
;
1164 if (!socket
->m_peer
)
1166 socket
->m_error
= GSOCK_INVADDR
;
1170 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
1171 if (err
!= GSOCK_NOERROR
)
1173 socket
->m_error
= err
;
1178 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
1181 /* Frees memory allocated from _GAddress_translate_to */
1187 void _GSocket_Detected_Read(GSocket
*socket
)
1191 if (recv(socket
->m_fd
, &c
, 1, MSG_PEEK
) > 0)
1193 CALL_CALLBACK(socket
, GSOCK_INPUT
);
1197 if (socket
->m_server
&& socket
->m_stream
)
1199 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
1203 CALL_CALLBACK(socket
, GSOCK_LOST
);
1208 void _GSocket_Detected_Write(GSocket
*socket
)
1210 if (socket
->m_establishing
&& !socket
->m_server
)
1213 SOCKLEN_T len
= sizeof(error
);
1215 socket
->m_establishing
= FALSE
;
1217 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*)&error
, &len
);
1221 CALL_CALLBACK(socket
, GSOCK_LOST
);
1225 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
1226 /* We have to fire this event by hand because CONNECTION (for clients)
1227 * and OUTPUT are internally the same and we just disabled CONNECTION
1228 * events with the above macro.
1230 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
1235 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
1240 * -------------------------------------------------------------------------
1242 * -------------------------------------------------------------------------
1245 /* CHECK_ADDRESS verifies that the current family is either GSOCK_NOFAMILY
1246 * or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it initalizes address
1247 * to be a GSOCK_*family*. In other cases, it returns GSOCK_INVADDR.
1249 #define CHECK_ADDRESS(address, family, retval) \
1251 if (address->m_family == GSOCK_NOFAMILY) \
1252 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1253 return address->m_error; \
1254 if (address->m_family != GSOCK_##family) \
1256 address->m_error = GSOCK_INVADDR; \
1261 GAddress
*GAddress_new(void)
1265 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1268 address
->m_family
= GSOCK_NOFAMILY
;
1269 address
->m_addr
= NULL
;
1275 GAddress
*GAddress_copy(GAddress
*address
)
1279 assert(address
!= NULL
);
1281 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1284 memcpy(addr2
, address
, sizeof(GAddress
));
1286 if (address
->m_addr
)
1288 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
1289 if (addr2
->m_addr
== NULL
)
1294 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1300 void GAddress_destroy(GAddress
*address
)
1302 assert(address
!= NULL
);
1304 if (address
->m_addr
)
1305 free(address
->m_addr
);
1310 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1312 assert(address
!= NULL
);
1314 address
->m_family
= type
;
1317 GAddressType
GAddress_GetFamily(GAddress
*address
)
1319 assert(address
!= NULL
);
1321 return address
->m_family
;
1324 GSocketError
_GAddress_translate_from(GAddress
*address
,
1325 struct sockaddr
*addr
, int len
)
1327 address
->m_realfamily
= addr
->sa_family
;
1328 switch (addr
->sa_family
)
1331 address
->m_family
= GSOCK_INET
;
1334 address
->m_family
= GSOCK_UNIX
;
1338 address
->m_family
= GSOCK_INET6
;
1343 address
->m_error
= GSOCK_INVOP
;
1348 if (address
->m_addr
)
1349 free(address
->m_addr
);
1351 address
->m_len
= len
;
1352 address
->m_addr
= (struct sockaddr
*)malloc(len
);
1354 if (address
->m_addr
== NULL
)
1356 address
->m_error
= GSOCK_MEMERR
;
1357 return GSOCK_MEMERR
;
1359 memcpy(address
->m_addr
, addr
, len
);
1361 return GSOCK_NOERROR
;
1364 GSocketError
_GAddress_translate_to(GAddress
*address
,
1365 struct sockaddr
**addr
, int *len
)
1367 if (!address
->m_addr
)
1369 address
->m_error
= GSOCK_INVADDR
;
1370 return GSOCK_INVADDR
;
1373 *len
= address
->m_len
;
1374 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
1375 if (*addr
== NULL
) {
1376 address
->m_error
= GSOCK_MEMERR
;
1377 return GSOCK_MEMERR
;
1380 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1381 return GSOCK_NOERROR
;
1385 * -------------------------------------------------------------------------
1386 * Internet address family
1387 * -------------------------------------------------------------------------
1390 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1392 address
->m_len
= sizeof(struct sockaddr_in
);
1393 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1394 if (address
->m_addr
== NULL
)
1396 address
->m_error
= GSOCK_MEMERR
;
1397 return GSOCK_MEMERR
;
1400 address
->m_family
= GSOCK_INET
;
1401 address
->m_realfamily
= PF_INET
;
1402 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1403 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1405 return GSOCK_NOERROR
;
1408 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1411 struct in_addr
*addr
;
1413 assert(address
!= NULL
);
1415 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1417 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1419 /* If it is a numeric host name, convert it now */
1420 #if defined(HAVE_INET_ATON)
1421 if (inet_aton(hostname
, addr
) == 0)
1423 #elif defined(HAVE_INET_ADDR)
1424 if ( (addr
->s_addr
= inet_addr(hostname
)) == -1 )
1427 /* Use gethostbyname by default */
1431 struct in_addr
*array_addr
;
1433 /* It is a real name, we solve it */
1434 if ((he
= gethostbyname(hostname
)) == NULL
)
1436 /* Reset to invalid address */
1437 addr
->s_addr
= INADDR_NONE
;
1438 address
->m_error
= GSOCK_NOHOST
;
1439 return GSOCK_NOHOST
;
1441 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1442 addr
->s_addr
= array_addr
[0].s_addr
;
1444 return GSOCK_NOERROR
;
1447 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1449 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1452 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1453 unsigned long hostaddr
)
1455 struct in_addr
*addr
;
1457 assert(address
!= NULL
);
1459 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1461 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1462 addr
->s_addr
= hostaddr
;
1464 return GSOCK_NOERROR
;
1467 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1468 const char *protocol
)
1471 struct sockaddr_in
*addr
;
1473 assert(address
!= NULL
);
1474 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1478 address
->m_error
= GSOCK_INVPORT
;
1479 return GSOCK_INVPORT
;
1482 se
= getservbyname(port
, protocol
);
1485 if (isdigit(port
[0]))
1489 port_int
= atoi(port
);
1490 addr
= (struct sockaddr_in
*)address
->m_addr
;
1491 addr
->sin_port
= htons(port_int
);
1492 return GSOCK_NOERROR
;
1495 address
->m_error
= GSOCK_INVPORT
;
1496 return GSOCK_INVPORT
;
1499 addr
= (struct sockaddr_in
*)address
->m_addr
;
1500 addr
->sin_port
= se
->s_port
;
1502 return GSOCK_NOERROR
;
1505 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1507 struct sockaddr_in
*addr
;
1509 assert(address
!= NULL
);
1510 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1512 addr
= (struct sockaddr_in
*)address
->m_addr
;
1513 addr
->sin_port
= htons(port
);
1515 return GSOCK_NOERROR
;
1518 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1522 struct sockaddr_in
*addr
;
1524 assert(address
!= NULL
);
1525 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1527 addr
= (struct sockaddr_in
*)address
->m_addr
;
1528 addr_buf
= (char *)&(addr
->sin_addr
);
1530 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1533 address
->m_error
= GSOCK_NOHOST
;
1534 return GSOCK_NOHOST
;
1537 strncpy(hostname
, he
->h_name
, sbuf
);
1539 return GSOCK_NOERROR
;
1542 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1544 struct sockaddr_in
*addr
;
1546 assert(address
!= NULL
);
1547 CHECK_ADDRESS(address
, INET
, 0);
1549 addr
= (struct sockaddr_in
*)address
->m_addr
;
1551 return addr
->sin_addr
.s_addr
;
1554 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1556 struct sockaddr_in
*addr
;
1558 assert(address
!= NULL
);
1559 CHECK_ADDRESS(address
, INET
, 0);
1561 addr
= (struct sockaddr_in
*)address
->m_addr
;
1562 return ntohs(addr
->sin_port
);
1566 * -------------------------------------------------------------------------
1567 * Unix address family
1568 * -------------------------------------------------------------------------
1571 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1573 address
->m_len
= sizeof(struct sockaddr_un
);
1574 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
1575 if (address
->m_addr
== NULL
)
1577 address
->m_error
= GSOCK_MEMERR
;
1578 return GSOCK_MEMERR
;
1581 address
->m_family
= GSOCK_UNIX
;
1582 address
->m_realfamily
= PF_UNIX
;
1583 ((struct sockaddr_un
*)address
->m_addr
)->sun_family
= AF_UNIX
;
1584 ((struct sockaddr_un
*)address
->m_addr
)->sun_path
[0] = 0;
1589 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1591 struct sockaddr_un
*addr
;
1593 assert(address
!= NULL
);
1595 CHECK_ADDRESS(address
, UNIX
, GSOCK_INVADDR
);
1597 addr
= ((struct sockaddr_un
*)address
->m_addr
);
1598 memcpy(addr
->sun_path
, path
, strlen(path
));
1600 return GSOCK_NOERROR
;
1603 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1605 struct sockaddr_un
*addr
;
1607 assert(address
!= NULL
);
1608 CHECK_ADDRESS(address
, UNIX
, GSOCK_INVADDR
);
1610 addr
= (struct sockaddr_un
*)address
->m_addr
;
1612 strncpy(path
, addr
->sun_path
, sbuf
);
1614 return GSOCK_NOERROR
;
1617 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */