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