map EAGAIN to wxSOCKET_WOULDBLOCK too as tit has this meaning for read() (even though...
[wxWidgets.git] / 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,
5 // Vadim Zeitlin
6 // Created: April 1997
7 // RCS-ID: $Id$
8 // Copyright: (c) 1997 Guilhem Lavaux
9 // (c) 2008 Vadim Zeitlin
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13
14 #include "wx/wxprec.h"
15
16 #if wxUSE_SOCKETS
17
18 #include "wx/private/fd.h"
19 #include "wx/private/socket.h"
20 #include "wx/unix/private/sockunix.h"
21
22 #if defined(__VISAGECPP__)
23 #define BSD_SELECT /* use Berkeley Sockets select */
24 #endif
25
26 #if defined(__WATCOMC__)
27 #include <errno.h>
28 #include <nerrno.h>
29 #endif
30
31 #include <assert.h>
32 #include <sys/types.h>
33 #ifdef __VISAGECPP__
34 #include <string.h>
35 #include <sys/time.h>
36 #include <types.h>
37 #include <netinet/in.h>
38 #endif
39 #include <netdb.h>
40 #include <sys/ioctl.h>
41
42 #ifdef HAVE_SYS_SELECT_H
43 # include <sys/select.h>
44 #endif
45
46 #ifdef __VMS__
47 #include <socket.h>
48 struct sockaddr_un
49 {
50 u_char sun_len; /* sockaddr len including null */
51 u_char sun_family; /* AF_UNIX */
52 char sun_path[108]; /* path name (gag) */
53 };
54 #else
55 #include <sys/socket.h>
56 #include <sys/un.h>
57 #endif
58
59 #ifndef __VISAGECPP__
60 #include <sys/time.h>
61 #include <netinet/in.h>
62 #include <arpa/inet.h>
63 #include <errno.h>
64 #include <string.h>
65 #include <unistd.h>
66 #else
67 #include <nerrno.h>
68 # if __IBMCPP__ < 400
69 #include <machine/endian.h>
70 #include <socket.h>
71 #include <ioctl.h>
72 #include <select.h>
73 #include <unistd.h>
74
75 #define EBADF SOCEBADF
76
77 # ifdef min
78 # undef min
79 # endif
80 # else
81 #include <sys/socket.h>
82 #include <sys/ioctl.h>
83 #include <sys/select.h>
84
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,
88 struct fd_set *,
89 struct fd_set *,
90 struct fd_set *,
91 struct timeval *);
92 int _System soclose(int);
93 # endif
94 #endif
95 #ifdef __EMX__
96 #include <sys/select.h>
97 #endif
98
99 #include <stdio.h>
100 #include <stdlib.h>
101 #include <stddef.h>
102 #include <ctype.h>
103 #ifdef sun
104 # include <sys/filio.h>
105 #endif
106 #ifdef sgi
107 # include <bstring.h>
108 #endif
109 #ifdef _AIX
110 # include <strings.h>
111 #endif
112 #include <signal.h>
113
114 #ifndef WX_SOCKLEN_T
115
116 #ifdef VMS
117 # define WX_SOCKLEN_T unsigned int
118 #else
119 # ifdef __GLIBC__
120 # if __GLIBC__ == 2
121 # define WX_SOCKLEN_T socklen_t
122 # endif
123 # elif defined(__WXMAC__)
124 # define WX_SOCKLEN_T socklen_t
125 # else
126 # define WX_SOCKLEN_T int
127 # endif
128 #endif
129
130 #endif /* SOCKLEN_T */
131
132 #ifndef SOCKOPTLEN_T
133 #define SOCKOPTLEN_T WX_SOCKLEN_T
134 #endif
135
136 /* UnixWare reportedly needs this for FIONBIO definition */
137 #ifdef __UNIXWARE__
138 #include <sys/filio.h>
139 #endif
140
141 /*
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.
144 */
145 #ifndef INADDR_NONE
146 #define INADDR_NONE INADDR_BROADCAST
147 #endif
148
149 #if defined(__VISAGECPP__) || defined(__WATCOMC__)
150
151 #define MASK_SIGNAL() {
152 #define UNMASK_SIGNAL() }
153
154 #else
155 extern "C" { typedef void (*wxSigHandler)(int); }
156
157 #define MASK_SIGNAL() \
158 { \
159 wxSigHandler old_handler = signal(SIGPIPE, SIG_IGN);
160
161 #define UNMASK_SIGNAL() \
162 signal(SIGPIPE, old_handler); \
163 }
164
165 #endif
166
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. */
171 #ifdef MSG_NOSIGNAL
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 */
176
177 #if wxUSE_THREADS && (defined(HAVE_GETHOSTBYNAME) || defined(HAVE_GETSERVBYNAME))
178 # include "wx/thread.h"
179 #endif
180
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)
185 {
186 /* copy old structure */
187 memcpy(h, he, sizeof(struct hostent));
188
189 /* copy name */
190 int len = strlen(h->h_name);
191 if (len > size)
192 {
193 *err = ENOMEM;
194 return NULL;
195 }
196 memcpy(buffer, h->h_name, len);
197 buffer[len] = '\0';
198 h->h_name = buffer;
199
200 /* track position in the buffer */
201 int pos = len + 1;
202
203 /* reuse len to store address length */
204 len = h->h_length;
205
206 /* ensure pointer alignment */
207 unsigned int misalign = sizeof(char *) - pos%sizeof(char *);
208 if(misalign < sizeof(char *))
209 pos += misalign;
210
211 /* leave space for pointer list */
212 char **p = h->h_addr_list, **q;
213 char **h_addr_list = (char **)(buffer + pos);
214 while(*(p++) != 0)
215 pos += sizeof(char *);
216
217 /* copy addresses and fill new pointer list */
218 for (p = h->h_addr_list, q = h_addr_list; *p != 0; p++, q++)
219 {
220 if (size < pos + len)
221 {
222 *err = ENOMEM;
223 return NULL;
224 }
225 memcpy(buffer + pos, *p, len); /* copy content */
226 *q = buffer + pos; /* set copied pointer to copied content */
227 pos += len;
228 }
229 *++q = 0; /* null terminate the pointer list */
230 h->h_addr_list = h_addr_list; /* copy pointer to pointers */
231
232 /* ensure word alignment of pointers */
233 misalign = sizeof(char *) - pos%sizeof(char *);
234 if(misalign < sizeof(char *))
235 pos += misalign;
236
237 /* leave space for pointer list */
238 p = h->h_aliases;
239 char **h_aliases = (char **)(buffer + pos);
240 while(*(p++) != 0)
241 pos += sizeof(char *);
242
243 /* copy aliases and fill new pointer list */
244 for (p = h->h_aliases, q = h_aliases; *p != 0; p++, q++)
245 {
246 len = strlen(*p);
247 if (size <= pos + len)
248 {
249 *err = ENOMEM;
250 return NULL;
251 }
252 memcpy(buffer + pos, *p, len); /* copy content */
253 buffer[pos + len] = '\0';
254 *q = buffer + pos; /* set copied pointer to copied content */
255 pos += len + 1;
256 }
257 *++q = 0; /* null terminate the pointer list */
258 h->h_aliases = h_aliases; /* copy pointer to pointers */
259
260 return h;
261 }
262 #endif
263
264 #if defined(HAVE_GETHOSTBYNAME) && wxUSE_THREADS
265 static wxMutex nameLock;
266 #endif
267 struct hostent * wxGethostbyname_r(const char *hostname, struct hostent *h,
268 void *buffer, int size, int *err)
269
270 {
271 struct hostent *he = NULL;
272 *err = 0;
273 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_6)
274 if (gethostbyname_r(hostname, h, (char*)buffer, size, &he, err))
275 he = NULL;
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))
280 {
281 he = NULL;
282 *err = h_errno;
283 }
284 else
285 he = h;
286 #elif defined(HAVE_GETHOSTBYNAME)
287 #if wxUSE_THREADS
288 wxMutexLocker locker(nameLock);
289 #endif
290 he = gethostbyname(hostname);
291 if (!he)
292 *err = h_errno;
293 else
294 he = deepCopyHostent(h, he, (char*)buffer, size, err);
295 #endif
296 return he;
297 }
298
299 #if defined(HAVE_GETHOSTBYNAME) && wxUSE_THREADS
300 static wxMutex addrLock;
301 #endif
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)
305 {
306 struct hostent *he = NULL;
307 *err = 0;
308 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_6)
309 if (gethostbyaddr_r(addr_buf, buf_size, proto, h,
310 (char*)buffer, size, &he, err))
311 he = NULL;
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))
317 {
318 he = NULL;
319 *err = h_errno;
320 }
321 else
322 he = h;
323 #elif defined(HAVE_GETHOSTBYNAME)
324 #if wxUSE_THREADS
325 wxMutexLocker locker(addrLock);
326 #endif
327 he = gethostbyaddr(addr_buf, buf_size, proto);
328 if (!he)
329 *err = h_errno;
330 else
331 he = deepCopyHostent(h, he, (char*)buffer, size, err);
332 #endif
333 return he;
334 }
335
336 #if defined(HAVE_GETSERVBYNAME)
337 static struct servent * deepCopyServent(struct servent *s,
338 const struct servent *se,
339 char *buffer, int size)
340 {
341 /* copy plain old structure */
342 memcpy(s, se, sizeof(struct servent));
343
344 /* copy name */
345 int len = strlen(s->s_name);
346 if (len >= size)
347 {
348 return NULL;
349 }
350 memcpy(buffer, s->s_name, len);
351 buffer[len] = '\0';
352 s->s_name = buffer;
353
354 /* track position in the buffer */
355 int pos = len + 1;
356
357 /* copy protocol */
358 len = strlen(s->s_proto);
359 if (pos + len >= size)
360 {
361 return NULL;
362 }
363 memcpy(buffer + pos, s->s_proto, len);
364 buffer[pos + len] = '\0';
365 s->s_proto = buffer + pos;
366
367 /* track position in the buffer */
368 pos += len + 1;
369
370 /* ensure pointer alignment */
371 unsigned int misalign = sizeof(char *) - pos%sizeof(char *);
372 if(misalign < sizeof(char *))
373 pos += misalign;
374
375 /* leave space for pointer list */
376 char **p = s->s_aliases, **q;
377 char **s_aliases = (char **)(buffer + pos);
378 while(*(p++) != 0)
379 pos += sizeof(char *);
380
381 /* copy addresses and fill new pointer list */
382 for (p = s->s_aliases, q = s_aliases; *p != 0; p++, q++){
383 len = strlen(*p);
384 if (size <= pos + len)
385 {
386 return NULL;
387 }
388 memcpy(buffer + pos, *p, len); /* copy content */
389 buffer[pos + len] = '\0';
390 *q = buffer + pos; /* set copied pointer to copied content */
391 pos += len + 1;
392 }
393 *++q = 0; /* null terminate the pointer list */
394 s->s_aliases = s_aliases; /* copy pointer to pointers */
395 return s;
396 }
397 #endif
398
399 #if defined(HAVE_GETSERVBYNAME) && wxUSE_THREADS
400 static wxMutex servLock;
401 #endif
402 struct servent *wxGetservbyname_r(const char *port, const char *protocol,
403 struct servent *serv, void *buffer, int size)
404 {
405 struct servent *se = NULL;
406 #if defined(HAVE_FUNC_GETSERVBYNAME_R_6)
407 if (getservbyname_r(port, protocol, serv, (char*)buffer, size, &se))
408 se = NULL;
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))
413 se = NULL;
414 else
415 se = serv;
416 #elif defined(HAVE_GETSERVBYNAME)
417 #if wxUSE_THREADS
418 wxMutexLocker locker(servLock);
419 #endif
420 se = getservbyname(port, protocol);
421 if (se)
422 se = deepCopyServent(serv, se, (char*)buffer, size);
423 #endif
424 return se;
425 }
426
427 /* static */
428 wxSocketImpl *wxSocketImpl::Create(wxSocketBase& wxsocket)
429 {
430 return new wxSocketImplUnix(wxsocket);
431 }
432
433
434 /*
435 * Disallow further read/write operations on this socket, close
436 * the fd and disable all callbacks.
437 */
438 void wxSocketImplUnix::Shutdown()
439 {
440 /* Don't allow events to fire after socket has been closed */
441 DisableEvents();
442
443 wxSocketImpl::Shutdown();
444 }
445
446 wxSocketError wxSocketImplUnix::GetLastError() const
447 {
448 switch ( errno )
449 {
450 case 0:
451 return wxSOCKET_NOERROR;
452
453 case ENOTSOCK:
454 return wxSOCKET_INVSOCK;
455
456 // unfortunately EAGAIN only has the "would block" meaning for read(),
457 // not for connect() for which it means something rather different but
458 // we can't distinguish between these two situations currently...
459 case EAGAIN:
460 case EINPROGRESS:
461 return wxSOCKET_WOULDBLOCK;
462
463 default:
464 return wxSOCKET_IOERR;
465 }
466 }
467
468 void wxSocketImplUnix::DoEnableEvents(bool flag)
469 {
470 wxSocketManager * const manager = wxSocketManager::Get();
471 if ( flag )
472 {
473 manager->Install_Callback(this, wxSOCKET_INPUT);
474 manager->Install_Callback(this, wxSOCKET_OUTPUT);
475 }
476 else // off
477 {
478 manager->Uninstall_Callback(this, wxSOCKET_INPUT);
479 manager->Uninstall_Callback(this, wxSOCKET_OUTPUT);
480 }
481 }
482
483 /* Generic IO */
484
485 /* Like recv(), send(), ... */
486 int wxSocketImplUnix::Read(void *buffer, int size)
487 {
488 int ret;
489
490 if (m_fd == INVALID_SOCKET || m_server)
491 {
492 m_error = wxSOCKET_INVSOCK;
493 return -1;
494 }
495
496 /* Disable events during query of socket status */
497 DisableEvent(wxSOCKET_INPUT);
498
499 /* Read the data */
500 if (m_stream)
501 ret = Recv_Stream(buffer, size);
502 else
503 ret = Recv_Dgram(buffer, size);
504
505 /*
506 * If recv returned zero for a TCP socket (if m_stream == NULL, it's an UDP
507 * socket and empty datagrams are possible), then the connection has been
508 * gracefully closed.
509 *
510 * Otherwise, recv has returned an error (-1), in which case we have lost
511 * the socket only if errno does _not_ indicate that there may be more data
512 * to read.
513 */
514 if ((ret == 0) && m_stream)
515 {
516 /* Make sure wxSOCKET_LOST event gets sent and shut down the socket */
517 m_detected = wxSOCKET_LOST_FLAG;
518 OnReadWaiting();
519 return 0;
520 }
521 else if (ret == -1)
522 {
523 if ((errno == EWOULDBLOCK) || (errno == EAGAIN))
524 m_error = wxSOCKET_WOULDBLOCK;
525 else
526 m_error = wxSOCKET_IOERR;
527 }
528
529 /* Enable events again now that we are done processing */
530 EnableEvent(wxSOCKET_INPUT);
531
532 return ret;
533 }
534
535 int wxSocketImplUnix::Write(const void *buffer, int size)
536 {
537 int ret;
538
539 if (m_fd == INVALID_SOCKET || m_server)
540 {
541 m_error = wxSOCKET_INVSOCK;
542 return -1;
543 }
544
545 /* Write the data */
546 if (m_stream)
547 ret = Send_Stream(buffer, size);
548 else
549 ret = Send_Dgram(buffer, size);
550
551 if (ret == -1)
552 {
553 if ((errno == EWOULDBLOCK) || (errno == EAGAIN))
554 {
555 m_error = wxSOCKET_WOULDBLOCK;
556 }
557 else
558 {
559 m_error = wxSOCKET_IOERR;
560 }
561
562 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
563 * in MSW). Once the first OUTPUT event is received, users can assume
564 * that the socket is writable until a read operation fails. Only then
565 * will further OUTPUT events be posted.
566 */
567 EnableEvent(wxSOCKET_OUTPUT);
568
569 return -1;
570 }
571
572 return ret;
573 }
574
575 /* Flags */
576
577 void wxSocketImplUnix::EnableEvent(wxSocketNotify event)
578 {
579 m_detected &= ~(1 << event);
580 wxSocketManager::Get()->Install_Callback(this, event);
581 }
582
583 void wxSocketImplUnix::DisableEvent(wxSocketNotify event)
584 {
585 m_detected |= (1 << event);
586 wxSocketManager::Get()->Uninstall_Callback(this, event);
587 }
588
589 int wxSocketImplUnix::Recv_Stream(void *buffer, int size)
590 {
591 int ret;
592 do
593 {
594 ret = recv(m_fd, buffer, size, GSOCKET_MSG_NOSIGNAL);
595 }
596 while (ret == -1 && errno == EINTR); /* Loop until not interrupted */
597
598 return ret;
599 }
600
601 int wxSocketImplUnix::Recv_Dgram(void *buffer, int size)
602 {
603 wxSockAddr from;
604 WX_SOCKLEN_T fromlen = sizeof(from);
605 int ret;
606 wxSocketError err;
607
608 fromlen = sizeof(from);
609
610 do
611 {
612 ret = recvfrom(m_fd, buffer, size, 0, (sockaddr*)&from, (WX_SOCKLEN_T *) &fromlen);
613 }
614 while (ret == -1 && errno == EINTR); /* Loop until not interrupted */
615
616 if (ret == -1)
617 return -1;
618
619 /* Translate a system address into a wxSocketImplUnix address */
620 if (!m_peer)
621 {
622 m_peer = GAddress_new();
623 if (!m_peer)
624 {
625 m_error = wxSOCKET_MEMERR;
626 return -1;
627 }
628 }
629
630 err = _GAddress_translate_from(m_peer, (sockaddr*)&from, fromlen);
631 if (err != wxSOCKET_NOERROR)
632 {
633 GAddress_destroy(m_peer);
634 m_peer = NULL;
635 m_error = err;
636 return -1;
637 }
638
639 return ret;
640 }
641
642 int wxSocketImplUnix::Send_Stream(const void *buffer, int size)
643 {
644 int ret;
645
646 MASK_SIGNAL();
647
648 do
649 {
650 ret = send(m_fd, buffer, size, GSOCKET_MSG_NOSIGNAL);
651 }
652 while (ret == -1 && errno == EINTR); /* Loop until not interrupted */
653
654 UNMASK_SIGNAL();
655
656 return ret;
657 }
658
659 int wxSocketImplUnix::Send_Dgram(const void *buffer, int size)
660 {
661 struct sockaddr *addr;
662 int len, ret;
663 wxSocketError err;
664
665 if (!m_peer)
666 {
667 m_error = wxSOCKET_INVADDR;
668 return -1;
669 }
670
671 err = _GAddress_translate_to(m_peer, &addr, &len);
672 if (err != wxSOCKET_NOERROR)
673 {
674 m_error = err;
675 return -1;
676 }
677
678 MASK_SIGNAL();
679
680 do
681 {
682 ret = sendto(m_fd, buffer, size, 0, addr, len);
683 }
684 while (ret == -1 && errno == EINTR); /* Loop until not interrupted */
685
686 UNMASK_SIGNAL();
687
688 /* Frees memory allocated from _GAddress_translate_to */
689 free(addr);
690
691 return ret;
692 }
693
694 void wxSocketImplUnix::OnStateChange(wxSocketNotify event)
695 {
696 DisableEvent(event);
697 NotifyOnStateChange(event);
698
699 if ( event == wxSOCKET_LOST )
700 Shutdown();
701 }
702
703 void wxSocketImplUnix::OnReadWaiting()
704 {
705 char c;
706
707 if (m_fd == INVALID_SOCKET)
708 {
709 return;
710 }
711
712 /* If we have already detected a LOST event, then don't try
713 * to do any further processing.
714 */
715 if ((m_detected & wxSOCKET_LOST_FLAG) != 0)
716 {
717 m_establishing = false;
718
719 OnStateChange(wxSOCKET_LOST);
720 return;
721 }
722
723 int num = recv(m_fd, &c, 1, MSG_PEEK | GSOCKET_MSG_NOSIGNAL);
724
725 if (num > 0)
726 {
727 OnStateChange(wxSOCKET_INPUT);
728 }
729 else
730 {
731 if (m_server && m_stream)
732 {
733 OnStateChange(wxSOCKET_CONNECTION);
734 }
735 else if (num == 0)
736 {
737 if (m_stream)
738 {
739 /* graceful shutdown */
740 OnStateChange(wxSOCKET_LOST);
741 }
742 else
743 {
744 /* Empty datagram received */
745 OnStateChange(wxSOCKET_INPUT);
746 }
747 }
748 else
749 {
750 /* Do not throw a lost event in cases where the socket isn't really lost */
751 if ((errno == EWOULDBLOCK) || (errno == EAGAIN) || (errno == EINTR))
752 {
753 OnStateChange(wxSOCKET_INPUT);
754 }
755 else
756 {
757 OnStateChange(wxSOCKET_LOST);
758 }
759 }
760 }
761 }
762
763 void wxSocketImplUnix::OnWriteWaiting()
764 {
765 /* If we have already detected a LOST event, then don't try
766 * to do any further processing.
767 */
768 if ((m_detected & wxSOCKET_LOST_FLAG) != 0)
769 {
770 m_establishing = false;
771
772 OnStateChange(wxSOCKET_LOST);
773 return;
774 }
775
776 if (m_establishing && !m_server)
777 {
778 int error;
779 SOCKOPTLEN_T len = sizeof(error);
780
781 m_establishing = false;
782
783 getsockopt(m_fd, SOL_SOCKET, SO_ERROR, (char*)&error, &len);
784
785 if (error)
786 {
787 OnStateChange(wxSOCKET_LOST);
788 }
789 else
790 {
791 OnStateChange(wxSOCKET_CONNECTION);
792 /* We have to fire this event by hand because CONNECTION (for clients)
793 * and OUTPUT are internally the same and we just disabled CONNECTION
794 * events with the above macro.
795 */
796 OnStateChange(wxSOCKET_OUTPUT);
797 }
798 }
799 else
800 {
801 OnStateChange(wxSOCKET_OUTPUT);
802 }
803 }
804
805 void wxSocketImplUnix::OnExceptionWaiting()
806 {
807 wxFAIL_MSG( "not supposed to be called" );
808 }
809
810 /*
811 * -------------------------------------------------------------------------
812 * GAddress
813 * -------------------------------------------------------------------------
814 */
815
816 /* CHECK_ADDRESS verifies that the current address family is either
817 * wxSOCKET_NOFAMILY or wxSOCKET_*family*, and if it is wxSOCKET_NOFAMILY, it
818 * initalizes it to be a wxSOCKET_*family*. In other cases, it returns
819 * an appropiate error code.
820 *
821 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
822 */
823 #define CHECK_ADDRESS(address, family) \
824 { \
825 if (address->m_family == wxSOCKET_NOFAMILY) \
826 if (_GAddress_Init_##family(address) != wxSOCKET_NOERROR) \
827 return address->m_error; \
828 if (address->m_family != wxSOCKET_##family) \
829 { \
830 address->m_error = wxSOCKET_INVADDR; \
831 return wxSOCKET_INVADDR; \
832 } \
833 }
834
835 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
836 { \
837 if (address->m_family == wxSOCKET_NOFAMILY) \
838 if (_GAddress_Init_##family(address) != wxSOCKET_NOERROR) \
839 return retval; \
840 if (address->m_family != wxSOCKET_##family) \
841 { \
842 address->m_error = wxSOCKET_INVADDR; \
843 return retval; \
844 } \
845 }
846
847
848 GAddress *GAddress_new(void)
849 {
850 GAddress *address;
851
852 if ((address = (GAddress *) malloc(sizeof(GAddress))) == NULL)
853 return NULL;
854
855 address->m_family = wxSOCKET_NOFAMILY;
856 address->m_addr = NULL;
857 address->m_len = 0;
858
859 return address;
860 }
861
862 GAddress *GAddress_copy(GAddress *address)
863 {
864 GAddress *addr2;
865
866 assert(address != NULL);
867
868 if ((addr2 = (GAddress *) malloc(sizeof(GAddress))) == NULL)
869 return NULL;
870
871 memcpy(addr2, address, sizeof(GAddress));
872
873 if (address->m_addr && address->m_len > 0)
874 {
875 addr2->m_addr = (struct sockaddr *)malloc(addr2->m_len);
876 if (addr2->m_addr == NULL)
877 {
878 free(addr2);
879 return NULL;
880 }
881 memcpy(addr2->m_addr, address->m_addr, addr2->m_len);
882 }
883
884 return addr2;
885 }
886
887 void GAddress_destroy(GAddress *address)
888 {
889 assert(address != NULL);
890
891 if (address->m_addr)
892 free(address->m_addr);
893
894 free(address);
895 }
896
897 void GAddress_SetFamily(GAddress *address, GAddressType type)
898 {
899 assert(address != NULL);
900
901 address->m_family = type;
902 }
903
904 GAddressType GAddress_GetFamily(GAddress *address)
905 {
906 assert(address != NULL);
907
908 return address->m_family;
909 }
910
911 wxSocketError _GAddress_translate_from(GAddress *address,
912 struct sockaddr *addr, int len)
913 {
914 address->m_realfamily = addr->sa_family;
915 switch (addr->sa_family)
916 {
917 case AF_INET:
918 address->m_family = wxSOCKET_INET;
919 break;
920 case AF_UNIX:
921 address->m_family = wxSOCKET_UNIX;
922 break;
923 #if wxUSE_IPV6
924 case AF_INET6:
925 address->m_family = wxSOCKET_INET6;
926 break;
927 #endif // wxUSE_IPV6
928 default:
929 {
930 address->m_error = wxSOCKET_INVOP;
931 return wxSOCKET_INVOP;
932 }
933 }
934
935 if (address->m_addr)
936 free(address->m_addr);
937
938 address->m_len = len;
939 address->m_addr = (struct sockaddr *)malloc(len);
940
941 if (address->m_addr == NULL)
942 {
943 address->m_error = wxSOCKET_MEMERR;
944 return wxSOCKET_MEMERR;
945 }
946
947 memcpy(address->m_addr, addr, len);
948
949 return wxSOCKET_NOERROR;
950 }
951
952 wxSocketError _GAddress_translate_to(GAddress *address,
953 struct sockaddr **addr, int *len)
954 {
955 if (!address->m_addr)
956 {
957 address->m_error = wxSOCKET_INVADDR;
958 return wxSOCKET_INVADDR;
959 }
960
961 *len = address->m_len;
962 *addr = (struct sockaddr *)malloc(address->m_len);
963 if (*addr == NULL)
964 {
965 address->m_error = wxSOCKET_MEMERR;
966 return wxSOCKET_MEMERR;
967 }
968
969 memcpy(*addr, address->m_addr, address->m_len);
970 return wxSOCKET_NOERROR;
971 }
972
973 /*
974 * -------------------------------------------------------------------------
975 * Internet address family
976 * -------------------------------------------------------------------------
977 */
978
979 wxSocketError _GAddress_Init_INET(GAddress *address)
980 {
981 address->m_len = sizeof(struct sockaddr_in);
982 address->m_addr = (struct sockaddr *) malloc(address->m_len);
983 if (address->m_addr == NULL)
984 {
985 address->m_error = wxSOCKET_MEMERR;
986 return wxSOCKET_MEMERR;
987 }
988
989 address->m_family = wxSOCKET_INET;
990 address->m_realfamily = PF_INET;
991 ((struct sockaddr_in *)address->m_addr)->sin_family = AF_INET;
992 ((struct sockaddr_in *)address->m_addr)->sin_addr.s_addr = INADDR_ANY;
993
994 return wxSOCKET_NOERROR;
995 }
996
997 wxSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname)
998 {
999 struct hostent *he;
1000 struct in_addr *addr;
1001
1002 assert(address != NULL);
1003
1004 CHECK_ADDRESS(address, INET);
1005
1006 addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr);
1007
1008 /* If it is a numeric host name, convert it now */
1009 #if defined(HAVE_INET_ATON)
1010 if (inet_aton(hostname, addr) == 0)
1011 {
1012 #elif defined(HAVE_INET_ADDR)
1013 if ( (addr->s_addr = inet_addr(hostname)) == (unsigned)-1 )
1014 {
1015 #else
1016 /* Use gethostbyname by default */
1017 #ifndef __WXMAC__
1018 int val = 1; /* VA doesn't like constants in conditional expressions */
1019 if (val)
1020 #endif
1021 {
1022 #endif
1023 struct in_addr *array_addr;
1024
1025 /* It is a real name, we solve it */
1026 struct hostent h;
1027 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
1028 struct hostent_data buffer;
1029 #else
1030 char buffer[1024];
1031 #endif
1032 int err;
1033 he = wxGethostbyname_r(hostname, &h, (void*)&buffer, sizeof(buffer), &err);
1034 if (he == NULL)
1035 {
1036 /* Reset to invalid address */
1037 addr->s_addr = INADDR_NONE;
1038 address->m_error = wxSOCKET_NOHOST;
1039 return wxSOCKET_NOHOST;
1040 }
1041
1042 array_addr = (struct in_addr *) *(he->h_addr_list);
1043 addr->s_addr = array_addr[0].s_addr;
1044 }
1045
1046 return wxSOCKET_NOERROR;
1047 }
1048
1049
1050 wxSocketError GAddress_INET_SetBroadcastAddress(GAddress *address)
1051 {
1052 return GAddress_INET_SetHostAddress(address, INADDR_BROADCAST);
1053 }
1054
1055 wxSocketError GAddress_INET_SetAnyAddress(GAddress *address)
1056 {
1057 return GAddress_INET_SetHostAddress(address, INADDR_ANY);
1058 }
1059
1060 wxSocketError GAddress_INET_SetHostAddress(GAddress *address,
1061 unsigned long hostaddr)
1062 {
1063 struct in_addr *addr;
1064
1065 assert(address != NULL);
1066
1067 CHECK_ADDRESS(address, INET);
1068
1069 addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr);
1070 addr->s_addr = htonl(hostaddr);
1071
1072 return wxSOCKET_NOERROR;
1073 }
1074
1075 wxSocketError GAddress_INET_SetPortName(GAddress *address, const char *port,
1076 const char *protocol)
1077 {
1078 struct servent *se;
1079 struct sockaddr_in *addr;
1080
1081 assert(address != NULL);
1082 CHECK_ADDRESS(address, INET);
1083
1084 if (!port)
1085 {
1086 address->m_error = wxSOCKET_INVPORT;
1087 return wxSOCKET_INVPORT;
1088 }
1089
1090 #if defined(HAVE_FUNC_GETSERVBYNAME_R_4)
1091 struct servent_data buffer;
1092 #else
1093 char buffer[1024];
1094 #endif
1095 struct servent serv;
1096 se = wxGetservbyname_r(port, protocol, &serv,
1097 (void*)&buffer, sizeof(buffer));
1098 if (!se)
1099 {
1100 /* the cast to int suppresses compiler warnings about subscript having the
1101 type char */
1102 if (isdigit((int)port[0]))
1103 {
1104 int port_int;
1105
1106 port_int = atoi(port);
1107 addr = (struct sockaddr_in *)address->m_addr;
1108 addr->sin_port = htons(port_int);
1109 return wxSOCKET_NOERROR;
1110 }
1111
1112 address->m_error = wxSOCKET_INVPORT;
1113 return wxSOCKET_INVPORT;
1114 }
1115
1116 addr = (struct sockaddr_in *)address->m_addr;
1117 addr->sin_port = se->s_port;
1118
1119 return wxSOCKET_NOERROR;
1120 }
1121
1122 wxSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port)
1123 {
1124 struct sockaddr_in *addr;
1125
1126 assert(address != NULL);
1127 CHECK_ADDRESS(address, INET);
1128
1129 addr = (struct sockaddr_in *)address->m_addr;
1130 addr->sin_port = htons(port);
1131
1132 return wxSOCKET_NOERROR;
1133 }
1134
1135 wxSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname, size_t sbuf)
1136 {
1137 struct hostent *he;
1138 char *addr_buf;
1139 struct sockaddr_in *addr;
1140
1141 assert(address != NULL);
1142 CHECK_ADDRESS(address, INET);
1143
1144 addr = (struct sockaddr_in *)address->m_addr;
1145 addr_buf = (char *)&(addr->sin_addr);
1146
1147 struct hostent temphost;
1148 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
1149 struct hostent_data buffer;
1150 #else
1151 char buffer[1024];
1152 #endif
1153 int err;
1154 he = wxGethostbyaddr_r(addr_buf, sizeof(addr->sin_addr), AF_INET, &temphost,
1155 (void*)&buffer, sizeof(buffer), &err);
1156 if (he == NULL)
1157 {
1158 address->m_error = wxSOCKET_NOHOST;
1159 return wxSOCKET_NOHOST;
1160 }
1161
1162 strncpy(hostname, he->h_name, sbuf);
1163
1164 return wxSOCKET_NOERROR;
1165 }
1166
1167 unsigned long GAddress_INET_GetHostAddress(GAddress *address)
1168 {
1169 struct sockaddr_in *addr;
1170
1171 assert(address != NULL);
1172 CHECK_ADDRESS_RETVAL(address, INET, 0);
1173
1174 addr = (struct sockaddr_in *)address->m_addr;
1175
1176 return ntohl(addr->sin_addr.s_addr);
1177 }
1178
1179 unsigned short GAddress_INET_GetPort(GAddress *address)
1180 {
1181 struct sockaddr_in *addr;
1182
1183 assert(address != NULL);
1184 CHECK_ADDRESS_RETVAL(address, INET, 0);
1185
1186 addr = (struct sockaddr_in *)address->m_addr;
1187 return ntohs(addr->sin_port);
1188 }
1189
1190 #if wxUSE_IPV6
1191 /*
1192 * -------------------------------------------------------------------------
1193 * Internet IPv6 address family
1194 * -------------------------------------------------------------------------
1195 */
1196
1197 wxSocketError _GAddress_Init_INET6(GAddress *address)
1198 {
1199 struct in6_addr any_address = IN6ADDR_ANY_INIT;
1200 address->m_len = sizeof(struct sockaddr_in6);
1201 address->m_addr = (struct sockaddr *) malloc(address->m_len);
1202 if (address->m_addr == NULL)
1203 {
1204 address->m_error = wxSOCKET_MEMERR;
1205 return wxSOCKET_MEMERR;
1206 }
1207 memset(address->m_addr,0,address->m_len);
1208
1209 address->m_family = wxSOCKET_INET6;
1210 address->m_realfamily = AF_INET6;
1211 ((struct sockaddr_in6 *)address->m_addr)->sin6_family = AF_INET6;
1212 ((struct sockaddr_in6 *)address->m_addr)->sin6_addr = any_address;
1213
1214 return wxSOCKET_NOERROR;
1215 }
1216
1217 wxSocketError GAddress_INET6_SetHostName(GAddress *address, const char *hostname)
1218 {
1219 assert(address != NULL);
1220 CHECK_ADDRESS(address, INET6);
1221
1222 addrinfo hints;
1223 memset( & hints, 0, sizeof( hints ) );
1224 hints.ai_family = AF_INET6;
1225 addrinfo * info = 0;
1226 if ( getaddrinfo( hostname, "0", & hints, & info ) || ! info )
1227 {
1228 address->m_error = wxSOCKET_NOHOST;
1229 return wxSOCKET_NOHOST;
1230 }
1231
1232 memcpy( address->m_addr, info->ai_addr, info->ai_addrlen );
1233 freeaddrinfo( info );
1234 return wxSOCKET_NOERROR;
1235 }
1236
1237 wxSocketError GAddress_INET6_SetAnyAddress(GAddress *address)
1238 {
1239 assert(address != NULL);
1240
1241 CHECK_ADDRESS(address, INET6);
1242
1243 struct in6_addr addr;
1244 memset( & addr, 0, sizeof( addr ) );
1245 return GAddress_INET6_SetHostAddress(address, addr);
1246 }
1247 wxSocketError GAddress_INET6_SetHostAddress(GAddress *address,
1248 struct in6_addr hostaddr)
1249 {
1250 assert(address != NULL);
1251
1252 CHECK_ADDRESS(address, INET6);
1253
1254 ((struct sockaddr_in6 *)address->m_addr)->sin6_addr = hostaddr;
1255
1256 return wxSOCKET_NOERROR;
1257 }
1258
1259 wxSocketError GAddress_INET6_SetPortName(GAddress *address, const char *port,
1260 const char *protocol)
1261 {
1262 struct servent *se;
1263 struct sockaddr_in6 *addr;
1264
1265 assert(address != NULL);
1266 CHECK_ADDRESS(address, INET6);
1267
1268 if (!port)
1269 {
1270 address->m_error = wxSOCKET_INVPORT;
1271 return wxSOCKET_INVPORT;
1272 }
1273
1274 se = getservbyname(port, protocol);
1275 if (!se)
1276 {
1277 if (isdigit(port[0]))
1278 {
1279 int port_int;
1280
1281 port_int = atoi(port);
1282 addr = (struct sockaddr_in6 *)address->m_addr;
1283 addr->sin6_port = htons((u_short) port_int);
1284 return wxSOCKET_NOERROR;
1285 }
1286
1287 address->m_error = wxSOCKET_INVPORT;
1288 return wxSOCKET_INVPORT;
1289 }
1290
1291 addr = (struct sockaddr_in6 *)address->m_addr;
1292 addr->sin6_port = se->s_port;
1293
1294 return wxSOCKET_NOERROR;
1295 }
1296
1297 wxSocketError GAddress_INET6_SetPort(GAddress *address, unsigned short port)
1298 {
1299 struct sockaddr_in6 *addr;
1300
1301 assert(address != NULL);
1302 CHECK_ADDRESS(address, INET6);
1303
1304 addr = (struct sockaddr_in6 *)address->m_addr;
1305 addr->sin6_port = htons(port);
1306
1307 return wxSOCKET_NOERROR;
1308 }
1309
1310 wxSocketError GAddress_INET6_GetHostName(GAddress *address, char *hostname, size_t sbuf)
1311 {
1312 struct hostent *he;
1313 char *addr_buf;
1314 struct sockaddr_in6 *addr;
1315
1316 assert(address != NULL);
1317 CHECK_ADDRESS(address, INET6);
1318
1319 addr = (struct sockaddr_in6 *)address->m_addr;
1320 addr_buf = (char *)&(addr->sin6_addr);
1321
1322 he = gethostbyaddr(addr_buf, sizeof(addr->sin6_addr), AF_INET6);
1323 if (he == NULL)
1324 {
1325 address->m_error = wxSOCKET_NOHOST;
1326 return wxSOCKET_NOHOST;
1327 }
1328
1329 strncpy(hostname, he->h_name, sbuf);
1330
1331 return wxSOCKET_NOERROR;
1332 }
1333
1334 wxSocketError GAddress_INET6_GetHostAddress(GAddress *address,struct in6_addr *hostaddr)
1335 {
1336 assert(address != NULL);
1337 assert(hostaddr != NULL);
1338 CHECK_ADDRESS_RETVAL(address, INET6, wxSOCKET_INVADDR);
1339 *hostaddr = ( (struct sockaddr_in6 *)address->m_addr )->sin6_addr;
1340 return wxSOCKET_NOERROR;
1341 }
1342
1343 unsigned short GAddress_INET6_GetPort(GAddress *address)
1344 {
1345 assert(address != NULL);
1346 CHECK_ADDRESS_RETVAL(address, INET6, 0);
1347
1348 return ntohs( ((struct sockaddr_in6 *)address->m_addr)->sin6_port );
1349 }
1350
1351 #endif // wxUSE_IPV6
1352
1353 /*
1354 * -------------------------------------------------------------------------
1355 * Unix address family
1356 * -------------------------------------------------------------------------
1357 */
1358
1359 #ifndef __VISAGECPP__
1360 wxSocketError _GAddress_Init_UNIX(GAddress *address)
1361 {
1362 address->m_len = sizeof(struct sockaddr_un);
1363 address->m_addr = (struct sockaddr *)malloc(address->m_len);
1364 if (address->m_addr == NULL)
1365 {
1366 address->m_error = wxSOCKET_MEMERR;
1367 return wxSOCKET_MEMERR;
1368 }
1369
1370 address->m_family = wxSOCKET_UNIX;
1371 address->m_realfamily = PF_UNIX;
1372 ((struct sockaddr_un *)address->m_addr)->sun_family = AF_UNIX;
1373 ((struct sockaddr_un *)address->m_addr)->sun_path[0] = 0;
1374
1375 return wxSOCKET_NOERROR;
1376 }
1377
1378 #define UNIX_SOCK_PATHLEN (sizeof(addr->sun_path)/sizeof(addr->sun_path[0]))
1379
1380 wxSocketError GAddress_UNIX_SetPath(GAddress *address, const char *path)
1381 {
1382 struct sockaddr_un *addr;
1383
1384 assert(address != NULL);
1385
1386 CHECK_ADDRESS(address, UNIX);
1387
1388 addr = ((struct sockaddr_un *)address->m_addr);
1389 strncpy(addr->sun_path, path, UNIX_SOCK_PATHLEN);
1390 addr->sun_path[UNIX_SOCK_PATHLEN - 1] = '\0';
1391
1392 return wxSOCKET_NOERROR;
1393 }
1394
1395 wxSocketError GAddress_UNIX_GetPath(GAddress *address, char *path, size_t sbuf)
1396 {
1397 struct sockaddr_un *addr;
1398
1399 assert(address != NULL);
1400 CHECK_ADDRESS(address, UNIX);
1401
1402 addr = (struct sockaddr_un *)address->m_addr;
1403
1404 strncpy(path, addr->sun_path, sbuf);
1405
1406 return wxSOCKET_NOERROR;
1407 }
1408 #endif /* !defined(__VISAGECPP__) */
1409
1410 #endif /* wxUSE_SOCKETS */