1 | /* $Id: proxy.c 73747 2018-08-17 18:27:04Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * NAT Network - proxy setup and utilities.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-2017 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 | closesocket(s);
|
---|
377 | return INVALID_SOCKET;
|
---|
378 | }
|
---|
379 | }
|
---|
380 |
|
---|
381 | #if defined(RT_OS_WINDOWS)
|
---|
382 | /*
|
---|
383 | * lwIP only holds one packet of "refused data" for us. Proxy
|
---|
384 | * relies on OS socket send buffer and doesn't do its own
|
---|
385 | * buffering. Unfortunately on Windows send buffer is very small
|
---|
386 | * (8K by default) and is not dynamically adpated by the OS it
|
---|
387 | * seems. So a single large write will fill it up and that will
|
---|
388 | * make lwIP drop segments, causing guest TCP into pathologic
|
---|
389 | * resend patterns. As a quick and dirty fix just bump it up.
|
---|
390 | */
|
---|
391 | if (stype == SOCK_STREAM) {
|
---|
392 | int sndbuf;
|
---|
393 | socklen_t optlen = sizeof(sndbuf);
|
---|
394 |
|
---|
395 | status = getsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&sndbuf, &optlen);
|
---|
396 | if (status == 0) {
|
---|
397 | if (sndbuf < 64 * 1024) {
|
---|
398 | sndbuf = 64 * 1024;
|
---|
399 | status = setsockopt(s, SOL_SOCKET, SO_SNDBUF,
|
---|
400 | (char *)&sndbuf, optlen);
|
---|
401 | if (status != 0) {
|
---|
402 | DPRINTF(("SO_SNDBUF: setsockopt: %R[sockerr]\n", SOCKERRNO()));
|
---|
403 | }
|
---|
404 | }
|
---|
405 | }
|
---|
406 | else {
|
---|
407 | DPRINTF(("SO_SNDBUF: getsockopt: %R[sockerr]\n", SOCKERRNO()));
|
---|
408 | }
|
---|
409 | }
|
---|
410 | #endif
|
---|
411 |
|
---|
412 | return s;
|
---|
413 | }
|
---|
414 |
|
---|
415 |
|
---|
416 | /**
|
---|
417 | * Create a socket for outbound connection to dst_addr:dst_port.
|
---|
418 | *
|
---|
419 | * The socket is non-blocking and TCP sockets has SIGPIPE disabled if
|
---|
420 | * possible. On Linux it's not possible and should be disabled for
|
---|
421 | * each send(2) individually.
|
---|
422 | */
|
---|
423 | SOCKET
|
---|
424 | proxy_connected_socket(int sdom, int stype,
|
---|
425 | ipX_addr_t *dst_addr, u16_t dst_port)
|
---|
426 | {
|
---|
427 | struct sockaddr_in6 dst_sin6;
|
---|
428 | struct sockaddr_in dst_sin;
|
---|
429 | struct sockaddr *pdst_sa;
|
---|
430 | socklen_t dst_sa_len;
|
---|
431 | void *pdst_addr;
|
---|
432 | const struct sockaddr *psrc_sa;
|
---|
433 | socklen_t src_sa_len;
|
---|
434 | int status;
|
---|
435 | int sockerr;
|
---|
436 | SOCKET s;
|
---|
437 |
|
---|
438 | LWIP_ASSERT1(sdom == PF_INET || sdom == PF_INET6);
|
---|
439 | LWIP_ASSERT1(stype == SOCK_STREAM || stype == SOCK_DGRAM);
|
---|
440 |
|
---|
441 | DPRINTF(("---> %s ", stype == SOCK_STREAM ? "TCP" : "UDP"));
|
---|
442 | if (sdom == PF_INET6) {
|
---|
443 | pdst_sa = (struct sockaddr *)&dst_sin6;
|
---|
444 | pdst_addr = (void *)&dst_sin6.sin6_addr;
|
---|
445 |
|
---|
446 | memset(&dst_sin6, 0, sizeof(dst_sin6));
|
---|
447 | #if HAVE_SA_LEN
|
---|
448 | dst_sin6.sin6_len =
|
---|
449 | #endif
|
---|
450 | dst_sa_len = sizeof(dst_sin6);
|
---|
451 | dst_sin6.sin6_family = AF_INET6;
|
---|
452 | memcpy(&dst_sin6.sin6_addr, &dst_addr->ip6, sizeof(ip6_addr_t));
|
---|
453 | dst_sin6.sin6_port = htons(dst_port);
|
---|
454 |
|
---|
455 | DPRINTF(("[%RTnaipv6]:%d ", &dst_sin6.sin6_addr, dst_port));
|
---|
456 | }
|
---|
457 | else { /* sdom = PF_INET */
|
---|
458 | pdst_sa = (struct sockaddr *)&dst_sin;
|
---|
459 | pdst_addr = (void *)&dst_sin.sin_addr;
|
---|
460 |
|
---|
461 | memset(&dst_sin, 0, sizeof(dst_sin));
|
---|
462 | #if HAVE_SA_LEN
|
---|
463 | dst_sin.sin_len =
|
---|
464 | #endif
|
---|
465 | dst_sa_len = sizeof(dst_sin);
|
---|
466 | dst_sin.sin_family = AF_INET;
|
---|
467 | dst_sin.sin_addr.s_addr = dst_addr->ip4.addr; /* byte-order? */
|
---|
468 | dst_sin.sin_port = htons(dst_port);
|
---|
469 |
|
---|
470 | DPRINTF(("%RTnaipv4:%d ", dst_sin.sin_addr.s_addr, dst_port));
|
---|
471 | }
|
---|
472 |
|
---|
473 | s = proxy_create_socket(sdom, stype);
|
---|
474 | if (s == INVALID_SOCKET) {
|
---|
475 | return INVALID_SOCKET;
|
---|
476 | }
|
---|
477 | DPRINTF(("socket %d\n", s));
|
---|
478 |
|
---|
479 | /** @todo needs locking if dynamic modifyvm is allowed */
|
---|
480 | if (sdom == PF_INET6) {
|
---|
481 | psrc_sa = (const struct sockaddr *)g_proxy_options->src6;
|
---|
482 | src_sa_len = sizeof(struct sockaddr_in6);
|
---|
483 | }
|
---|
484 | else {
|
---|
485 | psrc_sa = (const struct sockaddr *)g_proxy_options->src4;
|
---|
486 | src_sa_len = sizeof(struct sockaddr_in);
|
---|
487 | }
|
---|
488 | if (psrc_sa != NULL) {
|
---|
489 | status = bind(s, psrc_sa, src_sa_len);
|
---|
490 | if (status == SOCKET_ERROR) {
|
---|
491 | sockerr = SOCKERRNO();
|
---|
492 | DPRINTF(("socket %d: bind: %R[sockerr]\n", s, sockerr));
|
---|
493 | closesocket(s);
|
---|
494 | SET_SOCKERRNO(sockerr);
|
---|
495 | return INVALID_SOCKET;
|
---|
496 | }
|
---|
497 | }
|
---|
498 |
|
---|
499 | status = connect(s, pdst_sa, dst_sa_len);
|
---|
500 | if (status == SOCKET_ERROR
|
---|
501 | #if !defined(RT_OS_WINDOWS)
|
---|
502 | && SOCKERRNO() != EINPROGRESS
|
---|
503 | #else
|
---|
504 | && SOCKERRNO() != EWOULDBLOCK
|
---|
505 | #endif
|
---|
506 | )
|
---|
507 | {
|
---|
508 | sockerr = SOCKERRNO();
|
---|
509 | DPRINTF(("socket %d: connect: %R[sockerr]\n", s, sockerr));
|
---|
510 | closesocket(s);
|
---|
511 | SET_SOCKERRNO(sockerr);
|
---|
512 | return INVALID_SOCKET;
|
---|
513 | }
|
---|
514 |
|
---|
515 | return s;
|
---|
516 | }
|
---|
517 |
|
---|
518 |
|
---|
519 | /**
|
---|
520 | * Create a socket for inbound (port-forwarded) connections to
|
---|
521 | * src_addr (port is part of sockaddr, so not a separate argument).
|
---|
522 | *
|
---|
523 | * The socket is non-blocking and TCP sockets has SIGPIPE disabled if
|
---|
524 | * possible. On Linux it's not possible and should be disabled for
|
---|
525 | * each send(2) individually.
|
---|
526 | *
|
---|
527 | * TODO?: Support v6-mapped v4 so that user can specify she wants
|
---|
528 | * "udp" and get both versions?
|
---|
529 | */
|
---|
530 | SOCKET
|
---|
531 | proxy_bound_socket(int sdom, int stype, struct sockaddr *src_addr)
|
---|
532 | {
|
---|
533 | SOCKET s;
|
---|
534 | int on;
|
---|
535 | const socklen_t onlen = sizeof(on);
|
---|
536 | int status;
|
---|
537 | int sockerr;
|
---|
538 |
|
---|
539 | s = proxy_create_socket(sdom, stype);
|
---|
540 | if (s == INVALID_SOCKET) {
|
---|
541 | return INVALID_SOCKET;
|
---|
542 | }
|
---|
543 | DPRINTF(("socket %d\n", s));
|
---|
544 |
|
---|
545 | on = 1;
|
---|
546 | status = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&on, onlen);
|
---|
547 | if (status < 0) { /* not good, but not fatal */
|
---|
548 | DPRINTF(("SO_REUSEADDR: %R[sockerr]\n", SOCKERRNO()));
|
---|
549 | }
|
---|
550 |
|
---|
551 | status = bind(s, src_addr,
|
---|
552 | sdom == PF_INET ?
|
---|
553 | sizeof(struct sockaddr_in)
|
---|
554 | : sizeof(struct sockaddr_in6));
|
---|
555 | if (status == SOCKET_ERROR) {
|
---|
556 | sockerr = SOCKERRNO();
|
---|
557 | DPRINTF(("bind: %R[sockerr]\n", sockerr));
|
---|
558 | closesocket(s);
|
---|
559 | SET_SOCKERRNO(sockerr);
|
---|
560 | return INVALID_SOCKET;
|
---|
561 | }
|
---|
562 |
|
---|
563 | if (stype == SOCK_STREAM) {
|
---|
564 | status = listen(s, 5);
|
---|
565 | if (status == SOCKET_ERROR) {
|
---|
566 | sockerr = SOCKERRNO();
|
---|
567 | DPRINTF(("listen: %R[sockerr]\n", sockerr));
|
---|
568 | closesocket(s);
|
---|
569 | SET_SOCKERRNO(sockerr);
|
---|
570 | return INVALID_SOCKET;
|
---|
571 | }
|
---|
572 | }
|
---|
573 |
|
---|
574 | return s;
|
---|
575 | }
|
---|
576 |
|
---|
577 |
|
---|
578 | void
|
---|
579 | proxy_reset_socket(SOCKET s)
|
---|
580 | {
|
---|
581 | struct linger linger;
|
---|
582 |
|
---|
583 | linger.l_onoff = 1;
|
---|
584 | linger.l_linger = 0;
|
---|
585 |
|
---|
586 | /* On Windows we can run into issue here, perhaps SO_LINGER isn't enough, and
|
---|
587 | * we should use WSA{Send,Recv}Disconnect instead.
|
---|
588 | *
|
---|
589 | * Links for the reference:
|
---|
590 | * http://msdn.microsoft.com/en-us/library/windows/desktop/ms738547%28v=vs.85%29.aspx
|
---|
591 | * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4468997
|
---|
592 | */
|
---|
593 | setsockopt(s, SOL_SOCKET, SO_LINGER, (char *)&linger, sizeof(linger));
|
---|
594 |
|
---|
595 | closesocket(s);
|
---|
596 | }
|
---|
597 |
|
---|
598 |
|
---|
599 | int
|
---|
600 | proxy_sendto(SOCKET sock, struct pbuf *p, void *name, size_t namelen)
|
---|
601 | {
|
---|
602 | struct pbuf *q;
|
---|
603 | size_t i, clen;
|
---|
604 | #ifndef RT_OS_WINDOWS
|
---|
605 | struct msghdr mh;
|
---|
606 | ssize_t nsent;
|
---|
607 | #else
|
---|
608 | DWORD nsent;
|
---|
609 | #endif
|
---|
610 | int rc;
|
---|
611 | IOVEC fixiov[8]; /* fixed size (typical case) */
|
---|
612 | const size_t fixiovsize = sizeof(fixiov)/sizeof(fixiov[0]);
|
---|
613 | IOVEC *dyniov; /* dynamically sized */
|
---|
614 | IOVEC *iov;
|
---|
615 | int error = 0;
|
---|
616 |
|
---|
617 | /*
|
---|
618 | * Static iov[] is usually enough since UDP protocols use small
|
---|
619 | * datagrams to avoid fragmentation, but be prepared.
|
---|
620 | */
|
---|
621 | clen = pbuf_clen(p);
|
---|
622 | if (clen > fixiovsize) {
|
---|
623 | /*
|
---|
624 | * XXX: TODO: check that clen is shorter than IOV_MAX
|
---|
625 | */
|
---|
626 | dyniov = (IOVEC *)malloc(clen * sizeof(*dyniov));
|
---|
627 | if (dyniov == NULL) {
|
---|
628 | error = -errno; /* sic: not a socket error */
|
---|
629 | goto out;
|
---|
630 | }
|
---|
631 | iov = dyniov;
|
---|
632 | }
|
---|
633 | else {
|
---|
634 | dyniov = NULL;
|
---|
635 | iov = fixiov;
|
---|
636 | }
|
---|
637 |
|
---|
638 |
|
---|
639 | for (q = p, i = 0; i < clen; q = q->next, ++i) {
|
---|
640 | LWIP_ASSERT1(q != NULL);
|
---|
641 |
|
---|
642 | IOVEC_SET_BASE(iov[i], q->payload);
|
---|
643 | IOVEC_SET_LEN(iov[i], q->len);
|
---|
644 | }
|
---|
645 |
|
---|
646 | #ifndef RT_OS_WINDOWS
|
---|
647 | memset(&mh, 0, sizeof(mh));
|
---|
648 | mh.msg_name = name;
|
---|
649 | mh.msg_namelen = namelen;
|
---|
650 | mh.msg_iov = iov;
|
---|
651 | mh.msg_iovlen = clen;
|
---|
652 |
|
---|
653 | nsent = sendmsg(sock, &mh, 0);
|
---|
654 | rc = (nsent >= 0) ? 0 : SOCKET_ERROR;
|
---|
655 | #else
|
---|
656 | rc = WSASendTo(sock, iov, (DWORD)clen, &nsent, 0,
|
---|
657 | name, (int)namelen, NULL, NULL);
|
---|
658 | #endif
|
---|
659 | if (rc == SOCKET_ERROR) {
|
---|
660 | error = SOCKERRNO();
|
---|
661 | DPRINTF(("%s: socket %d: sendmsg: %R[sockerr]\n",
|
---|
662 | __func__, sock, error));
|
---|
663 | error = -error;
|
---|
664 | }
|
---|
665 |
|
---|
666 | out:
|
---|
667 | if (dyniov != NULL) {
|
---|
668 | free(dyniov);
|
---|
669 | }
|
---|
670 | return error;
|
---|
671 | }
|
---|
672 |
|
---|
673 |
|
---|
674 | static const char *lwiperr[] = {
|
---|
675 | "ERR_OK",
|
---|
676 | "ERR_MEM",
|
---|
677 | "ERR_BUF",
|
---|
678 | "ERR_TIMEOUT",
|
---|
679 | "ERR_RTE",
|
---|
680 | "ERR_INPROGRESS",
|
---|
681 | "ERR_VAL",
|
---|
682 | "ERR_WOULDBLOCK",
|
---|
683 | "ERR_USE",
|
---|
684 | "ERR_ISCONN",
|
---|
685 | "ERR_ABRT",
|
---|
686 | "ERR_RST",
|
---|
687 | "ERR_CLSD",
|
---|
688 | "ERR_CONN",
|
---|
689 | "ERR_ARG",
|
---|
690 | "ERR_IF"
|
---|
691 | };
|
---|
692 |
|
---|
693 |
|
---|
694 | const char *
|
---|
695 | proxy_lwip_strerr(err_t error)
|
---|
696 | {
|
---|
697 | static char buf[32];
|
---|
698 | int e = -error;
|
---|
699 |
|
---|
700 | if (0 <= e && e < (int)__arraycount(lwiperr)) {
|
---|
701 | return lwiperr[e];
|
---|
702 | }
|
---|
703 | else {
|
---|
704 | RTStrPrintf(buf, sizeof(buf), "unknown error %d", error);
|
---|
705 | return buf;
|
---|
706 | }
|
---|
707 | }
|
---|