Commit df46189412567906312684eb72ba87c6a86a4cdb

Authored by Jan Kiszka
Committed by Anthony Liguori
1 parent 9367964a

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>
slirp/misc.c
... ... @@ -16,23 +16,6 @@ int slirp_debug = DBG_CALL|DBG_MISC|DBG_ERROR;
16 16  
17 17 u_int curtime, time_fasttimo, last_slowtimo;
18 18  
19   -/*
20   - * Get our IP address and put it in our_addr
21   - */
22   -void
23   -getouraddr(void)
24   -{
25   - char buff[256];
26   - struct hostent *he = NULL;
27   -
28   - if (gethostname(buff,256) == 0)
29   - he = gethostbyname(buff);
30   - if (he)
31   - our_addr = *(struct in_addr *)he->h_addr;
32   - if (our_addr.s_addr == 0)
33   - our_addr.s_addr = loopback_addr.s_addr;
34   -}
35   -
36 19 struct quehead {
37 20 struct quehead *qh_link;
38 21 struct quehead *qh_rlink;
... ...
slirp/misc.h
... ... @@ -63,7 +63,6 @@ extern int x_port, x_server, x_display;
63 63  
64 64 int show_x _P((char *, struct socket *));
65 65 void redir_x _P((u_int32_t, int, int, int));
66   -void getouraddr _P((void));
67 66 void slirp_insque _P((void *, void *));
68 67 void slirp_remque _P((void *));
69 68 int add_exec _P((struct ex_list **, int, char *, struct in_addr, int));
... ...
slirp/slirp.c
... ... @@ -102,6 +102,11 @@ static int get_dns_addr(struct in_addr *pdns_addr)
102 102 return 0;
103 103 }
104 104  
  105 +static void winsock_cleanup(void)
  106 +{
  107 + WSACleanup();
  108 +}
  109 +
105 110 #else
106 111  
107 112 static int get_dns_addr(struct in_addr *pdns_addr)
... ... @@ -152,13 +157,44 @@ static int get_dns_addr(struct in_addr *pdns_addr)
152 157  
153 158 #endif
154 159  
155   -#ifdef _WIN32
156   -static void slirp_cleanup(void)
  160 +static void slirp_init_once(void)
157 161 {
158   - WSACleanup();
159   -}
  162 + static int initialized;
  163 + struct hostent *he;
  164 + char our_name[256];
  165 +#ifdef _WIN32
  166 + WSADATA Data;
160 167 #endif
161 168  
  169 + if (initialized) {
  170 + return;
  171 + }
  172 + initialized = 1;
  173 +
  174 +#ifdef _WIN32
  175 + WSAStartup(MAKEWORD(2,0), &Data);
  176 + atexit(winsock_cleanup);
  177 +#endif
  178 +
  179 + loopback_addr.s_addr = htonl(INADDR_LOOPBACK);
  180 +
  181 + /* FIXME: This address may change during runtime */
  182 + if (gethostname(our_name, sizeof(our_name)) == 0) {
  183 + he = gethostbyname(our_name);
  184 + if (he) {
  185 + our_addr = *(struct in_addr *)he->h_addr;
  186 + }
  187 + }
  188 + if (our_addr.s_addr == 0) {
  189 + our_addr = loopback_addr;
  190 + }
  191 +
  192 + /* FIXME: This address may change during runtime */
  193 + if (get_dns_addr(&dns_addr) < 0) {
  194 + dns_addr = loopback_addr;
  195 + }
  196 +}
  197 +
162 198 static void slirp_state_save(QEMUFile *f, void *opaque);
163 199 static int slirp_state_load(QEMUFile *f, void *opaque, int version_id);
164 200  
... ... @@ -168,12 +204,7 @@ void slirp_init(int restricted, struct in_addr vnetwork,
168 204 const char *bootfile, struct in_addr vdhcp_start,
169 205 struct in_addr vnameserver)
170 206 {
171   -#ifdef _WIN32
172   - WSADATA Data;
173   -
174   - WSAStartup(MAKEWORD(2,0), &Data);
175   - atexit(slirp_cleanup);
176   -#endif
  207 + slirp_init_once();
177 208  
178 209 link_up = 1;
179 210 slirp_restrict = restricted;
... ... @@ -184,14 +215,6 @@ void slirp_init(int restricted, struct in_addr vnetwork,
184 215 /* Initialise mbufs *after* setting the MTU */
185 216 m_init();
186 217  
187   - /* set default addresses */
188   - inet_aton("127.0.0.1", &loopback_addr);
189   -
190   - if (get_dns_addr(&dns_addr) < 0) {
191   - dns_addr = loopback_addr;
192   - fprintf (stderr, "Warning: No DNS servers found\n");
193   - }
194   -
195 218 vnetwork_addr = vnetwork;
196 219 vnetwork_mask = vnetmask;
197 220 vhost_addr = vhost;
... ... @@ -211,7 +234,6 @@ void slirp_init(int restricted, struct in_addr vnetwork,
211 234 vdhcp_startaddr = vdhcp_start;
212 235 vnameserver_addr = vnameserver;
213 236  
214   - getouraddr();
215 237 register_savevm("slirp", 0, 1, slirp_state_save, slirp_state_load, NULL);
216 238 }
217 239  
... ...