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