slirp.c revision 677833bc953b6cb418c701facbdcf4aa18d6c44e
#include "slirp.h"
#ifdef __OS2__
# include <paths.h>
#endif
#ifdef VBOX
#endif
/* host address */
/* host dns address */
/* host loopback address */
struct in_addr loopback_addr;
/* address for slirp virtual addresses */
struct in_addr special_addr;
/* virtual address alias for host */
struct in_addr alias_addr;
0x52, 0x54, 0x00, 0x12, 0x35, 0x00
};
int do_slowtimo;
int link_up;
/* XXX: suppress those select globals */
char slirp_hostname[33];
#ifdef _WIN32
{
BufLen = sizeof(FIXED_INFO);
if (FixedInfo) {
}
}
#ifndef VBOX
#else /* VBOX */
#endif /* VBOX */
if (FixedInfo) {
}
return -1;
}
#ifndef VBOX
#if 0
printf( "DNS Servers:\n" );
while ( pIPAddr ) {
}
#endif
#else /* VBOX */
Log(("nat: DNS Servers:\n"));
while ( pIPAddr ) {
}
#endif /* VBOX */
if (FixedInfo) {
}
return 0;
}
#else
{
char buff[512];
char buff2[256];
FILE *f;
int found = 0;
#ifdef __OS2__
/* Try various locations. */
f = NULL;
if (etc)
{
}
if (!f) {
}
if (!f) {
}
#else
#endif
if (!f)
return -1;
#ifndef VBOX
lprint("IP address of your DNS(s): ");
#else /* VBOX */
Log(("nat: IP address of your DNS(s): \n"));
#endif /* VBOX */
continue;
/* If it's the first one, set it to dns_addr */
if (!found)
#ifndef VBOX
else
lprint(", ");
#endif /* !VBOX */
if (++found > 3) {
#ifndef VBOX
lprint("(more)");
#else /* VBOX */
Log(("nat: (more)\n"));
#endif /* VBOX */
break;
} else
#ifndef VBOX
#else /* VBOX */
#endif /* VBOX */
}
}
fclose(f);
if (!found)
return -1;
return 0;
}
#endif
#ifndef VBOX
#ifdef _WIN32
void slirp_cleanup(void)
{
WSACleanup();
}
#endif
#endif /* !VBOX (see slirp_term) */
#ifndef VBOX
void slirp_init(void)
{
#else /* VBOX */
/** Number of slirp users. Used for making sure init and term are only executed once. */
static int g_cUsers = 0;
int slirp_init(void)
{
if (g_cUsers != 0) {
g_cUsers++;
return VINF_SUCCESS;
}
#endif /* VBOX */
#ifdef _WIN32
{
#ifndef VBOX
#endif /* !VBOX */
}
#endif
#ifdef VBOX
#endif /* VBOX */
link_up = 1;
if_init();
ip_init();
/* Initialise mbufs *after* setting the MTU */
m_init();
/* set default addresses */
if (get_dns_addr(&dns_addr) < 0) {
#ifndef VBOX
#else
return VERR_NAT_DNS;
#endif
}
getouraddr();
#ifdef VBOX
g_cUsers++;
return VINF_SUCCESS;
#endif
}
#ifdef VBOX
/**
* Marks the link as up, making it possible to establish new connections.
*/
void slirp_link_up(void)
{
link_up = 1;
}
/**
* Marks the link as down and cleans up the current connections.
*/
void slirp_link_down(void)
{
{
else
}
udp_detach(so);
link_up = 0;
}
/**
* Terminates the slirp component.
*/
void slirp_term(void)
{
if (--g_cUsers > 0)
return;
#ifdef WIN32
WSACleanup();
#endif
#ifdef LOG_ENABLED
Log(("\n"
"NAT statistics\n"
"--------------\n"
"\n"));
ipstats();
tcpstats();
udpstats();
icmpstats();
mbufstats();
sockstats();
Log(("\n"
"\n"
"\n"));
#endif
}
#endif /* VBOX */
#define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
/*
* curtime kept to an accuracy of 1ms
*/
#ifdef _WIN32
static void updtime(void)
{
}
#else
static void updtime(void)
{
gettimeofday(&tt, 0);
curtime++;
}
#endif
void slirp_select_fill(int *pnfds,
{
int nfds;
int tmp_time;
/* fail safe */
global_xfds = NULL;
/*
* 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
*/
/*
* 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.
*/
continue;
/*
* Set for reading sockets which are accepting
*/
continue;
}
/*
* Set for writing sockets which are connecting
*/
continue;
}
/*
* 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
*/
udp_detach(so);
continue;
} 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 ?)
*/
}
}
}
/*
* Setup timeout to use minimum CPU usage, especially when idle
*/
/*
* First, see the timeout needed by *timo
*/
/*
* If a slowtimo is needed, set timeout to 500ms from the last
* slow timeout. If a fast timeout is needed, set timeout within
* 200ms of when it was requested.
*/
if (do_slowtimo) {
/* XXX + 10000 because some select()'s aren't that accurate */
/* Can only fasttimo if we also slowtimo */
if (time_fasttimo) {
if (tmp_time < 0)
tmp_time = 0;
/* Choose the smallest of the 2 */
}
}
}
{
int ret;
global_xfds = xfds;
/* Update time */
updtime();
/*
* See if anything has timed out
*/
if (link_up) {
tcp_fasttimo();
time_fasttimo = 0;
}
ip_slowtimo();
tcp_slowtimo();
}
}
/*
* Check sockets
*/
if (link_up) {
/*
* Check TCP sockets
*/
/*
* FD_ISSET is meaningless on these sockets
* (and they can crash the program)
*/
continue;
/*
* Check for URG data
* This will soread as well, so no need to
* test for readfds below if this succeeds
*/
/*
* Check sockets for reading
*/
/*
* Check for incoming connections
*/
continue;
} /* else */
/* Output it if we read something */
if (ret > 0)
}
/*
* Check sockets for writing
*/
/*
* Check for non-blocking, still-connecting sockets
*/
/* Connected */
if (ret < 0) {
/* XXXXX Must fix, zero bytes is a NOP */
continue;
/* else failed */
}
/* else so->so_state &= ~SS_ISFCONNECTING; */
/*
* Continue tcp_input
*/
/* continue; */
} else
/*
* XXXXX If we wrote something (a lot), there
* could be a 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 */
continue; /* Still connecting, continue */
/* else failed */
/* tcp_input will take care of it */
} else {
if (ret < 0) {
/* XXX */
continue;
/* 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.
*/
sorecvfrom(so);
}
}
}
/*
* See if we can start outputting
*/
if_start();
/* clear global file descriptor sets.
* these reside on the stack in vl.c
* so they're unusable if we're not in
* slirp_select_fill or slirp_select_poll.
*/
global_xfds = NULL;
}
#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...
*/
};
#ifdef VBOX
static
#endif
{
int ar_op;
switch(ar_op) {
case ARPOP_REQUEST:
goto arp_ok;
goto arp_ok;
}
return;
/* XXX: make an ARP request to have the client address */
}
break;
default:
break;
}
}
{
struct mbuf *m;
int proto;
return;
switch(proto) {
case ETH_P_ARP:
break;
case ETH_P_IP:
m = m_get();
if (!m)
return;
/* Note: we add to align the IP header */
ip_input(m);
break;
default:
break;
}
}
/* output the IP packet to the ethernet device */
{
return;
/* XXX: not correct */
}
{
if (is_udp) {
htons(guest_port), 0))
return -1;
} else {
htons(guest_port), 0))
return -1;
}
return 0;
}
int guest_port)
{
}