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