]> git.saurik.com Git - wxWidgets.git/blame - src/msw/gsocket.c
inform the IM context about focus changes
[wxWidgets.git] / src / msw / gsocket.c
CommitLineData
9bbd7ba3 1/* -------------------------------------------------------------------------
9bf10d6b 2 * Project: GSocket (Generic Socket)
9bbd7ba3 3 * Name: gsocket.c
9bf10d6b 4 * Author: Guillermo Rodriguez Garcia <guille@iies.es>
9bbd7ba3 5 * Purpose: GSocket main MSW file
77ffb593 6 * Licence: The wxWidgets licence
9bbd7ba3
GRG
7 * CVSID: $Id$
8 * -------------------------------------------------------------------------
9 */
10
9bf10d6b
GRG
11/*
12 * PLEASE don't put C++ comments here - this is a C source file.
13 */
14
8a9c2246 15#ifdef _MSC_VER
29c25a8e
GRG
16 /* RPCNOTIFICATION_ROUTINE in rasasync.h (included from winsock.h),
17 * warning: conditional expression is constant.
18 */
19# pragma warning(disable:4115)
20 /* FD_SET,
21 * warning: named type definition in parentheses.
22 */
23# pragma warning(disable:4127)
24 /* GAddress_UNIX_GetPath,
25 * warning: unreferenced formal parameter.
26 */
27# pragma warning(disable:4100)
8b0107e4
VZ
28
29#ifdef __WXWINCE__
991420e6
WS
30 /* windows.h results in tons of warnings at max warning level */
31# ifdef _MSC_VER
32# pragma warning(push, 1)
33# endif
34# include <windows.h>
35# ifdef _MSC_VER
36# pragma warning(pop)
37# pragma warning(disable:4514)
38# endif
39#endif
40
29c25a8e
GRG
41#endif /* _MSC_VER */
42
5283098e
JS
43#include <winsock.h>
44
2f7c2af5 45#ifndef __GSOCKET_STANDALONE__
a3f7fa62
VZ
46# include "wx/platform.h"
47# include "wx/setup.h"
0ce742cf
GRG
48#endif
49
50#if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
51
0ce742cf 52#ifndef __GSOCKET_STANDALONE__
ed2eb9af
GRG
53# include "wx/msw/gsockmsw.h"
54# include "wx/gsocket.h"
9bbd7ba3 55#else
ed2eb9af
GRG
56# include "gsockmsw.h"
57# include "gsocket.h"
58#endif /* __GSOCKET_STANDALONE__ */
9bbd7ba3 59
882dfc67 60#ifndef __WXWINCE__
9bbd7ba3 61#include <assert.h>
882dfc67
JS
62#else
63#define assert(x)
64#ifndef isdigit
65#define isdigit(x) (x > 47 && x < 58)
66#endif
67#include "wx/msw/wince/net.h"
68#endif
69
9bbd7ba3
GRG
70#include <string.h>
71#include <stdio.h>
72#include <stdlib.h>
73#include <stddef.h>
74#include <ctype.h>
8a9c2246 75
c42404a5
VZ
76/* if we use configure for MSW SOCKLEN_T will be already defined */
77#ifndef SOCKLEN_T
ed2eb9af 78# define SOCKLEN_T int
c42404a5
VZ
79#endif
80
38bb138f
VS
81/* Table of GUI-related functions. We must call them indirectly because
82 * of wxBase and GUI separation: */
83
84static struct GSocketGUIFunctionsTable *gs_gui_functions;
85
86#define USE_GUI() (gs_gui_functions != NULL)
87
88/* Define macros to simplify indirection: */
89#define _GSocket_GUI_Init() \
90 if (gs_gui_functions) gs_gui_functions->GUI_Init()
91#define _GSocket_GUI_Cleanup() \
92 if (gs_gui_functions) gs_gui_functions->GUI_Cleanup()
93#define _GSocket_GUI_Init_Socket(socket) \
94 (gs_gui_functions ? gs_gui_functions->GUI_Init_Socket(socket) : 1)
95#define _GSocket_GUI_Destroy_Socket(socket) \
96 if (gs_gui_functions) gs_gui_functions->GUI_Destroy_Socket(socket)
97#define _GSocket_Enable_Events(socket) \
98 if (gs_gui_functions) gs_gui_functions->Enable_Events(socket)
99#define _GSocket_Disable_Events(socket) \
100 if (gs_gui_functions) gs_gui_functions->Disable_Events(socket)
101#define _GSocket_Install_Callback(socket, event) \
102 if (gs_gui_functions) gs_gui_functions->Install_Callback(socket, event)
103#define _GSocket_Uninstall_Callback(socket, event) \
104 if (gs_gui_functions) gs_gui_functions->Uninstall_Callback(socket, event)
105
106/* Global initialisers */
107
108void GSocket_SetGUIFunctions(struct GSocketGUIFunctionsTable *guifunc)
109{
110 gs_gui_functions = guifunc;
111}
112
113int GSocket_Init(void)
114{
115 WSADATA wsaData;
116
117 if (gs_gui_functions)
118 {
119 if ( !gs_gui_functions->GUI_Init() )
120 return 0;
121 }
122
123 /* Initialize WinSocket */
124 return (WSAStartup((1 << 8) | 1, &wsaData) == 0);
125}
126
127void GSocket_Cleanup(void)
128{
129 if (gs_gui_functions)
130 {
131 gs_gui_functions->GUI_Cleanup();
132 }
133
134 /* Cleanup WinSocket */
135 WSACleanup();
136}
9bbd7ba3 137
9bf10d6b 138/* Constructors / Destructors for GSocket */
9bbd7ba3 139
da051b23 140GSocket *GSocket_new(void)
9bbd7ba3 141{
ed2eb9af 142 int i, success;
9bbd7ba3
GRG
143 GSocket *socket;
144
145 if ((socket = (GSocket *) malloc(sizeof(GSocket))) == NULL)
146 return NULL;
147
148 socket->m_fd = INVALID_SOCKET;
149 for (i = 0; i < GSOCK_MAX_EVENT; i++)
150 {
151 socket->m_cbacks[i] = NULL;
152 }
ed2eb9af 153 socket->m_detected = 0;
33ac7e6f
KB
154 socket->m_local = NULL;
155 socket->m_peer = NULL;
9bbd7ba3 156 socket->m_error = GSOCK_NOERROR;
33ac7e6f 157 socket->m_server = FALSE;
9bbd7ba3
GRG
158 socket->m_stream = TRUE;
159 socket->m_non_blocking = FALSE;
160 socket->m_timeout.tv_sec = 10 * 60; /* 10 minutes */
b661e675 161 socket->m_timeout.tv_usec = 0;
ed2eb9af 162 socket->m_establishing = FALSE;
9bbd7ba3 163
70988afb 164 /* Per-socket GUI-specific initialization */
38bb138f 165 success = _GSocket_GUI_Init_Socket(socket);
ed2eb9af 166 if (!success)
9bbd7ba3 167 {
70988afb 168 free(socket);
3ca6a5f0 169 socket = NULL;
9bbd7ba3 170 }
9bbd7ba3
GRG
171
172 return socket;
173}
174
007c77ab
RL
175void GSocket_close(GSocket *socket)
176{
177 _GSocket_Disable_Events(socket);
178 closesocket(socket->m_fd);
179 socket->m_fd = INVALID_SOCKET;
180}
181
9bbd7ba3
GRG
182void GSocket_destroy(GSocket *socket)
183{
184 assert(socket != NULL);
185
70988afb 186 /* Per-socket GUI-specific cleanup */
38bb138f 187 _GSocket_GUI_Destroy_Socket(socket);
9bbd7ba3 188
75d684d9 189 /* Check that the socket is really shutdowned */
9bbd7ba3
GRG
190 if (socket->m_fd != INVALID_SOCKET)
191 GSocket_Shutdown(socket);
192
75d684d9 193 /* Destroy private addresses */
9bbd7ba3
GRG
194 if (socket->m_local)
195 GAddress_destroy(socket->m_local);
196
197 if (socket->m_peer)
198 GAddress_destroy(socket->m_peer);
199
9bf10d6b 200 /* Destroy the socket itself */
9bbd7ba3
GRG
201 free(socket);
202}
203
9bf10d6b
GRG
204/* GSocket_Shutdown:
205 * Disallow further read/write operations on this socket, close
206 * the fd and disable all callbacks.
207 */
9bbd7ba3
GRG
208void GSocket_Shutdown(GSocket *socket)
209{
210 int evt;
211
212 assert(socket != NULL);
213
75d684d9 214 /* If socket has been created, shutdown it */
9bbd7ba3
GRG
215 if (socket->m_fd != INVALID_SOCKET)
216 {
9bbd7ba3 217 shutdown(socket->m_fd, 2);
007c77ab 218 GSocket_close(socket);
9bbd7ba3
GRG
219 }
220
75d684d9 221 /* Disable GUI callbacks */
9bbd7ba3 222 for (evt = 0; evt < GSOCK_MAX_EVENT; evt++)
9bbd7ba3 223 socket->m_cbacks[evt] = NULL;
9bbd7ba3 224
557e7011 225 socket->m_detected = GSOCK_LOST_FLAG;
9bbd7ba3
GRG
226}
227
228/* Address handling */
229
9bf10d6b
GRG
230/* GSocket_SetLocal:
231 * GSocket_GetLocal:
232 * GSocket_SetPeer:
233 * GSocket_GetPeer:
234 * Set or get the local or peer address for this socket. The 'set'
235 * functions return GSOCK_NOERROR on success, an error code otherwise.
236 * The 'get' functions return a pointer to a GAddress object on success,
237 * or NULL otherwise, in which case they set the error code of the
238 * corresponding GSocket.
239 *
240 * Error codes:
241 * GSOCK_INVSOCK - the socket is not valid.
242 * GSOCK_INVADDR - the address is not valid.
243 */
9bbd7ba3
GRG
244GSocketError GSocket_SetLocal(GSocket *socket, GAddress *address)
245{
246 assert(socket != NULL);
247
9bf10d6b 248 /* the socket must be initialized, or it must be a server */
9bbd7ba3
GRG
249 if (socket->m_fd != INVALID_SOCKET && !socket->m_server)
250 {
251 socket->m_error = GSOCK_INVSOCK;
252 return GSOCK_INVSOCK;
253 }
254
9bf10d6b 255 /* check address */
9bbd7ba3
GRG
256 if (address == NULL || address->m_family == GSOCK_NOFAMILY)
257 {
258 socket->m_error = GSOCK_INVADDR;
259 return GSOCK_INVADDR;
260 }
261
262 if (socket->m_local)
263 GAddress_destroy(socket->m_local);
264
265 socket->m_local = GAddress_copy(address);
266
267 return GSOCK_NOERROR;
268}
269
270GSocketError GSocket_SetPeer(GSocket *socket, GAddress *address)
271{
272 assert(socket != NULL);
273
9bf10d6b 274 /* check address */
9bbd7ba3
GRG
275 if (address == NULL || address->m_family == GSOCK_NOFAMILY)
276 {
277 socket->m_error = GSOCK_INVADDR;
278 return GSOCK_INVADDR;
279 }
280
281 if (socket->m_peer)
282 GAddress_destroy(socket->m_peer);
283
284 socket->m_peer = GAddress_copy(address);
285
286 return GSOCK_NOERROR;
287}
288
289GAddress *GSocket_GetLocal(GSocket *socket)
290{
291 GAddress *address;
292 struct sockaddr addr;
85806dc2
GRG
293 SOCKLEN_T size = sizeof(addr);
294 GSocketError err;
9bbd7ba3
GRG
295
296 assert(socket != NULL);
297
9bf10d6b 298 /* try to get it from the m_local var first */
9bbd7ba3
GRG
299 if (socket->m_local)
300 return GAddress_copy(socket->m_local);
301
9bf10d6b 302 /* else, if the socket is initialized, try getsockname */
9bbd7ba3
GRG
303 if (socket->m_fd == INVALID_SOCKET)
304 {
305 socket->m_error = GSOCK_INVSOCK;
306 return NULL;
307 }
308
9bbd7ba3
GRG
309 if (getsockname(socket->m_fd, &addr, &size) == SOCKET_ERROR)
310 {
311 socket->m_error = GSOCK_IOERR;
312 return NULL;
313 }
314
9bf10d6b
GRG
315 /* got a valid address from getsockname, create a GAddress object */
316 if ((address = GAddress_new()) == NULL)
9bbd7ba3
GRG
317 {
318 socket->m_error = GSOCK_MEMERR;
319 return NULL;
320 }
9bf10d6b
GRG
321
322 if ((err = _GAddress_translate_from(address, &addr, size)) != GSOCK_NOERROR)
9bbd7ba3 323 {
9bbd7ba3 324 GAddress_destroy(address);
85806dc2 325 socket->m_error = err;
9bbd7ba3
GRG
326 return NULL;
327 }
328
329 return address;
330}
331
332GAddress *GSocket_GetPeer(GSocket *socket)
333{
334 assert(socket != NULL);
335
9bf10d6b 336 /* try to get it from the m_peer var */
9bbd7ba3
GRG
337 if (socket->m_peer)
338 return GAddress_copy(socket->m_peer);
339
340 return NULL;
341}
342
343/* Server specific parts */
344
345/* GSocket_SetServer:
9bf10d6b
GRG
346 * Sets up this socket as a server. The local address must have been
347 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
348 * Returns GSOCK_NOERROR on success, one of the following otherwise:
33ac7e6f 349 *
9bf10d6b
GRG
350 * Error codes:
351 * GSOCK_INVSOCK - the socket is in use.
352 * GSOCK_INVADDR - the local address has not been set.
33ac7e6f 353 * GSOCK_IOERR - low-level error.
9bbd7ba3
GRG
354 */
355GSocketError GSocket_SetServer(GSocket *sck)
356{
357 u_long arg = 1;
358
359 assert(sck != NULL);
360
9bf10d6b 361 /* must not be in use */
9bbd7ba3
GRG
362 if (sck->m_fd != INVALID_SOCKET)
363 {
364 sck->m_error = GSOCK_INVSOCK;
365 return GSOCK_INVSOCK;
366 }
367
9bf10d6b 368 /* the local addr must have been set */
9bbd7ba3
GRG
369 if (!sck->m_local)
370 {
371 sck->m_error = GSOCK_INVADDR;
372 return GSOCK_INVADDR;
373 }
374
375 /* Initialize all fields */
376 sck->m_server = TRUE;
377 sck->m_stream = TRUE;
378 sck->m_oriented = TRUE;
379
380 /* Create the socket */
381 sck->m_fd = socket(sck->m_local->m_realfamily, SOCK_STREAM, 0);
9bbd7ba3
GRG
382
383 if (sck->m_fd == INVALID_SOCKET)
384 {
385 sck->m_error = GSOCK_IOERR;
386 return GSOCK_IOERR;
387 }
388
483c6690 389 ioctlsocket(sck->m_fd, FIONBIO, (u_long FAR *) &arg);
75d684d9 390 _GSocket_Enable_Events(sck);
483c6690 391
e8b7a986
JS
392 /* allow a socket to re-bind if the socket is in the TIME_WAIT
393 state after being previously closed.
394 */
395 setsockopt(sck->m_fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&arg, sizeof(u_long));
396
9bf10d6b
GRG
397 /* Bind to the local address,
398 * retrieve the actual address bound,
399 * and listen up to 5 connections.
400 */
401 if ((bind(sck->m_fd, sck->m_local->m_addr, sck->m_local->m_len) != 0) ||
402 (getsockname(sck->m_fd,
403 sck->m_local->m_addr,
378d2bd3 404 (SOCKLEN_T *)&sck->m_local->m_len) != 0) ||
9bf10d6b 405 (listen(sck->m_fd, 5) != 0))
9bbd7ba3 406 {
007c77ab 407 GSocket_close(sck);
9bbd7ba3
GRG
408 sck->m_error = GSOCK_IOERR;
409 return GSOCK_IOERR;
410 }
411
412 return GSOCK_NOERROR;
b661e675 413}
9bbd7ba3
GRG
414
415/* GSocket_WaitConnection:
9bf10d6b
GRG
416 * Waits for an incoming client connection. Returns a pointer to
417 * a GSocket object, or NULL if there was an error, in which case
418 * the last error field will be updated for the calling GSocket.
419 *
420 * Error codes (set in the calling GSocket)
421 * GSOCK_INVSOCK - the socket is not valid or not a server.
422 * GSOCK_TIMEDOUT - timeout, no incoming connections.
423 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
424 * GSOCK_MEMERR - couldn't allocate memory.
33ac7e6f 425 * GSOCK_IOERR - low-level error.
9bbd7ba3
GRG
426 */
427GSocket *GSocket_WaitConnection(GSocket *sck)
428{
429 GSocket *connection;
85806dc2
GRG
430 struct sockaddr from;
431 SOCKLEN_T fromlen = sizeof(from);
432 GSocketError err;
9bbd7ba3
GRG
433 u_long arg = 1;
434
435 assert(sck != NULL);
436
9bf10d6b 437 /* Reenable CONNECTION events */
75d684d9
GRG
438 sck->m_detected &= ~GSOCK_CONNECTION_FLAG;
439
9bf10d6b 440 /* If the socket has already been created, we exit immediately */
9bbd7ba3
GRG
441 if (sck->m_fd == INVALID_SOCKET || !sck->m_server)
442 {
443 sck->m_error = GSOCK_INVSOCK;
444 return NULL;
445 }
446
447 /* Create a GSocket object for the new connection */
448 connection = GSocket_new();
449
450 if (!connection)
451 {
452 sck->m_error = GSOCK_MEMERR;
453 return NULL;
454 }
455
456 /* Wait for a connection (with timeout) */
457 if (_GSocket_Input_Timeout(sck) == GSOCK_TIMEDOUT)
458 {
459 GSocket_destroy(connection);
460 /* sck->m_error set by _GSocket_Input_Timeout */
461 return NULL;
462 }
463
85806dc2 464 connection->m_fd = accept(sck->m_fd, &from, &fromlen);
9bbd7ba3
GRG
465
466 if (connection->m_fd == INVALID_SOCKET)
467 {
aa3981f2
GRG
468 if (WSAGetLastError() == WSAEWOULDBLOCK)
469 sck->m_error = GSOCK_WOULDBLOCK;
470 else
471 sck->m_error = GSOCK_IOERR;
472
9bbd7ba3 473 GSocket_destroy(connection);
9bbd7ba3
GRG
474 return NULL;
475 }
476
477 /* Initialize all fields */
478 connection->m_server = FALSE;
479 connection->m_stream = TRUE;
480 connection->m_oriented = TRUE;
481
85806dc2
GRG
482 /* Setup the peer address field */
483 connection->m_peer = GAddress_new();
484 if (!connection->m_peer)
485 {
486 GSocket_destroy(connection);
487 sck->m_error = GSOCK_MEMERR;
488 return NULL;
489 }
490 err = _GAddress_translate_from(connection->m_peer, &from, fromlen);
491 if (err != GSOCK_NOERROR)
492 {
493 GAddress_destroy(connection->m_peer);
494 GSocket_destroy(connection);
495 sck->m_error = err;
496 return NULL;
497 }
498
cb421e53 499 ioctlsocket(connection->m_fd, FIONBIO, (u_long FAR *) &arg);
75d684d9 500 _GSocket_Enable_Events(connection);
cb421e53 501
9bbd7ba3
GRG
502 return connection;
503}
504
9bbd7ba3
GRG
505/* Client specific parts */
506
507/* GSocket_Connect:
9bf10d6b
GRG
508 * For stream (connection oriented) sockets, GSocket_Connect() tries
509 * to establish a client connection to a server using the peer address
510 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
511 * connection has been succesfully established, or one of the error
512 * codes listed below. Note that for nonblocking sockets, a return
513 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
514 * request can be completed later; you should use GSocket_Select()
515 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
516 * corresponding asynchronous events.
517 *
518 * For datagram (non connection oriented) sockets, GSocket_Connect()
519 * just sets the peer address established with GSocket_SetPeer() as
520 * default destination.
521 *
522 * Error codes:
523 * GSOCK_INVSOCK - the socket is in use or not valid.
524 * GSOCK_INVADDR - the peer address has not been established.
525 * GSOCK_TIMEDOUT - timeout, the connection failed.
526 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
527 * GSOCK_MEMERR - couldn't allocate memory.
33ac7e6f 528 * GSOCK_IOERR - low-level error.
9bbd7ba3
GRG
529 */
530GSocketError GSocket_Connect(GSocket *sck, GSocketStream stream)
531{
9bf10d6b 532 int ret, err;
9bbd7ba3 533 u_long arg = 1;
9bbd7ba3
GRG
534
535 assert(sck != NULL);
536
9bf10d6b 537 /* Enable CONNECTION events (needed for nonblocking connections) */
75d684d9
GRG
538 sck->m_detected &= ~GSOCK_CONNECTION_FLAG;
539
9bbd7ba3
GRG
540 if (sck->m_fd != INVALID_SOCKET)
541 {
542 sck->m_error = GSOCK_INVSOCK;
543 return GSOCK_INVSOCK;
544 }
545
546 if (!sck->m_peer)
547 {
548 sck->m_error = GSOCK_INVADDR;
549 return GSOCK_INVADDR;
550 }
551
9bf10d6b 552 /* Streamed or dgram socket? */
9bbd7ba3
GRG
553 sck->m_stream = (stream == GSOCK_STREAMED);
554 sck->m_oriented = TRUE;
555 sck->m_server = FALSE;
ed2eb9af 556 sck->m_establishing = FALSE;
9bbd7ba3 557
9bbd7ba3 558 /* Create the socket */
9bf10d6b
GRG
559 sck->m_fd = socket(sck->m_peer->m_realfamily,
560 sck->m_stream? SOCK_STREAM : SOCK_DGRAM, 0);
9bbd7ba3
GRG
561
562 if (sck->m_fd == INVALID_SOCKET)
563 {
564 sck->m_error = GSOCK_IOERR;
565 return GSOCK_IOERR;
566 }
567
483c6690 568 ioctlsocket(sck->m_fd, FIONBIO, (u_long FAR *) &arg);
75d684d9 569 _GSocket_Enable_Events(sck);
483c6690 570
9bf10d6b 571 /* Connect it to the peer address, with a timeout (see below) */
9bbd7ba3
GRG
572 ret = connect(sck->m_fd, sck->m_peer->m_addr, sck->m_peer->m_len);
573
574 if (ret == SOCKET_ERROR)
575 {
aa3981f2
GRG
576 err = WSAGetLastError();
577
578 /* If connect failed with EWOULDBLOCK and the GSocket object
579 * is in blocking mode, we select() for the specified timeout
580 * checking for writability to see if the connection request
581 * completes.
9bbd7ba3 582 */
cb421e53 583 if ((err == WSAEWOULDBLOCK) && (!sck->m_non_blocking))
9bbd7ba3 584 {
9bf10d6b
GRG
585 err = _GSocket_Connect_Timeout(sck);
586
587 if (err != GSOCK_NOERROR)
9bbd7ba3 588 {
007c77ab 589 GSocket_close(sck);
9bf10d6b 590 /* sck->m_error is set in _GSocket_Connect_Timeout */
9bbd7ba3 591 }
9bf10d6b 592
ba14d986 593 return (GSocketError) err;
9bbd7ba3 594 }
aa3981f2
GRG
595
596 /* If connect failed with EWOULDBLOCK and the GSocket object
597 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
598 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
599 * this way if the connection completes, a GSOCK_CONNECTION
600 * event will be generated, if enabled.
601 */
cb421e53 602 if ((err == WSAEWOULDBLOCK) && (sck->m_non_blocking))
9bbd7ba3 603 {
ed2eb9af 604 sck->m_establishing = TRUE;
aa3981f2
GRG
605 sck->m_error = GSOCK_WOULDBLOCK;
606 return GSOCK_WOULDBLOCK;
9bbd7ba3 607 }
aa3981f2
GRG
608
609 /* If connect failed with an error other than EWOULDBLOCK,
9bf10d6b 610 * then the call to GSocket_Connect() has failed.
aa3981f2 611 */
007c77ab 612 GSocket_close(sck);
aa3981f2
GRG
613 sck->m_error = GSOCK_IOERR;
614 return GSOCK_IOERR;
9bbd7ba3
GRG
615 }
616
617 return GSOCK_NOERROR;
618}
619
9bf10d6b
GRG
620/* Datagram sockets */
621
622/* GSocket_SetNonOriented:
623 * Sets up this socket as a non-connection oriented (datagram) socket.
624 * Before using this function, the local address must have been set
625 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
626 * on success, or one of the following otherwise.
627 *
628 * Error codes:
629 * GSOCK_INVSOCK - the socket is in use.
630 * GSOCK_INVADDR - the local address has not been set.
33ac7e6f 631 * GSOCK_IOERR - low-level error.
9bf10d6b
GRG
632 */
633GSocketError GSocket_SetNonOriented(GSocket *sck)
634{
635 u_long arg = 1;
636
637 assert(sck != NULL);
638
639 if (sck->m_fd != INVALID_SOCKET)
640 {
641 sck->m_error = GSOCK_INVSOCK;
642 return GSOCK_INVSOCK;
643 }
644
645 if (!sck->m_local)
646 {
647 sck->m_error = GSOCK_INVADDR;
648 return GSOCK_INVADDR;
649 }
650
651 /* Initialize all fields */
652 sck->m_stream = FALSE;
653 sck->m_server = FALSE;
654 sck->m_oriented = FALSE;
655
656 /* Create the socket */
657 sck->m_fd = socket(sck->m_local->m_realfamily, SOCK_DGRAM, 0);
658
659 if (sck->m_fd == INVALID_SOCKET)
660 {
661 sck->m_error = GSOCK_IOERR;
662 return GSOCK_IOERR;
663 }
664
665 ioctlsocket(sck->m_fd, FIONBIO, (u_long FAR *) &arg);
666 _GSocket_Enable_Events(sck);
667
668 /* Bind to the local address,
669 * and retrieve the actual address bound.
670 */
671 if ((bind(sck->m_fd, sck->m_local->m_addr, sck->m_local->m_len) != 0) ||
672 (getsockname(sck->m_fd,
673 sck->m_local->m_addr,
378d2bd3 674 (SOCKLEN_T *)&sck->m_local->m_len) != 0))
9bf10d6b 675 {
007c77ab 676 GSocket_close(sck);
9bf10d6b
GRG
677 sck->m_error = GSOCK_IOERR;
678 return GSOCK_IOERR;
679 }
680
681 return GSOCK_NOERROR;
682}
683
9bbd7ba3
GRG
684/* Generic IO */
685
686/* Like recv(), send(), ... */
687int GSocket_Read(GSocket *socket, char *buffer, int size)
688{
75d684d9
GRG
689 int ret;
690
9bbd7ba3
GRG
691 assert(socket != NULL);
692
9bf10d6b 693 /* Reenable INPUT events */
75d684d9
GRG
694 socket->m_detected &= ~GSOCK_INPUT_FLAG;
695
9bbd7ba3
GRG
696 if (socket->m_fd == INVALID_SOCKET || socket->m_server)
697 {
698 socket->m_error = GSOCK_INVSOCK;
699 return -1;
700 }
701
75d684d9 702 /* If the socket is blocking, wait for data (with a timeout) */
9bbd7ba3
GRG
703 if (_GSocket_Input_Timeout(socket) == GSOCK_TIMEDOUT)
704 return -1;
705
75d684d9 706 /* Read the data */
9bbd7ba3 707 if (socket->m_stream)
75d684d9 708 ret = _GSocket_Recv_Stream(socket, buffer, size);
9bbd7ba3 709 else
75d684d9
GRG
710 ret = _GSocket_Recv_Dgram(socket, buffer, size);
711
712 if (ret == SOCKET_ERROR)
713 {
75d684d9 714 if (WSAGetLastError() != WSAEWOULDBLOCK)
75d684d9 715 socket->m_error = GSOCK_IOERR;
75d684d9 716 else
75d684d9 717 socket->m_error = GSOCK_WOULDBLOCK;
9bf10d6b 718 return -1;
75d684d9
GRG
719 }
720
721 return ret;
9bbd7ba3
GRG
722}
723
724int GSocket_Write(GSocket *socket, const char *buffer, int size)
725{
75d684d9
GRG
726 int ret;
727
9bbd7ba3
GRG
728 assert(socket != NULL);
729
730 if (socket->m_fd == INVALID_SOCKET || socket->m_server)
731 {
732 socket->m_error = GSOCK_INVSOCK;
733 return -1;
734 }
735
75d684d9 736 /* If the socket is blocking, wait for writability (with a timeout) */
9bbd7ba3
GRG
737 if (_GSocket_Output_Timeout(socket) == GSOCK_TIMEDOUT)
738 return -1;
739
9bf10d6b 740 /* Write the data */
9bbd7ba3 741 if (socket->m_stream)
75d684d9 742 ret = _GSocket_Send_Stream(socket, buffer, size);
9bbd7ba3 743 else
75d684d9
GRG
744 ret = _GSocket_Send_Dgram(socket, buffer, size);
745
746 if (ret == SOCKET_ERROR)
747 {
748 if (WSAGetLastError() != WSAEWOULDBLOCK)
749 socket->m_error = GSOCK_IOERR;
750 else
751 socket->m_error = GSOCK_WOULDBLOCK;
752
9bf10d6b
GRG
753 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
754 * does). Once the first OUTPUT event is received, users can assume
755 * that the socket is writable until a read operation fails. Only then
756 * will further OUTPUT events be posted.
757 */
75d684d9
GRG
758 socket->m_detected &= ~GSOCK_OUTPUT_FLAG;
759 return -1;
760 }
761
762 return ret;
9bbd7ba3
GRG
763}
764
483c6690
GRG
765/* GSocket_Select:
766 * Polls the socket to determine its status. This function will
767 * check for the events specified in the 'flags' parameter, and
768 * it will return a mask indicating which operations can be
769 * performed. This function won't block, regardless of the
9bf10d6b 770 * mode (blocking | nonblocking) of the socket.
483c6690
GRG
771 */
772GSocketEventFlags GSocket_Select(GSocket *socket, GSocketEventFlags flags)
9bbd7ba3 773{
4368b1a6 774 if (!USE_GUI())
7235a82e
VS
775 {
776 GSocketEventFlags result = 0;
777 fd_set readfds;
778 fd_set writefds;
779 fd_set exceptfds;
b1a8a610 780
7235a82e 781 assert(socket != NULL);
9bbd7ba3 782
7235a82e
VS
783 FD_ZERO(&readfds);
784 FD_ZERO(&writefds);
785 FD_ZERO(&exceptfds);
786 FD_SET(socket->m_fd, &readfds);
991420e6 787 if (flags & GSOCK_OUTPUT_FLAG || flags & GSOCK_CONNECTION_FLAG)
b1a8a610 788 FD_SET(socket->m_fd, &writefds);
7235a82e 789 FD_SET(socket->m_fd, &exceptfds);
ed2eb9af 790
7235a82e
VS
791 /* Check 'sticky' CONNECTION flag first */
792 result |= (GSOCK_CONNECTION_FLAG & socket->m_detected);
42b7406d 793
7235a82e
VS
794 /* If we have already detected a LOST event, then don't try
795 * to do any further processing.
796 */
797 if ((socket->m_detected & GSOCK_LOST_FLAG) != 0)
798 {
799 socket->m_establishing = FALSE;
ed2eb9af 800
7235a82e
VS
801 return (GSOCK_LOST_FLAG & flags);
802 }
42b7406d 803
7235a82e 804 /* Try select now */
b1a8a610 805 if (select(socket->m_fd + 1, &readfds, &writefds, &exceptfds,
991420e6 806 &socket->m_timeout) <= 0)
42b7406d 807 {
7235a82e
VS
808 /* What to do here? */
809 return (result & flags);
42b7406d 810 }
7235a82e
VS
811
812 /* Check for readability */
813 if (FD_ISSET(socket->m_fd, &readfds))
42b7406d 814 {
7235a82e
VS
815 char c;
816
b1a8a610 817 if (!socket->m_stream || recv(socket->m_fd, &c, 1, MSG_PEEK) > 0)
42b7406d 818 {
7235a82e 819 result |= GSOCK_INPUT_FLAG;
42b7406d
SN
820 }
821 else
822 {
7235a82e
VS
823 if (socket->m_server && socket->m_stream)
824 {
825 result |= GSOCK_CONNECTION_FLAG;
826 socket->m_detected |= GSOCK_CONNECTION_FLAG;
827 }
828 else
829 {
830 socket->m_detected = GSOCK_LOST_FLAG;
831 socket->m_establishing = FALSE;
832
833 /* LOST event: Abort any further processing */
834 return (GSOCK_LOST_FLAG & flags);
835 }
42b7406d
SN
836 }
837 }
ed2eb9af 838
7235a82e
VS
839 /* Check for writability */
840 if (FD_ISSET(socket->m_fd, &writefds))
ed2eb9af 841 {
7235a82e
VS
842 if (socket->m_establishing && !socket->m_server)
843 {
844 int error;
845 SOCKLEN_T len = sizeof(error);
42b7406d 846
7235a82e 847 socket->m_establishing = FALSE;
42b7406d 848
7235a82e 849 getsockopt(socket->m_fd, SOL_SOCKET, SO_ERROR, (void*)&error, &len);
42b7406d 850
7235a82e
VS
851 if (error)
852 {
853 socket->m_detected = GSOCK_LOST_FLAG;
42b7406d 854
7235a82e
VS
855 /* LOST event: Abort any further processing */
856 return (GSOCK_LOST_FLAG & flags);
857 }
858 else
859 {
860 result |= GSOCK_CONNECTION_FLAG;
861 socket->m_detected |= GSOCK_CONNECTION_FLAG;
862 }
42b7406d
SN
863 }
864 else
865 {
7235a82e 866 result |= GSOCK_OUTPUT_FLAG;
42b7406d 867 }
ed2eb9af 868 }
7235a82e
VS
869
870 /* Check for exceptions and errors (is this useful in Unices?) */
871 if (FD_ISSET(socket->m_fd, &exceptfds))
42b7406d 872 {
7235a82e
VS
873 socket->m_establishing = FALSE;
874 socket->m_detected = GSOCK_LOST_FLAG;
875
876 /* LOST event: Abort any further processing */
877 return (GSOCK_LOST_FLAG & flags);
42b7406d 878 }
ed2eb9af 879
7235a82e
VS
880 return (result & flags);
881 }
4368b1a6 882 else /* USE_GUI() */
ed2eb9af 883 {
7235a82e
VS
884 assert(socket != NULL);
885 return flags & socket->m_detected;
ed2eb9af 886 }
9bbd7ba3
GRG
887}
888
9bf10d6b 889/* Attributes */
9bbd7ba3
GRG
890
891/* GSocket_SetNonBlocking:
9bf10d6b
GRG
892 * Sets the socket to non-blocking mode. All IO calls will return
893 * immediately.
9bbd7ba3 894 */
5c9eff30 895void GSocket_SetNonBlocking(GSocket *socket, int non_block)
9bbd7ba3
GRG
896{
897 assert(socket != NULL);
898
899 socket->m_non_blocking = non_block;
900}
901
902/* GSocket_SetTimeout:
9bf10d6b
GRG
903 * Sets the timeout for blocking calls. Time is expressed in
904 * milliseconds.
9bbd7ba3 905 */
75d684d9 906void GSocket_SetTimeout(GSocket *socket, unsigned long millis)
9bbd7ba3
GRG
907{
908 assert(socket != NULL);
909
75d684d9
GRG
910 socket->m_timeout.tv_sec = (millis / 1000);
911 socket->m_timeout.tv_usec = (millis % 1000) * 1000;
9bbd7ba3
GRG
912}
913
914/* GSocket_GetError:
9bf10d6b
GRG
915 * Returns the last error occured for this socket. Note that successful
916 * operations do not clear this back to GSOCK_NOERROR, so use it only
917 * after an error.
9bbd7ba3 918 */
82805fe2 919GSocketError GSocket_GetError(GSocket *socket)
9bbd7ba3
GRG
920{
921 assert(socket != NULL);
922
923 return socket->m_error;
924}
925
926/* Callbacks */
927
9bf10d6b
GRG
928/* GSOCK_INPUT:
929 * There is data to be read in the input buffer. If, after a read
930 * operation, there is still data available, the callback function will
931 * be called again.
932 * GSOCK_OUTPUT:
33ac7e6f 933 * The socket is available for writing. That is, the next write call
9bf10d6b
GRG
934 * won't block. This event is generated only once, when the connection is
935 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
936 * when the output buffer empties again. This means that the app should
937 * assume that it can write since the first OUTPUT event, and no more
938 * OUTPUT events will be generated unless an error occurs.
939 * GSOCK_CONNECTION:
940 * Connection succesfully established, for client sockets, or incoming
941 * client connection, for server sockets. Wait for this event (also watch
942 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
943 * GSOCK_LOST:
944 * The connection is lost (or a connection request failed); this could
945 * be due to a failure, or due to the peer closing it gracefully.
9bbd7ba3
GRG
946 */
947
948/* GSocket_SetCallback:
483c6690 949 * Enables the callbacks specified by 'flags'. Note that 'flags'
9bbd7ba3
GRG
950 * may be a combination of flags OR'ed toghether, so the same
951 * callback function can be made to accept different events.
952 * The callback function must have the following prototype:
953 *
954 * void function(GSocket *socket, GSocketEvent event, char *cdata)
955 */
483c6690 956void GSocket_SetCallback(GSocket *socket, GSocketEventFlags flags,
9bbd7ba3
GRG
957 GSocketCallback callback, char *cdata)
958{
959 int count;
960
9bf10d6b 961 assert(socket != NULL);
9bbd7ba3
GRG
962
963 for (count = 0; count < GSOCK_MAX_EVENT; count++)
964 {
483c6690 965 if ((flags & (1 << count)) != 0)
9bbd7ba3
GRG
966 {
967 socket->m_cbacks[count] = callback;
968 socket->m_data[count] = cdata;
969 }
970 }
9bbd7ba3
GRG
971}
972
973/* GSocket_UnsetCallback:
483c6690 974 * Disables all callbacks specified by 'flags', which may be a
9bbd7ba3
GRG
975 * combination of flags OR'ed toghether.
976 */
483c6690 977void GSocket_UnsetCallback(GSocket *socket, GSocketEventFlags flags)
9bbd7ba3 978{
9bf10d6b 979 int count;
9bbd7ba3
GRG
980
981 assert(socket != NULL);
982
983 for (count = 0; count < GSOCK_MAX_EVENT; count++)
984 {
483c6690 985 if ((flags & (1 << count)) != 0)
9bbd7ba3
GRG
986 {
987 socket->m_cbacks[count] = NULL;
75d684d9 988 socket->m_data[count] = NULL;
9bbd7ba3
GRG
989 }
990 }
9bbd7ba3
GRG
991}
992
70988afb 993/* Internals (IO) */
9bbd7ba3
GRG
994
995/* _GSocket_Input_Timeout:
996 * For blocking sockets, wait until data is available or
997 * until timeout ellapses.
998 */
999GSocketError _GSocket_Input_Timeout(GSocket *socket)
1000{
1001 fd_set readfds;
1002
cb421e53 1003 if (!socket->m_non_blocking)
9bbd7ba3
GRG
1004 {
1005 FD_ZERO(&readfds);
1006 FD_SET(socket->m_fd, &readfds);
1007 if (select(0, &readfds, NULL, NULL, &socket->m_timeout) == 0)
1008 {
1009 socket->m_error = GSOCK_TIMEDOUT;
1010 return GSOCK_TIMEDOUT;
1011 }
1012 }
1013 return GSOCK_NOERROR;
1014}
1015
1016/* _GSocket_Output_Timeout:
1017 * For blocking sockets, wait until data can be sent without
1018 * blocking or until timeout ellapses.
1019 */
1020GSocketError _GSocket_Output_Timeout(GSocket *socket)
1021{
1022 fd_set writefds;
1023
cb421e53 1024 if (!socket->m_non_blocking)
9bbd7ba3
GRG
1025 {
1026 FD_ZERO(&writefds);
1027 FD_SET(socket->m_fd, &writefds);
1028 if (select(0, NULL, &writefds, NULL, &socket->m_timeout) == 0)
1029 {
1030 socket->m_error = GSOCK_TIMEDOUT;
1031 return GSOCK_TIMEDOUT;
1032 }
1033 }
1034 return GSOCK_NOERROR;
1035}
1036
9bf10d6b
GRG
1037/* _GSocket_Connect_Timeout:
1038 * For blocking sockets, wait until the connection is
1039 * established or fails, or until timeout ellapses.
1040 */
1041GSocketError _GSocket_Connect_Timeout(GSocket *socket)
1042{
1043 fd_set writefds;
1044 fd_set exceptfds;
1045
5330a869
GRG
1046 FD_ZERO(&writefds);
1047 FD_ZERO(&exceptfds);
1048 FD_SET(socket->m_fd, &writefds);
1049 FD_SET(socket->m_fd, &exceptfds);
1050 if (select(0, NULL, &writefds, &exceptfds, &socket->m_timeout) == 0)
9bf10d6b 1051 {
5330a869
GRG
1052 socket->m_error = GSOCK_TIMEDOUT;
1053 return GSOCK_TIMEDOUT;
9bf10d6b
GRG
1054 }
1055 if (!FD_ISSET(socket->m_fd, &writefds))
1056 {
1057 socket->m_error = GSOCK_IOERR;
1058 return GSOCK_IOERR;
1059 }
1060
1061 return GSOCK_NOERROR;
1062}
1063
9bbd7ba3
GRG
1064int _GSocket_Recv_Stream(GSocket *socket, char *buffer, int size)
1065{
75d684d9 1066 return recv(socket->m_fd, buffer, size, 0);
9bbd7ba3
GRG
1067}
1068
1069int _GSocket_Recv_Dgram(GSocket *socket, char *buffer, int size)
1070{
1071 struct sockaddr from;
75d684d9 1072 SOCKLEN_T fromlen = sizeof(from);
9bbd7ba3 1073 int ret;
85806dc2 1074 GSocketError err;
9bbd7ba3 1075
9bbd7ba3
GRG
1076 ret = recvfrom(socket->m_fd, buffer, size, 0, &from, &fromlen);
1077
1078 if (ret == SOCKET_ERROR)
75d684d9 1079 return SOCKET_ERROR;
9bbd7ba3
GRG
1080
1081 /* Translate a system address into a GSocket address */
1082 if (!socket->m_peer)
1083 {
1084 socket->m_peer = GAddress_new();
1085 if (!socket->m_peer)
1086 {
1087 socket->m_error = GSOCK_MEMERR;
1088 return -1;
1089 }
1090 }
85806dc2
GRG
1091 err = _GAddress_translate_from(socket->m_peer, &from, fromlen);
1092 if (err != GSOCK_NOERROR)
9bbd7ba3 1093 {
cb421e53 1094 GAddress_destroy(socket->m_peer);
85806dc2
GRG
1095 socket->m_peer = NULL;
1096 socket->m_error = err;
9bbd7ba3
GRG
1097 return -1;
1098 }
1099
1100 return ret;
1101}
1102
1103int _GSocket_Send_Stream(GSocket *socket, const char *buffer, int size)
1104{
75d684d9 1105 return send(socket->m_fd, buffer, size, 0);
9bbd7ba3
GRG
1106}
1107
1108int _GSocket_Send_Dgram(GSocket *socket, const char *buffer, int size)
1109{
1110 struct sockaddr *addr;
1111 int len, ret;
85806dc2 1112 GSocketError err;
9bbd7ba3
GRG
1113
1114 if (!socket->m_peer)
1115 {
1116 socket->m_error = GSOCK_INVADDR;
1117 return -1;
1118 }
1119
85806dc2
GRG
1120 err = _GAddress_translate_to(socket->m_peer, &addr, &len);
1121 if (err != GSOCK_NOERROR)
9bbd7ba3 1122 {
85806dc2 1123 socket->m_error = err;
9bbd7ba3
GRG
1124 return -1;
1125 }
1126
1127 ret = sendto(socket->m_fd, buffer, size, 0, addr, len);
1128
1129 /* Frees memory allocated by _GAddress_translate_to */
1130 free(addr);
1131
9bbd7ba3
GRG
1132 return ret;
1133}
1134
1135
1136/*
1137 * -------------------------------------------------------------------------
1138 * GAddress
1139 * -------------------------------------------------------------------------
1140 */
1141
032d5581
GRG
1142/* CHECK_ADDRESS verifies that the current address family is either
1143 * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it
1144 * initalizes it to be a GSOCK_*family*. In other cases, it returns
1145 * an appropiate error code.
1146 *
1147 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
9bbd7ba3 1148 */
032d5581 1149#define CHECK_ADDRESS(address, family) \
9bbd7ba3
GRG
1150{ \
1151 if (address->m_family == GSOCK_NOFAMILY) \
1152 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1153 return address->m_error; \
1154 if (address->m_family != GSOCK_##family) \
032d5581
GRG
1155 { \
1156 address->m_error = GSOCK_INVADDR; \
1157 return GSOCK_INVADDR; \
1158 } \
1159}
1160
1161#define CHECK_ADDRESS_RETVAL(address, family, retval) \
1162{ \
1163 if (address->m_family == GSOCK_NOFAMILY) \
1164 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1165 return retval; \
1166 if (address->m_family != GSOCK_##family) \
9bbd7ba3
GRG
1167 { \
1168 address->m_error = GSOCK_INVADDR; \
1169 return retval; \
1170 } \
1171}
1172
032d5581 1173
da051b23 1174GAddress *GAddress_new(void)
9bbd7ba3
GRG
1175{
1176 GAddress *address;
1177
1178 if ((address = (GAddress *) malloc(sizeof(GAddress))) == NULL)
1179 return NULL;
1180
1181 address->m_family = GSOCK_NOFAMILY;
1182 address->m_addr = NULL;
1183 address->m_len = 0;
1184
1185 return address;
1186}
1187
1188GAddress *GAddress_copy(GAddress *address)
1189{
1190 GAddress *addr2;
1191
1192 assert(address != NULL);
1193
1194 if ((addr2 = (GAddress *) malloc(sizeof(GAddress))) == NULL)
1195 return NULL;
1196
1197 memcpy(addr2, address, sizeof(GAddress));
1198
1199 if (address->m_addr)
1200 {
1201 addr2->m_addr = (struct sockaddr *) malloc(addr2->m_len);
1202 if (addr2->m_addr == NULL)
1203 {
1204 free(addr2);
1205 return NULL;
1206 }
1207 memcpy(addr2->m_addr, address->m_addr, addr2->m_len);
1208 }
1209
1210 return addr2;
1211}
1212
1213void GAddress_destroy(GAddress *address)
1214{
1215 assert(address != NULL);
1216
29c25a8e
GRG
1217 if (address->m_addr)
1218 free(address->m_addr);
1219
9bbd7ba3
GRG
1220 free(address);
1221}
1222
1223void GAddress_SetFamily(GAddress *address, GAddressType type)
1224{
1225 assert(address != NULL);
1226
1227 address->m_family = type;
1228}
1229
1230GAddressType GAddress_GetFamily(GAddress *address)
1231{
1232 assert(address != NULL);
1233
1234 return address->m_family;
1235}
1236
1237GSocketError _GAddress_translate_from(GAddress *address,
1238 struct sockaddr *addr, int len)
1239{
1240 address->m_realfamily = addr->sa_family;
1241 switch (addr->sa_family)
1242 {
1243 case AF_INET:
1244 address->m_family = GSOCK_INET;
1245 break;
1246 case AF_UNIX:
1247 address->m_family = GSOCK_UNIX;
1248 break;
1249#ifdef AF_INET6
1250 case AF_INET6:
1251 address->m_family = GSOCK_INET6;
1252 break;
1253#endif
1254 default:
1255 {
1256 address->m_error = GSOCK_INVOP;
1257 return GSOCK_INVOP;
1258 }
1259 }
1260
1261 if (address->m_addr)
1262 free(address->m_addr);
1263
1264 address->m_len = len;
1265 address->m_addr = (struct sockaddr *) malloc(len);
1266
1267 if (address->m_addr == NULL)
1268 {
1269 address->m_error = GSOCK_MEMERR;
1270 return GSOCK_MEMERR;
1271 }
1272 memcpy(address->m_addr, addr, len);
1273
1274 return GSOCK_NOERROR;
1275}
1276
1277GSocketError _GAddress_translate_to(GAddress *address,
1278 struct sockaddr **addr, int *len)
1279{
1280 if (!address->m_addr)
1281 {
1282 address->m_error = GSOCK_INVADDR;
1283 return GSOCK_INVADDR;
1284 }
1285
1286 *len = address->m_len;
1287 *addr = (struct sockaddr *) malloc(address->m_len);
1288 if (*addr == NULL)
1289 {
1290 address->m_error = GSOCK_MEMERR;
1291 return GSOCK_MEMERR;
1292 }
1293
1294 memcpy(*addr, address->m_addr, address->m_len);
1295 return GSOCK_NOERROR;
1296}
1297
1298/*
1299 * -------------------------------------------------------------------------
1300 * Internet address family
1301 * -------------------------------------------------------------------------
1302 */
1303
1304GSocketError _GAddress_Init_INET(GAddress *address)
1305{
9bf10d6b 1306 address->m_len = sizeof(struct sockaddr_in);
9bbd7ba3
GRG
1307 address->m_addr = (struct sockaddr *) malloc(address->m_len);
1308 if (address->m_addr == NULL)
1309 {
1310 address->m_error = GSOCK_MEMERR;
1311 return GSOCK_MEMERR;
1312 }
1313
9bbd7ba3
GRG
1314 address->m_family = GSOCK_INET;
1315 address->m_realfamily = PF_INET;
1316 ((struct sockaddr_in *)address->m_addr)->sin_family = AF_INET;
9bf10d6b 1317 ((struct sockaddr_in *)address->m_addr)->sin_addr.s_addr = INADDR_ANY;
9bbd7ba3
GRG
1318
1319 return GSOCK_NOERROR;
1320}
1321
1322GSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname)
1323{
1324 struct hostent *he;
1325 struct in_addr *addr;
1326
1327 assert(address != NULL);
1328
032d5581 1329 CHECK_ADDRESS(address, INET);
9bbd7ba3
GRG
1330
1331 addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr);
1332
1333 addr->s_addr = inet_addr(hostname);
b661e675 1334
9bbd7ba3
GRG
1335 /* If it is a numeric host name, convert it now */
1336 if (addr->s_addr == INADDR_NONE)
1337 {
1338 struct in_addr *array_addr;
1339
1340 /* It is a real name, we solve it */
1341 if ((he = gethostbyname(hostname)) == NULL)
1342 {
9bf10d6b 1343 /* addr->s_addr = INADDR_NONE just done by inet_addr() above */
9bbd7ba3
GRG
1344 address->m_error = GSOCK_NOHOST;
1345 return GSOCK_NOHOST;
1346 }
1347 array_addr = (struct in_addr *) *(he->h_addr_list);
1348 addr->s_addr = array_addr[0].s_addr;
1349 }
1350 return GSOCK_NOERROR;
1351}
1352
9bf10d6b
GRG
1353GSocketError GAddress_INET_SetAnyAddress(GAddress *address)
1354{
1355 return GAddress_INET_SetHostAddress(address, INADDR_ANY);
1356}
1357
9bbd7ba3
GRG
1358GSocketError GAddress_INET_SetHostAddress(GAddress *address,
1359 unsigned long hostaddr)
1360{
1361 struct in_addr *addr;
1362
1363 assert(address != NULL);
1364
032d5581 1365 CHECK_ADDRESS(address, INET);
9bbd7ba3
GRG
1366
1367 addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr);
f925e7b4 1368 addr->s_addr = htonl(hostaddr);;
9bbd7ba3
GRG
1369
1370 return GSOCK_NOERROR;
1371}
1372
1373GSocketError GAddress_INET_SetPortName(GAddress *address, const char *port,
1374 const char *protocol)
1375{
1376 struct servent *se;
1377 struct sockaddr_in *addr;
1378
1379 assert(address != NULL);
032d5581 1380 CHECK_ADDRESS(address, INET);
9bbd7ba3
GRG
1381
1382 if (!port)
1383 {
1384 address->m_error = GSOCK_INVPORT;
9bf10d6b 1385 return GSOCK_INVPORT;
9bbd7ba3 1386 }
b661e675 1387
9bbd7ba3
GRG
1388 se = getservbyname(port, protocol);
1389 if (!se)
1390 {
1391 if (isdigit(port[0]))
1392 {
1393 int port_int;
1394
1395 port_int = atoi(port);
1396 addr = (struct sockaddr_in *)address->m_addr;
aa3981f2 1397 addr->sin_port = htons((u_short) port_int);
9bbd7ba3
GRG
1398 return GSOCK_NOERROR;
1399 }
1400
1401 address->m_error = GSOCK_INVPORT;
1402 return GSOCK_INVPORT;
1403 }
1404
1405 addr = (struct sockaddr_in *)address->m_addr;
1406 addr->sin_port = se->s_port;
1407
1408 return GSOCK_NOERROR;
1409}
1410
1411GSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port)
1412{
1413 struct sockaddr_in *addr;
1414
1415 assert(address != NULL);
032d5581 1416 CHECK_ADDRESS(address, INET);
b661e675 1417
9bbd7ba3
GRG
1418 addr = (struct sockaddr_in *)address->m_addr;
1419 addr->sin_port = htons(port);
1420
1421 return GSOCK_NOERROR;
1422}
1423
1424GSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname, size_t sbuf)
1425{
1426 struct hostent *he;
1427 char *addr_buf;
1428 struct sockaddr_in *addr;
1429
b661e675 1430 assert(address != NULL);
032d5581 1431 CHECK_ADDRESS(address, INET);
9bbd7ba3
GRG
1432
1433 addr = (struct sockaddr_in *)address->m_addr;
1434 addr_buf = (char *)&(addr->sin_addr);
1435
1436 he = gethostbyaddr(addr_buf, sizeof(addr->sin_addr), AF_INET);
1437 if (he == NULL)
1438 {
1439 address->m_error = GSOCK_NOHOST;
1440 return GSOCK_NOHOST;
1441 }
1442
1443 strncpy(hostname, he->h_name, sbuf);
1444
1445 return GSOCK_NOERROR;
1446}
1447
1448unsigned long GAddress_INET_GetHostAddress(GAddress *address)
1449{
1450 struct sockaddr_in *addr;
1451
b661e675 1452 assert(address != NULL);
032d5581 1453 CHECK_ADDRESS_RETVAL(address, INET, 0);
9bbd7ba3
GRG
1454
1455 addr = (struct sockaddr_in *)address->m_addr;
1456
f925e7b4 1457 return ntohl(addr->sin_addr.s_addr);
9bbd7ba3
GRG
1458}
1459
1460unsigned short GAddress_INET_GetPort(GAddress *address)
1461{
1462 struct sockaddr_in *addr;
1463
b661e675 1464 assert(address != NULL);
032d5581 1465 CHECK_ADDRESS_RETVAL(address, INET, 0);
9bbd7ba3
GRG
1466
1467 addr = (struct sockaddr_in *)address->m_addr;
1468 return ntohs(addr->sin_port);
1469}
1470
1471/*
1472 * -------------------------------------------------------------------------
1473 * Unix address family
1474 * -------------------------------------------------------------------------
1475 */
1476
1477GSocketError _GAddress_Init_UNIX(GAddress *address)
1478{
1479 assert (address != NULL);
1480 address->m_error = GSOCK_INVADDR;
1481 return GSOCK_INVADDR;
1482}
1483
1484GSocketError GAddress_UNIX_SetPath(GAddress *address, const char *path)
1485{
196be0f1
JS
1486#if defined(__BORLANDC__)
1487 /* prevents unused variable message in Borland */
1488 (void)path;
1489#endif
9bbd7ba3
GRG
1490 assert (address != NULL);
1491 address->m_error = GSOCK_INVADDR;
1492 return GSOCK_INVADDR;
1493}
1494
1495GSocketError GAddress_UNIX_GetPath(GAddress *address, char *path, size_t sbuf)
1496{
196be0f1
JS
1497#if defined(__BORLANDC__)
1498 /* prevents unused variable message in Borland */
1499 (void)path;
1500 (void)sbuf;
1501#endif
9bbd7ba3
GRG
1502 assert (address != NULL);
1503 address->m_error = GSOCK_INVADDR;
1504 return GSOCK_INVADDR;
1505}
1506
a497618a 1507#else /* !wxUSE_SOCKETS */
9bbd7ba3 1508
33ac7e6f 1509/*
9bf10d6b 1510 * Translation unit shouldn't be empty, so include this typedef to make the
a497618a
VZ
1511 * compiler (VC++ 6.0, for example) happy
1512 */
3a922bb4 1513typedef void (*wxDummy)();
9bbd7ba3 1514
a497618a 1515#endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */
007c77ab 1516