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