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