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