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