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