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