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