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