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