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