]>
Commit | Line | Data |
---|---|---|
97e6ee04 | 1 | /* ------------------------------------------------------------------------- |
99d80019 JS |
2 | * Project: GSocket (Generic Socket) for WX |
3 | * Name: gsocket.c | |
4 | * Copyright: (c) Guilhem Lavaux | |
5 | * Licence: wxWindows Licence | |
6 | * Authors: David Elliott (C++ conversion, maintainer) | |
7 | * Guilhem Lavaux, | |
8 | * Guillermo Rodriguez Garcia <guille@iies.es> | |
9 | * Purpose: GSocket main Unix and OS/2 file | |
10 | * Licence: The wxWindows licence | |
11 | * CVSID: $Id$ | |
97e6ee04 DE |
12 | * ------------------------------------------------------------------------- |
13 | */ | |
14 | ||
7e1e6965 WS |
15 | #if defined(__WATCOMC__) |
16 | #include "wx/wxprec.h" | |
17 | #include <errno.h> | |
18 | #include <nerrno.h> | |
19 | #endif | |
20 | ||
ba2a81d7 | 21 | #ifndef __GSOCKET_STANDALONE__ |
0beb8735 | 22 | #include "wx/defs.h" |
ba2a81d7 | 23 | #endif |
97e6ee04 DE |
24 | |
25 | #if defined(__VISAGECPP__) | |
97e6ee04 DE |
26 | #define BSD_SELECT /* use Berkley Sockets select */ |
27 | #endif | |
28 | ||
29 | #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) | |
30 | ||
31 | #include <assert.h> | |
32 | #include <sys/types.h> | |
33 | #ifdef __VISAGECPP__ | |
34 | #include <string.h> | |
35 | #include <sys/time.h> | |
36 | #include <types.h> | |
37 | #include <netinet/in.h> | |
38 | #endif | |
39 | #include <netdb.h> | |
40 | #include <sys/ioctl.h> | |
41 | ||
42 | #ifdef __VMS__ | |
43 | #include <socket.h> | |
44 | struct sockaddr_un | |
45 | { | |
46 | u_char sun_len; /* sockaddr len including null */ | |
47 | u_char sun_family; /* AF_UNIX */ | |
48 | char sun_path[108]; /* path name (gag) */ | |
49 | }; | |
50 | #else | |
51 | #include <sys/socket.h> | |
52 | #include <sys/un.h> | |
53 | #endif | |
54 | ||
55 | #ifndef __VISAGECPP__ | |
56 | #include <sys/time.h> | |
57 | #include <netinet/in.h> | |
58 | #include <arpa/inet.h> | |
59 | #include <errno.h> | |
60 | #include <string.h> | |
61 | #include <unistd.h> | |
62 | #else | |
63 | #include <nerrno.h> | |
64 | # if __IBMCPP__ < 400 | |
65 | #include <machine/endian.h> | |
66 | #include <socket.h> | |
67 | #include <ioctl.h> | |
68 | #include <select.h> | |
69 | #include <unistd.h> | |
70 | ||
71 | #define EBADF SOCEBADF | |
72 | ||
73 | # ifdef min | |
74 | # undef min | |
75 | # endif | |
76 | # else | |
77 | #include <sys/socket.h> | |
78 | #include <sys/ioctl.h> | |
79 | #include <sys/select.h> | |
80 | ||
81 | #define close(a) soclose(a) | |
82 | #define select(a,b,c,d,e) bsdselect(a,b,c,d,e) | |
83 | int _System bsdselect(int, | |
84 | struct fd_set *, | |
85 | struct fd_set *, | |
86 | struct fd_set *, | |
87 | struct timeval *); | |
88 | int _System soclose(int); | |
89 | # endif | |
90 | #endif | |
ebdab982 SN |
91 | #ifdef __EMX__ |
92 | #include <sys/select.h> | |
93 | #endif | |
94 | ||
97e6ee04 DE |
95 | #include <stdio.h> |
96 | #include <stdlib.h> | |
97 | #include <stddef.h> | |
98 | #include <ctype.h> | |
99 | #ifdef sun | |
100 | # include <sys/filio.h> | |
101 | #endif | |
102 | #ifdef sgi | |
103 | # include <bstring.h> | |
104 | #endif | |
d1f8e97b SN |
105 | #ifdef _AIX |
106 | # include <strings.h> | |
107 | #endif | |
97e6ee04 DE |
108 | #include <signal.h> |
109 | ||
9e03e02d | 110 | #ifndef WX_SOCKLEN_T |
97e6ee04 DE |
111 | |
112 | #ifdef VMS | |
9e03e02d | 113 | # define WX_SOCKLEN_T unsigned int |
97e6ee04 DE |
114 | #else |
115 | # ifdef __GLIBC__ | |
116 | # if __GLIBC__ == 2 | |
9e03e02d | 117 | # define WX_SOCKLEN_T socklen_t |
97e6ee04 | 118 | # endif |
fcbd7e5a | 119 | # elif defined(__WXMAC__) |
9e03e02d | 120 | # define WX_SOCKLEN_T socklen_t |
97e6ee04 | 121 | # else |
9e03e02d | 122 | # define WX_SOCKLEN_T int |
97e6ee04 DE |
123 | # endif |
124 | #endif | |
125 | ||
126 | #endif /* SOCKLEN_T */ | |
127 | ||
ddc1a35f | 128 | #ifndef SOCKOPTLEN_T |
9e03e02d | 129 | #define SOCKOPTLEN_T WX_SOCKLEN_T |
ddc1a35f DE |
130 | #endif |
131 | ||
97e6ee04 DE |
132 | /* |
133 | * MSW defines this, Unices don't. | |
134 | */ | |
135 | #ifndef INVALID_SOCKET | |
136 | #define INVALID_SOCKET -1 | |
137 | #endif | |
138 | ||
139 | /* UnixWare reportedly needs this for FIONBIO definition */ | |
140 | #ifdef __UNIXWARE__ | |
141 | #include <sys/filio.h> | |
142 | #endif | |
143 | ||
144 | /* | |
145 | * INADDR_BROADCAST is identical to INADDR_NONE which is not defined | |
146 | * on all systems. INADDR_BROADCAST should be fine to indicate an error. | |
147 | */ | |
148 | #ifndef INADDR_NONE | |
149 | #define INADDR_NONE INADDR_BROADCAST | |
150 | #endif | |
151 | ||
7e1e6965 | 152 | #if defined(__VISAGECPP__) || defined(__WATCOMC__) |
97e6ee04 | 153 | |
7e1e6965 WS |
154 | #define MASK_SIGNAL() { |
155 | #define UNMASK_SIGNAL() } | |
156 | ||
157 | #else | |
defbeca1 | 158 | extern "C" { typedef void (*wxSigHandler)(int); } |
7e1e6965 WS |
159 | |
160 | #define MASK_SIGNAL() \ | |
161 | { \ | |
defbeca1 | 162 | wxSigHandler old_handler = signal(SIGPIPE, SIG_IGN); |
7e1e6965 WS |
163 | |
164 | #define UNMASK_SIGNAL() \ | |
165 | signal(SIGPIPE, old_handler); \ | |
166 | } | |
167 | ||
168 | #endif | |
97e6ee04 | 169 | |
e400d27d KH |
170 | /* If a SIGPIPE is issued by a socket call on a remotely closed socket, |
171 | the program will "crash" unless it explicitly handles the SIGPIPE. | |
172 | By using MSG_NOSIGNAL, the SIGPIPE is suppressed. Later, we will | |
173 | use SO_NOSIGPIPE (if available), the BSD equivalent. */ | |
bb154f79 KH |
174 | #ifdef MSG_NOSIGNAL |
175 | # define GSOCKET_MSG_NOSIGNAL MSG_NOSIGNAL | |
176 | #else /* MSG_NOSIGNAL not available (FreeBSD including OS X) */ | |
177 | # define GSOCKET_MSG_NOSIGNAL 0 | |
178 | #endif /* MSG_NOSIGNAL */ | |
97e6ee04 DE |
179 | |
180 | #ifndef __GSOCKET_STANDALONE__ | |
181 | # include "wx/unix/gsockunx.h" | |
17a1ebd1 | 182 | # include "wx/unix/private.h" |
97e6ee04 DE |
183 | # include "wx/gsocket.h" |
184 | #else | |
185 | # include "gsockunx.h" | |
186 | # include "gsocket.h" | |
5fb9aa56 WS |
187 | # ifndef WXUNUSED |
188 | # define WXUNUSED(x) | |
189 | # endif | |
97e6ee04 DE |
190 | #endif /* __GSOCKET_STANDALONE__ */ |
191 | ||
192 | /* debugging helpers */ | |
193 | #ifdef __GSOCKET_DEBUG__ | |
194 | # define GSocket_Debug(args) printf args | |
195 | #else | |
196 | # define GSocket_Debug(args) | |
197 | #endif /* __GSOCKET_DEBUG__ */ | |
198 | ||
ba2a81d7 DE |
199 | /* Table of GUI-related functions. We must call them indirectly because |
200 | * of wxBase and GUI separation: */ | |
444cb1fd | 201 | |
3e1400ac | 202 | static GSocketGUIFunctionsTable *gs_gui_functions; |
b082b524 | 203 | |
ba2a81d7 | 204 | class GSocketGUIFunctionsTableNull: public GSocketGUIFunctionsTable |
444cb1fd | 205 | { |
ba2a81d7 DE |
206 | public: |
207 | virtual bool OnInit(); | |
208 | virtual void OnExit(); | |
209 | virtual bool CanUseEventLoop(); | |
210 | virtual bool Init_Socket(GSocket *socket); | |
211 | virtual void Destroy_Socket(GSocket *socket); | |
212 | virtual void Install_Callback(GSocket *socket, GSocketEvent event); | |
ba2a81d7 DE |
213 | virtual void Uninstall_Callback(GSocket *socket, GSocketEvent event); |
214 | virtual void Enable_Events(GSocket *socket); | |
215 | virtual void Disable_Events(GSocket *socket); | |
216 | }; | |
444cb1fd | 217 | |
ba2a81d7 DE |
218 | bool GSocketGUIFunctionsTableNull::OnInit() |
219 | { return true; } | |
220 | void GSocketGUIFunctionsTableNull::OnExit() | |
221 | {} | |
222 | bool GSocketGUIFunctionsTableNull::CanUseEventLoop() | |
223 | { return false; } | |
c40158e4 | 224 | bool GSocketGUIFunctionsTableNull::Init_Socket(GSocket *WXUNUSED(socket)) |
ba2a81d7 | 225 | { return true; } |
c40158e4 | 226 | void GSocketGUIFunctionsTableNull::Destroy_Socket(GSocket *WXUNUSED(socket)) |
ba2a81d7 | 227 | {} |
c40158e4 | 228 | void GSocketGUIFunctionsTableNull::Install_Callback(GSocket *WXUNUSED(socket), GSocketEvent WXUNUSED(event)) |
ba2a81d7 | 229 | {} |
c40158e4 | 230 | void GSocketGUIFunctionsTableNull::Uninstall_Callback(GSocket *WXUNUSED(socket), GSocketEvent WXUNUSED(event)) |
ba2a81d7 | 231 | {} |
c40158e4 | 232 | void GSocketGUIFunctionsTableNull::Enable_Events(GSocket *WXUNUSED(socket)) |
ba2a81d7 | 233 | {} |
c40158e4 | 234 | void GSocketGUIFunctionsTableNull::Disable_Events(GSocket *WXUNUSED(socket)) |
ba2a81d7 DE |
235 | {} |
236 | /* Global initialisers */ | |
444cb1fd | 237 | |
3e1400ac | 238 | void GSocket_SetGUIFunctions(GSocketGUIFunctionsTable *guifunc) |
444cb1fd | 239 | { |
ba2a81d7 | 240 | gs_gui_functions = guifunc; |
444cb1fd DE |
241 | } |
242 | ||
97e6ee04 DE |
243 | int GSocket_Init(void) |
244 | { | |
ba2a81d7 DE |
245 | if (!gs_gui_functions) |
246 | { | |
3e1400ac | 247 | static GSocketGUIFunctionsTableNull table; |
ba2a81d7 DE |
248 | gs_gui_functions = &table; |
249 | } | |
250 | if ( !gs_gui_functions->OnInit() ) | |
251 | return 0; | |
252 | return 1; | |
97e6ee04 DE |
253 | } |
254 | ||
255 | void GSocket_Cleanup(void) | |
256 | { | |
ba2a81d7 DE |
257 | if (gs_gui_functions) |
258 | { | |
259 | gs_gui_functions->OnExit(); | |
260 | } | |
97e6ee04 DE |
261 | } |
262 | ||
ba2a81d7 DE |
263 | /* Constructors / Destructors for GSocket */ |
264 | ||
265 | GSocket::GSocket() | |
97e6ee04 | 266 | { |
09e6e5ec | 267 | int i; |
97e6ee04 | 268 | |
09e6e5ec | 269 | m_fd = INVALID_SOCKET; |
97e6ee04 DE |
270 | for (i=0;i<GSOCK_MAX_EVENT;i++) |
271 | { | |
09e6e5ec | 272 | m_cbacks[i] = NULL; |
97e6ee04 | 273 | } |
09e6e5ec DE |
274 | m_detected = 0; |
275 | m_local = NULL; | |
276 | m_peer = NULL; | |
277 | m_error = GSOCK_NOERROR; | |
948c96ef DE |
278 | m_server = false; |
279 | m_stream = true; | |
09e6e5ec | 280 | m_gui_dependent = NULL; |
948c96ef | 281 | m_non_blocking = false; |
1eef64d8 | 282 | m_reusable = false; |
09e6e5ec | 283 | m_timeout = 10*60*1000; |
97e6ee04 | 284 | /* 10 minutes * 60 sec * 1000 millisec */ |
948c96ef | 285 | m_establishing = false; |
97e6ee04 | 286 | |
ba2a81d7 DE |
287 | assert(gs_gui_functions); |
288 | /* Per-socket GUI-specific initialization */ | |
289 | m_ok = gs_gui_functions->Init_Socket(this); | |
97e6ee04 DE |
290 | } |
291 | ||
ba2a81d7 | 292 | void GSocket::Close() |
97e6ee04 | 293 | { |
ba2a81d7 DE |
294 | gs_gui_functions->Disable_Events(this); |
295 | /* gsockosx.c calls CFSocketInvalidate which closes the socket for us */ | |
97e6ee04 | 296 | #if !(defined(__DARWIN__) && (defined(__WXMAC__) || defined(__WXCOCOA__))) |
09e6e5ec | 297 | close(m_fd); |
97e6ee04 | 298 | #endif |
09e6e5ec | 299 | m_fd = INVALID_SOCKET; |
97e6ee04 DE |
300 | } |
301 | ||
ba2a81d7 | 302 | GSocket::~GSocket() |
97e6ee04 | 303 | { |
ba2a81d7 DE |
304 | assert(this); |
305 | ||
306 | /* Check that the socket is really shutdowned */ | |
307 | if (m_fd != INVALID_SOCKET) | |
308 | Shutdown(); | |
309 | ||
310 | /* Per-socket GUI-specific cleanup */ | |
311 | gs_gui_functions->Destroy_Socket(this); | |
312 | ||
97e6ee04 | 313 | /* Destroy private addresses */ |
09e6e5ec DE |
314 | if (m_local) |
315 | GAddress_destroy(m_local); | |
97e6ee04 | 316 | |
09e6e5ec DE |
317 | if (m_peer) |
318 | GAddress_destroy(m_peer); | |
97e6ee04 DE |
319 | } |
320 | ||
321 | /* GSocket_Shutdown: | |
322 | * Disallow further read/write operations on this socket, close | |
323 | * the fd and disable all callbacks. | |
324 | */ | |
ba2a81d7 | 325 | void GSocket::Shutdown() |
97e6ee04 DE |
326 | { |
327 | int evt; | |
328 | ||
09e6e5ec | 329 | assert(this); |
97e6ee04 | 330 | |
c3b501e1 KH |
331 | /* Don't allow events to fire after socket has been closed */ |
332 | gs_gui_functions->Disable_Events(this); | |
333 | ||
97e6ee04 | 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 | |
17a1ebd1 | 724 | * call to Enable_Events now. |
a3687636 | 725 | */ |
17a1ebd1 | 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) */ |
976abb72 VZ |
872 | if (Input_Timeout() == GSOCK_TIMEDOUT) { |
873 | m_error = GSOCK_TIMEDOUT; | |
874 | /* Don't return here immediately, otherwise socket events would not be | |
875 | * re-enabled! */ | |
9d715960 | 876 | ret = -1; |
976abb72 | 877 | } |
9d715960 DE |
878 | else { |
879 | /* Read the data */ | |
880 | if (m_stream) | |
881 | ret = Recv_Stream(buffer, size); | |
882 | else | |
883 | ret = Recv_Dgram(buffer, size); | |
b082b524 | 884 | |
9b67181b KH |
885 | /* If recv returned zero, then the connection is lost, and errno is not set. |
886 | * Otherwise, recv has returned an error (-1), in which case we have lost the | |
887 | * socket only if errno does _not_ indicate that there may be more data to read. | |
888 | */ | |
889 | if (ret == 0) | |
890 | m_error = GSOCK_IOERR; | |
891 | else if (ret == -1) { | |
976abb72 VZ |
892 | if ((errno == EWOULDBLOCK) || (errno == EAGAIN)) |
893 | m_error = GSOCK_WOULDBLOCK; | |
894 | else | |
895 | m_error = GSOCK_IOERR; | |
896 | } | |
97e6ee04 | 897 | } |
b082b524 | 898 | |
9d715960 | 899 | /* Enable events again now that we are done processing */ |
09e6e5ec | 900 | Enable(GSOCK_INPUT); |
97e6ee04 DE |
901 | |
902 | return ret; | |
903 | } | |
904 | ||
ba2a81d7 | 905 | int GSocket::Write(const char *buffer, int size) |
b082b524 | 906 | { |
97e6ee04 DE |
907 | int ret; |
908 | ||
09e6e5ec | 909 | assert(this); |
b082b524 | 910 | |
97e6ee04 DE |
911 | GSocket_Debug(( "GSocket_Write #1, size %d\n", size )); |
912 | ||
09e6e5ec | 913 | if (m_fd == INVALID_SOCKET || m_server) |
97e6ee04 | 914 | { |
09e6e5ec | 915 | m_error = GSOCK_INVSOCK; |
97e6ee04 DE |
916 | return -1; |
917 | } | |
918 | ||
919 | GSocket_Debug(( "GSocket_Write #2, size %d\n", size )); | |
920 | ||
921 | /* If the socket is blocking, wait for writability (with a timeout) */ | |
09e6e5ec | 922 | if (Output_Timeout() == GSOCK_TIMEDOUT) |
97e6ee04 DE |
923 | return -1; |
924 | ||
925 | GSocket_Debug(( "GSocket_Write #3, size %d\n", size )); | |
926 | ||
927 | /* Write the data */ | |
09e6e5ec DE |
928 | if (m_stream) |
929 | ret = Send_Stream(buffer, size); | |
97e6ee04 | 930 | else |
09e6e5ec | 931 | ret = Send_Dgram(buffer, size); |
b082b524 | 932 | |
97e6ee04 DE |
933 | GSocket_Debug(( "GSocket_Write #4, size %d\n", size )); |
934 | ||
935 | if (ret == -1) | |
936 | { | |
7e1e6965 | 937 | if ((errno == EWOULDBLOCK) || (errno == EAGAIN)) |
97e6ee04 | 938 | { |
09e6e5ec | 939 | m_error = GSOCK_WOULDBLOCK; |
97e6ee04 DE |
940 | GSocket_Debug(( "GSocket_Write error WOULDBLOCK\n" )); |
941 | } | |
942 | else | |
943 | { | |
09e6e5ec | 944 | m_error = GSOCK_IOERR; |
97e6ee04 DE |
945 | GSocket_Debug(( "GSocket_Write error IOERR\n" )); |
946 | } | |
947 | ||
948 | /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect | |
949 | * in MSW). Once the first OUTPUT event is received, users can assume | |
950 | * that the socket is writable until a read operation fails. Only then | |
951 | * will further OUTPUT events be posted. | |
952 | */ | |
09e6e5ec | 953 | Enable(GSOCK_OUTPUT); |
97e6ee04 DE |
954 | return -1; |
955 | } | |
b082b524 | 956 | |
97e6ee04 DE |
957 | GSocket_Debug(( "GSocket_Write #5, size %d ret %d\n", size, ret )); |
958 | ||
959 | return ret; | |
960 | } | |
961 | ||
962 | /* GSocket_Select: | |
963 | * Polls the socket to determine its status. This function will | |
964 | * check for the events specified in the 'flags' parameter, and | |
965 | * it will return a mask indicating which operations can be | |
966 | * performed. This function won't block, regardless of the | |
967 | * mode (blocking | nonblocking) of the socket. | |
968 | */ | |
ba2a81d7 | 969 | GSocketEventFlags GSocket::Select(GSocketEventFlags flags) |
97e6ee04 | 970 | { |
ba2a81d7 | 971 | if (!gs_gui_functions->CanUseEventLoop()) |
97e6ee04 DE |
972 | { |
973 | ||
974 | GSocketEventFlags result = 0; | |
975 | fd_set readfds; | |
976 | fd_set writefds; | |
977 | fd_set exceptfds; | |
978 | struct timeval tv; | |
979 | ||
9d715960 DE |
980 | assert(this); |
981 | ||
60b0bd1d JS |
982 | if (m_fd == -1) |
983 | return (GSOCK_LOST_FLAG & flags); | |
17a1ebd1 | 984 | |
97e6ee04 | 985 | /* Do not use a static struct, Linux can garble it */ |
09e6e5ec | 986 | tv.tv_sec = m_timeout / 1000; |
7488a05e | 987 | tv.tv_usec = (m_timeout % 1000) * 1000; |
97e6ee04 | 988 | |
17a1ebd1 VZ |
989 | wxFD_ZERO(&readfds); |
990 | wxFD_ZERO(&writefds); | |
991 | wxFD_ZERO(&exceptfds); | |
992 | wxFD_SET(m_fd, &readfds); | |
9d715960 | 993 | if (flags & GSOCK_OUTPUT_FLAG || flags & GSOCK_CONNECTION_FLAG) |
17a1ebd1 VZ |
994 | wxFD_SET(m_fd, &writefds); |
995 | wxFD_SET(m_fd, &exceptfds); | |
97e6ee04 DE |
996 | |
997 | /* Check 'sticky' CONNECTION flag first */ | |
09e6e5ec | 998 | result |= (GSOCK_CONNECTION_FLAG & m_detected); |
97e6ee04 DE |
999 | |
1000 | /* If we have already detected a LOST event, then don't try | |
1001 | * to do any further processing. | |
1002 | */ | |
09e6e5ec | 1003 | if ((m_detected & GSOCK_LOST_FLAG) != 0) |
97e6ee04 | 1004 | { |
948c96ef | 1005 | m_establishing = false; |
97e6ee04 DE |
1006 | |
1007 | return (GSOCK_LOST_FLAG & flags); | |
1008 | } | |
1009 | ||
1010 | /* Try select now */ | |
09e6e5ec | 1011 | if (select(m_fd + 1, &readfds, &writefds, &exceptfds, &tv) <= 0) |
97e6ee04 DE |
1012 | { |
1013 | /* What to do here? */ | |
1014 | return (result & flags); | |
1015 | } | |
1016 | ||
1017 | /* Check for readability */ | |
17a1ebd1 | 1018 | if (wxFD_ISSET(m_fd, &readfds)) |
97e6ee04 DE |
1019 | { |
1020 | char c; | |
1021 | ||
bb154f79 KH |
1022 | int num = recv(m_fd, &c, 1, MSG_PEEK | GSOCKET_MSG_NOSIGNAL); |
1023 | ||
1024 | if (num > 0) | |
97e6ee04 DE |
1025 | { |
1026 | result |= GSOCK_INPUT_FLAG; | |
1027 | } | |
1028 | else | |
1029 | { | |
09e6e5ec | 1030 | if (m_server && m_stream) |
97e6ee04 DE |
1031 | { |
1032 | result |= GSOCK_CONNECTION_FLAG; | |
09e6e5ec | 1033 | m_detected |= GSOCK_CONNECTION_FLAG; |
97e6ee04 | 1034 | } |
9b67181b KH |
1035 | /* If recv returned zero, then the connection is lost, and errno is not set. |
1036 | * Otherwise, recv has returned an error (-1), in which case we have lost the | |
1037 | * socket only if errno does _not_ indicate that there may be more data to read. | |
1038 | */ | |
1039 | else if (num == 0 || | |
1040 | (errno != EWOULDBLOCK) && (errno != EAGAIN) && (errno != EINTR)) | |
97e6ee04 | 1041 | { |
09e6e5ec | 1042 | m_detected = GSOCK_LOST_FLAG; |
948c96ef | 1043 | m_establishing = false; |
b082b524 | 1044 | |
97e6ee04 DE |
1045 | /* LOST event: Abort any further processing */ |
1046 | return (GSOCK_LOST_FLAG & flags); | |
1047 | } | |
1048 | } | |
1049 | } | |
1050 | ||
1051 | /* Check for writability */ | |
17a1ebd1 | 1052 | if (wxFD_ISSET(m_fd, &writefds)) |
97e6ee04 | 1053 | { |
09e6e5ec | 1054 | if (m_establishing && !m_server) |
97e6ee04 DE |
1055 | { |
1056 | int error; | |
ddc1a35f | 1057 | SOCKOPTLEN_T len = sizeof(error); |
97e6ee04 | 1058 | |
948c96ef | 1059 | m_establishing = false; |
97e6ee04 | 1060 | |
f71bb8ef | 1061 | getsockopt(m_fd, SOL_SOCKET, SO_ERROR, (char*)&error, &len); |
97e6ee04 DE |
1062 | |
1063 | if (error) | |
1064 | { | |
09e6e5ec | 1065 | m_detected = GSOCK_LOST_FLAG; |
97e6ee04 DE |
1066 | |
1067 | /* LOST event: Abort any further processing */ | |
1068 | return (GSOCK_LOST_FLAG & flags); | |
1069 | } | |
1070 | else | |
1071 | { | |
1072 | result |= GSOCK_CONNECTION_FLAG; | |
09e6e5ec | 1073 | m_detected |= GSOCK_CONNECTION_FLAG; |
97e6ee04 DE |
1074 | } |
1075 | } | |
1076 | else | |
1077 | { | |
1078 | result |= GSOCK_OUTPUT_FLAG; | |
1079 | } | |
1080 | } | |
1081 | ||
1082 | /* Check for exceptions and errors (is this useful in Unices?) */ | |
17a1ebd1 | 1083 | if (wxFD_ISSET(m_fd, &exceptfds)) |
97e6ee04 | 1084 | { |
948c96ef | 1085 | m_establishing = false; |
09e6e5ec | 1086 | m_detected = GSOCK_LOST_FLAG; |
97e6ee04 DE |
1087 | |
1088 | /* LOST event: Abort any further processing */ | |
1089 | return (GSOCK_LOST_FLAG & flags); | |
1090 | } | |
1091 | ||
1092 | return (result & flags); | |
1093 | ||
1094 | } | |
1095 | else | |
1096 | { | |
1097 | ||
09e6e5ec DE |
1098 | assert(this); |
1099 | return flags & m_detected; | |
97e6ee04 DE |
1100 | |
1101 | } | |
1102 | } | |
1103 | ||
1104 | /* Flags */ | |
1105 | ||
1106 | /* GSocket_SetNonBlocking: | |
1107 | * Sets the socket to non-blocking mode. All IO calls will return | |
1108 | * immediately. | |
1109 | */ | |
948c96ef | 1110 | void GSocket::SetNonBlocking(bool non_block) |
97e6ee04 | 1111 | { |
09e6e5ec | 1112 | assert(this); |
97e6ee04 DE |
1113 | |
1114 | GSocket_Debug( ("GSocket_SetNonBlocking: %d\n", (int)non_block) ); | |
1115 | ||
09e6e5ec | 1116 | m_non_blocking = non_block; |
97e6ee04 DE |
1117 | } |
1118 | ||
1119 | /* GSocket_SetTimeout: | |
1120 | * Sets the timeout for blocking calls. Time is expressed in | |
1121 | * milliseconds. | |
1122 | */ | |
ba2a81d7 | 1123 | void GSocket::SetTimeout(unsigned long millisec) |
97e6ee04 | 1124 | { |
09e6e5ec | 1125 | assert(this); |
97e6ee04 | 1126 | |
09e6e5ec | 1127 | m_timeout = millisec; |
97e6ee04 DE |
1128 | } |
1129 | ||
1130 | /* GSocket_GetError: | |
d6922577 | 1131 | * Returns the last error occurred for this socket. Note that successful |
97e6ee04 DE |
1132 | * operations do not clear this back to GSOCK_NOERROR, so use it only |
1133 | * after an error. | |
1134 | */ | |
ba2a81d7 | 1135 | GSocketError WXDLLIMPEXP_NET GSocket::GetError() |
97e6ee04 | 1136 | { |
09e6e5ec | 1137 | assert(this); |
97e6ee04 | 1138 | |
09e6e5ec | 1139 | return m_error; |
97e6ee04 DE |
1140 | } |
1141 | ||
1142 | /* Callbacks */ | |
1143 | ||
1144 | /* GSOCK_INPUT: | |
1145 | * There is data to be read in the input buffer. If, after a read | |
1146 | * operation, there is still data available, the callback function will | |
1147 | * be called again. | |
1148 | * GSOCK_OUTPUT: | |
b082b524 | 1149 | * The socket is available for writing. That is, the next write call |
97e6ee04 DE |
1150 | * won't block. This event is generated only once, when the connection is |
1151 | * first established, and then only if a call failed with GSOCK_WOULDBLOCK, | |
1152 | * when the output buffer empties again. This means that the app should | |
1153 | * assume that it can write since the first OUTPUT event, and no more | |
1154 | * OUTPUT events will be generated unless an error occurs. | |
1155 | * GSOCK_CONNECTION: | |
d6922577 | 1156 | * Connection successfully established, for client sockets, or incoming |
97e6ee04 DE |
1157 | * client connection, for server sockets. Wait for this event (also watch |
1158 | * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call. | |
1159 | * GSOCK_LOST: | |
1160 | * The connection is lost (or a connection request failed); this could | |
1161 | * be due to a failure, or due to the peer closing it gracefully. | |
1162 | */ | |
1163 | ||
1164 | /* GSocket_SetCallback: | |
1165 | * Enables the callbacks specified by 'flags'. Note that 'flags' | |
1166 | * may be a combination of flags OR'ed toghether, so the same | |
1167 | * callback function can be made to accept different events. | |
1168 | * The callback function must have the following prototype: | |
1169 | * | |
1170 | * void function(GSocket *socket, GSocketEvent event, char *cdata) | |
1171 | */ | |
ba2a81d7 | 1172 | void GSocket::SetCallback(GSocketEventFlags flags, |
97e6ee04 DE |
1173 | GSocketCallback callback, char *cdata) |
1174 | { | |
1175 | int count; | |
1176 | ||
09e6e5ec | 1177 | assert(this); |
97e6ee04 DE |
1178 | |
1179 | for (count = 0; count < GSOCK_MAX_EVENT; count++) | |
1180 | { | |
1181 | if ((flags & (1 << count)) != 0) | |
1182 | { | |
09e6e5ec DE |
1183 | m_cbacks[count] = callback; |
1184 | m_data[count] = cdata; | |
97e6ee04 DE |
1185 | } |
1186 | } | |
1187 | } | |
1188 | ||
1189 | /* GSocket_UnsetCallback: | |
1190 | * Disables all callbacks specified by 'flags', which may be a | |
1191 | * combination of flags OR'ed toghether. | |
1192 | */ | |
ba2a81d7 | 1193 | void GSocket::UnsetCallback(GSocketEventFlags flags) |
97e6ee04 DE |
1194 | { |
1195 | int count; | |
1196 | ||
09e6e5ec | 1197 | assert(this); |
97e6ee04 DE |
1198 | |
1199 | for (count = 0; count < GSOCK_MAX_EVENT; count++) | |
1200 | { | |
1201 | if ((flags & (1 << count)) != 0) | |
1202 | { | |
09e6e5ec DE |
1203 | m_cbacks[count] = NULL; |
1204 | m_data[count] = NULL; | |
97e6ee04 DE |
1205 | } |
1206 | } | |
1207 | } | |
1208 | ||
ba2a81d7 | 1209 | GSocketError GSocket::GetSockOpt(int level, int optname, |
b082b524 DE |
1210 | void *optval, int *optlen) |
1211 | { | |
ddc1a35f | 1212 | if (getsockopt(m_fd, level, optname, (char*)optval, (SOCKOPTLEN_T*)optlen) == 0) |
b082b524 DE |
1213 | { |
1214 | return GSOCK_NOERROR; | |
1215 | } | |
1216 | return GSOCK_OPTERR; | |
1217 | } | |
1218 | ||
ba2a81d7 | 1219 | GSocketError GSocket::SetSockOpt(int level, int optname, |
b082b524 DE |
1220 | const void *optval, int optlen) |
1221 | { | |
f71bb8ef | 1222 | if (setsockopt(m_fd, level, optname, (const char*)optval, optlen) == 0) |
b082b524 DE |
1223 | { |
1224 | return GSOCK_NOERROR; | |
1225 | } | |
1226 | return GSOCK_OPTERR; | |
1227 | } | |
1228 | ||
ba2a81d7 | 1229 | #define CALL_CALLBACK(socket, event) { \ |
9a7e4a56 | 1230 | socket->Disable(event); \ |
ba2a81d7 DE |
1231 | if (socket->m_cbacks[event]) \ |
1232 | socket->m_cbacks[event](socket, event, socket->m_data[event]); \ | |
97e6ee04 DE |
1233 | } |
1234 | ||
1235 | ||
ba2a81d7 | 1236 | void GSocket::Enable(GSocketEvent event) |
97e6ee04 | 1237 | { |
09e6e5ec | 1238 | m_detected &= ~(1 << event); |
ba2a81d7 | 1239 | gs_gui_functions->Install_Callback(this, event); |
97e6ee04 DE |
1240 | } |
1241 | ||
ba2a81d7 | 1242 | void GSocket::Disable(GSocketEvent event) |
97e6ee04 | 1243 | { |
09e6e5ec | 1244 | m_detected |= (1 << event); |
ba2a81d7 | 1245 | gs_gui_functions->Uninstall_Callback(this, event); |
97e6ee04 DE |
1246 | } |
1247 | ||
1248 | /* _GSocket_Input_Timeout: | |
1249 | * For blocking sockets, wait until data is available or | |
1250 | * until timeout ellapses. | |
1251 | */ | |
ba2a81d7 | 1252 | GSocketError GSocket::Input_Timeout() |
97e6ee04 DE |
1253 | { |
1254 | struct timeval tv; | |
1255 | fd_set readfds; | |
1256 | int ret; | |
1257 | ||
1258 | /* Linux select() will overwrite the struct on return */ | |
09e6e5ec DE |
1259 | tv.tv_sec = (m_timeout / 1000); |
1260 | tv.tv_usec = (m_timeout % 1000) * 1000; | |
97e6ee04 | 1261 | |
09e6e5ec | 1262 | if (!m_non_blocking) |
97e6ee04 | 1263 | { |
17a1ebd1 VZ |
1264 | wxFD_ZERO(&readfds); |
1265 | wxFD_SET(m_fd, &readfds); | |
09e6e5ec | 1266 | ret = select(m_fd + 1, &readfds, NULL, NULL, &tv); |
97e6ee04 DE |
1267 | if (ret == 0) |
1268 | { | |
1269 | GSocket_Debug(( "GSocket_Input_Timeout, select returned 0\n" )); | |
09e6e5ec | 1270 | m_error = GSOCK_TIMEDOUT; |
97e6ee04 DE |
1271 | return GSOCK_TIMEDOUT; |
1272 | } | |
1273 | if (ret == -1) | |
1274 | { | |
1275 | GSocket_Debug(( "GSocket_Input_Timeout, select returned -1\n" )); | |
1276 | if (errno == EBADF) { GSocket_Debug(( "Invalid file descriptor\n" )); } | |
1277 | if (errno == EINTR) { GSocket_Debug(( "A non blocked signal was caught\n" )); } | |
1278 | if (errno == EINVAL) { GSocket_Debug(( "The highest number descriptor is negative\n" )); } | |
1279 | if (errno == ENOMEM) { GSocket_Debug(( "Not enough memory\n" )); } | |
09e6e5ec | 1280 | m_error = GSOCK_TIMEDOUT; |
97e6ee04 DE |
1281 | return GSOCK_TIMEDOUT; |
1282 | } | |
1283 | } | |
1284 | return GSOCK_NOERROR; | |
1285 | } | |
1286 | ||
1287 | /* _GSocket_Output_Timeout: | |
1288 | * For blocking sockets, wait until data can be sent without | |
1289 | * blocking or until timeout ellapses. | |
1290 | */ | |
ba2a81d7 | 1291 | GSocketError GSocket::Output_Timeout() |
97e6ee04 DE |
1292 | { |
1293 | struct timeval tv; | |
1294 | fd_set writefds; | |
1295 | int ret; | |
1296 | ||
1297 | /* Linux select() will overwrite the struct on return */ | |
09e6e5ec DE |
1298 | tv.tv_sec = (m_timeout / 1000); |
1299 | tv.tv_usec = (m_timeout % 1000) * 1000; | |
97e6ee04 | 1300 | |
09e6e5ec | 1301 | GSocket_Debug( ("m_non_blocking has: %d\n", (int)m_non_blocking) ); |
97e6ee04 | 1302 | |
09e6e5ec | 1303 | if (!m_non_blocking) |
97e6ee04 | 1304 | { |
17a1ebd1 VZ |
1305 | wxFD_ZERO(&writefds); |
1306 | wxFD_SET(m_fd, &writefds); | |
09e6e5ec | 1307 | ret = select(m_fd + 1, NULL, &writefds, NULL, &tv); |
97e6ee04 DE |
1308 | if (ret == 0) |
1309 | { | |
1310 | GSocket_Debug(( "GSocket_Output_Timeout, select returned 0\n" )); | |
09e6e5ec | 1311 | m_error = GSOCK_TIMEDOUT; |
97e6ee04 DE |
1312 | return GSOCK_TIMEDOUT; |
1313 | } | |
1314 | if (ret == -1) | |
1315 | { | |
1316 | GSocket_Debug(( "GSocket_Output_Timeout, select returned -1\n" )); | |
1317 | if (errno == EBADF) { GSocket_Debug(( "Invalid file descriptor\n" )); } | |
1318 | if (errno == EINTR) { GSocket_Debug(( "A non blocked signal was caught\n" )); } | |
1319 | if (errno == EINVAL) { GSocket_Debug(( "The highest number descriptor is negative\n" )); } | |
1320 | if (errno == ENOMEM) { GSocket_Debug(( "Not enough memory\n" )); } | |
09e6e5ec | 1321 | m_error = GSOCK_TIMEDOUT; |
97e6ee04 DE |
1322 | return GSOCK_TIMEDOUT; |
1323 | } | |
17a1ebd1 | 1324 | if ( ! wxFD_ISSET(m_fd, &writefds) ) { |
97e6ee04 DE |
1325 | GSocket_Debug(( "GSocket_Output_Timeout is buggy!\n" )); |
1326 | } | |
1327 | else { | |
1328 | GSocket_Debug(( "GSocket_Output_Timeout seems correct\n" )); | |
1329 | } | |
1330 | } | |
1331 | else | |
1332 | { | |
1333 | GSocket_Debug(( "GSocket_Output_Timeout, didn't try select!\n" )); | |
1334 | } | |
1335 | ||
1336 | return GSOCK_NOERROR; | |
1337 | } | |
1338 | ||
ba2a81d7 | 1339 | int GSocket::Recv_Stream(char *buffer, int size) |
97e6ee04 | 1340 | { |
bb154f79 | 1341 | int ret; |
7e1e6965 | 1342 | do |
bb154f79 KH |
1343 | { |
1344 | ret = recv(m_fd, buffer, size, GSOCKET_MSG_NOSIGNAL); | |
e400d27d | 1345 | } while (ret == -1 && errno == EINTR); /* Loop until not interrupted */ |
bb154f79 | 1346 | return ret; |
97e6ee04 DE |
1347 | } |
1348 | ||
ba2a81d7 | 1349 | int GSocket::Recv_Dgram(char *buffer, int size) |
97e6ee04 DE |
1350 | { |
1351 | struct sockaddr from; | |
9e03e02d | 1352 | WX_SOCKLEN_T fromlen = sizeof(from); |
97e6ee04 DE |
1353 | int ret; |
1354 | GSocketError err; | |
1355 | ||
1356 | fromlen = sizeof(from); | |
1357 | ||
7e1e6965 | 1358 | do |
bb154f79 | 1359 | { |
9e03e02d | 1360 | ret = recvfrom(m_fd, buffer, size, 0, &from, (WX_SOCKLEN_T *) &fromlen); |
e400d27d | 1361 | } while (ret == -1 && errno == EINTR); /* Loop until not interrupted */ |
97e6ee04 DE |
1362 | |
1363 | if (ret == -1) | |
1364 | return -1; | |
1365 | ||
1366 | /* Translate a system address into a GSocket address */ | |
09e6e5ec | 1367 | if (!m_peer) |
97e6ee04 | 1368 | { |
09e6e5ec DE |
1369 | m_peer = GAddress_new(); |
1370 | if (!m_peer) | |
97e6ee04 | 1371 | { |
09e6e5ec | 1372 | m_error = GSOCK_MEMERR; |
97e6ee04 DE |
1373 | return -1; |
1374 | } | |
1375 | } | |
09e6e5ec | 1376 | err = _GAddress_translate_from(m_peer, &from, fromlen); |
97e6ee04 DE |
1377 | if (err != GSOCK_NOERROR) |
1378 | { | |
09e6e5ec DE |
1379 | GAddress_destroy(m_peer); |
1380 | m_peer = NULL; | |
1381 | m_error = err; | |
97e6ee04 DE |
1382 | return -1; |
1383 | } | |
1384 | ||
1385 | return ret; | |
1386 | } | |
1387 | ||
ba2a81d7 | 1388 | int GSocket::Send_Stream(const char *buffer, int size) |
97e6ee04 DE |
1389 | { |
1390 | int ret; | |
1391 | ||
7e1e6965 WS |
1392 | MASK_SIGNAL(); |
1393 | ||
1394 | do | |
bb154f79 KH |
1395 | { |
1396 | ret = send(m_fd, (char *)buffer, size, GSOCKET_MSG_NOSIGNAL); | |
e400d27d | 1397 | } while (ret == -1 && errno == EINTR); /* Loop until not interrupted */ |
7e1e6965 | 1398 | |
97e6ee04 | 1399 | UNMASK_SIGNAL(); |
97e6ee04 DE |
1400 | |
1401 | return ret; | |
1402 | } | |
1403 | ||
ba2a81d7 | 1404 | int GSocket::Send_Dgram(const char *buffer, int size) |
97e6ee04 DE |
1405 | { |
1406 | struct sockaddr *addr; | |
1407 | int len, ret; | |
1408 | GSocketError err; | |
1409 | ||
09e6e5ec | 1410 | if (!m_peer) |
97e6ee04 | 1411 | { |
09e6e5ec | 1412 | m_error = GSOCK_INVADDR; |
97e6ee04 DE |
1413 | return -1; |
1414 | } | |
1415 | ||
09e6e5ec | 1416 | err = _GAddress_translate_to(m_peer, &addr, &len); |
97e6ee04 DE |
1417 | if (err != GSOCK_NOERROR) |
1418 | { | |
09e6e5ec | 1419 | m_error = err; |
97e6ee04 DE |
1420 | return -1; |
1421 | } | |
1422 | ||
97e6ee04 | 1423 | MASK_SIGNAL(); |
7e1e6965 WS |
1424 | |
1425 | do | |
bb154f79 KH |
1426 | { |
1427 | ret = sendto(m_fd, (char *)buffer, size, 0, addr, len); | |
e400d27d | 1428 | } while (ret == -1 && errno == EINTR); /* Loop until not interrupted */ |
7e1e6965 | 1429 | |
97e6ee04 | 1430 | UNMASK_SIGNAL(); |
97e6ee04 DE |
1431 | |
1432 | /* Frees memory allocated from _GAddress_translate_to */ | |
1433 | free(addr); | |
1434 | ||
1435 | return ret; | |
1436 | } | |
1437 | ||
ba2a81d7 | 1438 | void GSocket::Detected_Read() |
97e6ee04 DE |
1439 | { |
1440 | char c; | |
1441 | ||
bb154f79 KH |
1442 | /* Safeguard against straggling call to Detected_Read */ |
1443 | if (m_fd == INVALID_SOCKET) | |
1444 | { | |
1445 | return; | |
1446 | } | |
1447 | ||
97e6ee04 DE |
1448 | /* If we have already detected a LOST event, then don't try |
1449 | * to do any further processing. | |
1450 | */ | |
09e6e5ec | 1451 | if ((m_detected & GSOCK_LOST_FLAG) != 0) |
97e6ee04 | 1452 | { |
948c96ef | 1453 | m_establishing = false; |
97e6ee04 | 1454 | |
ba2a81d7 | 1455 | CALL_CALLBACK(this, GSOCK_LOST); |
09e6e5ec | 1456 | Shutdown(); |
97e6ee04 DE |
1457 | return; |
1458 | } | |
1459 | ||
bb154f79 KH |
1460 | int num = recv(m_fd, &c, 1, MSG_PEEK | GSOCKET_MSG_NOSIGNAL); |
1461 | ||
1462 | if (num > 0) | |
97e6ee04 | 1463 | { |
ba2a81d7 | 1464 | CALL_CALLBACK(this, GSOCK_INPUT); |
97e6ee04 DE |
1465 | } |
1466 | else | |
1467 | { | |
09e6e5ec | 1468 | if (m_server && m_stream) |
97e6ee04 | 1469 | { |
ba2a81d7 | 1470 | CALL_CALLBACK(this, GSOCK_CONNECTION); |
97e6ee04 DE |
1471 | } |
1472 | else | |
1473 | { | |
e400d27d | 1474 | /* Do not throw a lost event in cases where the socket isn't really lost */ |
7e1e6965 | 1475 | if ((errno == EWOULDBLOCK) || (errno == EAGAIN) || (errno == EINTR)) |
bb154f79 KH |
1476 | { |
1477 | CALL_CALLBACK(this, GSOCK_INPUT); | |
1478 | } | |
7e1e6965 | 1479 | else |
bb154f79 KH |
1480 | { |
1481 | CALL_CALLBACK(this, GSOCK_LOST); | |
1482 | Shutdown(); | |
7e1e6965 | 1483 | } |
97e6ee04 DE |
1484 | } |
1485 | } | |
1486 | } | |
1487 | ||
ba2a81d7 | 1488 | void GSocket::Detected_Write() |
97e6ee04 DE |
1489 | { |
1490 | /* If we have already detected a LOST event, then don't try | |
1491 | * to do any further processing. | |
1492 | */ | |
09e6e5ec | 1493 | if ((m_detected & GSOCK_LOST_FLAG) != 0) |
97e6ee04 | 1494 | { |
948c96ef | 1495 | m_establishing = false; |
97e6ee04 | 1496 | |
ba2a81d7 | 1497 | CALL_CALLBACK(this, GSOCK_LOST); |
09e6e5ec | 1498 | Shutdown(); |
97e6ee04 DE |
1499 | return; |
1500 | } | |
1501 | ||
09e6e5ec | 1502 | if (m_establishing && !m_server) |
97e6ee04 DE |
1503 | { |
1504 | int error; | |
ddc1a35f | 1505 | SOCKOPTLEN_T len = sizeof(error); |
97e6ee04 | 1506 | |
948c96ef | 1507 | m_establishing = false; |
97e6ee04 | 1508 | |
f71bb8ef | 1509 | getsockopt(m_fd, SOL_SOCKET, SO_ERROR, (char*)&error, &len); |
97e6ee04 DE |
1510 | |
1511 | if (error) | |
1512 | { | |
ba2a81d7 | 1513 | CALL_CALLBACK(this, GSOCK_LOST); |
09e6e5ec | 1514 | Shutdown(); |
97e6ee04 DE |
1515 | } |
1516 | else | |
1517 | { | |
ba2a81d7 | 1518 | CALL_CALLBACK(this, GSOCK_CONNECTION); |
97e6ee04 DE |
1519 | /* We have to fire this event by hand because CONNECTION (for clients) |
1520 | * and OUTPUT are internally the same and we just disabled CONNECTION | |
1521 | * events with the above macro. | |
1522 | */ | |
ba2a81d7 | 1523 | CALL_CALLBACK(this, GSOCK_OUTPUT); |
97e6ee04 DE |
1524 | } |
1525 | } | |
1526 | else | |
1527 | { | |
ba2a81d7 | 1528 | CALL_CALLBACK(this, GSOCK_OUTPUT); |
97e6ee04 DE |
1529 | } |
1530 | } | |
1531 | ||
09e6e5ec DE |
1532 | /* Compatibility functions for GSocket */ |
1533 | GSocket *GSocket_new(void) | |
1534 | { | |
ba2a81d7 | 1535 | GSocket *newsocket = new GSocket(); |
09e6e5ec DE |
1536 | if(newsocket->IsOk()) |
1537 | return newsocket; | |
1538 | delete newsocket; | |
1539 | return NULL; | |
1540 | } | |
1541 | ||
97e6ee04 DE |
1542 | /* |
1543 | * ------------------------------------------------------------------------- | |
1544 | * GAddress | |
1545 | * ------------------------------------------------------------------------- | |
1546 | */ | |
1547 | ||
1548 | /* CHECK_ADDRESS verifies that the current address family is either | |
1549 | * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it | |
1550 | * initalizes it to be a GSOCK_*family*. In other cases, it returns | |
1551 | * an appropiate error code. | |
1552 | * | |
1553 | * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error. | |
1554 | */ | |
1555 | #define CHECK_ADDRESS(address, family) \ | |
1556 | { \ | |
1557 | if (address->m_family == GSOCK_NOFAMILY) \ | |
1558 | if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \ | |
1559 | return address->m_error; \ | |
1560 | if (address->m_family != GSOCK_##family) \ | |
1561 | { \ | |
1562 | address->m_error = GSOCK_INVADDR; \ | |
1563 | return GSOCK_INVADDR; \ | |
1564 | } \ | |
1565 | } | |
1566 | ||
1567 | #define CHECK_ADDRESS_RETVAL(address, family, retval) \ | |
1568 | { \ | |
1569 | if (address->m_family == GSOCK_NOFAMILY) \ | |
1570 | if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \ | |
1571 | return retval; \ | |
1572 | if (address->m_family != GSOCK_##family) \ | |
1573 | { \ | |
1574 | address->m_error = GSOCK_INVADDR; \ | |
1575 | return retval; \ | |
1576 | } \ | |
1577 | } | |
1578 | ||
1579 | ||
1580 | GAddress *GAddress_new(void) | |
1581 | { | |
1582 | GAddress *address; | |
1583 | ||
1584 | if ((address = (GAddress *) malloc(sizeof(GAddress))) == NULL) | |
1585 | return NULL; | |
1586 | ||
1587 | address->m_family = GSOCK_NOFAMILY; | |
1588 | address->m_addr = NULL; | |
1589 | address->m_len = 0; | |
1590 | ||
1591 | return address; | |
1592 | } | |
1593 | ||
1594 | GAddress *GAddress_copy(GAddress *address) | |
1595 | { | |
1596 | GAddress *addr2; | |
1597 | ||
1598 | assert(address != NULL); | |
1599 | ||
1600 | if ((addr2 = (GAddress *) malloc(sizeof(GAddress))) == NULL) | |
1601 | return NULL; | |
1602 | ||
1603 | memcpy(addr2, address, sizeof(GAddress)); | |
1604 | ||
1605 | if (address->m_addr && address->m_len > 0) | |
1606 | { | |
1607 | addr2->m_addr = (struct sockaddr *)malloc(addr2->m_len); | |
1608 | if (addr2->m_addr == NULL) | |
1609 | { | |
1610 | free(addr2); | |
1611 | return NULL; | |
1612 | } | |
1613 | memcpy(addr2->m_addr, address->m_addr, addr2->m_len); | |
1614 | } | |
1615 | ||
1616 | return addr2; | |
1617 | } | |
1618 | ||
1619 | void GAddress_destroy(GAddress *address) | |
1620 | { | |
1621 | assert(address != NULL); | |
1622 | ||
1623 | if (address->m_addr) | |
1624 | free(address->m_addr); | |
1625 | ||
1626 | free(address); | |
1627 | } | |
1628 | ||
1629 | void GAddress_SetFamily(GAddress *address, GAddressType type) | |
1630 | { | |
1631 | assert(address != NULL); | |
1632 | ||
1633 | address->m_family = type; | |
1634 | } | |
1635 | ||
1636 | GAddressType GAddress_GetFamily(GAddress *address) | |
1637 | { | |
1638 | assert(address != NULL); | |
1639 | ||
1640 | return address->m_family; | |
1641 | } | |
1642 | ||
1643 | GSocketError _GAddress_translate_from(GAddress *address, | |
1644 | struct sockaddr *addr, int len) | |
1645 | { | |
1646 | address->m_realfamily = addr->sa_family; | |
1647 | switch (addr->sa_family) | |
1648 | { | |
1649 | case AF_INET: | |
1650 | address->m_family = GSOCK_INET; | |
1651 | break; | |
1652 | case AF_UNIX: | |
1653 | address->m_family = GSOCK_UNIX; | |
1654 | break; | |
1655 | #ifdef AF_INET6 | |
1656 | case AF_INET6: | |
1657 | address->m_family = GSOCK_INET6; | |
1658 | break; | |
1659 | #endif | |
1660 | default: | |
1661 | { | |
1662 | address->m_error = GSOCK_INVOP; | |
1663 | return GSOCK_INVOP; | |
1664 | } | |
1665 | } | |
1666 | ||
1667 | if (address->m_addr) | |
1668 | free(address->m_addr); | |
1669 | ||
1670 | address->m_len = len; | |
1671 | address->m_addr = (struct sockaddr *)malloc(len); | |
1672 | ||
1673 | if (address->m_addr == NULL) | |
1674 | { | |
1675 | address->m_error = GSOCK_MEMERR; | |
1676 | return GSOCK_MEMERR; | |
1677 | } | |
1678 | memcpy(address->m_addr, addr, len); | |
1679 | ||
1680 | return GSOCK_NOERROR; | |
1681 | } | |
1682 | ||
1683 | GSocketError _GAddress_translate_to(GAddress *address, | |
1684 | struct sockaddr **addr, int *len) | |
1685 | { | |
1686 | if (!address->m_addr) | |
1687 | { | |
1688 | address->m_error = GSOCK_INVADDR; | |
1689 | return GSOCK_INVADDR; | |
1690 | } | |
1691 | ||
1692 | *len = address->m_len; | |
1693 | *addr = (struct sockaddr *)malloc(address->m_len); | |
1694 | if (*addr == NULL) | |
1695 | { | |
1696 | address->m_error = GSOCK_MEMERR; | |
1697 | return GSOCK_MEMERR; | |
1698 | } | |
1699 | ||
1700 | memcpy(*addr, address->m_addr, address->m_len); | |
1701 | return GSOCK_NOERROR; | |
1702 | } | |
1703 | ||
1704 | /* | |
1705 | * ------------------------------------------------------------------------- | |
1706 | * Internet address family | |
1707 | * ------------------------------------------------------------------------- | |
1708 | */ | |
1709 | ||
1710 | GSocketError _GAddress_Init_INET(GAddress *address) | |
1711 | { | |
1712 | address->m_len = sizeof(struct sockaddr_in); | |
1713 | address->m_addr = (struct sockaddr *) malloc(address->m_len); | |
1714 | if (address->m_addr == NULL) | |
1715 | { | |
1716 | address->m_error = GSOCK_MEMERR; | |
1717 | return GSOCK_MEMERR; | |
1718 | } | |
1719 | ||
1720 | address->m_family = GSOCK_INET; | |
1721 | address->m_realfamily = PF_INET; | |
1722 | ((struct sockaddr_in *)address->m_addr)->sin_family = AF_INET; | |
1723 | ((struct sockaddr_in *)address->m_addr)->sin_addr.s_addr = INADDR_ANY; | |
1724 | ||
1725 | return GSOCK_NOERROR; | |
1726 | } | |
1727 | ||
1728 | GSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname) | |
1729 | { | |
1730 | struct hostent *he; | |
1731 | struct in_addr *addr; | |
1732 | ||
1733 | assert(address != NULL); | |
1734 | ||
1735 | CHECK_ADDRESS(address, INET); | |
1736 | ||
1737 | addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr); | |
1738 | ||
1739 | /* If it is a numeric host name, convert it now */ | |
1740 | #if defined(HAVE_INET_ATON) | |
1741 | if (inet_aton(hostname, addr) == 0) | |
1742 | { | |
1743 | #elif defined(HAVE_INET_ADDR) | |
8c0f8906 | 1744 | if ( (addr->s_addr = inet_addr(hostname)) == (unsigned)-1 ) |
97e6ee04 DE |
1745 | { |
1746 | #else | |
1747 | /* Use gethostbyname by default */ | |
9d715960 | 1748 | #ifndef __WXMAC__ |
ba2a81d7 | 1749 | int val = 1; /* VA doesn't like constants in conditional expressions */ |
97e6ee04 | 1750 | if (val) |
9d715960 | 1751 | #endif |
97e6ee04 DE |
1752 | { |
1753 | #endif | |
1754 | struct in_addr *array_addr; | |
1755 | ||
1756 | /* It is a real name, we solve it */ | |
1757 | if ((he = gethostbyname(hostname)) == NULL) | |
1758 | { | |
1759 | /* Reset to invalid address */ | |
1760 | addr->s_addr = INADDR_NONE; | |
1761 | address->m_error = GSOCK_NOHOST; | |
1762 | return GSOCK_NOHOST; | |
1763 | } | |
1764 | array_addr = (struct in_addr *) *(he->h_addr_list); | |
1765 | addr->s_addr = array_addr[0].s_addr; | |
1766 | } | |
1767 | return GSOCK_NOERROR; | |
1768 | } | |
1769 | ||
1770 | GSocketError GAddress_INET_SetAnyAddress(GAddress *address) | |
1771 | { | |
1772 | return GAddress_INET_SetHostAddress(address, INADDR_ANY); | |
1773 | } | |
1774 | ||
1775 | GSocketError GAddress_INET_SetHostAddress(GAddress *address, | |
1776 | unsigned long hostaddr) | |
1777 | { | |
1778 | struct in_addr *addr; | |
1779 | ||
1780 | assert(address != NULL); | |
1781 | ||
1782 | CHECK_ADDRESS(address, INET); | |
1783 | ||
1784 | addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr); | |
9d715960 | 1785 | addr->s_addr = htonl(hostaddr); |
97e6ee04 DE |
1786 | |
1787 | return GSOCK_NOERROR; | |
1788 | } | |
1789 | ||
1790 | GSocketError GAddress_INET_SetPortName(GAddress *address, const char *port, | |
1791 | const char *protocol) | |
1792 | { | |
1793 | struct servent *se; | |
1794 | struct sockaddr_in *addr; | |
1795 | ||
1796 | assert(address != NULL); | |
1797 | CHECK_ADDRESS(address, INET); | |
1798 | ||
1799 | if (!port) | |
1800 | { | |
1801 | address->m_error = GSOCK_INVPORT; | |
1802 | return GSOCK_INVPORT; | |
1803 | } | |
b082b524 | 1804 | |
f6bc1c74 DW |
1805 | #if defined(__WXPM__) && defined(__EMX__) |
1806 | se = getservbyname(port, (char*)protocol); | |
1807 | #else | |
97e6ee04 | 1808 | se = getservbyname(port, protocol); |
f6bc1c74 | 1809 | #endif |
97e6ee04 DE |
1810 | if (!se) |
1811 | { | |
1812 | /* the cast to int suppresses compiler warnings about subscript having the | |
1813 | type char */ | |
1814 | if (isdigit((int)port[0])) | |
1815 | { | |
1816 | int port_int; | |
1817 | ||
1818 | port_int = atoi(port); | |
1819 | addr = (struct sockaddr_in *)address->m_addr; | |
1820 | addr->sin_port = htons(port_int); | |
1821 | return GSOCK_NOERROR; | |
1822 | } | |
1823 | ||
1824 | address->m_error = GSOCK_INVPORT; | |
1825 | return GSOCK_INVPORT; | |
1826 | } | |
1827 | ||
1828 | addr = (struct sockaddr_in *)address->m_addr; | |
1829 | addr->sin_port = se->s_port; | |
1830 | ||
1831 | return GSOCK_NOERROR; | |
1832 | } | |
1833 | ||
1834 | GSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port) | |
1835 | { | |
1836 | struct sockaddr_in *addr; | |
1837 | ||
1838 | assert(address != NULL); | |
1839 | CHECK_ADDRESS(address, INET); | |
b082b524 | 1840 | |
97e6ee04 DE |
1841 | addr = (struct sockaddr_in *)address->m_addr; |
1842 | addr->sin_port = htons(port); | |
1843 | ||
1844 | return GSOCK_NOERROR; | |
1845 | } | |
1846 | ||
1847 | GSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname, size_t sbuf) | |
1848 | { | |
1849 | struct hostent *he; | |
1850 | char *addr_buf; | |
1851 | struct sockaddr_in *addr; | |
1852 | ||
b082b524 | 1853 | assert(address != NULL); |
97e6ee04 DE |
1854 | CHECK_ADDRESS(address, INET); |
1855 | ||
1856 | addr = (struct sockaddr_in *)address->m_addr; | |
1857 | addr_buf = (char *)&(addr->sin_addr); | |
1858 | ||
1859 | he = gethostbyaddr(addr_buf, sizeof(addr->sin_addr), AF_INET); | |
1860 | if (he == NULL) | |
1861 | { | |
1862 | address->m_error = GSOCK_NOHOST; | |
1863 | return GSOCK_NOHOST; | |
1864 | } | |
1865 | ||
1866 | strncpy(hostname, he->h_name, sbuf); | |
1867 | ||
1868 | return GSOCK_NOERROR; | |
1869 | } | |
1870 | ||
1871 | unsigned long GAddress_INET_GetHostAddress(GAddress *address) | |
1872 | { | |
1873 | struct sockaddr_in *addr; | |
1874 | ||
b082b524 DE |
1875 | assert(address != NULL); |
1876 | CHECK_ADDRESS_RETVAL(address, INET, 0); | |
97e6ee04 DE |
1877 | |
1878 | addr = (struct sockaddr_in *)address->m_addr; | |
1879 | ||
9d715960 | 1880 | return ntohl(addr->sin_addr.s_addr); |
97e6ee04 DE |
1881 | } |
1882 | ||
1883 | unsigned short GAddress_INET_GetPort(GAddress *address) | |
1884 | { | |
1885 | struct sockaddr_in *addr; | |
1886 | ||
b082b524 DE |
1887 | assert(address != NULL); |
1888 | CHECK_ADDRESS_RETVAL(address, INET, 0); | |
97e6ee04 DE |
1889 | |
1890 | addr = (struct sockaddr_in *)address->m_addr; | |
1891 | return ntohs(addr->sin_port); | |
1892 | } | |
1893 | ||
1894 | /* | |
1895 | * ------------------------------------------------------------------------- | |
1896 | * Unix address family | |
1897 | * ------------------------------------------------------------------------- | |
1898 | */ | |
1899 | ||
1900 | #ifndef __VISAGECPP__ | |
1901 | GSocketError _GAddress_Init_UNIX(GAddress *address) | |
1902 | { | |
1903 | address->m_len = sizeof(struct sockaddr_un); | |
1904 | address->m_addr = (struct sockaddr *)malloc(address->m_len); | |
1905 | if (address->m_addr == NULL) | |
1906 | { | |
1907 | address->m_error = GSOCK_MEMERR; | |
1908 | return GSOCK_MEMERR; | |
1909 | } | |
1910 | ||
1911 | address->m_family = GSOCK_UNIX; | |
1912 | address->m_realfamily = PF_UNIX; | |
1913 | ((struct sockaddr_un *)address->m_addr)->sun_family = AF_UNIX; | |
1914 | ((struct sockaddr_un *)address->m_addr)->sun_path[0] = 0; | |
1915 | ||
1916 | return GSOCK_NOERROR; | |
1917 | } | |
1918 | ||
1919 | #define UNIX_SOCK_PATHLEN (sizeof(addr->sun_path)/sizeof(addr->sun_path[0])) | |
1920 | ||
1921 | GSocketError GAddress_UNIX_SetPath(GAddress *address, const char *path) | |
1922 | { | |
1923 | struct sockaddr_un *addr; | |
1924 | ||
b082b524 | 1925 | assert(address != NULL); |
97e6ee04 | 1926 | |
b082b524 | 1927 | CHECK_ADDRESS(address, UNIX); |
97e6ee04 DE |
1928 | |
1929 | addr = ((struct sockaddr_un *)address->m_addr); | |
1930 | strncpy(addr->sun_path, path, UNIX_SOCK_PATHLEN); | |
1931 | addr->sun_path[UNIX_SOCK_PATHLEN - 1] = '\0'; | |
1932 | ||
1933 | return GSOCK_NOERROR; | |
1934 | } | |
1935 | ||
1936 | GSocketError GAddress_UNIX_GetPath(GAddress *address, char *path, size_t sbuf) | |
1937 | { | |
1938 | struct sockaddr_un *addr; | |
1939 | ||
1940 | assert(address != NULL); | |
1941 | CHECK_ADDRESS(address, UNIX); | |
1942 | ||
1943 | addr = (struct sockaddr_un *)address->m_addr; | |
1944 | ||
1945 | strncpy(path, addr->sun_path, sbuf); | |
1946 | ||
1947 | return GSOCK_NOERROR; | |
1948 | } | |
1949 | #endif /* !defined(__VISAGECPP__) */ | |
1950 | #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */ |