]>
git.saurik.com Git - wxWidgets.git/blob - src/unix/gsocket.c
1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket) for WX
4 * Authors: Guilhem Lavaux,
5 * Guillermo Rodriguez Garcia <guille@iies.es> (maintainer)
6 * Purpose: GSocket main Unix file
8 * -------------------------------------------------------------------------
12 * PLEASE don't put C++ comments here - this is a C source file.
15 #ifndef __GSOCKET_STANDALONE__
19 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
22 #include <sys/types.h>
24 #include <sys/ioctl.h>
29 u_char sun_len
; /* sockaddr len including null */
30 u_char sun_family
; /* AF_UNIX */
31 char sun_path
[108]; /* path name (gag) */
34 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
49 # include <sys/filio.h>
60 # define SOCKLEN_T unsigned int
64 # define SOCKLEN_T socklen_t
67 # define SOCKLEN_T int
71 #endif /* SOCKLEN_T */
75 * INADDR_BROADCAST is identical to INADDR_NONE which is not defined
76 * on all unices. INADDR_BROADCAST should be fine to indicate an error.
79 #define INADDR_NONE INADDR_BROADCAST
82 #define MASK_SIGNAL() \
84 void (*old_handler)(int); \
86 old_handler = signal(SIGPIPE, SIG_IGN);
88 #define UNMASK_SIGNAL() \
89 signal(SIGPIPE, old_handler); \
93 #ifndef __GSOCKET_STANDALONE__
95 #include "wx/gsocket.h"
96 #include "wx/unix/gsockunx.h"
101 #include "gsockunx.h"
103 #endif /* __GSOCKET_STANDALONE__ */
106 /* Global initialisers */
113 void GSocket_Cleanup()
117 /* Constructors / Destructors for GSocket */
119 GSocket
*GSocket_new()
124 socket
= (GSocket
*)malloc(sizeof(GSocket
));
130 for (i
=0;i
<GSOCK_MAX_EVENT
;i
++)
132 socket
->m_cbacks
[i
] = NULL
;
134 socket
->m_detected
= 0;
135 socket
->m_local
= NULL
;
136 socket
->m_peer
= NULL
;
137 socket
->m_error
= GSOCK_NOERROR
;
138 socket
->m_server
= FALSE
;
139 socket
->m_stream
= TRUE
;
140 socket
->m_gui_dependent
= NULL
;
141 socket
->m_non_blocking
= FALSE
;
142 socket
->m_timeout
= 10*60*1000;
143 /* 10 minutes * 60 sec * 1000 millisec */
144 socket
->m_establishing
= FALSE
;
146 /* We initialize the GUI specific entries here */
147 _GSocket_GUI_Init(socket
);
152 void GSocket_destroy(GSocket
*socket
)
154 assert(socket
!= NULL
);
156 /* Check that the socket is really shutdowned */
157 if (socket
->m_fd
!= -1)
158 GSocket_Shutdown(socket
);
160 /* We destroy GUI specific variables */
161 _GSocket_GUI_Destroy(socket
);
163 /* Destroy private addresses */
165 GAddress_destroy(socket
->m_local
);
168 GAddress_destroy(socket
->m_peer
);
170 /* Destroy the socket itself */
175 * Disallow further read/write operations on this socket, close
176 * the fd and disable all callbacks.
178 void GSocket_Shutdown(GSocket
*socket
)
182 assert(socket
!= NULL
);
184 /* If socket has been created, shutdown it */
185 if (socket
->m_fd
!= -1)
187 shutdown(socket
->m_fd
, 2);
192 /* Disable GUI callbacks */
193 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
194 socket
->m_cbacks
[evt
] = NULL
;
196 socket
->m_detected
= 0;
197 _GSocket_Disable_Events(socket
);
200 /* Address handling */
206 * Set or get the local or peer address for this socket. The 'set'
207 * functions return GSOCK_NOERROR on success, an error code otherwise.
208 * The 'get' functions return a pointer to a GAddress object on success,
209 * or NULL otherwise, in which case they set the error code of the
210 * corresponding GSocket.
213 * GSOCK_INVSOCK - the socket is not valid.
214 * GSOCK_INVADDR - the address is not valid.
216 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
218 assert(socket
!= NULL
);
220 /* the socket must be initialized, or it must be a server */
221 if ((socket
->m_fd
!= -1 && !socket
->m_server
))
223 socket
->m_error
= GSOCK_INVSOCK
;
224 return GSOCK_INVSOCK
;
228 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
230 socket
->m_error
= GSOCK_INVADDR
;
231 return GSOCK_INVADDR
;
235 GAddress_destroy(socket
->m_local
);
237 socket
->m_local
= GAddress_copy(address
);
239 return GSOCK_NOERROR
;
242 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
244 assert(socket
!= NULL
);
247 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
249 socket
->m_error
= GSOCK_INVADDR
;
250 return GSOCK_INVADDR
;
254 GAddress_destroy(socket
->m_peer
);
256 socket
->m_peer
= GAddress_copy(address
);
258 return GSOCK_NOERROR
;
261 GAddress
*GSocket_GetLocal(GSocket
*socket
)
264 struct sockaddr addr
;
265 SOCKLEN_T size
= sizeof(addr
);
268 assert(socket
!= NULL
);
270 /* try to get it from the m_local var first */
272 return GAddress_copy(socket
->m_local
);
274 /* else, if the socket is initialized, try getsockname */
275 if (socket
->m_fd
== -1)
277 socket
->m_error
= GSOCK_INVSOCK
;
281 if (getsockname(socket
->m_fd
, &addr
, (SOCKLEN_T
*) &size
) < 0)
283 socket
->m_error
= GSOCK_IOERR
;
287 /* got a valid address from getsockname, create a GAddress object */
288 address
= GAddress_new();
291 socket
->m_error
= GSOCK_MEMERR
;
295 err
= _GAddress_translate_from(address
, &addr
, size
);
296 if (err
!= GSOCK_NOERROR
)
298 GAddress_destroy(address
);
299 socket
->m_error
= err
;
306 GAddress
*GSocket_GetPeer(GSocket
*socket
)
308 assert(socket
!= NULL
);
310 /* try to get it from the m_peer var */
312 return GAddress_copy(socket
->m_peer
);
317 /* Server specific parts */
319 /* GSocket_SetServer:
320 * Sets up this socket as a server. The local address must have been
321 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
322 * Returns GSOCK_NOERROR on success, one of the following otherwise:
325 * GSOCK_INVSOCK - the socket is in use.
326 * GSOCK_INVADDR - the local address has not been set.
327 * GSOCK_IOERR - low-level error.
329 GSocketError
GSocket_SetServer(GSocket
*sck
)
336 /* must not be in use */
339 sck
->m_error
= GSOCK_INVSOCK
;
340 return GSOCK_INVSOCK
;
343 /* the local addr must have been set */
346 sck
->m_error
= GSOCK_INVADDR
;
347 return GSOCK_INVADDR
;
350 /* Initialize all fields */
351 sck
->m_stream
= TRUE
;
352 sck
->m_server
= TRUE
;
353 sck
->m_oriented
= TRUE
;
355 /* Create the socket */
356 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
360 sck
->m_error
= GSOCK_IOERR
;
364 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
365 _GSocket_Enable_Events(sck
);
367 /* Bind to the local address,
368 * retrieve the actual address bound,
369 * and listen up to 5 connections.
371 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
372 (getsockname(sck
->m_fd
,
373 sck
->m_local
->m_addr
,
374 (SOCKLEN_T
*) &sck
->m_local
->m_len
) != 0) ||
375 (listen(sck
->m_fd
, 5) != 0))
379 sck
->m_error
= GSOCK_IOERR
;
383 return GSOCK_NOERROR
;
386 /* GSocket_WaitConnection:
387 * Waits for an incoming client connection. Returns a pointer to
388 * a GSocket object, or NULL if there was an error, in which case
389 * the last error field will be updated for the calling GSocket.
391 * Error codes (set in the calling GSocket)
392 * GSOCK_INVSOCK - the socket is not valid or not a server.
393 * GSOCK_TIMEDOUT - timeout, no incoming connections.
394 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
395 * GSOCK_MEMERR - couldn't allocate memory.
396 * GSOCK_IOERR - low-level error.
398 GSocket
*GSocket_WaitConnection(GSocket
*socket
)
400 struct sockaddr from
;
401 SOCKLEN_T fromlen
= sizeof(from
);
406 assert(socket
!= NULL
);
408 /* Reenable CONNECTION events */
409 _GSocket_Enable(socket
, GSOCK_CONNECTION
);
411 /* If the socket has already been created, we exit immediately */
412 if (socket
->m_fd
== -1 || !socket
->m_server
)
414 socket
->m_error
= GSOCK_INVSOCK
;
418 /* Create a GSocket object for the new connection */
419 connection
= GSocket_new();
423 socket
->m_error
= GSOCK_MEMERR
;
427 /* Wait for a connection (with timeout) */
428 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
430 GSocket_destroy(connection
);
431 /* socket->m_error set by _GSocket_Input_Timeout */
435 connection
->m_fd
= accept(socket
->m_fd
, &from
, (SOCKLEN_T
*) &fromlen
);
437 if (connection
->m_fd
== -1)
439 if (errno
== EWOULDBLOCK
)
440 socket
->m_error
= GSOCK_WOULDBLOCK
;
442 socket
->m_error
= GSOCK_IOERR
;
444 GSocket_destroy(connection
);
448 /* Initialize all fields */
449 connection
->m_server
= FALSE
;
450 connection
->m_stream
= TRUE
;
451 connection
->m_oriented
= TRUE
;
453 /* Setup the peer address field */
454 connection
->m_peer
= GAddress_new();
455 if (!connection
->m_peer
)
457 GSocket_destroy(connection
);
458 socket
->m_error
= GSOCK_MEMERR
;
461 err
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
);
462 if (err
!= GSOCK_NOERROR
)
464 GAddress_destroy(connection
->m_peer
);
465 GSocket_destroy(connection
);
466 socket
->m_error
= err
;
470 ioctl(connection
->m_fd
, FIONBIO
, &arg
);
471 _GSocket_Enable_Events(connection
);
476 /* Datagram sockets */
478 /* GSocket_SetNonOriented:
479 * Sets up this socket as a non-connection oriented (datagram) socket.
480 * Before using this function, the local address must have been set
481 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
482 * on success, or one of the following otherwise.
485 * GSOCK_INVSOCK - the socket is in use.
486 * GSOCK_INVADDR - the local address has not been set.
487 * GSOCK_IOERR - low-level error.
489 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
497 sck
->m_error
= GSOCK_INVSOCK
;
498 return GSOCK_INVSOCK
;
503 sck
->m_error
= GSOCK_INVADDR
;
504 return GSOCK_INVADDR
;
507 /* Initialize all fields */
508 sck
->m_stream
= FALSE
;
509 sck
->m_server
= FALSE
;
510 sck
->m_oriented
= FALSE
;
512 /* Create the socket */
513 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
517 sck
->m_error
= GSOCK_IOERR
;
521 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
522 _GSocket_Enable_Events(sck
);
524 /* Bind to the local address,
525 * and retrieve the actual address bound.
527 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
528 (getsockname(sck
->m_fd
,
529 sck
->m_local
->m_addr
,
530 (SOCKLEN_T
*) &sck
->m_local
->m_len
) != 0))
534 sck
->m_error
= GSOCK_IOERR
;
538 return GSOCK_NOERROR
;
541 /* Client specific parts */
544 * For stream (connection oriented) sockets, GSocket_Connect() tries
545 * to establish a client connection to a server using the peer address
546 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
547 * connection has been succesfully established, or one of the error
548 * codes listed below. Note that for nonblocking sockets, a return
549 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
550 * request can be completed later; you should use GSocket_Select()
551 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
552 * corresponding asynchronous events.
554 * For datagram (non connection oriented) sockets, GSocket_Connect()
555 * just sets the peer address established with GSocket_SetPeer() as
556 * default destination.
559 * GSOCK_INVSOCK - the socket is in use or not valid.
560 * GSOCK_INVADDR - the peer address has not been established.
561 * GSOCK_TIMEDOUT - timeout, the connection failed.
562 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
563 * GSOCK_MEMERR - couldn't allocate memory.
564 * GSOCK_IOERR - low-level error.
566 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
573 /* Enable CONNECTION events (needed for nonblocking connections) */
574 _GSocket_Enable(sck
, GSOCK_CONNECTION
);
578 sck
->m_error
= GSOCK_INVSOCK
;
579 return GSOCK_INVSOCK
;
584 sck
->m_error
= GSOCK_INVADDR
;
585 return GSOCK_INVADDR
;
588 /* Streamed or dgram socket? */
589 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
590 sck
->m_oriented
= TRUE
;
591 sck
->m_server
= FALSE
;
592 sck
->m_establishing
= FALSE
;
594 /* Create the socket */
595 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
,
596 sck
->m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
600 sck
->m_error
= GSOCK_IOERR
;
604 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
605 _GSocket_Enable_Events(sck
);
607 /* Connect it to the peer address, with a timeout (see below) */
608 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
614 /* If connect failed with EINPROGRESS and the GSocket object
615 * is in blocking mode, we select() for the specified timeout
616 * checking for writability to see if the connection request
619 if ((err
== EINPROGRESS
) && (!sck
->m_non_blocking
))
621 if (_GSocket_Output_Timeout(sck
) == GSOCK_TIMEDOUT
)
625 /* sck->m_error is set in _GSocket_Output_Timeout */
626 return GSOCK_TIMEDOUT
;
631 SOCKLEN_T len
= sizeof(error
);
633 getsockopt(sck
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*) &error
, &len
);
636 return GSOCK_NOERROR
;
640 /* If connect failed with EINPROGRESS and the GSocket object
641 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
642 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
643 * this way if the connection completes, a GSOCK_CONNECTION
644 * event will be generated, if enabled.
646 if ((err
== EINPROGRESS
) && (sck
->m_non_blocking
))
648 sck
->m_establishing
= TRUE
;
649 sck
->m_error
= GSOCK_WOULDBLOCK
;
650 return GSOCK_WOULDBLOCK
;
653 /* If connect failed with an error other than EINPROGRESS,
654 * then the call to GSocket_Connect has failed.
658 sck
->m_error
= GSOCK_IOERR
;
662 return GSOCK_NOERROR
;
667 /* Like recv(), send(), ... */
668 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
672 assert(socket
!= NULL
);
674 /* Reenable INPUT events */
675 _GSocket_Enable(socket
, GSOCK_INPUT
);
677 if (socket
->m_fd
== -1 || socket
->m_server
)
679 socket
->m_error
= GSOCK_INVSOCK
;
683 /* If the socket is blocking, wait for data (with a timeout) */
684 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
688 if (socket
->m_stream
)
689 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
691 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
695 if (errno
== EWOULDBLOCK
)
696 socket
->m_error
= GSOCK_WOULDBLOCK
;
698 socket
->m_error
= GSOCK_IOERR
;
704 int GSocket_Write(GSocket
*socket
, const char *buffer
, int size
)
708 assert(socket
!= NULL
);
710 if (socket
->m_fd
== -1 || socket
->m_server
)
712 socket
->m_error
= GSOCK_INVSOCK
;
716 /* If the socket is blocking, wait for writability (with a timeout) */
717 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
721 if (socket
->m_stream
)
722 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
724 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
728 if (errno
== EWOULDBLOCK
)
729 socket
->m_error
= GSOCK_WOULDBLOCK
;
731 socket
->m_error
= GSOCK_IOERR
;
733 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
734 * in MSW). Once the first OUTPUT event is received, users can assume
735 * that the socket is writable until a read operation fails. Only then
736 * will further OUTPUT events be posted.
738 _GSocket_Enable(socket
, GSOCK_OUTPUT
);
746 * Polls the socket to determine its status. This function will
747 * check for the events specified in the 'flags' parameter, and
748 * it will return a mask indicating which operations can be
749 * performed. This function won't block, regardless of the
750 * mode (blocking | nonblocking) of the socket.
752 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
754 assert(socket
!= NULL
);
756 return (flags
& socket
->m_detected
);
761 /* GSocket_SetNonBlocking:
762 * Sets the socket to non-blocking mode. All IO calls will return
765 void GSocket_SetNonBlocking(GSocket
*socket
, bool non_block
)
767 assert(socket
!= NULL
);
769 socket
->m_non_blocking
= non_block
;
772 /* GSocket_SetTimeout:
773 * Sets the timeout for blocking calls. Time is expressed in
776 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
)
778 assert(socket
!= NULL
);
780 socket
->m_timeout
= millisec
;
784 * Returns the last error occured for this socket. Note that successful
785 * operations do not clear this back to GSOCK_NOERROR, so use it only
788 GSocketError
GSocket_GetError(GSocket
*socket
)
790 assert(socket
!= NULL
);
792 return socket
->m_error
;
798 * There is data to be read in the input buffer. If, after a read
799 * operation, there is still data available, the callback function will
802 * The socket is available for writing. That is, the next write call
803 * won't block. This event is generated only once, when the connection is
804 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
805 * when the output buffer empties again. This means that the app should
806 * assume that it can write since the first OUTPUT event, and no more
807 * OUTPUT events will be generated unless an error occurs.
809 * Connection succesfully established, for client sockets, or incoming
810 * client connection, for server sockets. Wait for this event (also watch
811 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
813 * The connection is lost (or a connection request failed); this could
814 * be due to a failure, or due to the peer closing it gracefully.
817 /* GSocket_SetCallback:
818 * Enables the callbacks specified by 'flags'. Note that 'flags'
819 * may be a combination of flags OR'ed toghether, so the same
820 * callback function can be made to accept different events.
821 * The callback function must have the following prototype:
823 * void function(GSocket *socket, GSocketEvent event, char *cdata)
825 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
826 GSocketCallback callback
, char *cdata
)
830 assert(socket
!= NULL
);
832 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
834 if ((flags
& (1 << count
)) != 0)
836 socket
->m_cbacks
[count
] = callback
;
837 socket
->m_data
[count
] = cdata
;
842 /* GSocket_UnsetCallback:
843 * Disables all callbacks specified by 'flags', which may be a
844 * combination of flags OR'ed toghether.
846 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
850 assert(socket
!= NULL
);
852 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
854 if ((flags
& (1 << count
)) != 0)
856 socket
->m_cbacks
[count
] = NULL
;
857 socket
->m_data
[count
] = NULL
;
863 #define CALL_CALLBACK(socket, event) { \
864 _GSocket_Disable(socket, event); \
865 if (socket->m_cbacks[event]) \
866 socket->m_cbacks[event](socket, event, socket->m_data[event]); \
870 void _GSocket_Enable(GSocket
*socket
, GSocketEvent event
)
872 socket
->m_detected
&= ~(1 << event
);
873 _GSocket_Install_Callback(socket
, event
);
876 void _GSocket_Disable(GSocket
*socket
, GSocketEvent event
)
878 socket
->m_detected
|= (1 << event
);
879 _GSocket_Uninstall_Callback(socket
, event
);
882 /* _GSocket_Input_Timeout:
883 * For blocking sockets, wait until data is available or
884 * until timeout ellapses.
886 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
891 /* Linux select() will overwrite the struct on return */
892 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
893 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
895 if (!socket
->m_non_blocking
)
898 FD_SET(socket
->m_fd
, &readfds
);
899 if (select(socket
->m_fd
+ 1, &readfds
, NULL
, NULL
, &tv
) == 0)
901 socket
->m_error
= GSOCK_TIMEDOUT
;
902 return GSOCK_TIMEDOUT
;
905 return GSOCK_NOERROR
;
908 /* _GSocket_Output_Timeout:
909 * For blocking sockets, wait until data can be sent without
910 * blocking or until timeout ellapses.
912 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
917 /* Linux select() will overwrite the struct on return */
918 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
919 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
921 if (!socket
->m_non_blocking
)
924 FD_SET(socket
->m_fd
, &writefds
);
925 if (select(socket
->m_fd
+ 1, NULL
, &writefds
, NULL
, &tv
) == 0)
927 socket
->m_error
= GSOCK_TIMEDOUT
;
928 return GSOCK_TIMEDOUT
;
931 return GSOCK_NOERROR
;
934 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
939 ret
= recv(socket
->m_fd
, buffer
, size
, 0);
945 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
947 struct sockaddr from
;
948 SOCKLEN_T fromlen
= sizeof(from
);
952 fromlen
= sizeof(from
);
955 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, (SOCKLEN_T
*) &fromlen
);
961 /* Translate a system address into a GSocket address */
964 socket
->m_peer
= GAddress_new();
967 socket
->m_error
= GSOCK_MEMERR
;
971 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
972 if (err
!= GSOCK_NOERROR
)
974 GAddress_destroy(socket
->m_peer
);
975 socket
->m_peer
= NULL
;
976 socket
->m_error
= err
;
983 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
988 ret
= send(socket
->m_fd
, buffer
, size
, 0);
994 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
996 struct sockaddr
*addr
;
1000 if (!socket
->m_peer
)
1002 socket
->m_error
= GSOCK_INVADDR
;
1006 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
1007 if (err
!= GSOCK_NOERROR
)
1009 socket
->m_error
= err
;
1014 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
1017 /* Frees memory allocated from _GAddress_translate_to */
1023 void _GSocket_Detected_Read(GSocket
*socket
)
1028 ret
= recv(socket
->m_fd
, &c
, 1, MSG_PEEK
);
1030 if (socket
->m_stream
)
1032 if (ret
< 0 && socket
->m_server
)
1034 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
1041 CALL_CALLBACK(socket
, GSOCK_INPUT
);
1045 CALL_CALLBACK(socket
, GSOCK_LOST
);
1049 void _GSocket_Detected_Write(GSocket
*socket
)
1051 if (socket
->m_establishing
&& !socket
->m_server
)
1054 SOCKLEN_T len
= sizeof(error
);
1056 socket
->m_establishing
= FALSE
;
1058 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*) &error
, &len
);
1062 CALL_CALLBACK(socket
, GSOCK_LOST
);
1066 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
1067 /* We have to fire this event by hand because CONNECTION (for clients)
1068 * and OUTPUT are internally the same and we just disabled CONNECTION
1069 * events with the above macro.
1071 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
1076 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
1081 * -------------------------------------------------------------------------
1083 * -------------------------------------------------------------------------
1086 /* CHECK_ADDRESS verifies that the current family is either GSOCK_NOFAMILY
1087 * or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it initalizes address
1088 * to be a GSOCK_*family*. In other cases, it returns GSOCK_INVADDR.
1090 #define CHECK_ADDRESS(address, family, retval) \
1092 if (address->m_family == GSOCK_NOFAMILY) \
1093 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1094 return address->m_error; \
1095 if (address->m_family != GSOCK_##family) \
1097 address->m_error = GSOCK_INVADDR; \
1102 GAddress
*GAddress_new()
1106 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1109 address
->m_family
= GSOCK_NOFAMILY
;
1110 address
->m_addr
= NULL
;
1116 GAddress
*GAddress_copy(GAddress
*address
)
1120 assert(address
!= NULL
);
1122 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1125 memcpy(addr2
, address
, sizeof(GAddress
));
1127 if (address
->m_addr
)
1129 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
1130 if (addr2
->m_addr
== NULL
)
1135 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1141 void GAddress_destroy(GAddress
*address
)
1143 assert(address
!= NULL
);
1148 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1150 assert(address
!= NULL
);
1152 address
->m_family
= type
;
1155 GAddressType
GAddress_GetFamily(GAddress
*address
)
1157 assert(address
!= NULL
);
1159 return address
->m_family
;
1162 GSocketError
_GAddress_translate_from(GAddress
*address
,
1163 struct sockaddr
*addr
, int len
)
1165 address
->m_realfamily
= addr
->sa_family
;
1166 switch (addr
->sa_family
)
1169 address
->m_family
= GSOCK_INET
;
1172 address
->m_family
= GSOCK_UNIX
;
1176 address
->m_family
= GSOCK_INET6
;
1181 address
->m_error
= GSOCK_INVOP
;
1186 if (address
->m_addr
)
1187 free(address
->m_addr
);
1189 address
->m_len
= len
;
1190 address
->m_addr
= (struct sockaddr
*)malloc(len
);
1192 if (address
->m_addr
== NULL
)
1194 address
->m_error
= GSOCK_MEMERR
;
1195 return GSOCK_MEMERR
;
1197 memcpy(address
->m_addr
, addr
, len
);
1199 return GSOCK_NOERROR
;
1202 GSocketError
_GAddress_translate_to(GAddress
*address
,
1203 struct sockaddr
**addr
, int *len
)
1205 if (!address
->m_addr
)
1207 address
->m_error
= GSOCK_INVADDR
;
1208 return GSOCK_INVADDR
;
1211 *len
= address
->m_len
;
1212 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
1213 if (*addr
== NULL
) {
1214 address
->m_error
= GSOCK_MEMERR
;
1215 return GSOCK_MEMERR
;
1218 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1219 return GSOCK_NOERROR
;
1223 * -------------------------------------------------------------------------
1224 * Internet address family
1225 * -------------------------------------------------------------------------
1228 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1230 address
->m_len
= sizeof(struct sockaddr_in
);
1231 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1232 if (address
->m_addr
== NULL
)
1234 address
->m_error
= GSOCK_MEMERR
;
1235 return GSOCK_MEMERR
;
1238 address
->m_family
= GSOCK_INET
;
1239 address
->m_realfamily
= PF_INET
;
1240 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1241 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1243 return GSOCK_NOERROR
;
1246 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1249 struct in_addr
*addr
;
1251 assert(address
!= NULL
);
1253 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1255 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1257 /* If it is a numeric host name, convert it now */
1258 #if defined(HAVE_INET_ATON)
1259 if (inet_aton(hostname
, addr
) == 0)
1261 #elif defined(HAVE_INET_ADDR)
1262 if ( (addr
->s_addr
= inet_addr(hostname
)) == -1 )
1265 /* Use gethostbyname by default */
1269 struct in_addr
*array_addr
;
1271 /* It is a real name, we solve it */
1272 if ((he
= gethostbyname(hostname
)) == NULL
)
1274 /* Reset to invalid address */
1275 addr
->s_addr
= INADDR_NONE
;
1276 address
->m_error
= GSOCK_NOHOST
;
1277 return GSOCK_NOHOST
;
1279 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1280 addr
->s_addr
= array_addr
[0].s_addr
;
1282 return GSOCK_NOERROR
;
1285 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1287 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1290 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1291 unsigned long hostaddr
)
1293 struct in_addr
*addr
;
1295 assert(address
!= NULL
);
1297 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1299 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1300 addr
->s_addr
= hostaddr
;
1302 return GSOCK_NOERROR
;
1305 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1306 const char *protocol
)
1309 struct sockaddr_in
*addr
;
1311 assert(address
!= NULL
);
1312 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1316 address
->m_error
= GSOCK_INVPORT
;
1317 return GSOCK_INVPORT
;
1320 se
= getservbyname(port
, protocol
);
1323 if (isdigit(port
[0]))
1327 port_int
= atoi(port
);
1328 addr
= (struct sockaddr_in
*)address
->m_addr
;
1329 addr
->sin_port
= htons(port_int
);
1330 return GSOCK_NOERROR
;
1333 address
->m_error
= GSOCK_INVPORT
;
1334 return GSOCK_INVPORT
;
1337 addr
= (struct sockaddr_in
*)address
->m_addr
;
1338 addr
->sin_port
= se
->s_port
;
1340 return GSOCK_NOERROR
;
1343 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1345 struct sockaddr_in
*addr
;
1347 assert(address
!= NULL
);
1348 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1350 addr
= (struct sockaddr_in
*)address
->m_addr
;
1351 addr
->sin_port
= htons(port
);
1353 return GSOCK_NOERROR
;
1356 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1360 struct sockaddr_in
*addr
;
1362 assert(address
!= NULL
);
1363 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1365 addr
= (struct sockaddr_in
*)address
->m_addr
;
1366 addr_buf
= (char *)&(addr
->sin_addr
);
1368 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1371 address
->m_error
= GSOCK_NOHOST
;
1372 return GSOCK_NOHOST
;
1375 strncpy(hostname
, he
->h_name
, sbuf
);
1377 return GSOCK_NOERROR
;
1380 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1382 struct sockaddr_in
*addr
;
1384 assert(address
!= NULL
);
1385 CHECK_ADDRESS(address
, INET
, 0);
1387 addr
= (struct sockaddr_in
*)address
->m_addr
;
1389 return addr
->sin_addr
.s_addr
;
1392 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1394 struct sockaddr_in
*addr
;
1396 assert(address
!= NULL
);
1397 CHECK_ADDRESS(address
, INET
, 0);
1399 addr
= (struct sockaddr_in
*)address
->m_addr
;
1400 return ntohs(addr
->sin_port
);
1404 * -------------------------------------------------------------------------
1405 * Unix address family
1406 * -------------------------------------------------------------------------
1409 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1411 address
->m_len
= sizeof(struct sockaddr_un
);
1412 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
1413 if (address
->m_addr
== NULL
)
1415 address
->m_error
= GSOCK_MEMERR
;
1416 return GSOCK_MEMERR
;
1419 address
->m_family
= GSOCK_UNIX
;
1420 address
->m_realfamily
= PF_UNIX
;
1421 ((struct sockaddr_un
*)address
->m_addr
)->sun_family
= AF_UNIX
;
1422 ((struct sockaddr_un
*)address
->m_addr
)->sun_path
[0] = 0;
1427 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1429 struct sockaddr_un
*addr
;
1431 assert(address
!= NULL
);
1433 CHECK_ADDRESS(address
, UNIX
, GSOCK_INVADDR
);
1435 addr
= ((struct sockaddr_un
*)address
->m_addr
);
1436 memcpy(addr
->sun_path
, path
, strlen(path
));
1438 return GSOCK_NOERROR
;
1441 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1443 struct sockaddr_un
*addr
;
1445 assert(address
!= NULL
);
1446 CHECK_ADDRESS(address
, UNIX
, GSOCK_INVADDR
);
1448 addr
= (struct sockaddr_un
*)address
->m_addr
;
1450 strncpy(path
, addr
->sun_path
, sbuf
);
1452 return GSOCK_NOERROR
;
1455 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */