]> git.saurik.com Git - wxWidgets.git/blame - src/unix/sockunix.cpp
move generic DispatchTimeout() implementation in the header as evtloopcmn.cpp is...
[wxWidgets.git] / src / unix / sockunix.cpp
CommitLineData
51fe4b60 1/////////////////////////////////////////////////////////////////////////////
60913641 2// Name: src/unix/sockunix.cpp
51fe4b60
VZ
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
97e6ee04 13
7e1e6965 14#include "wx/wxprec.h"
7e1e6965 15
ebf94940
VZ
16#if wxUSE_SOCKETS
17
ebf94940
VZ
18#include "wx/private/fd.h"
19#include "wx/private/socket.h"
60913641 20#include "wx/unix/private/sockunix.h"
97e6ee04
DE
21
22#if defined(__VISAGECPP__)
1aa7b427 23#define BSD_SELECT /* use Berkeley Sockets select */
97e6ee04
DE
24#endif
25
ebf94940
VZ
26#if defined(__WATCOMC__)
27#include <errno.h>
28#include <nerrno.h>
29#endif
02564412 30
97e6ee04
DE
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
bc023abb
MW
42#ifdef HAVE_SYS_SELECT_H
43# include <sys/select.h>
44#endif
45
97e6ee04
DE
46#ifdef __VMS__
47#include <socket.h>
48struct 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)
87int _System bsdselect(int,
88 struct fd_set *,
89 struct fd_set *,
90 struct fd_set *,
91 struct timeval *);
92int _System soclose(int);
93# endif
94#endif
ebdab982
SN
95#ifdef __EMX__
96#include <sys/select.h>
97#endif
98
97e6ee04
DE
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
d1f8e97b
SN
109#ifdef _AIX
110# include <strings.h>
111#endif
97e6ee04
DE
112#include <signal.h>
113
9e03e02d 114#ifndef WX_SOCKLEN_T
97e6ee04
DE
115
116#ifdef VMS
9e03e02d 117# define WX_SOCKLEN_T unsigned int
97e6ee04
DE
118#else
119# ifdef __GLIBC__
120# if __GLIBC__ == 2
9e03e02d 121# define WX_SOCKLEN_T socklen_t
97e6ee04 122# endif
fcbd7e5a 123# elif defined(__WXMAC__)
9e03e02d 124# define WX_SOCKLEN_T socklen_t
97e6ee04 125# else
9e03e02d 126# define WX_SOCKLEN_T int
97e6ee04
DE
127# endif
128#endif
129
130#endif /* SOCKLEN_T */
131
ddc1a35f 132#ifndef SOCKOPTLEN_T
9e03e02d 133#define SOCKOPTLEN_T WX_SOCKLEN_T
ddc1a35f
DE
134#endif
135
97e6ee04
DE
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
7e1e6965 149#if defined(__VISAGECPP__) || defined(__WATCOMC__)
97e6ee04 150
7e1e6965
WS
151 #define MASK_SIGNAL() {
152 #define UNMASK_SIGNAL() }
153
154#else
defbeca1 155 extern "C" { typedef void (*wxSigHandler)(int); }
7e1e6965
WS
156
157 #define MASK_SIGNAL() \
158 { \
defbeca1 159 wxSigHandler old_handler = signal(SIGPIPE, SIG_IGN);
7e1e6965
WS
160
161 #define UNMASK_SIGNAL() \
162 signal(SIGPIPE, old_handler); \
163 }
164
165#endif
97e6ee04 166
e400d27d
KH
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. */
bb154f79
KH
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 */
97e6ee04 176
71b4a9b8
SN
177#if wxUSE_THREADS && (defined(HAVE_GETHOSTBYNAME) || defined(HAVE_GETSERVBYNAME))
178# include "wx/thread.h"
179#endif
97e6ee04 180
71b4a9b8
SN
181#if defined(HAVE_GETHOSTBYNAME)
182static struct hostent * deepCopyHostent(struct hostent *h,
183 const struct hostent *he,
184 char *buffer, int size, int *err)
185{
2887cb4e 186 /* copy old structure */
71b4a9b8 187 memcpy(h, he, sizeof(struct hostent));
2887cb4e
SN
188
189 /* copy name */
71b4a9b8
SN
190 int len = strlen(h->h_name);
191 if (len > size)
2887cb4e
SN
192 {
193 *err = ENOMEM;
194 return NULL;
195 }
71b4a9b8
SN
196 memcpy(buffer, h->h_name, len);
197 buffer[len] = '\0';
198 h->h_name = buffer;
2887cb4e
SN
199
200 /* track position in the buffer */
201 int pos = len + 1;
202
203 /* reuse len to store address length */
71b4a9b8 204 len = h->h_length;
2887cb4e
SN
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 {
71b4a9b8
SN
222 *err = ENOMEM;
223 return NULL;
224 }
2887cb4e
SN
225 memcpy(buffer + pos, *p, len); /* copy content */
226 *q = buffer + pos; /* set copied pointer to copied content */
227 pos += len;
71b4a9b8 228 }
2887cb4e
SN
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;
01ba4b67 236
2887cb4e
SN
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 *);
01ba4b67 242
2887cb4e
SN
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;
71b4a9b8 256 }
2887cb4e
SN
257 *++q = 0; /* null terminate the pointer list */
258 h->h_aliases = h_aliases; /* copy pointer to pointers */
259
71b4a9b8
SN
260 return h;
261}
262#endif
263
2887cb4e
SN
264#if defined(HAVE_GETHOSTBYNAME) && wxUSE_THREADS
265static wxMutex nameLock;
266#endif
71b4a9b8
SN
267struct hostent * wxGethostbyname_r(const char *hostname, struct hostent *h,
268 void *buffer, int size, int *err)
269
270{
539ae551 271 struct hostent *he = NULL;
71b4a9b8
SN
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
71b4a9b8
SN
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
2887cb4e
SN
299#if defined(HAVE_GETHOSTBYNAME) && wxUSE_THREADS
300static wxMutex addrLock;
301#endif
71b4a9b8
SN
302struct hostent * wxGethostbyaddr_r(const char *addr_buf, int buf_size,
303 int proto, struct hostent *h,
304 void *buffer, int size, int *err)
305{
539ae551 306 struct hostent *he = NULL;
71b4a9b8
SN
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
71b4a9b8
SN
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
f0b805fa 336#if defined(HAVE_GETSERVBYNAME)
71b4a9b8
SN
337static struct servent * deepCopyServent(struct servent *s,
338 const struct servent *se,
339 char *buffer, int size)
340{
2887cb4e 341 /* copy plain old structure */
71b4a9b8 342 memcpy(s, se, sizeof(struct servent));
2887cb4e
SN
343
344 /* copy name */
71b4a9b8 345 int len = strlen(s->s_name);
2887cb4e
SN
346 if (len >= size)
347 {
348 return NULL;
349 }
71b4a9b8
SN
350 memcpy(buffer, s->s_name, len);
351 buffer[len] = '\0';
352 s->s_name = buffer;
2887cb4e
SN
353
354 /* track position in the buffer */
355 int pos = len + 1;
356
357 /* copy protocol */
71b4a9b8 358 len = strlen(s->s_proto);
2887cb4e
SN
359 if (pos + len >= size)
360 {
361 return NULL;
71b4a9b8 362 }
2887cb4e
SN
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;
01ba4b67 374
2887cb4e
SN
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 *);
01ba4b67 380
2887cb4e
SN
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 */
71b4a9b8
SN
395 return s;
396}
397#endif
398
2887cb4e
SN
399#if defined(HAVE_GETSERVBYNAME) && wxUSE_THREADS
400static wxMutex servLock;
401#endif
71b4a9b8
SN
402struct servent *wxGetservbyname_r(const char *port, const char *protocol,
403 struct servent *serv, void *buffer, int size)
404{
539ae551 405 struct servent *se = NULL;
71b4a9b8
SN
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
71b4a9b8
SN
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
97e6ee04
DE
427/* debugging helpers */
428#ifdef __GSOCKET_DEBUG__
51fe4b60 429# define SOCKET_DEBUG(args) printf args
97e6ee04 430#else
51fe4b60 431# define SOCKET_DEBUG(args)
97e6ee04
DE
432#endif /* __GSOCKET_DEBUG__ */
433
54cb21d6
VZ
434/* static */
435wxSocketImpl *wxSocketImpl::Create(wxSocketBase& wxsocket)
436{
437 return new wxSocketImplUnix(wxsocket);
438}
439
97e6ee04 440
51fe4b60 441/*
97e6ee04
DE
442 * Disallow further read/write operations on this socket, close
443 * the fd and disable all callbacks.
444 */
51fe4b60 445void wxSocketImplUnix::Shutdown()
97e6ee04 446{
eb97543d 447 /* Don't allow events to fire after socket has been closed */
f0fbbe23 448 DisableEvents();
97e6ee04 449
51fe4b60 450 wxSocketImpl::Shutdown();
97e6ee04
DE
451}
452
51fe4b60 453/*
97e6ee04 454 * Waits for an incoming client connection. Returns a pointer to
51fe4b60
VZ
455 * a wxSocketImplUnix object, or NULL if there was an error, in which case
456 * the last error field will be updated for the calling wxSocketImplUnix.
97e6ee04 457 *
51fe4b60
VZ
458 * Error codes (set in the calling wxSocketImplUnix)
459 * wxSOCKET_INVSOCK - the socket is not valid or not a server.
460 * wxSOCKET_TIMEDOUT - timeout, no incoming connections.
461 * wxSOCKET_WOULDBLOCK - the call would block and the socket is nonblocking.
462 * wxSOCKET_MEMERR - couldn't allocate memory.
463 * wxSOCKET_IOERR - low-level error.
97e6ee04 464 */
51fe4b60 465wxSocketImpl *wxSocketImplUnix::WaitConnection(wxSocketBase& wxsocket)
97e6ee04 466{
8575ff50 467 wxSockAddr from;
9e03e02d 468 WX_SOCKLEN_T fromlen = sizeof(from);
51fe4b60
VZ
469 wxSocketImpl *connection;
470 wxSocketError err;
97e6ee04
DE
471 int arg = 1;
472
97e6ee04 473 /* If the socket has already been created, we exit immediately */
09e6e5ec 474 if (m_fd == INVALID_SOCKET || !m_server)
97e6ee04 475 {
51fe4b60 476 m_error = wxSOCKET_INVSOCK;
97e6ee04
DE
477 return NULL;
478 }
479
51fe4b60
VZ
480 /* Create a wxSocketImplUnix object for the new connection */
481 connection = wxSocketImplUnix::Create(wxsocket);
97e6ee04 482
444cb1fd 483 if (!connection)
97e6ee04 484 {
51fe4b60 485 m_error = wxSOCKET_MEMERR;
97e6ee04
DE
486 return NULL;
487 }
488
489 /* Wait for a connection (with timeout) */
c6b10632 490 if ( !BlockForInputWithTimeout() )
97e6ee04 491 {
85431efa 492 delete connection;
97e6ee04
DE
493 return NULL;
494 }
495
8575ff50 496 connection->m_fd = accept(m_fd, (sockaddr*)&from, (WX_SOCKLEN_T *) &fromlen);
97e6ee04 497
bb154f79 498 /* Reenable CONNECTION events */
51fe4b60 499 EnableEvent(wxSOCKET_CONNECTION);
bb154f79 500
97e6ee04
DE
501 if (connection->m_fd == INVALID_SOCKET)
502 {
503 if (errno == EWOULDBLOCK)
51fe4b60 504 m_error = wxSOCKET_WOULDBLOCK;
97e6ee04 505 else
51fe4b60 506 m_error = wxSOCKET_IOERR;
97e6ee04 507
85431efa 508 delete connection;
97e6ee04
DE
509 return NULL;
510 }
511
512 /* Initialize all fields */
948c96ef
DE
513 connection->m_server = false;
514 connection->m_stream = true;
97e6ee04
DE
515
516 /* Setup the peer address field */
517 connection->m_peer = GAddress_new();
518 if (!connection->m_peer)
519 {
85431efa 520 delete connection;
51fe4b60 521 m_error = wxSOCKET_MEMERR;
97e6ee04
DE
522 return NULL;
523 }
1aa7b427 524
8575ff50 525 err = _GAddress_translate_from(connection->m_peer, (sockaddr*)&from, fromlen);
51fe4b60 526 if (err != wxSOCKET_NOERROR)
97e6ee04 527 {
85431efa 528 delete connection;
09e6e5ec 529 m_error = err;
97e6ee04
DE
530 return NULL;
531 }
1aa7b427 532
97e6ee04
DE
533#if defined(__EMX__) || defined(__VISAGECPP__)
534 ioctl(connection->m_fd, FIONBIO, (char*)&arg, sizeof(arg));
535#else
536 ioctl(connection->m_fd, FIONBIO, &arg);
537#endif
97e6ee04
DE
538
539 return connection;
540}
541
51fe4b60 542void wxSocketImplUnix::DoEnableEvents(bool flag)
2804f77d 543{
51fe4b60 544 wxSocketManager * const manager = wxSocketManager::Get();
f0fbbe23
VZ
545 if ( flag )
546 {
51fe4b60
VZ
547 manager->Install_Callback(this, wxSOCKET_INPUT);
548 manager->Install_Callback(this, wxSOCKET_OUTPUT);
f0fbbe23
VZ
549 }
550 else // off
551 {
51fe4b60
VZ
552 manager->Uninstall_Callback(this, wxSOCKET_INPUT);
553 manager->Uninstall_Callback(this, wxSOCKET_OUTPUT);
60edcf45 554 }
60edcf45
VZ
555}
556
51fe4b60 557wxSocketError wxSocketImplUnix::DoHandleConnect(int ret)
97e6ee04 558{
f0fbbe23
VZ
559 /* We only call EnableEvents() if we know we aren't shutting down the socket.
560 * NB: EnableEvents() needs to be called whether the socket is blocking or
0d34c30e
KH
561 * non-blocking, it just shouldn't be called prior to knowing there is a
562 * connection _if_ blocking sockets are being used.
563 * If connect above returns 0, we are already connected and need to make the
f0fbbe23 564 * call to EnableEvents() now.
a3687636 565 */
f0fbbe23 566 if ( m_non_blocking || (ret == 0) )
2804f77d 567 EnableEvents();
bb154f79 568
97e6ee04
DE
569 if (ret == -1)
570 {
51fe4b60 571 const int err = errno;
97e6ee04 572
51fe4b60 573 /* If connect failed with EINPROGRESS and the wxSocketImplUnix object
97e6ee04
DE
574 * is in blocking mode, we select() for the specified timeout
575 * checking for writability to see if the connection request
576 * completes.
577 */
09e6e5ec 578 if ((err == EINPROGRESS) && (!m_non_blocking))
97e6ee04 579 {
c6b10632 580 if ( !BlockForOutputWithTimeout() )
97e6ee04 581 {
09e6e5ec 582 Close();
51fe4b60 583 return wxSOCKET_TIMEDOUT;
97e6ee04 584 }
97e6ee04 585
c6b10632
VZ
586 int error;
587 SOCKOPTLEN_T len = sizeof(error);
bb154f79 588
c6b10632
VZ
589 getsockopt(m_fd, SOL_SOCKET, SO_ERROR, (char*) &error, &len);
590 EnableEvents();
591
592 if (!error)
593 return wxSOCKET_NOERROR;
97e6ee04
DE
594 }
595
51fe4b60
VZ
596 /* If connect failed with EINPROGRESS and the wxSocketImplUnix object
597 * is set to nonblocking, we set m_error to wxSOCKET_WOULDBLOCK
598 * (and return wxSOCKET_WOULDBLOCK) but we don't close the socket;
599 * this way if the connection completes, a wxSOCKET_CONNECTION
97e6ee04
DE
600 * event will be generated, if enabled.
601 */
09e6e5ec 602 if ((err == EINPROGRESS) && (m_non_blocking))
97e6ee04 603 {
948c96ef 604 m_establishing = true;
51fe4b60
VZ
605 m_error = wxSOCKET_WOULDBLOCK;
606 return wxSOCKET_WOULDBLOCK;
97e6ee04
DE
607 }
608
609 /* If connect failed with an error other than EINPROGRESS,
51fe4b60 610 * then the call to Connect has failed.
97e6ee04 611 */
09e6e5ec 612 Close();
51fe4b60 613 m_error = wxSOCKET_IOERR;
97e6ee04 614
51fe4b60 615 return wxSOCKET_IOERR;
01ba4b67
VZ
616 }
617
51fe4b60 618 return wxSOCKET_NOERROR;
97e6ee04
DE
619}
620
621/* Generic IO */
622
623/* Like recv(), send(), ... */
07792edb 624int wxSocketImplUnix::Read(void *buffer, int size)
97e6ee04
DE
625{
626 int ret;
627
09e6e5ec 628 if (m_fd == INVALID_SOCKET || m_server)
97e6ee04 629 {
51fe4b60 630 m_error = wxSOCKET_INVSOCK;
97e6ee04
DE
631 return -1;
632 }
633
9d715960 634 /* Disable events during query of socket status */
51fe4b60 635 DisableEvent(wxSOCKET_INPUT);
b082b524 636
97e6ee04 637 /* If the socket is blocking, wait for data (with a timeout) */
c6b10632
VZ
638 if ( !BlockForInputWithTimeout() )
639 {
51fe4b60 640 m_error = wxSOCKET_TIMEDOUT;
976abb72
VZ
641 /* Don't return here immediately, otherwise socket events would not be
642 * re-enabled! */
9d715960 643 ret = -1;
976abb72 644 }
1aa7b427
DS
645 else
646 {
9d715960
DE
647 /* Read the data */
648 if (m_stream)
649 ret = Recv_Stream(buffer, size);
650 else
651 ret = Recv_Dgram(buffer, size);
b082b524 652
01c03554
VZ
653 /*
654 * If recv returned zero for a TCP socket (if m_stream == NULL, it's an UDP
655 * socket and empty datagrams are possible), then the connection has been
656 * gracefully closed.
657 *
658 * Otherwise, recv has returned an error (-1), in which case we have lost
659 * the socket only if errno does _not_ indicate that there may be more data
660 * to read.
9b67181b 661 */
01c03554 662 if ((ret == 0) && m_stream)
98e7a7f9 663 {
f4854380 664 /* Make sure wxSOCKET_LOST event gets sent and shut down the socket */
22185a1f
VZ
665 m_detected = wxSOCKET_LOST_FLAG;
666 OnReadWaiting();
667 return 0;
98e7a7f9 668 }
1aa7b427
DS
669 else if (ret == -1)
670 {
976abb72 671 if ((errno == EWOULDBLOCK) || (errno == EAGAIN))
51fe4b60 672 m_error = wxSOCKET_WOULDBLOCK;
976abb72 673 else
51fe4b60 674 m_error = wxSOCKET_IOERR;
976abb72 675 }
97e6ee04 676 }
b082b524 677
9d715960 678 /* Enable events again now that we are done processing */
51fe4b60 679 EnableEvent(wxSOCKET_INPUT);
97e6ee04
DE
680
681 return ret;
682}
683
07792edb 684int wxSocketImplUnix::Write(const void *buffer, int size)
b082b524 685{
97e6ee04
DE
686 int ret;
687
51fe4b60 688 SOCKET_DEBUG(( "Write #1, size %d\n", size ));
97e6ee04 689
09e6e5ec 690 if (m_fd == INVALID_SOCKET || m_server)
97e6ee04 691 {
51fe4b60 692 m_error = wxSOCKET_INVSOCK;
97e6ee04
DE
693 return -1;
694 }
695
51fe4b60 696 SOCKET_DEBUG(( "Write #2, size %d\n", size ));
97e6ee04
DE
697
698 /* If the socket is blocking, wait for writability (with a timeout) */
c6b10632 699 if ( !BlockForOutputWithTimeout() )
97e6ee04
DE
700 return -1;
701
51fe4b60 702 SOCKET_DEBUG(( "Write #3, size %d\n", size ));
97e6ee04
DE
703
704 /* Write the data */
09e6e5ec
DE
705 if (m_stream)
706 ret = Send_Stream(buffer, size);
97e6ee04 707 else
09e6e5ec 708 ret = Send_Dgram(buffer, size);
b082b524 709
51fe4b60 710 SOCKET_DEBUG(( "Write #4, size %d\n", size ));
97e6ee04
DE
711
712 if (ret == -1)
713 {
7e1e6965 714 if ((errno == EWOULDBLOCK) || (errno == EAGAIN))
97e6ee04 715 {
51fe4b60
VZ
716 m_error = wxSOCKET_WOULDBLOCK;
717 SOCKET_DEBUG(( "Write error WOULDBLOCK\n" ));
97e6ee04
DE
718 }
719 else
720 {
51fe4b60
VZ
721 m_error = wxSOCKET_IOERR;
722 SOCKET_DEBUG(( "Write error IOERR\n" ));
97e6ee04
DE
723 }
724
725 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
726 * in MSW). Once the first OUTPUT event is received, users can assume
727 * that the socket is writable until a read operation fails. Only then
728 * will further OUTPUT events be posted.
729 */
51fe4b60 730 EnableEvent(wxSOCKET_OUTPUT);
1aa7b427 731
97e6ee04
DE
732 return -1;
733 }
b082b524 734
51fe4b60 735 SOCKET_DEBUG(( "Write #5, size %d ret %d\n", size, ret ));
97e6ee04
DE
736
737 return ret;
738}
739
97e6ee04
DE
740/* Flags */
741
51fe4b60 742void wxSocketImplUnix::EnableEvent(wxSocketNotify event)
97e6ee04 743{
22185a1f
VZ
744 m_detected &= ~(1 << event);
745 wxSocketManager::Get()->Install_Callback(this, event);
97e6ee04
DE
746}
747
51fe4b60 748void wxSocketImplUnix::DisableEvent(wxSocketNotify event)
97e6ee04 749{
22185a1f
VZ
750 m_detected |= (1 << event);
751 wxSocketManager::Get()->Uninstall_Callback(this, event);
97e6ee04
DE
752}
753
07792edb 754int wxSocketImplUnix::Recv_Stream(void *buffer, int size)
97e6ee04 755{
bb154f79 756 int ret;
7e1e6965 757 do
bb154f79
KH
758 {
759 ret = recv(m_fd, buffer, size, GSOCKET_MSG_NOSIGNAL);
1aa7b427
DS
760 }
761 while (ret == -1 && errno == EINTR); /* Loop until not interrupted */
762
bb154f79 763 return ret;
97e6ee04
DE
764}
765
07792edb 766int wxSocketImplUnix::Recv_Dgram(void *buffer, int size)
97e6ee04 767{
8575ff50 768 wxSockAddr from;
9e03e02d 769 WX_SOCKLEN_T fromlen = sizeof(from);
97e6ee04 770 int ret;
51fe4b60 771 wxSocketError err;
97e6ee04
DE
772
773 fromlen = sizeof(from);
774
7e1e6965 775 do
bb154f79 776 {
8575ff50 777 ret = recvfrom(m_fd, buffer, size, 0, (sockaddr*)&from, (WX_SOCKLEN_T *) &fromlen);
1aa7b427
DS
778 }
779 while (ret == -1 && errno == EINTR); /* Loop until not interrupted */
97e6ee04
DE
780
781 if (ret == -1)
782 return -1;
783
51fe4b60 784 /* Translate a system address into a wxSocketImplUnix address */
09e6e5ec 785 if (!m_peer)
97e6ee04 786 {
09e6e5ec
DE
787 m_peer = GAddress_new();
788 if (!m_peer)
97e6ee04 789 {
51fe4b60 790 m_error = wxSOCKET_MEMERR;
97e6ee04
DE
791 return -1;
792 }
793 }
1aa7b427 794
8575ff50 795 err = _GAddress_translate_from(m_peer, (sockaddr*)&from, fromlen);
51fe4b60 796 if (err != wxSOCKET_NOERROR)
97e6ee04 797 {
09e6e5ec
DE
798 GAddress_destroy(m_peer);
799 m_peer = NULL;
800 m_error = err;
97e6ee04
DE
801 return -1;
802 }
803
804 return ret;
805}
806
07792edb 807int wxSocketImplUnix::Send_Stream(const void *buffer, int size)
97e6ee04
DE
808{
809 int ret;
810
7e1e6965
WS
811 MASK_SIGNAL();
812
813 do
bb154f79 814 {
07792edb 815 ret = send(m_fd, buffer, size, GSOCKET_MSG_NOSIGNAL);
1aa7b427
DS
816 }
817 while (ret == -1 && errno == EINTR); /* Loop until not interrupted */
7e1e6965 818
97e6ee04 819 UNMASK_SIGNAL();
97e6ee04
DE
820
821 return ret;
822}
823
07792edb 824int wxSocketImplUnix::Send_Dgram(const void *buffer, int size)
97e6ee04
DE
825{
826 struct sockaddr *addr;
827 int len, ret;
51fe4b60 828 wxSocketError err;
97e6ee04 829
09e6e5ec 830 if (!m_peer)
97e6ee04 831 {
51fe4b60 832 m_error = wxSOCKET_INVADDR;
97e6ee04
DE
833 return -1;
834 }
835
09e6e5ec 836 err = _GAddress_translate_to(m_peer, &addr, &len);
51fe4b60 837 if (err != wxSOCKET_NOERROR)
97e6ee04 838 {
09e6e5ec 839 m_error = err;
97e6ee04
DE
840 return -1;
841 }
842
97e6ee04 843 MASK_SIGNAL();
7e1e6965
WS
844
845 do
bb154f79 846 {
07792edb 847 ret = sendto(m_fd, buffer, size, 0, addr, len);
1aa7b427
DS
848 }
849 while (ret == -1 && errno == EINTR); /* Loop until not interrupted */
7e1e6965 850
97e6ee04 851 UNMASK_SIGNAL();
97e6ee04
DE
852
853 /* Frees memory allocated from _GAddress_translate_to */
854 free(addr);
855
856 return ret;
857}
858
51fe4b60 859void wxSocketImplUnix::OnStateChange(wxSocketNotify event)
53a161e1 860{
f0fbbe23 861 DisableEvent(event);
53a161e1
VZ
862 NotifyOnStateChange(event);
863
51fe4b60 864 if ( event == wxSOCKET_LOST )
53a161e1
VZ
865 Shutdown();
866}
867
a9d859df 868void wxSocketImplUnix::OnReadWaiting()
97e6ee04
DE
869{
870 char c;
871
bb154f79
KH
872 if (m_fd == INVALID_SOCKET)
873 {
874 return;
875 }
876
97e6ee04
DE
877 /* If we have already detected a LOST event, then don't try
878 * to do any further processing.
879 */
51fe4b60 880 if ((m_detected & wxSOCKET_LOST_FLAG) != 0)
97e6ee04 881 {
948c96ef 882 m_establishing = false;
97e6ee04 883
51fe4b60 884 OnStateChange(wxSOCKET_LOST);
97e6ee04
DE
885 return;
886 }
887
bb154f79
KH
888 int num = recv(m_fd, &c, 1, MSG_PEEK | GSOCKET_MSG_NOSIGNAL);
889
890 if (num > 0)
97e6ee04 891 {
51fe4b60 892 OnStateChange(wxSOCKET_INPUT);
97e6ee04
DE
893 }
894 else
895 {
09e6e5ec 896 if (m_server && m_stream)
97e6ee04 897 {
51fe4b60 898 OnStateChange(wxSOCKET_CONNECTION);
97e6ee04 899 }
e37e082e
VZ
900 else if (num == 0)
901 {
01c03554
VZ
902 if (m_stream)
903 {
904 /* graceful shutdown */
51fe4b60 905 OnStateChange(wxSOCKET_LOST);
01c03554
VZ
906 }
907 else
908 {
909 /* Empty datagram received */
51fe4b60 910 OnStateChange(wxSOCKET_INPUT);
01c03554 911 }
e37e082e 912 }
97e6ee04
DE
913 else
914 {
e400d27d 915 /* Do not throw a lost event in cases where the socket isn't really lost */
7e1e6965 916 if ((errno == EWOULDBLOCK) || (errno == EAGAIN) || (errno == EINTR))
bb154f79 917 {
51fe4b60 918 OnStateChange(wxSOCKET_INPUT);
bb154f79 919 }
7e1e6965 920 else
bb154f79 921 {
51fe4b60 922 OnStateChange(wxSOCKET_LOST);
7e1e6965 923 }
97e6ee04
DE
924 }
925 }
926}
927
a9d859df 928void wxSocketImplUnix::OnWriteWaiting()
97e6ee04
DE
929{
930 /* If we have already detected a LOST event, then don't try
931 * to do any further processing.
932 */
51fe4b60 933 if ((m_detected & wxSOCKET_LOST_FLAG) != 0)
97e6ee04 934 {
948c96ef 935 m_establishing = false;
97e6ee04 936
51fe4b60 937 OnStateChange(wxSOCKET_LOST);
97e6ee04
DE
938 return;
939 }
940
09e6e5ec 941 if (m_establishing && !m_server)
97e6ee04
DE
942 {
943 int error;
ddc1a35f 944 SOCKOPTLEN_T len = sizeof(error);
97e6ee04 945
948c96ef 946 m_establishing = false;
97e6ee04 947
f71bb8ef 948 getsockopt(m_fd, SOL_SOCKET, SO_ERROR, (char*)&error, &len);
97e6ee04
DE
949
950 if (error)
951 {
51fe4b60 952 OnStateChange(wxSOCKET_LOST);
97e6ee04
DE
953 }
954 else
955 {
51fe4b60 956 OnStateChange(wxSOCKET_CONNECTION);
97e6ee04
DE
957 /* We have to fire this event by hand because CONNECTION (for clients)
958 * and OUTPUT are internally the same and we just disabled CONNECTION
959 * events with the above macro.
960 */
51fe4b60 961 OnStateChange(wxSOCKET_OUTPUT);
97e6ee04
DE
962 }
963 }
964 else
965 {
51fe4b60 966 OnStateChange(wxSOCKET_OUTPUT);
97e6ee04
DE
967 }
968}
969
a9d859df
VZ
970void wxSocketImplUnix::OnExceptionWaiting()
971{
972 wxFAIL_MSG( "not supposed to be called" );
973}
974
97e6ee04
DE
975/*
976 * -------------------------------------------------------------------------
977 * GAddress
978 * -------------------------------------------------------------------------
979 */
980
981/* CHECK_ADDRESS verifies that the current address family is either
51fe4b60
VZ
982 * wxSOCKET_NOFAMILY or wxSOCKET_*family*, and if it is wxSOCKET_NOFAMILY, it
983 * initalizes it to be a wxSOCKET_*family*. In other cases, it returns
97e6ee04
DE
984 * an appropiate error code.
985 *
986 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
987 */
988#define CHECK_ADDRESS(address, family) \
989{ \
51fe4b60
VZ
990 if (address->m_family == wxSOCKET_NOFAMILY) \
991 if (_GAddress_Init_##family(address) != wxSOCKET_NOERROR) \
97e6ee04 992 return address->m_error; \
51fe4b60 993 if (address->m_family != wxSOCKET_##family) \
97e6ee04 994 { \
51fe4b60
VZ
995 address->m_error = wxSOCKET_INVADDR; \
996 return wxSOCKET_INVADDR; \
97e6ee04
DE
997 } \
998}
999
1000#define CHECK_ADDRESS_RETVAL(address, family, retval) \
1001{ \
51fe4b60
VZ
1002 if (address->m_family == wxSOCKET_NOFAMILY) \
1003 if (_GAddress_Init_##family(address) != wxSOCKET_NOERROR) \
97e6ee04 1004 return retval; \
51fe4b60 1005 if (address->m_family != wxSOCKET_##family) \
97e6ee04 1006 { \
51fe4b60 1007 address->m_error = wxSOCKET_INVADDR; \
97e6ee04
DE
1008 return retval; \
1009 } \
1010}
1011
1012
1013GAddress *GAddress_new(void)
1014{
1015 GAddress *address;
1016
1017 if ((address = (GAddress *) malloc(sizeof(GAddress))) == NULL)
1018 return NULL;
1019
51fe4b60 1020 address->m_family = wxSOCKET_NOFAMILY;
97e6ee04
DE
1021 address->m_addr = NULL;
1022 address->m_len = 0;
1023
1024 return address;
1025}
1026
1027GAddress *GAddress_copy(GAddress *address)
1028{
1029 GAddress *addr2;
1030
1031 assert(address != NULL);
1032
1033 if ((addr2 = (GAddress *) malloc(sizeof(GAddress))) == NULL)
1034 return NULL;
1035
1036 memcpy(addr2, address, sizeof(GAddress));
1037
1038 if (address->m_addr && address->m_len > 0)
1039 {
1040 addr2->m_addr = (struct sockaddr *)malloc(addr2->m_len);
1041 if (addr2->m_addr == NULL)
1042 {
1043 free(addr2);
1044 return NULL;
1045 }
1046 memcpy(addr2->m_addr, address->m_addr, addr2->m_len);
1047 }
1048
1049 return addr2;
1050}
1051
1052void GAddress_destroy(GAddress *address)
1053{
1054 assert(address != NULL);
1055
1056 if (address->m_addr)
1057 free(address->m_addr);
1058
1059 free(address);
1060}
1061
1062void GAddress_SetFamily(GAddress *address, GAddressType type)
1063{
1064 assert(address != NULL);
1065
1066 address->m_family = type;
1067}
1068
1069GAddressType GAddress_GetFamily(GAddress *address)
1070{
1071 assert(address != NULL);
1072
1073 return address->m_family;
1074}
1075
51fe4b60 1076wxSocketError _GAddress_translate_from(GAddress *address,
97e6ee04
DE
1077 struct sockaddr *addr, int len)
1078{
1079 address->m_realfamily = addr->sa_family;
1080 switch (addr->sa_family)
1081 {
1082 case AF_INET:
51fe4b60 1083 address->m_family = wxSOCKET_INET;
97e6ee04
DE
1084 break;
1085 case AF_UNIX:
51fe4b60 1086 address->m_family = wxSOCKET_UNIX;
97e6ee04 1087 break;
8575ff50 1088#if wxUSE_IPV6
97e6ee04 1089 case AF_INET6:
51fe4b60 1090 address->m_family = wxSOCKET_INET6;
97e6ee04 1091 break;
8575ff50 1092#endif // wxUSE_IPV6
97e6ee04
DE
1093 default:
1094 {
51fe4b60
VZ
1095 address->m_error = wxSOCKET_INVOP;
1096 return wxSOCKET_INVOP;
97e6ee04
DE
1097 }
1098 }
1099
1100 if (address->m_addr)
1101 free(address->m_addr);
1102
1103 address->m_len = len;
1104 address->m_addr = (struct sockaddr *)malloc(len);
1105
1106 if (address->m_addr == NULL)
1107 {
51fe4b60
VZ
1108 address->m_error = wxSOCKET_MEMERR;
1109 return wxSOCKET_MEMERR;
97e6ee04 1110 }
1aa7b427 1111
97e6ee04
DE
1112 memcpy(address->m_addr, addr, len);
1113
51fe4b60 1114 return wxSOCKET_NOERROR;
97e6ee04
DE
1115}
1116
51fe4b60 1117wxSocketError _GAddress_translate_to(GAddress *address,
97e6ee04
DE
1118 struct sockaddr **addr, int *len)
1119{
1120 if (!address->m_addr)
1121 {
51fe4b60
VZ
1122 address->m_error = wxSOCKET_INVADDR;
1123 return wxSOCKET_INVADDR;
97e6ee04
DE
1124 }
1125
1126 *len = address->m_len;
1127 *addr = (struct sockaddr *)malloc(address->m_len);
1128 if (*addr == NULL)
1129 {
51fe4b60
VZ
1130 address->m_error = wxSOCKET_MEMERR;
1131 return wxSOCKET_MEMERR;
97e6ee04
DE
1132 }
1133
1134 memcpy(*addr, address->m_addr, address->m_len);
51fe4b60 1135 return wxSOCKET_NOERROR;
97e6ee04
DE
1136}
1137
1138/*
1139 * -------------------------------------------------------------------------
1140 * Internet address family
1141 * -------------------------------------------------------------------------
1142 */
1143
51fe4b60 1144wxSocketError _GAddress_Init_INET(GAddress *address)
97e6ee04
DE
1145{
1146 address->m_len = sizeof(struct sockaddr_in);
1147 address->m_addr = (struct sockaddr *) malloc(address->m_len);
1148 if (address->m_addr == NULL)
1149 {
51fe4b60
VZ
1150 address->m_error = wxSOCKET_MEMERR;
1151 return wxSOCKET_MEMERR;
97e6ee04
DE
1152 }
1153
51fe4b60 1154 address->m_family = wxSOCKET_INET;
97e6ee04
DE
1155 address->m_realfamily = PF_INET;
1156 ((struct sockaddr_in *)address->m_addr)->sin_family = AF_INET;
1157 ((struct sockaddr_in *)address->m_addr)->sin_addr.s_addr = INADDR_ANY;
1158
51fe4b60 1159 return wxSOCKET_NOERROR;
97e6ee04
DE
1160}
1161
51fe4b60 1162wxSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname)
97e6ee04
DE
1163{
1164 struct hostent *he;
1165 struct in_addr *addr;
1166
1167 assert(address != NULL);
1168
1169 CHECK_ADDRESS(address, INET);
1170
1171 addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr);
1172
1173 /* If it is a numeric host name, convert it now */
1174#if defined(HAVE_INET_ATON)
1175 if (inet_aton(hostname, addr) == 0)
1176 {
1177#elif defined(HAVE_INET_ADDR)
8c0f8906 1178 if ( (addr->s_addr = inet_addr(hostname)) == (unsigned)-1 )
97e6ee04
DE
1179 {
1180#else
1181 /* Use gethostbyname by default */
9d715960 1182#ifndef __WXMAC__
ba2a81d7 1183 int val = 1; /* VA doesn't like constants in conditional expressions */
97e6ee04 1184 if (val)
9d715960 1185#endif
97e6ee04
DE
1186 {
1187#endif
1188 struct in_addr *array_addr;
1189
1190 /* It is a real name, we solve it */
127189eb
SN
1191 struct hostent h;
1192#if defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
1193 struct hostent_data buffer;
1194#else
1195 char buffer[1024];
1196#endif
1197 int err;
1198 he = wxGethostbyname_r(hostname, &h, (void*)&buffer, sizeof(buffer), &err);
1199 if (he == NULL)
97e6ee04
DE
1200 {
1201 /* Reset to invalid address */
1202 addr->s_addr = INADDR_NONE;
51fe4b60
VZ
1203 address->m_error = wxSOCKET_NOHOST;
1204 return wxSOCKET_NOHOST;
97e6ee04 1205 }
1aa7b427 1206
97e6ee04
DE
1207 array_addr = (struct in_addr *) *(he->h_addr_list);
1208 addr->s_addr = array_addr[0].s_addr;
1209 }
1aa7b427 1210
51fe4b60 1211 return wxSOCKET_NOERROR;
97e6ee04
DE
1212}
1213
60edcf45 1214
51fe4b60 1215wxSocketError GAddress_INET_SetBroadcastAddress(GAddress *address)
60edcf45
VZ
1216{
1217 return GAddress_INET_SetHostAddress(address, INADDR_BROADCAST);
1218}
1219
51fe4b60 1220wxSocketError GAddress_INET_SetAnyAddress(GAddress *address)
97e6ee04
DE
1221{
1222 return GAddress_INET_SetHostAddress(address, INADDR_ANY);
1223}
1224
51fe4b60 1225wxSocketError GAddress_INET_SetHostAddress(GAddress *address,
97e6ee04
DE
1226 unsigned long hostaddr)
1227{
1228 struct in_addr *addr;
1229
1230 assert(address != NULL);
1231
1232 CHECK_ADDRESS(address, INET);
1233
1234 addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr);
9d715960 1235 addr->s_addr = htonl(hostaddr);
97e6ee04 1236
51fe4b60 1237 return wxSOCKET_NOERROR;
97e6ee04
DE
1238}
1239
51fe4b60 1240wxSocketError GAddress_INET_SetPortName(GAddress *address, const char *port,
97e6ee04
DE
1241 const char *protocol)
1242{
1243 struct servent *se;
1244 struct sockaddr_in *addr;
1245
1246 assert(address != NULL);
1247 CHECK_ADDRESS(address, INET);
1248
1249 if (!port)
1250 {
51fe4b60
VZ
1251 address->m_error = wxSOCKET_INVPORT;
1252 return wxSOCKET_INVPORT;
97e6ee04 1253 }
b082b524 1254
127189eb
SN
1255#if defined(HAVE_FUNC_GETSERVBYNAME_R_4)
1256 struct servent_data buffer;
f6bc1c74 1257#else
127189eb 1258 char buffer[1024];
f6bc1c74 1259#endif
127189eb
SN
1260 struct servent serv;
1261 se = wxGetservbyname_r(port, protocol, &serv,
1262 (void*)&buffer, sizeof(buffer));
97e6ee04
DE
1263 if (!se)
1264 {
1265 /* the cast to int suppresses compiler warnings about subscript having the
1266 type char */
1267 if (isdigit((int)port[0]))
1268 {
1269 int port_int;
1270
1271 port_int = atoi(port);
1272 addr = (struct sockaddr_in *)address->m_addr;
1273 addr->sin_port = htons(port_int);
51fe4b60 1274 return wxSOCKET_NOERROR;
97e6ee04
DE
1275 }
1276
51fe4b60
VZ
1277 address->m_error = wxSOCKET_INVPORT;
1278 return wxSOCKET_INVPORT;
97e6ee04
DE
1279 }
1280
1281 addr = (struct sockaddr_in *)address->m_addr;
1282 addr->sin_port = se->s_port;
1283
51fe4b60 1284 return wxSOCKET_NOERROR;
97e6ee04
DE
1285}
1286
51fe4b60 1287wxSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port)
97e6ee04
DE
1288{
1289 struct sockaddr_in *addr;
1290
1291 assert(address != NULL);
1292 CHECK_ADDRESS(address, INET);
b082b524 1293
97e6ee04
DE
1294 addr = (struct sockaddr_in *)address->m_addr;
1295 addr->sin_port = htons(port);
1296
51fe4b60 1297 return wxSOCKET_NOERROR;
97e6ee04
DE
1298}
1299
51fe4b60 1300wxSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname, size_t sbuf)
97e6ee04
DE
1301{
1302 struct hostent *he;
1303 char *addr_buf;
1304 struct sockaddr_in *addr;
1305
b082b524 1306 assert(address != NULL);
97e6ee04
DE
1307 CHECK_ADDRESS(address, INET);
1308
1309 addr = (struct sockaddr_in *)address->m_addr;
1310 addr_buf = (char *)&(addr->sin_addr);
1311
127189eb
SN
1312 struct hostent temphost;
1313#if defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
1314 struct hostent_data buffer;
1315#else
1316 char buffer[1024];
1317#endif
1318 int err;
1319 he = wxGethostbyaddr_r(addr_buf, sizeof(addr->sin_addr), AF_INET, &temphost,
1320 (void*)&buffer, sizeof(buffer), &err);
97e6ee04
DE
1321 if (he == NULL)
1322 {
51fe4b60
VZ
1323 address->m_error = wxSOCKET_NOHOST;
1324 return wxSOCKET_NOHOST;
97e6ee04
DE
1325 }
1326
1327 strncpy(hostname, he->h_name, sbuf);
1328
51fe4b60 1329 return wxSOCKET_NOERROR;
97e6ee04
DE
1330}
1331
1332unsigned long GAddress_INET_GetHostAddress(GAddress *address)
1333{
1334 struct sockaddr_in *addr;
1335
b082b524
DE
1336 assert(address != NULL);
1337 CHECK_ADDRESS_RETVAL(address, INET, 0);
97e6ee04
DE
1338
1339 addr = (struct sockaddr_in *)address->m_addr;
1340
9d715960 1341 return ntohl(addr->sin_addr.s_addr);
97e6ee04
DE
1342}
1343
1344unsigned short GAddress_INET_GetPort(GAddress *address)
1345{
1346 struct sockaddr_in *addr;
1347
b082b524
DE
1348 assert(address != NULL);
1349 CHECK_ADDRESS_RETVAL(address, INET, 0);
97e6ee04
DE
1350
1351 addr = (struct sockaddr_in *)address->m_addr;
1352 return ntohs(addr->sin_port);
1353}
1354
8575ff50
VZ
1355#if wxUSE_IPV6
1356/*
1357 * -------------------------------------------------------------------------
1358 * Internet IPv6 address family
1359 * -------------------------------------------------------------------------
1360 */
1361
51fe4b60 1362wxSocketError _GAddress_Init_INET6(GAddress *address)
8575ff50
VZ
1363{
1364 struct in6_addr any_address = IN6ADDR_ANY_INIT;
1365 address->m_len = sizeof(struct sockaddr_in6);
1366 address->m_addr = (struct sockaddr *) malloc(address->m_len);
1367 if (address->m_addr == NULL)
1368 {
51fe4b60
VZ
1369 address->m_error = wxSOCKET_MEMERR;
1370 return wxSOCKET_MEMERR;
8575ff50
VZ
1371 }
1372 memset(address->m_addr,0,address->m_len);
1373
51fe4b60 1374 address->m_family = wxSOCKET_INET6;
8575ff50
VZ
1375 address->m_realfamily = AF_INET6;
1376 ((struct sockaddr_in6 *)address->m_addr)->sin6_family = AF_INET6;
1377 ((struct sockaddr_in6 *)address->m_addr)->sin6_addr = any_address;
1378
51fe4b60 1379 return wxSOCKET_NOERROR;
8575ff50
VZ
1380}
1381
51fe4b60 1382wxSocketError GAddress_INET6_SetHostName(GAddress *address, const char *hostname)
8575ff50
VZ
1383{
1384 assert(address != NULL);
1385 CHECK_ADDRESS(address, INET6);
1386
1387 addrinfo hints;
1388 memset( & hints, 0, sizeof( hints ) );
1389 hints.ai_family = AF_INET6;
1390 addrinfo * info = 0;
1391 if ( getaddrinfo( hostname, "0", & hints, & info ) || ! info )
1392 {
51fe4b60
VZ
1393 address->m_error = wxSOCKET_NOHOST;
1394 return wxSOCKET_NOHOST;
8575ff50
VZ
1395 }
1396
1397 memcpy( address->m_addr, info->ai_addr, info->ai_addrlen );
1398 freeaddrinfo( info );
51fe4b60 1399 return wxSOCKET_NOERROR;
8575ff50
VZ
1400}
1401
51fe4b60 1402wxSocketError GAddress_INET6_SetAnyAddress(GAddress *address)
8575ff50
VZ
1403{
1404 assert(address != NULL);
1405
1406 CHECK_ADDRESS(address, INET6);
1407
1408 struct in6_addr addr;
1409 memset( & addr, 0, sizeof( addr ) );
1410 return GAddress_INET6_SetHostAddress(address, addr);
1411}
51fe4b60 1412wxSocketError GAddress_INET6_SetHostAddress(GAddress *address,
8575ff50
VZ
1413 struct in6_addr hostaddr)
1414{
1415 assert(address != NULL);
1416
1417 CHECK_ADDRESS(address, INET6);
1418
1419 ((struct sockaddr_in6 *)address->m_addr)->sin6_addr = hostaddr;
1420
51fe4b60 1421 return wxSOCKET_NOERROR;
8575ff50
VZ
1422}
1423
51fe4b60 1424wxSocketError GAddress_INET6_SetPortName(GAddress *address, const char *port,
8575ff50
VZ
1425 const char *protocol)
1426{
1427 struct servent *se;
1428 struct sockaddr_in6 *addr;
1429
1430 assert(address != NULL);
1431 CHECK_ADDRESS(address, INET6);
1432
1433 if (!port)
1434 {
51fe4b60
VZ
1435 address->m_error = wxSOCKET_INVPORT;
1436 return wxSOCKET_INVPORT;
8575ff50
VZ
1437 }
1438
1439 se = getservbyname(port, protocol);
1440 if (!se)
1441 {
1442 if (isdigit(port[0]))
1443 {
1444 int port_int;
1445
1446 port_int = atoi(port);
1447 addr = (struct sockaddr_in6 *)address->m_addr;
1448 addr->sin6_port = htons((u_short) port_int);
51fe4b60 1449 return wxSOCKET_NOERROR;
8575ff50
VZ
1450 }
1451
51fe4b60
VZ
1452 address->m_error = wxSOCKET_INVPORT;
1453 return wxSOCKET_INVPORT;
8575ff50
VZ
1454 }
1455
1456 addr = (struct sockaddr_in6 *)address->m_addr;
1457 addr->sin6_port = se->s_port;
1458
51fe4b60 1459 return wxSOCKET_NOERROR;
8575ff50
VZ
1460}
1461
51fe4b60 1462wxSocketError GAddress_INET6_SetPort(GAddress *address, unsigned short port)
8575ff50
VZ
1463{
1464 struct sockaddr_in6 *addr;
1465
1466 assert(address != NULL);
1467 CHECK_ADDRESS(address, INET6);
1468
1469 addr = (struct sockaddr_in6 *)address->m_addr;
1470 addr->sin6_port = htons(port);
1471
51fe4b60 1472 return wxSOCKET_NOERROR;
8575ff50
VZ
1473}
1474
51fe4b60 1475wxSocketError GAddress_INET6_GetHostName(GAddress *address, char *hostname, size_t sbuf)
8575ff50
VZ
1476{
1477 struct hostent *he;
1478 char *addr_buf;
1479 struct sockaddr_in6 *addr;
1480
1481 assert(address != NULL);
1482 CHECK_ADDRESS(address, INET6);
1483
1484 addr = (struct sockaddr_in6 *)address->m_addr;
1485 addr_buf = (char *)&(addr->sin6_addr);
1486
1487 he = gethostbyaddr(addr_buf, sizeof(addr->sin6_addr), AF_INET6);
1488 if (he == NULL)
1489 {
51fe4b60
VZ
1490 address->m_error = wxSOCKET_NOHOST;
1491 return wxSOCKET_NOHOST;
8575ff50
VZ
1492 }
1493
1494 strncpy(hostname, he->h_name, sbuf);
1495
51fe4b60 1496 return wxSOCKET_NOERROR;
8575ff50
VZ
1497}
1498
51fe4b60 1499wxSocketError GAddress_INET6_GetHostAddress(GAddress *address,struct in6_addr *hostaddr)
8575ff50
VZ
1500{
1501 assert(address != NULL);
1502 assert(hostaddr != NULL);
51fe4b60 1503 CHECK_ADDRESS_RETVAL(address, INET6, wxSOCKET_INVADDR);
8575ff50 1504 *hostaddr = ( (struct sockaddr_in6 *)address->m_addr )->sin6_addr;
51fe4b60 1505 return wxSOCKET_NOERROR;
8575ff50
VZ
1506}
1507
1508unsigned short GAddress_INET6_GetPort(GAddress *address)
1509{
1510 assert(address != NULL);
1511 CHECK_ADDRESS_RETVAL(address, INET6, 0);
1512
1513 return ntohs( ((struct sockaddr_in6 *)address->m_addr)->sin6_port );
1514}
1515
1516#endif // wxUSE_IPV6
1517
97e6ee04
DE
1518/*
1519 * -------------------------------------------------------------------------
1520 * Unix address family
1521 * -------------------------------------------------------------------------
1522 */
1523
1524#ifndef __VISAGECPP__
51fe4b60 1525wxSocketError _GAddress_Init_UNIX(GAddress *address)
97e6ee04
DE
1526{
1527 address->m_len = sizeof(struct sockaddr_un);
1528 address->m_addr = (struct sockaddr *)malloc(address->m_len);
1529 if (address->m_addr == NULL)
1530 {
51fe4b60
VZ
1531 address->m_error = wxSOCKET_MEMERR;
1532 return wxSOCKET_MEMERR;
97e6ee04
DE
1533 }
1534
51fe4b60 1535 address->m_family = wxSOCKET_UNIX;
97e6ee04
DE
1536 address->m_realfamily = PF_UNIX;
1537 ((struct sockaddr_un *)address->m_addr)->sun_family = AF_UNIX;
1538 ((struct sockaddr_un *)address->m_addr)->sun_path[0] = 0;
1539
51fe4b60 1540 return wxSOCKET_NOERROR;
97e6ee04
DE
1541}
1542
1543#define UNIX_SOCK_PATHLEN (sizeof(addr->sun_path)/sizeof(addr->sun_path[0]))
1544
51fe4b60 1545wxSocketError GAddress_UNIX_SetPath(GAddress *address, const char *path)
97e6ee04
DE
1546{
1547 struct sockaddr_un *addr;
1548
b082b524 1549 assert(address != NULL);
97e6ee04 1550
b082b524 1551 CHECK_ADDRESS(address, UNIX);
97e6ee04
DE
1552
1553 addr = ((struct sockaddr_un *)address->m_addr);
1554 strncpy(addr->sun_path, path, UNIX_SOCK_PATHLEN);
1555 addr->sun_path[UNIX_SOCK_PATHLEN - 1] = '\0';
1556
51fe4b60 1557 return wxSOCKET_NOERROR;
97e6ee04
DE
1558}
1559
51fe4b60 1560wxSocketError GAddress_UNIX_GetPath(GAddress *address, char *path, size_t sbuf)
97e6ee04
DE
1561{
1562 struct sockaddr_un *addr;
1563
1564 assert(address != NULL);
1565 CHECK_ADDRESS(address, UNIX);
1566
1567 addr = (struct sockaddr_un *)address->m_addr;
1568
1569 strncpy(path, addr->sun_path, sbuf);
1570
51fe4b60 1571 return wxSOCKET_NOERROR;
97e6ee04
DE
1572}
1573#endif /* !defined(__VISAGECPP__) */
54cb21d6 1574
ebf94940 1575#endif /* wxUSE_SOCKETS */