diff options
author | Jan Kiszka <jan.kiszka@siemens.com> | 2009-06-24 14:42:30 +0200 |
---|---|---|
committer | Anthony Liguori <aliguori@us.ibm.com> | 2009-06-29 08:52:47 -0500 |
commit | 8fb8cb1b6c3ab78e454a4cce5ba3a630be7dc7d6 (patch) | |
tree | 51a8ff17485b5637e3addd3745ff3d124837a2de /slirp.c | |
parent | f1e53121bfc9135023567ac5a35a7795526aa27c (diff) | |
download | slirp-8fb8cb1b6c3ab78e454a4cce5ba3a630be7dc7d6.zip slirp-8fb8cb1b6c3ab78e454a4cce5ba3a630be7dc7d6.tar.gz slirp-8fb8cb1b6c3ab78e454a4cce5ba3a630be7dc7d6.tar.bz2 |
slirp: Factor out one-time initialization
In order to prepare re-initialization and multi-instance slirp, factor
out init code that is of global scope and (at least for now) only need
to be run once.
This also fixes the potentially uninitialized use of our_addr in
get_dns_addr.
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Diffstat (limited to 'slirp.c')
-rw-r--r-- | slirp.c | 60 |
1 files changed, 41 insertions, 19 deletions
@@ -102,6 +102,11 @@ static int get_dns_addr(struct in_addr *pdns_addr) return 0; } +static void winsock_cleanup(void) +{ + WSACleanup(); +} + #else static int get_dns_addr(struct in_addr *pdns_addr) @@ -152,13 +157,44 @@ static int get_dns_addr(struct in_addr *pdns_addr) #endif -#ifdef _WIN32 -static void slirp_cleanup(void) +static void slirp_init_once(void) { - WSACleanup(); -} + static int initialized; + struct hostent *he; + char our_name[256]; +#ifdef _WIN32 + WSADATA Data; #endif + if (initialized) { + return; + } + initialized = 1; + +#ifdef _WIN32 + WSAStartup(MAKEWORD(2, 0), &Data); + atexit(winsock_cleanup); +#endif + + 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 int slirp_state_load(QEMUFile *f, void *opaque, int version_id); @@ -168,12 +204,7 @@ void slirp_init(int restricted, struct in_addr vnetwork, const char *bootfile, struct in_addr vdhcp_start, struct in_addr vnameserver) { -#ifdef _WIN32 - WSADATA Data; - - WSAStartup(MAKEWORD(2, 0), &Data); - atexit(slirp_cleanup); -#endif + slirp_init_once(); link_up = 1; slirp_restrict = restricted; @@ -184,14 +215,6 @@ void slirp_init(int restricted, struct in_addr vnetwork, /* Initialise mbufs *after* setting the MTU */ m_init(); - /* set default addresses */ - inet_aton("127.0.0.1", &loopback_addr); - - if (get_dns_addr(&dns_addr) < 0) { - dns_addr = loopback_addr; - fprintf(stderr, "Warning: No DNS servers found\n"); - } - vnetwork_addr = vnetwork; vnetwork_mask = vnetmask; vhost_addr = vhost; @@ -211,7 +234,6 @@ void slirp_init(int restricted, struct in_addr vnetwork, vdhcp_startaddr = vdhcp_start; vnameserver_addr = vnameserver; - getouraddr(); register_savevm("slirp", 0, 1, slirp_state_save, slirp_state_load, NULL); } |