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