version 1.1.1.6, 2018/04/24 17:25:46
|
version 1.1.1.7, 2018/04/24 17:41:03
|
Line 27
|
Line 27
|
#include "slirp.h" |
#include "slirp.h" |
#include "hw/hw.h" |
#include "hw/hw.h" |
|
|
/* host address */ |
|
struct in_addr our_addr; |
|
/* host dns address */ |
|
struct in_addr dns_addr; |
|
/* host loopback address */ |
/* host loopback address */ |
struct in_addr loopback_addr; |
struct in_addr loopback_addr; |
|
|
Line 48 u_int curtime;
|
Line 44 u_int curtime;
|
static u_int time_fasttimo, last_slowtimo; |
static u_int time_fasttimo, last_slowtimo; |
static int do_slowtimo; |
static int do_slowtimo; |
|
|
static TAILQ_HEAD(slirp_instances, Slirp) slirp_instances = |
static QTAILQ_HEAD(slirp_instances, Slirp) slirp_instances = |
TAILQ_HEAD_INITIALIZER(slirp_instances); |
QTAILQ_HEAD_INITIALIZER(slirp_instances); |
|
|
|
static struct in_addr dns_addr; |
|
static u_int dns_addr_time; |
|
|
#ifdef _WIN32 |
#ifdef _WIN32 |
|
|
static int get_dns_addr(struct in_addr *pdns_addr) |
int get_dns_addr(struct in_addr *pdns_addr) |
{ |
{ |
FIXED_INFO *FixedInfo=NULL; |
FIXED_INFO *FixedInfo=NULL; |
ULONG BufLen; |
ULONG BufLen; |
Line 61 static int get_dns_addr(struct in_addr *
|
Line 60 static int get_dns_addr(struct in_addr *
|
IP_ADDR_STRING *pIPAddr; |
IP_ADDR_STRING *pIPAddr; |
struct in_addr tmp_addr; |
struct in_addr tmp_addr; |
|
|
|
if (dns_addr.s_addr != 0 && (curtime - dns_addr_time) < 1000) { |
|
*pdns_addr = dns_addr; |
|
return 0; |
|
} |
|
|
FixedInfo = (FIXED_INFO *)GlobalAlloc(GPTR, sizeof(FIXED_INFO)); |
FixedInfo = (FIXED_INFO *)GlobalAlloc(GPTR, sizeof(FIXED_INFO)); |
BufLen = sizeof(FIXED_INFO); |
BufLen = sizeof(FIXED_INFO); |
|
|
Line 84 static int get_dns_addr(struct in_addr *
|
Line 88 static int get_dns_addr(struct in_addr *
|
pIPAddr = &(FixedInfo->DnsServerList); |
pIPAddr = &(FixedInfo->DnsServerList); |
inet_aton(pIPAddr->IpAddress.String, &tmp_addr); |
inet_aton(pIPAddr->IpAddress.String, &tmp_addr); |
*pdns_addr = tmp_addr; |
*pdns_addr = tmp_addr; |
|
dns_addr = tmp_addr; |
|
dns_addr_time = curtime; |
if (FixedInfo) { |
if (FixedInfo) { |
GlobalFree(FixedInfo); |
GlobalFree(FixedInfo); |
FixedInfo = NULL; |
FixedInfo = NULL; |
Line 98 static void winsock_cleanup(void)
|
Line 104 static void winsock_cleanup(void)
|
|
|
#else |
#else |
|
|
static int get_dns_addr(struct in_addr *pdns_addr) |
static struct stat dns_addr_stat; |
|
|
|
int get_dns_addr(struct in_addr *pdns_addr) |
{ |
{ |
char buff[512]; |
char buff[512]; |
char buff2[257]; |
char buff2[257]; |
Line 106 static int get_dns_addr(struct in_addr *
|
Line 114 static int get_dns_addr(struct in_addr *
|
int found = 0; |
int found = 0; |
struct in_addr tmp_addr; |
struct in_addr tmp_addr; |
|
|
|
if (dns_addr.s_addr != 0) { |
|
struct stat old_stat; |
|
if ((curtime - dns_addr_time) < 1000) { |
|
*pdns_addr = dns_addr; |
|
return 0; |
|
} |
|
old_stat = dns_addr_stat; |
|
if (stat("/etc/resolv.conf", &dns_addr_stat) != 0) |
|
return -1; |
|
if ((dns_addr_stat.st_dev == old_stat.st_dev) |
|
&& (dns_addr_stat.st_ino == old_stat.st_ino) |
|
&& (dns_addr_stat.st_size == old_stat.st_size) |
|
&& (dns_addr_stat.st_mtime == old_stat.st_mtime)) { |
|
*pdns_addr = dns_addr; |
|
return 0; |
|
} |
|
} |
|
|
f = fopen("/etc/resolv.conf", "r"); |
f = fopen("/etc/resolv.conf", "r"); |
if (!f) |
if (!f) |
return -1; |
return -1; |
Line 117 static int get_dns_addr(struct in_addr *
|
Line 143 static int get_dns_addr(struct in_addr *
|
if (sscanf(buff, "nameserver%*[ \t]%256s", buff2) == 1) { |
if (sscanf(buff, "nameserver%*[ \t]%256s", buff2) == 1) { |
if (!inet_aton(buff2, &tmp_addr)) |
if (!inet_aton(buff2, &tmp_addr)) |
continue; |
continue; |
if (tmp_addr.s_addr == loopback_addr.s_addr) |
|
tmp_addr = our_addr; |
|
/* If it's the first one, set it to dns_addr */ |
/* If it's the first one, set it to dns_addr */ |
if (!found) |
if (!found) { |
*pdns_addr = tmp_addr; |
*pdns_addr = tmp_addr; |
|
dns_addr = tmp_addr; |
|
dns_addr_time = curtime; |
|
} |
#ifdef DEBUG |
#ifdef DEBUG |
else |
else |
lprint(", "); |
lprint(", "); |
Line 149 static int get_dns_addr(struct in_addr *
|
Line 176 static int get_dns_addr(struct in_addr *
|
static void slirp_init_once(void) |
static void slirp_init_once(void) |
{ |
{ |
static int initialized; |
static int initialized; |
struct hostent *he; |
|
char our_name[256]; |
|
#ifdef _WIN32 |
#ifdef _WIN32 |
WSADATA Data; |
WSADATA Data; |
#endif |
#endif |
Line 166 static void slirp_init_once(void)
|
Line 191 static void slirp_init_once(void)
|
#endif |
#endif |
|
|
loopback_addr.s_addr = htonl(INADDR_LOOPBACK); |
loopback_addr.s_addr = htonl(INADDR_LOOPBACK); |
|
|
/* FIXME: This address may change during runtime */ |
|
if (gethostname(our_name, sizeof(our_name)) == 0) { |
|
he = gethostbyname(our_name); |
|
if (he) { |
|
our_addr = *(struct in_addr *)he->h_addr; |
|
} |
|
} |
|
if (our_addr.s_addr == 0) { |
|
our_addr = loopback_addr; |
|
} |
|
|
|
/* FIXME: This address may change during runtime */ |
|
if (get_dns_addr(&dns_addr) < 0) { |
|
dns_addr = loopback_addr; |
|
} |
|
} |
} |
|
|
static void slirp_state_save(QEMUFile *f, void *opaque); |
static void slirp_state_save(QEMUFile *f, void *opaque); |
Line 225 Slirp *slirp_init(int restricted, struct
|
Line 234 Slirp *slirp_init(int restricted, struct
|
|
|
register_savevm("slirp", 0, 3, slirp_state_save, slirp_state_load, slirp); |
register_savevm("slirp", 0, 3, slirp_state_save, slirp_state_load, slirp); |
|
|
TAILQ_INSERT_TAIL(&slirp_instances, slirp, entry); |
QTAILQ_INSERT_TAIL(&slirp_instances, slirp, entry); |
|
|
return slirp; |
return slirp; |
} |
} |
|
|
void slirp_cleanup(Slirp *slirp) |
void slirp_cleanup(Slirp *slirp) |
{ |
{ |
TAILQ_REMOVE(&slirp_instances, slirp, entry); |
QTAILQ_REMOVE(&slirp_instances, slirp, entry); |
|
|
unregister_savevm("slirp", slirp); |
unregister_savevm("slirp", slirp); |
|
|
Line 252 void slirp_select_fill(int *pnfds,
|
Line 261 void slirp_select_fill(int *pnfds,
|
struct socket *so, *so_next; |
struct socket *so, *so_next; |
int nfds; |
int nfds; |
|
|
if (TAILQ_EMPTY(&slirp_instances)) { |
if (QTAILQ_EMPTY(&slirp_instances)) { |
return; |
return; |
} |
} |
|
|
Line 267 void slirp_select_fill(int *pnfds,
|
Line 276 void slirp_select_fill(int *pnfds,
|
*/ |
*/ |
do_slowtimo = 0; |
do_slowtimo = 0; |
|
|
TAILQ_FOREACH(slirp, &slirp_instances, entry) { |
QTAILQ_FOREACH(slirp, &slirp_instances, entry) { |
/* |
/* |
* *_slowtimo needs calling if there are IP fragments |
* *_slowtimo needs calling if there are IP fragments |
* in the fragment queue, or there are TCP connections active |
* in the fragment queue, or there are TCP connections active |
Line 375 void slirp_select_poll(fd_set *readfds,
|
Line 384 void slirp_select_poll(fd_set *readfds,
|
struct socket *so, *so_next; |
struct socket *so, *so_next; |
int ret; |
int ret; |
|
|
if (TAILQ_EMPTY(&slirp_instances)) { |
if (QTAILQ_EMPTY(&slirp_instances)) { |
return; |
return; |
} |
} |
|
|
Line 385 void slirp_select_poll(fd_set *readfds,
|
Line 394 void slirp_select_poll(fd_set *readfds,
|
|
|
curtime = qemu_get_clock(rt_clock); |
curtime = qemu_get_clock(rt_clock); |
|
|
TAILQ_FOREACH(slirp, &slirp_instances, entry) { |
QTAILQ_FOREACH(slirp, &slirp_instances, entry) { |
/* |
/* |
* See if anything has timed out |
* See if anything has timed out |
*/ |
*/ |