Patchwork slirp: Handle whole 127.0.0.0/8 network as local addresses.

login
register
mail settings
Submitter Anders Waldenborg
Date June 12, 2012, 12:12 p.m.
Message ID <1339503122-32264-1-git-send-email-anders@0x63.nu>
Download mbox | patch
Permalink /patch/164439/
State New
Headers show

Comments

Anders Waldenborg - June 12, 2012, 12:12 p.m.
Changes so translation of remote address to the host's ip address in
the virtual network happens for all addresses in the 127.0.0.0/8
network, not just 127.0.0.1.

This fixes hostfwd bound to addresses such as 127.0.0.2 works

Signed-off-by: Anders Waldenborg <anders@0x63.nu>
---
 slirp/main.h     |    1 +
 slirp/slirp.c    |    3 +++
 slirp/tcp_subr.c |    4 +++-
 3 files changed, 7 insertions(+), 1 deletions(-)
Jan Kiszka - July 5, 2012, 11:35 p.m.
On 2012-06-12 14:12, Anders Waldenborg wrote:
> Changes so translation of remote address to the host's ip address in
> the virtual network happens for all addresses in the 127.0.0.0/8
> network, not just 127.0.0.1.
> 
> This fixes hostfwd bound to addresses such as 127.0.0.2 works

Looks good, just two nits below.

> 
> Signed-off-by: Anders Waldenborg <anders@0x63.nu>
> ---
>  slirp/main.h     |    1 +
>  slirp/slirp.c    |    3 +++
>  slirp/tcp_subr.c |    4 +++-
>  3 files changed, 7 insertions(+), 1 deletions(-)
> 
> diff --git a/slirp/main.h b/slirp/main.h
> index 028df4b..bf601e2 100644
> --- a/slirp/main.h
> +++ b/slirp/main.h
> @@ -31,6 +31,7 @@ extern char *exec_shell;
>  extern u_int curtime;
>  extern fd_set *global_readfds, *global_writefds, *global_xfds;
>  extern struct in_addr loopback_addr;
> +extern in_addr_t loopback_mask;
>  extern char *username;
>  extern char *socket_path;
>  extern int towrite_max;
> diff --git a/slirp/slirp.c b/slirp/slirp.c
> index 90473eb..dbb83ec 100644
> --- a/slirp/slirp.c
> +++ b/slirp/slirp.c
> @@ -29,6 +29,8 @@
>  
>  /* host loopback address */
>  struct in_addr loopback_addr;
> +/* host loopback network mask */
> +in_addr_t loopback_mask;
>  
>  /* emulated hosts use the MAC addr 52:55:IP:IP:IP:IP */
>  static const uint8_t special_ethaddr[ETH_ALEN] = {
> @@ -191,6 +193,7 @@ static void slirp_init_once(void)
>  #endif
>  
>      loopback_addr.s_addr = htonl(INADDR_LOOPBACK);
> +    loopback_mask = htonl(0xff000000); /* 255.0.0.0 */

IN_CLASSA_NET would be clearer.

>  }
>  
>  static void slirp_state_save(QEMUFile *f, void *opaque);
> diff --git a/slirp/tcp_subr.c b/slirp/tcp_subr.c
> index 0a545c4..71f2b19 100644
> --- a/slirp/tcp_subr.c
> +++ b/slirp/tcp_subr.c
> @@ -435,7 +435,9 @@ tcp_connect(struct socket *inso)
>  	so->so_fport = addr.sin_port;
>  	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)
> +	if ((so->so_faddr.s_addr == 0) ||

The inner braces are unneeded, also below (wouldn't mind if the above
remark didn't require a v2 anyway).

> +	    ((so->so_faddr.s_addr & loopback_mask) ==
> +	     (loopback_addr.s_addr & loopback_mask)))
>  	   so->so_faddr = slirp->vhost_addr;
>  
>  	/* Close the accept() socket, set right state */
> 

Thanks,
Jan

Patch

diff --git a/slirp/main.h b/slirp/main.h
index 028df4b..bf601e2 100644
--- a/slirp/main.h
+++ b/slirp/main.h
@@ -31,6 +31,7 @@  extern char *exec_shell;
 extern u_int curtime;
 extern fd_set *global_readfds, *global_writefds, *global_xfds;
 extern struct in_addr loopback_addr;
+extern in_addr_t loopback_mask;
 extern char *username;
 extern char *socket_path;
 extern int towrite_max;
diff --git a/slirp/slirp.c b/slirp/slirp.c
index 90473eb..dbb83ec 100644
--- a/slirp/slirp.c
+++ b/slirp/slirp.c
@@ -29,6 +29,8 @@ 
 
 /* host loopback address */
 struct in_addr loopback_addr;
+/* host loopback network mask */
+in_addr_t loopback_mask;
 
 /* emulated hosts use the MAC addr 52:55:IP:IP:IP:IP */
 static const uint8_t special_ethaddr[ETH_ALEN] = {
@@ -191,6 +193,7 @@  static void slirp_init_once(void)
 #endif
 
     loopback_addr.s_addr = htonl(INADDR_LOOPBACK);
+    loopback_mask = htonl(0xff000000); /* 255.0.0.0 */
 }
 
 static void slirp_state_save(QEMUFile *f, void *opaque);
diff --git a/slirp/tcp_subr.c b/slirp/tcp_subr.c
index 0a545c4..71f2b19 100644
--- a/slirp/tcp_subr.c
+++ b/slirp/tcp_subr.c
@@ -435,7 +435,9 @@  tcp_connect(struct socket *inso)
 	so->so_fport = addr.sin_port;
 	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)
+	if ((so->so_faddr.s_addr == 0) ||
+	    ((so->so_faddr.s_addr & loopback_mask) ==
+	     (loopback_addr.s_addr & loopback_mask)))
 	   so->so_faddr = slirp->vhost_addr;
 
 	/* Close the accept() socket, set right state */