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 #include "wx/wxprec.h"
19 #include "wx/private/gsocket.h"
21 #include "wx/private/fd.h"
22 #include "wx/private/socket.h"
23 #include "wx/private/gsocketiohandler.h"
25 #if defined(__VISAGECPP__)
26 #define BSD_SELECT /* use Berkeley Sockets select */
29 #if defined(__WATCOMC__)
35 #include <sys/types.h>
40 #include <netinet/in.h>
43 #include <sys/ioctl.h>
45 #ifdef HAVE_SYS_SELECT_H
46 # include <sys/select.h>
53 u_char sun_len
; /* sockaddr len including null */
54 u_char sun_family
; /* AF_UNIX */
55 char sun_path
[108]; /* path name (gag) */
58 #include <sys/socket.h>
64 #include <netinet/in.h>
65 #include <arpa/inet.h>
72 #include <machine/endian.h>
78 #define EBADF SOCEBADF
84 #include <sys/socket.h>
85 #include <sys/ioctl.h>
86 #include <sys/select.h>
88 #define close(a) soclose(a)
89 #define select(a,b,c,d,e) bsdselect(a,b,c,d,e)
90 int _System
bsdselect(int,
95 int _System
soclose(int);
99 #include <sys/select.h>
107 # include <sys/filio.h>
110 # include <bstring.h>
113 # include <strings.h>
120 # define WX_SOCKLEN_T unsigned int
124 # define WX_SOCKLEN_T socklen_t
126 # elif defined(__WXMAC__)
127 # define WX_SOCKLEN_T socklen_t
129 # define WX_SOCKLEN_T int
133 #endif /* SOCKLEN_T */
136 #define SOCKOPTLEN_T WX_SOCKLEN_T
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 #if wxUSE_THREADS && (defined(HAVE_GETHOSTBYNAME) || defined(HAVE_GETSERVBYNAME))
181 # include "wx/thread.h"
184 #if defined(HAVE_GETHOSTBYNAME)
185 static struct hostent
* deepCopyHostent(struct hostent
*h
,
186 const struct hostent
*he
,
187 char *buffer
, int size
, int *err
)
189 /* copy old structure */
190 memcpy(h
, he
, sizeof(struct hostent
));
193 int len
= strlen(h
->h_name
);
199 memcpy(buffer
, h
->h_name
, len
);
203 /* track position in the buffer */
206 /* reuse len to store address length */
209 /* ensure pointer alignment */
210 unsigned int misalign
= sizeof(char *) - pos%sizeof
(char *);
211 if(misalign
< sizeof(char *))
214 /* leave space for pointer list */
215 char **p
= h
->h_addr_list
, **q
;
216 char **h_addr_list
= (char **)(buffer
+ pos
);
218 pos
+= sizeof(char *);
220 /* copy addresses and fill new pointer list */
221 for (p
= h
->h_addr_list
, q
= h_addr_list
; *p
!= 0; p
++, q
++)
223 if (size
< pos
+ len
)
228 memcpy(buffer
+ pos
, *p
, len
); /* copy content */
229 *q
= buffer
+ pos
; /* set copied pointer to copied content */
232 *++q
= 0; /* null terminate the pointer list */
233 h
->h_addr_list
= h_addr_list
; /* copy pointer to pointers */
235 /* ensure word alignment of pointers */
236 misalign
= sizeof(char *) - pos%sizeof
(char *);
237 if(misalign
< sizeof(char *))
240 /* leave space for pointer list */
242 char **h_aliases
= (char **)(buffer
+ pos
);
244 pos
+= sizeof(char *);
246 /* copy aliases and fill new pointer list */
247 for (p
= h
->h_aliases
, q
= h_aliases
; *p
!= 0; p
++, q
++)
250 if (size
<= pos
+ len
)
255 memcpy(buffer
+ pos
, *p
, len
); /* copy content */
256 buffer
[pos
+ len
] = '\0';
257 *q
= buffer
+ pos
; /* set copied pointer to copied content */
260 *++q
= 0; /* null terminate the pointer list */
261 h
->h_aliases
= h_aliases
; /* copy pointer to pointers */
267 #if defined(HAVE_GETHOSTBYNAME) && wxUSE_THREADS
268 static wxMutex nameLock
;
270 struct hostent
* wxGethostbyname_r(const char *hostname
, struct hostent
*h
,
271 void *buffer
, int size
, int *err
)
274 struct hostent
*he
= NULL
;
276 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_6)
277 if (gethostbyname_r(hostname
, h
, (char*)buffer
, size
, &he
, err
))
279 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_5)
280 he
= gethostbyname_r(hostname
, h
, (char*)buffer
, size
, err
);
281 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
282 if (gethostbyname_r(hostname
, h
, (struct hostent_data
*) buffer
))
289 #elif defined(HAVE_GETHOSTBYNAME)
291 wxMutexLocker
locker(nameLock
);
293 he
= gethostbyname(hostname
);
297 he
= deepCopyHostent(h
, he
, (char*)buffer
, size
, err
);
302 #if defined(HAVE_GETHOSTBYNAME) && wxUSE_THREADS
303 static wxMutex addrLock
;
305 struct hostent
* wxGethostbyaddr_r(const char *addr_buf
, int buf_size
,
306 int proto
, struct hostent
*h
,
307 void *buffer
, int size
, int *err
)
309 struct hostent
*he
= NULL
;
311 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_6)
312 if (gethostbyaddr_r(addr_buf
, buf_size
, proto
, h
,
313 (char*)buffer
, size
, &he
, err
))
315 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_5)
316 he
= gethostbyaddr_r(addr_buf
, buf_size
, proto
, h
, (char*)buffer
, size
, err
);
317 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
318 if (gethostbyaddr_r(addr_buf
, buf_size
, proto
, h
,
319 (struct hostent_data
*) buffer
))
326 #elif defined(HAVE_GETHOSTBYNAME)
328 wxMutexLocker
locker(addrLock
);
330 he
= gethostbyaddr(addr_buf
, buf_size
, proto
);
334 he
= deepCopyHostent(h
, he
, (char*)buffer
, size
, err
);
339 #if defined(HAVE_GETSERVBYNAME)
340 static struct servent
* deepCopyServent(struct servent
*s
,
341 const struct servent
*se
,
342 char *buffer
, int size
)
344 /* copy plain old structure */
345 memcpy(s
, se
, sizeof(struct servent
));
348 int len
= strlen(s
->s_name
);
353 memcpy(buffer
, s
->s_name
, len
);
357 /* track position in the buffer */
361 len
= strlen(s
->s_proto
);
362 if (pos
+ len
>= size
)
366 memcpy(buffer
+ pos
, s
->s_proto
, len
);
367 buffer
[pos
+ len
] = '\0';
368 s
->s_proto
= buffer
+ pos
;
370 /* track position in the buffer */
373 /* ensure pointer alignment */
374 unsigned int misalign
= sizeof(char *) - pos%sizeof
(char *);
375 if(misalign
< sizeof(char *))
378 /* leave space for pointer list */
379 char **p
= s
->s_aliases
, **q
;
380 char **s_aliases
= (char **)(buffer
+ pos
);
382 pos
+= sizeof(char *);
384 /* copy addresses and fill new pointer list */
385 for (p
= s
->s_aliases
, q
= s_aliases
; *p
!= 0; p
++, q
++){
387 if (size
<= pos
+ len
)
391 memcpy(buffer
+ pos
, *p
, len
); /* copy content */
392 buffer
[pos
+ len
] = '\0';
393 *q
= buffer
+ pos
; /* set copied pointer to copied content */
396 *++q
= 0; /* null terminate the pointer list */
397 s
->s_aliases
= s_aliases
; /* copy pointer to pointers */
402 #if defined(HAVE_GETSERVBYNAME) && wxUSE_THREADS
403 static wxMutex servLock
;
405 struct servent
*wxGetservbyname_r(const char *port
, const char *protocol
,
406 struct servent
*serv
, void *buffer
, int size
)
408 struct servent
*se
= NULL
;
409 #if defined(HAVE_FUNC_GETSERVBYNAME_R_6)
410 if (getservbyname_r(port
, protocol
, serv
, (char*)buffer
, size
, &se
))
412 #elif defined(HAVE_FUNC_GETSERVBYNAME_R_5)
413 se
= getservbyname_r(port
, protocol
, serv
, (char*)buffer
, size
);
414 #elif defined(HAVE_FUNC_GETSERVBYNAME_R_4)
415 if (getservbyname_r(port
, protocol
, serv
, (struct servent_data
*) buffer
))
419 #elif defined(HAVE_GETSERVBYNAME)
421 wxMutexLocker
locker(servLock
);
423 se
= getservbyname(port
, protocol
);
425 se
= deepCopyServent(serv
, se
, (char*)buffer
, size
);
430 /* debugging helpers */
431 #ifdef __GSOCKET_DEBUG__
432 # define GSocket_Debug(args) printf args
434 # define GSocket_Debug(args)
435 #endif /* __GSOCKET_DEBUG__ */
437 /* Table of GUI-related functions. We must call them indirectly because
438 * of wxBase and GUI separation: */
442 GSocketManager
* const manager
= GSocketManager::Get();
443 return manager
&& manager
->OnInit();
446 void GSocket_Cleanup()
448 GSocketManager
* const manager
= GSocketManager::Get();
453 /* Constructors / Destructors for GSocket */
455 GSocket::GSocket(wxSocketBase
& wxsocket
)
456 : GSocketBase(wxsocket
)
460 m_gui_dependent
= NULL
;
461 m_use_events
= false;
470 * Disallow further read/write operations on this socket, close
471 * the fd and disable all callbacks.
473 void GSocket::Shutdown()
475 /* Don't allow events to fire after socket has been closed */
478 GSocketBase::Shutdown();
481 /* Server specific parts */
483 /* GSocket_SetServer:
484 * Sets up this socket as a server. The local address must have been
485 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
486 * Returns GSOCK_NOERROR on success, one of the following otherwise:
489 * GSOCK_INVSOCK - the socket is in use.
490 * GSOCK_INVADDR - the local address has not been set.
491 * GSOCK_IOERR - low-level error.
493 GSocketError
GSocket::SetServer()
499 /* must not be in use */
500 if (m_fd
!= INVALID_SOCKET
)
502 m_error
= GSOCK_INVSOCK
;
503 return GSOCK_INVSOCK
;
506 /* the local addr must have been set */
509 m_error
= GSOCK_INVADDR
;
510 return GSOCK_INVADDR
;
513 /* Initialize all fields */
517 /* Create the socket */
518 m_fd
= socket(m_local
->m_realfamily
, SOCK_STREAM
, 0);
520 if (m_fd
== INVALID_SOCKET
)
522 m_error
= GSOCK_IOERR
;
526 /* FreeBSD variants can't use MSG_NOSIGNAL, and instead use a socket option */
528 setsockopt(m_fd
, SOL_SOCKET
, SO_NOSIGPIPE
, (const char*)&arg
, sizeof(arg
));
531 ioctl(m_fd
, FIONBIO
, &arg
);
534 /* allow a socket to re-bind if the socket is in the TIME_WAIT
535 state after being previously closed.
539 setsockopt(m_fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char*)&arg
, sizeof(arg
));
541 setsockopt(m_fd
, SOL_SOCKET
, SO_REUSEPORT
, (const char*)&arg
, sizeof(arg
));
545 /* Bind to the local address,
546 * retrieve the actual address bound,
547 * and listen up to 5 connections.
549 if ((bind(m_fd
, m_local
->m_addr
, m_local
->m_len
) != 0) ||
552 (WX_SOCKLEN_T
*) &m_local
->m_len
) != 0) ||
553 (listen(m_fd
, 5) != 0))
556 m_error
= GSOCK_IOERR
;
560 return GSOCK_NOERROR
;
563 /* GSocket_WaitConnection:
564 * Waits for an incoming client connection. Returns a pointer to
565 * a GSocket object, or NULL if there was an error, in which case
566 * the last error field will be updated for the calling GSocket.
568 * Error codes (set in the calling GSocket)
569 * GSOCK_INVSOCK - the socket is not valid or not a server.
570 * GSOCK_TIMEDOUT - timeout, no incoming connections.
571 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
572 * GSOCK_MEMERR - couldn't allocate memory.
573 * GSOCK_IOERR - low-level error.
575 GSocket
*GSocket::WaitConnection(wxSocketBase
& wxsocket
)
578 WX_SOCKLEN_T fromlen
= sizeof(from
);
585 /* If the socket has already been created, we exit immediately */
586 if (m_fd
== INVALID_SOCKET
|| !m_server
)
588 m_error
= GSOCK_INVSOCK
;
592 /* Create a GSocket object for the new connection */
593 connection
= GSocket::Create(wxsocket
);
597 m_error
= GSOCK_MEMERR
;
601 /* Wait for a connection (with timeout) */
602 if (Input_Timeout() == GSOCK_TIMEDOUT
)
605 /* m_error set by _GSocket_Input_Timeout */
609 connection
->m_fd
= accept(m_fd
, (sockaddr
*)&from
, (WX_SOCKLEN_T
*) &fromlen
);
611 /* Reenable CONNECTION events */
612 EnableEvent(GSOCK_CONNECTION
);
614 if (connection
->m_fd
== INVALID_SOCKET
)
616 if (errno
== EWOULDBLOCK
)
617 m_error
= GSOCK_WOULDBLOCK
;
619 m_error
= GSOCK_IOERR
;
625 /* Initialize all fields */
626 connection
->m_server
= false;
627 connection
->m_stream
= true;
629 /* Setup the peer address field */
630 connection
->m_peer
= GAddress_new();
631 if (!connection
->m_peer
)
634 m_error
= GSOCK_MEMERR
;
638 err
= _GAddress_translate_from(connection
->m_peer
, (sockaddr
*)&from
, fromlen
);
639 if (err
!= GSOCK_NOERROR
)
646 #if defined(__EMX__) || defined(__VISAGECPP__)
647 ioctl(connection
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
649 ioctl(connection
->m_fd
, FIONBIO
, &arg
);
652 connection
->Notify(true);
657 void GSocket::Notify(bool flag
)
659 if (flag
== m_use_events
)
662 DoEnableEvents(flag
);
665 void GSocket::DoEnableEvents(bool flag
)
667 GSocketManager
* const manager
= GSocketManager::Get();
670 manager
->Install_Callback(this, GSOCK_INPUT
);
671 manager
->Install_Callback(this, GSOCK_OUTPUT
);
675 manager
->Uninstall_Callback(this, GSOCK_INPUT
);
676 manager
->Uninstall_Callback(this, GSOCK_OUTPUT
);
680 bool GSocket::SetReusable()
682 /* socket must not be null, and must not be in use/already bound */
683 if (this && m_fd
== INVALID_SOCKET
)
693 bool GSocket::SetBroadcast()
695 /* socket must not be in use/already bound */
696 if (m_fd
== INVALID_SOCKET
) {
703 bool GSocket::DontDoBind()
705 /* socket must not be in use/already bound */
706 if (m_fd
== INVALID_SOCKET
) {
713 /* Client specific parts */
716 * For stream (connection oriented) sockets, GSocket_Connect() tries
717 * to establish a client connection to a server using the peer address
718 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
719 * connection has been successfully established, or one of the error
720 * codes listed below. Note that for nonblocking sockets, a return
721 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
722 * request can be completed later; you should use GSocket_Select()
723 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
724 * corresponding asynchronous events.
726 * For datagram (non connection oriented) sockets, GSocket_Connect()
727 * just sets the peer address established with GSocket_SetPeer() as
728 * default destination.
731 * GSOCK_INVSOCK - the socket is in use or not valid.
732 * GSOCK_INVADDR - the peer address has not been established.
733 * GSOCK_TIMEDOUT - timeout, the connection failed.
734 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
735 * GSOCK_MEMERR - couldn't allocate memory.
736 * GSOCK_IOERR - low-level error.
738 GSocketError
GSocket::Connect(GSocketStream stream
)
745 /* Enable CONNECTION events (needed for nonblocking connections) */
746 EnableEvent(GSOCK_CONNECTION
);
748 if (m_fd
!= INVALID_SOCKET
)
750 m_error
= GSOCK_INVSOCK
;
751 return GSOCK_INVSOCK
;
756 m_error
= GSOCK_INVADDR
;
757 return GSOCK_INVADDR
;
760 /* Streamed or dgram socket? */
761 m_stream
= (stream
== GSOCK_STREAMED
);
763 m_establishing
= false;
765 /* Create the socket */
766 m_fd
= socket(m_peer
->m_realfamily
,
767 m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
769 if (m_fd
== INVALID_SOCKET
)
771 m_error
= GSOCK_IOERR
;
775 /* FreeBSD variants can't use MSG_NOSIGNAL, and instead use a socket option */
777 setsockopt(m_fd
, SOL_SOCKET
, SO_NOSIGPIPE
, (const char*)&arg
, sizeof(arg
));
780 #if defined(__EMX__) || defined(__VISAGECPP__)
781 ioctl(m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
783 ioctl(m_fd
, FIONBIO
, &arg
);
786 // If the reuse flag is set, use the applicable socket reuse flags(s)
789 setsockopt(m_fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char*)&arg
, sizeof(arg
));
791 setsockopt(m_fd
, SOL_SOCKET
, SO_REUSEPORT
, (const char*)&arg
, sizeof(arg
));
795 if (m_initialRecvBufferSize
>= 0)
796 setsockopt(m_fd
, SOL_SOCKET
, SO_RCVBUF
, (const char*)&m_initialRecvBufferSize
, sizeof(m_initialRecvBufferSize
));
797 if (m_initialSendBufferSize
>= 0)
798 setsockopt(m_fd
, SOL_SOCKET
, SO_SNDBUF
, (const char*)&m_initialSendBufferSize
, sizeof(m_initialSendBufferSize
));
800 // If a local address has been set, then we need to bind to it before calling connect
801 if (m_local
&& m_local
->m_addr
)
803 bind(m_fd
, m_local
->m_addr
, m_local
->m_len
);
806 /* Connect it to the peer address, with a timeout (see below) */
807 ret
= connect(m_fd
, m_peer
->m_addr
, m_peer
->m_len
);
809 /* We only call EnableEvents() if we know we aren't shutting down the socket.
810 * NB: EnableEvents() needs to be called whether the socket is blocking or
811 * non-blocking, it just shouldn't be called prior to knowing there is a
812 * connection _if_ blocking sockets are being used.
813 * If connect above returns 0, we are already connected and need to make the
814 * call to EnableEvents() now.
816 if ( m_non_blocking
|| (ret
== 0) )
823 /* If connect failed with EINPROGRESS and the GSocket object
824 * is in blocking mode, we select() for the specified timeout
825 * checking for writability to see if the connection request
828 if ((err
== EINPROGRESS
) && (!m_non_blocking
))
830 if (Output_Timeout() == GSOCK_TIMEDOUT
)
833 /* m_error is set in _GSocket_Output_Timeout */
834 return GSOCK_TIMEDOUT
;
839 SOCKOPTLEN_T len
= sizeof(error
);
841 getsockopt(m_fd
, SOL_SOCKET
, SO_ERROR
, (char*) &error
, &len
);
845 return GSOCK_NOERROR
;
849 /* If connect failed with EINPROGRESS and the GSocket object
850 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
851 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
852 * this way if the connection completes, a GSOCK_CONNECTION
853 * event will be generated, if enabled.
855 if ((err
== EINPROGRESS
) && (m_non_blocking
))
857 m_establishing
= true;
858 m_error
= GSOCK_WOULDBLOCK
;
859 return GSOCK_WOULDBLOCK
;
862 /* If connect failed with an error other than EINPROGRESS,
863 * then the call to GSocket_Connect has failed.
866 m_error
= GSOCK_IOERR
;
871 return GSOCK_NOERROR
;
874 /* Datagram sockets */
876 /* GSocket_SetNonOriented:
877 * Sets up this socket as a non-connection oriented (datagram) socket.
878 * Before using this function, the local address must have been set
879 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
880 * on success, or one of the following otherwise.
883 * GSOCK_INVSOCK - the socket is in use.
884 * GSOCK_INVADDR - the local address has not been set.
885 * GSOCK_IOERR - low-level error.
887 GSocketError
GSocket::SetNonOriented()
893 if (m_fd
!= INVALID_SOCKET
)
895 m_error
= GSOCK_INVSOCK
;
896 return GSOCK_INVSOCK
;
901 m_error
= GSOCK_INVADDR
;
902 return GSOCK_INVADDR
;
905 /* Initialize all fields */
909 /* Create the socket */
910 m_fd
= socket(m_local
->m_realfamily
, SOCK_DGRAM
, 0);
912 if (m_fd
== INVALID_SOCKET
)
914 m_error
= GSOCK_IOERR
;
917 #if defined(__EMX__) || defined(__VISAGECPP__)
918 ioctl(m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
920 ioctl(m_fd
, FIONBIO
, &arg
);
926 setsockopt(m_fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char*)&arg
, sizeof(arg
));
928 setsockopt(m_fd
, SOL_SOCKET
, SO_REUSEPORT
, (const char*)&arg
, sizeof(arg
));
934 setsockopt(m_fd
, SOL_SOCKET
, SO_BROADCAST
, (const char*)&arg
, sizeof(arg
));
938 /* Bind to the local address,
939 * and retrieve the actual address bound.
941 if ((bind(m_fd
, m_local
->m_addr
, m_local
->m_len
) != 0) ||
944 (WX_SOCKLEN_T
*) &m_local
->m_len
) != 0))
947 m_error
= GSOCK_IOERR
;
951 return GSOCK_NOERROR
;
956 /* Like recv(), send(), ... */
957 int GSocket::Read(char *buffer
, int size
)
963 if (m_fd
== INVALID_SOCKET
|| m_server
)
965 m_error
= GSOCK_INVSOCK
;
969 /* Disable events during query of socket status */
970 DisableEvent(GSOCK_INPUT
);
972 /* If the socket is blocking, wait for data (with a timeout) */
973 if (Input_Timeout() == GSOCK_TIMEDOUT
) {
974 m_error
= GSOCK_TIMEDOUT
;
975 /* Don't return here immediately, otherwise socket events would not be
983 ret
= Recv_Stream(buffer
, size
);
985 ret
= Recv_Dgram(buffer
, size
);
988 * If recv returned zero for a TCP socket (if m_stream == NULL, it's an UDP
989 * socket and empty datagrams are possible), then the connection has been
992 * Otherwise, recv has returned an error (-1), in which case we have lost
993 * the socket only if errno does _not_ indicate that there may be more data
996 if ((ret
== 0) && m_stream
)
998 /* Make sure wxSOCKET_LOST event gets sent and shut down the socket */
1001 m_detected
= GSOCK_LOST_FLAG
;
1008 if ((errno
== EWOULDBLOCK
) || (errno
== EAGAIN
))
1009 m_error
= GSOCK_WOULDBLOCK
;
1011 m_error
= GSOCK_IOERR
;
1015 /* Enable events again now that we are done processing */
1016 EnableEvent(GSOCK_INPUT
);
1021 int GSocket::Write(const char *buffer
, int size
)
1027 GSocket_Debug(( "GSocket_Write #1, size %d\n", size
));
1029 if (m_fd
== INVALID_SOCKET
|| m_server
)
1031 m_error
= GSOCK_INVSOCK
;
1035 GSocket_Debug(( "GSocket_Write #2, size %d\n", size
));
1037 /* If the socket is blocking, wait for writability (with a timeout) */
1038 if (Output_Timeout() == GSOCK_TIMEDOUT
)
1041 GSocket_Debug(( "GSocket_Write #3, size %d\n", size
));
1043 /* Write the data */
1045 ret
= Send_Stream(buffer
, size
);
1047 ret
= Send_Dgram(buffer
, size
);
1049 GSocket_Debug(( "GSocket_Write #4, size %d\n", size
));
1053 if ((errno
== EWOULDBLOCK
) || (errno
== EAGAIN
))
1055 m_error
= GSOCK_WOULDBLOCK
;
1056 GSocket_Debug(( "GSocket_Write error WOULDBLOCK\n" ));
1060 m_error
= GSOCK_IOERR
;
1061 GSocket_Debug(( "GSocket_Write error IOERR\n" ));
1064 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
1065 * in MSW). Once the first OUTPUT event is received, users can assume
1066 * that the socket is writable until a read operation fails. Only then
1067 * will further OUTPUT events be posted.
1069 EnableEvent(GSOCK_OUTPUT
);
1074 GSocket_Debug(( "GSocket_Write #5, size %d ret %d\n", size
, ret
));
1081 /* GSocket_SetNonBlocking:
1082 * Sets the socket to non-blocking mode. All IO calls will return
1085 void GSocket::SetNonBlocking(bool non_block
)
1089 GSocket_Debug( ("GSocket_SetNonBlocking: %d\n", (int)non_block
) );
1091 m_non_blocking
= non_block
;
1094 /* GSocket_GetError:
1095 * Returns the last error occurred for this socket. Note that successful
1096 * operations do not clear this back to GSOCK_NOERROR, so use it only
1099 GSocketError WXDLLIMPEXP_NET
GSocket::GetError()
1106 GSocketError
GSocket::GetSockOpt(int level
, int optname
,
1107 void *optval
, int *optlen
)
1109 if (getsockopt(m_fd
, level
, optname
, (char*)optval
, (SOCKOPTLEN_T
*)optlen
) == 0)
1110 return GSOCK_NOERROR
;
1112 return GSOCK_OPTERR
;
1115 GSocketError
GSocket::SetSockOpt(int level
, int optname
,
1116 const void *optval
, int optlen
)
1118 if (setsockopt(m_fd
, level
, optname
, (const char*)optval
, optlen
) == 0)
1119 return GSOCK_NOERROR
;
1121 return GSOCK_OPTERR
;
1124 void GSocket::EnableEvent(GSocketEvent event
)
1128 m_detected
&= ~(1 << event
);
1129 GSocketManager::Get()->Install_Callback(this, event
);
1133 void GSocket::DisableEvent(GSocketEvent event
)
1137 m_detected
|= (1 << event
);
1138 GSocketManager::Get()->Uninstall_Callback(this, event
);
1142 /* _GSocket_Input_Timeout:
1143 * For blocking sockets, wait until data is available or
1144 * until timeout ellapses.
1146 GSocketError
GSocket::Input_Timeout()
1151 // Linux select() will overwrite the struct on return so make a copy
1152 struct timeval tv
= m_timeout
;
1154 if (!m_non_blocking
)
1156 wxFD_ZERO(&readfds
);
1157 wxFD_SET(m_fd
, &readfds
);
1158 ret
= select(m_fd
+ 1, &readfds
, NULL
, NULL
, &tv
);
1161 GSocket_Debug(( "GSocket_Input_Timeout, select returned 0\n" ));
1162 m_error
= GSOCK_TIMEDOUT
;
1163 return GSOCK_TIMEDOUT
;
1168 GSocket_Debug(( "GSocket_Input_Timeout, select returned -1\n" ));
1169 if (errno
== EBADF
) { GSocket_Debug(( "Invalid file descriptor\n" )); }
1170 if (errno
== EINTR
) { GSocket_Debug(( "A non blocked signal was caught\n" )); }
1171 if (errno
== EINVAL
) { GSocket_Debug(( "The highest number descriptor is negative\n" )); }
1172 if (errno
== ENOMEM
) { GSocket_Debug(( "Not enough memory\n" )); }
1173 m_error
= GSOCK_TIMEDOUT
;
1174 return GSOCK_TIMEDOUT
;
1178 return GSOCK_NOERROR
;
1181 /* _GSocket_Output_Timeout:
1182 * For blocking sockets, wait until data can be sent without
1183 * blocking or until timeout ellapses.
1185 GSocketError
GSocket::Output_Timeout()
1190 // Linux select() will overwrite the struct on return so make a copy
1191 struct timeval tv
= m_timeout
;
1193 GSocket_Debug( ("m_non_blocking has: %d\n", (int)m_non_blocking
) );
1195 if (!m_non_blocking
)
1197 wxFD_ZERO(&writefds
);
1198 wxFD_SET(m_fd
, &writefds
);
1199 ret
= select(m_fd
+ 1, NULL
, &writefds
, NULL
, &tv
);
1202 GSocket_Debug(( "GSocket_Output_Timeout, select returned 0\n" ));
1203 m_error
= GSOCK_TIMEDOUT
;
1204 return GSOCK_TIMEDOUT
;
1209 GSocket_Debug(( "GSocket_Output_Timeout, select returned -1\n" ));
1210 if (errno
== EBADF
) { GSocket_Debug(( "Invalid file descriptor\n" )); }
1211 if (errno
== EINTR
) { GSocket_Debug(( "A non blocked signal was caught\n" )); }
1212 if (errno
== EINVAL
) { GSocket_Debug(( "The highest number descriptor is negative\n" )); }
1213 if (errno
== ENOMEM
) { GSocket_Debug(( "Not enough memory\n" )); }
1214 m_error
= GSOCK_TIMEDOUT
;
1215 return GSOCK_TIMEDOUT
;
1218 if ( ! wxFD_ISSET(m_fd
, &writefds
) )
1220 GSocket_Debug(( "GSocket_Output_Timeout is buggy!\n" ));
1224 GSocket_Debug(( "GSocket_Output_Timeout seems correct\n" ));
1229 GSocket_Debug(( "GSocket_Output_Timeout, didn't try select!\n" ));
1232 return GSOCK_NOERROR
;
1235 int GSocket::Recv_Stream(char *buffer
, int size
)
1240 ret
= recv(m_fd
, buffer
, size
, GSOCKET_MSG_NOSIGNAL
);
1242 while (ret
== -1 && errno
== EINTR
); /* Loop until not interrupted */
1247 int GSocket::Recv_Dgram(char *buffer
, int size
)
1250 WX_SOCKLEN_T fromlen
= sizeof(from
);
1254 fromlen
= sizeof(from
);
1258 ret
= recvfrom(m_fd
, buffer
, size
, 0, (sockaddr
*)&from
, (WX_SOCKLEN_T
*) &fromlen
);
1260 while (ret
== -1 && errno
== EINTR
); /* Loop until not interrupted */
1265 /* Translate a system address into a GSocket address */
1268 m_peer
= GAddress_new();
1271 m_error
= GSOCK_MEMERR
;
1276 err
= _GAddress_translate_from(m_peer
, (sockaddr
*)&from
, fromlen
);
1277 if (err
!= GSOCK_NOERROR
)
1279 GAddress_destroy(m_peer
);
1288 int GSocket::Send_Stream(const char *buffer
, int size
)
1296 ret
= send(m_fd
, (char *)buffer
, size
, GSOCKET_MSG_NOSIGNAL
);
1298 while (ret
== -1 && errno
== EINTR
); /* Loop until not interrupted */
1305 int GSocket::Send_Dgram(const char *buffer
, int size
)
1307 struct sockaddr
*addr
;
1313 m_error
= GSOCK_INVADDR
;
1317 err
= _GAddress_translate_to(m_peer
, &addr
, &len
);
1318 if (err
!= GSOCK_NOERROR
)
1328 ret
= sendto(m_fd
, (char *)buffer
, size
, 0, addr
, len
);
1330 while (ret
== -1 && errno
== EINTR
); /* Loop until not interrupted */
1334 /* Frees memory allocated from _GAddress_translate_to */
1340 void GSocket::OnStateChange(GSocketEvent event
)
1342 DisableEvent(event
);
1343 NotifyOnStateChange(event
);
1345 if ( event
== GSOCK_LOST
)
1349 void GSocket::Detected_Read()
1353 /* Safeguard against straggling call to Detected_Read */
1354 if (m_fd
== INVALID_SOCKET
)
1359 /* If we have already detected a LOST event, then don't try
1360 * to do any further processing.
1362 if ((m_detected
& GSOCK_LOST_FLAG
) != 0)
1364 m_establishing
= false;
1366 OnStateChange(GSOCK_LOST
);
1370 int num
= recv(m_fd
, &c
, 1, MSG_PEEK
| GSOCKET_MSG_NOSIGNAL
);
1374 OnStateChange(GSOCK_INPUT
);
1378 if (m_server
&& m_stream
)
1380 OnStateChange(GSOCK_CONNECTION
);
1386 /* graceful shutdown */
1387 OnStateChange(GSOCK_LOST
);
1391 /* Empty datagram received */
1392 OnStateChange(GSOCK_INPUT
);
1397 /* Do not throw a lost event in cases where the socket isn't really lost */
1398 if ((errno
== EWOULDBLOCK
) || (errno
== EAGAIN
) || (errno
== EINTR
))
1400 OnStateChange(GSOCK_INPUT
);
1404 OnStateChange(GSOCK_LOST
);
1410 void GSocket::Detected_Write()
1412 /* If we have already detected a LOST event, then don't try
1413 * to do any further processing.
1415 if ((m_detected
& GSOCK_LOST_FLAG
) != 0)
1417 m_establishing
= false;
1419 OnStateChange(GSOCK_LOST
);
1423 if (m_establishing
&& !m_server
)
1426 SOCKOPTLEN_T len
= sizeof(error
);
1428 m_establishing
= false;
1430 getsockopt(m_fd
, SOL_SOCKET
, SO_ERROR
, (char*)&error
, &len
);
1434 OnStateChange(GSOCK_LOST
);
1438 OnStateChange(GSOCK_CONNECTION
);
1439 /* We have to fire this event by hand because CONNECTION (for clients)
1440 * and OUTPUT are internally the same and we just disabled CONNECTION
1441 * events with the above macro.
1443 OnStateChange(GSOCK_OUTPUT
);
1448 OnStateChange(GSOCK_OUTPUT
);
1453 * -------------------------------------------------------------------------
1455 * -------------------------------------------------------------------------
1458 /* CHECK_ADDRESS verifies that the current address family is either
1459 * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it
1460 * initalizes it to be a GSOCK_*family*. In other cases, it returns
1461 * an appropiate error code.
1463 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
1465 #define CHECK_ADDRESS(address, family) \
1467 if (address->m_family == GSOCK_NOFAMILY) \
1468 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1469 return address->m_error; \
1470 if (address->m_family != GSOCK_##family) \
1472 address->m_error = GSOCK_INVADDR; \
1473 return GSOCK_INVADDR; \
1477 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
1479 if (address->m_family == GSOCK_NOFAMILY) \
1480 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1482 if (address->m_family != GSOCK_##family) \
1484 address->m_error = GSOCK_INVADDR; \
1490 GAddress
*GAddress_new(void)
1494 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1497 address
->m_family
= GSOCK_NOFAMILY
;
1498 address
->m_addr
= NULL
;
1504 GAddress
*GAddress_copy(GAddress
*address
)
1508 assert(address
!= NULL
);
1510 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1513 memcpy(addr2
, address
, sizeof(GAddress
));
1515 if (address
->m_addr
&& address
->m_len
> 0)
1517 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
1518 if (addr2
->m_addr
== NULL
)
1523 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1529 void GAddress_destroy(GAddress
*address
)
1531 assert(address
!= NULL
);
1533 if (address
->m_addr
)
1534 free(address
->m_addr
);
1539 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1541 assert(address
!= NULL
);
1543 address
->m_family
= type
;
1546 GAddressType
GAddress_GetFamily(GAddress
*address
)
1548 assert(address
!= NULL
);
1550 return address
->m_family
;
1553 GSocketError
_GAddress_translate_from(GAddress
*address
,
1554 struct sockaddr
*addr
, int len
)
1556 address
->m_realfamily
= addr
->sa_family
;
1557 switch (addr
->sa_family
)
1560 address
->m_family
= GSOCK_INET
;
1563 address
->m_family
= GSOCK_UNIX
;
1567 address
->m_family
= GSOCK_INET6
;
1569 #endif // wxUSE_IPV6
1572 address
->m_error
= GSOCK_INVOP
;
1577 if (address
->m_addr
)
1578 free(address
->m_addr
);
1580 address
->m_len
= len
;
1581 address
->m_addr
= (struct sockaddr
*)malloc(len
);
1583 if (address
->m_addr
== NULL
)
1585 address
->m_error
= GSOCK_MEMERR
;
1586 return GSOCK_MEMERR
;
1589 memcpy(address
->m_addr
, addr
, len
);
1591 return GSOCK_NOERROR
;
1594 GSocketError
_GAddress_translate_to(GAddress
*address
,
1595 struct sockaddr
**addr
, int *len
)
1597 if (!address
->m_addr
)
1599 address
->m_error
= GSOCK_INVADDR
;
1600 return GSOCK_INVADDR
;
1603 *len
= address
->m_len
;
1604 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
1607 address
->m_error
= GSOCK_MEMERR
;
1608 return GSOCK_MEMERR
;
1611 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1612 return GSOCK_NOERROR
;
1616 * -------------------------------------------------------------------------
1617 * Internet address family
1618 * -------------------------------------------------------------------------
1621 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1623 address
->m_len
= sizeof(struct sockaddr_in
);
1624 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1625 if (address
->m_addr
== NULL
)
1627 address
->m_error
= GSOCK_MEMERR
;
1628 return GSOCK_MEMERR
;
1631 address
->m_family
= GSOCK_INET
;
1632 address
->m_realfamily
= PF_INET
;
1633 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1634 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1636 return GSOCK_NOERROR
;
1639 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1642 struct in_addr
*addr
;
1644 assert(address
!= NULL
);
1646 CHECK_ADDRESS(address
, INET
);
1648 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1650 /* If it is a numeric host name, convert it now */
1651 #if defined(HAVE_INET_ATON)
1652 if (inet_aton(hostname
, addr
) == 0)
1654 #elif defined(HAVE_INET_ADDR)
1655 if ( (addr
->s_addr
= inet_addr(hostname
)) == (unsigned)-1 )
1658 /* Use gethostbyname by default */
1660 int val
= 1; /* VA doesn't like constants in conditional expressions */
1665 struct in_addr
*array_addr
;
1667 /* It is a real name, we solve it */
1669 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
1670 struct hostent_data buffer
;
1675 he
= wxGethostbyname_r(hostname
, &h
, (void*)&buffer
, sizeof(buffer
), &err
);
1678 /* Reset to invalid address */
1679 addr
->s_addr
= INADDR_NONE
;
1680 address
->m_error
= GSOCK_NOHOST
;
1681 return GSOCK_NOHOST
;
1684 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1685 addr
->s_addr
= array_addr
[0].s_addr
;
1688 return GSOCK_NOERROR
;
1692 GSocketError
GAddress_INET_SetBroadcastAddress(GAddress
*address
)
1694 return GAddress_INET_SetHostAddress(address
, INADDR_BROADCAST
);
1697 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1699 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1702 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1703 unsigned long hostaddr
)
1705 struct in_addr
*addr
;
1707 assert(address
!= NULL
);
1709 CHECK_ADDRESS(address
, INET
);
1711 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1712 addr
->s_addr
= htonl(hostaddr
);
1714 return GSOCK_NOERROR
;
1717 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1718 const char *protocol
)
1721 struct sockaddr_in
*addr
;
1723 assert(address
!= NULL
);
1724 CHECK_ADDRESS(address
, INET
);
1728 address
->m_error
= GSOCK_INVPORT
;
1729 return GSOCK_INVPORT
;
1732 #if defined(HAVE_FUNC_GETSERVBYNAME_R_4)
1733 struct servent_data buffer
;
1737 struct servent serv
;
1738 se
= wxGetservbyname_r(port
, protocol
, &serv
,
1739 (void*)&buffer
, sizeof(buffer
));
1742 /* the cast to int suppresses compiler warnings about subscript having the
1744 if (isdigit((int)port
[0]))
1748 port_int
= atoi(port
);
1749 addr
= (struct sockaddr_in
*)address
->m_addr
;
1750 addr
->sin_port
= htons(port_int
);
1751 return GSOCK_NOERROR
;
1754 address
->m_error
= GSOCK_INVPORT
;
1755 return GSOCK_INVPORT
;
1758 addr
= (struct sockaddr_in
*)address
->m_addr
;
1759 addr
->sin_port
= se
->s_port
;
1761 return GSOCK_NOERROR
;
1764 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1766 struct sockaddr_in
*addr
;
1768 assert(address
!= NULL
);
1769 CHECK_ADDRESS(address
, INET
);
1771 addr
= (struct sockaddr_in
*)address
->m_addr
;
1772 addr
->sin_port
= htons(port
);
1774 return GSOCK_NOERROR
;
1777 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1781 struct sockaddr_in
*addr
;
1783 assert(address
!= NULL
);
1784 CHECK_ADDRESS(address
, INET
);
1786 addr
= (struct sockaddr_in
*)address
->m_addr
;
1787 addr_buf
= (char *)&(addr
->sin_addr
);
1789 struct hostent temphost
;
1790 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
1791 struct hostent_data buffer
;
1796 he
= wxGethostbyaddr_r(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
, &temphost
,
1797 (void*)&buffer
, sizeof(buffer
), &err
);
1800 address
->m_error
= GSOCK_NOHOST
;
1801 return GSOCK_NOHOST
;
1804 strncpy(hostname
, he
->h_name
, sbuf
);
1806 return GSOCK_NOERROR
;
1809 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1811 struct sockaddr_in
*addr
;
1813 assert(address
!= NULL
);
1814 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1816 addr
= (struct sockaddr_in
*)address
->m_addr
;
1818 return ntohl(addr
->sin_addr
.s_addr
);
1821 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1823 struct sockaddr_in
*addr
;
1825 assert(address
!= NULL
);
1826 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1828 addr
= (struct sockaddr_in
*)address
->m_addr
;
1829 return ntohs(addr
->sin_port
);
1834 * -------------------------------------------------------------------------
1835 * Internet IPv6 address family
1836 * -------------------------------------------------------------------------
1839 GSocketError
_GAddress_Init_INET6(GAddress
*address
)
1841 struct in6_addr any_address
= IN6ADDR_ANY_INIT
;
1842 address
->m_len
= sizeof(struct sockaddr_in6
);
1843 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1844 if (address
->m_addr
== NULL
)
1846 address
->m_error
= GSOCK_MEMERR
;
1847 return GSOCK_MEMERR
;
1849 memset(address
->m_addr
,0,address
->m_len
);
1851 address
->m_family
= GSOCK_INET6
;
1852 address
->m_realfamily
= AF_INET6
;
1853 ((struct sockaddr_in6
*)address
->m_addr
)->sin6_family
= AF_INET6
;
1854 ((struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
= any_address
;
1856 return GSOCK_NOERROR
;
1859 GSocketError
GAddress_INET6_SetHostName(GAddress
*address
, const char *hostname
)
1861 assert(address
!= NULL
);
1862 CHECK_ADDRESS(address
, INET6
);
1865 memset( & hints
, 0, sizeof( hints
) );
1866 hints
.ai_family
= AF_INET6
;
1867 addrinfo
* info
= 0;
1868 if ( getaddrinfo( hostname
, "0", & hints
, & info
) || ! info
)
1870 address
->m_error
= GSOCK_NOHOST
;
1871 return GSOCK_NOHOST
;
1874 memcpy( address
->m_addr
, info
->ai_addr
, info
->ai_addrlen
);
1875 freeaddrinfo( info
);
1876 return GSOCK_NOERROR
;
1879 GSocketError
GAddress_INET6_SetAnyAddress(GAddress
*address
)
1881 assert(address
!= NULL
);
1883 CHECK_ADDRESS(address
, INET6
);
1885 struct in6_addr addr
;
1886 memset( & addr
, 0, sizeof( addr
) );
1887 return GAddress_INET6_SetHostAddress(address
, addr
);
1889 GSocketError
GAddress_INET6_SetHostAddress(GAddress
*address
,
1890 struct in6_addr hostaddr
)
1892 assert(address
!= NULL
);
1894 CHECK_ADDRESS(address
, INET6
);
1896 ((struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
= hostaddr
;
1898 return GSOCK_NOERROR
;
1901 GSocketError
GAddress_INET6_SetPortName(GAddress
*address
, const char *port
,
1902 const char *protocol
)
1905 struct sockaddr_in6
*addr
;
1907 assert(address
!= NULL
);
1908 CHECK_ADDRESS(address
, INET6
);
1912 address
->m_error
= GSOCK_INVPORT
;
1913 return GSOCK_INVPORT
;
1916 se
= getservbyname(port
, protocol
);
1919 if (isdigit(port
[0]))
1923 port_int
= atoi(port
);
1924 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1925 addr
->sin6_port
= htons((u_short
) port_int
);
1926 return GSOCK_NOERROR
;
1929 address
->m_error
= GSOCK_INVPORT
;
1930 return GSOCK_INVPORT
;
1933 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1934 addr
->sin6_port
= se
->s_port
;
1936 return GSOCK_NOERROR
;
1939 GSocketError
GAddress_INET6_SetPort(GAddress
*address
, unsigned short port
)
1941 struct sockaddr_in6
*addr
;
1943 assert(address
!= NULL
);
1944 CHECK_ADDRESS(address
, INET6
);
1946 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1947 addr
->sin6_port
= htons(port
);
1949 return GSOCK_NOERROR
;
1952 GSocketError
GAddress_INET6_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1956 struct sockaddr_in6
*addr
;
1958 assert(address
!= NULL
);
1959 CHECK_ADDRESS(address
, INET6
);
1961 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1962 addr_buf
= (char *)&(addr
->sin6_addr
);
1964 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin6_addr
), AF_INET6
);
1967 address
->m_error
= GSOCK_NOHOST
;
1968 return GSOCK_NOHOST
;
1971 strncpy(hostname
, he
->h_name
, sbuf
);
1973 return GSOCK_NOERROR
;
1976 GSocketError
GAddress_INET6_GetHostAddress(GAddress
*address
,struct in6_addr
*hostaddr
)
1978 assert(address
!= NULL
);
1979 assert(hostaddr
!= NULL
);
1980 CHECK_ADDRESS_RETVAL(address
, INET6
, GSOCK_INVADDR
);
1981 *hostaddr
= ( (struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
;
1982 return GSOCK_NOERROR
;
1985 unsigned short GAddress_INET6_GetPort(GAddress
*address
)
1987 assert(address
!= NULL
);
1988 CHECK_ADDRESS_RETVAL(address
, INET6
, 0);
1990 return ntohs( ((struct sockaddr_in6
*)address
->m_addr
)->sin6_port
);
1993 #endif // wxUSE_IPV6
1996 * -------------------------------------------------------------------------
1997 * Unix address family
1998 * -------------------------------------------------------------------------
2001 #ifndef __VISAGECPP__
2002 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
2004 address
->m_len
= sizeof(struct sockaddr_un
);
2005 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
2006 if (address
->m_addr
== NULL
)
2008 address
->m_error
= GSOCK_MEMERR
;
2009 return GSOCK_MEMERR
;
2012 address
->m_family
= GSOCK_UNIX
;
2013 address
->m_realfamily
= PF_UNIX
;
2014 ((struct sockaddr_un
*)address
->m_addr
)->sun_family
= AF_UNIX
;
2015 ((struct sockaddr_un
*)address
->m_addr
)->sun_path
[0] = 0;
2017 return GSOCK_NOERROR
;
2020 #define UNIX_SOCK_PATHLEN (sizeof(addr->sun_path)/sizeof(addr->sun_path[0]))
2022 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
2024 struct sockaddr_un
*addr
;
2026 assert(address
!= NULL
);
2028 CHECK_ADDRESS(address
, UNIX
);
2030 addr
= ((struct sockaddr_un
*)address
->m_addr
);
2031 strncpy(addr
->sun_path
, path
, UNIX_SOCK_PATHLEN
);
2032 addr
->sun_path
[UNIX_SOCK_PATHLEN
- 1] = '\0';
2034 return GSOCK_NOERROR
;
2037 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
2039 struct sockaddr_un
*addr
;
2041 assert(address
!= NULL
);
2042 CHECK_ADDRESS(address
, UNIX
);
2044 addr
= (struct sockaddr_un
*)address
->m_addr
;
2046 strncpy(path
, addr
->sun_path
, sbuf
);
2048 return GSOCK_NOERROR
;
2050 #endif /* !defined(__VISAGECPP__) */
2051 #endif /* wxUSE_SOCKETS */