]>
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); \
134 #ifndef __GSOCKET_STANDALONE__
135 # include "wx/unix/gsockunx.h"
136 # include "wx/gsocket.h"
138 # include "gsockunx.h"
139 # include "gsocket.h"
140 #endif /* __GSOCKET_STANDALONE__ */
142 /* redefine some GUI-only functions to do nothing in console mode */
143 #if defined(wxUSE_GUI) && !wxUSE_GUI
144 # define _GSocket_GUI_Init(socket) (1)
145 # define _GSocket_GUI_Destroy(socket)
146 # define _GSocket_Enable_Events(socket)
147 # define _GSocket_Disable_Events(socket)
148 # define _GSocket_Install_Callback(socket, event)
149 # define _GSocket_Uninstall_Callback(socket, event)
150 #endif /* wxUSE_GUI */
152 /* debugging helpers */
153 #define __GSOCKET_DEBUG__
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_destroy(GSocket
*socket
)
213 assert(socket
!= NULL
);
215 /* Check that the socket is really shutdowned */
216 if (socket
->m_fd
!= INVALID_SOCKET
)
217 GSocket_Shutdown(socket
);
219 /* Per-socket GUI-specific cleanup */
220 _GSocket_GUI_Destroy(socket
);
222 /* Destroy private addresses */
224 GAddress_destroy(socket
->m_local
);
227 GAddress_destroy(socket
->m_peer
);
229 /* Destroy the socket itself */
234 * Disallow further read/write operations on this socket, close
235 * the fd and disable all callbacks.
237 void GSocket_Shutdown(GSocket
*socket
)
241 assert(socket
!= NULL
);
243 /* If socket has been created, shutdown it */
244 if (socket
->m_fd
!= INVALID_SOCKET
)
246 shutdown(socket
->m_fd
, 2);
247 soclose(socket
->m_fd
);
248 socket
->m_fd
= INVALID_SOCKET
;
251 /* Disable GUI callbacks */
252 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
253 socket
->m_cbacks
[evt
] = NULL
;
255 socket
->m_detected
= GSOCK_LOST_FLAG
;
256 _GSocket_Disable_Events(socket
);
259 /* Address handling */
265 * Set or get the local or peer address for this socket. The 'set'
266 * functions return GSOCK_NOERROR on success, an error code otherwise.
267 * The 'get' functions return a pointer to a GAddress object on success,
268 * or NULL otherwise, in which case they set the error code of the
269 * corresponding GSocket.
272 * GSOCK_INVSOCK - the socket is not valid.
273 * GSOCK_INVADDR - the address is not valid.
275 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
277 assert(socket
!= NULL
);
279 /* the socket must be initialized, or it must be a server */
280 if ((socket
->m_fd
!= INVALID_SOCKET
&& !socket
->m_server
))
282 socket
->m_error
= GSOCK_INVSOCK
;
283 return GSOCK_INVSOCK
;
287 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
289 socket
->m_error
= GSOCK_INVADDR
;
290 return GSOCK_INVADDR
;
294 GAddress_destroy(socket
->m_local
);
296 socket
->m_local
= GAddress_copy(address
);
298 return GSOCK_NOERROR
;
301 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
303 assert(socket
!= NULL
);
306 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
308 socket
->m_error
= GSOCK_INVADDR
;
309 return GSOCK_INVADDR
;
313 GAddress_destroy(socket
->m_peer
);
315 socket
->m_peer
= GAddress_copy(address
);
317 return GSOCK_NOERROR
;
320 GAddress
*GSocket_GetLocal(GSocket
*socket
)
323 struct sockaddr addr
;
324 SOCKLEN_T size
= sizeof(addr
);
327 assert(socket
!= NULL
);
329 /* try to get it from the m_local var first */
331 return GAddress_copy(socket
->m_local
);
333 /* else, if the socket is initialized, try getsockname */
334 if (socket
->m_fd
== INVALID_SOCKET
)
336 socket
->m_error
= GSOCK_INVSOCK
;
340 if (getsockname(socket
->m_fd
, &addr
, (SOCKLEN_T
*) &size
) < 0)
342 socket
->m_error
= GSOCK_IOERR
;
346 /* got a valid address from getsockname, create a GAddress object */
347 address
= GAddress_new();
350 socket
->m_error
= GSOCK_MEMERR
;
354 err
= _GAddress_translate_from(address
, &addr
, size
);
355 if (err
!= GSOCK_NOERROR
)
357 GAddress_destroy(address
);
358 socket
->m_error
= err
;
365 GAddress
*GSocket_GetPeer(GSocket
*socket
)
367 assert(socket
!= NULL
);
369 /* try to get it from the m_peer var */
371 return GAddress_copy(socket
->m_peer
);
376 /* Server specific parts */
378 /* GSocket_SetServer:
379 * Sets up this socket as a server. The local address must have been
380 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
381 * Returns GSOCK_NOERROR on success, one of the following otherwise:
384 * GSOCK_INVSOCK - the socket is in use.
385 * GSOCK_INVADDR - the local address has not been set.
386 * GSOCK_IOERR - low-level error.
388 GSocketError
GSocket_SetServer(GSocket
*sck
)
394 /* must not be in use */
395 if (sck
->m_fd
!= INVALID_SOCKET
)
397 sck
->m_error
= GSOCK_INVSOCK
;
398 return GSOCK_INVSOCK
;
401 /* the local addr must have been set */
404 sck
->m_error
= GSOCK_INVADDR
;
405 return GSOCK_INVADDR
;
408 /* Initialize all fields */
409 sck
->m_stream
= TRUE
;
410 sck
->m_server
= TRUE
;
411 sck
->m_oriented
= TRUE
;
413 /* Create the socket */
414 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
416 if (sck
->m_fd
== INVALID_SOCKET
)
418 sck
->m_error
= GSOCK_IOERR
;
422 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
423 _GSocket_Enable_Events(sck
);
425 /* Bind to the local address,
426 * retrieve the actual address bound,
427 * and listen up to 5 connections.
429 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
430 (getsockname(sck
->m_fd
,
431 sck
->m_local
->m_addr
,
432 (SOCKLEN_T
*) &sck
->m_local
->m_len
) != 0) ||
433 (listen(sck
->m_fd
, 5) != 0))
436 sck
->m_fd
= INVALID_SOCKET
;
437 sck
->m_error
= GSOCK_IOERR
;
441 return GSOCK_NOERROR
;
444 /* GSocket_WaitConnection:
445 * Waits for an incoming client connection. Returns a pointer to
446 * a GSocket object, or NULL if there was an error, in which case
447 * the last error field will be updated for the calling GSocket.
449 * Error codes (set in the calling GSocket)
450 * GSOCK_INVSOCK - the socket is not valid or not a server.
451 * GSOCK_TIMEDOUT - timeout, no incoming connections.
452 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
453 * GSOCK_MEMERR - couldn't allocate memory.
454 * GSOCK_IOERR - low-level error.
456 GSocket
*GSocket_WaitConnection(GSocket
*socket
)
458 struct sockaddr from
;
459 SOCKLEN_T fromlen
= sizeof(from
);
464 assert(socket
!= NULL
);
466 /* Reenable CONNECTION events */
467 _GSocket_Enable(socket
, GSOCK_CONNECTION
);
469 /* If the socket has already been created, we exit immediately */
470 if (socket
->m_fd
== INVALID_SOCKET
|| !socket
->m_server
)
472 socket
->m_error
= GSOCK_INVSOCK
;
476 /* Create a GSocket object for the new connection */
477 connection
= GSocket_new();
481 socket
->m_error
= GSOCK_MEMERR
;
485 /* Wait for a connection (with timeout) */
486 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
488 GSocket_destroy(connection
);
489 /* socket->m_error set by _GSocket_Input_Timeout */
493 connection
->m_fd
= accept(socket
->m_fd
, &from
, (SOCKLEN_T
*) &fromlen
);
495 if (connection
->m_fd
== INVALID_SOCKET
)
497 if (errno
== EWOULDBLOCK
)
498 socket
->m_error
= GSOCK_WOULDBLOCK
;
500 socket
->m_error
= GSOCK_IOERR
;
502 GSocket_destroy(connection
);
506 /* Initialize all fields */
507 connection
->m_server
= FALSE
;
508 connection
->m_stream
= TRUE
;
509 connection
->m_oriented
= TRUE
;
511 /* Setup the peer address field */
512 connection
->m_peer
= GAddress_new();
513 if (!connection
->m_peer
)
515 GSocket_destroy(connection
);
516 socket
->m_error
= GSOCK_MEMERR
;
519 err
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
);
520 if (err
!= GSOCK_NOERROR
)
522 GAddress_destroy(connection
->m_peer
);
523 GSocket_destroy(connection
);
524 socket
->m_error
= err
;
528 ioctl(connection
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
529 _GSocket_Enable_Events(connection
);
534 /* Client specific parts */
537 * For stream (connection oriented) sockets, GSocket_Connect() tries
538 * to establish a client connection to a server using the peer address
539 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
540 * connection has been succesfully established, or one of the error
541 * codes listed below. Note that for nonblocking sockets, a return
542 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
543 * request can be completed later; you should use GSocket_Select()
544 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
545 * corresponding asynchronous events.
547 * For datagram (non connection oriented) sockets, GSocket_Connect()
548 * just sets the peer address established with GSocket_SetPeer() as
549 * default destination.
552 * GSOCK_INVSOCK - the socket is in use or not valid.
553 * GSOCK_INVADDR - the peer address has not been established.
554 * GSOCK_TIMEDOUT - timeout, the connection failed.
555 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
556 * GSOCK_MEMERR - couldn't allocate memory.
557 * GSOCK_IOERR - low-level error.
559 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
566 /* Enable CONNECTION events (needed for nonblocking connections) */
567 _GSocket_Enable(sck
, GSOCK_CONNECTION
);
569 if (sck
->m_fd
!= INVALID_SOCKET
)
571 sck
->m_error
= GSOCK_INVSOCK
;
572 return GSOCK_INVSOCK
;
577 sck
->m_error
= GSOCK_INVADDR
;
578 return GSOCK_INVADDR
;
581 /* Streamed or dgram socket? */
582 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
583 sck
->m_oriented
= TRUE
;
584 sck
->m_server
= FALSE
;
585 sck
->m_establishing
= FALSE
;
587 /* Create the socket */
588 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
,
589 sck
->m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
591 if (sck
->m_fd
== INVALID_SOCKET
)
593 sck
->m_error
= GSOCK_IOERR
;
597 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
598 _GSocket_Enable_Events(sck
);
600 /* Connect it to the peer address, with a timeout (see below) */
601 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
603 printf("connect on %d to %X (%d) returned %d, errno = %d\n",
604 sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
, ret
, errno
);
609 /* If connect failed with EINPROGRESS and the GSocket object
610 * is in blocking mode, we select() for the specified timeout
611 * checking for writability to see if the connection request
614 if ((err
== EINPROGRESS
) && (!sck
->m_non_blocking
))
616 if (_GSocket_Output_Timeout(sck
) == GSOCK_TIMEDOUT
)
619 sck
->m_fd
= INVALID_SOCKET
;
620 /* sck->m_error is set in _GSocket_Output_Timeout */
621 return GSOCK_TIMEDOUT
;
626 SOCKLEN_T len
= sizeof(error
);
628 getsockopt(sck
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*) &error
, &len
);
631 return GSOCK_NOERROR
;
635 /* If connect failed with EINPROGRESS and the GSocket object
636 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
637 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
638 * this way if the connection completes, a GSOCK_CONNECTION
639 * event will be generated, if enabled.
641 if ((err
== EINPROGRESS
) && (sck
->m_non_blocking
))
643 sck
->m_establishing
= TRUE
;
644 sck
->m_error
= GSOCK_WOULDBLOCK
;
645 return GSOCK_WOULDBLOCK
;
648 /* If connect failed with an error other than EINPROGRESS,
649 * then the call to GSocket_Connect has failed.
652 sck
->m_fd
= INVALID_SOCKET
;
653 sck
->m_error
= GSOCK_IOERR
;
657 return GSOCK_NOERROR
;
660 /* Datagram sockets */
662 /* GSocket_SetNonOriented:
663 * Sets up this socket as a non-connection oriented (datagram) socket.
664 * Before using this function, the local address must have been set
665 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
666 * on success, or one of the following otherwise.
669 * GSOCK_INVSOCK - the socket is in use.
670 * GSOCK_INVADDR - the local address has not been set.
671 * GSOCK_IOERR - low-level error.
673 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
679 if (sck
->m_fd
!= INVALID_SOCKET
)
681 sck
->m_error
= GSOCK_INVSOCK
;
682 return GSOCK_INVSOCK
;
687 sck
->m_error
= GSOCK_INVADDR
;
688 return GSOCK_INVADDR
;
691 /* Initialize all fields */
692 sck
->m_stream
= FALSE
;
693 sck
->m_server
= FALSE
;
694 sck
->m_oriented
= FALSE
;
696 /* Create the socket */
697 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
699 if (sck
->m_fd
== INVALID_SOCKET
)
701 sck
->m_error
= GSOCK_IOERR
;
705 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
706 _GSocket_Enable_Events(sck
);
708 /* Bind to the local address,
709 * and retrieve the actual address bound.
711 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
712 (getsockname(sck
->m_fd
,
713 sck
->m_local
->m_addr
,
714 (SOCKLEN_T
*) &sck
->m_local
->m_len
) != 0))
717 sck
->m_fd
= INVALID_SOCKET
;
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 known state first */
852 result
|= (GSOCK_CONNECTION_FLAG
& socket
->m_detected
& flags
);
853 result
|= (GSOCK_LOST_FLAG
& socket
->m_detected
& flags
);
856 if (select(socket
->m_fd
+ 1, &readfds
, &writefds
, &exceptfds
, &tv
) <= 0)
859 /* Check for readability */
860 if (FD_ISSET(socket
->m_fd
, &readfds
))
864 if (recv(socket
->m_fd
, &c
, 1, MSG_PEEK
) > 0)
866 result
|= (GSOCK_INPUT_FLAG
& flags
);
870 if (socket
->m_server
&& socket
->m_stream
)
872 result
|= (GSOCK_CONNECTION_FLAG
& flags
);
873 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
877 result
|= (GSOCK_LOST_FLAG
& flags
);
878 socket
->m_detected
= GSOCK_LOST_FLAG
;
883 /* Check for writability */
884 if (FD_ISSET(socket
->m_fd
, &writefds
))
886 if (socket
->m_establishing
&& !socket
->m_server
)
889 SOCKLEN_T len
= sizeof(error
);
891 socket
->m_establishing
= FALSE
;
893 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*)&error
, &len
);
897 result
|= (GSOCK_LOST_FLAG
& flags
);
898 socket
->m_detected
= GSOCK_LOST_FLAG
;
902 result
|= (GSOCK_CONNECTION_FLAG
& flags
);
903 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
908 result
|= (GSOCK_OUTPUT_FLAG
& flags
);
912 /* Check for exceptions and errors (is this useful in Unices?) */
913 if (FD_ISSET(socket
->m_fd
, &exceptfds
))
915 result
|= (GSOCK_LOST_FLAG
& flags
);
916 socket
->m_establishing
= FALSE
;
917 socket
->m_detected
= GSOCK_LOST_FLAG
;
924 assert(socket
!= NULL
);
925 return flags
& socket
->m_detected
;
927 #endif /* !wxUSE_GUI */
932 /* GSocket_SetNonBlocking:
933 * Sets the socket to non-blocking mode. All IO calls will return
936 void GSocket_SetNonBlocking(GSocket
*socket
, int non_block
)
938 assert(socket
!= NULL
);
940 GSocket_Debug( ("GSocket_SetNonBlocking: %d\n", (int)non_block
) );
942 socket
->m_non_blocking
= non_block
;
945 /* GSocket_SetTimeout:
946 * Sets the timeout for blocking calls. Time is expressed in
949 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
)
951 assert(socket
!= NULL
);
953 socket
->m_timeout
= millisec
;
957 * Returns the last error occured for this socket. Note that successful
958 * operations do not clear this back to GSOCK_NOERROR, so use it only
961 GSocketError
GSocket_GetError(GSocket
*socket
)
963 assert(socket
!= NULL
);
965 return socket
->m_error
;
971 * There is data to be read in the input buffer. If, after a read
972 * operation, there is still data available, the callback function will
975 * The socket is available for writing. That is, the next write call
976 * won't block. This event is generated only once, when the connection is
977 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
978 * when the output buffer empties again. This means that the app should
979 * assume that it can write since the first OUTPUT event, and no more
980 * OUTPUT events will be generated unless an error occurs.
982 * Connection succesfully established, for client sockets, or incoming
983 * client connection, for server sockets. Wait for this event (also watch
984 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
986 * The connection is lost (or a connection request failed); this could
987 * be due to a failure, or due to the peer closing it gracefully.
990 /* GSocket_SetCallback:
991 * Enables the callbacks specified by 'flags'. Note that 'flags'
992 * may be a combination of flags OR'ed toghether, so the same
993 * callback function can be made to accept different events.
994 * The callback function must have the following prototype:
996 * void function(GSocket *socket, GSocketEvent event, char *cdata)
998 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
999 GSocketCallback callback
, char *cdata
)
1003 assert(socket
!= NULL
);
1005 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
1007 if ((flags
& (1 << count
)) != 0)
1009 printf("Setting callback no %d for socket at %X to address %X,data %X\n",
1010 count
, socket
, callback
, cdata
);
1011 socket
->m_cbacks
[count
] = callback
;
1012 socket
->m_data
[count
] = cdata
;
1017 /* GSocket_UnsetCallback:
1018 * Disables all callbacks specified by 'flags', which may be a
1019 * combination of flags OR'ed toghether.
1021 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
1025 assert(socket
!= NULL
);
1027 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
1029 if ((flags
& (1 << count
)) != 0)
1031 printf("Removing callback no %d for socket at %X",
1033 socket
->m_cbacks
[count
] = NULL
;
1034 socket
->m_data
[count
] = NULL
;
1040 #define CALL_CALLBACK(socket, event) { \
1041 _GSocket_Disable(socket, event); \
1042 if (socket->m_cbacks[event]){ \
1043 printf("Call callback for event %d, socket %X: %X, %X\n", event, \
1044 socket, socket->m_cbacks[event], socket->m_data[event]); \
1045 socket->m_cbacks[event](socket, event, socket->m_data[event]); \
1050 void _GSocket_Enable(GSocket
*socket
, GSocketEvent event
)
1052 socket
->m_detected
&= ~(1 << event
);
1053 _GSocket_Install_Callback(socket
, event
);
1056 void _GSocket_Disable(GSocket
*socket
, GSocketEvent event
)
1058 socket
->m_detected
|= (1 << event
);
1059 _GSocket_Uninstall_Callback(socket
, event
);
1062 /* _GSocket_Input_Timeout:
1063 * For blocking sockets, wait until data is available or
1064 * until timeout ellapses.
1066 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
1072 /* Linux select() will overwrite the struct on return */
1073 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
1074 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
1076 if (!socket
->m_non_blocking
)
1079 FD_SET(socket
->m_fd
, &readfds
);
1080 ret
= select(socket
->m_fd
+ 1, &readfds
, NULL
, NULL
, &tv
);
1083 GSocket_Debug(( "GSocket_Input_Timeout, select returned 0\n" ));
1084 socket
->m_error
= GSOCK_TIMEDOUT
;
1085 return GSOCK_TIMEDOUT
;
1089 GSocket_Debug(( "GSocket_Input_Timeout, select returned -1\n" ));
1090 if (errno
== EBADF
) GSocket_Debug(( "Invalid file descriptor\n" ));
1091 if (errno
== EINTR
) GSocket_Debug(( "A non blocked signal was caught\n" ));
1092 if (errno
== EINVAL
) GSocket_Debug(( "The highest number descriptor is negative\n" ));
1093 if (errno
== ENOMEM
) GSocket_Debug(( "Not enough memory\n" ));
1094 socket
->m_error
= GSOCK_TIMEDOUT
;
1095 return GSOCK_TIMEDOUT
;
1098 return GSOCK_NOERROR
;
1101 /* _GSocket_Output_Timeout:
1102 * For blocking sockets, wait until data can be sent without
1103 * blocking or until timeout ellapses.
1105 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
1111 /* Linux select() will overwrite the struct on return */
1112 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
1113 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
1115 GSocket_Debug( ("m_non_blocking has: %d\n", (int)socket
->m_non_blocking
) );
1117 if (!socket
->m_non_blocking
)
1120 FD_SET(socket
->m_fd
, &writefds
);
1121 ret
= select(socket
->m_fd
+ 1, NULL
, &writefds
, NULL
, &tv
);
1124 GSocket_Debug(( "GSocket_Output_Timeout, select returned 0\n" ));
1125 socket
->m_error
= GSOCK_TIMEDOUT
;
1126 return GSOCK_TIMEDOUT
;
1130 GSocket_Debug(( "GSocket_Output_Timeout, select returned -1\n" ));
1131 if (errno
== EBADF
) GSocket_Debug(( "Invalid file descriptor\n" ));
1132 if (errno
== EINTR
) GSocket_Debug(( "A non blocked signal was caught\n" ));
1133 if (errno
== EINVAL
) GSocket_Debug(( "The highest number descriptor is negative\n" ));
1134 if (errno
== ENOMEM
) GSocket_Debug(( "Not enough memory\n" ));
1135 socket
->m_error
= GSOCK_TIMEDOUT
;
1136 return GSOCK_TIMEDOUT
;
1138 if ( ! FD_ISSET(socket
->m_fd
, &writefds
) )
1139 GSocket_Debug(( "GSocket_Output_Timeout is buggy!\n" ));
1141 GSocket_Debug(( "GSocket_Output_Timeout seems correct\n" ));
1145 GSocket_Debug(( "GSocket_Output_Timeout, didn't try select!\n" ));
1148 return GSOCK_NOERROR
;
1151 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
1153 return recv(socket
->m_fd
, buffer
, size
, 0);
1156 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
1158 struct sockaddr from
;
1159 SOCKLEN_T fromlen
= sizeof(from
);
1163 fromlen
= sizeof(from
);
1165 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, (SOCKLEN_T
*) &fromlen
);
1170 /* Translate a system address into a GSocket address */
1171 if (!socket
->m_peer
)
1173 socket
->m_peer
= GAddress_new();
1174 if (!socket
->m_peer
)
1176 socket
->m_error
= GSOCK_MEMERR
;
1180 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
1181 if (err
!= GSOCK_NOERROR
)
1183 GAddress_destroy(socket
->m_peer
);
1184 socket
->m_peer
= NULL
;
1185 socket
->m_error
= err
;
1192 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
1198 ret
= send(socket
->m_fd
, buffer
, size
, 0);
1201 ret
= send(socket
->m_fd
, (char *)buffer
, size
, 0);
1206 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
1208 struct sockaddr
*addr
;
1212 if (!socket
->m_peer
)
1214 socket
->m_error
= GSOCK_INVADDR
;
1218 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
1219 if (err
!= GSOCK_NOERROR
)
1221 socket
->m_error
= err
;
1227 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
1230 ret
= sendto(socket
->m_fd
, (char *)buffer
, size
, 0, addr
, len
);
1233 /* Frees memory allocated from _GAddress_translate_to */
1239 void _GSocket_Detected_Read(GSocket
*socket
)
1243 if (recv(socket
->m_fd
, &c
, 1, MSG_PEEK
) > 0)
1245 CALL_CALLBACK(socket
, GSOCK_INPUT
);
1249 if (socket
->m_server
&& socket
->m_stream
)
1251 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
1255 CALL_CALLBACK(socket
, GSOCK_LOST
);
1260 void _GSocket_Detected_Write(GSocket
*socket
)
1262 if (socket
->m_establishing
&& !socket
->m_server
)
1265 SOCKLEN_T len
= sizeof(error
);
1267 socket
->m_establishing
= FALSE
;
1269 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*)&error
, &len
);
1273 CALL_CALLBACK(socket
, GSOCK_LOST
);
1277 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
1278 /* We have to fire this event by hand because CONNECTION (for clients)
1279 * and OUTPUT are internally the same and we just disabled CONNECTION
1280 * events with the above macro.
1282 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
1287 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
1292 * -------------------------------------------------------------------------
1294 * -------------------------------------------------------------------------
1297 /* CHECK_ADDRESS verifies that the current address family is either
1298 * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it
1299 * initalizes it to be a GSOCK_*family*. In other cases, it returns
1300 * an appropiate error code.
1302 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
1304 #define CHECK_ADDRESS(address, family) \
1306 if (address->m_family == GSOCK_NOFAMILY) \
1307 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1308 return address->m_error; \
1309 if (address->m_family != GSOCK_##family) \
1311 address->m_error = GSOCK_INVADDR; \
1312 return GSOCK_INVADDR; \
1316 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
1318 if (address->m_family == GSOCK_NOFAMILY) \
1319 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1321 if (address->m_family != GSOCK_##family) \
1323 address->m_error = GSOCK_INVADDR; \
1329 GAddress
*GAddress_new(void)
1333 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1336 address
->m_family
= GSOCK_NOFAMILY
;
1337 address
->m_addr
= NULL
;
1343 GAddress
*GAddress_copy(GAddress
*address
)
1347 assert(address
!= NULL
);
1349 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1352 memcpy(addr2
, address
, sizeof(GAddress
));
1354 if (address
->m_addr
)
1356 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
1357 if (addr2
->m_addr
== NULL
)
1362 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1368 void GAddress_destroy(GAddress
*address
)
1370 assert(address
!= NULL
);
1372 if (address
->m_addr
)
1373 free(address
->m_addr
);
1375 /* free(address); */
1378 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1380 assert(address
!= NULL
);
1382 address
->m_family
= type
;
1385 GAddressType
GAddress_GetFamily(GAddress
*address
)
1387 assert(address
!= NULL
);
1389 return address
->m_family
;
1392 GSocketError
_GAddress_translate_from(GAddress
*address
,
1393 struct sockaddr
*addr
, int len
)
1395 address
->m_realfamily
= addr
->sa_family
;
1396 switch (addr
->sa_family
)
1399 address
->m_family
= GSOCK_INET
;
1402 address
->m_family
= GSOCK_UNIX
;
1406 address
->m_family
= GSOCK_INET6
;
1411 address
->m_error
= GSOCK_INVOP
;
1416 if (address
->m_addr
)
1417 free(address
->m_addr
);
1419 address
->m_len
= len
;
1420 address
->m_addr
= (struct sockaddr
*)malloc(len
);
1422 if (address
->m_addr
== NULL
)
1424 address
->m_error
= GSOCK_MEMERR
;
1425 return GSOCK_MEMERR
;
1427 memcpy(address
->m_addr
, addr
, len
);
1429 return GSOCK_NOERROR
;
1432 GSocketError
_GAddress_translate_to(GAddress
*address
,
1433 struct sockaddr
**addr
, int *len
)
1435 if (!address
->m_addr
)
1437 address
->m_error
= GSOCK_INVADDR
;
1438 return GSOCK_INVADDR
;
1441 *len
= address
->m_len
;
1442 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
1445 address
->m_error
= GSOCK_MEMERR
;
1446 return GSOCK_MEMERR
;
1449 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1450 return GSOCK_NOERROR
;
1454 * -------------------------------------------------------------------------
1455 * Internet address family
1456 * -------------------------------------------------------------------------
1459 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1461 address
->m_len
= sizeof(struct sockaddr_in
);
1462 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1463 if (address
->m_addr
== NULL
)
1465 address
->m_error
= GSOCK_MEMERR
;
1466 return GSOCK_MEMERR
;
1469 address
->m_family
= GSOCK_INET
;
1470 address
->m_realfamily
= PF_INET
;
1471 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1472 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1474 return GSOCK_NOERROR
;
1477 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1480 struct in_addr
*addr
;
1482 assert(address
!= NULL
);
1484 CHECK_ADDRESS(address
, INET
);
1486 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1488 /* If it is a numeric host name, convert it now */
1489 #if defined(HAVE_INET_ATON)
1490 if (inet_aton(hostname
, addr
) == 0)
1492 #elif defined(HAVE_INET_ADDR)
1493 addr
->s_addr
= inet_addr(hostname
);
1494 if ( (addr
->s_addr
== -1 )
1497 /* Use gethostbyname by default */
1498 // int val = 1; //VA doesn't like constants in conditional expressions at all
1502 struct in_addr
*array_addr
;
1504 /* It is a real name, we solve it */
1505 if ((he
= gethostbyname(hostname
)) == NULL
)
1507 /* Reset to invalid address */
1508 addr
->s_addr
= INADDR_NONE
;
1509 address
->m_error
= GSOCK_NOHOST
;
1510 return GSOCK_NOHOST
;
1512 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1513 addr
->s_addr
= array_addr
[0].s_addr
;
1515 return GSOCK_NOERROR
;
1518 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1520 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1523 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1524 unsigned long hostaddr
)
1526 struct in_addr
*addr
;
1528 assert(address
!= NULL
);
1530 CHECK_ADDRESS(address
, INET
);
1532 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1533 addr
->s_addr
= hostaddr
;
1535 return GSOCK_NOERROR
;
1538 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1539 const char *protocol
)
1542 struct sockaddr_in
*addr
;
1544 assert(address
!= NULL
);
1545 CHECK_ADDRESS(address
, INET
);
1549 address
->m_error
= GSOCK_INVPORT
;
1550 return GSOCK_INVPORT
;
1553 se
= getservbyname(port
, protocol
);
1556 if (isdigit((int)port
[0]))
1560 port_int
= atoi(port
);
1561 addr
= (struct sockaddr_in
*)address
->m_addr
;
1562 addr
->sin_port
= htons(port_int
);
1563 return GSOCK_NOERROR
;
1566 address
->m_error
= GSOCK_INVPORT
;
1567 return GSOCK_INVPORT
;
1570 addr
= (struct sockaddr_in
*)address
->m_addr
;
1571 addr
->sin_port
= se
->s_port
;
1573 return GSOCK_NOERROR
;
1576 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1578 struct sockaddr_in
*addr
;
1580 assert(address
!= NULL
);
1581 CHECK_ADDRESS(address
, INET
);
1583 addr
= (struct sockaddr_in
*)address
->m_addr
;
1584 addr
->sin_port
= htons(port
);
1586 return GSOCK_NOERROR
;
1589 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1593 struct sockaddr_in
*addr
;
1595 assert(address
!= NULL
);
1596 CHECK_ADDRESS(address
, INET
);
1598 addr
= (struct sockaddr_in
*)address
->m_addr
;
1599 addr_buf
= (char *)&(addr
->sin_addr
);
1601 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1604 address
->m_error
= GSOCK_NOHOST
;
1605 return GSOCK_NOHOST
;
1608 strncpy(hostname
, he
->h_name
, sbuf
);
1610 return GSOCK_NOERROR
;
1613 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1615 struct sockaddr_in
*addr
;
1617 assert(address
!= NULL
);
1618 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1620 addr
= (struct sockaddr_in
*)address
->m_addr
;
1622 return addr
->sin_addr
.s_addr
;
1625 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1627 struct sockaddr_in
*addr
;
1629 assert(address
!= NULL
);
1630 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1632 addr
= (struct sockaddr_in
*)address
->m_addr
;
1633 return ntohs(addr
->sin_port
);
1637 * -------------------------------------------------------------------------
1638 * Unix address family
1639 * -------------------------------------------------------------------------
1643 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1645 address
->m_len
= sizeof(struct sockaddr_un
);
1646 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
1647 if (address
->m_addr
== NULL
)
1649 address
->m_error
= GSOCK_MEMERR
;
1650 return GSOCK_MEMERR
;
1653 address
->m_family
= GSOCK_UNIX
;
1654 address
->m_realfamily
= PF_UNIX
;
1655 ((struct sockaddr_un
*)address
->m_addr
)->sun_family
= AF_UNIX
;
1656 ((struct sockaddr_un
*)address
->m_addr
)->sun_path
[0] = 0;
1658 return GSOCK_NOERROR
;
1661 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1663 struct sockaddr_un
*addr
;
1665 assert(address
!= NULL
);
1667 CHECK_ADDRESS(address
, UNIX
);
1669 addr
= ((struct sockaddr_un
*)address
->m_addr
);
1670 memcpy(addr
->sun_path
, path
, strlen(path
));
1672 return GSOCK_NOERROR
;
1675 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1677 struct sockaddr_un
*addr
;
1679 assert(address
!= NULL
);
1680 CHECK_ADDRESS(address
, UNIX
);
1682 addr
= (struct sockaddr_un
*)address
->m_addr
;
1684 strncpy(path
, addr
->sun_path
, sbuf
);
1686 return GSOCK_NOERROR
;
1691 /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */