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