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