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 wxWindows 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
);
265 socket
->m_fd
= INVALID_SOCKET
;
268 void GSocket_destroy(GSocket
*socket
)
270 assert(socket
!= NULL
);
272 /* When using CFSocket we MUST invalidate before closing the fd */
274 /* Per-socket GUI-specific cleanup */
275 _GSocket_GUI_Destroy_Socket(socket
);
278 /* Check that the socket is really shutdowned */
279 if (socket
->m_fd
!= INVALID_SOCKET
)
280 GSocket_Shutdown(socket
);
283 /* Per-socket GUI-specific cleanup */
284 _GSocket_GUI_Destroy_Socket(socket
);
287 /* Destroy private addresses */
289 GAddress_destroy(socket
->m_local
);
292 GAddress_destroy(socket
->m_peer
);
294 /* Destroy the socket itself */
299 * Disallow further read/write operations on this socket, close
300 * the fd and disable all callbacks.
302 void GSocket_Shutdown(GSocket
*socket
)
306 assert(socket
!= NULL
);
308 /* If socket has been created, shutdown it */
309 if (socket
->m_fd
!= INVALID_SOCKET
)
311 shutdown(socket
->m_fd
, 2);
312 GSocket_close(socket
);
315 /* Disable GUI callbacks */
316 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
317 socket
->m_cbacks
[evt
] = NULL
;
319 socket
->m_detected
= GSOCK_LOST_FLAG
;
322 /* Address handling */
328 * Set or get the local or peer address for this socket. The 'set'
329 * functions return GSOCK_NOERROR on success, an error code otherwise.
330 * The 'get' functions return a pointer to a GAddress object on success,
331 * or NULL otherwise, in which case they set the error code of the
332 * corresponding GSocket.
335 * GSOCK_INVSOCK - the socket is not valid.
336 * GSOCK_INVADDR - the address is not valid.
338 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
340 assert(socket
!= NULL
);
342 /* the socket must be initialized, or it must be a server */
343 if ((socket
->m_fd
!= INVALID_SOCKET
&& !socket
->m_server
))
345 socket
->m_error
= GSOCK_INVSOCK
;
346 return GSOCK_INVSOCK
;
350 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
352 socket
->m_error
= GSOCK_INVADDR
;
353 return GSOCK_INVADDR
;
357 GAddress_destroy(socket
->m_local
);
359 socket
->m_local
= GAddress_copy(address
);
361 return GSOCK_NOERROR
;
364 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
366 assert(socket
!= NULL
);
369 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
371 socket
->m_error
= GSOCK_INVADDR
;
372 return GSOCK_INVADDR
;
376 GAddress_destroy(socket
->m_peer
);
378 socket
->m_peer
= GAddress_copy(address
);
380 return GSOCK_NOERROR
;
383 GAddress
*GSocket_GetLocal(GSocket
*socket
)
386 struct sockaddr addr
;
387 SOCKLEN_T size
= sizeof(addr
);
390 assert(socket
!= NULL
);
392 /* try to get it from the m_local var first */
394 return GAddress_copy(socket
->m_local
);
396 /* else, if the socket is initialized, try getsockname */
397 if (socket
->m_fd
== INVALID_SOCKET
)
399 socket
->m_error
= GSOCK_INVSOCK
;
403 if (getsockname(socket
->m_fd
, &addr
, (SOCKLEN_T
*) &size
) < 0)
405 socket
->m_error
= GSOCK_IOERR
;
409 /* got a valid address from getsockname, create a GAddress object */
410 address
= GAddress_new();
413 socket
->m_error
= GSOCK_MEMERR
;
417 err
= _GAddress_translate_from(address
, &addr
, size
);
418 if (err
!= GSOCK_NOERROR
)
420 GAddress_destroy(address
);
421 socket
->m_error
= err
;
428 GAddress
*GSocket_GetPeer(GSocket
*socket
)
430 assert(socket
!= NULL
);
432 /* try to get it from the m_peer var */
434 return GAddress_copy(socket
->m_peer
);
439 /* Server specific parts */
441 /* GSocket_SetServer:
442 * Sets up this socket as a server. The local address must have been
443 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
444 * Returns GSOCK_NOERROR on success, one of the following otherwise:
447 * GSOCK_INVSOCK - the socket is in use.
448 * GSOCK_INVADDR - the local address has not been set.
449 * GSOCK_IOERR - low-level error.
451 GSocketError
GSocket_SetServer(GSocket
*sck
)
457 /* must not be in use */
458 if (sck
->m_fd
!= INVALID_SOCKET
)
460 sck
->m_error
= GSOCK_INVSOCK
;
461 return GSOCK_INVSOCK
;
464 /* the local addr must have been set */
467 sck
->m_error
= GSOCK_INVADDR
;
468 return GSOCK_INVADDR
;
471 /* Initialize all fields */
472 sck
->m_stream
= TRUE
;
473 sck
->m_server
= TRUE
;
474 sck
->m_oriented
= TRUE
;
476 /* Create the socket */
477 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
479 if (sck
->m_fd
== INVALID_SOCKET
)
481 sck
->m_error
= GSOCK_IOERR
;
484 #if defined(__EMX__) || defined(__VISAGECPP__)
485 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
487 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
489 _GSocket_Enable_Events(sck
);
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
;
574 connection
->m_oriented
= TRUE
;
576 /* Setup the peer address field */
577 connection
->m_peer
= GAddress_new();
578 if (!connection
->m_peer
)
580 GSocket_destroy(connection
);
581 socket
->m_error
= GSOCK_MEMERR
;
584 err
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
);
585 if (err
!= GSOCK_NOERROR
)
587 GAddress_destroy(connection
->m_peer
);
588 GSocket_destroy(connection
);
589 socket
->m_error
= err
;
592 #if defined(__EMX__) || defined(__VISAGECPP__)
593 ioctl(connection
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
595 ioctl(connection
->m_fd
, FIONBIO
, &arg
);
597 _GSocket_Enable_Events(connection
);
602 /* Client specific parts */
605 * For stream (connection oriented) sockets, GSocket_Connect() tries
606 * to establish a client connection to a server using the peer address
607 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
608 * connection has been succesfully established, or one of the error
609 * codes listed below. Note that for nonblocking sockets, a return
610 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
611 * request can be completed later; you should use GSocket_Select()
612 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
613 * corresponding asynchronous events.
615 * For datagram (non connection oriented) sockets, GSocket_Connect()
616 * just sets the peer address established with GSocket_SetPeer() as
617 * default destination.
620 * GSOCK_INVSOCK - the socket is in use or not valid.
621 * GSOCK_INVADDR - the peer address has not been established.
622 * GSOCK_TIMEDOUT - timeout, the connection failed.
623 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
624 * GSOCK_MEMERR - couldn't allocate memory.
625 * GSOCK_IOERR - low-level error.
627 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
634 /* Enable CONNECTION events (needed for nonblocking connections) */
635 _GSocket_Enable(sck
, GSOCK_CONNECTION
);
637 if (sck
->m_fd
!= INVALID_SOCKET
)
639 sck
->m_error
= GSOCK_INVSOCK
;
640 return GSOCK_INVSOCK
;
645 sck
->m_error
= GSOCK_INVADDR
;
646 return GSOCK_INVADDR
;
649 /* Streamed or dgram socket? */
650 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
651 sck
->m_oriented
= TRUE
;
652 sck
->m_server
= FALSE
;
653 sck
->m_establishing
= FALSE
;
655 /* Create the socket */
656 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
,
657 sck
->m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
659 if (sck
->m_fd
== INVALID_SOCKET
)
661 sck
->m_error
= GSOCK_IOERR
;
664 #if defined(__EMX__) || defined(__VISAGECPP__)
665 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
667 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
669 _GSocket_Enable_Events(sck
);
671 /* Connect it to the peer address, with a timeout (see below) */
672 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
678 /* If connect failed with EINPROGRESS and the GSocket object
679 * is in blocking mode, we select() for the specified timeout
680 * checking for writability to see if the connection request
683 if ((err
== EINPROGRESS
) && (!sck
->m_non_blocking
))
685 if (_GSocket_Output_Timeout(sck
) == GSOCK_TIMEDOUT
)
688 /* sck->m_error is set in _GSocket_Output_Timeout */
689 return GSOCK_TIMEDOUT
;
694 SOCKLEN_T len
= sizeof(error
);
696 getsockopt(sck
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*) &error
, &len
);
699 return GSOCK_NOERROR
;
703 /* If connect failed with EINPROGRESS and the GSocket object
704 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
705 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
706 * this way if the connection completes, a GSOCK_CONNECTION
707 * event will be generated, if enabled.
709 if ((err
== EINPROGRESS
) && (sck
->m_non_blocking
))
711 sck
->m_establishing
= TRUE
;
712 sck
->m_error
= GSOCK_WOULDBLOCK
;
713 return GSOCK_WOULDBLOCK
;
716 /* If connect failed with an error other than EINPROGRESS,
717 * then the call to GSocket_Connect has failed.
720 sck
->m_error
= GSOCK_IOERR
;
724 return GSOCK_NOERROR
;
727 /* Datagram sockets */
729 /* GSocket_SetNonOriented:
730 * Sets up this socket as a non-connection oriented (datagram) socket.
731 * Before using this function, the local address must have been set
732 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
733 * on success, or one of the following otherwise.
736 * GSOCK_INVSOCK - the socket is in use.
737 * GSOCK_INVADDR - the local address has not been set.
738 * GSOCK_IOERR - low-level error.
740 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
746 if (sck
->m_fd
!= INVALID_SOCKET
)
748 sck
->m_error
= GSOCK_INVSOCK
;
749 return GSOCK_INVSOCK
;
754 sck
->m_error
= GSOCK_INVADDR
;
755 return GSOCK_INVADDR
;
758 /* Initialize all fields */
759 sck
->m_stream
= FALSE
;
760 sck
->m_server
= FALSE
;
761 sck
->m_oriented
= FALSE
;
763 /* Create the socket */
764 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
766 if (sck
->m_fd
== INVALID_SOCKET
)
768 sck
->m_error
= GSOCK_IOERR
;
771 #if defined(__EMX__) || defined(__VISAGECPP__)
772 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
774 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
776 _GSocket_Enable_Events(sck
);
778 /* Bind to the local address,
779 * and retrieve the actual address bound.
781 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
782 (getsockname(sck
->m_fd
,
783 sck
->m_local
->m_addr
,
784 (SOCKLEN_T
*) &sck
->m_local
->m_len
) != 0))
787 sck
->m_error
= GSOCK_IOERR
;
791 return GSOCK_NOERROR
;
796 /* Like recv(), send(), ... */
797 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
801 assert(socket
!= NULL
);
803 /* When using CFSocket we MUST NOT reenable events until we finish reading */
805 /* Reenable INPUT events */
806 _GSocket_Enable(socket
, GSOCK_INPUT
);
809 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
811 socket
->m_error
= GSOCK_INVSOCK
;
815 /* If the socket is blocking, wait for data (with a timeout) */
816 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
820 if (socket
->m_stream
)
821 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
823 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
827 if (errno
== EWOULDBLOCK
)
828 socket
->m_error
= GSOCK_WOULDBLOCK
;
830 socket
->m_error
= GSOCK_IOERR
;
834 /* Reenable INPUT events */
835 _GSocket_Enable(socket
, GSOCK_INPUT
);
841 int GSocket_Write(GSocket
*socket
, const char *buffer
, int size
)
845 assert(socket
!= NULL
);
847 GSocket_Debug(( "GSocket_Write #1, size %d\n", size
));
849 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
851 socket
->m_error
= GSOCK_INVSOCK
;
855 GSocket_Debug(( "GSocket_Write #2, size %d\n", size
));
857 /* If the socket is blocking, wait for writability (with a timeout) */
858 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
861 GSocket_Debug(( "GSocket_Write #3, size %d\n", size
));
864 if (socket
->m_stream
)
865 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
867 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
869 GSocket_Debug(( "GSocket_Write #4, size %d\n", size
));
873 if (errno
== EWOULDBLOCK
)
875 socket
->m_error
= GSOCK_WOULDBLOCK
;
876 GSocket_Debug(( "GSocket_Write error WOULDBLOCK\n" ));
880 socket
->m_error
= GSOCK_IOERR
;
881 GSocket_Debug(( "GSocket_Write error IOERR\n" ));
884 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
885 * in MSW). Once the first OUTPUT event is received, users can assume
886 * that the socket is writable until a read operation fails. Only then
887 * will further OUTPUT events be posted.
889 _GSocket_Enable(socket
, GSOCK_OUTPUT
);
893 GSocket_Debug(( "GSocket_Write #5, size %d ret %d\n", size
, ret
));
899 * Polls the socket to determine its status. This function will
900 * check for the events specified in the 'flags' parameter, and
901 * it will return a mask indicating which operations can be
902 * performed. This function won't block, regardless of the
903 * mode (blocking | nonblocking) of the socket.
905 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
910 GSocketEventFlags result
= 0;
916 /* Do not use a static struct, Linux can garble it */
920 assert(socket
!= NULL
);
925 FD_SET(socket
->m_fd
, &readfds
);
926 FD_SET(socket
->m_fd
, &writefds
);
927 FD_SET(socket
->m_fd
, &exceptfds
);
929 /* Check 'sticky' CONNECTION flag first */
930 result
|= (GSOCK_CONNECTION_FLAG
& socket
->m_detected
);
932 /* If we have already detected a LOST event, then don't try
933 * to do any further processing.
935 if ((socket
->m_detected
& GSOCK_LOST_FLAG
) != 0)
937 socket
->m_establishing
= FALSE
;
939 return (GSOCK_LOST_FLAG
& flags
);
943 if (select(socket
->m_fd
+ 1, &readfds
, &writefds
, &exceptfds
, &tv
) <= 0)
945 /* What to do here? */
946 return (result
& flags
);
949 /* Check for readability */
950 if (FD_ISSET(socket
->m_fd
, &readfds
))
954 if (recv(socket
->m_fd
, &c
, 1, MSG_PEEK
) > 0)
956 result
|= GSOCK_INPUT_FLAG
;
960 if (socket
->m_server
&& socket
->m_stream
)
962 result
|= GSOCK_CONNECTION_FLAG
;
963 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
967 socket
->m_detected
= GSOCK_LOST_FLAG
;
968 socket
->m_establishing
= FALSE
;
970 /* LOST event: Abort any further processing */
971 return (GSOCK_LOST_FLAG
& flags
);
976 /* Check for writability */
977 if (FD_ISSET(socket
->m_fd
, &writefds
))
979 if (socket
->m_establishing
&& !socket
->m_server
)
982 SOCKLEN_T len
= sizeof(error
);
984 socket
->m_establishing
= FALSE
;
986 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*)&error
, &len
);
990 socket
->m_detected
= GSOCK_LOST_FLAG
;
992 /* LOST event: Abort any further processing */
993 return (GSOCK_LOST_FLAG
& flags
);
997 result
|= GSOCK_CONNECTION_FLAG
;
998 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
1003 result
|= GSOCK_OUTPUT_FLAG
;
1007 /* Check for exceptions and errors (is this useful in Unices?) */
1008 if (FD_ISSET(socket
->m_fd
, &exceptfds
))
1010 socket
->m_establishing
= FALSE
;
1011 socket
->m_detected
= GSOCK_LOST_FLAG
;
1013 /* LOST event: Abort any further processing */
1014 return (GSOCK_LOST_FLAG
& flags
);
1017 return (result
& flags
);
1023 assert(socket
!= NULL
);
1024 return flags
& socket
->m_detected
;
1031 /* GSocket_SetNonBlocking:
1032 * Sets the socket to non-blocking mode. All IO calls will return
1035 void GSocket_SetNonBlocking(GSocket
*socket
, int non_block
)
1037 assert(socket
!= NULL
);
1039 GSocket_Debug( ("GSocket_SetNonBlocking: %d\n", (int)non_block
) );
1041 socket
->m_non_blocking
= non_block
;
1044 /* GSocket_SetTimeout:
1045 * Sets the timeout for blocking calls. Time is expressed in
1048 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
)
1050 assert(socket
!= NULL
);
1052 socket
->m_timeout
= millisec
;
1055 /* GSocket_GetError:
1056 * Returns the last error occured for this socket. Note that successful
1057 * operations do not clear this back to GSOCK_NOERROR, so use it only
1060 GSocketError
GSocket_GetError(GSocket
*socket
)
1062 assert(socket
!= NULL
);
1064 return socket
->m_error
;
1070 * There is data to be read in the input buffer. If, after a read
1071 * operation, there is still data available, the callback function will
1074 * The socket is available for writing. That is, the next write call
1075 * won't block. This event is generated only once, when the connection is
1076 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
1077 * when the output buffer empties again. This means that the app should
1078 * assume that it can write since the first OUTPUT event, and no more
1079 * OUTPUT events will be generated unless an error occurs.
1081 * Connection succesfully established, for client sockets, or incoming
1082 * client connection, for server sockets. Wait for this event (also watch
1083 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
1085 * The connection is lost (or a connection request failed); this could
1086 * be due to a failure, or due to the peer closing it gracefully.
1089 /* GSocket_SetCallback:
1090 * Enables the callbacks specified by 'flags'. Note that 'flags'
1091 * may be a combination of flags OR'ed toghether, so the same
1092 * callback function can be made to accept different events.
1093 * The callback function must have the following prototype:
1095 * void function(GSocket *socket, GSocketEvent event, char *cdata)
1097 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
1098 GSocketCallback callback
, char *cdata
)
1102 assert(socket
!= NULL
);
1104 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
1106 if ((flags
& (1 << count
)) != 0)
1108 socket
->m_cbacks
[count
] = callback
;
1109 socket
->m_data
[count
] = cdata
;
1114 /* GSocket_UnsetCallback:
1115 * Disables all callbacks specified by 'flags', which may be a
1116 * combination of flags OR'ed toghether.
1118 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
1122 assert(socket
!= NULL
);
1124 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
1126 if ((flags
& (1 << count
)) != 0)
1128 socket
->m_cbacks
[count
] = NULL
;
1129 socket
->m_data
[count
] = NULL
;
1135 #define CALL_CALLBACK(socket, event) { \
1136 _GSocket_Disable(socket, event); \
1137 if (socket->m_cbacks[event]) \
1138 socket->m_cbacks[event](socket, event, socket->m_data[event]); \
1142 void _GSocket_Enable(GSocket
*socket
, GSocketEvent event
)
1144 socket
->m_detected
&= ~(1 << event
);
1145 _GSocket_Install_Callback(socket
, event
);
1148 void _GSocket_Disable(GSocket
*socket
, GSocketEvent event
)
1150 socket
->m_detected
|= (1 << event
);
1151 _GSocket_Uninstall_Callback(socket
, event
);
1154 /* _GSocket_Input_Timeout:
1155 * For blocking sockets, wait until data is available or
1156 * until timeout ellapses.
1158 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
1164 /* Linux select() will overwrite the struct on return */
1165 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
1166 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
1168 if (!socket
->m_non_blocking
)
1171 FD_SET(socket
->m_fd
, &readfds
);
1172 ret
= select(socket
->m_fd
+ 1, &readfds
, NULL
, NULL
, &tv
);
1175 GSocket_Debug(( "GSocket_Input_Timeout, select returned 0\n" ));
1176 socket
->m_error
= GSOCK_TIMEDOUT
;
1177 return GSOCK_TIMEDOUT
;
1181 GSocket_Debug(( "GSocket_Input_Timeout, select returned -1\n" ));
1182 if (errno
== EBADF
) { GSocket_Debug(( "Invalid file descriptor\n" )); }
1183 if (errno
== EINTR
) { GSocket_Debug(( "A non blocked signal was caught\n" )); }
1184 if (errno
== EINVAL
) { GSocket_Debug(( "The highest number descriptor is negative\n" )); }
1185 if (errno
== ENOMEM
) { GSocket_Debug(( "Not enough memory\n" )); }
1186 socket
->m_error
= GSOCK_TIMEDOUT
;
1187 return GSOCK_TIMEDOUT
;
1190 return GSOCK_NOERROR
;
1193 /* _GSocket_Output_Timeout:
1194 * For blocking sockets, wait until data can be sent without
1195 * blocking or until timeout ellapses.
1197 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
1203 /* Linux select() will overwrite the struct on return */
1204 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
1205 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
1207 GSocket_Debug( ("m_non_blocking has: %d\n", (int)socket
->m_non_blocking
) );
1209 if (!socket
->m_non_blocking
)
1212 FD_SET(socket
->m_fd
, &writefds
);
1213 ret
= select(socket
->m_fd
+ 1, NULL
, &writefds
, NULL
, &tv
);
1216 GSocket_Debug(( "GSocket_Output_Timeout, select returned 0\n" ));
1217 socket
->m_error
= GSOCK_TIMEDOUT
;
1218 return GSOCK_TIMEDOUT
;
1222 GSocket_Debug(( "GSocket_Output_Timeout, select returned -1\n" ));
1223 if (errno
== EBADF
) { GSocket_Debug(( "Invalid file descriptor\n" )); }
1224 if (errno
== EINTR
) { GSocket_Debug(( "A non blocked signal was caught\n" )); }
1225 if (errno
== EINVAL
) { GSocket_Debug(( "The highest number descriptor is negative\n" )); }
1226 if (errno
== ENOMEM
) { GSocket_Debug(( "Not enough memory\n" )); }
1227 socket
->m_error
= GSOCK_TIMEDOUT
;
1228 return GSOCK_TIMEDOUT
;
1230 if ( ! FD_ISSET(socket
->m_fd
, &writefds
) ) {
1231 GSocket_Debug(( "GSocket_Output_Timeout is buggy!\n" ));
1234 GSocket_Debug(( "GSocket_Output_Timeout seems correct\n" ));
1239 GSocket_Debug(( "GSocket_Output_Timeout, didn't try select!\n" ));
1242 return GSOCK_NOERROR
;
1245 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
1247 return recv(socket
->m_fd
, buffer
, size
, 0);
1250 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
1252 struct sockaddr from
;
1253 SOCKLEN_T fromlen
= sizeof(from
);
1257 fromlen
= sizeof(from
);
1259 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, (SOCKLEN_T
*) &fromlen
);
1264 /* Translate a system address into a GSocket address */
1265 if (!socket
->m_peer
)
1267 socket
->m_peer
= GAddress_new();
1268 if (!socket
->m_peer
)
1270 socket
->m_error
= GSOCK_MEMERR
;
1274 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
1275 if (err
!= GSOCK_NOERROR
)
1277 GAddress_destroy(socket
->m_peer
);
1278 socket
->m_peer
= NULL
;
1279 socket
->m_error
= err
;
1286 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
1290 #ifndef __VISAGECPP__
1292 ret
= send(socket
->m_fd
, buffer
, size
, 0);
1295 ret
= send(socket
->m_fd
, (char *)buffer
, size
, 0);
1301 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
1303 struct sockaddr
*addr
;
1307 if (!socket
->m_peer
)
1309 socket
->m_error
= GSOCK_INVADDR
;
1313 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
1314 if (err
!= GSOCK_NOERROR
)
1316 socket
->m_error
= err
;
1320 #ifndef __VISAGECPP__
1322 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
1325 ret
= sendto(socket
->m_fd
, (char *)buffer
, size
, 0, addr
, len
);
1328 /* Frees memory allocated from _GAddress_translate_to */
1334 void _GSocket_Detected_Read(GSocket
*socket
)
1338 /* If we have already detected a LOST event, then don't try
1339 * to do any further processing.
1341 if ((socket
->m_detected
& GSOCK_LOST_FLAG
) != 0)
1343 socket
->m_establishing
= FALSE
;
1345 CALL_CALLBACK(socket
, GSOCK_LOST
);
1346 GSocket_Shutdown(socket
);
1350 if (recv(socket
->m_fd
, &c
, 1, MSG_PEEK
) > 0)
1352 CALL_CALLBACK(socket
, GSOCK_INPUT
);
1356 if (socket
->m_server
&& socket
->m_stream
)
1358 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
1362 CALL_CALLBACK(socket
, GSOCK_LOST
);
1363 GSocket_Shutdown(socket
);
1368 void _GSocket_Detected_Write(GSocket
*socket
)
1370 /* If we have already detected a LOST event, then don't try
1371 * to do any further processing.
1373 if ((socket
->m_detected
& GSOCK_LOST_FLAG
) != 0)
1375 socket
->m_establishing
= FALSE
;
1377 CALL_CALLBACK(socket
, GSOCK_LOST
);
1378 GSocket_Shutdown(socket
);
1382 if (socket
->m_establishing
&& !socket
->m_server
)
1385 SOCKLEN_T len
= sizeof(error
);
1387 socket
->m_establishing
= FALSE
;
1389 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*)&error
, &len
);
1393 CALL_CALLBACK(socket
, GSOCK_LOST
);
1394 GSocket_Shutdown(socket
);
1398 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
1399 /* We have to fire this event by hand because CONNECTION (for clients)
1400 * and OUTPUT are internally the same and we just disabled CONNECTION
1401 * events with the above macro.
1403 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
1408 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
1413 * -------------------------------------------------------------------------
1415 * -------------------------------------------------------------------------
1418 /* CHECK_ADDRESS verifies that the current address family is either
1419 * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it
1420 * initalizes it to be a GSOCK_*family*. In other cases, it returns
1421 * an appropiate error code.
1423 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
1425 #define CHECK_ADDRESS(address, family) \
1427 if (address->m_family == GSOCK_NOFAMILY) \
1428 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1429 return address->m_error; \
1430 if (address->m_family != GSOCK_##family) \
1432 address->m_error = GSOCK_INVADDR; \
1433 return GSOCK_INVADDR; \
1437 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
1439 if (address->m_family == GSOCK_NOFAMILY) \
1440 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1442 if (address->m_family != GSOCK_##family) \
1444 address->m_error = GSOCK_INVADDR; \
1450 GAddress
*GAddress_new(void)
1454 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1457 address
->m_family
= GSOCK_NOFAMILY
;
1458 address
->m_addr
= NULL
;
1464 GAddress
*GAddress_copy(GAddress
*address
)
1468 assert(address
!= NULL
);
1470 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1473 memcpy(addr2
, address
, sizeof(GAddress
));
1475 if (address
->m_addr
&& address
->m_len
> 0)
1477 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
1478 if (addr2
->m_addr
== NULL
)
1483 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1489 void GAddress_destroy(GAddress
*address
)
1491 assert(address
!= NULL
);
1493 if (address
->m_addr
)
1494 free(address
->m_addr
);
1499 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1501 assert(address
!= NULL
);
1503 address
->m_family
= type
;
1506 GAddressType
GAddress_GetFamily(GAddress
*address
)
1508 assert(address
!= NULL
);
1510 return address
->m_family
;
1513 GSocketError
_GAddress_translate_from(GAddress
*address
,
1514 struct sockaddr
*addr
, int len
)
1516 address
->m_realfamily
= addr
->sa_family
;
1517 switch (addr
->sa_family
)
1520 address
->m_family
= GSOCK_INET
;
1523 address
->m_family
= GSOCK_UNIX
;
1527 address
->m_family
= GSOCK_INET6
;
1532 address
->m_error
= GSOCK_INVOP
;
1537 if (address
->m_addr
)
1538 free(address
->m_addr
);
1540 address
->m_len
= len
;
1541 address
->m_addr
= (struct sockaddr
*)malloc(len
);
1543 if (address
->m_addr
== NULL
)
1545 address
->m_error
= GSOCK_MEMERR
;
1546 return GSOCK_MEMERR
;
1548 memcpy(address
->m_addr
, addr
, len
);
1550 return GSOCK_NOERROR
;
1553 GSocketError
_GAddress_translate_to(GAddress
*address
,
1554 struct sockaddr
**addr
, int *len
)
1556 if (!address
->m_addr
)
1558 address
->m_error
= GSOCK_INVADDR
;
1559 return GSOCK_INVADDR
;
1562 *len
= address
->m_len
;
1563 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
1566 address
->m_error
= GSOCK_MEMERR
;
1567 return GSOCK_MEMERR
;
1570 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1571 return GSOCK_NOERROR
;
1575 * -------------------------------------------------------------------------
1576 * Internet address family
1577 * -------------------------------------------------------------------------
1580 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1582 address
->m_len
= sizeof(struct sockaddr_in
);
1583 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1584 if (address
->m_addr
== NULL
)
1586 address
->m_error
= GSOCK_MEMERR
;
1587 return GSOCK_MEMERR
;
1590 address
->m_family
= GSOCK_INET
;
1591 address
->m_realfamily
= PF_INET
;
1592 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1593 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1595 return GSOCK_NOERROR
;
1598 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1601 struct in_addr
*addr
;
1603 assert(address
!= NULL
);
1605 CHECK_ADDRESS(address
, INET
);
1607 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1609 /* If it is a numeric host name, convert it now */
1610 #if defined(HAVE_INET_ATON)
1611 if (inet_aton(hostname
, addr
) == 0)
1613 #elif defined(HAVE_INET_ADDR)
1614 if ( (addr
->s_addr
= inet_addr(hostname
)) == -1 )
1617 /* Use gethostbyname by default */
1618 int val
= 1; //VA doesn't like constants in conditional expressions at all
1622 struct in_addr
*array_addr
;
1624 /* It is a real name, we solve it */
1625 if ((he
= gethostbyname(hostname
)) == NULL
)
1627 /* Reset to invalid address */
1628 addr
->s_addr
= INADDR_NONE
;
1629 address
->m_error
= GSOCK_NOHOST
;
1630 return GSOCK_NOHOST
;
1632 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1633 addr
->s_addr
= array_addr
[0].s_addr
;
1635 return GSOCK_NOERROR
;
1638 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1640 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1643 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1644 unsigned long hostaddr
)
1646 struct in_addr
*addr
;
1648 assert(address
!= NULL
);
1650 CHECK_ADDRESS(address
, INET
);
1652 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1653 addr
->s_addr
= hostaddr
;
1655 return GSOCK_NOERROR
;
1658 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1659 const char *protocol
)
1662 struct sockaddr_in
*addr
;
1664 assert(address
!= NULL
);
1665 CHECK_ADDRESS(address
, INET
);
1669 address
->m_error
= GSOCK_INVPORT
;
1670 return GSOCK_INVPORT
;
1673 se
= getservbyname(port
, protocol
);
1676 /* the cast to int suppresses compiler warnings about subscript having the
1678 if (isdigit((int)port
[0]))
1682 port_int
= atoi(port
);
1683 addr
= (struct sockaddr_in
*)address
->m_addr
;
1684 addr
->sin_port
= htons(port_int
);
1685 return GSOCK_NOERROR
;
1688 address
->m_error
= GSOCK_INVPORT
;
1689 return GSOCK_INVPORT
;
1692 addr
= (struct sockaddr_in
*)address
->m_addr
;
1693 addr
->sin_port
= se
->s_port
;
1695 return GSOCK_NOERROR
;
1698 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1700 struct sockaddr_in
*addr
;
1702 assert(address
!= NULL
);
1703 CHECK_ADDRESS(address
, INET
);
1705 addr
= (struct sockaddr_in
*)address
->m_addr
;
1706 addr
->sin_port
= htons(port
);
1708 return GSOCK_NOERROR
;
1711 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1715 struct sockaddr_in
*addr
;
1717 assert(address
!= NULL
);
1718 CHECK_ADDRESS(address
, INET
);
1720 addr
= (struct sockaddr_in
*)address
->m_addr
;
1721 addr_buf
= (char *)&(addr
->sin_addr
);
1723 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1726 address
->m_error
= GSOCK_NOHOST
;
1727 return GSOCK_NOHOST
;
1730 strncpy(hostname
, he
->h_name
, sbuf
);
1732 return GSOCK_NOERROR
;
1735 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1737 struct sockaddr_in
*addr
;
1739 assert(address
!= NULL
);
1740 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1742 addr
= (struct sockaddr_in
*)address
->m_addr
;
1744 return addr
->sin_addr
.s_addr
;
1747 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1749 struct sockaddr_in
*addr
;
1751 assert(address
!= NULL
);
1752 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1754 addr
= (struct sockaddr_in
*)address
->m_addr
;
1755 return ntohs(addr
->sin_port
);
1759 * -------------------------------------------------------------------------
1760 * Unix address family
1761 * -------------------------------------------------------------------------
1764 #ifndef __VISAGECPP__
1765 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1767 address
->m_len
= sizeof(struct sockaddr_un
);
1768 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
1769 if (address
->m_addr
== NULL
)
1771 address
->m_error
= GSOCK_MEMERR
;
1772 return GSOCK_MEMERR
;
1775 address
->m_family
= GSOCK_UNIX
;
1776 address
->m_realfamily
= PF_UNIX
;
1777 ((struct sockaddr_un
*)address
->m_addr
)->sun_family
= AF_UNIX
;
1778 ((struct sockaddr_un
*)address
->m_addr
)->sun_path
[0] = 0;
1780 return GSOCK_NOERROR
;
1783 #define UNIX_SOCK_PATHLEN (sizeof(addr->sun_path)/sizeof(addr->sun_path[0]))
1785 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1787 struct sockaddr_un
*addr
;
1789 assert(address
!= NULL
);
1791 CHECK_ADDRESS(address
, UNIX
);
1793 addr
= ((struct sockaddr_un
*)address
->m_addr
);
1794 strncpy(addr
->sun_path
, path
, UNIX_SOCK_PATHLEN
);
1795 addr
->sun_path
[UNIX_SOCK_PATHLEN
- 1] = '\0';
1797 return GSOCK_NOERROR
;
1800 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1802 struct sockaddr_un
*addr
;
1804 assert(address
!= NULL
);
1805 CHECK_ADDRESS(address
, UNIX
);
1807 addr
= (struct sockaddr_un
*)address
->m_addr
;
1809 strncpy(path
, addr
->sun_path
, sbuf
);
1811 return GSOCK_NOERROR
;
1813 #endif /* !defined(__VISAGECPP__) */
1814 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */