1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket) for WX
4 * Copyright: (c) Guilhem Lavaux
5 * Licence: wxWindows Licence
6 * Authors: David Elliott (C++ conversion, maintainer)
8 * Guillermo Rodriguez Garcia <guille@iies.es>
9 * Purpose: GSocket main Unix and OS/2 file
10 * Licence: The wxWindows licence
12 * -------------------------------------------------------------------------
15 #if defined(__WATCOMC__)
16 #include "wx/wxprec.h"
21 #ifndef __GSOCKET_STANDALONE__
25 #if defined(__VISAGECPP__)
26 #define BSD_SELECT /* use Berkeley Sockets select */
29 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
32 #include <sys/types.h>
37 #include <netinet/in.h>
40 #include <sys/ioctl.h>
46 u_char sun_len
; /* sockaddr len including null */
47 u_char sun_family
; /* AF_UNIX */
48 char sun_path
[108]; /* path name (gag) */
51 #include <sys/socket.h>
57 #include <netinet/in.h>
58 #include <arpa/inet.h>
65 #include <machine/endian.h>
71 #define EBADF SOCEBADF
77 #include <sys/socket.h>
78 #include <sys/ioctl.h>
79 #include <sys/select.h>
81 #define close(a) soclose(a)
82 #define select(a,b,c,d,e) bsdselect(a,b,c,d,e)
83 int _System
bsdselect(int,
88 int _System
soclose(int);
92 #include <sys/select.h>
100 # include <sys/filio.h>
103 # include <bstring.h>
106 # include <strings.h>
113 # define WX_SOCKLEN_T unsigned int
117 # define WX_SOCKLEN_T socklen_t
119 # elif defined(__WXMAC__)
120 # define WX_SOCKLEN_T socklen_t
122 # define WX_SOCKLEN_T int
126 #endif /* SOCKLEN_T */
129 #define SOCKOPTLEN_T WX_SOCKLEN_T
133 * MSW defines this, Unices don't.
135 #ifndef INVALID_SOCKET
136 #define INVALID_SOCKET -1
139 /* UnixWare reportedly needs this for FIONBIO definition */
141 #include <sys/filio.h>
145 * INADDR_BROADCAST is identical to INADDR_NONE which is not defined
146 * on all systems. INADDR_BROADCAST should be fine to indicate an error.
149 #define INADDR_NONE INADDR_BROADCAST
152 #if defined(__VISAGECPP__) || defined(__WATCOMC__)
154 #define MASK_SIGNAL() {
155 #define UNMASK_SIGNAL() }
158 extern "C" { typedef void (*wxSigHandler
)(int); }
160 #define MASK_SIGNAL() \
162 wxSigHandler old_handler = signal(SIGPIPE, SIG_IGN);
164 #define UNMASK_SIGNAL() \
165 signal(SIGPIPE, old_handler); \
170 /* If a SIGPIPE is issued by a socket call on a remotely closed socket,
171 the program will "crash" unless it explicitly handles the SIGPIPE.
172 By using MSG_NOSIGNAL, the SIGPIPE is suppressed. Later, we will
173 use SO_NOSIGPIPE (if available), the BSD equivalent. */
175 # define GSOCKET_MSG_NOSIGNAL MSG_NOSIGNAL
176 #else /* MSG_NOSIGNAL not available (FreeBSD including OS X) */
177 # define GSOCKET_MSG_NOSIGNAL 0
178 #endif /* MSG_NOSIGNAL */
180 #ifndef __GSOCKET_STANDALONE__
181 # include "wx/unix/gsockunx.h"
182 # include "wx/unix/private.h"
183 # include "wx/gsocket.h"
185 # include "gsockunx.h"
186 # include "gsocket.h"
190 #endif /* __GSOCKET_STANDALONE__ */
192 /* debugging helpers */
193 #ifdef __GSOCKET_DEBUG__
194 # define GSocket_Debug(args) printf args
196 # define GSocket_Debug(args)
197 #endif /* __GSOCKET_DEBUG__ */
199 /* Table of GUI-related functions. We must call them indirectly because
200 * of wxBase and GUI separation: */
202 static GSocketGUIFunctionsTable
*gs_gui_functions
;
204 class GSocketGUIFunctionsTableNull
: public GSocketGUIFunctionsTable
207 virtual bool OnInit();
208 virtual void OnExit();
209 virtual bool CanUseEventLoop();
210 virtual bool Init_Socket(GSocket
*socket
);
211 virtual void Destroy_Socket(GSocket
*socket
);
212 virtual void Install_Callback(GSocket
*socket
, GSocketEvent event
);
213 virtual void Uninstall_Callback(GSocket
*socket
, GSocketEvent event
);
214 virtual void Enable_Events(GSocket
*socket
);
215 virtual void Disable_Events(GSocket
*socket
);
218 bool GSocketGUIFunctionsTableNull::OnInit()
220 void GSocketGUIFunctionsTableNull::OnExit()
222 bool GSocketGUIFunctionsTableNull::CanUseEventLoop()
224 bool GSocketGUIFunctionsTableNull::Init_Socket(GSocket
*WXUNUSED(socket
))
226 void GSocketGUIFunctionsTableNull::Destroy_Socket(GSocket
*WXUNUSED(socket
))
228 void GSocketGUIFunctionsTableNull::Install_Callback(GSocket
*WXUNUSED(socket
), GSocketEvent
WXUNUSED(event
))
230 void GSocketGUIFunctionsTableNull::Uninstall_Callback(GSocket
*WXUNUSED(socket
), GSocketEvent
WXUNUSED(event
))
232 void GSocketGUIFunctionsTableNull::Enable_Events(GSocket
*WXUNUSED(socket
))
234 void GSocketGUIFunctionsTableNull::Disable_Events(GSocket
*WXUNUSED(socket
))
236 /* Global initialisers */
238 void GSocket_SetGUIFunctions(GSocketGUIFunctionsTable
*guifunc
)
240 gs_gui_functions
= guifunc
;
243 int GSocket_Init(void)
245 if (!gs_gui_functions
)
247 static GSocketGUIFunctionsTableNull table
;
248 gs_gui_functions
= &table
;
250 if ( !gs_gui_functions
->OnInit() )
255 void GSocket_Cleanup(void)
257 if (gs_gui_functions
)
259 gs_gui_functions
->OnExit();
263 /* Constructors / Destructors for GSocket */
269 m_fd
= INVALID_SOCKET
;
270 for (i
=0;i
<GSOCK_MAX_EVENT
;i
++)
277 m_error
= GSOCK_NOERROR
;
280 m_gui_dependent
= NULL
;
281 m_non_blocking
= false;
283 m_timeout
= 10*60*1000;
284 /* 10 minutes * 60 sec * 1000 millisec */
285 m_establishing
= false;
287 assert(gs_gui_functions
);
288 /* Per-socket GUI-specific initialization */
289 m_ok
= gs_gui_functions
->Init_Socket(this);
292 void GSocket::Close()
294 gs_gui_functions
->Disable_Events(this);
295 /* gsockosx.c calls CFSocketInvalidate which closes the socket for us */
296 #if !(defined(__DARWIN__) && (defined(__WXMAC__) || defined(__WXCOCOA__)))
299 m_fd
= INVALID_SOCKET
;
306 /* Check that the socket is really shutdowned */
307 if (m_fd
!= INVALID_SOCKET
)
310 /* Per-socket GUI-specific cleanup */
311 gs_gui_functions
->Destroy_Socket(this);
313 /* Destroy private addresses */
315 GAddress_destroy(m_local
);
318 GAddress_destroy(m_peer
);
322 * Disallow further read/write operations on this socket, close
323 * the fd and disable all callbacks.
325 void GSocket::Shutdown()
331 /* Don't allow events to fire after socket has been closed */
332 gs_gui_functions
->Disable_Events(this);
334 /* If socket has been created, shutdown it */
335 if (m_fd
!= INVALID_SOCKET
)
341 /* Disable GUI callbacks */
342 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
343 m_cbacks
[evt
] = NULL
;
345 m_detected
= GSOCK_LOST_FLAG
;
348 /* Address handling */
354 * Set or get the local or peer address for this socket. The 'set'
355 * functions return GSOCK_NOERROR on success, an error code otherwise.
356 * The 'get' functions return a pointer to a GAddress object on success,
357 * or NULL otherwise, in which case they set the error code of the
358 * corresponding GSocket.
361 * GSOCK_INVSOCK - the socket is not valid.
362 * GSOCK_INVADDR - the address is not valid.
364 GSocketError
GSocket::SetLocal(GAddress
*address
)
368 /* the socket must be initialized, or it must be a server */
369 if ((m_fd
!= INVALID_SOCKET
&& !m_server
))
371 m_error
= GSOCK_INVSOCK
;
372 return GSOCK_INVSOCK
;
376 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
378 m_error
= GSOCK_INVADDR
;
379 return GSOCK_INVADDR
;
383 GAddress_destroy(m_local
);
385 m_local
= GAddress_copy(address
);
387 return GSOCK_NOERROR
;
390 GSocketError
GSocket::SetPeer(GAddress
*address
)
395 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
397 m_error
= GSOCK_INVADDR
;
398 return GSOCK_INVADDR
;
402 GAddress_destroy(m_peer
);
404 m_peer
= GAddress_copy(address
);
406 return GSOCK_NOERROR
;
409 GAddress
*GSocket::GetLocal()
412 struct sockaddr addr
;
413 WX_SOCKLEN_T size
= sizeof(addr
);
418 /* try to get it from the m_local var first */
420 return GAddress_copy(m_local
);
422 /* else, if the socket is initialized, try getsockname */
423 if (m_fd
== INVALID_SOCKET
)
425 m_error
= GSOCK_INVSOCK
;
429 if (getsockname(m_fd
, &addr
, (WX_SOCKLEN_T
*) &size
) < 0)
431 m_error
= GSOCK_IOERR
;
435 /* got a valid address from getsockname, create a GAddress object */
436 address
= GAddress_new();
439 m_error
= GSOCK_MEMERR
;
443 err
= _GAddress_translate_from(address
, &addr
, size
);
444 if (err
!= GSOCK_NOERROR
)
446 GAddress_destroy(address
);
454 GAddress
*GSocket::GetPeer()
458 /* try to get it from the m_peer var */
460 return GAddress_copy(m_peer
);
465 /* Server specific parts */
467 /* GSocket_SetServer:
468 * Sets up this socket as a server. The local address must have been
469 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
470 * Returns GSOCK_NOERROR on success, one of the following otherwise:
473 * GSOCK_INVSOCK - the socket is in use.
474 * GSOCK_INVADDR - the local address has not been set.
475 * GSOCK_IOERR - low-level error.
477 GSocketError
GSocket::SetServer()
483 /* must not be in use */
484 if (m_fd
!= INVALID_SOCKET
)
486 m_error
= GSOCK_INVSOCK
;
487 return GSOCK_INVSOCK
;
490 /* the local addr must have been set */
493 m_error
= GSOCK_INVADDR
;
494 return GSOCK_INVADDR
;
497 /* Initialize all fields */
501 /* Create the socket */
502 m_fd
= socket(m_local
->m_realfamily
, SOCK_STREAM
, 0);
504 if (m_fd
== INVALID_SOCKET
)
506 m_error
= GSOCK_IOERR
;
510 /* FreeBSD variants can't use MSG_NOSIGNAL, and instead use a socket option */
512 setsockopt(m_fd
, SOL_SOCKET
, SO_NOSIGPIPE
, (const char*)&arg
, sizeof(u_long
));
515 ioctl(m_fd
, FIONBIO
, &arg
);
516 gs_gui_functions
->Enable_Events(this);
518 /* allow a socket to re-bind if the socket is in the TIME_WAIT
519 state after being previously closed.
522 setsockopt(m_fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char*)&arg
, sizeof(u_long
));
524 /* Bind to the local address,
525 * retrieve the actual address bound,
526 * and listen up to 5 connections.
528 if ((bind(m_fd
, m_local
->m_addr
, m_local
->m_len
) != 0) ||
531 (WX_SOCKLEN_T
*) &m_local
->m_len
) != 0) ||
532 (listen(m_fd
, 5) != 0))
535 m_error
= GSOCK_IOERR
;
539 return GSOCK_NOERROR
;
542 /* GSocket_WaitConnection:
543 * Waits for an incoming client connection. Returns a pointer to
544 * a GSocket object, or NULL if there was an error, in which case
545 * the last error field will be updated for the calling GSocket.
547 * Error codes (set in the calling GSocket)
548 * GSOCK_INVSOCK - the socket is not valid or not a server.
549 * GSOCK_TIMEDOUT - timeout, no incoming connections.
550 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
551 * GSOCK_MEMERR - couldn't allocate memory.
552 * GSOCK_IOERR - low-level error.
554 GSocket
*GSocket::WaitConnection()
556 struct sockaddr from
;
557 WX_SOCKLEN_T fromlen
= sizeof(from
);
564 /* If the socket has already been created, we exit immediately */
565 if (m_fd
== INVALID_SOCKET
|| !m_server
)
567 m_error
= GSOCK_INVSOCK
;
571 /* Create a GSocket object for the new connection */
572 connection
= GSocket_new();
576 m_error
= GSOCK_MEMERR
;
580 /* Wait for a connection (with timeout) */
581 if (Input_Timeout() == GSOCK_TIMEDOUT
)
584 /* m_error set by _GSocket_Input_Timeout */
588 connection
->m_fd
= accept(m_fd
, &from
, (WX_SOCKLEN_T
*) &fromlen
);
590 /* Reenable CONNECTION events */
591 Enable(GSOCK_CONNECTION
);
593 if (connection
->m_fd
== INVALID_SOCKET
)
595 if (errno
== EWOULDBLOCK
)
596 m_error
= GSOCK_WOULDBLOCK
;
598 m_error
= GSOCK_IOERR
;
604 /* Initialize all fields */
605 connection
->m_server
= false;
606 connection
->m_stream
= true;
608 /* Setup the peer address field */
609 connection
->m_peer
= GAddress_new();
610 if (!connection
->m_peer
)
613 m_error
= GSOCK_MEMERR
;
617 err
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
);
618 if (err
!= GSOCK_NOERROR
)
625 #if defined(__EMX__) || defined(__VISAGECPP__)
626 ioctl(connection
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
628 ioctl(connection
->m_fd
, FIONBIO
, &arg
);
630 gs_gui_functions
->Enable_Events(connection
);
635 bool GSocket::SetReusable()
637 /* socket must not be null, and must not be in use/already bound */
638 if (this && m_fd
== INVALID_SOCKET
)
648 /* Client specific parts */
651 * For stream (connection oriented) sockets, GSocket_Connect() tries
652 * to establish a client connection to a server using the peer address
653 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
654 * connection has been successfully established, or one of the error
655 * codes listed below. Note that for nonblocking sockets, a return
656 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
657 * request can be completed later; you should use GSocket_Select()
658 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
659 * corresponding asynchronous events.
661 * For datagram (non connection oriented) sockets, GSocket_Connect()
662 * just sets the peer address established with GSocket_SetPeer() as
663 * default destination.
666 * GSOCK_INVSOCK - the socket is in use or not valid.
667 * GSOCK_INVADDR - the peer address has not been established.
668 * GSOCK_TIMEDOUT - timeout, the connection failed.
669 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
670 * GSOCK_MEMERR - couldn't allocate memory.
671 * GSOCK_IOERR - low-level error.
673 GSocketError
GSocket::Connect(GSocketStream stream
)
680 /* Enable CONNECTION events (needed for nonblocking connections) */
681 Enable(GSOCK_CONNECTION
);
683 if (m_fd
!= INVALID_SOCKET
)
685 m_error
= GSOCK_INVSOCK
;
686 return GSOCK_INVSOCK
;
691 m_error
= GSOCK_INVADDR
;
692 return GSOCK_INVADDR
;
695 /* Streamed or dgram socket? */
696 m_stream
= (stream
== GSOCK_STREAMED
);
698 m_establishing
= false;
700 /* Create the socket */
701 m_fd
= socket(m_peer
->m_realfamily
,
702 m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
704 if (m_fd
== INVALID_SOCKET
)
706 m_error
= GSOCK_IOERR
;
710 /* FreeBSD variants can't use MSG_NOSIGNAL, and instead use a socket option */
712 setsockopt(m_fd
, SOL_SOCKET
, SO_NOSIGPIPE
, (const char*)&arg
, sizeof(u_long
));
715 #if defined(__EMX__) || defined(__VISAGECPP__)
716 ioctl(m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
718 ioctl(m_fd
, FIONBIO
, &arg
);
721 /* Connect it to the peer address, with a timeout (see below) */
722 ret
= connect(m_fd
, m_peer
->m_addr
, m_peer
->m_len
);
724 /* We only call Enable_Events if we know we aren't shutting down the socket.
725 * NB: Enable_Events needs to be called whether the socket is blocking or
726 * non-blocking, it just shouldn't be called prior to knowing there is a
727 * connection _if_ blocking sockets are being used.
728 * If connect above returns 0, we are already connected and need to make the
729 * call to Enable_Events now.
732 if (m_non_blocking
|| ret
== 0)
733 gs_gui_functions
->Enable_Events(this);
739 /* If connect failed with EINPROGRESS and the GSocket object
740 * is in blocking mode, we select() for the specified timeout
741 * checking for writability to see if the connection request
744 if ((err
== EINPROGRESS
) && (!m_non_blocking
))
746 if (Output_Timeout() == GSOCK_TIMEDOUT
)
749 /* m_error is set in _GSocket_Output_Timeout */
750 return GSOCK_TIMEDOUT
;
755 SOCKOPTLEN_T len
= sizeof(error
);
757 getsockopt(m_fd
, SOL_SOCKET
, SO_ERROR
, (char*) &error
, &len
);
759 gs_gui_functions
->Enable_Events(this);
762 return GSOCK_NOERROR
;
766 /* If connect failed with EINPROGRESS and the GSocket object
767 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
768 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
769 * this way if the connection completes, a GSOCK_CONNECTION
770 * event will be generated, if enabled.
772 if ((err
== EINPROGRESS
) && (m_non_blocking
))
774 m_establishing
= true;
775 m_error
= GSOCK_WOULDBLOCK
;
776 return GSOCK_WOULDBLOCK
;
779 /* If connect failed with an error other than EINPROGRESS,
780 * then the call to GSocket_Connect has failed.
783 m_error
= GSOCK_IOERR
;
788 return GSOCK_NOERROR
;
791 /* Datagram sockets */
793 /* GSocket_SetNonOriented:
794 * Sets up this socket as a non-connection oriented (datagram) socket.
795 * Before using this function, the local address must have been set
796 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
797 * on success, or one of the following otherwise.
800 * GSOCK_INVSOCK - the socket is in use.
801 * GSOCK_INVADDR - the local address has not been set.
802 * GSOCK_IOERR - low-level error.
804 GSocketError
GSocket::SetNonOriented()
810 if (m_fd
!= INVALID_SOCKET
)
812 m_error
= GSOCK_INVSOCK
;
813 return GSOCK_INVSOCK
;
818 m_error
= GSOCK_INVADDR
;
819 return GSOCK_INVADDR
;
822 /* Initialize all fields */
826 /* Create the socket */
827 m_fd
= socket(m_local
->m_realfamily
, SOCK_DGRAM
, 0);
829 if (m_fd
== INVALID_SOCKET
)
831 m_error
= GSOCK_IOERR
;
834 #if defined(__EMX__) || defined(__VISAGECPP__)
835 ioctl(m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
837 ioctl(m_fd
, FIONBIO
, &arg
);
839 gs_gui_functions
->Enable_Events(this);
841 /* Bind to the local address,
842 * and retrieve the actual address bound.
844 if ((bind(m_fd
, m_local
->m_addr
, m_local
->m_len
) != 0) ||
847 (WX_SOCKLEN_T
*) &m_local
->m_len
) != 0))
850 m_error
= GSOCK_IOERR
;
854 return GSOCK_NOERROR
;
859 /* Like recv(), send(), ... */
860 int GSocket::Read(char *buffer
, int size
)
866 if (m_fd
== INVALID_SOCKET
|| m_server
)
868 m_error
= GSOCK_INVSOCK
;
872 /* Disable events during query of socket status */
873 Disable(GSOCK_INPUT
);
875 /* If the socket is blocking, wait for data (with a timeout) */
876 if (Input_Timeout() == GSOCK_TIMEDOUT
) {
877 m_error
= GSOCK_TIMEDOUT
;
878 /* Don't return here immediately, otherwise socket events would not be
886 ret
= Recv_Stream(buffer
, size
);
888 ret
= Recv_Dgram(buffer
, size
);
890 /* If recv returned zero, then the connection is lost, and errno is not set.
891 * Otherwise, recv has returned an error (-1), in which case we have lost the
892 * socket only if errno does _not_ indicate that there may be more data to read.
895 m_error
= GSOCK_IOERR
;
898 if ((errno
== EWOULDBLOCK
) || (errno
== EAGAIN
))
899 m_error
= GSOCK_WOULDBLOCK
;
901 m_error
= GSOCK_IOERR
;
905 /* Enable events again now that we are done processing */
911 int GSocket::Write(const char *buffer
, int size
)
917 GSocket_Debug(( "GSocket_Write #1, size %d\n", size
));
919 if (m_fd
== INVALID_SOCKET
|| m_server
)
921 m_error
= GSOCK_INVSOCK
;
925 GSocket_Debug(( "GSocket_Write #2, size %d\n", size
));
927 /* If the socket is blocking, wait for writability (with a timeout) */
928 if (Output_Timeout() == GSOCK_TIMEDOUT
)
931 GSocket_Debug(( "GSocket_Write #3, size %d\n", size
));
935 ret
= Send_Stream(buffer
, size
);
937 ret
= Send_Dgram(buffer
, size
);
939 GSocket_Debug(( "GSocket_Write #4, size %d\n", size
));
943 if ((errno
== EWOULDBLOCK
) || (errno
== EAGAIN
))
945 m_error
= GSOCK_WOULDBLOCK
;
946 GSocket_Debug(( "GSocket_Write error WOULDBLOCK\n" ));
950 m_error
= GSOCK_IOERR
;
951 GSocket_Debug(( "GSocket_Write error IOERR\n" ));
954 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
955 * in MSW). Once the first OUTPUT event is received, users can assume
956 * that the socket is writable until a read operation fails. Only then
957 * will further OUTPUT events be posted.
959 Enable(GSOCK_OUTPUT
);
964 GSocket_Debug(( "GSocket_Write #5, size %d ret %d\n", size
, ret
));
970 * Polls the socket to determine its status. This function will
971 * check for the events specified in the 'flags' parameter, and
972 * it will return a mask indicating which operations can be
973 * performed. This function won't block, regardless of the
974 * mode (blocking | nonblocking) of the socket.
976 GSocketEventFlags
GSocket::Select(GSocketEventFlags flags
)
978 if (!gs_gui_functions
->CanUseEventLoop())
981 GSocketEventFlags result
= 0;
990 return (GSOCK_LOST_FLAG
& flags
);
992 /* Do not use a static struct, Linux can garble it */
993 tv
.tv_sec
= m_timeout
/ 1000;
994 tv
.tv_usec
= (m_timeout
% 1000) * 1000;
997 wxFD_ZERO(&writefds
);
998 wxFD_ZERO(&exceptfds
);
999 wxFD_SET(m_fd
, &readfds
);
1000 if (flags
& GSOCK_OUTPUT_FLAG
|| flags
& GSOCK_CONNECTION_FLAG
)
1001 wxFD_SET(m_fd
, &writefds
);
1002 wxFD_SET(m_fd
, &exceptfds
);
1004 /* Check 'sticky' CONNECTION flag first */
1005 result
|= (GSOCK_CONNECTION_FLAG
& m_detected
);
1007 /* If we have already detected a LOST event, then don't try
1008 * to do any further processing.
1010 if ((m_detected
& GSOCK_LOST_FLAG
) != 0)
1012 m_establishing
= false;
1014 return (GSOCK_LOST_FLAG
& flags
);
1017 /* Try select now */
1018 if (select(m_fd
+ 1, &readfds
, &writefds
, &exceptfds
, &tv
) <= 0)
1020 /* What to do here? */
1021 return (result
& flags
);
1024 /* Check for readability */
1025 if (wxFD_ISSET(m_fd
, &readfds
))
1029 int num
= recv(m_fd
, &c
, 1, MSG_PEEK
| GSOCKET_MSG_NOSIGNAL
);
1033 result
|= GSOCK_INPUT_FLAG
;
1037 if (m_server
&& m_stream
)
1039 result
|= GSOCK_CONNECTION_FLAG
;
1040 m_detected
|= GSOCK_CONNECTION_FLAG
;
1042 /* If recv returned zero, then the connection is lost, and errno is not set.
1043 * Otherwise, recv has returned an error (-1), in which case we have lost the
1044 * socket only if errno does _not_ indicate that there may be more data to read.
1046 else if (num
== 0 ||
1047 (errno
!= EWOULDBLOCK
) && (errno
!= EAGAIN
) && (errno
!= EINTR
))
1049 m_detected
= GSOCK_LOST_FLAG
;
1050 m_establishing
= false;
1052 /* LOST event: Abort any further processing */
1053 return (GSOCK_LOST_FLAG
& flags
);
1058 /* Check for writability */
1059 if (wxFD_ISSET(m_fd
, &writefds
))
1061 if (m_establishing
&& !m_server
)
1064 SOCKOPTLEN_T len
= sizeof(error
);
1066 m_establishing
= false;
1068 getsockopt(m_fd
, SOL_SOCKET
, SO_ERROR
, (char*)&error
, &len
);
1072 m_detected
= GSOCK_LOST_FLAG
;
1074 /* LOST event: Abort any further processing */
1075 return (GSOCK_LOST_FLAG
& flags
);
1079 result
|= GSOCK_CONNECTION_FLAG
;
1080 m_detected
|= GSOCK_CONNECTION_FLAG
;
1085 result
|= GSOCK_OUTPUT_FLAG
;
1089 /* Check for exceptions and errors (is this useful in Unices?) */
1090 if (wxFD_ISSET(m_fd
, &exceptfds
))
1092 m_establishing
= false;
1093 m_detected
= GSOCK_LOST_FLAG
;
1095 /* LOST event: Abort any further processing */
1096 return (GSOCK_LOST_FLAG
& flags
);
1099 return (result
& flags
);
1105 return flags
& m_detected
;
1111 /* GSocket_SetNonBlocking:
1112 * Sets the socket to non-blocking mode. All IO calls will return
1115 void GSocket::SetNonBlocking(bool non_block
)
1119 GSocket_Debug( ("GSocket_SetNonBlocking: %d\n", (int)non_block
) );
1121 m_non_blocking
= non_block
;
1124 /* GSocket_SetTimeout:
1125 * Sets the timeout for blocking calls. Time is expressed in
1128 void GSocket::SetTimeout(unsigned long millisec
)
1132 m_timeout
= millisec
;
1135 /* GSocket_GetError:
1136 * Returns the last error occurred for this socket. Note that successful
1137 * operations do not clear this back to GSOCK_NOERROR, so use it only
1140 GSocketError WXDLLIMPEXP_NET
GSocket::GetError()
1150 * There is data to be read in the input buffer. If, after a read
1151 * operation, there is still data available, the callback function will
1154 * The socket is available for writing. That is, the next write call
1155 * won't block. This event is generated only once, when the connection is
1156 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
1157 * when the output buffer empties again. This means that the app should
1158 * assume that it can write since the first OUTPUT event, and no more
1159 * OUTPUT events will be generated unless an error occurs.
1161 * Connection successfully established, for client sockets, or incoming
1162 * client connection, for server sockets. Wait for this event (also watch
1163 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
1165 * The connection is lost (or a connection request failed); this could
1166 * be due to a failure, or due to the peer closing it gracefully.
1169 /* GSocket_SetCallback:
1170 * Enables the callbacks specified by 'flags'. Note that 'flags'
1171 * may be a combination of flags OR'ed toghether, so the same
1172 * callback function can be made to accept different events.
1173 * The callback function must have the following prototype:
1175 * void function(GSocket *socket, GSocketEvent event, char *cdata)
1177 void GSocket::SetCallback(GSocketEventFlags flags
,
1178 GSocketCallback callback
, char *cdata
)
1184 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
1186 if ((flags
& (1 << count
)) != 0)
1188 m_cbacks
[count
] = callback
;
1189 m_data
[count
] = cdata
;
1194 /* GSocket_UnsetCallback:
1195 * Disables all callbacks specified by 'flags', which may be a
1196 * combination of flags OR'ed toghether.
1198 void GSocket::UnsetCallback(GSocketEventFlags flags
)
1204 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
1206 if ((flags
& (1 << count
)) != 0)
1208 m_cbacks
[count
] = NULL
;
1209 m_data
[count
] = NULL
;
1214 GSocketError
GSocket::GetSockOpt(int level
, int optname
,
1215 void *optval
, int *optlen
)
1217 if (getsockopt(m_fd
, level
, optname
, (char*)optval
, (SOCKOPTLEN_T
*)optlen
) == 0)
1218 return GSOCK_NOERROR
;
1220 return GSOCK_OPTERR
;
1223 GSocketError
GSocket::SetSockOpt(int level
, int optname
,
1224 const void *optval
, int optlen
)
1226 if (setsockopt(m_fd
, level
, optname
, (const char*)optval
, optlen
) == 0)
1227 return GSOCK_NOERROR
;
1229 return GSOCK_OPTERR
;
1232 #define CALL_CALLBACK(socket, event) { \
1233 socket->Disable(event); \
1234 if (socket->m_cbacks[event]) \
1235 socket->m_cbacks[event](socket, event, socket->m_data[event]); \
1239 void GSocket::Enable(GSocketEvent event
)
1241 m_detected
&= ~(1 << event
);
1242 gs_gui_functions
->Install_Callback(this, event
);
1245 void GSocket::Disable(GSocketEvent event
)
1247 m_detected
|= (1 << event
);
1248 gs_gui_functions
->Uninstall_Callback(this, event
);
1251 /* _GSocket_Input_Timeout:
1252 * For blocking sockets, wait until data is available or
1253 * until timeout ellapses.
1255 GSocketError
GSocket::Input_Timeout()
1261 /* Linux select() will overwrite the struct on return */
1262 tv
.tv_sec
= (m_timeout
/ 1000);
1263 tv
.tv_usec
= (m_timeout
% 1000) * 1000;
1265 if (!m_non_blocking
)
1267 wxFD_ZERO(&readfds
);
1268 wxFD_SET(m_fd
, &readfds
);
1269 ret
= select(m_fd
+ 1, &readfds
, NULL
, NULL
, &tv
);
1272 GSocket_Debug(( "GSocket_Input_Timeout, select returned 0\n" ));
1273 m_error
= GSOCK_TIMEDOUT
;
1274 return GSOCK_TIMEDOUT
;
1279 GSocket_Debug(( "GSocket_Input_Timeout, select returned -1\n" ));
1280 if (errno
== EBADF
) { GSocket_Debug(( "Invalid file descriptor\n" )); }
1281 if (errno
== EINTR
) { GSocket_Debug(( "A non blocked signal was caught\n" )); }
1282 if (errno
== EINVAL
) { GSocket_Debug(( "The highest number descriptor is negative\n" )); }
1283 if (errno
== ENOMEM
) { GSocket_Debug(( "Not enough memory\n" )); }
1284 m_error
= GSOCK_TIMEDOUT
;
1285 return GSOCK_TIMEDOUT
;
1289 return GSOCK_NOERROR
;
1292 /* _GSocket_Output_Timeout:
1293 * For blocking sockets, wait until data can be sent without
1294 * blocking or until timeout ellapses.
1296 GSocketError
GSocket::Output_Timeout()
1302 /* Linux select() will overwrite the struct on return */
1303 tv
.tv_sec
= (m_timeout
/ 1000);
1304 tv
.tv_usec
= (m_timeout
% 1000) * 1000;
1306 GSocket_Debug( ("m_non_blocking has: %d\n", (int)m_non_blocking
) );
1308 if (!m_non_blocking
)
1310 wxFD_ZERO(&writefds
);
1311 wxFD_SET(m_fd
, &writefds
);
1312 ret
= select(m_fd
+ 1, NULL
, &writefds
, NULL
, &tv
);
1315 GSocket_Debug(( "GSocket_Output_Timeout, select returned 0\n" ));
1316 m_error
= GSOCK_TIMEDOUT
;
1317 return GSOCK_TIMEDOUT
;
1322 GSocket_Debug(( "GSocket_Output_Timeout, select returned -1\n" ));
1323 if (errno
== EBADF
) { GSocket_Debug(( "Invalid file descriptor\n" )); }
1324 if (errno
== EINTR
) { GSocket_Debug(( "A non blocked signal was caught\n" )); }
1325 if (errno
== EINVAL
) { GSocket_Debug(( "The highest number descriptor is negative\n" )); }
1326 if (errno
== ENOMEM
) { GSocket_Debug(( "Not enough memory\n" )); }
1327 m_error
= GSOCK_TIMEDOUT
;
1328 return GSOCK_TIMEDOUT
;
1331 if ( ! wxFD_ISSET(m_fd
, &writefds
) )
1333 GSocket_Debug(( "GSocket_Output_Timeout is buggy!\n" ));
1337 GSocket_Debug(( "GSocket_Output_Timeout seems correct\n" ));
1342 GSocket_Debug(( "GSocket_Output_Timeout, didn't try select!\n" ));
1345 return GSOCK_NOERROR
;
1348 int GSocket::Recv_Stream(char *buffer
, int size
)
1353 ret
= recv(m_fd
, buffer
, size
, GSOCKET_MSG_NOSIGNAL
);
1355 while (ret
== -1 && errno
== EINTR
); /* Loop until not interrupted */
1360 int GSocket::Recv_Dgram(char *buffer
, int size
)
1362 struct sockaddr from
;
1363 WX_SOCKLEN_T fromlen
= sizeof(from
);
1367 fromlen
= sizeof(from
);
1371 ret
= recvfrom(m_fd
, buffer
, size
, 0, &from
, (WX_SOCKLEN_T
*) &fromlen
);
1373 while (ret
== -1 && errno
== EINTR
); /* Loop until not interrupted */
1378 /* Translate a system address into a GSocket address */
1381 m_peer
= GAddress_new();
1384 m_error
= GSOCK_MEMERR
;
1389 err
= _GAddress_translate_from(m_peer
, &from
, fromlen
);
1390 if (err
!= GSOCK_NOERROR
)
1392 GAddress_destroy(m_peer
);
1401 int GSocket::Send_Stream(const char *buffer
, int size
)
1409 ret
= send(m_fd
, (char *)buffer
, size
, GSOCKET_MSG_NOSIGNAL
);
1411 while (ret
== -1 && errno
== EINTR
); /* Loop until not interrupted */
1418 int GSocket::Send_Dgram(const char *buffer
, int size
)
1420 struct sockaddr
*addr
;
1426 m_error
= GSOCK_INVADDR
;
1430 err
= _GAddress_translate_to(m_peer
, &addr
, &len
);
1431 if (err
!= GSOCK_NOERROR
)
1441 ret
= sendto(m_fd
, (char *)buffer
, size
, 0, addr
, len
);
1443 while (ret
== -1 && errno
== EINTR
); /* Loop until not interrupted */
1447 /* Frees memory allocated from _GAddress_translate_to */
1453 void GSocket::Detected_Read()
1457 /* Safeguard against straggling call to Detected_Read */
1458 if (m_fd
== INVALID_SOCKET
)
1463 /* If we have already detected a LOST event, then don't try
1464 * to do any further processing.
1466 if ((m_detected
& GSOCK_LOST_FLAG
) != 0)
1468 m_establishing
= false;
1470 CALL_CALLBACK(this, GSOCK_LOST
);
1475 int num
= recv(m_fd
, &c
, 1, MSG_PEEK
| GSOCKET_MSG_NOSIGNAL
);
1479 CALL_CALLBACK(this, GSOCK_INPUT
);
1483 if (m_server
&& m_stream
)
1485 CALL_CALLBACK(this, GSOCK_CONNECTION
);
1489 /* Do not throw a lost event in cases where the socket isn't really lost */
1490 if ((errno
== EWOULDBLOCK
) || (errno
== EAGAIN
) || (errno
== EINTR
))
1492 CALL_CALLBACK(this, GSOCK_INPUT
);
1496 CALL_CALLBACK(this, GSOCK_LOST
);
1503 void GSocket::Detected_Write()
1505 /* If we have already detected a LOST event, then don't try
1506 * to do any further processing.
1508 if ((m_detected
& GSOCK_LOST_FLAG
) != 0)
1510 m_establishing
= false;
1512 CALL_CALLBACK(this, GSOCK_LOST
);
1517 if (m_establishing
&& !m_server
)
1520 SOCKOPTLEN_T len
= sizeof(error
);
1522 m_establishing
= false;
1524 getsockopt(m_fd
, SOL_SOCKET
, SO_ERROR
, (char*)&error
, &len
);
1528 CALL_CALLBACK(this, GSOCK_LOST
);
1533 CALL_CALLBACK(this, GSOCK_CONNECTION
);
1534 /* We have to fire this event by hand because CONNECTION (for clients)
1535 * and OUTPUT are internally the same and we just disabled CONNECTION
1536 * events with the above macro.
1538 CALL_CALLBACK(this, GSOCK_OUTPUT
);
1543 CALL_CALLBACK(this, GSOCK_OUTPUT
);
1547 /* Compatibility functions for GSocket */
1548 GSocket
*GSocket_new(void)
1550 GSocket
*newsocket
= new GSocket();
1551 if (newsocket
->IsOk())
1560 * -------------------------------------------------------------------------
1562 * -------------------------------------------------------------------------
1565 /* CHECK_ADDRESS verifies that the current address family is either
1566 * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it
1567 * initalizes it to be a GSOCK_*family*. In other cases, it returns
1568 * an appropiate error code.
1570 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
1572 #define CHECK_ADDRESS(address, family) \
1574 if (address->m_family == GSOCK_NOFAMILY) \
1575 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1576 return address->m_error; \
1577 if (address->m_family != GSOCK_##family) \
1579 address->m_error = GSOCK_INVADDR; \
1580 return GSOCK_INVADDR; \
1584 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
1586 if (address->m_family == GSOCK_NOFAMILY) \
1587 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1589 if (address->m_family != GSOCK_##family) \
1591 address->m_error = GSOCK_INVADDR; \
1597 GAddress
*GAddress_new(void)
1601 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1604 address
->m_family
= GSOCK_NOFAMILY
;
1605 address
->m_addr
= NULL
;
1611 GAddress
*GAddress_copy(GAddress
*address
)
1615 assert(address
!= NULL
);
1617 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1620 memcpy(addr2
, address
, sizeof(GAddress
));
1622 if (address
->m_addr
&& address
->m_len
> 0)
1624 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
1625 if (addr2
->m_addr
== NULL
)
1630 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1636 void GAddress_destroy(GAddress
*address
)
1638 assert(address
!= NULL
);
1640 if (address
->m_addr
)
1641 free(address
->m_addr
);
1646 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1648 assert(address
!= NULL
);
1650 address
->m_family
= type
;
1653 GAddressType
GAddress_GetFamily(GAddress
*address
)
1655 assert(address
!= NULL
);
1657 return address
->m_family
;
1660 GSocketError
_GAddress_translate_from(GAddress
*address
,
1661 struct sockaddr
*addr
, int len
)
1663 address
->m_realfamily
= addr
->sa_family
;
1664 switch (addr
->sa_family
)
1667 address
->m_family
= GSOCK_INET
;
1670 address
->m_family
= GSOCK_UNIX
;
1674 address
->m_family
= GSOCK_INET6
;
1679 address
->m_error
= GSOCK_INVOP
;
1684 if (address
->m_addr
)
1685 free(address
->m_addr
);
1687 address
->m_len
= len
;
1688 address
->m_addr
= (struct sockaddr
*)malloc(len
);
1690 if (address
->m_addr
== NULL
)
1692 address
->m_error
= GSOCK_MEMERR
;
1693 return GSOCK_MEMERR
;
1696 memcpy(address
->m_addr
, addr
, len
);
1698 return GSOCK_NOERROR
;
1701 GSocketError
_GAddress_translate_to(GAddress
*address
,
1702 struct sockaddr
**addr
, int *len
)
1704 if (!address
->m_addr
)
1706 address
->m_error
= GSOCK_INVADDR
;
1707 return GSOCK_INVADDR
;
1710 *len
= address
->m_len
;
1711 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
1714 address
->m_error
= GSOCK_MEMERR
;
1715 return GSOCK_MEMERR
;
1718 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1719 return GSOCK_NOERROR
;
1723 * -------------------------------------------------------------------------
1724 * Internet address family
1725 * -------------------------------------------------------------------------
1728 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1730 address
->m_len
= sizeof(struct sockaddr_in
);
1731 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1732 if (address
->m_addr
== NULL
)
1734 address
->m_error
= GSOCK_MEMERR
;
1735 return GSOCK_MEMERR
;
1738 address
->m_family
= GSOCK_INET
;
1739 address
->m_realfamily
= PF_INET
;
1740 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1741 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1743 return GSOCK_NOERROR
;
1746 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1749 struct in_addr
*addr
;
1751 assert(address
!= NULL
);
1753 CHECK_ADDRESS(address
, INET
);
1755 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1757 /* If it is a numeric host name, convert it now */
1758 #if defined(HAVE_INET_ATON)
1759 if (inet_aton(hostname
, addr
) == 0)
1761 #elif defined(HAVE_INET_ADDR)
1762 if ( (addr
->s_addr
= inet_addr(hostname
)) == (unsigned)-1 )
1765 /* Use gethostbyname by default */
1767 int val
= 1; /* VA doesn't like constants in conditional expressions */
1772 struct in_addr
*array_addr
;
1774 /* It is a real name, we solve it */
1775 if ((he
= gethostbyname(hostname
)) == NULL
)
1777 /* Reset to invalid address */
1778 addr
->s_addr
= INADDR_NONE
;
1779 address
->m_error
= GSOCK_NOHOST
;
1780 return GSOCK_NOHOST
;
1783 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1784 addr
->s_addr
= array_addr
[0].s_addr
;
1787 return GSOCK_NOERROR
;
1790 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1792 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1795 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1796 unsigned long hostaddr
)
1798 struct in_addr
*addr
;
1800 assert(address
!= NULL
);
1802 CHECK_ADDRESS(address
, INET
);
1804 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1805 addr
->s_addr
= htonl(hostaddr
);
1807 return GSOCK_NOERROR
;
1810 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1811 const char *protocol
)
1814 struct sockaddr_in
*addr
;
1816 assert(address
!= NULL
);
1817 CHECK_ADDRESS(address
, INET
);
1821 address
->m_error
= GSOCK_INVPORT
;
1822 return GSOCK_INVPORT
;
1825 #if defined(__WXPM__) && defined(__EMX__)
1826 se
= getservbyname(port
, (char*)protocol
);
1828 se
= getservbyname(port
, protocol
);
1832 /* the cast to int suppresses compiler warnings about subscript having the
1834 if (isdigit((int)port
[0]))
1838 port_int
= atoi(port
);
1839 addr
= (struct sockaddr_in
*)address
->m_addr
;
1840 addr
->sin_port
= htons(port_int
);
1841 return GSOCK_NOERROR
;
1844 address
->m_error
= GSOCK_INVPORT
;
1845 return GSOCK_INVPORT
;
1848 addr
= (struct sockaddr_in
*)address
->m_addr
;
1849 addr
->sin_port
= se
->s_port
;
1851 return GSOCK_NOERROR
;
1854 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1856 struct sockaddr_in
*addr
;
1858 assert(address
!= NULL
);
1859 CHECK_ADDRESS(address
, INET
);
1861 addr
= (struct sockaddr_in
*)address
->m_addr
;
1862 addr
->sin_port
= htons(port
);
1864 return GSOCK_NOERROR
;
1867 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1871 struct sockaddr_in
*addr
;
1873 assert(address
!= NULL
);
1874 CHECK_ADDRESS(address
, INET
);
1876 addr
= (struct sockaddr_in
*)address
->m_addr
;
1877 addr_buf
= (char *)&(addr
->sin_addr
);
1879 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1882 address
->m_error
= GSOCK_NOHOST
;
1883 return GSOCK_NOHOST
;
1886 strncpy(hostname
, he
->h_name
, sbuf
);
1888 return GSOCK_NOERROR
;
1891 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1893 struct sockaddr_in
*addr
;
1895 assert(address
!= NULL
);
1896 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1898 addr
= (struct sockaddr_in
*)address
->m_addr
;
1900 return ntohl(addr
->sin_addr
.s_addr
);
1903 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1905 struct sockaddr_in
*addr
;
1907 assert(address
!= NULL
);
1908 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1910 addr
= (struct sockaddr_in
*)address
->m_addr
;
1911 return ntohs(addr
->sin_port
);
1915 * -------------------------------------------------------------------------
1916 * Unix address family
1917 * -------------------------------------------------------------------------
1920 #ifndef __VISAGECPP__
1921 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1923 address
->m_len
= sizeof(struct sockaddr_un
);
1924 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
1925 if (address
->m_addr
== NULL
)
1927 address
->m_error
= GSOCK_MEMERR
;
1928 return GSOCK_MEMERR
;
1931 address
->m_family
= GSOCK_UNIX
;
1932 address
->m_realfamily
= PF_UNIX
;
1933 ((struct sockaddr_un
*)address
->m_addr
)->sun_family
= AF_UNIX
;
1934 ((struct sockaddr_un
*)address
->m_addr
)->sun_path
[0] = 0;
1936 return GSOCK_NOERROR
;
1939 #define UNIX_SOCK_PATHLEN (sizeof(addr->sun_path)/sizeof(addr->sun_path[0]))
1941 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1943 struct sockaddr_un
*addr
;
1945 assert(address
!= NULL
);
1947 CHECK_ADDRESS(address
, UNIX
);
1949 addr
= ((struct sockaddr_un
*)address
->m_addr
);
1950 strncpy(addr
->sun_path
, path
, UNIX_SOCK_PATHLEN
);
1951 addr
->sun_path
[UNIX_SOCK_PATHLEN
- 1] = '\0';
1953 return GSOCK_NOERROR
;
1956 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1958 struct sockaddr_un
*addr
;
1960 assert(address
!= NULL
);
1961 CHECK_ADDRESS(address
, UNIX
);
1963 addr
= (struct sockaddr_un
*)address
->m_addr
;
1965 strncpy(path
, addr
->sun_path
, sbuf
);
1967 return GSOCK_NOERROR
;
1969 #endif /* !defined(__VISAGECPP__) */
1970 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */