iptunnel/ip6tunnel: Code cleanups

Use switch () instead of if () to compare tunnel type to fit into 80
columns and make code more readable. Print "\n" using fputc().

In iptunnel.c abstract tunnel parameters matching code in iptunnel.c
into ip_tunnel_parm_match() helper to conform with ip6tunnel.c. Use
memset() to initialize @p1.

In ip6tunnel.c no need to call ll_name_to_index() with name twice: just
use found previously index. Do not initialize @p1: this is done in
ip6_tnl_parm_init().

This is to show real differences between ip and ipv6 do_tunnels_list()
implementations and prepare for upcoming unification of them.

Signed-off-by: Serhey Popovych <serhe.popovych@gmail.com>
Signed-off-by: David Ahern <dsahern@gmail.com>
This commit is contained in:
Serhey Popovych 2018-02-07 08:30:54 +02:00 committed by David Ahern
parent affb361785
commit ec89594080
2 changed files with 46 additions and 26 deletions

View File

@ -357,11 +357,12 @@ static int do_tunnels_list(struct ip6_tnl_parm2 *p)
while (fgets(buf, sizeof(buf), fp) != NULL) { while (fgets(buf, sizeof(buf), fp) != NULL) {
char name[IFNAMSIZ]; char name[IFNAMSIZ];
int index, type; int index, type;
struct ip6_tnl_parm2 p1 = {}; struct ip6_tnl_parm2 p1;
char *ptr; char *ptr;
buf[sizeof(buf) - 1] = '\0'; buf[sizeof(buf) - 1] = '\0';
if ((ptr = strchr(buf, ':')) == NULL || ptr = strchr(buf, ':');
if (ptr == NULL ||
(*ptr++ = 0, sscanf(buf, "%s", name) != 1)) { (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
fprintf(stderr, "Wrong format for /proc/net/dev. Giving up.\n"); fprintf(stderr, "Wrong format for /proc/net/dev. Giving up.\n");
goto end; goto end;
@ -376,16 +377,19 @@ static int do_tunnels_list(struct ip6_tnl_parm2 *p)
fprintf(stderr, "Failed to get type of \"%s\"\n", name); fprintf(stderr, "Failed to get type of \"%s\"\n", name);
continue; continue;
} }
if (type != ARPHRD_TUNNEL6 && type != ARPHRD_IP6GRE) switch (type) {
case ARPHRD_TUNNEL6:
case ARPHRD_IP6GRE:
break;
default:
continue; continue;
}
ip6_tnl_parm_init(&p1, 0); ip6_tnl_parm_init(&p1, 0);
if (type == ARPHRD_IP6GRE) if (type == ARPHRD_IP6GRE)
p1.proto = IPPROTO_GRE; p1.proto = IPPROTO_GRE;
p1.link = index;
strcpy(p1.name, name); strcpy(p1.name, name);
p1.link = ll_name_to_index(p1.name); if (tnl_get_ioctl(name, &p1))
if (p1.link == 0)
continue;
if (tnl_get_ioctl(p1.name, &p1))
continue; continue;
if (!ip6_tnl_parm_match(p, &p1)) if (!ip6_tnl_parm_match(p, &p1))
continue; continue;
@ -396,7 +400,7 @@ static int do_tunnels_list(struct ip6_tnl_parm2 *p)
if (!tnl_get_stats(ptr, &s)) if (!tnl_get_stats(ptr, &s))
tnl_print_stats(&s); tnl_print_stats(&s);
} }
printf("\n"); fputc('\n', stdout);
} }
err = 0; err = 0;
end: end:
@ -416,14 +420,13 @@ static int do_show(int argc, char **argv)
return -1; return -1;
if (!p.name[0] || show_stats) if (!p.name[0] || show_stats)
do_tunnels_list(&p); return do_tunnels_list(&p);
else {
if (tnl_get_ioctl(p.name, &p))
return -1;
print_tunnel(&p);
printf("\n");
}
if (tnl_get_ioctl(p.name, &p))
return -1;
print_tunnel(&p);
fputc('\n', stdout);
return 0; return 0;
} }

View File

@ -373,6 +373,20 @@ static void print_tunnel(struct ip_tunnel_parm *p)
printf("%s Checksum output packets.", _SL_); printf("%s Checksum output packets.", _SL_);
} }
/*
* @p1: user specified parameter
* @p2: database entry
*/
static int ip_tunnel_parm_match(const struct ip_tunnel_parm *p1,
const struct ip_tunnel_parm *p2)
{
return ((!p1->link || p1->link == p2->link) &&
(!p1->name[0] || strcmp(p1->name, p2->name) == 0) &&
(!p1->iph.daddr || p1->iph.daddr == p2->iph.daddr) &&
(!p1->iph.saddr || p1->iph.saddr == p2->iph.saddr) &&
(!p1->i_key || p1->i_key == p2->i_key));
}
static int do_tunnels_list(struct ip_tunnel_parm *p) static int do_tunnels_list(struct ip_tunnel_parm *p)
{ {
char buf[512]; char buf[512];
@ -384,7 +398,7 @@ static int do_tunnels_list(struct ip_tunnel_parm *p)
return -1; return -1;
} }
/* skip header lines */ /* skip two lines at the begenning of the file */
if (!fgets(buf, sizeof(buf), fp) || if (!fgets(buf, sizeof(buf), fp) ||
!fgets(buf, sizeof(buf), fp)) { !fgets(buf, sizeof(buf), fp)) {
fprintf(stderr, "/proc/net/dev read error\n"); fprintf(stderr, "/proc/net/dev read error\n");
@ -394,10 +408,10 @@ static int do_tunnels_list(struct ip_tunnel_parm *p)
while (fgets(buf, sizeof(buf), fp) != NULL) { while (fgets(buf, sizeof(buf), fp) != NULL) {
char name[IFNAMSIZ]; char name[IFNAMSIZ];
int index, type; int index, type;
struct ip_tunnel_parm p1 = {}; struct ip_tunnel_parm p1;
char *ptr; char *ptr;
buf[sizeof(buf) - 1] = 0; buf[sizeof(buf) - 1] = '\0';
ptr = strchr(buf, ':'); ptr = strchr(buf, ':');
if (ptr == NULL || if (ptr == NULL ||
(*ptr++ = 0, sscanf(buf, "%s", name) != 1)) { (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
@ -414,15 +428,18 @@ static int do_tunnels_list(struct ip_tunnel_parm *p)
fprintf(stderr, "Failed to get type of \"%s\"\n", name); fprintf(stderr, "Failed to get type of \"%s\"\n", name);
continue; continue;
} }
if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT) switch (type) {
case ARPHRD_TUNNEL:
case ARPHRD_IPGRE:
case ARPHRD_SIT:
break;
default:
continue; continue;
}
memset(&p1, 0, sizeof(p1));
if (tnl_get_ioctl(name, &p1)) if (tnl_get_ioctl(name, &p1))
continue; continue;
if ((p->link && p1.link != p->link) || if (!ip_tunnel_parm_match(p, &p1))
(p->name[0] && strcmp(p1.name, p->name)) ||
(p->iph.daddr && p1.iph.daddr != p->iph.daddr) ||
(p->iph.saddr && p1.iph.saddr != p->iph.saddr) ||
(p->i_key && p1.i_key != p->i_key))
continue; continue;
print_tunnel(&p1); print_tunnel(&p1);
if (show_stats) { if (show_stats) {
@ -431,7 +448,7 @@ static int do_tunnels_list(struct ip_tunnel_parm *p)
if (!tnl_get_stats(ptr, &s)) if (!tnl_get_stats(ptr, &s))
tnl_print_stats(&s); tnl_print_stats(&s);
} }
printf("\n"); fputc('\n', stdout);
} }
err = 0; err = 0;
end: end:
@ -456,7 +473,7 @@ static int do_show(int argc, char **argv)
return -1; return -1;
print_tunnel(&p); print_tunnel(&p);
printf("\n"); fputc('\n', stdout);
return 0; return 0;
} }