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