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