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