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