VirtualBox

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

Last change on this file since 14161 was 14161, checked in by vboxsync, 16 years ago

removing all references to cEvent from polling routine

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

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