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