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