]>
Commit | Line | Data |
---|---|---|
1 | /* ------------------------------------------------------------------------- | |
2 | * Project: GSocket (Generic Socket) | |
3 | * Name: src/msw/gsocket.cpp | |
4 | * Copyright: (c) Guilhem Lavaux | |
5 | * Licence: wxWindows Licence | |
6 | * Author: Guillermo Rodriguez Garcia <guille@iies.es> | |
7 | * Purpose: GSocket main MSW file | |
8 | * Licence: The wxWindows licence | |
9 | * CVSID: $Id$ | |
10 | * ------------------------------------------------------------------------- | |
11 | */ | |
12 | ||
13 | // For compilers that support precompilation, includes "wx.h". | |
14 | #include "wx/wxprec.h" | |
15 | ||
16 | #ifdef __BORLANDC__ | |
17 | #pragma hdrstop | |
18 | #endif | |
19 | ||
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 | ||
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 | ||
54 | #include <winsock.h> | |
55 | ||
56 | #include "wx/platform.h" | |
57 | ||
58 | #if wxUSE_SOCKETS | |
59 | ||
60 | #include "wx/gsocket.h" | |
61 | #include "wx/link.h" | |
62 | ||
63 | wxFORCE_LINK_MODULE(gsockmsw) | |
64 | ||
65 | #ifdef __WXWINCE__ | |
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 | ||
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 | |
81 | #endif | |
82 | ||
83 | #if wxUSE_IPV6 | |
84 | typedef struct sockaddr_storage wxSockAddr; | |
85 | #else | |
86 | typedef struct sockaddr wxSockAddr; | |
87 | #endif | |
88 | ||
89 | bool GSocket_Init() | |
90 | { | |
91 | WSADATA wsaData; | |
92 | ||
93 | GSocketManager * const manager = GSocketManager::Get(); | |
94 | if ( !manager || !manager->OnInit() ) | |
95 | return false; | |
96 | ||
97 | /* Initialize WinSocket */ | |
98 | return WSAStartup((1 << 8) | 1, &wsaData) == 0; | |
99 | } | |
100 | ||
101 | void GSocket_Cleanup() | |
102 | { | |
103 | GSocketManager * const manager = GSocketManager::Get(); | |
104 | if ( manager ) | |
105 | manager->OnExit(); | |
106 | ||
107 | /* Cleanup WinSocket */ | |
108 | WSACleanup(); | |
109 | } | |
110 | ||
111 | /* Constructors / Destructors for GSocket */ | |
112 | ||
113 | GSocket::GSocket() | |
114 | { | |
115 | int i; | |
116 | ||
117 | m_fd = INVALID_SOCKET; | |
118 | for (i = 0; i < GSOCK_MAX_EVENT; i++) | |
119 | { | |
120 | m_cbacks[i] = NULL; | |
121 | } | |
122 | m_detected = 0; | |
123 | m_local = NULL; | |
124 | m_peer = NULL; | |
125 | m_error = GSOCK_NOERROR; | |
126 | m_server = false; | |
127 | m_stream = true; | |
128 | m_non_blocking = false; | |
129 | m_timeout.tv_sec = 10 * 60; /* 10 minutes */ | |
130 | m_timeout.tv_usec = 0; | |
131 | m_establishing = false; | |
132 | m_reusable = false; | |
133 | m_broadcast = false; | |
134 | m_dobind = true; | |
135 | m_initialRecvBufferSize = -1; | |
136 | m_initialSendBufferSize = -1; | |
137 | ||
138 | m_ok = GSocketManager::Get()->Init_Socket(this); | |
139 | } | |
140 | ||
141 | void GSocket::Close() | |
142 | { | |
143 | GSocketManager::Get()->Disable_Events(this); | |
144 | closesocket(m_fd); | |
145 | m_fd = INVALID_SOCKET; | |
146 | } | |
147 | ||
148 | GSocket::~GSocket() | |
149 | { | |
150 | GSocketManager::Get()->Destroy_Socket(this); | |
151 | ||
152 | /* Check that the socket is really shutdowned */ | |
153 | if (m_fd != INVALID_SOCKET) | |
154 | Shutdown(); | |
155 | ||
156 | /* Destroy private addresses */ | |
157 | if (m_local) | |
158 | GAddress_destroy(m_local); | |
159 | ||
160 | if (m_peer) | |
161 | GAddress_destroy(m_peer); | |
162 | } | |
163 | ||
164 | /* GSocket_Shutdown: | |
165 | * Disallow further read/write operations on this socket, close | |
166 | * the fd and disable all callbacks. | |
167 | */ | |
168 | void GSocket::Shutdown() | |
169 | { | |
170 | int evt; | |
171 | ||
172 | /* If socket has been created, shutdown it */ | |
173 | if (m_fd != INVALID_SOCKET) | |
174 | { | |
175 | shutdown(m_fd, 1 /* SD_SEND */); | |
176 | Close(); | |
177 | } | |
178 | ||
179 | /* Disable GUI callbacks */ | |
180 | for (evt = 0; evt < GSOCK_MAX_EVENT; evt++) | |
181 | m_cbacks[evt] = NULL; | |
182 | ||
183 | m_detected = GSOCK_LOST_FLAG; | |
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 | */ | |
202 | GSocketError GSocket::SetLocal(GAddress *address) | |
203 | { | |
204 | /* the socket must be initialized, or it must be a server */ | |
205 | if (m_fd != INVALID_SOCKET && !m_server) | |
206 | { | |
207 | m_error = GSOCK_INVSOCK; | |
208 | return GSOCK_INVSOCK; | |
209 | } | |
210 | ||
211 | /* check address */ | |
212 | if (address == NULL || address->m_family == GSOCK_NOFAMILY) | |
213 | { | |
214 | m_error = GSOCK_INVADDR; | |
215 | return GSOCK_INVADDR; | |
216 | } | |
217 | ||
218 | if (m_local) | |
219 | GAddress_destroy(m_local); | |
220 | ||
221 | m_local = GAddress_copy(address); | |
222 | ||
223 | return GSOCK_NOERROR; | |
224 | } | |
225 | ||
226 | GSocketError GSocket::SetPeer(GAddress *address) | |
227 | { | |
228 | /* check address */ | |
229 | if (address == NULL || address->m_family == GSOCK_NOFAMILY) | |
230 | { | |
231 | m_error = GSOCK_INVADDR; | |
232 | return GSOCK_INVADDR; | |
233 | } | |
234 | ||
235 | if (m_peer) | |
236 | GAddress_destroy(m_peer); | |
237 | ||
238 | m_peer = GAddress_copy(address); | |
239 | ||
240 | return GSOCK_NOERROR; | |
241 | } | |
242 | ||
243 | GAddress *GSocket::GetLocal() | |
244 | { | |
245 | GAddress *address; | |
246 | wxSockAddr addr; | |
247 | WX_SOCKLEN_T size = sizeof(addr); | |
248 | GSocketError err; | |
249 | ||
250 | /* try to get it from the m_local var first */ | |
251 | if (m_local) | |
252 | return GAddress_copy(m_local); | |
253 | ||
254 | /* else, if the socket is initialized, try getsockname */ | |
255 | if (m_fd == INVALID_SOCKET) | |
256 | { | |
257 | m_error = GSOCK_INVSOCK; | |
258 | return NULL; | |
259 | } | |
260 | ||
261 | if (getsockname(m_fd, (sockaddr*)&addr, &size) == SOCKET_ERROR) | |
262 | { | |
263 | m_error = GSOCK_IOERR; | |
264 | return NULL; | |
265 | } | |
266 | ||
267 | /* got a valid address from getsockname, create a GAddress object */ | |
268 | if ((address = GAddress_new()) == NULL) | |
269 | { | |
270 | m_error = GSOCK_MEMERR; | |
271 | return NULL; | |
272 | } | |
273 | ||
274 | if ((err = _GAddress_translate_from(address, (sockaddr*)&addr, size)) != GSOCK_NOERROR) | |
275 | { | |
276 | GAddress_destroy(address); | |
277 | m_error = err; | |
278 | return NULL; | |
279 | } | |
280 | ||
281 | return address; | |
282 | } | |
283 | ||
284 | GAddress *GSocket::GetPeer() | |
285 | { | |
286 | /* try to get it from the m_peer var */ | |
287 | if (m_peer) | |
288 | return GAddress_copy(m_peer); | |
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 | */ | |
305 | GSocketError GSocket::SetServer() | |
306 | { | |
307 | u_long arg = 1; | |
308 | ||
309 | /* must not be in use */ | |
310 | if (m_fd != INVALID_SOCKET) | |
311 | { | |
312 | m_error = GSOCK_INVSOCK; | |
313 | return GSOCK_INVSOCK; | |
314 | } | |
315 | ||
316 | /* the local addr must have been set */ | |
317 | if (!m_local) | |
318 | { | |
319 | m_error = GSOCK_INVADDR; | |
320 | return GSOCK_INVADDR; | |
321 | } | |
322 | ||
323 | /* Initialize all fields */ | |
324 | m_server = true; | |
325 | m_stream = true; | |
326 | ||
327 | /* Create the socket */ | |
328 | m_fd = socket(m_local->m_realfamily, SOCK_STREAM, 0); | |
329 | ||
330 | if (m_fd == INVALID_SOCKET) | |
331 | { | |
332 | m_error = GSOCK_IOERR; | |
333 | return GSOCK_IOERR; | |
334 | } | |
335 | ||
336 | ioctlsocket(m_fd, FIONBIO, (u_long FAR *) &arg); | |
337 | GSocketManager::Get()->Enable_Events(this); | |
338 | ||
339 | /* allow a socket to re-bind if the socket is in the TIME_WAIT | |
340 | state after being previously closed. | |
341 | */ | |
342 | if (m_reusable) | |
343 | { | |
344 | setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&arg, sizeof(arg)); | |
345 | } | |
346 | ||
347 | /* Bind to the local address, | |
348 | * retrieve the actual address bound, | |
349 | * and listen up to 5 connections. | |
350 | */ | |
351 | if ((bind(m_fd, m_local->m_addr, m_local->m_len) != 0) || | |
352 | (getsockname(m_fd, | |
353 | m_local->m_addr, | |
354 | (WX_SOCKLEN_T *)&m_local->m_len) != 0) || | |
355 | (listen(m_fd, 5) != 0)) | |
356 | { | |
357 | Close(); | |
358 | m_error = GSOCK_IOERR; | |
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 | */ | |
377 | GSocket *GSocket::WaitConnection() | |
378 | { | |
379 | GSocket *connection; | |
380 | wxSockAddr from; | |
381 | WX_SOCKLEN_T fromlen = sizeof(from); | |
382 | GSocketError err; | |
383 | u_long arg = 1; | |
384 | ||
385 | /* Reenable CONNECTION events */ | |
386 | m_detected &= ~GSOCK_CONNECTION_FLAG; | |
387 | ||
388 | /* If the socket has already been created, we exit immediately */ | |
389 | if (m_fd == INVALID_SOCKET || !m_server) | |
390 | { | |
391 | m_error = GSOCK_INVSOCK; | |
392 | return NULL; | |
393 | } | |
394 | ||
395 | /* Create a GSocket object for the new connection */ | |
396 | connection = GSocket_new(); | |
397 | ||
398 | if (!connection) | |
399 | { | |
400 | m_error = GSOCK_MEMERR; | |
401 | return NULL; | |
402 | } | |
403 | ||
404 | /* Wait for a connection (with timeout) */ | |
405 | if (Input_Timeout() == GSOCK_TIMEDOUT) | |
406 | { | |
407 | delete connection; | |
408 | /* m_error set by _GSocket_Input_Timeout */ | |
409 | return NULL; | |
410 | } | |
411 | ||
412 | connection->m_fd = accept(m_fd, (sockaddr*)&from, &fromlen); | |
413 | ||
414 | if (connection->m_fd == INVALID_SOCKET) | |
415 | { | |
416 | if (WSAGetLastError() == WSAEWOULDBLOCK) | |
417 | m_error = GSOCK_WOULDBLOCK; | |
418 | else | |
419 | m_error = GSOCK_IOERR; | |
420 | ||
421 | delete connection; | |
422 | return NULL; | |
423 | } | |
424 | ||
425 | /* Initialize all fields */ | |
426 | connection->m_server = false; | |
427 | connection->m_stream = true; | |
428 | ||
429 | /* Setup the peer address field */ | |
430 | connection->m_peer = GAddress_new(); | |
431 | if (!connection->m_peer) | |
432 | { | |
433 | delete connection; | |
434 | m_error = GSOCK_MEMERR; | |
435 | return NULL; | |
436 | } | |
437 | err = _GAddress_translate_from(connection->m_peer, (sockaddr*)&from, fromlen); | |
438 | if (err != GSOCK_NOERROR) | |
439 | { | |
440 | GAddress_destroy(connection->m_peer); | |
441 | delete connection; | |
442 | m_error = err; | |
443 | return NULL; | |
444 | } | |
445 | ||
446 | ioctlsocket(connection->m_fd, FIONBIO, (u_long FAR *) &arg); | |
447 | GSocketManager::Get()->Enable_Events(connection); | |
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. | |
457 | * Returns true if the flag was set correctly, false if an error occurred | |
458 | * (ie, if the parameter was NULL) | |
459 | */ | |
460 | bool GSocket::SetReusable() | |
461 | { | |
462 | /* socket must not be null, and must not be in use/already bound */ | |
463 | if (this && m_fd == INVALID_SOCKET) { | |
464 | m_reusable = true; | |
465 | return true; | |
466 | } | |
467 | return false; | |
468 | } | |
469 | ||
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 | */ | |
478 | bool 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 | ||
488 | bool 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 | ||
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 | |
504 | * connection has been successfully established, or one of the error | |
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 | */ | |
523 | GSocketError GSocket::Connect(GSocketStream stream) | |
524 | { | |
525 | int ret, err; | |
526 | u_long arg = 1; | |
527 | ||
528 | /* Enable CONNECTION events (needed for nonblocking connections) */ | |
529 | m_detected &= ~GSOCK_CONNECTION_FLAG; | |
530 | ||
531 | if (m_fd != INVALID_SOCKET) | |
532 | { | |
533 | m_error = GSOCK_INVSOCK; | |
534 | return GSOCK_INVSOCK; | |
535 | } | |
536 | ||
537 | if (!m_peer) | |
538 | { | |
539 | m_error = GSOCK_INVADDR; | |
540 | return GSOCK_INVADDR; | |
541 | } | |
542 | ||
543 | /* Streamed or dgram socket? */ | |
544 | m_stream = (stream == GSOCK_STREAMED); | |
545 | m_server = false; | |
546 | m_establishing = false; | |
547 | ||
548 | /* Create the socket */ | |
549 | m_fd = socket(m_peer->m_realfamily, | |
550 | m_stream? SOCK_STREAM : SOCK_DGRAM, 0); | |
551 | ||
552 | if (m_fd == INVALID_SOCKET) | |
553 | { | |
554 | m_error = GSOCK_IOERR; | |
555 | return GSOCK_IOERR; | |
556 | } | |
557 | ||
558 | ioctlsocket(m_fd, FIONBIO, (u_long FAR *) &arg); | |
559 | GSocketManager::Get()->Enable_Events(this); | |
560 | ||
561 | // If the reuse flag is set, use the applicable socket reuse flag | |
562 | if (m_reusable) | |
563 | { | |
564 | setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&arg, sizeof(arg)); | |
565 | } | |
566 | ||
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 | ||
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 | ||
578 | /* Connect it to the peer address, with a timeout (see below) */ | |
579 | ret = connect(m_fd, m_peer->m_addr, m_peer->m_len); | |
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 | */ | |
590 | if ((err == WSAEWOULDBLOCK) && (!m_non_blocking)) | |
591 | { | |
592 | err = Connect_Timeout(); | |
593 | ||
594 | if (err != GSOCK_NOERROR) | |
595 | { | |
596 | Close(); | |
597 | /* m_error is set in _GSocket_Connect_Timeout */ | |
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 | */ | |
609 | if ((err == WSAEWOULDBLOCK) && (m_non_blocking)) | |
610 | { | |
611 | m_establishing = true; | |
612 | m_error = GSOCK_WOULDBLOCK; | |
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 | */ | |
619 | Close(); | |
620 | m_error = GSOCK_IOERR; | |
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 | */ | |
640 | GSocketError GSocket::SetNonOriented() | |
641 | { | |
642 | u_long arg = 1; | |
643 | ||
644 | if (m_fd != INVALID_SOCKET) | |
645 | { | |
646 | m_error = GSOCK_INVSOCK; | |
647 | return GSOCK_INVSOCK; | |
648 | } | |
649 | ||
650 | if (!m_local) | |
651 | { | |
652 | m_error = GSOCK_INVADDR; | |
653 | return GSOCK_INVADDR; | |
654 | } | |
655 | ||
656 | /* Initialize all fields */ | |
657 | m_stream = false; | |
658 | m_server = false; | |
659 | ||
660 | /* Create the socket */ | |
661 | m_fd = socket(m_local->m_realfamily, SOCK_DGRAM, 0); | |
662 | ||
663 | if (m_fd == INVALID_SOCKET) | |
664 | { | |
665 | m_error = GSOCK_IOERR; | |
666 | return GSOCK_IOERR; | |
667 | } | |
668 | ||
669 | ioctlsocket(m_fd, FIONBIO, (u_long FAR *) &arg); | |
670 | GSocketManager::Get()->Enable_Events(this); | |
671 | ||
672 | if (m_reusable) | |
673 | { | |
674 | setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&arg, sizeof(arg)); | |
675 | } | |
676 | if (m_broadcast) | |
677 | { | |
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 | } | |
694 | } | |
695 | ||
696 | return GSOCK_NOERROR; | |
697 | } | |
698 | ||
699 | /* Generic IO */ | |
700 | ||
701 | /* Like recv(), send(), ... */ | |
702 | int GSocket::Read(char *buffer, int size) | |
703 | { | |
704 | int ret; | |
705 | ||
706 | /* Reenable INPUT events */ | |
707 | m_detected &= ~GSOCK_INPUT_FLAG; | |
708 | ||
709 | if (m_fd == INVALID_SOCKET || m_server) | |
710 | { | |
711 | m_error = GSOCK_INVSOCK; | |
712 | return -1; | |
713 | } | |
714 | ||
715 | /* If the socket is blocking, wait for data (with a timeout) */ | |
716 | if (Input_Timeout() == GSOCK_TIMEDOUT) | |
717 | { | |
718 | m_error = GSOCK_TIMEDOUT; | |
719 | return -1; | |
720 | } | |
721 | ||
722 | /* Read the data */ | |
723 | if (m_stream) | |
724 | ret = Recv_Stream(buffer, size); | |
725 | else | |
726 | ret = Recv_Dgram(buffer, size); | |
727 | ||
728 | if (ret == SOCKET_ERROR) | |
729 | { | |
730 | if (WSAGetLastError() != WSAEWOULDBLOCK) | |
731 | m_error = GSOCK_IOERR; | |
732 | else | |
733 | m_error = GSOCK_WOULDBLOCK; | |
734 | return -1; | |
735 | } | |
736 | ||
737 | return ret; | |
738 | } | |
739 | ||
740 | int GSocket::Write(const char *buffer, int size) | |
741 | { | |
742 | int ret; | |
743 | ||
744 | if (m_fd == INVALID_SOCKET || m_server) | |
745 | { | |
746 | m_error = GSOCK_INVSOCK; | |
747 | return -1; | |
748 | } | |
749 | ||
750 | /* If the socket is blocking, wait for writability (with a timeout) */ | |
751 | if (Output_Timeout() == GSOCK_TIMEDOUT) | |
752 | return -1; | |
753 | ||
754 | /* Write the data */ | |
755 | if (m_stream) | |
756 | ret = Send_Stream(buffer, size); | |
757 | else | |
758 | ret = Send_Dgram(buffer, size); | |
759 | ||
760 | if (ret == SOCKET_ERROR) | |
761 | { | |
762 | if (WSAGetLastError() != WSAEWOULDBLOCK) | |
763 | m_error = GSOCK_IOERR; | |
764 | else | |
765 | m_error = GSOCK_WOULDBLOCK; | |
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 | */ | |
772 | m_detected &= ~GSOCK_OUTPUT_FLAG; | |
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 | */ | |
786 | GSocketEventFlags GSocket::Select(GSocketEventFlags flags) | |
787 | { | |
788 | return flags & m_detected; | |
789 | } | |
790 | ||
791 | /* Attributes */ | |
792 | ||
793 | /* GSocket_SetNonBlocking: | |
794 | * Sets the socket to non-blocking mode. All IO calls will return | |
795 | * immediately. | |
796 | */ | |
797 | void GSocket::SetNonBlocking(bool non_block) | |
798 | { | |
799 | m_non_blocking = non_block; | |
800 | } | |
801 | ||
802 | /* GSocket_SetTimeout: | |
803 | * Sets the timeout for blocking calls. Time is expressed in | |
804 | * milliseconds. | |
805 | */ | |
806 | void GSocket::SetTimeout(unsigned long millis) | |
807 | { | |
808 | m_timeout.tv_sec = (millis / 1000); | |
809 | m_timeout.tv_usec = (millis % 1000) * 1000; | |
810 | } | |
811 | ||
812 | /* GSocket_GetError: | |
813 | * Returns the last error occurred for this socket. Note that successful | |
814 | * operations do not clear this back to GSOCK_NOERROR, so use it only | |
815 | * after an error. | |
816 | */ | |
817 | GSocketError WXDLLIMPEXP_NET GSocket::GetError() | |
818 | { | |
819 | return m_error; | |
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: | |
836 | * Connection successfully established, for client sockets, or incoming | |
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 | */ | |
852 | void GSocket::SetCallback(GSocketEventFlags flags, | |
853 | GSocketCallback callback, char *cdata) | |
854 | { | |
855 | int count; | |
856 | ||
857 | for (count = 0; count < GSOCK_MAX_EVENT; count++) | |
858 | { | |
859 | if ((flags & (1 << count)) != 0) | |
860 | { | |
861 | m_cbacks[count] = callback; | |
862 | m_data[count] = cdata; | |
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 | */ | |
871 | void GSocket::UnsetCallback(GSocketEventFlags flags) | |
872 | { | |
873 | int count; | |
874 | ||
875 | for (count = 0; count < GSOCK_MAX_EVENT; count++) | |
876 | { | |
877 | if ((flags & (1 << count)) != 0) | |
878 | { | |
879 | m_cbacks[count] = NULL; | |
880 | m_data[count] = NULL; | |
881 | } | |
882 | } | |
883 | } | |
884 | ||
885 | GSocketError GSocket::GetSockOpt(int level, int optname, | |
886 | void *optval, int *optlen) | |
887 | { | |
888 | if (getsockopt(m_fd, level, optname, (char*)optval, optlen) == 0) | |
889 | { | |
890 | return GSOCK_NOERROR; | |
891 | } | |
892 | return GSOCK_OPTERR; | |
893 | } | |
894 | ||
895 | GSocketError GSocket::SetSockOpt(int level, int optname, | |
896 | const void *optval, int optlen) | |
897 | { | |
898 | if (setsockopt(m_fd, level, optname, (char*)optval, optlen) == 0) | |
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 | */ | |
911 | GSocketError GSocket::Input_Timeout() | |
912 | { | |
913 | fd_set readfds; | |
914 | ||
915 | if (!m_non_blocking) | |
916 | { | |
917 | FD_ZERO(&readfds); | |
918 | FD_SET(m_fd, &readfds); | |
919 | if (select(0, &readfds, NULL, NULL, &m_timeout) == 0) | |
920 | { | |
921 | m_error = GSOCK_TIMEDOUT; | |
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 | */ | |
932 | GSocketError GSocket::Output_Timeout() | |
933 | { | |
934 | fd_set writefds; | |
935 | ||
936 | if (!m_non_blocking) | |
937 | { | |
938 | FD_ZERO(&writefds); | |
939 | FD_SET(m_fd, &writefds); | |
940 | if (select(0, NULL, &writefds, NULL, &m_timeout) == 0) | |
941 | { | |
942 | m_error = GSOCK_TIMEDOUT; | |
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 | */ | |
953 | GSocketError GSocket::Connect_Timeout() | |
954 | { | |
955 | fd_set writefds; | |
956 | fd_set exceptfds; | |
957 | ||
958 | FD_ZERO(&writefds); | |
959 | FD_ZERO(&exceptfds); | |
960 | FD_SET(m_fd, &writefds); | |
961 | FD_SET(m_fd, &exceptfds); | |
962 | if (select(0, NULL, &writefds, &exceptfds, &m_timeout) == 0) | |
963 | { | |
964 | m_error = GSOCK_TIMEDOUT; | |
965 | return GSOCK_TIMEDOUT; | |
966 | } | |
967 | if (!FD_ISSET(m_fd, &writefds)) | |
968 | { | |
969 | m_error = GSOCK_IOERR; | |
970 | return GSOCK_IOERR; | |
971 | } | |
972 | ||
973 | return GSOCK_NOERROR; | |
974 | } | |
975 | ||
976 | int GSocket::Recv_Stream(char *buffer, int size) | |
977 | { | |
978 | return recv(m_fd, buffer, size, 0); | |
979 | } | |
980 | ||
981 | int GSocket::Recv_Dgram(char *buffer, int size) | |
982 | { | |
983 | wxSockAddr from; | |
984 | WX_SOCKLEN_T fromlen = sizeof(from); | |
985 | int ret; | |
986 | GSocketError err; | |
987 | ||
988 | ret = recvfrom(m_fd, buffer, size, 0, (sockaddr*)&from, &fromlen); | |
989 | ||
990 | if (ret == SOCKET_ERROR) | |
991 | return SOCKET_ERROR; | |
992 | ||
993 | /* Translate a system address into a GSocket address */ | |
994 | if (!m_peer) | |
995 | { | |
996 | m_peer = GAddress_new(); | |
997 | if (!m_peer) | |
998 | { | |
999 | m_error = GSOCK_MEMERR; | |
1000 | return -1; | |
1001 | } | |
1002 | } | |
1003 | err = _GAddress_translate_from(m_peer, (sockaddr*)&from, fromlen); | |
1004 | if (err != GSOCK_NOERROR) | |
1005 | { | |
1006 | GAddress_destroy(m_peer); | |
1007 | m_peer = NULL; | |
1008 | m_error = err; | |
1009 | return -1; | |
1010 | } | |
1011 | ||
1012 | return ret; | |
1013 | } | |
1014 | ||
1015 | int GSocket::Send_Stream(const char *buffer, int size) | |
1016 | { | |
1017 | return send(m_fd, buffer, size, 0); | |
1018 | } | |
1019 | ||
1020 | int GSocket::Send_Dgram(const char *buffer, int size) | |
1021 | { | |
1022 | struct sockaddr *addr; | |
1023 | int len, ret; | |
1024 | GSocketError err; | |
1025 | ||
1026 | if (!m_peer) | |
1027 | { | |
1028 | m_error = GSOCK_INVADDR; | |
1029 | return -1; | |
1030 | } | |
1031 | ||
1032 | err = _GAddress_translate_to(m_peer, &addr, &len); | |
1033 | if (err != GSOCK_NOERROR) | |
1034 | { | |
1035 | m_error = err; | |
1036 | return -1; | |
1037 | } | |
1038 | ||
1039 | ret = sendto(m_fd, buffer, size, 0, addr, len); | |
1040 | ||
1041 | /* Frees memory allocated by _GAddress_translate_to */ | |
1042 | free(addr); | |
1043 | ||
1044 | return ret; | |
1045 | } | |
1046 | ||
1047 | /* Compatibility functions for GSocket */ | |
1048 | GSocket *GSocket_new() | |
1049 | { | |
1050 | GSocket *newsocket = new GSocket(); | |
1051 | if(newsocket->IsOk()) | |
1052 | return newsocket; | |
1053 | delete newsocket; | |
1054 | return NULL; | |
1055 | } | |
1056 | ||
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 | ||
1096 | GAddress *GAddress_new() | |
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 | ||
1110 | GAddress *GAddress_copy(GAddress *address) | |
1111 | { | |
1112 | GAddress *addr2; | |
1113 | ||
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 | ||
1133 | void GAddress_destroy(GAddress *address) | |
1134 | { | |
1135 | if (address->m_addr) | |
1136 | free(address->m_addr); | |
1137 | ||
1138 | free(address); | |
1139 | } | |
1140 | ||
1141 | void GAddress_SetFamily(GAddress *address, GAddressType type) | |
1142 | { | |
1143 | address->m_family = type; | |
1144 | } | |
1145 | ||
1146 | GAddressType GAddress_GetFamily(GAddress *address) | |
1147 | { | |
1148 | return address->m_family; | |
1149 | } | |
1150 | ||
1151 | GSocketError _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; | |
1163 | #if wxUSE_IPV6 | |
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 | ||
1191 | GSocketError _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 | ||
1218 | GSocketError _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 | ||
1236 | GSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname) | |
1237 | { | |
1238 | struct hostent *he; | |
1239 | struct in_addr *addr; | |
1240 | ||
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 | ||
1265 | GSocketError GAddress_INET_SetBroadcastAddress(GAddress *address) | |
1266 | { | |
1267 | return GAddress_INET_SetHostAddress(address, INADDR_BROADCAST); | |
1268 | } | |
1269 | ||
1270 | GSocketError GAddress_INET_SetAnyAddress(GAddress *address) | |
1271 | { | |
1272 | return GAddress_INET_SetHostAddress(address, INADDR_ANY); | |
1273 | } | |
1274 | ||
1275 | GSocketError GAddress_INET_SetHostAddress(GAddress *address, | |
1276 | unsigned long hostaddr) | |
1277 | { | |
1278 | struct in_addr *addr; | |
1279 | ||
1280 | CHECK_ADDRESS(address, INET); | |
1281 | ||
1282 | addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr); | |
1283 | addr->s_addr = htonl(hostaddr); | |
1284 | ||
1285 | return GSOCK_NOERROR; | |
1286 | } | |
1287 | ||
1288 | GSocketError GAddress_INET_SetPortName(GAddress *address, const char *port, | |
1289 | const char *protocol) | |
1290 | { | |
1291 | struct servent *se; | |
1292 | struct sockaddr_in *addr; | |
1293 | ||
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 | ||
1325 | GSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port) | |
1326 | { | |
1327 | struct sockaddr_in *addr; | |
1328 | ||
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 | ||
1337 | GSocketError 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 | ||
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 | ||
1360 | unsigned long GAddress_INET_GetHostAddress(GAddress *address) | |
1361 | { | |
1362 | struct sockaddr_in *addr; | |
1363 | ||
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 | ||
1371 | unsigned short GAddress_INET_GetPort(GAddress *address) | |
1372 | { | |
1373 | struct sockaddr_in *addr; | |
1374 | ||
1375 | CHECK_ADDRESS_RETVAL(address, INET, 0); | |
1376 | ||
1377 | addr = (struct sockaddr_in *)address->m_addr; | |
1378 | return ntohs(addr->sin_port); | |
1379 | } | |
1380 | ||
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 | ||
1394 | GSocketError _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 | ||
1414 | GSocketError GAddress_INET6_SetHostName(GAddress *address, const char *hostname) | |
1415 | { | |
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 | ||
1433 | GSocketError GAddress_INET6_SetAnyAddress(GAddress *address) | |
1434 | { | |
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 | } | |
1441 | GSocketError GAddress_INET6_SetHostAddress(GAddress *address, | |
1442 | struct in6_addr hostaddr) | |
1443 | { | |
1444 | CHECK_ADDRESS(address, INET6); | |
1445 | ||
1446 | ((struct sockaddr_in6 *)address->m_addr)->sin6_addr = hostaddr; | |
1447 | ||
1448 | return GSOCK_NOERROR; | |
1449 | } | |
1450 | ||
1451 | GSocketError GAddress_INET6_SetPortName(GAddress *address, const char *port, | |
1452 | const char *protocol) | |
1453 | { | |
1454 | struct servent *se; | |
1455 | struct sockaddr_in6 *addr; | |
1456 | ||
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 | { | |
1468 | if (isdigit((unsigned char) port[0])) | |
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 | ||
1488 | GSocketError GAddress_INET6_SetPort(GAddress *address, unsigned short port) | |
1489 | { | |
1490 | struct sockaddr_in6 *addr; | |
1491 | ||
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 | ||
1500 | GSocketError 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 | ||
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 | ||
1523 | GSocketError GAddress_INET6_GetHostAddress(GAddress *address,struct in6_addr *hostaddr) | |
1524 | { | |
1525 | CHECK_ADDRESS_RETVAL(address, INET6, GSOCK_INVADDR); | |
1526 | *hostaddr = ( (struct sockaddr_in6 *)address->m_addr )->sin6_addr; | |
1527 | return GSOCK_NOERROR; | |
1528 | } | |
1529 | ||
1530 | unsigned short GAddress_INET6_GetPort(GAddress *address) | |
1531 | { | |
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 | ||
1539 | /* | |
1540 | * ------------------------------------------------------------------------- | |
1541 | * Unix address family | |
1542 | * ------------------------------------------------------------------------- | |
1543 | */ | |
1544 | ||
1545 | GSocketError _GAddress_Init_UNIX(GAddress *address) | |
1546 | { | |
1547 | address->m_error = GSOCK_INVADDR; | |
1548 | return GSOCK_INVADDR; | |
1549 | } | |
1550 | ||
1551 | GSocketError GAddress_UNIX_SetPath(GAddress *address, const char *WXUNUSED(path)) | |
1552 | { | |
1553 | address->m_error = GSOCK_INVADDR; | |
1554 | return GSOCK_INVADDR; | |
1555 | } | |
1556 | ||
1557 | GSocketError GAddress_UNIX_GetPath(GAddress *address, char *WXUNUSED(path), size_t WXUNUSED(sbuf)) | |
1558 | { | |
1559 | address->m_error = GSOCK_INVADDR; | |
1560 | return GSOCK_INVADDR; | |
1561 | } | |
1562 | ||
1563 | #endif // wxUSE_SOCKETS |