lib/utils: introduce rt_addr_n2a_rta()
This simple macro eases calling rt_addr_n2a() with data from an rt_attr pointer. Signed-off-by: Phil Sutter <phil@nwl.cc>
This commit is contained in:
parent
d49f934c10
commit
7faf1588a7
|
|
@ -130,6 +130,8 @@ const char *format_host(int af, int lne, const void *addr);
|
||||||
const char *rt_addr_n2a_r(int af, int len, const void *addr,
|
const char *rt_addr_n2a_r(int af, int len, const void *addr,
|
||||||
char *buf, int buflen);
|
char *buf, int buflen);
|
||||||
const char *rt_addr_n2a(int af, int len, const void *addr);
|
const char *rt_addr_n2a(int af, int len, const void *addr);
|
||||||
|
#define rt_addr_n2a_rta(af, rta) \
|
||||||
|
rt_addr_n2a(af, RTA_PAYLOAD(rta), RTA_DATA(rta))
|
||||||
|
|
||||||
int read_family(const char *name);
|
int read_family(const char *name);
|
||||||
const char *family_name(int family);
|
const char *family_name(int family);
|
||||||
|
|
|
||||||
|
|
@ -422,9 +422,7 @@ static void bond_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
|
||||||
for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
|
for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
|
||||||
if (iptb[i])
|
if (iptb[i])
|
||||||
fprintf(f, "%s",
|
fprintf(f, "%s",
|
||||||
rt_addr_n2a(AF_INET,
|
rt_addr_n2a_rta(AF_INET, iptb[i]));
|
||||||
RTA_PAYLOAD(iptb[i]),
|
|
||||||
RTA_DATA(iptb[i])));
|
|
||||||
if (i < BOND_MAX_ARP_TARGETS-1 && iptb[i+1])
|
if (i < BOND_MAX_ARP_TARGETS-1 && iptb[i+1])
|
||||||
fprintf(f, ",");
|
fprintf(f, ",");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -123,16 +123,12 @@ int print_mroute(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
|
||||||
|
|
||||||
if (tb[RTA_SRC])
|
if (tb[RTA_SRC])
|
||||||
len = snprintf(obuf, sizeof(obuf),
|
len = snprintf(obuf, sizeof(obuf),
|
||||||
"(%s, ", rt_addr_n2a(family,
|
"(%s, ", rt_addr_n2a_rta(family, tb[RTA_SRC]));
|
||||||
RTA_PAYLOAD(tb[RTA_SRC]),
|
|
||||||
RTA_DATA(tb[RTA_SRC])));
|
|
||||||
else
|
else
|
||||||
len = sprintf(obuf, "(unknown, ");
|
len = sprintf(obuf, "(unknown, ");
|
||||||
if (tb[RTA_DST])
|
if (tb[RTA_DST])
|
||||||
snprintf(obuf + len, sizeof(obuf) - len,
|
snprintf(obuf + len, sizeof(obuf) - len,
|
||||||
"%s)", rt_addr_n2a(family,
|
"%s)", rt_addr_n2a_rta(family, tb[RTA_DST]));
|
||||||
RTA_PAYLOAD(tb[RTA_DST]),
|
|
||||||
RTA_DATA(tb[RTA_DST])));
|
|
||||||
else
|
else
|
||||||
snprintf(obuf + len, sizeof(obuf) - len, "unknown) ");
|
snprintf(obuf + len, sizeof(obuf) - len, "unknown) ");
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -71,19 +71,11 @@ int print_prefix(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
|
||||||
|
|
||||||
parse_rtattr(tb, RTA_MAX, RTM_RTA(prefix), len);
|
parse_rtattr(tb, RTA_MAX, RTM_RTA(prefix), len);
|
||||||
|
|
||||||
fprintf(fp, "prefix ");
|
|
||||||
|
|
||||||
if (tb[PREFIX_ADDRESS]) {
|
if (tb[PREFIX_ADDRESS]) {
|
||||||
struct in6_addr *pfx;
|
fprintf(fp, "prefix %s/%u",
|
||||||
|
rt_addr_n2a_rta(family, tb[PREFIX_ADDRESS]),
|
||||||
pfx = (struct in6_addr *)RTA_DATA(tb[PREFIX_ADDRESS]);
|
prefix->prefix_len);
|
||||||
|
|
||||||
fprintf(fp, "%s", rt_addr_n2a(family,
|
|
||||||
RTA_PAYLOAD(tb[PREFIX_ADDRESS]),
|
|
||||||
pfx));
|
|
||||||
}
|
}
|
||||||
fprintf(fp, "/%u ", prefix->prefix_len);
|
|
||||||
|
|
||||||
fprintf(fp, "dev %s ", ll_index_to_name(prefix->prefix_ifindex));
|
fprintf(fp, "dev %s ", ll_index_to_name(prefix->prefix_ifindex));
|
||||||
|
|
||||||
if (prefix->prefix_flags & IF_PREFIX_ONLINK)
|
if (prefix->prefix_flags & IF_PREFIX_ONLINK)
|
||||||
|
|
|
||||||
20
ip/iproute.c
20
ip/iproute.c
|
|
@ -370,11 +370,9 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
|
||||||
|
|
||||||
if (tb[RTA_DST]) {
|
if (tb[RTA_DST]) {
|
||||||
if (r->rtm_dst_len != host_len) {
|
if (r->rtm_dst_len != host_len) {
|
||||||
fprintf(fp, "%s/%u ", rt_addr_n2a(r->rtm_family,
|
fprintf(fp, "%s/%u ",
|
||||||
RTA_PAYLOAD(tb[RTA_DST]),
|
rt_addr_n2a_rta(r->rtm_family, tb[RTA_DST]),
|
||||||
RTA_DATA(tb[RTA_DST])),
|
r->rtm_dst_len);
|
||||||
r->rtm_dst_len
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
fprintf(fp, "%s ",
|
fprintf(fp, "%s ",
|
||||||
format_host_rta(r->rtm_family, tb[RTA_DST]));
|
format_host_rta(r->rtm_family, tb[RTA_DST]));
|
||||||
|
|
@ -386,11 +384,9 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
|
||||||
}
|
}
|
||||||
if (tb[RTA_SRC]) {
|
if (tb[RTA_SRC]) {
|
||||||
if (r->rtm_src_len != host_len) {
|
if (r->rtm_src_len != host_len) {
|
||||||
fprintf(fp, "from %s/%u ", rt_addr_n2a(r->rtm_family,
|
fprintf(fp, "from %s/%u ",
|
||||||
RTA_PAYLOAD(tb[RTA_SRC]),
|
rt_addr_n2a_rta(r->rtm_family, tb[RTA_SRC]),
|
||||||
RTA_DATA(tb[RTA_SRC])),
|
r->rtm_src_len);
|
||||||
r->rtm_src_len
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
fprintf(fp, "from %s ",
|
fprintf(fp, "from %s ",
|
||||||
format_host_rta(r->rtm_family, tb[RTA_SRC]));
|
format_host_rta(r->rtm_family, tb[RTA_SRC]));
|
||||||
|
|
@ -439,9 +435,7 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
|
||||||
and symbolic name will not be useful.
|
and symbolic name will not be useful.
|
||||||
*/
|
*/
|
||||||
fprintf(fp, " src %s ",
|
fprintf(fp, " src %s ",
|
||||||
rt_addr_n2a(r->rtm_family,
|
rt_addr_n2a_rta(r->rtm_family, tb[RTA_PREFSRC]));
|
||||||
RTA_PAYLOAD(tb[RTA_PREFSRC]),
|
|
||||||
RTA_DATA(tb[RTA_PREFSRC])));
|
|
||||||
}
|
}
|
||||||
if (tb[RTA_PRIORITY])
|
if (tb[RTA_PRIORITY])
|
||||||
fprintf(fp, " metric %u ", rta_getattr_u32(tb[RTA_PRIORITY]));
|
fprintf(fp, " metric %u ", rta_getattr_u32(tb[RTA_PRIORITY]));
|
||||||
|
|
|
||||||
|
|
@ -77,15 +77,11 @@ static void print_encap_ip(FILE *fp, struct rtattr *encap)
|
||||||
|
|
||||||
if (tb[LWTUNNEL_IP_SRC])
|
if (tb[LWTUNNEL_IP_SRC])
|
||||||
fprintf(fp, "src %s ",
|
fprintf(fp, "src %s ",
|
||||||
rt_addr_n2a(AF_INET,
|
rt_addr_n2a_rta(AF_INET, tb[LWTUNNEL_IP_SRC]));
|
||||||
RTA_PAYLOAD(tb[LWTUNNEL_IP_SRC]),
|
|
||||||
RTA_DATA(tb[LWTUNNEL_IP_SRC])));
|
|
||||||
|
|
||||||
if (tb[LWTUNNEL_IP_DST])
|
if (tb[LWTUNNEL_IP_DST])
|
||||||
fprintf(fp, "dst %s ",
|
fprintf(fp, "dst %s ",
|
||||||
rt_addr_n2a(AF_INET,
|
rt_addr_n2a_rta(AF_INET, tb[LWTUNNEL_IP_DST]));
|
||||||
RTA_PAYLOAD(tb[LWTUNNEL_IP_DST]),
|
|
||||||
RTA_DATA(tb[LWTUNNEL_IP_DST])));
|
|
||||||
|
|
||||||
if (tb[LWTUNNEL_IP_TTL])
|
if (tb[LWTUNNEL_IP_TTL])
|
||||||
fprintf(fp, "ttl %d ", rta_getattr_u8(tb[LWTUNNEL_IP_TTL]));
|
fprintf(fp, "ttl %d ", rta_getattr_u8(tb[LWTUNNEL_IP_TTL]));
|
||||||
|
|
@ -112,7 +108,6 @@ static void print_encap_ila(FILE *fp, struct rtattr *encap)
|
||||||
static void print_encap_ip6(FILE *fp, struct rtattr *encap)
|
static void print_encap_ip6(FILE *fp, struct rtattr *encap)
|
||||||
{
|
{
|
||||||
struct rtattr *tb[LWTUNNEL_IP6_MAX+1];
|
struct rtattr *tb[LWTUNNEL_IP6_MAX+1];
|
||||||
char abuf[256];
|
|
||||||
|
|
||||||
parse_rtattr_nested(tb, LWTUNNEL_IP6_MAX, encap);
|
parse_rtattr_nested(tb, LWTUNNEL_IP6_MAX, encap);
|
||||||
|
|
||||||
|
|
@ -121,17 +116,11 @@ static void print_encap_ip6(FILE *fp, struct rtattr *encap)
|
||||||
|
|
||||||
if (tb[LWTUNNEL_IP6_SRC])
|
if (tb[LWTUNNEL_IP6_SRC])
|
||||||
fprintf(fp, "src %s ",
|
fprintf(fp, "src %s ",
|
||||||
rt_addr_n2a(AF_INET6,
|
rt_addr_n2a_rta(AF_INET6, tb[LWTUNNEL_IP6_SRC]));
|
||||||
RTA_PAYLOAD(tb[LWTUNNEL_IP6_SRC]),
|
|
||||||
RTA_DATA(tb[LWTUNNEL_IP6_SRC]),
|
|
||||||
abuf, sizeof(abuf)));
|
|
||||||
|
|
||||||
if (tb[LWTUNNEL_IP6_DST])
|
if (tb[LWTUNNEL_IP6_DST])
|
||||||
fprintf(fp, "dst %s ",
|
fprintf(fp, "dst %s ",
|
||||||
rt_addr_n2a(AF_INET6,
|
rt_addr_n2a_rta(AF_INET6, tb[LWTUNNEL_IP6_DST]));
|
||||||
RTA_PAYLOAD(tb[LWTUNNEL_IP6_DST]),
|
|
||||||
RTA_DATA(tb[LWTUNNEL_IP6_DST]),
|
|
||||||
abuf, sizeof(abuf)));
|
|
||||||
|
|
||||||
if (tb[LWTUNNEL_IP6_HOPLIMIT])
|
if (tb[LWTUNNEL_IP6_HOPLIMIT])
|
||||||
fprintf(fp, "hoplimit %d ", rta_getattr_u8(tb[LWTUNNEL_IP6_HOPLIMIT]));
|
fprintf(fp, "hoplimit %d ", rta_getattr_u8(tb[LWTUNNEL_IP6_HOPLIMIT]));
|
||||||
|
|
|
||||||
16
ip/iprule.c
16
ip/iprule.c
|
|
@ -83,11 +83,9 @@ int print_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
|
||||||
|
|
||||||
if (tb[FRA_SRC]) {
|
if (tb[FRA_SRC]) {
|
||||||
if (r->rtm_src_len != host_len) {
|
if (r->rtm_src_len != host_len) {
|
||||||
fprintf(fp, "from %s/%u ", rt_addr_n2a(r->rtm_family,
|
fprintf(fp, "from %s/%u ",
|
||||||
RTA_PAYLOAD(tb[FRA_SRC]),
|
rt_addr_n2a_rta(r->rtm_family, tb[FRA_SRC]),
|
||||||
RTA_DATA(tb[FRA_SRC])),
|
r->rtm_src_len);
|
||||||
r->rtm_src_len
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
fprintf(fp, "from %s ",
|
fprintf(fp, "from %s ",
|
||||||
format_host_rta(r->rtm_family, tb[FRA_SRC]));
|
format_host_rta(r->rtm_family, tb[FRA_SRC]));
|
||||||
|
|
@ -100,11 +98,9 @@ int print_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
|
||||||
|
|
||||||
if (tb[FRA_DST]) {
|
if (tb[FRA_DST]) {
|
||||||
if (r->rtm_dst_len != host_len) {
|
if (r->rtm_dst_len != host_len) {
|
||||||
fprintf(fp, "to %s/%u ", rt_addr_n2a(r->rtm_family,
|
fprintf(fp, "to %s/%u ",
|
||||||
RTA_PAYLOAD(tb[FRA_DST]),
|
rt_addr_n2a_rta(r->rtm_family, tb[FRA_DST]),
|
||||||
RTA_DATA(tb[FRA_DST])),
|
r->rtm_dst_len);
|
||||||
r->rtm_dst_len
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
fprintf(fp, "to %s ",
|
fprintf(fp, "to %s ",
|
||||||
format_host_rta(r->rtm_family, tb[FRA_DST]));
|
format_host_rta(r->rtm_family, tb[FRA_DST]));
|
||||||
|
|
|
||||||
|
|
@ -289,16 +289,12 @@ static void ip6tunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb
|
||||||
|
|
||||||
if (tb[IFLA_IPTUN_REMOTE]) {
|
if (tb[IFLA_IPTUN_REMOTE]) {
|
||||||
fprintf(f, "remote %s ",
|
fprintf(f, "remote %s ",
|
||||||
rt_addr_n2a(AF_INET6,
|
rt_addr_n2a_rta(AF_INET6, tb[IFLA_IPTUN_REMOTE]));
|
||||||
RTA_PAYLOAD(tb[IFLA_IPTUN_REMOTE]),
|
|
||||||
RTA_DATA(tb[IFLA_IPTUN_REMOTE])));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tb[IFLA_IPTUN_LOCAL]) {
|
if (tb[IFLA_IPTUN_LOCAL]) {
|
||||||
fprintf(f, "local %s ",
|
fprintf(f, "local %s ",
|
||||||
rt_addr_n2a(AF_INET6,
|
rt_addr_n2a_rta(AF_INET6, tb[IFLA_IPTUN_LOCAL]));
|
||||||
RTA_PAYLOAD(tb[IFLA_IPTUN_LOCAL]),
|
|
||||||
RTA_DATA(tb[IFLA_IPTUN_LOCAL])));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tb[IFLA_IPTUN_LINK] && rta_getattr_u32(tb[IFLA_IPTUN_LINK])) {
|
if (tb[IFLA_IPTUN_LINK] && rta_getattr_u32(tb[IFLA_IPTUN_LINK])) {
|
||||||
|
|
|
||||||
|
|
@ -415,16 +415,12 @@ static void flower_print_ip_addr(FILE *f, char *name, __be16 eth_type,
|
||||||
}
|
}
|
||||||
if (!addr_attr || RTA_PAYLOAD(addr_attr) != len)
|
if (!addr_attr || RTA_PAYLOAD(addr_attr) != len)
|
||||||
return;
|
return;
|
||||||
fprintf(f, "\n %s %s", name, rt_addr_n2a(family,
|
fprintf(f, "\n %s %s", name, rt_addr_n2a_rta(family, addr_attr));
|
||||||
RTA_PAYLOAD(addr_attr),
|
|
||||||
RTA_DATA(addr_attr)));
|
|
||||||
if (!mask_attr || RTA_PAYLOAD(mask_attr) != len)
|
if (!mask_attr || RTA_PAYLOAD(mask_attr) != len)
|
||||||
return;
|
return;
|
||||||
bits = __mask_bits(RTA_DATA(mask_attr), len);
|
bits = __mask_bits(RTA_DATA(mask_attr), len);
|
||||||
if (bits < 0)
|
if (bits < 0)
|
||||||
fprintf(f, "/%s", rt_addr_n2a(family,
|
fprintf(f, "/%s", rt_addr_n2a_rta(family, mask_attr));
|
||||||
RTA_PAYLOAD(mask_attr),
|
|
||||||
RTA_DATA(mask_attr)));
|
|
||||||
else if (bits < len * 8)
|
else if (bits < len * 8)
|
||||||
fprintf(f, "/%d", bits);
|
fprintf(f, "/%d", bits);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue