]>
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
7 * Licence: The wxWindows licence
9 * -------------------------------------------------------------------------
13 * PLEASE don't put C++ comments here - this is a C source file.
16 #ifndef __GSOCKET_STANDALONE__
21 /* I don't see, why this include is needed, but it seems to be necessary
22 sometimes. For EMX, including C++ headers into plain C source breaks
23 compilation, so don't do it there. */
27 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
29 #define BSD_SELECT /* use Berkley Sockets select */
32 #include <sys/types.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
42 #define HAVE_INET_ADDR
50 #include <netinet/in.h>
56 #if defined(__VISAGECPP__) && __IBMCPP__ < 400
58 #include <machine/endian.h>
64 #define EBADF SOCEBADF
68 #include <sys/socket.h>
69 #include <sys/ioctl.h>
70 #include <sys/select.h>
73 #define soclose(a) close(a)
75 #define select(a,b,c,d,e) bsdselect(a,b,c,d,e)
76 int _System
bsdselect(int,
81 int _System
soclose(int);
86 #if (defined(__VISAGECPP__) && __IBMCPP__ < 400) || defined(__EMX__)
102 # define SOCKLEN_T socklen_t
105 # define SOCKLEN_T int
111 * MSW defines this, Unices don't.
113 #ifndef INVALID_SOCKET
114 #define INVALID_SOCKET -1
118 * INADDR_BROADCAST is identical to INADDR_NONE which is not defined
119 * on all systems. INADDR_BROADCAST should be fine to indicate an error.
122 #define INADDR_NONE INADDR_BROADCAST
125 #define MASK_SIGNAL() \
127 void (*old_handler)(int); \
129 old_handler = signal(SIGPIPE, SIG_IGN);
131 #define UNMASK_SIGNAL() \
132 signal(SIGPIPE, old_handler); \
136 #ifndef __GSOCKET_STANDALONE__
137 # include "wx/unix/gsockunx.h"
138 # include "wx/gsocket.h"
140 # include "gsockunx.h"
141 # include "gsocket.h"
142 #endif /* __GSOCKET_STANDALONE__ */
144 /* redefine some GUI-only functions to do nothing in console mode */
145 #if defined(wxUSE_GUI) && !wxUSE_GUI
146 # define _GSocket_GUI_Init(socket) (1)
147 # define _GSocket_GUI_Destroy(socket)
148 # define _GSocket_Enable_Events(socket)
149 # define _GSocket_Disable_Events(socket)
150 # define _GSocket_Install_Callback(socket, event)
151 # define _GSocket_Uninstall_Callback(socket, event)
152 #endif /* wxUSE_GUI */
154 /* debugging helpers */
155 #ifdef __GSOCKET_DEBUG__
156 # define GSocket_Debug(args) printf args
158 # define GSocket_Debug(args)
159 #endif /* __GSOCKET_DEBUG__ */
161 /* Global initialisers */
163 int GSocket_Init(void)
168 void GSocket_Cleanup(void)
172 /* Constructors / Destructors for GSocket */
174 GSocket
*GSocket_new(void)
179 socket
= (GSocket
*)malloc(sizeof(GSocket
));
184 socket
->m_fd
= INVALID_SOCKET
;
185 for (i
=0;i
<GSOCK_MAX_EVENT
;i
++)
187 socket
->m_cbacks
[i
] = NULL
;
189 socket
->m_detected
= 0;
190 socket
->m_local
= NULL
;
191 socket
->m_peer
= NULL
;
192 socket
->m_error
= GSOCK_NOERROR
;
193 socket
->m_server
= FALSE
;
194 socket
->m_stream
= TRUE
;
195 socket
->m_gui_dependent
= NULL
;
196 socket
->m_non_blocking
= FALSE
;
197 socket
->m_timeout
= 10*60*1000;
198 /* 10 minutes * 60 sec * 1000 millisec */
199 socket
->m_establishing
= FALSE
;
201 /* Per-socket GUI-specific initialization */
202 success
= _GSocket_GUI_Init(socket
);
212 void GSocket_close(GSocket
*socket
)
214 _GSocket_Disable_Events(socket
);
215 soclose(socket
->m_fd
);
216 socket
->m_fd
= INVALID_SOCKET
;
219 void GSocket_destroy(GSocket
*socket
)
221 assert(socket
!= NULL
);
223 /* Check that the socket is really shutdowned */
224 if (socket
->m_fd
!= INVALID_SOCKET
)
225 GSocket_Shutdown(socket
);
227 /* Per-socket GUI-specific cleanup */
228 _GSocket_GUI_Destroy(socket
);
230 /* Destroy private addresses */
232 GAddress_destroy(socket
->m_local
);
235 GAddress_destroy(socket
->m_peer
);
237 /* Destroy the socket itself */
242 * Disallow further read/write operations on this socket, close
243 * the fd and disable all callbacks.
245 void GSocket_Shutdown(GSocket
*socket
)
249 assert(socket
!= NULL
);
251 /* If socket has been created, shutdown it */
252 if (socket
->m_fd
!= INVALID_SOCKET
)
254 shutdown(socket
->m_fd
, 2);
255 GSocket_close(socket
);
258 /* Disable GUI callbacks */
259 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
260 socket
->m_cbacks
[evt
] = NULL
;
262 socket
->m_detected
= GSOCK_LOST_FLAG
;
263 _GSocket_Disable_Events(socket
);
266 /* Address handling */
272 * Set or get the local or peer address for this socket. The 'set'
273 * functions return GSOCK_NOERROR on success, an error code otherwise.
274 * The 'get' functions return a pointer to a GAddress object on success,
275 * or NULL otherwise, in which case they set the error code of the
276 * corresponding GSocket.
279 * GSOCK_INVSOCK - the socket is not valid.
280 * GSOCK_INVADDR - the address is not valid.
282 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
284 assert(socket
!= NULL
);
286 /* the socket must be initialized, or it must be a server */
287 if ((socket
->m_fd
!= INVALID_SOCKET
&& !socket
->m_server
))
289 socket
->m_error
= GSOCK_INVSOCK
;
290 return GSOCK_INVSOCK
;
294 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
296 socket
->m_error
= GSOCK_INVADDR
;
297 return GSOCK_INVADDR
;
301 GAddress_destroy(socket
->m_local
);
303 socket
->m_local
= GAddress_copy(address
);
305 return GSOCK_NOERROR
;
308 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
310 assert(socket
!= NULL
);
313 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
315 socket
->m_error
= GSOCK_INVADDR
;
316 return GSOCK_INVADDR
;
320 GAddress_destroy(socket
->m_peer
);
322 socket
->m_peer
= GAddress_copy(address
);
324 return GSOCK_NOERROR
;
327 GAddress
*GSocket_GetLocal(GSocket
*socket
)
330 struct sockaddr addr
;
331 SOCKLEN_T size
= sizeof(addr
);
334 assert(socket
!= NULL
);
336 /* try to get it from the m_local var first */
338 return GAddress_copy(socket
->m_local
);
340 /* else, if the socket is initialized, try getsockname */
341 if (socket
->m_fd
== INVALID_SOCKET
)
343 socket
->m_error
= GSOCK_INVSOCK
;
347 if (getsockname(socket
->m_fd
, &addr
, (SOCKLEN_T
*) &size
) < 0)
349 socket
->m_error
= GSOCK_IOERR
;
353 /* got a valid address from getsockname, create a GAddress object */
354 address
= GAddress_new();
357 socket
->m_error
= GSOCK_MEMERR
;
361 err
= _GAddress_translate_from(address
, &addr
, size
);
362 if (err
!= GSOCK_NOERROR
)
364 GAddress_destroy(address
);
365 socket
->m_error
= err
;
372 GAddress
*GSocket_GetPeer(GSocket
*socket
)
374 assert(socket
!= NULL
);
376 /* try to get it from the m_peer var */
378 return GAddress_copy(socket
->m_peer
);
383 /* Server specific parts */
385 /* GSocket_SetServer:
386 * Sets up this socket as a server. The local address must have been
387 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
388 * Returns GSOCK_NOERROR on success, one of the following otherwise:
391 * GSOCK_INVSOCK - the socket is in use.
392 * GSOCK_INVADDR - the local address has not been set.
393 * GSOCK_IOERR - low-level error.
395 GSocketError
GSocket_SetServer(GSocket
*sck
)
401 /* must not be in use */
402 if (sck
->m_fd
!= INVALID_SOCKET
)
404 sck
->m_error
= GSOCK_INVSOCK
;
405 return GSOCK_INVSOCK
;
408 /* the local addr must have been set */
411 sck
->m_error
= GSOCK_INVADDR
;
412 return GSOCK_INVADDR
;
415 /* Initialize all fields */
416 sck
->m_stream
= TRUE
;
417 sck
->m_server
= TRUE
;
418 sck
->m_oriented
= TRUE
;
420 /* Create the socket */
421 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
423 if (sck
->m_fd
== INVALID_SOCKET
)
425 sck
->m_error
= GSOCK_IOERR
;
429 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
430 _GSocket_Enable_Events(sck
);
432 /* Bind to the local address,
433 * retrieve the actual address bound,
434 * and listen up to 5 connections.
436 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
437 (getsockname(sck
->m_fd
,
438 sck
->m_local
->m_addr
,
439 (SOCKLEN_T
*) &sck
->m_local
->m_len
) != 0) ||
440 (listen(sck
->m_fd
, 5) != 0))
443 sck
->m_error
= GSOCK_IOERR
;
447 return GSOCK_NOERROR
;
450 /* GSocket_WaitConnection:
451 * Waits for an incoming client connection. Returns a pointer to
452 * a GSocket object, or NULL if there was an error, in which case
453 * the last error field will be updated for the calling GSocket.
455 * Error codes (set in the calling GSocket)
456 * GSOCK_INVSOCK - the socket is not valid or not a server.
457 * GSOCK_TIMEDOUT - timeout, no incoming connections.
458 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
459 * GSOCK_MEMERR - couldn't allocate memory.
460 * GSOCK_IOERR - low-level error.
462 GSocket
*GSocket_WaitConnection(GSocket
*socket
)
464 struct sockaddr from
;
465 SOCKLEN_T fromlen
= sizeof(from
);
470 assert(socket
!= NULL
);
472 /* Reenable CONNECTION events */
473 _GSocket_Enable(socket
, GSOCK_CONNECTION
);
475 /* If the socket has already been created, we exit immediately */
476 if (socket
->m_fd
== INVALID_SOCKET
|| !socket
->m_server
)
478 socket
->m_error
= GSOCK_INVSOCK
;
482 /* Create a GSocket object for the new connection */
483 connection
= GSocket_new();
487 socket
->m_error
= GSOCK_MEMERR
;
491 /* Wait for a connection (with timeout) */
492 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
494 GSocket_destroy(connection
);
495 /* socket->m_error set by _GSocket_Input_Timeout */
499 connection
->m_fd
= accept(socket
->m_fd
, &from
, (SOCKLEN_T
*) &fromlen
);
501 if (connection
->m_fd
== INVALID_SOCKET
)
503 if (errno
== EWOULDBLOCK
)
504 socket
->m_error
= GSOCK_WOULDBLOCK
;
506 socket
->m_error
= GSOCK_IOERR
;
508 GSocket_destroy(connection
);
512 /* Initialize all fields */
513 connection
->m_server
= FALSE
;
514 connection
->m_stream
= TRUE
;
515 connection
->m_oriented
= TRUE
;
517 /* Setup the peer address field */
518 connection
->m_peer
= GAddress_new();
519 if (!connection
->m_peer
)
521 GSocket_destroy(connection
);
522 socket
->m_error
= GSOCK_MEMERR
;
525 err
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
);
526 if (err
!= GSOCK_NOERROR
)
528 GAddress_destroy(connection
->m_peer
);
529 GSocket_destroy(connection
);
530 socket
->m_error
= err
;
534 ioctl(connection
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
535 _GSocket_Enable_Events(connection
);
540 /* Client specific parts */
543 * For stream (connection oriented) sockets, GSocket_Connect() tries
544 * to establish a client connection to a server using the peer address
545 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
546 * connection has been succesfully established, or one of the error
547 * codes listed below. Note that for nonblocking sockets, a return
548 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
549 * request can be completed later; you should use GSocket_Select()
550 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
551 * corresponding asynchronous events.
553 * For datagram (non connection oriented) sockets, GSocket_Connect()
554 * just sets the peer address established with GSocket_SetPeer() as
555 * default destination.
558 * GSOCK_INVSOCK - the socket is in use or not valid.
559 * GSOCK_INVADDR - the peer address has not been established.
560 * GSOCK_TIMEDOUT - timeout, the connection failed.
561 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
562 * GSOCK_MEMERR - couldn't allocate memory.
563 * GSOCK_IOERR - low-level error.
565 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
572 /* Enable CONNECTION events (needed for nonblocking connections) */
573 _GSocket_Enable(sck
, GSOCK_CONNECTION
);
575 if (sck
->m_fd
!= INVALID_SOCKET
)
577 sck
->m_error
= GSOCK_INVSOCK
;
578 return GSOCK_INVSOCK
;
583 sck
->m_error
= GSOCK_INVADDR
;
584 return GSOCK_INVADDR
;
587 /* Streamed or dgram socket? */
588 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
589 sck
->m_oriented
= TRUE
;
590 sck
->m_server
= FALSE
;
591 sck
->m_establishing
= FALSE
;
593 /* Create the socket */
594 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
,
595 sck
->m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
597 if (sck
->m_fd
== INVALID_SOCKET
)
599 sck
->m_error
= GSOCK_IOERR
;
603 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
604 _GSocket_Enable_Events(sck
);
606 /* Connect it to the peer address, with a timeout (see below) */
607 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
613 /* If connect failed with EINPROGRESS and the GSocket object
614 * is in blocking mode, we select() for the specified timeout
615 * checking for writability to see if the connection request
618 if ((err
== EINPROGRESS
) && (!sck
->m_non_blocking
))
620 if (_GSocket_Output_Timeout(sck
) == GSOCK_TIMEDOUT
)
623 /* sck->m_error is set in _GSocket_Output_Timeout */
624 return GSOCK_TIMEDOUT
;
629 SOCKLEN_T len
= sizeof(error
);
631 getsockopt(sck
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*) &error
, &len
);
634 return GSOCK_NOERROR
;
638 /* If connect failed with EINPROGRESS and the GSocket object
639 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
640 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
641 * this way if the connection completes, a GSOCK_CONNECTION
642 * event will be generated, if enabled.
644 if ((err
== EINPROGRESS
) && (sck
->m_non_blocking
))
646 sck
->m_establishing
= TRUE
;
647 sck
->m_error
= GSOCK_WOULDBLOCK
;
648 return GSOCK_WOULDBLOCK
;
651 /* If connect failed with an error other than EINPROGRESS,
652 * then the call to GSocket_Connect has failed.
655 sck
->m_error
= GSOCK_IOERR
;
659 return GSOCK_NOERROR
;
662 /* Datagram sockets */
664 /* GSocket_SetNonOriented:
665 * Sets up this socket as a non-connection oriented (datagram) socket.
666 * Before using this function, the local address must have been set
667 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
668 * on success, or one of the following otherwise.
671 * GSOCK_INVSOCK - the socket is in use.
672 * GSOCK_INVADDR - the local address has not been set.
673 * GSOCK_IOERR - low-level error.
675 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
681 if (sck
->m_fd
!= INVALID_SOCKET
)
683 sck
->m_error
= GSOCK_INVSOCK
;
684 return GSOCK_INVSOCK
;
689 sck
->m_error
= GSOCK_INVADDR
;
690 return GSOCK_INVADDR
;
693 /* Initialize all fields */
694 sck
->m_stream
= FALSE
;
695 sck
->m_server
= FALSE
;
696 sck
->m_oriented
= FALSE
;
698 /* Create the socket */
699 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
701 if (sck
->m_fd
== INVALID_SOCKET
)
703 sck
->m_error
= GSOCK_IOERR
;
707 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
708 _GSocket_Enable_Events(sck
);
710 /* Bind to the local address,
711 * and retrieve the actual address bound.
713 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
714 (getsockname(sck
->m_fd
,
715 sck
->m_local
->m_addr
,
716 (SOCKLEN_T
*) &sck
->m_local
->m_len
) != 0))
719 sck
->m_error
= GSOCK_IOERR
;
723 return GSOCK_NOERROR
;
728 /* Like recv(), send(), ... */
729 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
733 assert(socket
!= NULL
);
735 /* Reenable INPUT events */
736 _GSocket_Enable(socket
, GSOCK_INPUT
);
738 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
740 socket
->m_error
= GSOCK_INVSOCK
;
744 /* If the socket is blocking, wait for data (with a timeout) */
745 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
749 if (socket
->m_stream
)
750 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
752 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
756 if (errno
== EWOULDBLOCK
)
757 socket
->m_error
= GSOCK_WOULDBLOCK
;
759 socket
->m_error
= GSOCK_IOERR
;
765 int GSocket_Write(GSocket
*socket
, const char *buffer
, int size
)
769 assert(socket
!= NULL
);
771 GSocket_Debug(( "GSocket_Write #1, size %d\n", size
));
773 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
775 socket
->m_error
= GSOCK_INVSOCK
;
779 GSocket_Debug(( "GSocket_Write #2, size %d\n", size
));
781 /* If the socket is blocking, wait for writability (with a timeout) */
782 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
785 GSocket_Debug(( "GSocket_Write #3, size %d\n", size
));
788 if (socket
->m_stream
)
789 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
791 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
793 GSocket_Debug(( "GSocket_Write #4, size %d\n", size
));
797 if (errno
== EWOULDBLOCK
)
799 socket
->m_error
= GSOCK_WOULDBLOCK
;
800 GSocket_Debug(( "GSocket_Write error WOULDBLOCK\n" ));
804 socket
->m_error
= GSOCK_IOERR
;
805 GSocket_Debug(( "GSocket_Write error IOERR\n" ));
808 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
809 * in MSW). Once the first OUTPUT event is received, users can assume
810 * that the socket is writable until a read operation fails. Only then
811 * will further OUTPUT events be posted.
813 _GSocket_Enable(socket
, GSOCK_OUTPUT
);
817 GSocket_Debug(( "GSocket_Write #5, size %d ret %d\n", size
, ret
));
823 * Polls the socket to determine its status. This function will
824 * check for the events specified in the 'flags' parameter, and
825 * it will return a mask indicating which operations can be
826 * performed. This function won't block, regardless of the
827 * mode (blocking | nonblocking) of the socket.
829 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
831 #if defined(wxUSE_GUI) && !wxUSE_GUI
833 GSocketEventFlags result
= 0;
839 /* Do not use a static struct, Linux can garble it */
843 assert(socket
!= NULL
);
848 FD_SET(socket
->m_fd
, &readfds
);
849 FD_SET(socket
->m_fd
, &writefds
);
850 FD_SET(socket
->m_fd
, &exceptfds
);
852 /* Check 'sticky' CONNECTION flag first */
853 result
|= (GSOCK_CONNECTION_FLAG
& socket
->m_detected
);
855 /* If we have already detected a LOST event, then don't try
856 * to do any further processing.
858 if ((socket
->m_detected
& GSOCK_LOST_FLAG
) != 0)
860 socket
->m_establishing
= FALSE
;
862 return (GSOCK_LOST_FLAG
& flags
);
866 if (select(socket
->m_fd
+ 1, &readfds
, &writefds
, &exceptfds
, &tv
) <= 0)
868 /* What to do here? */
869 return (result
& flags
);
872 /* Check for readability */
873 if (FD_ISSET(socket
->m_fd
, &readfds
))
877 if (recv(socket
->m_fd
, &c
, 1, MSG_PEEK
) > 0)
879 result
|= GSOCK_INPUT_FLAG
;
883 if (socket
->m_server
&& socket
->m_stream
)
885 result
|= GSOCK_CONNECTION_FLAG
;
886 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
890 socket
->m_detected
= GSOCK_LOST_FLAG
;
891 socket
->m_establishing
= FALSE
;
893 /* LOST event: Abort any further processing */
894 return (GSOCK_LOST_FLAG
& flags
);
899 /* Check for writability */
900 if (FD_ISSET(socket
->m_fd
, &writefds
))
902 if (socket
->m_establishing
&& !socket
->m_server
)
905 SOCKLEN_T len
= sizeof(error
);
907 socket
->m_establishing
= FALSE
;
909 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*)&error
, &len
);
913 socket
->m_detected
= GSOCK_LOST_FLAG
;
915 /* LOST event: Abort any further processing */
916 return (GSOCK_LOST_FLAG
& flags
);
920 result
|= GSOCK_CONNECTION_FLAG
;
921 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
926 result
|= GSOCK_OUTPUT_FLAG
;
930 /* Check for exceptions and errors (is this useful in Unices?) */
931 if (FD_ISSET(socket
->m_fd
, &exceptfds
))
933 socket
->m_establishing
= FALSE
;
934 socket
->m_detected
= GSOCK_LOST_FLAG
;
936 /* LOST event: Abort any further processing */
937 return (GSOCK_LOST_FLAG
& flags
);
940 return (result
& flags
);
944 assert(socket
!= NULL
);
945 return flags
& socket
->m_detected
;
947 #endif /* !wxUSE_GUI */
952 /* GSocket_SetNonBlocking:
953 * Sets the socket to non-blocking mode. All IO calls will return
956 void GSocket_SetNonBlocking(GSocket
*socket
, int non_block
)
958 assert(socket
!= NULL
);
960 GSocket_Debug( ("GSocket_SetNonBlocking: %d\n", (int)non_block
) );
962 socket
->m_non_blocking
= non_block
;
965 /* GSocket_SetTimeout:
966 * Sets the timeout for blocking calls. Time is expressed in
969 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
)
971 assert(socket
!= NULL
);
973 socket
->m_timeout
= millisec
;
977 * Returns the last error occured for this socket. Note that successful
978 * operations do not clear this back to GSOCK_NOERROR, so use it only
981 GSocketError
GSocket_GetError(GSocket
*socket
)
983 assert(socket
!= NULL
);
985 return socket
->m_error
;
991 * There is data to be read in the input buffer. If, after a read
992 * operation, there is still data available, the callback function will
995 * The socket is available for writing. That is, the next write call
996 * won't block. This event is generated only once, when the connection is
997 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
998 * when the output buffer empties again. This means that the app should
999 * assume that it can write since the first OUTPUT event, and no more
1000 * OUTPUT events will be generated unless an error occurs.
1002 * Connection succesfully established, for client sockets, or incoming
1003 * client connection, for server sockets. Wait for this event (also watch
1004 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
1006 * The connection is lost (or a connection request failed); this could
1007 * be due to a failure, or due to the peer closing it gracefully.
1010 /* GSocket_SetCallback:
1011 * Enables the callbacks specified by 'flags'. Note that 'flags'
1012 * may be a combination of flags OR'ed toghether, so the same
1013 * callback function can be made to accept different events.
1014 * The callback function must have the following prototype:
1016 * void function(GSocket *socket, GSocketEvent event, char *cdata)
1018 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
1019 GSocketCallback callback
, char *cdata
)
1023 assert(socket
!= NULL
);
1025 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
1027 if ((flags
& (1 << count
)) != 0)
1029 socket
->m_cbacks
[count
] = callback
;
1030 socket
->m_data
[count
] = cdata
;
1035 /* GSocket_UnsetCallback:
1036 * Disables all callbacks specified by 'flags', which may be a
1037 * combination of flags OR'ed toghether.
1039 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
1043 assert(socket
!= NULL
);
1045 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
1047 if ((flags
& (1 << count
)) != 0)
1049 socket
->m_cbacks
[count
] = NULL
;
1050 socket
->m_data
[count
] = NULL
;
1056 #define CALL_CALLBACK(socket, event) { \
1057 _GSocket_Disable(socket, event); \
1058 if (socket->m_cbacks[event]) \
1059 socket->m_cbacks[event](socket, event, socket->m_data[event]); \
1063 void _GSocket_Enable(GSocket
*socket
, GSocketEvent event
)
1065 socket
->m_detected
&= ~(1 << event
);
1066 _GSocket_Install_Callback(socket
, event
);
1069 void _GSocket_Disable(GSocket
*socket
, GSocketEvent event
)
1071 socket
->m_detected
|= (1 << event
);
1072 _GSocket_Uninstall_Callback(socket
, event
);
1075 /* _GSocket_Input_Timeout:
1076 * For blocking sockets, wait until data is available or
1077 * until timeout ellapses.
1079 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
1085 /* Linux select() will overwrite the struct on return */
1086 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
1087 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
1089 if (!socket
->m_non_blocking
)
1092 FD_SET(socket
->m_fd
, &readfds
);
1093 ret
= select(socket
->m_fd
+ 1, &readfds
, NULL
, NULL
, &tv
);
1096 GSocket_Debug(( "GSocket_Input_Timeout, select returned 0\n" ));
1097 socket
->m_error
= GSOCK_TIMEDOUT
;
1098 return GSOCK_TIMEDOUT
;
1102 GSocket_Debug(( "GSocket_Input_Timeout, select returned -1\n" ));
1103 if (errno
== EBADF
) GSocket_Debug(( "Invalid file descriptor\n" ));
1104 if (errno
== EINTR
) GSocket_Debug(( "A non blocked signal was caught\n" ));
1105 if (errno
== EINVAL
) GSocket_Debug(( "The highest number descriptor is negative\n" ));
1106 if (errno
== ENOMEM
) GSocket_Debug(( "Not enough memory\n" ));
1107 socket
->m_error
= GSOCK_TIMEDOUT
;
1108 return GSOCK_TIMEDOUT
;
1111 return GSOCK_NOERROR
;
1114 /* _GSocket_Output_Timeout:
1115 * For blocking sockets, wait until data can be sent without
1116 * blocking or until timeout ellapses.
1118 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
1124 /* Linux select() will overwrite the struct on return */
1125 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
1126 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
1128 GSocket_Debug( ("m_non_blocking has: %d\n", (int)socket
->m_non_blocking
) );
1130 if (!socket
->m_non_blocking
)
1133 FD_SET(socket
->m_fd
, &writefds
);
1134 ret
= select(socket
->m_fd
+ 1, NULL
, &writefds
, NULL
, &tv
);
1137 GSocket_Debug(( "GSocket_Output_Timeout, select returned 0\n" ));
1138 socket
->m_error
= GSOCK_TIMEDOUT
;
1139 return GSOCK_TIMEDOUT
;
1143 GSocket_Debug(( "GSocket_Output_Timeout, select returned -1\n" ));
1144 if (errno
== EBADF
) GSocket_Debug(( "Invalid file descriptor\n" ));
1145 if (errno
== EINTR
) GSocket_Debug(( "A non blocked signal was caught\n" ));
1146 if (errno
== EINVAL
) GSocket_Debug(( "The highest number descriptor is negative\n" ));
1147 if (errno
== ENOMEM
) GSocket_Debug(( "Not enough memory\n" ));
1148 socket
->m_error
= GSOCK_TIMEDOUT
;
1149 return GSOCK_TIMEDOUT
;
1151 if ( ! FD_ISSET(socket
->m_fd
, &writefds
) )
1152 GSocket_Debug(( "GSocket_Output_Timeout is buggy!\n" ));
1154 GSocket_Debug(( "GSocket_Output_Timeout seems correct\n" ));
1158 GSocket_Debug(( "GSocket_Output_Timeout, didn't try select!\n" ));
1161 return GSOCK_NOERROR
;
1164 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
1166 return recv(socket
->m_fd
, buffer
, size
, 0);
1169 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
1171 struct sockaddr from
;
1172 SOCKLEN_T fromlen
= sizeof(from
);
1176 fromlen
= sizeof(from
);
1178 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, (SOCKLEN_T
*) &fromlen
);
1183 /* Translate a system address into a GSocket address */
1184 if (!socket
->m_peer
)
1186 socket
->m_peer
= GAddress_new();
1187 if (!socket
->m_peer
)
1189 socket
->m_error
= GSOCK_MEMERR
;
1193 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
1194 if (err
!= GSOCK_NOERROR
)
1196 GAddress_destroy(socket
->m_peer
);
1197 socket
->m_peer
= NULL
;
1198 socket
->m_error
= err
;
1205 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
1211 ret
= send(socket
->m_fd
, buffer
, size
, 0);
1214 ret
= send(socket
->m_fd
, (char *)buffer
, size
, 0);
1219 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
1221 struct sockaddr
*addr
;
1225 if (!socket
->m_peer
)
1227 socket
->m_error
= GSOCK_INVADDR
;
1231 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
1232 if (err
!= GSOCK_NOERROR
)
1234 socket
->m_error
= err
;
1240 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
1243 ret
= sendto(socket
->m_fd
, (char *)buffer
, size
, 0, addr
, len
);
1246 /* Frees memory allocated from _GAddress_translate_to */
1252 void _GSocket_Detected_Read(GSocket
*socket
)
1256 /* If we have already detected a LOST event, then don't try
1257 * to do any further processing.
1259 if ((socket
->m_detected
& GSOCK_LOST_FLAG
) != 0)
1261 socket
->m_establishing
= FALSE
;
1263 CALL_CALLBACK(socket
, GSOCK_LOST
);
1264 GSocket_Shutdown(socket
);
1268 if (recv(socket
->m_fd
, &c
, 1, MSG_PEEK
) > 0)
1270 CALL_CALLBACK(socket
, GSOCK_INPUT
);
1274 if (socket
->m_server
&& socket
->m_stream
)
1276 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
1280 CALL_CALLBACK(socket
, GSOCK_LOST
);
1281 GSocket_Shutdown(socket
);
1286 void _GSocket_Detected_Write(GSocket
*socket
)
1288 /* If we have already detected a LOST event, then don't try
1289 * to do any further processing.
1291 if ((socket
->m_detected
& GSOCK_LOST_FLAG
) != 0)
1293 socket
->m_establishing
= FALSE
;
1295 CALL_CALLBACK(socket
, GSOCK_LOST
);
1296 GSocket_Shutdown(socket
);
1300 if (socket
->m_establishing
&& !socket
->m_server
)
1303 SOCKLEN_T len
= sizeof(error
);
1305 socket
->m_establishing
= FALSE
;
1307 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*)&error
, &len
);
1311 CALL_CALLBACK(socket
, GSOCK_LOST
);
1312 GSocket_Shutdown(socket
);
1316 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
1317 /* We have to fire this event by hand because CONNECTION (for clients)
1318 * and OUTPUT are internally the same and we just disabled CONNECTION
1319 * events with the above macro.
1321 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
1326 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
1331 * -------------------------------------------------------------------------
1333 * -------------------------------------------------------------------------
1336 /* CHECK_ADDRESS verifies that the current address family is either
1337 * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it
1338 * initalizes it to be a GSOCK_*family*. In other cases, it returns
1339 * an appropiate error code.
1341 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
1343 #define CHECK_ADDRESS(address, family) \
1345 if (address->m_family == GSOCK_NOFAMILY) \
1346 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1347 return address->m_error; \
1348 if (address->m_family != GSOCK_##family) \
1350 address->m_error = GSOCK_INVADDR; \
1351 return GSOCK_INVADDR; \
1355 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
1357 if (address->m_family == GSOCK_NOFAMILY) \
1358 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1360 if (address->m_family != GSOCK_##family) \
1362 address->m_error = GSOCK_INVADDR; \
1368 GAddress
*GAddress_new(void)
1372 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1375 address
->m_family
= GSOCK_NOFAMILY
;
1376 address
->m_addr
= NULL
;
1382 GAddress
*GAddress_copy(GAddress
*address
)
1386 assert(address
!= NULL
);
1388 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1391 memcpy(addr2
, address
, sizeof(GAddress
));
1393 if (address
->m_addr
&& address
->m_len
> 0)
1395 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
1396 if (addr2
->m_addr
== NULL
)
1401 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1407 void GAddress_destroy(GAddress
*address
)
1409 assert(address
!= NULL
);
1411 if (address
->m_addr
)
1412 free(address
->m_addr
);
1414 /* free(address); */
1417 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1419 assert(address
!= NULL
);
1421 address
->m_family
= type
;
1424 GAddressType
GAddress_GetFamily(GAddress
*address
)
1426 assert(address
!= NULL
);
1428 return address
->m_family
;
1431 GSocketError
_GAddress_translate_from(GAddress
*address
,
1432 struct sockaddr
*addr
, int len
)
1434 address
->m_realfamily
= addr
->sa_family
;
1435 switch (addr
->sa_family
)
1438 address
->m_family
= GSOCK_INET
;
1441 address
->m_family
= GSOCK_UNIX
;
1445 address
->m_family
= GSOCK_INET6
;
1450 address
->m_error
= GSOCK_INVOP
;
1455 if (address
->m_addr
)
1456 free(address
->m_addr
);
1458 address
->m_len
= len
;
1459 address
->m_addr
= (struct sockaddr
*)malloc(len
);
1461 if (address
->m_addr
== NULL
)
1463 address
->m_error
= GSOCK_MEMERR
;
1464 return GSOCK_MEMERR
;
1466 memcpy(address
->m_addr
, addr
, len
);
1468 return GSOCK_NOERROR
;
1471 GSocketError
_GAddress_translate_to(GAddress
*address
,
1472 struct sockaddr
**addr
, int *len
)
1474 if (!address
->m_addr
)
1476 address
->m_error
= GSOCK_INVADDR
;
1477 return GSOCK_INVADDR
;
1480 *len
= address
->m_len
;
1481 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
1484 address
->m_error
= GSOCK_MEMERR
;
1485 return GSOCK_MEMERR
;
1488 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1489 return GSOCK_NOERROR
;
1493 * -------------------------------------------------------------------------
1494 * Internet address family
1495 * -------------------------------------------------------------------------
1498 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1500 address
->m_len
= sizeof(struct sockaddr_in
);
1501 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1502 if (address
->m_addr
== NULL
)
1504 address
->m_error
= GSOCK_MEMERR
;
1505 return GSOCK_MEMERR
;
1508 address
->m_family
= GSOCK_INET
;
1509 address
->m_realfamily
= PF_INET
;
1510 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1511 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1513 return GSOCK_NOERROR
;
1516 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1519 struct in_addr
*addr
;
1521 assert(address
!= NULL
);
1523 CHECK_ADDRESS(address
, INET
);
1525 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1527 /* If it is a numeric host name, convert it now */
1528 #if defined(HAVE_INET_ATON)
1529 if (inet_aton(hostname
, addr
) == 0)
1531 #elif defined(HAVE_INET_ADDR)
1532 addr
->s_addr
= inet_addr(hostname
);
1533 if ( addr
->s_addr
== -1 )
1536 /* Use gethostbyname by default */
1537 int val
= 1; //VA doesn't like constants in conditional expressions at all
1541 struct in_addr
*array_addr
;
1543 /* It is a real name, we solve it */
1544 if ((he
= gethostbyname(hostname
)) == NULL
)
1546 /* Reset to invalid address */
1547 addr
->s_addr
= INADDR_NONE
;
1548 address
->m_error
= GSOCK_NOHOST
;
1549 return GSOCK_NOHOST
;
1551 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1552 addr
->s_addr
= array_addr
[0].s_addr
;
1554 return GSOCK_NOERROR
;
1557 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1559 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1562 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1563 unsigned long hostaddr
)
1565 struct in_addr
*addr
;
1567 assert(address
!= NULL
);
1569 CHECK_ADDRESS(address
, INET
);
1571 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1572 addr
->s_addr
= hostaddr
;
1574 return GSOCK_NOERROR
;
1577 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1578 const char *protocol
)
1581 struct sockaddr_in
*addr
;
1583 assert(address
!= NULL
);
1584 CHECK_ADDRESS(address
, INET
);
1588 address
->m_error
= GSOCK_INVPORT
;
1589 return GSOCK_INVPORT
;
1592 se
= getservbyname(port
, protocol
);
1595 /* the cast to int suppresses compiler warnings about subscript having the
1597 if (isdigit((int)port
[0]))
1601 port_int
= atoi(port
);
1602 addr
= (struct sockaddr_in
*)address
->m_addr
;
1603 addr
->sin_port
= htons(port_int
);
1604 return GSOCK_NOERROR
;
1607 address
->m_error
= GSOCK_INVPORT
;
1608 return GSOCK_INVPORT
;
1611 addr
= (struct sockaddr_in
*)address
->m_addr
;
1612 addr
->sin_port
= se
->s_port
;
1614 return GSOCK_NOERROR
;
1617 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1619 struct sockaddr_in
*addr
;
1621 assert(address
!= NULL
);
1622 CHECK_ADDRESS(address
, INET
);
1624 addr
= (struct sockaddr_in
*)address
->m_addr
;
1625 addr
->sin_port
= htons(port
);
1627 return GSOCK_NOERROR
;
1630 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1634 struct sockaddr_in
*addr
;
1636 assert(address
!= NULL
);
1637 CHECK_ADDRESS(address
, INET
);
1639 addr
= (struct sockaddr_in
*)address
->m_addr
;
1640 addr_buf
= (char *)&(addr
->sin_addr
);
1642 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1645 address
->m_error
= GSOCK_NOHOST
;
1646 return GSOCK_NOHOST
;
1649 strncpy(hostname
, he
->h_name
, sbuf
);
1651 return GSOCK_NOERROR
;
1654 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1656 struct sockaddr_in
*addr
;
1658 assert(address
!= NULL
);
1659 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1661 addr
= (struct sockaddr_in
*)address
->m_addr
;
1663 return addr
->sin_addr
.s_addr
;
1666 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1668 struct sockaddr_in
*addr
;
1670 assert(address
!= NULL
);
1671 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1673 addr
= (struct sockaddr_in
*)address
->m_addr
;
1674 return ntohs(addr
->sin_port
);
1678 * -------------------------------------------------------------------------
1679 * Unix address family
1680 * -------------------------------------------------------------------------
1684 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1686 address
->m_len
= sizeof(struct sockaddr_un
);
1687 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
1688 if (address
->m_addr
== NULL
)
1690 address
->m_error
= GSOCK_MEMERR
;
1691 return GSOCK_MEMERR
;
1694 address
->m_family
= GSOCK_UNIX
;
1695 address
->m_realfamily
= PF_UNIX
;
1696 ((struct sockaddr_un
*)address
->m_addr
)->sun_family
= AF_UNIX
;
1697 ((struct sockaddr_un
*)address
->m_addr
)->sun_path
[0] = 0;
1699 return GSOCK_NOERROR
;
1702 #define UNIX_SOCK_PATHLEN (sizeof(addr->sun_path)/sizeof(addr->sun_path[0]))
1704 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1706 struct sockaddr_un
*addr
;
1708 assert(address
!= NULL
);
1710 CHECK_ADDRESS(address
, UNIX
);
1712 addr
= ((struct sockaddr_un
*)address
->m_addr
);
1713 strncpy(addr
->sun_path
, path
, UNIX_SOCK_PATHLEN
);
1714 addr
->sun_path
[UNIX_SOCK_PATHLEN
- 1] = '\0';
1716 return GSOCK_NOERROR
;
1719 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1721 struct sockaddr_un
*addr
;
1723 assert(address
!= NULL
);
1724 CHECK_ADDRESS(address
, UNIX
);
1726 addr
= (struct sockaddr_un
*)address
->m_addr
;
1728 strncpy(path
, addr
->sun_path
, sbuf
);
1730 return GSOCK_NOERROR
;
1735 /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */