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 /* if we use configure for MSW WX_SOCKLEN_T will be already defined */
80 # define WX_SOCKLEN_T int
84 typedef struct sockaddr_storage wxSockAddr
;
86 typedef struct sockaddr wxSockAddr
;
93 GSocketManager
* const manager
= GSocketManager::Get();
94 if ( !manager
|| !manager
->OnInit() )
97 /* Initialize WinSocket */
98 return WSAStartup((1 << 8) | 1, &wsaData
) == 0;
101 void GSocket_Cleanup()
103 GSocketManager
* const manager
= GSocketManager::Get();
107 /* Cleanup WinSocket */
111 /* Constructors / Destructors for GSocket */
113 void GSocket::Close()
115 GSocketManager::Get()->Disable_Events(this);
117 m_fd
= INVALID_SOCKET
;
120 /* Address handling */
126 * Set or get the local or peer address for this socket. The 'set'
127 * functions return GSOCK_NOERROR on success, an error code otherwise.
128 * The 'get' functions return a pointer to a GAddress object on success,
129 * or NULL otherwise, in which case they set the error code of the
130 * corresponding GSocket.
133 * GSOCK_INVSOCK - the socket is not valid.
134 * GSOCK_INVADDR - the address is not valid.
136 GSocketError
GSocket::SetLocal(GAddress
*address
)
138 /* the socket must be initialized, or it must be a server */
139 if (m_fd
!= INVALID_SOCKET
&& !m_server
)
141 m_error
= GSOCK_INVSOCK
;
142 return GSOCK_INVSOCK
;
146 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
148 m_error
= GSOCK_INVADDR
;
149 return GSOCK_INVADDR
;
153 GAddress_destroy(m_local
);
155 m_local
= GAddress_copy(address
);
157 return GSOCK_NOERROR
;
160 GSocketError
GSocket::SetPeer(GAddress
*address
)
163 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
165 m_error
= GSOCK_INVADDR
;
166 return GSOCK_INVADDR
;
170 GAddress_destroy(m_peer
);
172 m_peer
= GAddress_copy(address
);
174 return GSOCK_NOERROR
;
177 GAddress
*GSocket::GetLocal()
181 WX_SOCKLEN_T size
= sizeof(addr
);
184 /* try to get it from the m_local var first */
186 return GAddress_copy(m_local
);
188 /* else, if the socket is initialized, try getsockname */
189 if (m_fd
== INVALID_SOCKET
)
191 m_error
= GSOCK_INVSOCK
;
195 if (getsockname(m_fd
, (sockaddr
*)&addr
, &size
) == SOCKET_ERROR
)
197 m_error
= GSOCK_IOERR
;
201 /* got a valid address from getsockname, create a GAddress object */
202 if ((address
= GAddress_new()) == NULL
)
204 m_error
= GSOCK_MEMERR
;
208 if ((err
= _GAddress_translate_from(address
, (sockaddr
*)&addr
, size
)) != GSOCK_NOERROR
)
210 GAddress_destroy(address
);
218 GAddress
*GSocket::GetPeer()
220 /* try to get it from the m_peer var */
222 return GAddress_copy(m_peer
);
227 /* Server specific parts */
229 /* GSocket_SetServer:
230 * Sets up this socket as a server. The local address must have been
231 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
232 * Returns GSOCK_NOERROR on success, one of the following otherwise:
235 * GSOCK_INVSOCK - the socket is in use.
236 * GSOCK_INVADDR - the local address has not been set.
237 * GSOCK_IOERR - low-level error.
239 GSocketError
GSocket::SetServer()
243 /* must not be in use */
244 if (m_fd
!= INVALID_SOCKET
)
246 m_error
= GSOCK_INVSOCK
;
247 return GSOCK_INVSOCK
;
250 /* the local addr must have been set */
253 m_error
= GSOCK_INVADDR
;
254 return GSOCK_INVADDR
;
257 /* Initialize all fields */
261 /* Create the socket */
262 m_fd
= socket(m_local
->m_realfamily
, SOCK_STREAM
, 0);
264 if (m_fd
== INVALID_SOCKET
)
266 m_error
= GSOCK_IOERR
;
270 ioctlsocket(m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
271 GSocketManager::Get()->Enable_Events(this);
273 /* allow a socket to re-bind if the socket is in the TIME_WAIT
274 state after being previously closed.
278 setsockopt(m_fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char*)&arg
, sizeof(arg
));
281 /* Bind to the local address,
282 * retrieve the actual address bound,
283 * and listen up to 5 connections.
285 if ((bind(m_fd
, m_local
->m_addr
, m_local
->m_len
) != 0) ||
288 (WX_SOCKLEN_T
*)&m_local
->m_len
) != 0) ||
289 (listen(m_fd
, 5) != 0))
292 m_error
= GSOCK_IOERR
;
296 return GSOCK_NOERROR
;
299 /* GSocket_WaitConnection:
300 * Waits for an incoming client connection. Returns a pointer to
301 * a GSocket object, or NULL if there was an error, in which case
302 * the last error field will be updated for the calling GSocket.
304 * Error codes (set in the calling GSocket)
305 * GSOCK_INVSOCK - the socket is not valid or not a server.
306 * GSOCK_TIMEDOUT - timeout, no incoming connections.
307 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
308 * GSOCK_MEMERR - couldn't allocate memory.
309 * GSOCK_IOERR - low-level error.
311 GSocket
*GSocket::WaitConnection(wxSocketBase
& wxsocket
)
315 WX_SOCKLEN_T fromlen
= sizeof(from
);
319 /* Reenable CONNECTION events */
320 m_detected
&= ~GSOCK_CONNECTION_FLAG
;
322 /* If the socket has already been created, we exit immediately */
323 if (m_fd
== INVALID_SOCKET
|| !m_server
)
325 m_error
= GSOCK_INVSOCK
;
329 /* Create a GSocket object for the new connection */
330 connection
= GSocket::Create(wxsocket
);
334 m_error
= GSOCK_MEMERR
;
338 /* Wait for a connection (with timeout) */
339 if (Input_Timeout() == GSOCK_TIMEDOUT
)
342 /* m_error set by _GSocket_Input_Timeout */
346 connection
->m_fd
= accept(m_fd
, (sockaddr
*)&from
, &fromlen
);
348 if (connection
->m_fd
== INVALID_SOCKET
)
350 if (WSAGetLastError() == WSAEWOULDBLOCK
)
351 m_error
= GSOCK_WOULDBLOCK
;
353 m_error
= GSOCK_IOERR
;
359 /* Initialize all fields */
360 connection
->m_server
= false;
361 connection
->m_stream
= true;
363 /* Setup the peer address field */
364 connection
->m_peer
= GAddress_new();
365 if (!connection
->m_peer
)
368 m_error
= GSOCK_MEMERR
;
371 err
= _GAddress_translate_from(connection
->m_peer
, (sockaddr
*)&from
, fromlen
);
372 if (err
!= GSOCK_NOERROR
)
374 GAddress_destroy(connection
->m_peer
);
380 ioctlsocket(connection
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
381 GSocketManager::Get()->Enable_Events(connection
);
386 /* GSocket_SetReusable:
387 * Simply sets the m_resuable flag on the socket. GSocket_SetServer will
388 * make the appropriate setsockopt() call.
389 * Implemented as a GSocket function because clients (ie, wxSocketServer)
390 * don't have access to the GSocket struct information.
391 * Returns true if the flag was set correctly, false if an error occurred
392 * (ie, if the parameter was NULL)
394 bool GSocket::SetReusable()
396 /* socket must not be null, and must not be in use/already bound */
397 if (this && m_fd
== INVALID_SOCKET
) {
404 /* GSocket_SetBroadcast:
405 * Simply sets the m_broadcast flag on the socket. GSocket_SetServer will
406 * make the appropriate setsockopt() call.
407 * Implemented as a GSocket function because clients (ie, wxSocketServer)
408 * don't have access to the GSocket struct information.
409 * Returns true if the flag was set correctly, false if an error occurred
410 * (ie, if the parameter was NULL)
412 bool GSocket::SetBroadcast()
414 /* socket must not be in use/already bound */
415 if (m_fd
== INVALID_SOCKET
) {
422 bool GSocket::DontDoBind()
424 /* socket must not be in use/already bound */
425 if (m_fd
== INVALID_SOCKET
) {
432 /* Client specific parts */
435 * For stream (connection oriented) sockets, GSocket_Connect() tries
436 * to establish a client connection to a server using the peer address
437 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
438 * connection has been successfully established, or one of the error
439 * codes listed below. Note that for nonblocking sockets, a return
440 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
441 * request can be completed later; you should use GSocket_Select()
442 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
443 * corresponding asynchronous events.
445 * For datagram (non connection oriented) sockets, GSocket_Connect()
446 * just sets the peer address established with GSocket_SetPeer() as
447 * default destination.
450 * GSOCK_INVSOCK - the socket is in use or not valid.
451 * GSOCK_INVADDR - the peer address has not been established.
452 * GSOCK_TIMEDOUT - timeout, the connection failed.
453 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
454 * GSOCK_MEMERR - couldn't allocate memory.
455 * GSOCK_IOERR - low-level error.
457 GSocketError
GSocket::Connect(GSocketStream stream
)
462 /* Enable CONNECTION events (needed for nonblocking connections) */
463 m_detected
&= ~GSOCK_CONNECTION_FLAG
;
465 if (m_fd
!= INVALID_SOCKET
)
467 m_error
= GSOCK_INVSOCK
;
468 return GSOCK_INVSOCK
;
473 m_error
= GSOCK_INVADDR
;
474 return GSOCK_INVADDR
;
477 /* Streamed or dgram socket? */
478 m_stream
= (stream
== GSOCK_STREAMED
);
480 m_establishing
= false;
482 /* Create the socket */
483 m_fd
= socket(m_peer
->m_realfamily
,
484 m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
486 if (m_fd
== INVALID_SOCKET
)
488 m_error
= GSOCK_IOERR
;
492 ioctlsocket(m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
493 GSocketManager::Get()->Enable_Events(this);
495 // If the reuse flag is set, use the applicable socket reuse flag
498 setsockopt(m_fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char*)&arg
, sizeof(arg
));
501 if (m_initialRecvBufferSize
>= 0)
502 setsockopt(m_fd
, SOL_SOCKET
, SO_RCVBUF
, (const char*)&m_initialRecvBufferSize
, sizeof(m_initialRecvBufferSize
));
503 if (m_initialSendBufferSize
>= 0)
504 setsockopt(m_fd
, SOL_SOCKET
, SO_SNDBUF
, (const char*)&m_initialSendBufferSize
, sizeof(m_initialSendBufferSize
));
506 // If a local address has been set, then we need to bind to it before calling connect
507 if (m_local
&& m_local
->m_addr
)
509 bind(m_fd
, m_local
->m_addr
, m_local
->m_len
);
512 /* Connect it to the peer address, with a timeout (see below) */
513 ret
= connect(m_fd
, m_peer
->m_addr
, m_peer
->m_len
);
515 if (ret
== SOCKET_ERROR
)
517 err
= WSAGetLastError();
519 /* If connect failed with EWOULDBLOCK and the GSocket object
520 * is in blocking mode, we select() for the specified timeout
521 * checking for writability to see if the connection request
524 if ((err
== WSAEWOULDBLOCK
) && (!m_non_blocking
))
526 err
= Connect_Timeout();
528 if (err
!= GSOCK_NOERROR
)
531 /* m_error is set in _GSocket_Connect_Timeout */
534 return (GSocketError
) err
;
537 /* If connect failed with EWOULDBLOCK and the GSocket object
538 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
539 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
540 * this way if the connection completes, a GSOCK_CONNECTION
541 * event will be generated, if enabled.
543 if ((err
== WSAEWOULDBLOCK
) && (m_non_blocking
))
545 m_establishing
= true;
546 m_error
= GSOCK_WOULDBLOCK
;
547 return GSOCK_WOULDBLOCK
;
550 /* If connect failed with an error other than EWOULDBLOCK,
551 * then the call to GSocket_Connect() has failed.
554 m_error
= GSOCK_IOERR
;
558 return GSOCK_NOERROR
;
561 /* Datagram sockets */
563 /* GSocket_SetNonOriented:
564 * Sets up this socket as a non-connection oriented (datagram) socket.
565 * Before using this function, the local address must have been set
566 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
567 * on success, or one of the following otherwise.
570 * GSOCK_INVSOCK - the socket is in use.
571 * GSOCK_INVADDR - the local address has not been set.
572 * GSOCK_IOERR - low-level error.
574 GSocketError
GSocket::SetNonOriented()
578 if (m_fd
!= INVALID_SOCKET
)
580 m_error
= GSOCK_INVSOCK
;
581 return GSOCK_INVSOCK
;
586 m_error
= GSOCK_INVADDR
;
587 return GSOCK_INVADDR
;
590 /* Initialize all fields */
594 /* Create the socket */
595 m_fd
= socket(m_local
->m_realfamily
, SOCK_DGRAM
, 0);
597 if (m_fd
== INVALID_SOCKET
)
599 m_error
= GSOCK_IOERR
;
603 ioctlsocket(m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
604 GSocketManager::Get()->Enable_Events(this);
608 setsockopt(m_fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char*)&arg
, sizeof(arg
));
612 setsockopt(m_fd
, SOL_SOCKET
, SO_BROADCAST
, (const char*)&arg
, sizeof(arg
));
616 /* Bind to the local address,
617 * and retrieve the actual address bound.
619 if ((bind(m_fd
, m_local
->m_addr
, m_local
->m_len
) != 0) ||
622 (WX_SOCKLEN_T
*)&m_local
->m_len
) != 0))
625 m_error
= GSOCK_IOERR
;
630 return GSOCK_NOERROR
;
635 /* Like recv(), send(), ... */
636 int GSocket::Read(char *buffer
, int size
)
640 /* Reenable INPUT events */
641 m_detected
&= ~GSOCK_INPUT_FLAG
;
643 if (m_fd
== INVALID_SOCKET
|| m_server
)
645 m_error
= GSOCK_INVSOCK
;
649 /* If the socket is blocking, wait for data (with a timeout) */
650 if (Input_Timeout() == GSOCK_TIMEDOUT
)
652 m_error
= GSOCK_TIMEDOUT
;
658 ret
= Recv_Stream(buffer
, size
);
660 ret
= Recv_Dgram(buffer
, size
);
662 if (ret
== SOCKET_ERROR
)
664 if (WSAGetLastError() != WSAEWOULDBLOCK
)
665 m_error
= GSOCK_IOERR
;
667 m_error
= GSOCK_WOULDBLOCK
;
674 int GSocket::Write(const char *buffer
, int size
)
678 if (m_fd
== INVALID_SOCKET
|| m_server
)
680 m_error
= GSOCK_INVSOCK
;
684 /* If the socket is blocking, wait for writability (with a timeout) */
685 if (Output_Timeout() == GSOCK_TIMEDOUT
)
690 ret
= Send_Stream(buffer
, size
);
692 ret
= Send_Dgram(buffer
, size
);
694 if (ret
== SOCKET_ERROR
)
696 if (WSAGetLastError() != WSAEWOULDBLOCK
)
697 m_error
= GSOCK_IOERR
;
699 m_error
= GSOCK_WOULDBLOCK
;
701 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
702 * does). Once the first OUTPUT event is received, users can assume
703 * that the socket is writable until a read operation fails. Only then
704 * will further OUTPUT events be posted.
706 m_detected
&= ~GSOCK_OUTPUT_FLAG
;
715 /* GSocket_SetNonBlocking:
716 * Sets the socket to non-blocking mode. All IO calls will return
719 void GSocket::SetNonBlocking(bool non_block
)
721 m_non_blocking
= non_block
;
725 * Returns the last error occurred for this socket. Note that successful
726 * operations do not clear this back to GSOCK_NOERROR, so use it only
729 GSocketError WXDLLIMPEXP_NET
GSocket::GetError()
734 GSocketError
GSocket::GetSockOpt(int level
, int optname
,
735 void *optval
, int *optlen
)
737 if (getsockopt(m_fd
, level
, optname
, (char*)optval
, optlen
) == 0)
739 return GSOCK_NOERROR
;
744 GSocketError
GSocket::SetSockOpt(int level
, int optname
,
745 const void *optval
, int optlen
)
747 if (setsockopt(m_fd
, level
, optname
, (char*)optval
, optlen
) == 0)
749 return GSOCK_NOERROR
;
756 /* _GSocket_Input_Timeout:
757 * For blocking sockets, wait until data is available or
758 * until timeout ellapses.
760 GSocketError
GSocket::Input_Timeout()
767 FD_SET(m_fd
, &readfds
);
768 if (select(0, &readfds
, NULL
, NULL
, &m_timeout
) == 0)
770 m_error
= GSOCK_TIMEDOUT
;
771 return GSOCK_TIMEDOUT
;
774 return GSOCK_NOERROR
;
777 /* _GSocket_Output_Timeout:
778 * For blocking sockets, wait until data can be sent without
779 * blocking or until timeout ellapses.
781 GSocketError
GSocket::Output_Timeout()
788 FD_SET(m_fd
, &writefds
);
789 if (select(0, NULL
, &writefds
, NULL
, &m_timeout
) == 0)
791 m_error
= GSOCK_TIMEDOUT
;
792 return GSOCK_TIMEDOUT
;
795 return GSOCK_NOERROR
;
798 /* _GSocket_Connect_Timeout:
799 * For blocking sockets, wait until the connection is
800 * established or fails, or until timeout ellapses.
802 GSocketError
GSocket::Connect_Timeout()
809 FD_SET(m_fd
, &writefds
);
810 FD_SET(m_fd
, &exceptfds
);
811 if (select(0, NULL
, &writefds
, &exceptfds
, &m_timeout
) == 0)
813 m_error
= GSOCK_TIMEDOUT
;
814 return GSOCK_TIMEDOUT
;
816 if (!FD_ISSET(m_fd
, &writefds
))
818 m_error
= GSOCK_IOERR
;
822 return GSOCK_NOERROR
;
825 int GSocket::Recv_Stream(char *buffer
, int size
)
827 return recv(m_fd
, buffer
, size
, 0);
830 int GSocket::Recv_Dgram(char *buffer
, int size
)
833 WX_SOCKLEN_T fromlen
= sizeof(from
);
837 ret
= recvfrom(m_fd
, buffer
, size
, 0, (sockaddr
*)&from
, &fromlen
);
839 if (ret
== SOCKET_ERROR
)
842 /* Translate a system address into a GSocket address */
845 m_peer
= GAddress_new();
848 m_error
= GSOCK_MEMERR
;
852 err
= _GAddress_translate_from(m_peer
, (sockaddr
*)&from
, fromlen
);
853 if (err
!= GSOCK_NOERROR
)
855 GAddress_destroy(m_peer
);
864 int GSocket::Send_Stream(const char *buffer
, int size
)
866 return send(m_fd
, buffer
, size
, 0);
869 int GSocket::Send_Dgram(const char *buffer
, int size
)
871 struct sockaddr
*addr
;
877 m_error
= GSOCK_INVADDR
;
881 err
= _GAddress_translate_to(m_peer
, &addr
, &len
);
882 if (err
!= GSOCK_NOERROR
)
888 ret
= sendto(m_fd
, buffer
, size
, 0, addr
, len
);
890 /* Frees memory allocated by _GAddress_translate_to */
897 * -------------------------------------------------------------------------
899 * -------------------------------------------------------------------------
902 /* CHECK_ADDRESS verifies that the current address family is either
903 * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it
904 * initalizes it to be a GSOCK_*family*. In other cases, it returns
905 * an appropiate error code.
907 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
909 #define CHECK_ADDRESS(address, family) \
911 if (address->m_family == GSOCK_NOFAMILY) \
912 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
913 return address->m_error; \
914 if (address->m_family != GSOCK_##family) \
916 address->m_error = GSOCK_INVADDR; \
917 return GSOCK_INVADDR; \
921 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
923 if (address->m_family == GSOCK_NOFAMILY) \
924 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
926 if (address->m_family != GSOCK_##family) \
928 address->m_error = GSOCK_INVADDR; \
934 GAddress
*GAddress_new()
938 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
941 address
->m_family
= GSOCK_NOFAMILY
;
942 address
->m_addr
= NULL
;
948 GAddress
*GAddress_copy(GAddress
*address
)
952 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
955 memcpy(addr2
, address
, sizeof(GAddress
));
959 addr2
->m_addr
= (struct sockaddr
*) malloc(addr2
->m_len
);
960 if (addr2
->m_addr
== NULL
)
965 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
971 void GAddress_destroy(GAddress
*address
)
974 free(address
->m_addr
);
979 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
981 address
->m_family
= type
;
984 GAddressType
GAddress_GetFamily(GAddress
*address
)
986 return address
->m_family
;
989 GSocketError
_GAddress_translate_from(GAddress
*address
,
990 struct sockaddr
*addr
, int len
)
992 address
->m_realfamily
= addr
->sa_family
;
993 switch (addr
->sa_family
)
996 address
->m_family
= GSOCK_INET
;
999 address
->m_family
= GSOCK_UNIX
;
1003 address
->m_family
= GSOCK_INET6
;
1008 address
->m_error
= GSOCK_INVOP
;
1013 if (address
->m_addr
)
1014 free(address
->m_addr
);
1016 address
->m_len
= len
;
1017 address
->m_addr
= (struct sockaddr
*) malloc(len
);
1019 if (address
->m_addr
== NULL
)
1021 address
->m_error
= GSOCK_MEMERR
;
1022 return GSOCK_MEMERR
;
1024 memcpy(address
->m_addr
, addr
, len
);
1026 return GSOCK_NOERROR
;
1029 GSocketError
_GAddress_translate_to(GAddress
*address
,
1030 struct sockaddr
**addr
, int *len
)
1032 if (!address
->m_addr
)
1034 address
->m_error
= GSOCK_INVADDR
;
1035 return GSOCK_INVADDR
;
1038 *len
= address
->m_len
;
1039 *addr
= (struct sockaddr
*) malloc(address
->m_len
);
1042 address
->m_error
= GSOCK_MEMERR
;
1043 return GSOCK_MEMERR
;
1046 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1047 return GSOCK_NOERROR
;
1051 * -------------------------------------------------------------------------
1052 * Internet address family
1053 * -------------------------------------------------------------------------
1056 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1058 address
->m_len
= sizeof(struct sockaddr_in
);
1059 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1060 if (address
->m_addr
== NULL
)
1062 address
->m_error
= GSOCK_MEMERR
;
1063 return GSOCK_MEMERR
;
1066 address
->m_family
= GSOCK_INET
;
1067 address
->m_realfamily
= AF_INET
;
1068 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1069 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1071 return GSOCK_NOERROR
;
1074 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1077 struct in_addr
*addr
;
1079 CHECK_ADDRESS(address
, INET
);
1081 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1083 addr
->s_addr
= inet_addr(hostname
);
1085 /* If it is a numeric host name, convert it now */
1086 if (addr
->s_addr
== INADDR_NONE
)
1088 struct in_addr
*array_addr
;
1090 /* It is a real name, we solve it */
1091 if ((he
= gethostbyname(hostname
)) == NULL
)
1093 /* addr->s_addr = INADDR_NONE just done by inet_addr() above */
1094 address
->m_error
= GSOCK_NOHOST
;
1095 return GSOCK_NOHOST
;
1097 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1098 addr
->s_addr
= array_addr
[0].s_addr
;
1100 return GSOCK_NOERROR
;
1103 GSocketError
GAddress_INET_SetBroadcastAddress(GAddress
*address
)
1105 return GAddress_INET_SetHostAddress(address
, INADDR_BROADCAST
);
1108 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1110 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1113 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1114 unsigned long hostaddr
)
1116 struct in_addr
*addr
;
1118 CHECK_ADDRESS(address
, INET
);
1120 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1121 addr
->s_addr
= htonl(hostaddr
);
1123 return GSOCK_NOERROR
;
1126 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1127 const char *protocol
)
1130 struct sockaddr_in
*addr
;
1132 CHECK_ADDRESS(address
, INET
);
1136 address
->m_error
= GSOCK_INVPORT
;
1137 return GSOCK_INVPORT
;
1140 se
= getservbyname(port
, protocol
);
1143 if (isdigit(port
[0]))
1147 port_int
= atoi(port
);
1148 addr
= (struct sockaddr_in
*)address
->m_addr
;
1149 addr
->sin_port
= htons((u_short
) port_int
);
1150 return GSOCK_NOERROR
;
1153 address
->m_error
= GSOCK_INVPORT
;
1154 return GSOCK_INVPORT
;
1157 addr
= (struct sockaddr_in
*)address
->m_addr
;
1158 addr
->sin_port
= se
->s_port
;
1160 return GSOCK_NOERROR
;
1163 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1165 struct sockaddr_in
*addr
;
1167 CHECK_ADDRESS(address
, INET
);
1169 addr
= (struct sockaddr_in
*)address
->m_addr
;
1170 addr
->sin_port
= htons(port
);
1172 return GSOCK_NOERROR
;
1175 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1179 struct sockaddr_in
*addr
;
1181 CHECK_ADDRESS(address
, INET
);
1183 addr
= (struct sockaddr_in
*)address
->m_addr
;
1184 addr_buf
= (char *)&(addr
->sin_addr
);
1186 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1189 address
->m_error
= GSOCK_NOHOST
;
1190 return GSOCK_NOHOST
;
1193 strncpy(hostname
, he
->h_name
, sbuf
);
1195 return GSOCK_NOERROR
;
1198 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1200 struct sockaddr_in
*addr
;
1202 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1204 addr
= (struct sockaddr_in
*)address
->m_addr
;
1206 return ntohl(addr
->sin_addr
.s_addr
);
1209 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1211 struct sockaddr_in
*addr
;
1213 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1215 addr
= (struct sockaddr_in
*)address
->m_addr
;
1216 return ntohs(addr
->sin_port
);
1222 * -------------------------------------------------------------------------
1223 * Internet IPv6 address family
1224 * -------------------------------------------------------------------------
1226 #include "ws2tcpip.h"
1229 #pragma comment(lib,"ws2_32")
1230 #endif // __VISUALC__
1232 GSocketError
_GAddress_Init_INET6(GAddress
*address
)
1234 struct in6_addr any_address
= IN6ADDR_ANY_INIT
;
1235 address
->m_len
= sizeof(struct sockaddr_in6
);
1236 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1237 if (address
->m_addr
== NULL
)
1239 address
->m_error
= GSOCK_MEMERR
;
1240 return GSOCK_MEMERR
;
1242 memset(address
->m_addr
,0,address
->m_len
);
1244 address
->m_family
= GSOCK_INET6
;
1245 address
->m_realfamily
= AF_INET6
;
1246 ((struct sockaddr_in6
*)address
->m_addr
)->sin6_family
= AF_INET6
;
1247 ((struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
= any_address
;
1249 return GSOCK_NOERROR
;
1252 GSocketError
GAddress_INET6_SetHostName(GAddress
*address
, const char *hostname
)
1254 CHECK_ADDRESS(address
, INET6
);
1257 memset( & hints
, 0, sizeof( hints
) );
1258 hints
.ai_family
= AF_INET6
;
1259 addrinfo
* info
= 0;
1260 if ( getaddrinfo( hostname
, "0", & hints
, & info
) || ! info
)
1262 address
->m_error
= GSOCK_NOHOST
;
1263 return GSOCK_NOHOST
;
1266 memcpy( address
->m_addr
, info
->ai_addr
, info
->ai_addrlen
);
1267 freeaddrinfo( info
);
1268 return GSOCK_NOERROR
;
1271 GSocketError
GAddress_INET6_SetAnyAddress(GAddress
*address
)
1273 CHECK_ADDRESS(address
, INET6
);
1275 struct in6_addr addr
;
1276 memset( & addr
, 0, sizeof( addr
) );
1277 return GAddress_INET6_SetHostAddress(address
, addr
);
1279 GSocketError
GAddress_INET6_SetHostAddress(GAddress
*address
,
1280 struct in6_addr hostaddr
)
1282 CHECK_ADDRESS(address
, INET6
);
1284 ((struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
= hostaddr
;
1286 return GSOCK_NOERROR
;
1289 GSocketError
GAddress_INET6_SetPortName(GAddress
*address
, const char *port
,
1290 const char *protocol
)
1293 struct sockaddr_in6
*addr
;
1295 CHECK_ADDRESS(address
, INET6
);
1299 address
->m_error
= GSOCK_INVPORT
;
1300 return GSOCK_INVPORT
;
1303 se
= getservbyname(port
, protocol
);
1306 if (isdigit((unsigned char) port
[0]))
1310 port_int
= atoi(port
);
1311 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1312 addr
->sin6_port
= htons((u_short
) port_int
);
1313 return GSOCK_NOERROR
;
1316 address
->m_error
= GSOCK_INVPORT
;
1317 return GSOCK_INVPORT
;
1320 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1321 addr
->sin6_port
= se
->s_port
;
1323 return GSOCK_NOERROR
;
1326 GSocketError
GAddress_INET6_SetPort(GAddress
*address
, unsigned short port
)
1328 struct sockaddr_in6
*addr
;
1330 CHECK_ADDRESS(address
, INET6
);
1332 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1333 addr
->sin6_port
= htons(port
);
1335 return GSOCK_NOERROR
;
1338 GSocketError
GAddress_INET6_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1342 struct sockaddr_in6
*addr
;
1344 CHECK_ADDRESS(address
, INET6
);
1346 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1347 addr_buf
= (char *)&(addr
->sin6_addr
);
1349 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin6_addr
), AF_INET6
);
1352 address
->m_error
= GSOCK_NOHOST
;
1353 return GSOCK_NOHOST
;
1356 strncpy(hostname
, he
->h_name
, sbuf
);
1358 return GSOCK_NOERROR
;
1361 GSocketError
GAddress_INET6_GetHostAddress(GAddress
*address
,struct in6_addr
*hostaddr
)
1363 CHECK_ADDRESS_RETVAL(address
, INET6
, GSOCK_INVADDR
);
1364 *hostaddr
= ( (struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
;
1365 return GSOCK_NOERROR
;
1368 unsigned short GAddress_INET6_GetPort(GAddress
*address
)
1370 CHECK_ADDRESS_RETVAL(address
, INET6
, 0);
1372 return ntohs( ((struct sockaddr_in6
*)address
->m_addr
)->sin6_port
);
1375 #endif // wxUSE_IPV6
1378 * -------------------------------------------------------------------------
1379 * Unix address family
1380 * -------------------------------------------------------------------------
1383 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1385 address
->m_error
= GSOCK_INVADDR
;
1386 return GSOCK_INVADDR
;
1389 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *WXUNUSED(path
))
1391 address
->m_error
= GSOCK_INVADDR
;
1392 return GSOCK_INVADDR
;
1395 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *WXUNUSED(path
), size_t WXUNUSED(sbuf
))
1397 address
->m_error
= GSOCK_INVADDR
;
1398 return GSOCK_INVADDR
;
1401 #endif // wxUSE_SOCKETS