]>
git.saurik.com Git - wxWidgets.git/blob - src/os2/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-style file
8 * -------------------------------------------------------------------------
12 * PLEASE don't put C++ comments here - this is a C source file.
15 #ifndef __GSOCKET_STANDALONE__
20 /* I don't see, why this include is needed, but it seems to be necessary
21 sometimes. For EMX, including C++ headers into plain C source breaks
22 compilation, so don't do it there. */
26 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
28 #define BSD_SELECT /* use Berkley Sockets select */
31 #include <sys/types.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
41 #define HAVE_INET_ADDR
49 #include <netinet/in.h>
55 #if defined(__VISAGECPP__) && __IBMCPP__ < 400
57 #include <machine/endian.h>
63 #define EBADF SOCEBADF
67 #include <sys/socket.h>
68 #include <sys/ioctl.h>
69 #include <sys/select.h>
72 #define soclose(a) close(a)
74 #define select(a,b,c,d,e) bsdselect(a,b,c,d,e)
75 int _System
bsdselect(int,
80 int _System
soclose(int);
85 #if (defined(__VISAGECPP__) && __IBMCPP__ < 400) || defined(__EMX__)
101 # define SOCKLEN_T socklen_t
104 # define SOCKLEN_T int
110 * MSW defines this, Unices don't.
112 #ifndef INVALID_SOCKET
113 #define INVALID_SOCKET -1
117 * INADDR_BROADCAST is identical to INADDR_NONE which is not defined
118 * on all systems. INADDR_BROADCAST should be fine to indicate an error.
121 #define INADDR_NONE INADDR_BROADCAST
124 #define MASK_SIGNAL() \
126 void (*old_handler)(int); \
128 old_handler = signal(SIGPIPE, SIG_IGN);
130 #define UNMASK_SIGNAL() \
131 signal(SIGPIPE, old_handler); \
135 #ifndef __GSOCKET_STANDALONE__
136 # include "wx/unix/gsockunx.h"
137 # include "wx/gsocket.h"
139 # include "gsockunx.h"
140 # include "gsocket.h"
141 #endif /* __GSOCKET_STANDALONE__ */
143 /* redefine some GUI-only functions to do nothing in console mode */
144 #if defined(wxUSE_GUI) && !wxUSE_GUI
145 # define _GSocket_GUI_Init(socket) (1)
146 # define _GSocket_GUI_Destroy(socket)
147 # define _GSocket_Enable_Events(socket)
148 # define _GSocket_Disable_Events(socket)
149 # define _GSocket_Install_Callback(socket, event)
150 # define _GSocket_Uninstall_Callback(socket, event)
151 #endif /* wxUSE_GUI */
153 /* debugging helpers */
154 #ifdef __GSOCKET_DEBUG__
155 # define GSocket_Debug(args) printf args
157 # define GSocket_Debug(args)
158 #endif /* __GSOCKET_DEBUG__ */
160 /* Global initialisers */
162 int GSocket_Init(void)
167 void GSocket_Cleanup(void)
171 /* Constructors / Destructors for GSocket */
173 GSocket
*GSocket_new(void)
178 socket
= (GSocket
*)malloc(sizeof(GSocket
));
183 socket
->m_fd
= INVALID_SOCKET
;
184 for (i
=0;i
<GSOCK_MAX_EVENT
;i
++)
186 socket
->m_cbacks
[i
] = NULL
;
188 socket
->m_detected
= 0;
189 socket
->m_local
= NULL
;
190 socket
->m_peer
= NULL
;
191 socket
->m_error
= GSOCK_NOERROR
;
192 socket
->m_server
= FALSE
;
193 socket
->m_stream
= TRUE
;
194 socket
->m_gui_dependent
= NULL
;
195 socket
->m_non_blocking
= FALSE
;
196 socket
->m_timeout
= 10*60*1000;
197 /* 10 minutes * 60 sec * 1000 millisec */
198 socket
->m_establishing
= FALSE
;
200 /* Per-socket GUI-specific initialization */
201 success
= _GSocket_GUI_Init(socket
);
211 void GSocket_close(GSocket
*socket
)
213 _GSocket_Disable_Events(socket
);
214 soclose(socket
->m_fd
);
215 socket
->m_fd
= INVALID_SOCKET
;
218 void GSocket_destroy(GSocket
*socket
)
220 assert(socket
!= NULL
);
222 /* Check that the socket is really shutdowned */
223 if (socket
->m_fd
!= INVALID_SOCKET
)
224 GSocket_Shutdown(socket
);
226 /* Per-socket GUI-specific cleanup */
227 _GSocket_GUI_Destroy(socket
);
229 /* Destroy private addresses */
231 GAddress_destroy(socket
->m_local
);
234 GAddress_destroy(socket
->m_peer
);
236 /* Destroy the socket itself */
241 * Disallow further read/write operations on this socket, close
242 * the fd and disable all callbacks.
244 void GSocket_Shutdown(GSocket
*socket
)
248 assert(socket
!= NULL
);
250 /* If socket has been created, shutdown it */
251 if (socket
->m_fd
!= INVALID_SOCKET
)
253 shutdown(socket
->m_fd
, 2);
254 GSocket_close(socket
);
257 /* Disable GUI callbacks */
258 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
259 socket
->m_cbacks
[evt
] = NULL
;
261 socket
->m_detected
= GSOCK_LOST_FLAG
;
262 _GSocket_Disable_Events(socket
);
265 /* Address handling */
271 * Set or get the local or peer address for this socket. The 'set'
272 * functions return GSOCK_NOERROR on success, an error code otherwise.
273 * The 'get' functions return a pointer to a GAddress object on success,
274 * or NULL otherwise, in which case they set the error code of the
275 * corresponding GSocket.
278 * GSOCK_INVSOCK - the socket is not valid.
279 * GSOCK_INVADDR - the address is not valid.
281 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
283 assert(socket
!= NULL
);
285 /* the socket must be initialized, or it must be a server */
286 if ((socket
->m_fd
!= INVALID_SOCKET
&& !socket
->m_server
))
288 socket
->m_error
= GSOCK_INVSOCK
;
289 return GSOCK_INVSOCK
;
293 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
295 socket
->m_error
= GSOCK_INVADDR
;
296 return GSOCK_INVADDR
;
300 GAddress_destroy(socket
->m_local
);
302 socket
->m_local
= GAddress_copy(address
);
304 return GSOCK_NOERROR
;
307 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
309 assert(socket
!= NULL
);
312 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
314 socket
->m_error
= GSOCK_INVADDR
;
315 return GSOCK_INVADDR
;
319 GAddress_destroy(socket
->m_peer
);
321 socket
->m_peer
= GAddress_copy(address
);
323 return GSOCK_NOERROR
;
326 GAddress
*GSocket_GetLocal(GSocket
*socket
)
329 struct sockaddr addr
;
330 SOCKLEN_T size
= sizeof(addr
);
333 assert(socket
!= NULL
);
335 /* try to get it from the m_local var first */
337 return GAddress_copy(socket
->m_local
);
339 /* else, if the socket is initialized, try getsockname */
340 if (socket
->m_fd
== INVALID_SOCKET
)
342 socket
->m_error
= GSOCK_INVSOCK
;
346 if (getsockname(socket
->m_fd
, &addr
, (SOCKLEN_T
*) &size
) < 0)
348 socket
->m_error
= GSOCK_IOERR
;
352 /* got a valid address from getsockname, create a GAddress object */
353 address
= GAddress_new();
356 socket
->m_error
= GSOCK_MEMERR
;
360 err
= _GAddress_translate_from(address
, &addr
, size
);
361 if (err
!= GSOCK_NOERROR
)
363 GAddress_destroy(address
);
364 socket
->m_error
= err
;
371 GAddress
*GSocket_GetPeer(GSocket
*socket
)
373 assert(socket
!= NULL
);
375 /* try to get it from the m_peer var */
377 return GAddress_copy(socket
->m_peer
);
382 /* Server specific parts */
384 /* GSocket_SetServer:
385 * Sets up this socket as a server. The local address must have been
386 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
387 * Returns GSOCK_NOERROR on success, one of the following otherwise:
390 * GSOCK_INVSOCK - the socket is in use.
391 * GSOCK_INVADDR - the local address has not been set.
392 * GSOCK_IOERR - low-level error.
394 GSocketError
GSocket_SetServer(GSocket
*sck
)
400 /* must not be in use */
401 if (sck
->m_fd
!= INVALID_SOCKET
)
403 sck
->m_error
= GSOCK_INVSOCK
;
404 return GSOCK_INVSOCK
;
407 /* the local addr must have been set */
410 sck
->m_error
= GSOCK_INVADDR
;
411 return GSOCK_INVADDR
;
414 /* Initialize all fields */
415 sck
->m_stream
= TRUE
;
416 sck
->m_server
= TRUE
;
417 sck
->m_oriented
= TRUE
;
419 /* Create the socket */
420 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
422 if (sck
->m_fd
== INVALID_SOCKET
)
424 sck
->m_error
= GSOCK_IOERR
;
428 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
429 _GSocket_Enable_Events(sck
);
431 /* Bind to the local address,
432 * retrieve the actual address bound,
433 * and listen up to 5 connections.
435 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
436 (getsockname(sck
->m_fd
,
437 sck
->m_local
->m_addr
,
438 (SOCKLEN_T
*) &sck
->m_local
->m_len
) != 0) ||
439 (listen(sck
->m_fd
, 5) != 0))
442 sck
->m_error
= GSOCK_IOERR
;
446 return GSOCK_NOERROR
;
449 /* GSocket_WaitConnection:
450 * Waits for an incoming client connection. Returns a pointer to
451 * a GSocket object, or NULL if there was an error, in which case
452 * the last error field will be updated for the calling GSocket.
454 * Error codes (set in the calling GSocket)
455 * GSOCK_INVSOCK - the socket is not valid or not a server.
456 * GSOCK_TIMEDOUT - timeout, no incoming connections.
457 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
458 * GSOCK_MEMERR - couldn't allocate memory.
459 * GSOCK_IOERR - low-level error.
461 GSocket
*GSocket_WaitConnection(GSocket
*socket
)
463 struct sockaddr from
;
464 SOCKLEN_T fromlen
= sizeof(from
);
469 assert(socket
!= NULL
);
471 /* Reenable CONNECTION events */
472 _GSocket_Enable(socket
, GSOCK_CONNECTION
);
474 /* If the socket has already been created, we exit immediately */
475 if (socket
->m_fd
== INVALID_SOCKET
|| !socket
->m_server
)
477 socket
->m_error
= GSOCK_INVSOCK
;
481 /* Create a GSocket object for the new connection */
482 connection
= GSocket_new();
486 socket
->m_error
= GSOCK_MEMERR
;
490 /* Wait for a connection (with timeout) */
491 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
493 GSocket_destroy(connection
);
494 /* socket->m_error set by _GSocket_Input_Timeout */
498 connection
->m_fd
= accept(socket
->m_fd
, &from
, (SOCKLEN_T
*) &fromlen
);
500 if (connection
->m_fd
== INVALID_SOCKET
)
502 if (errno
== EWOULDBLOCK
)
503 socket
->m_error
= GSOCK_WOULDBLOCK
;
505 socket
->m_error
= GSOCK_IOERR
;
507 GSocket_destroy(connection
);
511 /* Initialize all fields */
512 connection
->m_server
= FALSE
;
513 connection
->m_stream
= TRUE
;
514 connection
->m_oriented
= TRUE
;
516 /* Setup the peer address field */
517 connection
->m_peer
= GAddress_new();
518 if (!connection
->m_peer
)
520 GSocket_destroy(connection
);
521 socket
->m_error
= GSOCK_MEMERR
;
524 err
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
);
525 if (err
!= GSOCK_NOERROR
)
527 GAddress_destroy(connection
->m_peer
);
528 GSocket_destroy(connection
);
529 socket
->m_error
= err
;
533 ioctl(connection
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
534 _GSocket_Enable_Events(connection
);
539 /* Client specific parts */
542 * For stream (connection oriented) sockets, GSocket_Connect() tries
543 * to establish a client connection to a server using the peer address
544 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
545 * connection has been succesfully established, or one of the error
546 * codes listed below. Note that for nonblocking sockets, a return
547 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
548 * request can be completed later; you should use GSocket_Select()
549 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
550 * corresponding asynchronous events.
552 * For datagram (non connection oriented) sockets, GSocket_Connect()
553 * just sets the peer address established with GSocket_SetPeer() as
554 * default destination.
557 * GSOCK_INVSOCK - the socket is in use or not valid.
558 * GSOCK_INVADDR - the peer address has not been established.
559 * GSOCK_TIMEDOUT - timeout, the connection failed.
560 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
561 * GSOCK_MEMERR - couldn't allocate memory.
562 * GSOCK_IOERR - low-level error.
564 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
571 /* Enable CONNECTION events (needed for nonblocking connections) */
572 _GSocket_Enable(sck
, GSOCK_CONNECTION
);
574 if (sck
->m_fd
!= INVALID_SOCKET
)
576 sck
->m_error
= GSOCK_INVSOCK
;
577 return GSOCK_INVSOCK
;
582 sck
->m_error
= GSOCK_INVADDR
;
583 return GSOCK_INVADDR
;
586 /* Streamed or dgram socket? */
587 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
588 sck
->m_oriented
= TRUE
;
589 sck
->m_server
= FALSE
;
590 sck
->m_establishing
= FALSE
;
592 /* Create the socket */
593 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
,
594 sck
->m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
596 if (sck
->m_fd
== INVALID_SOCKET
)
598 sck
->m_error
= GSOCK_IOERR
;
602 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
603 _GSocket_Enable_Events(sck
);
605 /* Connect it to the peer address, with a timeout (see below) */
606 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
612 /* If connect failed with EINPROGRESS and the GSocket object
613 * is in blocking mode, we select() for the specified timeout
614 * checking for writability to see if the connection request
617 if ((err
== EINPROGRESS
) && (!sck
->m_non_blocking
))
619 if (_GSocket_Output_Timeout(sck
) == GSOCK_TIMEDOUT
)
622 /* sck->m_error is set in _GSocket_Output_Timeout */
623 return GSOCK_TIMEDOUT
;
628 SOCKLEN_T len
= sizeof(error
);
630 getsockopt(sck
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*) &error
, &len
);
633 return GSOCK_NOERROR
;
637 /* If connect failed with EINPROGRESS and the GSocket object
638 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
639 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
640 * this way if the connection completes, a GSOCK_CONNECTION
641 * event will be generated, if enabled.
643 if ((err
== EINPROGRESS
) && (sck
->m_non_blocking
))
645 sck
->m_establishing
= TRUE
;
646 sck
->m_error
= GSOCK_WOULDBLOCK
;
647 return GSOCK_WOULDBLOCK
;
650 /* If connect failed with an error other than EINPROGRESS,
651 * then the call to GSocket_Connect has failed.
654 sck
->m_error
= GSOCK_IOERR
;
658 return GSOCK_NOERROR
;
661 /* Datagram sockets */
663 /* GSocket_SetNonOriented:
664 * Sets up this socket as a non-connection oriented (datagram) socket.
665 * Before using this function, the local address must have been set
666 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
667 * on success, or one of the following otherwise.
670 * GSOCK_INVSOCK - the socket is in use.
671 * GSOCK_INVADDR - the local address has not been set.
672 * GSOCK_IOERR - low-level error.
674 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
680 if (sck
->m_fd
!= INVALID_SOCKET
)
682 sck
->m_error
= GSOCK_INVSOCK
;
683 return GSOCK_INVSOCK
;
688 sck
->m_error
= GSOCK_INVADDR
;
689 return GSOCK_INVADDR
;
692 /* Initialize all fields */
693 sck
->m_stream
= FALSE
;
694 sck
->m_server
= FALSE
;
695 sck
->m_oriented
= FALSE
;
697 /* Create the socket */
698 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
700 if (sck
->m_fd
== INVALID_SOCKET
)
702 sck
->m_error
= GSOCK_IOERR
;
706 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
707 _GSocket_Enable_Events(sck
);
709 /* Bind to the local address,
710 * and retrieve the actual address bound.
712 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
713 (getsockname(sck
->m_fd
,
714 sck
->m_local
->m_addr
,
715 (SOCKLEN_T
*) &sck
->m_local
->m_len
) != 0))
718 sck
->m_error
= GSOCK_IOERR
;
722 return GSOCK_NOERROR
;
727 /* Like recv(), send(), ... */
728 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
732 assert(socket
!= NULL
);
734 /* Reenable INPUT events */
735 _GSocket_Enable(socket
, GSOCK_INPUT
);
737 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
739 socket
->m_error
= GSOCK_INVSOCK
;
743 /* If the socket is blocking, wait for data (with a timeout) */
744 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
748 if (socket
->m_stream
)
749 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
751 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
755 if (errno
== EWOULDBLOCK
)
756 socket
->m_error
= GSOCK_WOULDBLOCK
;
758 socket
->m_error
= GSOCK_IOERR
;
764 int GSocket_Write(GSocket
*socket
, const char *buffer
, int size
)
768 assert(socket
!= NULL
);
770 GSocket_Debug(( "GSocket_Write #1, size %d\n", size
));
772 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
774 socket
->m_error
= GSOCK_INVSOCK
;
778 GSocket_Debug(( "GSocket_Write #2, size %d\n", size
));
780 /* If the socket is blocking, wait for writability (with a timeout) */
781 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
784 GSocket_Debug(( "GSocket_Write #3, size %d\n", size
));
787 if (socket
->m_stream
)
788 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
790 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
792 GSocket_Debug(( "GSocket_Write #4, size %d\n", size
));
796 if (errno
== EWOULDBLOCK
)
798 socket
->m_error
= GSOCK_WOULDBLOCK
;
799 GSocket_Debug(( "GSocket_Write error WOULDBLOCK\n" ));
803 socket
->m_error
= GSOCK_IOERR
;
804 GSocket_Debug(( "GSocket_Write error IOERR\n" ));
807 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
808 * in MSW). Once the first OUTPUT event is received, users can assume
809 * that the socket is writable until a read operation fails. Only then
810 * will further OUTPUT events be posted.
812 _GSocket_Enable(socket
, GSOCK_OUTPUT
);
816 GSocket_Debug(( "GSocket_Write #5, size %d ret %d\n", size
, ret
));
822 * Polls the socket to determine its status. This function will
823 * check for the events specified in the 'flags' parameter, and
824 * it will return a mask indicating which operations can be
825 * performed. This function won't block, regardless of the
826 * mode (blocking | nonblocking) of the socket.
828 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
830 #if defined(wxUSE_GUI) && !wxUSE_GUI
832 GSocketEventFlags result
= 0;
838 /* Do not use a static struct, Linux can garble it */
842 assert(socket
!= NULL
);
847 FD_SET(socket
->m_fd
, &readfds
);
848 FD_SET(socket
->m_fd
, &writefds
);
849 FD_SET(socket
->m_fd
, &exceptfds
);
851 /* Check 'sticky' CONNECTION flag first */
852 result
|= (GSOCK_CONNECTION_FLAG
& socket
->m_detected
);
854 /* If we have already detected a LOST event, then don't try
855 * to do any further processing.
857 if ((socket
->m_detected
& GSOCK_LOST_FLAG
) != 0)
859 socket
->m_establishing
= FALSE
;
861 return (GSOCK_LOST_FLAG
& flags
);
865 if (select(socket
->m_fd
+ 1, &readfds
, &writefds
, &exceptfds
, &tv
) <= 0)
867 /* What to do here? */
868 return (result
& flags
);
871 /* Check for readability */
872 if (FD_ISSET(socket
->m_fd
, &readfds
))
876 if (recv(socket
->m_fd
, &c
, 1, MSG_PEEK
) > 0)
878 result
|= GSOCK_INPUT_FLAG
;
882 if (socket
->m_server
&& socket
->m_stream
)
884 result
|= GSOCK_CONNECTION_FLAG
;
885 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
889 socket
->m_detected
= GSOCK_LOST_FLAG
;
890 socket
->m_establishing
= FALSE
;
892 /* LOST event: Abort any further processing */
893 return (GSOCK_LOST_FLAG
& flags
);
898 /* Check for writability */
899 if (FD_ISSET(socket
->m_fd
, &writefds
))
901 if (socket
->m_establishing
&& !socket
->m_server
)
904 SOCKLEN_T len
= sizeof(error
);
906 socket
->m_establishing
= FALSE
;
908 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*)&error
, &len
);
912 socket
->m_detected
= GSOCK_LOST_FLAG
;
914 /* LOST event: Abort any further processing */
915 return (GSOCK_LOST_FLAG
& flags
);
919 result
|= GSOCK_CONNECTION_FLAG
;
920 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
925 result
|= GSOCK_OUTPUT_FLAG
;
929 /* Check for exceptions and errors (is this useful in Unices?) */
930 if (FD_ISSET(socket
->m_fd
, &exceptfds
))
932 socket
->m_establishing
= FALSE
;
933 socket
->m_detected
= GSOCK_LOST_FLAG
;
935 /* LOST event: Abort any further processing */
936 return (GSOCK_LOST_FLAG
& flags
);
939 return (result
& flags
);
943 assert(socket
!= NULL
);
944 return flags
& socket
->m_detected
;
946 #endif /* !wxUSE_GUI */
951 /* GSocket_SetNonBlocking:
952 * Sets the socket to non-blocking mode. All IO calls will return
955 void GSocket_SetNonBlocking(GSocket
*socket
, int non_block
)
957 assert(socket
!= NULL
);
959 GSocket_Debug( ("GSocket_SetNonBlocking: %d\n", (int)non_block
) );
961 socket
->m_non_blocking
= non_block
;
964 /* GSocket_SetTimeout:
965 * Sets the timeout for blocking calls. Time is expressed in
968 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
)
970 assert(socket
!= NULL
);
972 socket
->m_timeout
= millisec
;
976 * Returns the last error occured for this socket. Note that successful
977 * operations do not clear this back to GSOCK_NOERROR, so use it only
980 GSocketError
GSocket_GetError(GSocket
*socket
)
982 assert(socket
!= NULL
);
984 return socket
->m_error
;
990 * There is data to be read in the input buffer. If, after a read
991 * operation, there is still data available, the callback function will
994 * The socket is available for writing. That is, the next write call
995 * won't block. This event is generated only once, when the connection is
996 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
997 * when the output buffer empties again. This means that the app should
998 * assume that it can write since the first OUTPUT event, and no more
999 * OUTPUT events will be generated unless an error occurs.
1001 * Connection succesfully established, for client sockets, or incoming
1002 * client connection, for server sockets. Wait for this event (also watch
1003 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
1005 * The connection is lost (or a connection request failed); this could
1006 * be due to a failure, or due to the peer closing it gracefully.
1009 /* GSocket_SetCallback:
1010 * Enables the callbacks specified by 'flags'. Note that 'flags'
1011 * may be a combination of flags OR'ed toghether, so the same
1012 * callback function can be made to accept different events.
1013 * The callback function must have the following prototype:
1015 * void function(GSocket *socket, GSocketEvent event, char *cdata)
1017 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
1018 GSocketCallback callback
, char *cdata
)
1022 assert(socket
!= NULL
);
1024 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
1026 if ((flags
& (1 << count
)) != 0)
1028 socket
->m_cbacks
[count
] = callback
;
1029 socket
->m_data
[count
] = cdata
;
1034 /* GSocket_UnsetCallback:
1035 * Disables all callbacks specified by 'flags', which may be a
1036 * combination of flags OR'ed toghether.
1038 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
1042 assert(socket
!= NULL
);
1044 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
1046 if ((flags
& (1 << count
)) != 0)
1048 socket
->m_cbacks
[count
] = NULL
;
1049 socket
->m_data
[count
] = NULL
;
1055 #define CALL_CALLBACK(socket, event) { \
1056 _GSocket_Disable(socket, event); \
1057 if (socket->m_cbacks[event]) \
1058 socket->m_cbacks[event](socket, event, socket->m_data[event]); \
1062 void _GSocket_Enable(GSocket
*socket
, GSocketEvent event
)
1064 socket
->m_detected
&= ~(1 << event
);
1065 _GSocket_Install_Callback(socket
, event
);
1068 void _GSocket_Disable(GSocket
*socket
, GSocketEvent event
)
1070 socket
->m_detected
|= (1 << event
);
1071 _GSocket_Uninstall_Callback(socket
, event
);
1074 /* _GSocket_Input_Timeout:
1075 * For blocking sockets, wait until data is available or
1076 * until timeout ellapses.
1078 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
1084 /* Linux select() will overwrite the struct on return */
1085 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
1086 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
1088 if (!socket
->m_non_blocking
)
1091 FD_SET(socket
->m_fd
, &readfds
);
1092 ret
= select(socket
->m_fd
+ 1, &readfds
, NULL
, NULL
, &tv
);
1095 GSocket_Debug(( "GSocket_Input_Timeout, select returned 0\n" ));
1096 socket
->m_error
= GSOCK_TIMEDOUT
;
1097 return GSOCK_TIMEDOUT
;
1101 GSocket_Debug(( "GSocket_Input_Timeout, select returned -1\n" ));
1102 if (errno
== EBADF
) GSocket_Debug(( "Invalid file descriptor\n" ));
1103 if (errno
== EINTR
) GSocket_Debug(( "A non blocked signal was caught\n" ));
1104 if (errno
== EINVAL
) GSocket_Debug(( "The highest number descriptor is negative\n" ));
1105 if (errno
== ENOMEM
) GSocket_Debug(( "Not enough memory\n" ));
1106 socket
->m_error
= GSOCK_TIMEDOUT
;
1107 return GSOCK_TIMEDOUT
;
1110 return GSOCK_NOERROR
;
1113 /* _GSocket_Output_Timeout:
1114 * For blocking sockets, wait until data can be sent without
1115 * blocking or until timeout ellapses.
1117 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
1123 /* Linux select() will overwrite the struct on return */
1124 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
1125 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
1127 GSocket_Debug( ("m_non_blocking has: %d\n", (int)socket
->m_non_blocking
) );
1129 if (!socket
->m_non_blocking
)
1132 FD_SET(socket
->m_fd
, &writefds
);
1133 ret
= select(socket
->m_fd
+ 1, NULL
, &writefds
, NULL
, &tv
);
1136 GSocket_Debug(( "GSocket_Output_Timeout, select returned 0\n" ));
1137 socket
->m_error
= GSOCK_TIMEDOUT
;
1138 return GSOCK_TIMEDOUT
;
1142 GSocket_Debug(( "GSocket_Output_Timeout, select returned -1\n" ));
1143 if (errno
== EBADF
) GSocket_Debug(( "Invalid file descriptor\n" ));
1144 if (errno
== EINTR
) GSocket_Debug(( "A non blocked signal was caught\n" ));
1145 if (errno
== EINVAL
) GSocket_Debug(( "The highest number descriptor is negative\n" ));
1146 if (errno
== ENOMEM
) GSocket_Debug(( "Not enough memory\n" ));
1147 socket
->m_error
= GSOCK_TIMEDOUT
;
1148 return GSOCK_TIMEDOUT
;
1150 if ( ! FD_ISSET(socket
->m_fd
, &writefds
) )
1151 GSocket_Debug(( "GSocket_Output_Timeout is buggy!\n" ));
1153 GSocket_Debug(( "GSocket_Output_Timeout seems correct\n" ));
1157 GSocket_Debug(( "GSocket_Output_Timeout, didn't try select!\n" ));
1160 return GSOCK_NOERROR
;
1163 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
1165 return recv(socket
->m_fd
, buffer
, size
, 0);
1168 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
1170 struct sockaddr from
;
1171 SOCKLEN_T fromlen
= sizeof(from
);
1175 fromlen
= sizeof(from
);
1177 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, (SOCKLEN_T
*) &fromlen
);
1182 /* Translate a system address into a GSocket address */
1183 if (!socket
->m_peer
)
1185 socket
->m_peer
= GAddress_new();
1186 if (!socket
->m_peer
)
1188 socket
->m_error
= GSOCK_MEMERR
;
1192 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
1193 if (err
!= GSOCK_NOERROR
)
1195 GAddress_destroy(socket
->m_peer
);
1196 socket
->m_peer
= NULL
;
1197 socket
->m_error
= err
;
1204 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
1210 ret
= send(socket
->m_fd
, buffer
, size
, 0);
1213 ret
= send(socket
->m_fd
, (char *)buffer
, size
, 0);
1218 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
1220 struct sockaddr
*addr
;
1224 if (!socket
->m_peer
)
1226 socket
->m_error
= GSOCK_INVADDR
;
1230 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
1231 if (err
!= GSOCK_NOERROR
)
1233 socket
->m_error
= err
;
1239 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
1242 ret
= sendto(socket
->m_fd
, (char *)buffer
, size
, 0, addr
, len
);
1245 /* Frees memory allocated from _GAddress_translate_to */
1251 void _GSocket_Detected_Read(GSocket
*socket
)
1255 /* If we have already detected a LOST event, then don't try
1256 * to do any further processing.
1258 if ((socket
->m_detected
& GSOCK_LOST_FLAG
) != 0)
1260 socket
->m_establishing
= FALSE
;
1262 CALL_CALLBACK(socket
, GSOCK_LOST
);
1263 GSocket_Shutdown(socket
);
1267 if (recv(socket
->m_fd
, &c
, 1, MSG_PEEK
) > 0)
1269 CALL_CALLBACK(socket
, GSOCK_INPUT
);
1273 if (socket
->m_server
&& socket
->m_stream
)
1275 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
1279 CALL_CALLBACK(socket
, GSOCK_LOST
);
1280 GSocket_Shutdown(socket
);
1285 void _GSocket_Detected_Write(GSocket
*socket
)
1287 /* If we have already detected a LOST event, then don't try
1288 * to do any further processing.
1290 if ((socket
->m_detected
& GSOCK_LOST_FLAG
) != 0)
1292 socket
->m_establishing
= FALSE
;
1294 CALL_CALLBACK(socket
, GSOCK_LOST
);
1295 GSocket_Shutdown(socket
);
1299 if (socket
->m_establishing
&& !socket
->m_server
)
1302 SOCKLEN_T len
= sizeof(error
);
1304 socket
->m_establishing
= FALSE
;
1306 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*)&error
, &len
);
1310 CALL_CALLBACK(socket
, GSOCK_LOST
);
1311 GSocket_Shutdown(socket
);
1315 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
1316 /* We have to fire this event by hand because CONNECTION (for clients)
1317 * and OUTPUT are internally the same and we just disabled CONNECTION
1318 * events with the above macro.
1320 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
1325 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
1330 * -------------------------------------------------------------------------
1332 * -------------------------------------------------------------------------
1335 /* CHECK_ADDRESS verifies that the current address family is either
1336 * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it
1337 * initalizes it to be a GSOCK_*family*. In other cases, it returns
1338 * an appropiate error code.
1340 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
1342 #define CHECK_ADDRESS(address, family) \
1344 if (address->m_family == GSOCK_NOFAMILY) \
1345 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1346 return address->m_error; \
1347 if (address->m_family != GSOCK_##family) \
1349 address->m_error = GSOCK_INVADDR; \
1350 return GSOCK_INVADDR; \
1354 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
1356 if (address->m_family == GSOCK_NOFAMILY) \
1357 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1359 if (address->m_family != GSOCK_##family) \
1361 address->m_error = GSOCK_INVADDR; \
1367 GAddress
*GAddress_new(void)
1371 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1374 address
->m_family
= GSOCK_NOFAMILY
;
1375 address
->m_addr
= NULL
;
1381 GAddress
*GAddress_copy(GAddress
*address
)
1385 assert(address
!= NULL
);
1387 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1390 memcpy(addr2
, address
, sizeof(GAddress
));
1392 if (address
->m_addr
&& address
->m_len
> 0)
1394 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
1395 if (addr2
->m_addr
== NULL
)
1400 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1406 void GAddress_destroy(GAddress
*address
)
1408 assert(address
!= NULL
);
1410 if (address
->m_addr
)
1411 free(address
->m_addr
);
1413 /* free(address); */
1416 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1418 assert(address
!= NULL
);
1420 address
->m_family
= type
;
1423 GAddressType
GAddress_GetFamily(GAddress
*address
)
1425 assert(address
!= NULL
);
1427 return address
->m_family
;
1430 GSocketError
_GAddress_translate_from(GAddress
*address
,
1431 struct sockaddr
*addr
, int len
)
1433 address
->m_realfamily
= addr
->sa_family
;
1434 switch (addr
->sa_family
)
1437 address
->m_family
= GSOCK_INET
;
1440 address
->m_family
= GSOCK_UNIX
;
1444 address
->m_family
= GSOCK_INET6
;
1449 address
->m_error
= GSOCK_INVOP
;
1454 if (address
->m_addr
)
1455 free(address
->m_addr
);
1457 address
->m_len
= len
;
1458 address
->m_addr
= (struct sockaddr
*)malloc(len
);
1460 if (address
->m_addr
== NULL
)
1462 address
->m_error
= GSOCK_MEMERR
;
1463 return GSOCK_MEMERR
;
1465 memcpy(address
->m_addr
, addr
, len
);
1467 return GSOCK_NOERROR
;
1470 GSocketError
_GAddress_translate_to(GAddress
*address
,
1471 struct sockaddr
**addr
, int *len
)
1473 if (!address
->m_addr
)
1475 address
->m_error
= GSOCK_INVADDR
;
1476 return GSOCK_INVADDR
;
1479 *len
= address
->m_len
;
1480 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
1483 address
->m_error
= GSOCK_MEMERR
;
1484 return GSOCK_MEMERR
;
1487 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1488 return GSOCK_NOERROR
;
1492 * -------------------------------------------------------------------------
1493 * Internet address family
1494 * -------------------------------------------------------------------------
1497 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1499 address
->m_len
= sizeof(struct sockaddr_in
);
1500 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1501 if (address
->m_addr
== NULL
)
1503 address
->m_error
= GSOCK_MEMERR
;
1504 return GSOCK_MEMERR
;
1507 address
->m_family
= GSOCK_INET
;
1508 address
->m_realfamily
= PF_INET
;
1509 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1510 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1512 return GSOCK_NOERROR
;
1515 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1518 struct in_addr
*addr
;
1520 assert(address
!= NULL
);
1522 CHECK_ADDRESS(address
, INET
);
1524 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1526 /* If it is a numeric host name, convert it now */
1527 #if defined(HAVE_INET_ATON)
1528 if (inet_aton(hostname
, addr
) == 0)
1530 #elif defined(HAVE_INET_ADDR)
1531 addr
->s_addr
= inet_addr(hostname
);
1532 if ( addr
->s_addr
== -1 )
1535 /* Use gethostbyname by default */
1536 int val
= 1; //VA doesn't like constants in conditional expressions at all
1540 struct in_addr
*array_addr
;
1542 /* It is a real name, we solve it */
1543 if ((he
= gethostbyname(hostname
)) == NULL
)
1545 /* Reset to invalid address */
1546 addr
->s_addr
= INADDR_NONE
;
1547 address
->m_error
= GSOCK_NOHOST
;
1548 return GSOCK_NOHOST
;
1550 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1551 addr
->s_addr
= array_addr
[0].s_addr
;
1553 return GSOCK_NOERROR
;
1556 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1558 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1561 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1562 unsigned long hostaddr
)
1564 struct in_addr
*addr
;
1566 assert(address
!= NULL
);
1568 CHECK_ADDRESS(address
, INET
);
1570 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1571 addr
->s_addr
= hostaddr
;
1573 return GSOCK_NOERROR
;
1576 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1577 const char *protocol
)
1580 struct sockaddr_in
*addr
;
1582 assert(address
!= NULL
);
1583 CHECK_ADDRESS(address
, INET
);
1587 address
->m_error
= GSOCK_INVPORT
;
1588 return GSOCK_INVPORT
;
1591 se
= getservbyname(port
, protocol
);
1594 /* the cast to int suppresses compiler warnings about subscript having the
1596 if (isdigit((int)port
[0]))
1600 port_int
= atoi(port
);
1601 addr
= (struct sockaddr_in
*)address
->m_addr
;
1602 addr
->sin_port
= htons(port_int
);
1603 return GSOCK_NOERROR
;
1606 address
->m_error
= GSOCK_INVPORT
;
1607 return GSOCK_INVPORT
;
1610 addr
= (struct sockaddr_in
*)address
->m_addr
;
1611 addr
->sin_port
= se
->s_port
;
1613 return GSOCK_NOERROR
;
1616 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1618 struct sockaddr_in
*addr
;
1620 assert(address
!= NULL
);
1621 CHECK_ADDRESS(address
, INET
);
1623 addr
= (struct sockaddr_in
*)address
->m_addr
;
1624 addr
->sin_port
= htons(port
);
1626 return GSOCK_NOERROR
;
1629 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1633 struct sockaddr_in
*addr
;
1635 assert(address
!= NULL
);
1636 CHECK_ADDRESS(address
, INET
);
1638 addr
= (struct sockaddr_in
*)address
->m_addr
;
1639 addr_buf
= (char *)&(addr
->sin_addr
);
1641 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1644 address
->m_error
= GSOCK_NOHOST
;
1645 return GSOCK_NOHOST
;
1648 strncpy(hostname
, he
->h_name
, sbuf
);
1650 return GSOCK_NOERROR
;
1653 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1655 struct sockaddr_in
*addr
;
1657 assert(address
!= NULL
);
1658 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1660 addr
= (struct sockaddr_in
*)address
->m_addr
;
1662 return addr
->sin_addr
.s_addr
;
1665 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1667 struct sockaddr_in
*addr
;
1669 assert(address
!= NULL
);
1670 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1672 addr
= (struct sockaddr_in
*)address
->m_addr
;
1673 return ntohs(addr
->sin_port
);
1677 * -------------------------------------------------------------------------
1678 * Unix address family
1679 * -------------------------------------------------------------------------
1683 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1685 address
->m_len
= sizeof(struct sockaddr_un
);
1686 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
1687 if (address
->m_addr
== NULL
)
1689 address
->m_error
= GSOCK_MEMERR
;
1690 return GSOCK_MEMERR
;
1693 address
->m_family
= GSOCK_UNIX
;
1694 address
->m_realfamily
= PF_UNIX
;
1695 ((struct sockaddr_un
*)address
->m_addr
)->sun_family
= AF_UNIX
;
1696 ((struct sockaddr_un
*)address
->m_addr
)->sun_path
[0] = 0;
1698 return GSOCK_NOERROR
;
1701 #define UNIX_SOCK_PATHLEN (sizeof(addr->sun_path)/sizeof(addr->sun_path[0]))
1703 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1705 struct sockaddr_un
*addr
;
1707 assert(address
!= NULL
);
1709 CHECK_ADDRESS(address
, UNIX
);
1711 addr
= ((struct sockaddr_un
*)address
->m_addr
);
1712 strncpy(addr
->sun_path
, path
, UNIX_SOCK_PATHLEN
);
1713 addr
->sun_path
[UNIX_SOCK_PATHLEN
- 1] = '\0';
1715 return GSOCK_NOERROR
;
1718 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1720 struct sockaddr_un
*addr
;
1722 assert(address
!= NULL
);
1723 CHECK_ADDRESS(address
, UNIX
);
1725 addr
= (struct sockaddr_un
*)address
->m_addr
;
1727 strncpy(path
, addr
->sun_path
, sbuf
);
1729 return GSOCK_NOERROR
;
1734 /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */