Commit | Line | Data |
---|---|---|
97e6ee04 | 1 | /* ------------------------------------------------------------------------- |
99d80019 JS |
2 | * Project: GSocket (Generic Socket) for WX |
3 | * Name: gsocket.c | |
4 | * Copyright: (c) Guilhem Lavaux | |
5 | * Licence: wxWindows Licence | |
6 | * Authors: David Elliott (C++ conversion, maintainer) | |
7 | * Guilhem Lavaux, | |
8 | * Guillermo Rodriguez Garcia <guille@iies.es> | |
9 | * Purpose: GSocket main Unix and OS/2 file | |
10 | * Licence: The wxWindows licence | |
11 | * CVSID: $Id$ | |
97e6ee04 DE |
12 | * ------------------------------------------------------------------------- |
13 | */ | |
14 | ||
7e1e6965 WS |
15 | #if defined(__WATCOMC__) |
16 | #include "wx/wxprec.h" | |
17 | #include <errno.h> | |
18 | #include <nerrno.h> | |
19 | #endif | |
20 | ||
ba2a81d7 | 21 | #ifndef __GSOCKET_STANDALONE__ |
0beb8735 | 22 | #include "wx/defs.h" |
ba2a81d7 | 23 | #endif |
97e6ee04 DE |
24 | |
25 | #if defined(__VISAGECPP__) | |
1aa7b427 | 26 | #define BSD_SELECT /* use Berkeley Sockets select */ |
97e6ee04 DE |
27 | #endif |
28 | ||
29 | #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) | |
30 | ||
31 | #include <assert.h> | |
32 | #include <sys/types.h> | |
33 | #ifdef __VISAGECPP__ | |
34 | #include <string.h> | |
35 | #include <sys/time.h> | |
36 | #include <types.h> | |
37 | #include <netinet/in.h> | |
38 | #endif | |
39 | #include <netdb.h> | |
40 | #include <sys/ioctl.h> | |
41 | ||
bc023abb MW |
42 | #ifdef HAVE_SYS_SELECT_H |
43 | # include <sys/select.h> | |
44 | #endif | |
45 | ||
97e6ee04 DE |
46 | #ifdef __VMS__ |
47 | #include <socket.h> | |
48 | struct sockaddr_un | |
49 | { | |
50 | u_char sun_len; /* sockaddr len including null */ | |
51 | u_char sun_family; /* AF_UNIX */ | |
52 | char sun_path[108]; /* path name (gag) */ | |
53 | }; | |
54 | #else | |
55 | #include <sys/socket.h> | |
56 | #include <sys/un.h> | |
57 | #endif | |
58 | ||
59 | #ifndef __VISAGECPP__ | |
60 | #include <sys/time.h> | |
61 | #include <netinet/in.h> | |
62 | #include <arpa/inet.h> | |
63 | #include <errno.h> | |
64 | #include <string.h> | |
65 | #include <unistd.h> | |
66 | #else | |
67 | #include <nerrno.h> | |
68 | # if __IBMCPP__ < 400 | |
69 | #include <machine/endian.h> | |
70 | #include <socket.h> | |
71 | #include <ioctl.h> | |
72 | #include <select.h> | |
73 | #include <unistd.h> | |
74 | ||
75 | #define EBADF SOCEBADF | |
76 | ||
77 | # ifdef min | |
78 | # undef min | |
79 | # endif | |
80 | # else | |
81 | #include <sys/socket.h> | |
82 | #include <sys/ioctl.h> | |
83 | #include <sys/select.h> | |
84 | ||
85 | #define close(a) soclose(a) | |
86 | #define select(a,b,c,d,e) bsdselect(a,b,c,d,e) | |
87 | int _System bsdselect(int, | |
88 | struct fd_set *, | |
89 | struct fd_set *, | |
90 | struct fd_set *, | |
91 | struct timeval *); | |
92 | int _System soclose(int); | |
93 | # endif | |
94 | #endif | |
ebdab982 SN |
95 | #ifdef __EMX__ |
96 | #include <sys/select.h> | |
97 | #endif | |
98 | ||
97e6ee04 DE |
99 | #include <stdio.h> |
100 | #include <stdlib.h> | |
101 | #include <stddef.h> | |
102 | #include <ctype.h> | |
103 | #ifdef sun | |
104 | # include <sys/filio.h> | |
105 | #endif | |
106 | #ifdef sgi | |
107 | # include <bstring.h> | |
108 | #endif | |
d1f8e97b SN |
109 | #ifdef _AIX |
110 | # include <strings.h> | |
111 | #endif | |
97e6ee04 DE |
112 | #include <signal.h> |
113 | ||
9e03e02d | 114 | #ifndef WX_SOCKLEN_T |
97e6ee04 DE |
115 | |
116 | #ifdef VMS | |
9e03e02d | 117 | # define WX_SOCKLEN_T unsigned int |
97e6ee04 DE |
118 | #else |
119 | # ifdef __GLIBC__ | |
120 | # if __GLIBC__ == 2 | |
9e03e02d | 121 | # define WX_SOCKLEN_T socklen_t |
97e6ee04 | 122 | # endif |
fcbd7e5a | 123 | # elif defined(__WXMAC__) |
9e03e02d | 124 | # define WX_SOCKLEN_T socklen_t |
97e6ee04 | 125 | # else |
9e03e02d | 126 | # define WX_SOCKLEN_T int |
97e6ee04 DE |
127 | # endif |
128 | #endif | |
129 | ||
130 | #endif /* SOCKLEN_T */ | |
131 | ||
ddc1a35f | 132 | #ifndef SOCKOPTLEN_T |
9e03e02d | 133 | #define SOCKOPTLEN_T WX_SOCKLEN_T |
ddc1a35f DE |
134 | #endif |
135 | ||
97e6ee04 DE |
136 | /* |
137 | * MSW defines this, Unices don't. | |
138 | */ | |
139 | #ifndef INVALID_SOCKET | |
140 | #define INVALID_SOCKET -1 | |
141 | #endif | |
142 | ||
143 | /* UnixWare reportedly needs this for FIONBIO definition */ | |
144 | #ifdef __UNIXWARE__ | |
145 | #include <sys/filio.h> | |
146 | #endif | |
147 | ||
148 | /* | |
149 | * INADDR_BROADCAST is identical to INADDR_NONE which is not defined | |
150 | * on all systems. INADDR_BROADCAST should be fine to indicate an error. | |
151 | */ | |
152 | #ifndef INADDR_NONE | |
153 | #define INADDR_NONE INADDR_BROADCAST | |
154 | #endif | |
155 | ||
7e1e6965 | 156 | #if defined(__VISAGECPP__) || defined(__WATCOMC__) |
97e6ee04 | 157 | |
7e1e6965 WS |
158 | #define MASK_SIGNAL() { |
159 | #define UNMASK_SIGNAL() } | |
160 | ||
161 | #else | |
defbeca1 | 162 | extern "C" { typedef void (*wxSigHandler)(int); } |
7e1e6965 WS |
163 | |
164 | #define MASK_SIGNAL() \ | |
165 | { \ | |
defbeca1 | 166 | wxSigHandler old_handler = signal(SIGPIPE, SIG_IGN); |
7e1e6965 WS |
167 | |
168 | #define UNMASK_SIGNAL() \ | |
169 | signal(SIGPIPE, old_handler); \ | |
170 | } | |
171 | ||
172 | #endif | |
97e6ee04 | 173 | |
e400d27d KH |
174 | /* If a SIGPIPE is issued by a socket call on a remotely closed socket, |
175 | the program will "crash" unless it explicitly handles the SIGPIPE. | |
176 | By using MSG_NOSIGNAL, the SIGPIPE is suppressed. Later, we will | |
177 | use SO_NOSIGPIPE (if available), the BSD equivalent. */ | |
bb154f79 KH |
178 | #ifdef MSG_NOSIGNAL |
179 | # define GSOCKET_MSG_NOSIGNAL MSG_NOSIGNAL | |
180 | #else /* MSG_NOSIGNAL not available (FreeBSD including OS X) */ | |
181 | # define GSOCKET_MSG_NOSIGNAL 0 | |
182 | #endif /* MSG_NOSIGNAL */ | |
97e6ee04 DE |
183 | |
184 | #ifndef __GSOCKET_STANDALONE__ | |
185 | # include "wx/unix/gsockunx.h" | |
17a1ebd1 | 186 | # include "wx/unix/private.h" |
97e6ee04 | 187 | # include "wx/gsocket.h" |
71b4a9b8 SN |
188 | #if wxUSE_THREADS && (defined(HAVE_GETHOSTBYNAME) || defined(HAVE_GETSERVBYNAME)) |
189 | # include "wx/thread.h" | |
190 | #endif | |
97e6ee04 DE |
191 | #else |
192 | # include "gsockunx.h" | |
193 | # include "gsocket.h" | |
5fb9aa56 WS |
194 | # ifndef WXUNUSED |
195 | # define WXUNUSED(x) | |
196 | # endif | |
97e6ee04 DE |
197 | #endif /* __GSOCKET_STANDALONE__ */ |
198 | ||
71b4a9b8 SN |
199 | #if defined(HAVE_GETHOSTBYNAME) |
200 | static struct hostent * deepCopyHostent(struct hostent *h, | |
201 | const struct hostent *he, | |
202 | char *buffer, int size, int *err) | |
203 | { | |
2887cb4e | 204 | /* copy old structure */ |
71b4a9b8 | 205 | memcpy(h, he, sizeof(struct hostent)); |
2887cb4e SN |
206 | |
207 | /* copy name */ | |
71b4a9b8 SN |
208 | int len = strlen(h->h_name); |
209 | if (len > size) | |
2887cb4e SN |
210 | { |
211 | *err = ENOMEM; | |
212 | return NULL; | |
213 | } | |
71b4a9b8 SN |
214 | memcpy(buffer, h->h_name, len); |
215 | buffer[len] = '\0'; | |
216 | h->h_name = buffer; | |
2887cb4e SN |
217 | |
218 | /* track position in the buffer */ | |
219 | int pos = len + 1; | |
220 | ||
221 | /* reuse len to store address length */ | |
71b4a9b8 | 222 | len = h->h_length; |
2887cb4e SN |
223 | |
224 | /* ensure pointer alignment */ | |
225 | unsigned int misalign = sizeof(char *) - pos%sizeof(char *); | |
226 | if(misalign < sizeof(char *)) | |
227 | pos += misalign; | |
228 | ||
229 | /* leave space for pointer list */ | |
230 | char **p = h->h_addr_list, **q; | |
231 | char **h_addr_list = (char **)(buffer + pos); | |
232 | while(*(p++) != 0) | |
233 | pos += sizeof(char *); | |
234 | ||
235 | /* copy addresses and fill new pointer list */ | |
236 | for (p = h->h_addr_list, q = h_addr_list; *p != 0; p++, q++) | |
237 | { | |
238 | if (size < pos + len) | |
239 | { | |
71b4a9b8 SN |
240 | *err = ENOMEM; |
241 | return NULL; | |
242 | } | |
2887cb4e SN |
243 | memcpy(buffer + pos, *p, len); /* copy content */ |
244 | *q = buffer + pos; /* set copied pointer to copied content */ | |
245 | pos += len; | |
71b4a9b8 | 246 | } |
2887cb4e SN |
247 | *++q = 0; /* null terminate the pointer list */ |
248 | h->h_addr_list = h_addr_list; /* copy pointer to pointers */ | |
249 | ||
250 | /* ensure word alignment of pointers */ | |
251 | misalign = sizeof(char *) - pos%sizeof(char *); | |
252 | if(misalign < sizeof(char *)) | |
253 | pos += misalign; | |
01ba4b67 | 254 | |
2887cb4e SN |
255 | /* leave space for pointer list */ |
256 | p = h->h_aliases; | |
257 | char **h_aliases = (char **)(buffer + pos); | |
258 | while(*(p++) != 0) | |
259 | pos += sizeof(char *); | |
01ba4b67 | 260 | |
2887cb4e SN |
261 | /* copy aliases and fill new pointer list */ |
262 | for (p = h->h_aliases, q = h_aliases; *p != 0; p++, q++) | |
263 | { | |
264 | len = strlen(*p); | |
265 | if (size <= pos + len) | |
266 | { | |
267 | *err = ENOMEM; | |
268 | return NULL; | |
269 | } | |
270 | memcpy(buffer + pos, *p, len); /* copy content */ | |
271 | buffer[pos + len] = '\0'; | |
272 | *q = buffer + pos; /* set copied pointer to copied content */ | |
273 | pos += len + 1; | |
71b4a9b8 | 274 | } |
2887cb4e SN |
275 | *++q = 0; /* null terminate the pointer list */ |
276 | h->h_aliases = h_aliases; /* copy pointer to pointers */ | |
277 | ||
71b4a9b8 SN |
278 | return h; |
279 | } | |
280 | #endif | |
281 | ||
2887cb4e SN |
282 | #if defined(HAVE_GETHOSTBYNAME) && wxUSE_THREADS |
283 | static wxMutex nameLock; | |
284 | #endif | |
71b4a9b8 SN |
285 | struct hostent * wxGethostbyname_r(const char *hostname, struct hostent *h, |
286 | void *buffer, int size, int *err) | |
287 | ||
288 | { | |
539ae551 | 289 | struct hostent *he = NULL; |
71b4a9b8 SN |
290 | *err = 0; |
291 | #if defined(HAVE_FUNC_GETHOSTBYNAME_R_6) | |
292 | if (gethostbyname_r(hostname, h, (char*)buffer, size, &he, err)) | |
293 | he = NULL; | |
294 | #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_5) | |
295 | he = gethostbyname_r(hostname, h, (char*)buffer, size, err); | |
296 | #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_3) | |
297 | if (gethostbyname_r(hostname, h, (struct hostent_data*) buffer)) | |
298 | { | |
299 | he = NULL; | |
300 | *err = h_errno; | |
301 | } | |
302 | else | |
303 | he = h; | |
304 | #elif defined(HAVE_GETHOSTBYNAME) | |
305 | #if wxUSE_THREADS | |
71b4a9b8 SN |
306 | wxMutexLocker locker(nameLock); |
307 | #endif | |
308 | he = gethostbyname(hostname); | |
309 | if (!he) | |
310 | *err = h_errno; | |
311 | else | |
312 | he = deepCopyHostent(h, he, (char*)buffer, size, err); | |
313 | #endif | |
314 | return he; | |
315 | } | |
316 | ||
2887cb4e SN |
317 | #if defined(HAVE_GETHOSTBYNAME) && wxUSE_THREADS |
318 | static wxMutex addrLock; | |
319 | #endif | |
71b4a9b8 SN |
320 | struct hostent * wxGethostbyaddr_r(const char *addr_buf, int buf_size, |
321 | int proto, struct hostent *h, | |
322 | void *buffer, int size, int *err) | |
323 | { | |
539ae551 | 324 | struct hostent *he = NULL; |
71b4a9b8 SN |
325 | *err = 0; |
326 | #if defined(HAVE_FUNC_GETHOSTBYNAME_R_6) | |
327 | if (gethostbyaddr_r(addr_buf, buf_size, proto, h, | |
328 | (char*)buffer, size, &he, err)) | |
329 | he = NULL; | |
330 | #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_5) | |
331 | he = gethostbyaddr_r(addr_buf, buf_size, proto, h, (char*)buffer, size, err); | |
332 | #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_3) | |
333 | if (gethostbyaddr_r(addr_buf, buf_size, proto, h, | |
334 | (struct hostent_data*) buffer)) | |
335 | { | |
336 | he = NULL; | |
337 | *err = h_errno; | |
338 | } | |
339 | else | |
340 | he = h; | |
341 | #elif defined(HAVE_GETHOSTBYNAME) | |
342 | #if wxUSE_THREADS | |
71b4a9b8 SN |
343 | wxMutexLocker locker(addrLock); |
344 | #endif | |
345 | he = gethostbyaddr(addr_buf, buf_size, proto); | |
346 | if (!he) | |
347 | *err = h_errno; | |
348 | else | |
349 | he = deepCopyHostent(h, he, (char*)buffer, size, err); | |
350 | #endif | |
351 | return he; | |
352 | } | |
353 | ||
f0b805fa | 354 | #if defined(HAVE_GETSERVBYNAME) |
71b4a9b8 SN |
355 | static struct servent * deepCopyServent(struct servent *s, |
356 | const struct servent *se, | |
357 | char *buffer, int size) | |
358 | { | |
2887cb4e | 359 | /* copy plain old structure */ |
71b4a9b8 | 360 | memcpy(s, se, sizeof(struct servent)); |
2887cb4e SN |
361 | |
362 | /* copy name */ | |
71b4a9b8 | 363 | int len = strlen(s->s_name); |
2887cb4e SN |
364 | if (len >= size) |
365 | { | |
366 | return NULL; | |
367 | } | |
71b4a9b8 SN |
368 | memcpy(buffer, s->s_name, len); |
369 | buffer[len] = '\0'; | |
370 | s->s_name = buffer; | |
2887cb4e SN |
371 | |
372 | /* track position in the buffer */ | |
373 | int pos = len + 1; | |
374 | ||
375 | /* copy protocol */ | |
71b4a9b8 | 376 | len = strlen(s->s_proto); |
2887cb4e SN |
377 | if (pos + len >= size) |
378 | { | |
379 | return NULL; | |
71b4a9b8 | 380 | } |
2887cb4e SN |
381 | memcpy(buffer + pos, s->s_proto, len); |
382 | buffer[pos + len] = '\0'; | |
383 | s->s_proto = buffer + pos; | |
384 | ||
385 | /* track position in the buffer */ | |
386 | pos += len + 1; | |
387 | ||
388 | /* ensure pointer alignment */ | |
389 | unsigned int misalign = sizeof(char *) - pos%sizeof(char *); | |
390 | if(misalign < sizeof(char *)) | |
391 | pos += misalign; | |
01ba4b67 | 392 | |
2887cb4e SN |
393 | /* leave space for pointer list */ |
394 | char **p = s->s_aliases, **q; | |
395 | char **s_aliases = (char **)(buffer + pos); | |
396 | while(*(p++) != 0) | |
397 | pos += sizeof(char *); | |
01ba4b67 | 398 | |
2887cb4e SN |
399 | /* copy addresses and fill new pointer list */ |
400 | for (p = s->s_aliases, q = s_aliases; *p != 0; p++, q++){ | |
401 | len = strlen(*p); | |
402 | if (size <= pos + len) | |
403 | { | |
404 | return NULL; | |
405 | } | |
406 | memcpy(buffer + pos, *p, len); /* copy content */ | |
407 | buffer[pos + len] = '\0'; | |
408 | *q = buffer + pos; /* set copied pointer to copied content */ | |
409 | pos += len + 1; | |
410 | } | |
411 | *++q = 0; /* null terminate the pointer list */ | |
412 | s->s_aliases = s_aliases; /* copy pointer to pointers */ | |
71b4a9b8 SN |
413 | return s; |
414 | } | |
415 | #endif | |
416 | ||
2887cb4e SN |
417 | #if defined(HAVE_GETSERVBYNAME) && wxUSE_THREADS |
418 | static wxMutex servLock; | |
419 | #endif | |
71b4a9b8 SN |
420 | struct servent *wxGetservbyname_r(const char *port, const char *protocol, |
421 | struct servent *serv, void *buffer, int size) | |
422 | { | |
539ae551 | 423 | struct servent *se = NULL; |
71b4a9b8 SN |
424 | #if defined(HAVE_FUNC_GETSERVBYNAME_R_6) |
425 | if (getservbyname_r(port, protocol, serv, (char*)buffer, size, &se)) | |
426 | se = NULL; | |
427 | #elif defined(HAVE_FUNC_GETSERVBYNAME_R_5) | |
428 | se = getservbyname_r(port, protocol, serv, (char*)buffer, size); | |
429 | #elif defined(HAVE_FUNC_GETSERVBYNAME_R_4) | |
430 | if (getservbyname_r(port, protocol, serv, (struct servent_data*) buffer)) | |
431 | se = NULL; | |
432 | else | |
433 | se = serv; | |
434 | #elif defined(HAVE_GETSERVBYNAME) | |
435 | #if wxUSE_THREADS | |
71b4a9b8 SN |
436 | wxMutexLocker locker(servLock); |
437 | #endif | |
438 | se = getservbyname(port, protocol); | |
439 | if (se) | |
440 | se = deepCopyServent(serv, se, (char*)buffer, size); | |
441 | #endif | |
442 | return se; | |
443 | } | |
444 | ||
97e6ee04 DE |
445 | /* debugging helpers */ |
446 | #ifdef __GSOCKET_DEBUG__ | |
447 | # define GSocket_Debug(args) printf args | |
448 | #else | |
449 | # define GSocket_Debug(args) | |
450 | #endif /* __GSOCKET_DEBUG__ */ | |
451 | ||
ba2a81d7 DE |
452 | /* Table of GUI-related functions. We must call them indirectly because |
453 | * of wxBase and GUI separation: */ | |
444cb1fd | 454 | |
3e1400ac | 455 | static GSocketGUIFunctionsTable *gs_gui_functions; |
b082b524 | 456 | |
ba2a81d7 | 457 | class GSocketGUIFunctionsTableNull: public GSocketGUIFunctionsTable |
444cb1fd | 458 | { |
ba2a81d7 DE |
459 | public: |
460 | virtual bool OnInit(); | |
461 | virtual void OnExit(); | |
462 | virtual bool CanUseEventLoop(); | |
463 | virtual bool Init_Socket(GSocket *socket); | |
464 | virtual void Destroy_Socket(GSocket *socket); | |
465 | virtual void Install_Callback(GSocket *socket, GSocketEvent event); | |
ba2a81d7 DE |
466 | virtual void Uninstall_Callback(GSocket *socket, GSocketEvent event); |
467 | virtual void Enable_Events(GSocket *socket); | |
468 | virtual void Disable_Events(GSocket *socket); | |
469 | }; | |
444cb1fd | 470 | |
ba2a81d7 DE |
471 | bool GSocketGUIFunctionsTableNull::OnInit() |
472 | { return true; } | |
473 | void GSocketGUIFunctionsTableNull::OnExit() | |
474 | {} | |
475 | bool GSocketGUIFunctionsTableNull::CanUseEventLoop() | |
476 | { return false; } | |
c40158e4 | 477 | bool GSocketGUIFunctionsTableNull::Init_Socket(GSocket *WXUNUSED(socket)) |
ba2a81d7 | 478 | { return true; } |
c40158e4 | 479 | void GSocketGUIFunctionsTableNull::Destroy_Socket(GSocket *WXUNUSED(socket)) |
ba2a81d7 | 480 | {} |
c40158e4 | 481 | void GSocketGUIFunctionsTableNull::Install_Callback(GSocket *WXUNUSED(socket), GSocketEvent WXUNUSED(event)) |
ba2a81d7 | 482 | {} |
c40158e4 | 483 | void GSocketGUIFunctionsTableNull::Uninstall_Callback(GSocket *WXUNUSED(socket), GSocketEvent WXUNUSED(event)) |
ba2a81d7 | 484 | {} |
c40158e4 | 485 | void GSocketGUIFunctionsTableNull::Enable_Events(GSocket *WXUNUSED(socket)) |
ba2a81d7 | 486 | {} |
c40158e4 | 487 | void GSocketGUIFunctionsTableNull::Disable_Events(GSocket *WXUNUSED(socket)) |
ba2a81d7 DE |
488 | {} |
489 | /* Global initialisers */ | |
444cb1fd | 490 | |
3e1400ac | 491 | void GSocket_SetGUIFunctions(GSocketGUIFunctionsTable *guifunc) |
444cb1fd | 492 | { |
ba2a81d7 | 493 | gs_gui_functions = guifunc; |
444cb1fd DE |
494 | } |
495 | ||
97e6ee04 DE |
496 | int GSocket_Init(void) |
497 | { | |
ba2a81d7 DE |
498 | if (!gs_gui_functions) |
499 | { | |
3e1400ac | 500 | static GSocketGUIFunctionsTableNull table; |
ba2a81d7 DE |
501 | gs_gui_functions = &table; |
502 | } | |
503 | if ( !gs_gui_functions->OnInit() ) | |
504 | return 0; | |
505 | return 1; | |
97e6ee04 DE |
506 | } |
507 | ||
508 | void GSocket_Cleanup(void) | |
509 | { | |
ba2a81d7 DE |
510 | if (gs_gui_functions) |
511 | { | |
512 | gs_gui_functions->OnExit(); | |
513 | } | |
97e6ee04 DE |
514 | } |
515 | ||
ba2a81d7 DE |
516 | /* Constructors / Destructors for GSocket */ |
517 | ||
518 | GSocket::GSocket() | |
97e6ee04 | 519 | { |
09e6e5ec | 520 | int i; |
97e6ee04 | 521 | |
09e6e5ec | 522 | m_fd = INVALID_SOCKET; |
97e6ee04 DE |
523 | for (i=0;i<GSOCK_MAX_EVENT;i++) |
524 | { | |
09e6e5ec | 525 | m_cbacks[i] = NULL; |
97e6ee04 | 526 | } |
09e6e5ec DE |
527 | m_detected = 0; |
528 | m_local = NULL; | |
529 | m_peer = NULL; | |
530 | m_error = GSOCK_NOERROR; | |
948c96ef DE |
531 | m_server = false; |
532 | m_stream = true; | |
09e6e5ec | 533 | m_gui_dependent = NULL; |
948c96ef | 534 | m_non_blocking = false; |
1eef64d8 | 535 | m_reusable = false; |
60edcf45 VZ |
536 | m_broadcast = false; |
537 | m_dobind = true; | |
09e6e5ec | 538 | m_timeout = 10*60*1000; |
97e6ee04 | 539 | /* 10 minutes * 60 sec * 1000 millisec */ |
948c96ef | 540 | m_establishing = false; |
97e6ee04 | 541 | |
ba2a81d7 DE |
542 | assert(gs_gui_functions); |
543 | /* Per-socket GUI-specific initialization */ | |
544 | m_ok = gs_gui_functions->Init_Socket(this); | |
97e6ee04 DE |
545 | } |
546 | ||
ba2a81d7 | 547 | void GSocket::Close() |
97e6ee04 | 548 | { |
ba2a81d7 DE |
549 | gs_gui_functions->Disable_Events(this); |
550 | /* gsockosx.c calls CFSocketInvalidate which closes the socket for us */ | |
97e6ee04 | 551 | #if !(defined(__DARWIN__) && (defined(__WXMAC__) || defined(__WXCOCOA__))) |
09e6e5ec | 552 | close(m_fd); |
97e6ee04 | 553 | #endif |
09e6e5ec | 554 | m_fd = INVALID_SOCKET; |
97e6ee04 DE |
555 | } |
556 | ||
ba2a81d7 | 557 | GSocket::~GSocket() |
97e6ee04 | 558 | { |
ba2a81d7 DE |
559 | assert(this); |
560 | ||
561 | /* Check that the socket is really shutdowned */ | |
562 | if (m_fd != INVALID_SOCKET) | |
563 | Shutdown(); | |
564 | ||
565 | /* Per-socket GUI-specific cleanup */ | |
566 | gs_gui_functions->Destroy_Socket(this); | |
567 | ||
97e6ee04 | 568 | /* Destroy private addresses */ |
09e6e5ec DE |
569 | if (m_local) |
570 | GAddress_destroy(m_local); | |
97e6ee04 | 571 | |
09e6e5ec DE |
572 | if (m_peer) |
573 | GAddress_destroy(m_peer); | |
97e6ee04 DE |
574 | } |
575 | ||
576 | /* GSocket_Shutdown: | |
577 | * Disallow further read/write operations on this socket, close | |
578 | * the fd and disable all callbacks. | |
579 | */ | |
ba2a81d7 | 580 | void GSocket::Shutdown() |
97e6ee04 DE |
581 | { |
582 | int evt; | |
583 | ||
09e6e5ec | 584 | assert(this); |
97e6ee04 | 585 | |
c3b501e1 KH |
586 | /* Don't allow events to fire after socket has been closed */ |
587 | gs_gui_functions->Disable_Events(this); | |
588 | ||
97e6ee04 | 589 | /* If socket has been created, shutdown it */ |
09e6e5ec | 590 | if (m_fd != INVALID_SOCKET) |
97e6ee04 | 591 | { |
cd632a86 | 592 | shutdown(m_fd, 1); |
09e6e5ec | 593 | Close(); |
97e6ee04 DE |
594 | } |
595 | ||
596 | /* Disable GUI callbacks */ | |
597 | for (evt = 0; evt < GSOCK_MAX_EVENT; evt++) | |
09e6e5ec | 598 | m_cbacks[evt] = NULL; |
97e6ee04 | 599 | |
09e6e5ec | 600 | m_detected = GSOCK_LOST_FLAG; |
97e6ee04 DE |
601 | } |
602 | ||
603 | /* Address handling */ | |
604 | ||
605 | /* GSocket_SetLocal: | |
606 | * GSocket_GetLocal: | |
607 | * GSocket_SetPeer: | |
608 | * GSocket_GetPeer: | |
609 | * Set or get the local or peer address for this socket. The 'set' | |
610 | * functions return GSOCK_NOERROR on success, an error code otherwise. | |
611 | * The 'get' functions return a pointer to a GAddress object on success, | |
612 | * or NULL otherwise, in which case they set the error code of the | |
613 | * corresponding GSocket. | |
614 | * | |
615 | * Error codes: | |
616 | * GSOCK_INVSOCK - the socket is not valid. | |
617 | * GSOCK_INVADDR - the address is not valid. | |
618 | */ | |
ba2a81d7 | 619 | GSocketError GSocket::SetLocal(GAddress *address) |
97e6ee04 | 620 | { |
09e6e5ec | 621 | assert(this); |
97e6ee04 DE |
622 | |
623 | /* the socket must be initialized, or it must be a server */ | |
09e6e5ec | 624 | if ((m_fd != INVALID_SOCKET && !m_server)) |
97e6ee04 | 625 | { |
09e6e5ec | 626 | m_error = GSOCK_INVSOCK; |
97e6ee04 DE |
627 | return GSOCK_INVSOCK; |
628 | } | |
629 | ||
630 | /* check address */ | |
631 | if (address == NULL || address->m_family == GSOCK_NOFAMILY) | |
632 | { | |
09e6e5ec | 633 | m_error = GSOCK_INVADDR; |
97e6ee04 DE |
634 | return GSOCK_INVADDR; |
635 | } | |
636 | ||
09e6e5ec DE |
637 | if (m_local) |
638 | GAddress_destroy(m_local); | |
97e6ee04 | 639 | |
09e6e5ec | 640 | m_local = GAddress_copy(address); |
97e6ee04 DE |
641 | |
642 | return GSOCK_NOERROR; | |
643 | } | |
644 | ||
ba2a81d7 | 645 | GSocketError GSocket::SetPeer(GAddress *address) |
97e6ee04 | 646 | { |
09e6e5ec | 647 | assert(this); |
97e6ee04 DE |
648 | |
649 | /* check address */ | |
650 | if (address == NULL || address->m_family == GSOCK_NOFAMILY) | |
651 | { | |
09e6e5ec | 652 | m_error = GSOCK_INVADDR; |
97e6ee04 DE |
653 | return GSOCK_INVADDR; |
654 | } | |
655 | ||
09e6e5ec DE |
656 | if (m_peer) |
657 | GAddress_destroy(m_peer); | |
97e6ee04 | 658 | |
09e6e5ec | 659 | m_peer = GAddress_copy(address); |
97e6ee04 DE |
660 | |
661 | return GSOCK_NOERROR; | |
662 | } | |
663 | ||
ba2a81d7 | 664 | GAddress *GSocket::GetLocal() |
97e6ee04 DE |
665 | { |
666 | GAddress *address; | |
667 | struct sockaddr addr; | |
9e03e02d | 668 | WX_SOCKLEN_T size = sizeof(addr); |
97e6ee04 DE |
669 | GSocketError err; |
670 | ||
09e6e5ec | 671 | assert(this); |
97e6ee04 DE |
672 | |
673 | /* try to get it from the m_local var first */ | |
09e6e5ec DE |
674 | if (m_local) |
675 | return GAddress_copy(m_local); | |
97e6ee04 DE |
676 | |
677 | /* else, if the socket is initialized, try getsockname */ | |
09e6e5ec | 678 | if (m_fd == INVALID_SOCKET) |
97e6ee04 | 679 | { |
09e6e5ec | 680 | m_error = GSOCK_INVSOCK; |
97e6ee04 DE |
681 | return NULL; |
682 | } | |
683 | ||
9e03e02d | 684 | if (getsockname(m_fd, &addr, (WX_SOCKLEN_T *) &size) < 0) |
97e6ee04 | 685 | { |
09e6e5ec | 686 | m_error = GSOCK_IOERR; |
97e6ee04 DE |
687 | return NULL; |
688 | } | |
689 | ||
690 | /* got a valid address from getsockname, create a GAddress object */ | |
691 | address = GAddress_new(); | |
692 | if (address == NULL) | |
693 | { | |
09e6e5ec | 694 | m_error = GSOCK_MEMERR; |
97e6ee04 DE |
695 | return NULL; |
696 | } | |
697 | ||
698 | err = _GAddress_translate_from(address, &addr, size); | |
699 | if (err != GSOCK_NOERROR) | |
700 | { | |
701 | GAddress_destroy(address); | |
09e6e5ec | 702 | m_error = err; |
97e6ee04 DE |
703 | return NULL; |
704 | } | |
705 | ||
706 | return address; | |
707 | } | |
708 | ||
ba2a81d7 | 709 | GAddress *GSocket::GetPeer() |
97e6ee04 | 710 | { |
09e6e5ec | 711 | assert(this); |
97e6ee04 DE |
712 | |
713 | /* try to get it from the m_peer var */ | |
09e6e5ec DE |
714 | if (m_peer) |
715 | return GAddress_copy(m_peer); | |
97e6ee04 DE |
716 | |
717 | return NULL; | |
718 | } | |
719 | ||
720 | /* Server specific parts */ | |
721 | ||
722 | /* GSocket_SetServer: | |
723 | * Sets up this socket as a server. The local address must have been | |
724 | * set with GSocket_SetLocal() before GSocket_SetServer() is called. | |
725 | * Returns GSOCK_NOERROR on success, one of the following otherwise: | |
b082b524 | 726 | * |
97e6ee04 DE |
727 | * Error codes: |
728 | * GSOCK_INVSOCK - the socket is in use. | |
729 | * GSOCK_INVADDR - the local address has not been set. | |
b082b524 | 730 | * GSOCK_IOERR - low-level error. |
97e6ee04 | 731 | */ |
ba2a81d7 | 732 | GSocketError GSocket::SetServer() |
97e6ee04 DE |
733 | { |
734 | int arg = 1; | |
735 | ||
09e6e5ec | 736 | assert(this); |
97e6ee04 DE |
737 | |
738 | /* must not be in use */ | |
09e6e5ec | 739 | if (m_fd != INVALID_SOCKET) |
97e6ee04 | 740 | { |
09e6e5ec | 741 | m_error = GSOCK_INVSOCK; |
97e6ee04 DE |
742 | return GSOCK_INVSOCK; |
743 | } | |
744 | ||
745 | /* the local addr must have been set */ | |
09e6e5ec | 746 | if (!m_local) |
97e6ee04 | 747 | { |
09e6e5ec | 748 | m_error = GSOCK_INVADDR; |
97e6ee04 DE |
749 | return GSOCK_INVADDR; |
750 | } | |
751 | ||
752 | /* Initialize all fields */ | |
948c96ef DE |
753 | m_stream = true; |
754 | m_server = true; | |
97e6ee04 DE |
755 | |
756 | /* Create the socket */ | |
09e6e5ec | 757 | m_fd = socket(m_local->m_realfamily, SOCK_STREAM, 0); |
97e6ee04 | 758 | |
09e6e5ec | 759 | if (m_fd == INVALID_SOCKET) |
97e6ee04 | 760 | { |
09e6e5ec | 761 | m_error = GSOCK_IOERR; |
97e6ee04 DE |
762 | return GSOCK_IOERR; |
763 | } | |
bb154f79 KH |
764 | |
765 | /* FreeBSD variants can't use MSG_NOSIGNAL, and instead use a socket option */ | |
766 | #ifdef SO_NOSIGPIPE | |
01ba4b67 | 767 | setsockopt(m_fd, SOL_SOCKET, SO_NOSIGPIPE, (const char*)&arg, sizeof(arg)); |
bb154f79 KH |
768 | #endif |
769 | ||
09e6e5ec | 770 | ioctl(m_fd, FIONBIO, &arg); |
ba2a81d7 | 771 | gs_gui_functions->Enable_Events(this); |
97e6ee04 DE |
772 | |
773 | /* allow a socket to re-bind if the socket is in the TIME_WAIT | |
774 | state after being previously closed. | |
775 | */ | |
ba2a81d7 | 776 | if (m_reusable) |
c232b3cd | 777 | { |
01ba4b67 | 778 | setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&arg, sizeof(arg)); |
c232b3cd | 779 | #ifdef SO_REUSEPORT |
01ba4b67 | 780 | setsockopt(m_fd, SOL_SOCKET, SO_REUSEPORT, (const char*)&arg, sizeof(arg)); |
c232b3cd KH |
781 | #endif |
782 | } | |
97e6ee04 DE |
783 | |
784 | /* Bind to the local address, | |
785 | * retrieve the actual address bound, | |
786 | * and listen up to 5 connections. | |
787 | */ | |
09e6e5ec DE |
788 | if ((bind(m_fd, m_local->m_addr, m_local->m_len) != 0) || |
789 | (getsockname(m_fd, | |
790 | m_local->m_addr, | |
9e03e02d | 791 | (WX_SOCKLEN_T *) &m_local->m_len) != 0) || |
09e6e5ec | 792 | (listen(m_fd, 5) != 0)) |
97e6ee04 | 793 | { |
09e6e5ec DE |
794 | Close(); |
795 | m_error = GSOCK_IOERR; | |
97e6ee04 DE |
796 | return GSOCK_IOERR; |
797 | } | |
798 | ||
799 | return GSOCK_NOERROR; | |
800 | } | |
801 | ||
802 | /* GSocket_WaitConnection: | |
803 | * Waits for an incoming client connection. Returns a pointer to | |
804 | * a GSocket object, or NULL if there was an error, in which case | |
805 | * the last error field will be updated for the calling GSocket. | |
806 | * | |
807 | * Error codes (set in the calling GSocket) | |
808 | * GSOCK_INVSOCK - the socket is not valid or not a server. | |
809 | * GSOCK_TIMEDOUT - timeout, no incoming connections. | |
810 | * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking. | |
811 | * GSOCK_MEMERR - couldn't allocate memory. | |
b082b524 | 812 | * GSOCK_IOERR - low-level error. |
97e6ee04 | 813 | */ |
ba2a81d7 | 814 | GSocket *GSocket::WaitConnection() |
97e6ee04 DE |
815 | { |
816 | struct sockaddr from; | |
9e03e02d | 817 | WX_SOCKLEN_T fromlen = sizeof(from); |
97e6ee04 DE |
818 | GSocket *connection; |
819 | GSocketError err; | |
820 | int arg = 1; | |
821 | ||
09e6e5ec | 822 | assert(this); |
97e6ee04 | 823 | |
97e6ee04 | 824 | /* If the socket has already been created, we exit immediately */ |
09e6e5ec | 825 | if (m_fd == INVALID_SOCKET || !m_server) |
97e6ee04 | 826 | { |
09e6e5ec | 827 | m_error = GSOCK_INVSOCK; |
97e6ee04 DE |
828 | return NULL; |
829 | } | |
830 | ||
831 | /* Create a GSocket object for the new connection */ | |
444cb1fd | 832 | connection = GSocket_new(); |
97e6ee04 | 833 | |
444cb1fd | 834 | if (!connection) |
97e6ee04 | 835 | { |
09e6e5ec | 836 | m_error = GSOCK_MEMERR; |
97e6ee04 DE |
837 | return NULL; |
838 | } | |
839 | ||
840 | /* Wait for a connection (with timeout) */ | |
09e6e5ec | 841 | if (Input_Timeout() == GSOCK_TIMEDOUT) |
97e6ee04 | 842 | { |
85431efa | 843 | delete connection; |
09e6e5ec | 844 | /* m_error set by _GSocket_Input_Timeout */ |
97e6ee04 DE |
845 | return NULL; |
846 | } | |
847 | ||
9e03e02d | 848 | connection->m_fd = accept(m_fd, &from, (WX_SOCKLEN_T *) &fromlen); |
97e6ee04 | 849 | |
bb154f79 KH |
850 | /* Reenable CONNECTION events */ |
851 | Enable(GSOCK_CONNECTION); | |
bb154f79 | 852 | |
97e6ee04 DE |
853 | if (connection->m_fd == INVALID_SOCKET) |
854 | { | |
855 | if (errno == EWOULDBLOCK) | |
09e6e5ec | 856 | m_error = GSOCK_WOULDBLOCK; |
97e6ee04 | 857 | else |
09e6e5ec | 858 | m_error = GSOCK_IOERR; |
97e6ee04 | 859 | |
85431efa | 860 | delete connection; |
97e6ee04 DE |
861 | return NULL; |
862 | } | |
863 | ||
864 | /* Initialize all fields */ | |
948c96ef DE |
865 | connection->m_server = false; |
866 | connection->m_stream = true; | |
97e6ee04 DE |
867 | |
868 | /* Setup the peer address field */ | |
869 | connection->m_peer = GAddress_new(); | |
870 | if (!connection->m_peer) | |
871 | { | |
85431efa | 872 | delete connection; |
09e6e5ec | 873 | m_error = GSOCK_MEMERR; |
97e6ee04 DE |
874 | return NULL; |
875 | } | |
1aa7b427 | 876 | |
97e6ee04 DE |
877 | err = _GAddress_translate_from(connection->m_peer, &from, fromlen); |
878 | if (err != GSOCK_NOERROR) | |
879 | { | |
85431efa | 880 | delete connection; |
09e6e5ec | 881 | m_error = err; |
97e6ee04 DE |
882 | return NULL; |
883 | } | |
1aa7b427 | 884 | |
97e6ee04 DE |
885 | #if defined(__EMX__) || defined(__VISAGECPP__) |
886 | ioctl(connection->m_fd, FIONBIO, (char*)&arg, sizeof(arg)); | |
887 | #else | |
888 | ioctl(connection->m_fd, FIONBIO, &arg); | |
889 | #endif | |
ba2a81d7 | 890 | gs_gui_functions->Enable_Events(connection); |
97e6ee04 DE |
891 | |
892 | return connection; | |
893 | } | |
894 | ||
948c96ef | 895 | bool GSocket::SetReusable() |
b082b524 DE |
896 | { |
897 | /* socket must not be null, and must not be in use/already bound */ | |
1aa7b427 DS |
898 | if (this && m_fd == INVALID_SOCKET) |
899 | { | |
948c96ef | 900 | m_reusable = true; |
1aa7b427 | 901 | |
948c96ef | 902 | return true; |
b082b524 | 903 | } |
1aa7b427 | 904 | |
948c96ef | 905 | return false; |
b082b524 DE |
906 | } |
907 | ||
60edcf45 VZ |
908 | bool GSocket::SetBroadcast() |
909 | { | |
910 | /* socket must not be in use/already bound */ | |
911 | if (m_fd == INVALID_SOCKET) { | |
912 | m_broadcast = true; | |
913 | return true; | |
914 | } | |
915 | return false; | |
916 | } | |
917 | ||
918 | bool GSocket::DontDoBind() | |
919 | { | |
920 | /* socket must not be in use/already bound */ | |
921 | if (m_fd == INVALID_SOCKET) { | |
922 | m_dobind = false; | |
923 | return true; | |
924 | } | |
925 | return false; | |
926 | } | |
927 | ||
97e6ee04 DE |
928 | /* Client specific parts */ |
929 | ||
930 | /* GSocket_Connect: | |
931 | * For stream (connection oriented) sockets, GSocket_Connect() tries | |
932 | * to establish a client connection to a server using the peer address | |
933 | * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the | |
d6922577 | 934 | * connection has been successfully established, or one of the error |
97e6ee04 DE |
935 | * codes listed below. Note that for nonblocking sockets, a return |
936 | * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection | |
937 | * request can be completed later; you should use GSocket_Select() | |
938 | * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the | |
939 | * corresponding asynchronous events. | |
940 | * | |
941 | * For datagram (non connection oriented) sockets, GSocket_Connect() | |
942 | * just sets the peer address established with GSocket_SetPeer() as | |
943 | * default destination. | |
944 | * | |
945 | * Error codes: | |
946 | * GSOCK_INVSOCK - the socket is in use or not valid. | |
947 | * GSOCK_INVADDR - the peer address has not been established. | |
948 | * GSOCK_TIMEDOUT - timeout, the connection failed. | |
949 | * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only) | |
950 | * GSOCK_MEMERR - couldn't allocate memory. | |
b082b524 | 951 | * GSOCK_IOERR - low-level error. |
97e6ee04 | 952 | */ |
ba2a81d7 | 953 | GSocketError GSocket::Connect(GSocketStream stream) |
97e6ee04 DE |
954 | { |
955 | int err, ret; | |
956 | int arg = 1; | |
957 | ||
09e6e5ec | 958 | assert(this); |
97e6ee04 DE |
959 | |
960 | /* Enable CONNECTION events (needed for nonblocking connections) */ | |
09e6e5ec | 961 | Enable(GSOCK_CONNECTION); |
97e6ee04 | 962 | |
09e6e5ec | 963 | if (m_fd != INVALID_SOCKET) |
97e6ee04 | 964 | { |
09e6e5ec | 965 | m_error = GSOCK_INVSOCK; |
97e6ee04 DE |
966 | return GSOCK_INVSOCK; |
967 | } | |
968 | ||
09e6e5ec | 969 | if (!m_peer) |
97e6ee04 | 970 | { |
09e6e5ec | 971 | m_error = GSOCK_INVADDR; |
97e6ee04 DE |
972 | return GSOCK_INVADDR; |
973 | } | |
974 | ||
975 | /* Streamed or dgram socket? */ | |
09e6e5ec | 976 | m_stream = (stream == GSOCK_STREAMED); |
948c96ef DE |
977 | m_server = false; |
978 | m_establishing = false; | |
97e6ee04 DE |
979 | |
980 | /* Create the socket */ | |
09e6e5ec DE |
981 | m_fd = socket(m_peer->m_realfamily, |
982 | m_stream? SOCK_STREAM : SOCK_DGRAM, 0); | |
97e6ee04 | 983 | |
09e6e5ec | 984 | if (m_fd == INVALID_SOCKET) |
97e6ee04 | 985 | { |
09e6e5ec | 986 | m_error = GSOCK_IOERR; |
97e6ee04 DE |
987 | return GSOCK_IOERR; |
988 | } | |
bb154f79 KH |
989 | |
990 | /* FreeBSD variants can't use MSG_NOSIGNAL, and instead use a socket option */ | |
991 | #ifdef SO_NOSIGPIPE | |
01ba4b67 | 992 | setsockopt(m_fd, SOL_SOCKET, SO_NOSIGPIPE, (const char*)&arg, sizeof(arg)); |
bb154f79 KH |
993 | #endif |
994 | ||
97e6ee04 | 995 | #if defined(__EMX__) || defined(__VISAGECPP__) |
09e6e5ec | 996 | ioctl(m_fd, FIONBIO, (char*)&arg, sizeof(arg)); |
97e6ee04 | 997 | #else |
09e6e5ec | 998 | ioctl(m_fd, FIONBIO, &arg); |
97e6ee04 | 999 | #endif |
97e6ee04 | 1000 | |
c232b3cd KH |
1001 | // If the reuse flag is set, use the applicable socket reuse flags(s) |
1002 | if (m_reusable) | |
1003 | { | |
01ba4b67 | 1004 | setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&arg, sizeof(arg)); |
c232b3cd | 1005 | #ifdef SO_REUSEPORT |
01ba4b67 | 1006 | setsockopt(m_fd, SOL_SOCKET, SO_REUSEPORT, (const char*)&arg, sizeof(arg)); |
c232b3cd KH |
1007 | #endif |
1008 | } | |
1009 | ||
1010 | // If a local address has been set, then we need to bind to it before calling connect | |
1011 | if (m_local && m_local->m_addr) | |
1012 | { | |
1013 | bind(m_fd, m_local->m_addr, m_local->m_len); | |
1014 | } | |
1015 | ||
97e6ee04 | 1016 | /* Connect it to the peer address, with a timeout (see below) */ |
09e6e5ec | 1017 | ret = connect(m_fd, m_peer->m_addr, m_peer->m_len); |
97e6ee04 | 1018 | |
0d34c30e KH |
1019 | /* We only call Enable_Events if we know we aren't shutting down the socket. |
1020 | * NB: Enable_Events needs to be called whether the socket is blocking or | |
1021 | * non-blocking, it just shouldn't be called prior to knowing there is a | |
1022 | * connection _if_ blocking sockets are being used. | |
1023 | * If connect above returns 0, we are already connected and need to make the | |
17a1ebd1 | 1024 | * call to Enable_Events now. |
a3687636 | 1025 | */ |
17a1ebd1 | 1026 | |
0d34c30e | 1027 | if (m_non_blocking || ret == 0) |
bb154f79 | 1028 | gs_gui_functions->Enable_Events(this); |
bb154f79 | 1029 | |
97e6ee04 DE |
1030 | if (ret == -1) |
1031 | { | |
1032 | err = errno; | |
1033 | ||
1034 | /* If connect failed with EINPROGRESS and the GSocket object | |
1035 | * is in blocking mode, we select() for the specified timeout | |
1036 | * checking for writability to see if the connection request | |
1037 | * completes. | |
1038 | */ | |
09e6e5ec | 1039 | if ((err == EINPROGRESS) && (!m_non_blocking)) |
97e6ee04 | 1040 | { |
09e6e5ec | 1041 | if (Output_Timeout() == GSOCK_TIMEDOUT) |
97e6ee04 | 1042 | { |
09e6e5ec DE |
1043 | Close(); |
1044 | /* m_error is set in _GSocket_Output_Timeout */ | |
97e6ee04 DE |
1045 | return GSOCK_TIMEDOUT; |
1046 | } | |
1047 | else | |
1048 | { | |
1049 | int error; | |
ddc1a35f | 1050 | SOCKOPTLEN_T len = sizeof(error); |
97e6ee04 | 1051 | |
f71bb8ef | 1052 | getsockopt(m_fd, SOL_SOCKET, SO_ERROR, (char*) &error, &len); |
97e6ee04 | 1053 | |
bb154f79 KH |
1054 | gs_gui_functions->Enable_Events(this); |
1055 | ||
97e6ee04 DE |
1056 | if (!error) |
1057 | return GSOCK_NOERROR; | |
1058 | } | |
1059 | } | |
1060 | ||
1061 | /* If connect failed with EINPROGRESS and the GSocket object | |
1062 | * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK | |
1063 | * (and return GSOCK_WOULDBLOCK) but we don't close the socket; | |
1064 | * this way if the connection completes, a GSOCK_CONNECTION | |
1065 | * event will be generated, if enabled. | |
1066 | */ | |
09e6e5ec | 1067 | if ((err == EINPROGRESS) && (m_non_blocking)) |
97e6ee04 | 1068 | { |
948c96ef | 1069 | m_establishing = true; |
09e6e5ec | 1070 | m_error = GSOCK_WOULDBLOCK; |
97e6ee04 DE |
1071 | return GSOCK_WOULDBLOCK; |
1072 | } | |
1073 | ||
1074 | /* If connect failed with an error other than EINPROGRESS, | |
1075 | * then the call to GSocket_Connect has failed. | |
1076 | */ | |
09e6e5ec DE |
1077 | Close(); |
1078 | m_error = GSOCK_IOERR; | |
1aa7b427 | 1079 | |
97e6ee04 DE |
1080 | return GSOCK_IOERR; |
1081 | } | |
1082 | ||
1083 | return GSOCK_NOERROR; | |
1084 | } | |
1085 | ||
1086 | /* Datagram sockets */ | |
1087 | ||
1088 | /* GSocket_SetNonOriented: | |
1089 | * Sets up this socket as a non-connection oriented (datagram) socket. | |
1090 | * Before using this function, the local address must have been set | |
1091 | * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR | |
1092 | * on success, or one of the following otherwise. | |
1093 | * | |
1094 | * Error codes: | |
1095 | * GSOCK_INVSOCK - the socket is in use. | |
1096 | * GSOCK_INVADDR - the local address has not been set. | |
1097 | * GSOCK_IOERR - low-level error. | |
1098 | */ | |
ba2a81d7 | 1099 | GSocketError GSocket::SetNonOriented() |
97e6ee04 DE |
1100 | { |
1101 | int arg = 1; | |
1102 | ||
09e6e5ec | 1103 | assert(this); |
97e6ee04 | 1104 | |
09e6e5ec | 1105 | if (m_fd != INVALID_SOCKET) |
97e6ee04 | 1106 | { |
09e6e5ec | 1107 | m_error = GSOCK_INVSOCK; |
97e6ee04 DE |
1108 | return GSOCK_INVSOCK; |
1109 | } | |
1110 | ||
09e6e5ec | 1111 | if (!m_local) |
97e6ee04 | 1112 | { |
09e6e5ec | 1113 | m_error = GSOCK_INVADDR; |
97e6ee04 DE |
1114 | return GSOCK_INVADDR; |
1115 | } | |
1116 | ||
1117 | /* Initialize all fields */ | |
948c96ef DE |
1118 | m_stream = false; |
1119 | m_server = false; | |
97e6ee04 DE |
1120 | |
1121 | /* Create the socket */ | |
09e6e5ec | 1122 | m_fd = socket(m_local->m_realfamily, SOCK_DGRAM, 0); |
97e6ee04 | 1123 | |
09e6e5ec | 1124 | if (m_fd == INVALID_SOCKET) |
97e6ee04 | 1125 | { |
09e6e5ec | 1126 | m_error = GSOCK_IOERR; |
97e6ee04 DE |
1127 | return GSOCK_IOERR; |
1128 | } | |
1129 | #if defined(__EMX__) || defined(__VISAGECPP__) | |
09e6e5ec | 1130 | ioctl(m_fd, FIONBIO, (char*)&arg, sizeof(arg)); |
97e6ee04 | 1131 | #else |
09e6e5ec | 1132 | ioctl(m_fd, FIONBIO, &arg); |
97e6ee04 | 1133 | #endif |
ba2a81d7 | 1134 | gs_gui_functions->Enable_Events(this); |
97e6ee04 | 1135 | |
01ba4b67 VZ |
1136 | if (m_reusable) |
1137 | { | |
1138 | setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&arg, sizeof(arg)); | |
1139 | #ifdef SO_REUSEPORT | |
1140 | setsockopt(m_fd, SOL_SOCKET, SO_REUSEPORT, (const char*)&arg, sizeof(arg)); | |
1141 | #endif | |
1142 | } | |
1143 | ||
60edcf45 | 1144 | if (m_broadcast) |
97e6ee04 | 1145 | { |
60edcf45 VZ |
1146 | setsockopt(m_fd, SOL_SOCKET, SO_BROADCAST, (const char*)&arg, sizeof(arg)); |
1147 | } | |
1148 | if (m_dobind) | |
1149 | { | |
1150 | /* Bind to the local address, | |
1151 | * and retrieve the actual address bound. | |
1152 | */ | |
1153 | if ((bind(m_fd, m_local->m_addr, m_local->m_len) != 0) || | |
1154 | (getsockname(m_fd, | |
1155 | m_local->m_addr, | |
1156 | (WX_SOCKLEN_T *) &m_local->m_len) != 0)) | |
1157 | { | |
1158 | Close(); | |
1159 | m_error = GSOCK_IOERR; | |
1160 | return GSOCK_IOERR; | |
1161 | } | |
97e6ee04 | 1162 | } |
97e6ee04 DE |
1163 | return GSOCK_NOERROR; |
1164 | } | |
1165 | ||
1166 | /* Generic IO */ | |
1167 | ||
1168 | /* Like recv(), send(), ... */ | |
ba2a81d7 | 1169 | int GSocket::Read(char *buffer, int size) |
97e6ee04 DE |
1170 | { |
1171 | int ret; | |
1172 | ||
09e6e5ec | 1173 | assert(this); |
97e6ee04 | 1174 | |
09e6e5ec | 1175 | if (m_fd == INVALID_SOCKET || m_server) |
97e6ee04 | 1176 | { |
09e6e5ec | 1177 | m_error = GSOCK_INVSOCK; |
97e6ee04 DE |
1178 | return -1; |
1179 | } | |
1180 | ||
9d715960 DE |
1181 | /* Disable events during query of socket status */ |
1182 | Disable(GSOCK_INPUT); | |
b082b524 | 1183 | |
97e6ee04 | 1184 | /* If the socket is blocking, wait for data (with a timeout) */ |
976abb72 VZ |
1185 | if (Input_Timeout() == GSOCK_TIMEDOUT) { |
1186 | m_error = GSOCK_TIMEDOUT; | |
1187 | /* Don't return here immediately, otherwise socket events would not be | |
1188 | * re-enabled! */ | |
9d715960 | 1189 | ret = -1; |
976abb72 | 1190 | } |
1aa7b427 DS |
1191 | else |
1192 | { | |
9d715960 DE |
1193 | /* Read the data */ |
1194 | if (m_stream) | |
1195 | ret = Recv_Stream(buffer, size); | |
1196 | else | |
1197 | ret = Recv_Dgram(buffer, size); | |
b082b524 | 1198 | |
f4854380 | 1199 | /* If recv returned zero, then the connection has been gracefully closed. |
9b67181b KH |
1200 | * Otherwise, recv has returned an error (-1), in which case we have lost the |
1201 | * socket only if errno does _not_ indicate that there may be more data to read. | |
1202 | */ | |
1203 | if (ret == 0) | |
98e7a7f9 | 1204 | { |
f4854380 | 1205 | /* Make sure wxSOCKET_LOST event gets sent and shut down the socket */ |
98e7a7f9 | 1206 | m_detected = GSOCK_LOST_FLAG; |
f4854380 VZ |
1207 | Detected_Read(); |
1208 | return 0; | |
98e7a7f9 | 1209 | } |
1aa7b427 DS |
1210 | else if (ret == -1) |
1211 | { | |
976abb72 VZ |
1212 | if ((errno == EWOULDBLOCK) || (errno == EAGAIN)) |
1213 | m_error = GSOCK_WOULDBLOCK; | |
1214 | else | |
1215 | m_error = GSOCK_IOERR; | |
1216 | } | |
97e6ee04 | 1217 | } |
b082b524 | 1218 | |
9d715960 | 1219 | /* Enable events again now that we are done processing */ |
09e6e5ec | 1220 | Enable(GSOCK_INPUT); |
97e6ee04 DE |
1221 | |
1222 | return ret; | |
1223 | } | |
1224 | ||
ba2a81d7 | 1225 | int GSocket::Write(const char *buffer, int size) |
b082b524 | 1226 | { |
97e6ee04 DE |
1227 | int ret; |
1228 | ||
09e6e5ec | 1229 | assert(this); |
b082b524 | 1230 | |
97e6ee04 DE |
1231 | GSocket_Debug(( "GSocket_Write #1, size %d\n", size )); |
1232 | ||
09e6e5ec | 1233 | if (m_fd == INVALID_SOCKET || m_server) |
97e6ee04 | 1234 | { |
09e6e5ec | 1235 | m_error = GSOCK_INVSOCK; |
97e6ee04 DE |
1236 | return -1; |
1237 | } | |
1238 | ||
1239 | GSocket_Debug(( "GSocket_Write #2, size %d\n", size )); | |
1240 | ||
1241 | /* If the socket is blocking, wait for writability (with a timeout) */ | |
09e6e5ec | 1242 | if (Output_Timeout() == GSOCK_TIMEDOUT) |
97e6ee04 DE |
1243 | return -1; |
1244 | ||
1245 | GSocket_Debug(( "GSocket_Write #3, size %d\n", size )); | |
1246 | ||
1247 | /* Write the data */ | |
09e6e5ec DE |
1248 | if (m_stream) |
1249 | ret = Send_Stream(buffer, size); | |
97e6ee04 | 1250 | else |
09e6e5ec | 1251 | ret = Send_Dgram(buffer, size); |
b082b524 | 1252 | |
97e6ee04 DE |
1253 | GSocket_Debug(( "GSocket_Write #4, size %d\n", size )); |
1254 | ||
1255 | if (ret == -1) | |
1256 | { | |
7e1e6965 | 1257 | if ((errno == EWOULDBLOCK) || (errno == EAGAIN)) |
97e6ee04 | 1258 | { |
09e6e5ec | 1259 | m_error = GSOCK_WOULDBLOCK; |
97e6ee04 DE |
1260 | GSocket_Debug(( "GSocket_Write error WOULDBLOCK\n" )); |
1261 | } | |
1262 | else | |
1263 | { | |
09e6e5ec | 1264 | m_error = GSOCK_IOERR; |
97e6ee04 DE |
1265 | GSocket_Debug(( "GSocket_Write error IOERR\n" )); |
1266 | } | |
1267 | ||
1268 | /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect | |
1269 | * in MSW). Once the first OUTPUT event is received, users can assume | |
1270 | * that the socket is writable until a read operation fails. Only then | |
1271 | * will further OUTPUT events be posted. | |
1272 | */ | |
09e6e5ec | 1273 | Enable(GSOCK_OUTPUT); |
1aa7b427 | 1274 | |
97e6ee04 DE |
1275 | return -1; |
1276 | } | |
b082b524 | 1277 | |
97e6ee04 DE |
1278 | GSocket_Debug(( "GSocket_Write #5, size %d ret %d\n", size, ret )); |
1279 | ||
1280 | return ret; | |
1281 | } | |
1282 | ||
1283 | /* GSocket_Select: | |
1284 | * Polls the socket to determine its status. This function will | |
1285 | * check for the events specified in the 'flags' parameter, and | |
1286 | * it will return a mask indicating which operations can be | |
1287 | * performed. This function won't block, regardless of the | |
1288 | * mode (blocking | nonblocking) of the socket. | |
1289 | */ | |
ba2a81d7 | 1290 | GSocketEventFlags GSocket::Select(GSocketEventFlags flags) |
97e6ee04 | 1291 | { |
ba2a81d7 | 1292 | if (!gs_gui_functions->CanUseEventLoop()) |
97e6ee04 DE |
1293 | { |
1294 | ||
1295 | GSocketEventFlags result = 0; | |
1296 | fd_set readfds; | |
1297 | fd_set writefds; | |
1298 | fd_set exceptfds; | |
1299 | struct timeval tv; | |
1300 | ||
9d715960 DE |
1301 | assert(this); |
1302 | ||
60b0bd1d JS |
1303 | if (m_fd == -1) |
1304 | return (GSOCK_LOST_FLAG & flags); | |
17a1ebd1 | 1305 | |
97e6ee04 | 1306 | /* Do not use a static struct, Linux can garble it */ |
09e6e5ec | 1307 | tv.tv_sec = m_timeout / 1000; |
7488a05e | 1308 | tv.tv_usec = (m_timeout % 1000) * 1000; |
97e6ee04 | 1309 | |
17a1ebd1 VZ |
1310 | wxFD_ZERO(&readfds); |
1311 | wxFD_ZERO(&writefds); | |
1312 | wxFD_ZERO(&exceptfds); | |
1313 | wxFD_SET(m_fd, &readfds); | |
9d715960 | 1314 | if (flags & GSOCK_OUTPUT_FLAG || flags & GSOCK_CONNECTION_FLAG) |
17a1ebd1 VZ |
1315 | wxFD_SET(m_fd, &writefds); |
1316 | wxFD_SET(m_fd, &exceptfds); | |
97e6ee04 DE |
1317 | |
1318 | /* Check 'sticky' CONNECTION flag first */ | |
09e6e5ec | 1319 | result |= (GSOCK_CONNECTION_FLAG & m_detected); |
97e6ee04 DE |
1320 | |
1321 | /* If we have already detected a LOST event, then don't try | |
1322 | * to do any further processing. | |
1323 | */ | |
09e6e5ec | 1324 | if ((m_detected & GSOCK_LOST_FLAG) != 0) |
97e6ee04 | 1325 | { |
948c96ef | 1326 | m_establishing = false; |
97e6ee04 DE |
1327 | |
1328 | return (GSOCK_LOST_FLAG & flags); | |
1329 | } | |
1330 | ||
1331 | /* Try select now */ | |
09e6e5ec | 1332 | if (select(m_fd + 1, &readfds, &writefds, &exceptfds, &tv) <= 0) |
97e6ee04 DE |
1333 | { |
1334 | /* What to do here? */ | |
1335 | return (result & flags); | |
1336 | } | |
1337 | ||
b74865fa KH |
1338 | /* Check for exceptions and errors */ |
1339 | if (wxFD_ISSET(m_fd, &exceptfds)) | |
1340 | { | |
1341 | m_establishing = false; | |
1342 | m_detected = GSOCK_LOST_FLAG; | |
1343 | ||
1344 | /* LOST event: Abort any further processing */ | |
1345 | return (GSOCK_LOST_FLAG & flags); | |
1346 | } | |
1347 | ||
97e6ee04 | 1348 | /* Check for readability */ |
17a1ebd1 | 1349 | if (wxFD_ISSET(m_fd, &readfds)) |
97e6ee04 | 1350 | { |
b74865fa | 1351 | result |= GSOCK_INPUT_FLAG; |
bb154f79 | 1352 | |
b74865fa | 1353 | if (m_server && m_stream) |
97e6ee04 | 1354 | { |
b74865fa KH |
1355 | /* This is a TCP server socket that detected a connection. |
1356 | While the INPUT_FLAG is also set, it doesn't matter on | |
01ba4b67 | 1357 | this kind of sockets, as we can only Accept() from them. */ |
b74865fa KH |
1358 | result |= GSOCK_CONNECTION_FLAG; |
1359 | m_detected |= GSOCK_CONNECTION_FLAG; | |
97e6ee04 DE |
1360 | } |
1361 | } | |
1362 | ||
1363 | /* Check for writability */ | |
17a1ebd1 | 1364 | if (wxFD_ISSET(m_fd, &writefds)) |
97e6ee04 | 1365 | { |
09e6e5ec | 1366 | if (m_establishing && !m_server) |
97e6ee04 DE |
1367 | { |
1368 | int error; | |
ddc1a35f | 1369 | SOCKOPTLEN_T len = sizeof(error); |
97e6ee04 | 1370 | |
948c96ef | 1371 | m_establishing = false; |
97e6ee04 | 1372 | |
f71bb8ef | 1373 | getsockopt(m_fd, SOL_SOCKET, SO_ERROR, (char*)&error, &len); |
97e6ee04 DE |
1374 | |
1375 | if (error) | |
1376 | { | |
09e6e5ec | 1377 | m_detected = GSOCK_LOST_FLAG; |
97e6ee04 DE |
1378 | |
1379 | /* LOST event: Abort any further processing */ | |
1380 | return (GSOCK_LOST_FLAG & flags); | |
1381 | } | |
1382 | else | |
1383 | { | |
1384 | result |= GSOCK_CONNECTION_FLAG; | |
09e6e5ec | 1385 | m_detected |= GSOCK_CONNECTION_FLAG; |
97e6ee04 DE |
1386 | } |
1387 | } | |
1388 | else | |
1389 | { | |
1390 | result |= GSOCK_OUTPUT_FLAG; | |
1391 | } | |
1392 | } | |
97e6ee04 DE |
1393 | |
1394 | return (result & flags); | |
1395 | ||
1396 | } | |
1397 | else | |
1398 | { | |
09e6e5ec DE |
1399 | assert(this); |
1400 | return flags & m_detected; | |
97e6ee04 DE |
1401 | } |
1402 | } | |
1403 | ||
1404 | /* Flags */ | |
1405 | ||
1406 | /* GSocket_SetNonBlocking: | |
1407 | * Sets the socket to non-blocking mode. All IO calls will return | |
1408 | * immediately. | |
1409 | */ | |
948c96ef | 1410 | void GSocket::SetNonBlocking(bool non_block) |
97e6ee04 | 1411 | { |
09e6e5ec | 1412 | assert(this); |
97e6ee04 DE |
1413 | |
1414 | GSocket_Debug( ("GSocket_SetNonBlocking: %d\n", (int)non_block) ); | |
1415 | ||
09e6e5ec | 1416 | m_non_blocking = non_block; |
97e6ee04 DE |
1417 | } |
1418 | ||
1419 | /* GSocket_SetTimeout: | |
1420 | * Sets the timeout for blocking calls. Time is expressed in | |
1421 | * milliseconds. | |
1422 | */ | |
ba2a81d7 | 1423 | void GSocket::SetTimeout(unsigned long millisec) |
97e6ee04 | 1424 | { |
09e6e5ec | 1425 | assert(this); |
97e6ee04 | 1426 | |
09e6e5ec | 1427 | m_timeout = millisec; |
97e6ee04 DE |
1428 | } |
1429 | ||
1430 | /* GSocket_GetError: | |
d6922577 | 1431 | * Returns the last error occurred for this socket. Note that successful |
97e6ee04 DE |
1432 | * operations do not clear this back to GSOCK_NOERROR, so use it only |
1433 | * after an error. | |
1434 | */ | |
ba2a81d7 | 1435 | GSocketError WXDLLIMPEXP_NET GSocket::GetError() |
97e6ee04 | 1436 | { |
09e6e5ec | 1437 | assert(this); |
97e6ee04 | 1438 | |
09e6e5ec | 1439 | return m_error; |
97e6ee04 DE |
1440 | } |
1441 | ||
1442 | /* Callbacks */ | |
1443 | ||
1444 | /* GSOCK_INPUT: | |
1445 | * There is data to be read in the input buffer. If, after a read | |
1446 | * operation, there is still data available, the callback function will | |
1447 | * be called again. | |
1448 | * GSOCK_OUTPUT: | |
b082b524 | 1449 | * The socket is available for writing. That is, the next write call |
97e6ee04 DE |
1450 | * won't block. This event is generated only once, when the connection is |
1451 | * first established, and then only if a call failed with GSOCK_WOULDBLOCK, | |
1452 | * when the output buffer empties again. This means that the app should | |
1453 | * assume that it can write since the first OUTPUT event, and no more | |
1454 | * OUTPUT events will be generated unless an error occurs. | |
1455 | * GSOCK_CONNECTION: | |
d6922577 | 1456 | * Connection successfully established, for client sockets, or incoming |
97e6ee04 DE |
1457 | * client connection, for server sockets. Wait for this event (also watch |
1458 | * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call. | |
1459 | * GSOCK_LOST: | |
1460 | * The connection is lost (or a connection request failed); this could | |
1461 | * be due to a failure, or due to the peer closing it gracefully. | |
1462 | */ | |
1463 | ||
1464 | /* GSocket_SetCallback: | |
1465 | * Enables the callbacks specified by 'flags'. Note that 'flags' | |
1466 | * may be a combination of flags OR'ed toghether, so the same | |
1467 | * callback function can be made to accept different events. | |
1468 | * The callback function must have the following prototype: | |
1469 | * | |
1470 | * void function(GSocket *socket, GSocketEvent event, char *cdata) | |
1471 | */ | |
ba2a81d7 | 1472 | void GSocket::SetCallback(GSocketEventFlags flags, |
97e6ee04 DE |
1473 | GSocketCallback callback, char *cdata) |
1474 | { | |
1475 | int count; | |
1476 | ||
09e6e5ec | 1477 | assert(this); |
97e6ee04 DE |
1478 | |
1479 | for (count = 0; count < GSOCK_MAX_EVENT; count++) | |
1480 | { | |
1481 | if ((flags & (1 << count)) != 0) | |
1482 | { | |
09e6e5ec DE |
1483 | m_cbacks[count] = callback; |
1484 | m_data[count] = cdata; | |
97e6ee04 DE |
1485 | } |
1486 | } | |
1487 | } | |
1488 | ||
1489 | /* GSocket_UnsetCallback: | |
1490 | * Disables all callbacks specified by 'flags', which may be a | |
1491 | * combination of flags OR'ed toghether. | |
1492 | */ | |
ba2a81d7 | 1493 | void GSocket::UnsetCallback(GSocketEventFlags flags) |
97e6ee04 DE |
1494 | { |
1495 | int count; | |
1496 | ||
09e6e5ec | 1497 | assert(this); |
97e6ee04 DE |
1498 | |
1499 | for (count = 0; count < GSOCK_MAX_EVENT; count++) | |
1500 | { | |
1501 | if ((flags & (1 << count)) != 0) | |
1502 | { | |
09e6e5ec DE |
1503 | m_cbacks[count] = NULL; |
1504 | m_data[count] = NULL; | |
97e6ee04 DE |
1505 | } |
1506 | } | |
1507 | } | |
1508 | ||
ba2a81d7 | 1509 | GSocketError GSocket::GetSockOpt(int level, int optname, |
b082b524 DE |
1510 | void *optval, int *optlen) |
1511 | { | |
ddc1a35f | 1512 | if (getsockopt(m_fd, level, optname, (char*)optval, (SOCKOPTLEN_T*)optlen) == 0) |
b082b524 | 1513 | return GSOCK_NOERROR; |
1aa7b427 | 1514 | |
b082b524 DE |
1515 | return GSOCK_OPTERR; |
1516 | } | |
1517 | ||
ba2a81d7 | 1518 | GSocketError GSocket::SetSockOpt(int level, int optname, |
b082b524 DE |
1519 | const void *optval, int optlen) |
1520 | { | |
f71bb8ef | 1521 | if (setsockopt(m_fd, level, optname, (const char*)optval, optlen) == 0) |
b082b524 | 1522 | return GSOCK_NOERROR; |
1aa7b427 | 1523 | |
b082b524 DE |
1524 | return GSOCK_OPTERR; |
1525 | } | |
1526 | ||
ba2a81d7 | 1527 | #define CALL_CALLBACK(socket, event) { \ |
9a7e4a56 | 1528 | socket->Disable(event); \ |
ba2a81d7 DE |
1529 | if (socket->m_cbacks[event]) \ |
1530 | socket->m_cbacks[event](socket, event, socket->m_data[event]); \ | |
97e6ee04 DE |
1531 | } |
1532 | ||
1533 | ||
ba2a81d7 | 1534 | void GSocket::Enable(GSocketEvent event) |
97e6ee04 | 1535 | { |
09e6e5ec | 1536 | m_detected &= ~(1 << event); |
ba2a81d7 | 1537 | gs_gui_functions->Install_Callback(this, event); |
97e6ee04 DE |
1538 | } |
1539 | ||
ba2a81d7 | 1540 | void GSocket::Disable(GSocketEvent event) |
97e6ee04 | 1541 | { |
09e6e5ec | 1542 | m_detected |= (1 << event); |
ba2a81d7 | 1543 | gs_gui_functions->Uninstall_Callback(this, event); |
97e6ee04 DE |
1544 | } |
1545 | ||
1546 | /* _GSocket_Input_Timeout: | |
1547 | * For blocking sockets, wait until data is available or | |
1548 | * until timeout ellapses. | |
1549 | */ | |
ba2a81d7 | 1550 | GSocketError GSocket::Input_Timeout() |
97e6ee04 DE |
1551 | { |
1552 | struct timeval tv; | |
1553 | fd_set readfds; | |
1554 | int ret; | |
1555 | ||
1556 | /* Linux select() will overwrite the struct on return */ | |
09e6e5ec DE |
1557 | tv.tv_sec = (m_timeout / 1000); |
1558 | tv.tv_usec = (m_timeout % 1000) * 1000; | |
97e6ee04 | 1559 | |
09e6e5ec | 1560 | if (!m_non_blocking) |
97e6ee04 | 1561 | { |
17a1ebd1 VZ |
1562 | wxFD_ZERO(&readfds); |
1563 | wxFD_SET(m_fd, &readfds); | |
09e6e5ec | 1564 | ret = select(m_fd + 1, &readfds, NULL, NULL, &tv); |
97e6ee04 DE |
1565 | if (ret == 0) |
1566 | { | |
1567 | GSocket_Debug(( "GSocket_Input_Timeout, select returned 0\n" )); | |
09e6e5ec | 1568 | m_error = GSOCK_TIMEDOUT; |
97e6ee04 DE |
1569 | return GSOCK_TIMEDOUT; |
1570 | } | |
1aa7b427 | 1571 | |
97e6ee04 DE |
1572 | if (ret == -1) |
1573 | { | |
1574 | GSocket_Debug(( "GSocket_Input_Timeout, select returned -1\n" )); | |
1575 | if (errno == EBADF) { GSocket_Debug(( "Invalid file descriptor\n" )); } | |
1576 | if (errno == EINTR) { GSocket_Debug(( "A non blocked signal was caught\n" )); } | |
1577 | if (errno == EINVAL) { GSocket_Debug(( "The highest number descriptor is negative\n" )); } | |
1578 | if (errno == ENOMEM) { GSocket_Debug(( "Not enough memory\n" )); } | |
09e6e5ec | 1579 | m_error = GSOCK_TIMEDOUT; |
97e6ee04 DE |
1580 | return GSOCK_TIMEDOUT; |
1581 | } | |
1582 | } | |
1aa7b427 | 1583 | |
97e6ee04 DE |
1584 | return GSOCK_NOERROR; |
1585 | } | |
1586 | ||
1587 | /* _GSocket_Output_Timeout: | |
1588 | * For blocking sockets, wait until data can be sent without | |
1589 | * blocking or until timeout ellapses. | |
1590 | */ | |
ba2a81d7 | 1591 | GSocketError GSocket::Output_Timeout() |
97e6ee04 DE |
1592 | { |
1593 | struct timeval tv; | |
1594 | fd_set writefds; | |
1595 | int ret; | |
1596 | ||
1597 | /* Linux select() will overwrite the struct on return */ | |
09e6e5ec DE |
1598 | tv.tv_sec = (m_timeout / 1000); |
1599 | tv.tv_usec = (m_timeout % 1000) * 1000; | |
97e6ee04 | 1600 | |
09e6e5ec | 1601 | GSocket_Debug( ("m_non_blocking has: %d\n", (int)m_non_blocking) ); |
97e6ee04 | 1602 | |
09e6e5ec | 1603 | if (!m_non_blocking) |
97e6ee04 | 1604 | { |
17a1ebd1 VZ |
1605 | wxFD_ZERO(&writefds); |
1606 | wxFD_SET(m_fd, &writefds); | |
09e6e5ec | 1607 | ret = select(m_fd + 1, NULL, &writefds, NULL, &tv); |
97e6ee04 DE |
1608 | if (ret == 0) |
1609 | { | |
1610 | GSocket_Debug(( "GSocket_Output_Timeout, select returned 0\n" )); | |
09e6e5ec | 1611 | m_error = GSOCK_TIMEDOUT; |
97e6ee04 DE |
1612 | return GSOCK_TIMEDOUT; |
1613 | } | |
1aa7b427 | 1614 | |
97e6ee04 DE |
1615 | if (ret == -1) |
1616 | { | |
1617 | GSocket_Debug(( "GSocket_Output_Timeout, select returned -1\n" )); | |
1618 | if (errno == EBADF) { GSocket_Debug(( "Invalid file descriptor\n" )); } | |
1619 | if (errno == EINTR) { GSocket_Debug(( "A non blocked signal was caught\n" )); } | |
1620 | if (errno == EINVAL) { GSocket_Debug(( "The highest number descriptor is negative\n" )); } | |
1621 | if (errno == ENOMEM) { GSocket_Debug(( "Not enough memory\n" )); } | |
09e6e5ec | 1622 | m_error = GSOCK_TIMEDOUT; |
97e6ee04 DE |
1623 | return GSOCK_TIMEDOUT; |
1624 | } | |
1aa7b427 DS |
1625 | |
1626 | if ( ! wxFD_ISSET(m_fd, &writefds) ) | |
1627 | { | |
97e6ee04 DE |
1628 | GSocket_Debug(( "GSocket_Output_Timeout is buggy!\n" )); |
1629 | } | |
1aa7b427 DS |
1630 | else |
1631 | { | |
97e6ee04 DE |
1632 | GSocket_Debug(( "GSocket_Output_Timeout seems correct\n" )); |
1633 | } | |
1634 | } | |
1635 | else | |
1636 | { | |
1637 | GSocket_Debug(( "GSocket_Output_Timeout, didn't try select!\n" )); | |
1638 | } | |
1639 | ||
1640 | return GSOCK_NOERROR; | |
1641 | } | |
1642 | ||
ba2a81d7 | 1643 | int GSocket::Recv_Stream(char *buffer, int size) |
97e6ee04 | 1644 | { |
bb154f79 | 1645 | int ret; |
7e1e6965 | 1646 | do |
bb154f79 KH |
1647 | { |
1648 | ret = recv(m_fd, buffer, size, GSOCKET_MSG_NOSIGNAL); | |
1aa7b427 DS |
1649 | } |
1650 | while (ret == -1 && errno == EINTR); /* Loop until not interrupted */ | |
1651 | ||
bb154f79 | 1652 | return ret; |
97e6ee04 DE |
1653 | } |
1654 | ||
ba2a81d7 | 1655 | int GSocket::Recv_Dgram(char *buffer, int size) |
97e6ee04 DE |
1656 | { |
1657 | struct sockaddr from; | |
9e03e02d | 1658 | WX_SOCKLEN_T fromlen = sizeof(from); |
97e6ee04 DE |
1659 | int ret; |
1660 | GSocketError err; | |
1661 | ||
1662 | fromlen = sizeof(from); | |
1663 | ||
7e1e6965 | 1664 | do |
bb154f79 | 1665 | { |
9e03e02d | 1666 | ret = recvfrom(m_fd, buffer, size, 0, &from, (WX_SOCKLEN_T *) &fromlen); |
1aa7b427 DS |
1667 | } |
1668 | while (ret == -1 && errno == EINTR); /* Loop until not interrupted */ | |
97e6ee04 DE |
1669 | |
1670 | if (ret == -1) | |
1671 | return -1; | |
1672 | ||
1673 | /* Translate a system address into a GSocket address */ | |
09e6e5ec | 1674 | if (!m_peer) |
97e6ee04 | 1675 | { |
09e6e5ec DE |
1676 | m_peer = GAddress_new(); |
1677 | if (!m_peer) | |
97e6ee04 | 1678 | { |
09e6e5ec | 1679 | m_error = GSOCK_MEMERR; |
97e6ee04 DE |
1680 | return -1; |
1681 | } | |
1682 | } | |
1aa7b427 | 1683 | |
09e6e5ec | 1684 | err = _GAddress_translate_from(m_peer, &from, fromlen); |
97e6ee04 DE |
1685 | if (err != GSOCK_NOERROR) |
1686 | { | |
09e6e5ec DE |
1687 | GAddress_destroy(m_peer); |
1688 | m_peer = NULL; | |
1689 | m_error = err; | |
97e6ee04 DE |
1690 | return -1; |
1691 | } | |
1692 | ||
1693 | return ret; | |
1694 | } | |
1695 | ||
ba2a81d7 | 1696 | int GSocket::Send_Stream(const char *buffer, int size) |
97e6ee04 DE |
1697 | { |
1698 | int ret; | |
1699 | ||
7e1e6965 WS |
1700 | MASK_SIGNAL(); |
1701 | ||
1702 | do | |
bb154f79 KH |
1703 | { |
1704 | ret = send(m_fd, (char *)buffer, size, GSOCKET_MSG_NOSIGNAL); | |
1aa7b427 DS |
1705 | } |
1706 | while (ret == -1 && errno == EINTR); /* Loop until not interrupted */ | |
7e1e6965 | 1707 | |
97e6ee04 | 1708 | UNMASK_SIGNAL(); |
97e6ee04 DE |
1709 | |
1710 | return ret; | |
1711 | } | |
1712 | ||
ba2a81d7 | 1713 | int GSocket::Send_Dgram(const char *buffer, int size) |
97e6ee04 DE |
1714 | { |
1715 | struct sockaddr *addr; | |
1716 | int len, ret; | |
1717 | GSocketError err; | |
1718 | ||
09e6e5ec | 1719 | if (!m_peer) |
97e6ee04 | 1720 | { |
09e6e5ec | 1721 | m_error = GSOCK_INVADDR; |
97e6ee04 DE |
1722 | return -1; |
1723 | } | |
1724 | ||
09e6e5ec | 1725 | err = _GAddress_translate_to(m_peer, &addr, &len); |
97e6ee04 DE |
1726 | if (err != GSOCK_NOERROR) |
1727 | { | |
09e6e5ec | 1728 | m_error = err; |
97e6ee04 DE |
1729 | return -1; |
1730 | } | |
1731 | ||
97e6ee04 | 1732 | MASK_SIGNAL(); |
7e1e6965 WS |
1733 | |
1734 | do | |
bb154f79 KH |
1735 | { |
1736 | ret = sendto(m_fd, (char *)buffer, size, 0, addr, len); | |
1aa7b427 DS |
1737 | } |
1738 | while (ret == -1 && errno == EINTR); /* Loop until not interrupted */ | |
7e1e6965 | 1739 | |
97e6ee04 | 1740 | UNMASK_SIGNAL(); |
97e6ee04 DE |
1741 | |
1742 | /* Frees memory allocated from _GAddress_translate_to */ | |
1743 | free(addr); | |
1744 | ||
1745 | return ret; | |
1746 | } | |
1747 | ||
ba2a81d7 | 1748 | void GSocket::Detected_Read() |
97e6ee04 DE |
1749 | { |
1750 | char c; | |
1751 | ||
bb154f79 KH |
1752 | /* Safeguard against straggling call to Detected_Read */ |
1753 | if (m_fd == INVALID_SOCKET) | |
1754 | { | |
1755 | return; | |
1756 | } | |
1757 | ||
97e6ee04 DE |
1758 | /* If we have already detected a LOST event, then don't try |
1759 | * to do any further processing. | |
1760 | */ | |
09e6e5ec | 1761 | if ((m_detected & GSOCK_LOST_FLAG) != 0) |
97e6ee04 | 1762 | { |
948c96ef | 1763 | m_establishing = false; |
97e6ee04 | 1764 | |
ba2a81d7 | 1765 | CALL_CALLBACK(this, GSOCK_LOST); |
09e6e5ec | 1766 | Shutdown(); |
97e6ee04 DE |
1767 | return; |
1768 | } | |
1769 | ||
bb154f79 KH |
1770 | int num = recv(m_fd, &c, 1, MSG_PEEK | GSOCKET_MSG_NOSIGNAL); |
1771 | ||
1772 | if (num > 0) | |
97e6ee04 | 1773 | { |
ba2a81d7 | 1774 | CALL_CALLBACK(this, GSOCK_INPUT); |
97e6ee04 DE |
1775 | } |
1776 | else | |
1777 | { | |
09e6e5ec | 1778 | if (m_server && m_stream) |
97e6ee04 | 1779 | { |
ba2a81d7 | 1780 | CALL_CALLBACK(this, GSOCK_CONNECTION); |
97e6ee04 | 1781 | } |
e37e082e VZ |
1782 | else if (num == 0) |
1783 | { | |
1784 | /* graceful shutdown */ | |
1785 | CALL_CALLBACK(this, GSOCK_LOST); | |
1786 | Shutdown(); | |
1787 | } | |
97e6ee04 DE |
1788 | else |
1789 | { | |
e400d27d | 1790 | /* Do not throw a lost event in cases where the socket isn't really lost */ |
7e1e6965 | 1791 | if ((errno == EWOULDBLOCK) || (errno == EAGAIN) || (errno == EINTR)) |
bb154f79 KH |
1792 | { |
1793 | CALL_CALLBACK(this, GSOCK_INPUT); | |
1794 | } | |
7e1e6965 | 1795 | else |
bb154f79 KH |
1796 | { |
1797 | CALL_CALLBACK(this, GSOCK_LOST); | |
1798 | Shutdown(); | |
7e1e6965 | 1799 | } |
97e6ee04 DE |
1800 | } |
1801 | } | |
1802 | } | |
1803 | ||
ba2a81d7 | 1804 | void GSocket::Detected_Write() |
97e6ee04 DE |
1805 | { |
1806 | /* If we have already detected a LOST event, then don't try | |
1807 | * to do any further processing. | |
1808 | */ | |
09e6e5ec | 1809 | if ((m_detected & GSOCK_LOST_FLAG) != 0) |
97e6ee04 | 1810 | { |
948c96ef | 1811 | m_establishing = false; |
97e6ee04 | 1812 | |
ba2a81d7 | 1813 | CALL_CALLBACK(this, GSOCK_LOST); |
09e6e5ec | 1814 | Shutdown(); |
97e6ee04 DE |
1815 | return; |
1816 | } | |
1817 | ||
09e6e5ec | 1818 | if (m_establishing && !m_server) |
97e6ee04 DE |
1819 | { |
1820 | int error; | |
ddc1a35f | 1821 | SOCKOPTLEN_T len = sizeof(error); |
97e6ee04 | 1822 | |
948c96ef | 1823 | m_establishing = false; |
97e6ee04 | 1824 | |
f71bb8ef | 1825 | getsockopt(m_fd, SOL_SOCKET, SO_ERROR, (char*)&error, &len); |
97e6ee04 DE |
1826 | |
1827 | if (error) | |
1828 | { | |
ba2a81d7 | 1829 | CALL_CALLBACK(this, GSOCK_LOST); |
09e6e5ec | 1830 | Shutdown(); |
97e6ee04 DE |
1831 | } |
1832 | else | |
1833 | { | |
ba2a81d7 | 1834 | CALL_CALLBACK(this, GSOCK_CONNECTION); |
97e6ee04 DE |
1835 | /* We have to fire this event by hand because CONNECTION (for clients) |
1836 | * and OUTPUT are internally the same and we just disabled CONNECTION | |
1837 | * events with the above macro. | |
1838 | */ | |
ba2a81d7 | 1839 | CALL_CALLBACK(this, GSOCK_OUTPUT); |
97e6ee04 DE |
1840 | } |
1841 | } | |
1842 | else | |
1843 | { | |
ba2a81d7 | 1844 | CALL_CALLBACK(this, GSOCK_OUTPUT); |
97e6ee04 DE |
1845 | } |
1846 | } | |
1847 | ||
09e6e5ec DE |
1848 | /* Compatibility functions for GSocket */ |
1849 | GSocket *GSocket_new(void) | |
1850 | { | |
ba2a81d7 | 1851 | GSocket *newsocket = new GSocket(); |
1aa7b427 | 1852 | if (newsocket->IsOk()) |
09e6e5ec | 1853 | return newsocket; |
1aa7b427 | 1854 | |
09e6e5ec | 1855 | delete newsocket; |
1aa7b427 | 1856 | |
09e6e5ec DE |
1857 | return NULL; |
1858 | } | |
1859 | ||
97e6ee04 DE |
1860 | /* |
1861 | * ------------------------------------------------------------------------- | |
1862 | * GAddress | |
1863 | * ------------------------------------------------------------------------- | |
1864 | */ | |
1865 | ||
1866 | /* CHECK_ADDRESS verifies that the current address family is either | |
1867 | * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it | |
1868 | * initalizes it to be a GSOCK_*family*. In other cases, it returns | |
1869 | * an appropiate error code. | |
1870 | * | |
1871 | * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error. | |
1872 | */ | |
1873 | #define CHECK_ADDRESS(address, family) \ | |
1874 | { \ | |
1875 | if (address->m_family == GSOCK_NOFAMILY) \ | |
1876 | if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \ | |
1877 | return address->m_error; \ | |
1878 | if (address->m_family != GSOCK_##family) \ | |
1879 | { \ | |
1880 | address->m_error = GSOCK_INVADDR; \ | |
1881 | return GSOCK_INVADDR; \ | |
1882 | } \ | |
1883 | } | |
1884 | ||
1885 | #define CHECK_ADDRESS_RETVAL(address, family, retval) \ | |
1886 | { \ | |
1887 | if (address->m_family == GSOCK_NOFAMILY) \ | |
1888 | if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \ | |
1889 | return retval; \ | |
1890 | if (address->m_family != GSOCK_##family) \ | |
1891 | { \ | |
1892 | address->m_error = GSOCK_INVADDR; \ | |
1893 | return retval; \ | |
1894 | } \ | |
1895 | } | |
1896 | ||
1897 | ||
1898 | GAddress *GAddress_new(void) | |
1899 | { | |
1900 | GAddress *address; | |
1901 | ||
1902 | if ((address = (GAddress *) malloc(sizeof(GAddress))) == NULL) | |
1903 | return NULL; | |
1904 | ||
1905 | address->m_family = GSOCK_NOFAMILY; | |
1906 | address->m_addr = NULL; | |
1907 | address->m_len = 0; | |
1908 | ||
1909 | return address; | |
1910 | } | |
1911 | ||
1912 | GAddress *GAddress_copy(GAddress *address) | |
1913 | { | |
1914 | GAddress *addr2; | |
1915 | ||
1916 | assert(address != NULL); | |
1917 | ||
1918 | if ((addr2 = (GAddress *) malloc(sizeof(GAddress))) == NULL) | |
1919 | return NULL; | |
1920 | ||
1921 | memcpy(addr2, address, sizeof(GAddress)); | |
1922 | ||
1923 | if (address->m_addr && address->m_len > 0) | |
1924 | { | |
1925 | addr2->m_addr = (struct sockaddr *)malloc(addr2->m_len); | |
1926 | if (addr2->m_addr == NULL) | |
1927 | { | |
1928 | free(addr2); | |
1929 | return NULL; | |
1930 | } | |
1931 | memcpy(addr2->m_addr, address->m_addr, addr2->m_len); | |
1932 | } | |
1933 | ||
1934 | return addr2; | |
1935 | } | |
1936 | ||
1937 | void GAddress_destroy(GAddress *address) | |
1938 | { | |
1939 | assert(address != NULL); | |
1940 | ||
1941 | if (address->m_addr) | |
1942 | free(address->m_addr); | |
1943 | ||
1944 | free(address); | |
1945 | } | |
1946 | ||
1947 | void GAddress_SetFamily(GAddress *address, GAddressType type) | |
1948 | { | |
1949 | assert(address != NULL); | |
1950 | ||
1951 | address->m_family = type; | |
1952 | } | |
1953 | ||
1954 | GAddressType GAddress_GetFamily(GAddress *address) | |
1955 | { | |
1956 | assert(address != NULL); | |
1957 | ||
1958 | return address->m_family; | |
1959 | } | |
1960 | ||
1961 | GSocketError _GAddress_translate_from(GAddress *address, | |
1962 | struct sockaddr *addr, int len) | |
1963 | { | |
1964 | address->m_realfamily = addr->sa_family; | |
1965 | switch (addr->sa_family) | |
1966 | { | |
1967 | case AF_INET: | |
1968 | address->m_family = GSOCK_INET; | |
1969 | break; | |
1970 | case AF_UNIX: | |
1971 | address->m_family = GSOCK_UNIX; | |
1972 | break; | |
1973 | #ifdef AF_INET6 | |
1974 | case AF_INET6: | |
1975 | address->m_family = GSOCK_INET6; | |
1976 | break; | |
1977 | #endif | |
1978 | default: | |
1979 | { | |
1980 | address->m_error = GSOCK_INVOP; | |
1981 | return GSOCK_INVOP; | |
1982 | } | |
1983 | } | |
1984 | ||
1985 | if (address->m_addr) | |
1986 | free(address->m_addr); | |
1987 | ||
1988 | address->m_len = len; | |
1989 | address->m_addr = (struct sockaddr *)malloc(len); | |
1990 | ||
1991 | if (address->m_addr == NULL) | |
1992 | { | |
1993 | address->m_error = GSOCK_MEMERR; | |
1994 | return GSOCK_MEMERR; | |
1995 | } | |
1aa7b427 | 1996 | |
97e6ee04 DE |
1997 | memcpy(address->m_addr, addr, len); |
1998 | ||
1999 | return GSOCK_NOERROR; | |
2000 | } | |
2001 | ||
2002 | GSocketError _GAddress_translate_to(GAddress *address, | |
2003 | struct sockaddr **addr, int *len) | |
2004 | { | |
2005 | if (!address->m_addr) | |
2006 | { | |
2007 | address->m_error = GSOCK_INVADDR; | |
2008 | return GSOCK_INVADDR; | |
2009 | } | |
2010 | ||
2011 | *len = address->m_len; | |
2012 | *addr = (struct sockaddr *)malloc(address->m_len); | |
2013 | if (*addr == NULL) | |
2014 | { | |
2015 | address->m_error = GSOCK_MEMERR; | |
2016 | return GSOCK_MEMERR; | |
2017 | } | |
2018 | ||
2019 | memcpy(*addr, address->m_addr, address->m_len); | |
2020 | return GSOCK_NOERROR; | |
2021 | } | |
2022 | ||
2023 | /* | |
2024 | * ------------------------------------------------------------------------- | |
2025 | * Internet address family | |
2026 | * ------------------------------------------------------------------------- | |
2027 | */ | |
2028 | ||
2029 | GSocketError _GAddress_Init_INET(GAddress *address) | |
2030 | { | |
2031 | address->m_len = sizeof(struct sockaddr_in); | |
2032 | address->m_addr = (struct sockaddr *) malloc(address->m_len); | |
2033 | if (address->m_addr == NULL) | |
2034 | { | |
2035 | address->m_error = GSOCK_MEMERR; | |
2036 | return GSOCK_MEMERR; | |
2037 | } | |
2038 | ||
2039 | address->m_family = GSOCK_INET; | |
2040 | address->m_realfamily = PF_INET; | |
2041 | ((struct sockaddr_in *)address->m_addr)->sin_family = AF_INET; | |
2042 | ((struct sockaddr_in *)address->m_addr)->sin_addr.s_addr = INADDR_ANY; | |
2043 | ||
2044 | return GSOCK_NOERROR; | |
2045 | } | |
2046 | ||
2047 | GSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname) | |
2048 | { | |
2049 | struct hostent *he; | |
2050 | struct in_addr *addr; | |
2051 | ||
2052 | assert(address != NULL); | |
2053 | ||
2054 | CHECK_ADDRESS(address, INET); | |
2055 | ||
2056 | addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr); | |
2057 | ||
2058 | /* If it is a numeric host name, convert it now */ | |
2059 | #if defined(HAVE_INET_ATON) | |
2060 | if (inet_aton(hostname, addr) == 0) | |
2061 | { | |
2062 | #elif defined(HAVE_INET_ADDR) | |
8c0f8906 | 2063 | if ( (addr->s_addr = inet_addr(hostname)) == (unsigned)-1 ) |
97e6ee04 DE |
2064 | { |
2065 | #else | |
2066 | /* Use gethostbyname by default */ | |
9d715960 | 2067 | #ifndef __WXMAC__ |
ba2a81d7 | 2068 | int val = 1; /* VA doesn't like constants in conditional expressions */ |
97e6ee04 | 2069 | if (val) |
9d715960 | 2070 | #endif |
97e6ee04 DE |
2071 | { |
2072 | #endif | |
2073 | struct in_addr *array_addr; | |
2074 | ||
2075 | /* It is a real name, we solve it */ | |
127189eb SN |
2076 | struct hostent h; |
2077 | #if defined(HAVE_FUNC_GETHOSTBYNAME_R_3) | |
2078 | struct hostent_data buffer; | |
2079 | #else | |
2080 | char buffer[1024]; | |
2081 | #endif | |
2082 | int err; | |
2083 | he = wxGethostbyname_r(hostname, &h, (void*)&buffer, sizeof(buffer), &err); | |
2084 | if (he == NULL) | |
97e6ee04 DE |
2085 | { |
2086 | /* Reset to invalid address */ | |
2087 | addr->s_addr = INADDR_NONE; | |
2088 | address->m_error = GSOCK_NOHOST; | |
2089 | return GSOCK_NOHOST; | |
2090 | } | |
1aa7b427 | 2091 | |
97e6ee04 DE |
2092 | array_addr = (struct in_addr *) *(he->h_addr_list); |
2093 | addr->s_addr = array_addr[0].s_addr; | |
2094 | } | |
1aa7b427 | 2095 | |
97e6ee04 DE |
2096 | return GSOCK_NOERROR; |
2097 | } | |
2098 | ||
60edcf45 VZ |
2099 | |
2100 | GSocketError GAddress_INET_SetBroadcastAddress(GAddress *address) | |
2101 | { | |
2102 | return GAddress_INET_SetHostAddress(address, INADDR_BROADCAST); | |
2103 | } | |
2104 | ||
97e6ee04 DE |
2105 | GSocketError GAddress_INET_SetAnyAddress(GAddress *address) |
2106 | { | |
2107 | return GAddress_INET_SetHostAddress(address, INADDR_ANY); | |
2108 | } | |
2109 | ||
2110 | GSocketError GAddress_INET_SetHostAddress(GAddress *address, | |
2111 | unsigned long hostaddr) | |
2112 | { | |
2113 | struct in_addr *addr; | |
2114 | ||
2115 | assert(address != NULL); | |
2116 | ||
2117 | CHECK_ADDRESS(address, INET); | |
2118 | ||
2119 | addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr); | |
9d715960 | 2120 | addr->s_addr = htonl(hostaddr); |
97e6ee04 DE |
2121 | |
2122 | return GSOCK_NOERROR; | |
2123 | } | |
2124 | ||
2125 | GSocketError GAddress_INET_SetPortName(GAddress *address, const char *port, | |
2126 | const char *protocol) | |
2127 | { | |
2128 | struct servent *se; | |
2129 | struct sockaddr_in *addr; | |
2130 | ||
2131 | assert(address != NULL); | |
2132 | CHECK_ADDRESS(address, INET); | |
2133 | ||
2134 | if (!port) | |
2135 | { | |
2136 | address->m_error = GSOCK_INVPORT; | |
2137 | return GSOCK_INVPORT; | |
2138 | } | |
b082b524 | 2139 | |
127189eb SN |
2140 | #if defined(HAVE_FUNC_GETSERVBYNAME_R_4) |
2141 | struct servent_data buffer; | |
f6bc1c74 | 2142 | #else |
127189eb | 2143 | char buffer[1024]; |
f6bc1c74 | 2144 | #endif |
127189eb SN |
2145 | struct servent serv; |
2146 | se = wxGetservbyname_r(port, protocol, &serv, | |
2147 | (void*)&buffer, sizeof(buffer)); | |
97e6ee04 DE |
2148 | if (!se) |
2149 | { | |
2150 | /* the cast to int suppresses compiler warnings about subscript having the | |
2151 | type char */ | |
2152 | if (isdigit((int)port[0])) | |
2153 | { | |
2154 | int port_int; | |
2155 | ||
2156 | port_int = atoi(port); | |
2157 | addr = (struct sockaddr_in *)address->m_addr; | |
2158 | addr->sin_port = htons(port_int); | |
2159 | return GSOCK_NOERROR; | |
2160 | } | |
2161 | ||
2162 | address->m_error = GSOCK_INVPORT; | |
2163 | return GSOCK_INVPORT; | |
2164 | } | |
2165 | ||
2166 | addr = (struct sockaddr_in *)address->m_addr; | |
2167 | addr->sin_port = se->s_port; | |
2168 | ||
2169 | return GSOCK_NOERROR; | |
2170 | } | |
2171 | ||
2172 | GSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port) | |
2173 | { | |
2174 | struct sockaddr_in *addr; | |
2175 | ||
2176 | assert(address != NULL); | |
2177 | CHECK_ADDRESS(address, INET); | |
b082b524 | 2178 | |
97e6ee04 DE |
2179 | addr = (struct sockaddr_in *)address->m_addr; |
2180 | addr->sin_port = htons(port); | |
2181 | ||
2182 | return GSOCK_NOERROR; | |
2183 | } | |
2184 | ||
2185 | GSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname, size_t sbuf) | |
2186 | { | |
2187 | struct hostent *he; | |
2188 | char *addr_buf; | |
2189 | struct sockaddr_in *addr; | |
2190 | ||
b082b524 | 2191 | assert(address != NULL); |
97e6ee04 DE |
2192 | CHECK_ADDRESS(address, INET); |
2193 | ||
2194 | addr = (struct sockaddr_in *)address->m_addr; | |
2195 | addr_buf = (char *)&(addr->sin_addr); | |
2196 | ||
127189eb SN |
2197 | struct hostent temphost; |
2198 | #if defined(HAVE_FUNC_GETHOSTBYNAME_R_3) | |
2199 | struct hostent_data buffer; | |
2200 | #else | |
2201 | char buffer[1024]; | |
2202 | #endif | |
2203 | int err; | |
2204 | he = wxGethostbyaddr_r(addr_buf, sizeof(addr->sin_addr), AF_INET, &temphost, | |
2205 | (void*)&buffer, sizeof(buffer), &err); | |
97e6ee04 DE |
2206 | if (he == NULL) |
2207 | { | |
2208 | address->m_error = GSOCK_NOHOST; | |
2209 | return GSOCK_NOHOST; | |
2210 | } | |
2211 | ||
2212 | strncpy(hostname, he->h_name, sbuf); | |
2213 | ||
2214 | return GSOCK_NOERROR; | |
2215 | } | |
2216 | ||
2217 | unsigned long GAddress_INET_GetHostAddress(GAddress *address) | |
2218 | { | |
2219 | struct sockaddr_in *addr; | |
2220 | ||
b082b524 DE |
2221 | assert(address != NULL); |
2222 | CHECK_ADDRESS_RETVAL(address, INET, 0); | |
97e6ee04 DE |
2223 | |
2224 | addr = (struct sockaddr_in *)address->m_addr; | |
2225 | ||
9d715960 | 2226 | return ntohl(addr->sin_addr.s_addr); |
97e6ee04 DE |
2227 | } |
2228 | ||
2229 | unsigned short GAddress_INET_GetPort(GAddress *address) | |
2230 | { | |
2231 | struct sockaddr_in *addr; | |
2232 | ||
b082b524 DE |
2233 | assert(address != NULL); |
2234 | CHECK_ADDRESS_RETVAL(address, INET, 0); | |
97e6ee04 DE |
2235 | |
2236 | addr = (struct sockaddr_in *)address->m_addr; | |
2237 | return ntohs(addr->sin_port); | |
2238 | } | |
2239 | ||
2240 | /* | |
2241 | * ------------------------------------------------------------------------- | |
2242 | * Unix address family | |
2243 | * ------------------------------------------------------------------------- | |
2244 | */ | |
2245 | ||
2246 | #ifndef __VISAGECPP__ | |
2247 | GSocketError _GAddress_Init_UNIX(GAddress *address) | |
2248 | { | |
2249 | address->m_len = sizeof(struct sockaddr_un); | |
2250 | address->m_addr = (struct sockaddr *)malloc(address->m_len); | |
2251 | if (address->m_addr == NULL) | |
2252 | { | |
2253 | address->m_error = GSOCK_MEMERR; | |
2254 | return GSOCK_MEMERR; | |
2255 | } | |
2256 | ||
2257 | address->m_family = GSOCK_UNIX; | |
2258 | address->m_realfamily = PF_UNIX; | |
2259 | ((struct sockaddr_un *)address->m_addr)->sun_family = AF_UNIX; | |
2260 | ((struct sockaddr_un *)address->m_addr)->sun_path[0] = 0; | |
2261 | ||
2262 | return GSOCK_NOERROR; | |
2263 | } | |
2264 | ||
2265 | #define UNIX_SOCK_PATHLEN (sizeof(addr->sun_path)/sizeof(addr->sun_path[0])) | |
2266 | ||
2267 | GSocketError GAddress_UNIX_SetPath(GAddress *address, const char *path) | |
2268 | { | |
2269 | struct sockaddr_un *addr; | |
2270 | ||
b082b524 | 2271 | assert(address != NULL); |
97e6ee04 | 2272 | |
b082b524 | 2273 | CHECK_ADDRESS(address, UNIX); |
97e6ee04 DE |
2274 | |
2275 | addr = ((struct sockaddr_un *)address->m_addr); | |
2276 | strncpy(addr->sun_path, path, UNIX_SOCK_PATHLEN); | |
2277 | addr->sun_path[UNIX_SOCK_PATHLEN - 1] = '\0'; | |
2278 | ||
2279 | return GSOCK_NOERROR; | |
2280 | } | |
2281 | ||
2282 | GSocketError GAddress_UNIX_GetPath(GAddress *address, char *path, size_t sbuf) | |
2283 | { | |
2284 | struct sockaddr_un *addr; | |
2285 | ||
2286 | assert(address != NULL); | |
2287 | CHECK_ADDRESS(address, UNIX); | |
2288 | ||
2289 | addr = (struct sockaddr_un *)address->m_addr; | |
2290 | ||
2291 | strncpy(path, addr->sun_path, sbuf); | |
2292 | ||
2293 | return GSOCK_NOERROR; | |
2294 | } | |
2295 | #endif /* !defined(__VISAGECPP__) */ | |
2296 | #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */ |