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