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