]> git.saurik.com Git - wxWidgets.git/blame - src/os2/gsocket.c
removed unused variables
[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
e32c4e60 134
3958ae62
SN
135#ifndef __GSOCKET_STANDALONE__
136# include "wx/unix/gsockunx.h"
137# include "wx/gsocket.h"
138#else
139# include "gsockunx.h"
140# include "gsocket.h"
141#endif /* __GSOCKET_STANDALONE__ */
142
143/* redefine some GUI-only functions to do nothing in console mode */
144#if defined(wxUSE_GUI) && !wxUSE_GUI
145# define _GSocket_GUI_Init(socket) (1)
146# define _GSocket_GUI_Destroy(socket)
147# define _GSocket_Enable_Events(socket)
148# define _GSocket_Disable_Events(socket)
149# define _GSocket_Install_Callback(socket, event)
150# define _GSocket_Uninstall_Callback(socket, event)
151#endif /* wxUSE_GUI */
152
153/* debugging helpers */
3958ae62
SN
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
e32c4e60
SN
211void GSocket_close(GSocket *socket)
212{
213 _GSocket_Disable_Events(socket);
214 soclose(socket->m_fd);
215 socket->m_fd = INVALID_SOCKET;
216}
217
fb9010ed
DW
218void GSocket_destroy(GSocket *socket)
219{
220 assert(socket != NULL);
221
3958ae62
SN
222 /* Check that the socket is really shutdowned */
223 if (socket->m_fd != INVALID_SOCKET)
fb9010ed
DW
224 GSocket_Shutdown(socket);
225
3958ae62
SN
226 /* Per-socket GUI-specific cleanup */
227 _GSocket_GUI_Destroy(socket);
228
229 /* Destroy private addresses */
fb9010ed
DW
230 if (socket->m_local)
231 GAddress_destroy(socket->m_local);
232
233 if (socket->m_peer)
234 GAddress_destroy(socket->m_peer);
235
3958ae62 236 /* Destroy the socket itself */
fb9010ed
DW
237 free(socket);
238}
239
3958ae62
SN
240/* GSocket_Shutdown:
241 * Disallow further read/write operations on this socket, close
242 * the fd and disable all callbacks.
243 */
fb9010ed
DW
244void GSocket_Shutdown(GSocket *socket)
245{
246 int evt;
247
248 assert(socket != NULL);
249
3958ae62
SN
250 /* If socket has been created, shutdown it */
251 if (socket->m_fd != INVALID_SOCKET)
fb9010ed
DW
252 {
253 shutdown(socket->m_fd, 2);
e32c4e60 254 GSocket_close(socket);
fb9010ed
DW
255 }
256
3958ae62 257 /* Disable GUI callbacks */
fb9010ed
DW
258 for (evt = 0; evt < GSOCK_MAX_EVENT; evt++)
259 socket->m_cbacks[evt] = NULL;
260
3958ae62
SN
261 socket->m_detected = GSOCK_LOST_FLAG;
262 _GSocket_Disable_Events(socket);
fb9010ed
DW
263}
264
265/* Address handling */
266
3958ae62
SN
267/* GSocket_SetLocal:
268 * GSocket_GetLocal:
269 * GSocket_SetPeer:
270 * GSocket_GetPeer:
271 * Set or get the local or peer address for this socket. The 'set'
272 * functions return GSOCK_NOERROR on success, an error code otherwise.
273 * The 'get' functions return a pointer to a GAddress object on success,
274 * or NULL otherwise, in which case they set the error code of the
275 * corresponding GSocket.
276 *
277 * Error codes:
278 * GSOCK_INVSOCK - the socket is not valid.
279 * GSOCK_INVADDR - the address is not valid.
280 */
fb9010ed
DW
281GSocketError GSocket_SetLocal(GSocket *socket, GAddress *address)
282{
283 assert(socket != NULL);
284
3958ae62
SN
285 /* the socket must be initialized, or it must be a server */
286 if ((socket->m_fd != INVALID_SOCKET && !socket->m_server))
287 {
fb9010ed
DW
288 socket->m_error = GSOCK_INVSOCK;
289 return GSOCK_INVSOCK;
290 }
291
3958ae62
SN
292 /* check address */
293 if (address == NULL || address->m_family == GSOCK_NOFAMILY)
294 {
fb9010ed
DW
295 socket->m_error = GSOCK_INVADDR;
296 return GSOCK_INVADDR;
297 }
298
299 if (socket->m_local)
300 GAddress_destroy(socket->m_local);
301
302 socket->m_local = GAddress_copy(address);
303
304 return GSOCK_NOERROR;
305}
306
307GSocketError GSocket_SetPeer(GSocket *socket, GAddress *address)
308{
309 assert(socket != NULL);
310
3958ae62
SN
311 /* check address */
312 if (address == NULL || address->m_family == GSOCK_NOFAMILY)
313 {
fb9010ed
DW
314 socket->m_error = GSOCK_INVADDR;
315 return GSOCK_INVADDR;
316 }
317
318 if (socket->m_peer)
319 GAddress_destroy(socket->m_peer);
320
321 socket->m_peer = GAddress_copy(address);
322
323 return GSOCK_NOERROR;
324}
325
326GAddress *GSocket_GetLocal(GSocket *socket)
327{
328 GAddress *address;
329 struct sockaddr addr;
3958ae62 330 SOCKLEN_T size = sizeof(addr);
fb9010ed
DW
331 GSocketError err;
332
333 assert(socket != NULL);
334
3958ae62 335 /* try to get it from the m_local var first */
fb9010ed
DW
336 if (socket->m_local)
337 return GAddress_copy(socket->m_local);
338
3958ae62
SN
339 /* else, if the socket is initialized, try getsockname */
340 if (socket->m_fd == INVALID_SOCKET)
341 {
fb9010ed
DW
342 socket->m_error = GSOCK_INVSOCK;
343 return NULL;
344 }
345
3958ae62
SN
346 if (getsockname(socket->m_fd, &addr, (SOCKLEN_T *) &size) < 0)
347 {
fb9010ed
DW
348 socket->m_error = GSOCK_IOERR;
349 return NULL;
350 }
351
3958ae62 352 /* got a valid address from getsockname, create a GAddress object */
fb9010ed 353 address = GAddress_new();
3958ae62
SN
354 if (address == NULL)
355 {
fb9010ed
DW
356 socket->m_error = GSOCK_MEMERR;
357 return NULL;
358 }
3958ae62
SN
359
360 err = _GAddress_translate_from(address, &addr, size);
361 if (err != GSOCK_NOERROR)
362 {
fb9010ed 363 GAddress_destroy(address);
3958ae62 364 socket->m_error = err;
fb9010ed
DW
365 return NULL;
366 }
367
368 return address;
369}
370
371GAddress *GSocket_GetPeer(GSocket *socket)
372{
373 assert(socket != NULL);
374
3958ae62 375 /* try to get it from the m_peer var */
fb9010ed
DW
376 if (socket->m_peer)
377 return GAddress_copy(socket->m_peer);
378
379 return NULL;
380}
381
382/* Server specific parts */
383
384/* GSocket_SetServer:
3958ae62
SN
385 * Sets up this socket as a server. The local address must have been
386 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
387 * Returns GSOCK_NOERROR on success, one of the following otherwise:
e32c4e60 388 *
3958ae62
SN
389 * Error codes:
390 * GSOCK_INVSOCK - the socket is in use.
391 * GSOCK_INVADDR - the local address has not been set.
e32c4e60 392 * GSOCK_IOERR - low-level error.
fb9010ed
DW
393 */
394GSocketError GSocket_SetServer(GSocket *sck)
395{
fb9010ed
DW
396 int arg = 1;
397
398 assert(sck != NULL);
399
3958ae62
SN
400 /* must not be in use */
401 if (sck->m_fd != INVALID_SOCKET)
402 {
fb9010ed
DW
403 sck->m_error = GSOCK_INVSOCK;
404 return GSOCK_INVSOCK;
405 }
406
3958ae62
SN
407 /* the local addr must have been set */
408 if (!sck->m_local)
409 {
fb9010ed
DW
410 sck->m_error = GSOCK_INVADDR;
411 return GSOCK_INVADDR;
412 }
413
3958ae62
SN
414 /* Initialize all fields */
415 sck->m_stream = TRUE;
416 sck->m_server = TRUE;
417 sck->m_oriented = TRUE;
fb9010ed
DW
418
419 /* Create the socket */
420 sck->m_fd = socket(sck->m_local->m_realfamily, SOCK_STREAM, 0);
421
3958ae62
SN
422 if (sck->m_fd == INVALID_SOCKET)
423 {
fb9010ed
DW
424 sck->m_error = GSOCK_IOERR;
425 return GSOCK_IOERR;
426 }
427
19193a2c 428 ioctl(sck->m_fd, FIONBIO, (char*)&arg, sizeof(arg));
3958ae62
SN
429 _GSocket_Enable_Events(sck);
430
431 /* Bind to the local address,
432 * retrieve the actual address bound,
433 * and listen up to 5 connections.
434 */
435 if ((bind(sck->m_fd, sck->m_local->m_addr, sck->m_local->m_len) != 0) ||
436 (getsockname(sck->m_fd,
437 sck->m_local->m_addr,
438 (SOCKLEN_T *) &sck->m_local->m_len) != 0) ||
439 (listen(sck->m_fd, 5) != 0))
440 {
e32c4e60 441 GSocket_close(sck);
fb9010ed
DW
442 sck->m_error = GSOCK_IOERR;
443 return GSOCK_IOERR;
444 }
445
446 return GSOCK_NOERROR;
447}
448
449/* GSocket_WaitConnection:
3958ae62
SN
450 * Waits for an incoming client connection. Returns a pointer to
451 * a GSocket object, or NULL if there was an error, in which case
452 * the last error field will be updated for the calling GSocket.
453 *
454 * Error codes (set in the calling GSocket)
455 * GSOCK_INVSOCK - the socket is not valid or not a server.
456 * GSOCK_TIMEDOUT - timeout, no incoming connections.
457 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
458 * GSOCK_MEMERR - couldn't allocate memory.
e32c4e60 459 * GSOCK_IOERR - low-level error.
fb9010ed
DW
460 */
461GSocket *GSocket_WaitConnection(GSocket *socket)
462{
3958ae62
SN
463 struct sockaddr from;
464 SOCKLEN_T fromlen = sizeof(from);
fb9010ed 465 GSocket *connection;
3958ae62 466 GSocketError err;
fb9010ed
DW
467 int arg = 1;
468
469 assert(socket != NULL);
470
471 /* Reenable CONNECTION events */
472 _GSocket_Enable(socket, GSOCK_CONNECTION);
473
474 /* If the socket has already been created, we exit immediately */
3958ae62 475 if (socket->m_fd == INVALID_SOCKET || !socket->m_server)
fb9010ed
DW
476 {
477 socket->m_error = GSOCK_INVSOCK;
478 return NULL;
479 }
480
481 /* Create a GSocket object for the new connection */
482 connection = GSocket_new();
3958ae62 483
fb9010ed
DW
484 if (!connection)
485 {
3958ae62 486 socket->m_error = GSOCK_MEMERR;
fb9010ed
DW
487 return NULL;
488 }
489
3958ae62 490 /* Wait for a connection (with timeout) */
fb9010ed
DW
491 if (_GSocket_Input_Timeout(socket) == GSOCK_TIMEDOUT)
492 {
493 GSocket_destroy(connection);
494 /* socket->m_error set by _GSocket_Input_Timeout */
495 return NULL;
496 }
497
3958ae62 498 connection->m_fd = accept(socket->m_fd, &from, (SOCKLEN_T *) &fromlen);
fb9010ed 499
3958ae62 500 if (connection->m_fd == INVALID_SOCKET)
fb9010ed
DW
501 {
502 if (errno == EWOULDBLOCK)
503 socket->m_error = GSOCK_WOULDBLOCK;
504 else
505 socket->m_error = GSOCK_IOERR;
506
507 GSocket_destroy(connection);
508 return NULL;
509 }
510
511 /* Initialize all fields */
3958ae62
SN
512 connection->m_server = FALSE;
513 connection->m_stream = TRUE;
514 connection->m_oriented = TRUE;
fb9010ed 515
3958ae62
SN
516 /* Setup the peer address field */
517 connection->m_peer = GAddress_new();
518 if (!connection->m_peer)
519 {
520 GSocket_destroy(connection);
521 socket->m_error = GSOCK_MEMERR;
522 return NULL;
fb9010ed 523 }
3958ae62
SN
524 err = _GAddress_translate_from(connection->m_peer, &from, fromlen);
525 if (err != GSOCK_NOERROR)
526 {
527 GAddress_destroy(connection->m_peer);
528 GSocket_destroy(connection);
529 socket->m_error = err;
530 return NULL;
fb9010ed
DW
531 }
532
19193a2c 533 ioctl(connection->m_fd, FIONBIO, (char*)&arg, sizeof(arg));
3958ae62 534 _GSocket_Enable_Events(connection);
fb9010ed 535
3958ae62 536 return connection;
fb9010ed
DW
537}
538
539/* Client specific parts */
540
541/* GSocket_Connect:
3958ae62
SN
542 * For stream (connection oriented) sockets, GSocket_Connect() tries
543 * to establish a client connection to a server using the peer address
544 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
545 * connection has been succesfully established, or one of the error
546 * codes listed below. Note that for nonblocking sockets, a return
547 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
548 * request can be completed later; you should use GSocket_Select()
549 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
550 * corresponding asynchronous events.
551 *
552 * For datagram (non connection oriented) sockets, GSocket_Connect()
553 * just sets the peer address established with GSocket_SetPeer() as
554 * default destination.
555 *
556 * Error codes:
557 * GSOCK_INVSOCK - the socket is in use or not valid.
558 * GSOCK_INVADDR - the peer address has not been established.
559 * GSOCK_TIMEDOUT - timeout, the connection failed.
560 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
561 * GSOCK_MEMERR - couldn't allocate memory.
e32c4e60 562 * GSOCK_IOERR - low-level error.
fb9010ed
DW
563 */
564GSocketError GSocket_Connect(GSocket *sck, GSocketStream stream)
565{
3958ae62 566 int err, ret;
fb9010ed
DW
567 int arg = 1;
568
569 assert(sck != NULL);
570
571 /* Enable CONNECTION events (needed for nonblocking connections) */
572 _GSocket_Enable(sck, GSOCK_CONNECTION);
573
3958ae62 574 if (sck->m_fd != INVALID_SOCKET)
fb9010ed
DW
575 {
576 sck->m_error = GSOCK_INVSOCK;
577 return GSOCK_INVSOCK;
578 }
579
580 if (!sck->m_peer)
581 {
582 sck->m_error = GSOCK_INVADDR;
583 return GSOCK_INVADDR;
584 }
585
3958ae62 586 /* Streamed or dgram socket? */
fb9010ed 587 sck->m_stream = (stream == GSOCK_STREAMED);
3958ae62
SN
588 sck->m_oriented = TRUE;
589 sck->m_server = FALSE;
590 sck->m_establishing = FALSE;
fb9010ed
DW
591
592 /* Create the socket */
3958ae62
SN
593 sck->m_fd = socket(sck->m_peer->m_realfamily,
594 sck->m_stream? SOCK_STREAM : SOCK_DGRAM, 0);
fb9010ed 595
3958ae62
SN
596 if (sck->m_fd == INVALID_SOCKET)
597 {
fb9010ed
DW
598 sck->m_error = GSOCK_IOERR;
599 return GSOCK_IOERR;
600 }
601
19193a2c 602 ioctl(sck->m_fd, FIONBIO, (char*)&arg, sizeof(arg));
3958ae62 603 _GSocket_Enable_Events(sck);
fb9010ed 604
3958ae62 605 /* Connect it to the peer address, with a timeout (see below) */
fb9010ed
DW
606 ret = connect(sck->m_fd, sck->m_peer->m_addr, sck->m_peer->m_len);
607
608 if (ret == -1)
609 {
610 err = errno;
611
612 /* If connect failed with EINPROGRESS and the GSocket object
613 * is in blocking mode, we select() for the specified timeout
614 * checking for writability to see if the connection request
615 * completes.
616 */
617 if ((err == EINPROGRESS) && (!sck->m_non_blocking))
618 {
619 if (_GSocket_Output_Timeout(sck) == GSOCK_TIMEDOUT)
620 {
e32c4e60 621 GSocket_close(sck);
fb9010ed 622 /* sck->m_error is set in _GSocket_Output_Timeout */
fb9010ed
DW
623 return GSOCK_TIMEDOUT;
624 }
625 else
626 {
3958ae62
SN
627 int error;
628 SOCKLEN_T len = sizeof(error);
629
630 getsockopt(sck->m_fd, SOL_SOCKET, SO_ERROR, (void*) &error, &len);
631
632 if (!error)
633 return GSOCK_NOERROR;
fb9010ed
DW
634 }
635 }
636
637 /* If connect failed with EINPROGRESS and the GSocket object
638 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
639 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
640 * this way if the connection completes, a GSOCK_CONNECTION
641 * event will be generated, if enabled.
642 */
643 if ((err == EINPROGRESS) && (sck->m_non_blocking))
644 {
3958ae62 645 sck->m_establishing = TRUE;
fb9010ed 646 sck->m_error = GSOCK_WOULDBLOCK;
fb9010ed
DW
647 return GSOCK_WOULDBLOCK;
648 }
649
650 /* If connect failed with an error other than EINPROGRESS,
651 * then the call to GSocket_Connect has failed.
652 */
e32c4e60 653 GSocket_close(sck);
fb9010ed 654 sck->m_error = GSOCK_IOERR;
3958ae62
SN
655 return GSOCK_IOERR;
656 }
fb9010ed 657
3958ae62
SN
658 return GSOCK_NOERROR;
659}
660
661/* Datagram sockets */
662
663/* GSocket_SetNonOriented:
664 * Sets up this socket as a non-connection oriented (datagram) socket.
665 * Before using this function, the local address must have been set
666 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
667 * on success, or one of the following otherwise.
668 *
669 * Error codes:
670 * GSOCK_INVSOCK - the socket is in use.
671 * GSOCK_INVADDR - the local address has not been set.
672 * GSOCK_IOERR - low-level error.
673 */
674GSocketError GSocket_SetNonOriented(GSocket *sck)
675{
676 int arg = 1;
677
678 assert(sck != NULL);
679
680 if (sck->m_fd != INVALID_SOCKET)
681 {
682 sck->m_error = GSOCK_INVSOCK;
683 return GSOCK_INVSOCK;
684 }
685
686 if (!sck->m_local)
687 {
688 sck->m_error = GSOCK_INVADDR;
689 return GSOCK_INVADDR;
690 }
691
692 /* Initialize all fields */
693 sck->m_stream = FALSE;
694 sck->m_server = FALSE;
695 sck->m_oriented = FALSE;
696
697 /* Create the socket */
698 sck->m_fd = socket(sck->m_local->m_realfamily, SOCK_DGRAM, 0);
699
700 if (sck->m_fd == INVALID_SOCKET)
701 {
702 sck->m_error = GSOCK_IOERR;
703 return GSOCK_IOERR;
704 }
705
19193a2c 706 ioctl(sck->m_fd, FIONBIO, (char*)&arg, sizeof(arg));
3958ae62
SN
707 _GSocket_Enable_Events(sck);
708
709 /* Bind to the local address,
710 * and retrieve the actual address bound.
711 */
712 if ((bind(sck->m_fd, sck->m_local->m_addr, sck->m_local->m_len) != 0) ||
713 (getsockname(sck->m_fd,
714 sck->m_local->m_addr,
715 (SOCKLEN_T *) &sck->m_local->m_len) != 0))
716 {
e32c4e60 717 GSocket_close(sck);
3958ae62 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);
e32c4e60 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 }
e32c4e60 760
fb9010ed
DW
761 return ret;
762}
763
3958ae62 764int GSocket_Write(GSocket *socket, const char *buffer, int size)
e32c4e60 765{
fb9010ed
DW
766 int ret;
767
768 assert(socket != NULL);
e32c4e60 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);
e32c4e60 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 }
e32c4e60 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
e32c4e60
SN
851 /* Check 'sticky' CONNECTION flag first */
852 result |= (GSOCK_CONNECTION_FLAG & socket->m_detected);
853
854 /* If we have already detected a LOST event, then don't try
855 * to do any further processing.
856 */
857 if ((socket->m_detected & GSOCK_LOST_FLAG) != 0)
858 {
859 socket->m_establishing = FALSE;
860
861 return (GSOCK_LOST_FLAG & flags);
862 }
3958ae62
SN
863
864 /* Try select now */
865 if (select(socket->m_fd + 1, &readfds, &writefds, &exceptfds, &tv) <= 0)
e32c4e60
SN
866 {
867 /* What to do here? */
868 return (result & flags);
869 }
3958ae62
SN
870
871 /* Check for readability */
872 if (FD_ISSET(socket->m_fd, &readfds))
873 {
874 char c;
875
876 if (recv(socket->m_fd, &c, 1, MSG_PEEK) > 0)
877 {
e32c4e60 878 result |= GSOCK_INPUT_FLAG;
3958ae62
SN
879 }
880 else
881 {
882 if (socket->m_server && socket->m_stream)
883 {
e32c4e60 884 result |= GSOCK_CONNECTION_FLAG;
3958ae62
SN
885 socket->m_detected |= GSOCK_CONNECTION_FLAG;
886 }
887 else
888 {
3958ae62 889 socket->m_detected = GSOCK_LOST_FLAG;
e32c4e60
SN
890 socket->m_establishing = FALSE;
891
892 /* LOST event: Abort any further processing */
893 return (GSOCK_LOST_FLAG & flags);
3958ae62
SN
894 }
895 }
896 }
897
898 /* Check for writability */
899 if (FD_ISSET(socket->m_fd, &writefds))
900 {
901 if (socket->m_establishing && !socket->m_server)
902 {
903 int error;
904 SOCKLEN_T len = sizeof(error);
905
906 socket->m_establishing = FALSE;
907
908 getsockopt(socket->m_fd, SOL_SOCKET, SO_ERROR, (void*)&error, &len);
909
910 if (error)
911 {
3958ae62 912 socket->m_detected = GSOCK_LOST_FLAG;
e32c4e60
SN
913
914 /* LOST event: Abort any further processing */
915 return (GSOCK_LOST_FLAG & flags);
3958ae62
SN
916 }
917 else
918 {
e32c4e60 919 result |= GSOCK_CONNECTION_FLAG;
3958ae62
SN
920 socket->m_detected |= GSOCK_CONNECTION_FLAG;
921 }
922 }
923 else
924 {
e32c4e60 925 result |= GSOCK_OUTPUT_FLAG;
3958ae62
SN
926 }
927 }
928
929 /* Check for exceptions and errors (is this useful in Unices?) */
930 if (FD_ISSET(socket->m_fd, &exceptfds))
931 {
3958ae62
SN
932 socket->m_establishing = FALSE;
933 socket->m_detected = GSOCK_LOST_FLAG;
e32c4e60
SN
934
935 /* LOST event: Abort any further processing */
936 return (GSOCK_LOST_FLAG & flags);
3958ae62
SN
937 }
938
e32c4e60 939 return (result & flags);
3958ae62 940
e32c4e60 941#else
3958ae62
SN
942
943 assert(socket != NULL);
944 return flags & socket->m_detected;
945
946#endif /* !wxUSE_GUI */
fb9010ed
DW
947}
948
949/* Flags */
950
951/* GSocket_SetNonBlocking:
3958ae62
SN
952 * Sets the socket to non-blocking mode. All IO calls will return
953 * immediately.
fb9010ed 954 */
27f37ae5 955void GSocket_SetNonBlocking(GSocket *socket, int non_block)
fb9010ed
DW
956{
957 assert(socket != NULL);
958
3958ae62
SN
959 GSocket_Debug( ("GSocket_SetNonBlocking: %d\n", (int)non_block) );
960
fb9010ed
DW
961 socket->m_non_blocking = non_block;
962}
963
964/* GSocket_SetTimeout:
3958ae62
SN
965 * Sets the timeout for blocking calls. Time is expressed in
966 * milliseconds.
fb9010ed
DW
967 */
968void GSocket_SetTimeout(GSocket *socket, unsigned long millisec)
969{
970 assert(socket != NULL);
971
972 socket->m_timeout = millisec;
973}
974
975/* GSocket_GetError:
3958ae62
SN
976 * Returns the last error occured for this socket. Note that successful
977 * operations do not clear this back to GSOCK_NOERROR, so use it only
978 * after an error.
fb9010ed
DW
979 */
980GSocketError GSocket_GetError(GSocket *socket)
981{
982 assert(socket != NULL);
983
984 return socket->m_error;
985}
986
987/* Callbacks */
988
3958ae62
SN
989/* GSOCK_INPUT:
990 * There is data to be read in the input buffer. If, after a read
991 * operation, there is still data available, the callback function will
992 * be called again.
993 * GSOCK_OUTPUT:
e32c4e60 994 * The socket is available for writing. That is, the next write call
3958ae62
SN
995 * won't block. This event is generated only once, when the connection is
996 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
997 * when the output buffer empties again. This means that the app should
998 * assume that it can write since the first OUTPUT event, and no more
999 * OUTPUT events will be generated unless an error occurs.
1000 * GSOCK_CONNECTION:
1001 * Connection succesfully established, for client sockets, or incoming
1002 * client connection, for server sockets. Wait for this event (also watch
1003 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
1004 * GSOCK_LOST:
1005 * The connection is lost (or a connection request failed); this could
1006 * be due to a failure, or due to the peer closing it gracefully.
fb9010ed
DW
1007 */
1008
1009/* GSocket_SetCallback:
1010 * Enables the callbacks specified by 'flags'. Note that 'flags'
1011 * may be a combination of flags OR'ed toghether, so the same
1012 * callback function can be made to accept different events.
1013 * The callback function must have the following prototype:
1014 *
1015 * void function(GSocket *socket, GSocketEvent event, char *cdata)
1016 */
1017void GSocket_SetCallback(GSocket *socket, GSocketEventFlags flags,
3958ae62 1018 GSocketCallback callback, char *cdata)
fb9010ed
DW
1019{
1020 int count;
1021
1022 assert(socket != NULL);
1023
1024 for (count = 0; count < GSOCK_MAX_EVENT; count++)
1025 {
1026 if ((flags & (1 << count)) != 0)
1027 {
1028 socket->m_cbacks[count] = callback;
1029 socket->m_data[count] = cdata;
1030 }
1031 }
1032}
1033
1034/* GSocket_UnsetCallback:
1035 * Disables all callbacks specified by 'flags', which may be a
1036 * combination of flags OR'ed toghether.
1037 */
1038void GSocket_UnsetCallback(GSocket *socket, GSocketEventFlags flags)
1039{
1040 int count;
1041
1042 assert(socket != NULL);
1043
1044 for (count = 0; count < GSOCK_MAX_EVENT; count++)
1045 {
1046 if ((flags & (1 << count)) != 0)
1047 {
1048 socket->m_cbacks[count] = NULL;
1049 socket->m_data[count] = NULL;
1050 }
1051 }
1052}
1053
3958ae62 1054
fb9010ed
DW
1055#define CALL_CALLBACK(socket, event) { \
1056 _GSocket_Disable(socket, event); \
e32c4e60 1057 if (socket->m_cbacks[event]) \
fb9010ed
DW
1058 socket->m_cbacks[event](socket, event, socket->m_data[event]); \
1059}
1060
1061
1062void _GSocket_Enable(GSocket *socket, GSocketEvent event)
1063{
1064 socket->m_detected &= ~(1 << event);
3958ae62 1065 _GSocket_Install_Callback(socket, event);
fb9010ed
DW
1066}
1067
1068void _GSocket_Disable(GSocket *socket, GSocketEvent event)
1069{
1070 socket->m_detected |= (1 << event);
3958ae62 1071 _GSocket_Uninstall_Callback(socket, event);
fb9010ed
DW
1072}
1073
1074/* _GSocket_Input_Timeout:
1075 * For blocking sockets, wait until data is available or
1076 * until timeout ellapses.
1077 */
1078GSocketError _GSocket_Input_Timeout(GSocket *socket)
1079{
1080 struct timeval tv;
1081 fd_set readfds;
3958ae62 1082 int ret;
fb9010ed 1083
3958ae62 1084 /* Linux select() will overwrite the struct on return */
fb9010ed
DW
1085 tv.tv_sec = (socket->m_timeout / 1000);
1086 tv.tv_usec = (socket->m_timeout % 1000) * 1000;
1087
1088 if (!socket->m_non_blocking)
1089 {
1090 FD_ZERO(&readfds);
1091 FD_SET(socket->m_fd, &readfds);
3958ae62
SN
1092 ret = select(socket->m_fd + 1, &readfds, NULL, NULL, &tv);
1093 if (ret == 0)
fb9010ed 1094 {
3958ae62
SN
1095 GSocket_Debug(( "GSocket_Input_Timeout, select returned 0\n" ));
1096 socket->m_error = GSOCK_TIMEDOUT;
1097 return GSOCK_TIMEDOUT;
1098 }
1099 if (ret == -1)
1100 {
1101 GSocket_Debug(( "GSocket_Input_Timeout, select returned -1\n" ));
1102 if (errno == EBADF) GSocket_Debug(( "Invalid file descriptor\n" ));
1103 if (errno == EINTR) GSocket_Debug(( "A non blocked signal was caught\n" ));
1104 if (errno == EINVAL) GSocket_Debug(( "The highest number descriptor is negative\n" ));
1105 if (errno == ENOMEM) GSocket_Debug(( "Not enough memory\n" ));
fb9010ed
DW
1106 socket->m_error = GSOCK_TIMEDOUT;
1107 return GSOCK_TIMEDOUT;
1108 }
1109 }
1110 return GSOCK_NOERROR;
1111}
1112
1113/* _GSocket_Output_Timeout:
1114 * For blocking sockets, wait until data can be sent without
1115 * blocking or until timeout ellapses.
1116 */
1117GSocketError _GSocket_Output_Timeout(GSocket *socket)
1118{
1119 struct timeval tv;
1120 fd_set writefds;
3958ae62 1121 int ret;
fb9010ed 1122
3958ae62 1123 /* Linux select() will overwrite the struct on return */
fb9010ed
DW
1124 tv.tv_sec = (socket->m_timeout / 1000);
1125 tv.tv_usec = (socket->m_timeout % 1000) * 1000;
1126
3958ae62
SN
1127 GSocket_Debug( ("m_non_blocking has: %d\n", (int)socket->m_non_blocking) );
1128
fb9010ed
DW
1129 if (!socket->m_non_blocking)
1130 {
1131 FD_ZERO(&writefds);
1132 FD_SET(socket->m_fd, &writefds);
3958ae62
SN
1133 ret = select(socket->m_fd + 1, NULL, &writefds, NULL, &tv);
1134 if (ret == 0)
fb9010ed 1135 {
3958ae62 1136 GSocket_Debug(( "GSocket_Output_Timeout, select returned 0\n" ));
fb9010ed
DW
1137 socket->m_error = GSOCK_TIMEDOUT;
1138 return GSOCK_TIMEDOUT;
1139 }
3958ae62
SN
1140 if (ret == -1)
1141 {
1142 GSocket_Debug(( "GSocket_Output_Timeout, select returned -1\n" ));
1143 if (errno == EBADF) GSocket_Debug(( "Invalid file descriptor\n" ));
1144 if (errno == EINTR) GSocket_Debug(( "A non blocked signal was caught\n" ));
1145 if (errno == EINVAL) GSocket_Debug(( "The highest number descriptor is negative\n" ));
1146 if (errno == ENOMEM) GSocket_Debug(( "Not enough memory\n" ));
1147 socket->m_error = GSOCK_TIMEDOUT;
1148 return GSOCK_TIMEDOUT;
1149 }
1150 if ( ! FD_ISSET(socket->m_fd, &writefds) )
1151 GSocket_Debug(( "GSocket_Output_Timeout is buggy!\n" ));
1152 else
1153 GSocket_Debug(( "GSocket_Output_Timeout seems correct\n" ));
1154 }
1155 else
1156 {
1157 GSocket_Debug(( "GSocket_Output_Timeout, didn't try select!\n" ));
fb9010ed 1158 }
3958ae62 1159
fb9010ed
DW
1160 return GSOCK_NOERROR;
1161}
1162
1163int _GSocket_Recv_Stream(GSocket *socket, char *buffer, int size)
1164{
3958ae62 1165 return recv(socket->m_fd, buffer, size, 0);
fb9010ed
DW
1166}
1167
1168int _GSocket_Recv_Dgram(GSocket *socket, char *buffer, int size)
1169{
1170 struct sockaddr from;
3958ae62 1171 SOCKLEN_T fromlen = sizeof(from);
fb9010ed
DW
1172 int ret;
1173 GSocketError err;
1174
1175 fromlen = sizeof(from);
1176
3958ae62 1177 ret = recvfrom(socket->m_fd, buffer, size, 0, &from, (SOCKLEN_T *) &fromlen);
fb9010ed
DW
1178
1179 if (ret == -1)
1180 return -1;
1181
1182 /* Translate a system address into a GSocket address */
1183 if (!socket->m_peer)
1184 {
1185 socket->m_peer = GAddress_new();
1186 if (!socket->m_peer)
1187 {
1188 socket->m_error = GSOCK_MEMERR;
1189 return -1;
1190 }
1191 }
1192 err = _GAddress_translate_from(socket->m_peer, &from, fromlen);
1193 if (err != GSOCK_NOERROR)
1194 {
1195 GAddress_destroy(socket->m_peer);
1196 socket->m_peer = NULL;
1197 socket->m_error = err;
1198 return -1;
1199 }
1200
1201 return ret;
1202}
1203
1204int _GSocket_Send_Stream(GSocket *socket, const char *buffer, int size)
1205{
1206 int ret;
fb9010ed 1207
29d83fc1 1208#ifdef __EMX__
3958ae62
SN
1209 MASK_SIGNAL();
1210 ret = send(socket->m_fd, buffer, size, 0);
1211 UNMASK_SIGNAL();
29d83fc1 1212#else
19193a2c 1213 ret = send(socket->m_fd, (char *)buffer, size, 0);
29d83fc1 1214#endif
fb9010ed
DW
1215 return ret;
1216}
1217
1218int _GSocket_Send_Dgram(GSocket *socket, const char *buffer, int size)
1219{
1220 struct sockaddr *addr;
1221 int len, ret;
1222 GSocketError err;
1223
3958ae62
SN
1224 if (!socket->m_peer)
1225 {
fb9010ed
DW
1226 socket->m_error = GSOCK_INVADDR;
1227 return -1;
1228 }
1229
1230 err = _GAddress_translate_to(socket->m_peer, &addr, &len);
3958ae62
SN
1231 if (err != GSOCK_NOERROR)
1232 {
fb9010ed
DW
1233 socket->m_error = err;
1234 return -1;
1235 }
1236
29d83fc1 1237#ifdef __EMX__
3958ae62
SN
1238 MASK_SIGNAL();
1239 ret = sendto(socket->m_fd, buffer, size, 0, addr, len);
1240 UNMASK_SIGNAL();
29d83fc1 1241#else
19193a2c 1242 ret = sendto(socket->m_fd, (char *)buffer, size, 0, addr, len);
29d83fc1 1243#endif
fb9010ed
DW
1244
1245 /* Frees memory allocated from _GAddress_translate_to */
1246 free(addr);
1247
1248 return ret;
1249}
1250
1251void _GSocket_Detected_Read(GSocket *socket)
1252{
1253 char c;
fb9010ed 1254
e32c4e60
SN
1255 /* If we have already detected a LOST event, then don't try
1256 * to do any further processing.
1257 */
1258 if ((socket->m_detected & GSOCK_LOST_FLAG) != 0)
1259 {
1260 socket->m_establishing = FALSE;
1261
1262 CALL_CALLBACK(socket, GSOCK_LOST);
1263 GSocket_Shutdown(socket);
1264 return;
1265 }
1266
3958ae62 1267 if (recv(socket->m_fd, &c, 1, MSG_PEEK) > 0)
fb9010ed 1268 {
3958ae62
SN
1269 CALL_CALLBACK(socket, GSOCK_INPUT);
1270 }
1271 else
1272 {
1273 if (socket->m_server && socket->m_stream)
fb9010ed
DW
1274 {
1275 CALL_CALLBACK(socket, GSOCK_CONNECTION);
fb9010ed
DW
1276 }
1277 else
1278 {
1279 CALL_CALLBACK(socket, GSOCK_LOST);
e32c4e60 1280 GSocket_Shutdown(socket);
fb9010ed
DW
1281 }
1282 }
1283}
1284
1285void _GSocket_Detected_Write(GSocket *socket)
1286{
e32c4e60
SN
1287 /* If we have already detected a LOST event, then don't try
1288 * to do any further processing.
1289 */
1290 if ((socket->m_detected & GSOCK_LOST_FLAG) != 0)
1291 {
1292 socket->m_establishing = FALSE;
1293
1294 CALL_CALLBACK(socket, GSOCK_LOST);
1295 GSocket_Shutdown(socket);
1296 return;
1297 }
1298
fb9010ed
DW
1299 if (socket->m_establishing && !socket->m_server)
1300 {
3958ae62
SN
1301 int error;
1302 SOCKLEN_T len = sizeof(error);
fb9010ed 1303
3958ae62 1304 socket->m_establishing = FALSE;
fb9010ed 1305
3958ae62 1306 getsockopt(socket->m_fd, SOL_SOCKET, SO_ERROR, (void*)&error, &len);
fb9010ed
DW
1307
1308 if (error)
1309 {
1310 CALL_CALLBACK(socket, GSOCK_LOST);
e32c4e60 1311 GSocket_Shutdown(socket);
fb9010ed
DW
1312 }
1313 else
1314 {
1315 CALL_CALLBACK(socket, GSOCK_CONNECTION);
1316 /* We have to fire this event by hand because CONNECTION (for clients)
1317 * and OUTPUT are internally the same and we just disabled CONNECTION
1318 * events with the above macro.
1319 */
1320 CALL_CALLBACK(socket, GSOCK_OUTPUT);
1321 }
1322 }
1323 else
1324 {
1325 CALL_CALLBACK(socket, GSOCK_OUTPUT);
1326 }
1327}
1328
1329/*
1330 * -------------------------------------------------------------------------
1331 * GAddress
1332 * -------------------------------------------------------------------------
1333 */
1334
3958ae62
SN
1335/* CHECK_ADDRESS verifies that the current address family is either
1336 * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it
1337 * initalizes it to be a GSOCK_*family*. In other cases, it returns
1338 * an appropiate error code.
1339 *
1340 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
fb9010ed 1341 */
3958ae62
SN
1342#define CHECK_ADDRESS(address, family) \
1343{ \
1344 if (address->m_family == GSOCK_NOFAMILY) \
1345 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1346 return address->m_error; \
1347 if (address->m_family != GSOCK_##family) \
1348 { \
1349 address->m_error = GSOCK_INVADDR; \
1350 return GSOCK_INVADDR; \
1351 } \
1352}
1353
1354#define CHECK_ADDRESS_RETVAL(address, family, retval) \
1355{ \
1356 if (address->m_family == GSOCK_NOFAMILY) \
1357 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1358 return retval; \
1359 if (address->m_family != GSOCK_##family) \
1360 { \
1361 address->m_error = GSOCK_INVADDR; \
1362 return retval; \
1363 } \
fb9010ed
DW
1364}
1365
3958ae62
SN
1366
1367GAddress *GAddress_new(void)
fb9010ed
DW
1368{
1369 GAddress *address;
1370
3958ae62 1371 if ((address = (GAddress *) malloc(sizeof(GAddress))) == NULL)
fb9010ed
DW
1372 return NULL;
1373
1374 address->m_family = GSOCK_NOFAMILY;
1375 address->m_addr = NULL;
1376 address->m_len = 0;
1377
1378 return address;
1379}
1380
1381GAddress *GAddress_copy(GAddress *address)
1382{
1383 GAddress *addr2;
1384
1385 assert(address != NULL);
1386
3958ae62 1387 if ((addr2 = (GAddress *) malloc(sizeof(GAddress))) == NULL)
fb9010ed
DW
1388 return NULL;
1389
1390 memcpy(addr2, address, sizeof(GAddress));
1391
e32c4e60 1392 if (address->m_addr && address->m_len > 0)
3958ae62 1393 {
fb9010ed 1394 addr2->m_addr = (struct sockaddr *)malloc(addr2->m_len);
3958ae62
SN
1395 if (addr2->m_addr == NULL)
1396 {
fb9010ed
DW
1397 free(addr2);
1398 return NULL;
1399 }
1400 memcpy(addr2->m_addr, address->m_addr, addr2->m_len);
1401 }
1402
1403 return addr2;
1404}
1405
1406void GAddress_destroy(GAddress *address)
1407{
1408 assert(address != NULL);
1409
3958ae62
SN
1410 if (address->m_addr)
1411 free(address->m_addr);
1412
1413/* free(address); */
fb9010ed
DW
1414}
1415
1416void GAddress_SetFamily(GAddress *address, GAddressType type)
1417{
1418 assert(address != NULL);
1419
1420 address->m_family = type;
1421}
1422
1423GAddressType GAddress_GetFamily(GAddress *address)
1424{
1425 assert(address != NULL);
1426
1427 return address->m_family;
1428}
1429
3958ae62
SN
1430GSocketError _GAddress_translate_from(GAddress *address,
1431 struct sockaddr *addr, int len)
1432{
fb9010ed 1433 address->m_realfamily = addr->sa_family;
3958ae62
SN
1434 switch (addr->sa_family)
1435 {
1436 case AF_INET:
1437 address->m_family = GSOCK_INET;
1438 break;
1439 case AF_UNIX:
1440 address->m_family = GSOCK_UNIX;
1441 break;
fb9010ed 1442#ifdef AF_INET6
3958ae62
SN
1443 case AF_INET6:
1444 address->m_family = GSOCK_INET6;
1445 break;
fb9010ed 1446#endif
3958ae62 1447 default:
fb9010ed 1448 {
3958ae62
SN
1449 address->m_error = GSOCK_INVOP;
1450 return GSOCK_INVOP;
fb9010ed
DW
1451 }
1452 }
1453
1454 if (address->m_addr)
1455 free(address->m_addr);
1456
1457 address->m_len = len;
1458 address->m_addr = (struct sockaddr *)malloc(len);
3958ae62
SN
1459
1460 if (address->m_addr == NULL)
1461 {
fb9010ed
DW
1462 address->m_error = GSOCK_MEMERR;
1463 return GSOCK_MEMERR;
1464 }
1465 memcpy(address->m_addr, addr, len);
1466
1467 return GSOCK_NOERROR;
1468}
1469
1470GSocketError _GAddress_translate_to(GAddress *address,
1471 struct sockaddr **addr, int *len)
1472{
3958ae62
SN
1473 if (!address->m_addr)
1474 {
fb9010ed
DW
1475 address->m_error = GSOCK_INVADDR;
1476 return GSOCK_INVADDR;
1477 }
1478
1479 *len = address->m_len;
1480 *addr = (struct sockaddr *)malloc(address->m_len);
3958ae62
SN
1481 if (*addr == NULL)
1482 {
fb9010ed
DW
1483 address->m_error = GSOCK_MEMERR;
1484 return GSOCK_MEMERR;
1485 }
1486
1487 memcpy(*addr, address->m_addr, address->m_len);
1488 return GSOCK_NOERROR;
1489}
1490
1491/*
1492 * -------------------------------------------------------------------------
1493 * Internet address family
1494 * -------------------------------------------------------------------------
1495 */
1496
1497GSocketError _GAddress_Init_INET(GAddress *address)
1498{
3958ae62
SN
1499 address->m_len = sizeof(struct sockaddr_in);
1500 address->m_addr = (struct sockaddr *) malloc(address->m_len);
1501 if (address->m_addr == NULL)
1502 {
fb9010ed
DW
1503 address->m_error = GSOCK_MEMERR;
1504 return GSOCK_MEMERR;
1505 }
1506
fb9010ed
DW
1507 address->m_family = GSOCK_INET;
1508 address->m_realfamily = PF_INET;
1509 ((struct sockaddr_in *)address->m_addr)->sin_family = AF_INET;
3958ae62 1510 ((struct sockaddr_in *)address->m_addr)->sin_addr.s_addr = INADDR_ANY;
fb9010ed
DW
1511
1512 return GSOCK_NOERROR;
1513}
1514
1515GSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname)
1516{
1517 struct hostent *he;
1518 struct in_addr *addr;
1519
1520 assert(address != NULL);
1521
3958ae62 1522 CHECK_ADDRESS(address, INET);
fb9010ed
DW
1523
1524 addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr);
1525
1526 /* If it is a numeric host name, convert it now */
1527#if defined(HAVE_INET_ATON)
3958ae62
SN
1528 if (inet_aton(hostname, addr) == 0)
1529 {
fb9010ed 1530#elif defined(HAVE_INET_ADDR)
29d83fc1 1531 addr->s_addr = inet_addr(hostname);
d75262db 1532 if ( addr->s_addr == -1 )
3958ae62
SN
1533 {
1534#else
1535 /* Use gethostbyname by default */
987da0d4
DW
1536 int val = 1; //VA doesn't like constants in conditional expressions at all
1537 if (val)
3958ae62 1538 {
fb9010ed
DW
1539#endif
1540 struct in_addr *array_addr;
1541
1542 /* It is a real name, we solve it */
3958ae62
SN
1543 if ((he = gethostbyname(hostname)) == NULL)
1544 {
1545 /* Reset to invalid address */
1546 addr->s_addr = INADDR_NONE;
fb9010ed
DW
1547 address->m_error = GSOCK_NOHOST;
1548 return GSOCK_NOHOST;
1549 }
1550 array_addr = (struct in_addr *) *(he->h_addr_list);
1551 addr->s_addr = array_addr[0].s_addr;
fb9010ed 1552 }
fb9010ed
DW
1553 return GSOCK_NOERROR;
1554}
1555
b662c135
SN
1556GSocketError GAddress_INET_SetAnyAddress(GAddress *address)
1557{
1558 return GAddress_INET_SetHostAddress(address, INADDR_ANY);
1559}
1560
fb9010ed
DW
1561GSocketError GAddress_INET_SetHostAddress(GAddress *address,
1562 unsigned long hostaddr)
1563{
1564 struct in_addr *addr;
1565
1566 assert(address != NULL);
1567
3958ae62 1568 CHECK_ADDRESS(address, INET);
fb9010ed
DW
1569
1570 addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr);
1571 addr->s_addr = hostaddr;
1572
1573 return GSOCK_NOERROR;
1574}
1575
1576GSocketError GAddress_INET_SetPortName(GAddress *address, const char *port,
1577 const char *protocol)
1578{
1579 struct servent *se;
1580 struct sockaddr_in *addr;
1581
1582 assert(address != NULL);
3958ae62 1583 CHECK_ADDRESS(address, INET);
fb9010ed 1584
3958ae62
SN
1585 if (!port)
1586 {
fb9010ed
DW
1587 address->m_error = GSOCK_INVPORT;
1588 return GSOCK_INVPORT;
1589 }
e32c4e60 1590
3958ae62
SN
1591 se = getservbyname(port, protocol);
1592 if (!se)
1593 {
e32c4e60
SN
1594 /* the cast to int suppresses compiler warnings about subscript having the
1595 type char */
3958ae62
SN
1596 if (isdigit((int)port[0]))
1597 {
fb9010ed
DW
1598 int port_int;
1599
1600 port_int = atoi(port);
1601 addr = (struct sockaddr_in *)address->m_addr;
3958ae62 1602 addr->sin_port = htons(port_int);
fb9010ed
DW
1603 return GSOCK_NOERROR;
1604 }
1605
1606 address->m_error = GSOCK_INVPORT;
1607 return GSOCK_INVPORT;
1608 }
1609
1610 addr = (struct sockaddr_in *)address->m_addr;
1611 addr->sin_port = se->s_port;
1612
1613 return GSOCK_NOERROR;
1614}
1615
1616GSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port)
1617{
1618 struct sockaddr_in *addr;
1619
1620 assert(address != NULL);
3958ae62 1621 CHECK_ADDRESS(address, INET);
e32c4e60 1622
fb9010ed 1623 addr = (struct sockaddr_in *)address->m_addr;
3958ae62 1624 addr->sin_port = htons(port);
fb9010ed
DW
1625
1626 return GSOCK_NOERROR;
1627}
1628
1629GSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname, size_t sbuf)
1630{
1631 struct hostent *he;
1632 char *addr_buf;
1633 struct sockaddr_in *addr;
1634
e32c4e60 1635 assert(address != NULL);
3958ae62 1636 CHECK_ADDRESS(address, INET);
fb9010ed
DW
1637
1638 addr = (struct sockaddr_in *)address->m_addr;
1639 addr_buf = (char *)&(addr->sin_addr);
1640
3958ae62
SN
1641 he = gethostbyaddr(addr_buf, sizeof(addr->sin_addr), AF_INET);
1642 if (he == NULL)
1643 {
fb9010ed
DW
1644 address->m_error = GSOCK_NOHOST;
1645 return GSOCK_NOHOST;
1646 }
1647
1648 strncpy(hostname, he->h_name, sbuf);
1649
1650 return GSOCK_NOERROR;
1651}
1652
1653unsigned long GAddress_INET_GetHostAddress(GAddress *address)
1654{
1655 struct sockaddr_in *addr;
1656
e32c4e60
SN
1657 assert(address != NULL);
1658 CHECK_ADDRESS_RETVAL(address, INET, 0);
fb9010ed
DW
1659
1660 addr = (struct sockaddr_in *)address->m_addr;
1661
1662 return addr->sin_addr.s_addr;
1663}
1664
1665unsigned short GAddress_INET_GetPort(GAddress *address)
1666{
1667 struct sockaddr_in *addr;
1668
e32c4e60
SN
1669 assert(address != NULL);
1670 CHECK_ADDRESS_RETVAL(address, INET, 0);
fb9010ed
DW
1671
1672 addr = (struct sockaddr_in *)address->m_addr;
3958ae62 1673 return ntohs(addr->sin_port);
fb9010ed
DW
1674}
1675
b662c135
SN
1676/*
1677 * -------------------------------------------------------------------------
1678 * Unix address family
1679 * -------------------------------------------------------------------------
1680 */
1681
3958ae62 1682#ifdef __EMX__
b662c135
SN
1683GSocketError _GAddress_Init_UNIX(GAddress *address)
1684{
3958ae62
SN
1685 address->m_len = sizeof(struct sockaddr_un);
1686 address->m_addr = (struct sockaddr *)malloc(address->m_len);
1687 if (address->m_addr == NULL)
1688 {
1689 address->m_error = GSOCK_MEMERR;
1690 return GSOCK_MEMERR;
1691 }
1692
1693 address->m_family = GSOCK_UNIX;
1694 address->m_realfamily = PF_UNIX;
1695 ((struct sockaddr_un *)address->m_addr)->sun_family = AF_UNIX;
1696 ((struct sockaddr_un *)address->m_addr)->sun_path[0] = 0;
1697
1698 return GSOCK_NOERROR;
b662c135
SN
1699}
1700
e32c4e60
SN
1701#define UNIX_SOCK_PATHLEN (sizeof(addr->sun_path)/sizeof(addr->sun_path[0]))
1702
b662c135
SN
1703GSocketError GAddress_UNIX_SetPath(GAddress *address, const char *path)
1704{
3958ae62
SN
1705 struct sockaddr_un *addr;
1706
e32c4e60 1707 assert(address != NULL);
3958ae62 1708
e32c4e60 1709 CHECK_ADDRESS(address, UNIX);
3958ae62
SN
1710
1711 addr = ((struct sockaddr_un *)address->m_addr);
e32c4e60
SN
1712 strncpy(addr->sun_path, path, UNIX_SOCK_PATHLEN);
1713 addr->sun_path[UNIX_SOCK_PATHLEN - 1] = '\0';
3958ae62
SN
1714
1715 return GSOCK_NOERROR;
b662c135
SN
1716}
1717
1718GSocketError GAddress_UNIX_GetPath(GAddress *address, char *path, size_t sbuf)
1719{
3958ae62
SN
1720 struct sockaddr_un *addr;
1721
1722 assert(address != NULL);
1723 CHECK_ADDRESS(address, UNIX);
1724
1725 addr = (struct sockaddr_un *)address->m_addr;
1726
1727 strncpy(path, addr->sun_path, sbuf);
b662c135 1728
3958ae62
SN
1729 return GSOCK_NOERROR;
1730}
fb9010ed 1731#endif
3958ae62
SN
1732 /* __EMX__ */
1733#endif
1734 /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */
1735