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