]>
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 | { |
c90cc42e DE |
225 | shutdown(m_fd, 2); |
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 | */ | |
c90cc42e DE |
402 | if (m_reusable) { |
403 | setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&arg, sizeof(u_long)); | |
f7ff39c6 DE |
404 | } |
405 | ||
406 | /* Bind to the local address, | |
407 | * retrieve the actual address bound, | |
408 | * and listen up to 5 connections. | |
409 | */ | |
c90cc42e DE |
410 | if ((bind(m_fd, m_local->m_addr, m_local->m_len) != 0) || |
411 | (getsockname(m_fd, | |
412 | m_local->m_addr, | |
9e03e02d | 413 | (WX_SOCKLEN_T *)&m_local->m_len) != 0) || |
c90cc42e | 414 | (listen(m_fd, 5) != 0)) |
f7ff39c6 | 415 | { |
c90cc42e DE |
416 | Close(); |
417 | m_error = GSOCK_IOERR; | |
f7ff39c6 DE |
418 | return GSOCK_IOERR; |
419 | } | |
420 | ||
421 | return GSOCK_NOERROR; | |
422 | } | |
423 | ||
424 | /* GSocket_WaitConnection: | |
425 | * Waits for an incoming client connection. Returns a pointer to | |
426 | * a GSocket object, or NULL if there was an error, in which case | |
427 | * the last error field will be updated for the calling GSocket. | |
428 | * | |
429 | * Error codes (set in the calling GSocket) | |
430 | * GSOCK_INVSOCK - the socket is not valid or not a server. | |
431 | * GSOCK_TIMEDOUT - timeout, no incoming connections. | |
432 | * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking. | |
433 | * GSOCK_MEMERR - couldn't allocate memory. | |
434 | * GSOCK_IOERR - low-level error. | |
435 | */ | |
c90cc42e | 436 | GSocket *GSocket::WaitConnection() |
f7ff39c6 DE |
437 | { |
438 | GSocket *connection; | |
439 | struct sockaddr from; | |
9e03e02d | 440 | WX_SOCKLEN_T fromlen = sizeof(from); |
f7ff39c6 DE |
441 | GSocketError err; |
442 | u_long arg = 1; | |
443 | ||
c90cc42e | 444 | assert(this); |
f7ff39c6 DE |
445 | |
446 | /* Reenable CONNECTION events */ | |
c90cc42e | 447 | m_detected &= ~GSOCK_CONNECTION_FLAG; |
f7ff39c6 DE |
448 | |
449 | /* If the socket has already been created, we exit immediately */ | |
c90cc42e | 450 | if (m_fd == INVALID_SOCKET || !m_server) |
f7ff39c6 | 451 | { |
c90cc42e | 452 | m_error = GSOCK_INVSOCK; |
f7ff39c6 DE |
453 | return NULL; |
454 | } | |
455 | ||
456 | /* Create a GSocket object for the new connection */ | |
457 | connection = GSocket_new(); | |
458 | ||
459 | if (!connection) | |
460 | { | |
c90cc42e | 461 | m_error = GSOCK_MEMERR; |
f7ff39c6 DE |
462 | return NULL; |
463 | } | |
464 | ||
465 | /* Wait for a connection (with timeout) */ | |
c90cc42e | 466 | if (Input_Timeout() == GSOCK_TIMEDOUT) |
f7ff39c6 | 467 | { |
85431efa | 468 | delete connection; |
c90cc42e | 469 | /* m_error set by _GSocket_Input_Timeout */ |
f7ff39c6 DE |
470 | return NULL; |
471 | } | |
472 | ||
c90cc42e | 473 | connection->m_fd = accept(m_fd, &from, &fromlen); |
f7ff39c6 DE |
474 | |
475 | if (connection->m_fd == INVALID_SOCKET) | |
476 | { | |
477 | if (WSAGetLastError() == WSAEWOULDBLOCK) | |
c90cc42e | 478 | m_error = GSOCK_WOULDBLOCK; |
f7ff39c6 | 479 | else |
c90cc42e | 480 | m_error = GSOCK_IOERR; |
f7ff39c6 | 481 | |
85431efa | 482 | delete connection; |
f7ff39c6 DE |
483 | return NULL; |
484 | } | |
485 | ||
486 | /* Initialize all fields */ | |
948c96ef DE |
487 | connection->m_server = false; |
488 | connection->m_stream = true; | |
f7ff39c6 DE |
489 | |
490 | /* Setup the peer address field */ | |
491 | connection->m_peer = GAddress_new(); | |
492 | if (!connection->m_peer) | |
493 | { | |
85431efa | 494 | delete connection; |
c90cc42e | 495 | m_error = GSOCK_MEMERR; |
f7ff39c6 DE |
496 | return NULL; |
497 | } | |
498 | err = _GAddress_translate_from(connection->m_peer, &from, fromlen); | |
499 | if (err != GSOCK_NOERROR) | |
500 | { | |
501 | GAddress_destroy(connection->m_peer); | |
85431efa | 502 | delete connection; |
c90cc42e | 503 | m_error = err; |
f7ff39c6 DE |
504 | return NULL; |
505 | } | |
506 | ||
507 | ioctlsocket(connection->m_fd, FIONBIO, (u_long FAR *) &arg); | |
c90cc42e | 508 | gs_gui_functions->Enable_Events(connection); |
f7ff39c6 DE |
509 | |
510 | return connection; | |
511 | } | |
512 | ||
513 | /* GSocket_SetReusable: | |
514 | * Simply sets the m_resuable flag on the socket. GSocket_SetServer will | |
515 | * make the appropriate setsockopt() call. | |
516 | * Implemented as a GSocket function because clients (ie, wxSocketServer) | |
517 | * don't have access to the GSocket struct information. | |
3103e8a9 | 518 | * Returns true if the flag was set correctly, false if an error occurred |
f7ff39c6 DE |
519 | * (ie, if the parameter was NULL) |
520 | */ | |
948c96ef | 521 | bool GSocket::SetReusable() |
f7ff39c6 DE |
522 | { |
523 | /* socket must not be null, and must not be in use/already bound */ | |
c90cc42e | 524 | if (this && m_fd == INVALID_SOCKET) { |
948c96ef DE |
525 | m_reusable = true; |
526 | return true; | |
f7ff39c6 | 527 | } |
948c96ef | 528 | return false; |
f7ff39c6 DE |
529 | } |
530 | ||
531 | /* Client specific parts */ | |
532 | ||
533 | /* GSocket_Connect: | |
534 | * For stream (connection oriented) sockets, GSocket_Connect() tries | |
535 | * to establish a client connection to a server using the peer address | |
536 | * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the | |
3103e8a9 | 537 | * connection has been successfully established, or one of the error |
f7ff39c6 DE |
538 | * codes listed below. Note that for nonblocking sockets, a return |
539 | * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection | |
540 | * request can be completed later; you should use GSocket_Select() | |
541 | * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the | |
542 | * corresponding asynchronous events. | |
543 | * | |
544 | * For datagram (non connection oriented) sockets, GSocket_Connect() | |
545 | * just sets the peer address established with GSocket_SetPeer() as | |
546 | * default destination. | |
547 | * | |
548 | * Error codes: | |
549 | * GSOCK_INVSOCK - the socket is in use or not valid. | |
550 | * GSOCK_INVADDR - the peer address has not been established. | |
551 | * GSOCK_TIMEDOUT - timeout, the connection failed. | |
552 | * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only) | |
553 | * GSOCK_MEMERR - couldn't allocate memory. | |
554 | * GSOCK_IOERR - low-level error. | |
555 | */ | |
c90cc42e | 556 | GSocketError GSocket::Connect(GSocketStream stream) |
f7ff39c6 DE |
557 | { |
558 | int ret, err; | |
559 | u_long arg = 1; | |
560 | ||
c90cc42e | 561 | assert(this); |
f7ff39c6 DE |
562 | |
563 | /* Enable CONNECTION events (needed for nonblocking connections) */ | |
c90cc42e | 564 | m_detected &= ~GSOCK_CONNECTION_FLAG; |
f7ff39c6 | 565 | |
c90cc42e | 566 | if (m_fd != INVALID_SOCKET) |
f7ff39c6 | 567 | { |
c90cc42e | 568 | m_error = GSOCK_INVSOCK; |
f7ff39c6 DE |
569 | return GSOCK_INVSOCK; |
570 | } | |
571 | ||
c90cc42e | 572 | if (!m_peer) |
f7ff39c6 | 573 | { |
c90cc42e | 574 | m_error = GSOCK_INVADDR; |
f7ff39c6 DE |
575 | return GSOCK_INVADDR; |
576 | } | |
577 | ||
578 | /* Streamed or dgram socket? */ | |
c90cc42e | 579 | m_stream = (stream == GSOCK_STREAMED); |
948c96ef DE |
580 | m_server = false; |
581 | m_establishing = false; | |
f7ff39c6 DE |
582 | |
583 | /* Create the socket */ | |
c90cc42e DE |
584 | m_fd = socket(m_peer->m_realfamily, |
585 | m_stream? SOCK_STREAM : SOCK_DGRAM, 0); | |
f7ff39c6 | 586 | |
c90cc42e | 587 | if (m_fd == INVALID_SOCKET) |
f7ff39c6 | 588 | { |
c90cc42e | 589 | m_error = GSOCK_IOERR; |
f7ff39c6 DE |
590 | return GSOCK_IOERR; |
591 | } | |
592 | ||
c90cc42e DE |
593 | ioctlsocket(m_fd, FIONBIO, (u_long FAR *) &arg); |
594 | gs_gui_functions->Enable_Events(this); | |
f7ff39c6 DE |
595 | |
596 | /* Connect it to the peer address, with a timeout (see below) */ | |
c90cc42e | 597 | ret = connect(m_fd, m_peer->m_addr, m_peer->m_len); |
f7ff39c6 DE |
598 | |
599 | if (ret == SOCKET_ERROR) | |
600 | { | |
601 | err = WSAGetLastError(); | |
602 | ||
603 | /* If connect failed with EWOULDBLOCK and the GSocket object | |
604 | * is in blocking mode, we select() for the specified timeout | |
605 | * checking for writability to see if the connection request | |
606 | * completes. | |
607 | */ | |
c90cc42e | 608 | if ((err == WSAEWOULDBLOCK) && (!m_non_blocking)) |
f7ff39c6 | 609 | { |
c90cc42e | 610 | err = Connect_Timeout(); |
f7ff39c6 DE |
611 | |
612 | if (err != GSOCK_NOERROR) | |
613 | { | |
c90cc42e DE |
614 | Close(); |
615 | /* m_error is set in _GSocket_Connect_Timeout */ | |
f7ff39c6 DE |
616 | } |
617 | ||
618 | return (GSocketError) err; | |
619 | } | |
620 | ||
621 | /* If connect failed with EWOULDBLOCK and the GSocket object | |
622 | * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK | |
623 | * (and return GSOCK_WOULDBLOCK) but we don't close the socket; | |
624 | * this way if the connection completes, a GSOCK_CONNECTION | |
625 | * event will be generated, if enabled. | |
626 | */ | |
c90cc42e | 627 | if ((err == WSAEWOULDBLOCK) && (m_non_blocking)) |
f7ff39c6 | 628 | { |
948c96ef | 629 | m_establishing = true; |
c90cc42e | 630 | m_error = GSOCK_WOULDBLOCK; |
f7ff39c6 DE |
631 | return GSOCK_WOULDBLOCK; |
632 | } | |
633 | ||
634 | /* If connect failed with an error other than EWOULDBLOCK, | |
635 | * then the call to GSocket_Connect() has failed. | |
636 | */ | |
c90cc42e DE |
637 | Close(); |
638 | m_error = GSOCK_IOERR; | |
f7ff39c6 DE |
639 | return GSOCK_IOERR; |
640 | } | |
641 | ||
642 | return GSOCK_NOERROR; | |
643 | } | |
644 | ||
645 | /* Datagram sockets */ | |
646 | ||
647 | /* GSocket_SetNonOriented: | |
648 | * Sets up this socket as a non-connection oriented (datagram) socket. | |
649 | * Before using this function, the local address must have been set | |
650 | * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR | |
651 | * on success, or one of the following otherwise. | |
652 | * | |
653 | * Error codes: | |
654 | * GSOCK_INVSOCK - the socket is in use. | |
655 | * GSOCK_INVADDR - the local address has not been set. | |
656 | * GSOCK_IOERR - low-level error. | |
657 | */ | |
c90cc42e | 658 | GSocketError GSocket::SetNonOriented() |
f7ff39c6 DE |
659 | { |
660 | u_long arg = 1; | |
661 | ||
c90cc42e | 662 | assert(this); |
f7ff39c6 | 663 | |
c90cc42e | 664 | if (m_fd != INVALID_SOCKET) |
f7ff39c6 | 665 | { |
c90cc42e | 666 | m_error = GSOCK_INVSOCK; |
f7ff39c6 DE |
667 | return GSOCK_INVSOCK; |
668 | } | |
669 | ||
c90cc42e | 670 | if (!m_local) |
f7ff39c6 | 671 | { |
c90cc42e | 672 | m_error = GSOCK_INVADDR; |
f7ff39c6 DE |
673 | return GSOCK_INVADDR; |
674 | } | |
675 | ||
676 | /* Initialize all fields */ | |
948c96ef DE |
677 | m_stream = false; |
678 | m_server = false; | |
f7ff39c6 DE |
679 | |
680 | /* Create the socket */ | |
c90cc42e | 681 | m_fd = socket(m_local->m_realfamily, SOCK_DGRAM, 0); |
f7ff39c6 | 682 | |
c90cc42e | 683 | if (m_fd == INVALID_SOCKET) |
f7ff39c6 | 684 | { |
c90cc42e | 685 | m_error = GSOCK_IOERR; |
f7ff39c6 DE |
686 | return GSOCK_IOERR; |
687 | } | |
688 | ||
c90cc42e DE |
689 | ioctlsocket(m_fd, FIONBIO, (u_long FAR *) &arg); |
690 | gs_gui_functions->Enable_Events(this); | |
f7ff39c6 DE |
691 | |
692 | /* Bind to the local address, | |
693 | * and retrieve the actual address bound. | |
694 | */ | |
c90cc42e DE |
695 | if ((bind(m_fd, m_local->m_addr, m_local->m_len) != 0) || |
696 | (getsockname(m_fd, | |
697 | m_local->m_addr, | |
9e03e02d | 698 | (WX_SOCKLEN_T *)&m_local->m_len) != 0)) |
f7ff39c6 | 699 | { |
c90cc42e DE |
700 | Close(); |
701 | m_error = GSOCK_IOERR; | |
f7ff39c6 DE |
702 | return GSOCK_IOERR; |
703 | } | |
704 | ||
705 | return GSOCK_NOERROR; | |
706 | } | |
707 | ||
708 | /* Generic IO */ | |
709 | ||
710 | /* Like recv(), send(), ... */ | |
c90cc42e | 711 | int GSocket::Read(char *buffer, int size) |
f7ff39c6 DE |
712 | { |
713 | int ret; | |
714 | ||
c90cc42e | 715 | assert(this); |
f7ff39c6 DE |
716 | |
717 | /* Reenable INPUT events */ | |
c90cc42e | 718 | m_detected &= ~GSOCK_INPUT_FLAG; |
f7ff39c6 | 719 | |
c90cc42e | 720 | if (m_fd == INVALID_SOCKET || m_server) |
f7ff39c6 | 721 | { |
c90cc42e | 722 | m_error = GSOCK_INVSOCK; |
f7ff39c6 DE |
723 | return -1; |
724 | } | |
725 | ||
726 | /* If the socket is blocking, wait for data (with a timeout) */ | |
c90cc42e | 727 | if (Input_Timeout() == GSOCK_TIMEDOUT) |
976abb72 VZ |
728 | { |
729 | m_error = GSOCK_TIMEDOUT; | |
f7ff39c6 | 730 | return -1; |
976abb72 | 731 | } |
f7ff39c6 DE |
732 | |
733 | /* Read the data */ | |
c90cc42e DE |
734 | if (m_stream) |
735 | ret = Recv_Stream(buffer, size); | |
f7ff39c6 | 736 | else |
c90cc42e | 737 | ret = Recv_Dgram(buffer, size); |
f7ff39c6 DE |
738 | |
739 | if (ret == SOCKET_ERROR) | |
740 | { | |
741 | if (WSAGetLastError() != WSAEWOULDBLOCK) | |
c90cc42e | 742 | m_error = GSOCK_IOERR; |
f7ff39c6 | 743 | else |
c90cc42e | 744 | m_error = GSOCK_WOULDBLOCK; |
f7ff39c6 DE |
745 | return -1; |
746 | } | |
747 | ||
748 | return ret; | |
749 | } | |
750 | ||
c90cc42e | 751 | int GSocket::Write(const char *buffer, int size) |
f7ff39c6 DE |
752 | { |
753 | int ret; | |
754 | ||
c90cc42e | 755 | assert(this); |
f7ff39c6 | 756 | |
c90cc42e | 757 | if (m_fd == INVALID_SOCKET || m_server) |
f7ff39c6 | 758 | { |
c90cc42e | 759 | m_error = GSOCK_INVSOCK; |
f7ff39c6 DE |
760 | return -1; |
761 | } | |
762 | ||
763 | /* If the socket is blocking, wait for writability (with a timeout) */ | |
c90cc42e | 764 | if (Output_Timeout() == GSOCK_TIMEDOUT) |
f7ff39c6 DE |
765 | return -1; |
766 | ||
767 | /* Write the data */ | |
c90cc42e DE |
768 | if (m_stream) |
769 | ret = Send_Stream(buffer, size); | |
f7ff39c6 | 770 | else |
c90cc42e | 771 | ret = Send_Dgram(buffer, size); |
f7ff39c6 DE |
772 | |
773 | if (ret == SOCKET_ERROR) | |
774 | { | |
775 | if (WSAGetLastError() != WSAEWOULDBLOCK) | |
c90cc42e | 776 | m_error = GSOCK_IOERR; |
f7ff39c6 | 777 | else |
c90cc42e | 778 | m_error = GSOCK_WOULDBLOCK; |
f7ff39c6 DE |
779 | |
780 | /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect | |
781 | * does). Once the first OUTPUT event is received, users can assume | |
782 | * that the socket is writable until a read operation fails. Only then | |
783 | * will further OUTPUT events be posted. | |
784 | */ | |
c90cc42e | 785 | m_detected &= ~GSOCK_OUTPUT_FLAG; |
f7ff39c6 DE |
786 | return -1; |
787 | } | |
788 | ||
789 | return ret; | |
790 | } | |
791 | ||
792 | /* GSocket_Select: | |
793 | * Polls the socket to determine its status. This function will | |
794 | * check for the events specified in the 'flags' parameter, and | |
795 | * it will return a mask indicating which operations can be | |
796 | * performed. This function won't block, regardless of the | |
797 | * mode (blocking | nonblocking) of the socket. | |
798 | */ | |
c90cc42e | 799 | GSocketEventFlags GSocket::Select(GSocketEventFlags flags) |
f7ff39c6 | 800 | { |
c90cc42e | 801 | if (!gs_gui_functions->CanUseEventLoop()) |
f7ff39c6 DE |
802 | { |
803 | GSocketEventFlags result = 0; | |
804 | fd_set readfds; | |
805 | fd_set writefds; | |
806 | fd_set exceptfds; | |
807 | ||
c90cc42e | 808 | assert(this); |
f7ff39c6 DE |
809 | |
810 | FD_ZERO(&readfds); | |
811 | FD_ZERO(&writefds); | |
812 | FD_ZERO(&exceptfds); | |
c90cc42e | 813 | FD_SET(m_fd, &readfds); |
f7ff39c6 | 814 | if (flags & GSOCK_OUTPUT_FLAG || flags & GSOCK_CONNECTION_FLAG) |
c90cc42e DE |
815 | FD_SET(m_fd, &writefds); |
816 | FD_SET(m_fd, &exceptfds); | |
f7ff39c6 DE |
817 | |
818 | /* Check 'sticky' CONNECTION flag first */ | |
c90cc42e | 819 | result |= (GSOCK_CONNECTION_FLAG & m_detected); |
f7ff39c6 DE |
820 | |
821 | /* If we have already detected a LOST event, then don't try | |
822 | * to do any further processing. | |
823 | */ | |
c90cc42e | 824 | if ((m_detected & GSOCK_LOST_FLAG) != 0) |
f7ff39c6 | 825 | { |
948c96ef | 826 | m_establishing = false; |
f7ff39c6 DE |
827 | |
828 | return (GSOCK_LOST_FLAG & flags); | |
829 | } | |
830 | ||
831 | /* Try select now */ | |
c90cc42e DE |
832 | if (select(m_fd + 1, &readfds, &writefds, &exceptfds, |
833 | &m_timeout) <= 0) | |
f7ff39c6 DE |
834 | { |
835 | /* What to do here? */ | |
836 | return (result & flags); | |
837 | } | |
838 | ||
839 | /* Check for readability */ | |
c90cc42e | 840 | if (FD_ISSET(m_fd, &readfds)) |
f7ff39c6 DE |
841 | { |
842 | char c; | |
843 | ||
c90cc42e | 844 | if (!m_stream || recv(m_fd, &c, 1, MSG_PEEK) > 0) |
f7ff39c6 DE |
845 | { |
846 | result |= GSOCK_INPUT_FLAG; | |
847 | } | |
848 | else | |
849 | { | |
c90cc42e | 850 | if (m_server && m_stream) |
f7ff39c6 DE |
851 | { |
852 | result |= GSOCK_CONNECTION_FLAG; | |
c90cc42e | 853 | m_detected |= GSOCK_CONNECTION_FLAG; |
f7ff39c6 DE |
854 | } |
855 | else | |
856 | { | |
c90cc42e | 857 | m_detected = GSOCK_LOST_FLAG; |
948c96ef | 858 | m_establishing = false; |
f7ff39c6 DE |
859 | |
860 | /* LOST event: Abort any further processing */ | |
861 | return (GSOCK_LOST_FLAG & flags); | |
862 | } | |
863 | } | |
864 | } | |
865 | ||
866 | /* Check for writability */ | |
c90cc42e | 867 | if (FD_ISSET(m_fd, &writefds)) |
f7ff39c6 | 868 | { |
c90cc42e | 869 | if (m_establishing && !m_server) |
f7ff39c6 DE |
870 | { |
871 | int error; | |
9e03e02d | 872 | WX_SOCKLEN_T len = sizeof(error); |
f7ff39c6 | 873 | |
948c96ef | 874 | m_establishing = false; |
f7ff39c6 | 875 | |
c90cc42e | 876 | getsockopt(m_fd, SOL_SOCKET, SO_ERROR, (char*)&error, &len); |
f7ff39c6 DE |
877 | |
878 | if (error) | |
879 | { | |
c90cc42e | 880 | m_detected = GSOCK_LOST_FLAG; |
f7ff39c6 DE |
881 | |
882 | /* LOST event: Abort any further processing */ | |
883 | return (GSOCK_LOST_FLAG & flags); | |
884 | } | |
885 | else | |
886 | { | |
887 | result |= GSOCK_CONNECTION_FLAG; | |
c90cc42e | 888 | m_detected |= GSOCK_CONNECTION_FLAG; |
f7ff39c6 DE |
889 | } |
890 | } | |
891 | else | |
892 | { | |
893 | result |= GSOCK_OUTPUT_FLAG; | |
894 | } | |
895 | } | |
896 | ||
897 | /* Check for exceptions and errors (is this useful in Unices?) */ | |
c90cc42e | 898 | if (FD_ISSET(m_fd, &exceptfds)) |
f7ff39c6 | 899 | { |
948c96ef | 900 | m_establishing = false; |
c90cc42e | 901 | m_detected = GSOCK_LOST_FLAG; |
f7ff39c6 DE |
902 | |
903 | /* LOST event: Abort any further processing */ | |
904 | return (GSOCK_LOST_FLAG & flags); | |
905 | } | |
906 | ||
907 | return (result & flags); | |
908 | } | |
909 | else /* USE_GUI() */ | |
910 | { | |
c90cc42e DE |
911 | assert(this); |
912 | return flags & m_detected; | |
f7ff39c6 DE |
913 | } |
914 | } | |
915 | ||
916 | /* Attributes */ | |
917 | ||
918 | /* GSocket_SetNonBlocking: | |
919 | * Sets the socket to non-blocking mode. All IO calls will return | |
920 | * immediately. | |
921 | */ | |
c90cc42e | 922 | void GSocket::SetNonBlocking(bool non_block) |
f7ff39c6 | 923 | { |
c90cc42e | 924 | assert(this); |
f7ff39c6 | 925 | |
c90cc42e | 926 | m_non_blocking = non_block; |
f7ff39c6 DE |
927 | } |
928 | ||
929 | /* GSocket_SetTimeout: | |
930 | * Sets the timeout for blocking calls. Time is expressed in | |
931 | * milliseconds. | |
932 | */ | |
c90cc42e | 933 | void GSocket::SetTimeout(unsigned long millis) |
f7ff39c6 | 934 | { |
c90cc42e | 935 | assert(this); |
f7ff39c6 | 936 | |
c90cc42e DE |
937 | m_timeout.tv_sec = (millis / 1000); |
938 | m_timeout.tv_usec = (millis % 1000) * 1000; | |
f7ff39c6 DE |
939 | } |
940 | ||
941 | /* GSocket_GetError: | |
3103e8a9 | 942 | * Returns the last error occurred for this socket. Note that successful |
f7ff39c6 DE |
943 | * operations do not clear this back to GSOCK_NOERROR, so use it only |
944 | * after an error. | |
945 | */ | |
a4506848 | 946 | GSocketError WXDLLIMPEXP_NET GSocket::GetError() |
f7ff39c6 | 947 | { |
c90cc42e | 948 | assert(this); |
f7ff39c6 | 949 | |
c90cc42e | 950 | return m_error; |
f7ff39c6 DE |
951 | } |
952 | ||
953 | /* Callbacks */ | |
954 | ||
955 | /* GSOCK_INPUT: | |
956 | * There is data to be read in the input buffer. If, after a read | |
957 | * operation, there is still data available, the callback function will | |
958 | * be called again. | |
959 | * GSOCK_OUTPUT: | |
960 | * The socket is available for writing. That is, the next write call | |
961 | * won't block. This event is generated only once, when the connection is | |
962 | * first established, and then only if a call failed with GSOCK_WOULDBLOCK, | |
963 | * when the output buffer empties again. This means that the app should | |
964 | * assume that it can write since the first OUTPUT event, and no more | |
965 | * OUTPUT events will be generated unless an error occurs. | |
966 | * GSOCK_CONNECTION: | |
3103e8a9 | 967 | * Connection successfully established, for client sockets, or incoming |
f7ff39c6 DE |
968 | * client connection, for server sockets. Wait for this event (also watch |
969 | * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call. | |
970 | * GSOCK_LOST: | |
971 | * The connection is lost (or a connection request failed); this could | |
972 | * be due to a failure, or due to the peer closing it gracefully. | |
973 | */ | |
974 | ||
975 | /* GSocket_SetCallback: | |
976 | * Enables the callbacks specified by 'flags'. Note that 'flags' | |
977 | * may be a combination of flags OR'ed toghether, so the same | |
978 | * callback function can be made to accept different events. | |
979 | * The callback function must have the following prototype: | |
980 | * | |
981 | * void function(GSocket *socket, GSocketEvent event, char *cdata) | |
982 | */ | |
c90cc42e | 983 | void GSocket::SetCallback(GSocketEventFlags flags, |
f7ff39c6 DE |
984 | GSocketCallback callback, char *cdata) |
985 | { | |
986 | int count; | |
987 | ||
c90cc42e | 988 | assert(this); |
f7ff39c6 DE |
989 | |
990 | for (count = 0; count < GSOCK_MAX_EVENT; count++) | |
991 | { | |
992 | if ((flags & (1 << count)) != 0) | |
993 | { | |
c90cc42e DE |
994 | m_cbacks[count] = callback; |
995 | m_data[count] = cdata; | |
f7ff39c6 DE |
996 | } |
997 | } | |
998 | } | |
999 | ||
1000 | /* GSocket_UnsetCallback: | |
1001 | * Disables all callbacks specified by 'flags', which may be a | |
1002 | * combination of flags OR'ed toghether. | |
1003 | */ | |
c90cc42e | 1004 | void GSocket::UnsetCallback(GSocketEventFlags flags) |
f7ff39c6 DE |
1005 | { |
1006 | int count; | |
1007 | ||
c90cc42e | 1008 | assert(this); |
f7ff39c6 DE |
1009 | |
1010 | for (count = 0; count < GSOCK_MAX_EVENT; count++) | |
1011 | { | |
1012 | if ((flags & (1 << count)) != 0) | |
1013 | { | |
c90cc42e DE |
1014 | m_cbacks[count] = NULL; |
1015 | m_data[count] = NULL; | |
f7ff39c6 DE |
1016 | } |
1017 | } | |
1018 | } | |
1019 | ||
c90cc42e | 1020 | GSocketError GSocket::GetSockOpt(int level, int optname, |
f7ff39c6 DE |
1021 | void *optval, int *optlen) |
1022 | { | |
c90cc42e | 1023 | if (getsockopt(m_fd, level, optname, (char*)optval, optlen) == 0) |
f7ff39c6 DE |
1024 | { |
1025 | return GSOCK_NOERROR; | |
1026 | } | |
1027 | return GSOCK_OPTERR; | |
1028 | } | |
1029 | ||
c90cc42e | 1030 | GSocketError GSocket::SetSockOpt(int level, int optname, |
f7ff39c6 DE |
1031 | const void *optval, int optlen) |
1032 | { | |
c90cc42e | 1033 | if (setsockopt(m_fd, level, optname, (char*)optval, optlen) == 0) |
f7ff39c6 DE |
1034 | { |
1035 | return GSOCK_NOERROR; | |
1036 | } | |
1037 | return GSOCK_OPTERR; | |
1038 | } | |
1039 | ||
1040 | /* Internals (IO) */ | |
1041 | ||
1042 | /* _GSocket_Input_Timeout: | |
1043 | * For blocking sockets, wait until data is available or | |
1044 | * until timeout ellapses. | |
1045 | */ | |
c90cc42e | 1046 | GSocketError GSocket::Input_Timeout() |
f7ff39c6 DE |
1047 | { |
1048 | fd_set readfds; | |
1049 | ||
c90cc42e | 1050 | if (!m_non_blocking) |
f7ff39c6 DE |
1051 | { |
1052 | FD_ZERO(&readfds); | |
c90cc42e DE |
1053 | FD_SET(m_fd, &readfds); |
1054 | if (select(0, &readfds, NULL, NULL, &m_timeout) == 0) | |
f7ff39c6 | 1055 | { |
c90cc42e | 1056 | m_error = GSOCK_TIMEDOUT; |
f7ff39c6 DE |
1057 | return GSOCK_TIMEDOUT; |
1058 | } | |
1059 | } | |
1060 | return GSOCK_NOERROR; | |
1061 | } | |
1062 | ||
1063 | /* _GSocket_Output_Timeout: | |
1064 | * For blocking sockets, wait until data can be sent without | |
1065 | * blocking or until timeout ellapses. | |
1066 | */ | |
c90cc42e | 1067 | GSocketError GSocket::Output_Timeout() |
f7ff39c6 DE |
1068 | { |
1069 | fd_set writefds; | |
1070 | ||
c90cc42e | 1071 | if (!m_non_blocking) |
f7ff39c6 DE |
1072 | { |
1073 | FD_ZERO(&writefds); | |
c90cc42e DE |
1074 | FD_SET(m_fd, &writefds); |
1075 | if (select(0, NULL, &writefds, NULL, &m_timeout) == 0) | |
f7ff39c6 | 1076 | { |
c90cc42e | 1077 | m_error = GSOCK_TIMEDOUT; |
f7ff39c6 DE |
1078 | return GSOCK_TIMEDOUT; |
1079 | } | |
1080 | } | |
1081 | return GSOCK_NOERROR; | |
1082 | } | |
1083 | ||
1084 | /* _GSocket_Connect_Timeout: | |
1085 | * For blocking sockets, wait until the connection is | |
1086 | * established or fails, or until timeout ellapses. | |
1087 | */ | |
c90cc42e | 1088 | GSocketError GSocket::Connect_Timeout() |
f7ff39c6 DE |
1089 | { |
1090 | fd_set writefds; | |
1091 | fd_set exceptfds; | |
1092 | ||
1093 | FD_ZERO(&writefds); | |
1094 | FD_ZERO(&exceptfds); | |
c90cc42e DE |
1095 | FD_SET(m_fd, &writefds); |
1096 | FD_SET(m_fd, &exceptfds); | |
1097 | if (select(0, NULL, &writefds, &exceptfds, &m_timeout) == 0) | |
f7ff39c6 | 1098 | { |
c90cc42e | 1099 | m_error = GSOCK_TIMEDOUT; |
f7ff39c6 DE |
1100 | return GSOCK_TIMEDOUT; |
1101 | } | |
c90cc42e | 1102 | if (!FD_ISSET(m_fd, &writefds)) |
f7ff39c6 | 1103 | { |
c90cc42e | 1104 | m_error = GSOCK_IOERR; |
f7ff39c6 DE |
1105 | return GSOCK_IOERR; |
1106 | } | |
1107 | ||
1108 | return GSOCK_NOERROR; | |
1109 | } | |
1110 | ||
c90cc42e | 1111 | int GSocket::Recv_Stream(char *buffer, int size) |
f7ff39c6 | 1112 | { |
c90cc42e | 1113 | return recv(m_fd, buffer, size, 0); |
f7ff39c6 DE |
1114 | } |
1115 | ||
c90cc42e | 1116 | int GSocket::Recv_Dgram(char *buffer, int size) |
f7ff39c6 DE |
1117 | { |
1118 | struct sockaddr from; | |
9e03e02d | 1119 | WX_SOCKLEN_T fromlen = sizeof(from); |
f7ff39c6 DE |
1120 | int ret; |
1121 | GSocketError err; | |
1122 | ||
c90cc42e | 1123 | ret = recvfrom(m_fd, buffer, size, 0, &from, &fromlen); |
f7ff39c6 DE |
1124 | |
1125 | if (ret == SOCKET_ERROR) | |
1126 | return SOCKET_ERROR; | |
1127 | ||
1128 | /* Translate a system address into a GSocket address */ | |
c90cc42e | 1129 | if (!m_peer) |
f7ff39c6 | 1130 | { |
c90cc42e DE |
1131 | m_peer = GAddress_new(); |
1132 | if (!m_peer) | |
f7ff39c6 | 1133 | { |
c90cc42e | 1134 | m_error = GSOCK_MEMERR; |
f7ff39c6 DE |
1135 | return -1; |
1136 | } | |
1137 | } | |
c90cc42e | 1138 | err = _GAddress_translate_from(m_peer, &from, fromlen); |
f7ff39c6 DE |
1139 | if (err != GSOCK_NOERROR) |
1140 | { | |
c90cc42e DE |
1141 | GAddress_destroy(m_peer); |
1142 | m_peer = NULL; | |
1143 | m_error = err; | |
f7ff39c6 DE |
1144 | return -1; |
1145 | } | |
1146 | ||
1147 | return ret; | |
1148 | } | |
1149 | ||
c90cc42e | 1150 | int GSocket::Send_Stream(const char *buffer, int size) |
f7ff39c6 | 1151 | { |
c90cc42e | 1152 | return send(m_fd, buffer, size, 0); |
f7ff39c6 DE |
1153 | } |
1154 | ||
c90cc42e | 1155 | int GSocket::Send_Dgram(const char *buffer, int size) |
f7ff39c6 DE |
1156 | { |
1157 | struct sockaddr *addr; | |
1158 | int len, ret; | |
1159 | GSocketError err; | |
1160 | ||
c90cc42e | 1161 | if (!m_peer) |
f7ff39c6 | 1162 | { |
c90cc42e | 1163 | m_error = GSOCK_INVADDR; |
f7ff39c6 DE |
1164 | return -1; |
1165 | } | |
1166 | ||
c90cc42e | 1167 | err = _GAddress_translate_to(m_peer, &addr, &len); |
f7ff39c6 DE |
1168 | if (err != GSOCK_NOERROR) |
1169 | { | |
c90cc42e | 1170 | m_error = err; |
f7ff39c6 DE |
1171 | return -1; |
1172 | } | |
1173 | ||
c90cc42e | 1174 | ret = sendto(m_fd, buffer, size, 0, addr, len); |
f7ff39c6 DE |
1175 | |
1176 | /* Frees memory allocated by _GAddress_translate_to */ | |
1177 | free(addr); | |
1178 | ||
1179 | return ret; | |
1180 | } | |
1181 | ||
c90cc42e DE |
1182 | /* Compatibility functions for GSocket */ |
1183 | GSocket *GSocket_new(void) | |
1184 | { | |
1185 | GSocket *newsocket = new GSocket(); | |
1186 | if(newsocket->IsOk()) | |
1187 | return newsocket; | |
1188 | delete newsocket; | |
1189 | return NULL; | |
1190 | } | |
1191 | ||
f7ff39c6 DE |
1192 | |
1193 | /* | |
1194 | * ------------------------------------------------------------------------- | |
1195 | * GAddress | |
1196 | * ------------------------------------------------------------------------- | |
1197 | */ | |
1198 | ||
1199 | /* CHECK_ADDRESS verifies that the current address family is either | |
1200 | * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it | |
1201 | * initalizes it to be a GSOCK_*family*. In other cases, it returns | |
1202 | * an appropiate error code. | |
1203 | * | |
1204 | * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error. | |
1205 | */ | |
1206 | #define CHECK_ADDRESS(address, family) \ | |
1207 | { \ | |
1208 | if (address->m_family == GSOCK_NOFAMILY) \ | |
1209 | if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \ | |
1210 | return address->m_error; \ | |
1211 | if (address->m_family != GSOCK_##family) \ | |
1212 | { \ | |
1213 | address->m_error = GSOCK_INVADDR; \ | |
1214 | return GSOCK_INVADDR; \ | |
1215 | } \ | |
1216 | } | |
1217 | ||
1218 | #define CHECK_ADDRESS_RETVAL(address, family, retval) \ | |
1219 | { \ | |
1220 | if (address->m_family == GSOCK_NOFAMILY) \ | |
1221 | if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \ | |
1222 | return retval; \ | |
1223 | if (address->m_family != GSOCK_##family) \ | |
1224 | { \ | |
1225 | address->m_error = GSOCK_INVADDR; \ | |
1226 | return retval; \ | |
1227 | } \ | |
1228 | } | |
1229 | ||
1230 | ||
1231 | GAddress *GAddress_new(void) | |
1232 | { | |
1233 | GAddress *address; | |
1234 | ||
1235 | if ((address = (GAddress *) malloc(sizeof(GAddress))) == NULL) | |
1236 | return NULL; | |
1237 | ||
1238 | address->m_family = GSOCK_NOFAMILY; | |
1239 | address->m_addr = NULL; | |
1240 | address->m_len = 0; | |
1241 | ||
1242 | return address; | |
1243 | } | |
1244 | ||
1245 | GAddress *GAddress_copy(GAddress *address) | |
1246 | { | |
1247 | GAddress *addr2; | |
1248 | ||
1249 | assert(address != NULL); | |
1250 | ||
1251 | if ((addr2 = (GAddress *) malloc(sizeof(GAddress))) == NULL) | |
1252 | return NULL; | |
1253 | ||
1254 | memcpy(addr2, address, sizeof(GAddress)); | |
1255 | ||
1256 | if (address->m_addr) | |
1257 | { | |
1258 | addr2->m_addr = (struct sockaddr *) malloc(addr2->m_len); | |
1259 | if (addr2->m_addr == NULL) | |
1260 | { | |
1261 | free(addr2); | |
1262 | return NULL; | |
1263 | } | |
1264 | memcpy(addr2->m_addr, address->m_addr, addr2->m_len); | |
1265 | } | |
1266 | ||
1267 | return addr2; | |
1268 | } | |
1269 | ||
1270 | void GAddress_destroy(GAddress *address) | |
1271 | { | |
1272 | assert(address != NULL); | |
1273 | ||
1274 | if (address->m_addr) | |
1275 | free(address->m_addr); | |
1276 | ||
1277 | free(address); | |
1278 | } | |
1279 | ||
1280 | void GAddress_SetFamily(GAddress *address, GAddressType type) | |
1281 | { | |
1282 | assert(address != NULL); | |
1283 | ||
1284 | address->m_family = type; | |
1285 | } | |
1286 | ||
1287 | GAddressType GAddress_GetFamily(GAddress *address) | |
1288 | { | |
1289 | assert(address != NULL); | |
1290 | ||
1291 | return address->m_family; | |
1292 | } | |
1293 | ||
1294 | GSocketError _GAddress_translate_from(GAddress *address, | |
1295 | struct sockaddr *addr, int len) | |
1296 | { | |
1297 | address->m_realfamily = addr->sa_family; | |
1298 | switch (addr->sa_family) | |
1299 | { | |
1300 | case AF_INET: | |
1301 | address->m_family = GSOCK_INET; | |
1302 | break; | |
1303 | case AF_UNIX: | |
1304 | address->m_family = GSOCK_UNIX; | |
1305 | break; | |
1306 | #ifdef AF_INET6 | |
1307 | case AF_INET6: | |
1308 | address->m_family = GSOCK_INET6; | |
1309 | break; | |
1310 | #endif | |
1311 | default: | |
1312 | { | |
1313 | address->m_error = GSOCK_INVOP; | |
1314 | return GSOCK_INVOP; | |
1315 | } | |
1316 | } | |
1317 | ||
1318 | if (address->m_addr) | |
1319 | free(address->m_addr); | |
1320 | ||
1321 | address->m_len = len; | |
1322 | address->m_addr = (struct sockaddr *) malloc(len); | |
1323 | ||
1324 | if (address->m_addr == NULL) | |
1325 | { | |
1326 | address->m_error = GSOCK_MEMERR; | |
1327 | return GSOCK_MEMERR; | |
1328 | } | |
1329 | memcpy(address->m_addr, addr, len); | |
1330 | ||
1331 | return GSOCK_NOERROR; | |
1332 | } | |
1333 | ||
1334 | GSocketError _GAddress_translate_to(GAddress *address, | |
1335 | struct sockaddr **addr, int *len) | |
1336 | { | |
1337 | if (!address->m_addr) | |
1338 | { | |
1339 | address->m_error = GSOCK_INVADDR; | |
1340 | return GSOCK_INVADDR; | |
1341 | } | |
1342 | ||
1343 | *len = address->m_len; | |
1344 | *addr = (struct sockaddr *) malloc(address->m_len); | |
1345 | if (*addr == NULL) | |
1346 | { | |
1347 | address->m_error = GSOCK_MEMERR; | |
1348 | return GSOCK_MEMERR; | |
1349 | } | |
1350 | ||
1351 | memcpy(*addr, address->m_addr, address->m_len); | |
1352 | return GSOCK_NOERROR; | |
1353 | } | |
1354 | ||
1355 | /* | |
1356 | * ------------------------------------------------------------------------- | |
1357 | * Internet address family | |
1358 | * ------------------------------------------------------------------------- | |
1359 | */ | |
1360 | ||
1361 | GSocketError _GAddress_Init_INET(GAddress *address) | |
1362 | { | |
1363 | address->m_len = sizeof(struct sockaddr_in); | |
1364 | address->m_addr = (struct sockaddr *) malloc(address->m_len); | |
1365 | if (address->m_addr == NULL) | |
1366 | { | |
1367 | address->m_error = GSOCK_MEMERR; | |
1368 | return GSOCK_MEMERR; | |
1369 | } | |
1370 | ||
1371 | address->m_family = GSOCK_INET; | |
1372 | address->m_realfamily = AF_INET; | |
1373 | ((struct sockaddr_in *)address->m_addr)->sin_family = AF_INET; | |
1374 | ((struct sockaddr_in *)address->m_addr)->sin_addr.s_addr = INADDR_ANY; | |
1375 | ||
1376 | return GSOCK_NOERROR; | |
1377 | } | |
1378 | ||
1379 | GSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname) | |
1380 | { | |
1381 | struct hostent *he; | |
1382 | struct in_addr *addr; | |
1383 | ||
1384 | assert(address != NULL); | |
1385 | ||
1386 | CHECK_ADDRESS(address, INET); | |
1387 | ||
1388 | addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr); | |
1389 | ||
1390 | addr->s_addr = inet_addr(hostname); | |
1391 | ||
1392 | /* If it is a numeric host name, convert it now */ | |
1393 | if (addr->s_addr == INADDR_NONE) | |
1394 | { | |
1395 | struct in_addr *array_addr; | |
1396 | ||
1397 | /* It is a real name, we solve it */ | |
1398 | if ((he = gethostbyname(hostname)) == NULL) | |
1399 | { | |
1400 | /* addr->s_addr = INADDR_NONE just done by inet_addr() above */ | |
1401 | address->m_error = GSOCK_NOHOST; | |
1402 | return GSOCK_NOHOST; | |
1403 | } | |
1404 | array_addr = (struct in_addr *) *(he->h_addr_list); | |
1405 | addr->s_addr = array_addr[0].s_addr; | |
1406 | } | |
1407 | return GSOCK_NOERROR; | |
1408 | } | |
1409 | ||
1410 | GSocketError GAddress_INET_SetAnyAddress(GAddress *address) | |
1411 | { | |
1412 | return GAddress_INET_SetHostAddress(address, INADDR_ANY); | |
1413 | } | |
1414 | ||
1415 | GSocketError GAddress_INET_SetHostAddress(GAddress *address, | |
1416 | unsigned long hostaddr) | |
1417 | { | |
1418 | struct in_addr *addr; | |
1419 | ||
1420 | assert(address != NULL); | |
1421 | ||
1422 | CHECK_ADDRESS(address, INET); | |
1423 | ||
1424 | addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr); | |
d0ee33f5 | 1425 | addr->s_addr = htonl(hostaddr); |
f7ff39c6 DE |
1426 | |
1427 | return GSOCK_NOERROR; | |
1428 | } | |
1429 | ||
1430 | GSocketError GAddress_INET_SetPortName(GAddress *address, const char *port, | |
1431 | const char *protocol) | |
1432 | { | |
1433 | struct servent *se; | |
1434 | struct sockaddr_in *addr; | |
1435 | ||
1436 | assert(address != NULL); | |
1437 | CHECK_ADDRESS(address, INET); | |
1438 | ||
1439 | if (!port) | |
1440 | { | |
1441 | address->m_error = GSOCK_INVPORT; | |
1442 | return GSOCK_INVPORT; | |
1443 | } | |
1444 | ||
1445 | se = getservbyname(port, protocol); | |
1446 | if (!se) | |
1447 | { | |
1448 | if (isdigit(port[0])) | |
1449 | { | |
1450 | int port_int; | |
1451 | ||
1452 | port_int = atoi(port); | |
1453 | addr = (struct sockaddr_in *)address->m_addr; | |
1454 | addr->sin_port = htons((u_short) port_int); | |
1455 | return GSOCK_NOERROR; | |
1456 | } | |
1457 | ||
1458 | address->m_error = GSOCK_INVPORT; | |
1459 | return GSOCK_INVPORT; | |
1460 | } | |
1461 | ||
1462 | addr = (struct sockaddr_in *)address->m_addr; | |
1463 | addr->sin_port = se->s_port; | |
1464 | ||
1465 | return GSOCK_NOERROR; | |
1466 | } | |
1467 | ||
1468 | GSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port) | |
1469 | { | |
1470 | struct sockaddr_in *addr; | |
1471 | ||
1472 | assert(address != NULL); | |
1473 | CHECK_ADDRESS(address, INET); | |
1474 | ||
1475 | addr = (struct sockaddr_in *)address->m_addr; | |
1476 | addr->sin_port = htons(port); | |
1477 | ||
1478 | return GSOCK_NOERROR; | |
1479 | } | |
1480 | ||
1481 | GSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname, size_t sbuf) | |
1482 | { | |
1483 | struct hostent *he; | |
1484 | char *addr_buf; | |
1485 | struct sockaddr_in *addr; | |
1486 | ||
1487 | assert(address != NULL); | |
1488 | CHECK_ADDRESS(address, INET); | |
1489 | ||
1490 | addr = (struct sockaddr_in *)address->m_addr; | |
1491 | addr_buf = (char *)&(addr->sin_addr); | |
1492 | ||
1493 | he = gethostbyaddr(addr_buf, sizeof(addr->sin_addr), AF_INET); | |
1494 | if (he == NULL) | |
1495 | { | |
1496 | address->m_error = GSOCK_NOHOST; | |
1497 | return GSOCK_NOHOST; | |
1498 | } | |
1499 | ||
1500 | strncpy(hostname, he->h_name, sbuf); | |
1501 | ||
1502 | return GSOCK_NOERROR; | |
1503 | } | |
1504 | ||
1505 | unsigned long GAddress_INET_GetHostAddress(GAddress *address) | |
1506 | { | |
1507 | struct sockaddr_in *addr; | |
1508 | ||
1509 | assert(address != NULL); | |
1510 | CHECK_ADDRESS_RETVAL(address, INET, 0); | |
1511 | ||
1512 | addr = (struct sockaddr_in *)address->m_addr; | |
1513 | ||
1514 | return ntohl(addr->sin_addr.s_addr); | |
1515 | } | |
1516 | ||
1517 | unsigned short GAddress_INET_GetPort(GAddress *address) | |
1518 | { | |
1519 | struct sockaddr_in *addr; | |
1520 | ||
1521 | assert(address != NULL); | |
1522 | CHECK_ADDRESS_RETVAL(address, INET, 0); | |
1523 | ||
1524 | addr = (struct sockaddr_in *)address->m_addr; | |
1525 | return ntohs(addr->sin_port); | |
1526 | } | |
1527 | ||
1528 | /* | |
1529 | * ------------------------------------------------------------------------- | |
1530 | * Unix address family | |
1531 | * ------------------------------------------------------------------------- | |
1532 | */ | |
1533 | ||
1534 | GSocketError _GAddress_Init_UNIX(GAddress *address) | |
1535 | { | |
1536 | assert (address != NULL); | |
1537 | address->m_error = GSOCK_INVADDR; | |
1538 | return GSOCK_INVADDR; | |
1539 | } | |
1540 | ||
907173e5 | 1541 | GSocketError GAddress_UNIX_SetPath(GAddress *address, const char *WXUNUSED(path)) |
f7ff39c6 | 1542 | { |
f7ff39c6 DE |
1543 | assert (address != NULL); |
1544 | address->m_error = GSOCK_INVADDR; | |
1545 | return GSOCK_INVADDR; | |
1546 | } | |
1547 | ||
907173e5 | 1548 | GSocketError GAddress_UNIX_GetPath(GAddress *address, char *WXUNUSED(path), size_t WXUNUSED(sbuf)) |
f7ff39c6 | 1549 | { |
f7ff39c6 DE |
1550 | assert (address != NULL); |
1551 | address->m_error = GSOCK_INVADDR; | |
1552 | return GSOCK_INVADDR; | |
1553 | } | |
1554 | ||
1555 | #else /* !wxUSE_SOCKETS */ | |
1556 | ||
1557 | /* | |
1558 | * Translation unit shouldn't be empty, so include this typedef to make the | |
1559 | * compiler (VC++ 6.0, for example) happy | |
1560 | */ | |
1561 | typedef void (*wxDummy)(); | |
1562 | ||
1563 | #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */ |