]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
ip/tunnel: introduce tnl_parse_key()
authorPhil Sutter <phil@nwl.cc>
Fri, 13 Nov 2015 17:08:55 +0000 (18:08 +0100)
committerStephen Hemminger <shemming@brocade.com>
Mon, 23 Nov 2015 23:26:37 +0000 (15:26 -0800)
Instead of duplicating the same code six times (key, ikey and okey in
iptunnel and ip6tunnel), have a common parsing routine. This has the
added benefit of having the same verbose error message in ip6tunnel as
well as iptunnel.

I'm not sure if parsing an IPv4 address as key makes sense for
ip6tunnel, but the code was there before so this patch at least doesn't
make it worse.

Signed-off-by: Phil Sutter <phil@nwl.cc>
ip/ip6tunnel.c
ip/iptunnel.c
ip/tunnel.c
ip/tunnel.h

index 07010d3142a7988a06bc26f87f353ef92714bb71..8b842b686cb113c0848b8ff8e51d411a7f9a582d 100644 (file)
@@ -230,45 +230,18 @@ static int parse_args(int argc, char **argv, int cmd, struct ip6_tnl_parm2 *p)
                                invarg("not inherit", *argv);
                        p->flags |= IP6_TNL_F_RCV_DSCP_COPY;
                } else if (strcmp(*argv, "key") == 0) {
-                       unsigned uval;
                        NEXT_ARG();
                        p->i_flags |= GRE_KEY;
                        p->o_flags |= GRE_KEY;
-                       if (strchr(*argv, '.'))
-                               p->i_key = p->o_key = get_addr32(*argv);
-                       else {
-                               if (get_unsigned(&uval, *argv, 0) < 0) {
-                                       fprintf(stderr, "invalid value of \"key\"\n");
-                                       exit(-1);
-                               }
-                               p->i_key = p->o_key = htonl(uval);
-                       }
+                       p->i_key = p->o_key = tnl_parse_key("key", *argv);
                } else if (strcmp(*argv, "ikey") == 0) {
-                       unsigned uval;
                        NEXT_ARG();
                        p->i_flags |= GRE_KEY;
-                       if (strchr(*argv, '.'))
-                               p->i_key = get_addr32(*argv);
-                       else {
-                               if (get_unsigned(&uval, *argv, 0)<0) {
-                                       fprintf(stderr, "invalid value of \"ikey\"\n");
-                                       exit(-1);
-                               }
-                               p->i_key = htonl(uval);
-                       }
+                       p->i_key = tnl_parse_key("ikey", *argv);
                } else if (strcmp(*argv, "okey") == 0) {
-                       unsigned uval;
                        NEXT_ARG();
                        p->o_flags |= GRE_KEY;
-                       if (strchr(*argv, '.'))
-                               p->o_key = get_addr32(*argv);
-                       else {
-                               if (get_unsigned(&uval, *argv, 0)<0) {
-                                       fprintf(stderr, "invalid value of \"okey\"\n");
-                                       exit(-1);
-                               }
-                               p->o_key = htonl(uval);
-                       }
+                       p->o_key = tnl_parse_key("okey", *argv);
                } else if (strcmp(*argv, "seq") == 0) {
                        p->i_flags |= GRE_SEQ;
                        p->o_flags |= GRE_SEQ;
index 36534f2985d8b60c5a4ef0e99d1c0dfd523c2c4f..9c9dc548a1488d9ef2bfd785be163667d25693d1 100644 (file)
@@ -106,45 +106,18 @@ static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
                                exit(-1);
                        }
                } else if (strcmp(*argv, "key") == 0) {
-                       unsigned uval;
                        NEXT_ARG();
                        p->i_flags |= GRE_KEY;
                        p->o_flags |= GRE_KEY;
-                       if (strchr(*argv, '.'))
-                               p->i_key = p->o_key = get_addr32(*argv);
-                       else {
-                               if (get_unsigned(&uval, *argv, 0)<0) {
-                                       fprintf(stderr, "invalid value for \"key\": \"%s\"; it should be an unsigned integer\n", *argv);
-                                       exit(-1);
-                               }
-                               p->i_key = p->o_key = htonl(uval);
-                       }
+                       p->i_key = p->o_key = tnl_parse_key("key", *argv);
                } else if (strcmp(*argv, "ikey") == 0) {
-                       unsigned uval;
                        NEXT_ARG();
                        p->i_flags |= GRE_KEY;
-                       if (strchr(*argv, '.'))
-                               p->i_key = get_addr32(*argv);
-                       else {
-                               if (get_unsigned(&uval, *argv, 0)<0) {
-                                       fprintf(stderr, "invalid value for \"ikey\": \"%s\"; it should be an unsigned integer\n", *argv);
-                                       exit(-1);
-                               }
-                               p->i_key = htonl(uval);
-                       }
+                       p->i_key = tnl_parse_key("ikey", *argv);
                } else if (strcmp(*argv, "okey") == 0) {
-                       unsigned uval;
                        NEXT_ARG();
                        p->o_flags |= GRE_KEY;
-                       if (strchr(*argv, '.'))
-                               p->o_key = get_addr32(*argv);
-                       else {
-                               if (get_unsigned(&uval, *argv, 0)<0) {
-                                       fprintf(stderr, "invalid value for \"okey\": \"%s\"; it should be an unsigned integer\n", *argv);
-                                       exit(-1);
-                               }
-                               p->o_key = htonl(uval);
-                       }
+                       p->o_key = tnl_parse_key("okey", *argv);
                } else if (strcmp(*argv, "seq") == 0) {
                        p->i_flags |= GRE_SEQ;
                        p->o_flags |= GRE_SEQ;
index d69fe84d2af0a6d50a3b418cb2fbe1f35f4e8a84..79f2201f2356f9d271aa9933ff056d5231ceb33e 100644 (file)
@@ -180,3 +180,18 @@ int tnl_ioctl_get_6rd(const char *name, void *p)
 {
        return tnl_gen_ioctl(SIOCGET6RD, name, p, EINVAL);
 }
+
+__be32 tnl_parse_key(const char *name, const char *key)
+{
+       unsigned uval;
+
+       if (strchr(key, '.'))
+               return get_addr32(key);
+
+       if (get_unsigned(&uval, key, 0) < 0) {
+               fprintf(stderr, "invalid value for \"%s\": \"%s\";", name, key);
+               fprintf(stderr, " it should be an unsigned integer\n");
+               exit(-1);
+       }
+       return htonl(uval);
+}
index 9c2f5d293ded85762a2a1ef565a498c8c43cd868..9fb4a186176fa3cda0e3529a451c67b4dfc32690 100644 (file)
@@ -31,5 +31,6 @@ int tnl_del_ioctl(const char *basedev, const char *name, void *p);
 int tnl_prl_ioctl(int cmd, const char *name, void *p);
 int tnl_6rd_ioctl(int cmd, const char *name, void *p);
 int tnl_ioctl_get_6rd(const char *name, void *p);
+__be32 tnl_parse_key(const char *name, const char *key);
 
 #endif