qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH 24/41] slirp: Factor out one-time initialization


From: Jan Kiszka
Subject: [Qemu-devel] [PATCH 24/41] slirp: Factor out one-time initialization
Date: Wed, 24 Jun 2009 14:42:30 +0200
User-agent: StGIT/0.14.3

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 <address@hidden>
---

 slirp/misc.c  |   17 ----------------
 slirp/misc.h  |    1 -
 slirp/slirp.c |   60 +++++++++++++++++++++++++++++++++++++++------------------
 3 files changed, 41 insertions(+), 37 deletions(-)

diff --git a/slirp/misc.c b/slirp/misc.c
index ae6f211..cffb3a0 100644
--- a/slirp/misc.c
+++ b/slirp/misc.c
@@ -16,23 +16,6 @@ int slirp_debug = DBG_CALL|DBG_MISC|DBG_ERROR;
 
 u_int curtime, time_fasttimo, last_slowtimo;
 
-/*
- * Get our IP address and put it in our_addr
- */
-void
-getouraddr(void)
-{
-       char buff[256];
-       struct hostent *he = NULL;
-
-       if (gethostname(buff,256) == 0)
-            he = gethostbyname(buff);
-        if (he)
-            our_addr = *(struct in_addr *)he->h_addr;
-        if (our_addr.s_addr == 0)
-            our_addr.s_addr = loopback_addr.s_addr;
-}
-
 struct quehead {
        struct quehead *qh_link;
        struct quehead *qh_rlink;
diff --git a/slirp/misc.h b/slirp/misc.h
index 82dd596..c21f69b 100644
--- a/slirp/misc.h
+++ b/slirp/misc.h
@@ -63,7 +63,6 @@ extern int x_port, x_server, x_display;
 
 int show_x _P((char *, struct socket *));
 void redir_x _P((u_int32_t, int, int, int));
-void getouraddr _P((void));
 void slirp_insque _P((void *, void *));
 void slirp_remque _P((void *));
 int add_exec _P((struct ex_list **, int, char *, struct in_addr, int));
diff --git a/slirp/slirp.c b/slirp/slirp.c
index 44a53cd..ea66c0c 100644
--- a/slirp/slirp.c
+++ b/slirp/slirp.c
@@ -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);
 }
 





reply via email to

[Prev in Thread] Current Thread [Next in Thread]