remove the badle defined and apparently unnecessary wxSocketImpl::m_detected field
[wxWidgets.git] / src / msw / sockmsw.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/sockmsw.cpp
3 // Purpose: MSW-specific socket code
4 // Authors: Guilhem Lavaux, Guillermo Rodriguez Garcia
5 // Created: April 1997
6 // Copyright: (C) 1999-1997, Guilhem Lavaux
7 // (C) 1999-2000, Guillermo Rodriguez Garcia
8 // (C) 2008 Vadim Zeitlin
9 // RCS_ID: $Id$
10 // License: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
16
17 #ifdef __BORLANDC__
18 #pragma hdrstop
19 #endif
20
21 #if wxUSE_SOCKETS
22
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
26 */
27 #ifdef _MSC_VER
28 # pragma warning(disable:4115) /* named type definition in parentheses */
29 #endif
30
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"
36
37 #ifdef __WXWINCE__
38 /*
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
43 * message loop.
44 */
45 #include "wx/msw/wince/net.h"
46 #include "wx/hashmap.h"
47 WX_DECLARE_HASH_MAP(int,bool,wxIntegerHash,wxIntegerEqual,SocketHash);
48
49 #ifndef isdigit
50 #define isdigit(x) (x > 47 && x < 58)
51 #endif
52 #include "wx/msw/wince/net.h"
53
54 #endif // __WXWINCE__
55
56 #ifdef _MSC_VER
57 # pragma warning(default:4115) /* named type definition in parentheses */
58 #endif
59
60 #define CLASSNAME TEXT("_wxSocket_Internal_Window_Class")
61
62 /* implemented in utils.cpp */
63 extern "C" WXDLLIMPEXP_BASE HWND
64 wxCreateHiddenWindow(LPCTSTR *pclassname, LPCTSTR classname, WNDPROC wndproc);
65
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)
69 */
70 #define MAXSOCKETS 1024
71
72 #if (MAXSOCKETS > (0x7FFF - WM_USER + 1))
73 #error "MAXSOCKETS is too big!"
74 #endif
75
76 #ifndef __WXWINCE__
77 typedef int (PASCAL *WSAAsyncSelect_t)(SOCKET,HWND,u_int,long);
78 #else
79 /* Typedef the needed function prototypes and the WSANETWORKEVENTS structure
80 */
81 typedef struct _WSANETWORKEVENTS {
82 long lNetworkEvents;
83 int iErrorCode[10];
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);
89 #endif //__WXWINCE__
90
91 LRESULT CALLBACK wxSocket_Internal_WinProc(HWND, UINT, WPARAM, LPARAM);
92
93 /* Global variables */
94
95 static HWND hWin;
96 wxCRIT_SECT_DECLARE_MEMBER(gs_critical);
97 static wxSocketImplMSW *socketList[MAXSOCKETS];
98 static int firstAvailable;
99
100 #ifndef __WXWINCE__
101 static WSAAsyncSelect_t gs_WSAAsyncSelect = NULL;
102 #else
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.
111 */
112 typedef struct thread_data{
113 HWND hEvtWin;
114 unsigned long msgnumber;
115 unsigned long fd;
116 unsigned long lEvent;
117 }thread_data;
118 #endif
119
120 #ifdef __WXWINCE__
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.
125 */
126 DWORD WINAPI SocketThread(LPVOID data)
127 {
128 WSANETWORKEVENTS NetworkEvents;
129 thread_data* d = (thread_data *)data;
130
131 HANDLE NetworkEvent = gs_WSACreateEvent();
132 gs_WSAEventSelect(d->fd, NetworkEvent, d->lEvent);
133
134 while(socketHash[d->fd] == true)
135 {
136 if ((gs_WSAWaitForMultipleEvents(1, &NetworkEvent, FALSE,INFINITE, FALSE)) == WAIT_FAILED)
137 {
138 printf("WSAWaitForMultipleEvents failed with error %d\n", WSAGetLastError());
139 return 0;
140 }
141 if (gs_WSAEnumNetworkEvents(d->fd ,NetworkEvent, &NetworkEvents) == SOCKET_ERROR)
142 {
143 printf("WSAEnumNetworkEvents failed with error %d\n", WSAGetLastError());
144 return 0;
145 }
146
147 long flags = NetworkEvents.lNetworkEvents;
148 if (flags & FD_READ)
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);
152 if (flags & FD_OOB)
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);
160
161 }
162 gs_WSAEventSelect(d->fd, NetworkEvent, 0);
163 ExitThread(0);
164 return 0;
165 }
166 #endif
167
168 // ----------------------------------------------------------------------------
169 // MSW implementation of wxSocketManager
170 // ----------------------------------------------------------------------------
171
172 class wxSocketMSWManager : public wxSocketManager
173 {
174 public:
175 virtual bool OnInit();
176 virtual void OnExit();
177
178 virtual wxSocketImpl *CreateSocket(wxSocketBase& wxsocket)
179 {
180 return new wxSocketImplMSW(wxsocket);
181 }
182 virtual void Install_Callback(wxSocketImpl *socket, wxSocketNotify event);
183 virtual void Uninstall_Callback(wxSocketImpl *socket, wxSocketNotify event);
184
185 private:
186 static wxDynamicLibrary gs_wsock32dll;
187 };
188
189 wxDynamicLibrary wxSocketMSWManager::gs_wsock32dll;
190
191 bool wxSocketMSWManager::OnInit()
192 {
193 static LPCTSTR pclassname = NULL;
194 int i;
195
196 /* Create internal window for event notifications */
197 hWin = wxCreateHiddenWindow(&pclassname, CLASSNAME, wxSocket_Internal_WinProc);
198 if (!hWin)
199 return false;
200
201 /* Initialize socket list */
202 for (i = 0; i < MAXSOCKETS; i++)
203 {
204 socketList[i] = NULL;
205 }
206 firstAvailable = 0;
207
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
210 // sockets
211 #ifdef __WXWINCE__
212 #define WINSOCK_DLL_NAME _T("ws2.dll")
213 #else
214 #define WINSOCK_DLL_NAME _T("wsock32.dll")
215 #endif
216
217 gs_wsock32dll.Load(WINSOCK_DLL_NAME, wxDL_VERBATIM | wxDL_QUIET);
218 if ( !gs_wsock32dll.IsLoaded() )
219 return false;
220
221 #ifndef __WXWINCE__
222 wxDL_INIT_FUNC(gs_, WSAAsyncSelect, gs_wsock32dll);
223 if ( !gs_WSAAsyncSelect )
224 return false;
225 #else
226 wxDL_INIT_FUNC(gs_, WSAEventSelect, gs_wsock32dll);
227 if ( !gs_WSAEventSelect )
228 return false;
229
230 wxDL_INIT_FUNC(gs_, WSACreateEvent, gs_wsock32dll);
231 if ( !gs_WSACreateEvent )
232 return false;
233
234 wxDL_INIT_FUNC(gs_, WSAWaitForMultipleEvents, gs_wsock32dll);
235 if ( !gs_WSAWaitForMultipleEvents )
236 return false;
237
238 wxDL_INIT_FUNC(gs_, WSAEnumNetworkEvents, gs_wsock32dll);
239 if ( !gs_WSAEnumNetworkEvents )
240 return false;
241
242 currSocket = 0;
243 #endif // !__WXWINCE__/__WXWINCE__
244
245 // finally initialize WinSock
246 WSADATA wsaData;
247 return WSAStartup((1 << 8) | 1, &wsaData) == 0;
248 }
249
250 void wxSocketMSWManager::OnExit()
251 {
252 #ifdef __WXWINCE__
253 /* Delete the threads here */
254 for(unsigned int i=0; i < currSocket; i++)
255 CloseHandle(hThread[i]);
256 #endif
257 /* Destroy internal window */
258 DestroyWindow(hWin);
259 UnregisterClass(CLASSNAME, wxGetInstance());
260
261 WSACleanup();
262
263 gs_wsock32dll.Unload();
264 }
265
266 /* Per-socket GUI initialization / cleanup */
267
268 wxSocketImplMSW::wxSocketImplMSW(wxSocketBase& wxsocket)
269 : wxSocketImpl(wxsocket)
270 {
271 /* Allocate a new message number for this socket */
272 wxCRIT_SECT_LOCKER(lock, gs_critical);
273
274 int i = firstAvailable;
275 while (socketList[i] != NULL)
276 {
277 i = (i + 1) % MAXSOCKETS;
278
279 if (i == firstAvailable) /* abort! */
280 {
281 m_msgnumber = 0; // invalid
282 return;
283 }
284 }
285 socketList[i] = this;
286 firstAvailable = (i + 1) % MAXSOCKETS;
287 m_msgnumber = (i + WM_USER);
288 }
289
290 wxSocketImplMSW::~wxSocketImplMSW()
291 {
292 /* Remove the socket from the list */
293 wxCRIT_SECT_LOCKER(lock, gs_critical);
294
295 if ( m_msgnumber )
296 {
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
300 MSG msg;
301 while ( ::PeekMessage(&msg, hWin, m_msgnumber, m_msgnumber, PM_REMOVE) )
302 ;
303
304 socketList[m_msgnumber - WM_USER] = NULL;
305 }
306 //else: the socket has never been created successfully
307 }
308
309 /* Windows proc for asynchronous event handling */
310
311 LRESULT CALLBACK wxSocket_Internal_WinProc(HWND hWnd,
312 UINT uMsg,
313 WPARAM wParam,
314 LPARAM lParam)
315 {
316 if ( uMsg < WM_USER || uMsg > (WM_USER + MAXSOCKETS - 1))
317 return DefWindowProc(hWnd, uMsg, wParam, lParam);
318
319 wxSocketImplMSW *socket;
320 wxSocketNotify event = (wxSocketNotify)-1;
321 {
322 wxCRIT_SECT_LOCKER(lock, gs_critical);
323
324 socket = socketList[(uMsg - WM_USER)];
325 if ( !socket )
326 return 0;
327
328 wxASSERT_MSG( socket->m_fd == (SOCKET)wParam,
329 "mismatch between message and socket?" );
330
331 switch WSAGETSELECTEVENT(lParam)
332 {
333 case FD_READ:
334 event = wxSOCKET_INPUT;
335 break;
336
337 case FD_WRITE:
338 event = wxSOCKET_OUTPUT;
339 break;
340
341 case FD_ACCEPT:
342 event = wxSOCKET_CONNECTION;
343 break;
344
345 case FD_CONNECT:
346 event = WSAGETSELECTERROR(lParam) ? wxSOCKET_LOST
347 : wxSOCKET_CONNECTION;
348 break;
349
350 case FD_CLOSE:
351 event = wxSOCKET_LOST;
352 break;
353
354 default:
355 wxFAIL_MSG( "unexpected socket notification" );
356 return 0;
357 }
358 } // unlock gs_critical
359
360 socket->NotifyOnStateChange(event);
361
362 return 0;
363 }
364
365 /*
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.
369 */
370 void wxSocketMSWManager::Install_Callback(wxSocketImpl *socket_,
371 wxSocketNotify WXUNUSED(event))
372 {
373 wxSocketImplMSW * const socket = static_cast<wxSocketImplMSW *>(socket_);
374
375 if (socket->m_fd != INVALID_SOCKET)
376 {
377 /* We could probably just subscribe to all events regardless
378 * of the socket type, but MS recommends to do it this way.
379 */
380 long lEvent = socket->m_server?
381 FD_ACCEPT : (FD_READ | FD_WRITE | FD_CONNECT | FD_CLOSE);
382 #ifndef __WXWINCE__
383 gs_WSAAsyncSelect(socket->m_fd, hWin, socket->m_msgnumber, lEvent);
384 #else
385 /*
386 * WinCE creates a thread for socket event handling.
387 * All needed parameters get passed through the thread_data structure.
388 */
389
390 thread_data* d = new thread_data;
391 d->lEvent = lEvent;
392 d->hEvtWin = hWin;
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);
397 #endif
398 }
399 }
400
401 /*
402 * Disable event notifications (used when shutting down the socket)
403 */
404 void wxSocketMSWManager::Uninstall_Callback(wxSocketImpl *socket_,
405 wxSocketNotify WXUNUSED(event))
406 {
407 wxSocketImplMSW * const socket = static_cast<wxSocketImplMSW *>(socket_);
408
409 if (socket->m_fd != INVALID_SOCKET)
410 {
411 #ifndef __WXWINCE__
412 gs_WSAAsyncSelect(socket->m_fd, hWin, socket->m_msgnumber, 0);
413 #else
414 //Destroy the thread
415 socketHash[socket->m_fd] = false;
416 #endif
417 }
418 }
419
420 // set the wxBase variable to point to our wxSocketManager implementation
421 //
422 // see comments in wx/apptrait.h for the explanation of why do we do it
423 // like this
424 static struct ManagerSetter
425 {
426 ManagerSetter()
427 {
428 static wxSocketMSWManager s_manager;
429 wxAppTraits::SetDefaultSocketManager(&s_manager);
430 }
431 } gs_managerSetter;
432
433 // ============================================================================
434 // wxSocketImpl implementation
435 // ============================================================================
436
437 /* static */
438 wxSocketImpl *wxSocketImpl::Create(wxSocketBase& wxsocket)
439 {
440 return new wxSocketImplMSW(wxsocket);
441 }
442
443 void wxSocketImplMSW::DoClose()
444 {
445 wxSocketManager::Get()->
446 Uninstall_Callback(this, wxSOCKET_MAX_EVENT /* unused anyhow */);
447
448 closesocket(m_fd);
449 }
450
451 wxSocketError wxSocketImplMSW::GetLastError() const
452 {
453 switch ( WSAGetLastError() )
454 {
455 case 0:
456 return wxSOCKET_NOERROR;
457
458 case WSAENOTSOCK:
459 return wxSOCKET_INVSOCK;
460
461 case WSAEWOULDBLOCK:
462 return wxSOCKET_WOULDBLOCK;
463
464 default:
465 return wxSOCKET_IOERR;
466 }
467 }
468
469 /* Generic IO */
470
471 /* Like recv(), send(), ... */
472 int wxSocketImplMSW::Read(void *buffer, int size)
473 {
474 int ret;
475
476 if (m_fd == INVALID_SOCKET || m_server)
477 {
478 m_error = wxSOCKET_INVSOCK;
479 return -1;
480 }
481
482 /* Read the data */
483 if (m_stream)
484 ret = Recv_Stream(buffer, size);
485 else
486 ret = Recv_Dgram(buffer, size);
487
488 if (ret == SOCKET_ERROR)
489 {
490 if (WSAGetLastError() != WSAEWOULDBLOCK)
491 m_error = wxSOCKET_IOERR;
492 else
493 m_error = wxSOCKET_WOULDBLOCK;
494 return -1;
495 }
496
497 return ret;
498 }
499
500 int wxSocketImplMSW::Write(const void *buffer, int size)
501 {
502 int ret;
503
504 if (m_fd == INVALID_SOCKET || m_server)
505 {
506 m_error = wxSOCKET_INVSOCK;
507 return -1;
508 }
509
510 /* Write the data */
511 if (m_stream)
512 ret = Send_Stream(buffer, size);
513 else
514 ret = Send_Dgram(buffer, size);
515
516 if (ret == SOCKET_ERROR)
517 {
518 if (WSAGetLastError() != WSAEWOULDBLOCK)
519 m_error = wxSOCKET_IOERR;
520 else
521 m_error = wxSOCKET_WOULDBLOCK;
522
523 return -1;
524 }
525
526 return ret;
527 }
528
529 /* Internals (IO) */
530
531 /*
532 * For blocking sockets, wait until the connection is
533 * established or fails, or until timeout ellapses.
534 */
535 wxSocketError wxSocketImplMSW::Connect_Timeout()
536 {
537 fd_set writefds;
538 fd_set exceptfds;
539
540 FD_ZERO(&writefds);
541 FD_ZERO(&exceptfds);
542 FD_SET(m_fd, &writefds);
543 FD_SET(m_fd, &exceptfds);
544 if (select(0, NULL, &writefds, &exceptfds, &m_timeout) == 0)
545 {
546 m_error = wxSOCKET_TIMEDOUT;
547 return wxSOCKET_TIMEDOUT;
548 }
549 if (!FD_ISSET(m_fd, &writefds))
550 {
551 m_error = wxSOCKET_IOERR;
552 return wxSOCKET_IOERR;
553 }
554
555 return wxSOCKET_NOERROR;
556 }
557
558 int wxSocketImplMSW::Recv_Stream(void *buffer, int size)
559 {
560 return recv(m_fd, static_cast<char *>(buffer), size, 0);
561 }
562
563 int wxSocketImplMSW::Recv_Dgram(void *buffer, int size)
564 {
565 wxSockAddr from;
566 WX_SOCKLEN_T fromlen = sizeof(from);
567 int ret;
568 wxSocketError err;
569
570 ret = recvfrom(m_fd, static_cast<char *>(buffer),
571 size, 0, &from, &fromlen);
572
573 if (ret == SOCKET_ERROR)
574 return SOCKET_ERROR;
575
576 /* Translate a system address into a wxSocketImpl address */
577 if (!m_peer)
578 {
579 m_peer = GAddress_new();
580 if (!m_peer)
581 {
582 m_error = wxSOCKET_MEMERR;
583 return -1;
584 }
585 }
586 err = _GAddress_translate_from(m_peer, (sockaddr*)&from, fromlen);
587 if (err != wxSOCKET_NOERROR)
588 {
589 GAddress_destroy(m_peer);
590 m_peer = NULL;
591 m_error = err;
592 return -1;
593 }
594
595 return ret;
596 }
597
598 int wxSocketImplMSW::Send_Stream(const void *buffer, int size)
599 {
600 return send(m_fd, static_cast<const char *>(buffer), size, 0);
601 }
602
603 int wxSocketImplMSW::Send_Dgram(const void *buffer, int size)
604 {
605 struct sockaddr *addr;
606 int len, ret;
607 wxSocketError err;
608
609 if (!m_peer)
610 {
611 m_error = wxSOCKET_INVADDR;
612 return -1;
613 }
614
615 err = _GAddress_translate_to(m_peer, &addr, &len);
616 if (err != wxSOCKET_NOERROR)
617 {
618 m_error = err;
619 return -1;
620 }
621
622 ret = sendto(m_fd, static_cast<const char *>(buffer), size, 0, addr, len);
623
624 /* Frees memory allocated by _GAddress_translate_to */
625 free(addr);
626
627 return ret;
628 }
629
630 /*
631 * -------------------------------------------------------------------------
632 * GAddress
633 * -------------------------------------------------------------------------
634 */
635
636 /* CHECK_ADDRESS verifies that the current address family is either
637 * wxSOCKET_NOFAMILY or wxSOCKET_*family*, and if it is wxSOCKET_NOFAMILY, it
638 * initalizes it to be a wxSOCKET_*family*. In other cases, it returns
639 * an appropiate error code.
640 *
641 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
642 */
643 #define CHECK_ADDRESS(address, family) \
644 { \
645 if (address->m_family == wxSOCKET_NOFAMILY) \
646 if (_GAddress_Init_##family(address) != wxSOCKET_NOERROR) \
647 return address->m_error; \
648 if (address->m_family != wxSOCKET_##family) \
649 { \
650 address->m_error = wxSOCKET_INVADDR; \
651 return wxSOCKET_INVADDR; \
652 } \
653 }
654
655 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
656 { \
657 if (address->m_family == wxSOCKET_NOFAMILY) \
658 if (_GAddress_Init_##family(address) != wxSOCKET_NOERROR) \
659 return retval; \
660 if (address->m_family != wxSOCKET_##family) \
661 { \
662 address->m_error = wxSOCKET_INVADDR; \
663 return retval; \
664 } \
665 }
666
667
668 GAddress *GAddress_new()
669 {
670 GAddress *address;
671
672 if ((address = (GAddress *) malloc(sizeof(GAddress))) == NULL)
673 return NULL;
674
675 address->m_family = wxSOCKET_NOFAMILY;
676 address->m_addr = NULL;
677 address->m_len = 0;
678
679 return address;
680 }
681
682 GAddress *GAddress_copy(GAddress *address)
683 {
684 GAddress *addr2;
685
686 if ((addr2 = (GAddress *) malloc(sizeof(GAddress))) == NULL)
687 return NULL;
688
689 memcpy(addr2, address, sizeof(GAddress));
690
691 if (address->m_addr)
692 {
693 addr2->m_addr = (struct sockaddr *) malloc(addr2->m_len);
694 if (addr2->m_addr == NULL)
695 {
696 free(addr2);
697 return NULL;
698 }
699 memcpy(addr2->m_addr, address->m_addr, addr2->m_len);
700 }
701
702 return addr2;
703 }
704
705 void GAddress_destroy(GAddress *address)
706 {
707 if (address->m_addr)
708 free(address->m_addr);
709
710 free(address);
711 }
712
713 void GAddress_SetFamily(GAddress *address, GAddressType type)
714 {
715 address->m_family = type;
716 }
717
718 GAddressType GAddress_GetFamily(GAddress *address)
719 {
720 return address->m_family;
721 }
722
723 wxSocketError _GAddress_translate_from(GAddress *address,
724 struct sockaddr *addr, int len)
725 {
726 address->m_realfamily = addr->sa_family;
727 switch (addr->sa_family)
728 {
729 case AF_INET:
730 address->m_family = wxSOCKET_INET;
731 break;
732 case AF_UNIX:
733 address->m_family = wxSOCKET_UNIX;
734 break;
735 #if wxUSE_IPV6
736 case AF_INET6:
737 address->m_family = wxSOCKET_INET6;
738 break;
739 #endif
740 default:
741 {
742 address->m_error = wxSOCKET_INVOP;
743 return wxSOCKET_INVOP;
744 }
745 }
746
747 if (address->m_addr)
748 free(address->m_addr);
749
750 address->m_len = len;
751 address->m_addr = (struct sockaddr *) malloc(len);
752
753 if (address->m_addr == NULL)
754 {
755 address->m_error = wxSOCKET_MEMERR;
756 return wxSOCKET_MEMERR;
757 }
758 memcpy(address->m_addr, addr, len);
759
760 return wxSOCKET_NOERROR;
761 }
762
763 wxSocketError _GAddress_translate_to(GAddress *address,
764 struct sockaddr **addr, int *len)
765 {
766 if (!address->m_addr)
767 {
768 address->m_error = wxSOCKET_INVADDR;
769 return wxSOCKET_INVADDR;
770 }
771
772 *len = address->m_len;
773 *addr = (struct sockaddr *) malloc(address->m_len);
774 if (*addr == NULL)
775 {
776 address->m_error = wxSOCKET_MEMERR;
777 return wxSOCKET_MEMERR;
778 }
779
780 memcpy(*addr, address->m_addr, address->m_len);
781 return wxSOCKET_NOERROR;
782 }
783
784 /*
785 * -------------------------------------------------------------------------
786 * Internet address family
787 * -------------------------------------------------------------------------
788 */
789
790 wxSocketError _GAddress_Init_INET(GAddress *address)
791 {
792 address->m_len = sizeof(struct sockaddr_in);
793 address->m_addr = (struct sockaddr *) malloc(address->m_len);
794 if (address->m_addr == NULL)
795 {
796 address->m_error = wxSOCKET_MEMERR;
797 return wxSOCKET_MEMERR;
798 }
799
800 address->m_family = wxSOCKET_INET;
801 address->m_realfamily = AF_INET;
802 ((struct sockaddr_in *)address->m_addr)->sin_family = AF_INET;
803 ((struct sockaddr_in *)address->m_addr)->sin_addr.s_addr = INADDR_ANY;
804
805 return wxSOCKET_NOERROR;
806 }
807
808 wxSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname)
809 {
810 struct hostent *he;
811 struct in_addr *addr;
812
813 CHECK_ADDRESS(address, INET);
814
815 addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr);
816
817 addr->s_addr = inet_addr(hostname);
818
819 /* If it is a numeric host name, convert it now */
820 if (addr->s_addr == INADDR_NONE)
821 {
822 struct in_addr *array_addr;
823
824 /* It is a real name, we solve it */
825 if ((he = gethostbyname(hostname)) == NULL)
826 {
827 /* addr->s_addr = INADDR_NONE just done by inet_addr() above */
828 address->m_error = wxSOCKET_NOHOST;
829 return wxSOCKET_NOHOST;
830 }
831 array_addr = (struct in_addr *) *(he->h_addr_list);
832 addr->s_addr = array_addr[0].s_addr;
833 }
834 return wxSOCKET_NOERROR;
835 }
836
837 wxSocketError GAddress_INET_SetBroadcastAddress(GAddress *address)
838 {
839 return GAddress_INET_SetHostAddress(address, INADDR_BROADCAST);
840 }
841
842 wxSocketError GAddress_INET_SetAnyAddress(GAddress *address)
843 {
844 return GAddress_INET_SetHostAddress(address, INADDR_ANY);
845 }
846
847 wxSocketError GAddress_INET_SetHostAddress(GAddress *address,
848 unsigned long hostaddr)
849 {
850 struct in_addr *addr;
851
852 CHECK_ADDRESS(address, INET);
853
854 addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr);
855 addr->s_addr = htonl(hostaddr);
856
857 return wxSOCKET_NOERROR;
858 }
859
860 wxSocketError GAddress_INET_SetPortName(GAddress *address, const char *port,
861 const char *protocol)
862 {
863 struct servent *se;
864 struct sockaddr_in *addr;
865
866 CHECK_ADDRESS(address, INET);
867
868 if (!port)
869 {
870 address->m_error = wxSOCKET_INVPORT;
871 return wxSOCKET_INVPORT;
872 }
873
874 se = getservbyname(port, protocol);
875 if (!se)
876 {
877 if (isdigit(port[0]))
878 {
879 int port_int;
880
881 port_int = atoi(port);
882 addr = (struct sockaddr_in *)address->m_addr;
883 addr->sin_port = htons((u_short) port_int);
884 return wxSOCKET_NOERROR;
885 }
886
887 address->m_error = wxSOCKET_INVPORT;
888 return wxSOCKET_INVPORT;
889 }
890
891 addr = (struct sockaddr_in *)address->m_addr;
892 addr->sin_port = se->s_port;
893
894 return wxSOCKET_NOERROR;
895 }
896
897 wxSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port)
898 {
899 struct sockaddr_in *addr;
900
901 CHECK_ADDRESS(address, INET);
902
903 addr = (struct sockaddr_in *)address->m_addr;
904 addr->sin_port = htons(port);
905
906 return wxSOCKET_NOERROR;
907 }
908
909 wxSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname, size_t sbuf)
910 {
911 struct hostent *he;
912 char *addr_buf;
913 struct sockaddr_in *addr;
914
915 CHECK_ADDRESS(address, INET);
916
917 addr = (struct sockaddr_in *)address->m_addr;
918 addr_buf = (char *)&(addr->sin_addr);
919
920 he = gethostbyaddr(addr_buf, sizeof(addr->sin_addr), AF_INET);
921 if (he == NULL)
922 {
923 address->m_error = wxSOCKET_NOHOST;
924 return wxSOCKET_NOHOST;
925 }
926
927 strncpy(hostname, he->h_name, sbuf);
928
929 return wxSOCKET_NOERROR;
930 }
931
932 unsigned long GAddress_INET_GetHostAddress(GAddress *address)
933 {
934 struct sockaddr_in *addr;
935
936 CHECK_ADDRESS_RETVAL(address, INET, 0);
937
938 addr = (struct sockaddr_in *)address->m_addr;
939
940 return ntohl(addr->sin_addr.s_addr);
941 }
942
943 unsigned short GAddress_INET_GetPort(GAddress *address)
944 {
945 struct sockaddr_in *addr;
946
947 CHECK_ADDRESS_RETVAL(address, INET, 0);
948
949 addr = (struct sockaddr_in *)address->m_addr;
950 return ntohs(addr->sin_port);
951 }
952
953
954 #if wxUSE_IPV6
955 /*
956 * -------------------------------------------------------------------------
957 * Internet IPv6 address family
958 * -------------------------------------------------------------------------
959 */
960 #include "ws2tcpip.h"
961
962 #ifdef __VISUALC__
963 #pragma comment(lib,"ws2_32")
964 #endif // __VISUALC__
965
966 wxSocketError _GAddress_Init_INET6(GAddress *address)
967 {
968 struct in6_addr any_address = IN6ADDR_ANY_INIT;
969 address->m_len = sizeof(struct sockaddr_in6);
970 address->m_addr = (struct sockaddr *) malloc(address->m_len);
971 if (address->m_addr == NULL)
972 {
973 address->m_error = wxSOCKET_MEMERR;
974 return wxSOCKET_MEMERR;
975 }
976 memset(address->m_addr,0,address->m_len);
977
978 address->m_family = wxSOCKET_INET6;
979 address->m_realfamily = AF_INET6;
980 ((struct sockaddr_in6 *)address->m_addr)->sin6_family = AF_INET6;
981 ((struct sockaddr_in6 *)address->m_addr)->sin6_addr = any_address;
982
983 return wxSOCKET_NOERROR;
984 }
985
986 wxSocketError GAddress_INET6_SetHostName(GAddress *address, const char *hostname)
987 {
988 CHECK_ADDRESS(address, INET6);
989
990 addrinfo hints;
991 memset( & hints, 0, sizeof( hints ) );
992 hints.ai_family = AF_INET6;
993 addrinfo * info = 0;
994 if ( getaddrinfo( hostname, "0", & hints, & info ) || ! info )
995 {
996 address->m_error = wxSOCKET_NOHOST;
997 return wxSOCKET_NOHOST;
998 }
999
1000 memcpy( address->m_addr, info->ai_addr, info->ai_addrlen );
1001 freeaddrinfo( info );
1002 return wxSOCKET_NOERROR;
1003 }
1004
1005 wxSocketError GAddress_INET6_SetAnyAddress(GAddress *address)
1006 {
1007 CHECK_ADDRESS(address, INET6);
1008
1009 struct in6_addr addr;
1010 memset( & addr, 0, sizeof( addr ) );
1011 return GAddress_INET6_SetHostAddress(address, addr);
1012 }
1013 wxSocketError GAddress_INET6_SetHostAddress(GAddress *address,
1014 struct in6_addr hostaddr)
1015 {
1016 CHECK_ADDRESS(address, INET6);
1017
1018 ((struct sockaddr_in6 *)address->m_addr)->sin6_addr = hostaddr;
1019
1020 return wxSOCKET_NOERROR;
1021 }
1022
1023 wxSocketError GAddress_INET6_SetPortName(GAddress *address, const char *port,
1024 const char *protocol)
1025 {
1026 struct servent *se;
1027 struct sockaddr_in6 *addr;
1028
1029 CHECK_ADDRESS(address, INET6);
1030
1031 if (!port)
1032 {
1033 address->m_error = wxSOCKET_INVPORT;
1034 return wxSOCKET_INVPORT;
1035 }
1036
1037 se = getservbyname(port, protocol);
1038 if (!se)
1039 {
1040 if (isdigit((unsigned char) port[0]))
1041 {
1042 int port_int;
1043
1044 port_int = atoi(port);
1045 addr = (struct sockaddr_in6 *)address->m_addr;
1046 addr->sin6_port = htons((u_short) port_int);
1047 return wxSOCKET_NOERROR;
1048 }
1049
1050 address->m_error = wxSOCKET_INVPORT;
1051 return wxSOCKET_INVPORT;
1052 }
1053
1054 addr = (struct sockaddr_in6 *)address->m_addr;
1055 addr->sin6_port = se->s_port;
1056
1057 return wxSOCKET_NOERROR;
1058 }
1059
1060 wxSocketError GAddress_INET6_SetPort(GAddress *address, unsigned short port)
1061 {
1062 struct sockaddr_in6 *addr;
1063
1064 CHECK_ADDRESS(address, INET6);
1065
1066 addr = (struct sockaddr_in6 *)address->m_addr;
1067 addr->sin6_port = htons(port);
1068
1069 return wxSOCKET_NOERROR;
1070 }
1071
1072 wxSocketError GAddress_INET6_GetHostName(GAddress *address, char *hostname, size_t sbuf)
1073 {
1074 struct hostent *he;
1075 char *addr_buf;
1076 struct sockaddr_in6 *addr;
1077
1078 CHECK_ADDRESS(address, INET6);
1079
1080 addr = (struct sockaddr_in6 *)address->m_addr;
1081 addr_buf = (char *)&(addr->sin6_addr);
1082
1083 he = gethostbyaddr(addr_buf, sizeof(addr->sin6_addr), AF_INET6);
1084 if (he == NULL)
1085 {
1086 address->m_error = wxSOCKET_NOHOST;
1087 return wxSOCKET_NOHOST;
1088 }
1089
1090 strncpy(hostname, he->h_name, sbuf);
1091
1092 return wxSOCKET_NOERROR;
1093 }
1094
1095 wxSocketError GAddress_INET6_GetHostAddress(GAddress *address,struct in6_addr *hostaddr)
1096 {
1097 CHECK_ADDRESS_RETVAL(address, INET6, wxSOCKET_INVADDR);
1098 *hostaddr = ( (struct sockaddr_in6 *)address->m_addr )->sin6_addr;
1099 return wxSOCKET_NOERROR;
1100 }
1101
1102 unsigned short GAddress_INET6_GetPort(GAddress *address)
1103 {
1104 CHECK_ADDRESS_RETVAL(address, INET6, 0);
1105
1106 return ntohs( ((struct sockaddr_in6 *)address->m_addr)->sin6_port );
1107 }
1108
1109 #endif // wxUSE_IPV6
1110
1111 /*
1112 * -------------------------------------------------------------------------
1113 * Unix address family
1114 * -------------------------------------------------------------------------
1115 */
1116
1117 wxSocketError _GAddress_Init_UNIX(GAddress *address)
1118 {
1119 address->m_error = wxSOCKET_INVADDR;
1120 return wxSOCKET_INVADDR;
1121 }
1122
1123 wxSocketError GAddress_UNIX_SetPath(GAddress *address, const char *WXUNUSED(path))
1124 {
1125 address->m_error = wxSOCKET_INVADDR;
1126 return wxSOCKET_INVADDR;
1127 }
1128
1129 wxSocketError GAddress_UNIX_GetPath(GAddress *address, char *WXUNUSED(path), size_t WXUNUSED(sbuf))
1130 {
1131 address->m_error = wxSOCKET_INVADDR;
1132 return wxSOCKET_INVADDR;
1133 }
1134
1135 #endif // wxUSE_SOCKETS