]>
Commit | Line | Data |
---|---|---|
51fe4b60 | 1 | ///////////////////////////////////////////////////////////////////////////// |
116de991 VZ |
2 | // Name: src/msw/sockmsw.cpp |
3 | // Purpose: MSW-specific socket code | |
51fe4b60 VZ |
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 | ||
b7ceceb1 | 13 | |
86afa786 VS |
14 | // For compilers that support precompilation, includes "wx.h". |
15 | #include "wx/wxprec.h" | |
16 | ||
17 | #ifdef __BORLANDC__ | |
18 | #pragma hdrstop | |
19 | #endif | |
20 | ||
2804f77d VZ |
21 | #if wxUSE_SOCKETS |
22 | ||
b7ceceb1 DE |
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 | |
1c8681b4 | 28 | # pragma warning(disable:4115) /* named type definition in parentheses */ |
b7ceceb1 DE |
29 | #endif |
30 | ||
60913641 | 31 | #include "wx/private/socket.h" |
e8d9821d | 32 | #include "wx/msw/private.h" // for wxGetInstance() |
2804f77d | 33 | #include "wx/apptrait.h" |
f2c94e8a | 34 | #include "wx/thread.h" |
d8abb95f | 35 | #include "wx/dynlib.h" |
b7ceceb1 | 36 | |
116de991 VZ |
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 | */ | |
b7ceceb1 | 45 | #include "wx/msw/wince/net.h" |
7ec69821 | 46 | #include "wx/hashmap.h" |
84aa68c4 | 47 | WX_DECLARE_HASH_MAP(int,bool,wxIntegerHash,wxIntegerEqual,SocketHash); |
b7ceceb1 | 48 | |
116de991 VZ |
49 | #ifndef isdigit |
50 | #define isdigit(x) (x > 47 && x < 58) | |
51 | #endif | |
52 | #include "wx/msw/wince/net.h" | |
b7ceceb1 | 53 | |
116de991 | 54 | #endif // __WXWINCE__ |
b7ceceb1 DE |
55 | |
56 | #ifdef _MSC_VER | |
57 | # pragma warning(default:4115) /* named type definition in parentheses */ | |
58 | #endif | |
59 | ||
51fe4b60 | 60 | #define CLASSNAME TEXT("_wxSocket_Internal_Window_Class") |
b7ceceb1 DE |
61 | |
62 | /* implemented in utils.cpp */ | |
32524c8f | 63 | extern "C" WXDLLIMPEXP_BASE HWND |
b7ceceb1 DE |
64 | wxCreateHiddenWindow(LPCTSTR *pclassname, LPCTSTR classname, WNDPROC wndproc); |
65 | ||
51fe4b60 | 66 | /* Maximum number of different wxSocket objects at a given time. |
b7ceceb1 DE |
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 | ||
dbfb61b3 | 76 | #ifndef __WXWINCE__ |
d8abb95f | 77 | typedef int (PASCAL *WSAAsyncSelect_t)(SOCKET,HWND,u_int,long); |
dbfb61b3 JS |
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; | |
d8abb95f VZ |
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); | |
dbfb61b3 | 89 | #endif //__WXWINCE__ |
b7ceceb1 | 90 | |
51fe4b60 | 91 | LRESULT CALLBACK wxSocket_Internal_WinProc(HWND, UINT, WPARAM, LPARAM); |
c90cc42e | 92 | |
b7ceceb1 DE |
93 | /* Global variables */ |
94 | ||
b7ceceb1 | 95 | static HWND hWin; |
f2c94e8a | 96 | wxCRIT_SECT_DECLARE_MEMBER(gs_critical); |
51fe4b60 | 97 | static wxSocketImplMSW *socketList[MAXSOCKETS]; |
b7ceceb1 | 98 | static int firstAvailable; |
dbfb61b3 JS |
99 | |
100 | #ifndef __WXWINCE__ | |
d8abb95f | 101 | static WSAAsyncSelect_t gs_WSAAsyncSelect = NULL; |
dbfb61b3 | 102 | #else |
84aa68c4 | 103 | static SocketHash socketHash; |
dbfb61b3 JS |
104 | static unsigned int currSocket; |
105 | HANDLE hThread[MAXSOCKETS]; | |
d8abb95f VZ |
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; | |
dbfb61b3 JS |
110 | /* This structure will be used to pass data on to the thread that handles socket events. |
111 | */ | |
112 | typedef struct thread_data{ | |
7ec69821 WS |
113 | HWND hEvtWin; |
114 | unsigned long msgnumber; | |
115 | unsigned long fd; | |
116 | unsigned long lEvent; | |
dbfb61b3 JS |
117 | }thread_data; |
118 | #endif | |
119 | ||
dbfb61b3 | 120 | #ifdef __WXWINCE__ |
d8abb95f VZ |
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. | |
dbfb61b3 JS |
125 | */ |
126 | DWORD WINAPI SocketThread(LPVOID data) | |
127 | { | |
7ec69821 WS |
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; | |
dbfb61b3 JS |
165 | } |
166 | #endif | |
167 | ||
2804f77d | 168 | // ---------------------------------------------------------------------------- |
51fe4b60 | 169 | // MSW implementation of wxSocketManager |
2804f77d | 170 | // ---------------------------------------------------------------------------- |
dbfb61b3 | 171 | |
51fe4b60 | 172 | class wxSocketMSWManager : public wxSocketManager |
1c6dd11c | 173 | { |
2804f77d VZ |
174 | public: |
175 | virtual bool OnInit(); | |
176 | virtual void OnExit(); | |
177 | ||
51fe4b60 VZ |
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); | |
d8abb95f VZ |
184 | |
185 | private: | |
186 | static wxDynamicLibrary gs_wsock32dll; | |
2804f77d | 187 | }; |
c90cc42e | 188 | |
d8abb95f VZ |
189 | wxDynamicLibrary wxSocketMSWManager::gs_wsock32dll; |
190 | ||
51fe4b60 | 191 | bool wxSocketMSWManager::OnInit() |
b7ceceb1 | 192 | { |
84aa68c4 JS |
193 | static LPCTSTR pclassname = NULL; |
194 | int i; | |
b7ceceb1 | 195 | |
84aa68c4 | 196 | /* Create internal window for event notifications */ |
51fe4b60 | 197 | hWin = wxCreateHiddenWindow(&pclassname, CLASSNAME, wxSocket_Internal_WinProc); |
84aa68c4 JS |
198 | if (!hWin) |
199 | return false; | |
b7ceceb1 | 200 | |
84aa68c4 | 201 | /* Initialize socket list */ |
84aa68c4 JS |
202 | for (i = 0; i < MAXSOCKETS; i++) |
203 | { | |
204 | socketList[i] = NULL; | |
205 | } | |
206 | firstAvailable = 0; | |
b7ceceb1 | 207 | |
d8abb95f VZ |
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 | ||
dbfb61b3 | 221 | #ifndef __WXWINCE__ |
d8abb95f VZ |
222 | wxDL_INIT_FUNC(gs_, WSAAsyncSelect, gs_wsock32dll); |
223 | if ( !gs_WSAAsyncSelect ) | |
224 | return false; | |
dbfb61b3 | 225 | #else |
d8abb95f VZ |
226 | wxDL_INIT_FUNC(gs_, WSAEventSelect, gs_wsock32dll); |
227 | if ( !gs_WSAEventSelect ) | |
228 | return false; | |
1c6dd11c | 229 | |
d8abb95f VZ |
230 | wxDL_INIT_FUNC(gs_, WSACreateEvent, gs_wsock32dll); |
231 | if ( !gs_WSACreateEvent ) | |
232 | return false; | |
84aa68c4 | 233 | |
d8abb95f VZ |
234 | wxDL_INIT_FUNC(gs_, WSAWaitForMultipleEvents, gs_wsock32dll); |
235 | if ( !gs_WSAWaitForMultipleEvents ) | |
236 | return false; | |
84aa68c4 | 237 | |
d8abb95f VZ |
238 | wxDL_INIT_FUNC(gs_, WSAEnumNetworkEvents, gs_wsock32dll); |
239 | if ( !gs_WSAEnumNetworkEvents ) | |
240 | return false; | |
84aa68c4 | 241 | |
d8abb95f VZ |
242 | currSocket = 0; |
243 | #endif // !__WXWINCE__/__WXWINCE__ | |
7ec69821 | 244 | |
51fe4b60 VZ |
245 | // finally initialize WinSock |
246 | WSADATA wsaData; | |
247 | return WSAStartup((1 << 8) | 1, &wsaData) == 0; | |
b7ceceb1 DE |
248 | } |
249 | ||
51fe4b60 | 250 | void wxSocketMSWManager::OnExit() |
b7ceceb1 | 251 | { |
dbfb61b3 | 252 | #ifdef __WXWINCE__ |
1c6dd11c | 253 | /* Delete the threads here */ |
7ec69821 WS |
254 | for(unsigned int i=0; i < currSocket; i++) |
255 | CloseHandle(hThread[i]); | |
dbfb61b3 | 256 | #endif |
b7ceceb1 DE |
257 | /* Destroy internal window */ |
258 | DestroyWindow(hWin); | |
e8d9821d | 259 | UnregisterClass(CLASSNAME, wxGetInstance()); |
b7ceceb1 | 260 | |
51fe4b60 | 261 | WSACleanup(); |
d8abb95f VZ |
262 | |
263 | gs_wsock32dll.Unload(); | |
b7ceceb1 DE |
264 | } |
265 | ||
266 | /* Per-socket GUI initialization / cleanup */ | |
267 | ||
51fe4b60 VZ |
268 | wxSocketImplMSW::wxSocketImplMSW(wxSocketBase& wxsocket) |
269 | : wxSocketImpl(wxsocket) | |
b7ceceb1 | 270 | { |
b7ceceb1 | 271 | /* Allocate a new message number for this socket */ |
f2c94e8a | 272 | wxCRIT_SECT_LOCKER(lock, gs_critical); |
b7ceceb1 | 273 | |
51fe4b60 | 274 | int i = firstAvailable; |
b7ceceb1 DE |
275 | while (socketList[i] != NULL) |
276 | { | |
277 | i = (i + 1) % MAXSOCKETS; | |
278 | ||
279 | if (i == firstAvailable) /* abort! */ | |
280 | { | |
51fe4b60 VZ |
281 | m_msgnumber = 0; // invalid |
282 | return; | |
b7ceceb1 DE |
283 | } |
284 | } | |
51fe4b60 | 285 | socketList[i] = this; |
b7ceceb1 | 286 | firstAvailable = (i + 1) % MAXSOCKETS; |
51fe4b60 | 287 | m_msgnumber = (i + WM_USER); |
f0fbbe23 VZ |
288 | } |
289 | ||
51fe4b60 | 290 | wxSocketImplMSW::~wxSocketImplMSW() |
b7ceceb1 DE |
291 | { |
292 | /* Remove the socket from the list */ | |
f2c94e8a | 293 | wxCRIT_SECT_LOCKER(lock, gs_critical); |
cab9b205 | 294 | |
51fe4b60 | 295 | if ( m_msgnumber ) |
eb97543d | 296 | { |
cab9b205 VZ |
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; | |
51fe4b60 | 301 | while ( ::PeekMessage(&msg, hWin, m_msgnumber, m_msgnumber, PM_REMOVE) ) |
cab9b205 VZ |
302 | ; |
303 | ||
51fe4b60 | 304 | socketList[m_msgnumber - WM_USER] = NULL; |
cab9b205 | 305 | } |
eb97543d | 306 | //else: the socket has never been created successfully |
b7ceceb1 DE |
307 | } |
308 | ||
309 | /* Windows proc for asynchronous event handling */ | |
310 | ||
51fe4b60 | 311 | LRESULT CALLBACK wxSocket_Internal_WinProc(HWND hWnd, |
b7ceceb1 DE |
312 | UINT uMsg, |
313 | WPARAM wParam, | |
314 | LPARAM lParam) | |
315 | { | |
f2c94e8a VZ |
316 | if ( uMsg < WM_USER || uMsg > (WM_USER + MAXSOCKETS - 1)) |
317 | return DefWindowProc(hWnd, uMsg, wParam, lParam); | |
b7ceceb1 | 318 | |
f2c94e8a VZ |
319 | wxSocketImplMSW *socket; |
320 | wxSocketNotify event; | |
b7ceceb1 | 321 | { |
f2c94e8a VZ |
322 | wxCRIT_SECT_LOCKER(lock, gs_critical); |
323 | ||
324 | socket = socketList[(uMsg - WM_USER)]; | |
325 | event = (wxSocketNotify) -1; | |
326 | ||
327 | /* Check that the socket still exists (it has not been | |
328 | * destroyed) and for safety, check that the m_fd field | |
329 | * is what we expect it to be. | |
330 | */ | |
331 | if ((socket != NULL) && ((WPARAM)socket->m_fd == wParam)) | |
b7ceceb1 | 332 | { |
f2c94e8a VZ |
333 | switch WSAGETSELECTEVENT(lParam) |
334 | { | |
335 | case FD_READ: event = wxSOCKET_INPUT; break; | |
336 | case FD_WRITE: event = wxSOCKET_OUTPUT; break; | |
337 | case FD_ACCEPT: event = wxSOCKET_CONNECTION; break; | |
338 | case FD_CONNECT: | |
339 | { | |
340 | if (WSAGETSELECTERROR(lParam) != 0) | |
341 | event = wxSOCKET_LOST; | |
342 | else | |
343 | event = wxSOCKET_CONNECTION; | |
344 | break; | |
345 | } | |
346 | case FD_CLOSE: event = wxSOCKET_LOST; break; | |
347 | } | |
348 | ||
349 | if (event != -1) | |
350 | { | |
351 | if (event == wxSOCKET_LOST) | |
352 | socket->m_detected = wxSOCKET_LOST_FLAG; | |
353 | else | |
354 | socket->m_detected |= (1 << event); | |
355 | } | |
b7ceceb1 | 356 | } |
f2c94e8a | 357 | } // unlock gs_critical |
b7ceceb1 | 358 | |
83a7ab5c VZ |
359 | if ( socket ) |
360 | socket->NotifyOnStateChange(event); | |
b7ceceb1 DE |
361 | |
362 | return (LRESULT) 0; | |
b7ceceb1 DE |
363 | } |
364 | ||
f0fbbe23 | 365 | /* |
b7ceceb1 DE |
366 | * Enable all event notifications; we need to be notified of all |
367 | * events for internal processing, but we will only notify users | |
f0fbbe23 | 368 | * when an appropriate callback function has been installed. |
b7ceceb1 | 369 | */ |
51fe4b60 VZ |
370 | void wxSocketMSWManager::Install_Callback(wxSocketImpl *socket_, |
371 | wxSocketNotify WXUNUSED(event)) | |
b7ceceb1 | 372 | { |
51fe4b60 VZ |
373 | wxSocketImplMSW * const socket = static_cast<wxSocketImplMSW *>(socket_); |
374 | ||
b7ceceb1 DE |
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); | |
dbfb61b3 | 382 | #ifndef __WXWINCE__ |
1c8681b4 | 383 | gs_WSAAsyncSelect(socket->m_fd, hWin, socket->m_msgnumber, lEvent); |
dbfb61b3 JS |
384 | #else |
385 | /* | |
386 | * WinCE creates a thread for socket event handling. | |
1c6dd11c | 387 | * All needed parameters get passed through the thread_data structure. |
dbfb61b3 | 388 | */ |
84aa68c4 | 389 | |
7ec69821 WS |
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); | |
dbfb61b3 | 397 | #endif |
b7ceceb1 DE |
398 | } |
399 | } | |
400 | ||
f0fbbe23 VZ |
401 | /* |
402 | * Disable event notifications (used when shutting down the socket) | |
b7ceceb1 | 403 | */ |
51fe4b60 VZ |
404 | void wxSocketMSWManager::Uninstall_Callback(wxSocketImpl *socket_, |
405 | wxSocketNotify WXUNUSED(event)) | |
b7ceceb1 | 406 | { |
51fe4b60 VZ |
407 | wxSocketImplMSW * const socket = static_cast<wxSocketImplMSW *>(socket_); |
408 | ||
b7ceceb1 DE |
409 | if (socket->m_fd != INVALID_SOCKET) |
410 | { | |
dbfb61b3 | 411 | #ifndef __WXWINCE__ |
1c8681b4 | 412 | gs_WSAAsyncSelect(socket->m_fd, hWin, socket->m_msgnumber, 0); |
dbfb61b3 | 413 | #else |
7ec69821 WS |
414 | //Destroy the thread |
415 | socketHash[socket->m_fd] = false; | |
dbfb61b3 | 416 | #endif |
b7ceceb1 DE |
417 | } |
418 | } | |
419 | ||
51fe4b60 | 420 | // set the wxBase variable to point to our wxSocketManager implementation |
2804f77d | 421 | // |
51fe4b60 | 422 | // see comments in wx/apptrait.h for the explanation of why do we do it |
2804f77d VZ |
423 | // like this |
424 | static struct ManagerSetter | |
425 | { | |
426 | ManagerSetter() | |
427 | { | |
51fe4b60 | 428 | static wxSocketMSWManager s_manager; |
2804f77d VZ |
429 | wxAppTraits::SetDefaultSocketManager(&s_manager); |
430 | } | |
a3cf8dca | 431 | } gs_managerSetter; |
b7ceceb1 | 432 | |
116de991 VZ |
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 | /* | |
452 | * Waits for an incoming client connection. Returns a pointer to | |
453 | * a wxSocketImpl object, or NULL if there was an error, in which case | |
454 | * the last error field will be updated for the calling wxSocketImpl. | |
455 | * | |
456 | * Error codes (set in the calling wxSocketImpl) | |
457 | * wxSOCKET_INVSOCK - the socket is not valid or not a server. | |
458 | * wxSOCKET_TIMEDOUT - timeout, no incoming connections. | |
459 | * wxSOCKET_WOULDBLOCK - the call would block and the socket is nonblocking. | |
460 | * wxSOCKET_MEMERR - couldn't allocate memory. | |
461 | * wxSOCKET_IOERR - low-level error. | |
462 | */ | |
463 | wxSocketImpl *wxSocketImplMSW::WaitConnection(wxSocketBase& wxsocket) | |
464 | { | |
465 | wxSocketImpl *connection; | |
466 | wxSockAddr from; | |
467 | WX_SOCKLEN_T fromlen = sizeof(from); | |
468 | wxSocketError err; | |
469 | u_long arg = 1; | |
470 | ||
471 | /* Reenable CONNECTION events */ | |
472 | m_detected &= ~wxSOCKET_CONNECTION_FLAG; | |
473 | ||
474 | /* If the socket has already been created, we exit immediately */ | |
475 | if (m_fd == INVALID_SOCKET || !m_server) | |
476 | { | |
477 | m_error = wxSOCKET_INVSOCK; | |
478 | return NULL; | |
479 | } | |
480 | ||
481 | /* Create a wxSocketImpl object for the new connection */ | |
482 | connection = wxSocketImplMSW::Create(wxsocket); | |
483 | ||
484 | if (!connection) | |
485 | { | |
486 | m_error = wxSOCKET_MEMERR; | |
487 | return NULL; | |
488 | } | |
489 | ||
490 | /* Wait for a connection (with timeout) */ | |
c6b10632 | 491 | if ( !BlockForInputWithTimeout() ) |
116de991 VZ |
492 | { |
493 | delete connection; | |
116de991 VZ |
494 | return NULL; |
495 | } | |
496 | ||
497 | connection->m_fd = accept(m_fd, (sockaddr*)&from, &fromlen); | |
498 | ||
499 | if (connection->m_fd == INVALID_SOCKET) | |
500 | { | |
501 | if (WSAGetLastError() == WSAEWOULDBLOCK) | |
502 | m_error = wxSOCKET_WOULDBLOCK; | |
503 | else | |
504 | m_error = wxSOCKET_IOERR; | |
505 | ||
506 | delete connection; | |
507 | return NULL; | |
508 | } | |
509 | ||
510 | /* Initialize all fields */ | |
511 | connection->m_server = false; | |
512 | connection->m_stream = true; | |
513 | ||
514 | /* Setup the peer address field */ | |
515 | connection->m_peer = GAddress_new(); | |
516 | if (!connection->m_peer) | |
517 | { | |
518 | delete connection; | |
519 | m_error = wxSOCKET_MEMERR; | |
520 | return NULL; | |
521 | } | |
522 | err = _GAddress_translate_from(connection->m_peer, (sockaddr*)&from, fromlen); | |
523 | if (err != wxSOCKET_NOERROR) | |
524 | { | |
525 | GAddress_destroy(connection->m_peer); | |
526 | delete connection; | |
527 | m_error = err; | |
528 | return NULL; | |
529 | } | |
530 | ||
531 | ioctlsocket(connection->m_fd, FIONBIO, (u_long FAR *) &arg); | |
532 | wxSocketManager::Get()->Install_Callback(connection); | |
533 | ||
534 | return connection; | |
535 | } | |
536 | ||
537 | wxSocketError wxSocketImplMSW::DoHandleConnect(int ret) | |
538 | { | |
539 | // TODO: review this | |
540 | if (ret == SOCKET_ERROR) | |
541 | { | |
542 | int err = WSAGetLastError(); | |
543 | ||
544 | /* If connect failed with EWOULDBLOCK and the wxSocketImpl object | |
545 | * is in blocking mode, we select() for the specified timeout | |
546 | * checking for writability to see if the connection request | |
547 | * completes. | |
548 | */ | |
549 | if ((err == WSAEWOULDBLOCK) && (!m_non_blocking)) | |
550 | { | |
551 | err = Connect_Timeout(); | |
552 | ||
553 | if (err != wxSOCKET_NOERROR) | |
554 | { | |
555 | Close(); | |
556 | /* m_error is set in Connect_Timeout */ | |
557 | } | |
558 | ||
559 | return (wxSocketError) err; | |
560 | } | |
561 | ||
562 | /* If connect failed with EWOULDBLOCK and the wxSocketImpl object | |
563 | * is set to nonblocking, we set m_error to wxSOCKET_WOULDBLOCK | |
564 | * (and return wxSOCKET_WOULDBLOCK) but we don't close the socket; | |
565 | * this way if the connection completes, a wxSOCKET_CONNECTION | |
566 | * event will be generated, if enabled. | |
567 | */ | |
568 | if ((err == WSAEWOULDBLOCK) && (m_non_blocking)) | |
569 | { | |
570 | m_establishing = true; | |
571 | m_error = wxSOCKET_WOULDBLOCK; | |
572 | return wxSOCKET_WOULDBLOCK; | |
573 | } | |
574 | ||
575 | /* If connect failed with an error other than EWOULDBLOCK, | |
576 | * then the call to Connect() has failed. | |
577 | */ | |
578 | Close(); | |
579 | m_error = wxSOCKET_IOERR; | |
580 | return wxSOCKET_IOERR; | |
581 | } | |
582 | ||
583 | return wxSOCKET_NOERROR; | |
584 | } | |
585 | ||
586 | /* Generic IO */ | |
587 | ||
588 | /* Like recv(), send(), ... */ | |
589 | int wxSocketImplMSW::Read(void *buffer, int size) | |
590 | { | |
591 | int ret; | |
592 | ||
593 | /* Reenable INPUT events */ | |
594 | m_detected &= ~wxSOCKET_INPUT_FLAG; | |
595 | ||
596 | if (m_fd == INVALID_SOCKET || m_server) | |
597 | { | |
598 | m_error = wxSOCKET_INVSOCK; | |
599 | return -1; | |
600 | } | |
601 | ||
602 | /* If the socket is blocking, wait for data (with a timeout) */ | |
c6b10632 | 603 | if ( !BlockForInputWithTimeout() ) |
116de991 | 604 | { |
116de991 VZ |
605 | return -1; |
606 | } | |
607 | ||
608 | /* Read the data */ | |
609 | if (m_stream) | |
610 | ret = Recv_Stream(buffer, size); | |
611 | else | |
612 | ret = Recv_Dgram(buffer, size); | |
613 | ||
614 | if (ret == SOCKET_ERROR) | |
615 | { | |
616 | if (WSAGetLastError() != WSAEWOULDBLOCK) | |
617 | m_error = wxSOCKET_IOERR; | |
618 | else | |
619 | m_error = wxSOCKET_WOULDBLOCK; | |
620 | return -1; | |
621 | } | |
622 | ||
623 | return ret; | |
624 | } | |
625 | ||
626 | int wxSocketImplMSW::Write(const void *buffer, int size) | |
627 | { | |
628 | int ret; | |
629 | ||
630 | if (m_fd == INVALID_SOCKET || m_server) | |
631 | { | |
632 | m_error = wxSOCKET_INVSOCK; | |
633 | return -1; | |
634 | } | |
635 | ||
636 | /* If the socket is blocking, wait for writability (with a timeout) */ | |
74a33427 | 637 | if ( !BlockForOutputWithTimeout() ) |
116de991 VZ |
638 | return -1; |
639 | ||
640 | /* Write the data */ | |
641 | if (m_stream) | |
642 | ret = Send_Stream(buffer, size); | |
643 | else | |
644 | ret = Send_Dgram(buffer, size); | |
645 | ||
646 | if (ret == SOCKET_ERROR) | |
647 | { | |
648 | if (WSAGetLastError() != WSAEWOULDBLOCK) | |
649 | m_error = wxSOCKET_IOERR; | |
650 | else | |
651 | m_error = wxSOCKET_WOULDBLOCK; | |
652 | ||
653 | /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect | |
654 | * does). Once the first OUTPUT event is received, users can assume | |
655 | * that the socket is writable until a read operation fails. Only then | |
656 | * will further OUTPUT events be posted. | |
657 | */ | |
658 | m_detected &= ~wxSOCKET_OUTPUT_FLAG; | |
659 | return -1; | |
660 | } | |
661 | ||
662 | return ret; | |
663 | } | |
664 | ||
665 | /* Internals (IO) */ | |
666 | ||
116de991 VZ |
667 | /* |
668 | * For blocking sockets, wait until the connection is | |
669 | * established or fails, or until timeout ellapses. | |
670 | */ | |
671 | wxSocketError wxSocketImplMSW::Connect_Timeout() | |
672 | { | |
673 | fd_set writefds; | |
674 | fd_set exceptfds; | |
675 | ||
676 | FD_ZERO(&writefds); | |
677 | FD_ZERO(&exceptfds); | |
678 | FD_SET(m_fd, &writefds); | |
679 | FD_SET(m_fd, &exceptfds); | |
680 | if (select(0, NULL, &writefds, &exceptfds, &m_timeout) == 0) | |
681 | { | |
682 | m_error = wxSOCKET_TIMEDOUT; | |
683 | return wxSOCKET_TIMEDOUT; | |
684 | } | |
685 | if (!FD_ISSET(m_fd, &writefds)) | |
686 | { | |
687 | m_error = wxSOCKET_IOERR; | |
688 | return wxSOCKET_IOERR; | |
689 | } | |
690 | ||
691 | return wxSOCKET_NOERROR; | |
692 | } | |
693 | ||
694 | int wxSocketImplMSW::Recv_Stream(void *buffer, int size) | |
695 | { | |
696 | return recv(m_fd, static_cast<char *>(buffer), size, 0); | |
697 | } | |
698 | ||
699 | int wxSocketImplMSW::Recv_Dgram(void *buffer, int size) | |
700 | { | |
701 | wxSockAddr from; | |
702 | WX_SOCKLEN_T fromlen = sizeof(from); | |
703 | int ret; | |
704 | wxSocketError err; | |
705 | ||
706 | ret = recvfrom(m_fd, static_cast<char *>(buffer), | |
707 | size, 0, &from, &fromlen); | |
708 | ||
709 | if (ret == SOCKET_ERROR) | |
710 | return SOCKET_ERROR; | |
711 | ||
712 | /* Translate a system address into a wxSocketImpl address */ | |
713 | if (!m_peer) | |
714 | { | |
715 | m_peer = GAddress_new(); | |
716 | if (!m_peer) | |
717 | { | |
718 | m_error = wxSOCKET_MEMERR; | |
719 | return -1; | |
720 | } | |
721 | } | |
722 | err = _GAddress_translate_from(m_peer, (sockaddr*)&from, fromlen); | |
723 | if (err != wxSOCKET_NOERROR) | |
724 | { | |
725 | GAddress_destroy(m_peer); | |
726 | m_peer = NULL; | |
727 | m_error = err; | |
728 | return -1; | |
729 | } | |
730 | ||
731 | return ret; | |
732 | } | |
733 | ||
734 | int wxSocketImplMSW::Send_Stream(const void *buffer, int size) | |
735 | { | |
736 | return send(m_fd, static_cast<const char *>(buffer), size, 0); | |
737 | } | |
738 | ||
739 | int wxSocketImplMSW::Send_Dgram(const void *buffer, int size) | |
740 | { | |
741 | struct sockaddr *addr; | |
742 | int len, ret; | |
743 | wxSocketError err; | |
744 | ||
745 | if (!m_peer) | |
746 | { | |
747 | m_error = wxSOCKET_INVADDR; | |
748 | return -1; | |
749 | } | |
750 | ||
751 | err = _GAddress_translate_to(m_peer, &addr, &len); | |
752 | if (err != wxSOCKET_NOERROR) | |
753 | { | |
754 | m_error = err; | |
755 | return -1; | |
756 | } | |
757 | ||
758 | ret = sendto(m_fd, static_cast<const char *>(buffer), size, 0, addr, len); | |
759 | ||
760 | /* Frees memory allocated by _GAddress_translate_to */ | |
761 | free(addr); | |
762 | ||
763 | return ret; | |
764 | } | |
765 | ||
766 | /* | |
767 | * ------------------------------------------------------------------------- | |
768 | * GAddress | |
769 | * ------------------------------------------------------------------------- | |
770 | */ | |
771 | ||
772 | /* CHECK_ADDRESS verifies that the current address family is either | |
773 | * wxSOCKET_NOFAMILY or wxSOCKET_*family*, and if it is wxSOCKET_NOFAMILY, it | |
774 | * initalizes it to be a wxSOCKET_*family*. In other cases, it returns | |
775 | * an appropiate error code. | |
776 | * | |
777 | * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error. | |
778 | */ | |
779 | #define CHECK_ADDRESS(address, family) \ | |
780 | { \ | |
781 | if (address->m_family == wxSOCKET_NOFAMILY) \ | |
782 | if (_GAddress_Init_##family(address) != wxSOCKET_NOERROR) \ | |
783 | return address->m_error; \ | |
784 | if (address->m_family != wxSOCKET_##family) \ | |
785 | { \ | |
786 | address->m_error = wxSOCKET_INVADDR; \ | |
787 | return wxSOCKET_INVADDR; \ | |
788 | } \ | |
789 | } | |
790 | ||
791 | #define CHECK_ADDRESS_RETVAL(address, family, retval) \ | |
792 | { \ | |
793 | if (address->m_family == wxSOCKET_NOFAMILY) \ | |
794 | if (_GAddress_Init_##family(address) != wxSOCKET_NOERROR) \ | |
795 | return retval; \ | |
796 | if (address->m_family != wxSOCKET_##family) \ | |
797 | { \ | |
798 | address->m_error = wxSOCKET_INVADDR; \ | |
799 | return retval; \ | |
800 | } \ | |
801 | } | |
802 | ||
803 | ||
804 | GAddress *GAddress_new() | |
805 | { | |
806 | GAddress *address; | |
807 | ||
808 | if ((address = (GAddress *) malloc(sizeof(GAddress))) == NULL) | |
809 | return NULL; | |
810 | ||
811 | address->m_family = wxSOCKET_NOFAMILY; | |
812 | address->m_addr = NULL; | |
813 | address->m_len = 0; | |
814 | ||
815 | return address; | |
816 | } | |
817 | ||
818 | GAddress *GAddress_copy(GAddress *address) | |
819 | { | |
820 | GAddress *addr2; | |
821 | ||
822 | if ((addr2 = (GAddress *) malloc(sizeof(GAddress))) == NULL) | |
823 | return NULL; | |
824 | ||
825 | memcpy(addr2, address, sizeof(GAddress)); | |
826 | ||
827 | if (address->m_addr) | |
828 | { | |
829 | addr2->m_addr = (struct sockaddr *) malloc(addr2->m_len); | |
830 | if (addr2->m_addr == NULL) | |
831 | { | |
832 | free(addr2); | |
833 | return NULL; | |
834 | } | |
835 | memcpy(addr2->m_addr, address->m_addr, addr2->m_len); | |
836 | } | |
837 | ||
838 | return addr2; | |
839 | } | |
840 | ||
841 | void GAddress_destroy(GAddress *address) | |
842 | { | |
843 | if (address->m_addr) | |
844 | free(address->m_addr); | |
845 | ||
846 | free(address); | |
847 | } | |
848 | ||
849 | void GAddress_SetFamily(GAddress *address, GAddressType type) | |
850 | { | |
851 | address->m_family = type; | |
852 | } | |
853 | ||
854 | GAddressType GAddress_GetFamily(GAddress *address) | |
855 | { | |
856 | return address->m_family; | |
857 | } | |
858 | ||
859 | wxSocketError _GAddress_translate_from(GAddress *address, | |
860 | struct sockaddr *addr, int len) | |
861 | { | |
862 | address->m_realfamily = addr->sa_family; | |
863 | switch (addr->sa_family) | |
864 | { | |
865 | case AF_INET: | |
866 | address->m_family = wxSOCKET_INET; | |
867 | break; | |
868 | case AF_UNIX: | |
869 | address->m_family = wxSOCKET_UNIX; | |
870 | break; | |
871 | #if wxUSE_IPV6 | |
872 | case AF_INET6: | |
873 | address->m_family = wxSOCKET_INET6; | |
874 | break; | |
875 | #endif | |
876 | default: | |
877 | { | |
878 | address->m_error = wxSOCKET_INVOP; | |
879 | return wxSOCKET_INVOP; | |
880 | } | |
881 | } | |
882 | ||
883 | if (address->m_addr) | |
884 | free(address->m_addr); | |
885 | ||
886 | address->m_len = len; | |
887 | address->m_addr = (struct sockaddr *) malloc(len); | |
888 | ||
889 | if (address->m_addr == NULL) | |
890 | { | |
891 | address->m_error = wxSOCKET_MEMERR; | |
892 | return wxSOCKET_MEMERR; | |
893 | } | |
894 | memcpy(address->m_addr, addr, len); | |
895 | ||
896 | return wxSOCKET_NOERROR; | |
897 | } | |
898 | ||
899 | wxSocketError _GAddress_translate_to(GAddress *address, | |
900 | struct sockaddr **addr, int *len) | |
901 | { | |
902 | if (!address->m_addr) | |
903 | { | |
904 | address->m_error = wxSOCKET_INVADDR; | |
905 | return wxSOCKET_INVADDR; | |
906 | } | |
907 | ||
908 | *len = address->m_len; | |
909 | *addr = (struct sockaddr *) malloc(address->m_len); | |
910 | if (*addr == NULL) | |
911 | { | |
912 | address->m_error = wxSOCKET_MEMERR; | |
913 | return wxSOCKET_MEMERR; | |
914 | } | |
915 | ||
916 | memcpy(*addr, address->m_addr, address->m_len); | |
917 | return wxSOCKET_NOERROR; | |
918 | } | |
919 | ||
920 | /* | |
921 | * ------------------------------------------------------------------------- | |
922 | * Internet address family | |
923 | * ------------------------------------------------------------------------- | |
924 | */ | |
925 | ||
926 | wxSocketError _GAddress_Init_INET(GAddress *address) | |
927 | { | |
928 | address->m_len = sizeof(struct sockaddr_in); | |
929 | address->m_addr = (struct sockaddr *) malloc(address->m_len); | |
930 | if (address->m_addr == NULL) | |
931 | { | |
932 | address->m_error = wxSOCKET_MEMERR; | |
933 | return wxSOCKET_MEMERR; | |
934 | } | |
935 | ||
936 | address->m_family = wxSOCKET_INET; | |
937 | address->m_realfamily = AF_INET; | |
938 | ((struct sockaddr_in *)address->m_addr)->sin_family = AF_INET; | |
939 | ((struct sockaddr_in *)address->m_addr)->sin_addr.s_addr = INADDR_ANY; | |
940 | ||
941 | return wxSOCKET_NOERROR; | |
942 | } | |
943 | ||
944 | wxSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname) | |
945 | { | |
946 | struct hostent *he; | |
947 | struct in_addr *addr; | |
948 | ||
949 | CHECK_ADDRESS(address, INET); | |
950 | ||
951 | addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr); | |
952 | ||
953 | addr->s_addr = inet_addr(hostname); | |
954 | ||
955 | /* If it is a numeric host name, convert it now */ | |
956 | if (addr->s_addr == INADDR_NONE) | |
957 | { | |
958 | struct in_addr *array_addr; | |
959 | ||
960 | /* It is a real name, we solve it */ | |
961 | if ((he = gethostbyname(hostname)) == NULL) | |
962 | { | |
963 | /* addr->s_addr = INADDR_NONE just done by inet_addr() above */ | |
964 | address->m_error = wxSOCKET_NOHOST; | |
965 | return wxSOCKET_NOHOST; | |
966 | } | |
967 | array_addr = (struct in_addr *) *(he->h_addr_list); | |
968 | addr->s_addr = array_addr[0].s_addr; | |
969 | } | |
970 | return wxSOCKET_NOERROR; | |
971 | } | |
972 | ||
973 | wxSocketError GAddress_INET_SetBroadcastAddress(GAddress *address) | |
974 | { | |
975 | return GAddress_INET_SetHostAddress(address, INADDR_BROADCAST); | |
976 | } | |
977 | ||
978 | wxSocketError GAddress_INET_SetAnyAddress(GAddress *address) | |
979 | { | |
980 | return GAddress_INET_SetHostAddress(address, INADDR_ANY); | |
981 | } | |
982 | ||
983 | wxSocketError GAddress_INET_SetHostAddress(GAddress *address, | |
984 | unsigned long hostaddr) | |
985 | { | |
986 | struct in_addr *addr; | |
987 | ||
988 | CHECK_ADDRESS(address, INET); | |
989 | ||
990 | addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr); | |
991 | addr->s_addr = htonl(hostaddr); | |
992 | ||
993 | return wxSOCKET_NOERROR; | |
994 | } | |
995 | ||
996 | wxSocketError GAddress_INET_SetPortName(GAddress *address, const char *port, | |
997 | const char *protocol) | |
998 | { | |
999 | struct servent *se; | |
1000 | struct sockaddr_in *addr; | |
1001 | ||
1002 | CHECK_ADDRESS(address, INET); | |
1003 | ||
1004 | if (!port) | |
1005 | { | |
1006 | address->m_error = wxSOCKET_INVPORT; | |
1007 | return wxSOCKET_INVPORT; | |
1008 | } | |
1009 | ||
1010 | se = getservbyname(port, protocol); | |
1011 | if (!se) | |
1012 | { | |
1013 | if (isdigit(port[0])) | |
1014 | { | |
1015 | int port_int; | |
1016 | ||
1017 | port_int = atoi(port); | |
1018 | addr = (struct sockaddr_in *)address->m_addr; | |
1019 | addr->sin_port = htons((u_short) port_int); | |
1020 | return wxSOCKET_NOERROR; | |
1021 | } | |
1022 | ||
1023 | address->m_error = wxSOCKET_INVPORT; | |
1024 | return wxSOCKET_INVPORT; | |
1025 | } | |
1026 | ||
1027 | addr = (struct sockaddr_in *)address->m_addr; | |
1028 | addr->sin_port = se->s_port; | |
1029 | ||
1030 | return wxSOCKET_NOERROR; | |
1031 | } | |
1032 | ||
1033 | wxSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port) | |
1034 | { | |
1035 | struct sockaddr_in *addr; | |
1036 | ||
1037 | CHECK_ADDRESS(address, INET); | |
1038 | ||
1039 | addr = (struct sockaddr_in *)address->m_addr; | |
1040 | addr->sin_port = htons(port); | |
1041 | ||
1042 | return wxSOCKET_NOERROR; | |
1043 | } | |
1044 | ||
1045 | wxSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname, size_t sbuf) | |
1046 | { | |
1047 | struct hostent *he; | |
1048 | char *addr_buf; | |
1049 | struct sockaddr_in *addr; | |
1050 | ||
1051 | CHECK_ADDRESS(address, INET); | |
1052 | ||
1053 | addr = (struct sockaddr_in *)address->m_addr; | |
1054 | addr_buf = (char *)&(addr->sin_addr); | |
1055 | ||
1056 | he = gethostbyaddr(addr_buf, sizeof(addr->sin_addr), AF_INET); | |
1057 | if (he == NULL) | |
1058 | { | |
1059 | address->m_error = wxSOCKET_NOHOST; | |
1060 | return wxSOCKET_NOHOST; | |
1061 | } | |
1062 | ||
1063 | strncpy(hostname, he->h_name, sbuf); | |
1064 | ||
1065 | return wxSOCKET_NOERROR; | |
1066 | } | |
1067 | ||
1068 | unsigned long GAddress_INET_GetHostAddress(GAddress *address) | |
1069 | { | |
1070 | struct sockaddr_in *addr; | |
1071 | ||
1072 | CHECK_ADDRESS_RETVAL(address, INET, 0); | |
1073 | ||
1074 | addr = (struct sockaddr_in *)address->m_addr; | |
1075 | ||
1076 | return ntohl(addr->sin_addr.s_addr); | |
1077 | } | |
1078 | ||
1079 | unsigned short GAddress_INET_GetPort(GAddress *address) | |
1080 | { | |
1081 | struct sockaddr_in *addr; | |
1082 | ||
1083 | CHECK_ADDRESS_RETVAL(address, INET, 0); | |
1084 | ||
1085 | addr = (struct sockaddr_in *)address->m_addr; | |
1086 | return ntohs(addr->sin_port); | |
1087 | } | |
1088 | ||
1089 | ||
1090 | #if wxUSE_IPV6 | |
1091 | /* | |
1092 | * ------------------------------------------------------------------------- | |
1093 | * Internet IPv6 address family | |
1094 | * ------------------------------------------------------------------------- | |
1095 | */ | |
1096 | #include "ws2tcpip.h" | |
1097 | ||
1098 | #ifdef __VISUALC__ | |
1099 | #pragma comment(lib,"ws2_32") | |
1100 | #endif // __VISUALC__ | |
1101 | ||
1102 | wxSocketError _GAddress_Init_INET6(GAddress *address) | |
1103 | { | |
1104 | struct in6_addr any_address = IN6ADDR_ANY_INIT; | |
1105 | address->m_len = sizeof(struct sockaddr_in6); | |
1106 | address->m_addr = (struct sockaddr *) malloc(address->m_len); | |
1107 | if (address->m_addr == NULL) | |
1108 | { | |
1109 | address->m_error = wxSOCKET_MEMERR; | |
1110 | return wxSOCKET_MEMERR; | |
1111 | } | |
1112 | memset(address->m_addr,0,address->m_len); | |
1113 | ||
1114 | address->m_family = wxSOCKET_INET6; | |
1115 | address->m_realfamily = AF_INET6; | |
1116 | ((struct sockaddr_in6 *)address->m_addr)->sin6_family = AF_INET6; | |
1117 | ((struct sockaddr_in6 *)address->m_addr)->sin6_addr = any_address; | |
1118 | ||
1119 | return wxSOCKET_NOERROR; | |
1120 | } | |
1121 | ||
1122 | wxSocketError GAddress_INET6_SetHostName(GAddress *address, const char *hostname) | |
1123 | { | |
1124 | CHECK_ADDRESS(address, INET6); | |
1125 | ||
1126 | addrinfo hints; | |
1127 | memset( & hints, 0, sizeof( hints ) ); | |
1128 | hints.ai_family = AF_INET6; | |
1129 | addrinfo * info = 0; | |
1130 | if ( getaddrinfo( hostname, "0", & hints, & info ) || ! info ) | |
1131 | { | |
1132 | address->m_error = wxSOCKET_NOHOST; | |
1133 | return wxSOCKET_NOHOST; | |
1134 | } | |
1135 | ||
1136 | memcpy( address->m_addr, info->ai_addr, info->ai_addrlen ); | |
1137 | freeaddrinfo( info ); | |
1138 | return wxSOCKET_NOERROR; | |
1139 | } | |
1140 | ||
1141 | wxSocketError GAddress_INET6_SetAnyAddress(GAddress *address) | |
1142 | { | |
1143 | CHECK_ADDRESS(address, INET6); | |
1144 | ||
1145 | struct in6_addr addr; | |
1146 | memset( & addr, 0, sizeof( addr ) ); | |
1147 | return GAddress_INET6_SetHostAddress(address, addr); | |
1148 | } | |
1149 | wxSocketError GAddress_INET6_SetHostAddress(GAddress *address, | |
1150 | struct in6_addr hostaddr) | |
1151 | { | |
1152 | CHECK_ADDRESS(address, INET6); | |
1153 | ||
1154 | ((struct sockaddr_in6 *)address->m_addr)->sin6_addr = hostaddr; | |
1155 | ||
1156 | return wxSOCKET_NOERROR; | |
1157 | } | |
1158 | ||
1159 | wxSocketError GAddress_INET6_SetPortName(GAddress *address, const char *port, | |
1160 | const char *protocol) | |
1161 | { | |
1162 | struct servent *se; | |
1163 | struct sockaddr_in6 *addr; | |
1164 | ||
1165 | CHECK_ADDRESS(address, INET6); | |
1166 | ||
1167 | if (!port) | |
1168 | { | |
1169 | address->m_error = wxSOCKET_INVPORT; | |
1170 | return wxSOCKET_INVPORT; | |
1171 | } | |
1172 | ||
1173 | se = getservbyname(port, protocol); | |
1174 | if (!se) | |
1175 | { | |
1176 | if (isdigit((unsigned char) port[0])) | |
1177 | { | |
1178 | int port_int; | |
1179 | ||
1180 | port_int = atoi(port); | |
1181 | addr = (struct sockaddr_in6 *)address->m_addr; | |
1182 | addr->sin6_port = htons((u_short) port_int); | |
1183 | return wxSOCKET_NOERROR; | |
1184 | } | |
1185 | ||
1186 | address->m_error = wxSOCKET_INVPORT; | |
1187 | return wxSOCKET_INVPORT; | |
1188 | } | |
1189 | ||
1190 | addr = (struct sockaddr_in6 *)address->m_addr; | |
1191 | addr->sin6_port = se->s_port; | |
1192 | ||
1193 | return wxSOCKET_NOERROR; | |
1194 | } | |
1195 | ||
1196 | wxSocketError GAddress_INET6_SetPort(GAddress *address, unsigned short port) | |
1197 | { | |
1198 | struct sockaddr_in6 *addr; | |
1199 | ||
1200 | CHECK_ADDRESS(address, INET6); | |
1201 | ||
1202 | addr = (struct sockaddr_in6 *)address->m_addr; | |
1203 | addr->sin6_port = htons(port); | |
1204 | ||
1205 | return wxSOCKET_NOERROR; | |
1206 | } | |
1207 | ||
1208 | wxSocketError GAddress_INET6_GetHostName(GAddress *address, char *hostname, size_t sbuf) | |
1209 | { | |
1210 | struct hostent *he; | |
1211 | char *addr_buf; | |
1212 | struct sockaddr_in6 *addr; | |
1213 | ||
1214 | CHECK_ADDRESS(address, INET6); | |
1215 | ||
1216 | addr = (struct sockaddr_in6 *)address->m_addr; | |
1217 | addr_buf = (char *)&(addr->sin6_addr); | |
1218 | ||
1219 | he = gethostbyaddr(addr_buf, sizeof(addr->sin6_addr), AF_INET6); | |
1220 | if (he == NULL) | |
1221 | { | |
1222 | address->m_error = wxSOCKET_NOHOST; | |
1223 | return wxSOCKET_NOHOST; | |
1224 | } | |
1225 | ||
1226 | strncpy(hostname, he->h_name, sbuf); | |
1227 | ||
1228 | return wxSOCKET_NOERROR; | |
1229 | } | |
1230 | ||
1231 | wxSocketError GAddress_INET6_GetHostAddress(GAddress *address,struct in6_addr *hostaddr) | |
1232 | { | |
1233 | CHECK_ADDRESS_RETVAL(address, INET6, wxSOCKET_INVADDR); | |
1234 | *hostaddr = ( (struct sockaddr_in6 *)address->m_addr )->sin6_addr; | |
1235 | return wxSOCKET_NOERROR; | |
1236 | } | |
1237 | ||
1238 | unsigned short GAddress_INET6_GetPort(GAddress *address) | |
1239 | { | |
1240 | CHECK_ADDRESS_RETVAL(address, INET6, 0); | |
1241 | ||
1242 | return ntohs( ((struct sockaddr_in6 *)address->m_addr)->sin6_port ); | |
1243 | } | |
1244 | ||
1245 | #endif // wxUSE_IPV6 | |
1246 | ||
1247 | /* | |
1248 | * ------------------------------------------------------------------------- | |
1249 | * Unix address family | |
1250 | * ------------------------------------------------------------------------- | |
1251 | */ | |
1252 | ||
1253 | wxSocketError _GAddress_Init_UNIX(GAddress *address) | |
1254 | { | |
1255 | address->m_error = wxSOCKET_INVADDR; | |
1256 | return wxSOCKET_INVADDR; | |
1257 | } | |
1258 | ||
1259 | wxSocketError GAddress_UNIX_SetPath(GAddress *address, const char *WXUNUSED(path)) | |
1260 | { | |
1261 | address->m_error = wxSOCKET_INVADDR; | |
1262 | return wxSOCKET_INVADDR; | |
1263 | } | |
1264 | ||
1265 | wxSocketError GAddress_UNIX_GetPath(GAddress *address, char *WXUNUSED(path), size_t WXUNUSED(sbuf)) | |
1266 | { | |
1267 | address->m_error = wxSOCKET_INVADDR; | |
1268 | return wxSOCKET_INVADDR; | |
1269 | } | |
1270 | ||
2804f77d | 1271 | #endif // wxUSE_SOCKETS |