VirtualBox

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

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

introduced new event for handling link state changings (need some testing)

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