VirtualBox

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

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

correct definition and declaration of slirp_register_event

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

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