]> git.saurik.com Git - wxWidgets.git/blob - src/msw/gsocket.c
use Unix path separators under Cygwin
[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 /* allow a socket to re-bind if the socket is in the TIME_WAIT
393 state after being previously closed.
394 */
395 setsockopt(sck->m_fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&arg, sizeof(u_long));
396
397 /* Bind to the local address,
398 * retrieve the actual address bound,
399 * and listen up to 5 connections.
400 */
401 if ((bind(sck->m_fd, sck->m_local->m_addr, sck->m_local->m_len) != 0) ||
402 (getsockname(sck->m_fd,
403 sck->m_local->m_addr,
404 (SOCKLEN_T *)&sck->m_local->m_len) != 0) ||
405 (listen(sck->m_fd, 5) != 0))
406 {
407 GSocket_close(sck);
408 sck->m_error = GSOCK_IOERR;
409 return GSOCK_IOERR;
410 }
411
412 return GSOCK_NOERROR;
413 }
414
415 /* GSocket_WaitConnection:
416 * Waits for an incoming client connection. Returns a pointer to
417 * a GSocket object, or NULL if there was an error, in which case
418 * the last error field will be updated for the calling GSocket.
419 *
420 * Error codes (set in the calling GSocket)
421 * GSOCK_INVSOCK - the socket is not valid or not a server.
422 * GSOCK_TIMEDOUT - timeout, no incoming connections.
423 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
424 * GSOCK_MEMERR - couldn't allocate memory.
425 * GSOCK_IOERR - low-level error.
426 */
427 GSocket *GSocket_WaitConnection(GSocket *sck)
428 {
429 GSocket *connection;
430 struct sockaddr from;
431 SOCKLEN_T fromlen = sizeof(from);
432 GSocketError err;
433 u_long arg = 1;
434
435 assert(sck != NULL);
436
437 /* Reenable CONNECTION events */
438 sck->m_detected &= ~GSOCK_CONNECTION_FLAG;
439
440 /* If the socket has already been created, we exit immediately */
441 if (sck->m_fd == INVALID_SOCKET || !sck->m_server)
442 {
443 sck->m_error = GSOCK_INVSOCK;
444 return NULL;
445 }
446
447 /* Create a GSocket object for the new connection */
448 connection = GSocket_new();
449
450 if (!connection)
451 {
452 sck->m_error = GSOCK_MEMERR;
453 return NULL;
454 }
455
456 /* Wait for a connection (with timeout) */
457 if (_GSocket_Input_Timeout(sck) == GSOCK_TIMEDOUT)
458 {
459 GSocket_destroy(connection);
460 /* sck->m_error set by _GSocket_Input_Timeout */
461 return NULL;
462 }
463
464 connection->m_fd = accept(sck->m_fd, &from, &fromlen);
465
466 if (connection->m_fd == INVALID_SOCKET)
467 {
468 if (WSAGetLastError() == WSAEWOULDBLOCK)
469 sck->m_error = GSOCK_WOULDBLOCK;
470 else
471 sck->m_error = GSOCK_IOERR;
472
473 GSocket_destroy(connection);
474 return NULL;
475 }
476
477 /* Initialize all fields */
478 connection->m_server = FALSE;
479 connection->m_stream = TRUE;
480 connection->m_oriented = TRUE;
481
482 /* Setup the peer address field */
483 connection->m_peer = GAddress_new();
484 if (!connection->m_peer)
485 {
486 GSocket_destroy(connection);
487 sck->m_error = GSOCK_MEMERR;
488 return NULL;
489 }
490 err = _GAddress_translate_from(connection->m_peer, &from, fromlen);
491 if (err != GSOCK_NOERROR)
492 {
493 GAddress_destroy(connection->m_peer);
494 GSocket_destroy(connection);
495 sck->m_error = err;
496 return NULL;
497 }
498
499 ioctlsocket(connection->m_fd, FIONBIO, (u_long FAR *) &arg);
500 _GSocket_Enable_Events(connection);
501
502 return connection;
503 }
504
505 /* Client specific parts */
506
507 /* GSocket_Connect:
508 * For stream (connection oriented) sockets, GSocket_Connect() tries
509 * to establish a client connection to a server using the peer address
510 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
511 * connection has been succesfully established, or one of the error
512 * codes listed below. Note that for nonblocking sockets, a return
513 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
514 * request can be completed later; you should use GSocket_Select()
515 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
516 * corresponding asynchronous events.
517 *
518 * For datagram (non connection oriented) sockets, GSocket_Connect()
519 * just sets the peer address established with GSocket_SetPeer() as
520 * default destination.
521 *
522 * Error codes:
523 * GSOCK_INVSOCK - the socket is in use or not valid.
524 * GSOCK_INVADDR - the peer address has not been established.
525 * GSOCK_TIMEDOUT - timeout, the connection failed.
526 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
527 * GSOCK_MEMERR - couldn't allocate memory.
528 * GSOCK_IOERR - low-level error.
529 */
530 GSocketError GSocket_Connect(GSocket *sck, GSocketStream stream)
531 {
532 int ret, err;
533 u_long arg = 1;
534
535 assert(sck != NULL);
536
537 /* Enable CONNECTION events (needed for nonblocking connections) */
538 sck->m_detected &= ~GSOCK_CONNECTION_FLAG;
539
540 if (sck->m_fd != INVALID_SOCKET)
541 {
542 sck->m_error = GSOCK_INVSOCK;
543 return GSOCK_INVSOCK;
544 }
545
546 if (!sck->m_peer)
547 {
548 sck->m_error = GSOCK_INVADDR;
549 return GSOCK_INVADDR;
550 }
551
552 /* Streamed or dgram socket? */
553 sck->m_stream = (stream == GSOCK_STREAMED);
554 sck->m_oriented = TRUE;
555 sck->m_server = FALSE;
556 sck->m_establishing = FALSE;
557
558 /* Create the socket */
559 sck->m_fd = socket(sck->m_peer->m_realfamily,
560 sck->m_stream? SOCK_STREAM : SOCK_DGRAM, 0);
561
562 if (sck->m_fd == INVALID_SOCKET)
563 {
564 sck->m_error = GSOCK_IOERR;
565 return GSOCK_IOERR;
566 }
567
568 ioctlsocket(sck->m_fd, FIONBIO, (u_long FAR *) &arg);
569 _GSocket_Enable_Events(sck);
570
571 /* Connect it to the peer address, with a timeout (see below) */
572 ret = connect(sck->m_fd, sck->m_peer->m_addr, sck->m_peer->m_len);
573
574 if (ret == SOCKET_ERROR)
575 {
576 err = WSAGetLastError();
577
578 /* If connect failed with EWOULDBLOCK and the GSocket object
579 * is in blocking mode, we select() for the specified timeout
580 * checking for writability to see if the connection request
581 * completes.
582 */
583 if ((err == WSAEWOULDBLOCK) && (!sck->m_non_blocking))
584 {
585 err = _GSocket_Connect_Timeout(sck);
586
587 if (err != GSOCK_NOERROR)
588 {
589 GSocket_close(sck);
590 /* sck->m_error is set in _GSocket_Connect_Timeout */
591 }
592
593 return (GSocketError) err;
594 }
595
596 /* If connect failed with EWOULDBLOCK and the GSocket object
597 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
598 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
599 * this way if the connection completes, a GSOCK_CONNECTION
600 * event will be generated, if enabled.
601 */
602 if ((err == WSAEWOULDBLOCK) && (sck->m_non_blocking))
603 {
604 sck->m_establishing = TRUE;
605 sck->m_error = GSOCK_WOULDBLOCK;
606 return GSOCK_WOULDBLOCK;
607 }
608
609 /* If connect failed with an error other than EWOULDBLOCK,
610 * then the call to GSocket_Connect() has failed.
611 */
612 GSocket_close(sck);
613 sck->m_error = GSOCK_IOERR;
614 return GSOCK_IOERR;
615 }
616
617 return GSOCK_NOERROR;
618 }
619
620 /* Datagram sockets */
621
622 /* GSocket_SetNonOriented:
623 * Sets up this socket as a non-connection oriented (datagram) socket.
624 * Before using this function, the local address must have been set
625 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
626 * on success, or one of the following otherwise.
627 *
628 * Error codes:
629 * GSOCK_INVSOCK - the socket is in use.
630 * GSOCK_INVADDR - the local address has not been set.
631 * GSOCK_IOERR - low-level error.
632 */
633 GSocketError GSocket_SetNonOriented(GSocket *sck)
634 {
635 u_long arg = 1;
636
637 assert(sck != NULL);
638
639 if (sck->m_fd != INVALID_SOCKET)
640 {
641 sck->m_error = GSOCK_INVSOCK;
642 return GSOCK_INVSOCK;
643 }
644
645 if (!sck->m_local)
646 {
647 sck->m_error = GSOCK_INVADDR;
648 return GSOCK_INVADDR;
649 }
650
651 /* Initialize all fields */
652 sck->m_stream = FALSE;
653 sck->m_server = FALSE;
654 sck->m_oriented = FALSE;
655
656 /* Create the socket */
657 sck->m_fd = socket(sck->m_local->m_realfamily, SOCK_DGRAM, 0);
658
659 if (sck->m_fd == INVALID_SOCKET)
660 {
661 sck->m_error = GSOCK_IOERR;
662 return GSOCK_IOERR;
663 }
664
665 ioctlsocket(sck->m_fd, FIONBIO, (u_long FAR *) &arg);
666 _GSocket_Enable_Events(sck);
667
668 /* Bind to the local address,
669 * and retrieve the actual address bound.
670 */
671 if ((bind(sck->m_fd, sck->m_local->m_addr, sck->m_local->m_len) != 0) ||
672 (getsockname(sck->m_fd,
673 sck->m_local->m_addr,
674 (SOCKLEN_T *)&sck->m_local->m_len) != 0))
675 {
676 GSocket_close(sck);
677 sck->m_error = GSOCK_IOERR;
678 return GSOCK_IOERR;
679 }
680
681 return GSOCK_NOERROR;
682 }
683
684 /* Generic IO */
685
686 /* Like recv(), send(), ... */
687 int GSocket_Read(GSocket *socket, char *buffer, int size)
688 {
689 int ret;
690
691 assert(socket != NULL);
692
693 /* Reenable INPUT events */
694 socket->m_detected &= ~GSOCK_INPUT_FLAG;
695
696 if (socket->m_fd == INVALID_SOCKET || socket->m_server)
697 {
698 socket->m_error = GSOCK_INVSOCK;
699 return -1;
700 }
701
702 /* If the socket is blocking, wait for data (with a timeout) */
703 if (_GSocket_Input_Timeout(socket) == GSOCK_TIMEDOUT)
704 return -1;
705
706 /* Read the data */
707 if (socket->m_stream)
708 ret = _GSocket_Recv_Stream(socket, buffer, size);
709 else
710 ret = _GSocket_Recv_Dgram(socket, buffer, size);
711
712 if (ret == SOCKET_ERROR)
713 {
714 if (WSAGetLastError() != WSAEWOULDBLOCK)
715 socket->m_error = GSOCK_IOERR;
716 else
717 socket->m_error = GSOCK_WOULDBLOCK;
718 return -1;
719 }
720
721 return ret;
722 }
723
724 int GSocket_Write(GSocket *socket, const char *buffer, int size)
725 {
726 int ret;
727
728 assert(socket != NULL);
729
730 if (socket->m_fd == INVALID_SOCKET || socket->m_server)
731 {
732 socket->m_error = GSOCK_INVSOCK;
733 return -1;
734 }
735
736 /* If the socket is blocking, wait for writability (with a timeout) */
737 if (_GSocket_Output_Timeout(socket) == GSOCK_TIMEDOUT)
738 return -1;
739
740 /* Write the data */
741 if (socket->m_stream)
742 ret = _GSocket_Send_Stream(socket, buffer, size);
743 else
744 ret = _GSocket_Send_Dgram(socket, buffer, size);
745
746 if (ret == SOCKET_ERROR)
747 {
748 if (WSAGetLastError() != WSAEWOULDBLOCK)
749 socket->m_error = GSOCK_IOERR;
750 else
751 socket->m_error = GSOCK_WOULDBLOCK;
752
753 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
754 * does). Once the first OUTPUT event is received, users can assume
755 * that the socket is writable until a read operation fails. Only then
756 * will further OUTPUT events be posted.
757 */
758 socket->m_detected &= ~GSOCK_OUTPUT_FLAG;
759 return -1;
760 }
761
762 return ret;
763 }
764
765 /* GSocket_Select:
766 * Polls the socket to determine its status. This function will
767 * check for the events specified in the 'flags' parameter, and
768 * it will return a mask indicating which operations can be
769 * performed. This function won't block, regardless of the
770 * mode (blocking | nonblocking) of the socket.
771 */
772 GSocketEventFlags GSocket_Select(GSocket *socket, GSocketEventFlags flags)
773 {
774 if (!USE_GUI())
775 {
776 GSocketEventFlags result = 0;
777 fd_set readfds;
778 fd_set writefds;
779 fd_set exceptfds;
780
781 assert(socket != NULL);
782
783 FD_ZERO(&readfds);
784 FD_ZERO(&writefds);
785 FD_ZERO(&exceptfds);
786 FD_SET(socket->m_fd, &readfds);
787 if (flags & GSOCK_OUTPUT_FLAG || flags & GSOCK_CONNECTION_FLAG)
788 FD_SET(socket->m_fd, &writefds);
789 FD_SET(socket->m_fd, &exceptfds);
790
791 /* Check 'sticky' CONNECTION flag first */
792 result |= (GSOCK_CONNECTION_FLAG & socket->m_detected);
793
794 /* If we have already detected a LOST event, then don't try
795 * to do any further processing.
796 */
797 if ((socket->m_detected & GSOCK_LOST_FLAG) != 0)
798 {
799 socket->m_establishing = FALSE;
800
801 return (GSOCK_LOST_FLAG & flags);
802 }
803
804 /* Try select now */
805 if (select(socket->m_fd + 1, &readfds, &writefds, &exceptfds,
806 &socket->m_timeout) <= 0)
807 {
808 /* What to do here? */
809 return (result & flags);
810 }
811
812 /* Check for readability */
813 if (FD_ISSET(socket->m_fd, &readfds))
814 {
815 char c;
816
817 if (!socket->m_stream || recv(socket->m_fd, &c, 1, MSG_PEEK) > 0)
818 {
819 result |= GSOCK_INPUT_FLAG;
820 }
821 else
822 {
823 if (socket->m_server && socket->m_stream)
824 {
825 result |= GSOCK_CONNECTION_FLAG;
826 socket->m_detected |= GSOCK_CONNECTION_FLAG;
827 }
828 else
829 {
830 socket->m_detected = GSOCK_LOST_FLAG;
831 socket->m_establishing = FALSE;
832
833 /* LOST event: Abort any further processing */
834 return (GSOCK_LOST_FLAG & flags);
835 }
836 }
837 }
838
839 /* Check for writability */
840 if (FD_ISSET(socket->m_fd, &writefds))
841 {
842 if (socket->m_establishing && !socket->m_server)
843 {
844 int error;
845 SOCKLEN_T len = sizeof(error);
846
847 socket->m_establishing = FALSE;
848
849 getsockopt(socket->m_fd, SOL_SOCKET, SO_ERROR, (void*)&error, &len);
850
851 if (error)
852 {
853 socket->m_detected = GSOCK_LOST_FLAG;
854
855 /* LOST event: Abort any further processing */
856 return (GSOCK_LOST_FLAG & flags);
857 }
858 else
859 {
860 result |= GSOCK_CONNECTION_FLAG;
861 socket->m_detected |= GSOCK_CONNECTION_FLAG;
862 }
863 }
864 else
865 {
866 result |= GSOCK_OUTPUT_FLAG;
867 }
868 }
869
870 /* Check for exceptions and errors (is this useful in Unices?) */
871 if (FD_ISSET(socket->m_fd, &exceptfds))
872 {
873 socket->m_establishing = FALSE;
874 socket->m_detected = GSOCK_LOST_FLAG;
875
876 /* LOST event: Abort any further processing */
877 return (GSOCK_LOST_FLAG & flags);
878 }
879
880 return (result & flags);
881 }
882 else /* USE_GUI() */
883 {
884 assert(socket != NULL);
885 return flags & socket->m_detected;
886 }
887 }
888
889 /* Attributes */
890
891 /* GSocket_SetNonBlocking:
892 * Sets the socket to non-blocking mode. All IO calls will return
893 * immediately.
894 */
895 void GSocket_SetNonBlocking(GSocket *socket, int non_block)
896 {
897 assert(socket != NULL);
898
899 socket->m_non_blocking = non_block;
900 }
901
902 /* GSocket_SetTimeout:
903 * Sets the timeout for blocking calls. Time is expressed in
904 * milliseconds.
905 */
906 void GSocket_SetTimeout(GSocket *socket, unsigned long millis)
907 {
908 assert(socket != NULL);
909
910 socket->m_timeout.tv_sec = (millis / 1000);
911 socket->m_timeout.tv_usec = (millis % 1000) * 1000;
912 }
913
914 /* GSocket_GetError:
915 * Returns the last error occured for this socket. Note that successful
916 * operations do not clear this back to GSOCK_NOERROR, so use it only
917 * after an error.
918 */
919 GSocketError GSocket_GetError(GSocket *socket)
920 {
921 assert(socket != NULL);
922
923 return socket->m_error;
924 }
925
926 /* Callbacks */
927
928 /* GSOCK_INPUT:
929 * There is data to be read in the input buffer. If, after a read
930 * operation, there is still data available, the callback function will
931 * be called again.
932 * GSOCK_OUTPUT:
933 * The socket is available for writing. That is, the next write call
934 * won't block. This event is generated only once, when the connection is
935 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
936 * when the output buffer empties again. This means that the app should
937 * assume that it can write since the first OUTPUT event, and no more
938 * OUTPUT events will be generated unless an error occurs.
939 * GSOCK_CONNECTION:
940 * Connection succesfully established, for client sockets, or incoming
941 * client connection, for server sockets. Wait for this event (also watch
942 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
943 * GSOCK_LOST:
944 * The connection is lost (or a connection request failed); this could
945 * be due to a failure, or due to the peer closing it gracefully.
946 */
947
948 /* GSocket_SetCallback:
949 * Enables the callbacks specified by 'flags'. Note that 'flags'
950 * may be a combination of flags OR'ed toghether, so the same
951 * callback function can be made to accept different events.
952 * The callback function must have the following prototype:
953 *
954 * void function(GSocket *socket, GSocketEvent event, char *cdata)
955 */
956 void GSocket_SetCallback(GSocket *socket, GSocketEventFlags flags,
957 GSocketCallback callback, char *cdata)
958 {
959 int count;
960
961 assert(socket != NULL);
962
963 for (count = 0; count < GSOCK_MAX_EVENT; count++)
964 {
965 if ((flags & (1 << count)) != 0)
966 {
967 socket->m_cbacks[count] = callback;
968 socket->m_data[count] = cdata;
969 }
970 }
971 }
972
973 /* GSocket_UnsetCallback:
974 * Disables all callbacks specified by 'flags', which may be a
975 * combination of flags OR'ed toghether.
976 */
977 void GSocket_UnsetCallback(GSocket *socket, GSocketEventFlags flags)
978 {
979 int count;
980
981 assert(socket != NULL);
982
983 for (count = 0; count < GSOCK_MAX_EVENT; count++)
984 {
985 if ((flags & (1 << count)) != 0)
986 {
987 socket->m_cbacks[count] = NULL;
988 socket->m_data[count] = NULL;
989 }
990 }
991 }
992
993 GSocketError GSocket_GetSockOpt(GSocket *socket, int level, int optname,
994 void *optval, int *optlen)
995 {
996 if (getsockopt(socket->m_fd, level, optname, optval, optlen) == 0)
997 {
998 return GSOCK_NOERROR;
999 }
1000 return GSOCK_OPTERR;
1001 }
1002
1003 GSocketError GSocket_SetSockOpt(GSocket *socket, int level, int optname,
1004 const void *optval, int optlen)
1005 {
1006 if (setsockopt(socket->m_fd, level, optname, optval, optlen) == 0)
1007 {
1008 return GSOCK_NOERROR;
1009 }
1010 return GSOCK_OPTERR;
1011 }
1012
1013 void GSocket_Streamed(GSocket *socket)
1014 {
1015 socket->m_stream = TRUE;
1016 }
1017
1018 void GSocket_Unstreamed(GSocket *socket)
1019 {
1020 socket->m_stream = FALSE;
1021 }
1022
1023 /* Internals (IO) */
1024
1025 /* _GSocket_Input_Timeout:
1026 * For blocking sockets, wait until data is available or
1027 * until timeout ellapses.
1028 */
1029 GSocketError _GSocket_Input_Timeout(GSocket *socket)
1030 {
1031 fd_set readfds;
1032
1033 if (!socket->m_non_blocking)
1034 {
1035 FD_ZERO(&readfds);
1036 FD_SET(socket->m_fd, &readfds);
1037 if (select(0, &readfds, NULL, NULL, &socket->m_timeout) == 0)
1038 {
1039 socket->m_error = GSOCK_TIMEDOUT;
1040 return GSOCK_TIMEDOUT;
1041 }
1042 }
1043 return GSOCK_NOERROR;
1044 }
1045
1046 /* _GSocket_Output_Timeout:
1047 * For blocking sockets, wait until data can be sent without
1048 * blocking or until timeout ellapses.
1049 */
1050 GSocketError _GSocket_Output_Timeout(GSocket *socket)
1051 {
1052 fd_set writefds;
1053
1054 if (!socket->m_non_blocking)
1055 {
1056 FD_ZERO(&writefds);
1057 FD_SET(socket->m_fd, &writefds);
1058 if (select(0, NULL, &writefds, NULL, &socket->m_timeout) == 0)
1059 {
1060 socket->m_error = GSOCK_TIMEDOUT;
1061 return GSOCK_TIMEDOUT;
1062 }
1063 }
1064 return GSOCK_NOERROR;
1065 }
1066
1067 /* _GSocket_Connect_Timeout:
1068 * For blocking sockets, wait until the connection is
1069 * established or fails, or until timeout ellapses.
1070 */
1071 GSocketError _GSocket_Connect_Timeout(GSocket *socket)
1072 {
1073 fd_set writefds;
1074 fd_set exceptfds;
1075
1076 FD_ZERO(&writefds);
1077 FD_ZERO(&exceptfds);
1078 FD_SET(socket->m_fd, &writefds);
1079 FD_SET(socket->m_fd, &exceptfds);
1080 if (select(0, NULL, &writefds, &exceptfds, &socket->m_timeout) == 0)
1081 {
1082 socket->m_error = GSOCK_TIMEDOUT;
1083 return GSOCK_TIMEDOUT;
1084 }
1085 if (!FD_ISSET(socket->m_fd, &writefds))
1086 {
1087 socket->m_error = GSOCK_IOERR;
1088 return GSOCK_IOERR;
1089 }
1090
1091 return GSOCK_NOERROR;
1092 }
1093
1094 int _GSocket_Recv_Stream(GSocket *socket, char *buffer, int size)
1095 {
1096 return recv(socket->m_fd, buffer, size, 0);
1097 }
1098
1099 int _GSocket_Recv_Dgram(GSocket *socket, char *buffer, int size)
1100 {
1101 struct sockaddr from;
1102 SOCKLEN_T fromlen = sizeof(from);
1103 int ret;
1104 GSocketError err;
1105
1106 ret = recvfrom(socket->m_fd, buffer, size, 0, &from, &fromlen);
1107
1108 if (ret == SOCKET_ERROR)
1109 return SOCKET_ERROR;
1110
1111 /* Translate a system address into a GSocket address */
1112 if (!socket->m_peer)
1113 {
1114 socket->m_peer = GAddress_new();
1115 if (!socket->m_peer)
1116 {
1117 socket->m_error = GSOCK_MEMERR;
1118 return -1;
1119 }
1120 }
1121 err = _GAddress_translate_from(socket->m_peer, &from, fromlen);
1122 if (err != GSOCK_NOERROR)
1123 {
1124 GAddress_destroy(socket->m_peer);
1125 socket->m_peer = NULL;
1126 socket->m_error = err;
1127 return -1;
1128 }
1129
1130 return ret;
1131 }
1132
1133 int _GSocket_Send_Stream(GSocket *socket, const char *buffer, int size)
1134 {
1135 return send(socket->m_fd, buffer, size, 0);
1136 }
1137
1138 int _GSocket_Send_Dgram(GSocket *socket, const char *buffer, int size)
1139 {
1140 struct sockaddr *addr;
1141 int len, ret;
1142 GSocketError err;
1143
1144 if (!socket->m_peer)
1145 {
1146 socket->m_error = GSOCK_INVADDR;
1147 return -1;
1148 }
1149
1150 err = _GAddress_translate_to(socket->m_peer, &addr, &len);
1151 if (err != GSOCK_NOERROR)
1152 {
1153 socket->m_error = err;
1154 return -1;
1155 }
1156
1157 ret = sendto(socket->m_fd, buffer, size, 0, addr, len);
1158
1159 /* Frees memory allocated by _GAddress_translate_to */
1160 free(addr);
1161
1162 return ret;
1163 }
1164
1165
1166 /*
1167 * -------------------------------------------------------------------------
1168 * GAddress
1169 * -------------------------------------------------------------------------
1170 */
1171
1172 /* CHECK_ADDRESS verifies that the current address family is either
1173 * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it
1174 * initalizes it to be a GSOCK_*family*. In other cases, it returns
1175 * an appropiate error code.
1176 *
1177 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
1178 */
1179 #define CHECK_ADDRESS(address, family) \
1180 { \
1181 if (address->m_family == GSOCK_NOFAMILY) \
1182 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1183 return address->m_error; \
1184 if (address->m_family != GSOCK_##family) \
1185 { \
1186 address->m_error = GSOCK_INVADDR; \
1187 return GSOCK_INVADDR; \
1188 } \
1189 }
1190
1191 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
1192 { \
1193 if (address->m_family == GSOCK_NOFAMILY) \
1194 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1195 return retval; \
1196 if (address->m_family != GSOCK_##family) \
1197 { \
1198 address->m_error = GSOCK_INVADDR; \
1199 return retval; \
1200 } \
1201 }
1202
1203
1204 GAddress *GAddress_new(void)
1205 {
1206 GAddress *address;
1207
1208 if ((address = (GAddress *) malloc(sizeof(GAddress))) == NULL)
1209 return NULL;
1210
1211 address->m_family = GSOCK_NOFAMILY;
1212 address->m_addr = NULL;
1213 address->m_len = 0;
1214
1215 return address;
1216 }
1217
1218 GAddress *GAddress_copy(GAddress *address)
1219 {
1220 GAddress *addr2;
1221
1222 assert(address != NULL);
1223
1224 if ((addr2 = (GAddress *) malloc(sizeof(GAddress))) == NULL)
1225 return NULL;
1226
1227 memcpy(addr2, address, sizeof(GAddress));
1228
1229 if (address->m_addr)
1230 {
1231 addr2->m_addr = (struct sockaddr *) malloc(addr2->m_len);
1232 if (addr2->m_addr == NULL)
1233 {
1234 free(addr2);
1235 return NULL;
1236 }
1237 memcpy(addr2->m_addr, address->m_addr, addr2->m_len);
1238 }
1239
1240 return addr2;
1241 }
1242
1243 void GAddress_destroy(GAddress *address)
1244 {
1245 assert(address != NULL);
1246
1247 if (address->m_addr)
1248 free(address->m_addr);
1249
1250 free(address);
1251 }
1252
1253 void GAddress_SetFamily(GAddress *address, GAddressType type)
1254 {
1255 assert(address != NULL);
1256
1257 address->m_family = type;
1258 }
1259
1260 GAddressType GAddress_GetFamily(GAddress *address)
1261 {
1262 assert(address != NULL);
1263
1264 return address->m_family;
1265 }
1266
1267 GSocketError _GAddress_translate_from(GAddress *address,
1268 struct sockaddr *addr, int len)
1269 {
1270 address->m_realfamily = addr->sa_family;
1271 switch (addr->sa_family)
1272 {
1273 case AF_INET:
1274 address->m_family = GSOCK_INET;
1275 break;
1276 case AF_UNIX:
1277 address->m_family = GSOCK_UNIX;
1278 break;
1279 #ifdef AF_INET6
1280 case AF_INET6:
1281 address->m_family = GSOCK_INET6;
1282 break;
1283 #endif
1284 default:
1285 {
1286 address->m_error = GSOCK_INVOP;
1287 return GSOCK_INVOP;
1288 }
1289 }
1290
1291 if (address->m_addr)
1292 free(address->m_addr);
1293
1294 address->m_len = len;
1295 address->m_addr = (struct sockaddr *) malloc(len);
1296
1297 if (address->m_addr == NULL)
1298 {
1299 address->m_error = GSOCK_MEMERR;
1300 return GSOCK_MEMERR;
1301 }
1302 memcpy(address->m_addr, addr, len);
1303
1304 return GSOCK_NOERROR;
1305 }
1306
1307 GSocketError _GAddress_translate_to(GAddress *address,
1308 struct sockaddr **addr, int *len)
1309 {
1310 if (!address->m_addr)
1311 {
1312 address->m_error = GSOCK_INVADDR;
1313 return GSOCK_INVADDR;
1314 }
1315
1316 *len = address->m_len;
1317 *addr = (struct sockaddr *) malloc(address->m_len);
1318 if (*addr == NULL)
1319 {
1320 address->m_error = GSOCK_MEMERR;
1321 return GSOCK_MEMERR;
1322 }
1323
1324 memcpy(*addr, address->m_addr, address->m_len);
1325 return GSOCK_NOERROR;
1326 }
1327
1328 /*
1329 * -------------------------------------------------------------------------
1330 * Internet address family
1331 * -------------------------------------------------------------------------
1332 */
1333
1334 GSocketError _GAddress_Init_INET(GAddress *address)
1335 {
1336 address->m_len = sizeof(struct sockaddr_in);
1337 address->m_addr = (struct sockaddr *) malloc(address->m_len);
1338 if (address->m_addr == NULL)
1339 {
1340 address->m_error = GSOCK_MEMERR;
1341 return GSOCK_MEMERR;
1342 }
1343
1344 address->m_family = GSOCK_INET;
1345 address->m_realfamily = AF_INET;
1346 ((struct sockaddr_in *)address->m_addr)->sin_family = AF_INET;
1347 ((struct sockaddr_in *)address->m_addr)->sin_addr.s_addr = INADDR_ANY;
1348
1349 return GSOCK_NOERROR;
1350 }
1351
1352 GSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname)
1353 {
1354 struct hostent *he;
1355 struct in_addr *addr;
1356
1357 assert(address != NULL);
1358
1359 CHECK_ADDRESS(address, INET);
1360
1361 addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr);
1362
1363 addr->s_addr = inet_addr(hostname);
1364
1365 /* If it is a numeric host name, convert it now */
1366 if (addr->s_addr == INADDR_NONE)
1367 {
1368 struct in_addr *array_addr;
1369
1370 /* It is a real name, we solve it */
1371 if ((he = gethostbyname(hostname)) == NULL)
1372 {
1373 /* addr->s_addr = INADDR_NONE just done by inet_addr() above */
1374 address->m_error = GSOCK_NOHOST;
1375 return GSOCK_NOHOST;
1376 }
1377 array_addr = (struct in_addr *) *(he->h_addr_list);
1378 addr->s_addr = array_addr[0].s_addr;
1379 }
1380 return GSOCK_NOERROR;
1381 }
1382
1383 GSocketError GAddress_INET_SetAnyAddress(GAddress *address)
1384 {
1385 return GAddress_INET_SetHostAddress(address, INADDR_ANY);
1386 }
1387
1388 GSocketError GAddress_INET_SetHostAddress(GAddress *address,
1389 unsigned long hostaddr)
1390 {
1391 struct in_addr *addr;
1392
1393 assert(address != NULL);
1394
1395 CHECK_ADDRESS(address, INET);
1396
1397 addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr);
1398 addr->s_addr = htonl(hostaddr);;
1399
1400 return GSOCK_NOERROR;
1401 }
1402
1403 GSocketError GAddress_INET_SetPortName(GAddress *address, const char *port,
1404 const char *protocol)
1405 {
1406 struct servent *se;
1407 struct sockaddr_in *addr;
1408
1409 assert(address != NULL);
1410 CHECK_ADDRESS(address, INET);
1411
1412 if (!port)
1413 {
1414 address->m_error = GSOCK_INVPORT;
1415 return GSOCK_INVPORT;
1416 }
1417
1418 se = getservbyname(port, protocol);
1419 if (!se)
1420 {
1421 if (isdigit(port[0]))
1422 {
1423 int port_int;
1424
1425 port_int = atoi(port);
1426 addr = (struct sockaddr_in *)address->m_addr;
1427 addr->sin_port = htons((u_short) port_int);
1428 return GSOCK_NOERROR;
1429 }
1430
1431 address->m_error = GSOCK_INVPORT;
1432 return GSOCK_INVPORT;
1433 }
1434
1435 addr = (struct sockaddr_in *)address->m_addr;
1436 addr->sin_port = se->s_port;
1437
1438 return GSOCK_NOERROR;
1439 }
1440
1441 GSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port)
1442 {
1443 struct sockaddr_in *addr;
1444
1445 assert(address != NULL);
1446 CHECK_ADDRESS(address, INET);
1447
1448 addr = (struct sockaddr_in *)address->m_addr;
1449 addr->sin_port = htons(port);
1450
1451 return GSOCK_NOERROR;
1452 }
1453
1454 GSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname, size_t sbuf)
1455 {
1456 struct hostent *he;
1457 char *addr_buf;
1458 struct sockaddr_in *addr;
1459
1460 assert(address != NULL);
1461 CHECK_ADDRESS(address, INET);
1462
1463 addr = (struct sockaddr_in *)address->m_addr;
1464 addr_buf = (char *)&(addr->sin_addr);
1465
1466 he = gethostbyaddr(addr_buf, sizeof(addr->sin_addr), AF_INET);
1467 if (he == NULL)
1468 {
1469 address->m_error = GSOCK_NOHOST;
1470 return GSOCK_NOHOST;
1471 }
1472
1473 strncpy(hostname, he->h_name, sbuf);
1474
1475 return GSOCK_NOERROR;
1476 }
1477
1478 unsigned long GAddress_INET_GetHostAddress(GAddress *address)
1479 {
1480 struct sockaddr_in *addr;
1481
1482 assert(address != NULL);
1483 CHECK_ADDRESS_RETVAL(address, INET, 0);
1484
1485 addr = (struct sockaddr_in *)address->m_addr;
1486
1487 return ntohl(addr->sin_addr.s_addr);
1488 }
1489
1490 unsigned short GAddress_INET_GetPort(GAddress *address)
1491 {
1492 struct sockaddr_in *addr;
1493
1494 assert(address != NULL);
1495 CHECK_ADDRESS_RETVAL(address, INET, 0);
1496
1497 addr = (struct sockaddr_in *)address->m_addr;
1498 return ntohs(addr->sin_port);
1499 }
1500
1501 /*
1502 * -------------------------------------------------------------------------
1503 * Unix address family
1504 * -------------------------------------------------------------------------
1505 */
1506
1507 GSocketError _GAddress_Init_UNIX(GAddress *address)
1508 {
1509 assert (address != NULL);
1510 address->m_error = GSOCK_INVADDR;
1511 return GSOCK_INVADDR;
1512 }
1513
1514 GSocketError GAddress_UNIX_SetPath(GAddress *address, const char *path)
1515 {
1516 #if defined(__BORLANDC__)
1517 /* prevents unused variable message in Borland */
1518 (void)path;
1519 #endif
1520 assert (address != NULL);
1521 address->m_error = GSOCK_INVADDR;
1522 return GSOCK_INVADDR;
1523 }
1524
1525 GSocketError GAddress_UNIX_GetPath(GAddress *address, char *path, size_t sbuf)
1526 {
1527 #if defined(__BORLANDC__)
1528 /* prevents unused variable message in Borland */
1529 (void)path;
1530 (void)sbuf;
1531 #endif
1532 assert (address != NULL);
1533 address->m_error = GSOCK_INVADDR;
1534 return GSOCK_INVADDR;
1535 }
1536
1537 #else /* !wxUSE_SOCKETS */
1538
1539 /*
1540 * Translation unit shouldn't be empty, so include this typedef to make the
1541 * compiler (VC++ 6.0, for example) happy
1542 */
1543 typedef void (*wxDummy)();
1544
1545 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */
1546