]>
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>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
40 #define HAVE_INET_ADDR
49 #if defined(__VISAGECPP__) && __IBMCPP__ < 400
50 #include <machine\endian.h>
56 #include <sys\socket.h>
57 #include <sys\ioctl.h>
58 #include <sys\select.h>
60 #define soclose(a) close(a)
62 #define select(a,b,c,d,e) bsdselect(a,b,c,d,e)
63 int _System
bsdselect(int,
68 int _System
soclose(int);
74 #if (defined(__VISAGECPP__) && __IBMCPP__ < 400) || defined(__EMX__)
89 # define SOCKLEN_T socklen_t
92 # define SOCKLEN_T int
98 * MSW defines this, Unices don't.
100 #ifndef INVALID_SOCKET
101 #define INVALID_SOCKET -1
105 * INADDR_BROADCAST is identical to INADDR_NONE which is not defined
106 * on all systems. INADDR_BROADCAST should be fine to indicate an error.
109 #define INADDR_NONE INADDR_BROADCAST
112 #define MASK_SIGNAL() \
114 void (*old_handler)(int); \
116 old_handler = signal(SIGPIPE, SIG_IGN);
118 #define UNMASK_SIGNAL() \
119 signal(SIGPIPE, old_handler); \
122 #ifndef __GSOCKET_STANDALONE__
123 # include "wx/unix/gsockunx.h"
124 # include "wx/gsocket.h"
126 # include "gsockunx.h"
127 # include "gsocket.h"
128 #endif /* __GSOCKET_STANDALONE__ */
130 /* redefine some GUI-only functions to do nothing in console mode */
131 #if defined(wxUSE_GUI) && !wxUSE_GUI
132 # define _GSocket_GUI_Init(socket) (1)
133 # define _GSocket_GUI_Destroy(socket)
134 # define _GSocket_Enable_Events(socket)
135 # define _GSocket_Disable_Events(socket)
136 # define _GSocket_Install_Callback(socket, event)
137 # define _GSocket_Uninstall_Callback(socket, event)
138 #endif /* wxUSE_GUI */
140 /* debugging helpers */
141 #define __GSOCKET_DEBUG__
142 #ifdef __GSOCKET_DEBUG__
143 # define GSocket_Debug(args) printf args
145 # define GSocket_Debug(args)
146 #endif /* __GSOCKET_DEBUG__ */
148 /* Global initialisers */
150 int GSocket_Init(void)
155 void GSocket_Cleanup(void)
159 /* Constructors / Destructors for GSocket */
161 GSocket
*GSocket_new(void)
166 socket
= (GSocket
*)malloc(sizeof(GSocket
));
171 socket
->m_fd
= INVALID_SOCKET
;
172 for (i
=0;i
<GSOCK_MAX_EVENT
;i
++)
174 socket
->m_cbacks
[i
] = NULL
;
176 socket
->m_detected
= 0;
177 socket
->m_local
= NULL
;
178 socket
->m_peer
= NULL
;
179 socket
->m_error
= GSOCK_NOERROR
;
180 socket
->m_server
= FALSE
;
181 socket
->m_stream
= TRUE
;
182 socket
->m_gui_dependent
= NULL
;
183 socket
->m_non_blocking
= FALSE
;
184 socket
->m_timeout
= 10*60*1000;
185 /* 10 minutes * 60 sec * 1000 millisec */
186 socket
->m_establishing
= FALSE
;
188 /* Per-socket GUI-specific initialization */
189 success
= _GSocket_GUI_Init(socket
);
199 void GSocket_destroy(GSocket
*socket
)
201 assert(socket
!= NULL
);
203 /* Check that the socket is really shutdowned */
204 if (socket
->m_fd
!= INVALID_SOCKET
)
205 GSocket_Shutdown(socket
);
207 /* Per-socket GUI-specific cleanup */
208 _GSocket_GUI_Destroy(socket
);
210 /* Destroy private addresses */
212 GAddress_destroy(socket
->m_local
);
215 GAddress_destroy(socket
->m_peer
);
217 /* Destroy the socket itself */
222 * Disallow further read/write operations on this socket, close
223 * the fd and disable all callbacks.
225 void GSocket_Shutdown(GSocket
*socket
)
229 assert(socket
!= NULL
);
231 /* If socket has been created, shutdown it */
232 if (socket
->m_fd
!= INVALID_SOCKET
)
234 shutdown(socket
->m_fd
, 2);
235 soclose(socket
->m_fd
);
236 socket
->m_fd
= INVALID_SOCKET
;
239 /* Disable GUI callbacks */
240 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
241 socket
->m_cbacks
[evt
] = NULL
;
243 socket
->m_detected
= GSOCK_LOST_FLAG
;
244 _GSocket_Disable_Events(socket
);
247 /* Address handling */
253 * Set or get the local or peer address for this socket. The 'set'
254 * functions return GSOCK_NOERROR on success, an error code otherwise.
255 * The 'get' functions return a pointer to a GAddress object on success,
256 * or NULL otherwise, in which case they set the error code of the
257 * corresponding GSocket.
260 * GSOCK_INVSOCK - the socket is not valid.
261 * GSOCK_INVADDR - the address is not valid.
263 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
265 assert(socket
!= NULL
);
267 /* the socket must be initialized, or it must be a server */
268 if ((socket
->m_fd
!= INVALID_SOCKET
&& !socket
->m_server
))
270 socket
->m_error
= GSOCK_INVSOCK
;
271 return GSOCK_INVSOCK
;
275 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
277 socket
->m_error
= GSOCK_INVADDR
;
278 return GSOCK_INVADDR
;
282 GAddress_destroy(socket
->m_local
);
284 socket
->m_local
= GAddress_copy(address
);
286 return GSOCK_NOERROR
;
289 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
291 assert(socket
!= NULL
);
294 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
296 socket
->m_error
= GSOCK_INVADDR
;
297 return GSOCK_INVADDR
;
301 GAddress_destroy(socket
->m_peer
);
303 socket
->m_peer
= GAddress_copy(address
);
305 return GSOCK_NOERROR
;
308 GAddress
*GSocket_GetLocal(GSocket
*socket
)
311 struct sockaddr addr
;
312 SOCKLEN_T size
= sizeof(addr
);
315 assert(socket
!= NULL
);
317 /* try to get it from the m_local var first */
319 return GAddress_copy(socket
->m_local
);
321 /* else, if the socket is initialized, try getsockname */
322 if (socket
->m_fd
== INVALID_SOCKET
)
324 socket
->m_error
= GSOCK_INVSOCK
;
328 if (getsockname(socket
->m_fd
, &addr
, (SOCKLEN_T
*) &size
) < 0)
330 socket
->m_error
= GSOCK_IOERR
;
334 /* got a valid address from getsockname, create a GAddress object */
335 address
= GAddress_new();
338 socket
->m_error
= GSOCK_MEMERR
;
342 err
= _GAddress_translate_from(address
, &addr
, size
);
343 if (err
!= GSOCK_NOERROR
)
345 GAddress_destroy(address
);
346 socket
->m_error
= err
;
353 GAddress
*GSocket_GetPeer(GSocket
*socket
)
355 assert(socket
!= NULL
);
357 /* try to get it from the m_peer var */
359 return GAddress_copy(socket
->m_peer
);
364 /* Server specific parts */
366 /* GSocket_SetServer:
367 * Sets up this socket as a server. The local address must have been
368 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
369 * Returns GSOCK_NOERROR on success, one of the following otherwise:
372 * GSOCK_INVSOCK - the socket is in use.
373 * GSOCK_INVADDR - the local address has not been set.
374 * GSOCK_IOERR - low-level error.
376 GSocketError
GSocket_SetServer(GSocket
*sck
)
382 /* must not be in use */
383 if (sck
->m_fd
!= INVALID_SOCKET
)
385 sck
->m_error
= GSOCK_INVSOCK
;
386 return GSOCK_INVSOCK
;
389 /* the local addr must have been set */
392 sck
->m_error
= GSOCK_INVADDR
;
393 return GSOCK_INVADDR
;
396 /* Initialize all fields */
397 sck
->m_stream
= TRUE
;
398 sck
->m_server
= TRUE
;
399 sck
->m_oriented
= TRUE
;
401 /* Create the socket */
402 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
404 if (sck
->m_fd
== INVALID_SOCKET
)
406 sck
->m_error
= GSOCK_IOERR
;
410 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
411 _GSocket_Enable_Events(sck
);
413 /* Bind to the local address,
414 * retrieve the actual address bound,
415 * and listen up to 5 connections.
417 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
418 (getsockname(sck
->m_fd
,
419 sck
->m_local
->m_addr
,
420 (SOCKLEN_T
*) &sck
->m_local
->m_len
) != 0) ||
421 (listen(sck
->m_fd
, 5) != 0))
424 sck
->m_fd
= INVALID_SOCKET
;
425 sck
->m_error
= GSOCK_IOERR
;
429 return GSOCK_NOERROR
;
432 /* GSocket_WaitConnection:
433 * Waits for an incoming client connection. Returns a pointer to
434 * a GSocket object, or NULL if there was an error, in which case
435 * the last error field will be updated for the calling GSocket.
437 * Error codes (set in the calling GSocket)
438 * GSOCK_INVSOCK - the socket is not valid or not a server.
439 * GSOCK_TIMEDOUT - timeout, no incoming connections.
440 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
441 * GSOCK_MEMERR - couldn't allocate memory.
442 * GSOCK_IOERR - low-level error.
444 GSocket
*GSocket_WaitConnection(GSocket
*socket
)
446 struct sockaddr from
;
447 SOCKLEN_T fromlen
= sizeof(from
);
452 assert(socket
!= NULL
);
454 /* Reenable CONNECTION events */
455 _GSocket_Enable(socket
, GSOCK_CONNECTION
);
457 /* If the socket has already been created, we exit immediately */
458 if (socket
->m_fd
== INVALID_SOCKET
|| !socket
->m_server
)
460 socket
->m_error
= GSOCK_INVSOCK
;
464 /* Create a GSocket object for the new connection */
465 connection
= GSocket_new();
469 socket
->m_error
= GSOCK_MEMERR
;
473 /* Wait for a connection (with timeout) */
474 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
476 GSocket_destroy(connection
);
477 /* socket->m_error set by _GSocket_Input_Timeout */
481 connection
->m_fd
= accept(socket
->m_fd
, &from
, (SOCKLEN_T
*) &fromlen
);
483 if (connection
->m_fd
== INVALID_SOCKET
)
485 if (errno
== EWOULDBLOCK
)
486 socket
->m_error
= GSOCK_WOULDBLOCK
;
488 socket
->m_error
= GSOCK_IOERR
;
490 GSocket_destroy(connection
);
494 /* Initialize all fields */
495 connection
->m_server
= FALSE
;
496 connection
->m_stream
= TRUE
;
497 connection
->m_oriented
= TRUE
;
499 /* Setup the peer address field */
500 connection
->m_peer
= GAddress_new();
501 if (!connection
->m_peer
)
503 GSocket_destroy(connection
);
504 socket
->m_error
= GSOCK_MEMERR
;
507 err
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
);
508 if (err
!= GSOCK_NOERROR
)
510 GAddress_destroy(connection
->m_peer
);
511 GSocket_destroy(connection
);
512 socket
->m_error
= err
;
516 ioctl(connection
->m_fd
, FIONBIO
, &arg
);
517 _GSocket_Enable_Events(connection
);
522 /* Client specific parts */
525 * For stream (connection oriented) sockets, GSocket_Connect() tries
526 * to establish a client connection to a server using the peer address
527 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
528 * connection has been succesfully established, or one of the error
529 * codes listed below. Note that for nonblocking sockets, a return
530 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
531 * request can be completed later; you should use GSocket_Select()
532 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
533 * corresponding asynchronous events.
535 * For datagram (non connection oriented) sockets, GSocket_Connect()
536 * just sets the peer address established with GSocket_SetPeer() as
537 * default destination.
540 * GSOCK_INVSOCK - the socket is in use or not valid.
541 * GSOCK_INVADDR - the peer address has not been established.
542 * GSOCK_TIMEDOUT - timeout, the connection failed.
543 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
544 * GSOCK_MEMERR - couldn't allocate memory.
545 * GSOCK_IOERR - low-level error.
547 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
554 /* Enable CONNECTION events (needed for nonblocking connections) */
555 _GSocket_Enable(sck
, GSOCK_CONNECTION
);
557 if (sck
->m_fd
!= INVALID_SOCKET
)
559 sck
->m_error
= GSOCK_INVSOCK
;
560 return GSOCK_INVSOCK
;
565 sck
->m_error
= GSOCK_INVADDR
;
566 return GSOCK_INVADDR
;
569 /* Streamed or dgram socket? */
570 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
571 sck
->m_oriented
= TRUE
;
572 sck
->m_server
= FALSE
;
573 sck
->m_establishing
= FALSE
;
575 /* Create the socket */
576 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
,
577 sck
->m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
579 if (sck
->m_fd
== INVALID_SOCKET
)
581 sck
->m_error
= GSOCK_IOERR
;
585 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
586 _GSocket_Enable_Events(sck
);
588 /* Connect it to the peer address, with a timeout (see below) */
589 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
591 printf("connect on %d to %X (%d) returned %d, errno = %d\n",
592 sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
, ret
, errno
);
597 /* If connect failed with EINPROGRESS and the GSocket object
598 * is in blocking mode, we select() for the specified timeout
599 * checking for writability to see if the connection request
602 if ((err
== EINPROGRESS
) && (!sck
->m_non_blocking
))
604 if (_GSocket_Output_Timeout(sck
) == GSOCK_TIMEDOUT
)
607 sck
->m_fd
= INVALID_SOCKET
;
608 /* sck->m_error is set in _GSocket_Output_Timeout */
609 return GSOCK_TIMEDOUT
;
614 SOCKLEN_T len
= sizeof(error
);
616 getsockopt(sck
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*) &error
, &len
);
619 return GSOCK_NOERROR
;
623 /* If connect failed with EINPROGRESS and the GSocket object
624 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
625 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
626 * this way if the connection completes, a GSOCK_CONNECTION
627 * event will be generated, if enabled.
629 if ((err
== EINPROGRESS
) && (sck
->m_non_blocking
))
631 sck
->m_establishing
= TRUE
;
632 sck
->m_error
= GSOCK_WOULDBLOCK
;
633 return GSOCK_WOULDBLOCK
;
636 /* If connect failed with an error other than EINPROGRESS,
637 * then the call to GSocket_Connect has failed.
640 sck
->m_fd
= INVALID_SOCKET
;
641 sck
->m_error
= GSOCK_IOERR
;
645 return GSOCK_NOERROR
;
648 /* Datagram sockets */
650 /* GSocket_SetNonOriented:
651 * Sets up this socket as a non-connection oriented (datagram) socket.
652 * Before using this function, the local address must have been set
653 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
654 * on success, or one of the following otherwise.
657 * GSOCK_INVSOCK - the socket is in use.
658 * GSOCK_INVADDR - the local address has not been set.
659 * GSOCK_IOERR - low-level error.
661 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
667 if (sck
->m_fd
!= INVALID_SOCKET
)
669 sck
->m_error
= GSOCK_INVSOCK
;
670 return GSOCK_INVSOCK
;
675 sck
->m_error
= GSOCK_INVADDR
;
676 return GSOCK_INVADDR
;
679 /* Initialize all fields */
680 sck
->m_stream
= FALSE
;
681 sck
->m_server
= FALSE
;
682 sck
->m_oriented
= FALSE
;
684 /* Create the socket */
685 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
687 if (sck
->m_fd
== INVALID_SOCKET
)
689 sck
->m_error
= GSOCK_IOERR
;
693 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
694 _GSocket_Enable_Events(sck
);
696 /* Bind to the local address,
697 * and retrieve the actual address bound.
699 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
700 (getsockname(sck
->m_fd
,
701 sck
->m_local
->m_addr
,
702 (SOCKLEN_T
*) &sck
->m_local
->m_len
) != 0))
705 sck
->m_fd
= INVALID_SOCKET
;
706 sck
->m_error
= GSOCK_IOERR
;
710 return GSOCK_NOERROR
;
715 /* Like recv(), send(), ... */
716 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
720 assert(socket
!= NULL
);
722 /* Reenable INPUT events */
723 _GSocket_Enable(socket
, GSOCK_INPUT
);
725 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
727 socket
->m_error
= GSOCK_INVSOCK
;
731 /* If the socket is blocking, wait for data (with a timeout) */
732 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
736 if (socket
->m_stream
)
737 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
739 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
743 if (errno
== EWOULDBLOCK
)
744 socket
->m_error
= GSOCK_WOULDBLOCK
;
746 socket
->m_error
= GSOCK_IOERR
;
752 int GSocket_Write(GSocket
*socket
, const char *buffer
, int size
)
756 assert(socket
!= NULL
);
758 GSocket_Debug(( "GSocket_Write #1, size %d\n", size
));
760 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
762 socket
->m_error
= GSOCK_INVSOCK
;
766 GSocket_Debug(( "GSocket_Write #2, size %d\n", size
));
768 /* If the socket is blocking, wait for writability (with a timeout) */
769 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
772 GSocket_Debug(( "GSocket_Write #3, size %d\n", size
));
775 if (socket
->m_stream
)
776 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
778 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
780 GSocket_Debug(( "GSocket_Write #4, size %d\n", size
));
784 if (errno
== EWOULDBLOCK
)
786 socket
->m_error
= GSOCK_WOULDBLOCK
;
787 GSocket_Debug(( "GSocket_Write error WOULDBLOCK\n" ));
791 socket
->m_error
= GSOCK_IOERR
;
792 GSocket_Debug(( "GSocket_Write error IOERR\n" ));
795 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
796 * in MSW). Once the first OUTPUT event is received, users can assume
797 * that the socket is writable until a read operation fails. Only then
798 * will further OUTPUT events be posted.
800 _GSocket_Enable(socket
, GSOCK_OUTPUT
);
804 GSocket_Debug(( "GSocket_Write #5, size %d ret %d\n", size
, ret
));
810 * Polls the socket to determine its status. This function will
811 * check for the events specified in the 'flags' parameter, and
812 * it will return a mask indicating which operations can be
813 * performed. This function won't block, regardless of the
814 * mode (blocking | nonblocking) of the socket.
816 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
818 #if defined(wxUSE_GUI) && !wxUSE_GUI
820 GSocketEventFlags result
= 0;
826 /* Do not use a static struct, Linux can garble it */
830 assert(socket
!= NULL
);
835 FD_SET(socket
->m_fd
, &readfds
);
836 FD_SET(socket
->m_fd
, &writefds
);
837 FD_SET(socket
->m_fd
, &exceptfds
);
839 /* Check known state first */
840 result
|= (GSOCK_CONNECTION_FLAG
& socket
->m_detected
& flags
);
841 result
|= (GSOCK_LOST_FLAG
& socket
->m_detected
& flags
);
844 if (select(socket
->m_fd
+ 1, &readfds
, &writefds
, &exceptfds
, &tv
) <= 0)
847 /* Check for readability */
848 if (FD_ISSET(socket
->m_fd
, &readfds
))
852 if (recv(socket
->m_fd
, &c
, 1, MSG_PEEK
) > 0)
854 result
|= (GSOCK_INPUT_FLAG
& flags
);
858 if (socket
->m_server
&& socket
->m_stream
)
860 result
|= (GSOCK_CONNECTION_FLAG
& flags
);
861 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
865 result
|= (GSOCK_LOST_FLAG
& flags
);
866 socket
->m_detected
= GSOCK_LOST_FLAG
;
871 /* Check for writability */
872 if (FD_ISSET(socket
->m_fd
, &writefds
))
874 if (socket
->m_establishing
&& !socket
->m_server
)
877 SOCKLEN_T len
= sizeof(error
);
879 socket
->m_establishing
= FALSE
;
881 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*)&error
, &len
);
885 result
|= (GSOCK_LOST_FLAG
& flags
);
886 socket
->m_detected
= GSOCK_LOST_FLAG
;
890 result
|= (GSOCK_CONNECTION_FLAG
& flags
);
891 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
896 result
|= (GSOCK_OUTPUT_FLAG
& flags
);
900 /* Check for exceptions and errors (is this useful in Unices?) */
901 if (FD_ISSET(socket
->m_fd
, &exceptfds
))
903 result
|= (GSOCK_LOST_FLAG
& flags
);
904 socket
->m_establishing
= FALSE
;
905 socket
->m_detected
= GSOCK_LOST_FLAG
;
912 assert(socket
!= NULL
);
913 return flags
& socket
->m_detected
;
915 #endif /* !wxUSE_GUI */
920 /* GSocket_SetNonBlocking:
921 * Sets the socket to non-blocking mode. All IO calls will return
924 void GSocket_SetNonBlocking(GSocket
*socket
, int non_block
)
926 assert(socket
!= NULL
);
928 GSocket_Debug( ("GSocket_SetNonBlocking: %d\n", (int)non_block
) );
930 socket
->m_non_blocking
= non_block
;
933 /* GSocket_SetTimeout:
934 * Sets the timeout for blocking calls. Time is expressed in
937 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
)
939 assert(socket
!= NULL
);
941 socket
->m_timeout
= millisec
;
945 * Returns the last error occured for this socket. Note that successful
946 * operations do not clear this back to GSOCK_NOERROR, so use it only
949 GSocketError
GSocket_GetError(GSocket
*socket
)
951 assert(socket
!= NULL
);
953 return socket
->m_error
;
959 * There is data to be read in the input buffer. If, after a read
960 * operation, there is still data available, the callback function will
963 * The socket is available for writing. That is, the next write call
964 * won't block. This event is generated only once, when the connection is
965 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
966 * when the output buffer empties again. This means that the app should
967 * assume that it can write since the first OUTPUT event, and no more
968 * OUTPUT events will be generated unless an error occurs.
970 * Connection succesfully established, for client sockets, or incoming
971 * client connection, for server sockets. Wait for this event (also watch
972 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
974 * The connection is lost (or a connection request failed); this could
975 * be due to a failure, or due to the peer closing it gracefully.
978 /* GSocket_SetCallback:
979 * Enables the callbacks specified by 'flags'. Note that 'flags'
980 * may be a combination of flags OR'ed toghether, so the same
981 * callback function can be made to accept different events.
982 * The callback function must have the following prototype:
984 * void function(GSocket *socket, GSocketEvent event, char *cdata)
986 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
987 GSocketCallback callback
, char *cdata
)
991 assert(socket
!= NULL
);
993 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
995 if ((flags
& (1 << count
)) != 0)
997 printf("Setting callback no %d for socket at %X to address %X,data %X\n",
998 count
, socket
, callback
, cdata
);
999 socket
->m_cbacks
[count
] = callback
;
1000 socket
->m_data
[count
] = cdata
;
1005 /* GSocket_UnsetCallback:
1006 * Disables all callbacks specified by 'flags', which may be a
1007 * combination of flags OR'ed toghether.
1009 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
1013 assert(socket
!= NULL
);
1015 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
1017 if ((flags
& (1 << count
)) != 0)
1019 printf("Removing callback no %d for socket at %X",
1021 socket
->m_cbacks
[count
] = NULL
;
1022 socket
->m_data
[count
] = NULL
;
1028 #define CALL_CALLBACK(socket, event) { \
1029 _GSocket_Disable(socket, event); \
1030 if (socket->m_cbacks[event]){ \
1031 printf("Call callback for event %d, socket %X: %X, %X\n", event, \
1032 socket, socket->m_cbacks[event], socket->m_data[event]); \
1033 socket->m_cbacks[event](socket, event, socket->m_data[event]); \
1038 void _GSocket_Enable(GSocket
*socket
, GSocketEvent event
)
1040 socket
->m_detected
&= ~(1 << event
);
1041 _GSocket_Install_Callback(socket
, event
);
1044 void _GSocket_Disable(GSocket
*socket
, GSocketEvent event
)
1046 socket
->m_detected
|= (1 << event
);
1047 _GSocket_Uninstall_Callback(socket
, event
);
1050 /* _GSocket_Input_Timeout:
1051 * For blocking sockets, wait until data is available or
1052 * until timeout ellapses.
1054 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
1060 /* Linux select() will overwrite the struct on return */
1061 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
1062 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
1064 if (!socket
->m_non_blocking
)
1067 FD_SET(socket
->m_fd
, &readfds
);
1068 ret
= select(socket
->m_fd
+ 1, &readfds
, NULL
, NULL
, &tv
);
1071 GSocket_Debug(( "GSocket_Input_Timeout, select returned 0\n" ));
1072 socket
->m_error
= GSOCK_TIMEDOUT
;
1073 return GSOCK_TIMEDOUT
;
1077 GSocket_Debug(( "GSocket_Input_Timeout, select returned -1\n" ));
1078 if (errno
== EBADF
) GSocket_Debug(( "Invalid file descriptor\n" ));
1079 if (errno
== EINTR
) GSocket_Debug(( "A non blocked signal was caught\n" ));
1080 if (errno
== EINVAL
) GSocket_Debug(( "The highest number descriptor is negative\n" ));
1081 if (errno
== ENOMEM
) GSocket_Debug(( "Not enough memory\n" ));
1082 socket
->m_error
= GSOCK_TIMEDOUT
;
1083 return GSOCK_TIMEDOUT
;
1086 return GSOCK_NOERROR
;
1089 /* _GSocket_Output_Timeout:
1090 * For blocking sockets, wait until data can be sent without
1091 * blocking or until timeout ellapses.
1093 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
1099 /* Linux select() will overwrite the struct on return */
1100 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
1101 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
1103 GSocket_Debug( ("m_non_blocking has: %d\n", (int)socket
->m_non_blocking
) );
1105 if (!socket
->m_non_blocking
)
1108 FD_SET(socket
->m_fd
, &writefds
);
1109 ret
= select(socket
->m_fd
+ 1, NULL
, &writefds
, NULL
, &tv
);
1112 GSocket_Debug(( "GSocket_Output_Timeout, select returned 0\n" ));
1113 socket
->m_error
= GSOCK_TIMEDOUT
;
1114 return GSOCK_TIMEDOUT
;
1118 GSocket_Debug(( "GSocket_Output_Timeout, select returned -1\n" ));
1119 if (errno
== EBADF
) GSocket_Debug(( "Invalid file descriptor\n" ));
1120 if (errno
== EINTR
) GSocket_Debug(( "A non blocked signal was caught\n" ));
1121 if (errno
== EINVAL
) GSocket_Debug(( "The highest number descriptor is negative\n" ));
1122 if (errno
== ENOMEM
) GSocket_Debug(( "Not enough memory\n" ));
1123 socket
->m_error
= GSOCK_TIMEDOUT
;
1124 return GSOCK_TIMEDOUT
;
1126 if ( ! FD_ISSET(socket
->m_fd
, &writefds
) )
1127 GSocket_Debug(( "GSocket_Output_Timeout is buggy!\n" ));
1129 GSocket_Debug(( "GSocket_Output_Timeout seems correct\n" ));
1133 GSocket_Debug(( "GSocket_Output_Timeout, didn't try select!\n" ));
1136 return GSOCK_NOERROR
;
1139 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
1141 return recv(socket
->m_fd
, buffer
, size
, 0);
1144 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
1146 struct sockaddr from
;
1147 SOCKLEN_T fromlen
= sizeof(from
);
1151 fromlen
= sizeof(from
);
1153 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, (SOCKLEN_T
*) &fromlen
);
1158 /* Translate a system address into a GSocket address */
1159 if (!socket
->m_peer
)
1161 socket
->m_peer
= GAddress_new();
1162 if (!socket
->m_peer
)
1164 socket
->m_error
= GSOCK_MEMERR
;
1168 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
1169 if (err
!= GSOCK_NOERROR
)
1171 GAddress_destroy(socket
->m_peer
);
1172 socket
->m_peer
= NULL
;
1173 socket
->m_error
= err
;
1180 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
1185 ret
= send(socket
->m_fd
, buffer
, size
, 0);
1191 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
1193 struct sockaddr
*addr
;
1197 if (!socket
->m_peer
)
1199 socket
->m_error
= GSOCK_INVADDR
;
1203 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
1204 if (err
!= GSOCK_NOERROR
)
1206 socket
->m_error
= err
;
1211 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
1214 /* Frees memory allocated from _GAddress_translate_to */
1220 void _GSocket_Detected_Read(GSocket
*socket
)
1224 if (recv(socket
->m_fd
, &c
, 1, MSG_PEEK
) > 0)
1226 CALL_CALLBACK(socket
, GSOCK_INPUT
);
1230 if (socket
->m_server
&& socket
->m_stream
)
1232 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
1236 CALL_CALLBACK(socket
, GSOCK_LOST
);
1241 void _GSocket_Detected_Write(GSocket
*socket
)
1243 if (socket
->m_establishing
&& !socket
->m_server
)
1246 SOCKLEN_T len
= sizeof(error
);
1248 socket
->m_establishing
= FALSE
;
1250 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*)&error
, &len
);
1254 CALL_CALLBACK(socket
, GSOCK_LOST
);
1258 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
1259 /* We have to fire this event by hand because CONNECTION (for clients)
1260 * and OUTPUT are internally the same and we just disabled CONNECTION
1261 * events with the above macro.
1263 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
1268 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
1273 * -------------------------------------------------------------------------
1275 * -------------------------------------------------------------------------
1278 /* CHECK_ADDRESS verifies that the current address family is either
1279 * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it
1280 * initalizes it to be a GSOCK_*family*. In other cases, it returns
1281 * an appropiate error code.
1283 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
1285 #define CHECK_ADDRESS(address, family) \
1287 if (address->m_family == GSOCK_NOFAMILY) \
1288 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1289 return address->m_error; \
1290 if (address->m_family != GSOCK_##family) \
1292 address->m_error = GSOCK_INVADDR; \
1293 return GSOCK_INVADDR; \
1297 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
1299 if (address->m_family == GSOCK_NOFAMILY) \
1300 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1302 if (address->m_family != GSOCK_##family) \
1304 address->m_error = GSOCK_INVADDR; \
1310 GAddress
*GAddress_new(void)
1314 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1317 address
->m_family
= GSOCK_NOFAMILY
;
1318 address
->m_addr
= NULL
;
1324 GAddress
*GAddress_copy(GAddress
*address
)
1328 assert(address
!= NULL
);
1330 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1333 memcpy(addr2
, address
, sizeof(GAddress
));
1335 if (address
->m_addr
)
1337 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
1338 if (addr2
->m_addr
== NULL
)
1343 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1349 void GAddress_destroy(GAddress
*address
)
1351 assert(address
!= NULL
);
1353 if (address
->m_addr
)
1354 free(address
->m_addr
);
1356 /* free(address); */
1359 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1361 assert(address
!= NULL
);
1363 address
->m_family
= type
;
1366 GAddressType
GAddress_GetFamily(GAddress
*address
)
1368 assert(address
!= NULL
);
1370 return address
->m_family
;
1373 GSocketError
_GAddress_translate_from(GAddress
*address
,
1374 struct sockaddr
*addr
, int len
)
1376 address
->m_realfamily
= addr
->sa_family
;
1377 switch (addr
->sa_family
)
1380 address
->m_family
= GSOCK_INET
;
1383 address
->m_family
= GSOCK_UNIX
;
1387 address
->m_family
= GSOCK_INET6
;
1392 address
->m_error
= GSOCK_INVOP
;
1397 if (address
->m_addr
)
1398 free(address
->m_addr
);
1400 address
->m_len
= len
;
1401 address
->m_addr
= (struct sockaddr
*)malloc(len
);
1403 if (address
->m_addr
== NULL
)
1405 address
->m_error
= GSOCK_MEMERR
;
1406 return GSOCK_MEMERR
;
1408 memcpy(address
->m_addr
, addr
, len
);
1410 return GSOCK_NOERROR
;
1413 GSocketError
_GAddress_translate_to(GAddress
*address
,
1414 struct sockaddr
**addr
, int *len
)
1416 if (!address
->m_addr
)
1418 address
->m_error
= GSOCK_INVADDR
;
1419 return GSOCK_INVADDR
;
1422 *len
= address
->m_len
;
1423 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
1426 address
->m_error
= GSOCK_MEMERR
;
1427 return GSOCK_MEMERR
;
1430 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1431 return GSOCK_NOERROR
;
1435 * -------------------------------------------------------------------------
1436 * Internet address family
1437 * -------------------------------------------------------------------------
1440 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1442 address
->m_len
= sizeof(struct sockaddr_in
);
1443 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1444 if (address
->m_addr
== NULL
)
1446 address
->m_error
= GSOCK_MEMERR
;
1447 return GSOCK_MEMERR
;
1450 address
->m_family
= GSOCK_INET
;
1451 address
->m_realfamily
= PF_INET
;
1452 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1453 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1455 return GSOCK_NOERROR
;
1458 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1461 struct in_addr
*addr
;
1463 assert(address
!= NULL
);
1465 CHECK_ADDRESS(address
, INET
);
1467 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1469 /* If it is a numeric host name, convert it now */
1470 #if defined(HAVE_INET_ATON)
1471 if (inet_aton(hostname
, addr
) == 0)
1473 #elif defined(HAVE_INET_ADDR)
1474 if ( (addr
->s_addr
= inet_addr(hostname
)) == -1 )
1477 /* Use gethostbyname by default */
1481 struct in_addr
*array_addr
;
1483 /* It is a real name, we solve it */
1484 if ((he
= gethostbyname(hostname
)) == NULL
)
1486 /* Reset to invalid address */
1487 addr
->s_addr
= INADDR_NONE
;
1488 address
->m_error
= GSOCK_NOHOST
;
1489 return GSOCK_NOHOST
;
1491 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1492 addr
->s_addr
= array_addr
[0].s_addr
;
1494 return GSOCK_NOERROR
;
1497 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1499 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1502 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1503 unsigned long hostaddr
)
1505 struct in_addr
*addr
;
1507 assert(address
!= NULL
);
1509 CHECK_ADDRESS(address
, INET
);
1511 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1512 addr
->s_addr
= hostaddr
;
1514 return GSOCK_NOERROR
;
1517 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1518 const char *protocol
)
1521 struct sockaddr_in
*addr
;
1523 assert(address
!= NULL
);
1524 CHECK_ADDRESS(address
, INET
);
1528 address
->m_error
= GSOCK_INVPORT
;
1529 return GSOCK_INVPORT
;
1532 se
= getservbyname(port
, protocol
);
1535 if (isdigit((int)port
[0]))
1539 port_int
= atoi(port
);
1540 addr
= (struct sockaddr_in
*)address
->m_addr
;
1541 addr
->sin_port
= htons(port_int
);
1542 return GSOCK_NOERROR
;
1545 address
->m_error
= GSOCK_INVPORT
;
1546 return GSOCK_INVPORT
;
1549 addr
= (struct sockaddr_in
*)address
->m_addr
;
1550 addr
->sin_port
= se
->s_port
;
1552 return GSOCK_NOERROR
;
1555 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1557 struct sockaddr_in
*addr
;
1559 assert(address
!= NULL
);
1560 CHECK_ADDRESS(address
, INET
);
1562 addr
= (struct sockaddr_in
*)address
->m_addr
;
1563 addr
->sin_port
= htons(port
);
1565 return GSOCK_NOERROR
;
1568 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1572 struct sockaddr_in
*addr
;
1574 assert(address
!= NULL
);
1575 CHECK_ADDRESS(address
, INET
);
1577 addr
= (struct sockaddr_in
*)address
->m_addr
;
1578 addr_buf
= (char *)&(addr
->sin_addr
);
1580 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1583 address
->m_error
= GSOCK_NOHOST
;
1584 return GSOCK_NOHOST
;
1587 strncpy(hostname
, he
->h_name
, sbuf
);
1589 return GSOCK_NOERROR
;
1592 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1594 struct sockaddr_in
*addr
;
1596 assert(address
!= NULL
);
1597 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1599 addr
= (struct sockaddr_in
*)address
->m_addr
;
1601 return addr
->sin_addr
.s_addr
;
1604 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1606 struct sockaddr_in
*addr
;
1608 assert(address
!= NULL
);
1609 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1611 addr
= (struct sockaddr_in
*)address
->m_addr
;
1612 return ntohs(addr
->sin_port
);
1616 * -------------------------------------------------------------------------
1617 * Unix address family
1618 * -------------------------------------------------------------------------
1622 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1624 address
->m_len
= sizeof(struct sockaddr_un
);
1625 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
1626 if (address
->m_addr
== NULL
)
1628 address
->m_error
= GSOCK_MEMERR
;
1629 return GSOCK_MEMERR
;
1632 address
->m_family
= GSOCK_UNIX
;
1633 address
->m_realfamily
= PF_UNIX
;
1634 ((struct sockaddr_un
*)address
->m_addr
)->sun_family
= AF_UNIX
;
1635 ((struct sockaddr_un
*)address
->m_addr
)->sun_path
[0] = 0;
1637 return GSOCK_NOERROR
;
1640 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1642 struct sockaddr_un
*addr
;
1644 assert(address
!= NULL
);
1646 CHECK_ADDRESS(address
, UNIX
);
1648 addr
= ((struct sockaddr_un
*)address
->m_addr
);
1649 memcpy(addr
->sun_path
, path
, strlen(path
));
1651 return GSOCK_NOERROR
;
1654 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1656 struct sockaddr_un
*addr
;
1658 assert(address
!= NULL
);
1659 CHECK_ADDRESS(address
, UNIX
);
1661 addr
= (struct sockaddr_un
*)address
->m_addr
;
1663 strncpy(path
, addr
->sun_path
, sbuf
);
1665 return GSOCK_NOERROR
;
1670 /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */