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 void GSocket::Close()
106 GSocketManager::Get()->Disable_Events(this);
108 m_fd
= INVALID_SOCKET
;
111 /* Server specific parts */
113 /* GSocket_SetServer:
114 * Sets up this socket as a server. The local address must have been
115 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
116 * Returns GSOCK_NOERROR on success, one of the following otherwise:
119 * GSOCK_INVSOCK - the socket is in use.
120 * GSOCK_INVADDR - the local address has not been set.
121 * GSOCK_IOERR - low-level error.
123 GSocketError
GSocket::SetServer()
127 /* must not be in use */
128 if (m_fd
!= INVALID_SOCKET
)
130 m_error
= GSOCK_INVSOCK
;
131 return GSOCK_INVSOCK
;
134 /* the local addr must have been set */
137 m_error
= GSOCK_INVADDR
;
138 return GSOCK_INVADDR
;
141 /* Initialize all fields */
145 /* Create the socket */
146 m_fd
= socket(m_local
->m_realfamily
, SOCK_STREAM
, 0);
148 if (m_fd
== INVALID_SOCKET
)
150 m_error
= GSOCK_IOERR
;
154 ioctlsocket(m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
155 GSocketManager::Get()->Enable_Events(this);
157 /* allow a socket to re-bind if the socket is in the TIME_WAIT
158 state after being previously closed.
162 setsockopt(m_fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char*)&arg
, sizeof(arg
));
165 /* Bind to the local address,
166 * retrieve the actual address bound,
167 * and listen up to 5 connections.
169 if ((bind(m_fd
, m_local
->m_addr
, m_local
->m_len
) != 0) ||
172 (WX_SOCKLEN_T
*)&m_local
->m_len
) != 0) ||
173 (listen(m_fd
, 5) != 0))
176 m_error
= GSOCK_IOERR
;
180 return GSOCK_NOERROR
;
183 /* GSocket_WaitConnection:
184 * Waits for an incoming client connection. Returns a pointer to
185 * a GSocket object, or NULL if there was an error, in which case
186 * the last error field will be updated for the calling GSocket.
188 * Error codes (set in the calling GSocket)
189 * GSOCK_INVSOCK - the socket is not valid or not a server.
190 * GSOCK_TIMEDOUT - timeout, no incoming connections.
191 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
192 * GSOCK_MEMERR - couldn't allocate memory.
193 * GSOCK_IOERR - low-level error.
195 GSocket
*GSocket::WaitConnection(wxSocketBase
& wxsocket
)
199 WX_SOCKLEN_T fromlen
= sizeof(from
);
203 /* Reenable CONNECTION events */
204 m_detected
&= ~GSOCK_CONNECTION_FLAG
;
206 /* If the socket has already been created, we exit immediately */
207 if (m_fd
== INVALID_SOCKET
|| !m_server
)
209 m_error
= GSOCK_INVSOCK
;
213 /* Create a GSocket object for the new connection */
214 connection
= GSocket::Create(wxsocket
);
218 m_error
= GSOCK_MEMERR
;
222 /* Wait for a connection (with timeout) */
223 if (Input_Timeout() == GSOCK_TIMEDOUT
)
226 /* m_error set by _GSocket_Input_Timeout */
230 connection
->m_fd
= accept(m_fd
, (sockaddr
*)&from
, &fromlen
);
232 if (connection
->m_fd
== INVALID_SOCKET
)
234 if (WSAGetLastError() == WSAEWOULDBLOCK
)
235 m_error
= GSOCK_WOULDBLOCK
;
237 m_error
= GSOCK_IOERR
;
243 /* Initialize all fields */
244 connection
->m_server
= false;
245 connection
->m_stream
= true;
247 /* Setup the peer address field */
248 connection
->m_peer
= GAddress_new();
249 if (!connection
->m_peer
)
252 m_error
= GSOCK_MEMERR
;
255 err
= _GAddress_translate_from(connection
->m_peer
, (sockaddr
*)&from
, fromlen
);
256 if (err
!= GSOCK_NOERROR
)
258 GAddress_destroy(connection
->m_peer
);
264 ioctlsocket(connection
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
265 GSocketManager::Get()->Enable_Events(connection
);
270 /* GSocket_SetReusable:
271 * Simply sets the m_resuable flag on the socket. GSocket_SetServer will
272 * make the appropriate setsockopt() call.
273 * Implemented as a GSocket function because clients (ie, wxSocketServer)
274 * don't have access to the GSocket struct information.
275 * Returns true if the flag was set correctly, false if an error occurred
276 * (ie, if the parameter was NULL)
278 bool GSocket::SetReusable()
280 /* socket must not be null, and must not be in use/already bound */
281 if (this && m_fd
== INVALID_SOCKET
) {
288 /* GSocket_SetBroadcast:
289 * Simply sets the m_broadcast flag on the socket. GSocket_SetServer will
290 * make the appropriate setsockopt() call.
291 * Implemented as a GSocket function because clients (ie, wxSocketServer)
292 * don't have access to the GSocket struct information.
293 * Returns true if the flag was set correctly, false if an error occurred
294 * (ie, if the parameter was NULL)
296 bool GSocket::SetBroadcast()
298 /* socket must not be in use/already bound */
299 if (m_fd
== INVALID_SOCKET
) {
306 bool GSocket::DontDoBind()
308 /* socket must not be in use/already bound */
309 if (m_fd
== INVALID_SOCKET
) {
316 /* Client specific parts */
319 * For stream (connection oriented) sockets, GSocket_Connect() tries
320 * to establish a client connection to a server using the peer address
321 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
322 * connection has been successfully established, or one of the error
323 * codes listed below. Note that for nonblocking sockets, a return
324 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
325 * request can be completed later; you should use GSocket_Select()
326 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
327 * corresponding asynchronous events.
329 * For datagram (non connection oriented) sockets, GSocket_Connect()
330 * just sets the peer address established with GSocket_SetPeer() as
331 * default destination.
334 * GSOCK_INVSOCK - the socket is in use or not valid.
335 * GSOCK_INVADDR - the peer address has not been established.
336 * GSOCK_TIMEDOUT - timeout, the connection failed.
337 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
338 * GSOCK_MEMERR - couldn't allocate memory.
339 * GSOCK_IOERR - low-level error.
341 GSocketError
GSocket::Connect(GSocketStream stream
)
346 /* Enable CONNECTION events (needed for nonblocking connections) */
347 m_detected
&= ~GSOCK_CONNECTION_FLAG
;
349 if (m_fd
!= INVALID_SOCKET
)
351 m_error
= GSOCK_INVSOCK
;
352 return GSOCK_INVSOCK
;
357 m_error
= GSOCK_INVADDR
;
358 return GSOCK_INVADDR
;
361 /* Streamed or dgram socket? */
362 m_stream
= (stream
== GSOCK_STREAMED
);
364 m_establishing
= false;
366 /* Create the socket */
367 m_fd
= socket(m_peer
->m_realfamily
,
368 m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
370 if (m_fd
== INVALID_SOCKET
)
372 m_error
= GSOCK_IOERR
;
376 ioctlsocket(m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
377 GSocketManager::Get()->Enable_Events(this);
379 // If the reuse flag is set, use the applicable socket reuse flag
382 setsockopt(m_fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char*)&arg
, sizeof(arg
));
385 if (m_initialRecvBufferSize
>= 0)
386 setsockopt(m_fd
, SOL_SOCKET
, SO_RCVBUF
, (const char*)&m_initialRecvBufferSize
, sizeof(m_initialRecvBufferSize
));
387 if (m_initialSendBufferSize
>= 0)
388 setsockopt(m_fd
, SOL_SOCKET
, SO_SNDBUF
, (const char*)&m_initialSendBufferSize
, sizeof(m_initialSendBufferSize
));
390 // If a local address has been set, then we need to bind to it before calling connect
391 if (m_local
&& m_local
->m_addr
)
393 bind(m_fd
, m_local
->m_addr
, m_local
->m_len
);
396 /* Connect it to the peer address, with a timeout (see below) */
397 ret
= connect(m_fd
, m_peer
->m_addr
, m_peer
->m_len
);
399 if (ret
== SOCKET_ERROR
)
401 err
= WSAGetLastError();
403 /* If connect failed with EWOULDBLOCK and the GSocket object
404 * is in blocking mode, we select() for the specified timeout
405 * checking for writability to see if the connection request
408 if ((err
== WSAEWOULDBLOCK
) && (!m_non_blocking
))
410 err
= Connect_Timeout();
412 if (err
!= GSOCK_NOERROR
)
415 /* m_error is set in _GSocket_Connect_Timeout */
418 return (GSocketError
) err
;
421 /* If connect failed with EWOULDBLOCK and the GSocket object
422 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
423 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
424 * this way if the connection completes, a GSOCK_CONNECTION
425 * event will be generated, if enabled.
427 if ((err
== WSAEWOULDBLOCK
) && (m_non_blocking
))
429 m_establishing
= true;
430 m_error
= GSOCK_WOULDBLOCK
;
431 return GSOCK_WOULDBLOCK
;
434 /* If connect failed with an error other than EWOULDBLOCK,
435 * then the call to GSocket_Connect() has failed.
438 m_error
= GSOCK_IOERR
;
442 return GSOCK_NOERROR
;
445 /* Datagram sockets */
447 /* GSocket_SetNonOriented:
448 * Sets up this socket as a non-connection oriented (datagram) socket.
449 * Before using this function, the local address must have been set
450 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
451 * on success, or one of the following otherwise.
454 * GSOCK_INVSOCK - the socket is in use.
455 * GSOCK_INVADDR - the local address has not been set.
456 * GSOCK_IOERR - low-level error.
458 GSocketError
GSocket::SetNonOriented()
462 if (m_fd
!= INVALID_SOCKET
)
464 m_error
= GSOCK_INVSOCK
;
465 return GSOCK_INVSOCK
;
470 m_error
= GSOCK_INVADDR
;
471 return GSOCK_INVADDR
;
474 /* Initialize all fields */
478 /* Create the socket */
479 m_fd
= socket(m_local
->m_realfamily
, SOCK_DGRAM
, 0);
481 if (m_fd
== INVALID_SOCKET
)
483 m_error
= GSOCK_IOERR
;
487 ioctlsocket(m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
488 GSocketManager::Get()->Enable_Events(this);
492 setsockopt(m_fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char*)&arg
, sizeof(arg
));
496 setsockopt(m_fd
, SOL_SOCKET
, SO_BROADCAST
, (const char*)&arg
, sizeof(arg
));
500 /* Bind to the local address,
501 * and retrieve the actual address bound.
503 if ((bind(m_fd
, m_local
->m_addr
, m_local
->m_len
) != 0) ||
506 (WX_SOCKLEN_T
*)&m_local
->m_len
) != 0))
509 m_error
= GSOCK_IOERR
;
514 return GSOCK_NOERROR
;
519 /* Like recv(), send(), ... */
520 int GSocket::Read(char *buffer
, int size
)
524 /* Reenable INPUT events */
525 m_detected
&= ~GSOCK_INPUT_FLAG
;
527 if (m_fd
== INVALID_SOCKET
|| m_server
)
529 m_error
= GSOCK_INVSOCK
;
533 /* If the socket is blocking, wait for data (with a timeout) */
534 if (Input_Timeout() == GSOCK_TIMEDOUT
)
536 m_error
= GSOCK_TIMEDOUT
;
542 ret
= Recv_Stream(buffer
, size
);
544 ret
= Recv_Dgram(buffer
, size
);
546 if (ret
== SOCKET_ERROR
)
548 if (WSAGetLastError() != WSAEWOULDBLOCK
)
549 m_error
= GSOCK_IOERR
;
551 m_error
= GSOCK_WOULDBLOCK
;
558 int GSocket::Write(const char *buffer
, int size
)
562 if (m_fd
== INVALID_SOCKET
|| m_server
)
564 m_error
= GSOCK_INVSOCK
;
568 /* If the socket is blocking, wait for writability (with a timeout) */
569 if (Output_Timeout() == GSOCK_TIMEDOUT
)
574 ret
= Send_Stream(buffer
, size
);
576 ret
= Send_Dgram(buffer
, size
);
578 if (ret
== SOCKET_ERROR
)
580 if (WSAGetLastError() != WSAEWOULDBLOCK
)
581 m_error
= GSOCK_IOERR
;
583 m_error
= GSOCK_WOULDBLOCK
;
585 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
586 * does). Once the first OUTPUT event is received, users can assume
587 * that the socket is writable until a read operation fails. Only then
588 * will further OUTPUT events be posted.
590 m_detected
&= ~GSOCK_OUTPUT_FLAG
;
599 /* GSocket_SetNonBlocking:
600 * Sets the socket to non-blocking mode. All IO calls will return
603 void GSocket::SetNonBlocking(bool non_block
)
605 m_non_blocking
= non_block
;
609 * Returns the last error occurred for this socket. Note that successful
610 * operations do not clear this back to GSOCK_NOERROR, so use it only
613 GSocketError WXDLLIMPEXP_NET
GSocket::GetError()
618 GSocketError
GSocket::GetSockOpt(int level
, int optname
,
619 void *optval
, int *optlen
)
621 if (getsockopt(m_fd
, level
, optname
, (char*)optval
, optlen
) == 0)
623 return GSOCK_NOERROR
;
628 GSocketError
GSocket::SetSockOpt(int level
, int optname
,
629 const void *optval
, int optlen
)
631 if (setsockopt(m_fd
, level
, optname
, (char*)optval
, optlen
) == 0)
633 return GSOCK_NOERROR
;
640 /* _GSocket_Input_Timeout:
641 * For blocking sockets, wait until data is available or
642 * until timeout ellapses.
644 GSocketError
GSocket::Input_Timeout()
651 FD_SET(m_fd
, &readfds
);
652 if (select(0, &readfds
, NULL
, NULL
, &m_timeout
) == 0)
654 m_error
= GSOCK_TIMEDOUT
;
655 return GSOCK_TIMEDOUT
;
658 return GSOCK_NOERROR
;
661 /* _GSocket_Output_Timeout:
662 * For blocking sockets, wait until data can be sent without
663 * blocking or until timeout ellapses.
665 GSocketError
GSocket::Output_Timeout()
672 FD_SET(m_fd
, &writefds
);
673 if (select(0, NULL
, &writefds
, NULL
, &m_timeout
) == 0)
675 m_error
= GSOCK_TIMEDOUT
;
676 return GSOCK_TIMEDOUT
;
679 return GSOCK_NOERROR
;
682 /* _GSocket_Connect_Timeout:
683 * For blocking sockets, wait until the connection is
684 * established or fails, or until timeout ellapses.
686 GSocketError
GSocket::Connect_Timeout()
693 FD_SET(m_fd
, &writefds
);
694 FD_SET(m_fd
, &exceptfds
);
695 if (select(0, NULL
, &writefds
, &exceptfds
, &m_timeout
) == 0)
697 m_error
= GSOCK_TIMEDOUT
;
698 return GSOCK_TIMEDOUT
;
700 if (!FD_ISSET(m_fd
, &writefds
))
702 m_error
= GSOCK_IOERR
;
706 return GSOCK_NOERROR
;
709 int GSocket::Recv_Stream(char *buffer
, int size
)
711 return recv(m_fd
, buffer
, size
, 0);
714 int GSocket::Recv_Dgram(char *buffer
, int size
)
717 WX_SOCKLEN_T fromlen
= sizeof(from
);
721 ret
= recvfrom(m_fd
, buffer
, size
, 0, (sockaddr
*)&from
, &fromlen
);
723 if (ret
== SOCKET_ERROR
)
726 /* Translate a system address into a GSocket address */
729 m_peer
= GAddress_new();
732 m_error
= GSOCK_MEMERR
;
736 err
= _GAddress_translate_from(m_peer
, (sockaddr
*)&from
, fromlen
);
737 if (err
!= GSOCK_NOERROR
)
739 GAddress_destroy(m_peer
);
748 int GSocket::Send_Stream(const char *buffer
, int size
)
750 return send(m_fd
, buffer
, size
, 0);
753 int GSocket::Send_Dgram(const char *buffer
, int size
)
755 struct sockaddr
*addr
;
761 m_error
= GSOCK_INVADDR
;
765 err
= _GAddress_translate_to(m_peer
, &addr
, &len
);
766 if (err
!= GSOCK_NOERROR
)
772 ret
= sendto(m_fd
, buffer
, size
, 0, addr
, len
);
774 /* Frees memory allocated by _GAddress_translate_to */
781 * -------------------------------------------------------------------------
783 * -------------------------------------------------------------------------
786 /* CHECK_ADDRESS verifies that the current address family is either
787 * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it
788 * initalizes it to be a GSOCK_*family*. In other cases, it returns
789 * an appropiate error code.
791 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
793 #define CHECK_ADDRESS(address, family) \
795 if (address->m_family == GSOCK_NOFAMILY) \
796 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
797 return address->m_error; \
798 if (address->m_family != GSOCK_##family) \
800 address->m_error = GSOCK_INVADDR; \
801 return GSOCK_INVADDR; \
805 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
807 if (address->m_family == GSOCK_NOFAMILY) \
808 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
810 if (address->m_family != GSOCK_##family) \
812 address->m_error = GSOCK_INVADDR; \
818 GAddress
*GAddress_new()
822 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
825 address
->m_family
= GSOCK_NOFAMILY
;
826 address
->m_addr
= NULL
;
832 GAddress
*GAddress_copy(GAddress
*address
)
836 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
839 memcpy(addr2
, address
, sizeof(GAddress
));
843 addr2
->m_addr
= (struct sockaddr
*) malloc(addr2
->m_len
);
844 if (addr2
->m_addr
== NULL
)
849 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
855 void GAddress_destroy(GAddress
*address
)
858 free(address
->m_addr
);
863 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
865 address
->m_family
= type
;
868 GAddressType
GAddress_GetFamily(GAddress
*address
)
870 return address
->m_family
;
873 GSocketError
_GAddress_translate_from(GAddress
*address
,
874 struct sockaddr
*addr
, int len
)
876 address
->m_realfamily
= addr
->sa_family
;
877 switch (addr
->sa_family
)
880 address
->m_family
= GSOCK_INET
;
883 address
->m_family
= GSOCK_UNIX
;
887 address
->m_family
= GSOCK_INET6
;
892 address
->m_error
= GSOCK_INVOP
;
898 free(address
->m_addr
);
900 address
->m_len
= len
;
901 address
->m_addr
= (struct sockaddr
*) malloc(len
);
903 if (address
->m_addr
== NULL
)
905 address
->m_error
= GSOCK_MEMERR
;
908 memcpy(address
->m_addr
, addr
, len
);
910 return GSOCK_NOERROR
;
913 GSocketError
_GAddress_translate_to(GAddress
*address
,
914 struct sockaddr
**addr
, int *len
)
916 if (!address
->m_addr
)
918 address
->m_error
= GSOCK_INVADDR
;
919 return GSOCK_INVADDR
;
922 *len
= address
->m_len
;
923 *addr
= (struct sockaddr
*) malloc(address
->m_len
);
926 address
->m_error
= GSOCK_MEMERR
;
930 memcpy(*addr
, address
->m_addr
, address
->m_len
);
931 return GSOCK_NOERROR
;
935 * -------------------------------------------------------------------------
936 * Internet address family
937 * -------------------------------------------------------------------------
940 GSocketError
_GAddress_Init_INET(GAddress
*address
)
942 address
->m_len
= sizeof(struct sockaddr_in
);
943 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
944 if (address
->m_addr
== NULL
)
946 address
->m_error
= GSOCK_MEMERR
;
950 address
->m_family
= GSOCK_INET
;
951 address
->m_realfamily
= AF_INET
;
952 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
953 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
955 return GSOCK_NOERROR
;
958 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
961 struct in_addr
*addr
;
963 CHECK_ADDRESS(address
, INET
);
965 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
967 addr
->s_addr
= inet_addr(hostname
);
969 /* If it is a numeric host name, convert it now */
970 if (addr
->s_addr
== INADDR_NONE
)
972 struct in_addr
*array_addr
;
974 /* It is a real name, we solve it */
975 if ((he
= gethostbyname(hostname
)) == NULL
)
977 /* addr->s_addr = INADDR_NONE just done by inet_addr() above */
978 address
->m_error
= GSOCK_NOHOST
;
981 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
982 addr
->s_addr
= array_addr
[0].s_addr
;
984 return GSOCK_NOERROR
;
987 GSocketError
GAddress_INET_SetBroadcastAddress(GAddress
*address
)
989 return GAddress_INET_SetHostAddress(address
, INADDR_BROADCAST
);
992 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
994 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
997 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
998 unsigned long hostaddr
)
1000 struct in_addr
*addr
;
1002 CHECK_ADDRESS(address
, INET
);
1004 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1005 addr
->s_addr
= htonl(hostaddr
);
1007 return GSOCK_NOERROR
;
1010 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1011 const char *protocol
)
1014 struct sockaddr_in
*addr
;
1016 CHECK_ADDRESS(address
, INET
);
1020 address
->m_error
= GSOCK_INVPORT
;
1021 return GSOCK_INVPORT
;
1024 se
= getservbyname(port
, protocol
);
1027 if (isdigit(port
[0]))
1031 port_int
= atoi(port
);
1032 addr
= (struct sockaddr_in
*)address
->m_addr
;
1033 addr
->sin_port
= htons((u_short
) port_int
);
1034 return GSOCK_NOERROR
;
1037 address
->m_error
= GSOCK_INVPORT
;
1038 return GSOCK_INVPORT
;
1041 addr
= (struct sockaddr_in
*)address
->m_addr
;
1042 addr
->sin_port
= se
->s_port
;
1044 return GSOCK_NOERROR
;
1047 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1049 struct sockaddr_in
*addr
;
1051 CHECK_ADDRESS(address
, INET
);
1053 addr
= (struct sockaddr_in
*)address
->m_addr
;
1054 addr
->sin_port
= htons(port
);
1056 return GSOCK_NOERROR
;
1059 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1063 struct sockaddr_in
*addr
;
1065 CHECK_ADDRESS(address
, INET
);
1067 addr
= (struct sockaddr_in
*)address
->m_addr
;
1068 addr_buf
= (char *)&(addr
->sin_addr
);
1070 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1073 address
->m_error
= GSOCK_NOHOST
;
1074 return GSOCK_NOHOST
;
1077 strncpy(hostname
, he
->h_name
, sbuf
);
1079 return GSOCK_NOERROR
;
1082 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1084 struct sockaddr_in
*addr
;
1086 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1088 addr
= (struct sockaddr_in
*)address
->m_addr
;
1090 return ntohl(addr
->sin_addr
.s_addr
);
1093 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1095 struct sockaddr_in
*addr
;
1097 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1099 addr
= (struct sockaddr_in
*)address
->m_addr
;
1100 return ntohs(addr
->sin_port
);
1106 * -------------------------------------------------------------------------
1107 * Internet IPv6 address family
1108 * -------------------------------------------------------------------------
1110 #include "ws2tcpip.h"
1113 #pragma comment(lib,"ws2_32")
1114 #endif // __VISUALC__
1116 GSocketError
_GAddress_Init_INET6(GAddress
*address
)
1118 struct in6_addr any_address
= IN6ADDR_ANY_INIT
;
1119 address
->m_len
= sizeof(struct sockaddr_in6
);
1120 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1121 if (address
->m_addr
== NULL
)
1123 address
->m_error
= GSOCK_MEMERR
;
1124 return GSOCK_MEMERR
;
1126 memset(address
->m_addr
,0,address
->m_len
);
1128 address
->m_family
= GSOCK_INET6
;
1129 address
->m_realfamily
= AF_INET6
;
1130 ((struct sockaddr_in6
*)address
->m_addr
)->sin6_family
= AF_INET6
;
1131 ((struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
= any_address
;
1133 return GSOCK_NOERROR
;
1136 GSocketError
GAddress_INET6_SetHostName(GAddress
*address
, const char *hostname
)
1138 CHECK_ADDRESS(address
, INET6
);
1141 memset( & hints
, 0, sizeof( hints
) );
1142 hints
.ai_family
= AF_INET6
;
1143 addrinfo
* info
= 0;
1144 if ( getaddrinfo( hostname
, "0", & hints
, & info
) || ! info
)
1146 address
->m_error
= GSOCK_NOHOST
;
1147 return GSOCK_NOHOST
;
1150 memcpy( address
->m_addr
, info
->ai_addr
, info
->ai_addrlen
);
1151 freeaddrinfo( info
);
1152 return GSOCK_NOERROR
;
1155 GSocketError
GAddress_INET6_SetAnyAddress(GAddress
*address
)
1157 CHECK_ADDRESS(address
, INET6
);
1159 struct in6_addr addr
;
1160 memset( & addr
, 0, sizeof( addr
) );
1161 return GAddress_INET6_SetHostAddress(address
, addr
);
1163 GSocketError
GAddress_INET6_SetHostAddress(GAddress
*address
,
1164 struct in6_addr hostaddr
)
1166 CHECK_ADDRESS(address
, INET6
);
1168 ((struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
= hostaddr
;
1170 return GSOCK_NOERROR
;
1173 GSocketError
GAddress_INET6_SetPortName(GAddress
*address
, const char *port
,
1174 const char *protocol
)
1177 struct sockaddr_in6
*addr
;
1179 CHECK_ADDRESS(address
, INET6
);
1183 address
->m_error
= GSOCK_INVPORT
;
1184 return GSOCK_INVPORT
;
1187 se
= getservbyname(port
, protocol
);
1190 if (isdigit((unsigned char) port
[0]))
1194 port_int
= atoi(port
);
1195 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1196 addr
->sin6_port
= htons((u_short
) port_int
);
1197 return GSOCK_NOERROR
;
1200 address
->m_error
= GSOCK_INVPORT
;
1201 return GSOCK_INVPORT
;
1204 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1205 addr
->sin6_port
= se
->s_port
;
1207 return GSOCK_NOERROR
;
1210 GSocketError
GAddress_INET6_SetPort(GAddress
*address
, unsigned short port
)
1212 struct sockaddr_in6
*addr
;
1214 CHECK_ADDRESS(address
, INET6
);
1216 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1217 addr
->sin6_port
= htons(port
);
1219 return GSOCK_NOERROR
;
1222 GSocketError
GAddress_INET6_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1226 struct sockaddr_in6
*addr
;
1228 CHECK_ADDRESS(address
, INET6
);
1230 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1231 addr_buf
= (char *)&(addr
->sin6_addr
);
1233 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin6_addr
), AF_INET6
);
1236 address
->m_error
= GSOCK_NOHOST
;
1237 return GSOCK_NOHOST
;
1240 strncpy(hostname
, he
->h_name
, sbuf
);
1242 return GSOCK_NOERROR
;
1245 GSocketError
GAddress_INET6_GetHostAddress(GAddress
*address
,struct in6_addr
*hostaddr
)
1247 CHECK_ADDRESS_RETVAL(address
, INET6
, GSOCK_INVADDR
);
1248 *hostaddr
= ( (struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
;
1249 return GSOCK_NOERROR
;
1252 unsigned short GAddress_INET6_GetPort(GAddress
*address
)
1254 CHECK_ADDRESS_RETVAL(address
, INET6
, 0);
1256 return ntohs( ((struct sockaddr_in6
*)address
->m_addr
)->sin6_port
);
1259 #endif // wxUSE_IPV6
1262 * -------------------------------------------------------------------------
1263 * Unix address family
1264 * -------------------------------------------------------------------------
1267 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1269 address
->m_error
= GSOCK_INVADDR
;
1270 return GSOCK_INVADDR
;
1273 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *WXUNUSED(path
))
1275 address
->m_error
= GSOCK_INVADDR
;
1276 return GSOCK_INVADDR
;
1279 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *WXUNUSED(path
), size_t WXUNUSED(sbuf
))
1281 address
->m_error
= GSOCK_INVADDR
;
1282 return GSOCK_INVADDR
;
1285 #endif // wxUSE_SOCKETS