]>
Commit | Line | Data |
---|---|---|
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 | |
04889c59 | 60 | #include "wx/private/gsocket.h" |
a3cf8dca VZ |
61 | #include "wx/link.h" |
62 | ||
63 | wxFORCE_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 | ||
02564412 | 78 | #include "wx/private/socket.h" |
8575ff50 | 79 | |
2804f77d | 80 | bool GSocket_Init() |
f7ff39c6 DE |
81 | { |
82 | WSADATA wsaData; | |
83 | ||
2804f77d VZ |
84 | GSocketManager * const manager = GSocketManager::Get(); |
85 | if ( !manager || !manager->OnInit() ) | |
86 | return false; | |
f7ff39c6 DE |
87 | |
88 | /* Initialize WinSocket */ | |
2804f77d | 89 | return WSAStartup((1 << 8) | 1, &wsaData) == 0; |
f7ff39c6 DE |
90 | } |
91 | ||
2804f77d | 92 | void GSocket_Cleanup() |
f7ff39c6 | 93 | { |
2804f77d VZ |
94 | GSocketManager * const manager = GSocketManager::Get(); |
95 | if ( manager ) | |
96 | manager->OnExit(); | |
f7ff39c6 DE |
97 | |
98 | /* Cleanup WinSocket */ | |
99 | WSACleanup(); | |
100 | } | |
101 | ||
102 | /* Constructors / Destructors for GSocket */ | |
103 | ||
f7ff39c6 DE |
104 | /* Server specific parts */ |
105 | ||
106 | /* GSocket_SetServer: | |
107 | * Sets up this socket as a server. The local address must have been | |
108 | * set with GSocket_SetLocal() before GSocket_SetServer() is called. | |
109 | * Returns GSOCK_NOERROR on success, one of the following otherwise: | |
110 | * | |
111 | * Error codes: | |
112 | * GSOCK_INVSOCK - the socket is in use. | |
113 | * GSOCK_INVADDR - the local address has not been set. | |
114 | * GSOCK_IOERR - low-level error. | |
115 | */ | |
c90cc42e | 116 | GSocketError GSocket::SetServer() |
f7ff39c6 DE |
117 | { |
118 | u_long arg = 1; | |
119 | ||
f7ff39c6 | 120 | /* must not be in use */ |
c90cc42e | 121 | if (m_fd != INVALID_SOCKET) |
f7ff39c6 | 122 | { |
c90cc42e | 123 | m_error = GSOCK_INVSOCK; |
f7ff39c6 DE |
124 | return GSOCK_INVSOCK; |
125 | } | |
126 | ||
127 | /* the local addr must have been set */ | |
c90cc42e | 128 | if (!m_local) |
f7ff39c6 | 129 | { |
c90cc42e | 130 | m_error = GSOCK_INVADDR; |
f7ff39c6 DE |
131 | return GSOCK_INVADDR; |
132 | } | |
133 | ||
134 | /* Initialize all fields */ | |
948c96ef DE |
135 | m_server = true; |
136 | m_stream = true; | |
f7ff39c6 DE |
137 | |
138 | /* Create the socket */ | |
c90cc42e | 139 | m_fd = socket(m_local->m_realfamily, SOCK_STREAM, 0); |
f7ff39c6 | 140 | |
c90cc42e | 141 | if (m_fd == INVALID_SOCKET) |
f7ff39c6 | 142 | { |
c90cc42e | 143 | m_error = GSOCK_IOERR; |
f7ff39c6 DE |
144 | return GSOCK_IOERR; |
145 | } | |
146 | ||
c90cc42e | 147 | ioctlsocket(m_fd, FIONBIO, (u_long FAR *) &arg); |
f0fbbe23 | 148 | GSocketManager::Get()->Install_Callback(this); |
f7ff39c6 DE |
149 | |
150 | /* allow a socket to re-bind if the socket is in the TIME_WAIT | |
151 | state after being previously closed. | |
152 | */ | |
28bf2f3c VZ |
153 | if (m_reusable) |
154 | { | |
155 | setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&arg, sizeof(arg)); | |
f7ff39c6 DE |
156 | } |
157 | ||
158 | /* Bind to the local address, | |
159 | * retrieve the actual address bound, | |
160 | * and listen up to 5 connections. | |
161 | */ | |
c90cc42e DE |
162 | if ((bind(m_fd, m_local->m_addr, m_local->m_len) != 0) || |
163 | (getsockname(m_fd, | |
164 | m_local->m_addr, | |
9e03e02d | 165 | (WX_SOCKLEN_T *)&m_local->m_len) != 0) || |
c90cc42e | 166 | (listen(m_fd, 5) != 0)) |
f7ff39c6 | 167 | { |
c90cc42e DE |
168 | Close(); |
169 | m_error = GSOCK_IOERR; | |
f7ff39c6 DE |
170 | return GSOCK_IOERR; |
171 | } | |
172 | ||
173 | return GSOCK_NOERROR; | |
174 | } | |
175 | ||
176 | /* GSocket_WaitConnection: | |
177 | * Waits for an incoming client connection. Returns a pointer to | |
178 | * a GSocket object, or NULL if there was an error, in which case | |
179 | * the last error field will be updated for the calling GSocket. | |
180 | * | |
181 | * Error codes (set in the calling GSocket) | |
182 | * GSOCK_INVSOCK - the socket is not valid or not a server. | |
183 | * GSOCK_TIMEDOUT - timeout, no incoming connections. | |
184 | * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking. | |
185 | * GSOCK_MEMERR - couldn't allocate memory. | |
186 | * GSOCK_IOERR - low-level error. | |
187 | */ | |
53a161e1 | 188 | GSocket *GSocket::WaitConnection(wxSocketBase& wxsocket) |
f7ff39c6 DE |
189 | { |
190 | GSocket *connection; | |
8575ff50 | 191 | wxSockAddr from; |
9e03e02d | 192 | WX_SOCKLEN_T fromlen = sizeof(from); |
f7ff39c6 DE |
193 | GSocketError err; |
194 | u_long arg = 1; | |
195 | ||
f7ff39c6 | 196 | /* Reenable CONNECTION events */ |
c90cc42e | 197 | m_detected &= ~GSOCK_CONNECTION_FLAG; |
f7ff39c6 DE |
198 | |
199 | /* If the socket has already been created, we exit immediately */ | |
c90cc42e | 200 | if (m_fd == INVALID_SOCKET || !m_server) |
f7ff39c6 | 201 | { |
c90cc42e | 202 | m_error = GSOCK_INVSOCK; |
f7ff39c6 DE |
203 | return NULL; |
204 | } | |
205 | ||
206 | /* Create a GSocket object for the new connection */ | |
53a161e1 | 207 | connection = GSocket::Create(wxsocket); |
f7ff39c6 DE |
208 | |
209 | if (!connection) | |
210 | { | |
c90cc42e | 211 | m_error = GSOCK_MEMERR; |
f7ff39c6 DE |
212 | return NULL; |
213 | } | |
214 | ||
215 | /* Wait for a connection (with timeout) */ | |
c90cc42e | 216 | if (Input_Timeout() == GSOCK_TIMEDOUT) |
f7ff39c6 | 217 | { |
85431efa | 218 | delete connection; |
c90cc42e | 219 | /* m_error set by _GSocket_Input_Timeout */ |
f7ff39c6 DE |
220 | return NULL; |
221 | } | |
222 | ||
8575ff50 | 223 | connection->m_fd = accept(m_fd, (sockaddr*)&from, &fromlen); |
f7ff39c6 DE |
224 | |
225 | if (connection->m_fd == INVALID_SOCKET) | |
226 | { | |
227 | if (WSAGetLastError() == WSAEWOULDBLOCK) | |
c90cc42e | 228 | m_error = GSOCK_WOULDBLOCK; |
f7ff39c6 | 229 | else |
c90cc42e | 230 | m_error = GSOCK_IOERR; |
f7ff39c6 | 231 | |
85431efa | 232 | delete connection; |
f7ff39c6 DE |
233 | return NULL; |
234 | } | |
235 | ||
236 | /* Initialize all fields */ | |
948c96ef DE |
237 | connection->m_server = false; |
238 | connection->m_stream = true; | |
f7ff39c6 DE |
239 | |
240 | /* Setup the peer address field */ | |
241 | connection->m_peer = GAddress_new(); | |
242 | if (!connection->m_peer) | |
243 | { | |
85431efa | 244 | delete connection; |
c90cc42e | 245 | m_error = GSOCK_MEMERR; |
f7ff39c6 DE |
246 | return NULL; |
247 | } | |
8575ff50 | 248 | err = _GAddress_translate_from(connection->m_peer, (sockaddr*)&from, fromlen); |
f7ff39c6 DE |
249 | if (err != GSOCK_NOERROR) |
250 | { | |
251 | GAddress_destroy(connection->m_peer); | |
85431efa | 252 | delete connection; |
c90cc42e | 253 | m_error = err; |
f7ff39c6 DE |
254 | return NULL; |
255 | } | |
256 | ||
257 | ioctlsocket(connection->m_fd, FIONBIO, (u_long FAR *) &arg); | |
f0fbbe23 | 258 | GSocketManager::Get()->Install_Callback(connection); |
f7ff39c6 DE |
259 | |
260 | return connection; | |
261 | } | |
262 | ||
263 | /* GSocket_SetReusable: | |
264 | * Simply sets the m_resuable flag on the socket. GSocket_SetServer will | |
265 | * make the appropriate setsockopt() call. | |
266 | * Implemented as a GSocket function because clients (ie, wxSocketServer) | |
267 | * don't have access to the GSocket struct information. | |
3103e8a9 | 268 | * Returns true if the flag was set correctly, false if an error occurred |
f7ff39c6 DE |
269 | * (ie, if the parameter was NULL) |
270 | */ | |
948c96ef | 271 | bool GSocket::SetReusable() |
f7ff39c6 DE |
272 | { |
273 | /* socket must not be null, and must not be in use/already bound */ | |
c90cc42e | 274 | if (this && m_fd == INVALID_SOCKET) { |
948c96ef DE |
275 | m_reusable = true; |
276 | return true; | |
f7ff39c6 | 277 | } |
948c96ef | 278 | return false; |
f7ff39c6 DE |
279 | } |
280 | ||
60edcf45 VZ |
281 | /* GSocket_SetBroadcast: |
282 | * Simply sets the m_broadcast flag on the socket. GSocket_SetServer will | |
283 | * make the appropriate setsockopt() call. | |
284 | * Implemented as a GSocket function because clients (ie, wxSocketServer) | |
285 | * don't have access to the GSocket struct information. | |
286 | * Returns true if the flag was set correctly, false if an error occurred | |
287 | * (ie, if the parameter was NULL) | |
288 | */ | |
289 | bool GSocket::SetBroadcast() | |
290 | { | |
291 | /* socket must not be in use/already bound */ | |
292 | if (m_fd == INVALID_SOCKET) { | |
293 | m_broadcast = true; | |
294 | return true; | |
295 | } | |
296 | return false; | |
297 | } | |
298 | ||
299 | bool GSocket::DontDoBind() | |
300 | { | |
301 | /* socket must not be in use/already bound */ | |
302 | if (m_fd == INVALID_SOCKET) { | |
303 | m_dobind = false; | |
304 | return true; | |
305 | } | |
306 | return false; | |
307 | } | |
308 | ||
f7ff39c6 DE |
309 | /* Client specific parts */ |
310 | ||
311 | /* GSocket_Connect: | |
312 | * For stream (connection oriented) sockets, GSocket_Connect() tries | |
313 | * to establish a client connection to a server using the peer address | |
314 | * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the | |
3103e8a9 | 315 | * connection has been successfully established, or one of the error |
f7ff39c6 DE |
316 | * codes listed below. Note that for nonblocking sockets, a return |
317 | * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection | |
318 | * request can be completed later; you should use GSocket_Select() | |
319 | * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the | |
320 | * corresponding asynchronous events. | |
321 | * | |
322 | * For datagram (non connection oriented) sockets, GSocket_Connect() | |
323 | * just sets the peer address established with GSocket_SetPeer() as | |
324 | * default destination. | |
325 | * | |
326 | * Error codes: | |
327 | * GSOCK_INVSOCK - the socket is in use or not valid. | |
328 | * GSOCK_INVADDR - the peer address has not been established. | |
329 | * GSOCK_TIMEDOUT - timeout, the connection failed. | |
330 | * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only) | |
331 | * GSOCK_MEMERR - couldn't allocate memory. | |
332 | * GSOCK_IOERR - low-level error. | |
333 | */ | |
c90cc42e | 334 | GSocketError GSocket::Connect(GSocketStream stream) |
f7ff39c6 DE |
335 | { |
336 | int ret, err; | |
337 | u_long arg = 1; | |
338 | ||
f7ff39c6 | 339 | /* Enable CONNECTION events (needed for nonblocking connections) */ |
c90cc42e | 340 | m_detected &= ~GSOCK_CONNECTION_FLAG; |
f7ff39c6 | 341 | |
c90cc42e | 342 | if (m_fd != INVALID_SOCKET) |
f7ff39c6 | 343 | { |
c90cc42e | 344 | m_error = GSOCK_INVSOCK; |
f7ff39c6 DE |
345 | return GSOCK_INVSOCK; |
346 | } | |
347 | ||
c90cc42e | 348 | if (!m_peer) |
f7ff39c6 | 349 | { |
c90cc42e | 350 | m_error = GSOCK_INVADDR; |
f7ff39c6 DE |
351 | return GSOCK_INVADDR; |
352 | } | |
353 | ||
354 | /* Streamed or dgram socket? */ | |
c90cc42e | 355 | m_stream = (stream == GSOCK_STREAMED); |
948c96ef DE |
356 | m_server = false; |
357 | m_establishing = false; | |
f7ff39c6 DE |
358 | |
359 | /* Create the socket */ | |
c90cc42e DE |
360 | m_fd = socket(m_peer->m_realfamily, |
361 | m_stream? SOCK_STREAM : SOCK_DGRAM, 0); | |
f7ff39c6 | 362 | |
c90cc42e | 363 | if (m_fd == INVALID_SOCKET) |
f7ff39c6 | 364 | { |
c90cc42e | 365 | m_error = GSOCK_IOERR; |
f7ff39c6 DE |
366 | return GSOCK_IOERR; |
367 | } | |
368 | ||
c90cc42e | 369 | ioctlsocket(m_fd, FIONBIO, (u_long FAR *) &arg); |
f0fbbe23 | 370 | GSocketManager::Get()->Install_Callback(this); |
f7ff39c6 | 371 | |
716a5baa KH |
372 | // If the reuse flag is set, use the applicable socket reuse flag |
373 | if (m_reusable) | |
374 | { | |
28bf2f3c | 375 | setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&arg, sizeof(arg)); |
716a5baa KH |
376 | } |
377 | ||
8c029a5b VZ |
378 | if (m_initialRecvBufferSize >= 0) |
379 | setsockopt(m_fd, SOL_SOCKET, SO_RCVBUF, (const char*)&m_initialRecvBufferSize, sizeof(m_initialRecvBufferSize)); | |
380 | if (m_initialSendBufferSize >= 0) | |
381 | setsockopt(m_fd, SOL_SOCKET, SO_SNDBUF, (const char*)&m_initialSendBufferSize, sizeof(m_initialSendBufferSize)); | |
382 | ||
716a5baa KH |
383 | // If a local address has been set, then we need to bind to it before calling connect |
384 | if (m_local && m_local->m_addr) | |
385 | { | |
386 | bind(m_fd, m_local->m_addr, m_local->m_len); | |
387 | } | |
388 | ||
f7ff39c6 | 389 | /* Connect it to the peer address, with a timeout (see below) */ |
c90cc42e | 390 | ret = connect(m_fd, m_peer->m_addr, m_peer->m_len); |
f7ff39c6 DE |
391 | |
392 | if (ret == SOCKET_ERROR) | |
393 | { | |
394 | err = WSAGetLastError(); | |
395 | ||
396 | /* If connect failed with EWOULDBLOCK and the GSocket object | |
397 | * is in blocking mode, we select() for the specified timeout | |
398 | * checking for writability to see if the connection request | |
399 | * completes. | |
400 | */ | |
c90cc42e | 401 | if ((err == WSAEWOULDBLOCK) && (!m_non_blocking)) |
f7ff39c6 | 402 | { |
c90cc42e | 403 | err = Connect_Timeout(); |
f7ff39c6 DE |
404 | |
405 | if (err != GSOCK_NOERROR) | |
406 | { | |
c90cc42e DE |
407 | Close(); |
408 | /* m_error is set in _GSocket_Connect_Timeout */ | |
f7ff39c6 DE |
409 | } |
410 | ||
411 | return (GSocketError) err; | |
412 | } | |
413 | ||
414 | /* If connect failed with EWOULDBLOCK and the GSocket object | |
415 | * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK | |
416 | * (and return GSOCK_WOULDBLOCK) but we don't close the socket; | |
417 | * this way if the connection completes, a GSOCK_CONNECTION | |
418 | * event will be generated, if enabled. | |
419 | */ | |
c90cc42e | 420 | if ((err == WSAEWOULDBLOCK) && (m_non_blocking)) |
f7ff39c6 | 421 | { |
948c96ef | 422 | m_establishing = true; |
c90cc42e | 423 | m_error = GSOCK_WOULDBLOCK; |
f7ff39c6 DE |
424 | return GSOCK_WOULDBLOCK; |
425 | } | |
426 | ||
427 | /* If connect failed with an error other than EWOULDBLOCK, | |
428 | * then the call to GSocket_Connect() has failed. | |
429 | */ | |
c90cc42e DE |
430 | Close(); |
431 | m_error = GSOCK_IOERR; | |
f7ff39c6 DE |
432 | return GSOCK_IOERR; |
433 | } | |
434 | ||
435 | return GSOCK_NOERROR; | |
436 | } | |
437 | ||
438 | /* Datagram sockets */ | |
439 | ||
440 | /* GSocket_SetNonOriented: | |
441 | * Sets up this socket as a non-connection oriented (datagram) socket. | |
442 | * Before using this function, the local address must have been set | |
443 | * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR | |
444 | * on success, or one of the following otherwise. | |
445 | * | |
446 | * Error codes: | |
447 | * GSOCK_INVSOCK - the socket is in use. | |
448 | * GSOCK_INVADDR - the local address has not been set. | |
449 | * GSOCK_IOERR - low-level error. | |
450 | */ | |
c90cc42e | 451 | GSocketError GSocket::SetNonOriented() |
f7ff39c6 DE |
452 | { |
453 | u_long arg = 1; | |
454 | ||
c90cc42e | 455 | if (m_fd != INVALID_SOCKET) |
f7ff39c6 | 456 | { |
c90cc42e | 457 | m_error = GSOCK_INVSOCK; |
f7ff39c6 DE |
458 | return GSOCK_INVSOCK; |
459 | } | |
460 | ||
c90cc42e | 461 | if (!m_local) |
f7ff39c6 | 462 | { |
c90cc42e | 463 | m_error = GSOCK_INVADDR; |
f7ff39c6 DE |
464 | return GSOCK_INVADDR; |
465 | } | |
466 | ||
467 | /* Initialize all fields */ | |
948c96ef DE |
468 | m_stream = false; |
469 | m_server = false; | |
f7ff39c6 DE |
470 | |
471 | /* Create the socket */ | |
c90cc42e | 472 | m_fd = socket(m_local->m_realfamily, SOCK_DGRAM, 0); |
f7ff39c6 | 473 | |
c90cc42e | 474 | if (m_fd == INVALID_SOCKET) |
f7ff39c6 | 475 | { |
c90cc42e | 476 | m_error = GSOCK_IOERR; |
f7ff39c6 DE |
477 | return GSOCK_IOERR; |
478 | } | |
479 | ||
c90cc42e | 480 | ioctlsocket(m_fd, FIONBIO, (u_long FAR *) &arg); |
f0fbbe23 | 481 | GSocketManager::Get()->Install_Callback(this); |
f7ff39c6 | 482 | |
28bf2f3c VZ |
483 | if (m_reusable) |
484 | { | |
485 | setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&arg, sizeof(arg)); | |
486 | } | |
60edcf45 | 487 | if (m_broadcast) |
f7ff39c6 | 488 | { |
60edcf45 VZ |
489 | setsockopt(m_fd, SOL_SOCKET, SO_BROADCAST, (const char*)&arg, sizeof(arg)); |
490 | } | |
491 | if (m_dobind) | |
492 | { | |
493 | /* Bind to the local address, | |
494 | * and retrieve the actual address bound. | |
495 | */ | |
496 | if ((bind(m_fd, m_local->m_addr, m_local->m_len) != 0) || | |
497 | (getsockname(m_fd, | |
498 | m_local->m_addr, | |
499 | (WX_SOCKLEN_T *)&m_local->m_len) != 0)) | |
500 | { | |
501 | Close(); | |
502 | m_error = GSOCK_IOERR; | |
503 | return GSOCK_IOERR; | |
504 | } | |
f7ff39c6 DE |
505 | } |
506 | ||
507 | return GSOCK_NOERROR; | |
508 | } | |
509 | ||
510 | /* Generic IO */ | |
511 | ||
512 | /* Like recv(), send(), ... */ | |
c90cc42e | 513 | int GSocket::Read(char *buffer, int size) |
f7ff39c6 DE |
514 | { |
515 | int ret; | |
516 | ||
f7ff39c6 | 517 | /* Reenable INPUT events */ |
c90cc42e | 518 | m_detected &= ~GSOCK_INPUT_FLAG; |
f7ff39c6 | 519 | |
c90cc42e | 520 | if (m_fd == INVALID_SOCKET || m_server) |
f7ff39c6 | 521 | { |
c90cc42e | 522 | m_error = GSOCK_INVSOCK; |
f7ff39c6 DE |
523 | return -1; |
524 | } | |
525 | ||
526 | /* If the socket is blocking, wait for data (with a timeout) */ | |
c90cc42e | 527 | if (Input_Timeout() == GSOCK_TIMEDOUT) |
976abb72 VZ |
528 | { |
529 | m_error = GSOCK_TIMEDOUT; | |
f7ff39c6 | 530 | return -1; |
976abb72 | 531 | } |
f7ff39c6 DE |
532 | |
533 | /* Read the data */ | |
c90cc42e DE |
534 | if (m_stream) |
535 | ret = Recv_Stream(buffer, size); | |
f7ff39c6 | 536 | else |
c90cc42e | 537 | ret = Recv_Dgram(buffer, size); |
f7ff39c6 DE |
538 | |
539 | if (ret == SOCKET_ERROR) | |
540 | { | |
541 | if (WSAGetLastError() != WSAEWOULDBLOCK) | |
c90cc42e | 542 | m_error = GSOCK_IOERR; |
f7ff39c6 | 543 | else |
c90cc42e | 544 | m_error = GSOCK_WOULDBLOCK; |
f7ff39c6 DE |
545 | return -1; |
546 | } | |
547 | ||
548 | return ret; | |
549 | } | |
550 | ||
c90cc42e | 551 | int GSocket::Write(const char *buffer, int size) |
f7ff39c6 DE |
552 | { |
553 | int ret; | |
554 | ||
c90cc42e | 555 | if (m_fd == INVALID_SOCKET || m_server) |
f7ff39c6 | 556 | { |
c90cc42e | 557 | m_error = GSOCK_INVSOCK; |
f7ff39c6 DE |
558 | return -1; |
559 | } | |
560 | ||
561 | /* If the socket is blocking, wait for writability (with a timeout) */ | |
c90cc42e | 562 | if (Output_Timeout() == GSOCK_TIMEDOUT) |
f7ff39c6 DE |
563 | return -1; |
564 | ||
565 | /* Write the data */ | |
c90cc42e DE |
566 | if (m_stream) |
567 | ret = Send_Stream(buffer, size); | |
f7ff39c6 | 568 | else |
c90cc42e | 569 | ret = Send_Dgram(buffer, size); |
f7ff39c6 DE |
570 | |
571 | if (ret == SOCKET_ERROR) | |
572 | { | |
573 | if (WSAGetLastError() != WSAEWOULDBLOCK) | |
c90cc42e | 574 | m_error = GSOCK_IOERR; |
f7ff39c6 | 575 | else |
c90cc42e | 576 | m_error = GSOCK_WOULDBLOCK; |
f7ff39c6 DE |
577 | |
578 | /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect | |
579 | * does). Once the first OUTPUT event is received, users can assume | |
580 | * that the socket is writable until a read operation fails. Only then | |
581 | * will further OUTPUT events be posted. | |
582 | */ | |
c90cc42e | 583 | m_detected &= ~GSOCK_OUTPUT_FLAG; |
f7ff39c6 DE |
584 | return -1; |
585 | } | |
586 | ||
587 | return ret; | |
588 | } | |
589 | ||
f7ff39c6 DE |
590 | /* Attributes */ |
591 | ||
592 | /* GSocket_SetNonBlocking: | |
593 | * Sets the socket to non-blocking mode. All IO calls will return | |
594 | * immediately. | |
595 | */ | |
c90cc42e | 596 | void GSocket::SetNonBlocking(bool non_block) |
f7ff39c6 | 597 | { |
c90cc42e | 598 | m_non_blocking = non_block; |
f7ff39c6 DE |
599 | } |
600 | ||
f7ff39c6 | 601 | /* GSocket_GetError: |
3103e8a9 | 602 | * Returns the last error occurred for this socket. Note that successful |
f7ff39c6 DE |
603 | * operations do not clear this back to GSOCK_NOERROR, so use it only |
604 | * after an error. | |
605 | */ | |
a4506848 | 606 | GSocketError WXDLLIMPEXP_NET GSocket::GetError() |
f7ff39c6 | 607 | { |
c90cc42e | 608 | return m_error; |
f7ff39c6 DE |
609 | } |
610 | ||
c90cc42e | 611 | GSocketError GSocket::GetSockOpt(int level, int optname, |
f7ff39c6 DE |
612 | void *optval, int *optlen) |
613 | { | |
c90cc42e | 614 | if (getsockopt(m_fd, level, optname, (char*)optval, optlen) == 0) |
f7ff39c6 DE |
615 | { |
616 | return GSOCK_NOERROR; | |
617 | } | |
618 | return GSOCK_OPTERR; | |
619 | } | |
620 | ||
c90cc42e | 621 | GSocketError GSocket::SetSockOpt(int level, int optname, |
f7ff39c6 DE |
622 | const void *optval, int optlen) |
623 | { | |
c90cc42e | 624 | if (setsockopt(m_fd, level, optname, (char*)optval, optlen) == 0) |
f7ff39c6 DE |
625 | { |
626 | return GSOCK_NOERROR; | |
627 | } | |
628 | return GSOCK_OPTERR; | |
629 | } | |
630 | ||
631 | /* Internals (IO) */ | |
632 | ||
633 | /* _GSocket_Input_Timeout: | |
634 | * For blocking sockets, wait until data is available or | |
635 | * until timeout ellapses. | |
636 | */ | |
c90cc42e | 637 | GSocketError GSocket::Input_Timeout() |
f7ff39c6 DE |
638 | { |
639 | fd_set readfds; | |
640 | ||
c90cc42e | 641 | if (!m_non_blocking) |
f7ff39c6 DE |
642 | { |
643 | FD_ZERO(&readfds); | |
c90cc42e DE |
644 | FD_SET(m_fd, &readfds); |
645 | if (select(0, &readfds, NULL, NULL, &m_timeout) == 0) | |
f7ff39c6 | 646 | { |
c90cc42e | 647 | m_error = GSOCK_TIMEDOUT; |
f7ff39c6 DE |
648 | return GSOCK_TIMEDOUT; |
649 | } | |
650 | } | |
651 | return GSOCK_NOERROR; | |
652 | } | |
653 | ||
654 | /* _GSocket_Output_Timeout: | |
655 | * For blocking sockets, wait until data can be sent without | |
656 | * blocking or until timeout ellapses. | |
657 | */ | |
c90cc42e | 658 | GSocketError GSocket::Output_Timeout() |
f7ff39c6 DE |
659 | { |
660 | fd_set writefds; | |
661 | ||
c90cc42e | 662 | if (!m_non_blocking) |
f7ff39c6 DE |
663 | { |
664 | FD_ZERO(&writefds); | |
c90cc42e DE |
665 | FD_SET(m_fd, &writefds); |
666 | if (select(0, NULL, &writefds, NULL, &m_timeout) == 0) | |
f7ff39c6 | 667 | { |
c90cc42e | 668 | m_error = GSOCK_TIMEDOUT; |
f7ff39c6 DE |
669 | return GSOCK_TIMEDOUT; |
670 | } | |
671 | } | |
672 | return GSOCK_NOERROR; | |
673 | } | |
674 | ||
675 | /* _GSocket_Connect_Timeout: | |
676 | * For blocking sockets, wait until the connection is | |
677 | * established or fails, or until timeout ellapses. | |
678 | */ | |
c90cc42e | 679 | GSocketError GSocket::Connect_Timeout() |
f7ff39c6 DE |
680 | { |
681 | fd_set writefds; | |
682 | fd_set exceptfds; | |
683 | ||
684 | FD_ZERO(&writefds); | |
685 | FD_ZERO(&exceptfds); | |
c90cc42e DE |
686 | FD_SET(m_fd, &writefds); |
687 | FD_SET(m_fd, &exceptfds); | |
688 | if (select(0, NULL, &writefds, &exceptfds, &m_timeout) == 0) | |
f7ff39c6 | 689 | { |
c90cc42e | 690 | m_error = GSOCK_TIMEDOUT; |
f7ff39c6 DE |
691 | return GSOCK_TIMEDOUT; |
692 | } | |
c90cc42e | 693 | if (!FD_ISSET(m_fd, &writefds)) |
f7ff39c6 | 694 | { |
c90cc42e | 695 | m_error = GSOCK_IOERR; |
f7ff39c6 DE |
696 | return GSOCK_IOERR; |
697 | } | |
698 | ||
699 | return GSOCK_NOERROR; | |
700 | } | |
701 | ||
c90cc42e | 702 | int GSocket::Recv_Stream(char *buffer, int size) |
f7ff39c6 | 703 | { |
c90cc42e | 704 | return recv(m_fd, buffer, size, 0); |
f7ff39c6 DE |
705 | } |
706 | ||
c90cc42e | 707 | int GSocket::Recv_Dgram(char *buffer, int size) |
f7ff39c6 | 708 | { |
8575ff50 | 709 | wxSockAddr from; |
9e03e02d | 710 | WX_SOCKLEN_T fromlen = sizeof(from); |
f7ff39c6 DE |
711 | int ret; |
712 | GSocketError err; | |
713 | ||
8575ff50 | 714 | ret = recvfrom(m_fd, buffer, size, 0, (sockaddr*)&from, &fromlen); |
f7ff39c6 DE |
715 | |
716 | if (ret == SOCKET_ERROR) | |
717 | return SOCKET_ERROR; | |
718 | ||
719 | /* Translate a system address into a GSocket address */ | |
c90cc42e | 720 | if (!m_peer) |
f7ff39c6 | 721 | { |
c90cc42e DE |
722 | m_peer = GAddress_new(); |
723 | if (!m_peer) | |
f7ff39c6 | 724 | { |
c90cc42e | 725 | m_error = GSOCK_MEMERR; |
f7ff39c6 DE |
726 | return -1; |
727 | } | |
728 | } | |
8575ff50 | 729 | err = _GAddress_translate_from(m_peer, (sockaddr*)&from, fromlen); |
f7ff39c6 DE |
730 | if (err != GSOCK_NOERROR) |
731 | { | |
c90cc42e DE |
732 | GAddress_destroy(m_peer); |
733 | m_peer = NULL; | |
734 | m_error = err; | |
f7ff39c6 DE |
735 | return -1; |
736 | } | |
737 | ||
738 | return ret; | |
739 | } | |
740 | ||
c90cc42e | 741 | int GSocket::Send_Stream(const char *buffer, int size) |
f7ff39c6 | 742 | { |
c90cc42e | 743 | return send(m_fd, buffer, size, 0); |
f7ff39c6 DE |
744 | } |
745 | ||
c90cc42e | 746 | int GSocket::Send_Dgram(const char *buffer, int size) |
f7ff39c6 DE |
747 | { |
748 | struct sockaddr *addr; | |
749 | int len, ret; | |
750 | GSocketError err; | |
751 | ||
c90cc42e | 752 | if (!m_peer) |
f7ff39c6 | 753 | { |
c90cc42e | 754 | m_error = GSOCK_INVADDR; |
f7ff39c6 DE |
755 | return -1; |
756 | } | |
757 | ||
c90cc42e | 758 | err = _GAddress_translate_to(m_peer, &addr, &len); |
f7ff39c6 DE |
759 | if (err != GSOCK_NOERROR) |
760 | { | |
c90cc42e | 761 | m_error = err; |
f7ff39c6 DE |
762 | return -1; |
763 | } | |
764 | ||
c90cc42e | 765 | ret = sendto(m_fd, buffer, size, 0, addr, len); |
f7ff39c6 DE |
766 | |
767 | /* Frees memory allocated by _GAddress_translate_to */ | |
768 | free(addr); | |
769 | ||
770 | return ret; | |
771 | } | |
772 | ||
f7ff39c6 DE |
773 | /* |
774 | * ------------------------------------------------------------------------- | |
775 | * GAddress | |
776 | * ------------------------------------------------------------------------- | |
777 | */ | |
778 | ||
779 | /* CHECK_ADDRESS verifies that the current address family is either | |
780 | * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it | |
781 | * initalizes it to be a GSOCK_*family*. In other cases, it returns | |
782 | * an appropiate error code. | |
783 | * | |
784 | * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error. | |
785 | */ | |
786 | #define CHECK_ADDRESS(address, family) \ | |
787 | { \ | |
788 | if (address->m_family == GSOCK_NOFAMILY) \ | |
789 | if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \ | |
790 | return address->m_error; \ | |
791 | if (address->m_family != GSOCK_##family) \ | |
792 | { \ | |
793 | address->m_error = GSOCK_INVADDR; \ | |
794 | return GSOCK_INVADDR; \ | |
795 | } \ | |
796 | } | |
797 | ||
798 | #define CHECK_ADDRESS_RETVAL(address, family, retval) \ | |
799 | { \ | |
800 | if (address->m_family == GSOCK_NOFAMILY) \ | |
801 | if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \ | |
802 | return retval; \ | |
803 | if (address->m_family != GSOCK_##family) \ | |
804 | { \ | |
805 | address->m_error = GSOCK_INVADDR; \ | |
806 | return retval; \ | |
807 | } \ | |
808 | } | |
809 | ||
810 | ||
2804f77d | 811 | GAddress *GAddress_new() |
f7ff39c6 DE |
812 | { |
813 | GAddress *address; | |
814 | ||
815 | if ((address = (GAddress *) malloc(sizeof(GAddress))) == NULL) | |
816 | return NULL; | |
817 | ||
818 | address->m_family = GSOCK_NOFAMILY; | |
819 | address->m_addr = NULL; | |
820 | address->m_len = 0; | |
821 | ||
822 | return address; | |
823 | } | |
824 | ||
825 | GAddress *GAddress_copy(GAddress *address) | |
826 | { | |
827 | GAddress *addr2; | |
828 | ||
f7ff39c6 DE |
829 | if ((addr2 = (GAddress *) malloc(sizeof(GAddress))) == NULL) |
830 | return NULL; | |
831 | ||
832 | memcpy(addr2, address, sizeof(GAddress)); | |
833 | ||
834 | if (address->m_addr) | |
835 | { | |
836 | addr2->m_addr = (struct sockaddr *) malloc(addr2->m_len); | |
837 | if (addr2->m_addr == NULL) | |
838 | { | |
839 | free(addr2); | |
840 | return NULL; | |
841 | } | |
842 | memcpy(addr2->m_addr, address->m_addr, addr2->m_len); | |
843 | } | |
844 | ||
845 | return addr2; | |
846 | } | |
847 | ||
848 | void GAddress_destroy(GAddress *address) | |
849 | { | |
f7ff39c6 DE |
850 | if (address->m_addr) |
851 | free(address->m_addr); | |
852 | ||
853 | free(address); | |
854 | } | |
855 | ||
856 | void GAddress_SetFamily(GAddress *address, GAddressType type) | |
857 | { | |
f7ff39c6 DE |
858 | address->m_family = type; |
859 | } | |
860 | ||
861 | GAddressType GAddress_GetFamily(GAddress *address) | |
862 | { | |
f7ff39c6 DE |
863 | return address->m_family; |
864 | } | |
865 | ||
866 | GSocketError _GAddress_translate_from(GAddress *address, | |
867 | struct sockaddr *addr, int len) | |
868 | { | |
869 | address->m_realfamily = addr->sa_family; | |
870 | switch (addr->sa_family) | |
871 | { | |
872 | case AF_INET: | |
873 | address->m_family = GSOCK_INET; | |
874 | break; | |
875 | case AF_UNIX: | |
876 | address->m_family = GSOCK_UNIX; | |
877 | break; | |
8575ff50 | 878 | #if wxUSE_IPV6 |
f7ff39c6 DE |
879 | case AF_INET6: |
880 | address->m_family = GSOCK_INET6; | |
881 | break; | |
882 | #endif | |
883 | default: | |
884 | { | |
885 | address->m_error = GSOCK_INVOP; | |
886 | return GSOCK_INVOP; | |
887 | } | |
888 | } | |
889 | ||
890 | if (address->m_addr) | |
891 | free(address->m_addr); | |
892 | ||
893 | address->m_len = len; | |
894 | address->m_addr = (struct sockaddr *) malloc(len); | |
895 | ||
896 | if (address->m_addr == NULL) | |
897 | { | |
898 | address->m_error = GSOCK_MEMERR; | |
899 | return GSOCK_MEMERR; | |
900 | } | |
901 | memcpy(address->m_addr, addr, len); | |
902 | ||
903 | return GSOCK_NOERROR; | |
904 | } | |
905 | ||
906 | GSocketError _GAddress_translate_to(GAddress *address, | |
907 | struct sockaddr **addr, int *len) | |
908 | { | |
909 | if (!address->m_addr) | |
910 | { | |
911 | address->m_error = GSOCK_INVADDR; | |
912 | return GSOCK_INVADDR; | |
913 | } | |
914 | ||
915 | *len = address->m_len; | |
916 | *addr = (struct sockaddr *) malloc(address->m_len); | |
917 | if (*addr == NULL) | |
918 | { | |
919 | address->m_error = GSOCK_MEMERR; | |
920 | return GSOCK_MEMERR; | |
921 | } | |
922 | ||
923 | memcpy(*addr, address->m_addr, address->m_len); | |
924 | return GSOCK_NOERROR; | |
925 | } | |
926 | ||
927 | /* | |
928 | * ------------------------------------------------------------------------- | |
929 | * Internet address family | |
930 | * ------------------------------------------------------------------------- | |
931 | */ | |
932 | ||
933 | GSocketError _GAddress_Init_INET(GAddress *address) | |
934 | { | |
935 | address->m_len = sizeof(struct sockaddr_in); | |
936 | address->m_addr = (struct sockaddr *) malloc(address->m_len); | |
937 | if (address->m_addr == NULL) | |
938 | { | |
939 | address->m_error = GSOCK_MEMERR; | |
940 | return GSOCK_MEMERR; | |
941 | } | |
942 | ||
943 | address->m_family = GSOCK_INET; | |
944 | address->m_realfamily = AF_INET; | |
945 | ((struct sockaddr_in *)address->m_addr)->sin_family = AF_INET; | |
946 | ((struct sockaddr_in *)address->m_addr)->sin_addr.s_addr = INADDR_ANY; | |
947 | ||
948 | return GSOCK_NOERROR; | |
949 | } | |
950 | ||
951 | GSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname) | |
952 | { | |
953 | struct hostent *he; | |
954 | struct in_addr *addr; | |
955 | ||
f7ff39c6 DE |
956 | CHECK_ADDRESS(address, INET); |
957 | ||
958 | addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr); | |
959 | ||
960 | addr->s_addr = inet_addr(hostname); | |
961 | ||
962 | /* If it is a numeric host name, convert it now */ | |
963 | if (addr->s_addr == INADDR_NONE) | |
964 | { | |
965 | struct in_addr *array_addr; | |
966 | ||
967 | /* It is a real name, we solve it */ | |
968 | if ((he = gethostbyname(hostname)) == NULL) | |
969 | { | |
970 | /* addr->s_addr = INADDR_NONE just done by inet_addr() above */ | |
971 | address->m_error = GSOCK_NOHOST; | |
972 | return GSOCK_NOHOST; | |
973 | } | |
974 | array_addr = (struct in_addr *) *(he->h_addr_list); | |
975 | addr->s_addr = array_addr[0].s_addr; | |
976 | } | |
977 | return GSOCK_NOERROR; | |
978 | } | |
979 | ||
60edcf45 VZ |
980 | GSocketError GAddress_INET_SetBroadcastAddress(GAddress *address) |
981 | { | |
982 | return GAddress_INET_SetHostAddress(address, INADDR_BROADCAST); | |
983 | } | |
984 | ||
f7ff39c6 DE |
985 | GSocketError GAddress_INET_SetAnyAddress(GAddress *address) |
986 | { | |
987 | return GAddress_INET_SetHostAddress(address, INADDR_ANY); | |
988 | } | |
989 | ||
990 | GSocketError GAddress_INET_SetHostAddress(GAddress *address, | |
991 | unsigned long hostaddr) | |
992 | { | |
993 | struct in_addr *addr; | |
994 | ||
f7ff39c6 DE |
995 | CHECK_ADDRESS(address, INET); |
996 | ||
997 | addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr); | |
d0ee33f5 | 998 | addr->s_addr = htonl(hostaddr); |
f7ff39c6 DE |
999 | |
1000 | return GSOCK_NOERROR; | |
1001 | } | |
1002 | ||
1003 | GSocketError GAddress_INET_SetPortName(GAddress *address, const char *port, | |
1004 | const char *protocol) | |
1005 | { | |
1006 | struct servent *se; | |
1007 | struct sockaddr_in *addr; | |
1008 | ||
f7ff39c6 DE |
1009 | CHECK_ADDRESS(address, INET); |
1010 | ||
1011 | if (!port) | |
1012 | { | |
1013 | address->m_error = GSOCK_INVPORT; | |
1014 | return GSOCK_INVPORT; | |
1015 | } | |
1016 | ||
1017 | se = getservbyname(port, protocol); | |
1018 | if (!se) | |
1019 | { | |
1020 | if (isdigit(port[0])) | |
1021 | { | |
1022 | int port_int; | |
1023 | ||
1024 | port_int = atoi(port); | |
1025 | addr = (struct sockaddr_in *)address->m_addr; | |
1026 | addr->sin_port = htons((u_short) port_int); | |
1027 | return GSOCK_NOERROR; | |
1028 | } | |
1029 | ||
1030 | address->m_error = GSOCK_INVPORT; | |
1031 | return GSOCK_INVPORT; | |
1032 | } | |
1033 | ||
1034 | addr = (struct sockaddr_in *)address->m_addr; | |
1035 | addr->sin_port = se->s_port; | |
1036 | ||
1037 | return GSOCK_NOERROR; | |
1038 | } | |
1039 | ||
1040 | GSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port) | |
1041 | { | |
1042 | struct sockaddr_in *addr; | |
1043 | ||
f7ff39c6 DE |
1044 | CHECK_ADDRESS(address, INET); |
1045 | ||
1046 | addr = (struct sockaddr_in *)address->m_addr; | |
1047 | addr->sin_port = htons(port); | |
1048 | ||
1049 | return GSOCK_NOERROR; | |
1050 | } | |
1051 | ||
1052 | GSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname, size_t sbuf) | |
1053 | { | |
1054 | struct hostent *he; | |
1055 | char *addr_buf; | |
1056 | struct sockaddr_in *addr; | |
1057 | ||
f7ff39c6 DE |
1058 | CHECK_ADDRESS(address, INET); |
1059 | ||
1060 | addr = (struct sockaddr_in *)address->m_addr; | |
1061 | addr_buf = (char *)&(addr->sin_addr); | |
1062 | ||
1063 | he = gethostbyaddr(addr_buf, sizeof(addr->sin_addr), AF_INET); | |
1064 | if (he == NULL) | |
1065 | { | |
1066 | address->m_error = GSOCK_NOHOST; | |
1067 | return GSOCK_NOHOST; | |
1068 | } | |
1069 | ||
1070 | strncpy(hostname, he->h_name, sbuf); | |
1071 | ||
1072 | return GSOCK_NOERROR; | |
1073 | } | |
1074 | ||
1075 | unsigned long GAddress_INET_GetHostAddress(GAddress *address) | |
1076 | { | |
1077 | struct sockaddr_in *addr; | |
1078 | ||
f7ff39c6 DE |
1079 | CHECK_ADDRESS_RETVAL(address, INET, 0); |
1080 | ||
1081 | addr = (struct sockaddr_in *)address->m_addr; | |
1082 | ||
1083 | return ntohl(addr->sin_addr.s_addr); | |
1084 | } | |
1085 | ||
1086 | unsigned short GAddress_INET_GetPort(GAddress *address) | |
1087 | { | |
1088 | struct sockaddr_in *addr; | |
1089 | ||
f7ff39c6 DE |
1090 | CHECK_ADDRESS_RETVAL(address, INET, 0); |
1091 | ||
1092 | addr = (struct sockaddr_in *)address->m_addr; | |
1093 | return ntohs(addr->sin_port); | |
1094 | } | |
1095 | ||
8575ff50 VZ |
1096 | |
1097 | #if wxUSE_IPV6 | |
1098 | /* | |
1099 | * ------------------------------------------------------------------------- | |
1100 | * Internet IPv6 address family | |
1101 | * ------------------------------------------------------------------------- | |
1102 | */ | |
1103 | #include "ws2tcpip.h" | |
1104 | ||
1105 | #ifdef __VISUALC__ | |
1106 | #pragma comment(lib,"ws2_32") | |
1107 | #endif // __VISUALC__ | |
1108 | ||
1109 | GSocketError _GAddress_Init_INET6(GAddress *address) | |
1110 | { | |
1111 | struct in6_addr any_address = IN6ADDR_ANY_INIT; | |
1112 | address->m_len = sizeof(struct sockaddr_in6); | |
1113 | address->m_addr = (struct sockaddr *) malloc(address->m_len); | |
1114 | if (address->m_addr == NULL) | |
1115 | { | |
1116 | address->m_error = GSOCK_MEMERR; | |
1117 | return GSOCK_MEMERR; | |
1118 | } | |
1119 | memset(address->m_addr,0,address->m_len); | |
1120 | ||
1121 | address->m_family = GSOCK_INET6; | |
1122 | address->m_realfamily = AF_INET6; | |
1123 | ((struct sockaddr_in6 *)address->m_addr)->sin6_family = AF_INET6; | |
1124 | ((struct sockaddr_in6 *)address->m_addr)->sin6_addr = any_address; | |
1125 | ||
1126 | return GSOCK_NOERROR; | |
1127 | } | |
1128 | ||
1129 | GSocketError GAddress_INET6_SetHostName(GAddress *address, const char *hostname) | |
1130 | { | |
8575ff50 VZ |
1131 | CHECK_ADDRESS(address, INET6); |
1132 | ||
1133 | addrinfo hints; | |
1134 | memset( & hints, 0, sizeof( hints ) ); | |
1135 | hints.ai_family = AF_INET6; | |
1136 | addrinfo * info = 0; | |
1137 | if ( getaddrinfo( hostname, "0", & hints, & info ) || ! info ) | |
1138 | { | |
1139 | address->m_error = GSOCK_NOHOST; | |
1140 | return GSOCK_NOHOST; | |
1141 | } | |
1142 | ||
1143 | memcpy( address->m_addr, info->ai_addr, info->ai_addrlen ); | |
1144 | freeaddrinfo( info ); | |
1145 | return GSOCK_NOERROR; | |
1146 | } | |
1147 | ||
1148 | GSocketError GAddress_INET6_SetAnyAddress(GAddress *address) | |
1149 | { | |
8575ff50 VZ |
1150 | CHECK_ADDRESS(address, INET6); |
1151 | ||
1152 | struct in6_addr addr; | |
1153 | memset( & addr, 0, sizeof( addr ) ); | |
1154 | return GAddress_INET6_SetHostAddress(address, addr); | |
1155 | } | |
1156 | GSocketError GAddress_INET6_SetHostAddress(GAddress *address, | |
1157 | struct in6_addr hostaddr) | |
1158 | { | |
8575ff50 VZ |
1159 | CHECK_ADDRESS(address, INET6); |
1160 | ||
1161 | ((struct sockaddr_in6 *)address->m_addr)->sin6_addr = hostaddr; | |
1162 | ||
1163 | return GSOCK_NOERROR; | |
1164 | } | |
1165 | ||
1166 | GSocketError GAddress_INET6_SetPortName(GAddress *address, const char *port, | |
1167 | const char *protocol) | |
1168 | { | |
1169 | struct servent *se; | |
1170 | struct sockaddr_in6 *addr; | |
1171 | ||
8575ff50 VZ |
1172 | CHECK_ADDRESS(address, INET6); |
1173 | ||
1174 | if (!port) | |
1175 | { | |
1176 | address->m_error = GSOCK_INVPORT; | |
1177 | return GSOCK_INVPORT; | |
1178 | } | |
1179 | ||
1180 | se = getservbyname(port, protocol); | |
1181 | if (!se) | |
1182 | { | |
8a5a7042 | 1183 | if (isdigit((unsigned char) port[0])) |
8575ff50 VZ |
1184 | { |
1185 | int port_int; | |
1186 | ||
1187 | port_int = atoi(port); | |
1188 | addr = (struct sockaddr_in6 *)address->m_addr; | |
1189 | addr->sin6_port = htons((u_short) port_int); | |
1190 | return GSOCK_NOERROR; | |
1191 | } | |
1192 | ||
1193 | address->m_error = GSOCK_INVPORT; | |
1194 | return GSOCK_INVPORT; | |
1195 | } | |
1196 | ||
1197 | addr = (struct sockaddr_in6 *)address->m_addr; | |
1198 | addr->sin6_port = se->s_port; | |
1199 | ||
1200 | return GSOCK_NOERROR; | |
1201 | } | |
1202 | ||
1203 | GSocketError GAddress_INET6_SetPort(GAddress *address, unsigned short port) | |
1204 | { | |
1205 | struct sockaddr_in6 *addr; | |
1206 | ||
8575ff50 VZ |
1207 | CHECK_ADDRESS(address, INET6); |
1208 | ||
1209 | addr = (struct sockaddr_in6 *)address->m_addr; | |
1210 | addr->sin6_port = htons(port); | |
1211 | ||
1212 | return GSOCK_NOERROR; | |
1213 | } | |
1214 | ||
1215 | GSocketError GAddress_INET6_GetHostName(GAddress *address, char *hostname, size_t sbuf) | |
1216 | { | |
1217 | struct hostent *he; | |
1218 | char *addr_buf; | |
1219 | struct sockaddr_in6 *addr; | |
1220 | ||
8575ff50 VZ |
1221 | CHECK_ADDRESS(address, INET6); |
1222 | ||
1223 | addr = (struct sockaddr_in6 *)address->m_addr; | |
1224 | addr_buf = (char *)&(addr->sin6_addr); | |
1225 | ||
1226 | he = gethostbyaddr(addr_buf, sizeof(addr->sin6_addr), AF_INET6); | |
1227 | if (he == NULL) | |
1228 | { | |
1229 | address->m_error = GSOCK_NOHOST; | |
1230 | return GSOCK_NOHOST; | |
1231 | } | |
1232 | ||
1233 | strncpy(hostname, he->h_name, sbuf); | |
1234 | ||
1235 | return GSOCK_NOERROR; | |
1236 | } | |
1237 | ||
1238 | GSocketError GAddress_INET6_GetHostAddress(GAddress *address,struct in6_addr *hostaddr) | |
1239 | { | |
8575ff50 VZ |
1240 | CHECK_ADDRESS_RETVAL(address, INET6, GSOCK_INVADDR); |
1241 | *hostaddr = ( (struct sockaddr_in6 *)address->m_addr )->sin6_addr; | |
1242 | return GSOCK_NOERROR; | |
1243 | } | |
1244 | ||
1245 | unsigned short GAddress_INET6_GetPort(GAddress *address) | |
1246 | { | |
8575ff50 VZ |
1247 | CHECK_ADDRESS_RETVAL(address, INET6, 0); |
1248 | ||
1249 | return ntohs( ((struct sockaddr_in6 *)address->m_addr)->sin6_port ); | |
1250 | } | |
1251 | ||
1252 | #endif // wxUSE_IPV6 | |
1253 | ||
f7ff39c6 DE |
1254 | /* |
1255 | * ------------------------------------------------------------------------- | |
1256 | * Unix address family | |
1257 | * ------------------------------------------------------------------------- | |
1258 | */ | |
1259 | ||
1260 | GSocketError _GAddress_Init_UNIX(GAddress *address) | |
1261 | { | |
f7ff39c6 DE |
1262 | address->m_error = GSOCK_INVADDR; |
1263 | return GSOCK_INVADDR; | |
1264 | } | |
1265 | ||
907173e5 | 1266 | GSocketError GAddress_UNIX_SetPath(GAddress *address, const char *WXUNUSED(path)) |
f7ff39c6 | 1267 | { |
f7ff39c6 DE |
1268 | address->m_error = GSOCK_INVADDR; |
1269 | return GSOCK_INVADDR; | |
1270 | } | |
1271 | ||
907173e5 | 1272 | GSocketError GAddress_UNIX_GetPath(GAddress *address, char *WXUNUSED(path), size_t WXUNUSED(sbuf)) |
f7ff39c6 | 1273 | { |
f7ff39c6 DE |
1274 | address->m_error = GSOCK_INVADDR; |
1275 | return GSOCK_INVADDR; | |
1276 | } | |
1277 | ||
2804f77d | 1278 | #endif // wxUSE_SOCKETS |