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