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