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