]>
git.saurik.com Git - wxWidgets.git/blob - src/unix/sockunix.cpp
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
);
427 /* debugging helpers */
428 #ifdef __GSOCKET_DEBUG__
429 # define SOCKET_DEBUG(args) printf args
431 # define SOCKET_DEBUG(args)
432 #endif /* __GSOCKET_DEBUG__ */
435 wxSocketImpl
*wxSocketImpl::Create(wxSocketBase
& wxsocket
)
437 return new wxSocketImplUnix(wxsocket
);
442 * Disallow further read/write operations on this socket, close
443 * the fd and disable all callbacks.
445 void wxSocketImplUnix::Shutdown()
447 /* Don't allow events to fire after socket has been closed */
450 wxSocketImpl::Shutdown();
454 * Waits for an incoming client connection. Returns a pointer to
455 * a wxSocketImplUnix object, or NULL if there was an error, in which case
456 * the last error field will be updated for the calling wxSocketImplUnix.
458 * Error codes (set in the calling wxSocketImplUnix)
459 * wxSOCKET_INVSOCK - the socket is not valid or not a server.
460 * wxSOCKET_TIMEDOUT - timeout, no incoming connections.
461 * wxSOCKET_WOULDBLOCK - the call would block and the socket is nonblocking.
462 * wxSOCKET_MEMERR - couldn't allocate memory.
463 * wxSOCKET_IOERR - low-level error.
465 wxSocketImpl
*wxSocketImplUnix::WaitConnection(wxSocketBase
& wxsocket
)
468 WX_SOCKLEN_T fromlen
= sizeof(from
);
469 wxSocketImpl
*connection
;
473 /* If the socket has already been created, we exit immediately */
474 if (m_fd
== INVALID_SOCKET
|| !m_server
)
476 m_error
= wxSOCKET_INVSOCK
;
480 /* Create a wxSocketImplUnix object for the new connection */
481 connection
= wxSocketImplUnix::Create(wxsocket
);
485 m_error
= wxSOCKET_MEMERR
;
489 /* Wait for a connection (with timeout) */
490 if (Input_Timeout() == wxSOCKET_TIMEDOUT
)
493 /* m_error set by Input_Timeout */
497 connection
->m_fd
= accept(m_fd
, (sockaddr
*)&from
, (WX_SOCKLEN_T
*) &fromlen
);
499 /* Reenable CONNECTION events */
500 EnableEvent(wxSOCKET_CONNECTION
);
502 if (connection
->m_fd
== INVALID_SOCKET
)
504 if (errno
== EWOULDBLOCK
)
505 m_error
= wxSOCKET_WOULDBLOCK
;
507 m_error
= wxSOCKET_IOERR
;
513 /* Initialize all fields */
514 connection
->m_server
= false;
515 connection
->m_stream
= true;
517 /* Setup the peer address field */
518 connection
->m_peer
= GAddress_new();
519 if (!connection
->m_peer
)
522 m_error
= wxSOCKET_MEMERR
;
526 err
= _GAddress_translate_from(connection
->m_peer
, (sockaddr
*)&from
, fromlen
);
527 if (err
!= wxSOCKET_NOERROR
)
534 #if defined(__EMX__) || defined(__VISAGECPP__)
535 ioctl(connection
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
537 ioctl(connection
->m_fd
, FIONBIO
, &arg
);
540 connection
->Notify(true);
545 void wxSocketImplUnix::Notify(bool flag
)
547 if (flag
== m_use_events
)
550 DoEnableEvents(flag
);
553 void wxSocketImplUnix::DoEnableEvents(bool flag
)
555 wxSocketManager
* const manager
= wxSocketManager::Get();
558 manager
->Install_Callback(this, wxSOCKET_INPUT
);
559 manager
->Install_Callback(this, wxSOCKET_OUTPUT
);
563 manager
->Uninstall_Callback(this, wxSOCKET_INPUT
);
564 manager
->Uninstall_Callback(this, wxSOCKET_OUTPUT
);
568 wxSocketError
wxSocketImplUnix::DoHandleConnect(int ret
)
570 /* We only call EnableEvents() if we know we aren't shutting down the socket.
571 * NB: EnableEvents() needs to be called whether the socket is blocking or
572 * non-blocking, it just shouldn't be called prior to knowing there is a
573 * connection _if_ blocking sockets are being used.
574 * If connect above returns 0, we are already connected and need to make the
575 * call to EnableEvents() now.
577 if ( m_non_blocking
|| (ret
== 0) )
582 const int err
= errno
;
584 /* If connect failed with EINPROGRESS and the wxSocketImplUnix object
585 * is in blocking mode, we select() for the specified timeout
586 * checking for writability to see if the connection request
589 if ((err
== EINPROGRESS
) && (!m_non_blocking
))
591 if (Output_Timeout() == wxSOCKET_TIMEDOUT
)
594 /* m_error is set in Output_Timeout */
595 return wxSOCKET_TIMEDOUT
;
600 SOCKOPTLEN_T len
= sizeof(error
);
602 getsockopt(m_fd
, SOL_SOCKET
, SO_ERROR
, (char*) &error
, &len
);
606 return wxSOCKET_NOERROR
;
610 /* If connect failed with EINPROGRESS and the wxSocketImplUnix object
611 * is set to nonblocking, we set m_error to wxSOCKET_WOULDBLOCK
612 * (and return wxSOCKET_WOULDBLOCK) but we don't close the socket;
613 * this way if the connection completes, a wxSOCKET_CONNECTION
614 * event will be generated, if enabled.
616 if ((err
== EINPROGRESS
) && (m_non_blocking
))
618 m_establishing
= true;
619 m_error
= wxSOCKET_WOULDBLOCK
;
620 return wxSOCKET_WOULDBLOCK
;
623 /* If connect failed with an error other than EINPROGRESS,
624 * then the call to Connect has failed.
627 m_error
= wxSOCKET_IOERR
;
629 return wxSOCKET_IOERR
;
632 return wxSOCKET_NOERROR
;
637 /* Like recv(), send(), ... */
638 int wxSocketImplUnix::Read(char *buffer
, int size
)
642 if (m_fd
== INVALID_SOCKET
|| m_server
)
644 m_error
= wxSOCKET_INVSOCK
;
648 /* Disable events during query of socket status */
649 DisableEvent(wxSOCKET_INPUT
);
651 /* If the socket is blocking, wait for data (with a timeout) */
652 if (Input_Timeout() == wxSOCKET_TIMEDOUT
) {
653 m_error
= wxSOCKET_TIMEDOUT
;
654 /* Don't return here immediately, otherwise socket events would not be
662 ret
= Recv_Stream(buffer
, size
);
664 ret
= Recv_Dgram(buffer
, size
);
667 * If recv returned zero for a TCP socket (if m_stream == NULL, it's an UDP
668 * socket and empty datagrams are possible), then the connection has been
671 * Otherwise, recv has returned an error (-1), in which case we have lost
672 * the socket only if errno does _not_ indicate that there may be more data
675 if ((ret
== 0) && m_stream
)
677 /* Make sure wxSOCKET_LOST event gets sent and shut down the socket */
680 m_detected
= wxSOCKET_LOST_FLAG
;
687 if ((errno
== EWOULDBLOCK
) || (errno
== EAGAIN
))
688 m_error
= wxSOCKET_WOULDBLOCK
;
690 m_error
= wxSOCKET_IOERR
;
694 /* Enable events again now that we are done processing */
695 EnableEvent(wxSOCKET_INPUT
);
700 int wxSocketImplUnix::Write(const char *buffer
, int size
)
704 SOCKET_DEBUG(( "Write #1, size %d\n", size
));
706 if (m_fd
== INVALID_SOCKET
|| m_server
)
708 m_error
= wxSOCKET_INVSOCK
;
712 SOCKET_DEBUG(( "Write #2, size %d\n", size
));
714 /* If the socket is blocking, wait for writability (with a timeout) */
715 if (Output_Timeout() == wxSOCKET_TIMEDOUT
)
718 SOCKET_DEBUG(( "Write #3, size %d\n", size
));
722 ret
= Send_Stream(buffer
, size
);
724 ret
= Send_Dgram(buffer
, size
);
726 SOCKET_DEBUG(( "Write #4, size %d\n", size
));
730 if ((errno
== EWOULDBLOCK
) || (errno
== EAGAIN
))
732 m_error
= wxSOCKET_WOULDBLOCK
;
733 SOCKET_DEBUG(( "Write error WOULDBLOCK\n" ));
737 m_error
= wxSOCKET_IOERR
;
738 SOCKET_DEBUG(( "Write error IOERR\n" ));
741 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
742 * in MSW). Once the first OUTPUT event is received, users can assume
743 * that the socket is writable until a read operation fails. Only then
744 * will further OUTPUT events be posted.
746 EnableEvent(wxSOCKET_OUTPUT
);
751 SOCKET_DEBUG(( "Write #5, size %d ret %d\n", size
, ret
));
758 void wxSocketImplUnix::EnableEvent(wxSocketNotify event
)
762 m_detected
&= ~(1 << event
);
763 wxSocketManager::Get()->Install_Callback(this, event
);
767 void wxSocketImplUnix::DisableEvent(wxSocketNotify event
)
771 m_detected
|= (1 << event
);
772 wxSocketManager::Get()->Uninstall_Callback(this, event
);
777 * For blocking sockets, wait until data is available or
778 * until timeout ellapses.
780 wxSocketError
wxSocketImplUnix::Input_Timeout()
785 // Linux select() will overwrite the struct on return so make a copy
786 struct timeval tv
= m_timeout
;
791 wxFD_SET(m_fd
, &readfds
);
792 ret
= select(m_fd
+ 1, &readfds
, NULL
, NULL
, &tv
);
795 SOCKET_DEBUG(( "Input_Timeout, select returned 0\n" ));
796 m_error
= wxSOCKET_TIMEDOUT
;
797 return wxSOCKET_TIMEDOUT
;
802 SOCKET_DEBUG(( "Input_Timeout, select returned -1\n" ));
803 if (errno
== EBADF
) { SOCKET_DEBUG(( "Invalid file descriptor\n" )); }
804 if (errno
== EINTR
) { SOCKET_DEBUG(( "A non blocked signal was caught\n" )); }
805 if (errno
== EINVAL
) { SOCKET_DEBUG(( "The highest number descriptor is negative\n" )); }
806 if (errno
== ENOMEM
) { SOCKET_DEBUG(( "Not enough memory\n" )); }
807 m_error
= wxSOCKET_TIMEDOUT
;
808 return wxSOCKET_TIMEDOUT
;
812 return wxSOCKET_NOERROR
;
816 * For blocking sockets, wait until data can be sent without
817 * blocking or until timeout ellapses.
819 wxSocketError
wxSocketImplUnix::Output_Timeout()
824 // Linux select() will overwrite the struct on return so make a copy
825 struct timeval tv
= m_timeout
;
827 SOCKET_DEBUG( ("m_non_blocking has: %d\n", (int)m_non_blocking
) );
831 wxFD_ZERO(&writefds
);
832 wxFD_SET(m_fd
, &writefds
);
833 ret
= select(m_fd
+ 1, NULL
, &writefds
, NULL
, &tv
);
836 SOCKET_DEBUG(( "Output_Timeout, select returned 0\n" ));
837 m_error
= wxSOCKET_TIMEDOUT
;
838 return wxSOCKET_TIMEDOUT
;
843 SOCKET_DEBUG(( "Output_Timeout, select returned -1\n" ));
844 if (errno
== EBADF
) { SOCKET_DEBUG(( "Invalid file descriptor\n" )); }
845 if (errno
== EINTR
) { SOCKET_DEBUG(( "A non blocked signal was caught\n" )); }
846 if (errno
== EINVAL
) { SOCKET_DEBUG(( "The highest number descriptor is negative\n" )); }
847 if (errno
== ENOMEM
) { SOCKET_DEBUG(( "Not enough memory\n" )); }
848 m_error
= wxSOCKET_TIMEDOUT
;
849 return wxSOCKET_TIMEDOUT
;
852 if ( ! wxFD_ISSET(m_fd
, &writefds
) )
854 SOCKET_DEBUG(( "Output_Timeout is buggy!\n" ));
858 SOCKET_DEBUG(( "Output_Timeout seems correct\n" ));
863 SOCKET_DEBUG(( "Output_Timeout, didn't try select!\n" ));
866 return wxSOCKET_NOERROR
;
869 int wxSocketImplUnix::Recv_Stream(char *buffer
, int size
)
874 ret
= recv(m_fd
, buffer
, size
, GSOCKET_MSG_NOSIGNAL
);
876 while (ret
== -1 && errno
== EINTR
); /* Loop until not interrupted */
881 int wxSocketImplUnix::Recv_Dgram(char *buffer
, int size
)
884 WX_SOCKLEN_T fromlen
= sizeof(from
);
888 fromlen
= sizeof(from
);
892 ret
= recvfrom(m_fd
, buffer
, size
, 0, (sockaddr
*)&from
, (WX_SOCKLEN_T
*) &fromlen
);
894 while (ret
== -1 && errno
== EINTR
); /* Loop until not interrupted */
899 /* Translate a system address into a wxSocketImplUnix address */
902 m_peer
= GAddress_new();
905 m_error
= wxSOCKET_MEMERR
;
910 err
= _GAddress_translate_from(m_peer
, (sockaddr
*)&from
, fromlen
);
911 if (err
!= wxSOCKET_NOERROR
)
913 GAddress_destroy(m_peer
);
922 int wxSocketImplUnix::Send_Stream(const char *buffer
, int size
)
930 ret
= send(m_fd
, (char *)buffer
, size
, GSOCKET_MSG_NOSIGNAL
);
932 while (ret
== -1 && errno
== EINTR
); /* Loop until not interrupted */
939 int wxSocketImplUnix::Send_Dgram(const char *buffer
, int size
)
941 struct sockaddr
*addr
;
947 m_error
= wxSOCKET_INVADDR
;
951 err
= _GAddress_translate_to(m_peer
, &addr
, &len
);
952 if (err
!= wxSOCKET_NOERROR
)
962 ret
= sendto(m_fd
, (char *)buffer
, size
, 0, addr
, len
);
964 while (ret
== -1 && errno
== EINTR
); /* Loop until not interrupted */
968 /* Frees memory allocated from _GAddress_translate_to */
974 void wxSocketImplUnix::OnStateChange(wxSocketNotify event
)
977 NotifyOnStateChange(event
);
979 if ( event
== wxSOCKET_LOST
)
983 void wxSocketImplUnix::OnReadWaiting()
987 if (m_fd
== INVALID_SOCKET
)
992 /* If we have already detected a LOST event, then don't try
993 * to do any further processing.
995 if ((m_detected
& wxSOCKET_LOST_FLAG
) != 0)
997 m_establishing
= false;
999 OnStateChange(wxSOCKET_LOST
);
1003 int num
= recv(m_fd
, &c
, 1, MSG_PEEK
| GSOCKET_MSG_NOSIGNAL
);
1007 OnStateChange(wxSOCKET_INPUT
);
1011 if (m_server
&& m_stream
)
1013 OnStateChange(wxSOCKET_CONNECTION
);
1019 /* graceful shutdown */
1020 OnStateChange(wxSOCKET_LOST
);
1024 /* Empty datagram received */
1025 OnStateChange(wxSOCKET_INPUT
);
1030 /* Do not throw a lost event in cases where the socket isn't really lost */
1031 if ((errno
== EWOULDBLOCK
) || (errno
== EAGAIN
) || (errno
== EINTR
))
1033 OnStateChange(wxSOCKET_INPUT
);
1037 OnStateChange(wxSOCKET_LOST
);
1043 void wxSocketImplUnix::OnWriteWaiting()
1045 /* If we have already detected a LOST event, then don't try
1046 * to do any further processing.
1048 if ((m_detected
& wxSOCKET_LOST_FLAG
) != 0)
1050 m_establishing
= false;
1052 OnStateChange(wxSOCKET_LOST
);
1056 if (m_establishing
&& !m_server
)
1059 SOCKOPTLEN_T len
= sizeof(error
);
1061 m_establishing
= false;
1063 getsockopt(m_fd
, SOL_SOCKET
, SO_ERROR
, (char*)&error
, &len
);
1067 OnStateChange(wxSOCKET_LOST
);
1071 OnStateChange(wxSOCKET_CONNECTION
);
1072 /* We have to fire this event by hand because CONNECTION (for clients)
1073 * and OUTPUT are internally the same and we just disabled CONNECTION
1074 * events with the above macro.
1076 OnStateChange(wxSOCKET_OUTPUT
);
1081 OnStateChange(wxSOCKET_OUTPUT
);
1085 void wxSocketImplUnix::OnExceptionWaiting()
1087 wxFAIL_MSG( "not supposed to be called" );
1091 * -------------------------------------------------------------------------
1093 * -------------------------------------------------------------------------
1096 /* CHECK_ADDRESS verifies that the current address family is either
1097 * wxSOCKET_NOFAMILY or wxSOCKET_*family*, and if it is wxSOCKET_NOFAMILY, it
1098 * initalizes it to be a wxSOCKET_*family*. In other cases, it returns
1099 * an appropiate error code.
1101 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
1103 #define CHECK_ADDRESS(address, family) \
1105 if (address->m_family == wxSOCKET_NOFAMILY) \
1106 if (_GAddress_Init_##family(address) != wxSOCKET_NOERROR) \
1107 return address->m_error; \
1108 if (address->m_family != wxSOCKET_##family) \
1110 address->m_error = wxSOCKET_INVADDR; \
1111 return wxSOCKET_INVADDR; \
1115 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
1117 if (address->m_family == wxSOCKET_NOFAMILY) \
1118 if (_GAddress_Init_##family(address) != wxSOCKET_NOERROR) \
1120 if (address->m_family != wxSOCKET_##family) \
1122 address->m_error = wxSOCKET_INVADDR; \
1128 GAddress
*GAddress_new(void)
1132 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1135 address
->m_family
= wxSOCKET_NOFAMILY
;
1136 address
->m_addr
= NULL
;
1142 GAddress
*GAddress_copy(GAddress
*address
)
1146 assert(address
!= NULL
);
1148 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1151 memcpy(addr2
, address
, sizeof(GAddress
));
1153 if (address
->m_addr
&& address
->m_len
> 0)
1155 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
1156 if (addr2
->m_addr
== NULL
)
1161 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1167 void GAddress_destroy(GAddress
*address
)
1169 assert(address
!= NULL
);
1171 if (address
->m_addr
)
1172 free(address
->m_addr
);
1177 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1179 assert(address
!= NULL
);
1181 address
->m_family
= type
;
1184 GAddressType
GAddress_GetFamily(GAddress
*address
)
1186 assert(address
!= NULL
);
1188 return address
->m_family
;
1191 wxSocketError
_GAddress_translate_from(GAddress
*address
,
1192 struct sockaddr
*addr
, int len
)
1194 address
->m_realfamily
= addr
->sa_family
;
1195 switch (addr
->sa_family
)
1198 address
->m_family
= wxSOCKET_INET
;
1201 address
->m_family
= wxSOCKET_UNIX
;
1205 address
->m_family
= wxSOCKET_INET6
;
1207 #endif // wxUSE_IPV6
1210 address
->m_error
= wxSOCKET_INVOP
;
1211 return wxSOCKET_INVOP
;
1215 if (address
->m_addr
)
1216 free(address
->m_addr
);
1218 address
->m_len
= len
;
1219 address
->m_addr
= (struct sockaddr
*)malloc(len
);
1221 if (address
->m_addr
== NULL
)
1223 address
->m_error
= wxSOCKET_MEMERR
;
1224 return wxSOCKET_MEMERR
;
1227 memcpy(address
->m_addr
, addr
, len
);
1229 return wxSOCKET_NOERROR
;
1232 wxSocketError
_GAddress_translate_to(GAddress
*address
,
1233 struct sockaddr
**addr
, int *len
)
1235 if (!address
->m_addr
)
1237 address
->m_error
= wxSOCKET_INVADDR
;
1238 return wxSOCKET_INVADDR
;
1241 *len
= address
->m_len
;
1242 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
1245 address
->m_error
= wxSOCKET_MEMERR
;
1246 return wxSOCKET_MEMERR
;
1249 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1250 return wxSOCKET_NOERROR
;
1254 * -------------------------------------------------------------------------
1255 * Internet address family
1256 * -------------------------------------------------------------------------
1259 wxSocketError
_GAddress_Init_INET(GAddress
*address
)
1261 address
->m_len
= sizeof(struct sockaddr_in
);
1262 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1263 if (address
->m_addr
== NULL
)
1265 address
->m_error
= wxSOCKET_MEMERR
;
1266 return wxSOCKET_MEMERR
;
1269 address
->m_family
= wxSOCKET_INET
;
1270 address
->m_realfamily
= PF_INET
;
1271 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1272 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1274 return wxSOCKET_NOERROR
;
1277 wxSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1280 struct in_addr
*addr
;
1282 assert(address
!= NULL
);
1284 CHECK_ADDRESS(address
, INET
);
1286 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1288 /* If it is a numeric host name, convert it now */
1289 #if defined(HAVE_INET_ATON)
1290 if (inet_aton(hostname
, addr
) == 0)
1292 #elif defined(HAVE_INET_ADDR)
1293 if ( (addr
->s_addr
= inet_addr(hostname
)) == (unsigned)-1 )
1296 /* Use gethostbyname by default */
1298 int val
= 1; /* VA doesn't like constants in conditional expressions */
1303 struct in_addr
*array_addr
;
1305 /* It is a real name, we solve it */
1307 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
1308 struct hostent_data buffer
;
1313 he
= wxGethostbyname_r(hostname
, &h
, (void*)&buffer
, sizeof(buffer
), &err
);
1316 /* Reset to invalid address */
1317 addr
->s_addr
= INADDR_NONE
;
1318 address
->m_error
= wxSOCKET_NOHOST
;
1319 return wxSOCKET_NOHOST
;
1322 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1323 addr
->s_addr
= array_addr
[0].s_addr
;
1326 return wxSOCKET_NOERROR
;
1330 wxSocketError
GAddress_INET_SetBroadcastAddress(GAddress
*address
)
1332 return GAddress_INET_SetHostAddress(address
, INADDR_BROADCAST
);
1335 wxSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1337 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1340 wxSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1341 unsigned long hostaddr
)
1343 struct in_addr
*addr
;
1345 assert(address
!= NULL
);
1347 CHECK_ADDRESS(address
, INET
);
1349 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1350 addr
->s_addr
= htonl(hostaddr
);
1352 return wxSOCKET_NOERROR
;
1355 wxSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1356 const char *protocol
)
1359 struct sockaddr_in
*addr
;
1361 assert(address
!= NULL
);
1362 CHECK_ADDRESS(address
, INET
);
1366 address
->m_error
= wxSOCKET_INVPORT
;
1367 return wxSOCKET_INVPORT
;
1370 #if defined(HAVE_FUNC_GETSERVBYNAME_R_4)
1371 struct servent_data buffer
;
1375 struct servent serv
;
1376 se
= wxGetservbyname_r(port
, protocol
, &serv
,
1377 (void*)&buffer
, sizeof(buffer
));
1380 /* the cast to int suppresses compiler warnings about subscript having the
1382 if (isdigit((int)port
[0]))
1386 port_int
= atoi(port
);
1387 addr
= (struct sockaddr_in
*)address
->m_addr
;
1388 addr
->sin_port
= htons(port_int
);
1389 return wxSOCKET_NOERROR
;
1392 address
->m_error
= wxSOCKET_INVPORT
;
1393 return wxSOCKET_INVPORT
;
1396 addr
= (struct sockaddr_in
*)address
->m_addr
;
1397 addr
->sin_port
= se
->s_port
;
1399 return wxSOCKET_NOERROR
;
1402 wxSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1404 struct sockaddr_in
*addr
;
1406 assert(address
!= NULL
);
1407 CHECK_ADDRESS(address
, INET
);
1409 addr
= (struct sockaddr_in
*)address
->m_addr
;
1410 addr
->sin_port
= htons(port
);
1412 return wxSOCKET_NOERROR
;
1415 wxSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1419 struct sockaddr_in
*addr
;
1421 assert(address
!= NULL
);
1422 CHECK_ADDRESS(address
, INET
);
1424 addr
= (struct sockaddr_in
*)address
->m_addr
;
1425 addr_buf
= (char *)&(addr
->sin_addr
);
1427 struct hostent temphost
;
1428 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
1429 struct hostent_data buffer
;
1434 he
= wxGethostbyaddr_r(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
, &temphost
,
1435 (void*)&buffer
, sizeof(buffer
), &err
);
1438 address
->m_error
= wxSOCKET_NOHOST
;
1439 return wxSOCKET_NOHOST
;
1442 strncpy(hostname
, he
->h_name
, sbuf
);
1444 return wxSOCKET_NOERROR
;
1447 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1449 struct sockaddr_in
*addr
;
1451 assert(address
!= NULL
);
1452 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1454 addr
= (struct sockaddr_in
*)address
->m_addr
;
1456 return ntohl(addr
->sin_addr
.s_addr
);
1459 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1461 struct sockaddr_in
*addr
;
1463 assert(address
!= NULL
);
1464 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1466 addr
= (struct sockaddr_in
*)address
->m_addr
;
1467 return ntohs(addr
->sin_port
);
1472 * -------------------------------------------------------------------------
1473 * Internet IPv6 address family
1474 * -------------------------------------------------------------------------
1477 wxSocketError
_GAddress_Init_INET6(GAddress
*address
)
1479 struct in6_addr any_address
= IN6ADDR_ANY_INIT
;
1480 address
->m_len
= sizeof(struct sockaddr_in6
);
1481 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1482 if (address
->m_addr
== NULL
)
1484 address
->m_error
= wxSOCKET_MEMERR
;
1485 return wxSOCKET_MEMERR
;
1487 memset(address
->m_addr
,0,address
->m_len
);
1489 address
->m_family
= wxSOCKET_INET6
;
1490 address
->m_realfamily
= AF_INET6
;
1491 ((struct sockaddr_in6
*)address
->m_addr
)->sin6_family
= AF_INET6
;
1492 ((struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
= any_address
;
1494 return wxSOCKET_NOERROR
;
1497 wxSocketError
GAddress_INET6_SetHostName(GAddress
*address
, const char *hostname
)
1499 assert(address
!= NULL
);
1500 CHECK_ADDRESS(address
, INET6
);
1503 memset( & hints
, 0, sizeof( hints
) );
1504 hints
.ai_family
= AF_INET6
;
1505 addrinfo
* info
= 0;
1506 if ( getaddrinfo( hostname
, "0", & hints
, & info
) || ! info
)
1508 address
->m_error
= wxSOCKET_NOHOST
;
1509 return wxSOCKET_NOHOST
;
1512 memcpy( address
->m_addr
, info
->ai_addr
, info
->ai_addrlen
);
1513 freeaddrinfo( info
);
1514 return wxSOCKET_NOERROR
;
1517 wxSocketError
GAddress_INET6_SetAnyAddress(GAddress
*address
)
1519 assert(address
!= NULL
);
1521 CHECK_ADDRESS(address
, INET6
);
1523 struct in6_addr addr
;
1524 memset( & addr
, 0, sizeof( addr
) );
1525 return GAddress_INET6_SetHostAddress(address
, addr
);
1527 wxSocketError
GAddress_INET6_SetHostAddress(GAddress
*address
,
1528 struct in6_addr hostaddr
)
1530 assert(address
!= NULL
);
1532 CHECK_ADDRESS(address
, INET6
);
1534 ((struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
= hostaddr
;
1536 return wxSOCKET_NOERROR
;
1539 wxSocketError
GAddress_INET6_SetPortName(GAddress
*address
, const char *port
,
1540 const char *protocol
)
1543 struct sockaddr_in6
*addr
;
1545 assert(address
!= NULL
);
1546 CHECK_ADDRESS(address
, INET6
);
1550 address
->m_error
= wxSOCKET_INVPORT
;
1551 return wxSOCKET_INVPORT
;
1554 se
= getservbyname(port
, protocol
);
1557 if (isdigit(port
[0]))
1561 port_int
= atoi(port
);
1562 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1563 addr
->sin6_port
= htons((u_short
) port_int
);
1564 return wxSOCKET_NOERROR
;
1567 address
->m_error
= wxSOCKET_INVPORT
;
1568 return wxSOCKET_INVPORT
;
1571 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1572 addr
->sin6_port
= se
->s_port
;
1574 return wxSOCKET_NOERROR
;
1577 wxSocketError
GAddress_INET6_SetPort(GAddress
*address
, unsigned short port
)
1579 struct sockaddr_in6
*addr
;
1581 assert(address
!= NULL
);
1582 CHECK_ADDRESS(address
, INET6
);
1584 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1585 addr
->sin6_port
= htons(port
);
1587 return wxSOCKET_NOERROR
;
1590 wxSocketError
GAddress_INET6_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1594 struct sockaddr_in6
*addr
;
1596 assert(address
!= NULL
);
1597 CHECK_ADDRESS(address
, INET6
);
1599 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1600 addr_buf
= (char *)&(addr
->sin6_addr
);
1602 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin6_addr
), AF_INET6
);
1605 address
->m_error
= wxSOCKET_NOHOST
;
1606 return wxSOCKET_NOHOST
;
1609 strncpy(hostname
, he
->h_name
, sbuf
);
1611 return wxSOCKET_NOERROR
;
1614 wxSocketError
GAddress_INET6_GetHostAddress(GAddress
*address
,struct in6_addr
*hostaddr
)
1616 assert(address
!= NULL
);
1617 assert(hostaddr
!= NULL
);
1618 CHECK_ADDRESS_RETVAL(address
, INET6
, wxSOCKET_INVADDR
);
1619 *hostaddr
= ( (struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
;
1620 return wxSOCKET_NOERROR
;
1623 unsigned short GAddress_INET6_GetPort(GAddress
*address
)
1625 assert(address
!= NULL
);
1626 CHECK_ADDRESS_RETVAL(address
, INET6
, 0);
1628 return ntohs( ((struct sockaddr_in6
*)address
->m_addr
)->sin6_port
);
1631 #endif // wxUSE_IPV6
1634 * -------------------------------------------------------------------------
1635 * Unix address family
1636 * -------------------------------------------------------------------------
1639 #ifndef __VISAGECPP__
1640 wxSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1642 address
->m_len
= sizeof(struct sockaddr_un
);
1643 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
1644 if (address
->m_addr
== NULL
)
1646 address
->m_error
= wxSOCKET_MEMERR
;
1647 return wxSOCKET_MEMERR
;
1650 address
->m_family
= wxSOCKET_UNIX
;
1651 address
->m_realfamily
= PF_UNIX
;
1652 ((struct sockaddr_un
*)address
->m_addr
)->sun_family
= AF_UNIX
;
1653 ((struct sockaddr_un
*)address
->m_addr
)->sun_path
[0] = 0;
1655 return wxSOCKET_NOERROR
;
1658 #define UNIX_SOCK_PATHLEN (sizeof(addr->sun_path)/sizeof(addr->sun_path[0]))
1660 wxSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1662 struct sockaddr_un
*addr
;
1664 assert(address
!= NULL
);
1666 CHECK_ADDRESS(address
, UNIX
);
1668 addr
= ((struct sockaddr_un
*)address
->m_addr
);
1669 strncpy(addr
->sun_path
, path
, UNIX_SOCK_PATHLEN
);
1670 addr
->sun_path
[UNIX_SOCK_PATHLEN
- 1] = '\0';
1672 return wxSOCKET_NOERROR
;
1675 wxSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1677 struct sockaddr_un
*addr
;
1679 assert(address
!= NULL
);
1680 CHECK_ADDRESS(address
, UNIX
);
1682 addr
= (struct sockaddr_un
*)address
->m_addr
;
1684 strncpy(path
, addr
->sun_path
, sbuf
);
1686 return wxSOCKET_NOERROR
;
1688 #endif /* !defined(__VISAGECPP__) */
1690 #endif /* wxUSE_SOCKETS */