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