]>
git.saurik.com Git - wxWidgets.git/blob - src/unix/sockunix.cpp
1893e118e88ca491c1c8943495d40f390567dc7d
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__ */
436 * Disallow further read/write operations on this socket, close
437 * the fd and disable all callbacks.
439 void wxSocketImplUnix::Shutdown()
441 /* Don't allow events to fire after socket has been closed */
444 wxSocketImpl::Shutdown();
448 * Waits for an incoming client connection. Returns a pointer to
449 * a wxSocketImplUnix object, or NULL if there was an error, in which case
450 * the last error field will be updated for the calling wxSocketImplUnix.
452 * Error codes (set in the calling wxSocketImplUnix)
453 * wxSOCKET_INVSOCK - the socket is not valid or not a server.
454 * wxSOCKET_TIMEDOUT - timeout, no incoming connections.
455 * wxSOCKET_WOULDBLOCK - the call would block and the socket is nonblocking.
456 * wxSOCKET_MEMERR - couldn't allocate memory.
457 * wxSOCKET_IOERR - low-level error.
459 wxSocketImpl
*wxSocketImplUnix::WaitConnection(wxSocketBase
& wxsocket
)
462 WX_SOCKLEN_T fromlen
= sizeof(from
);
463 wxSocketImpl
*connection
;
467 /* If the socket has already been created, we exit immediately */
468 if (m_fd
== INVALID_SOCKET
|| !m_server
)
470 m_error
= wxSOCKET_INVSOCK
;
474 /* Create a wxSocketImplUnix object for the new connection */
475 connection
= wxSocketImplUnix::Create(wxsocket
);
479 m_error
= wxSOCKET_MEMERR
;
483 /* Wait for a connection (with timeout) */
484 if (Input_Timeout() == wxSOCKET_TIMEDOUT
)
487 /* m_error set by Input_Timeout */
491 connection
->m_fd
= accept(m_fd
, (sockaddr
*)&from
, (WX_SOCKLEN_T
*) &fromlen
);
493 /* Reenable CONNECTION events */
494 EnableEvent(wxSOCKET_CONNECTION
);
496 if (connection
->m_fd
== INVALID_SOCKET
)
498 if (errno
== EWOULDBLOCK
)
499 m_error
= wxSOCKET_WOULDBLOCK
;
501 m_error
= wxSOCKET_IOERR
;
507 /* Initialize all fields */
508 connection
->m_server
= false;
509 connection
->m_stream
= true;
511 /* Setup the peer address field */
512 connection
->m_peer
= GAddress_new();
513 if (!connection
->m_peer
)
516 m_error
= wxSOCKET_MEMERR
;
520 err
= _GAddress_translate_from(connection
->m_peer
, (sockaddr
*)&from
, fromlen
);
521 if (err
!= wxSOCKET_NOERROR
)
528 #if defined(__EMX__) || defined(__VISAGECPP__)
529 ioctl(connection
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
531 ioctl(connection
->m_fd
, FIONBIO
, &arg
);
534 connection
->Notify(true);
539 void wxSocketImplUnix::Notify(bool flag
)
541 if (flag
== m_use_events
)
544 DoEnableEvents(flag
);
547 void wxSocketImplUnix::DoEnableEvents(bool flag
)
549 wxSocketManager
* const manager
= wxSocketManager::Get();
552 manager
->Install_Callback(this, wxSOCKET_INPUT
);
553 manager
->Install_Callback(this, wxSOCKET_OUTPUT
);
557 manager
->Uninstall_Callback(this, wxSOCKET_INPUT
);
558 manager
->Uninstall_Callback(this, wxSOCKET_OUTPUT
);
562 wxSocketError
wxSocketImplUnix::DoHandleConnect(int ret
)
564 /* We only call EnableEvents() if we know we aren't shutting down the socket.
565 * NB: EnableEvents() needs to be called whether the socket is blocking or
566 * non-blocking, it just shouldn't be called prior to knowing there is a
567 * connection _if_ blocking sockets are being used.
568 * If connect above returns 0, we are already connected and need to make the
569 * call to EnableEvents() now.
571 if ( m_non_blocking
|| (ret
== 0) )
576 const int err
= errno
;
578 /* If connect failed with EINPROGRESS and the wxSocketImplUnix object
579 * is in blocking mode, we select() for the specified timeout
580 * checking for writability to see if the connection request
583 if ((err
== EINPROGRESS
) && (!m_non_blocking
))
585 if (Output_Timeout() == wxSOCKET_TIMEDOUT
)
588 /* m_error is set in Output_Timeout */
589 return wxSOCKET_TIMEDOUT
;
594 SOCKOPTLEN_T len
= sizeof(error
);
596 getsockopt(m_fd
, SOL_SOCKET
, SO_ERROR
, (char*) &error
, &len
);
600 return wxSOCKET_NOERROR
;
604 /* If connect failed with EINPROGRESS and the wxSocketImplUnix object
605 * is set to nonblocking, we set m_error to wxSOCKET_WOULDBLOCK
606 * (and return wxSOCKET_WOULDBLOCK) but we don't close the socket;
607 * this way if the connection completes, a wxSOCKET_CONNECTION
608 * event will be generated, if enabled.
610 if ((err
== EINPROGRESS
) && (m_non_blocking
))
612 m_establishing
= true;
613 m_error
= wxSOCKET_WOULDBLOCK
;
614 return wxSOCKET_WOULDBLOCK
;
617 /* If connect failed with an error other than EINPROGRESS,
618 * then the call to Connect has failed.
621 m_error
= wxSOCKET_IOERR
;
623 return wxSOCKET_IOERR
;
626 return wxSOCKET_NOERROR
;
631 /* Like recv(), send(), ... */
632 int wxSocketImplUnix::Read(char *buffer
, int size
)
636 if (m_fd
== INVALID_SOCKET
|| m_server
)
638 m_error
= wxSOCKET_INVSOCK
;
642 /* Disable events during query of socket status */
643 DisableEvent(wxSOCKET_INPUT
);
645 /* If the socket is blocking, wait for data (with a timeout) */
646 if (Input_Timeout() == wxSOCKET_TIMEDOUT
) {
647 m_error
= wxSOCKET_TIMEDOUT
;
648 /* Don't return here immediately, otherwise socket events would not be
656 ret
= Recv_Stream(buffer
, size
);
658 ret
= Recv_Dgram(buffer
, size
);
661 * If recv returned zero for a TCP socket (if m_stream == NULL, it's an UDP
662 * socket and empty datagrams are possible), then the connection has been
665 * Otherwise, recv has returned an error (-1), in which case we have lost
666 * the socket only if errno does _not_ indicate that there may be more data
669 if ((ret
== 0) && m_stream
)
671 /* Make sure wxSOCKET_LOST event gets sent and shut down the socket */
674 m_detected
= wxSOCKET_LOST_FLAG
;
681 if ((errno
== EWOULDBLOCK
) || (errno
== EAGAIN
))
682 m_error
= wxSOCKET_WOULDBLOCK
;
684 m_error
= wxSOCKET_IOERR
;
688 /* Enable events again now that we are done processing */
689 EnableEvent(wxSOCKET_INPUT
);
694 int wxSocketImplUnix::Write(const char *buffer
, int size
)
698 SOCKET_DEBUG(( "Write #1, size %d\n", size
));
700 if (m_fd
== INVALID_SOCKET
|| m_server
)
702 m_error
= wxSOCKET_INVSOCK
;
706 SOCKET_DEBUG(( "Write #2, size %d\n", size
));
708 /* If the socket is blocking, wait for writability (with a timeout) */
709 if (Output_Timeout() == wxSOCKET_TIMEDOUT
)
712 SOCKET_DEBUG(( "Write #3, size %d\n", size
));
716 ret
= Send_Stream(buffer
, size
);
718 ret
= Send_Dgram(buffer
, size
);
720 SOCKET_DEBUG(( "Write #4, size %d\n", size
));
724 if ((errno
== EWOULDBLOCK
) || (errno
== EAGAIN
))
726 m_error
= wxSOCKET_WOULDBLOCK
;
727 SOCKET_DEBUG(( "Write error WOULDBLOCK\n" ));
731 m_error
= wxSOCKET_IOERR
;
732 SOCKET_DEBUG(( "Write error IOERR\n" ));
735 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
736 * in MSW). Once the first OUTPUT event is received, users can assume
737 * that the socket is writable until a read operation fails. Only then
738 * will further OUTPUT events be posted.
740 EnableEvent(wxSOCKET_OUTPUT
);
745 SOCKET_DEBUG(( "Write #5, size %d ret %d\n", size
, ret
));
752 void wxSocketImplUnix::EnableEvent(wxSocketNotify event
)
756 m_detected
&= ~(1 << event
);
757 wxSocketManager::Get()->Install_Callback(this, event
);
761 void wxSocketImplUnix::DisableEvent(wxSocketNotify event
)
765 m_detected
|= (1 << event
);
766 wxSocketManager::Get()->Uninstall_Callback(this, event
);
771 * For blocking sockets, wait until data is available or
772 * until timeout ellapses.
774 wxSocketError
wxSocketImplUnix::Input_Timeout()
779 // Linux select() will overwrite the struct on return so make a copy
780 struct timeval tv
= m_timeout
;
785 wxFD_SET(m_fd
, &readfds
);
786 ret
= select(m_fd
+ 1, &readfds
, NULL
, NULL
, &tv
);
789 SOCKET_DEBUG(( "Input_Timeout, select returned 0\n" ));
790 m_error
= wxSOCKET_TIMEDOUT
;
791 return wxSOCKET_TIMEDOUT
;
796 SOCKET_DEBUG(( "Input_Timeout, select returned -1\n" ));
797 if (errno
== EBADF
) { SOCKET_DEBUG(( "Invalid file descriptor\n" )); }
798 if (errno
== EINTR
) { SOCKET_DEBUG(( "A non blocked signal was caught\n" )); }
799 if (errno
== EINVAL
) { SOCKET_DEBUG(( "The highest number descriptor is negative\n" )); }
800 if (errno
== ENOMEM
) { SOCKET_DEBUG(( "Not enough memory\n" )); }
801 m_error
= wxSOCKET_TIMEDOUT
;
802 return wxSOCKET_TIMEDOUT
;
806 return wxSOCKET_NOERROR
;
810 * For blocking sockets, wait until data can be sent without
811 * blocking or until timeout ellapses.
813 wxSocketError
wxSocketImplUnix::Output_Timeout()
818 // Linux select() will overwrite the struct on return so make a copy
819 struct timeval tv
= m_timeout
;
821 SOCKET_DEBUG( ("m_non_blocking has: %d\n", (int)m_non_blocking
) );
825 wxFD_ZERO(&writefds
);
826 wxFD_SET(m_fd
, &writefds
);
827 ret
= select(m_fd
+ 1, NULL
, &writefds
, NULL
, &tv
);
830 SOCKET_DEBUG(( "Output_Timeout, select returned 0\n" ));
831 m_error
= wxSOCKET_TIMEDOUT
;
832 return wxSOCKET_TIMEDOUT
;
837 SOCKET_DEBUG(( "Output_Timeout, select returned -1\n" ));
838 if (errno
== EBADF
) { SOCKET_DEBUG(( "Invalid file descriptor\n" )); }
839 if (errno
== EINTR
) { SOCKET_DEBUG(( "A non blocked signal was caught\n" )); }
840 if (errno
== EINVAL
) { SOCKET_DEBUG(( "The highest number descriptor is negative\n" )); }
841 if (errno
== ENOMEM
) { SOCKET_DEBUG(( "Not enough memory\n" )); }
842 m_error
= wxSOCKET_TIMEDOUT
;
843 return wxSOCKET_TIMEDOUT
;
846 if ( ! wxFD_ISSET(m_fd
, &writefds
) )
848 SOCKET_DEBUG(( "Output_Timeout is buggy!\n" ));
852 SOCKET_DEBUG(( "Output_Timeout seems correct\n" ));
857 SOCKET_DEBUG(( "Output_Timeout, didn't try select!\n" ));
860 return wxSOCKET_NOERROR
;
863 int wxSocketImplUnix::Recv_Stream(char *buffer
, int size
)
868 ret
= recv(m_fd
, buffer
, size
, GSOCKET_MSG_NOSIGNAL
);
870 while (ret
== -1 && errno
== EINTR
); /* Loop until not interrupted */
875 int wxSocketImplUnix::Recv_Dgram(char *buffer
, int size
)
878 WX_SOCKLEN_T fromlen
= sizeof(from
);
882 fromlen
= sizeof(from
);
886 ret
= recvfrom(m_fd
, buffer
, size
, 0, (sockaddr
*)&from
, (WX_SOCKLEN_T
*) &fromlen
);
888 while (ret
== -1 && errno
== EINTR
); /* Loop until not interrupted */
893 /* Translate a system address into a wxSocketImplUnix address */
896 m_peer
= GAddress_new();
899 m_error
= wxSOCKET_MEMERR
;
904 err
= _GAddress_translate_from(m_peer
, (sockaddr
*)&from
, fromlen
);
905 if (err
!= wxSOCKET_NOERROR
)
907 GAddress_destroy(m_peer
);
916 int wxSocketImplUnix::Send_Stream(const char *buffer
, int size
)
924 ret
= send(m_fd
, (char *)buffer
, size
, GSOCKET_MSG_NOSIGNAL
);
926 while (ret
== -1 && errno
== EINTR
); /* Loop until not interrupted */
933 int wxSocketImplUnix::Send_Dgram(const char *buffer
, int size
)
935 struct sockaddr
*addr
;
941 m_error
= wxSOCKET_INVADDR
;
945 err
= _GAddress_translate_to(m_peer
, &addr
, &len
);
946 if (err
!= wxSOCKET_NOERROR
)
956 ret
= sendto(m_fd
, (char *)buffer
, size
, 0, addr
, len
);
958 while (ret
== -1 && errno
== EINTR
); /* Loop until not interrupted */
962 /* Frees memory allocated from _GAddress_translate_to */
968 void wxSocketImplUnix::OnStateChange(wxSocketNotify event
)
971 NotifyOnStateChange(event
);
973 if ( event
== wxSOCKET_LOST
)
977 void wxSocketImplUnix::Detected_Read()
981 /* Safeguard against straggling call to Detected_Read */
982 if (m_fd
== INVALID_SOCKET
)
987 /* If we have already detected a LOST event, then don't try
988 * to do any further processing.
990 if ((m_detected
& wxSOCKET_LOST_FLAG
) != 0)
992 m_establishing
= false;
994 OnStateChange(wxSOCKET_LOST
);
998 int num
= recv(m_fd
, &c
, 1, MSG_PEEK
| GSOCKET_MSG_NOSIGNAL
);
1002 OnStateChange(wxSOCKET_INPUT
);
1006 if (m_server
&& m_stream
)
1008 OnStateChange(wxSOCKET_CONNECTION
);
1014 /* graceful shutdown */
1015 OnStateChange(wxSOCKET_LOST
);
1019 /* Empty datagram received */
1020 OnStateChange(wxSOCKET_INPUT
);
1025 /* Do not throw a lost event in cases where the socket isn't really lost */
1026 if ((errno
== EWOULDBLOCK
) || (errno
== EAGAIN
) || (errno
== EINTR
))
1028 OnStateChange(wxSOCKET_INPUT
);
1032 OnStateChange(wxSOCKET_LOST
);
1038 void wxSocketImplUnix::Detected_Write()
1040 /* If we have already detected a LOST event, then don't try
1041 * to do any further processing.
1043 if ((m_detected
& wxSOCKET_LOST_FLAG
) != 0)
1045 m_establishing
= false;
1047 OnStateChange(wxSOCKET_LOST
);
1051 if (m_establishing
&& !m_server
)
1054 SOCKOPTLEN_T len
= sizeof(error
);
1056 m_establishing
= false;
1058 getsockopt(m_fd
, SOL_SOCKET
, SO_ERROR
, (char*)&error
, &len
);
1062 OnStateChange(wxSOCKET_LOST
);
1066 OnStateChange(wxSOCKET_CONNECTION
);
1067 /* We have to fire this event by hand because CONNECTION (for clients)
1068 * and OUTPUT are internally the same and we just disabled CONNECTION
1069 * events with the above macro.
1071 OnStateChange(wxSOCKET_OUTPUT
);
1076 OnStateChange(wxSOCKET_OUTPUT
);
1081 * -------------------------------------------------------------------------
1083 * -------------------------------------------------------------------------
1086 /* CHECK_ADDRESS verifies that the current address family is either
1087 * wxSOCKET_NOFAMILY or wxSOCKET_*family*, and if it is wxSOCKET_NOFAMILY, it
1088 * initalizes it to be a wxSOCKET_*family*. In other cases, it returns
1089 * an appropiate error code.
1091 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
1093 #define CHECK_ADDRESS(address, family) \
1095 if (address->m_family == wxSOCKET_NOFAMILY) \
1096 if (_GAddress_Init_##family(address) != wxSOCKET_NOERROR) \
1097 return address->m_error; \
1098 if (address->m_family != wxSOCKET_##family) \
1100 address->m_error = wxSOCKET_INVADDR; \
1101 return wxSOCKET_INVADDR; \
1105 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
1107 if (address->m_family == wxSOCKET_NOFAMILY) \
1108 if (_GAddress_Init_##family(address) != wxSOCKET_NOERROR) \
1110 if (address->m_family != wxSOCKET_##family) \
1112 address->m_error = wxSOCKET_INVADDR; \
1118 GAddress
*GAddress_new(void)
1122 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1125 address
->m_family
= wxSOCKET_NOFAMILY
;
1126 address
->m_addr
= NULL
;
1132 GAddress
*GAddress_copy(GAddress
*address
)
1136 assert(address
!= NULL
);
1138 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1141 memcpy(addr2
, address
, sizeof(GAddress
));
1143 if (address
->m_addr
&& address
->m_len
> 0)
1145 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
1146 if (addr2
->m_addr
== NULL
)
1151 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1157 void GAddress_destroy(GAddress
*address
)
1159 assert(address
!= NULL
);
1161 if (address
->m_addr
)
1162 free(address
->m_addr
);
1167 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1169 assert(address
!= NULL
);
1171 address
->m_family
= type
;
1174 GAddressType
GAddress_GetFamily(GAddress
*address
)
1176 assert(address
!= NULL
);
1178 return address
->m_family
;
1181 wxSocketError
_GAddress_translate_from(GAddress
*address
,
1182 struct sockaddr
*addr
, int len
)
1184 address
->m_realfamily
= addr
->sa_family
;
1185 switch (addr
->sa_family
)
1188 address
->m_family
= wxSOCKET_INET
;
1191 address
->m_family
= wxSOCKET_UNIX
;
1195 address
->m_family
= wxSOCKET_INET6
;
1197 #endif // wxUSE_IPV6
1200 address
->m_error
= wxSOCKET_INVOP
;
1201 return wxSOCKET_INVOP
;
1205 if (address
->m_addr
)
1206 free(address
->m_addr
);
1208 address
->m_len
= len
;
1209 address
->m_addr
= (struct sockaddr
*)malloc(len
);
1211 if (address
->m_addr
== NULL
)
1213 address
->m_error
= wxSOCKET_MEMERR
;
1214 return wxSOCKET_MEMERR
;
1217 memcpy(address
->m_addr
, addr
, len
);
1219 return wxSOCKET_NOERROR
;
1222 wxSocketError
_GAddress_translate_to(GAddress
*address
,
1223 struct sockaddr
**addr
, int *len
)
1225 if (!address
->m_addr
)
1227 address
->m_error
= wxSOCKET_INVADDR
;
1228 return wxSOCKET_INVADDR
;
1231 *len
= address
->m_len
;
1232 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
1235 address
->m_error
= wxSOCKET_MEMERR
;
1236 return wxSOCKET_MEMERR
;
1239 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1240 return wxSOCKET_NOERROR
;
1244 * -------------------------------------------------------------------------
1245 * Internet address family
1246 * -------------------------------------------------------------------------
1249 wxSocketError
_GAddress_Init_INET(GAddress
*address
)
1251 address
->m_len
= sizeof(struct sockaddr_in
);
1252 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1253 if (address
->m_addr
== NULL
)
1255 address
->m_error
= wxSOCKET_MEMERR
;
1256 return wxSOCKET_MEMERR
;
1259 address
->m_family
= wxSOCKET_INET
;
1260 address
->m_realfamily
= PF_INET
;
1261 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1262 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1264 return wxSOCKET_NOERROR
;
1267 wxSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1270 struct in_addr
*addr
;
1272 assert(address
!= NULL
);
1274 CHECK_ADDRESS(address
, INET
);
1276 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1278 /* If it is a numeric host name, convert it now */
1279 #if defined(HAVE_INET_ATON)
1280 if (inet_aton(hostname
, addr
) == 0)
1282 #elif defined(HAVE_INET_ADDR)
1283 if ( (addr
->s_addr
= inet_addr(hostname
)) == (unsigned)-1 )
1286 /* Use gethostbyname by default */
1288 int val
= 1; /* VA doesn't like constants in conditional expressions */
1293 struct in_addr
*array_addr
;
1295 /* It is a real name, we solve it */
1297 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
1298 struct hostent_data buffer
;
1303 he
= wxGethostbyname_r(hostname
, &h
, (void*)&buffer
, sizeof(buffer
), &err
);
1306 /* Reset to invalid address */
1307 addr
->s_addr
= INADDR_NONE
;
1308 address
->m_error
= wxSOCKET_NOHOST
;
1309 return wxSOCKET_NOHOST
;
1312 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1313 addr
->s_addr
= array_addr
[0].s_addr
;
1316 return wxSOCKET_NOERROR
;
1320 wxSocketError
GAddress_INET_SetBroadcastAddress(GAddress
*address
)
1322 return GAddress_INET_SetHostAddress(address
, INADDR_BROADCAST
);
1325 wxSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1327 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1330 wxSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1331 unsigned long hostaddr
)
1333 struct in_addr
*addr
;
1335 assert(address
!= NULL
);
1337 CHECK_ADDRESS(address
, INET
);
1339 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1340 addr
->s_addr
= htonl(hostaddr
);
1342 return wxSOCKET_NOERROR
;
1345 wxSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1346 const char *protocol
)
1349 struct sockaddr_in
*addr
;
1351 assert(address
!= NULL
);
1352 CHECK_ADDRESS(address
, INET
);
1356 address
->m_error
= wxSOCKET_INVPORT
;
1357 return wxSOCKET_INVPORT
;
1360 #if defined(HAVE_FUNC_GETSERVBYNAME_R_4)
1361 struct servent_data buffer
;
1365 struct servent serv
;
1366 se
= wxGetservbyname_r(port
, protocol
, &serv
,
1367 (void*)&buffer
, sizeof(buffer
));
1370 /* the cast to int suppresses compiler warnings about subscript having the
1372 if (isdigit((int)port
[0]))
1376 port_int
= atoi(port
);
1377 addr
= (struct sockaddr_in
*)address
->m_addr
;
1378 addr
->sin_port
= htons(port_int
);
1379 return wxSOCKET_NOERROR
;
1382 address
->m_error
= wxSOCKET_INVPORT
;
1383 return wxSOCKET_INVPORT
;
1386 addr
= (struct sockaddr_in
*)address
->m_addr
;
1387 addr
->sin_port
= se
->s_port
;
1389 return wxSOCKET_NOERROR
;
1392 wxSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1394 struct sockaddr_in
*addr
;
1396 assert(address
!= NULL
);
1397 CHECK_ADDRESS(address
, INET
);
1399 addr
= (struct sockaddr_in
*)address
->m_addr
;
1400 addr
->sin_port
= htons(port
);
1402 return wxSOCKET_NOERROR
;
1405 wxSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1409 struct sockaddr_in
*addr
;
1411 assert(address
!= NULL
);
1412 CHECK_ADDRESS(address
, INET
);
1414 addr
= (struct sockaddr_in
*)address
->m_addr
;
1415 addr_buf
= (char *)&(addr
->sin_addr
);
1417 struct hostent temphost
;
1418 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
1419 struct hostent_data buffer
;
1424 he
= wxGethostbyaddr_r(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
, &temphost
,
1425 (void*)&buffer
, sizeof(buffer
), &err
);
1428 address
->m_error
= wxSOCKET_NOHOST
;
1429 return wxSOCKET_NOHOST
;
1432 strncpy(hostname
, he
->h_name
, sbuf
);
1434 return wxSOCKET_NOERROR
;
1437 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1439 struct sockaddr_in
*addr
;
1441 assert(address
!= NULL
);
1442 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1444 addr
= (struct sockaddr_in
*)address
->m_addr
;
1446 return ntohl(addr
->sin_addr
.s_addr
);
1449 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1451 struct sockaddr_in
*addr
;
1453 assert(address
!= NULL
);
1454 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1456 addr
= (struct sockaddr_in
*)address
->m_addr
;
1457 return ntohs(addr
->sin_port
);
1462 * -------------------------------------------------------------------------
1463 * Internet IPv6 address family
1464 * -------------------------------------------------------------------------
1467 wxSocketError
_GAddress_Init_INET6(GAddress
*address
)
1469 struct in6_addr any_address
= IN6ADDR_ANY_INIT
;
1470 address
->m_len
= sizeof(struct sockaddr_in6
);
1471 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1472 if (address
->m_addr
== NULL
)
1474 address
->m_error
= wxSOCKET_MEMERR
;
1475 return wxSOCKET_MEMERR
;
1477 memset(address
->m_addr
,0,address
->m_len
);
1479 address
->m_family
= wxSOCKET_INET6
;
1480 address
->m_realfamily
= AF_INET6
;
1481 ((struct sockaddr_in6
*)address
->m_addr
)->sin6_family
= AF_INET6
;
1482 ((struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
= any_address
;
1484 return wxSOCKET_NOERROR
;
1487 wxSocketError
GAddress_INET6_SetHostName(GAddress
*address
, const char *hostname
)
1489 assert(address
!= NULL
);
1490 CHECK_ADDRESS(address
, INET6
);
1493 memset( & hints
, 0, sizeof( hints
) );
1494 hints
.ai_family
= AF_INET6
;
1495 addrinfo
* info
= 0;
1496 if ( getaddrinfo( hostname
, "0", & hints
, & info
) || ! info
)
1498 address
->m_error
= wxSOCKET_NOHOST
;
1499 return wxSOCKET_NOHOST
;
1502 memcpy( address
->m_addr
, info
->ai_addr
, info
->ai_addrlen
);
1503 freeaddrinfo( info
);
1504 return wxSOCKET_NOERROR
;
1507 wxSocketError
GAddress_INET6_SetAnyAddress(GAddress
*address
)
1509 assert(address
!= NULL
);
1511 CHECK_ADDRESS(address
, INET6
);
1513 struct in6_addr addr
;
1514 memset( & addr
, 0, sizeof( addr
) );
1515 return GAddress_INET6_SetHostAddress(address
, addr
);
1517 wxSocketError
GAddress_INET6_SetHostAddress(GAddress
*address
,
1518 struct in6_addr hostaddr
)
1520 assert(address
!= NULL
);
1522 CHECK_ADDRESS(address
, INET6
);
1524 ((struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
= hostaddr
;
1526 return wxSOCKET_NOERROR
;
1529 wxSocketError
GAddress_INET6_SetPortName(GAddress
*address
, const char *port
,
1530 const char *protocol
)
1533 struct sockaddr_in6
*addr
;
1535 assert(address
!= NULL
);
1536 CHECK_ADDRESS(address
, INET6
);
1540 address
->m_error
= wxSOCKET_INVPORT
;
1541 return wxSOCKET_INVPORT
;
1544 se
= getservbyname(port
, protocol
);
1547 if (isdigit(port
[0]))
1551 port_int
= atoi(port
);
1552 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1553 addr
->sin6_port
= htons((u_short
) port_int
);
1554 return wxSOCKET_NOERROR
;
1557 address
->m_error
= wxSOCKET_INVPORT
;
1558 return wxSOCKET_INVPORT
;
1561 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1562 addr
->sin6_port
= se
->s_port
;
1564 return wxSOCKET_NOERROR
;
1567 wxSocketError
GAddress_INET6_SetPort(GAddress
*address
, unsigned short port
)
1569 struct sockaddr_in6
*addr
;
1571 assert(address
!= NULL
);
1572 CHECK_ADDRESS(address
, INET6
);
1574 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1575 addr
->sin6_port
= htons(port
);
1577 return wxSOCKET_NOERROR
;
1580 wxSocketError
GAddress_INET6_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1584 struct sockaddr_in6
*addr
;
1586 assert(address
!= NULL
);
1587 CHECK_ADDRESS(address
, INET6
);
1589 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1590 addr_buf
= (char *)&(addr
->sin6_addr
);
1592 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin6_addr
), AF_INET6
);
1595 address
->m_error
= wxSOCKET_NOHOST
;
1596 return wxSOCKET_NOHOST
;
1599 strncpy(hostname
, he
->h_name
, sbuf
);
1601 return wxSOCKET_NOERROR
;
1604 wxSocketError
GAddress_INET6_GetHostAddress(GAddress
*address
,struct in6_addr
*hostaddr
)
1606 assert(address
!= NULL
);
1607 assert(hostaddr
!= NULL
);
1608 CHECK_ADDRESS_RETVAL(address
, INET6
, wxSOCKET_INVADDR
);
1609 *hostaddr
= ( (struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
;
1610 return wxSOCKET_NOERROR
;
1613 unsigned short GAddress_INET6_GetPort(GAddress
*address
)
1615 assert(address
!= NULL
);
1616 CHECK_ADDRESS_RETVAL(address
, INET6
, 0);
1618 return ntohs( ((struct sockaddr_in6
*)address
->m_addr
)->sin6_port
);
1621 #endif // wxUSE_IPV6
1624 * -------------------------------------------------------------------------
1625 * Unix address family
1626 * -------------------------------------------------------------------------
1629 #ifndef __VISAGECPP__
1630 wxSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1632 address
->m_len
= sizeof(struct sockaddr_un
);
1633 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
1634 if (address
->m_addr
== NULL
)
1636 address
->m_error
= wxSOCKET_MEMERR
;
1637 return wxSOCKET_MEMERR
;
1640 address
->m_family
= wxSOCKET_UNIX
;
1641 address
->m_realfamily
= PF_UNIX
;
1642 ((struct sockaddr_un
*)address
->m_addr
)->sun_family
= AF_UNIX
;
1643 ((struct sockaddr_un
*)address
->m_addr
)->sun_path
[0] = 0;
1645 return wxSOCKET_NOERROR
;
1648 #define UNIX_SOCK_PATHLEN (sizeof(addr->sun_path)/sizeof(addr->sun_path[0]))
1650 wxSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1652 struct sockaddr_un
*addr
;
1654 assert(address
!= NULL
);
1656 CHECK_ADDRESS(address
, UNIX
);
1658 addr
= ((struct sockaddr_un
*)address
->m_addr
);
1659 strncpy(addr
->sun_path
, path
, UNIX_SOCK_PATHLEN
);
1660 addr
->sun_path
[UNIX_SOCK_PATHLEN
- 1] = '\0';
1662 return wxSOCKET_NOERROR
;
1665 wxSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1667 struct sockaddr_un
*addr
;
1669 assert(address
!= NULL
);
1670 CHECK_ADDRESS(address
, UNIX
);
1672 addr
= (struct sockaddr_un
*)address
->m_addr
;
1674 strncpy(path
, addr
->sun_path
, sbuf
);
1676 return wxSOCKET_NOERROR
;
1678 #endif /* !defined(__VISAGECPP__) */
1679 #endif /* wxUSE_SOCKETS */