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