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