1 | #include "slirp.h"
|
---|
2 | #ifdef RT_OS_OS2
|
---|
3 | # include <paths.h>
|
---|
4 | #endif
|
---|
5 |
|
---|
6 | #include <VBox/err.h>
|
---|
7 | #include <VBox/pdmdrv.h>
|
---|
8 | #include <iprt/assert.h>
|
---|
9 | #ifndef RT_OS_WINDOWS
|
---|
10 | #include <poll.h>
|
---|
11 | #endif
|
---|
12 |
|
---|
13 | #if !defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) || !defined(RT_OS_WINDOWS)
|
---|
14 |
|
---|
15 | # ifndef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
16 | # define DO_ENGAGE_EVENT1(so, fdset, label) \
|
---|
17 | do { \
|
---|
18 | FD_SET((so)->s, (fdset)); \
|
---|
19 | UPD_NFDS((so)->s); \
|
---|
20 | } while(0)
|
---|
21 |
|
---|
22 |
|
---|
23 | # define DO_ENGAGE_EVENT2(so, fdset1, fdset2, label) \
|
---|
24 | do { \
|
---|
25 | FD_SET((so)->s, (fdset1)); \
|
---|
26 | FD_SET((so)->s, (fdset2)); \
|
---|
27 | UPD_NFDS((so)->s); \
|
---|
28 | } while(0)
|
---|
29 |
|
---|
30 | # define DO_POLL_EVENTS(rc, error, so, events, label) do {} while (0)
|
---|
31 |
|
---|
32 | # define DO_CHECK_FD_SET(so, events, fdset) (FD_ISSET((so)->s, fdset))
|
---|
33 | # define DO_UNIX_CHECK_FD_SET(so, events, fdset ) 0 /*specific for Unix API */
|
---|
34 | # else /* !VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
|
---|
35 | # define DO_ENGAGE_EVENT1(so, fdset, label) \
|
---|
36 | do { \
|
---|
37 | polls[poll_index].fd = (so)->s; \
|
---|
38 | (so)->so_poll_index = poll_index; \
|
---|
39 | polls[poll_index].events = N_(fdset ## _poll); \
|
---|
40 | poll_index++; \
|
---|
41 | } while(0)
|
---|
42 |
|
---|
43 |
|
---|
44 | # define DO_ENGAGE_EVENT2(so, fdset1, fdset2, label) \
|
---|
45 | do { \
|
---|
46 | polls[poll_index].fd = (so)->s; \
|
---|
47 | (so)->so_poll_index = poll_index; \
|
---|
48 | polls[poll_index].events = N_(fdset1 ## _poll) | N_(fdset1 ## _poll); \
|
---|
49 | poll_index++; \
|
---|
50 | } while(0)
|
---|
51 |
|
---|
52 | # define DO_POLL_EVENTS(rc, error, so, events, label) do {} while (0)
|
---|
53 |
|
---|
54 | # define DO_CHECK_FD_SET(so, events, fdset) ( ((so)->so_poll_index != -1) \
|
---|
55 | && ((so)->so_poll_index <= ndfs) \
|
---|
56 | && ((so)->s == polls[so->so_poll_index].fd) \
|
---|
57 | && (polls[(so)->so_poll_index].revents & N_(fdset ## _poll)))
|
---|
58 | # define DO_UNIX_CHECK_FD_SET(so, events, fdset ) DO_CHECK_FD_SET((so), (events), fdset) /*specific for Unix API */
|
---|
59 | # define DO_WIN_CHECK_FD_SET(so, events, fdset ) 0 /* specific for Windows Winsock API */
|
---|
60 | # endif /* VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
|
---|
61 |
|
---|
62 | # ifndef RT_OS_WINDOWS
|
---|
63 |
|
---|
64 | # ifndef RT_OS_LINUX
|
---|
65 | # define readfds_poll (POLLRDNORM)
|
---|
66 | # define writefds_poll (POLLWRNORM)
|
---|
67 | # define xfds_poll (POLLRDBAND|POLLWRBAND|POLLPRI)
|
---|
68 | # else
|
---|
69 | # define readfds_poll (POLLIN)
|
---|
70 | # define writefds_poll (POLLOUT)
|
---|
71 | # define xfds_poll (POLLPRI)
|
---|
72 | # endif
|
---|
73 | # define rderr_poll (POLLERR)
|
---|
74 | # define rdhup_poll (POLLHUP)
|
---|
75 | # define nval_poll (POLLNVAL)
|
---|
76 |
|
---|
77 | # define ICMP_ENGAGE_EVENT(so, fdset) \
|
---|
78 | do { \
|
---|
79 | if (pData->icmp_socket.s != -1) \
|
---|
80 | DO_ENGAGE_EVENT1((so), fdset, ICMP); \
|
---|
81 | } while (0)
|
---|
82 | # else /* !RT_OS_WINDOWS */
|
---|
83 | # define DO_WIN_CHECK_FD_SET(so, events, fdset ) DO_CHECK_FD_SET((so), (events), fdset)
|
---|
84 | # define ICMP_ENGAGE_EVENT(so, fdset) do {} while(0)
|
---|
85 | #endif /* RT_OS_WINDOWS */
|
---|
86 |
|
---|
87 | #else /* defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS) */
|
---|
88 |
|
---|
89 | /*
|
---|
90 | * On Windows, we will be notified by IcmpSendEcho2() when the response arrives.
|
---|
91 | * So no call to WSAEventSelect necessary.
|
---|
92 | */
|
---|
93 | # define ICMP_ENGAGE_EVENT(so, fdset) do {} while(0)
|
---|
94 |
|
---|
95 | # define DO_ENGAGE_EVENT1(so, fdset1, label) \
|
---|
96 | do { \
|
---|
97 | rc = WSAEventSelect((so)->s, VBOX_SOCKET_EVENT, FD_ALL_EVENTS); \
|
---|
98 | if (rc == SOCKET_ERROR) \
|
---|
99 | { \
|
---|
100 | /* This should not happen */ \
|
---|
101 | error = WSAGetLastError(); \
|
---|
102 | LogRel(("WSAEventSelect (" #label ") error %d (so=%x, socket=%s, event=%x)\n", \
|
---|
103 | error, (so), (so)->s, VBOX_SOCKET_EVENT)); \
|
---|
104 | } \
|
---|
105 | } while(0); \
|
---|
106 | CONTINUE(label)
|
---|
107 |
|
---|
108 | # define DO_ENGAGE_EVENT2(so, fdset1, fdset2, label) \
|
---|
109 | DO_ENGAGE_EVENT1((so), (fdset1), label)
|
---|
110 |
|
---|
111 | # define DO_POLL_EVENTS(rc, error, so, events, label) \
|
---|
112 | (rc) = WSAEnumNetworkEvents((so)->s, VBOX_SOCKET_EVENT, (events)); \
|
---|
113 | if ((rc) == SOCKET_ERROR) \
|
---|
114 | { \
|
---|
115 | (error) = WSAGetLastError(); \
|
---|
116 | LogRel(("WSAEnumNetworkEvents " #label " error %d\n", (error))); \
|
---|
117 | CONTINUE(label); \
|
---|
118 | }
|
---|
119 |
|
---|
120 | # define acceptds_win FD_ACCEPT
|
---|
121 | # define acceptds_win_bit FD_ACCEPT_BIT
|
---|
122 |
|
---|
123 | # define readfds_win FD_READ
|
---|
124 | # define readfds_win_bit FD_READ_BIT
|
---|
125 |
|
---|
126 | # define writefds_win FD_WRITE
|
---|
127 | # define writefds_win_bit FD_WRITE_BIT
|
---|
128 |
|
---|
129 | # define xfds_win FD_OOB
|
---|
130 | # define xfds_win_bit FD_OOB_BIT
|
---|
131 |
|
---|
132 | # define DO_CHECK_FD_SET(so, events, fdset) \
|
---|
133 | (((events).lNetworkEvents & fdset ## _win) && ((events).iErrorCode[fdset ## _win_bit] == 0))
|
---|
134 |
|
---|
135 | # define DO_UNIX_CHECK_FD_SET(so, events, fdset ) 1 /*specific for Unix API */
|
---|
136 |
|
---|
137 | #endif /* defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS) */
|
---|
138 |
|
---|
139 | #define TCP_ENGAGE_EVENT1(so, fdset) \
|
---|
140 | DO_ENGAGE_EVENT1((so), fdset, tcp)
|
---|
141 |
|
---|
142 | #define TCP_ENGAGE_EVENT2(so, fdset1, fdset2) \
|
---|
143 | DO_ENGAGE_EVENT2((so), fdset1, fdset2, tcp)
|
---|
144 |
|
---|
145 | #define UDP_ENGAGE_EVENT(so, fdset) \
|
---|
146 | DO_ENGAGE_EVENT1((so), fdset, udp)
|
---|
147 |
|
---|
148 | #define POLL_TCP_EVENTS(rc, error, so, events) \
|
---|
149 | DO_POLL_EVENTS((rc), (error), (so), (events), tcp)
|
---|
150 |
|
---|
151 | #define POLL_UDP_EVENTS(rc, error, so, events) \
|
---|
152 | DO_POLL_EVENTS((rc), (error), (so), (events), udp)
|
---|
153 |
|
---|
154 | #define CHECK_FD_SET(so, events, set) \
|
---|
155 | (DO_CHECK_FD_SET((so), (events), set))
|
---|
156 |
|
---|
157 | #define WIN_CHECK_FD_SET(so, events, set) \
|
---|
158 | (DO_WIN_CHECK_FD_SET((so), (events), set))
|
---|
159 | #define UNIX_CHECK_FD_SET(so, events, set) \
|
---|
160 | (DO_UNIX_CHECK_FD_SET(so, events, set))
|
---|
161 |
|
---|
162 | /*
|
---|
163 | * Loging macros
|
---|
164 | */
|
---|
165 | #if VBOX_WITH_DEBUG_NAT_SOCKETS
|
---|
166 | # if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC)
|
---|
167 | # if defined(RT_OS_WINDOWS)
|
---|
168 | # define DO_LOG_NAT_SOCK(so, proto, winevent, r_fdset, w_fdset, x_fdset) \
|
---|
169 | do { \
|
---|
170 | LogRel((" " #proto " %R[natsock] %R[natwinnetevents]\n", (so), (winevent))); \
|
---|
171 | } while (0)
|
---|
172 | # else /* RT_OS_WINDOWS */
|
---|
173 | # define DO_LOG_NAT_SOCK(so, proto, winevent, r_fdset, w_fdset, x_fdset) \
|
---|
174 | do { \
|
---|
175 | LogRel((" " #proto " %R[natsock] %s %s %s er: %s, %s, %s\n", (so), \
|
---|
176 | CHECK_FD_SET(so, ign ,r_fdset) ? "READ":"", \
|
---|
177 | CHECK_FD_SET(so, ign, w_fdset) ? "WRITE":"", \
|
---|
178 | CHECK_FD_SET(so, ign, x_fdset) ? "OOB":"", \
|
---|
179 | CHECK_FD_SET(so, ign, rderr) ? "RDERR":"", \
|
---|
180 | CHECK_FD_SET(so, ign, rdhup) ? "RDHUP":"", \
|
---|
181 | CHECK_FD_SET(so, ign, nval) ? "RDNVAL":"")); \
|
---|
182 | } while (0)
|
---|
183 | # endif /* !RT_OS_WINDOWS */
|
---|
184 | # else /* VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
|
---|
185 | # define DO_LOG_NAT_SOCK(so, proto, winevent, r_fdset, w_fdset, x_fdset) \
|
---|
186 | do { \
|
---|
187 | LogRel((" " #proto " %R[natsock] %s %s %s\n", (so), FD_ISSET((so)->s, (r_fdset))?"READ":"",\
|
---|
188 | FD_ISSET((so)->s, (w_fdset))?"WRITE":"", FD_ISSET((so)->s, (x_fdset))?"OOB":"")); \
|
---|
189 | } while (0)
|
---|
190 | # endif
|
---|
191 | #else /* VBOX_WITH_DEBUG_NAT_SOCKETS */
|
---|
192 | # define DO_LOG_NAT_SOCK(so, proto, winevent, r_fdset, w_fdset, x_fdset) do {} while (0)
|
---|
193 | #endif /* !VBOX_WITH_DEBUG_NAT_SOCKETS */
|
---|
194 |
|
---|
195 | #define LOG_NAT_SOCK(so, proto, winevent, r_fdset, w_fdset, x_fdset) DO_LOG_NAT_SOCK((so), proto, (winevent), r_fdset, w_fdset, x_fdset)
|
---|
196 |
|
---|
197 | static const uint8_t special_ethaddr[6] =
|
---|
198 | {
|
---|
199 | 0x52, 0x54, 0x00, 0x12, 0x35, 0x00
|
---|
200 | };
|
---|
201 |
|
---|
202 | #ifdef RT_OS_WINDOWS
|
---|
203 |
|
---|
204 | static int get_dns_addr_domain(PNATState pData, bool fVerbose,
|
---|
205 | struct in_addr *pdns_addr,
|
---|
206 | const char **ppszDomain)
|
---|
207 | {
|
---|
208 | int rc = 0;
|
---|
209 | FIXED_INFO *FixedInfo = NULL;
|
---|
210 | ULONG BufLen;
|
---|
211 | DWORD ret;
|
---|
212 | IP_ADDR_STRING *pIPAddr;
|
---|
213 | struct in_addr tmp_addr;
|
---|
214 |
|
---|
215 | FixedInfo = (FIXED_INFO *)GlobalAlloc(GPTR, sizeof(FIXED_INFO));
|
---|
216 | BufLen = sizeof(FIXED_INFO);
|
---|
217 |
|
---|
218 | /** @todo: this API returns all DNS servers, no matter whether the
|
---|
219 | * corresponding network adapter is disabled or not. Maybe replace
|
---|
220 | * this by GetAdapterAddresses(), which is XP/Vista only though. */
|
---|
221 | if (ERROR_BUFFER_OVERFLOW == GetNetworkParams(FixedInfo, &BufLen))
|
---|
222 | {
|
---|
223 | if (FixedInfo)
|
---|
224 | {
|
---|
225 | GlobalFree(FixedInfo);
|
---|
226 | FixedInfo = NULL;
|
---|
227 | }
|
---|
228 | FixedInfo = GlobalAlloc(GPTR, BufLen);
|
---|
229 | }
|
---|
230 |
|
---|
231 | if ((ret = GetNetworkParams(FixedInfo, &BufLen)) != ERROR_SUCCESS)
|
---|
232 | {
|
---|
233 | Log(("GetNetworkParams failed. ret = %08x\n", (u_int)ret ));
|
---|
234 | if (FixedInfo)
|
---|
235 | {
|
---|
236 | GlobalFree(FixedInfo);
|
---|
237 | FixedInfo = NULL;
|
---|
238 | }
|
---|
239 | rc = -1;
|
---|
240 | goto get_dns_prefix;
|
---|
241 | }
|
---|
242 |
|
---|
243 | #ifndef VBOX_WITH_MULTI_DNS
|
---|
244 | pIPAddr = &(FixedInfo->DnsServerList);
|
---|
245 | inet_aton(pIPAddr->IpAddress.String, &tmp_addr);
|
---|
246 | Log(("nat: DNS Servers:\n"));
|
---|
247 | if (fVerbose || pdns_addr->s_addr != tmp_addr.s_addr)
|
---|
248 | LogRel(("NAT: DNS address: %s\n", pIPAddr->IpAddress.String));
|
---|
249 | *pdns_addr = tmp_addr;
|
---|
250 |
|
---|
251 | pIPAddr = FixedInfo -> DnsServerList.Next;
|
---|
252 | while (pIPAddr)
|
---|
253 | {
|
---|
254 | if (fVerbose)
|
---|
255 | LogRel(("NAT: ignored DNS address: %s\n", pIPAddr ->IpAddress.String));
|
---|
256 | pIPAddr = pIPAddr ->Next;
|
---|
257 | }
|
---|
258 | #else
|
---|
259 | /*localhost mask */
|
---|
260 | for (pIPAddr = &FixedInfo->DnsServerList; pIPAddr != NULL; pIPAddr = pIPAddr->Next)
|
---|
261 | {
|
---|
262 | struct dns_entry *da;
|
---|
263 | if(!inet_aton(pIPAddr->IpAddress.String, &tmp_addr))
|
---|
264 | continue;
|
---|
265 | da = RTMemAllocZ(sizeof (struct dns_entry));
|
---|
266 | if (da == NULL)
|
---|
267 | {
|
---|
268 | LogRel(("can't alloc memory for DNS entry\n"));
|
---|
269 | return -1;
|
---|
270 | }
|
---|
271 | /*check */
|
---|
272 | if ((da->de_addr.s_addr & htonl(IN_CLASSA_NET)) == ntohl(INADDR_LOOPBACK & IN_CLASSA_NET)) {
|
---|
273 | da->de_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
|
---|
274 | }
|
---|
275 | LIST_INSERT_HEAD(&pData->dns_list_head, da, de_list);
|
---|
276 | }
|
---|
277 | #endif
|
---|
278 | if (FixedInfo)
|
---|
279 | {
|
---|
280 | GlobalFree(FixedInfo);
|
---|
281 | FixedInfo = NULL;
|
---|
282 | }
|
---|
283 |
|
---|
284 | get_dns_prefix:
|
---|
285 | if (ppszDomain)
|
---|
286 | {
|
---|
287 | OSVERSIONINFO ver;
|
---|
288 | char szDnsDomain[256];
|
---|
289 | DWORD dwSize = sizeof(szDnsDomain);
|
---|
290 |
|
---|
291 | *ppszDomain = NULL;
|
---|
292 | GetVersionEx(&ver);
|
---|
293 | if (ver.dwMajorVersion >= 5)
|
---|
294 | {
|
---|
295 | /* GetComputerNameEx exists in Windows versions starting with 2000. */
|
---|
296 | if (GetComputerNameEx(ComputerNameDnsDomain, szDnsDomain, &dwSize))
|
---|
297 | {
|
---|
298 | if (szDnsDomain[0])
|
---|
299 | {
|
---|
300 | /* Just non-empty strings are valid. */
|
---|
301 | *ppszDomain = RTStrDup(szDnsDomain);
|
---|
302 | if (pData->fPassDomain)
|
---|
303 | {
|
---|
304 | if (fVerbose)
|
---|
305 | LogRel(("NAT: passing domain name %s\n", szDnsDomain));
|
---|
306 | }
|
---|
307 | else
|
---|
308 | Log(("nat: ignoring domain %s\n", szDnsDomain));
|
---|
309 | }
|
---|
310 | }
|
---|
311 | else
|
---|
312 | Log(("nat: GetComputerNameEx failed (%d)\n", GetLastError()));
|
---|
313 | }
|
---|
314 | }
|
---|
315 | return rc;
|
---|
316 | }
|
---|
317 |
|
---|
318 | #else
|
---|
319 |
|
---|
320 | static int get_dns_addr_domain(PNATState pData, bool fVerbose,
|
---|
321 | struct in_addr *pdns_addr,
|
---|
322 | const char **ppszDomain)
|
---|
323 | {
|
---|
324 | char buff[512];
|
---|
325 | char buff2[256];
|
---|
326 | FILE *f;
|
---|
327 | int found = 0;
|
---|
328 | struct in_addr tmp_addr;
|
---|
329 |
|
---|
330 | #ifdef RT_OS_OS2
|
---|
331 | /* Try various locations. */
|
---|
332 | char *etc = getenv("ETC");
|
---|
333 | f = NULL;
|
---|
334 | if (etc)
|
---|
335 | {
|
---|
336 | snprintf(buff, sizeof(buff), "%s/RESOLV2", etc);
|
---|
337 | f = fopen(buff, "rt");
|
---|
338 | }
|
---|
339 | if (!f)
|
---|
340 | {
|
---|
341 | snprintf(buff, sizeof(buff), "%s/RESOLV2", _PATH_ETC);
|
---|
342 | f = fopen(buff, "rt");
|
---|
343 | }
|
---|
344 | if (!f)
|
---|
345 | {
|
---|
346 | snprintf(buff, sizeof(buff), "%s/resolv.conf", _PATH_ETC);
|
---|
347 | f = fopen(buff, "rt");
|
---|
348 | }
|
---|
349 | #else
|
---|
350 | f = fopen("/etc/resolv.conf", "r");
|
---|
351 | #endif
|
---|
352 | if (!f)
|
---|
353 | return -1;
|
---|
354 |
|
---|
355 | if (ppszDomain)
|
---|
356 | *ppszDomain = NULL;
|
---|
357 | Log(("nat: DNS Servers:\n"));
|
---|
358 | while (fgets(buff, 512, f) != NULL)
|
---|
359 | {
|
---|
360 | #ifdef VBOX_WITH_MULTI_DNS
|
---|
361 | struct dns_entry *da = NULL;
|
---|
362 | #endif
|
---|
363 | if (sscanf(buff, "nameserver%*[ \t]%256s", buff2) == 1)
|
---|
364 | {
|
---|
365 | if (!inet_aton(buff2, &tmp_addr))
|
---|
366 | continue;
|
---|
367 | #ifndef VBOX_WITH_MULTI_DNS
|
---|
368 | /* If it's the first one, set it to dns_addr */
|
---|
369 | if (!found)
|
---|
370 | {
|
---|
371 | if (fVerbose || pdns_addr->s_addr != tmp_addr.s_addr)
|
---|
372 | LogRel(("NAT: DNS address: %s\n", buff2));
|
---|
373 | *pdns_addr = tmp_addr;
|
---|
374 | }
|
---|
375 | else
|
---|
376 | {
|
---|
377 | if (fVerbose)
|
---|
378 | LogRel(("NAT: ignored DNS address: %s\n", buff2));
|
---|
379 | }
|
---|
380 | #else
|
---|
381 | /*localhost mask */
|
---|
382 | da = RTMemAllocZ(sizeof (struct dns_entry));
|
---|
383 | if (da == NULL)
|
---|
384 | {
|
---|
385 | LogRel(("can't alloc memory for DNS entry\n"));
|
---|
386 | return -1;
|
---|
387 | }
|
---|
388 | /*check */
|
---|
389 | da->de_addr.s_addr = tmp_addr.s_addr;
|
---|
390 | if ((da->de_addr.s_addr & htonl(IN_CLASSA_NET)) == ntohl(INADDR_LOOPBACK & IN_CLASSA_NET)) {
|
---|
391 | da->de_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
|
---|
392 | }
|
---|
393 | LIST_INSERT_HEAD(&pData->dns_list_head, da, de_list);
|
---|
394 | #endif
|
---|
395 | found++;
|
---|
396 | }
|
---|
397 | if ( ppszDomain
|
---|
398 | && (!strncmp(buff, "domain", 6) || !strncmp(buff, "search", 6)))
|
---|
399 | {
|
---|
400 | /* Domain name/search list present. Pick first entry */
|
---|
401 | if (*ppszDomain == NULL)
|
---|
402 | {
|
---|
403 | char *tok;
|
---|
404 | char *saveptr;
|
---|
405 | tok = strtok_r(&buff[6], " \t\n", &saveptr);
|
---|
406 | if (tok)
|
---|
407 | {
|
---|
408 | *ppszDomain = RTStrDup(tok);
|
---|
409 | if (pData->fPassDomain)
|
---|
410 | {
|
---|
411 | if (fVerbose)
|
---|
412 | LogRel(("NAT: passing domain name %s\n", tok));
|
---|
413 | }
|
---|
414 | else
|
---|
415 | Log(("nat: ignoring domain %s\n", tok));
|
---|
416 | }
|
---|
417 | }
|
---|
418 | }
|
---|
419 | }
|
---|
420 | fclose(f);
|
---|
421 | if (!found)
|
---|
422 | return -1;
|
---|
423 | return 0;
|
---|
424 | }
|
---|
425 |
|
---|
426 | #endif
|
---|
427 |
|
---|
428 | int get_dns_addr(PNATState pData, struct in_addr *pdns_addr)
|
---|
429 | {
|
---|
430 | return get_dns_addr_domain(pData, false, pdns_addr, NULL);
|
---|
431 | }
|
---|
432 |
|
---|
433 | int slirp_init(PNATState *ppData, const char *pszNetAddr, uint32_t u32Netmask,
|
---|
434 | bool fPassDomain, const char *pszTFTPPrefix,
|
---|
435 | const char *pszBootFile, void *pvUser)
|
---|
436 | {
|
---|
437 | int fNATfailed = 0;
|
---|
438 | int rc;
|
---|
439 | PNATState pData = RTMemAllocZ(sizeof(NATState));
|
---|
440 | *ppData = pData;
|
---|
441 | if (!pData)
|
---|
442 | return VERR_NO_MEMORY;
|
---|
443 | if (u32Netmask & 0x1f)
|
---|
444 | /* CTL is x.x.x.15, bootp passes up to 16 IPs (15..31) */
|
---|
445 | return VERR_INVALID_PARAMETER;
|
---|
446 | memset(pData, '\0', sizeof(NATState));
|
---|
447 | pData->fPassDomain = fPassDomain;
|
---|
448 | pData->pvUser = pvUser;
|
---|
449 | tftp_prefix = pszTFTPPrefix;
|
---|
450 | bootp_filename = pszBootFile;
|
---|
451 | pData->netmask = u32Netmask;
|
---|
452 |
|
---|
453 | #ifdef RT_OS_WINDOWS
|
---|
454 | {
|
---|
455 | WSADATA Data;
|
---|
456 | WSAStartup(MAKEWORD(2,0), &Data);
|
---|
457 | }
|
---|
458 | # if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC)
|
---|
459 | pData->phEvents[VBOX_SOCKET_EVENT_INDEX] = CreateEvent(NULL, FALSE, FALSE, NULL);
|
---|
460 | # endif
|
---|
461 | #endif
|
---|
462 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
463 | QSOCKET_LOCK_CREATE(tcb);
|
---|
464 | QSOCKET_LOCK_CREATE(udb);
|
---|
465 | rc = RTReqCreateQueue(&pData->pReqQueue);
|
---|
466 | AssertReleaseRC(rc);
|
---|
467 | #endif
|
---|
468 |
|
---|
469 | link_up = 1;
|
---|
470 |
|
---|
471 | debug_init();
|
---|
472 | if_init(pData);
|
---|
473 | ip_init(pData);
|
---|
474 | icmp_init(pData);
|
---|
475 |
|
---|
476 | /* Initialise mbufs *after* setting the MTU */
|
---|
477 | m_init(pData);
|
---|
478 |
|
---|
479 | inet_aton(pszNetAddr, &special_addr);
|
---|
480 | alias_addr.s_addr = special_addr.s_addr | htonl(CTL_ALIAS);
|
---|
481 | /* @todo: add ability to configure this staff */
|
---|
482 | pData->tftp_server.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_TFTP);
|
---|
483 |
|
---|
484 | /* set default addresses */
|
---|
485 | inet_aton("127.0.0.1", &loopback_addr);
|
---|
486 | #ifndef VBOX_WITH_MULTI_DNS
|
---|
487 | inet_aton("127.0.0.1", &dns_addr);
|
---|
488 |
|
---|
489 | if (get_dns_addr_domain(pData, true, &dns_addr, &pData->pszDomain) < 0)
|
---|
490 | #else
|
---|
491 | LIST_INIT(&pData->dns_list_head);
|
---|
492 | if (get_dns_addr_domain(pData, true, NULL, &pData->pszDomain) < 0)
|
---|
493 | #endif
|
---|
494 | fNATfailed = 1;
|
---|
495 |
|
---|
496 | getouraddr(pData);
|
---|
497 | return fNATfailed ? VINF_NAT_DNS : VINF_SUCCESS;
|
---|
498 | }
|
---|
499 |
|
---|
500 | /**
|
---|
501 | * Statistics counters.
|
---|
502 | */
|
---|
503 | void slirp_register_timers(PNATState pData, PPDMDRVINS pDrvIns)
|
---|
504 | {
|
---|
505 | #ifdef VBOX_WITH_STATISTICS
|
---|
506 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatFill, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS,
|
---|
507 | STAMUNIT_TICKS_PER_CALL, "Profiling slirp fills", "/Drivers/NAT%d/Fill", pDrvIns->iInstance);
|
---|
508 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatPoll, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS,
|
---|
509 | STAMUNIT_TICKS_PER_CALL, "Profiling slirp polls", "/Drivers/NAT%d/Poll", pDrvIns->iInstance);
|
---|
510 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatFastTimer, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS,
|
---|
511 | STAMUNIT_TICKS_PER_CALL, "Profiling slirp fast timer", "/Drivers/NAT%d/TimerFast", pDrvIns->iInstance);
|
---|
512 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatSlowTimer, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS,
|
---|
513 | STAMUNIT_TICKS_PER_CALL, "Profiling slirp slow timer", "/Drivers/NAT%d/TimerSlow", pDrvIns->iInstance);
|
---|
514 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatTCP, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS,
|
---|
515 | STAMUNIT_COUNT, "TCP sockets", "/Drivers/NAT%d/SockTCP", pDrvIns->iInstance);
|
---|
516 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatTCPHot, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS,
|
---|
517 | STAMUNIT_COUNT, "TCP sockets active", "/Drivers/NAT%d/SockTCPHot", pDrvIns->iInstance);
|
---|
518 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatUDP, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS,
|
---|
519 | STAMUNIT_COUNT, "UDP sockets", "/Drivers/NAT%d/SockUDP", pDrvIns->iInstance);
|
---|
520 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatUDPHot, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS,
|
---|
521 | STAMUNIT_COUNT, "UDP sockets active", "/Drivers/NAT%d/SockUDPHot", pDrvIns->iInstance);
|
---|
522 | #endif /* VBOX_WITH_STATISTICS */
|
---|
523 | }
|
---|
524 |
|
---|
525 | /**
|
---|
526 | * Marks the link as up, making it possible to establish new connections.
|
---|
527 | */
|
---|
528 | void slirp_link_up(PNATState pData)
|
---|
529 | {
|
---|
530 | link_up = 1;
|
---|
531 | }
|
---|
532 |
|
---|
533 | /**
|
---|
534 | * Marks the link as down and cleans up the current connections.
|
---|
535 | */
|
---|
536 | void slirp_link_down(PNATState pData)
|
---|
537 | {
|
---|
538 | struct socket *so;
|
---|
539 |
|
---|
540 | while ((so = tcb.so_next) != &tcb)
|
---|
541 | {
|
---|
542 | if (so->so_state & SS_NOFDREF || so->s == -1)
|
---|
543 | sofree(pData, so);
|
---|
544 | else
|
---|
545 | tcp_drop(pData, sototcpcb(so), 0);
|
---|
546 | }
|
---|
547 |
|
---|
548 | while ((so = udb.so_next) != &udb)
|
---|
549 | udp_detach(pData, so);
|
---|
550 |
|
---|
551 | link_up = 0;
|
---|
552 | }
|
---|
553 |
|
---|
554 | /**
|
---|
555 | * Terminates the slirp component.
|
---|
556 | */
|
---|
557 | void slirp_term(PNATState pData)
|
---|
558 | {
|
---|
559 | #ifdef VBOX_WITH_MULTI_DNS
|
---|
560 | struct dns_entry *de = NULL;
|
---|
561 | #endif
|
---|
562 | if (pData->pszDomain)
|
---|
563 | RTStrFree((char *)(void *)pData->pszDomain);
|
---|
564 |
|
---|
565 | #ifdef RT_OS_WINDOWS
|
---|
566 | pData->pfIcmpCloseHandle(pData->icmp_socket.sh);
|
---|
567 | FreeLibrary(pData->hmIcmpLibrary);
|
---|
568 | RTMemFree(pData->pvIcmpBuffer);
|
---|
569 | # else
|
---|
570 | closesocket(pData->icmp_socket.s);
|
---|
571 | #endif
|
---|
572 |
|
---|
573 | slirp_link_down(pData);
|
---|
574 | #ifdef VBOX_WITH_MULTI_DNS
|
---|
575 | while(!LIST_EMPTY(&pData->dns_list_head)) {
|
---|
576 | de = LIST_FIRST(&pData->dns_list_head);
|
---|
577 | LIST_REMOVE(de, de_list);
|
---|
578 | RTMemFree(de);
|
---|
579 | }
|
---|
580 | #endif
|
---|
581 | #ifdef RT_OS_WINDOWS
|
---|
582 | WSACleanup();
|
---|
583 | #endif
|
---|
584 | #ifdef LOG_ENABLED
|
---|
585 | Log(("\n"
|
---|
586 | "NAT statistics\n"
|
---|
587 | "--------------\n"
|
---|
588 | "\n"));
|
---|
589 | ipstats(pData);
|
---|
590 | tcpstats(pData);
|
---|
591 | udpstats(pData);
|
---|
592 | icmpstats(pData);
|
---|
593 | mbufstats(pData);
|
---|
594 | sockstats(pData);
|
---|
595 | Log(("\n"
|
---|
596 | "\n"
|
---|
597 | "\n"));
|
---|
598 | #endif
|
---|
599 | RTMemFree(pData);
|
---|
600 | }
|
---|
601 |
|
---|
602 |
|
---|
603 | #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
|
---|
604 | #define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
|
---|
605 | #define UPD_NFDS(x) if (nfds < (x)) nfds = (x)
|
---|
606 |
|
---|
607 | /*
|
---|
608 | * curtime kept to an accuracy of 1ms
|
---|
609 | */
|
---|
610 | #ifdef RT_OS_WINDOWS
|
---|
611 | static void updtime(PNATState pData)
|
---|
612 | {
|
---|
613 | struct _timeb tb;
|
---|
614 |
|
---|
615 | _ftime(&tb);
|
---|
616 | curtime = (u_int)tb.time * (u_int)1000;
|
---|
617 | curtime += (u_int)tb.millitm;
|
---|
618 | }
|
---|
619 | #else
|
---|
620 | static void updtime(PNATState pData)
|
---|
621 | {
|
---|
622 | gettimeofday(&tt, 0);
|
---|
623 |
|
---|
624 | curtime = (u_int)tt.tv_sec * (u_int)1000;
|
---|
625 | curtime += (u_int)tt.tv_usec / (u_int)1000;
|
---|
626 |
|
---|
627 | if ((tt.tv_usec % 1000) >= 500)
|
---|
628 | curtime++;
|
---|
629 | }
|
---|
630 | #endif
|
---|
631 |
|
---|
632 | #ifndef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
633 | void slirp_select_fill(PNATState pData, int *pnfds,
|
---|
634 | fd_set *readfds, fd_set *writefds, fd_set *xfds)
|
---|
635 | #else /* !VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
|
---|
636 | # ifdef RT_OS_WINDOWS
|
---|
637 | void slirp_select_fill(PNATState pData, int *pnfds)
|
---|
638 | # else /* RT_OS_WINDOWS */
|
---|
639 | void slirp_select_fill(PNATState pData, int *pnfds, struct pollfd *polls)
|
---|
640 | # endif /* !RT_OS_WINDOWS */
|
---|
641 | #endif /* VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
|
---|
642 | {
|
---|
643 | struct socket *so, *so_next;
|
---|
644 | int nfds;
|
---|
645 | #if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
|
---|
646 | int rc;
|
---|
647 | int error;
|
---|
648 | #endif
|
---|
649 | #if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && !defined(RT_OS_WINDOWS)
|
---|
650 | int poll_index = 0;
|
---|
651 | #endif
|
---|
652 | int i;
|
---|
653 |
|
---|
654 | STAM_PROFILE_START(&pData->StatFill, a);
|
---|
655 |
|
---|
656 | nfds = *pnfds;
|
---|
657 |
|
---|
658 | /*
|
---|
659 | * First, TCP sockets
|
---|
660 | */
|
---|
661 | do_slowtimo = 0;
|
---|
662 | if (link_up)
|
---|
663 | {
|
---|
664 | /*
|
---|
665 | * *_slowtimo needs calling if there are IP fragments
|
---|
666 | * in the fragment queue, or there are TCP connections active
|
---|
667 | */
|
---|
668 | /* XXX:
|
---|
669 | * triggering of fragment expiration should be the same but use new macroses
|
---|
670 | */
|
---|
671 | do_slowtimo = (tcb.so_next != &tcb);
|
---|
672 | if (!do_slowtimo)
|
---|
673 | {
|
---|
674 | for (i = 0; i < IPREASS_NHASH; i++)
|
---|
675 | {
|
---|
676 | if (!TAILQ_EMPTY(&ipq[i]))
|
---|
677 | {
|
---|
678 | do_slowtimo = 1;
|
---|
679 | break;
|
---|
680 | }
|
---|
681 | }
|
---|
682 | }
|
---|
683 | ICMP_ENGAGE_EVENT(&pData->icmp_socket, readfds);
|
---|
684 |
|
---|
685 | STAM_COUNTER_RESET(&pData->StatTCP);
|
---|
686 | STAM_COUNTER_RESET(&pData->StatTCPHot);
|
---|
687 |
|
---|
688 | QSOCKET_FOREACH(so, so_next, tcp)
|
---|
689 | /* { */
|
---|
690 | STAM_COUNTER_INC(&pData->StatTCP);
|
---|
691 |
|
---|
692 | /*
|
---|
693 | * See if we need a tcp_fasttimo
|
---|
694 | */
|
---|
695 | if ( time_fasttimo == 0
|
---|
696 | && so->so_tcpcb != NULL
|
---|
697 | && so->so_tcpcb->t_flags & TF_DELACK)
|
---|
698 | time_fasttimo = curtime; /* Flag when we want a fasttimo */
|
---|
699 |
|
---|
700 | /*
|
---|
701 | * NOFDREF can include still connecting to local-host,
|
---|
702 | * newly socreated() sockets etc. Don't want to select these.
|
---|
703 | */
|
---|
704 | if (so->so_state & SS_NOFDREF || so->s == -1)
|
---|
705 | CONTINUE(tcp);
|
---|
706 |
|
---|
707 | /*
|
---|
708 | * Set for reading sockets which are accepting
|
---|
709 | */
|
---|
710 | if (so->so_state & SS_FACCEPTCONN)
|
---|
711 | {
|
---|
712 | STAM_COUNTER_INC(&pData->StatTCPHot);
|
---|
713 | TCP_ENGAGE_EVENT1(so, readfds);
|
---|
714 | CONTINUE(tcp);
|
---|
715 | }
|
---|
716 |
|
---|
717 | /*
|
---|
718 | * Set for writing sockets which are connecting
|
---|
719 | */
|
---|
720 | if (so->so_state & SS_ISFCONNECTING)
|
---|
721 | {
|
---|
722 | Log2(("connecting %R[natsock] engaged\n",so));
|
---|
723 | STAM_COUNTER_INC(&pData->StatTCPHot);
|
---|
724 | TCP_ENGAGE_EVENT1(so, writefds);
|
---|
725 | }
|
---|
726 |
|
---|
727 | /*
|
---|
728 | * Set for writing if we are connected, can send more, and
|
---|
729 | * we have something to send
|
---|
730 | */
|
---|
731 | if (CONN_CANFSEND(so) && so->so_rcv.sb_cc)
|
---|
732 | {
|
---|
733 | STAM_COUNTER_INC(&pData->StatTCPHot);
|
---|
734 | TCP_ENGAGE_EVENT1(so, writefds);
|
---|
735 | }
|
---|
736 |
|
---|
737 | /*
|
---|
738 | * Set for reading (and urgent data) if we are connected, can
|
---|
739 | * receive more, and we have room for it XXX /2 ?
|
---|
740 | */
|
---|
741 | if (CONN_CANFRCV(so) && (so->so_snd.sb_cc < (so->so_snd.sb_datalen/2)))
|
---|
742 | {
|
---|
743 | STAM_COUNTER_INC(&pData->StatTCPHot);
|
---|
744 | TCP_ENGAGE_EVENT2(so, readfds, xfds);
|
---|
745 | }
|
---|
746 | LOOP_LABEL(tcp, so, so_next);
|
---|
747 | }
|
---|
748 |
|
---|
749 | /*
|
---|
750 | * UDP sockets
|
---|
751 | */
|
---|
752 | STAM_COUNTER_RESET(&pData->StatUDP);
|
---|
753 | STAM_COUNTER_RESET(&pData->StatUDPHot);
|
---|
754 |
|
---|
755 | QSOCKET_FOREACH(so, so_next, udp)
|
---|
756 | /* { */
|
---|
757 |
|
---|
758 | STAM_COUNTER_INC(&pData->StatUDP);
|
---|
759 |
|
---|
760 | /*
|
---|
761 | * See if it's timed out
|
---|
762 | */
|
---|
763 | if (so->so_expire)
|
---|
764 | {
|
---|
765 | if (so->so_expire <= curtime)
|
---|
766 | {
|
---|
767 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
768 | /* we need so_next for continue our cycle*/
|
---|
769 | so_next = so->so_next;
|
---|
770 | #endif
|
---|
771 | UDP_DETACH(pData, so, so_next);
|
---|
772 | CONTINUE_NO_UNLOCK(udp);
|
---|
773 | }
|
---|
774 | else
|
---|
775 | do_slowtimo = 1; /* Let socket expire */
|
---|
776 | }
|
---|
777 |
|
---|
778 | /*
|
---|
779 | * When UDP packets are received from over the link, they're
|
---|
780 | * sendto()'d straight away, so no need for setting for writing
|
---|
781 | * Limit the number of packets queued by this session to 4.
|
---|
782 | * Note that even though we try and limit this to 4 packets,
|
---|
783 | * the session could have more queued if the packets needed
|
---|
784 | * to be fragmented.
|
---|
785 | *
|
---|
786 | * (XXX <= 4 ?)
|
---|
787 | */
|
---|
788 | if ((so->so_state & SS_ISFCONNECTED) && so->so_queued <= 4)
|
---|
789 | {
|
---|
790 | STAM_COUNTER_INC(&pData->StatUDPHot);
|
---|
791 | UDP_ENGAGE_EVENT(so, readfds);
|
---|
792 | }
|
---|
793 | LOOP_LABEL(udp, so, so_next);
|
---|
794 | }
|
---|
795 |
|
---|
796 | }
|
---|
797 |
|
---|
798 | #if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC)
|
---|
799 | # if defined(RT_OS_WINDOWS)
|
---|
800 | *pnfds = VBOX_EVENT_COUNT;
|
---|
801 | # else /* RT_OS_WINDOWS */
|
---|
802 | *pnfds = poll_index;
|
---|
803 | # endif /* !RT_OS_WINDOWS */
|
---|
804 | #else /* VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
|
---|
805 | *pnfds = nfds;
|
---|
806 | #endif /* !VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
|
---|
807 |
|
---|
808 | STAM_PROFILE_STOP(&pData->StatFill, a);
|
---|
809 | }
|
---|
810 |
|
---|
811 | #if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC)
|
---|
812 | # if defined(RT_OS_WINDOWS)
|
---|
813 | void slirp_select_poll(PNATState pData, int fTimeout, int fIcmp)
|
---|
814 | # else /* RT_OS_WINDOWS */
|
---|
815 | void slirp_select_poll(PNATState pData, struct pollfd *polls, int ndfs)
|
---|
816 | # endif /* !RT_OS_WINDOWS */
|
---|
817 | #else /* VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
|
---|
818 | void slirp_select_poll(PNATState pData, fd_set *readfds, fd_set *writefds, fd_set *xfds)
|
---|
819 | #endif /* !VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
|
---|
820 | {
|
---|
821 | struct socket *so, *so_next;
|
---|
822 | int ret;
|
---|
823 | #if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
|
---|
824 | WSANETWORKEVENTS NetworkEvents;
|
---|
825 | int rc;
|
---|
826 | int error;
|
---|
827 | #endif
|
---|
828 | #if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && !defined(RT_OS_WINDOWS)
|
---|
829 | int poll_index = 0;
|
---|
830 | #endif
|
---|
831 |
|
---|
832 | STAM_PROFILE_START(&pData->StatPoll, a);
|
---|
833 |
|
---|
834 | /* Update time */
|
---|
835 | updtime(pData);
|
---|
836 |
|
---|
837 | /*
|
---|
838 | * See if anything has timed out
|
---|
839 | */
|
---|
840 | if (link_up)
|
---|
841 | {
|
---|
842 | if (time_fasttimo && ((curtime - time_fasttimo) >= 2))
|
---|
843 | {
|
---|
844 | STAM_PROFILE_START(&pData->StatFastTimer, a);
|
---|
845 | tcp_fasttimo(pData);
|
---|
846 | time_fasttimo = 0;
|
---|
847 | STAM_PROFILE_STOP(&pData->StatFastTimer, a);
|
---|
848 | }
|
---|
849 | if (do_slowtimo && ((curtime - last_slowtimo) >= 499))
|
---|
850 | {
|
---|
851 | STAM_PROFILE_START(&pData->StatSlowTimer, a);
|
---|
852 | ip_slowtimo(pData);
|
---|
853 | tcp_slowtimo(pData);
|
---|
854 | last_slowtimo = curtime;
|
---|
855 | STAM_PROFILE_STOP(&pData->StatSlowTimer, a);
|
---|
856 | }
|
---|
857 | }
|
---|
858 | #if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
|
---|
859 | if (fTimeout)
|
---|
860 | return; /* only timer update */
|
---|
861 | #endif
|
---|
862 |
|
---|
863 | /*
|
---|
864 | * Check sockets
|
---|
865 | */
|
---|
866 | if (link_up)
|
---|
867 | {
|
---|
868 | #if defined(RT_OS_WINDOWS)
|
---|
869 | /*XXX: before renaming please make see define
|
---|
870 | * fIcmp in slirp_state.h
|
---|
871 | */
|
---|
872 | if (fIcmp)
|
---|
873 | sorecvfrom(pData, &pData->icmp_socket);
|
---|
874 | #else
|
---|
875 | if ( (pData->icmp_socket.s != -1)
|
---|
876 | && CHECK_FD_SET(&pData->icmp_socket, ignored, readfds))
|
---|
877 | sorecvfrom(pData, &pData->icmp_socket);
|
---|
878 | #endif
|
---|
879 | /*
|
---|
880 | * Check TCP sockets
|
---|
881 | */
|
---|
882 | QSOCKET_FOREACH(so, so_next, tcp)
|
---|
883 | /* { */
|
---|
884 |
|
---|
885 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
886 | if ( so->so_state & SS_NOFDREF
|
---|
887 | && so->so_deleted == 1)
|
---|
888 | {
|
---|
889 | struct socket *son, *sop = NULL;
|
---|
890 | QSOCKET_LOCK(tcb);
|
---|
891 | if (so->so_next != NULL)
|
---|
892 | {
|
---|
893 | if (so->so_next != &tcb)
|
---|
894 | SOCKET_LOCK(so->so_next);
|
---|
895 | son = so->so_next;
|
---|
896 | }
|
---|
897 | if ( so->so_prev != &tcb
|
---|
898 | && so->so_prev != NULL)
|
---|
899 | {
|
---|
900 | SOCKET_LOCK(so->so_prev);
|
---|
901 | sop = so->so_prev;
|
---|
902 | }
|
---|
903 | QSOCKET_UNLOCK(tcb);
|
---|
904 | remque(pData, so);
|
---|
905 | NSOCK_DEC();
|
---|
906 | SOCKET_UNLOCK(so);
|
---|
907 | SOCKET_LOCK_DESTROY(so);
|
---|
908 | RTMemFree(so);
|
---|
909 | so_next = son;
|
---|
910 | if (sop != NULL)
|
---|
911 | SOCKET_UNLOCK(sop);
|
---|
912 | CONTINUE_NO_UNLOCK(tcp);
|
---|
913 | }
|
---|
914 | #endif
|
---|
915 | /*
|
---|
916 | * FD_ISSET is meaningless on these sockets
|
---|
917 | * (and they can crash the program)
|
---|
918 | */
|
---|
919 | if (so->so_state & SS_NOFDREF || so->s == -1)
|
---|
920 | CONTINUE(tcp);
|
---|
921 |
|
---|
922 | POLL_TCP_EVENTS(rc, error, so, &NetworkEvents);
|
---|
923 |
|
---|
924 | LOG_NAT_SOCK(so, TCP, &NetworkEvents, readfds, writefds, xfds);
|
---|
925 |
|
---|
926 | /*
|
---|
927 | * Check for URG data
|
---|
928 | * This will soread as well, so no need to
|
---|
929 | * test for readfds below if this succeeds
|
---|
930 | */
|
---|
931 |
|
---|
932 | /* out-of-band data */
|
---|
933 | if (CHECK_FD_SET(so, NetworkEvents, xfds))
|
---|
934 | {
|
---|
935 | sorecvoob(pData, so);
|
---|
936 | }
|
---|
937 |
|
---|
938 | /*
|
---|
939 | * Check sockets for reading
|
---|
940 | */
|
---|
941 | else if ( CHECK_FD_SET(so, NetworkEvents, readfds)
|
---|
942 | || WIN_CHECK_FD_SET(so, NetworkEvents, acceptds))
|
---|
943 | {
|
---|
944 | /*
|
---|
945 | * Check for incoming connections
|
---|
946 | */
|
---|
947 | if (so->so_state & SS_FACCEPTCONN)
|
---|
948 | {
|
---|
949 | TCP_CONNECT(pData, so);
|
---|
950 | #if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
|
---|
951 | if (!(NetworkEvents.lNetworkEvents & FD_CLOSE))
|
---|
952 | #endif
|
---|
953 | CONTINUE(tcp);
|
---|
954 | }
|
---|
955 |
|
---|
956 | ret = soread(pData, so);
|
---|
957 | /* Output it if we read something */
|
---|
958 | if (ret > 0)
|
---|
959 | TCP_OUTPUT(pData, sototcpcb(so));
|
---|
960 | }
|
---|
961 |
|
---|
962 | #if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
|
---|
963 | /*
|
---|
964 | * Check for FD_CLOSE events.
|
---|
965 | */
|
---|
966 | if (NetworkEvents.lNetworkEvents & FD_CLOSE)
|
---|
967 | {
|
---|
968 | /*
|
---|
969 | * drain the socket
|
---|
970 | */
|
---|
971 | for (;;)
|
---|
972 | {
|
---|
973 | ret = soread(pData, so);
|
---|
974 | if (ret > 0)
|
---|
975 | TCP_OUTPUT(pData, sototcpcb(so));
|
---|
976 | else
|
---|
977 | break;
|
---|
978 | }
|
---|
979 | }
|
---|
980 | #endif
|
---|
981 |
|
---|
982 | /*
|
---|
983 | * Check sockets for writing
|
---|
984 | */
|
---|
985 | if (CHECK_FD_SET(so, NetworkEvents, writefds))
|
---|
986 | {
|
---|
987 | /*
|
---|
988 | * Check for non-blocking, still-connecting sockets
|
---|
989 | */
|
---|
990 | if (so->so_state & SS_ISFCONNECTING)
|
---|
991 | {
|
---|
992 | Log2(("connecting %R[natsock] catched\n", so));
|
---|
993 | /* Connected */
|
---|
994 | so->so_state &= ~SS_ISFCONNECTING;
|
---|
995 |
|
---|
996 | /*
|
---|
997 | * This should be probably guarded by PROBE_CONN too. Anyway,
|
---|
998 | * we disable it on OS/2 because the below send call returns
|
---|
999 | * EFAULT which causes the opened TCP socket to close right
|
---|
1000 | * after it has been opened and connected.
|
---|
1001 | */
|
---|
1002 | #ifndef RT_OS_OS2
|
---|
1003 | ret = send(so->s, (const char *)&ret, 0, 0);
|
---|
1004 | if (ret < 0)
|
---|
1005 | {
|
---|
1006 | /* XXXXX Must fix, zero bytes is a NOP */
|
---|
1007 | if ( errno == EAGAIN
|
---|
1008 | || errno == EWOULDBLOCK
|
---|
1009 | || errno == EINPROGRESS
|
---|
1010 | || errno == ENOTCONN)
|
---|
1011 | CONTINUE(tcp);
|
---|
1012 |
|
---|
1013 | /* else failed */
|
---|
1014 | so->so_state = SS_NOFDREF;
|
---|
1015 | }
|
---|
1016 | /* else so->so_state &= ~SS_ISFCONNECTING; */
|
---|
1017 | #endif
|
---|
1018 |
|
---|
1019 | /*
|
---|
1020 | * Continue tcp_input
|
---|
1021 | */
|
---|
1022 | TCP_INPUT(pData, (struct mbuf *)NULL, sizeof(struct ip), so);
|
---|
1023 | /* continue; */
|
---|
1024 | }
|
---|
1025 | else
|
---|
1026 | SOWRITE(ret, pData, so);
|
---|
1027 | /*
|
---|
1028 | * XXX If we wrote something (a lot), there could be the need
|
---|
1029 | * for a window update. In the worst case, the remote will send
|
---|
1030 | * a window probe to get things going again.
|
---|
1031 | */
|
---|
1032 | }
|
---|
1033 |
|
---|
1034 | /*
|
---|
1035 | * Probe a still-connecting, non-blocking socket
|
---|
1036 | * to check if it's still alive
|
---|
1037 | */
|
---|
1038 | #ifdef PROBE_CONN
|
---|
1039 | if (so->so_state & SS_ISFCONNECTING)
|
---|
1040 | {
|
---|
1041 | ret = recv(so->s, (char *)&ret, 0, 0);
|
---|
1042 |
|
---|
1043 | if (ret < 0)
|
---|
1044 | {
|
---|
1045 | /* XXX */
|
---|
1046 | if ( errno == EAGAIN
|
---|
1047 | || errno == EWOULDBLOCK
|
---|
1048 | || errno == EINPROGRESS
|
---|
1049 | || errno == ENOTCONN)
|
---|
1050 | {
|
---|
1051 | CONTINUE(tcp); /* Still connecting, continue */
|
---|
1052 | }
|
---|
1053 |
|
---|
1054 | /* else failed */
|
---|
1055 | so->so_state = SS_NOFDREF;
|
---|
1056 |
|
---|
1057 | /* tcp_input will take care of it */
|
---|
1058 | }
|
---|
1059 | else
|
---|
1060 | {
|
---|
1061 | ret = send(so->s, &ret, 0, 0);
|
---|
1062 | if (ret < 0)
|
---|
1063 | {
|
---|
1064 | /* XXX */
|
---|
1065 | if ( errno == EAGAIN
|
---|
1066 | || errno == EWOULDBLOCK
|
---|
1067 | || errno == EINPROGRESS
|
---|
1068 | || errno == ENOTCONN)
|
---|
1069 | {
|
---|
1070 | CONTINUE(tcp);
|
---|
1071 | }
|
---|
1072 | /* else failed */
|
---|
1073 | so->so_state = SS_NOFDREF;
|
---|
1074 | }
|
---|
1075 | else
|
---|
1076 | so->so_state &= ~SS_ISFCONNECTING;
|
---|
1077 |
|
---|
1078 | }
|
---|
1079 | TCP_INPUT((struct mbuf *)NULL, sizeof(struct ip),so);
|
---|
1080 | } /* SS_ISFCONNECTING */
|
---|
1081 | #endif
|
---|
1082 | #ifndef RT_OS_WINDOWS
|
---|
1083 | if ( UNIX_CHECK_FD_SET(so, NetworkEvents, rdhup)
|
---|
1084 | && UNIX_CHECK_FD_SET(so, NetworkEvents, rderr))
|
---|
1085 | {
|
---|
1086 | if (so->so_state & SS_ISFCONNECTING)
|
---|
1087 | {
|
---|
1088 | so->so_state = SS_NOFDREF;
|
---|
1089 | TCP_INPUT(pData, (struct mbuf *)NULL, sizeof(struct ip), so);
|
---|
1090 | }
|
---|
1091 | /* Here should be other error handlings */
|
---|
1092 | }
|
---|
1093 | #endif
|
---|
1094 | LOOP_LABEL(tcp, so, so_next);
|
---|
1095 | }
|
---|
1096 |
|
---|
1097 | /*
|
---|
1098 | * Now UDP sockets.
|
---|
1099 | * Incoming packets are sent straight away, they're not buffered.
|
---|
1100 | * Incoming UDP data isn't buffered either.
|
---|
1101 | */
|
---|
1102 | QSOCKET_FOREACH(so, so_next, udp)
|
---|
1103 | /* { */
|
---|
1104 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
1105 | if ( so->so_state & SS_NOFDREF
|
---|
1106 | && so->so_deleted == 1)
|
---|
1107 | {
|
---|
1108 | struct socket *son, *sop = NULL;
|
---|
1109 | QSOCKET_LOCK(udb);
|
---|
1110 | if (so->so_next != NULL)
|
---|
1111 | {
|
---|
1112 | if (so->so_next != &udb)
|
---|
1113 | SOCKET_LOCK(so->so_next);
|
---|
1114 | son = so->so_next;
|
---|
1115 | }
|
---|
1116 | if ( so->so_prev != &udb
|
---|
1117 | && so->so_prev != NULL)
|
---|
1118 | {
|
---|
1119 | SOCKET_LOCK(so->so_prev);
|
---|
1120 | sop = so->so_prev;
|
---|
1121 | }
|
---|
1122 | QSOCKET_UNLOCK(udb);
|
---|
1123 | remque(pData, so);
|
---|
1124 | NSOCK_DEC();
|
---|
1125 | SOCKET_UNLOCK(so);
|
---|
1126 | SOCKET_LOCK_DESTROY(so);
|
---|
1127 | RTMemFree(so);
|
---|
1128 | so_next = son;
|
---|
1129 | if (sop != NULL)
|
---|
1130 | SOCKET_UNLOCK(sop);
|
---|
1131 | CONTINUE_NO_UNLOCK(udp);
|
---|
1132 | }
|
---|
1133 | #endif
|
---|
1134 | POLL_UDP_EVENTS(rc, error, so, &NetworkEvents);
|
---|
1135 |
|
---|
1136 | LOG_NAT_SOCK(so, UDP, &NetworkEvents, readfds, writefds, xfds);
|
---|
1137 |
|
---|
1138 | if (so->s != -1 && CHECK_FD_SET(so, NetworkEvents, readfds))
|
---|
1139 | {
|
---|
1140 | SORECVFROM(pData, so);
|
---|
1141 | }
|
---|
1142 | LOOP_LABEL(udp, so, so_next);
|
---|
1143 | }
|
---|
1144 |
|
---|
1145 | }
|
---|
1146 |
|
---|
1147 | #ifndef VBOX_WITH_SLIRP_MT
|
---|
1148 | /*
|
---|
1149 | * See if we can start outputting
|
---|
1150 | */
|
---|
1151 | if (if_queued && link_up)
|
---|
1152 | if_start(pData);
|
---|
1153 | #endif
|
---|
1154 |
|
---|
1155 | STAM_PROFILE_STOP(&pData->StatPoll, a);
|
---|
1156 | }
|
---|
1157 |
|
---|
1158 | #define ETH_ALEN 6
|
---|
1159 | #define ETH_HLEN 14
|
---|
1160 |
|
---|
1161 | #define ARPOP_REQUEST 1 /* ARP request */
|
---|
1162 | #define ARPOP_REPLY 2 /* ARP reply */
|
---|
1163 |
|
---|
1164 | struct ethhdr
|
---|
1165 | {
|
---|
1166 | unsigned char h_dest[ETH_ALEN]; /* destination eth addr */
|
---|
1167 | unsigned char h_source[ETH_ALEN]; /* source ether addr */
|
---|
1168 | unsigned short h_proto; /* packet type ID field */
|
---|
1169 | };
|
---|
1170 |
|
---|
1171 | struct arphdr
|
---|
1172 | {
|
---|
1173 | unsigned short ar_hrd; /* format of hardware address */
|
---|
1174 | unsigned short ar_pro; /* format of protocol address */
|
---|
1175 | unsigned char ar_hln; /* length of hardware address */
|
---|
1176 | unsigned char ar_pln; /* length of protocol address */
|
---|
1177 | unsigned short ar_op; /* ARP opcode (command) */
|
---|
1178 |
|
---|
1179 | /*
|
---|
1180 | * Ethernet looks like this : This bit is variable sized however...
|
---|
1181 | */
|
---|
1182 | unsigned char ar_sha[ETH_ALEN]; /* sender hardware address */
|
---|
1183 | unsigned char ar_sip[4]; /* sender IP address */
|
---|
1184 | unsigned char ar_tha[ETH_ALEN]; /* target hardware address */
|
---|
1185 | unsigned char ar_tip[4]; /* target IP address */
|
---|
1186 | };
|
---|
1187 |
|
---|
1188 | static
|
---|
1189 | #ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
1190 | void arp_input(PNATState pData, struct mbuf *m)
|
---|
1191 | #else
|
---|
1192 | void arp_input(PNATState pData, const uint8_t *pkt, int pkt_len)
|
---|
1193 | #endif
|
---|
1194 | {
|
---|
1195 | struct ethhdr *eh;
|
---|
1196 | struct ethhdr *reh;
|
---|
1197 | struct arphdr *ah;
|
---|
1198 | struct arphdr *rah;
|
---|
1199 | int ar_op;
|
---|
1200 | struct ex_list *ex_ptr;
|
---|
1201 | uint32_t htip;
|
---|
1202 | #ifndef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
1203 | uint8_t arp_reply[sizeof(struct arphdr) + ETH_HLEN];
|
---|
1204 | eh = (struct ethhdr *)pkt;
|
---|
1205 | #else
|
---|
1206 | struct mbuf *mr;
|
---|
1207 | eh = mtod(m, struct ethhdr *);
|
---|
1208 | #endif
|
---|
1209 | ah = (struct arphdr *)&eh[1];
|
---|
1210 | htip = ntohl(*(uint32_t*)ah->ar_tip);
|
---|
1211 |
|
---|
1212 | #ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
1213 | mr = m_get(pData);
|
---|
1214 | mr->m_data += if_maxlinkhdr;
|
---|
1215 | mr->m_len = sizeof(struct arphdr);
|
---|
1216 | rah = mtod(mr, struct arphdr *);
|
---|
1217 | #else
|
---|
1218 | reh = (struct ethhdr *)arp_reply;
|
---|
1219 | rah = (struct arphdr *)&reh[1];
|
---|
1220 | #endif
|
---|
1221 |
|
---|
1222 | ar_op = ntohs(ah->ar_op);
|
---|
1223 | switch(ar_op)
|
---|
1224 | {
|
---|
1225 | case ARPOP_REQUEST:
|
---|
1226 | if ((htip & pData->netmask) == ntohl(special_addr.s_addr))
|
---|
1227 | {
|
---|
1228 | if ( CTL_CHECK(htip,CTL_DNS)
|
---|
1229 | || CTL_CHECK(htip, CTL_ALIAS)
|
---|
1230 | || CTL_CHECK(htip, CTL_TFTP))
|
---|
1231 | goto arp_ok;
|
---|
1232 | for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
|
---|
1233 | {
|
---|
1234 | if ((htip & ~pData->netmask) == ex_ptr->ex_addr)
|
---|
1235 | goto arp_ok;
|
---|
1236 | }
|
---|
1237 | return;
|
---|
1238 | arp_ok:
|
---|
1239 |
|
---|
1240 | #ifndef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
1241 | memcpy(reh->h_dest, eh->h_source, ETH_ALEN);
|
---|
1242 | memcpy(reh->h_source, &special_addr, ETH_ALEN);
|
---|
1243 | reh->h_source[5] = ah->ar_tip[3];
|
---|
1244 | reh->h_proto = htons(ETH_P_ARP);
|
---|
1245 | #endif
|
---|
1246 | rah->ar_hrd = htons(1);
|
---|
1247 | rah->ar_pro = htons(ETH_P_IP);
|
---|
1248 | rah->ar_hln = ETH_ALEN;
|
---|
1249 | rah->ar_pln = 4;
|
---|
1250 | rah->ar_op = htons(ARPOP_REPLY);
|
---|
1251 | memcpy(rah->ar_sha, special_ethaddr, ETH_ALEN);
|
---|
1252 |
|
---|
1253 | switch (htip & ~pData->netmask)
|
---|
1254 | {
|
---|
1255 | case CTL_DNS:
|
---|
1256 | case CTL_ALIAS:
|
---|
1257 | rah->ar_sha[5] = (uint8_t)(htip & ~pData->netmask);
|
---|
1258 | break;
|
---|
1259 | default:;
|
---|
1260 | }
|
---|
1261 |
|
---|
1262 | memcpy(rah->ar_sip, ah->ar_tip, 4);
|
---|
1263 | memcpy(rah->ar_tha, ah->ar_sha, ETH_ALEN);
|
---|
1264 | memcpy(rah->ar_tip, ah->ar_sip, 4);
|
---|
1265 | #ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
1266 | if_encap(pData, ETH_P_ARP, mr);
|
---|
1267 | m_free(pData, m);
|
---|
1268 | #else
|
---|
1269 | slirp_output(pData->pvUser, arp_reply, sizeof(arp_reply));
|
---|
1270 | #endif
|
---|
1271 | }
|
---|
1272 | break;
|
---|
1273 | default:
|
---|
1274 | break;
|
---|
1275 | }
|
---|
1276 | }
|
---|
1277 |
|
---|
1278 | void slirp_input(PNATState pData, const uint8_t *pkt, int pkt_len)
|
---|
1279 | {
|
---|
1280 | struct mbuf *m;
|
---|
1281 | int proto;
|
---|
1282 | static bool fWarnedIpv6;
|
---|
1283 |
|
---|
1284 | if (pkt_len < ETH_HLEN)
|
---|
1285 | {
|
---|
1286 | LogRel(("NAT: packet having size %d has been ingnored\n", pkt_len));
|
---|
1287 | return;
|
---|
1288 | }
|
---|
1289 |
|
---|
1290 | m = m_get(pData);
|
---|
1291 | if (!m)
|
---|
1292 | {
|
---|
1293 | LogRel(("can't allocate new mbuf\n"));
|
---|
1294 | return;
|
---|
1295 | }
|
---|
1296 |
|
---|
1297 | /* Note: we add to align the IP header */
|
---|
1298 |
|
---|
1299 | if (M_FREEROOM(m) < pkt_len)
|
---|
1300 | m_inc(m, pkt_len);
|
---|
1301 |
|
---|
1302 | m->m_len = pkt_len ;
|
---|
1303 | memcpy(m->m_data, pkt, pkt_len);
|
---|
1304 |
|
---|
1305 | proto = ntohs(*(uint16_t *)(pkt + 12));
|
---|
1306 | switch(proto)
|
---|
1307 | {
|
---|
1308 | case ETH_P_ARP:
|
---|
1309 | #ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
1310 | arp_input(pData, m);
|
---|
1311 | #else
|
---|
1312 | arp_input(pData, pkt, pkt_len);
|
---|
1313 | m_free(pData, m);
|
---|
1314 | #endif
|
---|
1315 | break;
|
---|
1316 | case ETH_P_IP:
|
---|
1317 | /* Update time. Important if the network is very quiet, as otherwise
|
---|
1318 | * the first outgoing connection gets an incorrect timestamp. */
|
---|
1319 | updtime(pData);
|
---|
1320 | m->m_data += ETH_HLEN;
|
---|
1321 | m->m_len -= ETH_HLEN;
|
---|
1322 | ip_input(pData, m);
|
---|
1323 | break;
|
---|
1324 | case ETH_P_IPV6:
|
---|
1325 | m_free(pData, m);
|
---|
1326 | if (!fWarnedIpv6)
|
---|
1327 | {
|
---|
1328 | LogRel(("NAT: IPv6 not supported\n"));
|
---|
1329 | fWarnedIpv6 = true;
|
---|
1330 | }
|
---|
1331 | break;
|
---|
1332 | default:
|
---|
1333 | LogRel(("NAT: Unsupported protocol %x\n", proto));
|
---|
1334 | m_free(pData, m);
|
---|
1335 | break;
|
---|
1336 | }
|
---|
1337 | RTMemFree((void *)pkt);
|
---|
1338 | }
|
---|
1339 |
|
---|
1340 | /* output the IP packet to the ethernet device */
|
---|
1341 | #ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
1342 | void if_encap(PNATState pData, uint16_t eth_proto, struct mbuf *m)
|
---|
1343 | #else
|
---|
1344 | void if_encap(PNATState pData, uint8_t *ip_data, int ip_data_len)
|
---|
1345 | #endif
|
---|
1346 | {
|
---|
1347 | #ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
1348 | struct ethhdr *eh;
|
---|
1349 | uint8_t *buf = RTMemAlloc(1600);
|
---|
1350 | m->m_data -= if_maxlinkhdr;
|
---|
1351 | m->m_len += ETH_HLEN;
|
---|
1352 | eh = mtod(m, struct ethhdr *);
|
---|
1353 | #else
|
---|
1354 | uint8_t buf[1600];
|
---|
1355 | struct ethhdr *eh = (struct ethhdr *)buf;
|
---|
1356 |
|
---|
1357 | if (ip_data_len + ETH_HLEN > sizeof(buf))
|
---|
1358 | return;
|
---|
1359 |
|
---|
1360 | memcpy(buf + sizeof(struct ethhdr), ip_data, ip_data_len);
|
---|
1361 | #endif
|
---|
1362 |
|
---|
1363 |
|
---|
1364 | memcpy(eh->h_dest, client_ethaddr, ETH_ALEN);
|
---|
1365 | memcpy(eh->h_source, special_ethaddr, ETH_ALEN - 1);
|
---|
1366 | /* XXX: not correct */
|
---|
1367 | eh->h_source[5] = CTL_ALIAS;
|
---|
1368 | #ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
1369 | eh->h_proto = htons(eth_proto);
|
---|
1370 | #if 0
|
---|
1371 | slirp_output(pData->pvUser, m, mtod(m, uint8_t *), m->m_len);
|
---|
1372 | #else
|
---|
1373 | memcpy(buf, mtod(m, uint8_t *), m->m_len);
|
---|
1374 | slirp_output(pData->pvUser, NULL, buf, m->m_len);
|
---|
1375 | m_free(pData, m);
|
---|
1376 | #endif
|
---|
1377 | #else
|
---|
1378 | eh->h_proto = htons(ETH_P_IP);
|
---|
1379 | slirp_output(pData->pvUser, buf, ip_data_len + ETH_HLEN);
|
---|
1380 | #endif
|
---|
1381 | }
|
---|
1382 |
|
---|
1383 | int slirp_redir(PNATState pData, int is_udp, int host_port,
|
---|
1384 | struct in_addr guest_addr, int guest_port)
|
---|
1385 | {
|
---|
1386 | if (is_udp)
|
---|
1387 | {
|
---|
1388 | if (!udp_listen(pData, htons(host_port), guest_addr.s_addr,
|
---|
1389 | htons(guest_port), 0))
|
---|
1390 | return -1;
|
---|
1391 | }
|
---|
1392 | else
|
---|
1393 | {
|
---|
1394 | if (!solisten(pData, htons(host_port), guest_addr.s_addr,
|
---|
1395 | htons(guest_port), 0))
|
---|
1396 | return -1;
|
---|
1397 | }
|
---|
1398 | return 0;
|
---|
1399 | }
|
---|
1400 |
|
---|
1401 | int slirp_add_exec(PNATState pData, int do_pty, const char *args, int addr_low_byte,
|
---|
1402 | int guest_port)
|
---|
1403 | {
|
---|
1404 | return add_exec(&exec_list, do_pty, (char *)args,
|
---|
1405 | addr_low_byte, htons(guest_port));
|
---|
1406 | }
|
---|
1407 |
|
---|
1408 | void slirp_set_ethaddr(PNATState pData, const uint8_t *ethaddr)
|
---|
1409 | {
|
---|
1410 | memcpy(client_ethaddr, ethaddr, ETH_ALEN);
|
---|
1411 | }
|
---|
1412 |
|
---|
1413 | #if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
|
---|
1414 | HANDLE *slirp_get_events(PNATState pData)
|
---|
1415 | {
|
---|
1416 | return pData->phEvents;
|
---|
1417 | }
|
---|
1418 | void slirp_register_external_event(PNATState pData, HANDLE hEvent, int index)
|
---|
1419 | {
|
---|
1420 | pData->phEvents[index] = hEvent;
|
---|
1421 | }
|
---|
1422 | #endif
|
---|
1423 |
|
---|
1424 | unsigned int slirp_get_timeout_ms(PNATState pData)
|
---|
1425 | {
|
---|
1426 | if (link_up)
|
---|
1427 | {
|
---|
1428 | if (time_fasttimo)
|
---|
1429 | return 2;
|
---|
1430 | if (do_slowtimo)
|
---|
1431 | return 500; /* see PR_SLOWHZ */
|
---|
1432 | }
|
---|
1433 | return 0;
|
---|
1434 | }
|
---|
1435 |
|
---|
1436 | #ifndef RT_OS_WINDOWS
|
---|
1437 | int slirp_get_nsock(PNATState pData)
|
---|
1438 | {
|
---|
1439 | return pData->nsock;
|
---|
1440 | }
|
---|
1441 | #endif
|
---|
1442 |
|
---|
1443 | /*
|
---|
1444 | * this function called from NAT thread
|
---|
1445 | */
|
---|
1446 | void slirp_post_sent(PNATState pData, void *pvArg)
|
---|
1447 | {
|
---|
1448 | struct socket *so = 0;
|
---|
1449 | struct tcpcb *tp = 0;
|
---|
1450 | struct mbuf *m = (struct mbuf *)pvArg;
|
---|
1451 | m_free(pData, m);
|
---|
1452 | }
|
---|
1453 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
1454 | void slirp_process_queue(PNATState pData)
|
---|
1455 | {
|
---|
1456 | RTReqProcess(pData->pReqQueue, RT_INDEFINITE_WAIT);
|
---|
1457 | }
|
---|
1458 | void *slirp_get_queue(PNATState pData)
|
---|
1459 | {
|
---|
1460 | return pData->pReqQueue;
|
---|
1461 | }
|
---|
1462 | #endif
|
---|