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
10 * -------------------------------------------------------------------------
13 // For compilers that support precompilation, includes "wx.h".
14 #include "wx/wxprec.h"
21 /* RPCNOTIFICATION_ROUTINE in rasasync.h (included from winsock.h),
22 * warning: conditional expression is constant.
24 # pragma warning(disable:4115)
26 * warning: named type definition in parentheses.
28 # pragma warning(disable:4127)
29 /* GAddress_UNIX_GetPath,
30 * warning: unreferenced formal parameter.
32 # pragma warning(disable:4100)
35 /* windows.h results in tons of warnings at max warning level */
37 # pragma warning(push, 1)
42 # pragma warning(disable:4514)
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>
56 #include "wx/platform.h"
60 #include "wx/gsocket.h"
63 wxFORCE_LINK_MODULE(gsockmsw
)
67 #define isdigit(x) (x > 47 && x < 58)
69 #include "wx/msw/wince/net.h"
78 #include "wx/private/socket.h"
84 GSocketManager
* const manager
= GSocketManager::Get();
85 if ( !manager
|| !manager
->OnInit() )
88 /* Initialize WinSocket */
89 return WSAStartup((1 << 8) | 1, &wsaData
) == 0;
92 void GSocket_Cleanup()
94 GSocketManager
* const manager
= GSocketManager::Get();
98 /* Cleanup WinSocket */
102 /* Constructors / Destructors for GSocket */
104 /* Server specific parts */
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:
112 * GSOCK_INVSOCK - the socket is in use.
113 * GSOCK_INVADDR - the local address has not been set.
114 * GSOCK_IOERR - low-level error.
116 GSocketError
GSocket::SetServer()
120 /* must not be in use */
121 if (m_fd
!= INVALID_SOCKET
)
123 m_error
= GSOCK_INVSOCK
;
124 return GSOCK_INVSOCK
;
127 /* the local addr must have been set */
130 m_error
= GSOCK_INVADDR
;
131 return GSOCK_INVADDR
;
134 /* Initialize all fields */
138 /* Create the socket */
139 m_fd
= socket(m_local
->m_realfamily
, SOCK_STREAM
, 0);
141 if (m_fd
== INVALID_SOCKET
)
143 m_error
= GSOCK_IOERR
;
147 ioctlsocket(m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
148 GSocketManager::Get()->Install_Callback(this);
150 /* allow a socket to re-bind if the socket is in the TIME_WAIT
151 state after being previously closed.
155 setsockopt(m_fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char*)&arg
, sizeof(arg
));
158 /* Bind to the local address,
159 * retrieve the actual address bound,
160 * and listen up to 5 connections.
162 if ((bind(m_fd
, m_local
->m_addr
, m_local
->m_len
) != 0) ||
165 (WX_SOCKLEN_T
*)&m_local
->m_len
) != 0) ||
166 (listen(m_fd
, 5) != 0))
169 m_error
= GSOCK_IOERR
;
173 return GSOCK_NOERROR
;
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.
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.
188 GSocket
*GSocket::WaitConnection(wxSocketBase
& wxsocket
)
192 WX_SOCKLEN_T fromlen
= sizeof(from
);
196 /* Reenable CONNECTION events */
197 m_detected
&= ~GSOCK_CONNECTION_FLAG
;
199 /* If the socket has already been created, we exit immediately */
200 if (m_fd
== INVALID_SOCKET
|| !m_server
)
202 m_error
= GSOCK_INVSOCK
;
206 /* Create a GSocket object for the new connection */
207 connection
= GSocket::Create(wxsocket
);
211 m_error
= GSOCK_MEMERR
;
215 /* Wait for a connection (with timeout) */
216 if (Input_Timeout() == GSOCK_TIMEDOUT
)
219 /* m_error set by _GSocket_Input_Timeout */
223 connection
->m_fd
= accept(m_fd
, (sockaddr
*)&from
, &fromlen
);
225 if (connection
->m_fd
== INVALID_SOCKET
)
227 if (WSAGetLastError() == WSAEWOULDBLOCK
)
228 m_error
= GSOCK_WOULDBLOCK
;
230 m_error
= GSOCK_IOERR
;
236 /* Initialize all fields */
237 connection
->m_server
= false;
238 connection
->m_stream
= true;
240 /* Setup the peer address field */
241 connection
->m_peer
= GAddress_new();
242 if (!connection
->m_peer
)
245 m_error
= GSOCK_MEMERR
;
248 err
= _GAddress_translate_from(connection
->m_peer
, (sockaddr
*)&from
, fromlen
);
249 if (err
!= GSOCK_NOERROR
)
251 GAddress_destroy(connection
->m_peer
);
257 ioctlsocket(connection
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
258 GSocketManager::Get()->Install_Callback(connection
);
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)
271 bool GSocket::SetReusable()
273 /* socket must not be null, and must not be in use/already bound */
274 if (this && m_fd
== INVALID_SOCKET
) {
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)
289 bool GSocket::SetBroadcast()
291 /* socket must not be in use/already bound */
292 if (m_fd
== INVALID_SOCKET
) {
299 bool GSocket::DontDoBind()
301 /* socket must not be in use/already bound */
302 if (m_fd
== INVALID_SOCKET
) {
309 /* Client specific parts */
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.
322 * For datagram (non connection oriented) sockets, GSocket_Connect()
323 * just sets the peer address established with GSocket_SetPeer() as
324 * default destination.
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.
334 GSocketError
GSocket::Connect(GSocketStream stream
)
339 /* Enable CONNECTION events (needed for nonblocking connections) */
340 m_detected
&= ~GSOCK_CONNECTION_FLAG
;
342 if (m_fd
!= INVALID_SOCKET
)
344 m_error
= GSOCK_INVSOCK
;
345 return GSOCK_INVSOCK
;
350 m_error
= GSOCK_INVADDR
;
351 return GSOCK_INVADDR
;
354 /* Streamed or dgram socket? */
355 m_stream
= (stream
== GSOCK_STREAMED
);
357 m_establishing
= false;
359 /* Create the socket */
360 m_fd
= socket(m_peer
->m_realfamily
,
361 m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
363 if (m_fd
== INVALID_SOCKET
)
365 m_error
= GSOCK_IOERR
;
369 ioctlsocket(m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
370 GSocketManager::Get()->Install_Callback(this);
372 // If the reuse flag is set, use the applicable socket reuse flag
375 setsockopt(m_fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char*)&arg
, sizeof(arg
));
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
));
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
)
386 bind(m_fd
, m_local
->m_addr
, m_local
->m_len
);
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
);
392 if (ret
== SOCKET_ERROR
)
394 err
= WSAGetLastError();
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
401 if ((err
== WSAEWOULDBLOCK
) && (!m_non_blocking
))
403 err
= Connect_Timeout();
405 if (err
!= GSOCK_NOERROR
)
408 /* m_error is set in _GSocket_Connect_Timeout */
411 return (GSocketError
) err
;
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.
420 if ((err
== WSAEWOULDBLOCK
) && (m_non_blocking
))
422 m_establishing
= true;
423 m_error
= GSOCK_WOULDBLOCK
;
424 return GSOCK_WOULDBLOCK
;
427 /* If connect failed with an error other than EWOULDBLOCK,
428 * then the call to GSocket_Connect() has failed.
431 m_error
= GSOCK_IOERR
;
435 return GSOCK_NOERROR
;
438 /* Datagram sockets */
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.
447 * GSOCK_INVSOCK - the socket is in use.
448 * GSOCK_INVADDR - the local address has not been set.
449 * GSOCK_IOERR - low-level error.
451 GSocketError
GSocket::SetNonOriented()
455 if (m_fd
!= INVALID_SOCKET
)
457 m_error
= GSOCK_INVSOCK
;
458 return GSOCK_INVSOCK
;
463 m_error
= GSOCK_INVADDR
;
464 return GSOCK_INVADDR
;
467 /* Initialize all fields */
471 /* Create the socket */
472 m_fd
= socket(m_local
->m_realfamily
, SOCK_DGRAM
, 0);
474 if (m_fd
== INVALID_SOCKET
)
476 m_error
= GSOCK_IOERR
;
480 ioctlsocket(m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
481 GSocketManager::Get()->Install_Callback(this);
485 setsockopt(m_fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char*)&arg
, sizeof(arg
));
489 setsockopt(m_fd
, SOL_SOCKET
, SO_BROADCAST
, (const char*)&arg
, sizeof(arg
));
493 /* Bind to the local address,
494 * and retrieve the actual address bound.
496 if ((bind(m_fd
, m_local
->m_addr
, m_local
->m_len
) != 0) ||
499 (WX_SOCKLEN_T
*)&m_local
->m_len
) != 0))
502 m_error
= GSOCK_IOERR
;
507 return GSOCK_NOERROR
;
512 /* Like recv(), send(), ... */
513 int GSocket::Read(char *buffer
, int size
)
517 /* Reenable INPUT events */
518 m_detected
&= ~GSOCK_INPUT_FLAG
;
520 if (m_fd
== INVALID_SOCKET
|| m_server
)
522 m_error
= GSOCK_INVSOCK
;
526 /* If the socket is blocking, wait for data (with a timeout) */
527 if (Input_Timeout() == GSOCK_TIMEDOUT
)
529 m_error
= GSOCK_TIMEDOUT
;
535 ret
= Recv_Stream(buffer
, size
);
537 ret
= Recv_Dgram(buffer
, size
);
539 if (ret
== SOCKET_ERROR
)
541 if (WSAGetLastError() != WSAEWOULDBLOCK
)
542 m_error
= GSOCK_IOERR
;
544 m_error
= GSOCK_WOULDBLOCK
;
551 int GSocket::Write(const char *buffer
, int size
)
555 if (m_fd
== INVALID_SOCKET
|| m_server
)
557 m_error
= GSOCK_INVSOCK
;
561 /* If the socket is blocking, wait for writability (with a timeout) */
562 if (Output_Timeout() == GSOCK_TIMEDOUT
)
567 ret
= Send_Stream(buffer
, size
);
569 ret
= Send_Dgram(buffer
, size
);
571 if (ret
== SOCKET_ERROR
)
573 if (WSAGetLastError() != WSAEWOULDBLOCK
)
574 m_error
= GSOCK_IOERR
;
576 m_error
= GSOCK_WOULDBLOCK
;
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.
583 m_detected
&= ~GSOCK_OUTPUT_FLAG
;
592 /* GSocket_SetNonBlocking:
593 * Sets the socket to non-blocking mode. All IO calls will return
596 void GSocket::SetNonBlocking(bool non_block
)
598 m_non_blocking
= non_block
;
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
606 GSocketError WXDLLIMPEXP_NET
GSocket::GetError()
611 GSocketError
GSocket::GetSockOpt(int level
, int optname
,
612 void *optval
, int *optlen
)
614 if (getsockopt(m_fd
, level
, optname
, (char*)optval
, optlen
) == 0)
616 return GSOCK_NOERROR
;
621 GSocketError
GSocket::SetSockOpt(int level
, int optname
,
622 const void *optval
, int optlen
)
624 if (setsockopt(m_fd
, level
, optname
, (char*)optval
, optlen
) == 0)
626 return GSOCK_NOERROR
;
633 /* _GSocket_Input_Timeout:
634 * For blocking sockets, wait until data is available or
635 * until timeout ellapses.
637 GSocketError
GSocket::Input_Timeout()
644 FD_SET(m_fd
, &readfds
);
645 if (select(0, &readfds
, NULL
, NULL
, &m_timeout
) == 0)
647 m_error
= GSOCK_TIMEDOUT
;
648 return GSOCK_TIMEDOUT
;
651 return GSOCK_NOERROR
;
654 /* _GSocket_Output_Timeout:
655 * For blocking sockets, wait until data can be sent without
656 * blocking or until timeout ellapses.
658 GSocketError
GSocket::Output_Timeout()
665 FD_SET(m_fd
, &writefds
);
666 if (select(0, NULL
, &writefds
, NULL
, &m_timeout
) == 0)
668 m_error
= GSOCK_TIMEDOUT
;
669 return GSOCK_TIMEDOUT
;
672 return GSOCK_NOERROR
;
675 /* _GSocket_Connect_Timeout:
676 * For blocking sockets, wait until the connection is
677 * established or fails, or until timeout ellapses.
679 GSocketError
GSocket::Connect_Timeout()
686 FD_SET(m_fd
, &writefds
);
687 FD_SET(m_fd
, &exceptfds
);
688 if (select(0, NULL
, &writefds
, &exceptfds
, &m_timeout
) == 0)
690 m_error
= GSOCK_TIMEDOUT
;
691 return GSOCK_TIMEDOUT
;
693 if (!FD_ISSET(m_fd
, &writefds
))
695 m_error
= GSOCK_IOERR
;
699 return GSOCK_NOERROR
;
702 int GSocket::Recv_Stream(char *buffer
, int size
)
704 return recv(m_fd
, buffer
, size
, 0);
707 int GSocket::Recv_Dgram(char *buffer
, int size
)
710 WX_SOCKLEN_T fromlen
= sizeof(from
);
714 ret
= recvfrom(m_fd
, buffer
, size
, 0, (sockaddr
*)&from
, &fromlen
);
716 if (ret
== SOCKET_ERROR
)
719 /* Translate a system address into a GSocket address */
722 m_peer
= GAddress_new();
725 m_error
= GSOCK_MEMERR
;
729 err
= _GAddress_translate_from(m_peer
, (sockaddr
*)&from
, fromlen
);
730 if (err
!= GSOCK_NOERROR
)
732 GAddress_destroy(m_peer
);
741 int GSocket::Send_Stream(const char *buffer
, int size
)
743 return send(m_fd
, buffer
, size
, 0);
746 int GSocket::Send_Dgram(const char *buffer
, int size
)
748 struct sockaddr
*addr
;
754 m_error
= GSOCK_INVADDR
;
758 err
= _GAddress_translate_to(m_peer
, &addr
, &len
);
759 if (err
!= GSOCK_NOERROR
)
765 ret
= sendto(m_fd
, buffer
, size
, 0, addr
, len
);
767 /* Frees memory allocated by _GAddress_translate_to */
774 * -------------------------------------------------------------------------
776 * -------------------------------------------------------------------------
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.
784 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
786 #define CHECK_ADDRESS(address, family) \
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) \
793 address->m_error = GSOCK_INVADDR; \
794 return GSOCK_INVADDR; \
798 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
800 if (address->m_family == GSOCK_NOFAMILY) \
801 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
803 if (address->m_family != GSOCK_##family) \
805 address->m_error = GSOCK_INVADDR; \
811 GAddress
*GAddress_new()
815 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
818 address
->m_family
= GSOCK_NOFAMILY
;
819 address
->m_addr
= NULL
;
825 GAddress
*GAddress_copy(GAddress
*address
)
829 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
832 memcpy(addr2
, address
, sizeof(GAddress
));
836 addr2
->m_addr
= (struct sockaddr
*) malloc(addr2
->m_len
);
837 if (addr2
->m_addr
== NULL
)
842 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
848 void GAddress_destroy(GAddress
*address
)
851 free(address
->m_addr
);
856 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
858 address
->m_family
= type
;
861 GAddressType
GAddress_GetFamily(GAddress
*address
)
863 return address
->m_family
;
866 GSocketError
_GAddress_translate_from(GAddress
*address
,
867 struct sockaddr
*addr
, int len
)
869 address
->m_realfamily
= addr
->sa_family
;
870 switch (addr
->sa_family
)
873 address
->m_family
= GSOCK_INET
;
876 address
->m_family
= GSOCK_UNIX
;
880 address
->m_family
= GSOCK_INET6
;
885 address
->m_error
= GSOCK_INVOP
;
891 free(address
->m_addr
);
893 address
->m_len
= len
;
894 address
->m_addr
= (struct sockaddr
*) malloc(len
);
896 if (address
->m_addr
== NULL
)
898 address
->m_error
= GSOCK_MEMERR
;
901 memcpy(address
->m_addr
, addr
, len
);
903 return GSOCK_NOERROR
;
906 GSocketError
_GAddress_translate_to(GAddress
*address
,
907 struct sockaddr
**addr
, int *len
)
909 if (!address
->m_addr
)
911 address
->m_error
= GSOCK_INVADDR
;
912 return GSOCK_INVADDR
;
915 *len
= address
->m_len
;
916 *addr
= (struct sockaddr
*) malloc(address
->m_len
);
919 address
->m_error
= GSOCK_MEMERR
;
923 memcpy(*addr
, address
->m_addr
, address
->m_len
);
924 return GSOCK_NOERROR
;
928 * -------------------------------------------------------------------------
929 * Internet address family
930 * -------------------------------------------------------------------------
933 GSocketError
_GAddress_Init_INET(GAddress
*address
)
935 address
->m_len
= sizeof(struct sockaddr_in
);
936 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
937 if (address
->m_addr
== NULL
)
939 address
->m_error
= GSOCK_MEMERR
;
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
;
948 return GSOCK_NOERROR
;
951 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
954 struct in_addr
*addr
;
956 CHECK_ADDRESS(address
, INET
);
958 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
960 addr
->s_addr
= inet_addr(hostname
);
962 /* If it is a numeric host name, convert it now */
963 if (addr
->s_addr
== INADDR_NONE
)
965 struct in_addr
*array_addr
;
967 /* It is a real name, we solve it */
968 if ((he
= gethostbyname(hostname
)) == NULL
)
970 /* addr->s_addr = INADDR_NONE just done by inet_addr() above */
971 address
->m_error
= GSOCK_NOHOST
;
974 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
975 addr
->s_addr
= array_addr
[0].s_addr
;
977 return GSOCK_NOERROR
;
980 GSocketError
GAddress_INET_SetBroadcastAddress(GAddress
*address
)
982 return GAddress_INET_SetHostAddress(address
, INADDR_BROADCAST
);
985 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
987 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
990 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
991 unsigned long hostaddr
)
993 struct in_addr
*addr
;
995 CHECK_ADDRESS(address
, INET
);
997 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
998 addr
->s_addr
= htonl(hostaddr
);
1000 return GSOCK_NOERROR
;
1003 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1004 const char *protocol
)
1007 struct sockaddr_in
*addr
;
1009 CHECK_ADDRESS(address
, INET
);
1013 address
->m_error
= GSOCK_INVPORT
;
1014 return GSOCK_INVPORT
;
1017 se
= getservbyname(port
, protocol
);
1020 if (isdigit(port
[0]))
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
;
1030 address
->m_error
= GSOCK_INVPORT
;
1031 return GSOCK_INVPORT
;
1034 addr
= (struct sockaddr_in
*)address
->m_addr
;
1035 addr
->sin_port
= se
->s_port
;
1037 return GSOCK_NOERROR
;
1040 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1042 struct sockaddr_in
*addr
;
1044 CHECK_ADDRESS(address
, INET
);
1046 addr
= (struct sockaddr_in
*)address
->m_addr
;
1047 addr
->sin_port
= htons(port
);
1049 return GSOCK_NOERROR
;
1052 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1056 struct sockaddr_in
*addr
;
1058 CHECK_ADDRESS(address
, INET
);
1060 addr
= (struct sockaddr_in
*)address
->m_addr
;
1061 addr_buf
= (char *)&(addr
->sin_addr
);
1063 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1066 address
->m_error
= GSOCK_NOHOST
;
1067 return GSOCK_NOHOST
;
1070 strncpy(hostname
, he
->h_name
, sbuf
);
1072 return GSOCK_NOERROR
;
1075 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1077 struct sockaddr_in
*addr
;
1079 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1081 addr
= (struct sockaddr_in
*)address
->m_addr
;
1083 return ntohl(addr
->sin_addr
.s_addr
);
1086 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1088 struct sockaddr_in
*addr
;
1090 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1092 addr
= (struct sockaddr_in
*)address
->m_addr
;
1093 return ntohs(addr
->sin_port
);
1099 * -------------------------------------------------------------------------
1100 * Internet IPv6 address family
1101 * -------------------------------------------------------------------------
1103 #include "ws2tcpip.h"
1106 #pragma comment(lib,"ws2_32")
1107 #endif // __VISUALC__
1109 GSocketError
_GAddress_Init_INET6(GAddress
*address
)
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
)
1116 address
->m_error
= GSOCK_MEMERR
;
1117 return GSOCK_MEMERR
;
1119 memset(address
->m_addr
,0,address
->m_len
);
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
;
1126 return GSOCK_NOERROR
;
1129 GSocketError
GAddress_INET6_SetHostName(GAddress
*address
, const char *hostname
)
1131 CHECK_ADDRESS(address
, INET6
);
1134 memset( & hints
, 0, sizeof( hints
) );
1135 hints
.ai_family
= AF_INET6
;
1136 addrinfo
* info
= 0;
1137 if ( getaddrinfo( hostname
, "0", & hints
, & info
) || ! info
)
1139 address
->m_error
= GSOCK_NOHOST
;
1140 return GSOCK_NOHOST
;
1143 memcpy( address
->m_addr
, info
->ai_addr
, info
->ai_addrlen
);
1144 freeaddrinfo( info
);
1145 return GSOCK_NOERROR
;
1148 GSocketError
GAddress_INET6_SetAnyAddress(GAddress
*address
)
1150 CHECK_ADDRESS(address
, INET6
);
1152 struct in6_addr addr
;
1153 memset( & addr
, 0, sizeof( addr
) );
1154 return GAddress_INET6_SetHostAddress(address
, addr
);
1156 GSocketError
GAddress_INET6_SetHostAddress(GAddress
*address
,
1157 struct in6_addr hostaddr
)
1159 CHECK_ADDRESS(address
, INET6
);
1161 ((struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
= hostaddr
;
1163 return GSOCK_NOERROR
;
1166 GSocketError
GAddress_INET6_SetPortName(GAddress
*address
, const char *port
,
1167 const char *protocol
)
1170 struct sockaddr_in6
*addr
;
1172 CHECK_ADDRESS(address
, INET6
);
1176 address
->m_error
= GSOCK_INVPORT
;
1177 return GSOCK_INVPORT
;
1180 se
= getservbyname(port
, protocol
);
1183 if (isdigit((unsigned char) port
[0]))
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
;
1193 address
->m_error
= GSOCK_INVPORT
;
1194 return GSOCK_INVPORT
;
1197 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1198 addr
->sin6_port
= se
->s_port
;
1200 return GSOCK_NOERROR
;
1203 GSocketError
GAddress_INET6_SetPort(GAddress
*address
, unsigned short port
)
1205 struct sockaddr_in6
*addr
;
1207 CHECK_ADDRESS(address
, INET6
);
1209 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1210 addr
->sin6_port
= htons(port
);
1212 return GSOCK_NOERROR
;
1215 GSocketError
GAddress_INET6_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1219 struct sockaddr_in6
*addr
;
1221 CHECK_ADDRESS(address
, INET6
);
1223 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1224 addr_buf
= (char *)&(addr
->sin6_addr
);
1226 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin6_addr
), AF_INET6
);
1229 address
->m_error
= GSOCK_NOHOST
;
1230 return GSOCK_NOHOST
;
1233 strncpy(hostname
, he
->h_name
, sbuf
);
1235 return GSOCK_NOERROR
;
1238 GSocketError
GAddress_INET6_GetHostAddress(GAddress
*address
,struct in6_addr
*hostaddr
)
1240 CHECK_ADDRESS_RETVAL(address
, INET6
, GSOCK_INVADDR
);
1241 *hostaddr
= ( (struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
;
1242 return GSOCK_NOERROR
;
1245 unsigned short GAddress_INET6_GetPort(GAddress
*address
)
1247 CHECK_ADDRESS_RETVAL(address
, INET6
, 0);
1249 return ntohs( ((struct sockaddr_in6
*)address
->m_addr
)->sin6_port
);
1252 #endif // wxUSE_IPV6
1255 * -------------------------------------------------------------------------
1256 * Unix address family
1257 * -------------------------------------------------------------------------
1260 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1262 address
->m_error
= GSOCK_INVADDR
;
1263 return GSOCK_INVADDR
;
1266 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *WXUNUSED(path
))
1268 address
->m_error
= GSOCK_INVADDR
;
1269 return GSOCK_INVADDR
;
1272 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *WXUNUSED(path
), size_t WXUNUSED(sbuf
))
1274 address
->m_error
= GSOCK_INVADDR
;
1275 return GSOCK_INVADDR
;
1278 #endif // wxUSE_SOCKETS