1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/unix/sockunix.cpp
3 // Purpose: wxSocketImpl implementation for Unix systems
4 // Authors: Guilhem Lavaux, Guillermo Rodriguez Garcia, David Elliott,
8 // Copyright: (c) 1997 Guilhem Lavaux
9 // (c) 2008 Vadim Zeitlin
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
14 #include "wx/wxprec.h"
18 #include "wx/private/fd.h"
19 #include "wx/private/socket.h"
20 #include "wx/unix/private/sockunix.h"
22 #if defined(__VISAGECPP__)
23 #define BSD_SELECT /* use Berkeley Sockets select */
26 #if defined(__WATCOMC__)
32 #include <sys/types.h>
37 #include <netinet/in.h>
40 #include <sys/ioctl.h>
42 #ifdef HAVE_SYS_SELECT_H
43 # include <sys/select.h>
50 u_char sun_len
; /* sockaddr len including null */
51 u_char sun_family
; /* AF_UNIX */
52 char sun_path
[108]; /* path name (gag) */
55 #include <sys/socket.h>
61 #include <netinet/in.h>
62 #include <arpa/inet.h>
69 #include <machine/endian.h>
75 #define EBADF SOCEBADF
81 #include <sys/socket.h>
82 #include <sys/ioctl.h>
83 #include <sys/select.h>
85 #define close(a) soclose(a)
86 #define select(a,b,c,d,e) bsdselect(a,b,c,d,e)
87 int _System
bsdselect(int,
92 int _System
soclose(int);
96 #include <sys/select.h>
104 # include <sys/filio.h>
107 # include <bstring.h>
110 # include <strings.h>
117 # define WX_SOCKLEN_T unsigned int
121 # define WX_SOCKLEN_T socklen_t
123 # elif defined(__WXMAC__)
124 # define WX_SOCKLEN_T socklen_t
126 # define WX_SOCKLEN_T int
130 #endif /* SOCKLEN_T */
133 #define SOCKOPTLEN_T WX_SOCKLEN_T
136 /* UnixWare reportedly needs this for FIONBIO definition */
138 #include <sys/filio.h>
142 * INADDR_BROADCAST is identical to INADDR_NONE which is not defined
143 * on all systems. INADDR_BROADCAST should be fine to indicate an error.
146 #define INADDR_NONE INADDR_BROADCAST
149 #if defined(__VISAGECPP__) || defined(__WATCOMC__)
151 #define MASK_SIGNAL() {
152 #define UNMASK_SIGNAL() }
155 extern "C" { typedef void (*wxSigHandler
)(int); }
157 #define MASK_SIGNAL() \
159 wxSigHandler old_handler = signal(SIGPIPE, SIG_IGN);
161 #define UNMASK_SIGNAL() \
162 signal(SIGPIPE, old_handler); \
167 /* If a SIGPIPE is issued by a socket call on a remotely closed socket,
168 the program will "crash" unless it explicitly handles the SIGPIPE.
169 By using MSG_NOSIGNAL, the SIGPIPE is suppressed. Later, we will
170 use SO_NOSIGPIPE (if available), the BSD equivalent. */
172 # define GSOCKET_MSG_NOSIGNAL MSG_NOSIGNAL
173 #else /* MSG_NOSIGNAL not available (FreeBSD including OS X) */
174 # define GSOCKET_MSG_NOSIGNAL 0
175 #endif /* MSG_NOSIGNAL */
177 // ----------------------------------------------------------------------------
178 // implementation of thread-safe/reentrant functions if they're missing
179 // ----------------------------------------------------------------------------
181 #if wxUSE_THREADS && (defined(HAVE_GETHOSTBYNAME) || defined(HAVE_GETSERVBYNAME))
182 # include "wx/thread.h"
185 #if defined(HAVE_GETHOSTBYNAME)
186 static struct hostent
* deepCopyHostent(struct hostent
*h
,
187 const struct hostent
*he
,
188 char *buffer
, int size
, int *err
)
190 /* copy old structure */
191 memcpy(h
, he
, sizeof(struct hostent
));
194 int len
= strlen(h
->h_name
);
200 memcpy(buffer
, h
->h_name
, len
);
204 /* track position in the buffer */
207 /* reuse len to store address length */
210 /* ensure pointer alignment */
211 unsigned int misalign
= sizeof(char *) - pos
%sizeof(char *);
212 if(misalign
< sizeof(char *))
215 /* leave space for pointer list */
216 char **p
= h
->h_addr_list
, **q
;
217 char **h_addr_list
= (char **)(buffer
+ pos
);
219 pos
+= sizeof(char *);
221 /* copy addresses and fill new pointer list */
222 for (p
= h
->h_addr_list
, q
= h_addr_list
; *p
!= 0; p
++, q
++)
224 if (size
< pos
+ len
)
229 memcpy(buffer
+ pos
, *p
, len
); /* copy content */
230 *q
= buffer
+ pos
; /* set copied pointer to copied content */
233 *++q
= 0; /* null terminate the pointer list */
234 h
->h_addr_list
= h_addr_list
; /* copy pointer to pointers */
236 /* ensure word alignment of pointers */
237 misalign
= sizeof(char *) - pos
%sizeof(char *);
238 if(misalign
< sizeof(char *))
241 /* leave space for pointer list */
243 char **h_aliases
= (char **)(buffer
+ pos
);
245 pos
+= sizeof(char *);
247 /* copy aliases and fill new pointer list */
248 for (p
= h
->h_aliases
, q
= h_aliases
; *p
!= 0; p
++, q
++)
251 if (size
<= pos
+ len
)
256 memcpy(buffer
+ pos
, *p
, len
); /* copy content */
257 buffer
[pos
+ len
] = '\0';
258 *q
= buffer
+ pos
; /* set copied pointer to copied content */
261 *++q
= 0; /* null terminate the pointer list */
262 h
->h_aliases
= h_aliases
; /* copy pointer to pointers */
268 #if defined(HAVE_GETHOSTBYNAME) && wxUSE_THREADS
269 static wxMutex nameLock
;
271 struct hostent
* wxGethostbyname_r(const char *hostname
, struct hostent
*h
,
272 void *buffer
, int size
, int *err
)
275 struct hostent
*he
= NULL
;
277 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_6)
278 if (gethostbyname_r(hostname
, h
, (char*)buffer
, size
, &he
, err
))
280 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_5)
281 he
= gethostbyname_r(hostname
, h
, (char*)buffer
, size
, err
);
282 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
283 if (gethostbyname_r(hostname
, h
, (struct hostent_data
*) buffer
))
290 #elif defined(HAVE_GETHOSTBYNAME)
292 wxMutexLocker
locker(nameLock
);
294 he
= gethostbyname(hostname
);
298 he
= deepCopyHostent(h
, he
, (char*)buffer
, size
, err
);
303 #if defined(HAVE_GETHOSTBYNAME) && wxUSE_THREADS
304 static wxMutex addrLock
;
306 struct hostent
* wxGethostbyaddr_r(const char *addr_buf
, int buf_size
,
307 int proto
, struct hostent
*h
,
308 void *buffer
, int size
, int *err
)
310 struct hostent
*he
= NULL
;
312 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_6)
313 if (gethostbyaddr_r(addr_buf
, buf_size
, proto
, h
,
314 (char*)buffer
, size
, &he
, err
))
316 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_5)
317 he
= gethostbyaddr_r(addr_buf
, buf_size
, proto
, h
, (char*)buffer
, size
, err
);
318 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
319 if (gethostbyaddr_r(addr_buf
, buf_size
, proto
, h
,
320 (struct hostent_data
*) buffer
))
327 #elif defined(HAVE_GETHOSTBYNAME)
329 wxMutexLocker
locker(addrLock
);
331 he
= gethostbyaddr(addr_buf
, buf_size
, proto
);
335 he
= deepCopyHostent(h
, he
, (char*)buffer
, size
, err
);
340 #if defined(HAVE_GETSERVBYNAME)
341 static struct servent
* deepCopyServent(struct servent
*s
,
342 const struct servent
*se
,
343 char *buffer
, int size
)
345 /* copy plain old structure */
346 memcpy(s
, se
, sizeof(struct servent
));
349 int len
= strlen(s
->s_name
);
354 memcpy(buffer
, s
->s_name
, len
);
358 /* track position in the buffer */
362 len
= strlen(s
->s_proto
);
363 if (pos
+ len
>= size
)
367 memcpy(buffer
+ pos
, s
->s_proto
, len
);
368 buffer
[pos
+ len
] = '\0';
369 s
->s_proto
= buffer
+ pos
;
371 /* track position in the buffer */
374 /* ensure pointer alignment */
375 unsigned int misalign
= sizeof(char *) - pos
%sizeof(char *);
376 if(misalign
< sizeof(char *))
379 /* leave space for pointer list */
380 char **p
= s
->s_aliases
, **q
;
381 char **s_aliases
= (char **)(buffer
+ pos
);
383 pos
+= sizeof(char *);
385 /* copy addresses and fill new pointer list */
386 for (p
= s
->s_aliases
, q
= s_aliases
; *p
!= 0; p
++, q
++){
388 if (size
<= pos
+ len
)
392 memcpy(buffer
+ pos
, *p
, len
); /* copy content */
393 buffer
[pos
+ len
] = '\0';
394 *q
= buffer
+ pos
; /* set copied pointer to copied content */
397 *++q
= 0; /* null terminate the pointer list */
398 s
->s_aliases
= s_aliases
; /* copy pointer to pointers */
403 #if defined(HAVE_GETSERVBYNAME) && wxUSE_THREADS
404 static wxMutex servLock
;
406 struct servent
*wxGetservbyname_r(const char *port
, const char *protocol
,
407 struct servent
*serv
, void *buffer
, int size
)
409 struct servent
*se
= NULL
;
410 #if defined(HAVE_FUNC_GETSERVBYNAME_R_6)
411 if (getservbyname_r(port
, protocol
, serv
, (char*)buffer
, size
, &se
))
413 #elif defined(HAVE_FUNC_GETSERVBYNAME_R_5)
414 se
= getservbyname_r(port
, protocol
, serv
, (char*)buffer
, size
);
415 #elif defined(HAVE_FUNC_GETSERVBYNAME_R_4)
416 if (getservbyname_r(port
, protocol
, serv
, (struct servent_data
*) buffer
))
420 #elif defined(HAVE_GETSERVBYNAME)
422 wxMutexLocker
locker(servLock
);
424 se
= getservbyname(port
, protocol
);
426 se
= deepCopyServent(serv
, se
, (char*)buffer
, size
);
431 // ============================================================================
432 // wxSocketImpl implementation
433 // ============================================================================
436 wxSocketImpl
*wxSocketImpl
::Create(wxSocketBase
& wxsocket
)
438 return new wxSocketImplUnix(wxsocket
);
442 wxSocketError wxSocketImplUnix
::GetLastError() const
447 return wxSOCKET_NOERROR
;
450 return wxSOCKET_INVSOCK
;
452 // unfortunately EAGAIN only has the "would block" meaning for read(),
453 // not for connect() for which it means something rather different but
454 // we can't distinguish between these two situations currently...
457 return wxSOCKET_WOULDBLOCK
;
460 return wxSOCKET_IOERR
;
464 void wxSocketImplUnix
::DoEnableEvents(bool flag
)
466 wxSocketManager
* const manager
= wxSocketManager
::Get();
469 manager
->Install_Callback(this, wxSOCKET_INPUT
);
470 manager
->Install_Callback(this, wxSOCKET_OUTPUT
);
474 manager
->Uninstall_Callback(this, wxSOCKET_INPUT
);
475 manager
->Uninstall_Callback(this, wxSOCKET_OUTPUT
);
481 /* Like recv(), send(), ... */
482 int wxSocketImplUnix
::Read(void *buffer
, int size
)
486 if (m_fd
== INVALID_SOCKET
|| m_server
)
488 m_error
= wxSOCKET_INVSOCK
;
494 ret
= Recv_Stream(buffer
, size
);
496 ret
= Recv_Dgram(buffer
, size
);
499 * If recv returned zero for a TCP socket (if m_stream == NULL, it's an UDP
500 * socket and empty datagrams are possible), then the connection has been
503 * Otherwise, recv has returned an error (-1), in which case we have lost
504 * the socket only if errno does _not_ indicate that there may be more data
507 if ((ret
== 0) && m_stream
)
509 m_establishing
= false;
510 OnStateChange(wxSOCKET_LOST
);
515 if ((errno
== EWOULDBLOCK
) || (errno
== EAGAIN
))
516 m_error
= wxSOCKET_WOULDBLOCK
;
518 m_error
= wxSOCKET_IOERR
;
524 int wxSocketImplUnix
::Write(const void *buffer
, int size
)
528 if (m_fd
== INVALID_SOCKET
|| m_server
)
530 m_error
= wxSOCKET_INVSOCK
;
536 ret
= Send_Stream(buffer
, size
);
538 ret
= Send_Dgram(buffer
, size
);
542 if ((errno
== EWOULDBLOCK
) || (errno
== EAGAIN
))
544 m_error
= wxSOCKET_WOULDBLOCK
;
548 m_error
= wxSOCKET_IOERR
;
557 int wxSocketImplUnix
::Recv_Stream(void *buffer
, int size
)
562 ret
= recv(m_fd
, buffer
, size
, GSOCKET_MSG_NOSIGNAL
);
564 while (ret
== -1 && errno
== EINTR
); /* Loop until not interrupted */
569 int wxSocketImplUnix
::Recv_Dgram(void *buffer
, int size
)
572 WX_SOCKLEN_T fromlen
= sizeof(from
);
576 fromlen
= sizeof(from
);
580 ret
= recvfrom(m_fd
, buffer
, size
, 0, (sockaddr
*)&from
, (WX_SOCKLEN_T
*) &fromlen
);
582 while (ret
== -1 && errno
== EINTR
); /* Loop until not interrupted */
587 /* Translate a system address into a wxSocketImplUnix address */
590 m_peer
= GAddress_new();
593 m_error
= wxSOCKET_MEMERR
;
598 err
= _GAddress_translate_from(m_peer
, (sockaddr
*)&from
, fromlen
);
599 if (err
!= wxSOCKET_NOERROR
)
601 GAddress_destroy(m_peer
);
610 int wxSocketImplUnix
::Send_Stream(const void *buffer
, int size
)
618 ret
= send(m_fd
, buffer
, size
, GSOCKET_MSG_NOSIGNAL
);
620 while (ret
== -1 && errno
== EINTR
); /* Loop until not interrupted */
627 int wxSocketImplUnix
::Send_Dgram(const void *buffer
, int size
)
629 struct sockaddr
*addr
;
635 m_error
= wxSOCKET_INVADDR
;
639 err
= _GAddress_translate_to(m_peer
, &addr
, &len
);
640 if (err
!= wxSOCKET_NOERROR
)
650 ret
= sendto(m_fd
, buffer
, size
, 0, addr
, len
);
652 while (ret
== -1 && errno
== EINTR
); /* Loop until not interrupted */
656 /* Frees memory allocated from _GAddress_translate_to */
662 void wxSocketImplUnix
::OnStateChange(wxSocketNotify event
)
664 NotifyOnStateChange(event
);
666 if ( event
== wxSOCKET_LOST
)
670 void wxSocketImplUnix
::OnReadWaiting()
674 if (m_fd
== INVALID_SOCKET
)
679 int num
= recv(m_fd
, &c
, 1, MSG_PEEK
| GSOCKET_MSG_NOSIGNAL
);
683 OnStateChange(wxSOCKET_INPUT
);
687 if (m_server
&& m_stream
)
689 OnStateChange(wxSOCKET_CONNECTION
);
695 /* graceful shutdown */
696 OnStateChange(wxSOCKET_LOST
);
700 /* Empty datagram received */
701 OnStateChange(wxSOCKET_INPUT
);
706 /* Do not throw a lost event in cases where the socket isn't really lost */
707 if ((errno
== EWOULDBLOCK
) || (errno
== EAGAIN
) || (errno
== EINTR
))
709 OnStateChange(wxSOCKET_INPUT
);
713 OnStateChange(wxSOCKET_LOST
);
719 void wxSocketImplUnix
::OnWriteWaiting()
721 if (m_establishing
&& !m_server
)
724 SOCKOPTLEN_T len
= sizeof(error
);
726 m_establishing
= false;
728 getsockopt(m_fd
, SOL_SOCKET
, SO_ERROR
, (char*)&error
, &len
);
732 OnStateChange(wxSOCKET_LOST
);
736 OnStateChange(wxSOCKET_CONNECTION
);
737 /* We have to fire this event by hand because CONNECTION (for clients)
738 * and OUTPUT are internally the same and we just disabled CONNECTION
739 * events with the above macro.
741 OnStateChange(wxSOCKET_OUTPUT
);
746 OnStateChange(wxSOCKET_OUTPUT
);
750 void wxSocketImplUnix
::OnExceptionWaiting()
752 wxFAIL_MSG( "not supposed to be called" );
756 * -------------------------------------------------------------------------
758 * -------------------------------------------------------------------------
761 /* CHECK_ADDRESS verifies that the current address family is either
762 * wxSOCKET_NOFAMILY or wxSOCKET_*family*, and if it is wxSOCKET_NOFAMILY, it
763 * initalizes it to be a wxSOCKET_*family*. In other cases, it returns
764 * an appropiate error code.
766 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
768 #define CHECK_ADDRESS(address, family) \
770 if (address->m_family == wxSOCKET_NOFAMILY) \
771 if (_GAddress_Init_##family(address) != wxSOCKET_NOERROR) \
772 return address->m_error; \
773 if (address->m_family != wxSOCKET_##family) \
775 address->m_error = wxSOCKET_INVADDR; \
776 return wxSOCKET_INVADDR; \
780 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
782 if (address->m_family == wxSOCKET_NOFAMILY) \
783 if (_GAddress_Init_##family(address) != wxSOCKET_NOERROR) \
785 if (address->m_family != wxSOCKET_##family) \
787 address->m_error = wxSOCKET_INVADDR; \
793 GAddress
*GAddress_new(void)
797 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
800 address
->m_family
= wxSOCKET_NOFAMILY
;
801 address
->m_addr
= NULL
;
807 GAddress
*GAddress_copy(GAddress
*address
)
811 assert(address
!= NULL
);
813 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
816 memcpy(addr2
, address
, sizeof(GAddress
));
818 if (address
->m_addr
&& address
->m_len
> 0)
820 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
821 if (addr2
->m_addr
== NULL
)
826 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
832 void GAddress_destroy(GAddress
*address
)
834 assert(address
!= NULL
);
837 free(address
->m_addr
);
842 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
844 assert(address
!= NULL
);
846 address
->m_family
= type
;
849 GAddressType
GAddress_GetFamily(GAddress
*address
)
851 assert(address
!= NULL
);
853 return address
->m_family
;
856 wxSocketError
_GAddress_translate_from(GAddress
*address
,
857 struct sockaddr
*addr
, int len
)
859 address
->m_realfamily
= addr
->sa_family
;
860 switch (addr
->sa_family
)
863 address
->m_family
= wxSOCKET_INET
;
866 address
->m_family
= wxSOCKET_UNIX
;
870 address
->m_family
= wxSOCKET_INET6
;
875 address
->m_error
= wxSOCKET_INVOP
;
876 return wxSOCKET_INVOP
;
881 free(address
->m_addr
);
883 address
->m_len
= len
;
884 address
->m_addr
= (struct sockaddr
*)malloc(len
);
886 if (address
->m_addr
== NULL
)
888 address
->m_error
= wxSOCKET_MEMERR
;
889 return wxSOCKET_MEMERR
;
892 memcpy(address
->m_addr
, addr
, len
);
894 return wxSOCKET_NOERROR
;
897 wxSocketError
_GAddress_translate_to(GAddress
*address
,
898 struct sockaddr
**addr
, int *len
)
900 if (!address
->m_addr
)
902 address
->m_error
= wxSOCKET_INVADDR
;
903 return wxSOCKET_INVADDR
;
906 *len
= address
->m_len
;
907 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
910 address
->m_error
= wxSOCKET_MEMERR
;
911 return wxSOCKET_MEMERR
;
914 memcpy(*addr
, address
->m_addr
, address
->m_len
);
915 return wxSOCKET_NOERROR
;
919 * -------------------------------------------------------------------------
920 * Internet address family
921 * -------------------------------------------------------------------------
924 wxSocketError
_GAddress_Init_INET(GAddress
*address
)
926 address
->m_len
= sizeof(struct sockaddr_in
);
927 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
928 if (address
->m_addr
== NULL
)
930 address
->m_error
= wxSOCKET_MEMERR
;
931 return wxSOCKET_MEMERR
;
934 address
->m_family
= wxSOCKET_INET
;
935 address
->m_realfamily
= PF_INET
;
936 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
937 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
939 return wxSOCKET_NOERROR
;
942 wxSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
945 struct in_addr
*addr
;
947 assert(address
!= NULL
);
949 CHECK_ADDRESS(address
, INET
);
951 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
953 /* If it is a numeric host name, convert it now */
954 #if defined(HAVE_INET_ATON)
955 if (inet_aton(hostname
, addr
) == 0)
957 #elif defined(HAVE_INET_ADDR)
958 if ( (addr
->s_addr
= inet_addr(hostname
)) == (unsigned)-1 )
961 /* Use gethostbyname by default */
963 int val
= 1; /* VA doesn't like constants in conditional expressions */
968 struct in_addr
*array_addr
;
970 /* It is a real name, we solve it */
972 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
973 struct hostent_data buffer
;
978 he
= wxGethostbyname_r(hostname
, &h
, (void*)&buffer
, sizeof(buffer
), &err
);
981 /* Reset to invalid address */
982 addr
->s_addr
= INADDR_NONE
;
983 address
->m_error
= wxSOCKET_NOHOST
;
984 return wxSOCKET_NOHOST
;
987 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
988 addr
->s_addr
= array_addr
[0].s_addr
;
991 return wxSOCKET_NOERROR
;
995 wxSocketError
GAddress_INET_SetBroadcastAddress(GAddress
*address
)
997 return GAddress_INET_SetHostAddress(address
, INADDR_BROADCAST
);
1000 wxSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1002 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1005 wxSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1006 unsigned long hostaddr
)
1008 struct in_addr
*addr
;
1010 assert(address
!= NULL
);
1012 CHECK_ADDRESS(address
, INET
);
1014 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1015 addr
->s_addr
= htonl(hostaddr
);
1017 return wxSOCKET_NOERROR
;
1020 wxSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1021 const char *protocol
)
1024 struct sockaddr_in
*addr
;
1026 assert(address
!= NULL
);
1027 CHECK_ADDRESS(address
, INET
);
1031 address
->m_error
= wxSOCKET_INVPORT
;
1032 return wxSOCKET_INVPORT
;
1035 #if defined(HAVE_FUNC_GETSERVBYNAME_R_4)
1036 struct servent_data buffer
;
1040 struct servent serv
;
1041 se
= wxGetservbyname_r(port
, protocol
, &serv
,
1042 (void*)&buffer
, sizeof(buffer
));
1045 /* the cast to int suppresses compiler warnings about subscript having the
1047 if (isdigit((int)port
[0]))
1051 port_int
= atoi(port
);
1052 addr
= (struct sockaddr_in
*)address
->m_addr
;
1053 addr
->sin_port
= htons(port_int
);
1054 return wxSOCKET_NOERROR
;
1057 address
->m_error
= wxSOCKET_INVPORT
;
1058 return wxSOCKET_INVPORT
;
1061 addr
= (struct sockaddr_in
*)address
->m_addr
;
1062 addr
->sin_port
= se
->s_port
;
1064 return wxSOCKET_NOERROR
;
1067 wxSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1069 struct sockaddr_in
*addr
;
1071 assert(address
!= NULL
);
1072 CHECK_ADDRESS(address
, INET
);
1074 addr
= (struct sockaddr_in
*)address
->m_addr
;
1075 addr
->sin_port
= htons(port
);
1077 return wxSOCKET_NOERROR
;
1080 wxSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1084 struct sockaddr_in
*addr
;
1086 assert(address
!= NULL
);
1087 CHECK_ADDRESS(address
, INET
);
1089 addr
= (struct sockaddr_in
*)address
->m_addr
;
1090 addr_buf
= (char *)&(addr
->sin_addr
);
1092 struct hostent temphost
;
1093 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
1094 struct hostent_data buffer
;
1099 he
= wxGethostbyaddr_r(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
, &temphost
,
1100 (void*)&buffer
, sizeof(buffer
), &err
);
1103 address
->m_error
= wxSOCKET_NOHOST
;
1104 return wxSOCKET_NOHOST
;
1107 strncpy(hostname
, he
->h_name
, sbuf
);
1109 return wxSOCKET_NOERROR
;
1112 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1114 struct sockaddr_in
*addr
;
1116 assert(address
!= NULL
);
1117 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1119 addr
= (struct sockaddr_in
*)address
->m_addr
;
1121 return ntohl(addr
->sin_addr
.s_addr
);
1124 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1126 struct sockaddr_in
*addr
;
1128 assert(address
!= NULL
);
1129 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1131 addr
= (struct sockaddr_in
*)address
->m_addr
;
1132 return ntohs(addr
->sin_port
);
1137 * -------------------------------------------------------------------------
1138 * Internet IPv6 address family
1139 * -------------------------------------------------------------------------
1142 wxSocketError
_GAddress_Init_INET6(GAddress
*address
)
1144 struct in6_addr any_address
= IN6ADDR_ANY_INIT
;
1145 address
->m_len
= sizeof(struct sockaddr_in6
);
1146 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1147 if (address
->m_addr
== NULL
)
1149 address
->m_error
= wxSOCKET_MEMERR
;
1150 return wxSOCKET_MEMERR
;
1152 memset(address
->m_addr
,0,address
->m_len
);
1154 address
->m_family
= wxSOCKET_INET6
;
1155 address
->m_realfamily
= AF_INET6
;
1156 ((struct sockaddr_in6
*)address
->m_addr
)->sin6_family
= AF_INET6
;
1157 ((struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
= any_address
;
1159 return wxSOCKET_NOERROR
;
1162 wxSocketError
GAddress_INET6_SetHostName(GAddress
*address
, const char *hostname
)
1164 assert(address
!= NULL
);
1165 CHECK_ADDRESS(address
, INET6
);
1168 memset( & hints
, 0, sizeof( hints
) );
1169 hints
.ai_family
= AF_INET6
;
1170 addrinfo
* info
= 0;
1171 if ( getaddrinfo( hostname
, "0", & hints
, & info
) || ! info
)
1173 address
->m_error
= wxSOCKET_NOHOST
;
1174 return wxSOCKET_NOHOST
;
1177 memcpy( address
->m_addr
, info
->ai_addr
, info
->ai_addrlen
);
1178 freeaddrinfo( info
);
1179 return wxSOCKET_NOERROR
;
1182 wxSocketError
GAddress_INET6_SetAnyAddress(GAddress
*address
)
1184 assert(address
!= NULL
);
1186 CHECK_ADDRESS(address
, INET6
);
1188 struct in6_addr addr
;
1189 memset( & addr
, 0, sizeof( addr
) );
1190 return GAddress_INET6_SetHostAddress(address
, addr
);
1192 wxSocketError
GAddress_INET6_SetHostAddress(GAddress
*address
,
1193 struct in6_addr hostaddr
)
1195 assert(address
!= NULL
);
1197 CHECK_ADDRESS(address
, INET6
);
1199 ((struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
= hostaddr
;
1201 return wxSOCKET_NOERROR
;
1204 wxSocketError
GAddress_INET6_SetPortName(GAddress
*address
, const char *port
,
1205 const char *protocol
)
1208 struct sockaddr_in6
*addr
;
1210 assert(address
!= NULL
);
1211 CHECK_ADDRESS(address
, INET6
);
1215 address
->m_error
= wxSOCKET_INVPORT
;
1216 return wxSOCKET_INVPORT
;
1219 se
= getservbyname(port
, protocol
);
1222 if (isdigit(port
[0]))
1226 port_int
= atoi(port
);
1227 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1228 addr
->sin6_port
= htons((u_short
) port_int
);
1229 return wxSOCKET_NOERROR
;
1232 address
->m_error
= wxSOCKET_INVPORT
;
1233 return wxSOCKET_INVPORT
;
1236 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1237 addr
->sin6_port
= se
->s_port
;
1239 return wxSOCKET_NOERROR
;
1242 wxSocketError
GAddress_INET6_SetPort(GAddress
*address
, unsigned short port
)
1244 struct sockaddr_in6
*addr
;
1246 assert(address
!= NULL
);
1247 CHECK_ADDRESS(address
, INET6
);
1249 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1250 addr
->sin6_port
= htons(port
);
1252 return wxSOCKET_NOERROR
;
1255 wxSocketError
GAddress_INET6_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1259 struct sockaddr_in6
*addr
;
1261 assert(address
!= NULL
);
1262 CHECK_ADDRESS(address
, INET6
);
1264 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1265 addr_buf
= (char *)&(addr
->sin6_addr
);
1267 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin6_addr
), AF_INET6
);
1270 address
->m_error
= wxSOCKET_NOHOST
;
1271 return wxSOCKET_NOHOST
;
1274 strncpy(hostname
, he
->h_name
, sbuf
);
1276 return wxSOCKET_NOERROR
;
1279 wxSocketError
GAddress_INET6_GetHostAddress(GAddress
*address
,struct in6_addr
*hostaddr
)
1281 assert(address
!= NULL
);
1282 assert(hostaddr
!= NULL
);
1283 CHECK_ADDRESS_RETVAL(address
, INET6
, wxSOCKET_INVADDR
);
1284 *hostaddr
= ( (struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
;
1285 return wxSOCKET_NOERROR
;
1288 unsigned short GAddress_INET6_GetPort(GAddress
*address
)
1290 assert(address
!= NULL
);
1291 CHECK_ADDRESS_RETVAL(address
, INET6
, 0);
1293 return ntohs( ((struct sockaddr_in6
*)address
->m_addr
)->sin6_port
);
1296 #endif // wxUSE_IPV6
1299 * -------------------------------------------------------------------------
1300 * Unix address family
1301 * -------------------------------------------------------------------------
1304 #ifndef __VISAGECPP__
1305 wxSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1307 address
->m_len
= sizeof(struct sockaddr_un
);
1308 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
1309 if (address
->m_addr
== NULL
)
1311 address
->m_error
= wxSOCKET_MEMERR
;
1312 return wxSOCKET_MEMERR
;
1315 address
->m_family
= wxSOCKET_UNIX
;
1316 address
->m_realfamily
= PF_UNIX
;
1317 ((struct sockaddr_un
*)address
->m_addr
)->sun_family
= AF_UNIX
;
1318 ((struct sockaddr_un
*)address
->m_addr
)->sun_path
[0] = 0;
1320 return wxSOCKET_NOERROR
;
1323 #define UNIX_SOCK_PATHLEN (sizeof(addr->sun_path)/sizeof(addr->sun_path[0]))
1325 wxSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1327 struct sockaddr_un
*addr
;
1329 assert(address
!= NULL
);
1331 CHECK_ADDRESS(address
, UNIX
);
1333 addr
= ((struct sockaddr_un
*)address
->m_addr
);
1334 strncpy(addr
->sun_path
, path
, UNIX_SOCK_PATHLEN
);
1335 addr
->sun_path
[UNIX_SOCK_PATHLEN
- 1] = '\0';
1337 return wxSOCKET_NOERROR
;
1340 wxSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1342 struct sockaddr_un
*addr
;
1344 assert(address
!= NULL
);
1345 CHECK_ADDRESS(address
, UNIX
);
1347 addr
= (struct sockaddr_un
*)address
->m_addr
;
1349 strncpy(path
, addr
->sun_path
, sbuf
);
1351 return wxSOCKET_NOERROR
;
1353 #endif /* !defined(__VISAGECPP__) */
1355 #endif /* wxUSE_SOCKETS */