slirp.c revision 0f252ad257bdf002284cb2a1359814cd9fa3f2a9
#include "slirp.h"
#ifdef RT_OS_OS2
# include <paths.h>
#endif
#if !defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) || !defined(RT_OS_WINDOWS)
do { \
} while(0)
do { \
} while(0)
# ifndef RT_OS_WINDOWS
do { \
} while (0)
# else /* !RT_OS_WINDOWS */
#endif /* RT_OS_WINDOWS */
#else /* defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS) */
/*
* On Windows, we will be notified by IcmpSendEcho2() when the response arrives.
* So no call to WSAEventSelect necessary.
*/
do { \
if (rc == SOCKET_ERROR) \
{ \
/* This should not happen */ \
error = WSAGetLastError(); \
} \
} while(0); \
if ((rc) == SOCKET_ERROR) \
{ \
(error) = WSAGetLastError(); \
}
# define acceptds_win FD_ACCEPT
# define acceptds_win_bit FD_ACCEPT_BIT
# define readfds_win FD_READ
# define readfds_win_bit FD_READ_BIT
# define writefds_win FD_WRITE
# define writefds_win_bit FD_WRITE_BIT
# define xfds_win_bit FD_OOB_BIT
#endif /* defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS) */
/*
* Loging macros
*/
# if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
do { \
} while (0)
# else
do { \
} while (0)
# endif /* VBOX_WITH_DEBUG_NAT_SOCKETS */
#else
#endif /* !VBOX_WITH_DEBUG_NAT_SOCKETS */
#define LOG_NAT_SOCK(so, proto, winevent, r_fdset, w_fdset, x_fdset) DO_LOG_NAT_SOCK((so), proto, (winevent), (r_fdset), (w_fdset), (x_fdset))
{
0x52, 0x54, 0x00, 0x12, 0x35, 0x00
};
#ifdef RT_OS_WINDOWS
const char **ppszDomain)
{
int rc = 0;
BufLen = sizeof(FIXED_INFO);
/** @todo: this API returns all DNS servers, no matter whether the
* corresponding network adapter is disabled or not. Maybe replace
{
if (FixedInfo)
{
}
}
{
if (FixedInfo)
{
}
rc = -1;
goto get_dns_prefix;
}
#ifndef VBOX_WITH_MULTI_DNS
Log(("nat: DNS Servers:\n"));
while (pIPAddr)
{
if (fVerbose)
}
#else
/*localhost mask */
{
continue;
{
LogRel(("can't alloc memory for DNS entry\n"));
return -1;
}
/*check */
}
}
#endif
if (FixedInfo)
{
}
if (ppszDomain)
{
char szDnsDomain[256];
*ppszDomain = NULL;
GetVersionEx(&ver);
{
/* GetComputerNameEx exists in Windows versions starting with 2000. */
{
if (szDnsDomain[0])
{
/* Just non-empty strings are valid. */
if (pData->fPassDomain)
{
if (fVerbose)
}
else
}
}
else
}
}
return rc;
}
#else
const char **ppszDomain)
{
char buff[512];
char buff2[256];
FILE *f;
int found = 0;
#ifdef RT_OS_OS2
/* Try various locations. */
f = NULL;
if (etc)
{
}
if (!f)
{
}
if (!f)
{
}
#else
#endif
if (!f)
return -1;
if (ppszDomain)
*ppszDomain = NULL;
Log(("nat: DNS Servers:\n"));
{
#ifdef VBOX_WITH_MULTI_DNS
#endif
{
continue;
#ifndef VBOX_WITH_MULTI_DNS
/* If it's the first one, set it to dns_addr */
if (!found)
{
}
else
{
if (fVerbose)
}
#else
/*localhost mask */
{
LogRel(("can't alloc memory for DNS entry\n"));
return -1;
}
/*check */
}
#endif
found++;
}
if ( ppszDomain
{
if (*ppszDomain == NULL)
{
char *tok;
char *saveptr;
if (tok)
{
if (pData->fPassDomain)
{
if (fVerbose)
}
else
}
}
}
}
fclose(f);
if (!found)
return -1;
return 0;
}
#endif
{
}
bool fPassDomain, const char *pszTFTPPrefix,
const char *pszBootFile, void *pvUser)
{
int fNATfailed = 0;
int rc;
if (!pData)
return VERR_NO_MEMORY;
if (u32Netmask & 0x1f)
/* CTL is x.x.x.15, bootp passes up to 16 IPs (15..31) */
return VERR_INVALID_PARAMETER;
#ifdef RT_OS_WINDOWS
{
}
# if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC)
# endif
#endif
#ifdef VBOX_WITH_SLIRP_MT
#endif
link_up = 1;
debug_init();
/* Initialise mbufs *after* setting the MTU */
/* @todo: add ability to configure this staff */
/* set default addresses */
#ifndef VBOX_WITH_MULTI_DNS
#else
#endif
fNATfailed = 1;
}
/**
* Statistics counters.
*/
{
#ifdef VBOX_WITH_STATISTICS
STAMUNIT_TICKS_PER_CALL, "Profiling slirp fast timer", "/Drivers/NAT%d/TimerFast", pDrvIns->iInstance);
STAMUNIT_TICKS_PER_CALL, "Profiling slirp slow timer", "/Drivers/NAT%d/TimerSlow", pDrvIns->iInstance);
#endif /* VBOX_WITH_STATISTICS */
}
/**
* Marks the link as up, making it possible to establish new connections.
*/
{
link_up = 1;
}
/**
* Marks the link as down and cleans up the current connections.
*/
{
{
else
}
link_up = 0;
}
/**
* Terminates the slirp component.
*/
{
#ifdef VBOX_WITH_MULTI_DNS
#endif
#ifdef RT_OS_WINDOWS
# else
#endif
#ifdef VBOX_WITH_MULTI_DNS
}
#endif
#ifdef RT_OS_WINDOWS
WSACleanup();
#endif
#ifdef LOG_ENABLED
Log(("\n"
"NAT statistics\n"
"--------------\n"
"\n"));
Log(("\n"
"\n"
"\n"));
#endif
}
#define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
/*
* curtime kept to an accuracy of 1ms
*/
#ifdef RT_OS_WINDOWS
{
}
#else
{
gettimeofday(&tt, 0);
curtime++;
}
#endif
{
int nfds;
#if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
int rc;
int error;
#endif
int i;
/*
* First, TCP sockets
*/
do_slowtimo = 0;
if (link_up)
{
/*
* *_slowtimo needs calling if there are IP fragments
* in the fragment queue, or there are TCP connections active
*/
/* XXX:
* triggering of fragment expiration should be the same but use new macroses
*/
if (!do_slowtimo)
{
for (i = 0; i < IPREASS_NHASH; i++)
{
if (!TAILQ_EMPTY(&ipq[i]))
{
do_slowtimo = 1;
break;
}
}
}
/* { */
/*
* See if we need a tcp_fasttimo
*/
/*
* NOFDREF can include still connecting to local-host,
* newly socreated() sockets etc. Don't want to select these.
*/
/*
* Set for reading sockets which are accepting
*/
{
}
/*
* Set for writing sockets which are connecting
*/
{
}
/*
* Set for writing if we are connected, can send more, and
* we have something to send
*/
{
}
/*
* Set for reading (and urgent data) if we are connected, can
* receive more, and we have room for it XXX /2 ?
*/
{
}
}
/*
* UDP sockets
*/
/* { */
/*
* See if it's timed out
*/
{
{
#ifdef VBOX_WITH_SLIRP_MT
/*we can determinate the next item after udb_detach*/
{
}
#endif
}
else
}
/*
* When UDP packets are received from over the link, they're
* sendto()'d straight away, so no need for setting for writing
* Limit the number of packets queued by this session to 4.
* Note that even though we try and limit this to 4 packets,
* the session could have more queued if the packets needed
* to be fragmented.
*
* (XXX <= 4 ?)
*/
{
}
}
}
#if !defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) || !defined(RT_OS_WINDOWS)
#else
#endif
}
#if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
#else
#endif
{
int ret;
#if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
int rc;
int error;
#endif
/* Update time */
/*
* See if anything has timed out
*/
if (link_up)
{
{
time_fasttimo = 0;
}
{
}
}
#if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
if (fTimeout)
return; /* only timer update */
#endif
/*
* Check sockets
*/
if (link_up)
{
#if defined(RT_OS_WINDOWS)
/*XXX: before renaming please make see define
* fIcmp in slirp_state.h
*/
if (fIcmp)
#else
#endif
/*
* Check TCP sockets
*/
/* { */
/*
* FD_ISSET is meaningless on these sockets
* (and they can crash the program)
*/
/*
* Check for URG data
* This will soread as well, so no need to
* test for readfds below if this succeeds
*/
/* out-of-band data */
{
}
/*
* Check sockets for reading
*/
{
/*
* Check for incoming connections
*/
{
#if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
#endif
}
/* Output it if we read something */
if (ret > 0)
}
#if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
/*
* Check for FD_CLOSE events.
*/
{
/*
* drain the socket
*/
for (;;)
{
if (ret > 0)
else
break;
}
}
#endif
/*
* Check sockets for writing
*/
{
/*
* Check for non-blocking, still-connecting sockets
*/
{
/* Connected */
/*
* This should be probably guarded by PROBE_CONN too. Anyway,
* we disable it on OS/2 because the below send call returns
* EFAULT which causes the opened TCP socket to close right
* after it has been opened and connected.
*/
#ifndef RT_OS_OS2
if (ret < 0)
{
/* XXXXX Must fix, zero bytes is a NOP */
|| errno == EWOULDBLOCK
|| errno == EINPROGRESS
/* else failed */
}
/* else so->so_state &= ~SS_ISFCONNECTING; */
#endif
/*
* Continue tcp_input
*/
/* continue; */
}
else
/*
* XXX If we wrote something (a lot), there could be the need
* for a window update. In the worst case, the remote will send
* a window probe to get things going again.
*/
}
/*
* Probe a still-connecting, non-blocking socket
* to check if it's still alive
*/
#ifdef PROBE_CONN
{
if (ret < 0)
{
/* XXX */
|| errno == EWOULDBLOCK
|| errno == EINPROGRESS
{
}
/* else failed */
/* tcp_input will take care of it */
}
else
{
if (ret < 0)
{
/* XXX */
|| errno == EWOULDBLOCK
|| errno == EINPROGRESS
{
}
/* else failed */
}
else
}
} /* SS_ISFCONNECTING */
#endif
}
/*
* Now UDP sockets.
* Incoming packets are sent straight away, they're not buffered.
* Incoming UDP data isn't buffered either.
*/
/* { */
{
}
}
}
#ifndef VBOX_WITH_SLIRP_MT
/*
* See if we can start outputting
*/
#endif
}
#define ETH_ALEN 6
#define ETH_HLEN 14
struct ethhdr
{
unsigned short h_proto; /* packet type ID field */
};
struct arphdr
{
unsigned short ar_hrd; /* format of hardware address */
unsigned short ar_pro; /* format of protocol address */
unsigned char ar_hln; /* length of hardware address */
unsigned char ar_pln; /* length of protocol address */
unsigned short ar_op; /* ARP opcode (command) */
/*
* Ethernet looks like this : This bit is variable sized however...
*/
};
static
#else
#endif
{
int ar_op;
#ifndef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
#else
#endif
#else
#endif
switch(ar_op)
{
case ARPOP_REQUEST:
{
goto arp_ok;
{
goto arp_ok;
}
return;
#ifndef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
#endif
{
case CTL_DNS:
case CTL_ALIAS:
break;
default:;
}
#else
#endif
}
break;
default:
break;
}
}
{
struct mbuf *m;
int proto;
static bool fWarnedIpv6;
{
return;
}
if (!m)
{
LogRel(("can't allocate new mbuf\n"));
return;
}
/* Note: we add to align the IP header */
if (M_FREEROOM(m) < pkt_len)
switch(proto)
{
case ETH_P_ARP:
#else
#endif
break;
case ETH_P_IP:
/* Update time. Important if the network is very quiet, as otherwise
* the first outgoing connection gets an incorrect timestamp. */
break;
case ETH_P_IPV6:
if (!fWarnedIpv6)
{
LogRel(("NAT: IPv6 not supported\n"));
fWarnedIpv6 = true;
}
break;
default:
break;
}
}
/* output the IP packet to the ethernet device */
#else
#endif
{
m->m_data -= if_maxlinkhdr;
#else
return;
#endif
/* XXX: not correct */
#if 0
#else
#endif
#else
#endif
}
{
if (is_udp)
{
htons(guest_port), 0))
return -1;
}
else
{
htons(guest_port), 0))
return -1;
}
return 0;
}
int guest_port)
{
}
{
}
#if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
{
}
{
}
#endif
{
if (link_up)
{
if (time_fasttimo)
return 2;
if (do_slowtimo)
return 500; /* see PR_SLOWHZ */
}
return 0;
}
/*
* this function called from NAT thread
*/
{
}
#ifdef VBOX_WITH_SLIRP_MT
{
}
{
}
#endif