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()
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();
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
;
724 /* GSocket_SetTimeout:
725 * Sets the timeout for blocking calls. Time is expressed in
728 void GSocket::SetTimeout(unsigned long millis
)
730 m_timeout
.tv_sec
= (millis
/ 1000);
731 m_timeout
.tv_usec
= (millis
% 1000) * 1000;
735 * Returns the last error occurred for this socket. Note that successful
736 * operations do not clear this back to GSOCK_NOERROR, so use it only
739 GSocketError WXDLLIMPEXP_NET
GSocket::GetError()
747 * There is data to be read in the input buffer. If, after a read
748 * operation, there is still data available, the callback function will
751 * The socket is available for writing. That is, the next write call
752 * won't block. This event is generated only once, when the connection is
753 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
754 * when the output buffer empties again. This means that the app should
755 * assume that it can write since the first OUTPUT event, and no more
756 * OUTPUT events will be generated unless an error occurs.
758 * Connection successfully established, for client sockets, or incoming
759 * client connection, for server sockets. Wait for this event (also watch
760 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
762 * The connection is lost (or a connection request failed); this could
763 * be due to a failure, or due to the peer closing it gracefully.
766 /* GSocket_SetCallback:
767 * Enables the callbacks specified by 'flags'. Note that 'flags'
768 * may be a combination of flags OR'ed toghether, so the same
769 * callback function can be made to accept different events.
770 * The callback function must have the following prototype:
772 * void function(GSocket *socket, GSocketEvent event, char *cdata)
774 void GSocket::SetCallback(GSocketEventFlags flags
,
775 GSocketCallback callback
, char *cdata
)
779 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
781 if ((flags
& (1 << count
)) != 0)
783 m_cbacks
[count
] = callback
;
784 m_data
[count
] = cdata
;
789 /* GSocket_UnsetCallback:
790 * Disables all callbacks specified by 'flags', which may be a
791 * combination of flags OR'ed toghether.
793 void GSocket::UnsetCallback(GSocketEventFlags flags
)
797 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
799 if ((flags
& (1 << count
)) != 0)
801 m_cbacks
[count
] = NULL
;
802 m_data
[count
] = NULL
;
807 GSocketError
GSocket::GetSockOpt(int level
, int optname
,
808 void *optval
, int *optlen
)
810 if (getsockopt(m_fd
, level
, optname
, (char*)optval
, optlen
) == 0)
812 return GSOCK_NOERROR
;
817 GSocketError
GSocket::SetSockOpt(int level
, int optname
,
818 const void *optval
, int optlen
)
820 if (setsockopt(m_fd
, level
, optname
, (char*)optval
, optlen
) == 0)
822 return GSOCK_NOERROR
;
829 /* _GSocket_Input_Timeout:
830 * For blocking sockets, wait until data is available or
831 * until timeout ellapses.
833 GSocketError
GSocket::Input_Timeout()
840 FD_SET(m_fd
, &readfds
);
841 if (select(0, &readfds
, NULL
, NULL
, &m_timeout
) == 0)
843 m_error
= GSOCK_TIMEDOUT
;
844 return GSOCK_TIMEDOUT
;
847 return GSOCK_NOERROR
;
850 /* _GSocket_Output_Timeout:
851 * For blocking sockets, wait until data can be sent without
852 * blocking or until timeout ellapses.
854 GSocketError
GSocket::Output_Timeout()
861 FD_SET(m_fd
, &writefds
);
862 if (select(0, NULL
, &writefds
, NULL
, &m_timeout
) == 0)
864 m_error
= GSOCK_TIMEDOUT
;
865 return GSOCK_TIMEDOUT
;
868 return GSOCK_NOERROR
;
871 /* _GSocket_Connect_Timeout:
872 * For blocking sockets, wait until the connection is
873 * established or fails, or until timeout ellapses.
875 GSocketError
GSocket::Connect_Timeout()
882 FD_SET(m_fd
, &writefds
);
883 FD_SET(m_fd
, &exceptfds
);
884 if (select(0, NULL
, &writefds
, &exceptfds
, &m_timeout
) == 0)
886 m_error
= GSOCK_TIMEDOUT
;
887 return GSOCK_TIMEDOUT
;
889 if (!FD_ISSET(m_fd
, &writefds
))
891 m_error
= GSOCK_IOERR
;
895 return GSOCK_NOERROR
;
898 int GSocket::Recv_Stream(char *buffer
, int size
)
900 return recv(m_fd
, buffer
, size
, 0);
903 int GSocket::Recv_Dgram(char *buffer
, int size
)
906 WX_SOCKLEN_T fromlen
= sizeof(from
);
910 ret
= recvfrom(m_fd
, buffer
, size
, 0, (sockaddr
*)&from
, &fromlen
);
912 if (ret
== SOCKET_ERROR
)
915 /* Translate a system address into a GSocket address */
918 m_peer
= GAddress_new();
921 m_error
= GSOCK_MEMERR
;
925 err
= _GAddress_translate_from(m_peer
, (sockaddr
*)&from
, fromlen
);
926 if (err
!= GSOCK_NOERROR
)
928 GAddress_destroy(m_peer
);
937 int GSocket::Send_Stream(const char *buffer
, int size
)
939 return send(m_fd
, buffer
, size
, 0);
942 int GSocket::Send_Dgram(const char *buffer
, int size
)
944 struct sockaddr
*addr
;
950 m_error
= GSOCK_INVADDR
;
954 err
= _GAddress_translate_to(m_peer
, &addr
, &len
);
955 if (err
!= GSOCK_NOERROR
)
961 ret
= sendto(m_fd
, buffer
, size
, 0, addr
, len
);
963 /* Frees memory allocated by _GAddress_translate_to */
970 * -------------------------------------------------------------------------
972 * -------------------------------------------------------------------------
975 /* CHECK_ADDRESS verifies that the current address family is either
976 * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it
977 * initalizes it to be a GSOCK_*family*. In other cases, it returns
978 * an appropiate error code.
980 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
982 #define CHECK_ADDRESS(address, family) \
984 if (address->m_family == GSOCK_NOFAMILY) \
985 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
986 return address->m_error; \
987 if (address->m_family != GSOCK_##family) \
989 address->m_error = GSOCK_INVADDR; \
990 return GSOCK_INVADDR; \
994 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
996 if (address->m_family == GSOCK_NOFAMILY) \
997 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
999 if (address->m_family != GSOCK_##family) \
1001 address->m_error = GSOCK_INVADDR; \
1007 GAddress
*GAddress_new()
1011 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1014 address
->m_family
= GSOCK_NOFAMILY
;
1015 address
->m_addr
= NULL
;
1021 GAddress
*GAddress_copy(GAddress
*address
)
1025 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1028 memcpy(addr2
, address
, sizeof(GAddress
));
1030 if (address
->m_addr
)
1032 addr2
->m_addr
= (struct sockaddr
*) malloc(addr2
->m_len
);
1033 if (addr2
->m_addr
== NULL
)
1038 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1044 void GAddress_destroy(GAddress
*address
)
1046 if (address
->m_addr
)
1047 free(address
->m_addr
);
1052 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1054 address
->m_family
= type
;
1057 GAddressType
GAddress_GetFamily(GAddress
*address
)
1059 return address
->m_family
;
1062 GSocketError
_GAddress_translate_from(GAddress
*address
,
1063 struct sockaddr
*addr
, int len
)
1065 address
->m_realfamily
= addr
->sa_family
;
1066 switch (addr
->sa_family
)
1069 address
->m_family
= GSOCK_INET
;
1072 address
->m_family
= GSOCK_UNIX
;
1076 address
->m_family
= GSOCK_INET6
;
1081 address
->m_error
= GSOCK_INVOP
;
1086 if (address
->m_addr
)
1087 free(address
->m_addr
);
1089 address
->m_len
= len
;
1090 address
->m_addr
= (struct sockaddr
*) malloc(len
);
1092 if (address
->m_addr
== NULL
)
1094 address
->m_error
= GSOCK_MEMERR
;
1095 return GSOCK_MEMERR
;
1097 memcpy(address
->m_addr
, addr
, len
);
1099 return GSOCK_NOERROR
;
1102 GSocketError
_GAddress_translate_to(GAddress
*address
,
1103 struct sockaddr
**addr
, int *len
)
1105 if (!address
->m_addr
)
1107 address
->m_error
= GSOCK_INVADDR
;
1108 return GSOCK_INVADDR
;
1111 *len
= address
->m_len
;
1112 *addr
= (struct sockaddr
*) malloc(address
->m_len
);
1115 address
->m_error
= GSOCK_MEMERR
;
1116 return GSOCK_MEMERR
;
1119 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1120 return GSOCK_NOERROR
;
1124 * -------------------------------------------------------------------------
1125 * Internet address family
1126 * -------------------------------------------------------------------------
1129 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1131 address
->m_len
= sizeof(struct sockaddr_in
);
1132 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1133 if (address
->m_addr
== NULL
)
1135 address
->m_error
= GSOCK_MEMERR
;
1136 return GSOCK_MEMERR
;
1139 address
->m_family
= GSOCK_INET
;
1140 address
->m_realfamily
= AF_INET
;
1141 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1142 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1144 return GSOCK_NOERROR
;
1147 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1150 struct in_addr
*addr
;
1152 CHECK_ADDRESS(address
, INET
);
1154 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1156 addr
->s_addr
= inet_addr(hostname
);
1158 /* If it is a numeric host name, convert it now */
1159 if (addr
->s_addr
== INADDR_NONE
)
1161 struct in_addr
*array_addr
;
1163 /* It is a real name, we solve it */
1164 if ((he
= gethostbyname(hostname
)) == NULL
)
1166 /* addr->s_addr = INADDR_NONE just done by inet_addr() above */
1167 address
->m_error
= GSOCK_NOHOST
;
1168 return GSOCK_NOHOST
;
1170 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1171 addr
->s_addr
= array_addr
[0].s_addr
;
1173 return GSOCK_NOERROR
;
1176 GSocketError
GAddress_INET_SetBroadcastAddress(GAddress
*address
)
1178 return GAddress_INET_SetHostAddress(address
, INADDR_BROADCAST
);
1181 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1183 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1186 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1187 unsigned long hostaddr
)
1189 struct in_addr
*addr
;
1191 CHECK_ADDRESS(address
, INET
);
1193 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1194 addr
->s_addr
= htonl(hostaddr
);
1196 return GSOCK_NOERROR
;
1199 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1200 const char *protocol
)
1203 struct sockaddr_in
*addr
;
1205 CHECK_ADDRESS(address
, INET
);
1209 address
->m_error
= GSOCK_INVPORT
;
1210 return GSOCK_INVPORT
;
1213 se
= getservbyname(port
, protocol
);
1216 if (isdigit(port
[0]))
1220 port_int
= atoi(port
);
1221 addr
= (struct sockaddr_in
*)address
->m_addr
;
1222 addr
->sin_port
= htons((u_short
) port_int
);
1223 return GSOCK_NOERROR
;
1226 address
->m_error
= GSOCK_INVPORT
;
1227 return GSOCK_INVPORT
;
1230 addr
= (struct sockaddr_in
*)address
->m_addr
;
1231 addr
->sin_port
= se
->s_port
;
1233 return GSOCK_NOERROR
;
1236 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1238 struct sockaddr_in
*addr
;
1240 CHECK_ADDRESS(address
, INET
);
1242 addr
= (struct sockaddr_in
*)address
->m_addr
;
1243 addr
->sin_port
= htons(port
);
1245 return GSOCK_NOERROR
;
1248 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1252 struct sockaddr_in
*addr
;
1254 CHECK_ADDRESS(address
, INET
);
1256 addr
= (struct sockaddr_in
*)address
->m_addr
;
1257 addr_buf
= (char *)&(addr
->sin_addr
);
1259 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1262 address
->m_error
= GSOCK_NOHOST
;
1263 return GSOCK_NOHOST
;
1266 strncpy(hostname
, he
->h_name
, sbuf
);
1268 return GSOCK_NOERROR
;
1271 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1273 struct sockaddr_in
*addr
;
1275 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1277 addr
= (struct sockaddr_in
*)address
->m_addr
;
1279 return ntohl(addr
->sin_addr
.s_addr
);
1282 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1284 struct sockaddr_in
*addr
;
1286 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1288 addr
= (struct sockaddr_in
*)address
->m_addr
;
1289 return ntohs(addr
->sin_port
);
1295 * -------------------------------------------------------------------------
1296 * Internet IPv6 address family
1297 * -------------------------------------------------------------------------
1299 #include "ws2tcpip.h"
1302 #pragma comment(lib,"ws2_32")
1303 #endif // __VISUALC__
1305 GSocketError
_GAddress_Init_INET6(GAddress
*address
)
1307 struct in6_addr any_address
= IN6ADDR_ANY_INIT
;
1308 address
->m_len
= sizeof(struct sockaddr_in6
);
1309 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1310 if (address
->m_addr
== NULL
)
1312 address
->m_error
= GSOCK_MEMERR
;
1313 return GSOCK_MEMERR
;
1315 memset(address
->m_addr
,0,address
->m_len
);
1317 address
->m_family
= GSOCK_INET6
;
1318 address
->m_realfamily
= AF_INET6
;
1319 ((struct sockaddr_in6
*)address
->m_addr
)->sin6_family
= AF_INET6
;
1320 ((struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
= any_address
;
1322 return GSOCK_NOERROR
;
1325 GSocketError
GAddress_INET6_SetHostName(GAddress
*address
, const char *hostname
)
1327 CHECK_ADDRESS(address
, INET6
);
1330 memset( & hints
, 0, sizeof( hints
) );
1331 hints
.ai_family
= AF_INET6
;
1332 addrinfo
* info
= 0;
1333 if ( getaddrinfo( hostname
, "0", & hints
, & info
) || ! info
)
1335 address
->m_error
= GSOCK_NOHOST
;
1336 return GSOCK_NOHOST
;
1339 memcpy( address
->m_addr
, info
->ai_addr
, info
->ai_addrlen
);
1340 freeaddrinfo( info
);
1341 return GSOCK_NOERROR
;
1344 GSocketError
GAddress_INET6_SetAnyAddress(GAddress
*address
)
1346 CHECK_ADDRESS(address
, INET6
);
1348 struct in6_addr addr
;
1349 memset( & addr
, 0, sizeof( addr
) );
1350 return GAddress_INET6_SetHostAddress(address
, addr
);
1352 GSocketError
GAddress_INET6_SetHostAddress(GAddress
*address
,
1353 struct in6_addr hostaddr
)
1355 CHECK_ADDRESS(address
, INET6
);
1357 ((struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
= hostaddr
;
1359 return GSOCK_NOERROR
;
1362 GSocketError
GAddress_INET6_SetPortName(GAddress
*address
, const char *port
,
1363 const char *protocol
)
1366 struct sockaddr_in6
*addr
;
1368 CHECK_ADDRESS(address
, INET6
);
1372 address
->m_error
= GSOCK_INVPORT
;
1373 return GSOCK_INVPORT
;
1376 se
= getservbyname(port
, protocol
);
1379 if (isdigit((unsigned char) port
[0]))
1383 port_int
= atoi(port
);
1384 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1385 addr
->sin6_port
= htons((u_short
) port_int
);
1386 return GSOCK_NOERROR
;
1389 address
->m_error
= GSOCK_INVPORT
;
1390 return GSOCK_INVPORT
;
1393 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1394 addr
->sin6_port
= se
->s_port
;
1396 return GSOCK_NOERROR
;
1399 GSocketError
GAddress_INET6_SetPort(GAddress
*address
, unsigned short port
)
1401 struct sockaddr_in6
*addr
;
1403 CHECK_ADDRESS(address
, INET6
);
1405 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1406 addr
->sin6_port
= htons(port
);
1408 return GSOCK_NOERROR
;
1411 GSocketError
GAddress_INET6_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1415 struct sockaddr_in6
*addr
;
1417 CHECK_ADDRESS(address
, INET6
);
1419 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1420 addr_buf
= (char *)&(addr
->sin6_addr
);
1422 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin6_addr
), AF_INET6
);
1425 address
->m_error
= GSOCK_NOHOST
;
1426 return GSOCK_NOHOST
;
1429 strncpy(hostname
, he
->h_name
, sbuf
);
1431 return GSOCK_NOERROR
;
1434 GSocketError
GAddress_INET6_GetHostAddress(GAddress
*address
,struct in6_addr
*hostaddr
)
1436 CHECK_ADDRESS_RETVAL(address
, INET6
, GSOCK_INVADDR
);
1437 *hostaddr
= ( (struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
;
1438 return GSOCK_NOERROR
;
1441 unsigned short GAddress_INET6_GetPort(GAddress
*address
)
1443 CHECK_ADDRESS_RETVAL(address
, INET6
, 0);
1445 return ntohs( ((struct sockaddr_in6
*)address
->m_addr
)->sin6_port
);
1448 #endif // wxUSE_IPV6
1451 * -------------------------------------------------------------------------
1452 * Unix address family
1453 * -------------------------------------------------------------------------
1456 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1458 address
->m_error
= GSOCK_INVADDR
;
1459 return GSOCK_INVADDR
;
1462 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *WXUNUSED(path
))
1464 address
->m_error
= GSOCK_INVADDR
;
1465 return GSOCK_INVADDR
;
1468 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *WXUNUSED(path
), size_t WXUNUSED(sbuf
))
1470 address
->m_error
= GSOCK_INVADDR
;
1471 return GSOCK_INVADDR
;
1474 #endif // wxUSE_SOCKETS