remove the badle defined and apparently unnecessary wxSocketImpl::m_detected field
[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 m_establishing = false;
517 OnStateChange(wxSOCKET_LOST);
518 return 0;
519 }
520 else if (ret == -1)
521 {
522 if ((errno == EWOULDBLOCK) || (errno == EAGAIN))
523 m_error = wxSOCKET_WOULDBLOCK;
524 else
525 m_error = wxSOCKET_IOERR;
526 }
527
528 /* Enable events again now that we are done processing */
529 EnableEvent(wxSOCKET_INPUT);
530
531 return ret;
532 }
533
534 int wxSocketImplUnix::Write(const void *buffer, int size)
535 {
536 int ret;
537
538 if (m_fd == INVALID_SOCKET || m_server)
539 {
540 m_error = wxSOCKET_INVSOCK;
541 return -1;
542 }
543
544 /* Write the data */
545 if (m_stream)
546 ret = Send_Stream(buffer, size);
547 else
548 ret = Send_Dgram(buffer, size);
549
550 if (ret == -1)
551 {
552 if ((errno == EWOULDBLOCK) || (errno == EAGAIN))
553 {
554 m_error = wxSOCKET_WOULDBLOCK;
555 }
556 else
557 {
558 m_error = wxSOCKET_IOERR;
559 }
560
561 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
562 * in MSW). Once the first OUTPUT event is received, users can assume
563 * that the socket is writable until a read operation fails. Only then
564 * will further OUTPUT events be posted.
565 */
566 EnableEvent(wxSOCKET_OUTPUT);
567
568 return -1;
569 }
570
571 return ret;
572 }
573
574 /* Flags */
575
576 void wxSocketImplUnix::EnableEvent(wxSocketNotify event)
577 {
578 wxSocketManager::Get()->Install_Callback(this, event);
579 }
580
581 void wxSocketImplUnix::DisableEvent(wxSocketNotify event)
582 {
583 wxSocketManager::Get()->Uninstall_Callback(this, event);
584 }
585
586 int wxSocketImplUnix::Recv_Stream(void *buffer, int size)
587 {
588 int ret;
589 do
590 {
591 ret = recv(m_fd, buffer, size, GSOCKET_MSG_NOSIGNAL);
592 }
593 while (ret == -1 && errno == EINTR); /* Loop until not interrupted */
594
595 return ret;
596 }
597
598 int wxSocketImplUnix::Recv_Dgram(void *buffer, int size)
599 {
600 wxSockAddr from;
601 WX_SOCKLEN_T fromlen = sizeof(from);
602 int ret;
603 wxSocketError err;
604
605 fromlen = sizeof(from);
606
607 do
608 {
609 ret = recvfrom(m_fd, buffer, size, 0, (sockaddr*)&from, (WX_SOCKLEN_T *) &fromlen);
610 }
611 while (ret == -1 && errno == EINTR); /* Loop until not interrupted */
612
613 if (ret == -1)
614 return -1;
615
616 /* Translate a system address into a wxSocketImplUnix address */
617 if (!m_peer)
618 {
619 m_peer = GAddress_new();
620 if (!m_peer)
621 {
622 m_error = wxSOCKET_MEMERR;
623 return -1;
624 }
625 }
626
627 err = _GAddress_translate_from(m_peer, (sockaddr*)&from, fromlen);
628 if (err != wxSOCKET_NOERROR)
629 {
630 GAddress_destroy(m_peer);
631 m_peer = NULL;
632 m_error = err;
633 return -1;
634 }
635
636 return ret;
637 }
638
639 int wxSocketImplUnix::Send_Stream(const void *buffer, int size)
640 {
641 int ret;
642
643 MASK_SIGNAL();
644
645 do
646 {
647 ret = send(m_fd, buffer, size, GSOCKET_MSG_NOSIGNAL);
648 }
649 while (ret == -1 && errno == EINTR); /* Loop until not interrupted */
650
651 UNMASK_SIGNAL();
652
653 return ret;
654 }
655
656 int wxSocketImplUnix::Send_Dgram(const void *buffer, int size)
657 {
658 struct sockaddr *addr;
659 int len, ret;
660 wxSocketError err;
661
662 if (!m_peer)
663 {
664 m_error = wxSOCKET_INVADDR;
665 return -1;
666 }
667
668 err = _GAddress_translate_to(m_peer, &addr, &len);
669 if (err != wxSOCKET_NOERROR)
670 {
671 m_error = err;
672 return -1;
673 }
674
675 MASK_SIGNAL();
676
677 do
678 {
679 ret = sendto(m_fd, buffer, size, 0, addr, len);
680 }
681 while (ret == -1 && errno == EINTR); /* Loop until not interrupted */
682
683 UNMASK_SIGNAL();
684
685 /* Frees memory allocated from _GAddress_translate_to */
686 free(addr);
687
688 return ret;
689 }
690
691 void wxSocketImplUnix::OnStateChange(wxSocketNotify event)
692 {
693 DisableEvent(event);
694 NotifyOnStateChange(event);
695
696 if ( event == wxSOCKET_LOST )
697 Shutdown();
698 }
699
700 void wxSocketImplUnix::OnReadWaiting()
701 {
702 char c;
703
704 if (m_fd == INVALID_SOCKET)
705 {
706 return;
707 }
708
709 int num = recv(m_fd, &c, 1, MSG_PEEK | GSOCKET_MSG_NOSIGNAL);
710
711 if (num > 0)
712 {
713 OnStateChange(wxSOCKET_INPUT);
714 }
715 else
716 {
717 if (m_server && m_stream)
718 {
719 OnStateChange(wxSOCKET_CONNECTION);
720 }
721 else if (num == 0)
722 {
723 if (m_stream)
724 {
725 /* graceful shutdown */
726 OnStateChange(wxSOCKET_LOST);
727 }
728 else
729 {
730 /* Empty datagram received */
731 OnStateChange(wxSOCKET_INPUT);
732 }
733 }
734 else
735 {
736 /* Do not throw a lost event in cases where the socket isn't really lost */
737 if ((errno == EWOULDBLOCK) || (errno == EAGAIN) || (errno == EINTR))
738 {
739 OnStateChange(wxSOCKET_INPUT);
740 }
741 else
742 {
743 OnStateChange(wxSOCKET_LOST);
744 }
745 }
746 }
747 }
748
749 void wxSocketImplUnix::OnWriteWaiting()
750 {
751 if (m_establishing && !m_server)
752 {
753 int error;
754 SOCKOPTLEN_T len = sizeof(error);
755
756 m_establishing = false;
757
758 getsockopt(m_fd, SOL_SOCKET, SO_ERROR, (char*)&error, &len);
759
760 if (error)
761 {
762 OnStateChange(wxSOCKET_LOST);
763 }
764 else
765 {
766 OnStateChange(wxSOCKET_CONNECTION);
767 /* We have to fire this event by hand because CONNECTION (for clients)
768 * and OUTPUT are internally the same and we just disabled CONNECTION
769 * events with the above macro.
770 */
771 OnStateChange(wxSOCKET_OUTPUT);
772 }
773 }
774 else
775 {
776 OnStateChange(wxSOCKET_OUTPUT);
777 }
778 }
779
780 void wxSocketImplUnix::OnExceptionWaiting()
781 {
782 wxFAIL_MSG( "not supposed to be called" );
783 }
784
785 /*
786 * -------------------------------------------------------------------------
787 * GAddress
788 * -------------------------------------------------------------------------
789 */
790
791 /* CHECK_ADDRESS verifies that the current address family is either
792 * wxSOCKET_NOFAMILY or wxSOCKET_*family*, and if it is wxSOCKET_NOFAMILY, it
793 * initalizes it to be a wxSOCKET_*family*. In other cases, it returns
794 * an appropiate error code.
795 *
796 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
797 */
798 #define CHECK_ADDRESS(address, family) \
799 { \
800 if (address->m_family == wxSOCKET_NOFAMILY) \
801 if (_GAddress_Init_##family(address) != wxSOCKET_NOERROR) \
802 return address->m_error; \
803 if (address->m_family != wxSOCKET_##family) \
804 { \
805 address->m_error = wxSOCKET_INVADDR; \
806 return wxSOCKET_INVADDR; \
807 } \
808 }
809
810 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
811 { \
812 if (address->m_family == wxSOCKET_NOFAMILY) \
813 if (_GAddress_Init_##family(address) != wxSOCKET_NOERROR) \
814 return retval; \
815 if (address->m_family != wxSOCKET_##family) \
816 { \
817 address->m_error = wxSOCKET_INVADDR; \
818 return retval; \
819 } \
820 }
821
822
823 GAddress *GAddress_new(void)
824 {
825 GAddress *address;
826
827 if ((address = (GAddress *) malloc(sizeof(GAddress))) == NULL)
828 return NULL;
829
830 address->m_family = wxSOCKET_NOFAMILY;
831 address->m_addr = NULL;
832 address->m_len = 0;
833
834 return address;
835 }
836
837 GAddress *GAddress_copy(GAddress *address)
838 {
839 GAddress *addr2;
840
841 assert(address != NULL);
842
843 if ((addr2 = (GAddress *) malloc(sizeof(GAddress))) == NULL)
844 return NULL;
845
846 memcpy(addr2, address, sizeof(GAddress));
847
848 if (address->m_addr && address->m_len > 0)
849 {
850 addr2->m_addr = (struct sockaddr *)malloc(addr2->m_len);
851 if (addr2->m_addr == NULL)
852 {
853 free(addr2);
854 return NULL;
855 }
856 memcpy(addr2->m_addr, address->m_addr, addr2->m_len);
857 }
858
859 return addr2;
860 }
861
862 void GAddress_destroy(GAddress *address)
863 {
864 assert(address != NULL);
865
866 if (address->m_addr)
867 free(address->m_addr);
868
869 free(address);
870 }
871
872 void GAddress_SetFamily(GAddress *address, GAddressType type)
873 {
874 assert(address != NULL);
875
876 address->m_family = type;
877 }
878
879 GAddressType GAddress_GetFamily(GAddress *address)
880 {
881 assert(address != NULL);
882
883 return address->m_family;
884 }
885
886 wxSocketError _GAddress_translate_from(GAddress *address,
887 struct sockaddr *addr, int len)
888 {
889 address->m_realfamily = addr->sa_family;
890 switch (addr->sa_family)
891 {
892 case AF_INET:
893 address->m_family = wxSOCKET_INET;
894 break;
895 case AF_UNIX:
896 address->m_family = wxSOCKET_UNIX;
897 break;
898 #if wxUSE_IPV6
899 case AF_INET6:
900 address->m_family = wxSOCKET_INET6;
901 break;
902 #endif // wxUSE_IPV6
903 default:
904 {
905 address->m_error = wxSOCKET_INVOP;
906 return wxSOCKET_INVOP;
907 }
908 }
909
910 if (address->m_addr)
911 free(address->m_addr);
912
913 address->m_len = len;
914 address->m_addr = (struct sockaddr *)malloc(len);
915
916 if (address->m_addr == NULL)
917 {
918 address->m_error = wxSOCKET_MEMERR;
919 return wxSOCKET_MEMERR;
920 }
921
922 memcpy(address->m_addr, addr, len);
923
924 return wxSOCKET_NOERROR;
925 }
926
927 wxSocketError _GAddress_translate_to(GAddress *address,
928 struct sockaddr **addr, int *len)
929 {
930 if (!address->m_addr)
931 {
932 address->m_error = wxSOCKET_INVADDR;
933 return wxSOCKET_INVADDR;
934 }
935
936 *len = address->m_len;
937 *addr = (struct sockaddr *)malloc(address->m_len);
938 if (*addr == NULL)
939 {
940 address->m_error = wxSOCKET_MEMERR;
941 return wxSOCKET_MEMERR;
942 }
943
944 memcpy(*addr, address->m_addr, address->m_len);
945 return wxSOCKET_NOERROR;
946 }
947
948 /*
949 * -------------------------------------------------------------------------
950 * Internet address family
951 * -------------------------------------------------------------------------
952 */
953
954 wxSocketError _GAddress_Init_INET(GAddress *address)
955 {
956 address->m_len = sizeof(struct sockaddr_in);
957 address->m_addr = (struct sockaddr *) malloc(address->m_len);
958 if (address->m_addr == NULL)
959 {
960 address->m_error = wxSOCKET_MEMERR;
961 return wxSOCKET_MEMERR;
962 }
963
964 address->m_family = wxSOCKET_INET;
965 address->m_realfamily = PF_INET;
966 ((struct sockaddr_in *)address->m_addr)->sin_family = AF_INET;
967 ((struct sockaddr_in *)address->m_addr)->sin_addr.s_addr = INADDR_ANY;
968
969 return wxSOCKET_NOERROR;
970 }
971
972 wxSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname)
973 {
974 struct hostent *he;
975 struct in_addr *addr;
976
977 assert(address != NULL);
978
979 CHECK_ADDRESS(address, INET);
980
981 addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr);
982
983 /* If it is a numeric host name, convert it now */
984 #if defined(HAVE_INET_ATON)
985 if (inet_aton(hostname, addr) == 0)
986 {
987 #elif defined(HAVE_INET_ADDR)
988 if ( (addr->s_addr = inet_addr(hostname)) == (unsigned)-1 )
989 {
990 #else
991 /* Use gethostbyname by default */
992 #ifndef __WXMAC__
993 int val = 1; /* VA doesn't like constants in conditional expressions */
994 if (val)
995 #endif
996 {
997 #endif
998 struct in_addr *array_addr;
999
1000 /* It is a real name, we solve it */
1001 struct hostent h;
1002 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
1003 struct hostent_data buffer;
1004 #else
1005 char buffer[1024];
1006 #endif
1007 int err;
1008 he = wxGethostbyname_r(hostname, &h, (void*)&buffer, sizeof(buffer), &err);
1009 if (he == NULL)
1010 {
1011 /* Reset to invalid address */
1012 addr->s_addr = INADDR_NONE;
1013 address->m_error = wxSOCKET_NOHOST;
1014 return wxSOCKET_NOHOST;
1015 }
1016
1017 array_addr = (struct in_addr *) *(he->h_addr_list);
1018 addr->s_addr = array_addr[0].s_addr;
1019 }
1020
1021 return wxSOCKET_NOERROR;
1022 }
1023
1024
1025 wxSocketError GAddress_INET_SetBroadcastAddress(GAddress *address)
1026 {
1027 return GAddress_INET_SetHostAddress(address, INADDR_BROADCAST);
1028 }
1029
1030 wxSocketError GAddress_INET_SetAnyAddress(GAddress *address)
1031 {
1032 return GAddress_INET_SetHostAddress(address, INADDR_ANY);
1033 }
1034
1035 wxSocketError GAddress_INET_SetHostAddress(GAddress *address,
1036 unsigned long hostaddr)
1037 {
1038 struct in_addr *addr;
1039
1040 assert(address != NULL);
1041
1042 CHECK_ADDRESS(address, INET);
1043
1044 addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr);
1045 addr->s_addr = htonl(hostaddr);
1046
1047 return wxSOCKET_NOERROR;
1048 }
1049
1050 wxSocketError GAddress_INET_SetPortName(GAddress *address, const char *port,
1051 const char *protocol)
1052 {
1053 struct servent *se;
1054 struct sockaddr_in *addr;
1055
1056 assert(address != NULL);
1057 CHECK_ADDRESS(address, INET);
1058
1059 if (!port)
1060 {
1061 address->m_error = wxSOCKET_INVPORT;
1062 return wxSOCKET_INVPORT;
1063 }
1064
1065 #if defined(HAVE_FUNC_GETSERVBYNAME_R_4)
1066 struct servent_data buffer;
1067 #else
1068 char buffer[1024];
1069 #endif
1070 struct servent serv;
1071 se = wxGetservbyname_r(port, protocol, &serv,
1072 (void*)&buffer, sizeof(buffer));
1073 if (!se)
1074 {
1075 /* the cast to int suppresses compiler warnings about subscript having the
1076 type char */
1077 if (isdigit((int)port[0]))
1078 {
1079 int port_int;
1080
1081 port_int = atoi(port);
1082 addr = (struct sockaddr_in *)address->m_addr;
1083 addr->sin_port = htons(port_int);
1084 return wxSOCKET_NOERROR;
1085 }
1086
1087 address->m_error = wxSOCKET_INVPORT;
1088 return wxSOCKET_INVPORT;
1089 }
1090
1091 addr = (struct sockaddr_in *)address->m_addr;
1092 addr->sin_port = se->s_port;
1093
1094 return wxSOCKET_NOERROR;
1095 }
1096
1097 wxSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port)
1098 {
1099 struct sockaddr_in *addr;
1100
1101 assert(address != NULL);
1102 CHECK_ADDRESS(address, INET);
1103
1104 addr = (struct sockaddr_in *)address->m_addr;
1105 addr->sin_port = htons(port);
1106
1107 return wxSOCKET_NOERROR;
1108 }
1109
1110 wxSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname, size_t sbuf)
1111 {
1112 struct hostent *he;
1113 char *addr_buf;
1114 struct sockaddr_in *addr;
1115
1116 assert(address != NULL);
1117 CHECK_ADDRESS(address, INET);
1118
1119 addr = (struct sockaddr_in *)address->m_addr;
1120 addr_buf = (char *)&(addr->sin_addr);
1121
1122 struct hostent temphost;
1123 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
1124 struct hostent_data buffer;
1125 #else
1126 char buffer[1024];
1127 #endif
1128 int err;
1129 he = wxGethostbyaddr_r(addr_buf, sizeof(addr->sin_addr), AF_INET, &temphost,
1130 (void*)&buffer, sizeof(buffer), &err);
1131 if (he == NULL)
1132 {
1133 address->m_error = wxSOCKET_NOHOST;
1134 return wxSOCKET_NOHOST;
1135 }
1136
1137 strncpy(hostname, he->h_name, sbuf);
1138
1139 return wxSOCKET_NOERROR;
1140 }
1141
1142 unsigned long GAddress_INET_GetHostAddress(GAddress *address)
1143 {
1144 struct sockaddr_in *addr;
1145
1146 assert(address != NULL);
1147 CHECK_ADDRESS_RETVAL(address, INET, 0);
1148
1149 addr = (struct sockaddr_in *)address->m_addr;
1150
1151 return ntohl(addr->sin_addr.s_addr);
1152 }
1153
1154 unsigned short GAddress_INET_GetPort(GAddress *address)
1155 {
1156 struct sockaddr_in *addr;
1157
1158 assert(address != NULL);
1159 CHECK_ADDRESS_RETVAL(address, INET, 0);
1160
1161 addr = (struct sockaddr_in *)address->m_addr;
1162 return ntohs(addr->sin_port);
1163 }
1164
1165 #if wxUSE_IPV6
1166 /*
1167 * -------------------------------------------------------------------------
1168 * Internet IPv6 address family
1169 * -------------------------------------------------------------------------
1170 */
1171
1172 wxSocketError _GAddress_Init_INET6(GAddress *address)
1173 {
1174 struct in6_addr any_address = IN6ADDR_ANY_INIT;
1175 address->m_len = sizeof(struct sockaddr_in6);
1176 address->m_addr = (struct sockaddr *) malloc(address->m_len);
1177 if (address->m_addr == NULL)
1178 {
1179 address->m_error = wxSOCKET_MEMERR;
1180 return wxSOCKET_MEMERR;
1181 }
1182 memset(address->m_addr,0,address->m_len);
1183
1184 address->m_family = wxSOCKET_INET6;
1185 address->m_realfamily = AF_INET6;
1186 ((struct sockaddr_in6 *)address->m_addr)->sin6_family = AF_INET6;
1187 ((struct sockaddr_in6 *)address->m_addr)->sin6_addr = any_address;
1188
1189 return wxSOCKET_NOERROR;
1190 }
1191
1192 wxSocketError GAddress_INET6_SetHostName(GAddress *address, const char *hostname)
1193 {
1194 assert(address != NULL);
1195 CHECK_ADDRESS(address, INET6);
1196
1197 addrinfo hints;
1198 memset( & hints, 0, sizeof( hints ) );
1199 hints.ai_family = AF_INET6;
1200 addrinfo * info = 0;
1201 if ( getaddrinfo( hostname, "0", & hints, & info ) || ! info )
1202 {
1203 address->m_error = wxSOCKET_NOHOST;
1204 return wxSOCKET_NOHOST;
1205 }
1206
1207 memcpy( address->m_addr, info->ai_addr, info->ai_addrlen );
1208 freeaddrinfo( info );
1209 return wxSOCKET_NOERROR;
1210 }
1211
1212 wxSocketError GAddress_INET6_SetAnyAddress(GAddress *address)
1213 {
1214 assert(address != NULL);
1215
1216 CHECK_ADDRESS(address, INET6);
1217
1218 struct in6_addr addr;
1219 memset( & addr, 0, sizeof( addr ) );
1220 return GAddress_INET6_SetHostAddress(address, addr);
1221 }
1222 wxSocketError GAddress_INET6_SetHostAddress(GAddress *address,
1223 struct in6_addr hostaddr)
1224 {
1225 assert(address != NULL);
1226
1227 CHECK_ADDRESS(address, INET6);
1228
1229 ((struct sockaddr_in6 *)address->m_addr)->sin6_addr = hostaddr;
1230
1231 return wxSOCKET_NOERROR;
1232 }
1233
1234 wxSocketError GAddress_INET6_SetPortName(GAddress *address, const char *port,
1235 const char *protocol)
1236 {
1237 struct servent *se;
1238 struct sockaddr_in6 *addr;
1239
1240 assert(address != NULL);
1241 CHECK_ADDRESS(address, INET6);
1242
1243 if (!port)
1244 {
1245 address->m_error = wxSOCKET_INVPORT;
1246 return wxSOCKET_INVPORT;
1247 }
1248
1249 se = getservbyname(port, protocol);
1250 if (!se)
1251 {
1252 if (isdigit(port[0]))
1253 {
1254 int port_int;
1255
1256 port_int = atoi(port);
1257 addr = (struct sockaddr_in6 *)address->m_addr;
1258 addr->sin6_port = htons((u_short) port_int);
1259 return wxSOCKET_NOERROR;
1260 }
1261
1262 address->m_error = wxSOCKET_INVPORT;
1263 return wxSOCKET_INVPORT;
1264 }
1265
1266 addr = (struct sockaddr_in6 *)address->m_addr;
1267 addr->sin6_port = se->s_port;
1268
1269 return wxSOCKET_NOERROR;
1270 }
1271
1272 wxSocketError GAddress_INET6_SetPort(GAddress *address, unsigned short port)
1273 {
1274 struct sockaddr_in6 *addr;
1275
1276 assert(address != NULL);
1277 CHECK_ADDRESS(address, INET6);
1278
1279 addr = (struct sockaddr_in6 *)address->m_addr;
1280 addr->sin6_port = htons(port);
1281
1282 return wxSOCKET_NOERROR;
1283 }
1284
1285 wxSocketError GAddress_INET6_GetHostName(GAddress *address, char *hostname, size_t sbuf)
1286 {
1287 struct hostent *he;
1288 char *addr_buf;
1289 struct sockaddr_in6 *addr;
1290
1291 assert(address != NULL);
1292 CHECK_ADDRESS(address, INET6);
1293
1294 addr = (struct sockaddr_in6 *)address->m_addr;
1295 addr_buf = (char *)&(addr->sin6_addr);
1296
1297 he = gethostbyaddr(addr_buf, sizeof(addr->sin6_addr), AF_INET6);
1298 if (he == NULL)
1299 {
1300 address->m_error = wxSOCKET_NOHOST;
1301 return wxSOCKET_NOHOST;
1302 }
1303
1304 strncpy(hostname, he->h_name, sbuf);
1305
1306 return wxSOCKET_NOERROR;
1307 }
1308
1309 wxSocketError GAddress_INET6_GetHostAddress(GAddress *address,struct in6_addr *hostaddr)
1310 {
1311 assert(address != NULL);
1312 assert(hostaddr != NULL);
1313 CHECK_ADDRESS_RETVAL(address, INET6, wxSOCKET_INVADDR);
1314 *hostaddr = ( (struct sockaddr_in6 *)address->m_addr )->sin6_addr;
1315 return wxSOCKET_NOERROR;
1316 }
1317
1318 unsigned short GAddress_INET6_GetPort(GAddress *address)
1319 {
1320 assert(address != NULL);
1321 CHECK_ADDRESS_RETVAL(address, INET6, 0);
1322
1323 return ntohs( ((struct sockaddr_in6 *)address->m_addr)->sin6_port );
1324 }
1325
1326 #endif // wxUSE_IPV6
1327
1328 /*
1329 * -------------------------------------------------------------------------
1330 * Unix address family
1331 * -------------------------------------------------------------------------
1332 */
1333
1334 #ifndef __VISAGECPP__
1335 wxSocketError _GAddress_Init_UNIX(GAddress *address)
1336 {
1337 address->m_len = sizeof(struct sockaddr_un);
1338 address->m_addr = (struct sockaddr *)malloc(address->m_len);
1339 if (address->m_addr == NULL)
1340 {
1341 address->m_error = wxSOCKET_MEMERR;
1342 return wxSOCKET_MEMERR;
1343 }
1344
1345 address->m_family = wxSOCKET_UNIX;
1346 address->m_realfamily = PF_UNIX;
1347 ((struct sockaddr_un *)address->m_addr)->sun_family = AF_UNIX;
1348 ((struct sockaddr_un *)address->m_addr)->sun_path[0] = 0;
1349
1350 return wxSOCKET_NOERROR;
1351 }
1352
1353 #define UNIX_SOCK_PATHLEN (sizeof(addr->sun_path)/sizeof(addr->sun_path[0]))
1354
1355 wxSocketError GAddress_UNIX_SetPath(GAddress *address, const char *path)
1356 {
1357 struct sockaddr_un *addr;
1358
1359 assert(address != NULL);
1360
1361 CHECK_ADDRESS(address, UNIX);
1362
1363 addr = ((struct sockaddr_un *)address->m_addr);
1364 strncpy(addr->sun_path, path, UNIX_SOCK_PATHLEN);
1365 addr->sun_path[UNIX_SOCK_PATHLEN - 1] = '\0';
1366
1367 return wxSOCKET_NOERROR;
1368 }
1369
1370 wxSocketError GAddress_UNIX_GetPath(GAddress *address, char *path, size_t sbuf)
1371 {
1372 struct sockaddr_un *addr;
1373
1374 assert(address != NULL);
1375 CHECK_ADDRESS(address, UNIX);
1376
1377 addr = (struct sockaddr_un *)address->m_addr;
1378
1379 strncpy(path, addr->sun_path, sbuf);
1380
1381 return wxSOCKET_NOERROR;
1382 }
1383 #endif /* !defined(__VISAGECPP__) */
1384
1385 #endif /* wxUSE_SOCKETS */