diff mbox

linux-user: convert /proc/net/route when endianess differs

Message ID 1356036804-19258-1-git-send-email-laurent@vivier.eu
State New
Headers show

Commit Message

Laurent Vivier Dec. 20, 2012, 8:53 p.m. UTC
This patch allows to have IP addresses in correct order
in the case of "netstat -nr" when the endianess of the
guest differs from one of the host.

For instance, an m68k guest on an x86_64 host:

WITHOUT this patch:

$ netstat -nr
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         1.3.0.10        0.0.0.0         UG        0 0          0 eth0
0.3.0.10        0.0.0.0         0.255.255.255   U         0 0          0 eth0
$ cat /proc/net/route
Iface	Destination	Gateway 	Flags	RefCnt	Use	Metric	Mask	MTU	Window	IRTT

eth0	00000000	0103000A	0003	0	0	0	000000000	0	0
eth0	0003000A	00000000	0001	0	0	0	00FFFFFF0	0	0

WITH this patch:

$ netstat -nr
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         10.0.3.1        0.0.0.0         UG        0 0          0 eth0
10.0.3.0        0.0.0.0         255.255.255.0   U         0 0          0 eth0
$ cat /proc/net/route
Iface	Destination	Gateway 	Flags	RefCnt	Use	Metric	Mask	MTU	Window	IRTT
eth0	00000000	0a000301	0003	0	0	0	000000000	0	0
eth0	0a000300	00000000	0001	0	0	0	ffffff000	0	0

Signed-off-by: Laurent Vivier <laurent@vivier.eu>
---
 linux-user/syscall.c |   42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)

Comments

Laurent Vivier Jan. 1, 2013, 11:08 p.m. UTC | #1
Ping !


Le jeudi 20 décembre 2012 à 21:53 +0100, Laurent Vivier a écrit :
> This patch allows to have IP addresses in correct order
> in the case of "netstat -nr" when the endianess of the
> guest differs from one of the host.
> 
> For instance, an m68k guest on an x86_64 host:
> 
> WITHOUT this patch:
> 
> $ netstat -nr
> Kernel IP routing table
> Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
> 0.0.0.0         1.3.0.10        0.0.0.0         UG        0 0          0 eth0
> 0.3.0.10        0.0.0.0         0.255.255.255   U         0 0          0 eth0
> $ cat /proc/net/route
> Iface	Destination	Gateway 	Flags	RefCnt	Use	Metric	Mask	MTU	Window	IRTT
> 
> eth0	00000000	0103000A	0003	0	0	0	000000000	0	0
> eth0	0003000A	00000000	0001	0	0	0	00FFFFFF0	0	0
> 
> WITH this patch:
> 
> $ netstat -nr
> Kernel IP routing table
> Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
> 0.0.0.0         10.0.3.1        0.0.0.0         UG        0 0          0 eth0
> 10.0.3.0        0.0.0.0         255.255.255.0   U         0 0          0 eth0
> $ cat /proc/net/route
> Iface	Destination	Gateway 	Flags	RefCnt	Use	Metric	Mask	MTU	Window	IRTT
> eth0	00000000	0a000301	0003	0	0	0	000000000	0	0
> eth0	0a000300	00000000	0001	0	0	0	ffffff000	0	0
> 
> Signed-off-by: Laurent Vivier <laurent@vivier.eu>
> ---
>  linux-user/syscall.c |   42 ++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 42 insertions(+)
> 
> diff --git a/linux-user/syscall.c b/linux-user/syscall.c
> index e99adab..501002b 100644
> --- a/linux-user/syscall.c
> +++ b/linux-user/syscall.c
> @@ -5085,6 +5085,45 @@ static int open_self_auxv(void *cpu_env, int fd)
>      return 0;
>  }
>  
> +#if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
> +static int open_net_route(void *cpu_env, int fd)
> +{
> +    FILE *fp;
> +    char *line = NULL;
> +    size_t len = 0;
> +    ssize_t read;
> +
> +    fp = fopen("/proc/net/route", "r");
> +    if (fp == NULL) {
> +        return -EACCES;
> +    }
> +
> +    /* read header */
> +
> +    read = getline(&line, &len, fp);
> +    dprintf(fd, "%s", line);
> +
> +    /* read routes */
> +
> +    while ((read = getline(&line, &len, fp)) != -1) {
> +        char iface[16];
> +        uint32_t dest, gw, mask;
> +        unsigned int flags, refcnt, use, metric, mtu, window, irtt;
> +        sscanf(line, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
> +                     iface, &dest, &gw, &flags, &refcnt, &use, &metric,
> +                     &mask, &mtu, &window, &irtt);
> +        dprintf(fd, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
> +                iface, tswap32(dest), tswap32(gw), flags, refcnt, use,
> +                metric, tswap32(mask), mtu, window, irtt);
> +    }
> +
> +    free(line);
> +    fclose(fp);
> +
> +    return 0;
> +}
> +#endif
> +
>  static int do_open(void *cpu_env, const char *pathname, int flags, mode_t mode)
>  {
>      struct fake_open {
> @@ -5096,6 +5135,9 @@ static int do_open(void *cpu_env, const char *pathname, int flags, mode_t mode)
>          { "/proc/self/maps", open_self_maps },
>          { "/proc/self/stat", open_self_stat },
>          { "/proc/self/auxv", open_self_auxv },
> +#if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
> +        { "/proc/net/route", open_net_route },
> +#endif
>          { NULL, NULL }
>      };
>
Laurent Vivier Jan. 26, 2013, 11:19 a.m. UTC | #2
Le mercredi 02 janvier 2013 à 00:08 +0100, Laurent Vivier a écrit :
> Ping !

ping

> Le jeudi 20 décembre 2012 à 21:53 +0100, Laurent Vivier a écrit :
> > This patch allows to have IP addresses in correct order
> > in the case of "netstat -nr" when the endianess of the
> > guest differs from one of the host.
> > 
> > For instance, an m68k guest on an x86_64 host:
> > 
> > WITHOUT this patch:
> > 
> > $ netstat -nr
> > Kernel IP routing table
> > Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
> > 0.0.0.0         1.3.0.10        0.0.0.0         UG        0 0          0 eth0
> > 0.3.0.10        0.0.0.0         0.255.255.255   U         0 0          0 eth0
> > $ cat /proc/net/route
> > Iface	Destination	Gateway 	Flags	RefCnt	Use	Metric	Mask	MTU	Window	IRTT
> > 
> > eth0	00000000	0103000A	0003	0	0	0	000000000	0	0
> > eth0	0003000A	00000000	0001	0	0	0	00FFFFFF0	0	0
> > 
> > WITH this patch:
> > 
> > $ netstat -nr
> > Kernel IP routing table
> > Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
> > 0.0.0.0         10.0.3.1        0.0.0.0         UG        0 0          0 eth0
> > 10.0.3.0        0.0.0.0         255.255.255.0   U         0 0          0 eth0
> > $ cat /proc/net/route
> > Iface	Destination	Gateway 	Flags	RefCnt	Use	Metric	Mask	MTU	Window	IRTT
> > eth0	00000000	0a000301	0003	0	0	0	000000000	0	0
> > eth0	0a000300	00000000	0001	0	0	0	ffffff000	0	0
> > 
> > Signed-off-by: Laurent Vivier <laurent@vivier.eu>
> > ---
> >  linux-user/syscall.c |   42 ++++++++++++++++++++++++++++++++++++++++++
> >  1 file changed, 42 insertions(+)
> > 
> > diff --git a/linux-user/syscall.c b/linux-user/syscall.c
> > index e99adab..501002b 100644
> > --- a/linux-user/syscall.c
> > +++ b/linux-user/syscall.c
> > @@ -5085,6 +5085,45 @@ static int open_self_auxv(void *cpu_env, int fd)
> >      return 0;
> >  }
> >  
> > +#if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
> > +static int open_net_route(void *cpu_env, int fd)
> > +{
> > +    FILE *fp;
> > +    char *line = NULL;
> > +    size_t len = 0;
> > +    ssize_t read;
> > +
> > +    fp = fopen("/proc/net/route", "r");
> > +    if (fp == NULL) {
> > +        return -EACCES;
> > +    }
> > +
> > +    /* read header */
> > +
> > +    read = getline(&line, &len, fp);
> > +    dprintf(fd, "%s", line);
> > +
> > +    /* read routes */
> > +
> > +    while ((read = getline(&line, &len, fp)) != -1) {
> > +        char iface[16];
> > +        uint32_t dest, gw, mask;
> > +        unsigned int flags, refcnt, use, metric, mtu, window, irtt;
> > +        sscanf(line, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
> > +                     iface, &dest, &gw, &flags, &refcnt, &use, &metric,
> > +                     &mask, &mtu, &window, &irtt);
> > +        dprintf(fd, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
> > +                iface, tswap32(dest), tswap32(gw), flags, refcnt, use,
> > +                metric, tswap32(mask), mtu, window, irtt);
> > +    }
> > +
> > +    free(line);
> > +    fclose(fp);
> > +
> > +    return 0;
> > +}
> > +#endif
> > +
> >  static int do_open(void *cpu_env, const char *pathname, int flags, mode_t mode)
> >  {
> >      struct fake_open {
> > @@ -5096,6 +5135,9 @@ static int do_open(void *cpu_env, const char *pathname, int flags, mode_t mode)
> >          { "/proc/self/maps", open_self_maps },
> >          { "/proc/self/stat", open_self_stat },
> >          { "/proc/self/auxv", open_self_auxv },
> > +#if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
> > +        { "/proc/net/route", open_net_route },
> > +#endif
> >          { NULL, NULL }
> >      };
> >  
>
diff mbox

Patch

diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index e99adab..501002b 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -5085,6 +5085,45 @@  static int open_self_auxv(void *cpu_env, int fd)
     return 0;
 }
 
+#if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
+static int open_net_route(void *cpu_env, int fd)
+{
+    FILE *fp;
+    char *line = NULL;
+    size_t len = 0;
+    ssize_t read;
+
+    fp = fopen("/proc/net/route", "r");
+    if (fp == NULL) {
+        return -EACCES;
+    }
+
+    /* read header */
+
+    read = getline(&line, &len, fp);
+    dprintf(fd, "%s", line);
+
+    /* read routes */
+
+    while ((read = getline(&line, &len, fp)) != -1) {
+        char iface[16];
+        uint32_t dest, gw, mask;
+        unsigned int flags, refcnt, use, metric, mtu, window, irtt;
+        sscanf(line, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
+                     iface, &dest, &gw, &flags, &refcnt, &use, &metric,
+                     &mask, &mtu, &window, &irtt);
+        dprintf(fd, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
+                iface, tswap32(dest), tswap32(gw), flags, refcnt, use,
+                metric, tswap32(mask), mtu, window, irtt);
+    }
+
+    free(line);
+    fclose(fp);
+
+    return 0;
+}
+#endif
+
 static int do_open(void *cpu_env, const char *pathname, int flags, mode_t mode)
 {
     struct fake_open {
@@ -5096,6 +5135,9 @@  static int do_open(void *cpu_env, const char *pathname, int flags, mode_t mode)
         { "/proc/self/maps", open_self_maps },
         { "/proc/self/stat", open_self_stat },
         { "/proc/self/auxv", open_self_auxv },
+#if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
+        { "/proc/net/route", open_net_route },
+#endif
         { NULL, NULL }
     };