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