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__
23 #include "wx/private/gsocketiohandler.h"
24 #include "wx/thread.h" // for wxThread::IsMain() used in assert
27 #if defined(__VISAGECPP__)
28 #define BSD_SELECT /* use Berkeley Sockets select */
31 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
34 #include <sys/types.h>
39 #include <netinet/in.h>
42 #include <sys/ioctl.h>
44 #ifdef HAVE_SYS_SELECT_H
45 # include <sys/select.h>
52 u_char sun_len
; /* sockaddr len including null */
53 u_char sun_family
; /* AF_UNIX */
54 char sun_path
[108]; /* path name (gag) */
57 #include <sys/socket.h>
63 #include <netinet/in.h>
64 #include <arpa/inet.h>
71 #include <machine/endian.h>
77 #define EBADF SOCEBADF
83 #include <sys/socket.h>
84 #include <sys/ioctl.h>
85 #include <sys/select.h>
87 #define close(a) soclose(a)
88 #define select(a,b,c,d,e) bsdselect(a,b,c,d,e)
89 int _System
bsdselect(int,
94 int _System
soclose(int);
98 #include <sys/select.h>
106 # include <sys/filio.h>
109 # include <bstring.h>
112 # include <strings.h>
119 # define WX_SOCKLEN_T unsigned int
123 # define WX_SOCKLEN_T socklen_t
125 # elif defined(__WXMAC__)
126 # define WX_SOCKLEN_T socklen_t
128 # define WX_SOCKLEN_T int
132 #endif /* SOCKLEN_T */
135 #define SOCKOPTLEN_T WX_SOCKLEN_T
139 * MSW defines this, Unices don't.
141 #ifndef INVALID_SOCKET
142 #define INVALID_SOCKET -1
145 /* UnixWare reportedly needs this for FIONBIO definition */
147 #include <sys/filio.h>
151 * INADDR_BROADCAST is identical to INADDR_NONE which is not defined
152 * on all systems. INADDR_BROADCAST should be fine to indicate an error.
155 #define INADDR_NONE INADDR_BROADCAST
158 #if defined(__VISAGECPP__) || defined(__WATCOMC__)
160 #define MASK_SIGNAL() {
161 #define UNMASK_SIGNAL() }
164 extern "C" { typedef void (*wxSigHandler
)(int); }
166 #define MASK_SIGNAL() \
168 wxSigHandler old_handler = signal(SIGPIPE, SIG_IGN);
170 #define UNMASK_SIGNAL() \
171 signal(SIGPIPE, old_handler); \
176 /* If a SIGPIPE is issued by a socket call on a remotely closed socket,
177 the program will "crash" unless it explicitly handles the SIGPIPE.
178 By using MSG_NOSIGNAL, the SIGPIPE is suppressed. Later, we will
179 use SO_NOSIGPIPE (if available), the BSD equivalent. */
181 # define GSOCKET_MSG_NOSIGNAL MSG_NOSIGNAL
182 #else /* MSG_NOSIGNAL not available (FreeBSD including OS X) */
183 # define GSOCKET_MSG_NOSIGNAL 0
184 #endif /* MSG_NOSIGNAL */
186 #ifndef __GSOCKET_STANDALONE__
187 # include "wx/unix/gsockunx.h"
188 # include "wx/unix/private.h"
189 # include "wx/gsocket.h"
190 #if wxUSE_THREADS && (defined(HAVE_GETHOSTBYNAME) || defined(HAVE_GETSERVBYNAME))
191 # include "wx/thread.h"
194 # include "gsockunx.h"
195 # include "gsocket.h"
199 #endif /* __GSOCKET_STANDALONE__ */
201 #if defined(HAVE_GETHOSTBYNAME)
202 static struct hostent
* deepCopyHostent(struct hostent
*h
,
203 const struct hostent
*he
,
204 char *buffer
, int size
, int *err
)
206 /* copy old structure */
207 memcpy(h
, he
, sizeof(struct hostent
));
210 int len
= strlen(h
->h_name
);
216 memcpy(buffer
, h
->h_name
, len
);
220 /* track position in the buffer */
223 /* reuse len to store address length */
226 /* ensure pointer alignment */
227 unsigned int misalign
= sizeof(char *) - pos%sizeof
(char *);
228 if(misalign
< sizeof(char *))
231 /* leave space for pointer list */
232 char **p
= h
->h_addr_list
, **q
;
233 char **h_addr_list
= (char **)(buffer
+ pos
);
235 pos
+= sizeof(char *);
237 /* copy addresses and fill new pointer list */
238 for (p
= h
->h_addr_list
, q
= h_addr_list
; *p
!= 0; p
++, q
++)
240 if (size
< pos
+ len
)
245 memcpy(buffer
+ pos
, *p
, len
); /* copy content */
246 *q
= buffer
+ pos
; /* set copied pointer to copied content */
249 *++q
= 0; /* null terminate the pointer list */
250 h
->h_addr_list
= h_addr_list
; /* copy pointer to pointers */
252 /* ensure word alignment of pointers */
253 misalign
= sizeof(char *) - pos%sizeof
(char *);
254 if(misalign
< sizeof(char *))
257 /* leave space for pointer list */
259 char **h_aliases
= (char **)(buffer
+ pos
);
261 pos
+= sizeof(char *);
263 /* copy aliases and fill new pointer list */
264 for (p
= h
->h_aliases
, q
= h_aliases
; *p
!= 0; p
++, q
++)
267 if (size
<= pos
+ len
)
272 memcpy(buffer
+ pos
, *p
, len
); /* copy content */
273 buffer
[pos
+ len
] = '\0';
274 *q
= buffer
+ pos
; /* set copied pointer to copied content */
277 *++q
= 0; /* null terminate the pointer list */
278 h
->h_aliases
= h_aliases
; /* copy pointer to pointers */
284 #if defined(HAVE_GETHOSTBYNAME) && wxUSE_THREADS
285 static wxMutex nameLock
;
287 struct hostent
* wxGethostbyname_r(const char *hostname
, struct hostent
*h
,
288 void *buffer
, int size
, int *err
)
291 struct hostent
*he
= NULL
;
293 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_6)
294 if (gethostbyname_r(hostname
, h
, (char*)buffer
, size
, &he
, err
))
296 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_5)
297 he
= gethostbyname_r(hostname
, h
, (char*)buffer
, size
, err
);
298 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
299 if (gethostbyname_r(hostname
, h
, (struct hostent_data
*) buffer
))
306 #elif defined(HAVE_GETHOSTBYNAME)
308 wxMutexLocker
locker(nameLock
);
310 he
= gethostbyname(hostname
);
314 he
= deepCopyHostent(h
, he
, (char*)buffer
, size
, err
);
319 #if defined(HAVE_GETHOSTBYNAME) && wxUSE_THREADS
320 static wxMutex addrLock
;
322 struct hostent
* wxGethostbyaddr_r(const char *addr_buf
, int buf_size
,
323 int proto
, struct hostent
*h
,
324 void *buffer
, int size
, int *err
)
326 struct hostent
*he
= NULL
;
328 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_6)
329 if (gethostbyaddr_r(addr_buf
, buf_size
, proto
, h
,
330 (char*)buffer
, size
, &he
, err
))
332 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_5)
333 he
= gethostbyaddr_r(addr_buf
, buf_size
, proto
, h
, (char*)buffer
, size
, err
);
334 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
335 if (gethostbyaddr_r(addr_buf
, buf_size
, proto
, h
,
336 (struct hostent_data
*) buffer
))
343 #elif defined(HAVE_GETHOSTBYNAME)
345 wxMutexLocker
locker(addrLock
);
347 he
= gethostbyaddr(addr_buf
, buf_size
, proto
);
351 he
= deepCopyHostent(h
, he
, (char*)buffer
, size
, err
);
356 #if defined(HAVE_GETSERVBYNAME)
357 static struct servent
* deepCopyServent(struct servent
*s
,
358 const struct servent
*se
,
359 char *buffer
, int size
)
361 /* copy plain old structure */
362 memcpy(s
, se
, sizeof(struct servent
));
365 int len
= strlen(s
->s_name
);
370 memcpy(buffer
, s
->s_name
, len
);
374 /* track position in the buffer */
378 len
= strlen(s
->s_proto
);
379 if (pos
+ len
>= size
)
383 memcpy(buffer
+ pos
, s
->s_proto
, len
);
384 buffer
[pos
+ len
] = '\0';
385 s
->s_proto
= buffer
+ pos
;
387 /* track position in the buffer */
390 /* ensure pointer alignment */
391 unsigned int misalign
= sizeof(char *) - pos%sizeof
(char *);
392 if(misalign
< sizeof(char *))
395 /* leave space for pointer list */
396 char **p
= s
->s_aliases
, **q
;
397 char **s_aliases
= (char **)(buffer
+ pos
);
399 pos
+= sizeof(char *);
401 /* copy addresses and fill new pointer list */
402 for (p
= s
->s_aliases
, q
= s_aliases
; *p
!= 0; p
++, q
++){
404 if (size
<= pos
+ len
)
408 memcpy(buffer
+ pos
, *p
, len
); /* copy content */
409 buffer
[pos
+ len
] = '\0';
410 *q
= buffer
+ pos
; /* set copied pointer to copied content */
413 *++q
= 0; /* null terminate the pointer list */
414 s
->s_aliases
= s_aliases
; /* copy pointer to pointers */
419 #if defined(HAVE_GETSERVBYNAME) && wxUSE_THREADS
420 static wxMutex servLock
;
422 struct servent
*wxGetservbyname_r(const char *port
, const char *protocol
,
423 struct servent
*serv
, void *buffer
, int size
)
425 struct servent
*se
= NULL
;
426 #if defined(HAVE_FUNC_GETSERVBYNAME_R_6)
427 if (getservbyname_r(port
, protocol
, serv
, (char*)buffer
, size
, &se
))
429 #elif defined(HAVE_FUNC_GETSERVBYNAME_R_5)
430 se
= getservbyname_r(port
, protocol
, serv
, (char*)buffer
, size
);
431 #elif defined(HAVE_FUNC_GETSERVBYNAME_R_4)
432 if (getservbyname_r(port
, protocol
, serv
, (struct servent_data
*) buffer
))
436 #elif defined(HAVE_GETSERVBYNAME)
438 wxMutexLocker
locker(servLock
);
440 se
= getservbyname(port
, protocol
);
442 se
= deepCopyServent(serv
, se
, (char*)buffer
, size
);
447 /* debugging helpers */
448 #ifdef __GSOCKET_DEBUG__
449 # define GSocket_Debug(args) printf args
451 # define GSocket_Debug(args)
452 #endif /* __GSOCKET_DEBUG__ */
455 typedef struct sockaddr_storage wxSockAddr
;
457 typedef struct sockaddr wxSockAddr
;
460 /* Table of GUI-related functions. We must call them indirectly because
461 * of wxBase and GUI separation: */
465 GSocketManager
* const manager
= GSocketManager::Get();
466 return manager
&& manager
->OnInit();
469 void GSocket_Cleanup()
471 GSocketManager
* const manager
= GSocketManager::Get();
476 /* Constructors / Destructors for GSocket */
482 m_fd
= INVALID_SOCKET
;
485 for (i
=0;i
<GSOCK_MAX_EVENT
;i
++)
492 m_error
= GSOCK_NOERROR
;
495 m_gui_dependent
= NULL
;
496 m_non_blocking
= false;
500 m_timeout
= 10*60*1000;
501 /* 10 minutes * 60 sec * 1000 millisec */
502 m_establishing
= false;
503 m_use_events
= false;
504 m_initialRecvBufferSize
= -1;
505 m_initialSendBufferSize
= -1;
507 m_ok
= GSocketManager::Get()->Init_Socket(this);
510 void GSocket::Close()
515 /* When running on OS X, the gsockosx implementation of GSocketGUIFunctionsTable
516 will close the socket during Disable_Events. However, it will only do this
517 if it is being used. That is, it won't do it in a console program. To
518 ensure we get the right behavior, we have gsockosx set m_fd = INVALID_SOCKET
519 if it has closed the socket which indicates to us (at runtime, instead of
520 at compile time as this had been before) that the socket has already
523 if(m_fd
!= INVALID_SOCKET
)
525 m_fd
= INVALID_SOCKET
;
532 /* Check that the socket is really shutdowned */
533 if (m_fd
!= INVALID_SOCKET
)
536 GSocketManager::Get()->Destroy_Socket(this);
540 /* Destroy private addresses */
542 GAddress_destroy(m_local
);
545 GAddress_destroy(m_peer
);
550 * Disallow further read/write operations on this socket, close
551 * the fd and disable all callbacks.
553 void GSocket::Shutdown()
559 /* Don't allow events to fire after socket has been closed */
563 /* If socket has been created, shutdown it */
564 if (m_fd
!= INVALID_SOCKET
)
570 /* Disable GUI callbacks */
571 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
572 m_cbacks
[evt
] = NULL
;
574 m_detected
= GSOCK_LOST_FLAG
;
577 /* Address handling */
583 * Set or get the local or peer address for this socket. The 'set'
584 * functions return GSOCK_NOERROR on success, an error code otherwise.
585 * The 'get' functions return a pointer to a GAddress object on success,
586 * or NULL otherwise, in which case they set the error code of the
587 * corresponding GSocket.
590 * GSOCK_INVSOCK - the socket is not valid.
591 * GSOCK_INVADDR - the address is not valid.
593 GSocketError
GSocket::SetLocal(GAddress
*address
)
597 /* the socket must be initialized, or it must be a server */
598 if ((m_fd
!= INVALID_SOCKET
&& !m_server
))
600 m_error
= GSOCK_INVSOCK
;
601 return GSOCK_INVSOCK
;
605 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
607 m_error
= GSOCK_INVADDR
;
608 return GSOCK_INVADDR
;
612 GAddress_destroy(m_local
);
614 m_local
= GAddress_copy(address
);
616 return GSOCK_NOERROR
;
619 GSocketError
GSocket::SetPeer(GAddress
*address
)
624 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
626 m_error
= GSOCK_INVADDR
;
627 return GSOCK_INVADDR
;
631 GAddress_destroy(m_peer
);
633 m_peer
= GAddress_copy(address
);
635 return GSOCK_NOERROR
;
638 GAddress
*GSocket::GetLocal()
642 WX_SOCKLEN_T size
= sizeof(addr
);
647 /* try to get it from the m_local var first */
649 return GAddress_copy(m_local
);
651 /* else, if the socket is initialized, try getsockname */
652 if (m_fd
== INVALID_SOCKET
)
654 m_error
= GSOCK_INVSOCK
;
658 if (getsockname(m_fd
, (sockaddr
*)&addr
, (WX_SOCKLEN_T
*) &size
) < 0)
660 m_error
= GSOCK_IOERR
;
664 /* got a valid address from getsockname, create a GAddress object */
665 address
= GAddress_new();
668 m_error
= GSOCK_MEMERR
;
672 err
= _GAddress_translate_from(address
, (sockaddr
*)&addr
, size
);
673 if (err
!= GSOCK_NOERROR
)
675 GAddress_destroy(address
);
683 GAddress
*GSocket::GetPeer()
687 /* try to get it from the m_peer var */
689 return GAddress_copy(m_peer
);
694 /* Server specific parts */
696 /* GSocket_SetServer:
697 * Sets up this socket as a server. The local address must have been
698 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
699 * Returns GSOCK_NOERROR on success, one of the following otherwise:
702 * GSOCK_INVSOCK - the socket is in use.
703 * GSOCK_INVADDR - the local address has not been set.
704 * GSOCK_IOERR - low-level error.
706 GSocketError
GSocket::SetServer()
712 /* must not be in use */
713 if (m_fd
!= INVALID_SOCKET
)
715 m_error
= GSOCK_INVSOCK
;
716 return GSOCK_INVSOCK
;
719 /* the local addr must have been set */
722 m_error
= GSOCK_INVADDR
;
723 return GSOCK_INVADDR
;
726 /* Initialize all fields */
730 /* Create the socket */
731 m_fd
= socket(m_local
->m_realfamily
, SOCK_STREAM
, 0);
733 if (m_fd
== INVALID_SOCKET
)
735 m_error
= GSOCK_IOERR
;
739 /* FreeBSD variants can't use MSG_NOSIGNAL, and instead use a socket option */
741 setsockopt(m_fd
, SOL_SOCKET
, SO_NOSIGPIPE
, (const char*)&arg
, sizeof(arg
));
744 ioctl(m_fd
, FIONBIO
, &arg
);
748 /* allow a socket to re-bind if the socket is in the TIME_WAIT
749 state after being previously closed.
753 setsockopt(m_fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char*)&arg
, sizeof(arg
));
755 setsockopt(m_fd
, SOL_SOCKET
, SO_REUSEPORT
, (const char*)&arg
, sizeof(arg
));
759 /* Bind to the local address,
760 * retrieve the actual address bound,
761 * and listen up to 5 connections.
763 if ((bind(m_fd
, m_local
->m_addr
, m_local
->m_len
) != 0) ||
766 (WX_SOCKLEN_T
*) &m_local
->m_len
) != 0) ||
767 (listen(m_fd
, 5) != 0))
770 m_error
= GSOCK_IOERR
;
774 return GSOCK_NOERROR
;
777 /* GSocket_WaitConnection:
778 * Waits for an incoming client connection. Returns a pointer to
779 * a GSocket object, or NULL if there was an error, in which case
780 * the last error field will be updated for the calling GSocket.
782 * Error codes (set in the calling GSocket)
783 * GSOCK_INVSOCK - the socket is not valid or not a server.
784 * GSOCK_TIMEDOUT - timeout, no incoming connections.
785 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
786 * GSOCK_MEMERR - couldn't allocate memory.
787 * GSOCK_IOERR - low-level error.
789 GSocket
*GSocket::WaitConnection()
792 WX_SOCKLEN_T fromlen
= sizeof(from
);
799 /* If the socket has already been created, we exit immediately */
800 if (m_fd
== INVALID_SOCKET
|| !m_server
)
802 m_error
= GSOCK_INVSOCK
;
806 /* Create a GSocket object for the new connection */
807 connection
= GSocket_new();
811 m_error
= GSOCK_MEMERR
;
815 /* Wait for a connection (with timeout) */
816 if (Input_Timeout() == GSOCK_TIMEDOUT
)
819 /* m_error set by _GSocket_Input_Timeout */
823 connection
->m_fd
= accept(m_fd
, (sockaddr
*)&from
, (WX_SOCKLEN_T
*) &fromlen
);
825 /* Reenable CONNECTION events */
826 Enable(GSOCK_CONNECTION
);
828 if (connection
->m_fd
== INVALID_SOCKET
)
830 if (errno
== EWOULDBLOCK
)
831 m_error
= GSOCK_WOULDBLOCK
;
833 m_error
= GSOCK_IOERR
;
839 /* Initialize all fields */
840 connection
->m_server
= false;
841 connection
->m_stream
= true;
843 /* Setup the peer address field */
844 connection
->m_peer
= GAddress_new();
845 if (!connection
->m_peer
)
848 m_error
= GSOCK_MEMERR
;
852 err
= _GAddress_translate_from(connection
->m_peer
, (sockaddr
*)&from
, fromlen
);
853 if (err
!= GSOCK_NOERROR
)
860 #if defined(__EMX__) || defined(__VISAGECPP__)
861 ioctl(connection
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
863 ioctl(connection
->m_fd
, FIONBIO
, &arg
);
866 connection
->Notify(true);
871 void GSocket::Notify(bool flag
)
873 if (flag
== m_use_events
)
876 // it is not safe to attach or detach i/o descriptor in child thread
877 wxASSERT_MSG( wxThread::IsMain(), "should be called in main thread only" );
883 void GSocket::EnableEvents(bool flag
)
886 GSocketManager::Get()->Enable_Events(this);
888 GSocketManager::Get()->Disable_Events(this);
891 bool GSocket::SetReusable()
893 /* socket must not be null, and must not be in use/already bound */
894 if (this && m_fd
== INVALID_SOCKET
)
904 bool GSocket::SetBroadcast()
906 /* socket must not be in use/already bound */
907 if (m_fd
== INVALID_SOCKET
) {
914 bool GSocket::DontDoBind()
916 /* socket must not be in use/already bound */
917 if (m_fd
== INVALID_SOCKET
) {
924 /* Client specific parts */
927 * For stream (connection oriented) sockets, GSocket_Connect() tries
928 * to establish a client connection to a server using the peer address
929 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
930 * connection has been successfully established, or one of the error
931 * codes listed below. Note that for nonblocking sockets, a return
932 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
933 * request can be completed later; you should use GSocket_Select()
934 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
935 * corresponding asynchronous events.
937 * For datagram (non connection oriented) sockets, GSocket_Connect()
938 * just sets the peer address established with GSocket_SetPeer() as
939 * default destination.
942 * GSOCK_INVSOCK - the socket is in use or not valid.
943 * GSOCK_INVADDR - the peer address has not been established.
944 * GSOCK_TIMEDOUT - timeout, the connection failed.
945 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
946 * GSOCK_MEMERR - couldn't allocate memory.
947 * GSOCK_IOERR - low-level error.
949 GSocketError
GSocket::Connect(GSocketStream stream
)
956 /* Enable CONNECTION events (needed for nonblocking connections) */
957 Enable(GSOCK_CONNECTION
);
959 if (m_fd
!= INVALID_SOCKET
)
961 m_error
= GSOCK_INVSOCK
;
962 return GSOCK_INVSOCK
;
967 m_error
= GSOCK_INVADDR
;
968 return GSOCK_INVADDR
;
971 /* Streamed or dgram socket? */
972 m_stream
= (stream
== GSOCK_STREAMED
);
974 m_establishing
= false;
976 /* Create the socket */
977 m_fd
= socket(m_peer
->m_realfamily
,
978 m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
980 if (m_fd
== INVALID_SOCKET
)
982 m_error
= GSOCK_IOERR
;
986 /* FreeBSD variants can't use MSG_NOSIGNAL, and instead use a socket option */
988 setsockopt(m_fd
, SOL_SOCKET
, SO_NOSIGPIPE
, (const char*)&arg
, sizeof(arg
));
991 #if defined(__EMX__) || defined(__VISAGECPP__)
992 ioctl(m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
994 ioctl(m_fd
, FIONBIO
, &arg
);
997 // If the reuse flag is set, use the applicable socket reuse flags(s)
1000 setsockopt(m_fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char*)&arg
, sizeof(arg
));
1002 setsockopt(m_fd
, SOL_SOCKET
, SO_REUSEPORT
, (const char*)&arg
, sizeof(arg
));
1006 if (m_initialRecvBufferSize
>= 0)
1007 setsockopt(m_fd
, SOL_SOCKET
, SO_RCVBUF
, (const char*)&m_initialRecvBufferSize
, sizeof(m_initialRecvBufferSize
));
1008 if (m_initialSendBufferSize
>= 0)
1009 setsockopt(m_fd
, SOL_SOCKET
, SO_SNDBUF
, (const char*)&m_initialSendBufferSize
, sizeof(m_initialSendBufferSize
));
1011 // If a local address has been set, then we need to bind to it before calling connect
1012 if (m_local
&& m_local
->m_addr
)
1014 bind(m_fd
, m_local
->m_addr
, m_local
->m_len
);
1017 /* Connect it to the peer address, with a timeout (see below) */
1018 ret
= connect(m_fd
, m_peer
->m_addr
, m_peer
->m_len
);
1020 /* We only call Enable_Events if we know we aren't shutting down the socket.
1021 * NB: Enable_Events needs to be called whether the socket is blocking or
1022 * non-blocking, it just shouldn't be called prior to knowing there is a
1023 * connection _if_ blocking sockets are being used.
1024 * If connect above returns 0, we are already connected and need to make the
1025 * call to Enable_Events now.
1028 if (m_use_events
&& (m_non_blocking
|| ret
== 0))
1035 /* If connect failed with EINPROGRESS and the GSocket object
1036 * is in blocking mode, we select() for the specified timeout
1037 * checking for writability to see if the connection request
1040 if ((err
== EINPROGRESS
) && (!m_non_blocking
))
1042 if (Output_Timeout() == GSOCK_TIMEDOUT
)
1045 /* m_error is set in _GSocket_Output_Timeout */
1046 return GSOCK_TIMEDOUT
;
1051 SOCKOPTLEN_T len
= sizeof(error
);
1053 getsockopt(m_fd
, SOL_SOCKET
, SO_ERROR
, (char*) &error
, &len
);
1058 return GSOCK_NOERROR
;
1062 /* If connect failed with EINPROGRESS and the GSocket object
1063 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
1064 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
1065 * this way if the connection completes, a GSOCK_CONNECTION
1066 * event will be generated, if enabled.
1068 if ((err
== EINPROGRESS
) && (m_non_blocking
))
1070 m_establishing
= true;
1071 m_error
= GSOCK_WOULDBLOCK
;
1072 return GSOCK_WOULDBLOCK
;
1075 /* If connect failed with an error other than EINPROGRESS,
1076 * then the call to GSocket_Connect has failed.
1079 m_error
= GSOCK_IOERR
;
1084 return GSOCK_NOERROR
;
1087 /* Datagram sockets */
1089 /* GSocket_SetNonOriented:
1090 * Sets up this socket as a non-connection oriented (datagram) socket.
1091 * Before using this function, the local address must have been set
1092 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
1093 * on success, or one of the following otherwise.
1096 * GSOCK_INVSOCK - the socket is in use.
1097 * GSOCK_INVADDR - the local address has not been set.
1098 * GSOCK_IOERR - low-level error.
1100 GSocketError
GSocket::SetNonOriented()
1106 if (m_fd
!= INVALID_SOCKET
)
1108 m_error
= GSOCK_INVSOCK
;
1109 return GSOCK_INVSOCK
;
1114 m_error
= GSOCK_INVADDR
;
1115 return GSOCK_INVADDR
;
1118 /* Initialize all fields */
1122 /* Create the socket */
1123 m_fd
= socket(m_local
->m_realfamily
, SOCK_DGRAM
, 0);
1125 if (m_fd
== INVALID_SOCKET
)
1127 m_error
= GSOCK_IOERR
;
1130 #if defined(__EMX__) || defined(__VISAGECPP__)
1131 ioctl(m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
1133 ioctl(m_fd
, FIONBIO
, &arg
);
1140 setsockopt(m_fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char*)&arg
, sizeof(arg
));
1142 setsockopt(m_fd
, SOL_SOCKET
, SO_REUSEPORT
, (const char*)&arg
, sizeof(arg
));
1148 setsockopt(m_fd
, SOL_SOCKET
, SO_BROADCAST
, (const char*)&arg
, sizeof(arg
));
1152 /* Bind to the local address,
1153 * and retrieve the actual address bound.
1155 if ((bind(m_fd
, m_local
->m_addr
, m_local
->m_len
) != 0) ||
1158 (WX_SOCKLEN_T
*) &m_local
->m_len
) != 0))
1161 m_error
= GSOCK_IOERR
;
1165 return GSOCK_NOERROR
;
1170 /* Like recv(), send(), ... */
1171 int GSocket::Read(char *buffer
, int size
)
1177 if (m_fd
== INVALID_SOCKET
|| m_server
)
1179 m_error
= GSOCK_INVSOCK
;
1183 /* Disable events during query of socket status */
1184 Disable(GSOCK_INPUT
);
1186 /* If the socket is blocking, wait for data (with a timeout) */
1187 if (Input_Timeout() == GSOCK_TIMEDOUT
) {
1188 m_error
= GSOCK_TIMEDOUT
;
1189 /* Don't return here immediately, otherwise socket events would not be
1197 ret
= Recv_Stream(buffer
, size
);
1199 ret
= Recv_Dgram(buffer
, size
);
1201 /* If recv returned zero, then the connection has been gracefully closed.
1202 * Otherwise, recv has returned an error (-1), in which case we have lost the
1203 * socket only if errno does _not_ indicate that there may be more data to read.
1207 /* Make sure wxSOCKET_LOST event gets sent and shut down the socket */
1210 m_detected
= GSOCK_LOST_FLAG
;
1217 if ((errno
== EWOULDBLOCK
) || (errno
== EAGAIN
))
1218 m_error
= GSOCK_WOULDBLOCK
;
1220 m_error
= GSOCK_IOERR
;
1224 /* Enable events again now that we are done processing */
1225 Enable(GSOCK_INPUT
);
1230 int GSocket::Write(const char *buffer
, int size
)
1236 GSocket_Debug(( "GSocket_Write #1, size %d\n", size
));
1238 if (m_fd
== INVALID_SOCKET
|| m_server
)
1240 m_error
= GSOCK_INVSOCK
;
1244 GSocket_Debug(( "GSocket_Write #2, size %d\n", size
));
1246 /* If the socket is blocking, wait for writability (with a timeout) */
1247 if (Output_Timeout() == GSOCK_TIMEDOUT
)
1250 GSocket_Debug(( "GSocket_Write #3, size %d\n", size
));
1252 /* Write the data */
1254 ret
= Send_Stream(buffer
, size
);
1256 ret
= Send_Dgram(buffer
, size
);
1258 GSocket_Debug(( "GSocket_Write #4, size %d\n", size
));
1262 if ((errno
== EWOULDBLOCK
) || (errno
== EAGAIN
))
1264 m_error
= GSOCK_WOULDBLOCK
;
1265 GSocket_Debug(( "GSocket_Write error WOULDBLOCK\n" ));
1269 m_error
= GSOCK_IOERR
;
1270 GSocket_Debug(( "GSocket_Write error IOERR\n" ));
1273 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
1274 * in MSW). Once the first OUTPUT event is received, users can assume
1275 * that the socket is writable until a read operation fails. Only then
1276 * will further OUTPUT events be posted.
1278 Enable(GSOCK_OUTPUT
);
1283 GSocket_Debug(( "GSocket_Write #5, size %d ret %d\n", size
, ret
));
1289 * Polls the socket to determine its status. This function will
1290 * check for the events specified in the 'flags' parameter, and
1291 * it will return a mask indicating which operations can be
1292 * performed. This function won't block, regardless of the
1293 * mode (blocking | nonblocking) of the socket.
1295 GSocketEventFlags
GSocket::Select(GSocketEventFlags flags
)
1299 GSocketEventFlags result
= 0;
1306 return (GSOCK_LOST_FLAG
& flags
);
1308 /* Do not use a static struct, Linux can garble it */
1312 wxFD_ZERO(&readfds
);
1313 wxFD_ZERO(&writefds
);
1314 wxFD_ZERO(&exceptfds
);
1315 wxFD_SET(m_fd
, &readfds
);
1316 if (flags
& GSOCK_OUTPUT_FLAG
|| flags
& GSOCK_CONNECTION_FLAG
)
1317 wxFD_SET(m_fd
, &writefds
);
1318 wxFD_SET(m_fd
, &exceptfds
);
1320 /* Check 'sticky' CONNECTION flag first */
1321 result
|= GSOCK_CONNECTION_FLAG
& m_detected
;
1323 /* If we have already detected a LOST event, then don't try
1324 * to do any further processing.
1326 if ((m_detected
& GSOCK_LOST_FLAG
) != 0)
1328 m_establishing
= false;
1329 return (GSOCK_LOST_FLAG
& flags
);
1332 /* Try select now */
1333 if (select(m_fd
+ 1, &readfds
, &writefds
, &exceptfds
, &tv
) < 0)
1335 /* What to do here? */
1336 return (result
& flags
);
1339 /* Check for exceptions and errors */
1340 if (wxFD_ISSET(m_fd
, &exceptfds
))
1342 m_establishing
= false;
1343 m_detected
= GSOCK_LOST_FLAG
;
1345 /* LOST event: Abort any further processing */
1346 return (GSOCK_LOST_FLAG
& flags
);
1349 /* Check for readability */
1350 if (wxFD_ISSET(m_fd
, &readfds
))
1352 result
|= GSOCK_INPUT_FLAG
;
1354 if (m_server
&& m_stream
)
1356 /* This is a TCP server socket that detected a connection.
1357 While the INPUT_FLAG is also set, it doesn't matter on
1358 this kind of sockets, as we can only Accept() from them. */
1359 m_detected
|= GSOCK_CONNECTION_FLAG
;
1363 /* Check for writability */
1364 if (wxFD_ISSET(m_fd
, &writefds
))
1366 if (m_establishing
&& !m_server
)
1369 SOCKOPTLEN_T len
= sizeof(error
);
1370 m_establishing
= false;
1371 getsockopt(m_fd
, SOL_SOCKET
, SO_ERROR
, (char*)&error
, &len
);
1375 m_detected
= GSOCK_LOST_FLAG
;
1377 /* LOST event: Abort any further processing */
1378 return (GSOCK_LOST_FLAG
& flags
);
1382 m_detected
|= GSOCK_CONNECTION_FLAG
;
1387 result
|= GSOCK_OUTPUT_FLAG
;
1391 return (result
| m_detected
) & flags
;
1396 /* GSocket_SetNonBlocking:
1397 * Sets the socket to non-blocking mode. All IO calls will return
1400 void GSocket::SetNonBlocking(bool non_block
)
1404 GSocket_Debug( ("GSocket_SetNonBlocking: %d\n", (int)non_block
) );
1406 m_non_blocking
= non_block
;
1409 /* GSocket_SetTimeout:
1410 * Sets the timeout for blocking calls. Time is expressed in
1413 void GSocket::SetTimeout(unsigned long millisec
)
1417 m_timeout
= millisec
;
1420 /* GSocket_GetError:
1421 * Returns the last error occurred for this socket. Note that successful
1422 * operations do not clear this back to GSOCK_NOERROR, so use it only
1425 GSocketError WXDLLIMPEXP_NET
GSocket::GetError()
1435 * There is data to be read in the input buffer. If, after a read
1436 * operation, there is still data available, the callback function will
1439 * The socket is available for writing. That is, the next write call
1440 * won't block. This event is generated only once, when the connection is
1441 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
1442 * when the output buffer empties again. This means that the app should
1443 * assume that it can write since the first OUTPUT event, and no more
1444 * OUTPUT events will be generated unless an error occurs.
1446 * Connection successfully established, for client sockets, or incoming
1447 * client connection, for server sockets. Wait for this event (also watch
1448 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
1450 * The connection is lost (or a connection request failed); this could
1451 * be due to a failure, or due to the peer closing it gracefully.
1454 /* GSocket_SetCallback:
1455 * Enables the callbacks specified by 'flags'. Note that 'flags'
1456 * may be a combination of flags OR'ed toghether, so the same
1457 * callback function can be made to accept different events.
1458 * The callback function must have the following prototype:
1460 * void function(GSocket *socket, GSocketEvent event, char *cdata)
1462 void GSocket::SetCallback(GSocketEventFlags flags
,
1463 GSocketCallback callback
, char *cdata
)
1469 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
1471 if ((flags
& (1 << count
)) != 0)
1473 m_cbacks
[count
] = callback
;
1474 m_data
[count
] = cdata
;
1479 /* GSocket_UnsetCallback:
1480 * Disables all callbacks specified by 'flags', which may be a
1481 * combination of flags OR'ed toghether.
1483 void GSocket::UnsetCallback(GSocketEventFlags flags
)
1489 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
1491 if ((flags
& (1 << count
)) != 0)
1493 m_cbacks
[count
] = NULL
;
1494 m_data
[count
] = NULL
;
1499 GSocketError
GSocket::GetSockOpt(int level
, int optname
,
1500 void *optval
, int *optlen
)
1502 if (getsockopt(m_fd
, level
, optname
, (char*)optval
, (SOCKOPTLEN_T
*)optlen
) == 0)
1503 return GSOCK_NOERROR
;
1505 return GSOCK_OPTERR
;
1508 GSocketError
GSocket::SetSockOpt(int level
, int optname
,
1509 const void *optval
, int optlen
)
1511 if (setsockopt(m_fd
, level
, optname
, (const char*)optval
, optlen
) == 0)
1512 return GSOCK_NOERROR
;
1514 return GSOCK_OPTERR
;
1517 #define CALL_CALLBACK(socket, event) { \
1518 socket->Disable(event); \
1519 if (socket->m_cbacks[event]) \
1520 socket->m_cbacks[event](socket, event, socket->m_data[event]); \
1524 void GSocket::Enable(GSocketEvent event
)
1528 m_detected
&= ~(1 << event
);
1529 GSocketManager::Get()->Install_Callback(this, event
);
1533 void GSocket::Disable(GSocketEvent event
)
1537 m_detected
|= (1 << event
);
1538 GSocketManager::Get()->Uninstall_Callback(this, event
);
1542 /* _GSocket_Input_Timeout:
1543 * For blocking sockets, wait until data is available or
1544 * until timeout ellapses.
1546 GSocketError
GSocket::Input_Timeout()
1552 /* Linux select() will overwrite the struct on return */
1553 tv
.tv_sec
= (m_timeout
/ 1000);
1554 tv
.tv_usec
= (m_timeout
% 1000) * 1000;
1556 if (!m_non_blocking
)
1558 wxFD_ZERO(&readfds
);
1559 wxFD_SET(m_fd
, &readfds
);
1560 ret
= select(m_fd
+ 1, &readfds
, NULL
, NULL
, &tv
);
1563 GSocket_Debug(( "GSocket_Input_Timeout, select returned 0\n" ));
1564 m_error
= GSOCK_TIMEDOUT
;
1565 return GSOCK_TIMEDOUT
;
1570 GSocket_Debug(( "GSocket_Input_Timeout, select returned -1\n" ));
1571 if (errno
== EBADF
) { GSocket_Debug(( "Invalid file descriptor\n" )); }
1572 if (errno
== EINTR
) { GSocket_Debug(( "A non blocked signal was caught\n" )); }
1573 if (errno
== EINVAL
) { GSocket_Debug(( "The highest number descriptor is negative\n" )); }
1574 if (errno
== ENOMEM
) { GSocket_Debug(( "Not enough memory\n" )); }
1575 m_error
= GSOCK_TIMEDOUT
;
1576 return GSOCK_TIMEDOUT
;
1580 return GSOCK_NOERROR
;
1583 /* _GSocket_Output_Timeout:
1584 * For blocking sockets, wait until data can be sent without
1585 * blocking or until timeout ellapses.
1587 GSocketError
GSocket::Output_Timeout()
1593 /* Linux select() will overwrite the struct on return */
1594 tv
.tv_sec
= (m_timeout
/ 1000);
1595 tv
.tv_usec
= (m_timeout
% 1000) * 1000;
1597 GSocket_Debug( ("m_non_blocking has: %d\n", (int)m_non_blocking
) );
1599 if (!m_non_blocking
)
1601 wxFD_ZERO(&writefds
);
1602 wxFD_SET(m_fd
, &writefds
);
1603 ret
= select(m_fd
+ 1, NULL
, &writefds
, NULL
, &tv
);
1606 GSocket_Debug(( "GSocket_Output_Timeout, select returned 0\n" ));
1607 m_error
= GSOCK_TIMEDOUT
;
1608 return GSOCK_TIMEDOUT
;
1613 GSocket_Debug(( "GSocket_Output_Timeout, select returned -1\n" ));
1614 if (errno
== EBADF
) { GSocket_Debug(( "Invalid file descriptor\n" )); }
1615 if (errno
== EINTR
) { GSocket_Debug(( "A non blocked signal was caught\n" )); }
1616 if (errno
== EINVAL
) { GSocket_Debug(( "The highest number descriptor is negative\n" )); }
1617 if (errno
== ENOMEM
) { GSocket_Debug(( "Not enough memory\n" )); }
1618 m_error
= GSOCK_TIMEDOUT
;
1619 return GSOCK_TIMEDOUT
;
1622 if ( ! wxFD_ISSET(m_fd
, &writefds
) )
1624 GSocket_Debug(( "GSocket_Output_Timeout is buggy!\n" ));
1628 GSocket_Debug(( "GSocket_Output_Timeout seems correct\n" ));
1633 GSocket_Debug(( "GSocket_Output_Timeout, didn't try select!\n" ));
1636 return GSOCK_NOERROR
;
1639 int GSocket::Recv_Stream(char *buffer
, int size
)
1644 ret
= recv(m_fd
, buffer
, size
, GSOCKET_MSG_NOSIGNAL
);
1646 while (ret
== -1 && errno
== EINTR
); /* Loop until not interrupted */
1651 int GSocket::Recv_Dgram(char *buffer
, int size
)
1654 WX_SOCKLEN_T fromlen
= sizeof(from
);
1658 fromlen
= sizeof(from
);
1662 ret
= recvfrom(m_fd
, buffer
, size
, 0, (sockaddr
*)&from
, (WX_SOCKLEN_T
*) &fromlen
);
1664 while (ret
== -1 && errno
== EINTR
); /* Loop until not interrupted */
1669 /* Translate a system address into a GSocket address */
1672 m_peer
= GAddress_new();
1675 m_error
= GSOCK_MEMERR
;
1680 err
= _GAddress_translate_from(m_peer
, (sockaddr
*)&from
, fromlen
);
1681 if (err
!= GSOCK_NOERROR
)
1683 GAddress_destroy(m_peer
);
1692 int GSocket::Send_Stream(const char *buffer
, int size
)
1700 ret
= send(m_fd
, (char *)buffer
, size
, GSOCKET_MSG_NOSIGNAL
);
1702 while (ret
== -1 && errno
== EINTR
); /* Loop until not interrupted */
1709 int GSocket::Send_Dgram(const char *buffer
, int size
)
1711 struct sockaddr
*addr
;
1717 m_error
= GSOCK_INVADDR
;
1721 err
= _GAddress_translate_to(m_peer
, &addr
, &len
);
1722 if (err
!= GSOCK_NOERROR
)
1732 ret
= sendto(m_fd
, (char *)buffer
, size
, 0, addr
, len
);
1734 while (ret
== -1 && errno
== EINTR
); /* Loop until not interrupted */
1738 /* Frees memory allocated from _GAddress_translate_to */
1744 void GSocket::Detected_Read()
1748 /* Safeguard against straggling call to Detected_Read */
1749 if (m_fd
== INVALID_SOCKET
)
1754 /* If we have already detected a LOST event, then don't try
1755 * to do any further processing.
1757 if ((m_detected
& GSOCK_LOST_FLAG
) != 0)
1759 m_establishing
= false;
1761 CALL_CALLBACK(this, GSOCK_LOST
);
1766 int num
= recv(m_fd
, &c
, 1, MSG_PEEK
| GSOCKET_MSG_NOSIGNAL
);
1770 CALL_CALLBACK(this, GSOCK_INPUT
);
1774 if (m_server
&& m_stream
)
1776 CALL_CALLBACK(this, GSOCK_CONNECTION
);
1780 /* graceful shutdown */
1781 CALL_CALLBACK(this, GSOCK_LOST
);
1786 /* Do not throw a lost event in cases where the socket isn't really lost */
1787 if ((errno
== EWOULDBLOCK
) || (errno
== EAGAIN
) || (errno
== EINTR
))
1789 CALL_CALLBACK(this, GSOCK_INPUT
);
1793 CALL_CALLBACK(this, GSOCK_LOST
);
1800 void GSocket::Detected_Write()
1802 /* If we have already detected a LOST event, then don't try
1803 * to do any further processing.
1805 if ((m_detected
& GSOCK_LOST_FLAG
) != 0)
1807 m_establishing
= false;
1809 CALL_CALLBACK(this, GSOCK_LOST
);
1814 if (m_establishing
&& !m_server
)
1817 SOCKOPTLEN_T len
= sizeof(error
);
1819 m_establishing
= false;
1821 getsockopt(m_fd
, SOL_SOCKET
, SO_ERROR
, (char*)&error
, &len
);
1825 CALL_CALLBACK(this, GSOCK_LOST
);
1830 CALL_CALLBACK(this, GSOCK_CONNECTION
);
1831 /* We have to fire this event by hand because CONNECTION (for clients)
1832 * and OUTPUT are internally the same and we just disabled CONNECTION
1833 * events with the above macro.
1835 CALL_CALLBACK(this, GSOCK_OUTPUT
);
1840 CALL_CALLBACK(this, GSOCK_OUTPUT
);
1844 /* Compatibility functions for GSocket */
1845 GSocket
*GSocket_new(void)
1847 GSocket
*newsocket
= new GSocket();
1848 if (newsocket
->IsOk())
1857 * -------------------------------------------------------------------------
1859 * -------------------------------------------------------------------------
1862 /* CHECK_ADDRESS verifies that the current address family is either
1863 * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it
1864 * initalizes it to be a GSOCK_*family*. In other cases, it returns
1865 * an appropiate error code.
1867 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
1869 #define CHECK_ADDRESS(address, family) \
1871 if (address->m_family == GSOCK_NOFAMILY) \
1872 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1873 return address->m_error; \
1874 if (address->m_family != GSOCK_##family) \
1876 address->m_error = GSOCK_INVADDR; \
1877 return GSOCK_INVADDR; \
1881 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
1883 if (address->m_family == GSOCK_NOFAMILY) \
1884 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1886 if (address->m_family != GSOCK_##family) \
1888 address->m_error = GSOCK_INVADDR; \
1894 GAddress
*GAddress_new(void)
1898 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1901 address
->m_family
= GSOCK_NOFAMILY
;
1902 address
->m_addr
= NULL
;
1908 GAddress
*GAddress_copy(GAddress
*address
)
1912 assert(address
!= NULL
);
1914 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1917 memcpy(addr2
, address
, sizeof(GAddress
));
1919 if (address
->m_addr
&& address
->m_len
> 0)
1921 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
1922 if (addr2
->m_addr
== NULL
)
1927 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1933 void GAddress_destroy(GAddress
*address
)
1935 assert(address
!= NULL
);
1937 if (address
->m_addr
)
1938 free(address
->m_addr
);
1943 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1945 assert(address
!= NULL
);
1947 address
->m_family
= type
;
1950 GAddressType
GAddress_GetFamily(GAddress
*address
)
1952 assert(address
!= NULL
);
1954 return address
->m_family
;
1957 GSocketError
_GAddress_translate_from(GAddress
*address
,
1958 struct sockaddr
*addr
, int len
)
1960 address
->m_realfamily
= addr
->sa_family
;
1961 switch (addr
->sa_family
)
1964 address
->m_family
= GSOCK_INET
;
1967 address
->m_family
= GSOCK_UNIX
;
1971 address
->m_family
= GSOCK_INET6
;
1973 #endif // wxUSE_IPV6
1976 address
->m_error
= GSOCK_INVOP
;
1981 if (address
->m_addr
)
1982 free(address
->m_addr
);
1984 address
->m_len
= len
;
1985 address
->m_addr
= (struct sockaddr
*)malloc(len
);
1987 if (address
->m_addr
== NULL
)
1989 address
->m_error
= GSOCK_MEMERR
;
1990 return GSOCK_MEMERR
;
1993 memcpy(address
->m_addr
, addr
, len
);
1995 return GSOCK_NOERROR
;
1998 GSocketError
_GAddress_translate_to(GAddress
*address
,
1999 struct sockaddr
**addr
, int *len
)
2001 if (!address
->m_addr
)
2003 address
->m_error
= GSOCK_INVADDR
;
2004 return GSOCK_INVADDR
;
2007 *len
= address
->m_len
;
2008 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
2011 address
->m_error
= GSOCK_MEMERR
;
2012 return GSOCK_MEMERR
;
2015 memcpy(*addr
, address
->m_addr
, address
->m_len
);
2016 return GSOCK_NOERROR
;
2020 * -------------------------------------------------------------------------
2021 * Internet address family
2022 * -------------------------------------------------------------------------
2025 GSocketError
_GAddress_Init_INET(GAddress
*address
)
2027 address
->m_len
= sizeof(struct sockaddr_in
);
2028 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
2029 if (address
->m_addr
== NULL
)
2031 address
->m_error
= GSOCK_MEMERR
;
2032 return GSOCK_MEMERR
;
2035 address
->m_family
= GSOCK_INET
;
2036 address
->m_realfamily
= PF_INET
;
2037 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
2038 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
2040 return GSOCK_NOERROR
;
2043 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
2046 struct in_addr
*addr
;
2048 assert(address
!= NULL
);
2050 CHECK_ADDRESS(address
, INET
);
2052 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
2054 /* If it is a numeric host name, convert it now */
2055 #if defined(HAVE_INET_ATON)
2056 if (inet_aton(hostname
, addr
) == 0)
2058 #elif defined(HAVE_INET_ADDR)
2059 if ( (addr
->s_addr
= inet_addr(hostname
)) == (unsigned)-1 )
2062 /* Use gethostbyname by default */
2064 int val
= 1; /* VA doesn't like constants in conditional expressions */
2069 struct in_addr
*array_addr
;
2071 /* It is a real name, we solve it */
2073 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
2074 struct hostent_data buffer
;
2079 he
= wxGethostbyname_r(hostname
, &h
, (void*)&buffer
, sizeof(buffer
), &err
);
2082 /* Reset to invalid address */
2083 addr
->s_addr
= INADDR_NONE
;
2084 address
->m_error
= GSOCK_NOHOST
;
2085 return GSOCK_NOHOST
;
2088 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
2089 addr
->s_addr
= array_addr
[0].s_addr
;
2092 return GSOCK_NOERROR
;
2096 GSocketError
GAddress_INET_SetBroadcastAddress(GAddress
*address
)
2098 return GAddress_INET_SetHostAddress(address
, INADDR_BROADCAST
);
2101 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
2103 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
2106 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
2107 unsigned long hostaddr
)
2109 struct in_addr
*addr
;
2111 assert(address
!= NULL
);
2113 CHECK_ADDRESS(address
, INET
);
2115 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
2116 addr
->s_addr
= htonl(hostaddr
);
2118 return GSOCK_NOERROR
;
2121 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
2122 const char *protocol
)
2125 struct sockaddr_in
*addr
;
2127 assert(address
!= NULL
);
2128 CHECK_ADDRESS(address
, INET
);
2132 address
->m_error
= GSOCK_INVPORT
;
2133 return GSOCK_INVPORT
;
2136 #if defined(HAVE_FUNC_GETSERVBYNAME_R_4)
2137 struct servent_data buffer
;
2141 struct servent serv
;
2142 se
= wxGetservbyname_r(port
, protocol
, &serv
,
2143 (void*)&buffer
, sizeof(buffer
));
2146 /* the cast to int suppresses compiler warnings about subscript having the
2148 if (isdigit((int)port
[0]))
2152 port_int
= atoi(port
);
2153 addr
= (struct sockaddr_in
*)address
->m_addr
;
2154 addr
->sin_port
= htons(port_int
);
2155 return GSOCK_NOERROR
;
2158 address
->m_error
= GSOCK_INVPORT
;
2159 return GSOCK_INVPORT
;
2162 addr
= (struct sockaddr_in
*)address
->m_addr
;
2163 addr
->sin_port
= se
->s_port
;
2165 return GSOCK_NOERROR
;
2168 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
2170 struct sockaddr_in
*addr
;
2172 assert(address
!= NULL
);
2173 CHECK_ADDRESS(address
, INET
);
2175 addr
= (struct sockaddr_in
*)address
->m_addr
;
2176 addr
->sin_port
= htons(port
);
2178 return GSOCK_NOERROR
;
2181 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
2185 struct sockaddr_in
*addr
;
2187 assert(address
!= NULL
);
2188 CHECK_ADDRESS(address
, INET
);
2190 addr
= (struct sockaddr_in
*)address
->m_addr
;
2191 addr_buf
= (char *)&(addr
->sin_addr
);
2193 struct hostent temphost
;
2194 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
2195 struct hostent_data buffer
;
2200 he
= wxGethostbyaddr_r(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
, &temphost
,
2201 (void*)&buffer
, sizeof(buffer
), &err
);
2204 address
->m_error
= GSOCK_NOHOST
;
2205 return GSOCK_NOHOST
;
2208 strncpy(hostname
, he
->h_name
, sbuf
);
2210 return GSOCK_NOERROR
;
2213 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
2215 struct sockaddr_in
*addr
;
2217 assert(address
!= NULL
);
2218 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
2220 addr
= (struct sockaddr_in
*)address
->m_addr
;
2222 return ntohl(addr
->sin_addr
.s_addr
);
2225 unsigned short GAddress_INET_GetPort(GAddress
*address
)
2227 struct sockaddr_in
*addr
;
2229 assert(address
!= NULL
);
2230 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
2232 addr
= (struct sockaddr_in
*)address
->m_addr
;
2233 return ntohs(addr
->sin_port
);
2238 * -------------------------------------------------------------------------
2239 * Internet IPv6 address family
2240 * -------------------------------------------------------------------------
2243 GSocketError
_GAddress_Init_INET6(GAddress
*address
)
2245 struct in6_addr any_address
= IN6ADDR_ANY_INIT
;
2246 address
->m_len
= sizeof(struct sockaddr_in6
);
2247 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
2248 if (address
->m_addr
== NULL
)
2250 address
->m_error
= GSOCK_MEMERR
;
2251 return GSOCK_MEMERR
;
2253 memset(address
->m_addr
,0,address
->m_len
);
2255 address
->m_family
= GSOCK_INET6
;
2256 address
->m_realfamily
= AF_INET6
;
2257 ((struct sockaddr_in6
*)address
->m_addr
)->sin6_family
= AF_INET6
;
2258 ((struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
= any_address
;
2260 return GSOCK_NOERROR
;
2263 GSocketError
GAddress_INET6_SetHostName(GAddress
*address
, const char *hostname
)
2265 assert(address
!= NULL
);
2266 CHECK_ADDRESS(address
, INET6
);
2269 memset( & hints
, 0, sizeof( hints
) );
2270 hints
.ai_family
= AF_INET6
;
2271 addrinfo
* info
= 0;
2272 if ( getaddrinfo( hostname
, "0", & hints
, & info
) || ! info
)
2274 address
->m_error
= GSOCK_NOHOST
;
2275 return GSOCK_NOHOST
;
2278 memcpy( address
->m_addr
, info
->ai_addr
, info
->ai_addrlen
);
2279 freeaddrinfo( info
);
2280 return GSOCK_NOERROR
;
2283 GSocketError
GAddress_INET6_SetAnyAddress(GAddress
*address
)
2285 assert(address
!= NULL
);
2287 CHECK_ADDRESS(address
, INET6
);
2289 struct in6_addr addr
;
2290 memset( & addr
, 0, sizeof( addr
) );
2291 return GAddress_INET6_SetHostAddress(address
, addr
);
2293 GSocketError
GAddress_INET6_SetHostAddress(GAddress
*address
,
2294 struct in6_addr hostaddr
)
2296 assert(address
!= NULL
);
2298 CHECK_ADDRESS(address
, INET6
);
2300 ((struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
= hostaddr
;
2302 return GSOCK_NOERROR
;
2305 GSocketError
GAddress_INET6_SetPortName(GAddress
*address
, const char *port
,
2306 const char *protocol
)
2309 struct sockaddr_in6
*addr
;
2311 assert(address
!= NULL
);
2312 CHECK_ADDRESS(address
, INET6
);
2316 address
->m_error
= GSOCK_INVPORT
;
2317 return GSOCK_INVPORT
;
2320 se
= getservbyname(port
, protocol
);
2323 if (isdigit(port
[0]))
2327 port_int
= atoi(port
);
2328 addr
= (struct sockaddr_in6
*)address
->m_addr
;
2329 addr
->sin6_port
= htons((u_short
) port_int
);
2330 return GSOCK_NOERROR
;
2333 address
->m_error
= GSOCK_INVPORT
;
2334 return GSOCK_INVPORT
;
2337 addr
= (struct sockaddr_in6
*)address
->m_addr
;
2338 addr
->sin6_port
= se
->s_port
;
2340 return GSOCK_NOERROR
;
2343 GSocketError
GAddress_INET6_SetPort(GAddress
*address
, unsigned short port
)
2345 struct sockaddr_in6
*addr
;
2347 assert(address
!= NULL
);
2348 CHECK_ADDRESS(address
, INET6
);
2350 addr
= (struct sockaddr_in6
*)address
->m_addr
;
2351 addr
->sin6_port
= htons(port
);
2353 return GSOCK_NOERROR
;
2356 GSocketError
GAddress_INET6_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
2360 struct sockaddr_in6
*addr
;
2362 assert(address
!= NULL
);
2363 CHECK_ADDRESS(address
, INET6
);
2365 addr
= (struct sockaddr_in6
*)address
->m_addr
;
2366 addr_buf
= (char *)&(addr
->sin6_addr
);
2368 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin6_addr
), AF_INET6
);
2371 address
->m_error
= GSOCK_NOHOST
;
2372 return GSOCK_NOHOST
;
2375 strncpy(hostname
, he
->h_name
, sbuf
);
2377 return GSOCK_NOERROR
;
2380 GSocketError
GAddress_INET6_GetHostAddress(GAddress
*address
,struct in6_addr
*hostaddr
)
2382 assert(address
!= NULL
);
2383 assert(hostaddr
!= NULL
);
2384 CHECK_ADDRESS_RETVAL(address
, INET6
, GSOCK_INVADDR
);
2385 *hostaddr
= ( (struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
;
2386 return GSOCK_NOERROR
;
2389 unsigned short GAddress_INET6_GetPort(GAddress
*address
)
2391 assert(address
!= NULL
);
2392 CHECK_ADDRESS_RETVAL(address
, INET6
, 0);
2394 return ntohs( ((struct sockaddr_in6
*)address
->m_addr
)->sin6_port
);
2397 #endif // wxUSE_IPV6
2400 * -------------------------------------------------------------------------
2401 * Unix address family
2402 * -------------------------------------------------------------------------
2405 #ifndef __VISAGECPP__
2406 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
2408 address
->m_len
= sizeof(struct sockaddr_un
);
2409 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
2410 if (address
->m_addr
== NULL
)
2412 address
->m_error
= GSOCK_MEMERR
;
2413 return GSOCK_MEMERR
;
2416 address
->m_family
= GSOCK_UNIX
;
2417 address
->m_realfamily
= PF_UNIX
;
2418 ((struct sockaddr_un
*)address
->m_addr
)->sun_family
= AF_UNIX
;
2419 ((struct sockaddr_un
*)address
->m_addr
)->sun_path
[0] = 0;
2421 return GSOCK_NOERROR
;
2424 #define UNIX_SOCK_PATHLEN (sizeof(addr->sun_path)/sizeof(addr->sun_path[0]))
2426 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
2428 struct sockaddr_un
*addr
;
2430 assert(address
!= NULL
);
2432 CHECK_ADDRESS(address
, UNIX
);
2434 addr
= ((struct sockaddr_un
*)address
->m_addr
);
2435 strncpy(addr
->sun_path
, path
, UNIX_SOCK_PATHLEN
);
2436 addr
->sun_path
[UNIX_SOCK_PATHLEN
- 1] = '\0';
2438 return GSOCK_NOERROR
;
2441 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
2443 struct sockaddr_un
*addr
;
2445 assert(address
!= NULL
);
2446 CHECK_ADDRESS(address
, UNIX
);
2448 addr
= (struct sockaddr_un
*)address
->m_addr
;
2450 strncpy(path
, addr
->sun_path
, sbuf
);
2452 return GSOCK_NOERROR
;
2454 #endif /* !defined(__VISAGECPP__) */
2455 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */