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