1 | /* $Id: proxy.c 76553 2019-01-01 01:45:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * NAT Network - proxy setup and utilities.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-2019 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #define LOG_GROUP LOG_GROUP_NAT_SERVICE
|
---|
19 |
|
---|
20 | #include "winutils.h"
|
---|
21 |
|
---|
22 | #include "proxy.h"
|
---|
23 | #include "proxy_pollmgr.h"
|
---|
24 | #include "portfwd.h"
|
---|
25 |
|
---|
26 | #include "lwip/opt.h"
|
---|
27 |
|
---|
28 | #include "lwip/sys.h"
|
---|
29 | #include "lwip/tcpip.h"
|
---|
30 |
|
---|
31 | #ifndef RT_OS_WINDOWS
|
---|
32 | #include <sys/poll.h>
|
---|
33 | #include <sys/socket.h>
|
---|
34 | #include <netinet/in.h>
|
---|
35 | #include <netinet/tcp.h>
|
---|
36 | #include <arpa/inet.h>
|
---|
37 | #include <fcntl.h>
|
---|
38 | #include <stdio.h>
|
---|
39 | #include <iprt/string.h>
|
---|
40 | #include <unistd.h>
|
---|
41 | #include <err.h>
|
---|
42 | #else
|
---|
43 | # include <iprt/string.h>
|
---|
44 | #endif
|
---|
45 |
|
---|
46 | #if defined(SOCK_NONBLOCK) && defined(RT_OS_NETBSD) /* XXX: PR kern/47569 */
|
---|
47 | # undef SOCK_NONBLOCK
|
---|
48 | #endif
|
---|
49 |
|
---|
50 | #ifndef __arraycount
|
---|
51 | # define __arraycount(a) (sizeof(a)/sizeof(a[0]))
|
---|
52 | #endif
|
---|
53 |
|
---|
54 | static FNRTSTRFORMATTYPE proxy_sockerr_rtstrfmt;
|
---|
55 |
|
---|
56 | static SOCKET proxy_create_socket(int, int);
|
---|
57 |
|
---|
58 | volatile struct proxy_options *g_proxy_options;
|
---|
59 | static sys_thread_t pollmgr_tid;
|
---|
60 |
|
---|
61 | /* XXX: for mapping loopbacks to addresses in our network (ip4) */
|
---|
62 | struct netif *g_proxy_netif;
|
---|
63 |
|
---|
64 |
|
---|
65 | /*
|
---|
66 | * Called on the lwip thread (aka tcpip thread) from tcpip_init() via
|
---|
67 | * its "tcpip_init_done" callback. Raw API is ok to use here
|
---|
68 | * (e.g. rtadvd), but netconn API is not.
|
---|
69 | */
|
---|
70 | void
|
---|
71 | proxy_init(struct netif *proxy_netif, struct proxy_options *opts)
|
---|
72 | {
|
---|
73 | int status;
|
---|
74 |
|
---|
75 | LWIP_ASSERT1(opts != NULL);
|
---|
76 | LWIP_UNUSED_ARG(proxy_netif);
|
---|
77 |
|
---|
78 | status = RTStrFormatTypeRegister("sockerr", proxy_sockerr_rtstrfmt, NULL);
|
---|
79 | AssertRC(status);
|
---|
80 |
|
---|
81 | g_proxy_options = opts;
|
---|
82 | g_proxy_netif = proxy_netif;
|
---|
83 |
|
---|
84 | #if 1
|
---|
85 | proxy_rtadvd_start(proxy_netif);
|
---|
86 | #endif
|
---|
87 |
|
---|
88 | /*
|
---|
89 | * XXX: We use stateless DHCPv6 only to report IPv6 address(es) of
|
---|
90 | * nameserver(s). Since we don't yet support IPv6 addresses in
|
---|
91 | * HostDnsService, there's no point in running DHCPv6.
|
---|
92 | */
|
---|
93 | #if 0
|
---|
94 | dhcp6ds_init(proxy_netif);
|
---|
95 | #endif
|
---|
96 |
|
---|
97 | if (opts->tftp_root != NULL) {
|
---|
98 | tftpd_init(proxy_netif, opts->tftp_root);
|
---|
99 | }
|
---|
100 |
|
---|
101 | status = pollmgr_init();
|
---|
102 | if (status < 0) {
|
---|
103 | errx(EXIT_FAILURE, "failed to initialize poll manager");
|
---|
104 | /* NOTREACHED */
|
---|
105 | }
|
---|
106 |
|
---|
107 | pxtcp_init();
|
---|
108 | pxudp_init();
|
---|
109 |
|
---|
110 | portfwd_init();
|
---|
111 |
|
---|
112 | pxdns_init(proxy_netif);
|
---|
113 |
|
---|
114 | pxping_init(proxy_netif, opts->icmpsock4, opts->icmpsock6);
|
---|
115 |
|
---|
116 | pollmgr_tid = sys_thread_new("pollmgr_thread",
|
---|
117 | pollmgr_thread, NULL,
|
---|
118 | DEFAULT_THREAD_STACKSIZE,
|
---|
119 | DEFAULT_THREAD_PRIO);
|
---|
120 | if (!pollmgr_tid) {
|
---|
121 | errx(EXIT_FAILURE, "failed to create poll manager thread");
|
---|
122 | /* NOTREACHED */
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 |
|
---|
127 | #if !defined(RT_OS_WINDOWS)
|
---|
128 | /**
|
---|
129 | * Formatter for %R[sockerr] - unix strerror_r() version.
|
---|
130 | */
|
---|
131 | static DECLCALLBACK(size_t)
|
---|
132 | proxy_sockerr_rtstrfmt(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
|
---|
133 | const char *pszType, const void *pvValue,
|
---|
134 | int cchWidth, int cchPrecision, unsigned int fFlags,
|
---|
135 | void *pvUser)
|
---|
136 | {
|
---|
137 | const int error = (int)(intptr_t)pvValue;
|
---|
138 |
|
---|
139 | const char *msg;
|
---|
140 | char buf[128];
|
---|
141 |
|
---|
142 | NOREF(cchWidth);
|
---|
143 | NOREF(cchPrecision);
|
---|
144 | NOREF(fFlags);
|
---|
145 | NOREF(pvUser);
|
---|
146 |
|
---|
147 | AssertReturn(strcmp(pszType, "sockerr") == 0, 0);
|
---|
148 |
|
---|
149 | /* make sure return type mismatch is caught */
|
---|
150 | buf[0] = '\0';
|
---|
151 | #if defined(RT_OS_LINUX) && defined(_GNU_SOURCE)
|
---|
152 | msg = strerror_r(error, buf, sizeof(buf));
|
---|
153 | #else
|
---|
154 | strerror_r(error, buf, sizeof(buf));
|
---|
155 | msg = buf;
|
---|
156 | #endif
|
---|
157 | return RTStrFormat(pfnOutput, pvArgOutput, NULL, NULL, "%s", msg);
|
---|
158 | }
|
---|
159 |
|
---|
160 | #else /* RT_OS_WINDOWS */
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * Formatter for %R[sockerr] - windows FormatMessage() version.
|
---|
164 | */
|
---|
165 | static DECLCALLBACK(size_t)
|
---|
166 | proxy_sockerr_rtstrfmt(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
|
---|
167 | const char *pszType, const void *pvValue,
|
---|
168 | int cchWidth, int cchPrecision, unsigned int fFlags,
|
---|
169 | void *pvUser)
|
---|
170 | {
|
---|
171 | const int error = (int)(intptr_t)pvValue;
|
---|
172 | size_t cb = 0;
|
---|
173 |
|
---|
174 | NOREF(cchWidth);
|
---|
175 | NOREF(cchPrecision);
|
---|
176 | NOREF(fFlags);
|
---|
177 | NOREF(pvUser);
|
---|
178 |
|
---|
179 | AssertReturn(strcmp(pszType, "sockerr") == 0, 0);
|
---|
180 |
|
---|
181 | /*
|
---|
182 | * XXX: Windows strerror() doesn't handle posix error codes, but
|
---|
183 | * since winsock uses its own, it shouldn't be much of a problem.
|
---|
184 | * If you see a strange error message, it's probably from
|
---|
185 | * FormatMessage() for an error from <WinError.h> that has the
|
---|
186 | * same numeric value.
|
---|
187 | */
|
---|
188 | if (error < _sys_nerr) {
|
---|
189 | char buf[128] = "";
|
---|
190 | int status;
|
---|
191 |
|
---|
192 | status = strerror_s(buf, sizeof(buf), error);
|
---|
193 | if (status == 0) {
|
---|
194 | if (strcmp(buf, "Unknown error") == 0) {
|
---|
195 | /* windows strerror() doesn't add the numeric value */
|
---|
196 | cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, NULL,
|
---|
197 | "Unknown error: %d", error);
|
---|
198 | }
|
---|
199 | else {
|
---|
200 | cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, NULL,
|
---|
201 | "%s", buf);
|
---|
202 | }
|
---|
203 | }
|
---|
204 | else {
|
---|
205 | cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, NULL,
|
---|
206 | "Unknown error: %d", error);
|
---|
207 | }
|
---|
208 | }
|
---|
209 | else {
|
---|
210 | DWORD nchars;
|
---|
211 | char *msg = NULL;
|
---|
212 |
|
---|
213 | nchars = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM
|
---|
214 | | FORMAT_MESSAGE_ALLOCATE_BUFFER,
|
---|
215 | NULL, error, LANG_NEUTRAL,
|
---|
216 | (LPSTR)&msg, 0,
|
---|
217 | NULL);
|
---|
218 | if (nchars == 0 || msg == NULL) {
|
---|
219 | cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, NULL,
|
---|
220 | "Unknown error: %d", error);
|
---|
221 | }
|
---|
222 | else {
|
---|
223 | /* FormatMessage() "helpfully" adds newline; get rid of it */
|
---|
224 | char *crpos = strchr(msg, '\r');
|
---|
225 | if (crpos != NULL) {
|
---|
226 | *crpos = '\0';
|
---|
227 | }
|
---|
228 |
|
---|
229 | cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, NULL,
|
---|
230 | "%s", msg);
|
---|
231 | }
|
---|
232 |
|
---|
233 | if (msg != NULL) {
|
---|
234 | LocalFree(msg);
|
---|
235 | }
|
---|
236 | }
|
---|
237 |
|
---|
238 | return cb;
|
---|
239 | }
|
---|
240 | #endif /* RT_OS_WINDOWS */
|
---|
241 |
|
---|
242 |
|
---|
243 | /**
|
---|
244 | * Send static callback message from poll manager thread to lwip
|
---|
245 | * thread, scheduling a function call in lwip thread context.
|
---|
246 | *
|
---|
247 | * XXX: Existing lwip api only provides non-blocking version for this.
|
---|
248 | * It may fail when lwip thread is not running (mbox invalid) or if
|
---|
249 | * post failed (mbox full). How to handle these?
|
---|
250 | */
|
---|
251 | void
|
---|
252 | proxy_lwip_post(struct tcpip_msg *msg)
|
---|
253 | {
|
---|
254 | struct tcpip_callback_msg *m;
|
---|
255 | err_t error;
|
---|
256 |
|
---|
257 | LWIP_ASSERT1(msg != NULL);
|
---|
258 |
|
---|
259 | /*
|
---|
260 | * lwip plays games with fake incomplete struct tag to enforce API
|
---|
261 | */
|
---|
262 | m = (struct tcpip_callback_msg *)msg;
|
---|
263 | error = tcpip_callbackmsg(m);
|
---|
264 |
|
---|
265 | if (error == ERR_VAL) {
|
---|
266 | /* XXX: lwip thread is not running (mbox invalid) */
|
---|
267 | LWIP_ASSERT1(error != ERR_VAL);
|
---|
268 | }
|
---|
269 |
|
---|
270 | LWIP_ASSERT1(error == ERR_OK);
|
---|
271 | }
|
---|
272 |
|
---|
273 |
|
---|
274 | /**
|
---|
275 | * Create a non-blocking socket. Disable SIGPIPE for TCP sockets if
|
---|
276 | * possible. On Linux it's not possible and should be disabled for
|
---|
277 | * each send(2) individually.
|
---|
278 | */
|
---|
279 | static SOCKET
|
---|
280 | proxy_create_socket(int sdom, int stype)
|
---|
281 | {
|
---|
282 | SOCKET s;
|
---|
283 | int stype_and_flags;
|
---|
284 | int status;
|
---|
285 |
|
---|
286 | LWIP_UNUSED_ARG(status); /* depends on ifdefs */
|
---|
287 |
|
---|
288 |
|
---|
289 | stype_and_flags = stype;
|
---|
290 |
|
---|
291 | #if defined(SOCK_NONBLOCK)
|
---|
292 | stype_and_flags |= SOCK_NONBLOCK;
|
---|
293 | #endif
|
---|
294 |
|
---|
295 | /*
|
---|
296 | * Disable SIGPIPE on disconnected socket. It might be easier to
|
---|
297 | * forgo it and just use MSG_NOSIGNAL on each send*(2), since we
|
---|
298 | * have to do it for Linux anyway, but Darwin does NOT have that
|
---|
299 | * flag (but has SO_NOSIGPIPE socket option).
|
---|
300 | */
|
---|
301 | #if !defined(SOCK_NOSIGPIPE) && !defined(SO_NOSIGPIPE) && !defined(MSG_NOSIGNAL)
|
---|
302 | #if 0 /* XXX: Solaris has neither, the program should ignore SIGPIPE globally */
|
---|
303 | #error Need a way to disable SIGPIPE on connection oriented sockets!
|
---|
304 | #endif
|
---|
305 | #endif
|
---|
306 |
|
---|
307 | #if defined(SOCK_NOSIGPIPE)
|
---|
308 | if (stype == SOCK_STREAM) {
|
---|
309 | stype_and_flags |= SOCK_NOSIGPIPE;
|
---|
310 | }
|
---|
311 | #endif
|
---|
312 |
|
---|
313 | s = socket(sdom, stype_and_flags, 0);
|
---|
314 | if (s == INVALID_SOCKET) {
|
---|
315 | DPRINTF(("socket: %R[sockerr]\n", SOCKERRNO()));
|
---|
316 | return INVALID_SOCKET;
|
---|
317 | }
|
---|
318 |
|
---|
319 | #if defined(RT_OS_WINDOWS)
|
---|
320 | {
|
---|
321 | u_long mode = 1;
|
---|
322 | status = ioctlsocket(s, FIONBIO, &mode);
|
---|
323 | if (status == SOCKET_ERROR) {
|
---|
324 | DPRINTF(("FIONBIO: %R[sockerr]\n", SOCKERRNO()));
|
---|
325 | closesocket(s);
|
---|
326 | return INVALID_SOCKET;
|
---|
327 | }
|
---|
328 | }
|
---|
329 | #elif !defined(SOCK_NONBLOCK)
|
---|
330 | {
|
---|
331 | int sflags;
|
---|
332 |
|
---|
333 | sflags = fcntl(s, F_GETFL, 0);
|
---|
334 | if (sflags < 0) {
|
---|
335 | DPRINTF(("F_GETFL: %R[sockerr]\n", SOCKERRNO()));
|
---|
336 | closesocket(s);
|
---|
337 | return INVALID_SOCKET;
|
---|
338 | }
|
---|
339 |
|
---|
340 | status = fcntl(s, F_SETFL, sflags | O_NONBLOCK);
|
---|
341 | if (status < 0) {
|
---|
342 | DPRINTF(("O_NONBLOCK: %R[sockerr]\n", SOCKERRNO()));
|
---|
343 | closesocket(s);
|
---|
344 | return INVALID_SOCKET;
|
---|
345 | }
|
---|
346 | }
|
---|
347 | #endif
|
---|
348 |
|
---|
349 | #if !defined(SOCK_NOSIGPIPE) && defined(SO_NOSIGPIPE)
|
---|
350 | if (stype == SOCK_STREAM) {
|
---|
351 | int on = 1;
|
---|
352 | const socklen_t onlen = sizeof(on);
|
---|
353 |
|
---|
354 | status = setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, &on, onlen);
|
---|
355 | if (status < 0) {
|
---|
356 | DPRINTF(("SO_NOSIGPIPE: %R[sockerr]\n", SOCKERRNO()));
|
---|
357 | closesocket(s);
|
---|
358 | return INVALID_SOCKET;
|
---|
359 | }
|
---|
360 | }
|
---|
361 | #endif
|
---|
362 |
|
---|
363 | /*
|
---|
364 | * Disable the Nagle algorithm. Otherwise the host may hold back
|
---|
365 | * packets that the guest wants to go out, causing potentially
|
---|
366 | * horrible performance. The guest is already applying the Nagle
|
---|
367 | * algorithm (or not) the way it wants.
|
---|
368 | */
|
---|
369 | if (stype == SOCK_STREAM) {
|
---|
370 | int on = 1;
|
---|
371 | const socklen_t onlen = sizeof(on);
|
---|
372 |
|
---|
373 | status = setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (char *)&on, onlen);
|
---|
374 | if (status < 0) {
|
---|
375 | DPRINTF(("TCP_NODELAY: %R[sockerr]\n", SOCKERRNO()));
|
---|
376 | }
|
---|
377 | }
|
---|
378 |
|
---|
379 | #if defined(RT_OS_WINDOWS)
|
---|
380 | /*
|
---|
381 | * lwIP only holds one packet of "refused data" for us. Proxy
|
---|
382 | * relies on OS socket send buffer and doesn't do its own
|
---|
383 | * buffering. Unfortunately on Windows send buffer is very small
|
---|
384 | * (8K by default) and is not dynamically adpated by the OS it
|
---|
385 | * seems. So a single large write will fill it up and that will
|
---|
386 | * make lwIP drop segments, causing guest TCP into pathologic
|
---|
387 | * resend patterns. As a quick and dirty fix just bump it up.
|
---|
388 | */
|
---|
389 | if (stype == SOCK_STREAM) {
|
---|
390 | int sndbuf;
|
---|
391 | socklen_t optlen = sizeof(sndbuf);
|
---|
392 |
|
---|
393 | status = getsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&sndbuf, &optlen);
|
---|
394 | if (status == 0) {
|
---|
395 | if (sndbuf < 64 * 1024) {
|
---|
396 | sndbuf = 64 * 1024;
|
---|
397 | status = setsockopt(s, SOL_SOCKET, SO_SNDBUF,
|
---|
398 | (char *)&sndbuf, optlen);
|
---|
399 | if (status != 0) {
|
---|
400 | DPRINTF(("SO_SNDBUF: setsockopt: %R[sockerr]\n", SOCKERRNO()));
|
---|
401 | }
|
---|
402 | }
|
---|
403 | }
|
---|
404 | else {
|
---|
405 | DPRINTF(("SO_SNDBUF: getsockopt: %R[sockerr]\n", SOCKERRNO()));
|
---|
406 | }
|
---|
407 | }
|
---|
408 | #endif
|
---|
409 |
|
---|
410 | return s;
|
---|
411 | }
|
---|
412 |
|
---|
413 |
|
---|
414 | /**
|
---|
415 | * Create a socket for outbound connection to dst_addr:dst_port.
|
---|
416 | *
|
---|
417 | * The socket is non-blocking and TCP sockets has SIGPIPE disabled if
|
---|
418 | * possible. On Linux it's not possible and should be disabled for
|
---|
419 | * each send(2) individually.
|
---|
420 | */
|
---|
421 | SOCKET
|
---|
422 | proxy_connected_socket(int sdom, int stype,
|
---|
423 | ipX_addr_t *dst_addr, u16_t dst_port)
|
---|
424 | {
|
---|
425 | struct sockaddr_in6 dst_sin6;
|
---|
426 | struct sockaddr_in dst_sin;
|
---|
427 | struct sockaddr *pdst_sa;
|
---|
428 | socklen_t dst_sa_len;
|
---|
429 | void *pdst_addr;
|
---|
430 | const struct sockaddr *psrc_sa;
|
---|
431 | socklen_t src_sa_len;
|
---|
432 | int status;
|
---|
433 | int sockerr;
|
---|
434 | SOCKET s;
|
---|
435 |
|
---|
436 | LWIP_ASSERT1(sdom == PF_INET || sdom == PF_INET6);
|
---|
437 | LWIP_ASSERT1(stype == SOCK_STREAM || stype == SOCK_DGRAM);
|
---|
438 |
|
---|
439 | DPRINTF(("---> %s ", stype == SOCK_STREAM ? "TCP" : "UDP"));
|
---|
440 | if (sdom == PF_INET6) {
|
---|
441 | pdst_sa = (struct sockaddr *)&dst_sin6;
|
---|
442 | pdst_addr = (void *)&dst_sin6.sin6_addr;
|
---|
443 |
|
---|
444 | memset(&dst_sin6, 0, sizeof(dst_sin6));
|
---|
445 | #if HAVE_SA_LEN
|
---|
446 | dst_sin6.sin6_len =
|
---|
447 | #endif
|
---|
448 | dst_sa_len = sizeof(dst_sin6);
|
---|
449 | dst_sin6.sin6_family = AF_INET6;
|
---|
450 | memcpy(&dst_sin6.sin6_addr, &dst_addr->ip6, sizeof(ip6_addr_t));
|
---|
451 | dst_sin6.sin6_port = htons(dst_port);
|
---|
452 |
|
---|
453 | DPRINTF(("[%RTnaipv6]:%d ", &dst_sin6.sin6_addr, dst_port));
|
---|
454 | }
|
---|
455 | else { /* sdom = PF_INET */
|
---|
456 | pdst_sa = (struct sockaddr *)&dst_sin;
|
---|
457 | pdst_addr = (void *)&dst_sin.sin_addr;
|
---|
458 |
|
---|
459 | memset(&dst_sin, 0, sizeof(dst_sin));
|
---|
460 | #if HAVE_SA_LEN
|
---|
461 | dst_sin.sin_len =
|
---|
462 | #endif
|
---|
463 | dst_sa_len = sizeof(dst_sin);
|
---|
464 | dst_sin.sin_family = AF_INET;
|
---|
465 | dst_sin.sin_addr.s_addr = dst_addr->ip4.addr; /* byte-order? */
|
---|
466 | dst_sin.sin_port = htons(dst_port);
|
---|
467 |
|
---|
468 | DPRINTF(("%RTnaipv4:%d ", dst_sin.sin_addr.s_addr, dst_port));
|
---|
469 | }
|
---|
470 |
|
---|
471 | s = proxy_create_socket(sdom, stype);
|
---|
472 | if (s == INVALID_SOCKET) {
|
---|
473 | return INVALID_SOCKET;
|
---|
474 | }
|
---|
475 | DPRINTF(("socket %d\n", s));
|
---|
476 |
|
---|
477 | /** @todo needs locking if dynamic modifyvm is allowed */
|
---|
478 | if (sdom == PF_INET6) {
|
---|
479 | psrc_sa = (const struct sockaddr *)g_proxy_options->src6;
|
---|
480 | src_sa_len = sizeof(struct sockaddr_in6);
|
---|
481 | }
|
---|
482 | else {
|
---|
483 | psrc_sa = (const struct sockaddr *)g_proxy_options->src4;
|
---|
484 | src_sa_len = sizeof(struct sockaddr_in);
|
---|
485 | }
|
---|
486 | if (psrc_sa != NULL) {
|
---|
487 | status = bind(s, psrc_sa, src_sa_len);
|
---|
488 | if (status == SOCKET_ERROR) {
|
---|
489 | sockerr = SOCKERRNO();
|
---|
490 | DPRINTF(("socket %d: bind: %R[sockerr]\n", s, sockerr));
|
---|
491 | closesocket(s);
|
---|
492 | SET_SOCKERRNO(sockerr);
|
---|
493 | return INVALID_SOCKET;
|
---|
494 | }
|
---|
495 | }
|
---|
496 |
|
---|
497 | status = connect(s, pdst_sa, dst_sa_len);
|
---|
498 | if (status == SOCKET_ERROR
|
---|
499 | #if !defined(RT_OS_WINDOWS)
|
---|
500 | && SOCKERRNO() != EINPROGRESS
|
---|
501 | #else
|
---|
502 | && SOCKERRNO() != EWOULDBLOCK
|
---|
503 | #endif
|
---|
504 | )
|
---|
505 | {
|
---|
506 | sockerr = SOCKERRNO();
|
---|
507 | DPRINTF(("socket %d: connect: %R[sockerr]\n", s, sockerr));
|
---|
508 | closesocket(s);
|
---|
509 | SET_SOCKERRNO(sockerr);
|
---|
510 | return INVALID_SOCKET;
|
---|
511 | }
|
---|
512 |
|
---|
513 | return s;
|
---|
514 | }
|
---|
515 |
|
---|
516 |
|
---|
517 | /**
|
---|
518 | * Create a socket for inbound (port-forwarded) connections to
|
---|
519 | * src_addr (port is part of sockaddr, so not a separate argument).
|
---|
520 | *
|
---|
521 | * The socket is non-blocking and TCP sockets has SIGPIPE disabled if
|
---|
522 | * possible. On Linux it's not possible and should be disabled for
|
---|
523 | * each send(2) individually.
|
---|
524 | *
|
---|
525 | * TODO?: Support v6-mapped v4 so that user can specify she wants
|
---|
526 | * "udp" and get both versions?
|
---|
527 | */
|
---|
528 | SOCKET
|
---|
529 | proxy_bound_socket(int sdom, int stype, struct sockaddr *src_addr)
|
---|
530 | {
|
---|
531 | SOCKET s;
|
---|
532 | int on;
|
---|
533 | const socklen_t onlen = sizeof(on);
|
---|
534 | int status;
|
---|
535 | int sockerr;
|
---|
536 |
|
---|
537 | s = proxy_create_socket(sdom, stype);
|
---|
538 | if (s == INVALID_SOCKET) {
|
---|
539 | return INVALID_SOCKET;
|
---|
540 | }
|
---|
541 | DPRINTF(("socket %d\n", s));
|
---|
542 |
|
---|
543 | on = 1;
|
---|
544 | status = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&on, onlen);
|
---|
545 | if (status < 0) { /* not good, but not fatal */
|
---|
546 | DPRINTF(("SO_REUSEADDR: %R[sockerr]\n", SOCKERRNO()));
|
---|
547 | }
|
---|
548 |
|
---|
549 | status = bind(s, src_addr,
|
---|
550 | sdom == PF_INET ?
|
---|
551 | sizeof(struct sockaddr_in)
|
---|
552 | : sizeof(struct sockaddr_in6));
|
---|
553 | if (status == SOCKET_ERROR) {
|
---|
554 | sockerr = SOCKERRNO();
|
---|
555 | DPRINTF(("bind: %R[sockerr]\n", sockerr));
|
---|
556 | closesocket(s);
|
---|
557 | SET_SOCKERRNO(sockerr);
|
---|
558 | return INVALID_SOCKET;
|
---|
559 | }
|
---|
560 |
|
---|
561 | if (stype == SOCK_STREAM) {
|
---|
562 | status = listen(s, 5);
|
---|
563 | if (status == SOCKET_ERROR) {
|
---|
564 | sockerr = SOCKERRNO();
|
---|
565 | DPRINTF(("listen: %R[sockerr]\n", sockerr));
|
---|
566 | closesocket(s);
|
---|
567 | SET_SOCKERRNO(sockerr);
|
---|
568 | return INVALID_SOCKET;
|
---|
569 | }
|
---|
570 | }
|
---|
571 |
|
---|
572 | return s;
|
---|
573 | }
|
---|
574 |
|
---|
575 |
|
---|
576 | void
|
---|
577 | proxy_reset_socket(SOCKET s)
|
---|
578 | {
|
---|
579 | struct linger linger;
|
---|
580 |
|
---|
581 | linger.l_onoff = 1;
|
---|
582 | linger.l_linger = 0;
|
---|
583 |
|
---|
584 | /* On Windows we can run into issue here, perhaps SO_LINGER isn't enough, and
|
---|
585 | * we should use WSA{Send,Recv}Disconnect instead.
|
---|
586 | *
|
---|
587 | * Links for the reference:
|
---|
588 | * http://msdn.microsoft.com/en-us/library/windows/desktop/ms738547%28v=vs.85%29.aspx
|
---|
589 | * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4468997
|
---|
590 | */
|
---|
591 | setsockopt(s, SOL_SOCKET, SO_LINGER, (char *)&linger, sizeof(linger));
|
---|
592 |
|
---|
593 | closesocket(s);
|
---|
594 | }
|
---|
595 |
|
---|
596 |
|
---|
597 | int
|
---|
598 | proxy_sendto(SOCKET sock, struct pbuf *p, void *name, size_t namelen)
|
---|
599 | {
|
---|
600 | struct pbuf *q;
|
---|
601 | size_t i, clen;
|
---|
602 | #ifndef RT_OS_WINDOWS
|
---|
603 | struct msghdr mh;
|
---|
604 | ssize_t nsent;
|
---|
605 | #else
|
---|
606 | DWORD nsent;
|
---|
607 | #endif
|
---|
608 | int rc;
|
---|
609 | IOVEC fixiov[8]; /* fixed size (typical case) */
|
---|
610 | const size_t fixiovsize = sizeof(fixiov)/sizeof(fixiov[0]);
|
---|
611 | IOVEC *dyniov; /* dynamically sized */
|
---|
612 | IOVEC *iov;
|
---|
613 | int error = 0;
|
---|
614 |
|
---|
615 | /*
|
---|
616 | * Static iov[] is usually enough since UDP protocols use small
|
---|
617 | * datagrams to avoid fragmentation, but be prepared.
|
---|
618 | */
|
---|
619 | clen = pbuf_clen(p);
|
---|
620 | if (clen > fixiovsize) {
|
---|
621 | /*
|
---|
622 | * XXX: TODO: check that clen is shorter than IOV_MAX
|
---|
623 | */
|
---|
624 | dyniov = (IOVEC *)malloc(clen * sizeof(*dyniov));
|
---|
625 | if (dyniov == NULL) {
|
---|
626 | error = -errno; /* sic: not a socket error */
|
---|
627 | goto out;
|
---|
628 | }
|
---|
629 | iov = dyniov;
|
---|
630 | }
|
---|
631 | else {
|
---|
632 | dyniov = NULL;
|
---|
633 | iov = fixiov;
|
---|
634 | }
|
---|
635 |
|
---|
636 |
|
---|
637 | for (q = p, i = 0; i < clen; q = q->next, ++i) {
|
---|
638 | LWIP_ASSERT1(q != NULL);
|
---|
639 |
|
---|
640 | IOVEC_SET_BASE(iov[i], q->payload);
|
---|
641 | IOVEC_SET_LEN(iov[i], q->len);
|
---|
642 | }
|
---|
643 |
|
---|
644 | #ifndef RT_OS_WINDOWS
|
---|
645 | memset(&mh, 0, sizeof(mh));
|
---|
646 | mh.msg_name = name;
|
---|
647 | mh.msg_namelen = namelen;
|
---|
648 | mh.msg_iov = iov;
|
---|
649 | mh.msg_iovlen = clen;
|
---|
650 |
|
---|
651 | nsent = sendmsg(sock, &mh, 0);
|
---|
652 | rc = (nsent >= 0) ? 0 : SOCKET_ERROR;
|
---|
653 | #else
|
---|
654 | rc = WSASendTo(sock, iov, (DWORD)clen, &nsent, 0,
|
---|
655 | name, (int)namelen, NULL, NULL);
|
---|
656 | #endif
|
---|
657 | if (rc == SOCKET_ERROR) {
|
---|
658 | error = SOCKERRNO();
|
---|
659 | DPRINTF(("%s: socket %d: sendmsg: %R[sockerr]\n",
|
---|
660 | __func__, sock, error));
|
---|
661 | error = -error;
|
---|
662 | }
|
---|
663 |
|
---|
664 | out:
|
---|
665 | if (dyniov != NULL) {
|
---|
666 | free(dyniov);
|
---|
667 | }
|
---|
668 | return error;
|
---|
669 | }
|
---|
670 |
|
---|
671 |
|
---|
672 | static const char *lwiperr[] = {
|
---|
673 | "ERR_OK",
|
---|
674 | "ERR_MEM",
|
---|
675 | "ERR_BUF",
|
---|
676 | "ERR_TIMEOUT",
|
---|
677 | "ERR_RTE",
|
---|
678 | "ERR_INPROGRESS",
|
---|
679 | "ERR_VAL",
|
---|
680 | "ERR_WOULDBLOCK",
|
---|
681 | "ERR_USE",
|
---|
682 | "ERR_ISCONN",
|
---|
683 | "ERR_ABRT",
|
---|
684 | "ERR_RST",
|
---|
685 | "ERR_CLSD",
|
---|
686 | "ERR_CONN",
|
---|
687 | "ERR_ARG",
|
---|
688 | "ERR_IF"
|
---|
689 | };
|
---|
690 |
|
---|
691 |
|
---|
692 | const char *
|
---|
693 | proxy_lwip_strerr(err_t error)
|
---|
694 | {
|
---|
695 | static char buf[32];
|
---|
696 | int e = -error;
|
---|
697 |
|
---|
698 | if (0 <= e && e < (int)__arraycount(lwiperr)) {
|
---|
699 | return lwiperr[e];
|
---|
700 | }
|
---|
701 | else {
|
---|
702 | RTStrPrintf(buf, sizeof(buf), "unknown error %d", error);
|
---|
703 | return buf;
|
---|
704 | }
|
---|
705 | }
|
---|