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