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