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