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