Skip to contentSkip to conversation

Separate alias_addr (10.0.2.2) from our_addr in slirp

PATCH Ben Taylor 2 participants 2 messages May 2, 2006

lore
Share & tools
lore

Ben Taylor

author
#0 lore raw
Quoted 4 lines · ---- Ed Swierk

On 5/1/06, Fabrice Bellard <[email protected]> wrote:
I am not sure this patch is sufficient: sometimes our_addr is used to open socket on the host side and 10.0.2.2 has a meanning only on the VM side.

Indeed, our_addr seems to be used for two distinct purposes:

- application protocol emulation code (like udp_emu() in udp.c) uses the "host" address for opening sockets on the host; I'm not familiar with the details of these protocols, but in any event it must be some address that is meaningful to applications running on the host side;

- elsewhere in slirp, the "alias" address (10.0.2.2) is used to rewrite the source address of packets originating from the host to the VM; this address makes sense only within the VM.

The attached patch adds a new global, alias_addr, that is set to 10.0.2.2 and replaces our_addr in the latter cases. The patch also changes getouraddr() so that it always returns either a "real" address (as determined by gethostbyname(gethostname()) or 127.0.0.1, but never 10.0.2.2.

--Ed

Am I seeing a problem in line 98 of slirp/misc.c? The line above reads:

if (our_addr.s_addr == 0)
our_addr.s_addr == loopback_addr.s_addr:

Shouldn't that be "our_addr.s_addr = loopback_addr.s_addr;" (Single =)

Ben

#1 lore raw
Quoted 1 line · Ben Taylor

Am I seeing a problem in line 98 of slirp/misc.c?

Yes--thanks for finding that.

An amended patch is attached.

--Ed

slirp/ip_icmp.c +3 -4
diff -BurN qemu.orig/slirp/ip_icmp.c qemu/slirp/ip_icmp.c
@@ -114,8 +114,7 @@
   case ICMP_ECHO:
     icp->icmp_type = ICMP_ECHOREPLY;
     ip->ip_len += hlen;	             /* since ip_input subtracts this */
-    if (ip->ip_dst.s_addr == our_addr.s_addr || 
-	(ip->ip_dst.s_addr == (special_addr.s_addr|htonl(CTL_ALIAS))) ) {
+    if (ip->ip_dst.s_addr == alias_addr.s_addr) {
       icmp_reflect(m);
     } else {
       struct socket *so;
@@ -161,7 +160,7 @@
 	icmp_error(m, ICMP_UNREACH,ICMP_UNREACH_NET, 0,strerror(errno)); 
 	udp_detach(so);
       }
-    } /* if ip->ip_dst.s_addr == our_addr.s_addr */
+    } /* if ip->ip_dst.s_addr == alias_addr.s_addr */
     break;
   case ICMP_UNREACH:
     /* XXX? report error? close socket? */
@@ -311,7 +310,7 @@
   ip->ip_ttl = MAXTTL;
   ip->ip_p = IPPROTO_ICMP;
   ip->ip_dst = ip->ip_src;    /* ip adresses */
-  ip->ip_src = our_addr;
+  ip->ip_src = alias_addr;
 
   (void ) ip_output((struct socket *)NULL, m);
   
slirp/main.h +1 -0
diff -BurN qemu.orig/slirp/main.h qemu/slirp/main.h
@@ -34,6 +34,7 @@
 extern fd_set *global_readfds, *global_writefds, *global_xfds;
 extern struct in_addr ctl_addr;
 extern struct in_addr special_addr;
+extern struct in_addr alias_addr;
 extern struct in_addr our_addr;
 extern struct in_addr loopback_addr;
 extern struct in_addr dns_addr;
slirp/misc.c +2 -4
diff -BurN qemu.orig/slirp/misc.c qemu/slirp/misc.c
@@ -94,10 +94,8 @@
             he = gethostbyname(buff);
         if (he)
             our_addr = *(struct in_addr *)he->h_addr;
-        /* If the host doesn't have a useful IP address then use the
-           guest side address.  */
-        if (our_addr.s_addr == 0 || our_addr.s_addr == loopback_addr.s_addr)
-            our_addr.s_addr = special_addr.s_addr | htonl(CTL_ALIAS);
+        if (our_addr.s_addr == 0)
+            our_addr.s_addr = loopback_addr.s_addr;
 }
 
 #if SIZEOF_CHAR_P == 8
slirp/slirp.c +3 -0
diff -BurN qemu.orig/slirp/slirp.c qemu/slirp/slirp.c
@@ -9,6 +9,8 @@
 
 /* address for slirp virtual addresses */
 struct in_addr special_addr;
+/* virtual address alias for host */
+struct in_addr alias_addr;
 
 const uint8_t special_ethaddr[6] = { 
     0x52, 0x54, 0x00, 0x12, 0x35, 0x00
@@ -154,6 +156,7 @@
     }
 
     inet_aton(CTL_SPECIAL, &special_addr);
+    alias_addr.s_addr = special_addr.s_addr | htonl(CTL_ALIAS);
     getouraddr();
 }
 
slirp/socket.c +1 -1
diff -BurN qemu.orig/slirp/socket.c qemu/slirp/socket.c
@@ -596,7 +596,7 @@
 	getsockname(s,(struct sockaddr *)&addr,&addrlen);
 	so->so_fport = addr.sin_port;
 	if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr)
-	   so->so_faddr = our_addr;
+	   so->so_faddr = alias_addr;
 	else
 	   so->so_faddr = addr.sin_addr;
 
slirp/tcp_subr.c +2 -2
diff -BurN qemu.orig/slirp/tcp_subr.c qemu/slirp/tcp_subr.c
@@ -504,7 +504,7 @@
 	so->so_faddr = addr.sin_addr;
 	/* Translate connections from localhost to the real hostname */
 	if (so->so_faddr.s_addr == 0 || so->so_faddr.s_addr == loopback_addr.s_addr)
-	   so->so_faddr = our_addr;
+	   so->so_faddr = alias_addr;
 	
 	/* Close the accept() socket, set right state */
 	if (inso->so_state & SS_FACCEPTONCE) {
@@ -840,7 +840,7 @@
 
 				if (ns->so_faddr.s_addr == 0 || 
 					ns->so_faddr.s_addr == loopback_addr.s_addr)
-                  ns->so_faddr = our_addr;
+                  ns->so_faddr = alias_addr;
 
 				ns->so_iptos = tcp_tos(ns);
 				tp = sototcpcb(ns);
slirp/udp.c +1 -1
diff -BurN qemu.orig/slirp/udp.c qemu/slirp/udp.c
@@ -657,7 +657,7 @@
 	getsockname(so->s,(struct sockaddr *)&addr,&addrlen);
 	so->so_fport = addr.sin_port;
 	if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr)
-	   so->so_faddr = our_addr;
+	   so->so_faddr = alias_addr;
 	else
 	   so->so_faddr = addr.sin_addr;