1 | #include "slirp.h"
|
---|
2 | #ifdef RT_OS_OS2
|
---|
3 | # include <paths.h>
|
---|
4 | #endif
|
---|
5 |
|
---|
6 | #include <VBox/err.h>
|
---|
7 | #include <iprt/assert.h>
|
---|
8 |
|
---|
9 | static const uint8_t special_ethaddr[6] = {
|
---|
10 | 0x52, 0x54, 0x00, 0x12, 0x35, 0x00
|
---|
11 | };
|
---|
12 |
|
---|
13 | #ifdef _WIN32
|
---|
14 |
|
---|
15 | static int get_dns_addr_domain(PNATState pData, bool fVerbose,
|
---|
16 | struct in_addr *pdns_addr,
|
---|
17 | const char **ppszDomain)
|
---|
18 | {
|
---|
19 | int rc = 0;
|
---|
20 | FIXED_INFO *FixedInfo=NULL;
|
---|
21 | ULONG BufLen;
|
---|
22 | DWORD ret;
|
---|
23 | IP_ADDR_STRING *pIPAddr;
|
---|
24 | struct in_addr tmp_addr;
|
---|
25 |
|
---|
26 | FixedInfo = (FIXED_INFO *)GlobalAlloc(GPTR, sizeof(FIXED_INFO));
|
---|
27 | BufLen = sizeof(FIXED_INFO);
|
---|
28 |
|
---|
29 | /** @todo: this API returns all DNS servers, no matter whether the
|
---|
30 | * corresponding network adapter is disabled or not. Maybe replace
|
---|
31 | * this by GetAdapterAddresses(), which is XP/Vista only though. */
|
---|
32 | if (ERROR_BUFFER_OVERFLOW == GetNetworkParams(FixedInfo, &BufLen)) {
|
---|
33 | if (FixedInfo) {
|
---|
34 | GlobalFree(FixedInfo);
|
---|
35 | FixedInfo = NULL;
|
---|
36 | }
|
---|
37 | FixedInfo = GlobalAlloc(GPTR, BufLen);
|
---|
38 | }
|
---|
39 |
|
---|
40 | if ((ret = GetNetworkParams(FixedInfo, &BufLen)) != ERROR_SUCCESS) {
|
---|
41 | Log(("GetNetworkParams failed. ret = %08x\n", (u_int)ret ));
|
---|
42 | if (FixedInfo) {
|
---|
43 | GlobalFree(FixedInfo);
|
---|
44 | FixedInfo = NULL;
|
---|
45 | }
|
---|
46 | rc = -1;
|
---|
47 | goto get_dns_prefix;
|
---|
48 | }
|
---|
49 |
|
---|
50 | pIPAddr = &(FixedInfo->DnsServerList);
|
---|
51 | inet_aton(pIPAddr->IpAddress.String, &tmp_addr);
|
---|
52 | Log(("nat: DNS Servers:\n"));
|
---|
53 | if (fVerbose || pdns_addr->s_addr != tmp_addr.s_addr)
|
---|
54 | LogRel(("NAT: DNS address: %s\n", pIPAddr->IpAddress.String));
|
---|
55 | *pdns_addr = tmp_addr;
|
---|
56 |
|
---|
57 | pIPAddr = FixedInfo -> DnsServerList.Next;
|
---|
58 | while ( pIPAddr )
|
---|
59 | {
|
---|
60 | if (fVerbose)
|
---|
61 | LogRel(("NAT: ignored DNS address: %s\n", pIPAddr ->IpAddress.String));
|
---|
62 | pIPAddr = pIPAddr ->Next;
|
---|
63 | }
|
---|
64 | if (FixedInfo) {
|
---|
65 | GlobalFree(FixedInfo);
|
---|
66 | FixedInfo = NULL;
|
---|
67 | }
|
---|
68 |
|
---|
69 | get_dns_prefix:
|
---|
70 | if (ppszDomain)
|
---|
71 | {
|
---|
72 | OSVERSIONINFO ver;
|
---|
73 | char szDnsDomain[256];
|
---|
74 | DWORD dwSize = sizeof(szDnsDomain);
|
---|
75 |
|
---|
76 | *ppszDomain = NULL;
|
---|
77 | GetVersionEx(&ver);
|
---|
78 | if (ver.dwMajorVersion >= 5)
|
---|
79 | {
|
---|
80 | /* GetComputerNameEx exists in Windows versions starting with 2000. */
|
---|
81 | if (GetComputerNameEx(ComputerNameDnsDomain, szDnsDomain, &dwSize))
|
---|
82 | {
|
---|
83 | if (szDnsDomain[0])
|
---|
84 | {
|
---|
85 | /* Just non-empty strings are valid. */
|
---|
86 | *ppszDomain = RTStrDup(szDnsDomain);
|
---|
87 | if (pData->fPassDomain)
|
---|
88 | {
|
---|
89 | if (fVerbose)
|
---|
90 | LogRel(("NAT: passing domain name %s\n", szDnsDomain));
|
---|
91 | }
|
---|
92 | else
|
---|
93 | Log(("nat: ignoring domain %s\n", szDnsDomain));
|
---|
94 | }
|
---|
95 | }
|
---|
96 | else
|
---|
97 | Log(("nat: GetComputerNameEx failed (%d)\n", GetLastError()));
|
---|
98 | }
|
---|
99 | }
|
---|
100 | return rc;
|
---|
101 | }
|
---|
102 |
|
---|
103 | #else
|
---|
104 |
|
---|
105 | static int get_dns_addr_domain(PNATState pData, bool fVerbose,
|
---|
106 | struct in_addr *pdns_addr,
|
---|
107 | const char **ppszDomain)
|
---|
108 | {
|
---|
109 | char buff[512];
|
---|
110 | char buff2[256];
|
---|
111 | FILE *f;
|
---|
112 | int found = 0;
|
---|
113 | struct in_addr tmp_addr;
|
---|
114 |
|
---|
115 | #ifdef RT_OS_OS2
|
---|
116 | /* Try various locations. */
|
---|
117 | char *etc = getenv("ETC");
|
---|
118 | f = NULL;
|
---|
119 | if (etc)
|
---|
120 | {
|
---|
121 | snprintf(buff, sizeof(buff), "%s/RESOLV2", etc);
|
---|
122 | f = fopen(buff, "rt");
|
---|
123 | }
|
---|
124 | if (!f) {
|
---|
125 | snprintf(buff, sizeof(buff), "%s/RESOLV2", _PATH_ETC);
|
---|
126 | f = fopen(buff, "rt");
|
---|
127 | }
|
---|
128 | if (!f) {
|
---|
129 | snprintf(buff, sizeof(buff), "%s/resolv.conf", _PATH_ETC);
|
---|
130 | f = fopen(buff, "rt");
|
---|
131 | }
|
---|
132 | #else
|
---|
133 | f = fopen("/etc/resolv.conf", "r");
|
---|
134 | #endif
|
---|
135 | if (!f)
|
---|
136 | return -1;
|
---|
137 |
|
---|
138 | if (ppszDomain)
|
---|
139 | *ppszDomain = NULL;
|
---|
140 | Log(("nat: DNS Servers:\n"));
|
---|
141 | while (fgets(buff, 512, f) != NULL) {
|
---|
142 | if (sscanf(buff, "nameserver%*[ \t]%256s", buff2) == 1) {
|
---|
143 | if (!inet_aton(buff2, &tmp_addr))
|
---|
144 | continue;
|
---|
145 | if (tmp_addr.s_addr == loopback_addr.s_addr)
|
---|
146 | tmp_addr = our_addr;
|
---|
147 | /* If it's the first one, set it to dns_addr */
|
---|
148 | if (!found)
|
---|
149 | {
|
---|
150 | if (fVerbose || pdns_addr->s_addr != tmp_addr.s_addr)
|
---|
151 | LogRel(("NAT: DNS address: %s\n", buff2));
|
---|
152 | *pdns_addr = tmp_addr;
|
---|
153 | }
|
---|
154 | else
|
---|
155 | {
|
---|
156 | if (fVerbose)
|
---|
157 | LogRel(("NAT: ignored DNS address: %s\n", buff2));
|
---|
158 | }
|
---|
159 | found++;
|
---|
160 | }
|
---|
161 | if ( ppszDomain
|
---|
162 | && (!strncmp(buff, "domain", 6) || !strncmp(buff, "search", 6)))
|
---|
163 | {
|
---|
164 | /* Domain name/search list present. Pick first entry */
|
---|
165 | if (*ppszDomain == NULL)
|
---|
166 | {
|
---|
167 | char *tok;
|
---|
168 | char *saveptr;
|
---|
169 | tok = strtok_r(&buff[6], " \t\n", &saveptr);
|
---|
170 | if (tok)
|
---|
171 | {
|
---|
172 | *ppszDomain = RTStrDup(tok);
|
---|
173 | if (pData->fPassDomain)
|
---|
174 | {
|
---|
175 | if (fVerbose)
|
---|
176 | LogRel(("NAT: passing domain name %s\n", tok));
|
---|
177 | }
|
---|
178 | else
|
---|
179 | Log(("nat: ignoring domain %s\n", tok));
|
---|
180 | }
|
---|
181 | }
|
---|
182 | }
|
---|
183 | }
|
---|
184 | fclose(f);
|
---|
185 | if (!found)
|
---|
186 | return -1;
|
---|
187 | return 0;
|
---|
188 | }
|
---|
189 |
|
---|
190 | #endif
|
---|
191 |
|
---|
192 | int get_dns_addr(PNATState pData, struct in_addr *pdns_addr)
|
---|
193 | {
|
---|
194 | return get_dns_addr_domain(pData, false, pdns_addr, NULL);
|
---|
195 | }
|
---|
196 |
|
---|
197 | int slirp_init(PNATState *ppData, const char *pszNetAddr, bool fPassDomain,
|
---|
198 | const char *pszTFTPPrefix, const char *pszBootFile,
|
---|
199 | void *pvUser)
|
---|
200 | {
|
---|
201 | int fNATfailed = 0;
|
---|
202 | PNATState pData = malloc(sizeof(NATState));
|
---|
203 | *ppData = pData;
|
---|
204 | if (!pData)
|
---|
205 | return VERR_NO_MEMORY;
|
---|
206 | memset(pData, '\0', sizeof(NATState));
|
---|
207 | pData->fPassDomain = fPassDomain;
|
---|
208 | pData->pvUser = pvUser;
|
---|
209 | #if ARCH_BITS == 64
|
---|
210 | pData->cpvHashUsed = 1;
|
---|
211 | #endif
|
---|
212 | tftp_prefix = pszTFTPPrefix;
|
---|
213 | bootp_filename = pszBootFile;
|
---|
214 |
|
---|
215 | #ifdef _WIN32
|
---|
216 | {
|
---|
217 | WSADATA Data;
|
---|
218 | WSAStartup(MAKEWORD(2,0), &Data);
|
---|
219 | }
|
---|
220 | #endif
|
---|
221 |
|
---|
222 | Assert(sizeof(struct ip) == 20);
|
---|
223 | link_up = 1;
|
---|
224 |
|
---|
225 | if_init(pData);
|
---|
226 | ip_init(pData);
|
---|
227 |
|
---|
228 | /* Initialise mbufs *after* setting the MTU */
|
---|
229 | m_init(pData);
|
---|
230 |
|
---|
231 | /* set default addresses */
|
---|
232 | inet_aton("127.0.0.1", &loopback_addr);
|
---|
233 | inet_aton("127.0.0.1", &dns_addr);
|
---|
234 |
|
---|
235 | if (get_dns_addr_domain(pData, true, &dns_addr, &pData->pszDomain) < 0)
|
---|
236 | fNATfailed = 1;
|
---|
237 |
|
---|
238 | inet_aton(pszNetAddr, &special_addr);
|
---|
239 | alias_addr.s_addr = special_addr.s_addr | htonl(CTL_ALIAS);
|
---|
240 | getouraddr(pData);
|
---|
241 | return fNATfailed ? VINF_NAT_DNS : VINF_SUCCESS;
|
---|
242 | }
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * Marks the link as up, making it possible to establish new connections.
|
---|
246 | */
|
---|
247 | void slirp_link_up(PNATState pData)
|
---|
248 | {
|
---|
249 | link_up = 1;
|
---|
250 | }
|
---|
251 |
|
---|
252 | /**
|
---|
253 | * Marks the link as down and cleans up the current connections.
|
---|
254 | */
|
---|
255 | void slirp_link_down(PNATState pData)
|
---|
256 | {
|
---|
257 | struct socket *so;
|
---|
258 |
|
---|
259 | while ((so = tcb.so_next) != &tcb)
|
---|
260 | {
|
---|
261 | if (so->so_state & SS_NOFDREF || so->s == -1)
|
---|
262 | sofree(pData, so);
|
---|
263 | else
|
---|
264 | tcp_drop(pData, sototcpcb(so), 0);
|
---|
265 | }
|
---|
266 |
|
---|
267 | while ((so = udb.so_next) != &udb)
|
---|
268 | udp_detach(pData, so);
|
---|
269 |
|
---|
270 | link_up = 0;
|
---|
271 | }
|
---|
272 |
|
---|
273 | /**
|
---|
274 | * Terminates the slirp component.
|
---|
275 | */
|
---|
276 | void slirp_term(PNATState pData)
|
---|
277 | {
|
---|
278 | if (pData->pszDomain)
|
---|
279 | RTStrFree((char *)(void *)pData->pszDomain);
|
---|
280 |
|
---|
281 | #if ARCH_BITS == 64
|
---|
282 | LogRel(("NAT: cpvHashUsed=%RU32 cpvHashCollisions=%RU32 cpvHashInserts=%RU64 cpvHashDone=%RU64\n",
|
---|
283 | pData->cpvHashUsed, pData->cpvHashCollisions, pData->cpvHashInserts, pData->cpvHashDone));
|
---|
284 | #endif
|
---|
285 |
|
---|
286 | slirp_link_down(pData);
|
---|
287 | #ifdef WIN32
|
---|
288 | WSACleanup();
|
---|
289 | #endif
|
---|
290 | #ifdef LOG_ENABLED
|
---|
291 | Log(("\n"
|
---|
292 | "NAT statistics\n"
|
---|
293 | "--------------\n"
|
---|
294 | "\n"));
|
---|
295 | ipstats(pData);
|
---|
296 | tcpstats(pData);
|
---|
297 | udpstats(pData);
|
---|
298 | icmpstats(pData);
|
---|
299 | mbufstats(pData);
|
---|
300 | sockstats(pData);
|
---|
301 | Log(("\n"
|
---|
302 | "\n"
|
---|
303 | "\n"));
|
---|
304 | #endif
|
---|
305 | free(pData);
|
---|
306 | }
|
---|
307 |
|
---|
308 |
|
---|
309 | #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
|
---|
310 | #define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
|
---|
311 | #define UPD_NFDS(x) if (nfds < (x)) nfds = (x)
|
---|
312 |
|
---|
313 | /*
|
---|
314 | * curtime kept to an accuracy of 1ms
|
---|
315 | */
|
---|
316 | #ifdef _WIN32
|
---|
317 | static void updtime(PNATState pData)
|
---|
318 | {
|
---|
319 | struct _timeb tb;
|
---|
320 |
|
---|
321 | _ftime(&tb);
|
---|
322 | curtime = (u_int)tb.time * (u_int)1000;
|
---|
323 | curtime += (u_int)tb.millitm;
|
---|
324 | }
|
---|
325 | #else
|
---|
326 | static void updtime(PNATState pData)
|
---|
327 | {
|
---|
328 | gettimeofday(&tt, 0);
|
---|
329 |
|
---|
330 | curtime = (u_int)tt.tv_sec * (u_int)1000;
|
---|
331 | curtime += (u_int)tt.tv_usec / (u_int)1000;
|
---|
332 |
|
---|
333 | if ((tt.tv_usec % 1000) >= 500)
|
---|
334 | curtime++;
|
---|
335 | }
|
---|
336 | #endif
|
---|
337 |
|
---|
338 | void slirp_select_fill(PNATState pData, int *pnfds,
|
---|
339 | fd_set *readfds, fd_set *writefds, fd_set *xfds)
|
---|
340 | {
|
---|
341 | struct socket *so, *so_next;
|
---|
342 | struct timeval timeout;
|
---|
343 | int nfds;
|
---|
344 | int tmp_time;
|
---|
345 |
|
---|
346 | nfds = *pnfds;
|
---|
347 | /*
|
---|
348 | * First, TCP sockets
|
---|
349 | */
|
---|
350 | do_slowtimo = 0;
|
---|
351 | if (link_up) {
|
---|
352 | /*
|
---|
353 | * *_slowtimo needs calling if there are IP fragments
|
---|
354 | * in the fragment queue, or there are TCP connections active
|
---|
355 | */
|
---|
356 | do_slowtimo = ((tcb.so_next != &tcb) ||
|
---|
357 | ((struct ipasfrag *)&ipq != u32_to_ptr(pData, ipq.next, struct ipasfrag *)));
|
---|
358 |
|
---|
359 | for (so = tcb.so_next; so != &tcb; so = so_next) {
|
---|
360 | so_next = so->so_next;
|
---|
361 |
|
---|
362 | /*
|
---|
363 | * See if we need a tcp_fasttimo
|
---|
364 | */
|
---|
365 | if (time_fasttimo == 0 && so->so_tcpcb->t_flags & TF_DELACK)
|
---|
366 | time_fasttimo = curtime; /* Flag when we want a fasttimo */
|
---|
367 |
|
---|
368 | /*
|
---|
369 | * NOFDREF can include still connecting to local-host,
|
---|
370 | * newly socreated() sockets etc. Don't want to select these.
|
---|
371 | */
|
---|
372 | if (so->so_state & SS_NOFDREF || so->s == -1)
|
---|
373 | continue;
|
---|
374 |
|
---|
375 | /*
|
---|
376 | * Set for reading sockets which are accepting
|
---|
377 | */
|
---|
378 | if (so->so_state & SS_FACCEPTCONN) {
|
---|
379 | FD_SET(so->s, readfds);
|
---|
380 | UPD_NFDS(so->s);
|
---|
381 | continue;
|
---|
382 | }
|
---|
383 |
|
---|
384 | /*
|
---|
385 | * Set for writing sockets which are connecting
|
---|
386 | */
|
---|
387 | if (so->so_state & SS_ISFCONNECTING) {
|
---|
388 | FD_SET(so->s, writefds);
|
---|
389 | UPD_NFDS(so->s);
|
---|
390 | continue;
|
---|
391 | }
|
---|
392 |
|
---|
393 | /*
|
---|
394 | * Set for writing if we are connected, can send more, and
|
---|
395 | * we have something to send
|
---|
396 | */
|
---|
397 | if (CONN_CANFSEND(so) && so->so_rcv.sb_cc) {
|
---|
398 | FD_SET(so->s, writefds);
|
---|
399 | UPD_NFDS(so->s);
|
---|
400 | }
|
---|
401 |
|
---|
402 | /*
|
---|
403 | * Set for reading (and urgent data) if we are connected, can
|
---|
404 | * receive more, and we have room for it XXX /2 ?
|
---|
405 | */
|
---|
406 | if (CONN_CANFRCV(so) && (so->so_snd.sb_cc < (so->so_snd.sb_datalen/2))) {
|
---|
407 | FD_SET(so->s, readfds);
|
---|
408 | FD_SET(so->s, xfds);
|
---|
409 | UPD_NFDS(so->s);
|
---|
410 | }
|
---|
411 | }
|
---|
412 |
|
---|
413 | /*
|
---|
414 | * UDP sockets
|
---|
415 | */
|
---|
416 | for (so = udb.so_next; so != &udb; so = so_next) {
|
---|
417 | so_next = so->so_next;
|
---|
418 |
|
---|
419 | /*
|
---|
420 | * See if it's timed out
|
---|
421 | */
|
---|
422 | if (so->so_expire) {
|
---|
423 | if (so->so_expire <= curtime) {
|
---|
424 | udp_detach(pData, so);
|
---|
425 | continue;
|
---|
426 | } else
|
---|
427 | do_slowtimo = 1; /* Let socket expire */
|
---|
428 | }
|
---|
429 |
|
---|
430 | /*
|
---|
431 | * When UDP packets are received from over the
|
---|
432 | * link, they're sendto()'d straight away, so
|
---|
433 | * no need for setting for writing
|
---|
434 | * Limit the number of packets queued by this session
|
---|
435 | * to 4. Note that even though we try and limit this
|
---|
436 | * to 4 packets, the session could have more queued
|
---|
437 | * if the packets needed to be fragmented
|
---|
438 | * (XXX <= 4 ?)
|
---|
439 | */
|
---|
440 | if ((so->so_state & SS_ISFCONNECTED) && so->so_queued <= 4) {
|
---|
441 | FD_SET(so->s, readfds);
|
---|
442 | UPD_NFDS(so->s);
|
---|
443 | }
|
---|
444 | }
|
---|
445 | }
|
---|
446 |
|
---|
447 | /*
|
---|
448 | * Setup timeout to use minimum CPU usage, especially when idle
|
---|
449 | */
|
---|
450 |
|
---|
451 | /*
|
---|
452 | * First, see the timeout needed by *timo
|
---|
453 | */
|
---|
454 | timeout.tv_sec = 0;
|
---|
455 | timeout.tv_usec = -1;
|
---|
456 | /*
|
---|
457 | * If a slowtimo is needed, set timeout to 500ms from the last
|
---|
458 | * slow timeout. If a fast timeout is needed, set timeout within
|
---|
459 | * 200ms of when it was requested.
|
---|
460 | */
|
---|
461 | if (do_slowtimo) {
|
---|
462 | /* XXX + 10000 because some select()'s aren't that accurate */
|
---|
463 | timeout.tv_usec = ((500 - (curtime - last_slowtimo)) * 1000) + 10000;
|
---|
464 | if (timeout.tv_usec < 0)
|
---|
465 | timeout.tv_usec = 0;
|
---|
466 | else if (timeout.tv_usec > 510000)
|
---|
467 | timeout.tv_usec = 510000;
|
---|
468 |
|
---|
469 | /* Can only fasttimo if we also slowtimo */
|
---|
470 | if (time_fasttimo) {
|
---|
471 | tmp_time = (200 - (curtime - time_fasttimo)) * 1000;
|
---|
472 | if (tmp_time < 0)
|
---|
473 | tmp_time = 0;
|
---|
474 |
|
---|
475 | /* Choose the smallest of the 2 */
|
---|
476 | if (tmp_time < timeout.tv_usec)
|
---|
477 | timeout.tv_usec = (u_int)tmp_time;
|
---|
478 | }
|
---|
479 | }
|
---|
480 | *pnfds = nfds;
|
---|
481 | }
|
---|
482 |
|
---|
483 | void slirp_select_poll(PNATState pData, fd_set *readfds, fd_set *writefds, fd_set *xfds)
|
---|
484 | {
|
---|
485 | struct socket *so, *so_next;
|
---|
486 | int ret;
|
---|
487 |
|
---|
488 | /* Update time */
|
---|
489 | updtime(pData);
|
---|
490 |
|
---|
491 | /*
|
---|
492 | * See if anything has timed out
|
---|
493 | */
|
---|
494 | if (link_up) {
|
---|
495 | if (time_fasttimo && ((curtime - time_fasttimo) >= 2)) {
|
---|
496 | tcp_fasttimo(pData);
|
---|
497 | time_fasttimo = 0;
|
---|
498 | }
|
---|
499 | if (do_slowtimo && ((curtime - last_slowtimo) >= 499)) {
|
---|
500 | ip_slowtimo(pData);
|
---|
501 | tcp_slowtimo(pData);
|
---|
502 | last_slowtimo = curtime;
|
---|
503 | }
|
---|
504 | }
|
---|
505 |
|
---|
506 | /*
|
---|
507 | * Check sockets
|
---|
508 | */
|
---|
509 | if (link_up) {
|
---|
510 | /*
|
---|
511 | * Check TCP sockets
|
---|
512 | */
|
---|
513 | for (so = tcb.so_next; so != &tcb; so = so_next) {
|
---|
514 | so_next = so->so_next;
|
---|
515 |
|
---|
516 | /*
|
---|
517 | * FD_ISSET is meaningless on these sockets
|
---|
518 | * (and they can crash the program)
|
---|
519 | */
|
---|
520 | if (so->so_state & SS_NOFDREF || so->s == -1)
|
---|
521 | continue;
|
---|
522 |
|
---|
523 | /*
|
---|
524 | * Check for URG data
|
---|
525 | * This will soread as well, so no need to
|
---|
526 | * test for readfds below if this succeeds
|
---|
527 | */
|
---|
528 | if (FD_ISSET(so->s, xfds))
|
---|
529 | sorecvoob(pData, so);
|
---|
530 | /*
|
---|
531 | * Check sockets for reading
|
---|
532 | */
|
---|
533 | else if (FD_ISSET(so->s, readfds)) {
|
---|
534 | /*
|
---|
535 | * Check for incoming connections
|
---|
536 | */
|
---|
537 | if (so->so_state & SS_FACCEPTCONN) {
|
---|
538 | tcp_connect(pData, so);
|
---|
539 | continue;
|
---|
540 | } /* else */
|
---|
541 | ret = soread(pData, so);
|
---|
542 |
|
---|
543 | /* Output it if we read something */
|
---|
544 | if (ret > 0)
|
---|
545 | tcp_output(pData, sototcpcb(so));
|
---|
546 | }
|
---|
547 |
|
---|
548 | /*
|
---|
549 | * Check sockets for writing
|
---|
550 | */
|
---|
551 | if (FD_ISSET(so->s, writefds)) {
|
---|
552 | /*
|
---|
553 | * Check for non-blocking, still-connecting sockets
|
---|
554 | */
|
---|
555 | if (so->so_state & SS_ISFCONNECTING) {
|
---|
556 | /* Connected */
|
---|
557 | so->so_state &= ~SS_ISFCONNECTING;
|
---|
558 |
|
---|
559 | ret = send(so->s, (const char *)&ret, 0, 0);
|
---|
560 | if (ret < 0) {
|
---|
561 | /* XXXXX Must fix, zero bytes is a NOP */
|
---|
562 | if (errno == EAGAIN || errno == EWOULDBLOCK ||
|
---|
563 | errno == EINPROGRESS || errno == ENOTCONN)
|
---|
564 | continue;
|
---|
565 |
|
---|
566 | /* else failed */
|
---|
567 | so->so_state = SS_NOFDREF;
|
---|
568 | }
|
---|
569 | /* else so->so_state &= ~SS_ISFCONNECTING; */
|
---|
570 |
|
---|
571 | /*
|
---|
572 | * Continue tcp_input
|
---|
573 | */
|
---|
574 | tcp_input(pData, (struct mbuf *)NULL, sizeof(struct ip), so);
|
---|
575 | /* continue; */
|
---|
576 | } else
|
---|
577 | ret = sowrite(pData, so);
|
---|
578 | /*
|
---|
579 | * XXXXX If we wrote something (a lot), there
|
---|
580 | * could be a need for a window update.
|
---|
581 | * In the worst case, the remote will send
|
---|
582 | * a window probe to get things going again
|
---|
583 | */
|
---|
584 | }
|
---|
585 |
|
---|
586 | /*
|
---|
587 | * Probe a still-connecting, non-blocking socket
|
---|
588 | * to check if it's still alive
|
---|
589 | */
|
---|
590 | #ifdef PROBE_CONN
|
---|
591 | if (so->so_state & SS_ISFCONNECTING) {
|
---|
592 | ret = recv(so->s, (char *)&ret, 0,0);
|
---|
593 |
|
---|
594 | if (ret < 0) {
|
---|
595 | /* XXX */
|
---|
596 | if (errno == EAGAIN || errno == EWOULDBLOCK ||
|
---|
597 | errno == EINPROGRESS || errno == ENOTCONN)
|
---|
598 | continue; /* Still connecting, continue */
|
---|
599 |
|
---|
600 | /* else failed */
|
---|
601 | so->so_state = SS_NOFDREF;
|
---|
602 |
|
---|
603 | /* tcp_input will take care of it */
|
---|
604 | } else {
|
---|
605 | ret = send(so->s, &ret, 0,0);
|
---|
606 | if (ret < 0) {
|
---|
607 | /* XXX */
|
---|
608 | if (errno == EAGAIN || errno == EWOULDBLOCK ||
|
---|
609 | errno == EINPROGRESS || errno == ENOTCONN)
|
---|
610 | continue;
|
---|
611 | /* else failed */
|
---|
612 | so->so_state = SS_NOFDREF;
|
---|
613 | } else
|
---|
614 | so->so_state &= ~SS_ISFCONNECTING;
|
---|
615 |
|
---|
616 | }
|
---|
617 | tcp_input((struct mbuf *)NULL, sizeof(struct ip),so);
|
---|
618 | } /* SS_ISFCONNECTING */
|
---|
619 | #endif
|
---|
620 | }
|
---|
621 |
|
---|
622 | /*
|
---|
623 | * Now UDP sockets.
|
---|
624 | * Incoming packets are sent straight away, they're not buffered.
|
---|
625 | * Incoming UDP data isn't buffered either.
|
---|
626 | */
|
---|
627 | for (so = udb.so_next; so != &udb; so = so_next) {
|
---|
628 | so_next = so->so_next;
|
---|
629 |
|
---|
630 | if (so->s != -1 && FD_ISSET(so->s, readfds)) {
|
---|
631 | sorecvfrom(pData, so);
|
---|
632 | }
|
---|
633 | }
|
---|
634 | }
|
---|
635 |
|
---|
636 | /*
|
---|
637 | * See if we can start outputting
|
---|
638 | */
|
---|
639 | if (if_queued && link_up)
|
---|
640 | if_start(pData);
|
---|
641 | }
|
---|
642 |
|
---|
643 | #define ETH_ALEN 6
|
---|
644 | #define ETH_HLEN 14
|
---|
645 |
|
---|
646 | #define ETH_P_IP 0x0800 /* Internet Protocol packet */
|
---|
647 | #define ETH_P_ARP 0x0806 /* Address Resolution packet */
|
---|
648 |
|
---|
649 | #define ARPOP_REQUEST 1 /* ARP request */
|
---|
650 | #define ARPOP_REPLY 2 /* ARP reply */
|
---|
651 |
|
---|
652 | struct ethhdr
|
---|
653 | {
|
---|
654 | unsigned char h_dest[ETH_ALEN]; /* destination eth addr */
|
---|
655 | unsigned char h_source[ETH_ALEN]; /* source ether addr */
|
---|
656 | unsigned short h_proto; /* packet type ID field */
|
---|
657 | };
|
---|
658 |
|
---|
659 | struct arphdr
|
---|
660 | {
|
---|
661 | unsigned short ar_hrd; /* format of hardware address */
|
---|
662 | unsigned short ar_pro; /* format of protocol address */
|
---|
663 | unsigned char ar_hln; /* length of hardware address */
|
---|
664 | unsigned char ar_pln; /* length of protocol address */
|
---|
665 | unsigned short ar_op; /* ARP opcode (command) */
|
---|
666 |
|
---|
667 | /*
|
---|
668 | * Ethernet looks like this : This bit is variable sized however...
|
---|
669 | */
|
---|
670 | unsigned char ar_sha[ETH_ALEN]; /* sender hardware address */
|
---|
671 | unsigned char ar_sip[4]; /* sender IP address */
|
---|
672 | unsigned char ar_tha[ETH_ALEN]; /* target hardware address */
|
---|
673 | unsigned char ar_tip[4]; /* target IP address */
|
---|
674 | };
|
---|
675 |
|
---|
676 | static
|
---|
677 | void arp_input(PNATState pData, const uint8_t *pkt, int pkt_len)
|
---|
678 | {
|
---|
679 | struct ethhdr *eh = (struct ethhdr *)pkt;
|
---|
680 | struct arphdr *ah = (struct arphdr *)(pkt + ETH_HLEN);
|
---|
681 | uint8_t arp_reply[ETH_HLEN + sizeof(struct arphdr)];
|
---|
682 | struct ethhdr *reh = (struct ethhdr *)arp_reply;
|
---|
683 | struct arphdr *rah = (struct arphdr *)(arp_reply + ETH_HLEN);
|
---|
684 | int ar_op;
|
---|
685 | struct ex_list *ex_ptr;
|
---|
686 |
|
---|
687 | ar_op = ntohs(ah->ar_op);
|
---|
688 | switch(ar_op) {
|
---|
689 | case ARPOP_REQUEST:
|
---|
690 | if (!memcmp(ah->ar_tip, &special_addr, 3)) {
|
---|
691 | if (ah->ar_tip[3] == CTL_DNS || ah->ar_tip[3] == CTL_ALIAS)
|
---|
692 | goto arp_ok;
|
---|
693 | for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
|
---|
694 | if (ex_ptr->ex_addr == ah->ar_tip[3])
|
---|
695 | goto arp_ok;
|
---|
696 | }
|
---|
697 | return;
|
---|
698 | arp_ok:
|
---|
699 | /* XXX: make an ARP request to have the client address */
|
---|
700 | memcpy(client_ethaddr, eh->h_source, ETH_ALEN);
|
---|
701 |
|
---|
702 | /* ARP request for alias/dns mac address */
|
---|
703 | memcpy(reh->h_dest, pkt + ETH_ALEN, ETH_ALEN);
|
---|
704 | memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 1);
|
---|
705 | reh->h_source[5] = ah->ar_tip[3];
|
---|
706 | reh->h_proto = htons(ETH_P_ARP);
|
---|
707 |
|
---|
708 | rah->ar_hrd = htons(1);
|
---|
709 | rah->ar_pro = htons(ETH_P_IP);
|
---|
710 | rah->ar_hln = ETH_ALEN;
|
---|
711 | rah->ar_pln = 4;
|
---|
712 | rah->ar_op = htons(ARPOP_REPLY);
|
---|
713 | memcpy(rah->ar_sha, reh->h_source, ETH_ALEN);
|
---|
714 | memcpy(rah->ar_sip, ah->ar_tip, 4);
|
---|
715 | memcpy(rah->ar_tha, ah->ar_sha, ETH_ALEN);
|
---|
716 | memcpy(rah->ar_tip, ah->ar_sip, 4);
|
---|
717 | slirp_output(pData->pvUser, arp_reply, sizeof(arp_reply));
|
---|
718 | }
|
---|
719 | break;
|
---|
720 | default:
|
---|
721 | break;
|
---|
722 | }
|
---|
723 | }
|
---|
724 |
|
---|
725 | void slirp_input(PNATState pData, const uint8_t *pkt, int pkt_len)
|
---|
726 | {
|
---|
727 | struct mbuf *m;
|
---|
728 | int proto;
|
---|
729 |
|
---|
730 | if (pkt_len < ETH_HLEN)
|
---|
731 | return;
|
---|
732 |
|
---|
733 | proto = ntohs(*(uint16_t *)(pkt + 12));
|
---|
734 | switch(proto) {
|
---|
735 | case ETH_P_ARP:
|
---|
736 | arp_input(pData, pkt, pkt_len);
|
---|
737 | break;
|
---|
738 | case ETH_P_IP:
|
---|
739 | /* Update time. Important if the network is very quiet, as otherwise
|
---|
740 | * the first outgoing connection gets an incorrect timestamp. */
|
---|
741 | updtime(pData);
|
---|
742 |
|
---|
743 | m = m_get(pData);
|
---|
744 | if (!m)
|
---|
745 | return;
|
---|
746 | /* Note: we add to align the IP header */
|
---|
747 | if (M_FREEROOM(m) < pkt_len + 2) {
|
---|
748 | m_inc(m, pkt_len + 2);
|
---|
749 | }
|
---|
750 | m->m_len = pkt_len + 2;
|
---|
751 | memcpy(m->m_data + 2, pkt, pkt_len);
|
---|
752 |
|
---|
753 | m->m_data += 2 + ETH_HLEN;
|
---|
754 | m->m_len -= 2 + ETH_HLEN;
|
---|
755 |
|
---|
756 | ip_input(pData, m);
|
---|
757 | break;
|
---|
758 | default:
|
---|
759 | break;
|
---|
760 | }
|
---|
761 | }
|
---|
762 |
|
---|
763 | /* output the IP packet to the ethernet device */
|
---|
764 | void if_encap(PNATState pData, const uint8_t *ip_data, int ip_data_len)
|
---|
765 | {
|
---|
766 | uint8_t buf[1600];
|
---|
767 | struct ethhdr *eh = (struct ethhdr *)buf;
|
---|
768 |
|
---|
769 | if (ip_data_len + ETH_HLEN > sizeof(buf))
|
---|
770 | return;
|
---|
771 |
|
---|
772 | memcpy(eh->h_dest, client_ethaddr, ETH_ALEN);
|
---|
773 | memcpy(eh->h_source, special_ethaddr, ETH_ALEN - 1);
|
---|
774 | /* XXX: not correct */
|
---|
775 | eh->h_source[5] = CTL_ALIAS;
|
---|
776 | eh->h_proto = htons(ETH_P_IP);
|
---|
777 | memcpy(buf + sizeof(struct ethhdr), ip_data, ip_data_len);
|
---|
778 | slirp_output(pData->pvUser, buf, ip_data_len + ETH_HLEN);
|
---|
779 | }
|
---|
780 |
|
---|
781 | int slirp_redir(PNATState pData, int is_udp, int host_port,
|
---|
782 | struct in_addr guest_addr, int guest_port)
|
---|
783 | {
|
---|
784 | if (is_udp) {
|
---|
785 | if (!udp_listen(pData, htons(host_port), guest_addr.s_addr,
|
---|
786 | htons(guest_port), 0))
|
---|
787 | return -1;
|
---|
788 | } else {
|
---|
789 | if (!solisten(pData, htons(host_port), guest_addr.s_addr,
|
---|
790 | htons(guest_port), 0))
|
---|
791 | return -1;
|
---|
792 | }
|
---|
793 | return 0;
|
---|
794 | }
|
---|
795 |
|
---|
796 | int slirp_add_exec(PNATState pData, int do_pty, const char *args, int addr_low_byte,
|
---|
797 | int guest_port)
|
---|
798 | {
|
---|
799 | return add_exec(&exec_list, do_pty, (char *)args,
|
---|
800 | addr_low_byte, htons(guest_port));
|
---|
801 | }
|
---|