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
;
470 /* Create the socket */
471 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
473 if (sck
->m_fd
== INVALID_SOCKET
)
475 sck
->m_error
= GSOCK_IOERR
;
478 #if defined(__EMX__) || defined(__VISAGECPP__)
479 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
481 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
483 _GSocket_Enable_Events(sck
);
485 /* allow a socket to re-bind if the socket is in the TIME_WAIT
486 state after being previously closed.
489 setsockopt(sck
->m_fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char*)&arg
, sizeof(u_long
));
491 /* Bind to the local address,
492 * retrieve the actual address bound,
493 * and listen up to 5 connections.
495 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
496 (getsockname(sck
->m_fd
,
497 sck
->m_local
->m_addr
,
498 (SOCKLEN_T
*) &sck
->m_local
->m_len
) != 0) ||
499 (listen(sck
->m_fd
, 5) != 0))
502 sck
->m_error
= GSOCK_IOERR
;
506 return GSOCK_NOERROR
;
509 /* GSocket_WaitConnection:
510 * Waits for an incoming client connection. Returns a pointer to
511 * a GSocket object, or NULL if there was an error, in which case
512 * the last error field will be updated for the calling GSocket.
514 * Error codes (set in the calling GSocket)
515 * GSOCK_INVSOCK - the socket is not valid or not a server.
516 * GSOCK_TIMEDOUT - timeout, no incoming connections.
517 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
518 * GSOCK_MEMERR - couldn't allocate memory.
519 * GSOCK_IOERR - low-level error.
521 GSocket
*GSocket_WaitConnection(GSocket
*socket
)
523 struct sockaddr from
;
524 SOCKLEN_T fromlen
= sizeof(from
);
529 assert(socket
!= NULL
);
531 /* Reenable CONNECTION events */
532 _GSocket_Enable(socket
, GSOCK_CONNECTION
);
534 /* If the socket has already been created, we exit immediately */
535 if (socket
->m_fd
== INVALID_SOCKET
|| !socket
->m_server
)
537 socket
->m_error
= GSOCK_INVSOCK
;
541 /* Create a GSocket object for the new connection */
542 connection
= GSocket_new();
546 socket
->m_error
= GSOCK_MEMERR
;
550 /* Wait for a connection (with timeout) */
551 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
553 GSocket_destroy(connection
);
554 /* socket->m_error set by _GSocket_Input_Timeout */
558 connection
->m_fd
= accept(socket
->m_fd
, &from
, (SOCKLEN_T
*) &fromlen
);
560 if (connection
->m_fd
== INVALID_SOCKET
)
562 if (errno
== EWOULDBLOCK
)
563 socket
->m_error
= GSOCK_WOULDBLOCK
;
565 socket
->m_error
= GSOCK_IOERR
;
567 GSocket_destroy(connection
);
571 /* Initialize all fields */
572 connection
->m_server
= FALSE
;
573 connection
->m_stream
= TRUE
;
575 /* Setup the peer address field */
576 connection
->m_peer
= GAddress_new();
577 if (!connection
->m_peer
)
579 GSocket_destroy(connection
);
580 socket
->m_error
= GSOCK_MEMERR
;
583 err
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
);
584 if (err
!= GSOCK_NOERROR
)
586 GAddress_destroy(connection
->m_peer
);
587 GSocket_destroy(connection
);
588 socket
->m_error
= err
;
591 #if defined(__EMX__) || defined(__VISAGECPP__)
592 ioctl(connection
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
594 ioctl(connection
->m_fd
, FIONBIO
, &arg
);
596 _GSocket_Enable_Events(connection
);
601 int GSocket_SetReusable(GSocket
*socket
)
603 /* socket must not be null, and must not be in use/already bound */
604 if (NULL
!= socket
&& socket
->m_fd
== INVALID_SOCKET
) {
605 socket
->m_reusable
= TRUE
;
611 /* Client specific parts */
614 * For stream (connection oriented) sockets, GSocket_Connect() tries
615 * to establish a client connection to a server using the peer address
616 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
617 * connection has been succesfully established, or one of the error
618 * codes listed below. Note that for nonblocking sockets, a return
619 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
620 * request can be completed later; you should use GSocket_Select()
621 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
622 * corresponding asynchronous events.
624 * For datagram (non connection oriented) sockets, GSocket_Connect()
625 * just sets the peer address established with GSocket_SetPeer() as
626 * default destination.
629 * GSOCK_INVSOCK - the socket is in use or not valid.
630 * GSOCK_INVADDR - the peer address has not been established.
631 * GSOCK_TIMEDOUT - timeout, the connection failed.
632 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
633 * GSOCK_MEMERR - couldn't allocate memory.
634 * GSOCK_IOERR - low-level error.
636 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
643 /* Enable CONNECTION events (needed for nonblocking connections) */
644 _GSocket_Enable(sck
, GSOCK_CONNECTION
);
646 if (sck
->m_fd
!= INVALID_SOCKET
)
648 sck
->m_error
= GSOCK_INVSOCK
;
649 return GSOCK_INVSOCK
;
654 sck
->m_error
= GSOCK_INVADDR
;
655 return GSOCK_INVADDR
;
658 /* Streamed or dgram socket? */
659 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
660 sck
->m_server
= FALSE
;
661 sck
->m_establishing
= FALSE
;
663 /* Create the socket */
664 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
,
665 sck
->m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
667 if (sck
->m_fd
== INVALID_SOCKET
)
669 sck
->m_error
= GSOCK_IOERR
;
672 #if defined(__EMX__) || defined(__VISAGECPP__)
673 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
675 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
677 _GSocket_Enable_Events(sck
);
679 /* Connect it to the peer address, with a timeout (see below) */
680 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
686 /* If connect failed with EINPROGRESS and the GSocket object
687 * is in blocking mode, we select() for the specified timeout
688 * checking for writability to see if the connection request
691 if ((err
== EINPROGRESS
) && (!sck
->m_non_blocking
))
693 if (_GSocket_Output_Timeout(sck
) == GSOCK_TIMEDOUT
)
696 /* sck->m_error is set in _GSocket_Output_Timeout */
697 return GSOCK_TIMEDOUT
;
702 SOCKLEN_T len
= sizeof(error
);
704 getsockopt(sck
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*) &error
, &len
);
707 return GSOCK_NOERROR
;
711 /* If connect failed with EINPROGRESS and the GSocket object
712 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
713 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
714 * this way if the connection completes, a GSOCK_CONNECTION
715 * event will be generated, if enabled.
717 if ((err
== EINPROGRESS
) && (sck
->m_non_blocking
))
719 sck
->m_establishing
= TRUE
;
720 sck
->m_error
= GSOCK_WOULDBLOCK
;
721 return GSOCK_WOULDBLOCK
;
724 /* If connect failed with an error other than EINPROGRESS,
725 * then the call to GSocket_Connect has failed.
728 sck
->m_error
= GSOCK_IOERR
;
732 return GSOCK_NOERROR
;
735 /* Datagram sockets */
737 /* GSocket_SetNonOriented:
738 * Sets up this socket as a non-connection oriented (datagram) socket.
739 * Before using this function, the local address must have been set
740 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
741 * on success, or one of the following otherwise.
744 * GSOCK_INVSOCK - the socket is in use.
745 * GSOCK_INVADDR - the local address has not been set.
746 * GSOCK_IOERR - low-level error.
748 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
754 if (sck
->m_fd
!= INVALID_SOCKET
)
756 sck
->m_error
= GSOCK_INVSOCK
;
757 return GSOCK_INVSOCK
;
762 sck
->m_error
= GSOCK_INVADDR
;
763 return GSOCK_INVADDR
;
766 /* Initialize all fields */
767 sck
->m_stream
= FALSE
;
768 sck
->m_server
= FALSE
;
770 /* Create the socket */
771 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
773 if (sck
->m_fd
== INVALID_SOCKET
)
775 sck
->m_error
= GSOCK_IOERR
;
778 #if defined(__EMX__) || defined(__VISAGECPP__)
779 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
781 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
783 _GSocket_Enable_Events(sck
);
785 /* Bind to the local address,
786 * and retrieve the actual address bound.
788 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
789 (getsockname(sck
->m_fd
,
790 sck
->m_local
->m_addr
,
791 (SOCKLEN_T
*) &sck
->m_local
->m_len
) != 0))
794 sck
->m_error
= GSOCK_IOERR
;
798 return GSOCK_NOERROR
;
803 /* Like recv(), send(), ... */
804 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
808 assert(socket
!= NULL
);
810 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
812 socket
->m_error
= GSOCK_INVSOCK
;
816 /* Disable events during query of socket status */
817 _GSocket_Disable(socket
, GSOCK_INPUT
);
819 /* If the socket is blocking, wait for data (with a timeout) */
820 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
821 /* We no longer return here immediately, otherwise socket events would not be re-enabled! */
825 if (socket
->m_stream
)
826 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
828 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
833 if (errno
== EWOULDBLOCK
)
834 socket
->m_error
= GSOCK_WOULDBLOCK
;
836 socket
->m_error
= GSOCK_IOERR
;
839 /* Enable events again now that we are done processing */
840 _GSocket_Enable(socket
, GSOCK_INPUT
);
845 int GSocket_Write(GSocket
*socket
, const char *buffer
, int size
)
849 assert(socket
!= NULL
);
851 GSocket_Debug(( "GSocket_Write #1, size %d\n", size
));
853 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
855 socket
->m_error
= GSOCK_INVSOCK
;
859 GSocket_Debug(( "GSocket_Write #2, size %d\n", size
));
861 /* If the socket is blocking, wait for writability (with a timeout) */
862 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
865 GSocket_Debug(( "GSocket_Write #3, size %d\n", size
));
868 if (socket
->m_stream
)
869 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
871 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
873 GSocket_Debug(( "GSocket_Write #4, size %d\n", size
));
877 if (errno
== EWOULDBLOCK
)
879 socket
->m_error
= GSOCK_WOULDBLOCK
;
880 GSocket_Debug(( "GSocket_Write error WOULDBLOCK\n" ));
884 socket
->m_error
= GSOCK_IOERR
;
885 GSocket_Debug(( "GSocket_Write error IOERR\n" ));
888 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
889 * in MSW). Once the first OUTPUT event is received, users can assume
890 * that the socket is writable until a read operation fails. Only then
891 * will further OUTPUT events be posted.
893 _GSocket_Enable(socket
, GSOCK_OUTPUT
);
897 GSocket_Debug(( "GSocket_Write #5, size %d ret %d\n", size
, ret
));
903 * Polls the socket to determine its status. This function will
904 * check for the events specified in the 'flags' parameter, and
905 * it will return a mask indicating which operations can be
906 * performed. This function won't block, regardless of the
907 * mode (blocking | nonblocking) of the socket.
909 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
914 GSocketEventFlags result
= 0;
920 assert(socket
!= NULL
);
922 /* Do not use a static struct, Linux can garble it */
923 tv
.tv_sec
= socket
->m_timeout
/ 1000;
924 tv
.tv_usec
= (socket
->m_timeout
% 1000) / 1000;
929 FD_SET(socket
->m_fd
, &readfds
);
930 if (flags
& GSOCK_OUTPUT_FLAG
|| flags
& GSOCK_CONNECTION_FLAG
)
931 FD_SET(socket
->m_fd
, &writefds
);
932 FD_SET(socket
->m_fd
, &exceptfds
);
934 /* Check 'sticky' CONNECTION flag first */
935 result
|= (GSOCK_CONNECTION_FLAG
& socket
->m_detected
);
937 /* If we have already detected a LOST event, then don't try
938 * to do any further processing.
940 if ((socket
->m_detected
& GSOCK_LOST_FLAG
) != 0)
942 socket
->m_establishing
= FALSE
;
944 return (GSOCK_LOST_FLAG
& flags
);
948 if (select(socket
->m_fd
+ 1, &readfds
, &writefds
, &exceptfds
, &tv
) <= 0)
950 /* What to do here? */
951 return (result
& flags
);
954 /* Check for readability */
955 if (FD_ISSET(socket
->m_fd
, &readfds
))
959 if (recv(socket
->m_fd
, &c
, 1, MSG_PEEK
) > 0)
961 result
|= GSOCK_INPUT_FLAG
;
965 if (socket
->m_server
&& socket
->m_stream
)
967 result
|= GSOCK_CONNECTION_FLAG
;
968 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
972 socket
->m_detected
= GSOCK_LOST_FLAG
;
973 socket
->m_establishing
= FALSE
;
975 /* LOST event: Abort any further processing */
976 return (GSOCK_LOST_FLAG
& flags
);
981 /* Check for writability */
982 if (FD_ISSET(socket
->m_fd
, &writefds
))
984 if (socket
->m_establishing
&& !socket
->m_server
)
987 SOCKLEN_T len
= sizeof(error
);
989 socket
->m_establishing
= FALSE
;
991 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*)&error
, &len
);
995 socket
->m_detected
= GSOCK_LOST_FLAG
;
997 /* LOST event: Abort any further processing */
998 return (GSOCK_LOST_FLAG
& flags
);
1002 result
|= GSOCK_CONNECTION_FLAG
;
1003 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
1008 result
|= GSOCK_OUTPUT_FLAG
;
1012 /* Check for exceptions and errors (is this useful in Unices?) */
1013 if (FD_ISSET(socket
->m_fd
, &exceptfds
))
1015 socket
->m_establishing
= FALSE
;
1016 socket
->m_detected
= GSOCK_LOST_FLAG
;
1018 /* LOST event: Abort any further processing */
1019 return (GSOCK_LOST_FLAG
& flags
);
1022 return (result
& flags
);
1028 assert(socket
!= NULL
);
1029 return flags
& socket
->m_detected
;
1036 /* GSocket_SetNonBlocking:
1037 * Sets the socket to non-blocking mode. All IO calls will return
1040 void GSocket_SetNonBlocking(GSocket
*socket
, int non_block
)
1042 assert(socket
!= NULL
);
1044 GSocket_Debug( ("GSocket_SetNonBlocking: %d\n", (int)non_block
) );
1046 socket
->m_non_blocking
= non_block
;
1049 /* GSocket_SetTimeout:
1050 * Sets the timeout for blocking calls. Time is expressed in
1053 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
)
1055 assert(socket
!= NULL
);
1057 socket
->m_timeout
= millisec
;
1060 /* GSocket_GetError:
1061 * Returns the last error occured for this socket. Note that successful
1062 * operations do not clear this back to GSOCK_NOERROR, so use it only
1065 GSocketError
GSocket_GetError(GSocket
*socket
)
1067 assert(socket
!= NULL
);
1069 return socket
->m_error
;
1075 * There is data to be read in the input buffer. If, after a read
1076 * operation, there is still data available, the callback function will
1079 * The socket is available for writing. That is, the next write call
1080 * won't block. This event is generated only once, when the connection is
1081 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
1082 * when the output buffer empties again. This means that the app should
1083 * assume that it can write since the first OUTPUT event, and no more
1084 * OUTPUT events will be generated unless an error occurs.
1086 * Connection succesfully established, for client sockets, or incoming
1087 * client connection, for server sockets. Wait for this event (also watch
1088 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
1090 * The connection is lost (or a connection request failed); this could
1091 * be due to a failure, or due to the peer closing it gracefully.
1094 /* GSocket_SetCallback:
1095 * Enables the callbacks specified by 'flags'. Note that 'flags'
1096 * may be a combination of flags OR'ed toghether, so the same
1097 * callback function can be made to accept different events.
1098 * The callback function must have the following prototype:
1100 * void function(GSocket *socket, GSocketEvent event, char *cdata)
1102 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
1103 GSocketCallback callback
, char *cdata
)
1107 assert(socket
!= NULL
);
1109 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
1111 if ((flags
& (1 << count
)) != 0)
1113 socket
->m_cbacks
[count
] = callback
;
1114 socket
->m_data
[count
] = cdata
;
1119 /* GSocket_UnsetCallback:
1120 * Disables all callbacks specified by 'flags', which may be a
1121 * combination of flags OR'ed toghether.
1123 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
1127 assert(socket
!= NULL
);
1129 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
1131 if ((flags
& (1 << count
)) != 0)
1133 socket
->m_cbacks
[count
] = NULL
;
1134 socket
->m_data
[count
] = NULL
;
1139 GSocketError
GSocket_GetSockOpt(GSocket
*socket
, int level
, int optname
,
1140 void *optval
, int *optlen
)
1142 if (getsockopt(socket
->m_fd
, level
, optname
, optval
, optlen
) == 0)
1144 return GSOCK_NOERROR
;
1146 return GSOCK_OPTERR
;
1149 GSocketError
GSocket_SetSockOpt(GSocket
*socket
, int level
, int optname
,
1150 const void *optval
, int optlen
)
1152 if (setsockopt(socket
->m_fd
, level
, optname
, optval
, optlen
) == 0)
1154 return GSOCK_NOERROR
;
1156 return GSOCK_OPTERR
;
1159 #define CALL_CALLBACK(socket, event) { \
1160 _GSocket_Disable(socket, event); \
1161 if (socket->m_cbacks[event]) \
1162 socket->m_cbacks[event](socket, event, socket->m_data[event]); \
1166 void _GSocket_Enable(GSocket
*socket
, GSocketEvent event
)
1168 socket
->m_detected
&= ~(1 << event
);
1169 _GSocket_Install_Callback(socket
, event
);
1172 void _GSocket_Disable(GSocket
*socket
, GSocketEvent event
)
1174 socket
->m_detected
|= (1 << event
);
1175 _GSocket_Uninstall_Callback(socket
, event
);
1178 /* _GSocket_Input_Timeout:
1179 * For blocking sockets, wait until data is available or
1180 * until timeout ellapses.
1182 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
1188 /* Linux select() will overwrite the struct on return */
1189 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
1190 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
1192 if (!socket
->m_non_blocking
)
1195 FD_SET(socket
->m_fd
, &readfds
);
1196 ret
= select(socket
->m_fd
+ 1, &readfds
, NULL
, NULL
, &tv
);
1199 GSocket_Debug(( "GSocket_Input_Timeout, select returned 0\n" ));
1200 socket
->m_error
= GSOCK_TIMEDOUT
;
1201 return GSOCK_TIMEDOUT
;
1205 GSocket_Debug(( "GSocket_Input_Timeout, select returned -1\n" ));
1206 if (errno
== EBADF
) { GSocket_Debug(( "Invalid file descriptor\n" )); }
1207 if (errno
== EINTR
) { GSocket_Debug(( "A non blocked signal was caught\n" )); }
1208 if (errno
== EINVAL
) { GSocket_Debug(( "The highest number descriptor is negative\n" )); }
1209 if (errno
== ENOMEM
) { GSocket_Debug(( "Not enough memory\n" )); }
1210 socket
->m_error
= GSOCK_TIMEDOUT
;
1211 return GSOCK_TIMEDOUT
;
1214 return GSOCK_NOERROR
;
1217 /* _GSocket_Output_Timeout:
1218 * For blocking sockets, wait until data can be sent without
1219 * blocking or until timeout ellapses.
1221 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
1227 /* Linux select() will overwrite the struct on return */
1228 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
1229 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
1231 GSocket_Debug( ("m_non_blocking has: %d\n", (int)socket
->m_non_blocking
) );
1233 if (!socket
->m_non_blocking
)
1236 FD_SET(socket
->m_fd
, &writefds
);
1237 ret
= select(socket
->m_fd
+ 1, NULL
, &writefds
, NULL
, &tv
);
1240 GSocket_Debug(( "GSocket_Output_Timeout, select returned 0\n" ));
1241 socket
->m_error
= GSOCK_TIMEDOUT
;
1242 return GSOCK_TIMEDOUT
;
1246 GSocket_Debug(( "GSocket_Output_Timeout, select returned -1\n" ));
1247 if (errno
== EBADF
) { GSocket_Debug(( "Invalid file descriptor\n" )); }
1248 if (errno
== EINTR
) { GSocket_Debug(( "A non blocked signal was caught\n" )); }
1249 if (errno
== EINVAL
) { GSocket_Debug(( "The highest number descriptor is negative\n" )); }
1250 if (errno
== ENOMEM
) { GSocket_Debug(( "Not enough memory\n" )); }
1251 socket
->m_error
= GSOCK_TIMEDOUT
;
1252 return GSOCK_TIMEDOUT
;
1254 if ( ! FD_ISSET(socket
->m_fd
, &writefds
) ) {
1255 GSocket_Debug(( "GSocket_Output_Timeout is buggy!\n" ));
1258 GSocket_Debug(( "GSocket_Output_Timeout seems correct\n" ));
1263 GSocket_Debug(( "GSocket_Output_Timeout, didn't try select!\n" ));
1266 return GSOCK_NOERROR
;
1269 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
1271 return recv(socket
->m_fd
, buffer
, size
, 0);
1274 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
1276 struct sockaddr from
;
1277 SOCKLEN_T fromlen
= sizeof(from
);
1281 fromlen
= sizeof(from
);
1283 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, (SOCKLEN_T
*) &fromlen
);
1288 /* Translate a system address into a GSocket address */
1289 if (!socket
->m_peer
)
1291 socket
->m_peer
= GAddress_new();
1292 if (!socket
->m_peer
)
1294 socket
->m_error
= GSOCK_MEMERR
;
1298 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
1299 if (err
!= GSOCK_NOERROR
)
1301 GAddress_destroy(socket
->m_peer
);
1302 socket
->m_peer
= NULL
;
1303 socket
->m_error
= err
;
1310 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
1314 #ifndef __VISAGECPP__
1316 ret
= send(socket
->m_fd
, buffer
, size
, 0);
1319 ret
= send(socket
->m_fd
, (char *)buffer
, size
, 0);
1325 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
1327 struct sockaddr
*addr
;
1331 if (!socket
->m_peer
)
1333 socket
->m_error
= GSOCK_INVADDR
;
1337 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
1338 if (err
!= GSOCK_NOERROR
)
1340 socket
->m_error
= err
;
1344 #ifndef __VISAGECPP__
1346 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
1349 ret
= sendto(socket
->m_fd
, (char *)buffer
, size
, 0, addr
, len
);
1352 /* Frees memory allocated from _GAddress_translate_to */
1358 void _GSocket_Detected_Read(GSocket
*socket
)
1362 /* If we have already detected a LOST event, then don't try
1363 * to do any further processing.
1365 if ((socket
->m_detected
& GSOCK_LOST_FLAG
) != 0)
1367 socket
->m_establishing
= FALSE
;
1369 CALL_CALLBACK(socket
, GSOCK_LOST
);
1370 GSocket_Shutdown(socket
);
1374 if (recv(socket
->m_fd
, &c
, 1, MSG_PEEK
) > 0)
1376 CALL_CALLBACK(socket
, GSOCK_INPUT
);
1380 if (socket
->m_server
&& socket
->m_stream
)
1382 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
1386 CALL_CALLBACK(socket
, GSOCK_LOST
);
1387 GSocket_Shutdown(socket
);
1392 void _GSocket_Detected_Write(GSocket
*socket
)
1394 /* If we have already detected a LOST event, then don't try
1395 * to do any further processing.
1397 if ((socket
->m_detected
& GSOCK_LOST_FLAG
) != 0)
1399 socket
->m_establishing
= FALSE
;
1401 CALL_CALLBACK(socket
, GSOCK_LOST
);
1402 GSocket_Shutdown(socket
);
1406 if (socket
->m_establishing
&& !socket
->m_server
)
1409 SOCKLEN_T len
= sizeof(error
);
1411 socket
->m_establishing
= FALSE
;
1413 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*)&error
, &len
);
1417 CALL_CALLBACK(socket
, GSOCK_LOST
);
1418 GSocket_Shutdown(socket
);
1422 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
1423 /* We have to fire this event by hand because CONNECTION (for clients)
1424 * and OUTPUT are internally the same and we just disabled CONNECTION
1425 * events with the above macro.
1427 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
1432 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
1437 * -------------------------------------------------------------------------
1439 * -------------------------------------------------------------------------
1442 /* CHECK_ADDRESS verifies that the current address family is either
1443 * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it
1444 * initalizes it to be a GSOCK_*family*. In other cases, it returns
1445 * an appropiate error code.
1447 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
1449 #define CHECK_ADDRESS(address, family) \
1451 if (address->m_family == GSOCK_NOFAMILY) \
1452 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1453 return address->m_error; \
1454 if (address->m_family != GSOCK_##family) \
1456 address->m_error = GSOCK_INVADDR; \
1457 return GSOCK_INVADDR; \
1461 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
1463 if (address->m_family == GSOCK_NOFAMILY) \
1464 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1466 if (address->m_family != GSOCK_##family) \
1468 address->m_error = GSOCK_INVADDR; \
1474 GAddress
*GAddress_new(void)
1478 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1481 address
->m_family
= GSOCK_NOFAMILY
;
1482 address
->m_addr
= NULL
;
1488 GAddress
*GAddress_copy(GAddress
*address
)
1492 assert(address
!= NULL
);
1494 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1497 memcpy(addr2
, address
, sizeof(GAddress
));
1499 if (address
->m_addr
&& address
->m_len
> 0)
1501 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
1502 if (addr2
->m_addr
== NULL
)
1507 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1513 void GAddress_destroy(GAddress
*address
)
1515 assert(address
!= NULL
);
1517 if (address
->m_addr
)
1518 free(address
->m_addr
);
1523 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1525 assert(address
!= NULL
);
1527 address
->m_family
= type
;
1530 GAddressType
GAddress_GetFamily(GAddress
*address
)
1532 assert(address
!= NULL
);
1534 return address
->m_family
;
1537 GSocketError
_GAddress_translate_from(GAddress
*address
,
1538 struct sockaddr
*addr
, int len
)
1540 address
->m_realfamily
= addr
->sa_family
;
1541 switch (addr
->sa_family
)
1544 address
->m_family
= GSOCK_INET
;
1547 address
->m_family
= GSOCK_UNIX
;
1551 address
->m_family
= GSOCK_INET6
;
1556 address
->m_error
= GSOCK_INVOP
;
1561 if (address
->m_addr
)
1562 free(address
->m_addr
);
1564 address
->m_len
= len
;
1565 address
->m_addr
= (struct sockaddr
*)malloc(len
);
1567 if (address
->m_addr
== NULL
)
1569 address
->m_error
= GSOCK_MEMERR
;
1570 return GSOCK_MEMERR
;
1572 memcpy(address
->m_addr
, addr
, len
);
1574 return GSOCK_NOERROR
;
1577 GSocketError
_GAddress_translate_to(GAddress
*address
,
1578 struct sockaddr
**addr
, int *len
)
1580 if (!address
->m_addr
)
1582 address
->m_error
= GSOCK_INVADDR
;
1583 return GSOCK_INVADDR
;
1586 *len
= address
->m_len
;
1587 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
1590 address
->m_error
= GSOCK_MEMERR
;
1591 return GSOCK_MEMERR
;
1594 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1595 return GSOCK_NOERROR
;
1599 * -------------------------------------------------------------------------
1600 * Internet address family
1601 * -------------------------------------------------------------------------
1604 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1606 address
->m_len
= sizeof(struct sockaddr_in
);
1607 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1608 if (address
->m_addr
== NULL
)
1610 address
->m_error
= GSOCK_MEMERR
;
1611 return GSOCK_MEMERR
;
1614 address
->m_family
= GSOCK_INET
;
1615 address
->m_realfamily
= PF_INET
;
1616 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1617 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1619 return GSOCK_NOERROR
;
1622 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1625 struct in_addr
*addr
;
1627 assert(address
!= NULL
);
1629 CHECK_ADDRESS(address
, INET
);
1631 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1633 /* If it is a numeric host name, convert it now */
1634 #if defined(HAVE_INET_ATON)
1635 if (inet_aton(hostname
, addr
) == 0)
1637 #elif defined(HAVE_INET_ADDR)
1638 if ( (addr
->s_addr
= inet_addr(hostname
)) == -1 )
1641 /* Use gethostbyname by default */
1643 int val
= 1; /* VA doesn't like constants in conditional expressions */
1648 struct in_addr
*array_addr
;
1650 /* It is a real name, we solve it */
1651 if ((he
= gethostbyname(hostname
)) == NULL
)
1653 /* Reset to invalid address */
1654 addr
->s_addr
= INADDR_NONE
;
1655 address
->m_error
= GSOCK_NOHOST
;
1656 return GSOCK_NOHOST
;
1658 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1659 addr
->s_addr
= array_addr
[0].s_addr
;
1661 return GSOCK_NOERROR
;
1664 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1666 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1669 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1670 unsigned long hostaddr
)
1672 struct in_addr
*addr
;
1674 assert(address
!= NULL
);
1676 CHECK_ADDRESS(address
, INET
);
1678 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1679 addr
->s_addr
= htonl(hostaddr
);
1681 return GSOCK_NOERROR
;
1684 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1685 const char *protocol
)
1688 struct sockaddr_in
*addr
;
1690 assert(address
!= NULL
);
1691 CHECK_ADDRESS(address
, INET
);
1695 address
->m_error
= GSOCK_INVPORT
;
1696 return GSOCK_INVPORT
;
1699 se
= getservbyname(port
, protocol
);
1702 /* the cast to int suppresses compiler warnings about subscript having the
1704 if (isdigit((int)port
[0]))
1708 port_int
= atoi(port
);
1709 addr
= (struct sockaddr_in
*)address
->m_addr
;
1710 addr
->sin_port
= htons(port_int
);
1711 return GSOCK_NOERROR
;
1714 address
->m_error
= GSOCK_INVPORT
;
1715 return GSOCK_INVPORT
;
1718 addr
= (struct sockaddr_in
*)address
->m_addr
;
1719 addr
->sin_port
= se
->s_port
;
1721 return GSOCK_NOERROR
;
1724 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1726 struct sockaddr_in
*addr
;
1728 assert(address
!= NULL
);
1729 CHECK_ADDRESS(address
, INET
);
1731 addr
= (struct sockaddr_in
*)address
->m_addr
;
1732 addr
->sin_port
= htons(port
);
1734 return GSOCK_NOERROR
;
1737 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1741 struct sockaddr_in
*addr
;
1743 assert(address
!= NULL
);
1744 CHECK_ADDRESS(address
, INET
);
1746 addr
= (struct sockaddr_in
*)address
->m_addr
;
1747 addr_buf
= (char *)&(addr
->sin_addr
);
1749 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1752 address
->m_error
= GSOCK_NOHOST
;
1753 return GSOCK_NOHOST
;
1756 strncpy(hostname
, he
->h_name
, sbuf
);
1758 return GSOCK_NOERROR
;
1761 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1763 struct sockaddr_in
*addr
;
1765 assert(address
!= NULL
);
1766 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1768 addr
= (struct sockaddr_in
*)address
->m_addr
;
1770 return ntohl(addr
->sin_addr
.s_addr
);
1773 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1775 struct sockaddr_in
*addr
;
1777 assert(address
!= NULL
);
1778 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1780 addr
= (struct sockaddr_in
*)address
->m_addr
;
1781 return ntohs(addr
->sin_port
);
1785 * -------------------------------------------------------------------------
1786 * Unix address family
1787 * -------------------------------------------------------------------------
1790 #ifndef __VISAGECPP__
1791 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1793 address
->m_len
= sizeof(struct sockaddr_un
);
1794 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
1795 if (address
->m_addr
== NULL
)
1797 address
->m_error
= GSOCK_MEMERR
;
1798 return GSOCK_MEMERR
;
1801 address
->m_family
= GSOCK_UNIX
;
1802 address
->m_realfamily
= PF_UNIX
;
1803 ((struct sockaddr_un
*)address
->m_addr
)->sun_family
= AF_UNIX
;
1804 ((struct sockaddr_un
*)address
->m_addr
)->sun_path
[0] = 0;
1806 return GSOCK_NOERROR
;
1809 #define UNIX_SOCK_PATHLEN (sizeof(addr->sun_path)/sizeof(addr->sun_path[0]))
1811 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1813 struct sockaddr_un
*addr
;
1815 assert(address
!= NULL
);
1817 CHECK_ADDRESS(address
, UNIX
);
1819 addr
= ((struct sockaddr_un
*)address
->m_addr
);
1820 strncpy(addr
->sun_path
, path
, UNIX_SOCK_PATHLEN
);
1821 addr
->sun_path
[UNIX_SOCK_PATHLEN
- 1] = '\0';
1823 return GSOCK_NOERROR
;
1826 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1828 struct sockaddr_un
*addr
;
1830 assert(address
!= NULL
);
1831 CHECK_ADDRESS(address
, UNIX
);
1833 addr
= (struct sockaddr_un
*)address
->m_addr
;
1835 strncpy(path
, addr
->sun_path
, sbuf
);
1837 return GSOCK_NOERROR
;
1839 #endif /* !defined(__VISAGECPP__) */
1840 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */