]>
git.saurik.com Git - wxWidgets.git/blob - src/unix/gsocket.cpp
2bdfa8d43b1583f02151ef640dfa1385d12cda2f
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/unix/gsocket.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/gsocket.h"
20 #include "wx/private/fd.h"
21 #include "wx/private/socket.h"
22 #include "wx/private/gsocketiohandler.h"
24 #if defined(__VISAGECPP__)
25 #define BSD_SELECT /* use Berkeley Sockets select */
28 #if defined(__WATCOMC__)
34 #include <sys/types.h>
39 #include <netinet/in.h>
42 #include <sys/ioctl.h>
44 #ifdef HAVE_SYS_SELECT_H
45 # include <sys/select.h>
52 u_char sun_len
; /* sockaddr len including null */
53 u_char sun_family
; /* AF_UNIX */
54 char sun_path
[108]; /* path name (gag) */
57 #include <sys/socket.h>
63 #include <netinet/in.h>
64 #include <arpa/inet.h>
71 #include <machine/endian.h>
77 #define EBADF SOCEBADF
83 #include <sys/socket.h>
84 #include <sys/ioctl.h>
85 #include <sys/select.h>
87 #define close(a) soclose(a)
88 #define select(a,b,c,d,e) bsdselect(a,b,c,d,e)
89 int _System
bsdselect(int,
94 int _System
soclose(int);
98 #include <sys/select.h>
106 # include <sys/filio.h>
109 # include <bstring.h>
112 # include <strings.h>
119 # define WX_SOCKLEN_T unsigned int
123 # define WX_SOCKLEN_T socklen_t
125 # elif defined(__WXMAC__)
126 # define WX_SOCKLEN_T socklen_t
128 # define WX_SOCKLEN_T int
132 #endif /* SOCKLEN_T */
135 #define SOCKOPTLEN_T WX_SOCKLEN_T
138 /* UnixWare reportedly needs this for FIONBIO definition */
140 #include <sys/filio.h>
144 * INADDR_BROADCAST is identical to INADDR_NONE which is not defined
145 * on all systems. INADDR_BROADCAST should be fine to indicate an error.
148 #define INADDR_NONE INADDR_BROADCAST
151 #if defined(__VISAGECPP__) || defined(__WATCOMC__)
153 #define MASK_SIGNAL() {
154 #define UNMASK_SIGNAL() }
157 extern "C" { typedef void (*wxSigHandler
)(int); }
159 #define MASK_SIGNAL() \
161 wxSigHandler old_handler = signal(SIGPIPE, SIG_IGN);
163 #define UNMASK_SIGNAL() \
164 signal(SIGPIPE, old_handler); \
169 /* If a SIGPIPE is issued by a socket call on a remotely closed socket,
170 the program will "crash" unless it explicitly handles the SIGPIPE.
171 By using MSG_NOSIGNAL, the SIGPIPE is suppressed. Later, we will
172 use SO_NOSIGPIPE (if available), the BSD equivalent. */
174 # define GSOCKET_MSG_NOSIGNAL MSG_NOSIGNAL
175 #else /* MSG_NOSIGNAL not available (FreeBSD including OS X) */
176 # define GSOCKET_MSG_NOSIGNAL 0
177 #endif /* MSG_NOSIGNAL */
179 #if wxUSE_THREADS && (defined(HAVE_GETHOSTBYNAME) || defined(HAVE_GETSERVBYNAME))
180 # include "wx/thread.h"
183 #if defined(HAVE_GETHOSTBYNAME)
184 static struct hostent
* deepCopyHostent(struct hostent
*h
,
185 const struct hostent
*he
,
186 char *buffer
, int size
, int *err
)
188 /* copy old structure */
189 memcpy(h
, he
, sizeof(struct hostent
));
192 int len
= strlen(h
->h_name
);
198 memcpy(buffer
, h
->h_name
, len
);
202 /* track position in the buffer */
205 /* reuse len to store address length */
208 /* ensure pointer alignment */
209 unsigned int misalign
= sizeof(char *) - pos%sizeof
(char *);
210 if(misalign
< sizeof(char *))
213 /* leave space for pointer list */
214 char **p
= h
->h_addr_list
, **q
;
215 char **h_addr_list
= (char **)(buffer
+ pos
);
217 pos
+= sizeof(char *);
219 /* copy addresses and fill new pointer list */
220 for (p
= h
->h_addr_list
, q
= h_addr_list
; *p
!= 0; p
++, q
++)
222 if (size
< pos
+ len
)
227 memcpy(buffer
+ pos
, *p
, len
); /* copy content */
228 *q
= buffer
+ pos
; /* set copied pointer to copied content */
231 *++q
= 0; /* null terminate the pointer list */
232 h
->h_addr_list
= h_addr_list
; /* copy pointer to pointers */
234 /* ensure word alignment of pointers */
235 misalign
= sizeof(char *) - pos%sizeof
(char *);
236 if(misalign
< sizeof(char *))
239 /* leave space for pointer list */
241 char **h_aliases
= (char **)(buffer
+ pos
);
243 pos
+= sizeof(char *);
245 /* copy aliases and fill new pointer list */
246 for (p
= h
->h_aliases
, q
= h_aliases
; *p
!= 0; p
++, q
++)
249 if (size
<= pos
+ len
)
254 memcpy(buffer
+ pos
, *p
, len
); /* copy content */
255 buffer
[pos
+ len
] = '\0';
256 *q
= buffer
+ pos
; /* set copied pointer to copied content */
259 *++q
= 0; /* null terminate the pointer list */
260 h
->h_aliases
= h_aliases
; /* copy pointer to pointers */
266 #if defined(HAVE_GETHOSTBYNAME) && wxUSE_THREADS
267 static wxMutex nameLock
;
269 struct hostent
* wxGethostbyname_r(const char *hostname
, struct hostent
*h
,
270 void *buffer
, int size
, int *err
)
273 struct hostent
*he
= NULL
;
275 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_6)
276 if (gethostbyname_r(hostname
, h
, (char*)buffer
, size
, &he
, err
))
278 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_5)
279 he
= gethostbyname_r(hostname
, h
, (char*)buffer
, size
, err
);
280 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
281 if (gethostbyname_r(hostname
, h
, (struct hostent_data
*) buffer
))
288 #elif defined(HAVE_GETHOSTBYNAME)
290 wxMutexLocker
locker(nameLock
);
292 he
= gethostbyname(hostname
);
296 he
= deepCopyHostent(h
, he
, (char*)buffer
, size
, err
);
301 #if defined(HAVE_GETHOSTBYNAME) && wxUSE_THREADS
302 static wxMutex addrLock
;
304 struct hostent
* wxGethostbyaddr_r(const char *addr_buf
, int buf_size
,
305 int proto
, struct hostent
*h
,
306 void *buffer
, int size
, int *err
)
308 struct hostent
*he
= NULL
;
310 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_6)
311 if (gethostbyaddr_r(addr_buf
, buf_size
, proto
, h
,
312 (char*)buffer
, size
, &he
, err
))
314 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_5)
315 he
= gethostbyaddr_r(addr_buf
, buf_size
, proto
, h
, (char*)buffer
, size
, err
);
316 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
317 if (gethostbyaddr_r(addr_buf
, buf_size
, proto
, h
,
318 (struct hostent_data
*) buffer
))
325 #elif defined(HAVE_GETHOSTBYNAME)
327 wxMutexLocker
locker(addrLock
);
329 he
= gethostbyaddr(addr_buf
, buf_size
, proto
);
333 he
= deepCopyHostent(h
, he
, (char*)buffer
, size
, err
);
338 #if defined(HAVE_GETSERVBYNAME)
339 static struct servent
* deepCopyServent(struct servent
*s
,
340 const struct servent
*se
,
341 char *buffer
, int size
)
343 /* copy plain old structure */
344 memcpy(s
, se
, sizeof(struct servent
));
347 int len
= strlen(s
->s_name
);
352 memcpy(buffer
, s
->s_name
, len
);
356 /* track position in the buffer */
360 len
= strlen(s
->s_proto
);
361 if (pos
+ len
>= size
)
365 memcpy(buffer
+ pos
, s
->s_proto
, len
);
366 buffer
[pos
+ len
] = '\0';
367 s
->s_proto
= buffer
+ pos
;
369 /* track position in the buffer */
372 /* ensure pointer alignment */
373 unsigned int misalign
= sizeof(char *) - pos%sizeof
(char *);
374 if(misalign
< sizeof(char *))
377 /* leave space for pointer list */
378 char **p
= s
->s_aliases
, **q
;
379 char **s_aliases
= (char **)(buffer
+ pos
);
381 pos
+= sizeof(char *);
383 /* copy addresses and fill new pointer list */
384 for (p
= s
->s_aliases
, q
= s_aliases
; *p
!= 0; p
++, q
++){
386 if (size
<= pos
+ len
)
390 memcpy(buffer
+ pos
, *p
, len
); /* copy content */
391 buffer
[pos
+ len
] = '\0';
392 *q
= buffer
+ pos
; /* set copied pointer to copied content */
395 *++q
= 0; /* null terminate the pointer list */
396 s
->s_aliases
= s_aliases
; /* copy pointer to pointers */
401 #if defined(HAVE_GETSERVBYNAME) && wxUSE_THREADS
402 static wxMutex servLock
;
404 struct servent
*wxGetservbyname_r(const char *port
, const char *protocol
,
405 struct servent
*serv
, void *buffer
, int size
)
407 struct servent
*se
= NULL
;
408 #if defined(HAVE_FUNC_GETSERVBYNAME_R_6)
409 if (getservbyname_r(port
, protocol
, serv
, (char*)buffer
, size
, &se
))
411 #elif defined(HAVE_FUNC_GETSERVBYNAME_R_5)
412 se
= getservbyname_r(port
, protocol
, serv
, (char*)buffer
, size
);
413 #elif defined(HAVE_FUNC_GETSERVBYNAME_R_4)
414 if (getservbyname_r(port
, protocol
, serv
, (struct servent_data
*) buffer
))
418 #elif defined(HAVE_GETSERVBYNAME)
420 wxMutexLocker
locker(servLock
);
422 se
= getservbyname(port
, protocol
);
424 se
= deepCopyServent(serv
, se
, (char*)buffer
, size
);
429 /* debugging helpers */
430 #ifdef __GSOCKET_DEBUG__
431 # define SOCKET_DEBUG(args) printf args
433 # define SOCKET_DEBUG(args)
434 #endif /* __GSOCKET_DEBUG__ */
436 /* Constructors / Destructors for wxSocketImplUnix */
438 wxSocketImplUnix::wxSocketImplUnix(wxSocketBase
& wxsocket
)
439 : wxSocketImpl(wxsocket
)
444 m_use_events
= false;
448 * Disallow further read/write operations on this socket, close
449 * the fd and disable all callbacks.
451 void wxSocketImplUnix::Shutdown()
453 /* Don't allow events to fire after socket has been closed */
456 wxSocketImpl::Shutdown();
460 * Waits for an incoming client connection. Returns a pointer to
461 * a wxSocketImplUnix object, or NULL if there was an error, in which case
462 * the last error field will be updated for the calling wxSocketImplUnix.
464 * Error codes (set in the calling wxSocketImplUnix)
465 * wxSOCKET_INVSOCK - the socket is not valid or not a server.
466 * wxSOCKET_TIMEDOUT - timeout, no incoming connections.
467 * wxSOCKET_WOULDBLOCK - the call would block and the socket is nonblocking.
468 * wxSOCKET_MEMERR - couldn't allocate memory.
469 * wxSOCKET_IOERR - low-level error.
471 wxSocketImpl
*wxSocketImplUnix::WaitConnection(wxSocketBase
& wxsocket
)
474 WX_SOCKLEN_T fromlen
= sizeof(from
);
475 wxSocketImpl
*connection
;
479 /* If the socket has already been created, we exit immediately */
480 if (m_fd
== INVALID_SOCKET
|| !m_server
)
482 m_error
= wxSOCKET_INVSOCK
;
486 /* Create a wxSocketImplUnix object for the new connection */
487 connection
= wxSocketImplUnix::Create(wxsocket
);
491 m_error
= wxSOCKET_MEMERR
;
495 /* Wait for a connection (with timeout) */
496 if (Input_Timeout() == wxSOCKET_TIMEDOUT
)
499 /* m_error set by Input_Timeout */
503 connection
->m_fd
= accept(m_fd
, (sockaddr
*)&from
, (WX_SOCKLEN_T
*) &fromlen
);
505 /* Reenable CONNECTION events */
506 EnableEvent(wxSOCKET_CONNECTION
);
508 if (connection
->m_fd
== INVALID_SOCKET
)
510 if (errno
== EWOULDBLOCK
)
511 m_error
= wxSOCKET_WOULDBLOCK
;
513 m_error
= wxSOCKET_IOERR
;
519 /* Initialize all fields */
520 connection
->m_server
= false;
521 connection
->m_stream
= true;
523 /* Setup the peer address field */
524 connection
->m_peer
= GAddress_new();
525 if (!connection
->m_peer
)
528 m_error
= wxSOCKET_MEMERR
;
532 err
= _GAddress_translate_from(connection
->m_peer
, (sockaddr
*)&from
, fromlen
);
533 if (err
!= wxSOCKET_NOERROR
)
540 #if defined(__EMX__) || defined(__VISAGECPP__)
541 ioctl(connection
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
543 ioctl(connection
->m_fd
, FIONBIO
, &arg
);
546 connection
->Notify(true);
551 void wxSocketImplUnix::Notify(bool flag
)
553 if (flag
== m_use_events
)
556 DoEnableEvents(flag
);
559 void wxSocketImplUnix::DoEnableEvents(bool flag
)
561 wxSocketManager
* const manager
= wxSocketManager::Get();
564 manager
->Install_Callback(this, wxSOCKET_INPUT
);
565 manager
->Install_Callback(this, wxSOCKET_OUTPUT
);
569 manager
->Uninstall_Callback(this, wxSOCKET_INPUT
);
570 manager
->Uninstall_Callback(this, wxSOCKET_OUTPUT
);
574 wxSocketError
wxSocketImplUnix::DoHandleConnect(int ret
)
576 /* We only call EnableEvents() if we know we aren't shutting down the socket.
577 * NB: EnableEvents() needs to be called whether the socket is blocking or
578 * non-blocking, it just shouldn't be called prior to knowing there is a
579 * connection _if_ blocking sockets are being used.
580 * If connect above returns 0, we are already connected and need to make the
581 * call to EnableEvents() now.
583 if ( m_non_blocking
|| (ret
== 0) )
588 const int err
= errno
;
590 /* If connect failed with EINPROGRESS and the wxSocketImplUnix object
591 * is in blocking mode, we select() for the specified timeout
592 * checking for writability to see if the connection request
595 if ((err
== EINPROGRESS
) && (!m_non_blocking
))
597 if (Output_Timeout() == wxSOCKET_TIMEDOUT
)
600 /* m_error is set in Output_Timeout */
601 return wxSOCKET_TIMEDOUT
;
606 SOCKOPTLEN_T len
= sizeof(error
);
608 getsockopt(m_fd
, SOL_SOCKET
, SO_ERROR
, (char*) &error
, &len
);
612 return wxSOCKET_NOERROR
;
616 /* If connect failed with EINPROGRESS and the wxSocketImplUnix object
617 * is set to nonblocking, we set m_error to wxSOCKET_WOULDBLOCK
618 * (and return wxSOCKET_WOULDBLOCK) but we don't close the socket;
619 * this way if the connection completes, a wxSOCKET_CONNECTION
620 * event will be generated, if enabled.
622 if ((err
== EINPROGRESS
) && (m_non_blocking
))
624 m_establishing
= true;
625 m_error
= wxSOCKET_WOULDBLOCK
;
626 return wxSOCKET_WOULDBLOCK
;
629 /* If connect failed with an error other than EINPROGRESS,
630 * then the call to Connect has failed.
633 m_error
= wxSOCKET_IOERR
;
635 return wxSOCKET_IOERR
;
638 return wxSOCKET_NOERROR
;
643 /* Like recv(), send(), ... */
644 int wxSocketImplUnix::Read(char *buffer
, int size
)
648 if (m_fd
== INVALID_SOCKET
|| m_server
)
650 m_error
= wxSOCKET_INVSOCK
;
654 /* Disable events during query of socket status */
655 DisableEvent(wxSOCKET_INPUT
);
657 /* If the socket is blocking, wait for data (with a timeout) */
658 if (Input_Timeout() == wxSOCKET_TIMEDOUT
) {
659 m_error
= wxSOCKET_TIMEDOUT
;
660 /* Don't return here immediately, otherwise socket events would not be
668 ret
= Recv_Stream(buffer
, size
);
670 ret
= Recv_Dgram(buffer
, size
);
673 * If recv returned zero for a TCP socket (if m_stream == NULL, it's an UDP
674 * socket and empty datagrams are possible), then the connection has been
677 * Otherwise, recv has returned an error (-1), in which case we have lost
678 * the socket only if errno does _not_ indicate that there may be more data
681 if ((ret
== 0) && m_stream
)
683 /* Make sure wxSOCKET_LOST event gets sent and shut down the socket */
686 m_detected
= wxSOCKET_LOST_FLAG
;
693 if ((errno
== EWOULDBLOCK
) || (errno
== EAGAIN
))
694 m_error
= wxSOCKET_WOULDBLOCK
;
696 m_error
= wxSOCKET_IOERR
;
700 /* Enable events again now that we are done processing */
701 EnableEvent(wxSOCKET_INPUT
);
706 int wxSocketImplUnix::Write(const char *buffer
, int size
)
710 SOCKET_DEBUG(( "Write #1, size %d\n", size
));
712 if (m_fd
== INVALID_SOCKET
|| m_server
)
714 m_error
= wxSOCKET_INVSOCK
;
718 SOCKET_DEBUG(( "Write #2, size %d\n", size
));
720 /* If the socket is blocking, wait for writability (with a timeout) */
721 if (Output_Timeout() == wxSOCKET_TIMEDOUT
)
724 SOCKET_DEBUG(( "Write #3, size %d\n", size
));
728 ret
= Send_Stream(buffer
, size
);
730 ret
= Send_Dgram(buffer
, size
);
732 SOCKET_DEBUG(( "Write #4, size %d\n", size
));
736 if ((errno
== EWOULDBLOCK
) || (errno
== EAGAIN
))
738 m_error
= wxSOCKET_WOULDBLOCK
;
739 SOCKET_DEBUG(( "Write error WOULDBLOCK\n" ));
743 m_error
= wxSOCKET_IOERR
;
744 SOCKET_DEBUG(( "Write error IOERR\n" ));
747 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
748 * in MSW). Once the first OUTPUT event is received, users can assume
749 * that the socket is writable until a read operation fails. Only then
750 * will further OUTPUT events be posted.
752 EnableEvent(wxSOCKET_OUTPUT
);
757 SOCKET_DEBUG(( "Write #5, size %d ret %d\n", size
, ret
));
764 void wxSocketImplUnix::EnableEvent(wxSocketNotify event
)
768 m_detected
&= ~(1 << event
);
769 wxSocketManager::Get()->Install_Callback(this, event
);
773 void wxSocketImplUnix::DisableEvent(wxSocketNotify event
)
777 m_detected
|= (1 << event
);
778 wxSocketManager::Get()->Uninstall_Callback(this, event
);
783 * For blocking sockets, wait until data is available or
784 * until timeout ellapses.
786 wxSocketError
wxSocketImplUnix::Input_Timeout()
791 // Linux select() will overwrite the struct on return so make a copy
792 struct timeval tv
= m_timeout
;
797 wxFD_SET(m_fd
, &readfds
);
798 ret
= select(m_fd
+ 1, &readfds
, NULL
, NULL
, &tv
);
801 SOCKET_DEBUG(( "Input_Timeout, select returned 0\n" ));
802 m_error
= wxSOCKET_TIMEDOUT
;
803 return wxSOCKET_TIMEDOUT
;
808 SOCKET_DEBUG(( "Input_Timeout, select returned -1\n" ));
809 if (errno
== EBADF
) { SOCKET_DEBUG(( "Invalid file descriptor\n" )); }
810 if (errno
== EINTR
) { SOCKET_DEBUG(( "A non blocked signal was caught\n" )); }
811 if (errno
== EINVAL
) { SOCKET_DEBUG(( "The highest number descriptor is negative\n" )); }
812 if (errno
== ENOMEM
) { SOCKET_DEBUG(( "Not enough memory\n" )); }
813 m_error
= wxSOCKET_TIMEDOUT
;
814 return wxSOCKET_TIMEDOUT
;
818 return wxSOCKET_NOERROR
;
822 * For blocking sockets, wait until data can be sent without
823 * blocking or until timeout ellapses.
825 wxSocketError
wxSocketImplUnix::Output_Timeout()
830 // Linux select() will overwrite the struct on return so make a copy
831 struct timeval tv
= m_timeout
;
833 SOCKET_DEBUG( ("m_non_blocking has: %d\n", (int)m_non_blocking
) );
837 wxFD_ZERO(&writefds
);
838 wxFD_SET(m_fd
, &writefds
);
839 ret
= select(m_fd
+ 1, NULL
, &writefds
, NULL
, &tv
);
842 SOCKET_DEBUG(( "Output_Timeout, select returned 0\n" ));
843 m_error
= wxSOCKET_TIMEDOUT
;
844 return wxSOCKET_TIMEDOUT
;
849 SOCKET_DEBUG(( "Output_Timeout, select returned -1\n" ));
850 if (errno
== EBADF
) { SOCKET_DEBUG(( "Invalid file descriptor\n" )); }
851 if (errno
== EINTR
) { SOCKET_DEBUG(( "A non blocked signal was caught\n" )); }
852 if (errno
== EINVAL
) { SOCKET_DEBUG(( "The highest number descriptor is negative\n" )); }
853 if (errno
== ENOMEM
) { SOCKET_DEBUG(( "Not enough memory\n" )); }
854 m_error
= wxSOCKET_TIMEDOUT
;
855 return wxSOCKET_TIMEDOUT
;
858 if ( ! wxFD_ISSET(m_fd
, &writefds
) )
860 SOCKET_DEBUG(( "Output_Timeout is buggy!\n" ));
864 SOCKET_DEBUG(( "Output_Timeout seems correct\n" ));
869 SOCKET_DEBUG(( "Output_Timeout, didn't try select!\n" ));
872 return wxSOCKET_NOERROR
;
875 int wxSocketImplUnix::Recv_Stream(char *buffer
, int size
)
880 ret
= recv(m_fd
, buffer
, size
, GSOCKET_MSG_NOSIGNAL
);
882 while (ret
== -1 && errno
== EINTR
); /* Loop until not interrupted */
887 int wxSocketImplUnix::Recv_Dgram(char *buffer
, int size
)
890 WX_SOCKLEN_T fromlen
= sizeof(from
);
894 fromlen
= sizeof(from
);
898 ret
= recvfrom(m_fd
, buffer
, size
, 0, (sockaddr
*)&from
, (WX_SOCKLEN_T
*) &fromlen
);
900 while (ret
== -1 && errno
== EINTR
); /* Loop until not interrupted */
905 /* Translate a system address into a wxSocketImplUnix address */
908 m_peer
= GAddress_new();
911 m_error
= wxSOCKET_MEMERR
;
916 err
= _GAddress_translate_from(m_peer
, (sockaddr
*)&from
, fromlen
);
917 if (err
!= wxSOCKET_NOERROR
)
919 GAddress_destroy(m_peer
);
928 int wxSocketImplUnix::Send_Stream(const char *buffer
, int size
)
936 ret
= send(m_fd
, (char *)buffer
, size
, GSOCKET_MSG_NOSIGNAL
);
938 while (ret
== -1 && errno
== EINTR
); /* Loop until not interrupted */
945 int wxSocketImplUnix::Send_Dgram(const char *buffer
, int size
)
947 struct sockaddr
*addr
;
953 m_error
= wxSOCKET_INVADDR
;
957 err
= _GAddress_translate_to(m_peer
, &addr
, &len
);
958 if (err
!= wxSOCKET_NOERROR
)
968 ret
= sendto(m_fd
, (char *)buffer
, size
, 0, addr
, len
);
970 while (ret
== -1 && errno
== EINTR
); /* Loop until not interrupted */
974 /* Frees memory allocated from _GAddress_translate_to */
980 void wxSocketImplUnix::OnStateChange(wxSocketNotify event
)
983 NotifyOnStateChange(event
);
985 if ( event
== wxSOCKET_LOST
)
989 void wxSocketImplUnix::Detected_Read()
993 /* Safeguard against straggling call to Detected_Read */
994 if (m_fd
== INVALID_SOCKET
)
999 /* If we have already detected a LOST event, then don't try
1000 * to do any further processing.
1002 if ((m_detected
& wxSOCKET_LOST_FLAG
) != 0)
1004 m_establishing
= false;
1006 OnStateChange(wxSOCKET_LOST
);
1010 int num
= recv(m_fd
, &c
, 1, MSG_PEEK
| GSOCKET_MSG_NOSIGNAL
);
1014 OnStateChange(wxSOCKET_INPUT
);
1018 if (m_server
&& m_stream
)
1020 OnStateChange(wxSOCKET_CONNECTION
);
1026 /* graceful shutdown */
1027 OnStateChange(wxSOCKET_LOST
);
1031 /* Empty datagram received */
1032 OnStateChange(wxSOCKET_INPUT
);
1037 /* Do not throw a lost event in cases where the socket isn't really lost */
1038 if ((errno
== EWOULDBLOCK
) || (errno
== EAGAIN
) || (errno
== EINTR
))
1040 OnStateChange(wxSOCKET_INPUT
);
1044 OnStateChange(wxSOCKET_LOST
);
1050 void wxSocketImplUnix::Detected_Write()
1052 /* If we have already detected a LOST event, then don't try
1053 * to do any further processing.
1055 if ((m_detected
& wxSOCKET_LOST_FLAG
) != 0)
1057 m_establishing
= false;
1059 OnStateChange(wxSOCKET_LOST
);
1063 if (m_establishing
&& !m_server
)
1066 SOCKOPTLEN_T len
= sizeof(error
);
1068 m_establishing
= false;
1070 getsockopt(m_fd
, SOL_SOCKET
, SO_ERROR
, (char*)&error
, &len
);
1074 OnStateChange(wxSOCKET_LOST
);
1078 OnStateChange(wxSOCKET_CONNECTION
);
1079 /* We have to fire this event by hand because CONNECTION (for clients)
1080 * and OUTPUT are internally the same and we just disabled CONNECTION
1081 * events with the above macro.
1083 OnStateChange(wxSOCKET_OUTPUT
);
1088 OnStateChange(wxSOCKET_OUTPUT
);
1093 * -------------------------------------------------------------------------
1095 * -------------------------------------------------------------------------
1098 /* CHECK_ADDRESS verifies that the current address family is either
1099 * wxSOCKET_NOFAMILY or wxSOCKET_*family*, and if it is wxSOCKET_NOFAMILY, it
1100 * initalizes it to be a wxSOCKET_*family*. In other cases, it returns
1101 * an appropiate error code.
1103 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
1105 #define CHECK_ADDRESS(address, family) \
1107 if (address->m_family == wxSOCKET_NOFAMILY) \
1108 if (_GAddress_Init_##family(address) != wxSOCKET_NOERROR) \
1109 return address->m_error; \
1110 if (address->m_family != wxSOCKET_##family) \
1112 address->m_error = wxSOCKET_INVADDR; \
1113 return wxSOCKET_INVADDR; \
1117 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
1119 if (address->m_family == wxSOCKET_NOFAMILY) \
1120 if (_GAddress_Init_##family(address) != wxSOCKET_NOERROR) \
1122 if (address->m_family != wxSOCKET_##family) \
1124 address->m_error = wxSOCKET_INVADDR; \
1130 GAddress
*GAddress_new(void)
1134 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1137 address
->m_family
= wxSOCKET_NOFAMILY
;
1138 address
->m_addr
= NULL
;
1144 GAddress
*GAddress_copy(GAddress
*address
)
1148 assert(address
!= NULL
);
1150 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1153 memcpy(addr2
, address
, sizeof(GAddress
));
1155 if (address
->m_addr
&& address
->m_len
> 0)
1157 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
1158 if (addr2
->m_addr
== NULL
)
1163 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1169 void GAddress_destroy(GAddress
*address
)
1171 assert(address
!= NULL
);
1173 if (address
->m_addr
)
1174 free(address
->m_addr
);
1179 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1181 assert(address
!= NULL
);
1183 address
->m_family
= type
;
1186 GAddressType
GAddress_GetFamily(GAddress
*address
)
1188 assert(address
!= NULL
);
1190 return address
->m_family
;
1193 wxSocketError
_GAddress_translate_from(GAddress
*address
,
1194 struct sockaddr
*addr
, int len
)
1196 address
->m_realfamily
= addr
->sa_family
;
1197 switch (addr
->sa_family
)
1200 address
->m_family
= wxSOCKET_INET
;
1203 address
->m_family
= wxSOCKET_UNIX
;
1207 address
->m_family
= wxSOCKET_INET6
;
1209 #endif // wxUSE_IPV6
1212 address
->m_error
= wxSOCKET_INVOP
;
1213 return wxSOCKET_INVOP
;
1217 if (address
->m_addr
)
1218 free(address
->m_addr
);
1220 address
->m_len
= len
;
1221 address
->m_addr
= (struct sockaddr
*)malloc(len
);
1223 if (address
->m_addr
== NULL
)
1225 address
->m_error
= wxSOCKET_MEMERR
;
1226 return wxSOCKET_MEMERR
;
1229 memcpy(address
->m_addr
, addr
, len
);
1231 return wxSOCKET_NOERROR
;
1234 wxSocketError
_GAddress_translate_to(GAddress
*address
,
1235 struct sockaddr
**addr
, int *len
)
1237 if (!address
->m_addr
)
1239 address
->m_error
= wxSOCKET_INVADDR
;
1240 return wxSOCKET_INVADDR
;
1243 *len
= address
->m_len
;
1244 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
1247 address
->m_error
= wxSOCKET_MEMERR
;
1248 return wxSOCKET_MEMERR
;
1251 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1252 return wxSOCKET_NOERROR
;
1256 * -------------------------------------------------------------------------
1257 * Internet address family
1258 * -------------------------------------------------------------------------
1261 wxSocketError
_GAddress_Init_INET(GAddress
*address
)
1263 address
->m_len
= sizeof(struct sockaddr_in
);
1264 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1265 if (address
->m_addr
== NULL
)
1267 address
->m_error
= wxSOCKET_MEMERR
;
1268 return wxSOCKET_MEMERR
;
1271 address
->m_family
= wxSOCKET_INET
;
1272 address
->m_realfamily
= PF_INET
;
1273 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1274 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1276 return wxSOCKET_NOERROR
;
1279 wxSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1282 struct in_addr
*addr
;
1284 assert(address
!= NULL
);
1286 CHECK_ADDRESS(address
, INET
);
1288 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1290 /* If it is a numeric host name, convert it now */
1291 #if defined(HAVE_INET_ATON)
1292 if (inet_aton(hostname
, addr
) == 0)
1294 #elif defined(HAVE_INET_ADDR)
1295 if ( (addr
->s_addr
= inet_addr(hostname
)) == (unsigned)-1 )
1298 /* Use gethostbyname by default */
1300 int val
= 1; /* VA doesn't like constants in conditional expressions */
1305 struct in_addr
*array_addr
;
1307 /* It is a real name, we solve it */
1309 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
1310 struct hostent_data buffer
;
1315 he
= wxGethostbyname_r(hostname
, &h
, (void*)&buffer
, sizeof(buffer
), &err
);
1318 /* Reset to invalid address */
1319 addr
->s_addr
= INADDR_NONE
;
1320 address
->m_error
= wxSOCKET_NOHOST
;
1321 return wxSOCKET_NOHOST
;
1324 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1325 addr
->s_addr
= array_addr
[0].s_addr
;
1328 return wxSOCKET_NOERROR
;
1332 wxSocketError
GAddress_INET_SetBroadcastAddress(GAddress
*address
)
1334 return GAddress_INET_SetHostAddress(address
, INADDR_BROADCAST
);
1337 wxSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1339 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1342 wxSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1343 unsigned long hostaddr
)
1345 struct in_addr
*addr
;
1347 assert(address
!= NULL
);
1349 CHECK_ADDRESS(address
, INET
);
1351 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1352 addr
->s_addr
= htonl(hostaddr
);
1354 return wxSOCKET_NOERROR
;
1357 wxSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1358 const char *protocol
)
1361 struct sockaddr_in
*addr
;
1363 assert(address
!= NULL
);
1364 CHECK_ADDRESS(address
, INET
);
1368 address
->m_error
= wxSOCKET_INVPORT
;
1369 return wxSOCKET_INVPORT
;
1372 #if defined(HAVE_FUNC_GETSERVBYNAME_R_4)
1373 struct servent_data buffer
;
1377 struct servent serv
;
1378 se
= wxGetservbyname_r(port
, protocol
, &serv
,
1379 (void*)&buffer
, sizeof(buffer
));
1382 /* the cast to int suppresses compiler warnings about subscript having the
1384 if (isdigit((int)port
[0]))
1388 port_int
= atoi(port
);
1389 addr
= (struct sockaddr_in
*)address
->m_addr
;
1390 addr
->sin_port
= htons(port_int
);
1391 return wxSOCKET_NOERROR
;
1394 address
->m_error
= wxSOCKET_INVPORT
;
1395 return wxSOCKET_INVPORT
;
1398 addr
= (struct sockaddr_in
*)address
->m_addr
;
1399 addr
->sin_port
= se
->s_port
;
1401 return wxSOCKET_NOERROR
;
1404 wxSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1406 struct sockaddr_in
*addr
;
1408 assert(address
!= NULL
);
1409 CHECK_ADDRESS(address
, INET
);
1411 addr
= (struct sockaddr_in
*)address
->m_addr
;
1412 addr
->sin_port
= htons(port
);
1414 return wxSOCKET_NOERROR
;
1417 wxSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1421 struct sockaddr_in
*addr
;
1423 assert(address
!= NULL
);
1424 CHECK_ADDRESS(address
, INET
);
1426 addr
= (struct sockaddr_in
*)address
->m_addr
;
1427 addr_buf
= (char *)&(addr
->sin_addr
);
1429 struct hostent temphost
;
1430 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
1431 struct hostent_data buffer
;
1436 he
= wxGethostbyaddr_r(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
, &temphost
,
1437 (void*)&buffer
, sizeof(buffer
), &err
);
1440 address
->m_error
= wxSOCKET_NOHOST
;
1441 return wxSOCKET_NOHOST
;
1444 strncpy(hostname
, he
->h_name
, sbuf
);
1446 return wxSOCKET_NOERROR
;
1449 unsigned long GAddress_INET_GetHostAddress(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
;
1458 return ntohl(addr
->sin_addr
.s_addr
);
1461 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1463 struct sockaddr_in
*addr
;
1465 assert(address
!= NULL
);
1466 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1468 addr
= (struct sockaddr_in
*)address
->m_addr
;
1469 return ntohs(addr
->sin_port
);
1474 * -------------------------------------------------------------------------
1475 * Internet IPv6 address family
1476 * -------------------------------------------------------------------------
1479 wxSocketError
_GAddress_Init_INET6(GAddress
*address
)
1481 struct in6_addr any_address
= IN6ADDR_ANY_INIT
;
1482 address
->m_len
= sizeof(struct sockaddr_in6
);
1483 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1484 if (address
->m_addr
== NULL
)
1486 address
->m_error
= wxSOCKET_MEMERR
;
1487 return wxSOCKET_MEMERR
;
1489 memset(address
->m_addr
,0,address
->m_len
);
1491 address
->m_family
= wxSOCKET_INET6
;
1492 address
->m_realfamily
= AF_INET6
;
1493 ((struct sockaddr_in6
*)address
->m_addr
)->sin6_family
= AF_INET6
;
1494 ((struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
= any_address
;
1496 return wxSOCKET_NOERROR
;
1499 wxSocketError
GAddress_INET6_SetHostName(GAddress
*address
, const char *hostname
)
1501 assert(address
!= NULL
);
1502 CHECK_ADDRESS(address
, INET6
);
1505 memset( & hints
, 0, sizeof( hints
) );
1506 hints
.ai_family
= AF_INET6
;
1507 addrinfo
* info
= 0;
1508 if ( getaddrinfo( hostname
, "0", & hints
, & info
) || ! info
)
1510 address
->m_error
= wxSOCKET_NOHOST
;
1511 return wxSOCKET_NOHOST
;
1514 memcpy( address
->m_addr
, info
->ai_addr
, info
->ai_addrlen
);
1515 freeaddrinfo( info
);
1516 return wxSOCKET_NOERROR
;
1519 wxSocketError
GAddress_INET6_SetAnyAddress(GAddress
*address
)
1521 assert(address
!= NULL
);
1523 CHECK_ADDRESS(address
, INET6
);
1525 struct in6_addr addr
;
1526 memset( & addr
, 0, sizeof( addr
) );
1527 return GAddress_INET6_SetHostAddress(address
, addr
);
1529 wxSocketError
GAddress_INET6_SetHostAddress(GAddress
*address
,
1530 struct in6_addr hostaddr
)
1532 assert(address
!= NULL
);
1534 CHECK_ADDRESS(address
, INET6
);
1536 ((struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
= hostaddr
;
1538 return wxSOCKET_NOERROR
;
1541 wxSocketError
GAddress_INET6_SetPortName(GAddress
*address
, const char *port
,
1542 const char *protocol
)
1545 struct sockaddr_in6
*addr
;
1547 assert(address
!= NULL
);
1548 CHECK_ADDRESS(address
, INET6
);
1552 address
->m_error
= wxSOCKET_INVPORT
;
1553 return wxSOCKET_INVPORT
;
1556 se
= getservbyname(port
, protocol
);
1559 if (isdigit(port
[0]))
1563 port_int
= atoi(port
);
1564 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1565 addr
->sin6_port
= htons((u_short
) port_int
);
1566 return wxSOCKET_NOERROR
;
1569 address
->m_error
= wxSOCKET_INVPORT
;
1570 return wxSOCKET_INVPORT
;
1573 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1574 addr
->sin6_port
= se
->s_port
;
1576 return wxSOCKET_NOERROR
;
1579 wxSocketError
GAddress_INET6_SetPort(GAddress
*address
, unsigned short port
)
1581 struct sockaddr_in6
*addr
;
1583 assert(address
!= NULL
);
1584 CHECK_ADDRESS(address
, INET6
);
1586 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1587 addr
->sin6_port
= htons(port
);
1589 return wxSOCKET_NOERROR
;
1592 wxSocketError
GAddress_INET6_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1596 struct sockaddr_in6
*addr
;
1598 assert(address
!= NULL
);
1599 CHECK_ADDRESS(address
, INET6
);
1601 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1602 addr_buf
= (char *)&(addr
->sin6_addr
);
1604 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin6_addr
), AF_INET6
);
1607 address
->m_error
= wxSOCKET_NOHOST
;
1608 return wxSOCKET_NOHOST
;
1611 strncpy(hostname
, he
->h_name
, sbuf
);
1613 return wxSOCKET_NOERROR
;
1616 wxSocketError
GAddress_INET6_GetHostAddress(GAddress
*address
,struct in6_addr
*hostaddr
)
1618 assert(address
!= NULL
);
1619 assert(hostaddr
!= NULL
);
1620 CHECK_ADDRESS_RETVAL(address
, INET6
, wxSOCKET_INVADDR
);
1621 *hostaddr
= ( (struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
;
1622 return wxSOCKET_NOERROR
;
1625 unsigned short GAddress_INET6_GetPort(GAddress
*address
)
1627 assert(address
!= NULL
);
1628 CHECK_ADDRESS_RETVAL(address
, INET6
, 0);
1630 return ntohs( ((struct sockaddr_in6
*)address
->m_addr
)->sin6_port
);
1633 #endif // wxUSE_IPV6
1636 * -------------------------------------------------------------------------
1637 * Unix address family
1638 * -------------------------------------------------------------------------
1641 #ifndef __VISAGECPP__
1642 wxSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1644 address
->m_len
= sizeof(struct sockaddr_un
);
1645 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
1646 if (address
->m_addr
== NULL
)
1648 address
->m_error
= wxSOCKET_MEMERR
;
1649 return wxSOCKET_MEMERR
;
1652 address
->m_family
= wxSOCKET_UNIX
;
1653 address
->m_realfamily
= PF_UNIX
;
1654 ((struct sockaddr_un
*)address
->m_addr
)->sun_family
= AF_UNIX
;
1655 ((struct sockaddr_un
*)address
->m_addr
)->sun_path
[0] = 0;
1657 return wxSOCKET_NOERROR
;
1660 #define UNIX_SOCK_PATHLEN (sizeof(addr->sun_path)/sizeof(addr->sun_path[0]))
1662 wxSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1664 struct sockaddr_un
*addr
;
1666 assert(address
!= NULL
);
1668 CHECK_ADDRESS(address
, UNIX
);
1670 addr
= ((struct sockaddr_un
*)address
->m_addr
);
1671 strncpy(addr
->sun_path
, path
, UNIX_SOCK_PATHLEN
);
1672 addr
->sun_path
[UNIX_SOCK_PATHLEN
- 1] = '\0';
1674 return wxSOCKET_NOERROR
;
1677 wxSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1679 struct sockaddr_un
*addr
;
1681 assert(address
!= NULL
);
1682 CHECK_ADDRESS(address
, UNIX
);
1684 addr
= (struct sockaddr_un
*)address
->m_addr
;
1686 strncpy(path
, addr
->sun_path
, sbuf
);
1688 return wxSOCKET_NOERROR
;
1690 #endif /* !defined(__VISAGECPP__) */
1691 #endif /* wxUSE_SOCKETS */