VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/slirp/slirp.c@ 11284

最後變更 在這個檔案從11284是 9150,由 vboxsync 提交於 17 年 前

fixed windows builds

  • 屬性 svn:eol-style 設為 native
檔案大小: 21.6 KB
 
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
9static const uint8_t special_ethaddr[6] = {
10 0x52, 0x54, 0x00, 0x12, 0x35, 0x00
11};
12
13#ifdef _WIN32
14
15static 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
69get_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
105static 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
192int get_dns_addr(PNATState pData, struct in_addr *pdns_addr)
193{
194 return get_dns_addr_domain(pData, false, pdns_addr, NULL);
195}
196
197int slirp_init(PNATState *ppData, const char *pszNetAddr, uint32_t u32Netmask,
198 bool fPassDomain, const char *pszTFTPPrefix,
199 const char *pszBootFile, 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 if (u32Netmask & 0x1f)
207 /* CTL is x.x.x.15, bootp passes up to 16 IPs (15..31) */
208 return VERR_INVALID_PARAMETER;
209 memset(pData, '\0', sizeof(NATState));
210 pData->fPassDomain = fPassDomain;
211 pData->pvUser = pvUser;
212#if ARCH_BITS == 64
213 pData->cpvHashUsed = 1;
214#endif
215 tftp_prefix = pszTFTPPrefix;
216 bootp_filename = pszBootFile;
217 pData->netmask = u32Netmask;
218
219#ifdef _WIN32
220 {
221 WSADATA Data;
222 WSAStartup(MAKEWORD(2,0), &Data);
223 }
224#endif
225
226 Assert(sizeof(struct ip) == 20);
227 link_up = 1;
228
229 if_init(pData);
230 ip_init(pData);
231
232 /* Initialise mbufs *after* setting the MTU */
233 m_init(pData);
234
235 /* set default addresses */
236 inet_aton("127.0.0.1", &loopback_addr);
237 inet_aton("127.0.0.1", &dns_addr);
238
239 if (get_dns_addr_domain(pData, true, &dns_addr, &pData->pszDomain) < 0)
240 fNATfailed = 1;
241
242 inet_aton(pszNetAddr, &special_addr);
243 alias_addr.s_addr = special_addr.s_addr | htonl(CTL_ALIAS);
244 getouraddr(pData);
245 return fNATfailed ? VINF_NAT_DNS : VINF_SUCCESS;
246}
247
248/**
249 * Marks the link as up, making it possible to establish new connections.
250 */
251void slirp_link_up(PNATState pData)
252{
253 link_up = 1;
254}
255
256/**
257 * Marks the link as down and cleans up the current connections.
258 */
259void slirp_link_down(PNATState pData)
260{
261 struct socket *so;
262
263 while ((so = tcb.so_next) != &tcb)
264 {
265 if (so->so_state & SS_NOFDREF || so->s == -1)
266 sofree(pData, so);
267 else
268 tcp_drop(pData, sototcpcb(so), 0);
269 }
270
271 while ((so = udb.so_next) != &udb)
272 udp_detach(pData, so);
273
274 link_up = 0;
275}
276
277/**
278 * Terminates the slirp component.
279 */
280void slirp_term(PNATState pData)
281{
282 if (pData->pszDomain)
283 RTStrFree((char *)(void *)pData->pszDomain);
284
285#if ARCH_BITS == 64
286 LogRel(("NAT: cpvHashUsed=%RU32 cpvHashCollisions=%RU32 cpvHashInserts=%RU64 cpvHashDone=%RU64\n",
287 pData->cpvHashUsed, pData->cpvHashCollisions, pData->cpvHashInserts, pData->cpvHashDone));
288#endif
289
290 slirp_link_down(pData);
291#ifdef WIN32
292 WSACleanup();
293#endif
294#ifdef LOG_ENABLED
295 Log(("\n"
296 "NAT statistics\n"
297 "--------------\n"
298 "\n"));
299 ipstats(pData);
300 tcpstats(pData);
301 udpstats(pData);
302 icmpstats(pData);
303 mbufstats(pData);
304 sockstats(pData);
305 Log(("\n"
306 "\n"
307 "\n"));
308#endif
309 free(pData);
310}
311
312
313#define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
314#define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
315#define UPD_NFDS(x) if (nfds < (x)) nfds = (x)
316
317/*
318 * curtime kept to an accuracy of 1ms
319 */
320#ifdef _WIN32
321static void updtime(PNATState pData)
322{
323 struct _timeb tb;
324
325 _ftime(&tb);
326 curtime = (u_int)tb.time * (u_int)1000;
327 curtime += (u_int)tb.millitm;
328}
329#else
330static void updtime(PNATState pData)
331{
332 gettimeofday(&tt, 0);
333
334 curtime = (u_int)tt.tv_sec * (u_int)1000;
335 curtime += (u_int)tt.tv_usec / (u_int)1000;
336
337 if ((tt.tv_usec % 1000) >= 500)
338 curtime++;
339}
340#endif
341
342void slirp_select_fill(PNATState pData, int *pnfds,
343 fd_set *readfds, fd_set *writefds, fd_set *xfds)
344{
345 struct socket *so, *so_next;
346 struct timeval timeout;
347 int nfds;
348 int tmp_time;
349
350 nfds = *pnfds;
351 /*
352 * First, TCP sockets
353 */
354 do_slowtimo = 0;
355 if (link_up) {
356 /*
357 * *_slowtimo needs calling if there are IP fragments
358 * in the fragment queue, or there are TCP connections active
359 */
360 do_slowtimo = ((tcb.so_next != &tcb) ||
361 ((struct ipasfrag *)&ipq != u32_to_ptr(pData, ipq.next, struct ipasfrag *)));
362
363 for (so = tcb.so_next; so != &tcb; so = so_next) {
364 so_next = so->so_next;
365
366 /*
367 * See if we need a tcp_fasttimo
368 */
369 if (time_fasttimo == 0 && so->so_tcpcb->t_flags & TF_DELACK)
370 time_fasttimo = curtime; /* Flag when we want a fasttimo */
371
372 /*
373 * NOFDREF can include still connecting to local-host,
374 * newly socreated() sockets etc. Don't want to select these.
375 */
376 if (so->so_state & SS_NOFDREF || so->s == -1)
377 continue;
378
379 /*
380 * Set for reading sockets which are accepting
381 */
382 if (so->so_state & SS_FACCEPTCONN) {
383 FD_SET(so->s, readfds);
384 UPD_NFDS(so->s);
385 continue;
386 }
387
388 /*
389 * Set for writing sockets which are connecting
390 */
391 if (so->so_state & SS_ISFCONNECTING) {
392 FD_SET(so->s, writefds);
393 UPD_NFDS(so->s);
394 continue;
395 }
396
397 /*
398 * Set for writing if we are connected, can send more, and
399 * we have something to send
400 */
401 if (CONN_CANFSEND(so) && so->so_rcv.sb_cc) {
402 FD_SET(so->s, writefds);
403 UPD_NFDS(so->s);
404 }
405
406 /*
407 * Set for reading (and urgent data) if we are connected, can
408 * receive more, and we have room for it XXX /2 ?
409 */
410 if (CONN_CANFRCV(so) && (so->so_snd.sb_cc < (so->so_snd.sb_datalen/2))) {
411 FD_SET(so->s, readfds);
412 FD_SET(so->s, xfds);
413 UPD_NFDS(so->s);
414 }
415 }
416
417 /*
418 * UDP sockets
419 */
420 for (so = udb.so_next; so != &udb; so = so_next) {
421 so_next = so->so_next;
422
423 /*
424 * See if it's timed out
425 */
426 if (so->so_expire) {
427 if (so->so_expire <= curtime) {
428 udp_detach(pData, so);
429 continue;
430 } else
431 do_slowtimo = 1; /* Let socket expire */
432 }
433
434 /*
435 * When UDP packets are received from over the
436 * link, they're sendto()'d straight away, so
437 * no need for setting for writing
438 * Limit the number of packets queued by this session
439 * to 4. Note that even though we try and limit this
440 * to 4 packets, the session could have more queued
441 * if the packets needed to be fragmented
442 * (XXX <= 4 ?)
443 */
444 if ((so->so_state & SS_ISFCONNECTED) && so->so_queued <= 4) {
445 FD_SET(so->s, readfds);
446 UPD_NFDS(so->s);
447 }
448 }
449 }
450
451 /*
452 * Setup timeout to use minimum CPU usage, especially when idle
453 */
454
455 /*
456 * First, see the timeout needed by *timo
457 */
458 timeout.tv_sec = 0;
459 timeout.tv_usec = -1;
460 /*
461 * If a slowtimo is needed, set timeout to 500ms from the last
462 * slow timeout. If a fast timeout is needed, set timeout within
463 * 200ms of when it was requested.
464 */
465 if (do_slowtimo) {
466 /* XXX + 10000 because some select()'s aren't that accurate */
467 timeout.tv_usec = ((500 - (curtime - last_slowtimo)) * 1000) + 10000;
468 if (timeout.tv_usec < 0)
469 timeout.tv_usec = 0;
470 else if (timeout.tv_usec > 510000)
471 timeout.tv_usec = 510000;
472
473 /* Can only fasttimo if we also slowtimo */
474 if (time_fasttimo) {
475 tmp_time = (200 - (curtime - time_fasttimo)) * 1000;
476 if (tmp_time < 0)
477 tmp_time = 0;
478
479 /* Choose the smallest of the 2 */
480 if (tmp_time < timeout.tv_usec)
481 timeout.tv_usec = (u_int)tmp_time;
482 }
483 }
484 *pnfds = nfds;
485}
486
487void slirp_select_poll(PNATState pData, fd_set *readfds, fd_set *writefds, fd_set *xfds)
488{
489 struct socket *so, *so_next;
490 int ret;
491
492 /* Update time */
493 updtime(pData);
494
495 /*
496 * See if anything has timed out
497 */
498 if (link_up) {
499 if (time_fasttimo && ((curtime - time_fasttimo) >= 2)) {
500 tcp_fasttimo(pData);
501 time_fasttimo = 0;
502 }
503 if (do_slowtimo && ((curtime - last_slowtimo) >= 499)) {
504 ip_slowtimo(pData);
505 tcp_slowtimo(pData);
506 last_slowtimo = curtime;
507 }
508 }
509
510 /*
511 * Check sockets
512 */
513 if (link_up) {
514 /*
515 * Check TCP sockets
516 */
517 for (so = tcb.so_next; so != &tcb; so = so_next) {
518 so_next = so->so_next;
519
520 /*
521 * FD_ISSET is meaningless on these sockets
522 * (and they can crash the program)
523 */
524 if (so->so_state & SS_NOFDREF || so->s == -1)
525 continue;
526
527 /*
528 * Check for URG data
529 * This will soread as well, so no need to
530 * test for readfds below if this succeeds
531 */
532 if (FD_ISSET(so->s, xfds))
533 sorecvoob(pData, so);
534 /*
535 * Check sockets for reading
536 */
537 else if (FD_ISSET(so->s, readfds)) {
538 /*
539 * Check for incoming connections
540 */
541 if (so->so_state & SS_FACCEPTCONN) {
542 tcp_connect(pData, so);
543 continue;
544 } /* else */
545 ret = soread(pData, so);
546
547 /* Output it if we read something */
548 if (ret > 0)
549 tcp_output(pData, sototcpcb(so));
550 }
551
552 /*
553 * Check sockets for writing
554 */
555 if (FD_ISSET(so->s, writefds)) {
556 /*
557 * Check for non-blocking, still-connecting sockets
558 */
559 if (so->so_state & SS_ISFCONNECTING) {
560 /* Connected */
561 so->so_state &= ~SS_ISFCONNECTING;
562
563 /*
564 * This should be probably guarded by PROBE_CONN too. Anyway,
565 * we disable it on OS/2 because the below send call returns
566 * EFAULT which causes the opened TCP socket to close right
567 * after it has been opened and connected.
568 */
569#ifndef RT_OS_OS2
570 ret = send(so->s, (const char *)&ret, 0, 0);
571 if (ret < 0) {
572 /* XXXXX Must fix, zero bytes is a NOP */
573 if (errno == EAGAIN || errno == EWOULDBLOCK ||
574 errno == EINPROGRESS || errno == ENOTCONN)
575 continue;
576
577 /* else failed */
578 so->so_state = SS_NOFDREF;
579 }
580 /* else so->so_state &= ~SS_ISFCONNECTING; */
581#endif
582
583 /*
584 * Continue tcp_input
585 */
586 tcp_input(pData, (struct mbuf *)NULL, sizeof(struct ip), so);
587 /* continue; */
588 } else
589 ret = sowrite(pData, so);
590 /*
591 * XXXXX If we wrote something (a lot), there
592 * could be a need for a window update.
593 * In the worst case, the remote will send
594 * a window probe to get things going again
595 */
596 }
597
598 /*
599 * Probe a still-connecting, non-blocking socket
600 * to check if it's still alive
601 */
602#ifdef PROBE_CONN
603 if (so->so_state & SS_ISFCONNECTING) {
604 ret = recv(so->s, (char *)&ret, 0,0);
605
606 if (ret < 0) {
607 /* XXX */
608 if (errno == EAGAIN || errno == EWOULDBLOCK ||
609 errno == EINPROGRESS || errno == ENOTCONN)
610 continue; /* Still connecting, continue */
611
612 /* else failed */
613 so->so_state = SS_NOFDREF;
614
615 /* tcp_input will take care of it */
616 } else {
617 ret = send(so->s, &ret, 0,0);
618 if (ret < 0) {
619 /* XXX */
620 if (errno == EAGAIN || errno == EWOULDBLOCK ||
621 errno == EINPROGRESS || errno == ENOTCONN)
622 continue;
623 /* else failed */
624 so->so_state = SS_NOFDREF;
625 } else
626 so->so_state &= ~SS_ISFCONNECTING;
627
628 }
629 tcp_input((struct mbuf *)NULL, sizeof(struct ip),so);
630 } /* SS_ISFCONNECTING */
631#endif
632 }
633
634 /*
635 * Now UDP sockets.
636 * Incoming packets are sent straight away, they're not buffered.
637 * Incoming UDP data isn't buffered either.
638 */
639 for (so = udb.so_next; so != &udb; so = so_next) {
640 so_next = so->so_next;
641
642 if (so->s != -1 && FD_ISSET(so->s, readfds)) {
643 sorecvfrom(pData, so);
644 }
645 }
646 }
647
648 /*
649 * See if we can start outputting
650 */
651 if (if_queued && link_up)
652 if_start(pData);
653}
654
655#define ETH_ALEN 6
656#define ETH_HLEN 14
657
658#define ETH_P_IP 0x0800 /* Internet Protocol packet */
659#define ETH_P_ARP 0x0806 /* Address Resolution packet */
660
661#define ARPOP_REQUEST 1 /* ARP request */
662#define ARPOP_REPLY 2 /* ARP reply */
663
664struct ethhdr
665{
666 unsigned char h_dest[ETH_ALEN]; /* destination eth addr */
667 unsigned char h_source[ETH_ALEN]; /* source ether addr */
668 unsigned short h_proto; /* packet type ID field */
669};
670
671struct arphdr
672{
673 unsigned short ar_hrd; /* format of hardware address */
674 unsigned short ar_pro; /* format of protocol address */
675 unsigned char ar_hln; /* length of hardware address */
676 unsigned char ar_pln; /* length of protocol address */
677 unsigned short ar_op; /* ARP opcode (command) */
678
679 /*
680 * Ethernet looks like this : This bit is variable sized however...
681 */
682 unsigned char ar_sha[ETH_ALEN]; /* sender hardware address */
683 unsigned char ar_sip[4]; /* sender IP address */
684 unsigned char ar_tha[ETH_ALEN]; /* target hardware address */
685 unsigned char ar_tip[4]; /* target IP address */
686};
687
688static
689void arp_input(PNATState pData, const uint8_t *pkt, int pkt_len)
690{
691 struct ethhdr *eh = (struct ethhdr *)pkt;
692 struct arphdr *ah = (struct arphdr *)(pkt + ETH_HLEN);
693 uint8_t arp_reply[ETH_HLEN + sizeof(struct arphdr)];
694 struct ethhdr *reh = (struct ethhdr *)arp_reply;
695 struct arphdr *rah = (struct arphdr *)(arp_reply + ETH_HLEN);
696 int ar_op;
697 struct ex_list *ex_ptr;
698 uint32_t htip = ntohl(*(uint32_t*)ah->ar_tip);
699
700 ar_op = ntohs(ah->ar_op);
701 switch(ar_op) {
702 case ARPOP_REQUEST:
703 if ((htip & pData->netmask) == ntohl(special_addr.s_addr)) {
704 if ( (htip & ~pData->netmask) == CTL_DNS
705 || (htip & ~pData->netmask) == CTL_ALIAS)
706 goto arp_ok;
707 for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
708 if ((htip & ~pData->netmask) == ex_ptr->ex_addr)
709 goto arp_ok;
710 }
711 return;
712 arp_ok:
713 /* XXX: make an ARP request to have the client address */
714 memcpy(client_ethaddr, eh->h_source, ETH_ALEN);
715
716 /* ARP request for alias/dns mac address */
717 memcpy(reh->h_dest, pkt + ETH_ALEN, ETH_ALEN);
718 memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 1);
719 reh->h_source[5] = ah->ar_tip[3];
720 reh->h_proto = htons(ETH_P_ARP);
721
722 rah->ar_hrd = htons(1);
723 rah->ar_pro = htons(ETH_P_IP);
724 rah->ar_hln = ETH_ALEN;
725 rah->ar_pln = 4;
726 rah->ar_op = htons(ARPOP_REPLY);
727 memcpy(rah->ar_sha, reh->h_source, ETH_ALEN);
728 memcpy(rah->ar_sip, ah->ar_tip, 4);
729 memcpy(rah->ar_tha, ah->ar_sha, ETH_ALEN);
730 memcpy(rah->ar_tip, ah->ar_sip, 4);
731 slirp_output(pData->pvUser, arp_reply, sizeof(arp_reply));
732 }
733 break;
734 default:
735 break;
736 }
737}
738
739void slirp_input(PNATState pData, const uint8_t *pkt, int pkt_len)
740{
741 struct mbuf *m;
742 int proto;
743
744 if (pkt_len < ETH_HLEN)
745 return;
746
747 proto = ntohs(*(uint16_t *)(pkt + 12));
748 switch(proto) {
749 case ETH_P_ARP:
750 arp_input(pData, pkt, pkt_len);
751 break;
752 case ETH_P_IP:
753 /* Update time. Important if the network is very quiet, as otherwise
754 * the first outgoing connection gets an incorrect timestamp. */
755 updtime(pData);
756
757 m = m_get(pData);
758 if (!m)
759 return;
760 /* Note: we add to align the IP header */
761 if (M_FREEROOM(m) < pkt_len + 2) {
762 m_inc(m, pkt_len + 2);
763 }
764 m->m_len = pkt_len + 2;
765 memcpy(m->m_data + 2, pkt, pkt_len);
766
767 m->m_data += 2 + ETH_HLEN;
768 m->m_len -= 2 + ETH_HLEN;
769
770 ip_input(pData, m);
771 break;
772 default:
773 break;
774 }
775}
776
777/* output the IP packet to the ethernet device */
778void if_encap(PNATState pData, const uint8_t *ip_data, int ip_data_len)
779{
780 uint8_t buf[1600];
781 struct ethhdr *eh = (struct ethhdr *)buf;
782
783 if (ip_data_len + ETH_HLEN > sizeof(buf))
784 return;
785
786 memcpy(eh->h_dest, client_ethaddr, ETH_ALEN);
787 memcpy(eh->h_source, special_ethaddr, ETH_ALEN - 1);
788 /* XXX: not correct */
789 eh->h_source[5] = CTL_ALIAS;
790 eh->h_proto = htons(ETH_P_IP);
791 memcpy(buf + sizeof(struct ethhdr), ip_data, ip_data_len);
792 slirp_output(pData->pvUser, buf, ip_data_len + ETH_HLEN);
793}
794
795int slirp_redir(PNATState pData, int is_udp, int host_port,
796 struct in_addr guest_addr, int guest_port)
797{
798 if (is_udp) {
799 if (!udp_listen(pData, htons(host_port), guest_addr.s_addr,
800 htons(guest_port), 0))
801 return -1;
802 } else {
803 if (!solisten(pData, htons(host_port), guest_addr.s_addr,
804 htons(guest_port), 0))
805 return -1;
806 }
807 return 0;
808}
809
810int slirp_add_exec(PNATState pData, int do_pty, const char *args, int addr_low_byte,
811 int guest_port)
812{
813 return add_exec(&exec_list, do_pty, (char *)args,
814 addr_low_byte, htons(guest_port));
815}
816
817void slirp_set_ethaddr(PNATState pData, const uint8_t *ethaddr)
818{
819 memcpy(client_ethaddr, ethaddr, ETH_ALEN);
820}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette