]> git.saurik.com Git - wxWidgets.git/blob - src/unix/gsocket.cpp
95d59e581b9fbe13304db7305ca5dacd1a3f58be
[wxWidgets.git] / src / unix / gsocket.cpp
1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket) for WX
3 * Name: gsocket.c
4 * Copyright: (c) Guilhem Lavaux
5 * Licence: wxWindows Licence
6 * Authors: David Elliott (C++ conversion, maintainer)
7 * Guilhem Lavaux,
8 * Guillermo Rodriguez Garcia <guille@iies.es>
9 * Purpose: GSocket main Unix and OS/2 file
10 * Licence: The wxWindows licence
11 * CVSID: $Id$
12 * -------------------------------------------------------------------------
13 */
14
15 #include "wx/wxprec.h"
16
17 #if wxUSE_SOCKETS
18
19 #include "wx/gsocket.h"
20
21 #include "wx/private/fd.h"
22 #include "wx/private/socket.h"
23 #include "wx/private/gsocketiohandler.h"
24
25 #if defined(__VISAGECPP__)
26 #define BSD_SELECT /* use Berkeley Sockets select */
27 #endif
28
29 #if defined(__WATCOMC__)
30 #include <errno.h>
31 #include <nerrno.h>
32 #endif
33
34 #include <assert.h>
35 #include <sys/types.h>
36 #ifdef __VISAGECPP__
37 #include <string.h>
38 #include <sys/time.h>
39 #include <types.h>
40 #include <netinet/in.h>
41 #endif
42 #include <netdb.h>
43 #include <sys/ioctl.h>
44
45 #ifdef HAVE_SYS_SELECT_H
46 # include <sys/select.h>
47 #endif
48
49 #ifdef __VMS__
50 #include <socket.h>
51 struct sockaddr_un
52 {
53 u_char sun_len; /* sockaddr len including null */
54 u_char sun_family; /* AF_UNIX */
55 char sun_path[108]; /* path name (gag) */
56 };
57 #else
58 #include <sys/socket.h>
59 #include <sys/un.h>
60 #endif
61
62 #ifndef __VISAGECPP__
63 #include <sys/time.h>
64 #include <netinet/in.h>
65 #include <arpa/inet.h>
66 #include <errno.h>
67 #include <string.h>
68 #include <unistd.h>
69 #else
70 #include <nerrno.h>
71 # if __IBMCPP__ < 400
72 #include <machine/endian.h>
73 #include <socket.h>
74 #include <ioctl.h>
75 #include <select.h>
76 #include <unistd.h>
77
78 #define EBADF SOCEBADF
79
80 # ifdef min
81 # undef min
82 # endif
83 # else
84 #include <sys/socket.h>
85 #include <sys/ioctl.h>
86 #include <sys/select.h>
87
88 #define close(a) soclose(a)
89 #define select(a,b,c,d,e) bsdselect(a,b,c,d,e)
90 int _System bsdselect(int,
91 struct fd_set *,
92 struct fd_set *,
93 struct fd_set *,
94 struct timeval *);
95 int _System soclose(int);
96 # endif
97 #endif
98 #ifdef __EMX__
99 #include <sys/select.h>
100 #endif
101
102 #include <stdio.h>
103 #include <stdlib.h>
104 #include <stddef.h>
105 #include <ctype.h>
106 #ifdef sun
107 # include <sys/filio.h>
108 #endif
109 #ifdef sgi
110 # include <bstring.h>
111 #endif
112 #ifdef _AIX
113 # include <strings.h>
114 #endif
115 #include <signal.h>
116
117 #ifndef WX_SOCKLEN_T
118
119 #ifdef VMS
120 # define WX_SOCKLEN_T unsigned int
121 #else
122 # ifdef __GLIBC__
123 # if __GLIBC__ == 2
124 # define WX_SOCKLEN_T socklen_t
125 # endif
126 # elif defined(__WXMAC__)
127 # define WX_SOCKLEN_T socklen_t
128 # else
129 # define WX_SOCKLEN_T int
130 # endif
131 #endif
132
133 #endif /* SOCKLEN_T */
134
135 #ifndef SOCKOPTLEN_T
136 #define SOCKOPTLEN_T WX_SOCKLEN_T
137 #endif
138
139 /* UnixWare reportedly needs this for FIONBIO definition */
140 #ifdef __UNIXWARE__
141 #include <sys/filio.h>
142 #endif
143
144 /*
145 * INADDR_BROADCAST is identical to INADDR_NONE which is not defined
146 * on all systems. INADDR_BROADCAST should be fine to indicate an error.
147 */
148 #ifndef INADDR_NONE
149 #define INADDR_NONE INADDR_BROADCAST
150 #endif
151
152 #if defined(__VISAGECPP__) || defined(__WATCOMC__)
153
154 #define MASK_SIGNAL() {
155 #define UNMASK_SIGNAL() }
156
157 #else
158 extern "C" { typedef void (*wxSigHandler)(int); }
159
160 #define MASK_SIGNAL() \
161 { \
162 wxSigHandler old_handler = signal(SIGPIPE, SIG_IGN);
163
164 #define UNMASK_SIGNAL() \
165 signal(SIGPIPE, old_handler); \
166 }
167
168 #endif
169
170 /* If a SIGPIPE is issued by a socket call on a remotely closed socket,
171 the program will "crash" unless it explicitly handles the SIGPIPE.
172 By using MSG_NOSIGNAL, the SIGPIPE is suppressed. Later, we will
173 use SO_NOSIGPIPE (if available), the BSD equivalent. */
174 #ifdef MSG_NOSIGNAL
175 # define GSOCKET_MSG_NOSIGNAL MSG_NOSIGNAL
176 #else /* MSG_NOSIGNAL not available (FreeBSD including OS X) */
177 # define GSOCKET_MSG_NOSIGNAL 0
178 #endif /* MSG_NOSIGNAL */
179
180 #if wxUSE_THREADS && (defined(HAVE_GETHOSTBYNAME) || defined(HAVE_GETSERVBYNAME))
181 # include "wx/thread.h"
182 #endif
183
184 #if defined(HAVE_GETHOSTBYNAME)
185 static struct hostent * deepCopyHostent(struct hostent *h,
186 const struct hostent *he,
187 char *buffer, int size, int *err)
188 {
189 /* copy old structure */
190 memcpy(h, he, sizeof(struct hostent));
191
192 /* copy name */
193 int len = strlen(h->h_name);
194 if (len > size)
195 {
196 *err = ENOMEM;
197 return NULL;
198 }
199 memcpy(buffer, h->h_name, len);
200 buffer[len] = '\0';
201 h->h_name = buffer;
202
203 /* track position in the buffer */
204 int pos = len + 1;
205
206 /* reuse len to store address length */
207 len = h->h_length;
208
209 /* ensure pointer alignment */
210 unsigned int misalign = sizeof(char *) - pos%sizeof(char *);
211 if(misalign < sizeof(char *))
212 pos += misalign;
213
214 /* leave space for pointer list */
215 char **p = h->h_addr_list, **q;
216 char **h_addr_list = (char **)(buffer + pos);
217 while(*(p++) != 0)
218 pos += sizeof(char *);
219
220 /* copy addresses and fill new pointer list */
221 for (p = h->h_addr_list, q = h_addr_list; *p != 0; p++, q++)
222 {
223 if (size < pos + len)
224 {
225 *err = ENOMEM;
226 return NULL;
227 }
228 memcpy(buffer + pos, *p, len); /* copy content */
229 *q = buffer + pos; /* set copied pointer to copied content */
230 pos += len;
231 }
232 *++q = 0; /* null terminate the pointer list */
233 h->h_addr_list = h_addr_list; /* copy pointer to pointers */
234
235 /* ensure word alignment of pointers */
236 misalign = sizeof(char *) - pos%sizeof(char *);
237 if(misalign < sizeof(char *))
238 pos += misalign;
239
240 /* leave space for pointer list */
241 p = h->h_aliases;
242 char **h_aliases = (char **)(buffer + pos);
243 while(*(p++) != 0)
244 pos += sizeof(char *);
245
246 /* copy aliases and fill new pointer list */
247 for (p = h->h_aliases, q = h_aliases; *p != 0; p++, q++)
248 {
249 len = strlen(*p);
250 if (size <= pos + len)
251 {
252 *err = ENOMEM;
253 return NULL;
254 }
255 memcpy(buffer + pos, *p, len); /* copy content */
256 buffer[pos + len] = '\0';
257 *q = buffer + pos; /* set copied pointer to copied content */
258 pos += len + 1;
259 }
260 *++q = 0; /* null terminate the pointer list */
261 h->h_aliases = h_aliases; /* copy pointer to pointers */
262
263 return h;
264 }
265 #endif
266
267 #if defined(HAVE_GETHOSTBYNAME) && wxUSE_THREADS
268 static wxMutex nameLock;
269 #endif
270 struct hostent * wxGethostbyname_r(const char *hostname, struct hostent *h,
271 void *buffer, int size, int *err)
272
273 {
274 struct hostent *he = NULL;
275 *err = 0;
276 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_6)
277 if (gethostbyname_r(hostname, h, (char*)buffer, size, &he, err))
278 he = NULL;
279 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_5)
280 he = gethostbyname_r(hostname, h, (char*)buffer, size, err);
281 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
282 if (gethostbyname_r(hostname, h, (struct hostent_data*) buffer))
283 {
284 he = NULL;
285 *err = h_errno;
286 }
287 else
288 he = h;
289 #elif defined(HAVE_GETHOSTBYNAME)
290 #if wxUSE_THREADS
291 wxMutexLocker locker(nameLock);
292 #endif
293 he = gethostbyname(hostname);
294 if (!he)
295 *err = h_errno;
296 else
297 he = deepCopyHostent(h, he, (char*)buffer, size, err);
298 #endif
299 return he;
300 }
301
302 #if defined(HAVE_GETHOSTBYNAME) && wxUSE_THREADS
303 static wxMutex addrLock;
304 #endif
305 struct hostent * wxGethostbyaddr_r(const char *addr_buf, int buf_size,
306 int proto, struct hostent *h,
307 void *buffer, int size, int *err)
308 {
309 struct hostent *he = NULL;
310 *err = 0;
311 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_6)
312 if (gethostbyaddr_r(addr_buf, buf_size, proto, h,
313 (char*)buffer, size, &he, err))
314 he = NULL;
315 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_5)
316 he = gethostbyaddr_r(addr_buf, buf_size, proto, h, (char*)buffer, size, err);
317 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
318 if (gethostbyaddr_r(addr_buf, buf_size, proto, h,
319 (struct hostent_data*) buffer))
320 {
321 he = NULL;
322 *err = h_errno;
323 }
324 else
325 he = h;
326 #elif defined(HAVE_GETHOSTBYNAME)
327 #if wxUSE_THREADS
328 wxMutexLocker locker(addrLock);
329 #endif
330 he = gethostbyaddr(addr_buf, buf_size, proto);
331 if (!he)
332 *err = h_errno;
333 else
334 he = deepCopyHostent(h, he, (char*)buffer, size, err);
335 #endif
336 return he;
337 }
338
339 #if defined(HAVE_GETSERVBYNAME)
340 static struct servent * deepCopyServent(struct servent *s,
341 const struct servent *se,
342 char *buffer, int size)
343 {
344 /* copy plain old structure */
345 memcpy(s, se, sizeof(struct servent));
346
347 /* copy name */
348 int len = strlen(s->s_name);
349 if (len >= size)
350 {
351 return NULL;
352 }
353 memcpy(buffer, s->s_name, len);
354 buffer[len] = '\0';
355 s->s_name = buffer;
356
357 /* track position in the buffer */
358 int pos = len + 1;
359
360 /* copy protocol */
361 len = strlen(s->s_proto);
362 if (pos + len >= size)
363 {
364 return NULL;
365 }
366 memcpy(buffer + pos, s->s_proto, len);
367 buffer[pos + len] = '\0';
368 s->s_proto = buffer + pos;
369
370 /* track position in the buffer */
371 pos += len + 1;
372
373 /* ensure pointer alignment */
374 unsigned int misalign = sizeof(char *) - pos%sizeof(char *);
375 if(misalign < sizeof(char *))
376 pos += misalign;
377
378 /* leave space for pointer list */
379 char **p = s->s_aliases, **q;
380 char **s_aliases = (char **)(buffer + pos);
381 while(*(p++) != 0)
382 pos += sizeof(char *);
383
384 /* copy addresses and fill new pointer list */
385 for (p = s->s_aliases, q = s_aliases; *p != 0; p++, q++){
386 len = strlen(*p);
387 if (size <= pos + len)
388 {
389 return NULL;
390 }
391 memcpy(buffer + pos, *p, len); /* copy content */
392 buffer[pos + len] = '\0';
393 *q = buffer + pos; /* set copied pointer to copied content */
394 pos += len + 1;
395 }
396 *++q = 0; /* null terminate the pointer list */
397 s->s_aliases = s_aliases; /* copy pointer to pointers */
398 return s;
399 }
400 #endif
401
402 #if defined(HAVE_GETSERVBYNAME) && wxUSE_THREADS
403 static wxMutex servLock;
404 #endif
405 struct servent *wxGetservbyname_r(const char *port, const char *protocol,
406 struct servent *serv, void *buffer, int size)
407 {
408 struct servent *se = NULL;
409 #if defined(HAVE_FUNC_GETSERVBYNAME_R_6)
410 if (getservbyname_r(port, protocol, serv, (char*)buffer, size, &se))
411 se = NULL;
412 #elif defined(HAVE_FUNC_GETSERVBYNAME_R_5)
413 se = getservbyname_r(port, protocol, serv, (char*)buffer, size);
414 #elif defined(HAVE_FUNC_GETSERVBYNAME_R_4)
415 if (getservbyname_r(port, protocol, serv, (struct servent_data*) buffer))
416 se = NULL;
417 else
418 se = serv;
419 #elif defined(HAVE_GETSERVBYNAME)
420 #if wxUSE_THREADS
421 wxMutexLocker locker(servLock);
422 #endif
423 se = getservbyname(port, protocol);
424 if (se)
425 se = deepCopyServent(serv, se, (char*)buffer, size);
426 #endif
427 return se;
428 }
429
430 /* debugging helpers */
431 #ifdef __GSOCKET_DEBUG__
432 # define GSocket_Debug(args) printf args
433 #else
434 # define GSocket_Debug(args)
435 #endif /* __GSOCKET_DEBUG__ */
436
437 /* Table of GUI-related functions. We must call them indirectly because
438 * of wxBase and GUI separation: */
439
440 bool GSocket_Init()
441 {
442 GSocketManager * const manager = GSocketManager::Get();
443 return manager && manager->OnInit();
444 }
445
446 void GSocket_Cleanup()
447 {
448 GSocketManager * const manager = GSocketManager::Get();
449 if ( manager )
450 manager->OnExit();
451 }
452
453 /* Constructors / Destructors for GSocket */
454
455 GSocket::GSocket(wxSocketBase& wxsocket)
456 : GSocketBase(wxsocket)
457 {
458 m_handler = NULL;
459
460 m_gui_dependent = NULL;
461 m_use_events = false;
462 }
463
464 void GSocket::Close()
465 {
466 if (m_use_events)
467 DisableEvents();
468
469 /* When running on OS X, the gsockosx implementation of GSocketGUIFunctionsTable
470 will close the socket during Disable_Events. However, it will only do this
471 if it is being used. That is, it won't do it in a console program. To
472 ensure we get the right behavior, we have gsockosx set m_fd = INVALID_SOCKET
473 if it has closed the socket which indicates to us (at runtime, instead of
474 at compile time as this had been before) that the socket has already
475 been closed.
476 */
477 if(m_fd != INVALID_SOCKET)
478 close(m_fd);
479 m_fd = INVALID_SOCKET;
480 }
481
482 GSocket::~GSocket()
483 {
484 delete m_handler;
485 }
486
487 /* GSocket_Shutdown:
488 * Disallow further read/write operations on this socket, close
489 * the fd and disable all callbacks.
490 */
491 void GSocket::Shutdown()
492 {
493 /* Don't allow events to fire after socket has been closed */
494 if (m_use_events)
495 DisableEvents();
496
497 GSocketBase::Shutdown();
498 }
499
500 /* Server specific parts */
501
502 /* GSocket_SetServer:
503 * Sets up this socket as a server. The local address must have been
504 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
505 * Returns GSOCK_NOERROR on success, one of the following otherwise:
506 *
507 * Error codes:
508 * GSOCK_INVSOCK - the socket is in use.
509 * GSOCK_INVADDR - the local address has not been set.
510 * GSOCK_IOERR - low-level error.
511 */
512 GSocketError GSocket::SetServer()
513 {
514 int arg = 1;
515
516 assert(this);
517
518 /* must not be in use */
519 if (m_fd != INVALID_SOCKET)
520 {
521 m_error = GSOCK_INVSOCK;
522 return GSOCK_INVSOCK;
523 }
524
525 /* the local addr must have been set */
526 if (!m_local)
527 {
528 m_error = GSOCK_INVADDR;
529 return GSOCK_INVADDR;
530 }
531
532 /* Initialize all fields */
533 m_stream = true;
534 m_server = true;
535
536 /* Create the socket */
537 m_fd = socket(m_local->m_realfamily, SOCK_STREAM, 0);
538
539 if (m_fd == INVALID_SOCKET)
540 {
541 m_error = GSOCK_IOERR;
542 return GSOCK_IOERR;
543 }
544
545 /* FreeBSD variants can't use MSG_NOSIGNAL, and instead use a socket option */
546 #ifdef SO_NOSIGPIPE
547 setsockopt(m_fd, SOL_SOCKET, SO_NOSIGPIPE, (const char*)&arg, sizeof(arg));
548 #endif
549
550 ioctl(m_fd, FIONBIO, &arg);
551 if (m_use_events)
552 EnableEvents();
553
554 /* allow a socket to re-bind if the socket is in the TIME_WAIT
555 state after being previously closed.
556 */
557 if (m_reusable)
558 {
559 setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&arg, sizeof(arg));
560 #ifdef SO_REUSEPORT
561 setsockopt(m_fd, SOL_SOCKET, SO_REUSEPORT, (const char*)&arg, sizeof(arg));
562 #endif
563 }
564
565 /* Bind to the local address,
566 * retrieve the actual address bound,
567 * and listen up to 5 connections.
568 */
569 if ((bind(m_fd, m_local->m_addr, m_local->m_len) != 0) ||
570 (getsockname(m_fd,
571 m_local->m_addr,
572 (WX_SOCKLEN_T *) &m_local->m_len) != 0) ||
573 (listen(m_fd, 5) != 0))
574 {
575 Close();
576 m_error = GSOCK_IOERR;
577 return GSOCK_IOERR;
578 }
579
580 return GSOCK_NOERROR;
581 }
582
583 /* GSocket_WaitConnection:
584 * Waits for an incoming client connection. Returns a pointer to
585 * a GSocket object, or NULL if there was an error, in which case
586 * the last error field will be updated for the calling GSocket.
587 *
588 * Error codes (set in the calling GSocket)
589 * GSOCK_INVSOCK - the socket is not valid or not a server.
590 * GSOCK_TIMEDOUT - timeout, no incoming connections.
591 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
592 * GSOCK_MEMERR - couldn't allocate memory.
593 * GSOCK_IOERR - low-level error.
594 */
595 GSocket *GSocket::WaitConnection(wxSocketBase& wxsocket)
596 {
597 wxSockAddr from;
598 WX_SOCKLEN_T fromlen = sizeof(from);
599 GSocket *connection;
600 GSocketError err;
601 int arg = 1;
602
603 assert(this);
604
605 /* If the socket has already been created, we exit immediately */
606 if (m_fd == INVALID_SOCKET || !m_server)
607 {
608 m_error = GSOCK_INVSOCK;
609 return NULL;
610 }
611
612 /* Create a GSocket object for the new connection */
613 connection = GSocket::Create(wxsocket);
614
615 if (!connection)
616 {
617 m_error = GSOCK_MEMERR;
618 return NULL;
619 }
620
621 /* Wait for a connection (with timeout) */
622 if (Input_Timeout() == GSOCK_TIMEDOUT)
623 {
624 delete connection;
625 /* m_error set by _GSocket_Input_Timeout */
626 return NULL;
627 }
628
629 connection->m_fd = accept(m_fd, (sockaddr*)&from, (WX_SOCKLEN_T *) &fromlen);
630
631 /* Reenable CONNECTION events */
632 Enable(GSOCK_CONNECTION);
633
634 if (connection->m_fd == INVALID_SOCKET)
635 {
636 if (errno == EWOULDBLOCK)
637 m_error = GSOCK_WOULDBLOCK;
638 else
639 m_error = GSOCK_IOERR;
640
641 delete connection;
642 return NULL;
643 }
644
645 /* Initialize all fields */
646 connection->m_server = false;
647 connection->m_stream = true;
648
649 /* Setup the peer address field */
650 connection->m_peer = GAddress_new();
651 if (!connection->m_peer)
652 {
653 delete connection;
654 m_error = GSOCK_MEMERR;
655 return NULL;
656 }
657
658 err = _GAddress_translate_from(connection->m_peer, (sockaddr*)&from, fromlen);
659 if (err != GSOCK_NOERROR)
660 {
661 delete connection;
662 m_error = err;
663 return NULL;
664 }
665
666 #if defined(__EMX__) || defined(__VISAGECPP__)
667 ioctl(connection->m_fd, FIONBIO, (char*)&arg, sizeof(arg));
668 #else
669 ioctl(connection->m_fd, FIONBIO, &arg);
670 #endif
671 if (m_use_events)
672 connection->Notify(true);
673
674 return connection;
675 }
676
677 void GSocket::Notify(bool flag)
678 {
679 if (flag == m_use_events)
680 return;
681 m_use_events = flag;
682 EnableEvents(flag);
683 }
684
685 void GSocket::EnableEvents(bool flag)
686 {
687 if (flag)
688 GSocketManager::Get()->Enable_Events(this);
689 else
690 GSocketManager::Get()->Disable_Events(this);
691 }
692
693 bool GSocket::SetReusable()
694 {
695 /* socket must not be null, and must not be in use/already bound */
696 if (this && m_fd == INVALID_SOCKET)
697 {
698 m_reusable = true;
699
700 return true;
701 }
702
703 return false;
704 }
705
706 bool GSocket::SetBroadcast()
707 {
708 /* socket must not be in use/already bound */
709 if (m_fd == INVALID_SOCKET) {
710 m_broadcast = true;
711 return true;
712 }
713 return false;
714 }
715
716 bool GSocket::DontDoBind()
717 {
718 /* socket must not be in use/already bound */
719 if (m_fd == INVALID_SOCKET) {
720 m_dobind = false;
721 return true;
722 }
723 return false;
724 }
725
726 /* Client specific parts */
727
728 /* GSocket_Connect:
729 * For stream (connection oriented) sockets, GSocket_Connect() tries
730 * to establish a client connection to a server using the peer address
731 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
732 * connection has been successfully established, or one of the error
733 * codes listed below. Note that for nonblocking sockets, a return
734 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
735 * request can be completed later; you should use GSocket_Select()
736 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
737 * corresponding asynchronous events.
738 *
739 * For datagram (non connection oriented) sockets, GSocket_Connect()
740 * just sets the peer address established with GSocket_SetPeer() as
741 * default destination.
742 *
743 * Error codes:
744 * GSOCK_INVSOCK - the socket is in use or not valid.
745 * GSOCK_INVADDR - the peer address has not been established.
746 * GSOCK_TIMEDOUT - timeout, the connection failed.
747 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
748 * GSOCK_MEMERR - couldn't allocate memory.
749 * GSOCK_IOERR - low-level error.
750 */
751 GSocketError GSocket::Connect(GSocketStream stream)
752 {
753 int err, ret;
754 int arg = 1;
755
756 assert(this);
757
758 /* Enable CONNECTION events (needed for nonblocking connections) */
759 Enable(GSOCK_CONNECTION);
760
761 if (m_fd != INVALID_SOCKET)
762 {
763 m_error = GSOCK_INVSOCK;
764 return GSOCK_INVSOCK;
765 }
766
767 if (!m_peer)
768 {
769 m_error = GSOCK_INVADDR;
770 return GSOCK_INVADDR;
771 }
772
773 /* Streamed or dgram socket? */
774 m_stream = (stream == GSOCK_STREAMED);
775 m_server = false;
776 m_establishing = false;
777
778 /* Create the socket */
779 m_fd = socket(m_peer->m_realfamily,
780 m_stream? SOCK_STREAM : SOCK_DGRAM, 0);
781
782 if (m_fd == INVALID_SOCKET)
783 {
784 m_error = GSOCK_IOERR;
785 return GSOCK_IOERR;
786 }
787
788 /* FreeBSD variants can't use MSG_NOSIGNAL, and instead use a socket option */
789 #ifdef SO_NOSIGPIPE
790 setsockopt(m_fd, SOL_SOCKET, SO_NOSIGPIPE, (const char*)&arg, sizeof(arg));
791 #endif
792
793 #if defined(__EMX__) || defined(__VISAGECPP__)
794 ioctl(m_fd, FIONBIO, (char*)&arg, sizeof(arg));
795 #else
796 ioctl(m_fd, FIONBIO, &arg);
797 #endif
798
799 // If the reuse flag is set, use the applicable socket reuse flags(s)
800 if (m_reusable)
801 {
802 setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&arg, sizeof(arg));
803 #ifdef SO_REUSEPORT
804 setsockopt(m_fd, SOL_SOCKET, SO_REUSEPORT, (const char*)&arg, sizeof(arg));
805 #endif
806 }
807
808 if (m_initialRecvBufferSize >= 0)
809 setsockopt(m_fd, SOL_SOCKET, SO_RCVBUF, (const char*)&m_initialRecvBufferSize, sizeof(m_initialRecvBufferSize));
810 if (m_initialSendBufferSize >= 0)
811 setsockopt(m_fd, SOL_SOCKET, SO_SNDBUF, (const char*)&m_initialSendBufferSize, sizeof(m_initialSendBufferSize));
812
813 // If a local address has been set, then we need to bind to it before calling connect
814 if (m_local && m_local->m_addr)
815 {
816 bind(m_fd, m_local->m_addr, m_local->m_len);
817 }
818
819 /* Connect it to the peer address, with a timeout (see below) */
820 ret = connect(m_fd, m_peer->m_addr, m_peer->m_len);
821
822 /* We only call Enable_Events if we know we aren't shutting down the socket.
823 * NB: Enable_Events needs to be called whether the socket is blocking or
824 * non-blocking, it just shouldn't be called prior to knowing there is a
825 * connection _if_ blocking sockets are being used.
826 * If connect above returns 0, we are already connected and need to make the
827 * call to Enable_Events now.
828 */
829
830 if (m_use_events && (m_non_blocking || ret == 0))
831 EnableEvents();
832
833 if (ret == -1)
834 {
835 err = errno;
836
837 /* If connect failed with EINPROGRESS and the GSocket object
838 * is in blocking mode, we select() for the specified timeout
839 * checking for writability to see if the connection request
840 * completes.
841 */
842 if ((err == EINPROGRESS) && (!m_non_blocking))
843 {
844 if (Output_Timeout() == GSOCK_TIMEDOUT)
845 {
846 Close();
847 /* m_error is set in _GSocket_Output_Timeout */
848 return GSOCK_TIMEDOUT;
849 }
850 else
851 {
852 int error;
853 SOCKOPTLEN_T len = sizeof(error);
854
855 getsockopt(m_fd, SOL_SOCKET, SO_ERROR, (char*) &error, &len);
856 if (m_use_events)
857 EnableEvents();
858
859 if (!error)
860 return GSOCK_NOERROR;
861 }
862 }
863
864 /* If connect failed with EINPROGRESS and the GSocket object
865 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
866 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
867 * this way if the connection completes, a GSOCK_CONNECTION
868 * event will be generated, if enabled.
869 */
870 if ((err == EINPROGRESS) && (m_non_blocking))
871 {
872 m_establishing = true;
873 m_error = GSOCK_WOULDBLOCK;
874 return GSOCK_WOULDBLOCK;
875 }
876
877 /* If connect failed with an error other than EINPROGRESS,
878 * then the call to GSocket_Connect has failed.
879 */
880 Close();
881 m_error = GSOCK_IOERR;
882
883 return GSOCK_IOERR;
884 }
885
886 return GSOCK_NOERROR;
887 }
888
889 /* Datagram sockets */
890
891 /* GSocket_SetNonOriented:
892 * Sets up this socket as a non-connection oriented (datagram) socket.
893 * Before using this function, the local address must have been set
894 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
895 * on success, or one of the following otherwise.
896 *
897 * Error codes:
898 * GSOCK_INVSOCK - the socket is in use.
899 * GSOCK_INVADDR - the local address has not been set.
900 * GSOCK_IOERR - low-level error.
901 */
902 GSocketError GSocket::SetNonOriented()
903 {
904 int arg = 1;
905
906 assert(this);
907
908 if (m_fd != INVALID_SOCKET)
909 {
910 m_error = GSOCK_INVSOCK;
911 return GSOCK_INVSOCK;
912 }
913
914 if (!m_local)
915 {
916 m_error = GSOCK_INVADDR;
917 return GSOCK_INVADDR;
918 }
919
920 /* Initialize all fields */
921 m_stream = false;
922 m_server = false;
923
924 /* Create the socket */
925 m_fd = socket(m_local->m_realfamily, SOCK_DGRAM, 0);
926
927 if (m_fd == INVALID_SOCKET)
928 {
929 m_error = GSOCK_IOERR;
930 return GSOCK_IOERR;
931 }
932 #if defined(__EMX__) || defined(__VISAGECPP__)
933 ioctl(m_fd, FIONBIO, (char*)&arg, sizeof(arg));
934 #else
935 ioctl(m_fd, FIONBIO, &arg);
936 #endif
937 if (m_use_events)
938 EnableEvents();
939
940 if (m_reusable)
941 {
942 setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&arg, sizeof(arg));
943 #ifdef SO_REUSEPORT
944 setsockopt(m_fd, SOL_SOCKET, SO_REUSEPORT, (const char*)&arg, sizeof(arg));
945 #endif
946 }
947
948 if (m_broadcast)
949 {
950 setsockopt(m_fd, SOL_SOCKET, SO_BROADCAST, (const char*)&arg, sizeof(arg));
951 }
952 if (m_dobind)
953 {
954 /* Bind to the local address,
955 * and retrieve the actual address bound.
956 */
957 if ((bind(m_fd, m_local->m_addr, m_local->m_len) != 0) ||
958 (getsockname(m_fd,
959 m_local->m_addr,
960 (WX_SOCKLEN_T *) &m_local->m_len) != 0))
961 {
962 Close();
963 m_error = GSOCK_IOERR;
964 return GSOCK_IOERR;
965 }
966 }
967 return GSOCK_NOERROR;
968 }
969
970 /* Generic IO */
971
972 /* Like recv(), send(), ... */
973 int GSocket::Read(char *buffer, int size)
974 {
975 int ret;
976
977 assert(this);
978
979 if (m_fd == INVALID_SOCKET || m_server)
980 {
981 m_error = GSOCK_INVSOCK;
982 return -1;
983 }
984
985 /* Disable events during query of socket status */
986 Disable(GSOCK_INPUT);
987
988 /* If the socket is blocking, wait for data (with a timeout) */
989 if (Input_Timeout() == GSOCK_TIMEDOUT) {
990 m_error = GSOCK_TIMEDOUT;
991 /* Don't return here immediately, otherwise socket events would not be
992 * re-enabled! */
993 ret = -1;
994 }
995 else
996 {
997 /* Read the data */
998 if (m_stream)
999 ret = Recv_Stream(buffer, size);
1000 else
1001 ret = Recv_Dgram(buffer, size);
1002
1003 /*
1004 * If recv returned zero for a TCP socket (if m_stream == NULL, it's an UDP
1005 * socket and empty datagrams are possible), then the connection has been
1006 * gracefully closed.
1007 *
1008 * Otherwise, recv has returned an error (-1), in which case we have lost
1009 * the socket only if errno does _not_ indicate that there may be more data
1010 * to read.
1011 */
1012 if ((ret == 0) && m_stream)
1013 {
1014 /* Make sure wxSOCKET_LOST event gets sent and shut down the socket */
1015 if (m_use_events)
1016 {
1017 m_detected = GSOCK_LOST_FLAG;
1018 Detected_Read();
1019 return 0;
1020 }
1021 }
1022 else if (ret == -1)
1023 {
1024 if ((errno == EWOULDBLOCK) || (errno == EAGAIN))
1025 m_error = GSOCK_WOULDBLOCK;
1026 else
1027 m_error = GSOCK_IOERR;
1028 }
1029 }
1030
1031 /* Enable events again now that we are done processing */
1032 Enable(GSOCK_INPUT);
1033
1034 return ret;
1035 }
1036
1037 int GSocket::Write(const char *buffer, int size)
1038 {
1039 int ret;
1040
1041 assert(this);
1042
1043 GSocket_Debug(( "GSocket_Write #1, size %d\n", size ));
1044
1045 if (m_fd == INVALID_SOCKET || m_server)
1046 {
1047 m_error = GSOCK_INVSOCK;
1048 return -1;
1049 }
1050
1051 GSocket_Debug(( "GSocket_Write #2, size %d\n", size ));
1052
1053 /* If the socket is blocking, wait for writability (with a timeout) */
1054 if (Output_Timeout() == GSOCK_TIMEDOUT)
1055 return -1;
1056
1057 GSocket_Debug(( "GSocket_Write #3, size %d\n", size ));
1058
1059 /* Write the data */
1060 if (m_stream)
1061 ret = Send_Stream(buffer, size);
1062 else
1063 ret = Send_Dgram(buffer, size);
1064
1065 GSocket_Debug(( "GSocket_Write #4, size %d\n", size ));
1066
1067 if (ret == -1)
1068 {
1069 if ((errno == EWOULDBLOCK) || (errno == EAGAIN))
1070 {
1071 m_error = GSOCK_WOULDBLOCK;
1072 GSocket_Debug(( "GSocket_Write error WOULDBLOCK\n" ));
1073 }
1074 else
1075 {
1076 m_error = GSOCK_IOERR;
1077 GSocket_Debug(( "GSocket_Write error IOERR\n" ));
1078 }
1079
1080 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
1081 * in MSW). Once the first OUTPUT event is received, users can assume
1082 * that the socket is writable until a read operation fails. Only then
1083 * will further OUTPUT events be posted.
1084 */
1085 Enable(GSOCK_OUTPUT);
1086
1087 return -1;
1088 }
1089
1090 GSocket_Debug(( "GSocket_Write #5, size %d ret %d\n", size, ret ));
1091
1092 return ret;
1093 }
1094
1095 /* Flags */
1096
1097 /* GSocket_SetNonBlocking:
1098 * Sets the socket to non-blocking mode. All IO calls will return
1099 * immediately.
1100 */
1101 void GSocket::SetNonBlocking(bool non_block)
1102 {
1103 assert(this);
1104
1105 GSocket_Debug( ("GSocket_SetNonBlocking: %d\n", (int)non_block) );
1106
1107 m_non_blocking = non_block;
1108 }
1109
1110 /* GSocket_GetError:
1111 * Returns the last error occurred for this socket. Note that successful
1112 * operations do not clear this back to GSOCK_NOERROR, so use it only
1113 * after an error.
1114 */
1115 GSocketError WXDLLIMPEXP_NET GSocket::GetError()
1116 {
1117 assert(this);
1118
1119 return m_error;
1120 }
1121
1122 GSocketError GSocket::GetSockOpt(int level, int optname,
1123 void *optval, int *optlen)
1124 {
1125 if (getsockopt(m_fd, level, optname, (char*)optval, (SOCKOPTLEN_T*)optlen) == 0)
1126 return GSOCK_NOERROR;
1127
1128 return GSOCK_OPTERR;
1129 }
1130
1131 GSocketError GSocket::SetSockOpt(int level, int optname,
1132 const void *optval, int optlen)
1133 {
1134 if (setsockopt(m_fd, level, optname, (const char*)optval, optlen) == 0)
1135 return GSOCK_NOERROR;
1136
1137 return GSOCK_OPTERR;
1138 }
1139
1140 void GSocket::Enable(GSocketEvent event)
1141 {
1142 if (m_use_events)
1143 {
1144 m_detected &= ~(1 << event);
1145 GSocketManager::Get()->Install_Callback(this, event);
1146 }
1147 }
1148
1149 void GSocket::Disable(GSocketEvent event)
1150 {
1151 if (m_use_events)
1152 {
1153 m_detected |= (1 << event);
1154 GSocketManager::Get()->Uninstall_Callback(this, event);
1155 }
1156 }
1157
1158 /* _GSocket_Input_Timeout:
1159 * For blocking sockets, wait until data is available or
1160 * until timeout ellapses.
1161 */
1162 GSocketError GSocket::Input_Timeout()
1163 {
1164 fd_set readfds;
1165 int ret;
1166
1167 // Linux select() will overwrite the struct on return so make a copy
1168 struct timeval tv = m_timeout;
1169
1170 if (!m_non_blocking)
1171 {
1172 wxFD_ZERO(&readfds);
1173 wxFD_SET(m_fd, &readfds);
1174 ret = select(m_fd + 1, &readfds, NULL, NULL, &tv);
1175 if (ret == 0)
1176 {
1177 GSocket_Debug(( "GSocket_Input_Timeout, select returned 0\n" ));
1178 m_error = GSOCK_TIMEDOUT;
1179 return GSOCK_TIMEDOUT;
1180 }
1181
1182 if (ret == -1)
1183 {
1184 GSocket_Debug(( "GSocket_Input_Timeout, select returned -1\n" ));
1185 if (errno == EBADF) { GSocket_Debug(( "Invalid file descriptor\n" )); }
1186 if (errno == EINTR) { GSocket_Debug(( "A non blocked signal was caught\n" )); }
1187 if (errno == EINVAL) { GSocket_Debug(( "The highest number descriptor is negative\n" )); }
1188 if (errno == ENOMEM) { GSocket_Debug(( "Not enough memory\n" )); }
1189 m_error = GSOCK_TIMEDOUT;
1190 return GSOCK_TIMEDOUT;
1191 }
1192 }
1193
1194 return GSOCK_NOERROR;
1195 }
1196
1197 /* _GSocket_Output_Timeout:
1198 * For blocking sockets, wait until data can be sent without
1199 * blocking or until timeout ellapses.
1200 */
1201 GSocketError GSocket::Output_Timeout()
1202 {
1203 fd_set writefds;
1204 int ret;
1205
1206 // Linux select() will overwrite the struct on return so make a copy
1207 struct timeval tv = m_timeout;
1208
1209 GSocket_Debug( ("m_non_blocking has: %d\n", (int)m_non_blocking) );
1210
1211 if (!m_non_blocking)
1212 {
1213 wxFD_ZERO(&writefds);
1214 wxFD_SET(m_fd, &writefds);
1215 ret = select(m_fd + 1, NULL, &writefds, NULL, &tv);
1216 if (ret == 0)
1217 {
1218 GSocket_Debug(( "GSocket_Output_Timeout, select returned 0\n" ));
1219 m_error = GSOCK_TIMEDOUT;
1220 return GSOCK_TIMEDOUT;
1221 }
1222
1223 if (ret == -1)
1224 {
1225 GSocket_Debug(( "GSocket_Output_Timeout, select returned -1\n" ));
1226 if (errno == EBADF) { GSocket_Debug(( "Invalid file descriptor\n" )); }
1227 if (errno == EINTR) { GSocket_Debug(( "A non blocked signal was caught\n" )); }
1228 if (errno == EINVAL) { GSocket_Debug(( "The highest number descriptor is negative\n" )); }
1229 if (errno == ENOMEM) { GSocket_Debug(( "Not enough memory\n" )); }
1230 m_error = GSOCK_TIMEDOUT;
1231 return GSOCK_TIMEDOUT;
1232 }
1233
1234 if ( ! wxFD_ISSET(m_fd, &writefds) )
1235 {
1236 GSocket_Debug(( "GSocket_Output_Timeout is buggy!\n" ));
1237 }
1238 else
1239 {
1240 GSocket_Debug(( "GSocket_Output_Timeout seems correct\n" ));
1241 }
1242 }
1243 else
1244 {
1245 GSocket_Debug(( "GSocket_Output_Timeout, didn't try select!\n" ));
1246 }
1247
1248 return GSOCK_NOERROR;
1249 }
1250
1251 int GSocket::Recv_Stream(char *buffer, int size)
1252 {
1253 int ret;
1254 do
1255 {
1256 ret = recv(m_fd, buffer, size, GSOCKET_MSG_NOSIGNAL);
1257 }
1258 while (ret == -1 && errno == EINTR); /* Loop until not interrupted */
1259
1260 return ret;
1261 }
1262
1263 int GSocket::Recv_Dgram(char *buffer, int size)
1264 {
1265 wxSockAddr from;
1266 WX_SOCKLEN_T fromlen = sizeof(from);
1267 int ret;
1268 GSocketError err;
1269
1270 fromlen = sizeof(from);
1271
1272 do
1273 {
1274 ret = recvfrom(m_fd, buffer, size, 0, (sockaddr*)&from, (WX_SOCKLEN_T *) &fromlen);
1275 }
1276 while (ret == -1 && errno == EINTR); /* Loop until not interrupted */
1277
1278 if (ret == -1)
1279 return -1;
1280
1281 /* Translate a system address into a GSocket address */
1282 if (!m_peer)
1283 {
1284 m_peer = GAddress_new();
1285 if (!m_peer)
1286 {
1287 m_error = GSOCK_MEMERR;
1288 return -1;
1289 }
1290 }
1291
1292 err = _GAddress_translate_from(m_peer, (sockaddr*)&from, fromlen);
1293 if (err != GSOCK_NOERROR)
1294 {
1295 GAddress_destroy(m_peer);
1296 m_peer = NULL;
1297 m_error = err;
1298 return -1;
1299 }
1300
1301 return ret;
1302 }
1303
1304 int GSocket::Send_Stream(const char *buffer, int size)
1305 {
1306 int ret;
1307
1308 MASK_SIGNAL();
1309
1310 do
1311 {
1312 ret = send(m_fd, (char *)buffer, size, GSOCKET_MSG_NOSIGNAL);
1313 }
1314 while (ret == -1 && errno == EINTR); /* Loop until not interrupted */
1315
1316 UNMASK_SIGNAL();
1317
1318 return ret;
1319 }
1320
1321 int GSocket::Send_Dgram(const char *buffer, int size)
1322 {
1323 struct sockaddr *addr;
1324 int len, ret;
1325 GSocketError err;
1326
1327 if (!m_peer)
1328 {
1329 m_error = GSOCK_INVADDR;
1330 return -1;
1331 }
1332
1333 err = _GAddress_translate_to(m_peer, &addr, &len);
1334 if (err != GSOCK_NOERROR)
1335 {
1336 m_error = err;
1337 return -1;
1338 }
1339
1340 MASK_SIGNAL();
1341
1342 do
1343 {
1344 ret = sendto(m_fd, (char *)buffer, size, 0, addr, len);
1345 }
1346 while (ret == -1 && errno == EINTR); /* Loop until not interrupted */
1347
1348 UNMASK_SIGNAL();
1349
1350 /* Frees memory allocated from _GAddress_translate_to */
1351 free(addr);
1352
1353 return ret;
1354 }
1355
1356 void GSocket::OnStateChange(GSocketEvent event)
1357 {
1358 Disable(event);
1359 NotifyOnStateChange(event);
1360
1361 if ( event == GSOCK_LOST )
1362 Shutdown();
1363 }
1364
1365 void GSocket::Detected_Read()
1366 {
1367 char c;
1368
1369 /* Safeguard against straggling call to Detected_Read */
1370 if (m_fd == INVALID_SOCKET)
1371 {
1372 return;
1373 }
1374
1375 /* If we have already detected a LOST event, then don't try
1376 * to do any further processing.
1377 */
1378 if ((m_detected & GSOCK_LOST_FLAG) != 0)
1379 {
1380 m_establishing = false;
1381
1382 OnStateChange(GSOCK_LOST);
1383 return;
1384 }
1385
1386 int num = recv(m_fd, &c, 1, MSG_PEEK | GSOCKET_MSG_NOSIGNAL);
1387
1388 if (num > 0)
1389 {
1390 OnStateChange(GSOCK_INPUT);
1391 }
1392 else
1393 {
1394 if (m_server && m_stream)
1395 {
1396 OnStateChange(GSOCK_CONNECTION);
1397 }
1398 else if (num == 0)
1399 {
1400 if (m_stream)
1401 {
1402 /* graceful shutdown */
1403 OnStateChange(GSOCK_LOST);
1404 }
1405 else
1406 {
1407 /* Empty datagram received */
1408 OnStateChange(GSOCK_INPUT);
1409 }
1410 }
1411 else
1412 {
1413 /* Do not throw a lost event in cases where the socket isn't really lost */
1414 if ((errno == EWOULDBLOCK) || (errno == EAGAIN) || (errno == EINTR))
1415 {
1416 OnStateChange(GSOCK_INPUT);
1417 }
1418 else
1419 {
1420 OnStateChange(GSOCK_LOST);
1421 }
1422 }
1423 }
1424 }
1425
1426 void GSocket::Detected_Write()
1427 {
1428 /* If we have already detected a LOST event, then don't try
1429 * to do any further processing.
1430 */
1431 if ((m_detected & GSOCK_LOST_FLAG) != 0)
1432 {
1433 m_establishing = false;
1434
1435 OnStateChange(GSOCK_LOST);
1436 return;
1437 }
1438
1439 if (m_establishing && !m_server)
1440 {
1441 int error;
1442 SOCKOPTLEN_T len = sizeof(error);
1443
1444 m_establishing = false;
1445
1446 getsockopt(m_fd, SOL_SOCKET, SO_ERROR, (char*)&error, &len);
1447
1448 if (error)
1449 {
1450 OnStateChange(GSOCK_LOST);
1451 }
1452 else
1453 {
1454 OnStateChange(GSOCK_CONNECTION);
1455 /* We have to fire this event by hand because CONNECTION (for clients)
1456 * and OUTPUT are internally the same and we just disabled CONNECTION
1457 * events with the above macro.
1458 */
1459 OnStateChange(GSOCK_OUTPUT);
1460 }
1461 }
1462 else
1463 {
1464 OnStateChange(GSOCK_OUTPUT);
1465 }
1466 }
1467
1468 /*
1469 * -------------------------------------------------------------------------
1470 * GAddress
1471 * -------------------------------------------------------------------------
1472 */
1473
1474 /* CHECK_ADDRESS verifies that the current address family is either
1475 * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it
1476 * initalizes it to be a GSOCK_*family*. In other cases, it returns
1477 * an appropiate error code.
1478 *
1479 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
1480 */
1481 #define CHECK_ADDRESS(address, family) \
1482 { \
1483 if (address->m_family == GSOCK_NOFAMILY) \
1484 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1485 return address->m_error; \
1486 if (address->m_family != GSOCK_##family) \
1487 { \
1488 address->m_error = GSOCK_INVADDR; \
1489 return GSOCK_INVADDR; \
1490 } \
1491 }
1492
1493 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
1494 { \
1495 if (address->m_family == GSOCK_NOFAMILY) \
1496 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1497 return retval; \
1498 if (address->m_family != GSOCK_##family) \
1499 { \
1500 address->m_error = GSOCK_INVADDR; \
1501 return retval; \
1502 } \
1503 }
1504
1505
1506 GAddress *GAddress_new(void)
1507 {
1508 GAddress *address;
1509
1510 if ((address = (GAddress *) malloc(sizeof(GAddress))) == NULL)
1511 return NULL;
1512
1513 address->m_family = GSOCK_NOFAMILY;
1514 address->m_addr = NULL;
1515 address->m_len = 0;
1516
1517 return address;
1518 }
1519
1520 GAddress *GAddress_copy(GAddress *address)
1521 {
1522 GAddress *addr2;
1523
1524 assert(address != NULL);
1525
1526 if ((addr2 = (GAddress *) malloc(sizeof(GAddress))) == NULL)
1527 return NULL;
1528
1529 memcpy(addr2, address, sizeof(GAddress));
1530
1531 if (address->m_addr && address->m_len > 0)
1532 {
1533 addr2->m_addr = (struct sockaddr *)malloc(addr2->m_len);
1534 if (addr2->m_addr == NULL)
1535 {
1536 free(addr2);
1537 return NULL;
1538 }
1539 memcpy(addr2->m_addr, address->m_addr, addr2->m_len);
1540 }
1541
1542 return addr2;
1543 }
1544
1545 void GAddress_destroy(GAddress *address)
1546 {
1547 assert(address != NULL);
1548
1549 if (address->m_addr)
1550 free(address->m_addr);
1551
1552 free(address);
1553 }
1554
1555 void GAddress_SetFamily(GAddress *address, GAddressType type)
1556 {
1557 assert(address != NULL);
1558
1559 address->m_family = type;
1560 }
1561
1562 GAddressType GAddress_GetFamily(GAddress *address)
1563 {
1564 assert(address != NULL);
1565
1566 return address->m_family;
1567 }
1568
1569 GSocketError _GAddress_translate_from(GAddress *address,
1570 struct sockaddr *addr, int len)
1571 {
1572 address->m_realfamily = addr->sa_family;
1573 switch (addr->sa_family)
1574 {
1575 case AF_INET:
1576 address->m_family = GSOCK_INET;
1577 break;
1578 case AF_UNIX:
1579 address->m_family = GSOCK_UNIX;
1580 break;
1581 #if wxUSE_IPV6
1582 case AF_INET6:
1583 address->m_family = GSOCK_INET6;
1584 break;
1585 #endif // wxUSE_IPV6
1586 default:
1587 {
1588 address->m_error = GSOCK_INVOP;
1589 return GSOCK_INVOP;
1590 }
1591 }
1592
1593 if (address->m_addr)
1594 free(address->m_addr);
1595
1596 address->m_len = len;
1597 address->m_addr = (struct sockaddr *)malloc(len);
1598
1599 if (address->m_addr == NULL)
1600 {
1601 address->m_error = GSOCK_MEMERR;
1602 return GSOCK_MEMERR;
1603 }
1604
1605 memcpy(address->m_addr, addr, len);
1606
1607 return GSOCK_NOERROR;
1608 }
1609
1610 GSocketError _GAddress_translate_to(GAddress *address,
1611 struct sockaddr **addr, int *len)
1612 {
1613 if (!address->m_addr)
1614 {
1615 address->m_error = GSOCK_INVADDR;
1616 return GSOCK_INVADDR;
1617 }
1618
1619 *len = address->m_len;
1620 *addr = (struct sockaddr *)malloc(address->m_len);
1621 if (*addr == NULL)
1622 {
1623 address->m_error = GSOCK_MEMERR;
1624 return GSOCK_MEMERR;
1625 }
1626
1627 memcpy(*addr, address->m_addr, address->m_len);
1628 return GSOCK_NOERROR;
1629 }
1630
1631 /*
1632 * -------------------------------------------------------------------------
1633 * Internet address family
1634 * -------------------------------------------------------------------------
1635 */
1636
1637 GSocketError _GAddress_Init_INET(GAddress *address)
1638 {
1639 address->m_len = sizeof(struct sockaddr_in);
1640 address->m_addr = (struct sockaddr *) malloc(address->m_len);
1641 if (address->m_addr == NULL)
1642 {
1643 address->m_error = GSOCK_MEMERR;
1644 return GSOCK_MEMERR;
1645 }
1646
1647 address->m_family = GSOCK_INET;
1648 address->m_realfamily = PF_INET;
1649 ((struct sockaddr_in *)address->m_addr)->sin_family = AF_INET;
1650 ((struct sockaddr_in *)address->m_addr)->sin_addr.s_addr = INADDR_ANY;
1651
1652 return GSOCK_NOERROR;
1653 }
1654
1655 GSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname)
1656 {
1657 struct hostent *he;
1658 struct in_addr *addr;
1659
1660 assert(address != NULL);
1661
1662 CHECK_ADDRESS(address, INET);
1663
1664 addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr);
1665
1666 /* If it is a numeric host name, convert it now */
1667 #if defined(HAVE_INET_ATON)
1668 if (inet_aton(hostname, addr) == 0)
1669 {
1670 #elif defined(HAVE_INET_ADDR)
1671 if ( (addr->s_addr = inet_addr(hostname)) == (unsigned)-1 )
1672 {
1673 #else
1674 /* Use gethostbyname by default */
1675 #ifndef __WXMAC__
1676 int val = 1; /* VA doesn't like constants in conditional expressions */
1677 if (val)
1678 #endif
1679 {
1680 #endif
1681 struct in_addr *array_addr;
1682
1683 /* It is a real name, we solve it */
1684 struct hostent h;
1685 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
1686 struct hostent_data buffer;
1687 #else
1688 char buffer[1024];
1689 #endif
1690 int err;
1691 he = wxGethostbyname_r(hostname, &h, (void*)&buffer, sizeof(buffer), &err);
1692 if (he == NULL)
1693 {
1694 /* Reset to invalid address */
1695 addr->s_addr = INADDR_NONE;
1696 address->m_error = GSOCK_NOHOST;
1697 return GSOCK_NOHOST;
1698 }
1699
1700 array_addr = (struct in_addr *) *(he->h_addr_list);
1701 addr->s_addr = array_addr[0].s_addr;
1702 }
1703
1704 return GSOCK_NOERROR;
1705 }
1706
1707
1708 GSocketError GAddress_INET_SetBroadcastAddress(GAddress *address)
1709 {
1710 return GAddress_INET_SetHostAddress(address, INADDR_BROADCAST);
1711 }
1712
1713 GSocketError GAddress_INET_SetAnyAddress(GAddress *address)
1714 {
1715 return GAddress_INET_SetHostAddress(address, INADDR_ANY);
1716 }
1717
1718 GSocketError GAddress_INET_SetHostAddress(GAddress *address,
1719 unsigned long hostaddr)
1720 {
1721 struct in_addr *addr;
1722
1723 assert(address != NULL);
1724
1725 CHECK_ADDRESS(address, INET);
1726
1727 addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr);
1728 addr->s_addr = htonl(hostaddr);
1729
1730 return GSOCK_NOERROR;
1731 }
1732
1733 GSocketError GAddress_INET_SetPortName(GAddress *address, const char *port,
1734 const char *protocol)
1735 {
1736 struct servent *se;
1737 struct sockaddr_in *addr;
1738
1739 assert(address != NULL);
1740 CHECK_ADDRESS(address, INET);
1741
1742 if (!port)
1743 {
1744 address->m_error = GSOCK_INVPORT;
1745 return GSOCK_INVPORT;
1746 }
1747
1748 #if defined(HAVE_FUNC_GETSERVBYNAME_R_4)
1749 struct servent_data buffer;
1750 #else
1751 char buffer[1024];
1752 #endif
1753 struct servent serv;
1754 se = wxGetservbyname_r(port, protocol, &serv,
1755 (void*)&buffer, sizeof(buffer));
1756 if (!se)
1757 {
1758 /* the cast to int suppresses compiler warnings about subscript having the
1759 type char */
1760 if (isdigit((int)port[0]))
1761 {
1762 int port_int;
1763
1764 port_int = atoi(port);
1765 addr = (struct sockaddr_in *)address->m_addr;
1766 addr->sin_port = htons(port_int);
1767 return GSOCK_NOERROR;
1768 }
1769
1770 address->m_error = GSOCK_INVPORT;
1771 return GSOCK_INVPORT;
1772 }
1773
1774 addr = (struct sockaddr_in *)address->m_addr;
1775 addr->sin_port = se->s_port;
1776
1777 return GSOCK_NOERROR;
1778 }
1779
1780 GSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port)
1781 {
1782 struct sockaddr_in *addr;
1783
1784 assert(address != NULL);
1785 CHECK_ADDRESS(address, INET);
1786
1787 addr = (struct sockaddr_in *)address->m_addr;
1788 addr->sin_port = htons(port);
1789
1790 return GSOCK_NOERROR;
1791 }
1792
1793 GSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname, size_t sbuf)
1794 {
1795 struct hostent *he;
1796 char *addr_buf;
1797 struct sockaddr_in *addr;
1798
1799 assert(address != NULL);
1800 CHECK_ADDRESS(address, INET);
1801
1802 addr = (struct sockaddr_in *)address->m_addr;
1803 addr_buf = (char *)&(addr->sin_addr);
1804
1805 struct hostent temphost;
1806 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
1807 struct hostent_data buffer;
1808 #else
1809 char buffer[1024];
1810 #endif
1811 int err;
1812 he = wxGethostbyaddr_r(addr_buf, sizeof(addr->sin_addr), AF_INET, &temphost,
1813 (void*)&buffer, sizeof(buffer), &err);
1814 if (he == NULL)
1815 {
1816 address->m_error = GSOCK_NOHOST;
1817 return GSOCK_NOHOST;
1818 }
1819
1820 strncpy(hostname, he->h_name, sbuf);
1821
1822 return GSOCK_NOERROR;
1823 }
1824
1825 unsigned long GAddress_INET_GetHostAddress(GAddress *address)
1826 {
1827 struct sockaddr_in *addr;
1828
1829 assert(address != NULL);
1830 CHECK_ADDRESS_RETVAL(address, INET, 0);
1831
1832 addr = (struct sockaddr_in *)address->m_addr;
1833
1834 return ntohl(addr->sin_addr.s_addr);
1835 }
1836
1837 unsigned short GAddress_INET_GetPort(GAddress *address)
1838 {
1839 struct sockaddr_in *addr;
1840
1841 assert(address != NULL);
1842 CHECK_ADDRESS_RETVAL(address, INET, 0);
1843
1844 addr = (struct sockaddr_in *)address->m_addr;
1845 return ntohs(addr->sin_port);
1846 }
1847
1848 #if wxUSE_IPV6
1849 /*
1850 * -------------------------------------------------------------------------
1851 * Internet IPv6 address family
1852 * -------------------------------------------------------------------------
1853 */
1854
1855 GSocketError _GAddress_Init_INET6(GAddress *address)
1856 {
1857 struct in6_addr any_address = IN6ADDR_ANY_INIT;
1858 address->m_len = sizeof(struct sockaddr_in6);
1859 address->m_addr = (struct sockaddr *) malloc(address->m_len);
1860 if (address->m_addr == NULL)
1861 {
1862 address->m_error = GSOCK_MEMERR;
1863 return GSOCK_MEMERR;
1864 }
1865 memset(address->m_addr,0,address->m_len);
1866
1867 address->m_family = GSOCK_INET6;
1868 address->m_realfamily = AF_INET6;
1869 ((struct sockaddr_in6 *)address->m_addr)->sin6_family = AF_INET6;
1870 ((struct sockaddr_in6 *)address->m_addr)->sin6_addr = any_address;
1871
1872 return GSOCK_NOERROR;
1873 }
1874
1875 GSocketError GAddress_INET6_SetHostName(GAddress *address, const char *hostname)
1876 {
1877 assert(address != NULL);
1878 CHECK_ADDRESS(address, INET6);
1879
1880 addrinfo hints;
1881 memset( & hints, 0, sizeof( hints ) );
1882 hints.ai_family = AF_INET6;
1883 addrinfo * info = 0;
1884 if ( getaddrinfo( hostname, "0", & hints, & info ) || ! info )
1885 {
1886 address->m_error = GSOCK_NOHOST;
1887 return GSOCK_NOHOST;
1888 }
1889
1890 memcpy( address->m_addr, info->ai_addr, info->ai_addrlen );
1891 freeaddrinfo( info );
1892 return GSOCK_NOERROR;
1893 }
1894
1895 GSocketError GAddress_INET6_SetAnyAddress(GAddress *address)
1896 {
1897 assert(address != NULL);
1898
1899 CHECK_ADDRESS(address, INET6);
1900
1901 struct in6_addr addr;
1902 memset( & addr, 0, sizeof( addr ) );
1903 return GAddress_INET6_SetHostAddress(address, addr);
1904 }
1905 GSocketError GAddress_INET6_SetHostAddress(GAddress *address,
1906 struct in6_addr hostaddr)
1907 {
1908 assert(address != NULL);
1909
1910 CHECK_ADDRESS(address, INET6);
1911
1912 ((struct sockaddr_in6 *)address->m_addr)->sin6_addr = hostaddr;
1913
1914 return GSOCK_NOERROR;
1915 }
1916
1917 GSocketError GAddress_INET6_SetPortName(GAddress *address, const char *port,
1918 const char *protocol)
1919 {
1920 struct servent *se;
1921 struct sockaddr_in6 *addr;
1922
1923 assert(address != NULL);
1924 CHECK_ADDRESS(address, INET6);
1925
1926 if (!port)
1927 {
1928 address->m_error = GSOCK_INVPORT;
1929 return GSOCK_INVPORT;
1930 }
1931
1932 se = getservbyname(port, protocol);
1933 if (!se)
1934 {
1935 if (isdigit(port[0]))
1936 {
1937 int port_int;
1938
1939 port_int = atoi(port);
1940 addr = (struct sockaddr_in6 *)address->m_addr;
1941 addr->sin6_port = htons((u_short) port_int);
1942 return GSOCK_NOERROR;
1943 }
1944
1945 address->m_error = GSOCK_INVPORT;
1946 return GSOCK_INVPORT;
1947 }
1948
1949 addr = (struct sockaddr_in6 *)address->m_addr;
1950 addr->sin6_port = se->s_port;
1951
1952 return GSOCK_NOERROR;
1953 }
1954
1955 GSocketError GAddress_INET6_SetPort(GAddress *address, unsigned short port)
1956 {
1957 struct sockaddr_in6 *addr;
1958
1959 assert(address != NULL);
1960 CHECK_ADDRESS(address, INET6);
1961
1962 addr = (struct sockaddr_in6 *)address->m_addr;
1963 addr->sin6_port = htons(port);
1964
1965 return GSOCK_NOERROR;
1966 }
1967
1968 GSocketError GAddress_INET6_GetHostName(GAddress *address, char *hostname, size_t sbuf)
1969 {
1970 struct hostent *he;
1971 char *addr_buf;
1972 struct sockaddr_in6 *addr;
1973
1974 assert(address != NULL);
1975 CHECK_ADDRESS(address, INET6);
1976
1977 addr = (struct sockaddr_in6 *)address->m_addr;
1978 addr_buf = (char *)&(addr->sin6_addr);
1979
1980 he = gethostbyaddr(addr_buf, sizeof(addr->sin6_addr), AF_INET6);
1981 if (he == NULL)
1982 {
1983 address->m_error = GSOCK_NOHOST;
1984 return GSOCK_NOHOST;
1985 }
1986
1987 strncpy(hostname, he->h_name, sbuf);
1988
1989 return GSOCK_NOERROR;
1990 }
1991
1992 GSocketError GAddress_INET6_GetHostAddress(GAddress *address,struct in6_addr *hostaddr)
1993 {
1994 assert(address != NULL);
1995 assert(hostaddr != NULL);
1996 CHECK_ADDRESS_RETVAL(address, INET6, GSOCK_INVADDR);
1997 *hostaddr = ( (struct sockaddr_in6 *)address->m_addr )->sin6_addr;
1998 return GSOCK_NOERROR;
1999 }
2000
2001 unsigned short GAddress_INET6_GetPort(GAddress *address)
2002 {
2003 assert(address != NULL);
2004 CHECK_ADDRESS_RETVAL(address, INET6, 0);
2005
2006 return ntohs( ((struct sockaddr_in6 *)address->m_addr)->sin6_port );
2007 }
2008
2009 #endif // wxUSE_IPV6
2010
2011 /*
2012 * -------------------------------------------------------------------------
2013 * Unix address family
2014 * -------------------------------------------------------------------------
2015 */
2016
2017 #ifndef __VISAGECPP__
2018 GSocketError _GAddress_Init_UNIX(GAddress *address)
2019 {
2020 address->m_len = sizeof(struct sockaddr_un);
2021 address->m_addr = (struct sockaddr *)malloc(address->m_len);
2022 if (address->m_addr == NULL)
2023 {
2024 address->m_error = GSOCK_MEMERR;
2025 return GSOCK_MEMERR;
2026 }
2027
2028 address->m_family = GSOCK_UNIX;
2029 address->m_realfamily = PF_UNIX;
2030 ((struct sockaddr_un *)address->m_addr)->sun_family = AF_UNIX;
2031 ((struct sockaddr_un *)address->m_addr)->sun_path[0] = 0;
2032
2033 return GSOCK_NOERROR;
2034 }
2035
2036 #define UNIX_SOCK_PATHLEN (sizeof(addr->sun_path)/sizeof(addr->sun_path[0]))
2037
2038 GSocketError GAddress_UNIX_SetPath(GAddress *address, const char *path)
2039 {
2040 struct sockaddr_un *addr;
2041
2042 assert(address != NULL);
2043
2044 CHECK_ADDRESS(address, UNIX);
2045
2046 addr = ((struct sockaddr_un *)address->m_addr);
2047 strncpy(addr->sun_path, path, UNIX_SOCK_PATHLEN);
2048 addr->sun_path[UNIX_SOCK_PATHLEN - 1] = '\0';
2049
2050 return GSOCK_NOERROR;
2051 }
2052
2053 GSocketError GAddress_UNIX_GetPath(GAddress *address, char *path, size_t sbuf)
2054 {
2055 struct sockaddr_un *addr;
2056
2057 assert(address != NULL);
2058 CHECK_ADDRESS(address, UNIX);
2059
2060 addr = (struct sockaddr_un *)address->m_addr;
2061
2062 strncpy(path, addr->sun_path, sbuf);
2063
2064 return GSOCK_NOERROR;
2065 }
2066 #endif /* !defined(__VISAGECPP__) */
2067 #endif /* wxUSE_SOCKETS */