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 /* Check that the socket is really shutdowned */
273 if (socket
->m_fd
!= INVALID_SOCKET
)
274 GSocket_Shutdown(socket
);
276 /* Per-socket GUI-specific cleanup */
277 _GSocket_GUI_Destroy_Socket(socket
);
279 /* Destroy private addresses */
281 GAddress_destroy(socket
->m_local
);
284 GAddress_destroy(socket
->m_peer
);
286 /* Destroy the socket itself */
291 * Disallow further read/write operations on this socket, close
292 * the fd and disable all callbacks.
294 void GSocket_Shutdown(GSocket
*socket
)
298 assert(socket
!= NULL
);
300 /* If socket has been created, shutdown it */
301 if (socket
->m_fd
!= INVALID_SOCKET
)
303 shutdown(socket
->m_fd
, 2);
304 GSocket_close(socket
);
307 /* Disable GUI callbacks */
308 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
309 socket
->m_cbacks
[evt
] = NULL
;
311 socket
->m_detected
= GSOCK_LOST_FLAG
;
314 /* Address handling */
320 * Set or get the local or peer address for this socket. The 'set'
321 * functions return GSOCK_NOERROR on success, an error code otherwise.
322 * The 'get' functions return a pointer to a GAddress object on success,
323 * or NULL otherwise, in which case they set the error code of the
324 * corresponding GSocket.
327 * GSOCK_INVSOCK - the socket is not valid.
328 * GSOCK_INVADDR - the address is not valid.
330 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
332 assert(socket
!= NULL
);
334 /* the socket must be initialized, or it must be a server */
335 if ((socket
->m_fd
!= INVALID_SOCKET
&& !socket
->m_server
))
337 socket
->m_error
= GSOCK_INVSOCK
;
338 return GSOCK_INVSOCK
;
342 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
344 socket
->m_error
= GSOCK_INVADDR
;
345 return GSOCK_INVADDR
;
349 GAddress_destroy(socket
->m_local
);
351 socket
->m_local
= GAddress_copy(address
);
353 return GSOCK_NOERROR
;
356 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
358 assert(socket
!= NULL
);
361 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
363 socket
->m_error
= GSOCK_INVADDR
;
364 return GSOCK_INVADDR
;
368 GAddress_destroy(socket
->m_peer
);
370 socket
->m_peer
= GAddress_copy(address
);
372 return GSOCK_NOERROR
;
375 GAddress
*GSocket_GetLocal(GSocket
*socket
)
378 struct sockaddr addr
;
379 SOCKLEN_T size
= sizeof(addr
);
382 assert(socket
!= NULL
);
384 /* try to get it from the m_local var first */
386 return GAddress_copy(socket
->m_local
);
388 /* else, if the socket is initialized, try getsockname */
389 if (socket
->m_fd
== INVALID_SOCKET
)
391 socket
->m_error
= GSOCK_INVSOCK
;
395 if (getsockname(socket
->m_fd
, &addr
, (SOCKLEN_T
*) &size
) < 0)
397 socket
->m_error
= GSOCK_IOERR
;
401 /* got a valid address from getsockname, create a GAddress object */
402 address
= GAddress_new();
405 socket
->m_error
= GSOCK_MEMERR
;
409 err
= _GAddress_translate_from(address
, &addr
, size
);
410 if (err
!= GSOCK_NOERROR
)
412 GAddress_destroy(address
);
413 socket
->m_error
= err
;
420 GAddress
*GSocket_GetPeer(GSocket
*socket
)
422 assert(socket
!= NULL
);
424 /* try to get it from the m_peer var */
426 return GAddress_copy(socket
->m_peer
);
431 /* Server specific parts */
433 /* GSocket_SetServer:
434 * Sets up this socket as a server. The local address must have been
435 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
436 * Returns GSOCK_NOERROR on success, one of the following otherwise:
439 * GSOCK_INVSOCK - the socket is in use.
440 * GSOCK_INVADDR - the local address has not been set.
441 * GSOCK_IOERR - low-level error.
443 GSocketError
GSocket_SetServer(GSocket
*sck
)
449 /* must not be in use */
450 if (sck
->m_fd
!= INVALID_SOCKET
)
452 sck
->m_error
= GSOCK_INVSOCK
;
453 return GSOCK_INVSOCK
;
456 /* the local addr must have been set */
459 sck
->m_error
= GSOCK_INVADDR
;
460 return GSOCK_INVADDR
;
463 /* Initialize all fields */
464 sck
->m_stream
= TRUE
;
465 sck
->m_server
= TRUE
;
466 sck
->m_oriented
= TRUE
;
468 /* Create the socket */
469 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
471 if (sck
->m_fd
== INVALID_SOCKET
)
473 sck
->m_error
= GSOCK_IOERR
;
476 #if defined(__EMX__) || defined(__VISAGECPP__)
477 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
479 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
481 _GSocket_Enable_Events(sck
);
483 /* Bind to the local address,
484 * retrieve the actual address bound,
485 * and listen up to 5 connections.
487 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
488 (getsockname(sck
->m_fd
,
489 sck
->m_local
->m_addr
,
490 (SOCKLEN_T
*) &sck
->m_local
->m_len
) != 0) ||
491 (listen(sck
->m_fd
, 5) != 0))
494 sck
->m_error
= GSOCK_IOERR
;
498 return GSOCK_NOERROR
;
501 /* GSocket_WaitConnection:
502 * Waits for an incoming client connection. Returns a pointer to
503 * a GSocket object, or NULL if there was an error, in which case
504 * the last error field will be updated for the calling GSocket.
506 * Error codes (set in the calling GSocket)
507 * GSOCK_INVSOCK - the socket is not valid or not a server.
508 * GSOCK_TIMEDOUT - timeout, no incoming connections.
509 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
510 * GSOCK_MEMERR - couldn't allocate memory.
511 * GSOCK_IOERR - low-level error.
513 GSocket
*GSocket_WaitConnection(GSocket
*socket
)
515 struct sockaddr from
;
516 SOCKLEN_T fromlen
= sizeof(from
);
521 assert(socket
!= NULL
);
523 /* Reenable CONNECTION events */
524 _GSocket_Enable(socket
, GSOCK_CONNECTION
);
526 /* If the socket has already been created, we exit immediately */
527 if (socket
->m_fd
== INVALID_SOCKET
|| !socket
->m_server
)
529 socket
->m_error
= GSOCK_INVSOCK
;
533 /* Create a GSocket object for the new connection */
534 connection
= GSocket_new();
538 socket
->m_error
= GSOCK_MEMERR
;
542 /* Wait for a connection (with timeout) */
543 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
545 GSocket_destroy(connection
);
546 /* socket->m_error set by _GSocket_Input_Timeout */
550 connection
->m_fd
= accept(socket
->m_fd
, &from
, (SOCKLEN_T
*) &fromlen
);
552 if (connection
->m_fd
== INVALID_SOCKET
)
554 if (errno
== EWOULDBLOCK
)
555 socket
->m_error
= GSOCK_WOULDBLOCK
;
557 socket
->m_error
= GSOCK_IOERR
;
559 GSocket_destroy(connection
);
563 /* Initialize all fields */
564 connection
->m_server
= FALSE
;
565 connection
->m_stream
= TRUE
;
566 connection
->m_oriented
= TRUE
;
568 /* Setup the peer address field */
569 connection
->m_peer
= GAddress_new();
570 if (!connection
->m_peer
)
572 GSocket_destroy(connection
);
573 socket
->m_error
= GSOCK_MEMERR
;
576 err
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
);
577 if (err
!= GSOCK_NOERROR
)
579 GAddress_destroy(connection
->m_peer
);
580 GSocket_destroy(connection
);
581 socket
->m_error
= err
;
584 #if defined(__EMX__) || defined(__VISAGECPP__)
585 ioctl(connection
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
587 ioctl(connection
->m_fd
, FIONBIO
, &arg
);
589 _GSocket_Enable_Events(connection
);
594 /* Client specific parts */
597 * For stream (connection oriented) sockets, GSocket_Connect() tries
598 * to establish a client connection to a server using the peer address
599 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
600 * connection has been succesfully established, or one of the error
601 * codes listed below. Note that for nonblocking sockets, a return
602 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
603 * request can be completed later; you should use GSocket_Select()
604 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
605 * corresponding asynchronous events.
607 * For datagram (non connection oriented) sockets, GSocket_Connect()
608 * just sets the peer address established with GSocket_SetPeer() as
609 * default destination.
612 * GSOCK_INVSOCK - the socket is in use or not valid.
613 * GSOCK_INVADDR - the peer address has not been established.
614 * GSOCK_TIMEDOUT - timeout, the connection failed.
615 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
616 * GSOCK_MEMERR - couldn't allocate memory.
617 * GSOCK_IOERR - low-level error.
619 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
626 /* Enable CONNECTION events (needed for nonblocking connections) */
627 _GSocket_Enable(sck
, GSOCK_CONNECTION
);
629 if (sck
->m_fd
!= INVALID_SOCKET
)
631 sck
->m_error
= GSOCK_INVSOCK
;
632 return GSOCK_INVSOCK
;
637 sck
->m_error
= GSOCK_INVADDR
;
638 return GSOCK_INVADDR
;
641 /* Streamed or dgram socket? */
642 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
643 sck
->m_oriented
= TRUE
;
644 sck
->m_server
= FALSE
;
645 sck
->m_establishing
= FALSE
;
647 /* Create the socket */
648 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
,
649 sck
->m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
651 if (sck
->m_fd
== INVALID_SOCKET
)
653 sck
->m_error
= GSOCK_IOERR
;
656 #if defined(__EMX__) || defined(__VISAGECPP__)
657 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
659 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
661 _GSocket_Enable_Events(sck
);
663 /* Connect it to the peer address, with a timeout (see below) */
664 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
670 /* If connect failed with EINPROGRESS and the GSocket object
671 * is in blocking mode, we select() for the specified timeout
672 * checking for writability to see if the connection request
675 if ((err
== EINPROGRESS
) && (!sck
->m_non_blocking
))
677 if (_GSocket_Output_Timeout(sck
) == GSOCK_TIMEDOUT
)
680 /* sck->m_error is set in _GSocket_Output_Timeout */
681 return GSOCK_TIMEDOUT
;
686 SOCKLEN_T len
= sizeof(error
);
688 getsockopt(sck
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*) &error
, &len
);
691 return GSOCK_NOERROR
;
695 /* If connect failed with EINPROGRESS and the GSocket object
696 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
697 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
698 * this way if the connection completes, a GSOCK_CONNECTION
699 * event will be generated, if enabled.
701 if ((err
== EINPROGRESS
) && (sck
->m_non_blocking
))
703 sck
->m_establishing
= TRUE
;
704 sck
->m_error
= GSOCK_WOULDBLOCK
;
705 return GSOCK_WOULDBLOCK
;
708 /* If connect failed with an error other than EINPROGRESS,
709 * then the call to GSocket_Connect has failed.
712 sck
->m_error
= GSOCK_IOERR
;
716 return GSOCK_NOERROR
;
719 /* Datagram sockets */
721 /* GSocket_SetNonOriented:
722 * Sets up this socket as a non-connection oriented (datagram) socket.
723 * Before using this function, the local address must have been set
724 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
725 * on success, or one of the following otherwise.
728 * GSOCK_INVSOCK - the socket is in use.
729 * GSOCK_INVADDR - the local address has not been set.
730 * GSOCK_IOERR - low-level error.
732 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
738 if (sck
->m_fd
!= INVALID_SOCKET
)
740 sck
->m_error
= GSOCK_INVSOCK
;
741 return GSOCK_INVSOCK
;
746 sck
->m_error
= GSOCK_INVADDR
;
747 return GSOCK_INVADDR
;
750 /* Initialize all fields */
751 sck
->m_stream
= FALSE
;
752 sck
->m_server
= FALSE
;
753 sck
->m_oriented
= FALSE
;
755 /* Create the socket */
756 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
758 if (sck
->m_fd
== INVALID_SOCKET
)
760 sck
->m_error
= GSOCK_IOERR
;
763 #if defined(__EMX__) || defined(__VISAGECPP__)
764 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
766 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
768 _GSocket_Enable_Events(sck
);
770 /* Bind to the local address,
771 * and retrieve the actual address bound.
773 if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) ||
774 (getsockname(sck
->m_fd
,
775 sck
->m_local
->m_addr
,
776 (SOCKLEN_T
*) &sck
->m_local
->m_len
) != 0))
779 sck
->m_error
= GSOCK_IOERR
;
783 return GSOCK_NOERROR
;
788 /* Like recv(), send(), ... */
789 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
793 assert(socket
!= NULL
);
795 /* Reenable INPUT events */
796 _GSocket_Enable(socket
, GSOCK_INPUT
);
798 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
800 socket
->m_error
= GSOCK_INVSOCK
;
804 /* If the socket is blocking, wait for data (with a timeout) */
805 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
809 if (socket
->m_stream
)
810 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
812 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
816 if (errno
== EWOULDBLOCK
)
817 socket
->m_error
= GSOCK_WOULDBLOCK
;
819 socket
->m_error
= GSOCK_IOERR
;
825 int GSocket_Write(GSocket
*socket
, const char *buffer
, int size
)
829 assert(socket
!= NULL
);
831 GSocket_Debug(( "GSocket_Write #1, size %d\n", size
));
833 if (socket
->m_fd
== INVALID_SOCKET
|| socket
->m_server
)
835 socket
->m_error
= GSOCK_INVSOCK
;
839 GSocket_Debug(( "GSocket_Write #2, size %d\n", size
));
841 /* If the socket is blocking, wait for writability (with a timeout) */
842 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
845 GSocket_Debug(( "GSocket_Write #3, size %d\n", size
));
848 if (socket
->m_stream
)
849 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
851 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
853 GSocket_Debug(( "GSocket_Write #4, size %d\n", size
));
857 if (errno
== EWOULDBLOCK
)
859 socket
->m_error
= GSOCK_WOULDBLOCK
;
860 GSocket_Debug(( "GSocket_Write error WOULDBLOCK\n" ));
864 socket
->m_error
= GSOCK_IOERR
;
865 GSocket_Debug(( "GSocket_Write error IOERR\n" ));
868 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
869 * in MSW). Once the first OUTPUT event is received, users can assume
870 * that the socket is writable until a read operation fails. Only then
871 * will further OUTPUT events be posted.
873 _GSocket_Enable(socket
, GSOCK_OUTPUT
);
877 GSocket_Debug(( "GSocket_Write #5, size %d ret %d\n", size
, ret
));
883 * Polls the socket to determine its status. This function will
884 * check for the events specified in the 'flags' parameter, and
885 * it will return a mask indicating which operations can be
886 * performed. This function won't block, regardless of the
887 * mode (blocking | nonblocking) of the socket.
889 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
894 GSocketEventFlags result
= 0;
900 /* Do not use a static struct, Linux can garble it */
904 assert(socket
!= NULL
);
909 FD_SET(socket
->m_fd
, &readfds
);
910 FD_SET(socket
->m_fd
, &writefds
);
911 FD_SET(socket
->m_fd
, &exceptfds
);
913 /* Check 'sticky' CONNECTION flag first */
914 result
|= (GSOCK_CONNECTION_FLAG
& socket
->m_detected
);
916 /* If we have already detected a LOST event, then don't try
917 * to do any further processing.
919 if ((socket
->m_detected
& GSOCK_LOST_FLAG
) != 0)
921 socket
->m_establishing
= FALSE
;
923 return (GSOCK_LOST_FLAG
& flags
);
927 if (select(socket
->m_fd
+ 1, &readfds
, &writefds
, &exceptfds
, &tv
) <= 0)
929 /* What to do here? */
930 return (result
& flags
);
933 /* Check for readability */
934 if (FD_ISSET(socket
->m_fd
, &readfds
))
938 if (recv(socket
->m_fd
, &c
, 1, MSG_PEEK
) > 0)
940 result
|= GSOCK_INPUT_FLAG
;
944 if (socket
->m_server
&& socket
->m_stream
)
946 result
|= GSOCK_CONNECTION_FLAG
;
947 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
951 socket
->m_detected
= GSOCK_LOST_FLAG
;
952 socket
->m_establishing
= FALSE
;
954 /* LOST event: Abort any further processing */
955 return (GSOCK_LOST_FLAG
& flags
);
960 /* Check for writability */
961 if (FD_ISSET(socket
->m_fd
, &writefds
))
963 if (socket
->m_establishing
&& !socket
->m_server
)
966 SOCKLEN_T len
= sizeof(error
);
968 socket
->m_establishing
= FALSE
;
970 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*)&error
, &len
);
974 socket
->m_detected
= GSOCK_LOST_FLAG
;
976 /* LOST event: Abort any further processing */
977 return (GSOCK_LOST_FLAG
& flags
);
981 result
|= GSOCK_CONNECTION_FLAG
;
982 socket
->m_detected
|= GSOCK_CONNECTION_FLAG
;
987 result
|= GSOCK_OUTPUT_FLAG
;
991 /* Check for exceptions and errors (is this useful in Unices?) */
992 if (FD_ISSET(socket
->m_fd
, &exceptfds
))
994 socket
->m_establishing
= FALSE
;
995 socket
->m_detected
= GSOCK_LOST_FLAG
;
997 /* LOST event: Abort any further processing */
998 return (GSOCK_LOST_FLAG
& flags
);
1001 return (result
& flags
);
1007 assert(socket
!= NULL
);
1008 return flags
& socket
->m_detected
;
1015 /* GSocket_SetNonBlocking:
1016 * Sets the socket to non-blocking mode. All IO calls will return
1019 void GSocket_SetNonBlocking(GSocket
*socket
, int non_block
)
1021 assert(socket
!= NULL
);
1023 GSocket_Debug( ("GSocket_SetNonBlocking: %d\n", (int)non_block
) );
1025 socket
->m_non_blocking
= non_block
;
1028 /* GSocket_SetTimeout:
1029 * Sets the timeout for blocking calls. Time is expressed in
1032 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
)
1034 assert(socket
!= NULL
);
1036 socket
->m_timeout
= millisec
;
1039 /* GSocket_GetError:
1040 * Returns the last error occured for this socket. Note that successful
1041 * operations do not clear this back to GSOCK_NOERROR, so use it only
1044 GSocketError
GSocket_GetError(GSocket
*socket
)
1046 assert(socket
!= NULL
);
1048 return socket
->m_error
;
1054 * There is data to be read in the input buffer. If, after a read
1055 * operation, there is still data available, the callback function will
1058 * The socket is available for writing. That is, the next write call
1059 * won't block. This event is generated only once, when the connection is
1060 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
1061 * when the output buffer empties again. This means that the app should
1062 * assume that it can write since the first OUTPUT event, and no more
1063 * OUTPUT events will be generated unless an error occurs.
1065 * Connection succesfully established, for client sockets, or incoming
1066 * client connection, for server sockets. Wait for this event (also watch
1067 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
1069 * The connection is lost (or a connection request failed); this could
1070 * be due to a failure, or due to the peer closing it gracefully.
1073 /* GSocket_SetCallback:
1074 * Enables the callbacks specified by 'flags'. Note that 'flags'
1075 * may be a combination of flags OR'ed toghether, so the same
1076 * callback function can be made to accept different events.
1077 * The callback function must have the following prototype:
1079 * void function(GSocket *socket, GSocketEvent event, char *cdata)
1081 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
1082 GSocketCallback callback
, char *cdata
)
1086 assert(socket
!= NULL
);
1088 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
1090 if ((flags
& (1 << count
)) != 0)
1092 socket
->m_cbacks
[count
] = callback
;
1093 socket
->m_data
[count
] = cdata
;
1098 /* GSocket_UnsetCallback:
1099 * Disables all callbacks specified by 'flags', which may be a
1100 * combination of flags OR'ed toghether.
1102 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
1106 assert(socket
!= NULL
);
1108 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
1110 if ((flags
& (1 << count
)) != 0)
1112 socket
->m_cbacks
[count
] = NULL
;
1113 socket
->m_data
[count
] = NULL
;
1119 #define CALL_CALLBACK(socket, event) { \
1120 _GSocket_Disable(socket, event); \
1121 if (socket->m_cbacks[event]) \
1122 socket->m_cbacks[event](socket, event, socket->m_data[event]); \
1126 void _GSocket_Enable(GSocket
*socket
, GSocketEvent event
)
1128 socket
->m_detected
&= ~(1 << event
);
1129 _GSocket_Install_Callback(socket
, event
);
1132 void _GSocket_Disable(GSocket
*socket
, GSocketEvent event
)
1134 socket
->m_detected
|= (1 << event
);
1135 _GSocket_Uninstall_Callback(socket
, event
);
1138 /* _GSocket_Input_Timeout:
1139 * For blocking sockets, wait until data is available or
1140 * until timeout ellapses.
1142 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
1148 /* Linux select() will overwrite the struct on return */
1149 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
1150 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
1152 if (!socket
->m_non_blocking
)
1155 FD_SET(socket
->m_fd
, &readfds
);
1156 ret
= select(socket
->m_fd
+ 1, &readfds
, NULL
, NULL
, &tv
);
1159 GSocket_Debug(( "GSocket_Input_Timeout, select returned 0\n" ));
1160 socket
->m_error
= GSOCK_TIMEDOUT
;
1161 return GSOCK_TIMEDOUT
;
1165 GSocket_Debug(( "GSocket_Input_Timeout, select returned -1\n" ));
1166 if (errno
== EBADF
) { GSocket_Debug(( "Invalid file descriptor\n" )); }
1167 if (errno
== EINTR
) { GSocket_Debug(( "A non blocked signal was caught\n" )); }
1168 if (errno
== EINVAL
) { GSocket_Debug(( "The highest number descriptor is negative\n" )); }
1169 if (errno
== ENOMEM
) { GSocket_Debug(( "Not enough memory\n" )); }
1170 socket
->m_error
= GSOCK_TIMEDOUT
;
1171 return GSOCK_TIMEDOUT
;
1174 return GSOCK_NOERROR
;
1177 /* _GSocket_Output_Timeout:
1178 * For blocking sockets, wait until data can be sent without
1179 * blocking or until timeout ellapses.
1181 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
1187 /* Linux select() will overwrite the struct on return */
1188 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
1189 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
1191 GSocket_Debug( ("m_non_blocking has: %d\n", (int)socket
->m_non_blocking
) );
1193 if (!socket
->m_non_blocking
)
1196 FD_SET(socket
->m_fd
, &writefds
);
1197 ret
= select(socket
->m_fd
+ 1, NULL
, &writefds
, NULL
, &tv
);
1200 GSocket_Debug(( "GSocket_Output_Timeout, select returned 0\n" ));
1201 socket
->m_error
= GSOCK_TIMEDOUT
;
1202 return GSOCK_TIMEDOUT
;
1206 GSocket_Debug(( "GSocket_Output_Timeout, select returned -1\n" ));
1207 if (errno
== EBADF
) { GSocket_Debug(( "Invalid file descriptor\n" )); }
1208 if (errno
== EINTR
) { GSocket_Debug(( "A non blocked signal was caught\n" )); }
1209 if (errno
== EINVAL
) { GSocket_Debug(( "The highest number descriptor is negative\n" )); }
1210 if (errno
== ENOMEM
) { GSocket_Debug(( "Not enough memory\n" )); }
1211 socket
->m_error
= GSOCK_TIMEDOUT
;
1212 return GSOCK_TIMEDOUT
;
1214 if ( ! FD_ISSET(socket
->m_fd
, &writefds
) ) {
1215 GSocket_Debug(( "GSocket_Output_Timeout is buggy!\n" ));
1218 GSocket_Debug(( "GSocket_Output_Timeout seems correct\n" ));
1223 GSocket_Debug(( "GSocket_Output_Timeout, didn't try select!\n" ));
1226 return GSOCK_NOERROR
;
1229 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
1231 return recv(socket
->m_fd
, buffer
, size
, 0);
1234 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
1236 struct sockaddr from
;
1237 SOCKLEN_T fromlen
= sizeof(from
);
1241 fromlen
= sizeof(from
);
1243 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, (SOCKLEN_T
*) &fromlen
);
1248 /* Translate a system address into a GSocket address */
1249 if (!socket
->m_peer
)
1251 socket
->m_peer
= GAddress_new();
1252 if (!socket
->m_peer
)
1254 socket
->m_error
= GSOCK_MEMERR
;
1258 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
1259 if (err
!= GSOCK_NOERROR
)
1261 GAddress_destroy(socket
->m_peer
);
1262 socket
->m_peer
= NULL
;
1263 socket
->m_error
= err
;
1270 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
1274 #ifndef __VISAGECPP__
1276 ret
= send(socket
->m_fd
, buffer
, size
, 0);
1279 ret
= send(socket
->m_fd
, (char *)buffer
, size
, 0);
1285 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
1287 struct sockaddr
*addr
;
1291 if (!socket
->m_peer
)
1293 socket
->m_error
= GSOCK_INVADDR
;
1297 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
1298 if (err
!= GSOCK_NOERROR
)
1300 socket
->m_error
= err
;
1304 #ifndef __VISAGECPP__
1306 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
1309 ret
= sendto(socket
->m_fd
, (char *)buffer
, size
, 0, addr
, len
);
1312 /* Frees memory allocated from _GAddress_translate_to */
1318 void _GSocket_Detected_Read(GSocket
*socket
)
1322 /* If we have already detected a LOST event, then don't try
1323 * to do any further processing.
1325 if ((socket
->m_detected
& GSOCK_LOST_FLAG
) != 0)
1327 socket
->m_establishing
= FALSE
;
1329 CALL_CALLBACK(socket
, GSOCK_LOST
);
1330 GSocket_Shutdown(socket
);
1334 if (recv(socket
->m_fd
, &c
, 1, MSG_PEEK
) > 0)
1336 CALL_CALLBACK(socket
, GSOCK_INPUT
);
1340 if (socket
->m_server
&& socket
->m_stream
)
1342 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
1346 CALL_CALLBACK(socket
, GSOCK_LOST
);
1347 GSocket_Shutdown(socket
);
1352 void _GSocket_Detected_Write(GSocket
*socket
)
1354 /* If we have already detected a LOST event, then don't try
1355 * to do any further processing.
1357 if ((socket
->m_detected
& GSOCK_LOST_FLAG
) != 0)
1359 socket
->m_establishing
= FALSE
;
1361 CALL_CALLBACK(socket
, GSOCK_LOST
);
1362 GSocket_Shutdown(socket
);
1366 if (socket
->m_establishing
&& !socket
->m_server
)
1369 SOCKLEN_T len
= sizeof(error
);
1371 socket
->m_establishing
= FALSE
;
1373 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*)&error
, &len
);
1377 CALL_CALLBACK(socket
, GSOCK_LOST
);
1378 GSocket_Shutdown(socket
);
1382 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
1383 /* We have to fire this event by hand because CONNECTION (for clients)
1384 * and OUTPUT are internally the same and we just disabled CONNECTION
1385 * events with the above macro.
1387 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
1392 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
1397 * -------------------------------------------------------------------------
1399 * -------------------------------------------------------------------------
1402 /* CHECK_ADDRESS verifies that the current address family is either
1403 * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it
1404 * initalizes it to be a GSOCK_*family*. In other cases, it returns
1405 * an appropiate error code.
1407 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
1409 #define CHECK_ADDRESS(address, family) \
1411 if (address->m_family == GSOCK_NOFAMILY) \
1412 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1413 return address->m_error; \
1414 if (address->m_family != GSOCK_##family) \
1416 address->m_error = GSOCK_INVADDR; \
1417 return GSOCK_INVADDR; \
1421 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
1423 if (address->m_family == GSOCK_NOFAMILY) \
1424 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1426 if (address->m_family != GSOCK_##family) \
1428 address->m_error = GSOCK_INVADDR; \
1434 GAddress
*GAddress_new(void)
1438 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1441 address
->m_family
= GSOCK_NOFAMILY
;
1442 address
->m_addr
= NULL
;
1448 GAddress
*GAddress_copy(GAddress
*address
)
1452 assert(address
!= NULL
);
1454 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1457 memcpy(addr2
, address
, sizeof(GAddress
));
1459 if (address
->m_addr
&& address
->m_len
> 0)
1461 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
1462 if (addr2
->m_addr
== NULL
)
1467 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1473 void GAddress_destroy(GAddress
*address
)
1475 assert(address
!= NULL
);
1477 if (address
->m_addr
)
1478 free(address
->m_addr
);
1483 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1485 assert(address
!= NULL
);
1487 address
->m_family
= type
;
1490 GAddressType
GAddress_GetFamily(GAddress
*address
)
1492 assert(address
!= NULL
);
1494 return address
->m_family
;
1497 GSocketError
_GAddress_translate_from(GAddress
*address
,
1498 struct sockaddr
*addr
, int len
)
1500 address
->m_realfamily
= addr
->sa_family
;
1501 switch (addr
->sa_family
)
1504 address
->m_family
= GSOCK_INET
;
1507 address
->m_family
= GSOCK_UNIX
;
1511 address
->m_family
= GSOCK_INET6
;
1516 address
->m_error
= GSOCK_INVOP
;
1521 if (address
->m_addr
)
1522 free(address
->m_addr
);
1524 address
->m_len
= len
;
1525 address
->m_addr
= (struct sockaddr
*)malloc(len
);
1527 if (address
->m_addr
== NULL
)
1529 address
->m_error
= GSOCK_MEMERR
;
1530 return GSOCK_MEMERR
;
1532 memcpy(address
->m_addr
, addr
, len
);
1534 return GSOCK_NOERROR
;
1537 GSocketError
_GAddress_translate_to(GAddress
*address
,
1538 struct sockaddr
**addr
, int *len
)
1540 if (!address
->m_addr
)
1542 address
->m_error
= GSOCK_INVADDR
;
1543 return GSOCK_INVADDR
;
1546 *len
= address
->m_len
;
1547 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
1550 address
->m_error
= GSOCK_MEMERR
;
1551 return GSOCK_MEMERR
;
1554 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1555 return GSOCK_NOERROR
;
1559 * -------------------------------------------------------------------------
1560 * Internet address family
1561 * -------------------------------------------------------------------------
1564 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1566 address
->m_len
= sizeof(struct sockaddr_in
);
1567 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1568 if (address
->m_addr
== NULL
)
1570 address
->m_error
= GSOCK_MEMERR
;
1571 return GSOCK_MEMERR
;
1574 address
->m_family
= GSOCK_INET
;
1575 address
->m_realfamily
= PF_INET
;
1576 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1577 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1579 return GSOCK_NOERROR
;
1582 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1585 struct in_addr
*addr
;
1587 assert(address
!= NULL
);
1589 CHECK_ADDRESS(address
, INET
);
1591 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1593 /* If it is a numeric host name, convert it now */
1594 #if defined(HAVE_INET_ATON)
1595 if (inet_aton(hostname
, addr
) == 0)
1597 #elif defined(HAVE_INET_ADDR)
1598 if ( (addr
->s_addr
= inet_addr(hostname
)) == -1 )
1601 /* Use gethostbyname by default */
1602 int val
= 1; //VA doesn't like constants in conditional expressions at all
1606 struct in_addr
*array_addr
;
1608 /* It is a real name, we solve it */
1609 if ((he
= gethostbyname(hostname
)) == NULL
)
1611 /* Reset to invalid address */
1612 addr
->s_addr
= INADDR_NONE
;
1613 address
->m_error
= GSOCK_NOHOST
;
1614 return GSOCK_NOHOST
;
1616 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1617 addr
->s_addr
= array_addr
[0].s_addr
;
1619 return GSOCK_NOERROR
;
1622 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1624 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1627 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1628 unsigned long hostaddr
)
1630 struct in_addr
*addr
;
1632 assert(address
!= NULL
);
1634 CHECK_ADDRESS(address
, INET
);
1636 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1637 addr
->s_addr
= hostaddr
;
1639 return GSOCK_NOERROR
;
1642 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1643 const char *protocol
)
1646 struct sockaddr_in
*addr
;
1648 assert(address
!= NULL
);
1649 CHECK_ADDRESS(address
, INET
);
1653 address
->m_error
= GSOCK_INVPORT
;
1654 return GSOCK_INVPORT
;
1657 se
= getservbyname(port
, protocol
);
1660 /* the cast to int suppresses compiler warnings about subscript having the
1662 if (isdigit((int)port
[0]))
1666 port_int
= atoi(port
);
1667 addr
= (struct sockaddr_in
*)address
->m_addr
;
1668 addr
->sin_port
= htons(port_int
);
1669 return GSOCK_NOERROR
;
1672 address
->m_error
= GSOCK_INVPORT
;
1673 return GSOCK_INVPORT
;
1676 addr
= (struct sockaddr_in
*)address
->m_addr
;
1677 addr
->sin_port
= se
->s_port
;
1679 return GSOCK_NOERROR
;
1682 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1684 struct sockaddr_in
*addr
;
1686 assert(address
!= NULL
);
1687 CHECK_ADDRESS(address
, INET
);
1689 addr
= (struct sockaddr_in
*)address
->m_addr
;
1690 addr
->sin_port
= htons(port
);
1692 return GSOCK_NOERROR
;
1695 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1699 struct sockaddr_in
*addr
;
1701 assert(address
!= NULL
);
1702 CHECK_ADDRESS(address
, INET
);
1704 addr
= (struct sockaddr_in
*)address
->m_addr
;
1705 addr_buf
= (char *)&(addr
->sin_addr
);
1707 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1710 address
->m_error
= GSOCK_NOHOST
;
1711 return GSOCK_NOHOST
;
1714 strncpy(hostname
, he
->h_name
, sbuf
);
1716 return GSOCK_NOERROR
;
1719 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1721 struct sockaddr_in
*addr
;
1723 assert(address
!= NULL
);
1724 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1726 addr
= (struct sockaddr_in
*)address
->m_addr
;
1728 return addr
->sin_addr
.s_addr
;
1731 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1733 struct sockaddr_in
*addr
;
1735 assert(address
!= NULL
);
1736 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1738 addr
= (struct sockaddr_in
*)address
->m_addr
;
1739 return ntohs(addr
->sin_port
);
1743 * -------------------------------------------------------------------------
1744 * Unix address family
1745 * -------------------------------------------------------------------------
1748 #ifndef __VISAGECPP__
1749 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1751 address
->m_len
= sizeof(struct sockaddr_un
);
1752 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
1753 if (address
->m_addr
== NULL
)
1755 address
->m_error
= GSOCK_MEMERR
;
1756 return GSOCK_MEMERR
;
1759 address
->m_family
= GSOCK_UNIX
;
1760 address
->m_realfamily
= PF_UNIX
;
1761 ((struct sockaddr_un
*)address
->m_addr
)->sun_family
= AF_UNIX
;
1762 ((struct sockaddr_un
*)address
->m_addr
)->sun_path
[0] = 0;
1764 return GSOCK_NOERROR
;
1767 #define UNIX_SOCK_PATHLEN (sizeof(addr->sun_path)/sizeof(addr->sun_path[0]))
1769 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1771 struct sockaddr_un
*addr
;
1773 assert(address
!= NULL
);
1775 CHECK_ADDRESS(address
, UNIX
);
1777 addr
= ((struct sockaddr_un
*)address
->m_addr
);
1778 strncpy(addr
->sun_path
, path
, UNIX_SOCK_PATHLEN
);
1779 addr
->sun_path
[UNIX_SOCK_PATHLEN
- 1] = '\0';
1781 return GSOCK_NOERROR
;
1784 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1786 struct sockaddr_un
*addr
;
1788 assert(address
!= NULL
);
1789 CHECK_ADDRESS(address
, UNIX
);
1791 addr
= (struct sockaddr_un
*)address
->m_addr
;
1793 strncpy(path
, addr
->sun_path
, sbuf
);
1795 return GSOCK_NOERROR
;
1797 #endif /* !defined(__VISAGECPP__) */
1798 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */