]>
Commit | Line | Data |
---|---|---|
f7ff39c6 | 1 | /* ------------------------------------------------------------------------- |
99d80019 | 2 | * Project: GSocket (Generic Socket) |
521bf4ff | 3 | * Name: src/msw/gsocket.cpp |
99d80019 JS |
4 | * Copyright: (c) Guilhem Lavaux |
5 | * Licence: wxWindows Licence | |
6 | * Author: Guillermo Rodriguez Garcia <guille@iies.es> | |
7 | * Purpose: GSocket main MSW file | |
8 | * Licence: The wxWindows licence | |
9 | * CVSID: $Id$ | |
f7ff39c6 DE |
10 | * ------------------------------------------------------------------------- |
11 | */ | |
12 | ||
86afa786 VS |
13 | // For compilers that support precompilation, includes "wx.h". |
14 | #include "wx/wxprec.h" | |
15 | ||
16 | #ifdef __BORLANDC__ | |
17 | #pragma hdrstop | |
18 | #endif | |
19 | ||
f7ff39c6 DE |
20 | #ifdef _MSC_VER |
21 | /* RPCNOTIFICATION_ROUTINE in rasasync.h (included from winsock.h), | |
22 | * warning: conditional expression is constant. | |
23 | */ | |
24 | # pragma warning(disable:4115) | |
25 | /* FD_SET, | |
26 | * warning: named type definition in parentheses. | |
27 | */ | |
28 | # pragma warning(disable:4127) | |
29 | /* GAddress_UNIX_GetPath, | |
30 | * warning: unreferenced formal parameter. | |
31 | */ | |
32 | # pragma warning(disable:4100) | |
33 | ||
34 | #ifdef __WXWINCE__ | |
35 | /* windows.h results in tons of warnings at max warning level */ | |
36 | # ifdef _MSC_VER | |
37 | # pragma warning(push, 1) | |
38 | # endif | |
39 | # include <windows.h> | |
40 | # ifdef _MSC_VER | |
41 | # pragma warning(pop) | |
42 | # pragma warning(disable:4514) | |
43 | # endif | |
44 | #endif | |
45 | ||
46 | #endif /* _MSC_VER */ | |
47 | ||
f6afe7fd RN |
48 | #if defined(__CYGWIN__) |
49 | //CYGWIN gives annoying warning about runtime stuff if we don't do this | |
50 | # define USE_SYS_TYPES_FD_SET | |
51 | # include <sys/types.h> | |
52 | #endif | |
53 | ||
f7ff39c6 DE |
54 | #include <winsock.h> |
55 | ||
56 | #ifndef __GSOCKET_STANDALONE__ | |
57 | # include "wx/platform.h" | |
f7ff39c6 DE |
58 | #endif |
59 | ||
60 | #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) | |
61 | ||
62 | #ifndef __GSOCKET_STANDALONE__ | |
63 | # include "wx/msw/gsockmsw.h" | |
64 | # include "wx/gsocket.h" | |
65 | #else | |
66 | # include "gsockmsw.h" | |
67 | # include "gsocket.h" | |
68 | #endif /* __GSOCKET_STANDALONE__ */ | |
69 | ||
70 | #ifndef __WXWINCE__ | |
71 | #include <assert.h> | |
72 | #else | |
73 | #define assert(x) | |
74 | #ifndef isdigit | |
75 | #define isdigit(x) (x > 47 && x < 58) | |
76 | #endif | |
77 | #include "wx/msw/wince/net.h" | |
78 | #endif | |
79 | ||
80 | #include <string.h> | |
81 | #include <stdio.h> | |
82 | #include <stdlib.h> | |
83 | #include <stddef.h> | |
84 | #include <ctype.h> | |
85 | ||
9e03e02d SN |
86 | /* if we use configure for MSW WX_SOCKLEN_T will be already defined */ |
87 | #ifndef WX_SOCKLEN_T | |
88 | # define WX_SOCKLEN_T int | |
f7ff39c6 DE |
89 | #endif |
90 | ||
91 | /* Table of GUI-related functions. We must call them indirectly because | |
92 | * of wxBase and GUI separation: */ | |
93 | ||
3e1400ac | 94 | static GSocketGUIFunctionsTable *gs_gui_functions; |
f7ff39c6 | 95 | |
a4506848 DE |
96 | class GSocketGUIFunctionsTableNull: public GSocketGUIFunctionsTable |
97 | { | |
98 | public: | |
99 | virtual bool OnInit(); | |
100 | virtual void OnExit(); | |
101 | virtual bool CanUseEventLoop(); | |
102 | virtual bool Init_Socket(GSocket *socket); | |
103 | virtual void Destroy_Socket(GSocket *socket); | |
104 | virtual void Enable_Events(GSocket *socket); | |
105 | virtual void Disable_Events(GSocket *socket); | |
106 | }; | |
107 | ||
108 | bool GSocketGUIFunctionsTableNull::OnInit() | |
109 | { return true; } | |
110 | void GSocketGUIFunctionsTableNull::OnExit() | |
111 | {} | |
112 | bool GSocketGUIFunctionsTableNull::CanUseEventLoop() | |
113 | { return false; } | |
d95de154 | 114 | bool GSocketGUIFunctionsTableNull::Init_Socket(GSocket *WXUNUSED(socket)) |
a4506848 | 115 | { return true; } |
d95de154 | 116 | void GSocketGUIFunctionsTableNull::Destroy_Socket(GSocket *WXUNUSED(socket)) |
a4506848 | 117 | {} |
d95de154 | 118 | void GSocketGUIFunctionsTableNull::Enable_Events(GSocket *WXUNUSED(socket)) |
a4506848 | 119 | {} |
d95de154 | 120 | void GSocketGUIFunctionsTableNull::Disable_Events(GSocket *WXUNUSED(socket)) |
a4506848 | 121 | {} |
f7ff39c6 DE |
122 | /* Global initialisers */ |
123 | ||
3e1400ac | 124 | void GSocket_SetGUIFunctions(GSocketGUIFunctionsTable *guifunc) |
f7ff39c6 DE |
125 | { |
126 | gs_gui_functions = guifunc; | |
127 | } | |
128 | ||
129 | int GSocket_Init(void) | |
130 | { | |
131 | WSADATA wsaData; | |
132 | ||
a4506848 | 133 | if (!gs_gui_functions) |
f7ff39c6 | 134 | { |
3e1400ac | 135 | static GSocketGUIFunctionsTableNull table; |
a4506848 DE |
136 | gs_gui_functions = &table; |
137 | } | |
138 | if ( !gs_gui_functions->OnInit() ) | |
139 | { | |
140 | return 0; | |
f7ff39c6 DE |
141 | } |
142 | ||
143 | /* Initialize WinSocket */ | |
144 | return (WSAStartup((1 << 8) | 1, &wsaData) == 0); | |
145 | } | |
146 | ||
147 | void GSocket_Cleanup(void) | |
148 | { | |
149 | if (gs_gui_functions) | |
150 | { | |
c90cc42e | 151 | gs_gui_functions->OnExit(); |
f7ff39c6 DE |
152 | } |
153 | ||
154 | /* Cleanup WinSocket */ | |
155 | WSACleanup(); | |
156 | } | |
157 | ||
158 | /* Constructors / Destructors for GSocket */ | |
159 | ||
c90cc42e | 160 | GSocket::GSocket() |
f7ff39c6 | 161 | { |
c90cc42e | 162 | int i; |
f7ff39c6 | 163 | |
c90cc42e | 164 | m_fd = INVALID_SOCKET; |
f7ff39c6 DE |
165 | for (i = 0; i < GSOCK_MAX_EVENT; i++) |
166 | { | |
c90cc42e | 167 | m_cbacks[i] = NULL; |
f7ff39c6 | 168 | } |
c90cc42e DE |
169 | m_detected = 0; |
170 | m_local = NULL; | |
171 | m_peer = NULL; | |
172 | m_error = GSOCK_NOERROR; | |
948c96ef DE |
173 | m_server = false; |
174 | m_stream = true; | |
c90cc42e DE |
175 | m_non_blocking = false; |
176 | m_timeout.tv_sec = 10 * 60; /* 10 minutes */ | |
177 | m_timeout.tv_usec = 0; | |
948c96ef DE |
178 | m_establishing = false; |
179 | m_reusable = false; | |
c90cc42e DE |
180 | |
181 | assert(gs_gui_functions); | |
f7ff39c6 | 182 | /* Per-socket GUI-specific initialization */ |
c90cc42e | 183 | m_ok = gs_gui_functions->Init_Socket(this); |
f7ff39c6 DE |
184 | } |
185 | ||
c90cc42e | 186 | void GSocket::Close() |
f7ff39c6 | 187 | { |
c90cc42e DE |
188 | gs_gui_functions->Disable_Events(this); |
189 | closesocket(m_fd); | |
190 | m_fd = INVALID_SOCKET; | |
f7ff39c6 DE |
191 | } |
192 | ||
c90cc42e | 193 | GSocket::~GSocket() |
f7ff39c6 | 194 | { |
c90cc42e | 195 | assert(this); |
f7ff39c6 DE |
196 | |
197 | /* Per-socket GUI-specific cleanup */ | |
c90cc42e | 198 | gs_gui_functions->Destroy_Socket(this); |
f7ff39c6 DE |
199 | |
200 | /* Check that the socket is really shutdowned */ | |
c90cc42e DE |
201 | if (m_fd != INVALID_SOCKET) |
202 | Shutdown(); | |
f7ff39c6 DE |
203 | |
204 | /* Destroy private addresses */ | |
c90cc42e DE |
205 | if (m_local) |
206 | GAddress_destroy(m_local); | |
f7ff39c6 | 207 | |
c90cc42e DE |
208 | if (m_peer) |
209 | GAddress_destroy(m_peer); | |
f7ff39c6 DE |
210 | } |
211 | ||
212 | /* GSocket_Shutdown: | |
213 | * Disallow further read/write operations on this socket, close | |
214 | * the fd and disable all callbacks. | |
215 | */ | |
c90cc42e | 216 | void GSocket::Shutdown() |
f7ff39c6 DE |
217 | { |
218 | int evt; | |
219 | ||
c90cc42e | 220 | assert(this); |
f7ff39c6 DE |
221 | |
222 | /* If socket has been created, shutdown it */ | |
c90cc42e | 223 | if (m_fd != INVALID_SOCKET) |
f7ff39c6 | 224 | { |
cd632a86 | 225 | shutdown(m_fd, 1 /* SD_SEND */); |
c90cc42e | 226 | Close(); |
f7ff39c6 DE |
227 | } |
228 | ||
229 | /* Disable GUI callbacks */ | |
230 | for (evt = 0; evt < GSOCK_MAX_EVENT; evt++) | |
c90cc42e | 231 | m_cbacks[evt] = NULL; |
f7ff39c6 | 232 | |
c90cc42e | 233 | m_detected = GSOCK_LOST_FLAG; |
f7ff39c6 DE |
234 | } |
235 | ||
236 | /* Address handling */ | |
237 | ||
238 | /* GSocket_SetLocal: | |
239 | * GSocket_GetLocal: | |
240 | * GSocket_SetPeer: | |
241 | * GSocket_GetPeer: | |
242 | * Set or get the local or peer address for this socket. The 'set' | |
243 | * functions return GSOCK_NOERROR on success, an error code otherwise. | |
244 | * The 'get' functions return a pointer to a GAddress object on success, | |
245 | * or NULL otherwise, in which case they set the error code of the | |
246 | * corresponding GSocket. | |
247 | * | |
248 | * Error codes: | |
249 | * GSOCK_INVSOCK - the socket is not valid. | |
250 | * GSOCK_INVADDR - the address is not valid. | |
251 | */ | |
c90cc42e | 252 | GSocketError GSocket::SetLocal(GAddress *address) |
f7ff39c6 | 253 | { |
c90cc42e | 254 | assert(this); |
f7ff39c6 DE |
255 | |
256 | /* the socket must be initialized, or it must be a server */ | |
c90cc42e | 257 | if (m_fd != INVALID_SOCKET && !m_server) |
f7ff39c6 | 258 | { |
c90cc42e | 259 | m_error = GSOCK_INVSOCK; |
f7ff39c6 DE |
260 | return GSOCK_INVSOCK; |
261 | } | |
262 | ||
263 | /* check address */ | |
264 | if (address == NULL || address->m_family == GSOCK_NOFAMILY) | |
265 | { | |
c90cc42e | 266 | m_error = GSOCK_INVADDR; |
f7ff39c6 DE |
267 | return GSOCK_INVADDR; |
268 | } | |
269 | ||
c90cc42e DE |
270 | if (m_local) |
271 | GAddress_destroy(m_local); | |
f7ff39c6 | 272 | |
c90cc42e | 273 | m_local = GAddress_copy(address); |
f7ff39c6 DE |
274 | |
275 | return GSOCK_NOERROR; | |
276 | } | |
277 | ||
c90cc42e | 278 | GSocketError GSocket::SetPeer(GAddress *address) |
f7ff39c6 | 279 | { |
c90cc42e | 280 | assert(this); |
f7ff39c6 DE |
281 | |
282 | /* check address */ | |
283 | if (address == NULL || address->m_family == GSOCK_NOFAMILY) | |
284 | { | |
c90cc42e | 285 | m_error = GSOCK_INVADDR; |
f7ff39c6 DE |
286 | return GSOCK_INVADDR; |
287 | } | |
288 | ||
c90cc42e DE |
289 | if (m_peer) |
290 | GAddress_destroy(m_peer); | |
f7ff39c6 | 291 | |
c90cc42e | 292 | m_peer = GAddress_copy(address); |
f7ff39c6 DE |
293 | |
294 | return GSOCK_NOERROR; | |
295 | } | |
296 | ||
c90cc42e | 297 | GAddress *GSocket::GetLocal() |
f7ff39c6 DE |
298 | { |
299 | GAddress *address; | |
300 | struct sockaddr addr; | |
9e03e02d | 301 | WX_SOCKLEN_T size = sizeof(addr); |
f7ff39c6 DE |
302 | GSocketError err; |
303 | ||
c90cc42e | 304 | assert(this); |
f7ff39c6 DE |
305 | |
306 | /* try to get it from the m_local var first */ | |
c90cc42e DE |
307 | if (m_local) |
308 | return GAddress_copy(m_local); | |
f7ff39c6 DE |
309 | |
310 | /* else, if the socket is initialized, try getsockname */ | |
c90cc42e | 311 | if (m_fd == INVALID_SOCKET) |
f7ff39c6 | 312 | { |
c90cc42e | 313 | m_error = GSOCK_INVSOCK; |
f7ff39c6 DE |
314 | return NULL; |
315 | } | |
316 | ||
c90cc42e | 317 | if (getsockname(m_fd, &addr, &size) == SOCKET_ERROR) |
f7ff39c6 | 318 | { |
c90cc42e | 319 | m_error = GSOCK_IOERR; |
f7ff39c6 DE |
320 | return NULL; |
321 | } | |
322 | ||
323 | /* got a valid address from getsockname, create a GAddress object */ | |
324 | if ((address = GAddress_new()) == NULL) | |
325 | { | |
c90cc42e | 326 | m_error = GSOCK_MEMERR; |
f7ff39c6 DE |
327 | return NULL; |
328 | } | |
329 | ||
330 | if ((err = _GAddress_translate_from(address, &addr, size)) != GSOCK_NOERROR) | |
331 | { | |
332 | GAddress_destroy(address); | |
c90cc42e | 333 | m_error = err; |
f7ff39c6 DE |
334 | return NULL; |
335 | } | |
336 | ||
337 | return address; | |
338 | } | |
339 | ||
c90cc42e | 340 | GAddress *GSocket::GetPeer() |
f7ff39c6 | 341 | { |
c90cc42e | 342 | assert(this); |
f7ff39c6 DE |
343 | |
344 | /* try to get it from the m_peer var */ | |
c90cc42e DE |
345 | if (m_peer) |
346 | return GAddress_copy(m_peer); | |
f7ff39c6 DE |
347 | |
348 | return NULL; | |
349 | } | |
350 | ||
351 | /* Server specific parts */ | |
352 | ||
353 | /* GSocket_SetServer: | |
354 | * Sets up this socket as a server. The local address must have been | |
355 | * set with GSocket_SetLocal() before GSocket_SetServer() is called. | |
356 | * Returns GSOCK_NOERROR on success, one of the following otherwise: | |
357 | * | |
358 | * Error codes: | |
359 | * GSOCK_INVSOCK - the socket is in use. | |
360 | * GSOCK_INVADDR - the local address has not been set. | |
361 | * GSOCK_IOERR - low-level error. | |
362 | */ | |
c90cc42e | 363 | GSocketError GSocket::SetServer() |
f7ff39c6 DE |
364 | { |
365 | u_long arg = 1; | |
366 | ||
c90cc42e | 367 | assert(this); |
f7ff39c6 DE |
368 | |
369 | /* must not be in use */ | |
c90cc42e | 370 | if (m_fd != INVALID_SOCKET) |
f7ff39c6 | 371 | { |
c90cc42e | 372 | m_error = GSOCK_INVSOCK; |
f7ff39c6 DE |
373 | return GSOCK_INVSOCK; |
374 | } | |
375 | ||
376 | /* the local addr must have been set */ | |
c90cc42e | 377 | if (!m_local) |
f7ff39c6 | 378 | { |
c90cc42e | 379 | m_error = GSOCK_INVADDR; |
f7ff39c6 DE |
380 | return GSOCK_INVADDR; |
381 | } | |
382 | ||
383 | /* Initialize all fields */ | |
948c96ef DE |
384 | m_server = true; |
385 | m_stream = true; | |
f7ff39c6 DE |
386 | |
387 | /* Create the socket */ | |
c90cc42e | 388 | m_fd = socket(m_local->m_realfamily, SOCK_STREAM, 0); |
f7ff39c6 | 389 | |
c90cc42e | 390 | if (m_fd == INVALID_SOCKET) |
f7ff39c6 | 391 | { |
c90cc42e | 392 | m_error = GSOCK_IOERR; |
f7ff39c6 DE |
393 | return GSOCK_IOERR; |
394 | } | |
395 | ||
c90cc42e DE |
396 | ioctlsocket(m_fd, FIONBIO, (u_long FAR *) &arg); |
397 | gs_gui_functions->Enable_Events(this); | |
f7ff39c6 DE |
398 | |
399 | /* allow a socket to re-bind if the socket is in the TIME_WAIT | |
400 | state after being previously closed. | |
401 | */ | |
28bf2f3c VZ |
402 | if (m_reusable) |
403 | { | |
404 | setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&arg, sizeof(arg)); | |
f7ff39c6 DE |
405 | } |
406 | ||
407 | /* Bind to the local address, | |
408 | * retrieve the actual address bound, | |
409 | * and listen up to 5 connections. | |
410 | */ | |
c90cc42e DE |
411 | if ((bind(m_fd, m_local->m_addr, m_local->m_len) != 0) || |
412 | (getsockname(m_fd, | |
413 | m_local->m_addr, | |
9e03e02d | 414 | (WX_SOCKLEN_T *)&m_local->m_len) != 0) || |
c90cc42e | 415 | (listen(m_fd, 5) != 0)) |
f7ff39c6 | 416 | { |
c90cc42e DE |
417 | Close(); |
418 | m_error = GSOCK_IOERR; | |
f7ff39c6 DE |
419 | return GSOCK_IOERR; |
420 | } | |
421 | ||
422 | return GSOCK_NOERROR; | |
423 | } | |
424 | ||
425 | /* GSocket_WaitConnection: | |
426 | * Waits for an incoming client connection. Returns a pointer to | |
427 | * a GSocket object, or NULL if there was an error, in which case | |
428 | * the last error field will be updated for the calling GSocket. | |
429 | * | |
430 | * Error codes (set in the calling GSocket) | |
431 | * GSOCK_INVSOCK - the socket is not valid or not a server. | |
432 | * GSOCK_TIMEDOUT - timeout, no incoming connections. | |
433 | * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking. | |
434 | * GSOCK_MEMERR - couldn't allocate memory. | |
435 | * GSOCK_IOERR - low-level error. | |
436 | */ | |
c90cc42e | 437 | GSocket *GSocket::WaitConnection() |
f7ff39c6 DE |
438 | { |
439 | GSocket *connection; | |
440 | struct sockaddr from; | |
9e03e02d | 441 | WX_SOCKLEN_T fromlen = sizeof(from); |
f7ff39c6 DE |
442 | GSocketError err; |
443 | u_long arg = 1; | |
444 | ||
c90cc42e | 445 | assert(this); |
f7ff39c6 DE |
446 | |
447 | /* Reenable CONNECTION events */ | |
c90cc42e | 448 | m_detected &= ~GSOCK_CONNECTION_FLAG; |
f7ff39c6 DE |
449 | |
450 | /* If the socket has already been created, we exit immediately */ | |
c90cc42e | 451 | if (m_fd == INVALID_SOCKET || !m_server) |
f7ff39c6 | 452 | { |
c90cc42e | 453 | m_error = GSOCK_INVSOCK; |
f7ff39c6 DE |
454 | return NULL; |
455 | } | |
456 | ||
457 | /* Create a GSocket object for the new connection */ | |
458 | connection = GSocket_new(); | |
459 | ||
460 | if (!connection) | |
461 | { | |
c90cc42e | 462 | m_error = GSOCK_MEMERR; |
f7ff39c6 DE |
463 | return NULL; |
464 | } | |
465 | ||
466 | /* Wait for a connection (with timeout) */ | |
c90cc42e | 467 | if (Input_Timeout() == GSOCK_TIMEDOUT) |
f7ff39c6 | 468 | { |
85431efa | 469 | delete connection; |
c90cc42e | 470 | /* m_error set by _GSocket_Input_Timeout */ |
f7ff39c6 DE |
471 | return NULL; |
472 | } | |
473 | ||
c90cc42e | 474 | connection->m_fd = accept(m_fd, &from, &fromlen); |
f7ff39c6 DE |
475 | |
476 | if (connection->m_fd == INVALID_SOCKET) | |
477 | { | |
478 | if (WSAGetLastError() == WSAEWOULDBLOCK) | |
c90cc42e | 479 | m_error = GSOCK_WOULDBLOCK; |
f7ff39c6 | 480 | else |
c90cc42e | 481 | m_error = GSOCK_IOERR; |
f7ff39c6 | 482 | |
85431efa | 483 | delete connection; |
f7ff39c6 DE |
484 | return NULL; |
485 | } | |
486 | ||
487 | /* Initialize all fields */ | |
948c96ef DE |
488 | connection->m_server = false; |
489 | connection->m_stream = true; | |
f7ff39c6 DE |
490 | |
491 | /* Setup the peer address field */ | |
492 | connection->m_peer = GAddress_new(); | |
493 | if (!connection->m_peer) | |
494 | { | |
85431efa | 495 | delete connection; |
c90cc42e | 496 | m_error = GSOCK_MEMERR; |
f7ff39c6 DE |
497 | return NULL; |
498 | } | |
499 | err = _GAddress_translate_from(connection->m_peer, &from, fromlen); | |
500 | if (err != GSOCK_NOERROR) | |
501 | { | |
502 | GAddress_destroy(connection->m_peer); | |
85431efa | 503 | delete connection; |
c90cc42e | 504 | m_error = err; |
f7ff39c6 DE |
505 | return NULL; |
506 | } | |
507 | ||
508 | ioctlsocket(connection->m_fd, FIONBIO, (u_long FAR *) &arg); | |
c90cc42e | 509 | gs_gui_functions->Enable_Events(connection); |
f7ff39c6 DE |
510 | |
511 | return connection; | |
512 | } | |
513 | ||
514 | /* GSocket_SetReusable: | |
515 | * Simply sets the m_resuable flag on the socket. GSocket_SetServer will | |
516 | * make the appropriate setsockopt() call. | |
517 | * Implemented as a GSocket function because clients (ie, wxSocketServer) | |
518 | * don't have access to the GSocket struct information. | |
3103e8a9 | 519 | * Returns true if the flag was set correctly, false if an error occurred |
f7ff39c6 DE |
520 | * (ie, if the parameter was NULL) |
521 | */ | |
948c96ef | 522 | bool GSocket::SetReusable() |
f7ff39c6 DE |
523 | { |
524 | /* socket must not be null, and must not be in use/already bound */ | |
c90cc42e | 525 | if (this && m_fd == INVALID_SOCKET) { |
948c96ef DE |
526 | m_reusable = true; |
527 | return true; | |
f7ff39c6 | 528 | } |
948c96ef | 529 | return false; |
f7ff39c6 DE |
530 | } |
531 | ||
532 | /* Client specific parts */ | |
533 | ||
534 | /* GSocket_Connect: | |
535 | * For stream (connection oriented) sockets, GSocket_Connect() tries | |
536 | * to establish a client connection to a server using the peer address | |
537 | * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the | |
3103e8a9 | 538 | * connection has been successfully established, or one of the error |
f7ff39c6 DE |
539 | * codes listed below. Note that for nonblocking sockets, a return |
540 | * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection | |
541 | * request can be completed later; you should use GSocket_Select() | |
542 | * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the | |
543 | * corresponding asynchronous events. | |
544 | * | |
545 | * For datagram (non connection oriented) sockets, GSocket_Connect() | |
546 | * just sets the peer address established with GSocket_SetPeer() as | |
547 | * default destination. | |
548 | * | |
549 | * Error codes: | |
550 | * GSOCK_INVSOCK - the socket is in use or not valid. | |
551 | * GSOCK_INVADDR - the peer address has not been established. | |
552 | * GSOCK_TIMEDOUT - timeout, the connection failed. | |
553 | * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only) | |
554 | * GSOCK_MEMERR - couldn't allocate memory. | |
555 | * GSOCK_IOERR - low-level error. | |
556 | */ | |
c90cc42e | 557 | GSocketError GSocket::Connect(GSocketStream stream) |
f7ff39c6 DE |
558 | { |
559 | int ret, err; | |
560 | u_long arg = 1; | |
561 | ||
c90cc42e | 562 | assert(this); |
f7ff39c6 DE |
563 | |
564 | /* Enable CONNECTION events (needed for nonblocking connections) */ | |
c90cc42e | 565 | m_detected &= ~GSOCK_CONNECTION_FLAG; |
f7ff39c6 | 566 | |
c90cc42e | 567 | if (m_fd != INVALID_SOCKET) |
f7ff39c6 | 568 | { |
c90cc42e | 569 | m_error = GSOCK_INVSOCK; |
f7ff39c6 DE |
570 | return GSOCK_INVSOCK; |
571 | } | |
572 | ||
c90cc42e | 573 | if (!m_peer) |
f7ff39c6 | 574 | { |
c90cc42e | 575 | m_error = GSOCK_INVADDR; |
f7ff39c6 DE |
576 | return GSOCK_INVADDR; |
577 | } | |
578 | ||
579 | /* Streamed or dgram socket? */ | |
c90cc42e | 580 | m_stream = (stream == GSOCK_STREAMED); |
948c96ef DE |
581 | m_server = false; |
582 | m_establishing = false; | |
f7ff39c6 DE |
583 | |
584 | /* Create the socket */ | |
c90cc42e DE |
585 | m_fd = socket(m_peer->m_realfamily, |
586 | m_stream? SOCK_STREAM : SOCK_DGRAM, 0); | |
f7ff39c6 | 587 | |
c90cc42e | 588 | if (m_fd == INVALID_SOCKET) |
f7ff39c6 | 589 | { |
c90cc42e | 590 | m_error = GSOCK_IOERR; |
f7ff39c6 DE |
591 | return GSOCK_IOERR; |
592 | } | |
593 | ||
c90cc42e DE |
594 | ioctlsocket(m_fd, FIONBIO, (u_long FAR *) &arg); |
595 | gs_gui_functions->Enable_Events(this); | |
f7ff39c6 | 596 | |
716a5baa KH |
597 | // If the reuse flag is set, use the applicable socket reuse flag |
598 | if (m_reusable) | |
599 | { | |
28bf2f3c | 600 | setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&arg, sizeof(arg)); |
716a5baa KH |
601 | } |
602 | ||
603 | // If a local address has been set, then we need to bind to it before calling connect | |
604 | if (m_local && m_local->m_addr) | |
605 | { | |
606 | bind(m_fd, m_local->m_addr, m_local->m_len); | |
607 | } | |
608 | ||
f7ff39c6 | 609 | /* Connect it to the peer address, with a timeout (see below) */ |
c90cc42e | 610 | ret = connect(m_fd, m_peer->m_addr, m_peer->m_len); |
f7ff39c6 DE |
611 | |
612 | if (ret == SOCKET_ERROR) | |
613 | { | |
614 | err = WSAGetLastError(); | |
615 | ||
616 | /* If connect failed with EWOULDBLOCK and the GSocket object | |
617 | * is in blocking mode, we select() for the specified timeout | |
618 | * checking for writability to see if the connection request | |
619 | * completes. | |
620 | */ | |
c90cc42e | 621 | if ((err == WSAEWOULDBLOCK) && (!m_non_blocking)) |
f7ff39c6 | 622 | { |
c90cc42e | 623 | err = Connect_Timeout(); |
f7ff39c6 DE |
624 | |
625 | if (err != GSOCK_NOERROR) | |
626 | { | |
c90cc42e DE |
627 | Close(); |
628 | /* m_error is set in _GSocket_Connect_Timeout */ | |
f7ff39c6 DE |
629 | } |
630 | ||
631 | return (GSocketError) err; | |
632 | } | |
633 | ||
634 | /* If connect failed with EWOULDBLOCK and the GSocket object | |
635 | * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK | |
636 | * (and return GSOCK_WOULDBLOCK) but we don't close the socket; | |
637 | * this way if the connection completes, a GSOCK_CONNECTION | |
638 | * event will be generated, if enabled. | |
639 | */ | |
c90cc42e | 640 | if ((err == WSAEWOULDBLOCK) && (m_non_blocking)) |
f7ff39c6 | 641 | { |
948c96ef | 642 | m_establishing = true; |
c90cc42e | 643 | m_error = GSOCK_WOULDBLOCK; |
f7ff39c6 DE |
644 | return GSOCK_WOULDBLOCK; |
645 | } | |
646 | ||
647 | /* If connect failed with an error other than EWOULDBLOCK, | |
648 | * then the call to GSocket_Connect() has failed. | |
649 | */ | |
c90cc42e DE |
650 | Close(); |
651 | m_error = GSOCK_IOERR; | |
f7ff39c6 DE |
652 | return GSOCK_IOERR; |
653 | } | |
654 | ||
655 | return GSOCK_NOERROR; | |
656 | } | |
657 | ||
658 | /* Datagram sockets */ | |
659 | ||
660 | /* GSocket_SetNonOriented: | |
661 | * Sets up this socket as a non-connection oriented (datagram) socket. | |
662 | * Before using this function, the local address must have been set | |
663 | * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR | |
664 | * on success, or one of the following otherwise. | |
665 | * | |
666 | * Error codes: | |
667 | * GSOCK_INVSOCK - the socket is in use. | |
668 | * GSOCK_INVADDR - the local address has not been set. | |
669 | * GSOCK_IOERR - low-level error. | |
670 | */ | |
c90cc42e | 671 | GSocketError GSocket::SetNonOriented() |
f7ff39c6 DE |
672 | { |
673 | u_long arg = 1; | |
674 | ||
c90cc42e | 675 | assert(this); |
f7ff39c6 | 676 | |
c90cc42e | 677 | if (m_fd != INVALID_SOCKET) |
f7ff39c6 | 678 | { |
c90cc42e | 679 | m_error = GSOCK_INVSOCK; |
f7ff39c6 DE |
680 | return GSOCK_INVSOCK; |
681 | } | |
682 | ||
c90cc42e | 683 | if (!m_local) |
f7ff39c6 | 684 | { |
c90cc42e | 685 | m_error = GSOCK_INVADDR; |
f7ff39c6 DE |
686 | return GSOCK_INVADDR; |
687 | } | |
688 | ||
689 | /* Initialize all fields */ | |
948c96ef DE |
690 | m_stream = false; |
691 | m_server = false; | |
f7ff39c6 DE |
692 | |
693 | /* Create the socket */ | |
c90cc42e | 694 | m_fd = socket(m_local->m_realfamily, SOCK_DGRAM, 0); |
f7ff39c6 | 695 | |
c90cc42e | 696 | if (m_fd == INVALID_SOCKET) |
f7ff39c6 | 697 | { |
c90cc42e | 698 | m_error = GSOCK_IOERR; |
f7ff39c6 DE |
699 | return GSOCK_IOERR; |
700 | } | |
701 | ||
c90cc42e DE |
702 | ioctlsocket(m_fd, FIONBIO, (u_long FAR *) &arg); |
703 | gs_gui_functions->Enable_Events(this); | |
f7ff39c6 | 704 | |
28bf2f3c VZ |
705 | if (m_reusable) |
706 | { | |
707 | setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&arg, sizeof(arg)); | |
708 | } | |
709 | ||
f7ff39c6 DE |
710 | /* Bind to the local address, |
711 | * and retrieve the actual address bound. | |
712 | */ | |
c90cc42e DE |
713 | if ((bind(m_fd, m_local->m_addr, m_local->m_len) != 0) || |
714 | (getsockname(m_fd, | |
715 | m_local->m_addr, | |
9e03e02d | 716 | (WX_SOCKLEN_T *)&m_local->m_len) != 0)) |
f7ff39c6 | 717 | { |
c90cc42e DE |
718 | Close(); |
719 | m_error = GSOCK_IOERR; | |
f7ff39c6 DE |
720 | return GSOCK_IOERR; |
721 | } | |
722 | ||
723 | return GSOCK_NOERROR; | |
724 | } | |
725 | ||
726 | /* Generic IO */ | |
727 | ||
728 | /* Like recv(), send(), ... */ | |
c90cc42e | 729 | int GSocket::Read(char *buffer, int size) |
f7ff39c6 DE |
730 | { |
731 | int ret; | |
732 | ||
c90cc42e | 733 | assert(this); |
f7ff39c6 DE |
734 | |
735 | /* Reenable INPUT events */ | |
c90cc42e | 736 | m_detected &= ~GSOCK_INPUT_FLAG; |
f7ff39c6 | 737 | |
c90cc42e | 738 | if (m_fd == INVALID_SOCKET || m_server) |
f7ff39c6 | 739 | { |
c90cc42e | 740 | m_error = GSOCK_INVSOCK; |
f7ff39c6 DE |
741 | return -1; |
742 | } | |
743 | ||
744 | /* If the socket is blocking, wait for data (with a timeout) */ | |
c90cc42e | 745 | if (Input_Timeout() == GSOCK_TIMEDOUT) |
976abb72 VZ |
746 | { |
747 | m_error = GSOCK_TIMEDOUT; | |
f7ff39c6 | 748 | return -1; |
976abb72 | 749 | } |
f7ff39c6 DE |
750 | |
751 | /* Read the data */ | |
c90cc42e DE |
752 | if (m_stream) |
753 | ret = Recv_Stream(buffer, size); | |
f7ff39c6 | 754 | else |
c90cc42e | 755 | ret = Recv_Dgram(buffer, size); |
f7ff39c6 DE |
756 | |
757 | if (ret == SOCKET_ERROR) | |
758 | { | |
759 | if (WSAGetLastError() != WSAEWOULDBLOCK) | |
c90cc42e | 760 | m_error = GSOCK_IOERR; |
f7ff39c6 | 761 | else |
c90cc42e | 762 | m_error = GSOCK_WOULDBLOCK; |
f7ff39c6 DE |
763 | return -1; |
764 | } | |
765 | ||
766 | return ret; | |
767 | } | |
768 | ||
c90cc42e | 769 | int GSocket::Write(const char *buffer, int size) |
f7ff39c6 DE |
770 | { |
771 | int ret; | |
772 | ||
c90cc42e | 773 | assert(this); |
f7ff39c6 | 774 | |
c90cc42e | 775 | if (m_fd == INVALID_SOCKET || m_server) |
f7ff39c6 | 776 | { |
c90cc42e | 777 | m_error = GSOCK_INVSOCK; |
f7ff39c6 DE |
778 | return -1; |
779 | } | |
780 | ||
781 | /* If the socket is blocking, wait for writability (with a timeout) */ | |
c90cc42e | 782 | if (Output_Timeout() == GSOCK_TIMEDOUT) |
f7ff39c6 DE |
783 | return -1; |
784 | ||
785 | /* Write the data */ | |
c90cc42e DE |
786 | if (m_stream) |
787 | ret = Send_Stream(buffer, size); | |
f7ff39c6 | 788 | else |
c90cc42e | 789 | ret = Send_Dgram(buffer, size); |
f7ff39c6 DE |
790 | |
791 | if (ret == SOCKET_ERROR) | |
792 | { | |
793 | if (WSAGetLastError() != WSAEWOULDBLOCK) | |
c90cc42e | 794 | m_error = GSOCK_IOERR; |
f7ff39c6 | 795 | else |
c90cc42e | 796 | m_error = GSOCK_WOULDBLOCK; |
f7ff39c6 DE |
797 | |
798 | /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect | |
799 | * does). Once the first OUTPUT event is received, users can assume | |
800 | * that the socket is writable until a read operation fails. Only then | |
801 | * will further OUTPUT events be posted. | |
802 | */ | |
c90cc42e | 803 | m_detected &= ~GSOCK_OUTPUT_FLAG; |
f7ff39c6 DE |
804 | return -1; |
805 | } | |
806 | ||
807 | return ret; | |
808 | } | |
809 | ||
810 | /* GSocket_Select: | |
811 | * Polls the socket to determine its status. This function will | |
812 | * check for the events specified in the 'flags' parameter, and | |
813 | * it will return a mask indicating which operations can be | |
814 | * performed. This function won't block, regardless of the | |
815 | * mode (blocking | nonblocking) of the socket. | |
816 | */ | |
c90cc42e | 817 | GSocketEventFlags GSocket::Select(GSocketEventFlags flags) |
f7ff39c6 | 818 | { |
c90cc42e | 819 | if (!gs_gui_functions->CanUseEventLoop()) |
f7ff39c6 DE |
820 | { |
821 | GSocketEventFlags result = 0; | |
822 | fd_set readfds; | |
823 | fd_set writefds; | |
824 | fd_set exceptfds; | |
825 | ||
c90cc42e | 826 | assert(this); |
f7ff39c6 DE |
827 | |
828 | FD_ZERO(&readfds); | |
829 | FD_ZERO(&writefds); | |
830 | FD_ZERO(&exceptfds); | |
c90cc42e | 831 | FD_SET(m_fd, &readfds); |
f7ff39c6 | 832 | if (flags & GSOCK_OUTPUT_FLAG || flags & GSOCK_CONNECTION_FLAG) |
c90cc42e DE |
833 | FD_SET(m_fd, &writefds); |
834 | FD_SET(m_fd, &exceptfds); | |
f7ff39c6 DE |
835 | |
836 | /* Check 'sticky' CONNECTION flag first */ | |
c90cc42e | 837 | result |= (GSOCK_CONNECTION_FLAG & m_detected); |
f7ff39c6 DE |
838 | |
839 | /* If we have already detected a LOST event, then don't try | |
840 | * to do any further processing. | |
841 | */ | |
c90cc42e | 842 | if ((m_detected & GSOCK_LOST_FLAG) != 0) |
f7ff39c6 | 843 | { |
948c96ef | 844 | m_establishing = false; |
f7ff39c6 DE |
845 | |
846 | return (GSOCK_LOST_FLAG & flags); | |
847 | } | |
848 | ||
849 | /* Try select now */ | |
c90cc42e DE |
850 | if (select(m_fd + 1, &readfds, &writefds, &exceptfds, |
851 | &m_timeout) <= 0) | |
f7ff39c6 DE |
852 | { |
853 | /* What to do here? */ | |
854 | return (result & flags); | |
855 | } | |
856 | ||
b74865fa KH |
857 | /* Check for exceptions and errors */ |
858 | if (FD_ISSET(m_fd, &exceptfds)) | |
f7ff39c6 | 859 | { |
b74865fa KH |
860 | m_establishing = false; |
861 | m_detected = GSOCK_LOST_FLAG; | |
f7ff39c6 | 862 | |
b74865fa KH |
863 | /* LOST event: Abort any further processing */ |
864 | return (GSOCK_LOST_FLAG & flags); | |
865 | } | |
f7ff39c6 | 866 | |
b74865fa KH |
867 | /* Check for readability */ |
868 | if (FD_ISSET(m_fd, &readfds)) | |
869 | { | |
870 | result |= GSOCK_INPUT_FLAG; | |
871 | ||
872 | if (m_server && m_stream) | |
f4322df6 VZ |
873 | { |
874 | /* This is a TCP server socket that detected a connection. | |
875 | While the INPUT_FLAG is also set, it doesn't matter on | |
b74865fa KH |
876 | this kind of sockets, as we can only Accept() from them. */ |
877 | result |= GSOCK_CONNECTION_FLAG; | |
878 | m_detected |= GSOCK_CONNECTION_FLAG; | |
f7ff39c6 DE |
879 | } |
880 | } | |
881 | ||
882 | /* Check for writability */ | |
c90cc42e | 883 | if (FD_ISSET(m_fd, &writefds)) |
f7ff39c6 | 884 | { |
c90cc42e | 885 | if (m_establishing && !m_server) |
f7ff39c6 DE |
886 | { |
887 | int error; | |
9e03e02d | 888 | WX_SOCKLEN_T len = sizeof(error); |
f7ff39c6 | 889 | |
948c96ef | 890 | m_establishing = false; |
f7ff39c6 | 891 | |
c90cc42e | 892 | getsockopt(m_fd, SOL_SOCKET, SO_ERROR, (char*)&error, &len); |
f7ff39c6 DE |
893 | |
894 | if (error) | |
895 | { | |
c90cc42e | 896 | m_detected = GSOCK_LOST_FLAG; |
f7ff39c6 DE |
897 | |
898 | /* LOST event: Abort any further processing */ | |
899 | return (GSOCK_LOST_FLAG & flags); | |
900 | } | |
901 | else | |
902 | { | |
903 | result |= GSOCK_CONNECTION_FLAG; | |
c90cc42e | 904 | m_detected |= GSOCK_CONNECTION_FLAG; |
f7ff39c6 DE |
905 | } |
906 | } | |
907 | else | |
908 | { | |
909 | result |= GSOCK_OUTPUT_FLAG; | |
910 | } | |
911 | } | |
912 | ||
f7ff39c6 DE |
913 | return (result & flags); |
914 | } | |
915 | else /* USE_GUI() */ | |
916 | { | |
c90cc42e DE |
917 | assert(this); |
918 | return flags & m_detected; | |
f7ff39c6 DE |
919 | } |
920 | } | |
921 | ||
922 | /* Attributes */ | |
923 | ||
924 | /* GSocket_SetNonBlocking: | |
925 | * Sets the socket to non-blocking mode. All IO calls will return | |
926 | * immediately. | |
927 | */ | |
c90cc42e | 928 | void GSocket::SetNonBlocking(bool non_block) |
f7ff39c6 | 929 | { |
c90cc42e | 930 | assert(this); |
f7ff39c6 | 931 | |
c90cc42e | 932 | m_non_blocking = non_block; |
f7ff39c6 DE |
933 | } |
934 | ||
935 | /* GSocket_SetTimeout: | |
936 | * Sets the timeout for blocking calls. Time is expressed in | |
937 | * milliseconds. | |
938 | */ | |
c90cc42e | 939 | void GSocket::SetTimeout(unsigned long millis) |
f7ff39c6 | 940 | { |
c90cc42e | 941 | assert(this); |
f7ff39c6 | 942 | |
c90cc42e DE |
943 | m_timeout.tv_sec = (millis / 1000); |
944 | m_timeout.tv_usec = (millis % 1000) * 1000; | |
f7ff39c6 DE |
945 | } |
946 | ||
947 | /* GSocket_GetError: | |
3103e8a9 | 948 | * Returns the last error occurred for this socket. Note that successful |
f7ff39c6 DE |
949 | * operations do not clear this back to GSOCK_NOERROR, so use it only |
950 | * after an error. | |
951 | */ | |
a4506848 | 952 | GSocketError WXDLLIMPEXP_NET GSocket::GetError() |
f7ff39c6 | 953 | { |
c90cc42e | 954 | assert(this); |
f7ff39c6 | 955 | |
c90cc42e | 956 | return m_error; |
f7ff39c6 DE |
957 | } |
958 | ||
959 | /* Callbacks */ | |
960 | ||
961 | /* GSOCK_INPUT: | |
962 | * There is data to be read in the input buffer. If, after a read | |
963 | * operation, there is still data available, the callback function will | |
964 | * be called again. | |
965 | * GSOCK_OUTPUT: | |
966 | * The socket is available for writing. That is, the next write call | |
967 | * won't block. This event is generated only once, when the connection is | |
968 | * first established, and then only if a call failed with GSOCK_WOULDBLOCK, | |
969 | * when the output buffer empties again. This means that the app should | |
970 | * assume that it can write since the first OUTPUT event, and no more | |
971 | * OUTPUT events will be generated unless an error occurs. | |
972 | * GSOCK_CONNECTION: | |
3103e8a9 | 973 | * Connection successfully established, for client sockets, or incoming |
f7ff39c6 DE |
974 | * client connection, for server sockets. Wait for this event (also watch |
975 | * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call. | |
976 | * GSOCK_LOST: | |
977 | * The connection is lost (or a connection request failed); this could | |
978 | * be due to a failure, or due to the peer closing it gracefully. | |
979 | */ | |
980 | ||
981 | /* GSocket_SetCallback: | |
982 | * Enables the callbacks specified by 'flags'. Note that 'flags' | |
983 | * may be a combination of flags OR'ed toghether, so the same | |
984 | * callback function can be made to accept different events. | |
985 | * The callback function must have the following prototype: | |
986 | * | |
987 | * void function(GSocket *socket, GSocketEvent event, char *cdata) | |
988 | */ | |
c90cc42e | 989 | void GSocket::SetCallback(GSocketEventFlags flags, |
f7ff39c6 DE |
990 | GSocketCallback callback, char *cdata) |
991 | { | |
992 | int count; | |
993 | ||
c90cc42e | 994 | assert(this); |
f7ff39c6 DE |
995 | |
996 | for (count = 0; count < GSOCK_MAX_EVENT; count++) | |
997 | { | |
998 | if ((flags & (1 << count)) != 0) | |
999 | { | |
c90cc42e DE |
1000 | m_cbacks[count] = callback; |
1001 | m_data[count] = cdata; | |
f7ff39c6 DE |
1002 | } |
1003 | } | |
1004 | } | |
1005 | ||
1006 | /* GSocket_UnsetCallback: | |
1007 | * Disables all callbacks specified by 'flags', which may be a | |
1008 | * combination of flags OR'ed toghether. | |
1009 | */ | |
c90cc42e | 1010 | void GSocket::UnsetCallback(GSocketEventFlags flags) |
f7ff39c6 DE |
1011 | { |
1012 | int count; | |
1013 | ||
c90cc42e | 1014 | assert(this); |
f7ff39c6 DE |
1015 | |
1016 | for (count = 0; count < GSOCK_MAX_EVENT; count++) | |
1017 | { | |
1018 | if ((flags & (1 << count)) != 0) | |
1019 | { | |
c90cc42e DE |
1020 | m_cbacks[count] = NULL; |
1021 | m_data[count] = NULL; | |
f7ff39c6 DE |
1022 | } |
1023 | } | |
1024 | } | |
1025 | ||
c90cc42e | 1026 | GSocketError GSocket::GetSockOpt(int level, int optname, |
f7ff39c6 DE |
1027 | void *optval, int *optlen) |
1028 | { | |
c90cc42e | 1029 | if (getsockopt(m_fd, level, optname, (char*)optval, optlen) == 0) |
f7ff39c6 DE |
1030 | { |
1031 | return GSOCK_NOERROR; | |
1032 | } | |
1033 | return GSOCK_OPTERR; | |
1034 | } | |
1035 | ||
c90cc42e | 1036 | GSocketError GSocket::SetSockOpt(int level, int optname, |
f7ff39c6 DE |
1037 | const void *optval, int optlen) |
1038 | { | |
c90cc42e | 1039 | if (setsockopt(m_fd, level, optname, (char*)optval, optlen) == 0) |
f7ff39c6 DE |
1040 | { |
1041 | return GSOCK_NOERROR; | |
1042 | } | |
1043 | return GSOCK_OPTERR; | |
1044 | } | |
1045 | ||
1046 | /* Internals (IO) */ | |
1047 | ||
1048 | /* _GSocket_Input_Timeout: | |
1049 | * For blocking sockets, wait until data is available or | |
1050 | * until timeout ellapses. | |
1051 | */ | |
c90cc42e | 1052 | GSocketError GSocket::Input_Timeout() |
f7ff39c6 DE |
1053 | { |
1054 | fd_set readfds; | |
1055 | ||
c90cc42e | 1056 | if (!m_non_blocking) |
f7ff39c6 DE |
1057 | { |
1058 | FD_ZERO(&readfds); | |
c90cc42e DE |
1059 | FD_SET(m_fd, &readfds); |
1060 | if (select(0, &readfds, NULL, NULL, &m_timeout) == 0) | |
f7ff39c6 | 1061 | { |
c90cc42e | 1062 | m_error = GSOCK_TIMEDOUT; |
f7ff39c6 DE |
1063 | return GSOCK_TIMEDOUT; |
1064 | } | |
1065 | } | |
1066 | return GSOCK_NOERROR; | |
1067 | } | |
1068 | ||
1069 | /* _GSocket_Output_Timeout: | |
1070 | * For blocking sockets, wait until data can be sent without | |
1071 | * blocking or until timeout ellapses. | |
1072 | */ | |
c90cc42e | 1073 | GSocketError GSocket::Output_Timeout() |
f7ff39c6 DE |
1074 | { |
1075 | fd_set writefds; | |
1076 | ||
c90cc42e | 1077 | if (!m_non_blocking) |
f7ff39c6 DE |
1078 | { |
1079 | FD_ZERO(&writefds); | |
c90cc42e DE |
1080 | FD_SET(m_fd, &writefds); |
1081 | if (select(0, NULL, &writefds, NULL, &m_timeout) == 0) | |
f7ff39c6 | 1082 | { |
c90cc42e | 1083 | m_error = GSOCK_TIMEDOUT; |
f7ff39c6 DE |
1084 | return GSOCK_TIMEDOUT; |
1085 | } | |
1086 | } | |
1087 | return GSOCK_NOERROR; | |
1088 | } | |
1089 | ||
1090 | /* _GSocket_Connect_Timeout: | |
1091 | * For blocking sockets, wait until the connection is | |
1092 | * established or fails, or until timeout ellapses. | |
1093 | */ | |
c90cc42e | 1094 | GSocketError GSocket::Connect_Timeout() |
f7ff39c6 DE |
1095 | { |
1096 | fd_set writefds; | |
1097 | fd_set exceptfds; | |
1098 | ||
1099 | FD_ZERO(&writefds); | |
1100 | FD_ZERO(&exceptfds); | |
c90cc42e DE |
1101 | FD_SET(m_fd, &writefds); |
1102 | FD_SET(m_fd, &exceptfds); | |
1103 | if (select(0, NULL, &writefds, &exceptfds, &m_timeout) == 0) | |
f7ff39c6 | 1104 | { |
c90cc42e | 1105 | m_error = GSOCK_TIMEDOUT; |
f7ff39c6 DE |
1106 | return GSOCK_TIMEDOUT; |
1107 | } | |
c90cc42e | 1108 | if (!FD_ISSET(m_fd, &writefds)) |
f7ff39c6 | 1109 | { |
c90cc42e | 1110 | m_error = GSOCK_IOERR; |
f7ff39c6 DE |
1111 | return GSOCK_IOERR; |
1112 | } | |
1113 | ||
1114 | return GSOCK_NOERROR; | |
1115 | } | |
1116 | ||
c90cc42e | 1117 | int GSocket::Recv_Stream(char *buffer, int size) |
f7ff39c6 | 1118 | { |
c90cc42e | 1119 | return recv(m_fd, buffer, size, 0); |
f7ff39c6 DE |
1120 | } |
1121 | ||
c90cc42e | 1122 | int GSocket::Recv_Dgram(char *buffer, int size) |
f7ff39c6 DE |
1123 | { |
1124 | struct sockaddr from; | |
9e03e02d | 1125 | WX_SOCKLEN_T fromlen = sizeof(from); |
f7ff39c6 DE |
1126 | int ret; |
1127 | GSocketError err; | |
1128 | ||
c90cc42e | 1129 | ret = recvfrom(m_fd, buffer, size, 0, &from, &fromlen); |
f7ff39c6 DE |
1130 | |
1131 | if (ret == SOCKET_ERROR) | |
1132 | return SOCKET_ERROR; | |
1133 | ||
1134 | /* Translate a system address into a GSocket address */ | |
c90cc42e | 1135 | if (!m_peer) |
f7ff39c6 | 1136 | { |
c90cc42e DE |
1137 | m_peer = GAddress_new(); |
1138 | if (!m_peer) | |
f7ff39c6 | 1139 | { |
c90cc42e | 1140 | m_error = GSOCK_MEMERR; |
f7ff39c6 DE |
1141 | return -1; |
1142 | } | |
1143 | } | |
c90cc42e | 1144 | err = _GAddress_translate_from(m_peer, &from, fromlen); |
f7ff39c6 DE |
1145 | if (err != GSOCK_NOERROR) |
1146 | { | |
c90cc42e DE |
1147 | GAddress_destroy(m_peer); |
1148 | m_peer = NULL; | |
1149 | m_error = err; | |
f7ff39c6 DE |
1150 | return -1; |
1151 | } | |
1152 | ||
1153 | return ret; | |
1154 | } | |
1155 | ||
c90cc42e | 1156 | int GSocket::Send_Stream(const char *buffer, int size) |
f7ff39c6 | 1157 | { |
c90cc42e | 1158 | return send(m_fd, buffer, size, 0); |
f7ff39c6 DE |
1159 | } |
1160 | ||
c90cc42e | 1161 | int GSocket::Send_Dgram(const char *buffer, int size) |
f7ff39c6 DE |
1162 | { |
1163 | struct sockaddr *addr; | |
1164 | int len, ret; | |
1165 | GSocketError err; | |
1166 | ||
c90cc42e | 1167 | if (!m_peer) |
f7ff39c6 | 1168 | { |
c90cc42e | 1169 | m_error = GSOCK_INVADDR; |
f7ff39c6 DE |
1170 | return -1; |
1171 | } | |
1172 | ||
c90cc42e | 1173 | err = _GAddress_translate_to(m_peer, &addr, &len); |
f7ff39c6 DE |
1174 | if (err != GSOCK_NOERROR) |
1175 | { | |
c90cc42e | 1176 | m_error = err; |
f7ff39c6 DE |
1177 | return -1; |
1178 | } | |
1179 | ||
c90cc42e | 1180 | ret = sendto(m_fd, buffer, size, 0, addr, len); |
f7ff39c6 DE |
1181 | |
1182 | /* Frees memory allocated by _GAddress_translate_to */ | |
1183 | free(addr); | |
1184 | ||
1185 | return ret; | |
1186 | } | |
1187 | ||
c90cc42e DE |
1188 | /* Compatibility functions for GSocket */ |
1189 | GSocket *GSocket_new(void) | |
1190 | { | |
1191 | GSocket *newsocket = new GSocket(); | |
1192 | if(newsocket->IsOk()) | |
1193 | return newsocket; | |
1194 | delete newsocket; | |
1195 | return NULL; | |
1196 | } | |
1197 | ||
f7ff39c6 DE |
1198 | |
1199 | /* | |
1200 | * ------------------------------------------------------------------------- | |
1201 | * GAddress | |
1202 | * ------------------------------------------------------------------------- | |
1203 | */ | |
1204 | ||
1205 | /* CHECK_ADDRESS verifies that the current address family is either | |
1206 | * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it | |
1207 | * initalizes it to be a GSOCK_*family*. In other cases, it returns | |
1208 | * an appropiate error code. | |
1209 | * | |
1210 | * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error. | |
1211 | */ | |
1212 | #define CHECK_ADDRESS(address, family) \ | |
1213 | { \ | |
1214 | if (address->m_family == GSOCK_NOFAMILY) \ | |
1215 | if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \ | |
1216 | return address->m_error; \ | |
1217 | if (address->m_family != GSOCK_##family) \ | |
1218 | { \ | |
1219 | address->m_error = GSOCK_INVADDR; \ | |
1220 | return GSOCK_INVADDR; \ | |
1221 | } \ | |
1222 | } | |
1223 | ||
1224 | #define CHECK_ADDRESS_RETVAL(address, family, retval) \ | |
1225 | { \ | |
1226 | if (address->m_family == GSOCK_NOFAMILY) \ | |
1227 | if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \ | |
1228 | return retval; \ | |
1229 | if (address->m_family != GSOCK_##family) \ | |
1230 | { \ | |
1231 | address->m_error = GSOCK_INVADDR; \ | |
1232 | return retval; \ | |
1233 | } \ | |
1234 | } | |
1235 | ||
1236 | ||
1237 | GAddress *GAddress_new(void) | |
1238 | { | |
1239 | GAddress *address; | |
1240 | ||
1241 | if ((address = (GAddress *) malloc(sizeof(GAddress))) == NULL) | |
1242 | return NULL; | |
1243 | ||
1244 | address->m_family = GSOCK_NOFAMILY; | |
1245 | address->m_addr = NULL; | |
1246 | address->m_len = 0; | |
1247 | ||
1248 | return address; | |
1249 | } | |
1250 | ||
1251 | GAddress *GAddress_copy(GAddress *address) | |
1252 | { | |
1253 | GAddress *addr2; | |
1254 | ||
1255 | assert(address != NULL); | |
1256 | ||
1257 | if ((addr2 = (GAddress *) malloc(sizeof(GAddress))) == NULL) | |
1258 | return NULL; | |
1259 | ||
1260 | memcpy(addr2, address, sizeof(GAddress)); | |
1261 | ||
1262 | if (address->m_addr) | |
1263 | { | |
1264 | addr2->m_addr = (struct sockaddr *) malloc(addr2->m_len); | |
1265 | if (addr2->m_addr == NULL) | |
1266 | { | |
1267 | free(addr2); | |
1268 | return NULL; | |
1269 | } | |
1270 | memcpy(addr2->m_addr, address->m_addr, addr2->m_len); | |
1271 | } | |
1272 | ||
1273 | return addr2; | |
1274 | } | |
1275 | ||
1276 | void GAddress_destroy(GAddress *address) | |
1277 | { | |
1278 | assert(address != NULL); | |
1279 | ||
1280 | if (address->m_addr) | |
1281 | free(address->m_addr); | |
1282 | ||
1283 | free(address); | |
1284 | } | |
1285 | ||
1286 | void GAddress_SetFamily(GAddress *address, GAddressType type) | |
1287 | { | |
1288 | assert(address != NULL); | |
1289 | ||
1290 | address->m_family = type; | |
1291 | } | |
1292 | ||
1293 | GAddressType GAddress_GetFamily(GAddress *address) | |
1294 | { | |
1295 | assert(address != NULL); | |
1296 | ||
1297 | return address->m_family; | |
1298 | } | |
1299 | ||
1300 | GSocketError _GAddress_translate_from(GAddress *address, | |
1301 | struct sockaddr *addr, int len) | |
1302 | { | |
1303 | address->m_realfamily = addr->sa_family; | |
1304 | switch (addr->sa_family) | |
1305 | { | |
1306 | case AF_INET: | |
1307 | address->m_family = GSOCK_INET; | |
1308 | break; | |
1309 | case AF_UNIX: | |
1310 | address->m_family = GSOCK_UNIX; | |
1311 | break; | |
1312 | #ifdef AF_INET6 | |
1313 | case AF_INET6: | |
1314 | address->m_family = GSOCK_INET6; | |
1315 | break; | |
1316 | #endif | |
1317 | default: | |
1318 | { | |
1319 | address->m_error = GSOCK_INVOP; | |
1320 | return GSOCK_INVOP; | |
1321 | } | |
1322 | } | |
1323 | ||
1324 | if (address->m_addr) | |
1325 | free(address->m_addr); | |
1326 | ||
1327 | address->m_len = len; | |
1328 | address->m_addr = (struct sockaddr *) malloc(len); | |
1329 | ||
1330 | if (address->m_addr == NULL) | |
1331 | { | |
1332 | address->m_error = GSOCK_MEMERR; | |
1333 | return GSOCK_MEMERR; | |
1334 | } | |
1335 | memcpy(address->m_addr, addr, len); | |
1336 | ||
1337 | return GSOCK_NOERROR; | |
1338 | } | |
1339 | ||
1340 | GSocketError _GAddress_translate_to(GAddress *address, | |
1341 | struct sockaddr **addr, int *len) | |
1342 | { | |
1343 | if (!address->m_addr) | |
1344 | { | |
1345 | address->m_error = GSOCK_INVADDR; | |
1346 | return GSOCK_INVADDR; | |
1347 | } | |
1348 | ||
1349 | *len = address->m_len; | |
1350 | *addr = (struct sockaddr *) malloc(address->m_len); | |
1351 | if (*addr == NULL) | |
1352 | { | |
1353 | address->m_error = GSOCK_MEMERR; | |
1354 | return GSOCK_MEMERR; | |
1355 | } | |
1356 | ||
1357 | memcpy(*addr, address->m_addr, address->m_len); | |
1358 | return GSOCK_NOERROR; | |
1359 | } | |
1360 | ||
1361 | /* | |
1362 | * ------------------------------------------------------------------------- | |
1363 | * Internet address family | |
1364 | * ------------------------------------------------------------------------- | |
1365 | */ | |
1366 | ||
1367 | GSocketError _GAddress_Init_INET(GAddress *address) | |
1368 | { | |
1369 | address->m_len = sizeof(struct sockaddr_in); | |
1370 | address->m_addr = (struct sockaddr *) malloc(address->m_len); | |
1371 | if (address->m_addr == NULL) | |
1372 | { | |
1373 | address->m_error = GSOCK_MEMERR; | |
1374 | return GSOCK_MEMERR; | |
1375 | } | |
1376 | ||
1377 | address->m_family = GSOCK_INET; | |
1378 | address->m_realfamily = AF_INET; | |
1379 | ((struct sockaddr_in *)address->m_addr)->sin_family = AF_INET; | |
1380 | ((struct sockaddr_in *)address->m_addr)->sin_addr.s_addr = INADDR_ANY; | |
1381 | ||
1382 | return GSOCK_NOERROR; | |
1383 | } | |
1384 | ||
1385 | GSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname) | |
1386 | { | |
1387 | struct hostent *he; | |
1388 | struct in_addr *addr; | |
1389 | ||
1390 | assert(address != NULL); | |
1391 | ||
1392 | CHECK_ADDRESS(address, INET); | |
1393 | ||
1394 | addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr); | |
1395 | ||
1396 | addr->s_addr = inet_addr(hostname); | |
1397 | ||
1398 | /* If it is a numeric host name, convert it now */ | |
1399 | if (addr->s_addr == INADDR_NONE) | |
1400 | { | |
1401 | struct in_addr *array_addr; | |
1402 | ||
1403 | /* It is a real name, we solve it */ | |
1404 | if ((he = gethostbyname(hostname)) == NULL) | |
1405 | { | |
1406 | /* addr->s_addr = INADDR_NONE just done by inet_addr() above */ | |
1407 | address->m_error = GSOCK_NOHOST; | |
1408 | return GSOCK_NOHOST; | |
1409 | } | |
1410 | array_addr = (struct in_addr *) *(he->h_addr_list); | |
1411 | addr->s_addr = array_addr[0].s_addr; | |
1412 | } | |
1413 | return GSOCK_NOERROR; | |
1414 | } | |
1415 | ||
1416 | GSocketError GAddress_INET_SetAnyAddress(GAddress *address) | |
1417 | { | |
1418 | return GAddress_INET_SetHostAddress(address, INADDR_ANY); | |
1419 | } | |
1420 | ||
1421 | GSocketError GAddress_INET_SetHostAddress(GAddress *address, | |
1422 | unsigned long hostaddr) | |
1423 | { | |
1424 | struct in_addr *addr; | |
1425 | ||
1426 | assert(address != NULL); | |
1427 | ||
1428 | CHECK_ADDRESS(address, INET); | |
1429 | ||
1430 | addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr); | |
d0ee33f5 | 1431 | addr->s_addr = htonl(hostaddr); |
f7ff39c6 DE |
1432 | |
1433 | return GSOCK_NOERROR; | |
1434 | } | |
1435 | ||
1436 | GSocketError GAddress_INET_SetPortName(GAddress *address, const char *port, | |
1437 | const char *protocol) | |
1438 | { | |
1439 | struct servent *se; | |
1440 | struct sockaddr_in *addr; | |
1441 | ||
1442 | assert(address != NULL); | |
1443 | CHECK_ADDRESS(address, INET); | |
1444 | ||
1445 | if (!port) | |
1446 | { | |
1447 | address->m_error = GSOCK_INVPORT; | |
1448 | return GSOCK_INVPORT; | |
1449 | } | |
1450 | ||
1451 | se = getservbyname(port, protocol); | |
1452 | if (!se) | |
1453 | { | |
1454 | if (isdigit(port[0])) | |
1455 | { | |
1456 | int port_int; | |
1457 | ||
1458 | port_int = atoi(port); | |
1459 | addr = (struct sockaddr_in *)address->m_addr; | |
1460 | addr->sin_port = htons((u_short) port_int); | |
1461 | return GSOCK_NOERROR; | |
1462 | } | |
1463 | ||
1464 | address->m_error = GSOCK_INVPORT; | |
1465 | return GSOCK_INVPORT; | |
1466 | } | |
1467 | ||
1468 | addr = (struct sockaddr_in *)address->m_addr; | |
1469 | addr->sin_port = se->s_port; | |
1470 | ||
1471 | return GSOCK_NOERROR; | |
1472 | } | |
1473 | ||
1474 | GSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port) | |
1475 | { | |
1476 | struct sockaddr_in *addr; | |
1477 | ||
1478 | assert(address != NULL); | |
1479 | CHECK_ADDRESS(address, INET); | |
1480 | ||
1481 | addr = (struct sockaddr_in *)address->m_addr; | |
1482 | addr->sin_port = htons(port); | |
1483 | ||
1484 | return GSOCK_NOERROR; | |
1485 | } | |
1486 | ||
1487 | GSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname, size_t sbuf) | |
1488 | { | |
1489 | struct hostent *he; | |
1490 | char *addr_buf; | |
1491 | struct sockaddr_in *addr; | |
1492 | ||
1493 | assert(address != NULL); | |
1494 | CHECK_ADDRESS(address, INET); | |
1495 | ||
1496 | addr = (struct sockaddr_in *)address->m_addr; | |
1497 | addr_buf = (char *)&(addr->sin_addr); | |
1498 | ||
1499 | he = gethostbyaddr(addr_buf, sizeof(addr->sin_addr), AF_INET); | |
1500 | if (he == NULL) | |
1501 | { | |
1502 | address->m_error = GSOCK_NOHOST; | |
1503 | return GSOCK_NOHOST; | |
1504 | } | |
1505 | ||
1506 | strncpy(hostname, he->h_name, sbuf); | |
1507 | ||
1508 | return GSOCK_NOERROR; | |
1509 | } | |
1510 | ||
1511 | unsigned long GAddress_INET_GetHostAddress(GAddress *address) | |
1512 | { | |
1513 | struct sockaddr_in *addr; | |
1514 | ||
1515 | assert(address != NULL); | |
1516 | CHECK_ADDRESS_RETVAL(address, INET, 0); | |
1517 | ||
1518 | addr = (struct sockaddr_in *)address->m_addr; | |
1519 | ||
1520 | return ntohl(addr->sin_addr.s_addr); | |
1521 | } | |
1522 | ||
1523 | unsigned short GAddress_INET_GetPort(GAddress *address) | |
1524 | { | |
1525 | struct sockaddr_in *addr; | |
1526 | ||
1527 | assert(address != NULL); | |
1528 | CHECK_ADDRESS_RETVAL(address, INET, 0); | |
1529 | ||
1530 | addr = (struct sockaddr_in *)address->m_addr; | |
1531 | return ntohs(addr->sin_port); | |
1532 | } | |
1533 | ||
1534 | /* | |
1535 | * ------------------------------------------------------------------------- | |
1536 | * Unix address family | |
1537 | * ------------------------------------------------------------------------- | |
1538 | */ | |
1539 | ||
1540 | GSocketError _GAddress_Init_UNIX(GAddress *address) | |
1541 | { | |
1542 | assert (address != NULL); | |
1543 | address->m_error = GSOCK_INVADDR; | |
1544 | return GSOCK_INVADDR; | |
1545 | } | |
1546 | ||
907173e5 | 1547 | GSocketError GAddress_UNIX_SetPath(GAddress *address, const char *WXUNUSED(path)) |
f7ff39c6 | 1548 | { |
f7ff39c6 DE |
1549 | assert (address != NULL); |
1550 | address->m_error = GSOCK_INVADDR; | |
1551 | return GSOCK_INVADDR; | |
1552 | } | |
1553 | ||
907173e5 | 1554 | GSocketError GAddress_UNIX_GetPath(GAddress *address, char *WXUNUSED(path), size_t WXUNUSED(sbuf)) |
f7ff39c6 | 1555 | { |
f7ff39c6 DE |
1556 | assert (address != NULL); |
1557 | address->m_error = GSOCK_INVADDR; | |
1558 | return GSOCK_INVADDR; | |
1559 | } | |
1560 | ||
1561 | #else /* !wxUSE_SOCKETS */ | |
1562 | ||
1563 | /* | |
1564 | * Translation unit shouldn't be empty, so include this typedef to make the | |
1565 | * compiler (VC++ 6.0, for example) happy | |
1566 | */ | |
1567 | typedef void (*wxDummy)(); | |
1568 | ||
1569 | #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */ |