1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/sockmsw.cpp
3 // Purpose: MSW-specific socket code
4 // Authors: Guilhem Lavaux, Guillermo Rodriguez Garcia
6 // Copyright: (C) 1999-1997, Guilhem Lavaux
7 // (C) 1999-2000, Guillermo Rodriguez Garcia
8 // (C) 2008 Vadim Zeitlin
10 // License: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
23 /* including rasasync.h (included from windows.h itself included from
24 * wx/setup.h and/or winsock.h results in this warning for
25 * RPCNOTIFICATION_ROUTINE
28 # pragma warning(disable:4115) /* named type definition in parentheses */
31 #include "wx/private/socket.h"
32 #include "wx/msw/private.h" // for wxGetInstance()
33 #include "wx/apptrait.h"
34 #include "wx/thread.h"
35 #include "wx/dynlib.h"
39 * As WSAAsyncSelect is not present on WinCE, it now uses WSACreateEvent,
40 * WSAEventSelect, WSAWaitForMultipleEvents and WSAEnumNetworkEvents. When
41 * enabling eventhandling for a socket a new thread it created that keeps track
42 * of the events and posts a messageto the hidden window to use the standard
45 #include "wx/msw/wince/net.h"
46 #include "wx/hashmap.h"
47 WX_DECLARE_HASH_MAP(int,bool,wxIntegerHash
,wxIntegerEqual
,SocketHash
);
50 #define isdigit(x) (x > 47 && x < 58)
52 #include "wx/msw/wince/net.h"
57 # pragma warning(default:4115) /* named type definition in parentheses */
60 #define CLASSNAME TEXT("_wxSocket_Internal_Window_Class")
62 /* implemented in utils.cpp */
63 extern "C" WXDLLIMPEXP_BASE HWND
64 wxCreateHiddenWindow(LPCTSTR
*pclassname
, LPCTSTR classname
, WNDPROC wndproc
);
66 /* Maximum number of different wxSocket objects at a given time.
67 * This value can be modified at will, but it CANNOT be greater
68 * than (0x7FFF - WM_USER + 1)
70 #define MAXSOCKETS 1024
72 #if (MAXSOCKETS > (0x7FFF - WM_USER + 1))
73 #error "MAXSOCKETS is too big!"
77 typedef int (PASCAL
*WSAAsyncSelect_t
)(SOCKET
,HWND
,u_int
,long);
79 /* Typedef the needed function prototypes and the WSANETWORKEVENTS structure
81 typedef struct _WSANETWORKEVENTS
{
84 } WSANETWORKEVENTS
, FAR
* LPWSANETWORKEVENTS
;
85 typedef HANDLE (PASCAL
*WSACreateEvent_t
)();
86 typedef int (PASCAL
*WSAEventSelect_t
)(SOCKET
,HANDLE
,long);
87 typedef int (PASCAL
*WSAWaitForMultipleEvents_t
)(long,HANDLE
,BOOL
,long,BOOL
);
88 typedef int (PASCAL
*WSAEnumNetworkEvents_t
)(SOCKET
,HANDLE
,LPWSANETWORKEVENTS
);
91 LRESULT CALLBACK
wxSocket_Internal_WinProc(HWND
, UINT
, WPARAM
, LPARAM
);
93 /* Global variables */
96 wxCRIT_SECT_DECLARE_MEMBER(gs_critical
);
97 static wxSocketImplMSW
*socketList
[MAXSOCKETS
];
98 static int firstAvailable
;
101 static WSAAsyncSelect_t gs_WSAAsyncSelect
= NULL
;
103 static SocketHash socketHash
;
104 static unsigned int currSocket
;
105 HANDLE hThread
[MAXSOCKETS
];
106 static WSACreateEvent_t gs_WSACreateEvent
= NULL
;
107 static WSAEventSelect_t gs_WSAEventSelect
= NULL
;
108 static WSAWaitForMultipleEvents_t gs_WSAWaitForMultipleEvents
= NULL
;
109 static WSAEnumNetworkEvents_t gs_WSAEnumNetworkEvents
= NULL
;
110 /* This structure will be used to pass data on to the thread that handles socket events.
112 typedef struct thread_data
{
114 unsigned long msgnumber
;
116 unsigned long lEvent
;
121 /* This thread handles socket events on WinCE using WSAEventSelect() as
122 * WSAAsyncSelect is not supported. When an event occurs for the socket, it is
123 * checked what kind of event happend and the correct message gets posted so
124 * that the hidden window can handle it as it would in other MSW builds.
126 DWORD WINAPI
SocketThread(LPVOID data
)
128 WSANETWORKEVENTS NetworkEvents
;
129 thread_data
* d
= (thread_data
*)data
;
131 HANDLE NetworkEvent
= gs_WSACreateEvent();
132 gs_WSAEventSelect(d
->fd
, NetworkEvent
, d
->lEvent
);
134 while(socketHash
[d
->fd
] == true)
136 if ((gs_WSAWaitForMultipleEvents(1, &NetworkEvent
, FALSE
,INFINITE
, FALSE
)) == WAIT_FAILED
)
138 printf("WSAWaitForMultipleEvents failed with error %d\n", WSAGetLastError());
141 if (gs_WSAEnumNetworkEvents(d
->fd
,NetworkEvent
, &NetworkEvents
) == SOCKET_ERROR
)
143 printf("WSAEnumNetworkEvents failed with error %d\n", WSAGetLastError());
147 long flags
= NetworkEvents
.lNetworkEvents
;
149 ::PostMessage(d
->hEvtWin
, d
->msgnumber
,d
->fd
, FD_READ
);
150 if (flags
& FD_WRITE
)
151 ::PostMessage(d
->hEvtWin
, d
->msgnumber
,d
->fd
, FD_WRITE
);
153 ::PostMessage(d
->hEvtWin
, d
->msgnumber
,d
->fd
, FD_OOB
);
154 if (flags
& FD_ACCEPT
)
155 ::PostMessage(d
->hEvtWin
, d
->msgnumber
,d
->fd
, FD_ACCEPT
);
156 if (flags
& FD_CONNECT
)
157 ::PostMessage(d
->hEvtWin
, d
->msgnumber
,d
->fd
, FD_CONNECT
);
158 if (flags
& FD_CLOSE
)
159 ::PostMessage(d
->hEvtWin
, d
->msgnumber
,d
->fd
, FD_CLOSE
);
162 gs_WSAEventSelect(d
->fd
, NetworkEvent
, 0);
168 // ----------------------------------------------------------------------------
169 // MSW implementation of wxSocketManager
170 // ----------------------------------------------------------------------------
172 class wxSocketMSWManager
: public wxSocketManager
175 virtual bool OnInit();
176 virtual void OnExit();
178 virtual wxSocketImpl
*CreateSocket(wxSocketBase
& wxsocket
)
180 return new wxSocketImplMSW(wxsocket
);
182 virtual void Install_Callback(wxSocketImpl
*socket
, wxSocketNotify event
);
183 virtual void Uninstall_Callback(wxSocketImpl
*socket
, wxSocketNotify event
);
186 static wxDynamicLibrary gs_wsock32dll
;
189 wxDynamicLibrary
wxSocketMSWManager::gs_wsock32dll
;
191 bool wxSocketMSWManager::OnInit()
193 static LPCTSTR pclassname
= NULL
;
196 /* Create internal window for event notifications */
197 hWin
= wxCreateHiddenWindow(&pclassname
, CLASSNAME
, wxSocket_Internal_WinProc
);
201 /* Initialize socket list */
202 for (i
= 0; i
< MAXSOCKETS
; i
++)
204 socketList
[i
] = NULL
;
208 // we don't link with wsock32.dll (or ws2 in CE case) statically to avoid
209 // dependencies on it for all the application using wx even if they don't use
212 #define WINSOCK_DLL_NAME _T("ws2.dll")
214 #define WINSOCK_DLL_NAME _T("wsock32.dll")
217 gs_wsock32dll
.Load(WINSOCK_DLL_NAME
, wxDL_VERBATIM
| wxDL_QUIET
);
218 if ( !gs_wsock32dll
.IsLoaded() )
222 wxDL_INIT_FUNC(gs_
, WSAAsyncSelect
, gs_wsock32dll
);
223 if ( !gs_WSAAsyncSelect
)
226 wxDL_INIT_FUNC(gs_
, WSAEventSelect
, gs_wsock32dll
);
227 if ( !gs_WSAEventSelect
)
230 wxDL_INIT_FUNC(gs_
, WSACreateEvent
, gs_wsock32dll
);
231 if ( !gs_WSACreateEvent
)
234 wxDL_INIT_FUNC(gs_
, WSAWaitForMultipleEvents
, gs_wsock32dll
);
235 if ( !gs_WSAWaitForMultipleEvents
)
238 wxDL_INIT_FUNC(gs_
, WSAEnumNetworkEvents
, gs_wsock32dll
);
239 if ( !gs_WSAEnumNetworkEvents
)
243 #endif // !__WXWINCE__/__WXWINCE__
245 // finally initialize WinSock
247 return WSAStartup((1 << 8) | 1, &wsaData
) == 0;
250 void wxSocketMSWManager::OnExit()
253 /* Delete the threads here */
254 for(unsigned int i
=0; i
< currSocket
; i
++)
255 CloseHandle(hThread
[i
]);
257 /* Destroy internal window */
259 UnregisterClass(CLASSNAME
, wxGetInstance());
263 gs_wsock32dll
.Unload();
266 /* Per-socket GUI initialization / cleanup */
268 wxSocketImplMSW::wxSocketImplMSW(wxSocketBase
& wxsocket
)
269 : wxSocketImpl(wxsocket
)
271 /* Allocate a new message number for this socket */
272 wxCRIT_SECT_LOCKER(lock
, gs_critical
);
274 int i
= firstAvailable
;
275 while (socketList
[i
] != NULL
)
277 i
= (i
+ 1) % MAXSOCKETS
;
279 if (i
== firstAvailable
) /* abort! */
281 m_msgnumber
= 0; // invalid
285 socketList
[i
] = this;
286 firstAvailable
= (i
+ 1) % MAXSOCKETS
;
287 m_msgnumber
= (i
+ WM_USER
);
290 wxSocketImplMSW::~wxSocketImplMSW()
292 /* Remove the socket from the list */
293 wxCRIT_SECT_LOCKER(lock
, gs_critical
);
297 // we need to remove any pending messages for this socket to avoid having
298 // them sent to a new socket which could reuse the same message number as
299 // soon as we destroy this one
301 while ( ::PeekMessage(&msg
, hWin
, m_msgnumber
, m_msgnumber
, PM_REMOVE
) )
304 socketList
[m_msgnumber
- WM_USER
] = NULL
;
306 //else: the socket has never been created successfully
309 /* Windows proc for asynchronous event handling */
311 LRESULT CALLBACK
wxSocket_Internal_WinProc(HWND hWnd
,
316 if ( uMsg
< WM_USER
|| uMsg
> (WM_USER
+ MAXSOCKETS
- 1))
317 return DefWindowProc(hWnd
, uMsg
, wParam
, lParam
);
319 wxSocketImplMSW
*socket
;
320 wxSocketNotify event
= (wxSocketNotify
)-1;
322 wxCRIT_SECT_LOCKER(lock
, gs_critical
);
324 socket
= socketList
[(uMsg
- WM_USER
)];
328 wxASSERT_MSG( socket
->m_fd
== (SOCKET
)wParam
,
329 "mismatch between message and socket?" );
331 switch WSAGETSELECTEVENT(lParam
)
334 event
= wxSOCKET_INPUT
;
338 event
= wxSOCKET_OUTPUT
;
342 event
= wxSOCKET_CONNECTION
;
346 event
= WSAGETSELECTERROR(lParam
) ? wxSOCKET_LOST
347 : wxSOCKET_CONNECTION
;
351 event
= wxSOCKET_LOST
;
355 wxFAIL_MSG( "unexpected socket notification" );
358 } // unlock gs_critical
360 socket
->NotifyOnStateChange(event
);
366 * Enable all event notifications; we need to be notified of all
367 * events for internal processing, but we will only notify users
368 * when an appropriate callback function has been installed.
370 void wxSocketMSWManager::Install_Callback(wxSocketImpl
*socket_
,
371 wxSocketNotify
WXUNUSED(event
))
373 wxSocketImplMSW
* const socket
= static_cast<wxSocketImplMSW
*>(socket_
);
375 if (socket
->m_fd
!= INVALID_SOCKET
)
377 /* We could probably just subscribe to all events regardless
378 * of the socket type, but MS recommends to do it this way.
380 long lEvent
= socket
->m_server
?
381 FD_ACCEPT
: (FD_READ
| FD_WRITE
| FD_CONNECT
| FD_CLOSE
);
383 gs_WSAAsyncSelect(socket
->m_fd
, hWin
, socket
->m_msgnumber
, lEvent
);
386 * WinCE creates a thread for socket event handling.
387 * All needed parameters get passed through the thread_data structure.
390 thread_data
* d
= new thread_data
;
393 d
->msgnumber
= socket
->m_msgnumber
;
394 d
->fd
= socket
->m_fd
;
395 socketHash
[socket
->m_fd
] = true;
396 hThread
[currSocket
++] = CreateThread(NULL
, 0, &SocketThread
,(LPVOID
)d
, 0, NULL
);
402 * Disable event notifications (used when shutting down the socket)
404 void wxSocketMSWManager::Uninstall_Callback(wxSocketImpl
*socket_
,
405 wxSocketNotify
WXUNUSED(event
))
407 wxSocketImplMSW
* const socket
= static_cast<wxSocketImplMSW
*>(socket_
);
409 if (socket
->m_fd
!= INVALID_SOCKET
)
412 gs_WSAAsyncSelect(socket
->m_fd
, hWin
, socket
->m_msgnumber
, 0);
415 socketHash
[socket
->m_fd
] = false;
420 // set the wxBase variable to point to our wxSocketManager implementation
422 // see comments in wx/apptrait.h for the explanation of why do we do it
424 static struct ManagerSetter
428 static wxSocketMSWManager s_manager
;
429 wxAppTraits::SetDefaultSocketManager(&s_manager
);
433 // ============================================================================
434 // wxSocketImpl implementation
435 // ============================================================================
438 wxSocketImpl
*wxSocketImpl::Create(wxSocketBase
& wxsocket
)
440 return new wxSocketImplMSW(wxsocket
);
443 void wxSocketImplMSW::DoClose()
445 wxSocketManager::Get()->
446 Uninstall_Callback(this, wxSOCKET_MAX_EVENT
/* unused anyhow */);
451 wxSocketError
wxSocketImplMSW::GetLastError() const
453 switch ( WSAGetLastError() )
456 return wxSOCKET_NOERROR
;
459 return wxSOCKET_INVSOCK
;
462 return wxSOCKET_WOULDBLOCK
;
465 return wxSOCKET_IOERR
;
470 * -------------------------------------------------------------------------
472 * -------------------------------------------------------------------------
475 /* CHECK_ADDRESS verifies that the current address family is either
476 * wxSOCKET_NOFAMILY or wxSOCKET_*family*, and if it is wxSOCKET_NOFAMILY, it
477 * initalizes it to be a wxSOCKET_*family*. In other cases, it returns
478 * an appropiate error code.
480 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
482 #define CHECK_ADDRESS(address, family) \
484 if (address->m_family == wxSOCKET_NOFAMILY) \
485 if (_GAddress_Init_##family(address) != wxSOCKET_NOERROR) \
486 return address->m_error; \
487 if (address->m_family != wxSOCKET_##family) \
489 address->m_error = wxSOCKET_INVADDR; \
490 return wxSOCKET_INVADDR; \
494 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
496 if (address->m_family == wxSOCKET_NOFAMILY) \
497 if (_GAddress_Init_##family(address) != wxSOCKET_NOERROR) \
499 if (address->m_family != wxSOCKET_##family) \
501 address->m_error = wxSOCKET_INVADDR; \
507 GAddress
*GAddress_new()
511 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
514 address
->m_family
= wxSOCKET_NOFAMILY
;
515 address
->m_addr
= NULL
;
521 GAddress
*GAddress_copy(GAddress
*address
)
525 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
528 memcpy(addr2
, address
, sizeof(GAddress
));
532 addr2
->m_addr
= (struct sockaddr
*) malloc(addr2
->m_len
);
533 if (addr2
->m_addr
== NULL
)
538 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
544 void GAddress_destroy(GAddress
*address
)
547 free(address
->m_addr
);
552 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
554 address
->m_family
= type
;
557 GAddressType
GAddress_GetFamily(GAddress
*address
)
559 return address
->m_family
;
562 wxSocketError
_GAddress_translate_from(GAddress
*address
,
563 struct sockaddr
*addr
, int len
)
565 address
->m_realfamily
= addr
->sa_family
;
566 switch (addr
->sa_family
)
569 address
->m_family
= wxSOCKET_INET
;
572 address
->m_family
= wxSOCKET_UNIX
;
576 address
->m_family
= wxSOCKET_INET6
;
581 address
->m_error
= wxSOCKET_INVOP
;
582 return wxSOCKET_INVOP
;
587 free(address
->m_addr
);
589 address
->m_len
= len
;
590 address
->m_addr
= (struct sockaddr
*) malloc(len
);
592 if (address
->m_addr
== NULL
)
594 address
->m_error
= wxSOCKET_MEMERR
;
595 return wxSOCKET_MEMERR
;
597 memcpy(address
->m_addr
, addr
, len
);
599 return wxSOCKET_NOERROR
;
602 wxSocketError
_GAddress_translate_to(GAddress
*address
,
603 struct sockaddr
**addr
, int *len
)
605 if (!address
->m_addr
)
607 address
->m_error
= wxSOCKET_INVADDR
;
608 return wxSOCKET_INVADDR
;
611 *len
= address
->m_len
;
612 *addr
= (struct sockaddr
*) malloc(address
->m_len
);
615 address
->m_error
= wxSOCKET_MEMERR
;
616 return wxSOCKET_MEMERR
;
619 memcpy(*addr
, address
->m_addr
, address
->m_len
);
620 return wxSOCKET_NOERROR
;
624 * -------------------------------------------------------------------------
625 * Internet address family
626 * -------------------------------------------------------------------------
629 wxSocketError
_GAddress_Init_INET(GAddress
*address
)
631 address
->m_len
= sizeof(struct sockaddr_in
);
632 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
633 if (address
->m_addr
== NULL
)
635 address
->m_error
= wxSOCKET_MEMERR
;
636 return wxSOCKET_MEMERR
;
639 address
->m_family
= wxSOCKET_INET
;
640 address
->m_realfamily
= AF_INET
;
641 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
642 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
644 return wxSOCKET_NOERROR
;
647 wxSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
650 struct in_addr
*addr
;
652 CHECK_ADDRESS(address
, INET
);
654 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
656 addr
->s_addr
= inet_addr(hostname
);
658 /* If it is a numeric host name, convert it now */
659 if (addr
->s_addr
== INADDR_NONE
)
661 struct in_addr
*array_addr
;
663 /* It is a real name, we solve it */
664 if ((he
= gethostbyname(hostname
)) == NULL
)
666 /* addr->s_addr = INADDR_NONE just done by inet_addr() above */
667 address
->m_error
= wxSOCKET_NOHOST
;
668 return wxSOCKET_NOHOST
;
670 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
671 addr
->s_addr
= array_addr
[0].s_addr
;
673 return wxSOCKET_NOERROR
;
676 wxSocketError
GAddress_INET_SetBroadcastAddress(GAddress
*address
)
678 return GAddress_INET_SetHostAddress(address
, INADDR_BROADCAST
);
681 wxSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
683 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
686 wxSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
687 unsigned long hostaddr
)
689 struct in_addr
*addr
;
691 CHECK_ADDRESS(address
, INET
);
693 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
694 addr
->s_addr
= htonl(hostaddr
);
696 return wxSOCKET_NOERROR
;
699 wxSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
700 const char *protocol
)
703 struct sockaddr_in
*addr
;
705 CHECK_ADDRESS(address
, INET
);
709 address
->m_error
= wxSOCKET_INVPORT
;
710 return wxSOCKET_INVPORT
;
713 se
= getservbyname(port
, protocol
);
716 if (isdigit(port
[0]))
720 port_int
= atoi(port
);
721 addr
= (struct sockaddr_in
*)address
->m_addr
;
722 addr
->sin_port
= htons((u_short
) port_int
);
723 return wxSOCKET_NOERROR
;
726 address
->m_error
= wxSOCKET_INVPORT
;
727 return wxSOCKET_INVPORT
;
730 addr
= (struct sockaddr_in
*)address
->m_addr
;
731 addr
->sin_port
= se
->s_port
;
733 return wxSOCKET_NOERROR
;
736 wxSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
738 struct sockaddr_in
*addr
;
740 CHECK_ADDRESS(address
, INET
);
742 addr
= (struct sockaddr_in
*)address
->m_addr
;
743 addr
->sin_port
= htons(port
);
745 return wxSOCKET_NOERROR
;
748 wxSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
752 struct sockaddr_in
*addr
;
754 CHECK_ADDRESS(address
, INET
);
756 addr
= (struct sockaddr_in
*)address
->m_addr
;
757 addr_buf
= (char *)&(addr
->sin_addr
);
759 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
762 address
->m_error
= wxSOCKET_NOHOST
;
763 return wxSOCKET_NOHOST
;
766 strncpy(hostname
, he
->h_name
, sbuf
);
768 return wxSOCKET_NOERROR
;
771 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
773 struct sockaddr_in
*addr
;
775 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
777 addr
= (struct sockaddr_in
*)address
->m_addr
;
779 return ntohl(addr
->sin_addr
.s_addr
);
782 unsigned short GAddress_INET_GetPort(GAddress
*address
)
784 struct sockaddr_in
*addr
;
786 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
788 addr
= (struct sockaddr_in
*)address
->m_addr
;
789 return ntohs(addr
->sin_port
);
795 * -------------------------------------------------------------------------
796 * Internet IPv6 address family
797 * -------------------------------------------------------------------------
799 #include "ws2tcpip.h"
802 #pragma comment(lib,"ws2_32")
803 #endif // __VISUALC__
805 wxSocketError
_GAddress_Init_INET6(GAddress
*address
)
807 struct in6_addr any_address
= IN6ADDR_ANY_INIT
;
808 address
->m_len
= sizeof(struct sockaddr_in6
);
809 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
810 if (address
->m_addr
== NULL
)
812 address
->m_error
= wxSOCKET_MEMERR
;
813 return wxSOCKET_MEMERR
;
815 memset(address
->m_addr
,0,address
->m_len
);
817 address
->m_family
= wxSOCKET_INET6
;
818 address
->m_realfamily
= AF_INET6
;
819 ((struct sockaddr_in6
*)address
->m_addr
)->sin6_family
= AF_INET6
;
820 ((struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
= any_address
;
822 return wxSOCKET_NOERROR
;
825 wxSocketError
GAddress_INET6_SetHostName(GAddress
*address
, const char *hostname
)
827 CHECK_ADDRESS(address
, INET6
);
830 memset( & hints
, 0, sizeof( hints
) );
831 hints
.ai_family
= AF_INET6
;
833 if ( getaddrinfo( hostname
, "0", & hints
, & info
) || ! info
)
835 address
->m_error
= wxSOCKET_NOHOST
;
836 return wxSOCKET_NOHOST
;
839 memcpy( address
->m_addr
, info
->ai_addr
, info
->ai_addrlen
);
840 freeaddrinfo( info
);
841 return wxSOCKET_NOERROR
;
844 wxSocketError
GAddress_INET6_SetAnyAddress(GAddress
*address
)
846 CHECK_ADDRESS(address
, INET6
);
848 struct in6_addr addr
;
849 memset( & addr
, 0, sizeof( addr
) );
850 return GAddress_INET6_SetHostAddress(address
, addr
);
852 wxSocketError
GAddress_INET6_SetHostAddress(GAddress
*address
,
853 struct in6_addr hostaddr
)
855 CHECK_ADDRESS(address
, INET6
);
857 ((struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
= hostaddr
;
859 return wxSOCKET_NOERROR
;
862 wxSocketError
GAddress_INET6_SetPortName(GAddress
*address
, const char *port
,
863 const char *protocol
)
866 struct sockaddr_in6
*addr
;
868 CHECK_ADDRESS(address
, INET6
);
872 address
->m_error
= wxSOCKET_INVPORT
;
873 return wxSOCKET_INVPORT
;
876 se
= getservbyname(port
, protocol
);
879 if (isdigit((unsigned char) port
[0]))
883 port_int
= atoi(port
);
884 addr
= (struct sockaddr_in6
*)address
->m_addr
;
885 addr
->sin6_port
= htons((u_short
) port_int
);
886 return wxSOCKET_NOERROR
;
889 address
->m_error
= wxSOCKET_INVPORT
;
890 return wxSOCKET_INVPORT
;
893 addr
= (struct sockaddr_in6
*)address
->m_addr
;
894 addr
->sin6_port
= se
->s_port
;
896 return wxSOCKET_NOERROR
;
899 wxSocketError
GAddress_INET6_SetPort(GAddress
*address
, unsigned short port
)
901 struct sockaddr_in6
*addr
;
903 CHECK_ADDRESS(address
, INET6
);
905 addr
= (struct sockaddr_in6
*)address
->m_addr
;
906 addr
->sin6_port
= htons(port
);
908 return wxSOCKET_NOERROR
;
911 wxSocketError
GAddress_INET6_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
915 struct sockaddr_in6
*addr
;
917 CHECK_ADDRESS(address
, INET6
);
919 addr
= (struct sockaddr_in6
*)address
->m_addr
;
920 addr_buf
= (char *)&(addr
->sin6_addr
);
922 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin6_addr
), AF_INET6
);
925 address
->m_error
= wxSOCKET_NOHOST
;
926 return wxSOCKET_NOHOST
;
929 strncpy(hostname
, he
->h_name
, sbuf
);
931 return wxSOCKET_NOERROR
;
934 wxSocketError
GAddress_INET6_GetHostAddress(GAddress
*address
,struct in6_addr
*hostaddr
)
936 CHECK_ADDRESS_RETVAL(address
, INET6
, wxSOCKET_INVADDR
);
937 *hostaddr
= ( (struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
;
938 return wxSOCKET_NOERROR
;
941 unsigned short GAddress_INET6_GetPort(GAddress
*address
)
943 CHECK_ADDRESS_RETVAL(address
, INET6
, 0);
945 return ntohs( ((struct sockaddr_in6
*)address
->m_addr
)->sin6_port
);
951 * -------------------------------------------------------------------------
952 * Unix address family
953 * -------------------------------------------------------------------------
956 wxSocketError
_GAddress_Init_UNIX(GAddress
*address
)
958 address
->m_error
= wxSOCKET_INVADDR
;
959 return wxSOCKET_INVADDR
;
962 wxSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *WXUNUSED(path
))
964 address
->m_error
= wxSOCKET_INVADDR
;
965 return wxSOCKET_INVADDR
;
968 wxSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *WXUNUSED(path
), size_t WXUNUSED(sbuf
))
970 address
->m_error
= wxSOCKET_INVADDR
;
971 return wxSOCKET_INVADDR
;
974 #endif // wxUSE_SOCKETS