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 and OS/2 file
7 * Licence: The wxWidgets licence
9 * -------------------------------------------------------------------------
13 * PLEASE don't put C++ comments here - this is a C source file.
16 #ifndef __GSOCKET_STANDALONE__
20 #if defined(__VISAGECPP__)
21 /* Seems to be needed by Visual Age C++, though I don't see how it manages
22 to not break on including a C++ header into a plain C source file */
24 #define BSD_SELECT /* use Berkley Sockets select */
27 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
30 #include <sys/types.h>
35 #include <netinet/in.h>
38 #include <sys/ioctl.h>
44 u_char sun_len
; /* sockaddr len including null */
45 u_char sun_family
; /* AF_UNIX */
46 char sun_path
[108]; /* path name (gag) */
49 #include <sys/socket.h>
55 #include <netinet/in.h>
56 #include <arpa/inet.h>
63 #include <machine/endian.h>
69 #define EBADF SOCEBADF
75 #include <sys/socket.h>
76 #include <sys/ioctl.h>
77 #include <sys/select.h>
79 #define close(a) soclose(a)
80 #define select(a,b,c,d,e) bsdselect(a,b,c,d,e)
81 int _System
bsdselect(int,
86 int _System
soclose(int);
94 # include <sys/filio.h>
104 # define SOCKLEN_T unsigned int
108 # define SOCKLEN_T socklen_t
111 # define SOCKLEN_T int
115 #endif /* SOCKLEN_T */
118 * MSW defines this, Unices don't.
120 #ifndef INVALID_SOCKET
121 #define INVALID_SOCKET -1
124 /* UnixWare reportedly needs this for FIONBIO definition */
126 #include <sys/filio.h>
130 * INADDR_BROADCAST is identical to INADDR_NONE which is not defined
131 * on all systems. INADDR_BROADCAST should be fine to indicate an error.
134 #define INADDR_NONE INADDR_BROADCAST
137 #define MASK_SIGNAL() \
139 void (*old_handler)(int); \
141 old_handler = signal(SIGPIPE, SIG_IGN);
143 #define UNMASK_SIGNAL() \
144 signal(SIGPIPE, old_handler); \
148 #ifndef __GSOCKET_STANDALONE__
149 # include "wx/unix/gsockunx.h"
150 # include "wx/gsocket.h"
152 # include "gsockunx.h"
153 # include "gsocket.h"
154 #endif /* __GSOCKET_STANDALONE__ */
156 /* debugging helpers */
157 #ifdef __GSOCKET_DEBUG__
158 # define GSocket_Debug(args) printf args
160 # define GSocket_Debug(args)
161 #endif /* __GSOCKET_DEBUG__ */
163 /* Table of GUI-related functions. We must call them indirectly because
164 * of wxBase and GUI separation: */
166 static struct GSocketGUIFunctionsTable
*gs_gui_functions
;
168 #define USE_GUI() (gs_gui_functions != NULL)
170 /* Define macros to simplify indirection: */
171 #define _GSocket_GUI_Init() \
172 if (gs_gui_functions) gs_gui_functions->GUI_Init()
173 #define _GSocket_GUI_Cleanup() \
174 if (gs_gui_functions) gs_gui_functions->GUI_Cleanup()
175 #define _GSocket_GUI_Init_Socket(socket) \
176 (gs_gui_functions ? gs_gui_functions->GUI_Init_Socket(socket) : 1)
177 #define _GSocket_GUI_Destroy_Socket(socket) \
178 if (gs_gui_functions) gs_gui_functions->GUI_Destroy_Socket(socket)
179 #define _GSocket_Enable_Events(socket) \
180 if (gs_gui_functions) gs_gui_functions->Enable_Events(socket)
181 #define _GSocket_Disable_Events(socket) \
182 if (gs_gui_functions) gs_gui_functions->Disable_Events(socket)
183 #define _GSocket_Install_Callback(socket, event) \
184 if (gs_gui_functions) gs_gui_functions->Install_Callback(socket, event)
185 #define _GSocket_Uninstall_Callback(socket, event) \
186 if (gs_gui_functions) gs_gui_functions->Uninstall_Callback(socket, event)
188 static struct GSocketBaseFunctionsTable gs_base_functions
=
190 _GSocket_Detected_Read
,
191 _GSocket_Detected_Write
194 /* Global initialisers */
196 void GSocket_SetGUIFunctions(struct GSocketGUIFunctionsTable
*guifunc
)
198 gs_gui_functions
= guifunc
;
201 int GSocket_Init(void)
203 if (gs_gui_functions
)
205 if ( !gs_gui_functions
->GUI_Init() )
211 void GSocket_Cleanup(void)
213 if (gs_gui_functions
)
215 gs_gui_functions
->GUI_Cleanup();
219 /* Constructors / Destructors for GSocket */
221 GSocket
*GSocket_new(void)
226 socket
= (GSocket
*)malloc(sizeof(GSocket
));
231 socket
->m_fd
= INVALID_SOCKET
;
232 for (i
=0;i
<GSOCK_MAX_EVENT
;i
++)
234 socket
->m_cbacks
[i
] = NULL
;
236 socket
->m_detected
= 0;
237 socket
->m_local
= NULL
;
238 socket
->m_peer
= NULL
;
239 socket
->m_error
= GSOCK_NOERROR
;
240 socket
->m_server
= FALSE
;
241 socket
->m_stream
= TRUE
;
242 socket
->m_gui_dependent
= NULL
;
243 socket
->m_non_blocking
= FALSE
;
244 socket
->m_timeout
= 10*60*1000;
245 /* 10 minutes * 60 sec * 1000 millisec */
246 socket
->m_establishing
= FALSE
;
248 socket
->m_functions
= &gs_base_functions
;
250 /* Per-socket GUI-specific initialization */
251 success
= _GSocket_GUI_Init_Socket(socket
);
261 void GSocket_close(GSocket
*socket
)
263 _GSocket_Disable_Events(socket
);
264 /* gsockosx.c calls CFSocketInvalidate which closes the socket for us */
265 #if !(defined(__DARWIN__) && (defined(__WXMAC__) || defined(__WXCOCOA__)))
268 socket
->m_fd
= INVALID_SOCKET
;
271 void GSocket_destroy(GSocket
*socket
)
273 assert(socket
!= NULL
);
275 /* Check that the socket is really shutdowned */
276 if (socket
->m_fd
!= INVALID_SOCKET
)
277 GSocket_Shutdown(socket
);
279 /* Per-socket GUI-specific cleanup */
280 _GSocket_GUI_Destroy_Socket(socket
);
282 /* Destroy private addresses */
284 GAddress_destroy(socket
->m_local
);
287 GAddress_destroy(socket
->m_peer
);
289 /* Destroy the socket itself */
294 * Disallow further read/write operations on this socket, close
295 * the fd and disable all callbacks.
297 void GSocket_Shutdown(GSocket
*socket
)
301 assert(socket
!= NULL
);
303 /* If socket has been created, shutdown it */
304 if (socket
->m_fd
!= INVALID_SOCKET
)
306 shutdown(socket
->m_fd
, 2);
307 GSocket_close(socket
);
310 /* Disable GUI callbacks */
311 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
312 socket
->m_cbacks
[evt
] = NULL
;
314 socket
->m_detected
= GSOCK_LOST_FLAG
;
317 /* Address handling */
323 * Set or get the local or peer address for this socket. The 'set'
324 * functions return GSOCK_NOERROR on success, an error code otherwise.
325 * The 'get' functions return a pointer to a GAddress object on success,
326 * or NULL otherwise, in which case they set the error code of the
327 * corresponding GSocket.
330 * GSOCK_INVSOCK - the socket is not valid.
331 * GSOCK_INVADDR - the address is not valid.
333 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
335 assert(socket
!= NULL
);
337 /* the socket must be initialized, or it must be a server */
338 if ((socket
->m_fd
!= INVALID_SOCKET
&& !socket
->m_server
))
340 socket
->m_error
= GSOCK_INVSOCK
;
341 return GSOCK_INVSOCK
;
345 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
347 socket
->m_error
= GSOCK_INVADDR
;
348 return GSOCK_INVADDR
;
352 GAddress_destroy(socket
->m_local
);
354 socket
->m_local
= GAddress_copy(address
);
356 return GSOCK_NOERROR
;
359 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
361 assert(socket
!= NULL
);
364 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
366 socket
->m_error
= GSOCK_INVADDR
;
367 return GSOCK_INVADDR
;
371 GAddress_destroy(socket
->m_peer
);
373 socket
->m_peer
= GAddress_copy(address
);
375 return GSOCK_NOERROR
;
378 GAddress
*GSocket_GetLocal(GSocket
*socket
)
381 struct sockaddr addr
;
382 SOCKLEN_T size
= sizeof(addr
);
385 assert(socket
!= NULL
);
387 /* try to get it from the m_local var first */
389 return GAddress_copy(socket
->m_local
);
391 /* else, if the socket is initialized, try getsockname */
392 if (socket
->m_fd
== INVALID_SOCKET
)
394 socket
->m_error
= GSOCK_INVSOCK
;
398 if (getsockname(socket
->m_fd
, &addr
, (SOCKLEN_T
*) &size
) < 0)
400 socket
->m_error
= GSOCK_IOERR
;
404 /* got a valid address from getsockname, create a GAddress object */
405 address
= GAddress_new();
408 socket
->m_error
= GSOCK_MEMERR
;
412 err
= _GAddress_translate_from(address
, &addr
, size
);
413 if (err
!= GSOCK_NOERROR
)
415 GAddress_destroy(address
);
416 socket
->m_error
= err
;
423 GAddress
*GSocket_GetPeer(GSocket
*socket
)
425 assert(socket
!= NULL
);
427 /* try to get it from the m_peer var */
429 return GAddress_copy(socket
->m_peer
);
434 /* Server specific parts */
436 /* GSocket_SetServer:
437 * Sets up this socket as a server. The local address must have been
438 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
439 * Returns GSOCK_NOERROR on success, one of the following otherwise:
442 * GSOCK_INVSOCK - the socket is in use.
443 * GSOCK_INVADDR - the local address has not been set.
444 * GSOCK_IOERR - low-level error.
446 GSocketError
GSocket_SetServer(GSocket
*sck
)
452 /* must not be in use */
453 if (sck
->m_fd
!= INVALID_SOCKET
)
455 sck
->m_error
= GSOCK_INVSOCK
;
456 return GSOCK_INVSOCK
;
459 /* the local addr must have been set */
462 sck
->m_error
= GSOCK_INVADDR
;
463 return GSOCK_INVADDR
;
466 /* Initialize all fields */
467 sck
->m_stream
= TRUE
;
468 sck
->m_server
= TRUE
;
469 sck
->m_oriented
= TRUE
;
471 /* Create the socket */
472 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
474 if (sck
->m_fd
== INVALID_SOCKET
)
476 sck
->m_error
= GSOCK_IOERR
;
479 #if defined(__EMX__) || defined(__VISAGECPP__)
480 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
482 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
484 _GSocket_Enable_Events(sck
);
486 /* allow a socket to re-bind if the socket is in the TIME_WAIT
487 state after being previously closed.
490 setsockopt(sck
->m_fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char*)&arg
, sizeof(u_long
));
492 /* Bind to the local address,
493 * retrieve the actual address bound,
494 * and listen up to 5 connections.
496 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
497 (getsockname(sck
->m_fd
,
498 sck
->m_local
->m_addr
,
499 (SOCKLEN_T
*) &sck
->m_local
->m_len
) != 0) ||
500 (listen(sck
->m_fd
, 5) != 0))
503 sck
->m_error
= GSOCK_IOERR
;
507 return GSOCK_NOERROR
;
510 /* GSocket_WaitConnection:
511 * Waits for an incoming client connection. Returns a pointer to
512 * a GSocket object, or NULL if there was an error, in which case
513 * the last error field will be updated for the calling GSocket.
515 * Error codes (set in the calling GSocket)
516 * GSOCK_INVSOCK - the socket is not valid or not a server.
517 * GSOCK_TIMEDOUT - timeout, no incoming connections.
518 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
519 * GSOCK_MEMERR - couldn't allocate memory.
520 * GSOCK_IOERR - low-level error.
522 GSocket
*GSocket_WaitConnection(GSocket
*socket
)
524 struct sockaddr from
;
525 SOCKLEN_T fromlen
= sizeof(from
);
530 assert(socket
!= NULL
);
532 /* Reenable CONNECTION events */
533 _GSocket_Enable(socket
, GSOCK_CONNECTION
);
535 /* If the socket has already been created, we exit immediately */
536 if (socket
->m_fd
== INVALID_SOCKET
|| !socket
->m_server
)
538 socket
->m_error
= GSOCK_INVSOCK
;
542 /* Create a GSocket object for the new connection */
543 connection
= GSocket_new();
547 socket
->m_error
= GSOCK_MEMERR
;
551 /* Wait for a connection (with timeout) */
552 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
554 GSocket_destroy(connection
);
555 /* socket->m_error set by _GSocket_Input_Timeout */
559 connection
->m_fd
= accept(socket
->m_fd
, &from
, (SOCKLEN_T
*) &fromlen
);
561 if (connection
->m_fd
== INVALID_SOCKET
)
563 if (errno
== EWOULDBLOCK
)
564 socket
->m_error
= GSOCK_WOULDBLOCK
;
566 socket
->m_error
= GSOCK_IOERR
;
568 GSocket_destroy(connection
);
572 /* Initialize all fields */
573 connection
->m_server
= FALSE
;
574 connection
->m_stream
= TRUE
;
575 connection
->m_oriented
= TRUE
;
577 /* Setup the peer address field */
578 connection
->m_peer
= GAddress_new();
579 if (!connection
->m_peer
)
581 GSocket_destroy(connection
);
582 socket
->m_error
= GSOCK_MEMERR
;
585 err
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
);
586 if (err
!= GSOCK_NOERROR
)
588 GAddress_destroy(connection
->m_peer
);
589 GSocket_destroy(connection
);
590 socket
->m_error
= err
;
593 #if defined(__EMX__) || defined(__VISAGECPP__)
594 ioctl(connection
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
596 ioctl(connection
->m_fd
, FIONBIO
, &arg
);
598 _GSocket_Enable_Events(connection
);
603 int GSocket_SetReusable(GSocket
*socket
)
605 /* socket must not be null, and must not be in use/already bound */
606 if (NULL
!= socket
&& socket
->m_fd
== INVALID_SOCKET
) {
607 socket
->m_reusable
= TRUE
;
613 /* Client specific parts */
616 * For stream (connection oriented) sockets, GSocket_Connect() tries
617 * to establish a client connection to a server using the peer address
618 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
619 * connection has been succesfully established, or one of the error
620 * codes listed below. Note that for nonblocking sockets, a return
621 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
622 * request can be completed later; you should use GSocket_Select()
623 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
624 * corresponding asynchronous events.
626 * For datagram (non connection oriented) sockets, GSocket_Connect()
627 * just sets the peer address established with GSocket_SetPeer() as
628 * default destination.
631 * GSOCK_INVSOCK - the socket is in use or not valid.
632 * GSOCK_INVADDR - the peer address has not been established.
633 * GSOCK_TIMEDOUT - timeout, the connection failed.
634 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
635 * GSOCK_MEMERR - couldn't allocate memory.
636 * GSOCK_IOERR - low-level error.
638 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
645 /* Enable CONNECTION events (needed for nonblocking connections) */
646 _GSocket_Enable(sck
, GSOCK_CONNECTION
);
648 if (sck
->m_fd
!= INVALID_SOCKET
)
650 sck
->m_error
= GSOCK_INVSOCK
;
651 return GSOCK_INVSOCK
;
656 sck
->m_error
= GSOCK_INVADDR
;
657 return GSOCK_INVADDR
;
660 /* Streamed or dgram socket? */
661 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
662 sck
->m_oriented
= TRUE
;
663 sck
->m_server
= FALSE
;
664 sck
->m_establishing
= FALSE
;
666 /* Create the socket */
667 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
,
668 sck
->m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
670 if (sck
->m_fd
== INVALID_SOCKET
)
672 sck
->m_error
= GSOCK_IOERR
;
675 #if defined(__EMX__) || defined(__VISAGECPP__)
676 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
678 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
680 _GSocket_Enable_Events(sck
);
682 /* Connect it to the peer address, with a timeout (see below) */
683 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
689 /* If connect failed with EINPROGRESS and the GSocket object
690 * is in blocking mode, we select() for the specified timeout
691 * checking for writability to see if the connection request
694 if ((err
== EINPROGRESS
) && (!sck
->m_non_blocking
))
696 if (_GSocket_Output_Timeout(sck
) == GSOCK_TIMEDOUT
)
699 /* sck->m_error is set in _GSocket_Output_Timeout */
700 return GSOCK_TIMEDOUT
;
705 SOCKLEN_T len
= sizeof(error
);
707 getsockopt(sck
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*) &error
, &len
);
710 return GSOCK_NOERROR
;
714 /* If connect failed with EINPROGRESS and the GSocket object
715 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
716 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
717 * this way if the connection completes, a GSOCK_CONNECTION
718 * event will be generated, if enabled.
720 if ((err
== EINPROGRESS
) && (sck
->m_non_blocking
))
722 sck
->m_establishing
= TRUE
;
723 sck
->m_error
= GSOCK_WOULDBLOCK
;
724 return GSOCK_WOULDBLOCK
;
727 /* If connect failed with an error other than EINPROGRESS,
728 * then the call to GSocket_Connect has failed.
731 sck
->m_error
= GSOCK_IOERR
;
735 return GSOCK_NOERROR
;
738 /* Datagram sockets */
740 /* GSocket_SetNonOriented:
741 * Sets up this socket as a non-connection oriented (datagram) socket.
742 * Before using this function, the local address must have been set
743 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
744 * on success, or one of the following otherwise.
747 * GSOCK_INVSOCK - the socket is in use.
748 * GSOCK_INVADDR - the local address has not been set.
749 * GSOCK_IOERR - low-level error.
751 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
757 if (sck
->m_fd
!= INVALID_SOCKET
)
759 sck
->m_error
= GSOCK_INVSOCK
;
760 return GSOCK_INVSOCK
;
765 sck
->m_error
= GSOCK_INVADDR
;
766 return GSOCK_INVADDR
;
769 /* Initialize all fields */
770 sck
->m_stream
= FALSE
;
771 sck
->m_server
= FALSE
;
772 sck
->m_oriented
= FALSE
;
774 /* Create the socket */
775 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
777 if (sck
->m_fd
== INVALID_SOCKET
)
779 sck
->m_error
= GSOCK_IOERR
;
782 #if defined(__EMX__) || defined(__VISAGECPP__)
783 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
785 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
787 _GSocket_Enable_Events(sck
);
789 /* Bind to the local address,
790 * and retrieve the actual address bound.
792 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
793 (getsockname(sck
->m_fd
,
794 sck
->m_local
->m_addr
,
795 (SOCKLEN_T
*) &sck
->m_local
->m_len
) != 0))
798 sck
->m_error
= GSOCK_IOERR
;
802 return GSOCK_NOERROR
;
807 /* Like recv(), send(), ... */
808 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
812 assert(socket
!= NULL
);
814 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
816 socket
->m_error
= GSOCK_INVSOCK
;
820 /* Disable events during query of socket status */
821 _GSocket_Disable(socket
, GSOCK_INPUT
);
823 /* If the socket is blocking, wait for data (with a timeout) */
824 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
825 /* We no longer return here immediately, otherwise socket events would not be re-enabled! */
829 if (socket
->m_stream
)
830 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
832 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
837 if (errno
== EWOULDBLOCK
)
838 socket
->m_error
= GSOCK_WOULDBLOCK
;
840 socket
->m_error
= GSOCK_IOERR
;
843 /* Enable events again now that we are done processing */
844 _GSocket_Enable(socket
, GSOCK_INPUT
);
849 int GSocket_Write(GSocket
*socket
, const char *buffer
, int size
)
853 assert(socket
!= NULL
);
855 GSocket_Debug(( "GSocket_Write #1, size %d\n", size
));
857 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
859 socket
->m_error
= GSOCK_INVSOCK
;
863 GSocket_Debug(( "GSocket_Write #2, size %d\n", size
));
865 /* If the socket is blocking, wait for writability (with a timeout) */
866 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
869 GSocket_Debug(( "GSocket_Write #3, size %d\n", size
));
872 if (socket
->m_stream
)
873 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
875 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
877 GSocket_Debug(( "GSocket_Write #4, size %d\n", size
));
881 if (errno
== EWOULDBLOCK
)
883 socket
->m_error
= GSOCK_WOULDBLOCK
;
884 GSocket_Debug(( "GSocket_Write error WOULDBLOCK\n" ));
888 socket
->m_error
= GSOCK_IOERR
;
889 GSocket_Debug(( "GSocket_Write error IOERR\n" ));
892 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
893 * in MSW). Once the first OUTPUT event is received, users can assume
894 * that the socket is writable until a read operation fails. Only then
895 * will further OUTPUT events be posted.
897 _GSocket_Enable(socket
, GSOCK_OUTPUT
);
901 GSocket_Debug(( "GSocket_Write #5, size %d ret %d\n", size
, ret
));
907 * Polls the socket to determine its status. This function will
908 * check for the events specified in the 'flags' parameter, and
909 * it will return a mask indicating which operations can be
910 * performed. This function won't block, regardless of the
911 * mode (blocking | nonblocking) of the socket.
913 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
918 GSocketEventFlags result
= 0;
924 assert(socket
!= NULL
);
926 /* Do not use a static struct, Linux can garble it */
927 tv
.tv_sec
= socket
->m_timeout
/ 1000;
928 tv
.tv_usec
= (socket
->m_timeout
% 1000) / 1000;
933 FD_SET(socket
->m_fd
, &readfds
);
934 if (flags
& GSOCK_OUTPUT_FLAG
|| flags
& GSOCK_CONNECTION_FLAG
)
935 FD_SET(socket
->m_fd
, &writefds
);
936 FD_SET(socket
->m_fd
, &exceptfds
);
938 /* Check 'sticky' CONNECTION flag first */
939 result
|= (GSOCK_CONNECTION_FLAG
& socket
->m_detected
);
941 /* If we have already detected a LOST event, then don't try
942 * to do any further processing.
944 if ((socket
->m_detected
& GSOCK_LOST_FLAG
) != 0)
946 socket
->m_establishing
= FALSE
;
948 return (GSOCK_LOST_FLAG
& flags
);
952 if (select(socket
->m_fd
+ 1, &readfds
, &writefds
, &exceptfds
, &tv
) <= 0)
954 /* What to do here? */
955 return (result
& flags
);
958 /* Check for readability */
959 if (FD_ISSET(socket
->m_fd
, &readfds
))
963 if (recv(socket
->m_fd
, &c
, 1, MSG_PEEK
) > 0)
965 result
|= GSOCK_INPUT_FLAG
;
969 if (socket
->m_server
&& socket
->m_stream
)
971 result
|= GSOCK_CONNECTION_FLAG
;
972 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
976 socket
->m_detected
= GSOCK_LOST_FLAG
;
977 socket
->m_establishing
= FALSE
;
979 /* LOST event: Abort any further processing */
980 return (GSOCK_LOST_FLAG
& flags
);
985 /* Check for writability */
986 if (FD_ISSET(socket
->m_fd
, &writefds
))
988 if (socket
->m_establishing
&& !socket
->m_server
)
991 SOCKLEN_T len
= sizeof(error
);
993 socket
->m_establishing
= FALSE
;
995 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*)&error
, &len
);
999 socket
->m_detected
= GSOCK_LOST_FLAG
;
1001 /* LOST event: Abort any further processing */
1002 return (GSOCK_LOST_FLAG
& flags
);
1006 result
|= GSOCK_CONNECTION_FLAG
;
1007 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
1012 result
|= GSOCK_OUTPUT_FLAG
;
1016 /* Check for exceptions and errors (is this useful in Unices?) */
1017 if (FD_ISSET(socket
->m_fd
, &exceptfds
))
1019 socket
->m_establishing
= FALSE
;
1020 socket
->m_detected
= GSOCK_LOST_FLAG
;
1022 /* LOST event: Abort any further processing */
1023 return (GSOCK_LOST_FLAG
& flags
);
1026 return (result
& flags
);
1032 assert(socket
!= NULL
);
1033 return flags
& socket
->m_detected
;
1040 /* GSocket_SetNonBlocking:
1041 * Sets the socket to non-blocking mode. All IO calls will return
1044 void GSocket_SetNonBlocking(GSocket
*socket
, int non_block
)
1046 assert(socket
!= NULL
);
1048 GSocket_Debug( ("GSocket_SetNonBlocking: %d\n", (int)non_block
) );
1050 socket
->m_non_blocking
= non_block
;
1053 /* GSocket_SetTimeout:
1054 * Sets the timeout for blocking calls. Time is expressed in
1057 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
)
1059 assert(socket
!= NULL
);
1061 socket
->m_timeout
= millisec
;
1064 /* GSocket_GetError:
1065 * Returns the last error occured for this socket. Note that successful
1066 * operations do not clear this back to GSOCK_NOERROR, so use it only
1069 GSocketError
GSocket_GetError(GSocket
*socket
)
1071 assert(socket
!= NULL
);
1073 return socket
->m_error
;
1079 * There is data to be read in the input buffer. If, after a read
1080 * operation, there is still data available, the callback function will
1083 * The socket is available for writing. That is, the next write call
1084 * won't block. This event is generated only once, when the connection is
1085 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
1086 * when the output buffer empties again. This means that the app should
1087 * assume that it can write since the first OUTPUT event, and no more
1088 * OUTPUT events will be generated unless an error occurs.
1090 * Connection succesfully established, for client sockets, or incoming
1091 * client connection, for server sockets. Wait for this event (also watch
1092 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
1094 * The connection is lost (or a connection request failed); this could
1095 * be due to a failure, or due to the peer closing it gracefully.
1098 /* GSocket_SetCallback:
1099 * Enables the callbacks specified by 'flags'. Note that 'flags'
1100 * may be a combination of flags OR'ed toghether, so the same
1101 * callback function can be made to accept different events.
1102 * The callback function must have the following prototype:
1104 * void function(GSocket *socket, GSocketEvent event, char *cdata)
1106 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
1107 GSocketCallback callback
, char *cdata
)
1111 assert(socket
!= NULL
);
1113 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
1115 if ((flags
& (1 << count
)) != 0)
1117 socket
->m_cbacks
[count
] = callback
;
1118 socket
->m_data
[count
] = cdata
;
1123 /* GSocket_UnsetCallback:
1124 * Disables all callbacks specified by 'flags', which may be a
1125 * combination of flags OR'ed toghether.
1127 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
1131 assert(socket
!= NULL
);
1133 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
1135 if ((flags
& (1 << count
)) != 0)
1137 socket
->m_cbacks
[count
] = NULL
;
1138 socket
->m_data
[count
] = NULL
;
1143 GSocketError
GSocket_GetSockOpt(GSocket
*socket
, int level
, int optname
,
1144 void *optval
, int *optlen
)
1146 if (getsockopt(socket
->m_fd
, level
, optname
, optval
, optlen
) == 0)
1148 return GSOCK_NOERROR
;
1150 return GSOCK_OPTERR
;
1153 GSocketError
GSocket_SetSockOpt(GSocket
*socket
, int level
, int optname
,
1154 const void *optval
, int optlen
)
1156 if (setsockopt(socket
->m_fd
, level
, optname
, optval
, optlen
) == 0)
1158 return GSOCK_NOERROR
;
1160 return GSOCK_OPTERR
;
1163 void GSocket_Streamed(GSocket
*socket
)
1165 socket
->m_stream
= TRUE
;
1168 void GSocket_Unstreamed(GSocket
*socket
)
1170 socket
->m_stream
= FALSE
;
1173 #define CALL_CALLBACK(socket, event) { \
1174 _GSocket_Disable(socket, event); \
1175 if (socket->m_cbacks[event]) \
1176 socket->m_cbacks[event](socket, event, socket->m_data[event]); \
1180 void _GSocket_Enable(GSocket
*socket
, GSocketEvent event
)
1182 socket
->m_detected
&= ~(1 << event
);
1183 _GSocket_Install_Callback(socket
, event
);
1186 void _GSocket_Disable(GSocket
*socket
, GSocketEvent event
)
1188 socket
->m_detected
|= (1 << event
);
1189 _GSocket_Uninstall_Callback(socket
, event
);
1192 /* _GSocket_Input_Timeout:
1193 * For blocking sockets, wait until data is available or
1194 * until timeout ellapses.
1196 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
1202 /* Linux select() will overwrite the struct on return */
1203 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
1204 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
1206 if (!socket
->m_non_blocking
)
1209 FD_SET(socket
->m_fd
, &readfds
);
1210 ret
= select(socket
->m_fd
+ 1, &readfds
, NULL
, NULL
, &tv
);
1213 GSocket_Debug(( "GSocket_Input_Timeout, select returned 0\n" ));
1214 socket
->m_error
= GSOCK_TIMEDOUT
;
1215 return GSOCK_TIMEDOUT
;
1219 GSocket_Debug(( "GSocket_Input_Timeout, select returned -1\n" ));
1220 if (errno
== EBADF
) { GSocket_Debug(( "Invalid file descriptor\n" )); }
1221 if (errno
== EINTR
) { GSocket_Debug(( "A non blocked signal was caught\n" )); }
1222 if (errno
== EINVAL
) { GSocket_Debug(( "The highest number descriptor is negative\n" )); }
1223 if (errno
== ENOMEM
) { GSocket_Debug(( "Not enough memory\n" )); }
1224 socket
->m_error
= GSOCK_TIMEDOUT
;
1225 return GSOCK_TIMEDOUT
;
1228 return GSOCK_NOERROR
;
1231 /* _GSocket_Output_Timeout:
1232 * For blocking sockets, wait until data can be sent without
1233 * blocking or until timeout ellapses.
1235 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
1241 /* Linux select() will overwrite the struct on return */
1242 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
1243 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
1245 GSocket_Debug( ("m_non_blocking has: %d\n", (int)socket
->m_non_blocking
) );
1247 if (!socket
->m_non_blocking
)
1250 FD_SET(socket
->m_fd
, &writefds
);
1251 ret
= select(socket
->m_fd
+ 1, NULL
, &writefds
, NULL
, &tv
);
1254 GSocket_Debug(( "GSocket_Output_Timeout, select returned 0\n" ));
1255 socket
->m_error
= GSOCK_TIMEDOUT
;
1256 return GSOCK_TIMEDOUT
;
1260 GSocket_Debug(( "GSocket_Output_Timeout, select returned -1\n" ));
1261 if (errno
== EBADF
) { GSocket_Debug(( "Invalid file descriptor\n" )); }
1262 if (errno
== EINTR
) { GSocket_Debug(( "A non blocked signal was caught\n" )); }
1263 if (errno
== EINVAL
) { GSocket_Debug(( "The highest number descriptor is negative\n" )); }
1264 if (errno
== ENOMEM
) { GSocket_Debug(( "Not enough memory\n" )); }
1265 socket
->m_error
= GSOCK_TIMEDOUT
;
1266 return GSOCK_TIMEDOUT
;
1268 if ( ! FD_ISSET(socket
->m_fd
, &writefds
) ) {
1269 GSocket_Debug(( "GSocket_Output_Timeout is buggy!\n" ));
1272 GSocket_Debug(( "GSocket_Output_Timeout seems correct\n" ));
1277 GSocket_Debug(( "GSocket_Output_Timeout, didn't try select!\n" ));
1280 return GSOCK_NOERROR
;
1283 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
1285 return recv(socket
->m_fd
, buffer
, size
, 0);
1288 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
1290 struct sockaddr from
;
1291 SOCKLEN_T fromlen
= sizeof(from
);
1295 fromlen
= sizeof(from
);
1297 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, (SOCKLEN_T
*) &fromlen
);
1302 /* Translate a system address into a GSocket address */
1303 if (!socket
->m_peer
)
1305 socket
->m_peer
= GAddress_new();
1306 if (!socket
->m_peer
)
1308 socket
->m_error
= GSOCK_MEMERR
;
1312 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
1313 if (err
!= GSOCK_NOERROR
)
1315 GAddress_destroy(socket
->m_peer
);
1316 socket
->m_peer
= NULL
;
1317 socket
->m_error
= err
;
1324 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
1328 #ifndef __VISAGECPP__
1330 ret
= send(socket
->m_fd
, buffer
, size
, 0);
1333 ret
= send(socket
->m_fd
, (char *)buffer
, size
, 0);
1339 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
1341 struct sockaddr
*addr
;
1345 if (!socket
->m_peer
)
1347 socket
->m_error
= GSOCK_INVADDR
;
1351 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
1352 if (err
!= GSOCK_NOERROR
)
1354 socket
->m_error
= err
;
1358 #ifndef __VISAGECPP__
1360 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
1363 ret
= sendto(socket
->m_fd
, (char *)buffer
, size
, 0, addr
, len
);
1366 /* Frees memory allocated from _GAddress_translate_to */
1372 void _GSocket_Detected_Read(GSocket
*socket
)
1376 /* If we have already detected a LOST event, then don't try
1377 * to do any further processing.
1379 if ((socket
->m_detected
& GSOCK_LOST_FLAG
) != 0)
1381 socket
->m_establishing
= FALSE
;
1383 CALL_CALLBACK(socket
, GSOCK_LOST
);
1384 GSocket_Shutdown(socket
);
1388 if (recv(socket
->m_fd
, &c
, 1, MSG_PEEK
) > 0)
1390 CALL_CALLBACK(socket
, GSOCK_INPUT
);
1394 if (socket
->m_server
&& socket
->m_stream
)
1396 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
1400 CALL_CALLBACK(socket
, GSOCK_LOST
);
1401 GSocket_Shutdown(socket
);
1406 void _GSocket_Detected_Write(GSocket
*socket
)
1408 /* If we have already detected a LOST event, then don't try
1409 * to do any further processing.
1411 if ((socket
->m_detected
& GSOCK_LOST_FLAG
) != 0)
1413 socket
->m_establishing
= FALSE
;
1415 CALL_CALLBACK(socket
, GSOCK_LOST
);
1416 GSocket_Shutdown(socket
);
1420 if (socket
->m_establishing
&& !socket
->m_server
)
1423 SOCKLEN_T len
= sizeof(error
);
1425 socket
->m_establishing
= FALSE
;
1427 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*)&error
, &len
);
1431 CALL_CALLBACK(socket
, GSOCK_LOST
);
1432 GSocket_Shutdown(socket
);
1436 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
1437 /* We have to fire this event by hand because CONNECTION (for clients)
1438 * and OUTPUT are internally the same and we just disabled CONNECTION
1439 * events with the above macro.
1441 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
1446 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
1451 * -------------------------------------------------------------------------
1453 * -------------------------------------------------------------------------
1456 /* CHECK_ADDRESS verifies that the current address family is either
1457 * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it
1458 * initalizes it to be a GSOCK_*family*. In other cases, it returns
1459 * an appropiate error code.
1461 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
1463 #define CHECK_ADDRESS(address, family) \
1465 if (address->m_family == GSOCK_NOFAMILY) \
1466 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1467 return address->m_error; \
1468 if (address->m_family != GSOCK_##family) \
1470 address->m_error = GSOCK_INVADDR; \
1471 return GSOCK_INVADDR; \
1475 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
1477 if (address->m_family == GSOCK_NOFAMILY) \
1478 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1480 if (address->m_family != GSOCK_##family) \
1482 address->m_error = GSOCK_INVADDR; \
1488 GAddress
*GAddress_new(void)
1492 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1495 address
->m_family
= GSOCK_NOFAMILY
;
1496 address
->m_addr
= NULL
;
1502 GAddress
*GAddress_copy(GAddress
*address
)
1506 assert(address
!= NULL
);
1508 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1511 memcpy(addr2
, address
, sizeof(GAddress
));
1513 if (address
->m_addr
&& address
->m_len
> 0)
1515 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
1516 if (addr2
->m_addr
== NULL
)
1521 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1527 void GAddress_destroy(GAddress
*address
)
1529 assert(address
!= NULL
);
1531 if (address
->m_addr
)
1532 free(address
->m_addr
);
1537 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1539 assert(address
!= NULL
);
1541 address
->m_family
= type
;
1544 GAddressType
GAddress_GetFamily(GAddress
*address
)
1546 assert(address
!= NULL
);
1548 return address
->m_family
;
1551 GSocketError
_GAddress_translate_from(GAddress
*address
,
1552 struct sockaddr
*addr
, int len
)
1554 address
->m_realfamily
= addr
->sa_family
;
1555 switch (addr
->sa_family
)
1558 address
->m_family
= GSOCK_INET
;
1561 address
->m_family
= GSOCK_UNIX
;
1565 address
->m_family
= GSOCK_INET6
;
1570 address
->m_error
= GSOCK_INVOP
;
1575 if (address
->m_addr
)
1576 free(address
->m_addr
);
1578 address
->m_len
= len
;
1579 address
->m_addr
= (struct sockaddr
*)malloc(len
);
1581 if (address
->m_addr
== NULL
)
1583 address
->m_error
= GSOCK_MEMERR
;
1584 return GSOCK_MEMERR
;
1586 memcpy(address
->m_addr
, addr
, len
);
1588 return GSOCK_NOERROR
;
1591 GSocketError
_GAddress_translate_to(GAddress
*address
,
1592 struct sockaddr
**addr
, int *len
)
1594 if (!address
->m_addr
)
1596 address
->m_error
= GSOCK_INVADDR
;
1597 return GSOCK_INVADDR
;
1600 *len
= address
->m_len
;
1601 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
1604 address
->m_error
= GSOCK_MEMERR
;
1605 return GSOCK_MEMERR
;
1608 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1609 return GSOCK_NOERROR
;
1613 * -------------------------------------------------------------------------
1614 * Internet address family
1615 * -------------------------------------------------------------------------
1618 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1620 address
->m_len
= sizeof(struct sockaddr_in
);
1621 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1622 if (address
->m_addr
== NULL
)
1624 address
->m_error
= GSOCK_MEMERR
;
1625 return GSOCK_MEMERR
;
1628 address
->m_family
= GSOCK_INET
;
1629 address
->m_realfamily
= PF_INET
;
1630 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1631 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1633 return GSOCK_NOERROR
;
1636 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1639 struct in_addr
*addr
;
1641 assert(address
!= NULL
);
1643 CHECK_ADDRESS(address
, INET
);
1645 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1647 /* If it is a numeric host name, convert it now */
1648 #if defined(HAVE_INET_ATON)
1649 if (inet_aton(hostname
, addr
) == 0)
1651 #elif defined(HAVE_INET_ADDR)
1652 if ( (addr
->s_addr
= inet_addr(hostname
)) == -1 )
1655 /* Use gethostbyname by default */
1657 int val
= 1; /* VA doesn't like constants in conditional expressions */
1662 struct in_addr
*array_addr
;
1664 /* It is a real name, we solve it */
1665 if ((he
= gethostbyname(hostname
)) == NULL
)
1667 /* Reset to invalid address */
1668 addr
->s_addr
= INADDR_NONE
;
1669 address
->m_error
= GSOCK_NOHOST
;
1670 return GSOCK_NOHOST
;
1672 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1673 addr
->s_addr
= array_addr
[0].s_addr
;
1675 return GSOCK_NOERROR
;
1678 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1680 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1683 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1684 unsigned long hostaddr
)
1686 struct in_addr
*addr
;
1688 assert(address
!= NULL
);
1690 CHECK_ADDRESS(address
, INET
);
1692 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1693 addr
->s_addr
= htonl(hostaddr
);
1695 return GSOCK_NOERROR
;
1698 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1699 const char *protocol
)
1702 struct sockaddr_in
*addr
;
1704 assert(address
!= NULL
);
1705 CHECK_ADDRESS(address
, INET
);
1709 address
->m_error
= GSOCK_INVPORT
;
1710 return GSOCK_INVPORT
;
1713 se
= getservbyname(port
, protocol
);
1716 /* the cast to int suppresses compiler warnings about subscript having the
1718 if (isdigit((int)port
[0]))
1722 port_int
= atoi(port
);
1723 addr
= (struct sockaddr_in
*)address
->m_addr
;
1724 addr
->sin_port
= htons(port_int
);
1725 return GSOCK_NOERROR
;
1728 address
->m_error
= GSOCK_INVPORT
;
1729 return GSOCK_INVPORT
;
1732 addr
= (struct sockaddr_in
*)address
->m_addr
;
1733 addr
->sin_port
= se
->s_port
;
1735 return GSOCK_NOERROR
;
1738 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1740 struct sockaddr_in
*addr
;
1742 assert(address
!= NULL
);
1743 CHECK_ADDRESS(address
, INET
);
1745 addr
= (struct sockaddr_in
*)address
->m_addr
;
1746 addr
->sin_port
= htons(port
);
1748 return GSOCK_NOERROR
;
1751 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1755 struct sockaddr_in
*addr
;
1757 assert(address
!= NULL
);
1758 CHECK_ADDRESS(address
, INET
);
1760 addr
= (struct sockaddr_in
*)address
->m_addr
;
1761 addr_buf
= (char *)&(addr
->sin_addr
);
1763 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1766 address
->m_error
= GSOCK_NOHOST
;
1767 return GSOCK_NOHOST
;
1770 strncpy(hostname
, he
->h_name
, sbuf
);
1772 return GSOCK_NOERROR
;
1775 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1777 struct sockaddr_in
*addr
;
1779 assert(address
!= NULL
);
1780 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1782 addr
= (struct sockaddr_in
*)address
->m_addr
;
1784 return ntohl(addr
->sin_addr
.s_addr
);
1787 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1789 struct sockaddr_in
*addr
;
1791 assert(address
!= NULL
);
1792 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1794 addr
= (struct sockaddr_in
*)address
->m_addr
;
1795 return ntohs(addr
->sin_port
);
1799 * -------------------------------------------------------------------------
1800 * Unix address family
1801 * -------------------------------------------------------------------------
1804 #ifndef __VISAGECPP__
1805 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1807 address
->m_len
= sizeof(struct sockaddr_un
);
1808 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
1809 if (address
->m_addr
== NULL
)
1811 address
->m_error
= GSOCK_MEMERR
;
1812 return GSOCK_MEMERR
;
1815 address
->m_family
= GSOCK_UNIX
;
1816 address
->m_realfamily
= PF_UNIX
;
1817 ((struct sockaddr_un
*)address
->m_addr
)->sun_family
= AF_UNIX
;
1818 ((struct sockaddr_un
*)address
->m_addr
)->sun_path
[0] = 0;
1820 return GSOCK_NOERROR
;
1823 #define UNIX_SOCK_PATHLEN (sizeof(addr->sun_path)/sizeof(addr->sun_path[0]))
1825 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1827 struct sockaddr_un
*addr
;
1829 assert(address
!= NULL
);
1831 CHECK_ADDRESS(address
, UNIX
);
1833 addr
= ((struct sockaddr_un
*)address
->m_addr
);
1834 strncpy(addr
->sun_path
, path
, UNIX_SOCK_PATHLEN
);
1835 addr
->sun_path
[UNIX_SOCK_PATHLEN
- 1] = '\0';
1837 return GSOCK_NOERROR
;
1840 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1842 struct sockaddr_un
*addr
;
1844 assert(address
!= NULL
);
1845 CHECK_ADDRESS(address
, UNIX
);
1847 addr
= (struct sockaddr_un
*)address
->m_addr
;
1849 strncpy(path
, addr
->sun_path
, sbuf
);
1851 return GSOCK_NOERROR
;
1853 #endif /* !defined(__VISAGECPP__) */
1854 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */