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