]>
git.saurik.com Git - wxWidgets.git/blob - src/unix/sockunix.cpp
60143e73a1aa24fdf423d1216ca9d8cb943e521f
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 #if wxUSE_THREADS && (defined(HAVE_GETHOSTBYNAME) || defined(HAVE_GETSERVBYNAME))
178 # include "wx/thread.h"
181 #if defined(HAVE_GETHOSTBYNAME)
182 static struct hostent
* deepCopyHostent(struct hostent
*h
,
183 const struct hostent
*he
,
184 char *buffer
, int size
, int *err
)
186 /* copy old structure */
187 memcpy(h
, he
, sizeof(struct hostent
));
190 int len
= strlen(h
->h_name
);
196 memcpy(buffer
, h
->h_name
, len
);
200 /* track position in the buffer */
203 /* reuse len to store address length */
206 /* ensure pointer alignment */
207 unsigned int misalign
= sizeof(char *) - pos%sizeof
(char *);
208 if(misalign
< sizeof(char *))
211 /* leave space for pointer list */
212 char **p
= h
->h_addr_list
, **q
;
213 char **h_addr_list
= (char **)(buffer
+ pos
);
215 pos
+= sizeof(char *);
217 /* copy addresses and fill new pointer list */
218 for (p
= h
->h_addr_list
, q
= h_addr_list
; *p
!= 0; p
++, q
++)
220 if (size
< pos
+ len
)
225 memcpy(buffer
+ pos
, *p
, len
); /* copy content */
226 *q
= buffer
+ pos
; /* set copied pointer to copied content */
229 *++q
= 0; /* null terminate the pointer list */
230 h
->h_addr_list
= h_addr_list
; /* copy pointer to pointers */
232 /* ensure word alignment of pointers */
233 misalign
= sizeof(char *) - pos%sizeof
(char *);
234 if(misalign
< sizeof(char *))
237 /* leave space for pointer list */
239 char **h_aliases
= (char **)(buffer
+ pos
);
241 pos
+= sizeof(char *);
243 /* copy aliases and fill new pointer list */
244 for (p
= h
->h_aliases
, q
= h_aliases
; *p
!= 0; p
++, q
++)
247 if (size
<= pos
+ len
)
252 memcpy(buffer
+ pos
, *p
, len
); /* copy content */
253 buffer
[pos
+ len
] = '\0';
254 *q
= buffer
+ pos
; /* set copied pointer to copied content */
257 *++q
= 0; /* null terminate the pointer list */
258 h
->h_aliases
= h_aliases
; /* copy pointer to pointers */
264 #if defined(HAVE_GETHOSTBYNAME) && wxUSE_THREADS
265 static wxMutex nameLock
;
267 struct hostent
* wxGethostbyname_r(const char *hostname
, struct hostent
*h
,
268 void *buffer
, int size
, int *err
)
271 struct hostent
*he
= NULL
;
273 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_6)
274 if (gethostbyname_r(hostname
, h
, (char*)buffer
, size
, &he
, err
))
276 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_5)
277 he
= gethostbyname_r(hostname
, h
, (char*)buffer
, size
, err
);
278 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
279 if (gethostbyname_r(hostname
, h
, (struct hostent_data
*) buffer
))
286 #elif defined(HAVE_GETHOSTBYNAME)
288 wxMutexLocker
locker(nameLock
);
290 he
= gethostbyname(hostname
);
294 he
= deepCopyHostent(h
, he
, (char*)buffer
, size
, err
);
299 #if defined(HAVE_GETHOSTBYNAME) && wxUSE_THREADS
300 static wxMutex addrLock
;
302 struct hostent
* wxGethostbyaddr_r(const char *addr_buf
, int buf_size
,
303 int proto
, struct hostent
*h
,
304 void *buffer
, int size
, int *err
)
306 struct hostent
*he
= NULL
;
308 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_6)
309 if (gethostbyaddr_r(addr_buf
, buf_size
, proto
, h
,
310 (char*)buffer
, size
, &he
, err
))
312 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_5)
313 he
= gethostbyaddr_r(addr_buf
, buf_size
, proto
, h
, (char*)buffer
, size
, err
);
314 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
315 if (gethostbyaddr_r(addr_buf
, buf_size
, proto
, h
,
316 (struct hostent_data
*) buffer
))
323 #elif defined(HAVE_GETHOSTBYNAME)
325 wxMutexLocker
locker(addrLock
);
327 he
= gethostbyaddr(addr_buf
, buf_size
, proto
);
331 he
= deepCopyHostent(h
, he
, (char*)buffer
, size
, err
);
336 #if defined(HAVE_GETSERVBYNAME)
337 static struct servent
* deepCopyServent(struct servent
*s
,
338 const struct servent
*se
,
339 char *buffer
, int size
)
341 /* copy plain old structure */
342 memcpy(s
, se
, sizeof(struct servent
));
345 int len
= strlen(s
->s_name
);
350 memcpy(buffer
, s
->s_name
, len
);
354 /* track position in the buffer */
358 len
= strlen(s
->s_proto
);
359 if (pos
+ len
>= size
)
363 memcpy(buffer
+ pos
, s
->s_proto
, len
);
364 buffer
[pos
+ len
] = '\0';
365 s
->s_proto
= buffer
+ pos
;
367 /* track position in the buffer */
370 /* ensure pointer alignment */
371 unsigned int misalign
= sizeof(char *) - pos%sizeof
(char *);
372 if(misalign
< sizeof(char *))
375 /* leave space for pointer list */
376 char **p
= s
->s_aliases
, **q
;
377 char **s_aliases
= (char **)(buffer
+ pos
);
379 pos
+= sizeof(char *);
381 /* copy addresses and fill new pointer list */
382 for (p
= s
->s_aliases
, q
= s_aliases
; *p
!= 0; p
++, q
++){
384 if (size
<= pos
+ len
)
388 memcpy(buffer
+ pos
, *p
, len
); /* copy content */
389 buffer
[pos
+ len
] = '\0';
390 *q
= buffer
+ pos
; /* set copied pointer to copied content */
393 *++q
= 0; /* null terminate the pointer list */
394 s
->s_aliases
= s_aliases
; /* copy pointer to pointers */
399 #if defined(HAVE_GETSERVBYNAME) && wxUSE_THREADS
400 static wxMutex servLock
;
402 struct servent
*wxGetservbyname_r(const char *port
, const char *protocol
,
403 struct servent
*serv
, void *buffer
, int size
)
405 struct servent
*se
= NULL
;
406 #if defined(HAVE_FUNC_GETSERVBYNAME_R_6)
407 if (getservbyname_r(port
, protocol
, serv
, (char*)buffer
, size
, &se
))
409 #elif defined(HAVE_FUNC_GETSERVBYNAME_R_5)
410 se
= getservbyname_r(port
, protocol
, serv
, (char*)buffer
, size
);
411 #elif defined(HAVE_FUNC_GETSERVBYNAME_R_4)
412 if (getservbyname_r(port
, protocol
, serv
, (struct servent_data
*) buffer
))
416 #elif defined(HAVE_GETSERVBYNAME)
418 wxMutexLocker
locker(servLock
);
420 se
= getservbyname(port
, protocol
);
422 se
= deepCopyServent(serv
, se
, (char*)buffer
, size
);
428 wxSocketImpl
*wxSocketImpl::Create(wxSocketBase
& wxsocket
)
430 return new wxSocketImplUnix(wxsocket
);
435 * Disallow further read/write operations on this socket, close
436 * the fd and disable all callbacks.
438 void wxSocketImplUnix::Shutdown()
440 /* Don't allow events to fire after socket has been closed */
443 wxSocketImpl::Shutdown();
446 wxSocketError
wxSocketImplUnix::GetLastError() const
451 return wxSOCKET_NOERROR
;
454 return wxSOCKET_INVSOCK
;
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
;
492 /* Disable events during query of socket status */
493 DisableEvent(wxSOCKET_INPUT
);
497 ret
= Recv_Stream(buffer
, size
);
499 ret
= Recv_Dgram(buffer
, size
);
502 * If recv returned zero for a TCP socket (if m_stream == NULL, it's an UDP
503 * socket and empty datagrams are possible), then the connection has been
506 * Otherwise, recv has returned an error (-1), in which case we have lost
507 * the socket only if errno does _not_ indicate that there may be more data
510 if ((ret
== 0) && m_stream
)
512 /* Make sure wxSOCKET_LOST event gets sent and shut down the socket */
513 m_detected
= wxSOCKET_LOST_FLAG
;
519 if ((errno
== EWOULDBLOCK
) || (errno
== EAGAIN
))
520 m_error
= wxSOCKET_WOULDBLOCK
;
522 m_error
= wxSOCKET_IOERR
;
525 /* Enable events again now that we are done processing */
526 EnableEvent(wxSOCKET_INPUT
);
531 int wxSocketImplUnix::Write(const void *buffer
, int size
)
535 if (m_fd
== INVALID_SOCKET
|| m_server
)
537 m_error
= wxSOCKET_INVSOCK
;
543 ret
= Send_Stream(buffer
, size
);
545 ret
= Send_Dgram(buffer
, size
);
549 if ((errno
== EWOULDBLOCK
) || (errno
== EAGAIN
))
551 m_error
= wxSOCKET_WOULDBLOCK
;
555 m_error
= wxSOCKET_IOERR
;
558 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
559 * in MSW). Once the first OUTPUT event is received, users can assume
560 * that the socket is writable until a read operation fails. Only then
561 * will further OUTPUT events be posted.
563 EnableEvent(wxSOCKET_OUTPUT
);
573 void wxSocketImplUnix::EnableEvent(wxSocketNotify event
)
575 m_detected
&= ~(1 << event
);
576 wxSocketManager::Get()->Install_Callback(this, event
);
579 void wxSocketImplUnix::DisableEvent(wxSocketNotify event
)
581 m_detected
|= (1 << event
);
582 wxSocketManager::Get()->Uninstall_Callback(this, event
);
585 int wxSocketImplUnix::Recv_Stream(void *buffer
, int size
)
590 ret
= recv(m_fd
, buffer
, size
, GSOCKET_MSG_NOSIGNAL
);
592 while (ret
== -1 && errno
== EINTR
); /* Loop until not interrupted */
597 int wxSocketImplUnix::Recv_Dgram(void *buffer
, int size
)
600 WX_SOCKLEN_T fromlen
= sizeof(from
);
604 fromlen
= sizeof(from
);
608 ret
= recvfrom(m_fd
, buffer
, size
, 0, (sockaddr
*)&from
, (WX_SOCKLEN_T
*) &fromlen
);
610 while (ret
== -1 && errno
== EINTR
); /* Loop until not interrupted */
615 /* Translate a system address into a wxSocketImplUnix address */
618 m_peer
= GAddress_new();
621 m_error
= wxSOCKET_MEMERR
;
626 err
= _GAddress_translate_from(m_peer
, (sockaddr
*)&from
, fromlen
);
627 if (err
!= wxSOCKET_NOERROR
)
629 GAddress_destroy(m_peer
);
638 int wxSocketImplUnix::Send_Stream(const void *buffer
, int size
)
646 ret
= send(m_fd
, buffer
, size
, GSOCKET_MSG_NOSIGNAL
);
648 while (ret
== -1 && errno
== EINTR
); /* Loop until not interrupted */
655 int wxSocketImplUnix::Send_Dgram(const void *buffer
, int size
)
657 struct sockaddr
*addr
;
663 m_error
= wxSOCKET_INVADDR
;
667 err
= _GAddress_translate_to(m_peer
, &addr
, &len
);
668 if (err
!= wxSOCKET_NOERROR
)
678 ret
= sendto(m_fd
, buffer
, size
, 0, addr
, len
);
680 while (ret
== -1 && errno
== EINTR
); /* Loop until not interrupted */
684 /* Frees memory allocated from _GAddress_translate_to */
690 void wxSocketImplUnix::OnStateChange(wxSocketNotify event
)
693 NotifyOnStateChange(event
);
695 if ( event
== wxSOCKET_LOST
)
699 void wxSocketImplUnix::OnReadWaiting()
703 if (m_fd
== INVALID_SOCKET
)
708 /* If we have already detected a LOST event, then don't try
709 * to do any further processing.
711 if ((m_detected
& wxSOCKET_LOST_FLAG
) != 0)
713 m_establishing
= false;
715 OnStateChange(wxSOCKET_LOST
);
719 int num
= recv(m_fd
, &c
, 1, MSG_PEEK
| GSOCKET_MSG_NOSIGNAL
);
723 OnStateChange(wxSOCKET_INPUT
);
727 if (m_server
&& m_stream
)
729 OnStateChange(wxSOCKET_CONNECTION
);
735 /* graceful shutdown */
736 OnStateChange(wxSOCKET_LOST
);
740 /* Empty datagram received */
741 OnStateChange(wxSOCKET_INPUT
);
746 /* Do not throw a lost event in cases where the socket isn't really lost */
747 if ((errno
== EWOULDBLOCK
) || (errno
== EAGAIN
) || (errno
== EINTR
))
749 OnStateChange(wxSOCKET_INPUT
);
753 OnStateChange(wxSOCKET_LOST
);
759 void wxSocketImplUnix::OnWriteWaiting()
761 /* If we have already detected a LOST event, then don't try
762 * to do any further processing.
764 if ((m_detected
& wxSOCKET_LOST_FLAG
) != 0)
766 m_establishing
= false;
768 OnStateChange(wxSOCKET_LOST
);
772 if (m_establishing
&& !m_server
)
775 SOCKOPTLEN_T len
= sizeof(error
);
777 m_establishing
= false;
779 getsockopt(m_fd
, SOL_SOCKET
, SO_ERROR
, (char*)&error
, &len
);
783 OnStateChange(wxSOCKET_LOST
);
787 OnStateChange(wxSOCKET_CONNECTION
);
788 /* We have to fire this event by hand because CONNECTION (for clients)
789 * and OUTPUT are internally the same and we just disabled CONNECTION
790 * events with the above macro.
792 OnStateChange(wxSOCKET_OUTPUT
);
797 OnStateChange(wxSOCKET_OUTPUT
);
801 void wxSocketImplUnix::OnExceptionWaiting()
803 wxFAIL_MSG( "not supposed to be called" );
807 * -------------------------------------------------------------------------
809 * -------------------------------------------------------------------------
812 /* CHECK_ADDRESS verifies that the current address family is either
813 * wxSOCKET_NOFAMILY or wxSOCKET_*family*, and if it is wxSOCKET_NOFAMILY, it
814 * initalizes it to be a wxSOCKET_*family*. In other cases, it returns
815 * an appropiate error code.
817 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
819 #define CHECK_ADDRESS(address, family) \
821 if (address->m_family == wxSOCKET_NOFAMILY) \
822 if (_GAddress_Init_##family(address) != wxSOCKET_NOERROR) \
823 return address->m_error; \
824 if (address->m_family != wxSOCKET_##family) \
826 address->m_error = wxSOCKET_INVADDR; \
827 return wxSOCKET_INVADDR; \
831 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
833 if (address->m_family == wxSOCKET_NOFAMILY) \
834 if (_GAddress_Init_##family(address) != wxSOCKET_NOERROR) \
836 if (address->m_family != wxSOCKET_##family) \
838 address->m_error = wxSOCKET_INVADDR; \
844 GAddress
*GAddress_new(void)
848 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
851 address
->m_family
= wxSOCKET_NOFAMILY
;
852 address
->m_addr
= NULL
;
858 GAddress
*GAddress_copy(GAddress
*address
)
862 assert(address
!= NULL
);
864 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
867 memcpy(addr2
, address
, sizeof(GAddress
));
869 if (address
->m_addr
&& address
->m_len
> 0)
871 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
872 if (addr2
->m_addr
== NULL
)
877 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
883 void GAddress_destroy(GAddress
*address
)
885 assert(address
!= NULL
);
888 free(address
->m_addr
);
893 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
895 assert(address
!= NULL
);
897 address
->m_family
= type
;
900 GAddressType
GAddress_GetFamily(GAddress
*address
)
902 assert(address
!= NULL
);
904 return address
->m_family
;
907 wxSocketError
_GAddress_translate_from(GAddress
*address
,
908 struct sockaddr
*addr
, int len
)
910 address
->m_realfamily
= addr
->sa_family
;
911 switch (addr
->sa_family
)
914 address
->m_family
= wxSOCKET_INET
;
917 address
->m_family
= wxSOCKET_UNIX
;
921 address
->m_family
= wxSOCKET_INET6
;
926 address
->m_error
= wxSOCKET_INVOP
;
927 return wxSOCKET_INVOP
;
932 free(address
->m_addr
);
934 address
->m_len
= len
;
935 address
->m_addr
= (struct sockaddr
*)malloc(len
);
937 if (address
->m_addr
== NULL
)
939 address
->m_error
= wxSOCKET_MEMERR
;
940 return wxSOCKET_MEMERR
;
943 memcpy(address
->m_addr
, addr
, len
);
945 return wxSOCKET_NOERROR
;
948 wxSocketError
_GAddress_translate_to(GAddress
*address
,
949 struct sockaddr
**addr
, int *len
)
951 if (!address
->m_addr
)
953 address
->m_error
= wxSOCKET_INVADDR
;
954 return wxSOCKET_INVADDR
;
957 *len
= address
->m_len
;
958 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
961 address
->m_error
= wxSOCKET_MEMERR
;
962 return wxSOCKET_MEMERR
;
965 memcpy(*addr
, address
->m_addr
, address
->m_len
);
966 return wxSOCKET_NOERROR
;
970 * -------------------------------------------------------------------------
971 * Internet address family
972 * -------------------------------------------------------------------------
975 wxSocketError
_GAddress_Init_INET(GAddress
*address
)
977 address
->m_len
= sizeof(struct sockaddr_in
);
978 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
979 if (address
->m_addr
== NULL
)
981 address
->m_error
= wxSOCKET_MEMERR
;
982 return wxSOCKET_MEMERR
;
985 address
->m_family
= wxSOCKET_INET
;
986 address
->m_realfamily
= PF_INET
;
987 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
988 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
990 return wxSOCKET_NOERROR
;
993 wxSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
996 struct in_addr
*addr
;
998 assert(address
!= NULL
);
1000 CHECK_ADDRESS(address
, INET
);
1002 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1004 /* If it is a numeric host name, convert it now */
1005 #if defined(HAVE_INET_ATON)
1006 if (inet_aton(hostname
, addr
) == 0)
1008 #elif defined(HAVE_INET_ADDR)
1009 if ( (addr
->s_addr
= inet_addr(hostname
)) == (unsigned)-1 )
1012 /* Use gethostbyname by default */
1014 int val
= 1; /* VA doesn't like constants in conditional expressions */
1019 struct in_addr
*array_addr
;
1021 /* It is a real name, we solve it */
1023 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
1024 struct hostent_data buffer
;
1029 he
= wxGethostbyname_r(hostname
, &h
, (void*)&buffer
, sizeof(buffer
), &err
);
1032 /* Reset to invalid address */
1033 addr
->s_addr
= INADDR_NONE
;
1034 address
->m_error
= wxSOCKET_NOHOST
;
1035 return wxSOCKET_NOHOST
;
1038 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1039 addr
->s_addr
= array_addr
[0].s_addr
;
1042 return wxSOCKET_NOERROR
;
1046 wxSocketError
GAddress_INET_SetBroadcastAddress(GAddress
*address
)
1048 return GAddress_INET_SetHostAddress(address
, INADDR_BROADCAST
);
1051 wxSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1053 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1056 wxSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1057 unsigned long hostaddr
)
1059 struct in_addr
*addr
;
1061 assert(address
!= NULL
);
1063 CHECK_ADDRESS(address
, INET
);
1065 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1066 addr
->s_addr
= htonl(hostaddr
);
1068 return wxSOCKET_NOERROR
;
1071 wxSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1072 const char *protocol
)
1075 struct sockaddr_in
*addr
;
1077 assert(address
!= NULL
);
1078 CHECK_ADDRESS(address
, INET
);
1082 address
->m_error
= wxSOCKET_INVPORT
;
1083 return wxSOCKET_INVPORT
;
1086 #if defined(HAVE_FUNC_GETSERVBYNAME_R_4)
1087 struct servent_data buffer
;
1091 struct servent serv
;
1092 se
= wxGetservbyname_r(port
, protocol
, &serv
,
1093 (void*)&buffer
, sizeof(buffer
));
1096 /* the cast to int suppresses compiler warnings about subscript having the
1098 if (isdigit((int)port
[0]))
1102 port_int
= atoi(port
);
1103 addr
= (struct sockaddr_in
*)address
->m_addr
;
1104 addr
->sin_port
= htons(port_int
);
1105 return wxSOCKET_NOERROR
;
1108 address
->m_error
= wxSOCKET_INVPORT
;
1109 return wxSOCKET_INVPORT
;
1112 addr
= (struct sockaddr_in
*)address
->m_addr
;
1113 addr
->sin_port
= se
->s_port
;
1115 return wxSOCKET_NOERROR
;
1118 wxSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1120 struct sockaddr_in
*addr
;
1122 assert(address
!= NULL
);
1123 CHECK_ADDRESS(address
, INET
);
1125 addr
= (struct sockaddr_in
*)address
->m_addr
;
1126 addr
->sin_port
= htons(port
);
1128 return wxSOCKET_NOERROR
;
1131 wxSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1135 struct sockaddr_in
*addr
;
1137 assert(address
!= NULL
);
1138 CHECK_ADDRESS(address
, INET
);
1140 addr
= (struct sockaddr_in
*)address
->m_addr
;
1141 addr_buf
= (char *)&(addr
->sin_addr
);
1143 struct hostent temphost
;
1144 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
1145 struct hostent_data buffer
;
1150 he
= wxGethostbyaddr_r(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
, &temphost
,
1151 (void*)&buffer
, sizeof(buffer
), &err
);
1154 address
->m_error
= wxSOCKET_NOHOST
;
1155 return wxSOCKET_NOHOST
;
1158 strncpy(hostname
, he
->h_name
, sbuf
);
1160 return wxSOCKET_NOERROR
;
1163 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1165 struct sockaddr_in
*addr
;
1167 assert(address
!= NULL
);
1168 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1170 addr
= (struct sockaddr_in
*)address
->m_addr
;
1172 return ntohl(addr
->sin_addr
.s_addr
);
1175 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1177 struct sockaddr_in
*addr
;
1179 assert(address
!= NULL
);
1180 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1182 addr
= (struct sockaddr_in
*)address
->m_addr
;
1183 return ntohs(addr
->sin_port
);
1188 * -------------------------------------------------------------------------
1189 * Internet IPv6 address family
1190 * -------------------------------------------------------------------------
1193 wxSocketError
_GAddress_Init_INET6(GAddress
*address
)
1195 struct in6_addr any_address
= IN6ADDR_ANY_INIT
;
1196 address
->m_len
= sizeof(struct sockaddr_in6
);
1197 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1198 if (address
->m_addr
== NULL
)
1200 address
->m_error
= wxSOCKET_MEMERR
;
1201 return wxSOCKET_MEMERR
;
1203 memset(address
->m_addr
,0,address
->m_len
);
1205 address
->m_family
= wxSOCKET_INET6
;
1206 address
->m_realfamily
= AF_INET6
;
1207 ((struct sockaddr_in6
*)address
->m_addr
)->sin6_family
= AF_INET6
;
1208 ((struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
= any_address
;
1210 return wxSOCKET_NOERROR
;
1213 wxSocketError
GAddress_INET6_SetHostName(GAddress
*address
, const char *hostname
)
1215 assert(address
!= NULL
);
1216 CHECK_ADDRESS(address
, INET6
);
1219 memset( & hints
, 0, sizeof( hints
) );
1220 hints
.ai_family
= AF_INET6
;
1221 addrinfo
* info
= 0;
1222 if ( getaddrinfo( hostname
, "0", & hints
, & info
) || ! info
)
1224 address
->m_error
= wxSOCKET_NOHOST
;
1225 return wxSOCKET_NOHOST
;
1228 memcpy( address
->m_addr
, info
->ai_addr
, info
->ai_addrlen
);
1229 freeaddrinfo( info
);
1230 return wxSOCKET_NOERROR
;
1233 wxSocketError
GAddress_INET6_SetAnyAddress(GAddress
*address
)
1235 assert(address
!= NULL
);
1237 CHECK_ADDRESS(address
, INET6
);
1239 struct in6_addr addr
;
1240 memset( & addr
, 0, sizeof( addr
) );
1241 return GAddress_INET6_SetHostAddress(address
, addr
);
1243 wxSocketError
GAddress_INET6_SetHostAddress(GAddress
*address
,
1244 struct in6_addr hostaddr
)
1246 assert(address
!= NULL
);
1248 CHECK_ADDRESS(address
, INET6
);
1250 ((struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
= hostaddr
;
1252 return wxSOCKET_NOERROR
;
1255 wxSocketError
GAddress_INET6_SetPortName(GAddress
*address
, const char *port
,
1256 const char *protocol
)
1259 struct sockaddr_in6
*addr
;
1261 assert(address
!= NULL
);
1262 CHECK_ADDRESS(address
, INET6
);
1266 address
->m_error
= wxSOCKET_INVPORT
;
1267 return wxSOCKET_INVPORT
;
1270 se
= getservbyname(port
, protocol
);
1273 if (isdigit(port
[0]))
1277 port_int
= atoi(port
);
1278 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1279 addr
->sin6_port
= htons((u_short
) port_int
);
1280 return wxSOCKET_NOERROR
;
1283 address
->m_error
= wxSOCKET_INVPORT
;
1284 return wxSOCKET_INVPORT
;
1287 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1288 addr
->sin6_port
= se
->s_port
;
1290 return wxSOCKET_NOERROR
;
1293 wxSocketError
GAddress_INET6_SetPort(GAddress
*address
, unsigned short port
)
1295 struct sockaddr_in6
*addr
;
1297 assert(address
!= NULL
);
1298 CHECK_ADDRESS(address
, INET6
);
1300 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1301 addr
->sin6_port
= htons(port
);
1303 return wxSOCKET_NOERROR
;
1306 wxSocketError
GAddress_INET6_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1310 struct sockaddr_in6
*addr
;
1312 assert(address
!= NULL
);
1313 CHECK_ADDRESS(address
, INET6
);
1315 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1316 addr_buf
= (char *)&(addr
->sin6_addr
);
1318 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin6_addr
), AF_INET6
);
1321 address
->m_error
= wxSOCKET_NOHOST
;
1322 return wxSOCKET_NOHOST
;
1325 strncpy(hostname
, he
->h_name
, sbuf
);
1327 return wxSOCKET_NOERROR
;
1330 wxSocketError
GAddress_INET6_GetHostAddress(GAddress
*address
,struct in6_addr
*hostaddr
)
1332 assert(address
!= NULL
);
1333 assert(hostaddr
!= NULL
);
1334 CHECK_ADDRESS_RETVAL(address
, INET6
, wxSOCKET_INVADDR
);
1335 *hostaddr
= ( (struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
;
1336 return wxSOCKET_NOERROR
;
1339 unsigned short GAddress_INET6_GetPort(GAddress
*address
)
1341 assert(address
!= NULL
);
1342 CHECK_ADDRESS_RETVAL(address
, INET6
, 0);
1344 return ntohs( ((struct sockaddr_in6
*)address
->m_addr
)->sin6_port
);
1347 #endif // wxUSE_IPV6
1350 * -------------------------------------------------------------------------
1351 * Unix address family
1352 * -------------------------------------------------------------------------
1355 #ifndef __VISAGECPP__
1356 wxSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1358 address
->m_len
= sizeof(struct sockaddr_un
);
1359 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
1360 if (address
->m_addr
== NULL
)
1362 address
->m_error
= wxSOCKET_MEMERR
;
1363 return wxSOCKET_MEMERR
;
1366 address
->m_family
= wxSOCKET_UNIX
;
1367 address
->m_realfamily
= PF_UNIX
;
1368 ((struct sockaddr_un
*)address
->m_addr
)->sun_family
= AF_UNIX
;
1369 ((struct sockaddr_un
*)address
->m_addr
)->sun_path
[0] = 0;
1371 return wxSOCKET_NOERROR
;
1374 #define UNIX_SOCK_PATHLEN (sizeof(addr->sun_path)/sizeof(addr->sun_path[0]))
1376 wxSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1378 struct sockaddr_un
*addr
;
1380 assert(address
!= NULL
);
1382 CHECK_ADDRESS(address
, UNIX
);
1384 addr
= ((struct sockaddr_un
*)address
->m_addr
);
1385 strncpy(addr
->sun_path
, path
, UNIX_SOCK_PATHLEN
);
1386 addr
->sun_path
[UNIX_SOCK_PATHLEN
- 1] = '\0';
1388 return wxSOCKET_NOERROR
;
1391 wxSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1393 struct sockaddr_un
*addr
;
1395 assert(address
!= NULL
);
1396 CHECK_ADDRESS(address
, UNIX
);
1398 addr
= (struct sockaddr_un
*)address
->m_addr
;
1400 strncpy(path
, addr
->sun_path
, sbuf
);
1402 return wxSOCKET_NOERROR
;
1404 #endif /* !defined(__VISAGECPP__) */
1406 #endif /* wxUSE_SOCKETS */