]>
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 | ||
ba2a81d7 DE |
15 | /* |
16 | * PLEASE don't put C++ comments here - this is a C source file. | |
17 | */ | |
18 | ||
7e1e6965 WS |
19 | #if defined(__WATCOMC__) |
20 | #include "wx/wxprec.h" | |
21 | #include <errno.h> | |
22 | #include <nerrno.h> | |
23 | #endif | |
24 | ||
ba2a81d7 | 25 | #ifndef __GSOCKET_STANDALONE__ |
0beb8735 | 26 | #include "wx/defs.h" |
ba2a81d7 | 27 | #endif |
97e6ee04 DE |
28 | |
29 | #if defined(__VISAGECPP__) | |
97e6ee04 DE |
30 | #define BSD_SELECT /* use Berkley Sockets select */ |
31 | #endif | |
32 | ||
33 | #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) | |
34 | ||
35 | #include <assert.h> | |
36 | #include <sys/types.h> | |
37 | #ifdef __VISAGECPP__ | |
38 | #include <string.h> | |
39 | #include <sys/time.h> | |
40 | #include <types.h> | |
41 | #include <netinet/in.h> | |
42 | #endif | |
43 | #include <netdb.h> | |
44 | #include <sys/ioctl.h> | |
45 | ||
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" | |
186 | # include "wx/gsocket.h" | |
187 | #else | |
188 | # include "gsockunx.h" | |
189 | # include "gsocket.h" | |
5fb9aa56 WS |
190 | # ifndef WXUNUSED |
191 | # define WXUNUSED(x) | |
192 | # endif | |
97e6ee04 DE |
193 | #endif /* __GSOCKET_STANDALONE__ */ |
194 | ||
195 | /* debugging helpers */ | |
196 | #ifdef __GSOCKET_DEBUG__ | |
197 | # define GSocket_Debug(args) printf args | |
198 | #else | |
199 | # define GSocket_Debug(args) | |
200 | #endif /* __GSOCKET_DEBUG__ */ | |
201 | ||
ba2a81d7 DE |
202 | /* Table of GUI-related functions. We must call them indirectly because |
203 | * of wxBase and GUI separation: */ | |
444cb1fd | 204 | |
3e1400ac | 205 | static GSocketGUIFunctionsTable *gs_gui_functions; |
b082b524 | 206 | |
ba2a81d7 | 207 | class GSocketGUIFunctionsTableNull: public GSocketGUIFunctionsTable |
444cb1fd | 208 | { |
ba2a81d7 DE |
209 | public: |
210 | virtual bool OnInit(); | |
211 | virtual void OnExit(); | |
212 | virtual bool CanUseEventLoop(); | |
213 | virtual bool Init_Socket(GSocket *socket); | |
214 | virtual void Destroy_Socket(GSocket *socket); | |
215 | virtual void Install_Callback(GSocket *socket, GSocketEvent event); | |
ba2a81d7 DE |
216 | virtual void Uninstall_Callback(GSocket *socket, GSocketEvent event); |
217 | virtual void Enable_Events(GSocket *socket); | |
218 | virtual void Disable_Events(GSocket *socket); | |
219 | }; | |
444cb1fd | 220 | |
ba2a81d7 DE |
221 | bool GSocketGUIFunctionsTableNull::OnInit() |
222 | { return true; } | |
223 | void GSocketGUIFunctionsTableNull::OnExit() | |
224 | {} | |
225 | bool GSocketGUIFunctionsTableNull::CanUseEventLoop() | |
226 | { return false; } | |
c40158e4 | 227 | bool GSocketGUIFunctionsTableNull::Init_Socket(GSocket *WXUNUSED(socket)) |
ba2a81d7 | 228 | { return true; } |
c40158e4 | 229 | void GSocketGUIFunctionsTableNull::Destroy_Socket(GSocket *WXUNUSED(socket)) |
ba2a81d7 | 230 | {} |
c40158e4 | 231 | void GSocketGUIFunctionsTableNull::Install_Callback(GSocket *WXUNUSED(socket), GSocketEvent WXUNUSED(event)) |
ba2a81d7 | 232 | {} |
c40158e4 | 233 | void GSocketGUIFunctionsTableNull::Uninstall_Callback(GSocket *WXUNUSED(socket), GSocketEvent WXUNUSED(event)) |
ba2a81d7 | 234 | {} |
c40158e4 | 235 | void GSocketGUIFunctionsTableNull::Enable_Events(GSocket *WXUNUSED(socket)) |
ba2a81d7 | 236 | {} |
c40158e4 | 237 | void GSocketGUIFunctionsTableNull::Disable_Events(GSocket *WXUNUSED(socket)) |
ba2a81d7 DE |
238 | {} |
239 | /* Global initialisers */ | |
444cb1fd | 240 | |
3e1400ac | 241 | void GSocket_SetGUIFunctions(GSocketGUIFunctionsTable *guifunc) |
444cb1fd | 242 | { |
ba2a81d7 | 243 | gs_gui_functions = guifunc; |
444cb1fd DE |
244 | } |
245 | ||
97e6ee04 DE |
246 | int GSocket_Init(void) |
247 | { | |
ba2a81d7 DE |
248 | if (!gs_gui_functions) |
249 | { | |
3e1400ac | 250 | static GSocketGUIFunctionsTableNull table; |
ba2a81d7 DE |
251 | gs_gui_functions = &table; |
252 | } | |
253 | if ( !gs_gui_functions->OnInit() ) | |
254 | return 0; | |
255 | return 1; | |
97e6ee04 DE |
256 | } |
257 | ||
258 | void GSocket_Cleanup(void) | |
259 | { | |
ba2a81d7 DE |
260 | if (gs_gui_functions) |
261 | { | |
262 | gs_gui_functions->OnExit(); | |
263 | } | |
97e6ee04 DE |
264 | } |
265 | ||
ba2a81d7 DE |
266 | /* Constructors / Destructors for GSocket */ |
267 | ||
268 | GSocket::GSocket() | |
97e6ee04 | 269 | { |
09e6e5ec | 270 | int i; |
97e6ee04 | 271 | |
09e6e5ec | 272 | m_fd = INVALID_SOCKET; |
97e6ee04 DE |
273 | for (i=0;i<GSOCK_MAX_EVENT;i++) |
274 | { | |
09e6e5ec | 275 | m_cbacks[i] = NULL; |
97e6ee04 | 276 | } |
09e6e5ec DE |
277 | m_detected = 0; |
278 | m_local = NULL; | |
279 | m_peer = NULL; | |
280 | m_error = GSOCK_NOERROR; | |
948c96ef DE |
281 | m_server = false; |
282 | m_stream = true; | |
09e6e5ec | 283 | m_gui_dependent = NULL; |
948c96ef | 284 | m_non_blocking = false; |
1eef64d8 | 285 | m_reusable = false; |
09e6e5ec | 286 | m_timeout = 10*60*1000; |
97e6ee04 | 287 | /* 10 minutes * 60 sec * 1000 millisec */ |
948c96ef | 288 | m_establishing = false; |
97e6ee04 | 289 | |
ba2a81d7 DE |
290 | assert(gs_gui_functions); |
291 | /* Per-socket GUI-specific initialization */ | |
292 | m_ok = gs_gui_functions->Init_Socket(this); | |
97e6ee04 DE |
293 | } |
294 | ||
ba2a81d7 | 295 | void GSocket::Close() |
97e6ee04 | 296 | { |
ba2a81d7 DE |
297 | gs_gui_functions->Disable_Events(this); |
298 | /* gsockosx.c calls CFSocketInvalidate which closes the socket for us */ | |
97e6ee04 | 299 | #if !(defined(__DARWIN__) && (defined(__WXMAC__) || defined(__WXCOCOA__))) |
09e6e5ec | 300 | close(m_fd); |
97e6ee04 | 301 | #endif |
09e6e5ec | 302 | m_fd = INVALID_SOCKET; |
97e6ee04 DE |
303 | } |
304 | ||
ba2a81d7 | 305 | GSocket::~GSocket() |
97e6ee04 | 306 | { |
ba2a81d7 DE |
307 | assert(this); |
308 | ||
309 | /* Check that the socket is really shutdowned */ | |
310 | if (m_fd != INVALID_SOCKET) | |
311 | Shutdown(); | |
312 | ||
313 | /* Per-socket GUI-specific cleanup */ | |
314 | gs_gui_functions->Destroy_Socket(this); | |
315 | ||
97e6ee04 | 316 | /* Destroy private addresses */ |
09e6e5ec DE |
317 | if (m_local) |
318 | GAddress_destroy(m_local); | |
97e6ee04 | 319 | |
09e6e5ec DE |
320 | if (m_peer) |
321 | GAddress_destroy(m_peer); | |
97e6ee04 DE |
322 | } |
323 | ||
324 | /* GSocket_Shutdown: | |
325 | * Disallow further read/write operations on this socket, close | |
326 | * the fd and disable all callbacks. | |
327 | */ | |
ba2a81d7 | 328 | void GSocket::Shutdown() |
97e6ee04 DE |
329 | { |
330 | int evt; | |
331 | ||
09e6e5ec | 332 | assert(this); |
97e6ee04 DE |
333 | |
334 | /* If socket has been created, shutdown it */ | |
09e6e5ec | 335 | if (m_fd != INVALID_SOCKET) |
97e6ee04 | 336 | { |
09e6e5ec DE |
337 | shutdown(m_fd, 2); |
338 | Close(); | |
97e6ee04 DE |
339 | } |
340 | ||
341 | /* Disable GUI callbacks */ | |
342 | for (evt = 0; evt < GSOCK_MAX_EVENT; evt++) | |
09e6e5ec | 343 | m_cbacks[evt] = NULL; |
97e6ee04 | 344 | |
09e6e5ec | 345 | m_detected = GSOCK_LOST_FLAG; |
97e6ee04 DE |
346 | } |
347 | ||
348 | /* Address handling */ | |
349 | ||
350 | /* GSocket_SetLocal: | |
351 | * GSocket_GetLocal: | |
352 | * GSocket_SetPeer: | |
353 | * GSocket_GetPeer: | |
354 | * Set or get the local or peer address for this socket. The 'set' | |
355 | * functions return GSOCK_NOERROR on success, an error code otherwise. | |
356 | * The 'get' functions return a pointer to a GAddress object on success, | |
357 | * or NULL otherwise, in which case they set the error code of the | |
358 | * corresponding GSocket. | |
359 | * | |
360 | * Error codes: | |
361 | * GSOCK_INVSOCK - the socket is not valid. | |
362 | * GSOCK_INVADDR - the address is not valid. | |
363 | */ | |
ba2a81d7 | 364 | GSocketError GSocket::SetLocal(GAddress *address) |
97e6ee04 | 365 | { |
09e6e5ec | 366 | assert(this); |
97e6ee04 DE |
367 | |
368 | /* the socket must be initialized, or it must be a server */ | |
09e6e5ec | 369 | if ((m_fd != INVALID_SOCKET && !m_server)) |
97e6ee04 | 370 | { |
09e6e5ec | 371 | m_error = GSOCK_INVSOCK; |
97e6ee04 DE |
372 | return GSOCK_INVSOCK; |
373 | } | |
374 | ||
375 | /* check address */ | |
376 | if (address == NULL || address->m_family == GSOCK_NOFAMILY) | |
377 | { | |
09e6e5ec | 378 | m_error = GSOCK_INVADDR; |
97e6ee04 DE |
379 | return GSOCK_INVADDR; |
380 | } | |
381 | ||
09e6e5ec DE |
382 | if (m_local) |
383 | GAddress_destroy(m_local); | |
97e6ee04 | 384 | |
09e6e5ec | 385 | m_local = GAddress_copy(address); |
97e6ee04 DE |
386 | |
387 | return GSOCK_NOERROR; | |
388 | } | |
389 | ||
ba2a81d7 | 390 | GSocketError GSocket::SetPeer(GAddress *address) |
97e6ee04 | 391 | { |
09e6e5ec | 392 | assert(this); |
97e6ee04 DE |
393 | |
394 | /* check address */ | |
395 | if (address == NULL || address->m_family == GSOCK_NOFAMILY) | |
396 | { | |
09e6e5ec | 397 | m_error = GSOCK_INVADDR; |
97e6ee04 DE |
398 | return GSOCK_INVADDR; |
399 | } | |
400 | ||
09e6e5ec DE |
401 | if (m_peer) |
402 | GAddress_destroy(m_peer); | |
97e6ee04 | 403 | |
09e6e5ec | 404 | m_peer = GAddress_copy(address); |
97e6ee04 DE |
405 | |
406 | return GSOCK_NOERROR; | |
407 | } | |
408 | ||
ba2a81d7 | 409 | GAddress *GSocket::GetLocal() |
97e6ee04 DE |
410 | { |
411 | GAddress *address; | |
412 | struct sockaddr addr; | |
9e03e02d | 413 | WX_SOCKLEN_T size = sizeof(addr); |
97e6ee04 DE |
414 | GSocketError err; |
415 | ||
09e6e5ec | 416 | assert(this); |
97e6ee04 DE |
417 | |
418 | /* try to get it from the m_local var first */ | |
09e6e5ec DE |
419 | if (m_local) |
420 | return GAddress_copy(m_local); | |
97e6ee04 DE |
421 | |
422 | /* else, if the socket is initialized, try getsockname */ | |
09e6e5ec | 423 | if (m_fd == INVALID_SOCKET) |
97e6ee04 | 424 | { |
09e6e5ec | 425 | m_error = GSOCK_INVSOCK; |
97e6ee04 DE |
426 | return NULL; |
427 | } | |
428 | ||
9e03e02d | 429 | if (getsockname(m_fd, &addr, (WX_SOCKLEN_T *) &size) < 0) |
97e6ee04 | 430 | { |
09e6e5ec | 431 | m_error = GSOCK_IOERR; |
97e6ee04 DE |
432 | return NULL; |
433 | } | |
434 | ||
435 | /* got a valid address from getsockname, create a GAddress object */ | |
436 | address = GAddress_new(); | |
437 | if (address == NULL) | |
438 | { | |
09e6e5ec | 439 | m_error = GSOCK_MEMERR; |
97e6ee04 DE |
440 | return NULL; |
441 | } | |
442 | ||
443 | err = _GAddress_translate_from(address, &addr, size); | |
444 | if (err != GSOCK_NOERROR) | |
445 | { | |
446 | GAddress_destroy(address); | |
09e6e5ec | 447 | m_error = err; |
97e6ee04 DE |
448 | return NULL; |
449 | } | |
450 | ||
451 | return address; | |
452 | } | |
453 | ||
ba2a81d7 | 454 | GAddress *GSocket::GetPeer() |
97e6ee04 | 455 | { |
09e6e5ec | 456 | assert(this); |
97e6ee04 DE |
457 | |
458 | /* try to get it from the m_peer var */ | |
09e6e5ec DE |
459 | if (m_peer) |
460 | return GAddress_copy(m_peer); | |
97e6ee04 DE |
461 | |
462 | return NULL; | |
463 | } | |
464 | ||
465 | /* Server specific parts */ | |
466 | ||
467 | /* GSocket_SetServer: | |
468 | * Sets up this socket as a server. The local address must have been | |
469 | * set with GSocket_SetLocal() before GSocket_SetServer() is called. | |
470 | * Returns GSOCK_NOERROR on success, one of the following otherwise: | |
b082b524 | 471 | * |
97e6ee04 DE |
472 | * Error codes: |
473 | * GSOCK_INVSOCK - the socket is in use. | |
474 | * GSOCK_INVADDR - the local address has not been set. | |
b082b524 | 475 | * GSOCK_IOERR - low-level error. |
97e6ee04 | 476 | */ |
ba2a81d7 | 477 | GSocketError GSocket::SetServer() |
97e6ee04 DE |
478 | { |
479 | int arg = 1; | |
480 | ||
09e6e5ec | 481 | assert(this); |
97e6ee04 DE |
482 | |
483 | /* must not be in use */ | |
09e6e5ec | 484 | if (m_fd != INVALID_SOCKET) |
97e6ee04 | 485 | { |
09e6e5ec | 486 | m_error = GSOCK_INVSOCK; |
97e6ee04 DE |
487 | return GSOCK_INVSOCK; |
488 | } | |
489 | ||
490 | /* the local addr must have been set */ | |
09e6e5ec | 491 | if (!m_local) |
97e6ee04 | 492 | { |
09e6e5ec | 493 | m_error = GSOCK_INVADDR; |
97e6ee04 DE |
494 | return GSOCK_INVADDR; |
495 | } | |
496 | ||
497 | /* Initialize all fields */ | |
948c96ef DE |
498 | m_stream = true; |
499 | m_server = true; | |
97e6ee04 DE |
500 | |
501 | /* Create the socket */ | |
09e6e5ec | 502 | m_fd = socket(m_local->m_realfamily, SOCK_STREAM, 0); |
97e6ee04 | 503 | |
09e6e5ec | 504 | if (m_fd == INVALID_SOCKET) |
97e6ee04 | 505 | { |
09e6e5ec | 506 | m_error = GSOCK_IOERR; |
97e6ee04 DE |
507 | return GSOCK_IOERR; |
508 | } | |
bb154f79 KH |
509 | |
510 | /* FreeBSD variants can't use MSG_NOSIGNAL, and instead use a socket option */ | |
511 | #ifdef SO_NOSIGPIPE | |
512 | setsockopt(m_fd, SOL_SOCKET, SO_NOSIGPIPE, (const char*)&arg, sizeof(u_long)); | |
513 | #endif | |
514 | ||
09e6e5ec | 515 | ioctl(m_fd, FIONBIO, &arg); |
ba2a81d7 | 516 | gs_gui_functions->Enable_Events(this); |
97e6ee04 DE |
517 | |
518 | /* allow a socket to re-bind if the socket is in the TIME_WAIT | |
519 | state after being previously closed. | |
520 | */ | |
ba2a81d7 | 521 | if (m_reusable) |
b082b524 | 522 | setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&arg, sizeof(u_long)); |
97e6ee04 DE |
523 | |
524 | /* Bind to the local address, | |
525 | * retrieve the actual address bound, | |
526 | * and listen up to 5 connections. | |
527 | */ | |
09e6e5ec DE |
528 | if ((bind(m_fd, m_local->m_addr, m_local->m_len) != 0) || |
529 | (getsockname(m_fd, | |
530 | m_local->m_addr, | |
9e03e02d | 531 | (WX_SOCKLEN_T *) &m_local->m_len) != 0) || |
09e6e5ec | 532 | (listen(m_fd, 5) != 0)) |
97e6ee04 | 533 | { |
09e6e5ec DE |
534 | Close(); |
535 | m_error = GSOCK_IOERR; | |
97e6ee04 DE |
536 | return GSOCK_IOERR; |
537 | } | |
538 | ||
539 | return GSOCK_NOERROR; | |
540 | } | |
541 | ||
542 | /* GSocket_WaitConnection: | |
543 | * Waits for an incoming client connection. Returns a pointer to | |
544 | * a GSocket object, or NULL if there was an error, in which case | |
545 | * the last error field will be updated for the calling GSocket. | |
546 | * | |
547 | * Error codes (set in the calling GSocket) | |
548 | * GSOCK_INVSOCK - the socket is not valid or not a server. | |
549 | * GSOCK_TIMEDOUT - timeout, no incoming connections. | |
550 | * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking. | |
551 | * GSOCK_MEMERR - couldn't allocate memory. | |
b082b524 | 552 | * GSOCK_IOERR - low-level error. |
97e6ee04 | 553 | */ |
ba2a81d7 | 554 | GSocket *GSocket::WaitConnection() |
97e6ee04 DE |
555 | { |
556 | struct sockaddr from; | |
9e03e02d | 557 | WX_SOCKLEN_T fromlen = sizeof(from); |
97e6ee04 DE |
558 | GSocket *connection; |
559 | GSocketError err; | |
560 | int arg = 1; | |
561 | ||
09e6e5ec | 562 | assert(this); |
97e6ee04 | 563 | |
97e6ee04 | 564 | /* If the socket has already been created, we exit immediately */ |
09e6e5ec | 565 | if (m_fd == INVALID_SOCKET || !m_server) |
97e6ee04 | 566 | { |
09e6e5ec | 567 | m_error = GSOCK_INVSOCK; |
97e6ee04 DE |
568 | return NULL; |
569 | } | |
570 | ||
571 | /* Create a GSocket object for the new connection */ | |
444cb1fd | 572 | connection = GSocket_new(); |
97e6ee04 | 573 | |
444cb1fd | 574 | if (!connection) |
97e6ee04 | 575 | { |
09e6e5ec | 576 | m_error = GSOCK_MEMERR; |
97e6ee04 DE |
577 | return NULL; |
578 | } | |
579 | ||
580 | /* Wait for a connection (with timeout) */ | |
09e6e5ec | 581 | if (Input_Timeout() == GSOCK_TIMEDOUT) |
97e6ee04 | 582 | { |
85431efa | 583 | delete connection; |
09e6e5ec | 584 | /* m_error set by _GSocket_Input_Timeout */ |
97e6ee04 DE |
585 | return NULL; |
586 | } | |
587 | ||
9e03e02d | 588 | connection->m_fd = accept(m_fd, &from, (WX_SOCKLEN_T *) &fromlen); |
97e6ee04 | 589 | |
bb154f79 KH |
590 | /* Reenable CONNECTION events */ |
591 | Enable(GSOCK_CONNECTION); | |
bb154f79 | 592 | |
97e6ee04 DE |
593 | if (connection->m_fd == INVALID_SOCKET) |
594 | { | |
595 | if (errno == EWOULDBLOCK) | |
09e6e5ec | 596 | m_error = GSOCK_WOULDBLOCK; |
97e6ee04 | 597 | else |
09e6e5ec | 598 | m_error = GSOCK_IOERR; |
97e6ee04 | 599 | |
85431efa | 600 | delete connection; |
97e6ee04 DE |
601 | return NULL; |
602 | } | |
603 | ||
604 | /* Initialize all fields */ | |
948c96ef DE |
605 | connection->m_server = false; |
606 | connection->m_stream = true; | |
97e6ee04 DE |
607 | |
608 | /* Setup the peer address field */ | |
609 | connection->m_peer = GAddress_new(); | |
610 | if (!connection->m_peer) | |
611 | { | |
85431efa | 612 | delete connection; |
09e6e5ec | 613 | m_error = GSOCK_MEMERR; |
97e6ee04 DE |
614 | return NULL; |
615 | } | |
616 | err = _GAddress_translate_from(connection->m_peer, &from, fromlen); | |
617 | if (err != GSOCK_NOERROR) | |
618 | { | |
85431efa | 619 | delete connection; |
09e6e5ec | 620 | m_error = err; |
97e6ee04 DE |
621 | return NULL; |
622 | } | |
623 | #if defined(__EMX__) || defined(__VISAGECPP__) | |
624 | ioctl(connection->m_fd, FIONBIO, (char*)&arg, sizeof(arg)); | |
625 | #else | |
626 | ioctl(connection->m_fd, FIONBIO, &arg); | |
627 | #endif | |
ba2a81d7 | 628 | gs_gui_functions->Enable_Events(connection); |
97e6ee04 DE |
629 | |
630 | return connection; | |
631 | } | |
632 | ||
948c96ef | 633 | bool GSocket::SetReusable() |
b082b524 DE |
634 | { |
635 | /* socket must not be null, and must not be in use/already bound */ | |
ba2a81d7 | 636 | if (this && m_fd == INVALID_SOCKET) { |
948c96ef DE |
637 | m_reusable = true; |
638 | return true; | |
b082b524 | 639 | } |
948c96ef | 640 | return false; |
b082b524 DE |
641 | } |
642 | ||
97e6ee04 DE |
643 | /* Client specific parts */ |
644 | ||
645 | /* GSocket_Connect: | |
646 | * For stream (connection oriented) sockets, GSocket_Connect() tries | |
647 | * to establish a client connection to a server using the peer address | |
648 | * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the | |
d6922577 | 649 | * connection has been successfully established, or one of the error |
97e6ee04 DE |
650 | * codes listed below. Note that for nonblocking sockets, a return |
651 | * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection | |
652 | * request can be completed later; you should use GSocket_Select() | |
653 | * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the | |
654 | * corresponding asynchronous events. | |
655 | * | |
656 | * For datagram (non connection oriented) sockets, GSocket_Connect() | |
657 | * just sets the peer address established with GSocket_SetPeer() as | |
658 | * default destination. | |
659 | * | |
660 | * Error codes: | |
661 | * GSOCK_INVSOCK - the socket is in use or not valid. | |
662 | * GSOCK_INVADDR - the peer address has not been established. | |
663 | * GSOCK_TIMEDOUT - timeout, the connection failed. | |
664 | * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only) | |
665 | * GSOCK_MEMERR - couldn't allocate memory. | |
b082b524 | 666 | * GSOCK_IOERR - low-level error. |
97e6ee04 | 667 | */ |
ba2a81d7 | 668 | GSocketError GSocket::Connect(GSocketStream stream) |
97e6ee04 DE |
669 | { |
670 | int err, ret; | |
671 | int arg = 1; | |
672 | ||
09e6e5ec | 673 | assert(this); |
97e6ee04 DE |
674 | |
675 | /* Enable CONNECTION events (needed for nonblocking connections) */ | |
09e6e5ec | 676 | Enable(GSOCK_CONNECTION); |
97e6ee04 | 677 | |
09e6e5ec | 678 | if (m_fd != INVALID_SOCKET) |
97e6ee04 | 679 | { |
09e6e5ec | 680 | m_error = GSOCK_INVSOCK; |
97e6ee04 DE |
681 | return GSOCK_INVSOCK; |
682 | } | |
683 | ||
09e6e5ec | 684 | if (!m_peer) |
97e6ee04 | 685 | { |
09e6e5ec | 686 | m_error = GSOCK_INVADDR; |
97e6ee04 DE |
687 | return GSOCK_INVADDR; |
688 | } | |
689 | ||
690 | /* Streamed or dgram socket? */ | |
09e6e5ec | 691 | m_stream = (stream == GSOCK_STREAMED); |
948c96ef DE |
692 | m_server = false; |
693 | m_establishing = false; | |
97e6ee04 DE |
694 | |
695 | /* Create the socket */ | |
09e6e5ec DE |
696 | m_fd = socket(m_peer->m_realfamily, |
697 | m_stream? SOCK_STREAM : SOCK_DGRAM, 0); | |
97e6ee04 | 698 | |
09e6e5ec | 699 | if (m_fd == INVALID_SOCKET) |
97e6ee04 | 700 | { |
09e6e5ec | 701 | m_error = GSOCK_IOERR; |
97e6ee04 DE |
702 | return GSOCK_IOERR; |
703 | } | |
bb154f79 KH |
704 | |
705 | /* FreeBSD variants can't use MSG_NOSIGNAL, and instead use a socket option */ | |
706 | #ifdef SO_NOSIGPIPE | |
707 | setsockopt(m_fd, SOL_SOCKET, SO_NOSIGPIPE, (const char*)&arg, sizeof(u_long)); | |
708 | #endif | |
709 | ||
97e6ee04 | 710 | #if defined(__EMX__) || defined(__VISAGECPP__) |
09e6e5ec | 711 | ioctl(m_fd, FIONBIO, (char*)&arg, sizeof(arg)); |
97e6ee04 | 712 | #else |
09e6e5ec | 713 | ioctl(m_fd, FIONBIO, &arg); |
97e6ee04 | 714 | #endif |
97e6ee04 DE |
715 | |
716 | /* Connect it to the peer address, with a timeout (see below) */ | |
09e6e5ec | 717 | ret = connect(m_fd, m_peer->m_addr, m_peer->m_len); |
97e6ee04 | 718 | |
0d34c30e KH |
719 | /* We only call Enable_Events if we know we aren't shutting down the socket. |
720 | * NB: Enable_Events needs to be called whether the socket is blocking or | |
721 | * non-blocking, it just shouldn't be called prior to knowing there is a | |
722 | * connection _if_ blocking sockets are being used. | |
723 | * If connect above returns 0, we are already connected and need to make the | |
724 | * call to Enable_Events now. | |
a3687636 JS |
725 | */ |
726 | ||
0d34c30e | 727 | if (m_non_blocking || ret == 0) |
bb154f79 KH |
728 | { |
729 | gs_gui_functions->Enable_Events(this); | |
730 | } | |
731 | ||
97e6ee04 DE |
732 | if (ret == -1) |
733 | { | |
734 | err = errno; | |
735 | ||
736 | /* If connect failed with EINPROGRESS and the GSocket object | |
737 | * is in blocking mode, we select() for the specified timeout | |
738 | * checking for writability to see if the connection request | |
739 | * completes. | |
740 | */ | |
09e6e5ec | 741 | if ((err == EINPROGRESS) && (!m_non_blocking)) |
97e6ee04 | 742 | { |
09e6e5ec | 743 | if (Output_Timeout() == GSOCK_TIMEDOUT) |
97e6ee04 | 744 | { |
09e6e5ec DE |
745 | Close(); |
746 | /* m_error is set in _GSocket_Output_Timeout */ | |
97e6ee04 DE |
747 | return GSOCK_TIMEDOUT; |
748 | } | |
749 | else | |
750 | { | |
751 | int error; | |
ddc1a35f | 752 | SOCKOPTLEN_T len = sizeof(error); |
97e6ee04 | 753 | |
f71bb8ef | 754 | getsockopt(m_fd, SOL_SOCKET, SO_ERROR, (char*) &error, &len); |
97e6ee04 | 755 | |
bb154f79 KH |
756 | gs_gui_functions->Enable_Events(this); |
757 | ||
97e6ee04 DE |
758 | if (!error) |
759 | return GSOCK_NOERROR; | |
760 | } | |
761 | } | |
762 | ||
763 | /* If connect failed with EINPROGRESS and the GSocket object | |
764 | * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK | |
765 | * (and return GSOCK_WOULDBLOCK) but we don't close the socket; | |
766 | * this way if the connection completes, a GSOCK_CONNECTION | |
767 | * event will be generated, if enabled. | |
768 | */ | |
09e6e5ec | 769 | if ((err == EINPROGRESS) && (m_non_blocking)) |
97e6ee04 | 770 | { |
948c96ef | 771 | m_establishing = true; |
09e6e5ec | 772 | m_error = GSOCK_WOULDBLOCK; |
97e6ee04 DE |
773 | return GSOCK_WOULDBLOCK; |
774 | } | |
775 | ||
776 | /* If connect failed with an error other than EINPROGRESS, | |
777 | * then the call to GSocket_Connect has failed. | |
778 | */ | |
09e6e5ec DE |
779 | Close(); |
780 | m_error = GSOCK_IOERR; | |
97e6ee04 DE |
781 | return GSOCK_IOERR; |
782 | } | |
783 | ||
784 | return GSOCK_NOERROR; | |
785 | } | |
786 | ||
787 | /* Datagram sockets */ | |
788 | ||
789 | /* GSocket_SetNonOriented: | |
790 | * Sets up this socket as a non-connection oriented (datagram) socket. | |
791 | * Before using this function, the local address must have been set | |
792 | * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR | |
793 | * on success, or one of the following otherwise. | |
794 | * | |
795 | * Error codes: | |
796 | * GSOCK_INVSOCK - the socket is in use. | |
797 | * GSOCK_INVADDR - the local address has not been set. | |
798 | * GSOCK_IOERR - low-level error. | |
799 | */ | |
ba2a81d7 | 800 | GSocketError GSocket::SetNonOriented() |
97e6ee04 DE |
801 | { |
802 | int arg = 1; | |
803 | ||
09e6e5ec | 804 | assert(this); |
97e6ee04 | 805 | |
09e6e5ec | 806 | if (m_fd != INVALID_SOCKET) |
97e6ee04 | 807 | { |
09e6e5ec | 808 | m_error = GSOCK_INVSOCK; |
97e6ee04 DE |
809 | return GSOCK_INVSOCK; |
810 | } | |
811 | ||
09e6e5ec | 812 | if (!m_local) |
97e6ee04 | 813 | { |
09e6e5ec | 814 | m_error = GSOCK_INVADDR; |
97e6ee04 DE |
815 | return GSOCK_INVADDR; |
816 | } | |
817 | ||
818 | /* Initialize all fields */ | |
948c96ef DE |
819 | m_stream = false; |
820 | m_server = false; | |
97e6ee04 DE |
821 | |
822 | /* Create the socket */ | |
09e6e5ec | 823 | m_fd = socket(m_local->m_realfamily, SOCK_DGRAM, 0); |
97e6ee04 | 824 | |
09e6e5ec | 825 | if (m_fd == INVALID_SOCKET) |
97e6ee04 | 826 | { |
09e6e5ec | 827 | m_error = GSOCK_IOERR; |
97e6ee04 DE |
828 | return GSOCK_IOERR; |
829 | } | |
830 | #if defined(__EMX__) || defined(__VISAGECPP__) | |
09e6e5ec | 831 | ioctl(m_fd, FIONBIO, (char*)&arg, sizeof(arg)); |
97e6ee04 | 832 | #else |
09e6e5ec | 833 | ioctl(m_fd, FIONBIO, &arg); |
97e6ee04 | 834 | #endif |
ba2a81d7 | 835 | gs_gui_functions->Enable_Events(this); |
97e6ee04 DE |
836 | |
837 | /* Bind to the local address, | |
838 | * and retrieve the actual address bound. | |
839 | */ | |
09e6e5ec DE |
840 | if ((bind(m_fd, m_local->m_addr, m_local->m_len) != 0) || |
841 | (getsockname(m_fd, | |
842 | m_local->m_addr, | |
9e03e02d | 843 | (WX_SOCKLEN_T *) &m_local->m_len) != 0)) |
97e6ee04 | 844 | { |
09e6e5ec DE |
845 | Close(); |
846 | m_error = GSOCK_IOERR; | |
97e6ee04 DE |
847 | return GSOCK_IOERR; |
848 | } | |
849 | ||
850 | return GSOCK_NOERROR; | |
851 | } | |
852 | ||
853 | /* Generic IO */ | |
854 | ||
855 | /* Like recv(), send(), ... */ | |
ba2a81d7 | 856 | int GSocket::Read(char *buffer, int size) |
97e6ee04 DE |
857 | { |
858 | int ret; | |
859 | ||
09e6e5ec | 860 | assert(this); |
97e6ee04 | 861 | |
09e6e5ec | 862 | if (m_fd == INVALID_SOCKET || m_server) |
97e6ee04 | 863 | { |
09e6e5ec | 864 | m_error = GSOCK_INVSOCK; |
97e6ee04 DE |
865 | return -1; |
866 | } | |
867 | ||
9d715960 DE |
868 | /* Disable events during query of socket status */ |
869 | Disable(GSOCK_INPUT); | |
b082b524 | 870 | |
97e6ee04 | 871 | /* If the socket is blocking, wait for data (with a timeout) */ |
09e6e5ec | 872 | if (Input_Timeout() == GSOCK_TIMEDOUT) |
9d715960 DE |
873 | /* We no longer return here immediately, otherwise socket events would not be re-enabled! */ |
874 | ret = -1; | |
875 | else { | |
876 | /* Read the data */ | |
877 | if (m_stream) | |
878 | ret = Recv_Stream(buffer, size); | |
879 | else | |
880 | ret = Recv_Dgram(buffer, size); | |
881 | } | |
b082b524 | 882 | |
97e6ee04 DE |
883 | if (ret == -1) |
884 | { | |
bb154f79 | 885 | if ((errno == EWOULDBLOCK) || (errno == EAGAIN)) |
09e6e5ec | 886 | m_error = GSOCK_WOULDBLOCK; |
97e6ee04 | 887 | else |
09e6e5ec | 888 | m_error = GSOCK_IOERR; |
97e6ee04 | 889 | } |
b082b524 | 890 | |
9d715960 | 891 | /* Enable events again now that we are done processing */ |
09e6e5ec | 892 | Enable(GSOCK_INPUT); |
97e6ee04 DE |
893 | |
894 | return ret; | |
895 | } | |
896 | ||
ba2a81d7 | 897 | int GSocket::Write(const char *buffer, int size) |
b082b524 | 898 | { |
97e6ee04 DE |
899 | int ret; |
900 | ||
09e6e5ec | 901 | assert(this); |
b082b524 | 902 | |
97e6ee04 DE |
903 | GSocket_Debug(( "GSocket_Write #1, size %d\n", size )); |
904 | ||
09e6e5ec | 905 | if (m_fd == INVALID_SOCKET || m_server) |
97e6ee04 | 906 | { |
09e6e5ec | 907 | m_error = GSOCK_INVSOCK; |
97e6ee04 DE |
908 | return -1; |
909 | } | |
910 | ||
911 | GSocket_Debug(( "GSocket_Write #2, size %d\n", size )); | |
912 | ||
913 | /* If the socket is blocking, wait for writability (with a timeout) */ | |
09e6e5ec | 914 | if (Output_Timeout() == GSOCK_TIMEDOUT) |
97e6ee04 DE |
915 | return -1; |
916 | ||
917 | GSocket_Debug(( "GSocket_Write #3, size %d\n", size )); | |
918 | ||
919 | /* Write the data */ | |
09e6e5ec DE |
920 | if (m_stream) |
921 | ret = Send_Stream(buffer, size); | |
97e6ee04 | 922 | else |
09e6e5ec | 923 | ret = Send_Dgram(buffer, size); |
b082b524 | 924 | |
97e6ee04 DE |
925 | GSocket_Debug(( "GSocket_Write #4, size %d\n", size )); |
926 | ||
927 | if (ret == -1) | |
928 | { | |
7e1e6965 | 929 | if ((errno == EWOULDBLOCK) || (errno == EAGAIN)) |
97e6ee04 | 930 | { |
09e6e5ec | 931 | m_error = GSOCK_WOULDBLOCK; |
97e6ee04 DE |
932 | GSocket_Debug(( "GSocket_Write error WOULDBLOCK\n" )); |
933 | } | |
934 | else | |
935 | { | |
09e6e5ec | 936 | m_error = GSOCK_IOERR; |
97e6ee04 DE |
937 | GSocket_Debug(( "GSocket_Write error IOERR\n" )); |
938 | } | |
939 | ||
940 | /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect | |
941 | * in MSW). Once the first OUTPUT event is received, users can assume | |
942 | * that the socket is writable until a read operation fails. Only then | |
943 | * will further OUTPUT events be posted. | |
944 | */ | |
09e6e5ec | 945 | Enable(GSOCK_OUTPUT); |
97e6ee04 DE |
946 | return -1; |
947 | } | |
b082b524 | 948 | |
97e6ee04 DE |
949 | GSocket_Debug(( "GSocket_Write #5, size %d ret %d\n", size, ret )); |
950 | ||
951 | return ret; | |
952 | } | |
953 | ||
954 | /* GSocket_Select: | |
955 | * Polls the socket to determine its status. This function will | |
956 | * check for the events specified in the 'flags' parameter, and | |
957 | * it will return a mask indicating which operations can be | |
958 | * performed. This function won't block, regardless of the | |
959 | * mode (blocking | nonblocking) of the socket. | |
960 | */ | |
ba2a81d7 | 961 | GSocketEventFlags GSocket::Select(GSocketEventFlags flags) |
97e6ee04 | 962 | { |
ba2a81d7 | 963 | if (!gs_gui_functions->CanUseEventLoop()) |
97e6ee04 DE |
964 | { |
965 | ||
966 | GSocketEventFlags result = 0; | |
967 | fd_set readfds; | |
968 | fd_set writefds; | |
969 | fd_set exceptfds; | |
970 | struct timeval tv; | |
971 | ||
9d715960 DE |
972 | assert(this); |
973 | ||
60b0bd1d JS |
974 | if (m_fd == -1) |
975 | return (GSOCK_LOST_FLAG & flags); | |
976 | ||
97e6ee04 | 977 | /* Do not use a static struct, Linux can garble it */ |
09e6e5ec | 978 | tv.tv_sec = m_timeout / 1000; |
7488a05e | 979 | tv.tv_usec = (m_timeout % 1000) * 1000; |
97e6ee04 | 980 | |
97e6ee04 DE |
981 | FD_ZERO(&readfds); |
982 | FD_ZERO(&writefds); | |
983 | FD_ZERO(&exceptfds); | |
09e6e5ec | 984 | FD_SET(m_fd, &readfds); |
9d715960 | 985 | if (flags & GSOCK_OUTPUT_FLAG || flags & GSOCK_CONNECTION_FLAG) |
09e6e5ec DE |
986 | FD_SET(m_fd, &writefds); |
987 | FD_SET(m_fd, &exceptfds); | |
97e6ee04 DE |
988 | |
989 | /* Check 'sticky' CONNECTION flag first */ | |
09e6e5ec | 990 | result |= (GSOCK_CONNECTION_FLAG & m_detected); |
97e6ee04 DE |
991 | |
992 | /* If we have already detected a LOST event, then don't try | |
993 | * to do any further processing. | |
994 | */ | |
09e6e5ec | 995 | if ((m_detected & GSOCK_LOST_FLAG) != 0) |
97e6ee04 | 996 | { |
948c96ef | 997 | m_establishing = false; |
97e6ee04 DE |
998 | |
999 | return (GSOCK_LOST_FLAG & flags); | |
1000 | } | |
1001 | ||
1002 | /* Try select now */ | |
09e6e5ec | 1003 | if (select(m_fd + 1, &readfds, &writefds, &exceptfds, &tv) <= 0) |
97e6ee04 DE |
1004 | { |
1005 | /* What to do here? */ | |
1006 | return (result & flags); | |
1007 | } | |
1008 | ||
1009 | /* Check for readability */ | |
09e6e5ec | 1010 | if (FD_ISSET(m_fd, &readfds)) |
97e6ee04 DE |
1011 | { |
1012 | char c; | |
1013 | ||
bb154f79 KH |
1014 | int num = recv(m_fd, &c, 1, MSG_PEEK | GSOCKET_MSG_NOSIGNAL); |
1015 | ||
1016 | if (num > 0) | |
97e6ee04 DE |
1017 | { |
1018 | result |= GSOCK_INPUT_FLAG; | |
1019 | } | |
1020 | else | |
1021 | { | |
09e6e5ec | 1022 | if (m_server && m_stream) |
97e6ee04 DE |
1023 | { |
1024 | result |= GSOCK_CONNECTION_FLAG; | |
09e6e5ec | 1025 | m_detected |= GSOCK_CONNECTION_FLAG; |
97e6ee04 | 1026 | } |
bb154f79 | 1027 | else if ((errno != EWOULDBLOCK) && (errno != EAGAIN) && (errno != EINTR)) |
97e6ee04 | 1028 | { |
09e6e5ec | 1029 | m_detected = GSOCK_LOST_FLAG; |
948c96ef | 1030 | m_establishing = false; |
b082b524 | 1031 | |
97e6ee04 DE |
1032 | /* LOST event: Abort any further processing */ |
1033 | return (GSOCK_LOST_FLAG & flags); | |
1034 | } | |
1035 | } | |
1036 | } | |
1037 | ||
1038 | /* Check for writability */ | |
09e6e5ec | 1039 | if (FD_ISSET(m_fd, &writefds)) |
97e6ee04 | 1040 | { |
09e6e5ec | 1041 | if (m_establishing && !m_server) |
97e6ee04 DE |
1042 | { |
1043 | int error; | |
ddc1a35f | 1044 | SOCKOPTLEN_T len = sizeof(error); |
97e6ee04 | 1045 | |
948c96ef | 1046 | m_establishing = false; |
97e6ee04 | 1047 | |
f71bb8ef | 1048 | getsockopt(m_fd, SOL_SOCKET, SO_ERROR, (char*)&error, &len); |
97e6ee04 DE |
1049 | |
1050 | if (error) | |
1051 | { | |
09e6e5ec | 1052 | m_detected = GSOCK_LOST_FLAG; |
97e6ee04 DE |
1053 | |
1054 | /* LOST event: Abort any further processing */ | |
1055 | return (GSOCK_LOST_FLAG & flags); | |
1056 | } | |
1057 | else | |
1058 | { | |
1059 | result |= GSOCK_CONNECTION_FLAG; | |
09e6e5ec | 1060 | m_detected |= GSOCK_CONNECTION_FLAG; |
97e6ee04 DE |
1061 | } |
1062 | } | |
1063 | else | |
1064 | { | |
1065 | result |= GSOCK_OUTPUT_FLAG; | |
1066 | } | |
1067 | } | |
1068 | ||
1069 | /* Check for exceptions and errors (is this useful in Unices?) */ | |
09e6e5ec | 1070 | if (FD_ISSET(m_fd, &exceptfds)) |
97e6ee04 | 1071 | { |
948c96ef | 1072 | m_establishing = false; |
09e6e5ec | 1073 | m_detected = GSOCK_LOST_FLAG; |
97e6ee04 DE |
1074 | |
1075 | /* LOST event: Abort any further processing */ | |
1076 | return (GSOCK_LOST_FLAG & flags); | |
1077 | } | |
1078 | ||
1079 | return (result & flags); | |
1080 | ||
1081 | } | |
1082 | else | |
1083 | { | |
1084 | ||
09e6e5ec DE |
1085 | assert(this); |
1086 | return flags & m_detected; | |
97e6ee04 DE |
1087 | |
1088 | } | |
1089 | } | |
1090 | ||
1091 | /* Flags */ | |
1092 | ||
1093 | /* GSocket_SetNonBlocking: | |
1094 | * Sets the socket to non-blocking mode. All IO calls will return | |
1095 | * immediately. | |
1096 | */ | |
948c96ef | 1097 | void GSocket::SetNonBlocking(bool non_block) |
97e6ee04 | 1098 | { |
09e6e5ec | 1099 | assert(this); |
97e6ee04 DE |
1100 | |
1101 | GSocket_Debug( ("GSocket_SetNonBlocking: %d\n", (int)non_block) ); | |
1102 | ||
09e6e5ec | 1103 | m_non_blocking = non_block; |
97e6ee04 DE |
1104 | } |
1105 | ||
1106 | /* GSocket_SetTimeout: | |
1107 | * Sets the timeout for blocking calls. Time is expressed in | |
1108 | * milliseconds. | |
1109 | */ | |
ba2a81d7 | 1110 | void GSocket::SetTimeout(unsigned long millisec) |
97e6ee04 | 1111 | { |
09e6e5ec | 1112 | assert(this); |
97e6ee04 | 1113 | |
09e6e5ec | 1114 | m_timeout = millisec; |
97e6ee04 DE |
1115 | } |
1116 | ||
1117 | /* GSocket_GetError: | |
d6922577 | 1118 | * Returns the last error occurred for this socket. Note that successful |
97e6ee04 DE |
1119 | * operations do not clear this back to GSOCK_NOERROR, so use it only |
1120 | * after an error. | |
1121 | */ | |
ba2a81d7 | 1122 | GSocketError WXDLLIMPEXP_NET GSocket::GetError() |
97e6ee04 | 1123 | { |
09e6e5ec | 1124 | assert(this); |
97e6ee04 | 1125 | |
09e6e5ec | 1126 | return m_error; |
97e6ee04 DE |
1127 | } |
1128 | ||
1129 | /* Callbacks */ | |
1130 | ||
1131 | /* GSOCK_INPUT: | |
1132 | * There is data to be read in the input buffer. If, after a read | |
1133 | * operation, there is still data available, the callback function will | |
1134 | * be called again. | |
1135 | * GSOCK_OUTPUT: | |
b082b524 | 1136 | * The socket is available for writing. That is, the next write call |
97e6ee04 DE |
1137 | * won't block. This event is generated only once, when the connection is |
1138 | * first established, and then only if a call failed with GSOCK_WOULDBLOCK, | |
1139 | * when the output buffer empties again. This means that the app should | |
1140 | * assume that it can write since the first OUTPUT event, and no more | |
1141 | * OUTPUT events will be generated unless an error occurs. | |
1142 | * GSOCK_CONNECTION: | |
d6922577 | 1143 | * Connection successfully established, for client sockets, or incoming |
97e6ee04 DE |
1144 | * client connection, for server sockets. Wait for this event (also watch |
1145 | * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call. | |
1146 | * GSOCK_LOST: | |
1147 | * The connection is lost (or a connection request failed); this could | |
1148 | * be due to a failure, or due to the peer closing it gracefully. | |
1149 | */ | |
1150 | ||
1151 | /* GSocket_SetCallback: | |
1152 | * Enables the callbacks specified by 'flags'. Note that 'flags' | |
1153 | * may be a combination of flags OR'ed toghether, so the same | |
1154 | * callback function can be made to accept different events. | |
1155 | * The callback function must have the following prototype: | |
1156 | * | |
1157 | * void function(GSocket *socket, GSocketEvent event, char *cdata) | |
1158 | */ | |
ba2a81d7 | 1159 | void GSocket::SetCallback(GSocketEventFlags flags, |
97e6ee04 DE |
1160 | GSocketCallback callback, char *cdata) |
1161 | { | |
1162 | int count; | |
1163 | ||
09e6e5ec | 1164 | assert(this); |
97e6ee04 DE |
1165 | |
1166 | for (count = 0; count < GSOCK_MAX_EVENT; count++) | |
1167 | { | |
1168 | if ((flags & (1 << count)) != 0) | |
1169 | { | |
09e6e5ec DE |
1170 | m_cbacks[count] = callback; |
1171 | m_data[count] = cdata; | |
97e6ee04 DE |
1172 | } |
1173 | } | |
1174 | } | |
1175 | ||
1176 | /* GSocket_UnsetCallback: | |
1177 | * Disables all callbacks specified by 'flags', which may be a | |
1178 | * combination of flags OR'ed toghether. | |
1179 | */ | |
ba2a81d7 | 1180 | void GSocket::UnsetCallback(GSocketEventFlags flags) |
97e6ee04 DE |
1181 | { |
1182 | int count; | |
1183 | ||
09e6e5ec | 1184 | assert(this); |
97e6ee04 DE |
1185 | |
1186 | for (count = 0; count < GSOCK_MAX_EVENT; count++) | |
1187 | { | |
1188 | if ((flags & (1 << count)) != 0) | |
1189 | { | |
09e6e5ec DE |
1190 | m_cbacks[count] = NULL; |
1191 | m_data[count] = NULL; | |
97e6ee04 DE |
1192 | } |
1193 | } | |
1194 | } | |
1195 | ||
ba2a81d7 | 1196 | GSocketError GSocket::GetSockOpt(int level, int optname, |
b082b524 DE |
1197 | void *optval, int *optlen) |
1198 | { | |
ddc1a35f | 1199 | if (getsockopt(m_fd, level, optname, (char*)optval, (SOCKOPTLEN_T*)optlen) == 0) |
b082b524 DE |
1200 | { |
1201 | return GSOCK_NOERROR; | |
1202 | } | |
1203 | return GSOCK_OPTERR; | |
1204 | } | |
1205 | ||
ba2a81d7 | 1206 | GSocketError GSocket::SetSockOpt(int level, int optname, |
b082b524 DE |
1207 | const void *optval, int optlen) |
1208 | { | |
f71bb8ef | 1209 | if (setsockopt(m_fd, level, optname, (const char*)optval, optlen) == 0) |
b082b524 DE |
1210 | { |
1211 | return GSOCK_NOERROR; | |
1212 | } | |
1213 | return GSOCK_OPTERR; | |
1214 | } | |
1215 | ||
ba2a81d7 | 1216 | #define CALL_CALLBACK(socket, event) { \ |
9a7e4a56 | 1217 | socket->Disable(event); \ |
ba2a81d7 DE |
1218 | if (socket->m_cbacks[event]) \ |
1219 | socket->m_cbacks[event](socket, event, socket->m_data[event]); \ | |
97e6ee04 DE |
1220 | } |
1221 | ||
1222 | ||
ba2a81d7 | 1223 | void GSocket::Enable(GSocketEvent event) |
97e6ee04 | 1224 | { |
09e6e5ec | 1225 | m_detected &= ~(1 << event); |
ba2a81d7 | 1226 | gs_gui_functions->Install_Callback(this, event); |
97e6ee04 DE |
1227 | } |
1228 | ||
ba2a81d7 | 1229 | void GSocket::Disable(GSocketEvent event) |
97e6ee04 | 1230 | { |
09e6e5ec | 1231 | m_detected |= (1 << event); |
ba2a81d7 | 1232 | gs_gui_functions->Uninstall_Callback(this, event); |
97e6ee04 DE |
1233 | } |
1234 | ||
1235 | /* _GSocket_Input_Timeout: | |
1236 | * For blocking sockets, wait until data is available or | |
1237 | * until timeout ellapses. | |
1238 | */ | |
ba2a81d7 | 1239 | GSocketError GSocket::Input_Timeout() |
97e6ee04 DE |
1240 | { |
1241 | struct timeval tv; | |
1242 | fd_set readfds; | |
1243 | int ret; | |
1244 | ||
1245 | /* Linux select() will overwrite the struct on return */ | |
09e6e5ec DE |
1246 | tv.tv_sec = (m_timeout / 1000); |
1247 | tv.tv_usec = (m_timeout % 1000) * 1000; | |
97e6ee04 | 1248 | |
09e6e5ec | 1249 | if (!m_non_blocking) |
97e6ee04 DE |
1250 | { |
1251 | FD_ZERO(&readfds); | |
09e6e5ec DE |
1252 | FD_SET(m_fd, &readfds); |
1253 | ret = select(m_fd + 1, &readfds, NULL, NULL, &tv); | |
97e6ee04 DE |
1254 | if (ret == 0) |
1255 | { | |
1256 | GSocket_Debug(( "GSocket_Input_Timeout, select returned 0\n" )); | |
09e6e5ec | 1257 | m_error = GSOCK_TIMEDOUT; |
97e6ee04 DE |
1258 | return GSOCK_TIMEDOUT; |
1259 | } | |
1260 | if (ret == -1) | |
1261 | { | |
1262 | GSocket_Debug(( "GSocket_Input_Timeout, select returned -1\n" )); | |
1263 | if (errno == EBADF) { GSocket_Debug(( "Invalid file descriptor\n" )); } | |
1264 | if (errno == EINTR) { GSocket_Debug(( "A non blocked signal was caught\n" )); } | |
1265 | if (errno == EINVAL) { GSocket_Debug(( "The highest number descriptor is negative\n" )); } | |
1266 | if (errno == ENOMEM) { GSocket_Debug(( "Not enough memory\n" )); } | |
09e6e5ec | 1267 | m_error = GSOCK_TIMEDOUT; |
97e6ee04 DE |
1268 | return GSOCK_TIMEDOUT; |
1269 | } | |
1270 | } | |
1271 | return GSOCK_NOERROR; | |
1272 | } | |
1273 | ||
1274 | /* _GSocket_Output_Timeout: | |
1275 | * For blocking sockets, wait until data can be sent without | |
1276 | * blocking or until timeout ellapses. | |
1277 | */ | |
ba2a81d7 | 1278 | GSocketError GSocket::Output_Timeout() |
97e6ee04 DE |
1279 | { |
1280 | struct timeval tv; | |
1281 | fd_set writefds; | |
1282 | int ret; | |
1283 | ||
1284 | /* Linux select() will overwrite the struct on return */ | |
09e6e5ec DE |
1285 | tv.tv_sec = (m_timeout / 1000); |
1286 | tv.tv_usec = (m_timeout % 1000) * 1000; | |
97e6ee04 | 1287 | |
09e6e5ec | 1288 | GSocket_Debug( ("m_non_blocking has: %d\n", (int)m_non_blocking) ); |
97e6ee04 | 1289 | |
09e6e5ec | 1290 | if (!m_non_blocking) |
97e6ee04 DE |
1291 | { |
1292 | FD_ZERO(&writefds); | |
09e6e5ec DE |
1293 | FD_SET(m_fd, &writefds); |
1294 | ret = select(m_fd + 1, NULL, &writefds, NULL, &tv); | |
97e6ee04 DE |
1295 | if (ret == 0) |
1296 | { | |
1297 | GSocket_Debug(( "GSocket_Output_Timeout, select returned 0\n" )); | |
09e6e5ec | 1298 | m_error = GSOCK_TIMEDOUT; |
97e6ee04 DE |
1299 | return GSOCK_TIMEDOUT; |
1300 | } | |
1301 | if (ret == -1) | |
1302 | { | |
1303 | GSocket_Debug(( "GSocket_Output_Timeout, select returned -1\n" )); | |
1304 | if (errno == EBADF) { GSocket_Debug(( "Invalid file descriptor\n" )); } | |
1305 | if (errno == EINTR) { GSocket_Debug(( "A non blocked signal was caught\n" )); } | |
1306 | if (errno == EINVAL) { GSocket_Debug(( "The highest number descriptor is negative\n" )); } | |
1307 | if (errno == ENOMEM) { GSocket_Debug(( "Not enough memory\n" )); } | |
09e6e5ec | 1308 | m_error = GSOCK_TIMEDOUT; |
97e6ee04 DE |
1309 | return GSOCK_TIMEDOUT; |
1310 | } | |
09e6e5ec | 1311 | if ( ! FD_ISSET(m_fd, &writefds) ) { |
97e6ee04 DE |
1312 | GSocket_Debug(( "GSocket_Output_Timeout is buggy!\n" )); |
1313 | } | |
1314 | else { | |
1315 | GSocket_Debug(( "GSocket_Output_Timeout seems correct\n" )); | |
1316 | } | |
1317 | } | |
1318 | else | |
1319 | { | |
1320 | GSocket_Debug(( "GSocket_Output_Timeout, didn't try select!\n" )); | |
1321 | } | |
1322 | ||
1323 | return GSOCK_NOERROR; | |
1324 | } | |
1325 | ||
ba2a81d7 | 1326 | int GSocket::Recv_Stream(char *buffer, int size) |
97e6ee04 | 1327 | { |
bb154f79 | 1328 | int ret; |
7e1e6965 | 1329 | do |
bb154f79 KH |
1330 | { |
1331 | ret = recv(m_fd, buffer, size, GSOCKET_MSG_NOSIGNAL); | |
e400d27d | 1332 | } while (ret == -1 && errno == EINTR); /* Loop until not interrupted */ |
bb154f79 | 1333 | return ret; |
97e6ee04 DE |
1334 | } |
1335 | ||
ba2a81d7 | 1336 | int GSocket::Recv_Dgram(char *buffer, int size) |
97e6ee04 DE |
1337 | { |
1338 | struct sockaddr from; | |
9e03e02d | 1339 | WX_SOCKLEN_T fromlen = sizeof(from); |
97e6ee04 DE |
1340 | int ret; |
1341 | GSocketError err; | |
1342 | ||
1343 | fromlen = sizeof(from); | |
1344 | ||
7e1e6965 | 1345 | do |
bb154f79 | 1346 | { |
9e03e02d | 1347 | ret = recvfrom(m_fd, buffer, size, 0, &from, (WX_SOCKLEN_T *) &fromlen); |
e400d27d | 1348 | } while (ret == -1 && errno == EINTR); /* Loop until not interrupted */ |
97e6ee04 DE |
1349 | |
1350 | if (ret == -1) | |
1351 | return -1; | |
1352 | ||
1353 | /* Translate a system address into a GSocket address */ | |
09e6e5ec | 1354 | if (!m_peer) |
97e6ee04 | 1355 | { |
09e6e5ec DE |
1356 | m_peer = GAddress_new(); |
1357 | if (!m_peer) | |
97e6ee04 | 1358 | { |
09e6e5ec | 1359 | m_error = GSOCK_MEMERR; |
97e6ee04 DE |
1360 | return -1; |
1361 | } | |
1362 | } | |
09e6e5ec | 1363 | err = _GAddress_translate_from(m_peer, &from, fromlen); |
97e6ee04 DE |
1364 | if (err != GSOCK_NOERROR) |
1365 | { | |
09e6e5ec DE |
1366 | GAddress_destroy(m_peer); |
1367 | m_peer = NULL; | |
1368 | m_error = err; | |
97e6ee04 DE |
1369 | return -1; |
1370 | } | |
1371 | ||
1372 | return ret; | |
1373 | } | |
1374 | ||
ba2a81d7 | 1375 | int GSocket::Send_Stream(const char *buffer, int size) |
97e6ee04 DE |
1376 | { |
1377 | int ret; | |
1378 | ||
7e1e6965 WS |
1379 | MASK_SIGNAL(); |
1380 | ||
1381 | do | |
bb154f79 KH |
1382 | { |
1383 | ret = send(m_fd, (char *)buffer, size, GSOCKET_MSG_NOSIGNAL); | |
e400d27d | 1384 | } while (ret == -1 && errno == EINTR); /* Loop until not interrupted */ |
7e1e6965 | 1385 | |
97e6ee04 | 1386 | UNMASK_SIGNAL(); |
97e6ee04 DE |
1387 | |
1388 | return ret; | |
1389 | } | |
1390 | ||
ba2a81d7 | 1391 | int GSocket::Send_Dgram(const char *buffer, int size) |
97e6ee04 DE |
1392 | { |
1393 | struct sockaddr *addr; | |
1394 | int len, ret; | |
1395 | GSocketError err; | |
1396 | ||
09e6e5ec | 1397 | if (!m_peer) |
97e6ee04 | 1398 | { |
09e6e5ec | 1399 | m_error = GSOCK_INVADDR; |
97e6ee04 DE |
1400 | return -1; |
1401 | } | |
1402 | ||
09e6e5ec | 1403 | err = _GAddress_translate_to(m_peer, &addr, &len); |
97e6ee04 DE |
1404 | if (err != GSOCK_NOERROR) |
1405 | { | |
09e6e5ec | 1406 | m_error = err; |
97e6ee04 DE |
1407 | return -1; |
1408 | } | |
1409 | ||
97e6ee04 | 1410 | MASK_SIGNAL(); |
7e1e6965 WS |
1411 | |
1412 | do | |
bb154f79 KH |
1413 | { |
1414 | ret = sendto(m_fd, (char *)buffer, size, 0, addr, len); | |
e400d27d | 1415 | } while (ret == -1 && errno == EINTR); /* Loop until not interrupted */ |
7e1e6965 | 1416 | |
97e6ee04 | 1417 | UNMASK_SIGNAL(); |
97e6ee04 DE |
1418 | |
1419 | /* Frees memory allocated from _GAddress_translate_to */ | |
1420 | free(addr); | |
1421 | ||
1422 | return ret; | |
1423 | } | |
1424 | ||
ba2a81d7 | 1425 | void GSocket::Detected_Read() |
97e6ee04 DE |
1426 | { |
1427 | char c; | |
1428 | ||
bb154f79 KH |
1429 | /* Safeguard against straggling call to Detected_Read */ |
1430 | if (m_fd == INVALID_SOCKET) | |
1431 | { | |
1432 | return; | |
1433 | } | |
1434 | ||
97e6ee04 DE |
1435 | /* If we have already detected a LOST event, then don't try |
1436 | * to do any further processing. | |
1437 | */ | |
09e6e5ec | 1438 | if ((m_detected & GSOCK_LOST_FLAG) != 0) |
97e6ee04 | 1439 | { |
948c96ef | 1440 | m_establishing = false; |
97e6ee04 | 1441 | |
ba2a81d7 | 1442 | CALL_CALLBACK(this, GSOCK_LOST); |
09e6e5ec | 1443 | Shutdown(); |
97e6ee04 DE |
1444 | return; |
1445 | } | |
1446 | ||
bb154f79 KH |
1447 | int num = recv(m_fd, &c, 1, MSG_PEEK | GSOCKET_MSG_NOSIGNAL); |
1448 | ||
1449 | if (num > 0) | |
97e6ee04 | 1450 | { |
ba2a81d7 | 1451 | CALL_CALLBACK(this, GSOCK_INPUT); |
97e6ee04 DE |
1452 | } |
1453 | else | |
1454 | { | |
09e6e5ec | 1455 | if (m_server && m_stream) |
97e6ee04 | 1456 | { |
ba2a81d7 | 1457 | CALL_CALLBACK(this, GSOCK_CONNECTION); |
97e6ee04 DE |
1458 | } |
1459 | else | |
1460 | { | |
e400d27d | 1461 | /* Do not throw a lost event in cases where the socket isn't really lost */ |
7e1e6965 | 1462 | if ((errno == EWOULDBLOCK) || (errno == EAGAIN) || (errno == EINTR)) |
bb154f79 KH |
1463 | { |
1464 | CALL_CALLBACK(this, GSOCK_INPUT); | |
1465 | } | |
7e1e6965 | 1466 | else |
bb154f79 KH |
1467 | { |
1468 | CALL_CALLBACK(this, GSOCK_LOST); | |
1469 | Shutdown(); | |
7e1e6965 | 1470 | } |
97e6ee04 DE |
1471 | } |
1472 | } | |
1473 | } | |
1474 | ||
ba2a81d7 | 1475 | void GSocket::Detected_Write() |
97e6ee04 DE |
1476 | { |
1477 | /* If we have already detected a LOST event, then don't try | |
1478 | * to do any further processing. | |
1479 | */ | |
09e6e5ec | 1480 | if ((m_detected & GSOCK_LOST_FLAG) != 0) |
97e6ee04 | 1481 | { |
948c96ef | 1482 | m_establishing = false; |
97e6ee04 | 1483 | |
ba2a81d7 | 1484 | CALL_CALLBACK(this, GSOCK_LOST); |
09e6e5ec | 1485 | Shutdown(); |
97e6ee04 DE |
1486 | return; |
1487 | } | |
1488 | ||
09e6e5ec | 1489 | if (m_establishing && !m_server) |
97e6ee04 DE |
1490 | { |
1491 | int error; | |
ddc1a35f | 1492 | SOCKOPTLEN_T len = sizeof(error); |
97e6ee04 | 1493 | |
948c96ef | 1494 | m_establishing = false; |
97e6ee04 | 1495 | |
f71bb8ef | 1496 | getsockopt(m_fd, SOL_SOCKET, SO_ERROR, (char*)&error, &len); |
97e6ee04 DE |
1497 | |
1498 | if (error) | |
1499 | { | |
ba2a81d7 | 1500 | CALL_CALLBACK(this, GSOCK_LOST); |
09e6e5ec | 1501 | Shutdown(); |
97e6ee04 DE |
1502 | } |
1503 | else | |
1504 | { | |
ba2a81d7 | 1505 | CALL_CALLBACK(this, GSOCK_CONNECTION); |
97e6ee04 DE |
1506 | /* We have to fire this event by hand because CONNECTION (for clients) |
1507 | * and OUTPUT are internally the same and we just disabled CONNECTION | |
1508 | * events with the above macro. | |
1509 | */ | |
ba2a81d7 | 1510 | CALL_CALLBACK(this, GSOCK_OUTPUT); |
97e6ee04 DE |
1511 | } |
1512 | } | |
1513 | else | |
1514 | { | |
ba2a81d7 | 1515 | CALL_CALLBACK(this, GSOCK_OUTPUT); |
97e6ee04 DE |
1516 | } |
1517 | } | |
1518 | ||
09e6e5ec DE |
1519 | /* Compatibility functions for GSocket */ |
1520 | GSocket *GSocket_new(void) | |
1521 | { | |
ba2a81d7 | 1522 | GSocket *newsocket = new GSocket(); |
09e6e5ec DE |
1523 | if(newsocket->IsOk()) |
1524 | return newsocket; | |
1525 | delete newsocket; | |
1526 | return NULL; | |
1527 | } | |
1528 | ||
97e6ee04 DE |
1529 | /* |
1530 | * ------------------------------------------------------------------------- | |
1531 | * GAddress | |
1532 | * ------------------------------------------------------------------------- | |
1533 | */ | |
1534 | ||
1535 | /* CHECK_ADDRESS verifies that the current address family is either | |
1536 | * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it | |
1537 | * initalizes it to be a GSOCK_*family*. In other cases, it returns | |
1538 | * an appropiate error code. | |
1539 | * | |
1540 | * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error. | |
1541 | */ | |
1542 | #define CHECK_ADDRESS(address, family) \ | |
1543 | { \ | |
1544 | if (address->m_family == GSOCK_NOFAMILY) \ | |
1545 | if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \ | |
1546 | return address->m_error; \ | |
1547 | if (address->m_family != GSOCK_##family) \ | |
1548 | { \ | |
1549 | address->m_error = GSOCK_INVADDR; \ | |
1550 | return GSOCK_INVADDR; \ | |
1551 | } \ | |
1552 | } | |
1553 | ||
1554 | #define CHECK_ADDRESS_RETVAL(address, family, retval) \ | |
1555 | { \ | |
1556 | if (address->m_family == GSOCK_NOFAMILY) \ | |
1557 | if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \ | |
1558 | return retval; \ | |
1559 | if (address->m_family != GSOCK_##family) \ | |
1560 | { \ | |
1561 | address->m_error = GSOCK_INVADDR; \ | |
1562 | return retval; \ | |
1563 | } \ | |
1564 | } | |
1565 | ||
1566 | ||
1567 | GAddress *GAddress_new(void) | |
1568 | { | |
1569 | GAddress *address; | |
1570 | ||
1571 | if ((address = (GAddress *) malloc(sizeof(GAddress))) == NULL) | |
1572 | return NULL; | |
1573 | ||
1574 | address->m_family = GSOCK_NOFAMILY; | |
1575 | address->m_addr = NULL; | |
1576 | address->m_len = 0; | |
1577 | ||
1578 | return address; | |
1579 | } | |
1580 | ||
1581 | GAddress *GAddress_copy(GAddress *address) | |
1582 | { | |
1583 | GAddress *addr2; | |
1584 | ||
1585 | assert(address != NULL); | |
1586 | ||
1587 | if ((addr2 = (GAddress *) malloc(sizeof(GAddress))) == NULL) | |
1588 | return NULL; | |
1589 | ||
1590 | memcpy(addr2, address, sizeof(GAddress)); | |
1591 | ||
1592 | if (address->m_addr && address->m_len > 0) | |
1593 | { | |
1594 | addr2->m_addr = (struct sockaddr *)malloc(addr2->m_len); | |
1595 | if (addr2->m_addr == NULL) | |
1596 | { | |
1597 | free(addr2); | |
1598 | return NULL; | |
1599 | } | |
1600 | memcpy(addr2->m_addr, address->m_addr, addr2->m_len); | |
1601 | } | |
1602 | ||
1603 | return addr2; | |
1604 | } | |
1605 | ||
1606 | void GAddress_destroy(GAddress *address) | |
1607 | { | |
1608 | assert(address != NULL); | |
1609 | ||
1610 | if (address->m_addr) | |
1611 | free(address->m_addr); | |
1612 | ||
1613 | free(address); | |
1614 | } | |
1615 | ||
1616 | void GAddress_SetFamily(GAddress *address, GAddressType type) | |
1617 | { | |
1618 | assert(address != NULL); | |
1619 | ||
1620 | address->m_family = type; | |
1621 | } | |
1622 | ||
1623 | GAddressType GAddress_GetFamily(GAddress *address) | |
1624 | { | |
1625 | assert(address != NULL); | |
1626 | ||
1627 | return address->m_family; | |
1628 | } | |
1629 | ||
1630 | GSocketError _GAddress_translate_from(GAddress *address, | |
1631 | struct sockaddr *addr, int len) | |
1632 | { | |
1633 | address->m_realfamily = addr->sa_family; | |
1634 | switch (addr->sa_family) | |
1635 | { | |
1636 | case AF_INET: | |
1637 | address->m_family = GSOCK_INET; | |
1638 | break; | |
1639 | case AF_UNIX: | |
1640 | address->m_family = GSOCK_UNIX; | |
1641 | break; | |
1642 | #ifdef AF_INET6 | |
1643 | case AF_INET6: | |
1644 | address->m_family = GSOCK_INET6; | |
1645 | break; | |
1646 | #endif | |
1647 | default: | |
1648 | { | |
1649 | address->m_error = GSOCK_INVOP; | |
1650 | return GSOCK_INVOP; | |
1651 | } | |
1652 | } | |
1653 | ||
1654 | if (address->m_addr) | |
1655 | free(address->m_addr); | |
1656 | ||
1657 | address->m_len = len; | |
1658 | address->m_addr = (struct sockaddr *)malloc(len); | |
1659 | ||
1660 | if (address->m_addr == NULL) | |
1661 | { | |
1662 | address->m_error = GSOCK_MEMERR; | |
1663 | return GSOCK_MEMERR; | |
1664 | } | |
1665 | memcpy(address->m_addr, addr, len); | |
1666 | ||
1667 | return GSOCK_NOERROR; | |
1668 | } | |
1669 | ||
1670 | GSocketError _GAddress_translate_to(GAddress *address, | |
1671 | struct sockaddr **addr, int *len) | |
1672 | { | |
1673 | if (!address->m_addr) | |
1674 | { | |
1675 | address->m_error = GSOCK_INVADDR; | |
1676 | return GSOCK_INVADDR; | |
1677 | } | |
1678 | ||
1679 | *len = address->m_len; | |
1680 | *addr = (struct sockaddr *)malloc(address->m_len); | |
1681 | if (*addr == NULL) | |
1682 | { | |
1683 | address->m_error = GSOCK_MEMERR; | |
1684 | return GSOCK_MEMERR; | |
1685 | } | |
1686 | ||
1687 | memcpy(*addr, address->m_addr, address->m_len); | |
1688 | return GSOCK_NOERROR; | |
1689 | } | |
1690 | ||
1691 | /* | |
1692 | * ------------------------------------------------------------------------- | |
1693 | * Internet address family | |
1694 | * ------------------------------------------------------------------------- | |
1695 | */ | |
1696 | ||
1697 | GSocketError _GAddress_Init_INET(GAddress *address) | |
1698 | { | |
1699 | address->m_len = sizeof(struct sockaddr_in); | |
1700 | address->m_addr = (struct sockaddr *) malloc(address->m_len); | |
1701 | if (address->m_addr == NULL) | |
1702 | { | |
1703 | address->m_error = GSOCK_MEMERR; | |
1704 | return GSOCK_MEMERR; | |
1705 | } | |
1706 | ||
1707 | address->m_family = GSOCK_INET; | |
1708 | address->m_realfamily = PF_INET; | |
1709 | ((struct sockaddr_in *)address->m_addr)->sin_family = AF_INET; | |
1710 | ((struct sockaddr_in *)address->m_addr)->sin_addr.s_addr = INADDR_ANY; | |
1711 | ||
1712 | return GSOCK_NOERROR; | |
1713 | } | |
1714 | ||
1715 | GSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname) | |
1716 | { | |
1717 | struct hostent *he; | |
1718 | struct in_addr *addr; | |
1719 | ||
1720 | assert(address != NULL); | |
1721 | ||
1722 | CHECK_ADDRESS(address, INET); | |
1723 | ||
1724 | addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr); | |
1725 | ||
1726 | /* If it is a numeric host name, convert it now */ | |
1727 | #if defined(HAVE_INET_ATON) | |
1728 | if (inet_aton(hostname, addr) == 0) | |
1729 | { | |
1730 | #elif defined(HAVE_INET_ADDR) | |
8c0f8906 | 1731 | if ( (addr->s_addr = inet_addr(hostname)) == (unsigned)-1 ) |
97e6ee04 DE |
1732 | { |
1733 | #else | |
1734 | /* Use gethostbyname by default */ | |
9d715960 | 1735 | #ifndef __WXMAC__ |
ba2a81d7 | 1736 | int val = 1; /* VA doesn't like constants in conditional expressions */ |
97e6ee04 | 1737 | if (val) |
9d715960 | 1738 | #endif |
97e6ee04 DE |
1739 | { |
1740 | #endif | |
1741 | struct in_addr *array_addr; | |
1742 | ||
1743 | /* It is a real name, we solve it */ | |
1744 | if ((he = gethostbyname(hostname)) == NULL) | |
1745 | { | |
1746 | /* Reset to invalid address */ | |
1747 | addr->s_addr = INADDR_NONE; | |
1748 | address->m_error = GSOCK_NOHOST; | |
1749 | return GSOCK_NOHOST; | |
1750 | } | |
1751 | array_addr = (struct in_addr *) *(he->h_addr_list); | |
1752 | addr->s_addr = array_addr[0].s_addr; | |
1753 | } | |
1754 | return GSOCK_NOERROR; | |
1755 | } | |
1756 | ||
1757 | GSocketError GAddress_INET_SetAnyAddress(GAddress *address) | |
1758 | { | |
1759 | return GAddress_INET_SetHostAddress(address, INADDR_ANY); | |
1760 | } | |
1761 | ||
1762 | GSocketError GAddress_INET_SetHostAddress(GAddress *address, | |
1763 | unsigned long hostaddr) | |
1764 | { | |
1765 | struct in_addr *addr; | |
1766 | ||
1767 | assert(address != NULL); | |
1768 | ||
1769 | CHECK_ADDRESS(address, INET); | |
1770 | ||
1771 | addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr); | |
9d715960 | 1772 | addr->s_addr = htonl(hostaddr); |
97e6ee04 DE |
1773 | |
1774 | return GSOCK_NOERROR; | |
1775 | } | |
1776 | ||
1777 | GSocketError GAddress_INET_SetPortName(GAddress *address, const char *port, | |
1778 | const char *protocol) | |
1779 | { | |
1780 | struct servent *se; | |
1781 | struct sockaddr_in *addr; | |
1782 | ||
1783 | assert(address != NULL); | |
1784 | CHECK_ADDRESS(address, INET); | |
1785 | ||
1786 | if (!port) | |
1787 | { | |
1788 | address->m_error = GSOCK_INVPORT; | |
1789 | return GSOCK_INVPORT; | |
1790 | } | |
b082b524 | 1791 | |
f6bc1c74 DW |
1792 | #if defined(__WXPM__) && defined(__EMX__) |
1793 | se = getservbyname(port, (char*)protocol); | |
1794 | #else | |
97e6ee04 | 1795 | se = getservbyname(port, protocol); |
f6bc1c74 | 1796 | #endif |
97e6ee04 DE |
1797 | if (!se) |
1798 | { | |
1799 | /* the cast to int suppresses compiler warnings about subscript having the | |
1800 | type char */ | |
1801 | if (isdigit((int)port[0])) | |
1802 | { | |
1803 | int port_int; | |
1804 | ||
1805 | port_int = atoi(port); | |
1806 | addr = (struct sockaddr_in *)address->m_addr; | |
1807 | addr->sin_port = htons(port_int); | |
1808 | return GSOCK_NOERROR; | |
1809 | } | |
1810 | ||
1811 | address->m_error = GSOCK_INVPORT; | |
1812 | return GSOCK_INVPORT; | |
1813 | } | |
1814 | ||
1815 | addr = (struct sockaddr_in *)address->m_addr; | |
1816 | addr->sin_port = se->s_port; | |
1817 | ||
1818 | return GSOCK_NOERROR; | |
1819 | } | |
1820 | ||
1821 | GSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port) | |
1822 | { | |
1823 | struct sockaddr_in *addr; | |
1824 | ||
1825 | assert(address != NULL); | |
1826 | CHECK_ADDRESS(address, INET); | |
b082b524 | 1827 | |
97e6ee04 DE |
1828 | addr = (struct sockaddr_in *)address->m_addr; |
1829 | addr->sin_port = htons(port); | |
1830 | ||
1831 | return GSOCK_NOERROR; | |
1832 | } | |
1833 | ||
1834 | GSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname, size_t sbuf) | |
1835 | { | |
1836 | struct hostent *he; | |
1837 | char *addr_buf; | |
1838 | struct sockaddr_in *addr; | |
1839 | ||
b082b524 | 1840 | assert(address != NULL); |
97e6ee04 DE |
1841 | CHECK_ADDRESS(address, INET); |
1842 | ||
1843 | addr = (struct sockaddr_in *)address->m_addr; | |
1844 | addr_buf = (char *)&(addr->sin_addr); | |
1845 | ||
1846 | he = gethostbyaddr(addr_buf, sizeof(addr->sin_addr), AF_INET); | |
1847 | if (he == NULL) | |
1848 | { | |
1849 | address->m_error = GSOCK_NOHOST; | |
1850 | return GSOCK_NOHOST; | |
1851 | } | |
1852 | ||
1853 | strncpy(hostname, he->h_name, sbuf); | |
1854 | ||
1855 | return GSOCK_NOERROR; | |
1856 | } | |
1857 | ||
1858 | unsigned long GAddress_INET_GetHostAddress(GAddress *address) | |
1859 | { | |
1860 | struct sockaddr_in *addr; | |
1861 | ||
b082b524 DE |
1862 | assert(address != NULL); |
1863 | CHECK_ADDRESS_RETVAL(address, INET, 0); | |
97e6ee04 DE |
1864 | |
1865 | addr = (struct sockaddr_in *)address->m_addr; | |
1866 | ||
9d715960 | 1867 | return ntohl(addr->sin_addr.s_addr); |
97e6ee04 DE |
1868 | } |
1869 | ||
1870 | unsigned short GAddress_INET_GetPort(GAddress *address) | |
1871 | { | |
1872 | struct sockaddr_in *addr; | |
1873 | ||
b082b524 DE |
1874 | assert(address != NULL); |
1875 | CHECK_ADDRESS_RETVAL(address, INET, 0); | |
97e6ee04 DE |
1876 | |
1877 | addr = (struct sockaddr_in *)address->m_addr; | |
1878 | return ntohs(addr->sin_port); | |
1879 | } | |
1880 | ||
1881 | /* | |
1882 | * ------------------------------------------------------------------------- | |
1883 | * Unix address family | |
1884 | * ------------------------------------------------------------------------- | |
1885 | */ | |
1886 | ||
1887 | #ifndef __VISAGECPP__ | |
1888 | GSocketError _GAddress_Init_UNIX(GAddress *address) | |
1889 | { | |
1890 | address->m_len = sizeof(struct sockaddr_un); | |
1891 | address->m_addr = (struct sockaddr *)malloc(address->m_len); | |
1892 | if (address->m_addr == NULL) | |
1893 | { | |
1894 | address->m_error = GSOCK_MEMERR; | |
1895 | return GSOCK_MEMERR; | |
1896 | } | |
1897 | ||
1898 | address->m_family = GSOCK_UNIX; | |
1899 | address->m_realfamily = PF_UNIX; | |
1900 | ((struct sockaddr_un *)address->m_addr)->sun_family = AF_UNIX; | |
1901 | ((struct sockaddr_un *)address->m_addr)->sun_path[0] = 0; | |
1902 | ||
1903 | return GSOCK_NOERROR; | |
1904 | } | |
1905 | ||
1906 | #define UNIX_SOCK_PATHLEN (sizeof(addr->sun_path)/sizeof(addr->sun_path[0])) | |
1907 | ||
1908 | GSocketError GAddress_UNIX_SetPath(GAddress *address, const char *path) | |
1909 | { | |
1910 | struct sockaddr_un *addr; | |
1911 | ||
b082b524 | 1912 | assert(address != NULL); |
97e6ee04 | 1913 | |
b082b524 | 1914 | CHECK_ADDRESS(address, UNIX); |
97e6ee04 DE |
1915 | |
1916 | addr = ((struct sockaddr_un *)address->m_addr); | |
1917 | strncpy(addr->sun_path, path, UNIX_SOCK_PATHLEN); | |
1918 | addr->sun_path[UNIX_SOCK_PATHLEN - 1] = '\0'; | |
1919 | ||
1920 | return GSOCK_NOERROR; | |
1921 | } | |
1922 | ||
1923 | GSocketError GAddress_UNIX_GetPath(GAddress *address, char *path, size_t sbuf) | |
1924 | { | |
1925 | struct sockaddr_un *addr; | |
1926 | ||
1927 | assert(address != NULL); | |
1928 | CHECK_ADDRESS(address, UNIX); | |
1929 | ||
1930 | addr = (struct sockaddr_un *)address->m_addr; | |
1931 | ||
1932 | strncpy(path, addr->sun_path, sbuf); | |
1933 | ||
1934 | return GSOCK_NOERROR; | |
1935 | } | |
1936 | #endif /* !defined(__VISAGECPP__) */ | |
1937 | #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */ |