Commit d49f934c authored by Phil Sutter's avatar Phil Sutter Committed by Stephen Hemminger

lib/utils: introduce format_host_rta()

This simple macro eases calling format_host() with data from an rt_attr
pointer.
Signed-off-by: default avatarPhil Sutter <phil@nwl.cc>
parent 2e96d2cc
...@@ -125,6 +125,8 @@ int af_byte_len(int af); ...@@ -125,6 +125,8 @@ int af_byte_len(int af);
const char *format_host_r(int af, int len, const void *addr, const char *format_host_r(int af, int len, const void *addr,
char *buf, int buflen); char *buf, int buflen);
const char *format_host(int af, int lne, const void *addr); const char *format_host(int af, int lne, const void *addr);
#define format_host_rta(af, rta) \
format_host(af, RTA_PAYLOAD(rta), RTA_DATA(rta))
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);
......
...@@ -1058,18 +1058,16 @@ int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n, ...@@ -1058,18 +1058,16 @@ int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n,
if (rta_tb[IFA_LOCAL]) { if (rta_tb[IFA_LOCAL]) {
color_fprintf(fp, ifa_family_color(ifa->ifa_family), "%s", color_fprintf(fp, ifa_family_color(ifa->ifa_family), "%s",
format_host(ifa->ifa_family, format_host_rta(ifa->ifa_family,
RTA_PAYLOAD(rta_tb[IFA_LOCAL]), rta_tb[IFA_LOCAL]));
RTA_DATA(rta_tb[IFA_LOCAL])));
if (rta_tb[IFA_ADDRESS] && if (rta_tb[IFA_ADDRESS] &&
memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]),
RTA_DATA(rta_tb[IFA_LOCAL]), RTA_DATA(rta_tb[IFA_LOCAL]),
ifa->ifa_family == AF_INET ? 4 : 16)) { ifa->ifa_family == AF_INET ? 4 : 16)) {
fprintf(fp, " peer "); fprintf(fp, " peer ");
color_fprintf(fp, ifa_family_color(ifa->ifa_family), color_fprintf(fp, ifa_family_color(ifa->ifa_family),
"%s", format_host(ifa->ifa_family, "%s", format_host_rta(ifa->ifa_family,
RTA_PAYLOAD(rta_tb[IFA_ADDRESS]), rta_tb[IFA_ADDRESS]));
RTA_DATA(rta_tb[IFA_ADDRESS])));
} }
fprintf(fp, "/%d ", ifa->ifa_prefixlen); fprintf(fp, "/%d ", ifa->ifa_prefixlen);
} }
...@@ -1080,16 +1078,14 @@ int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n, ...@@ -1080,16 +1078,14 @@ int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n,
if (rta_tb[IFA_BROADCAST]) { if (rta_tb[IFA_BROADCAST]) {
fprintf(fp, "brd "); fprintf(fp, "brd ");
color_fprintf(fp, ifa_family_color(ifa->ifa_family), "%s ", color_fprintf(fp, ifa_family_color(ifa->ifa_family), "%s ",
format_host(ifa->ifa_family, format_host_rta(ifa->ifa_family,
RTA_PAYLOAD(rta_tb[IFA_BROADCAST]), rta_tb[IFA_BROADCAST]));
RTA_DATA(rta_tb[IFA_BROADCAST])));
} }
if (rta_tb[IFA_ANYCAST]) { if (rta_tb[IFA_ANYCAST]) {
fprintf(fp, "any "); fprintf(fp, "any ");
color_fprintf(fp, ifa_family_color(ifa->ifa_family), "%s ", color_fprintf(fp, ifa_family_color(ifa->ifa_family), "%s ",
format_host(ifa->ifa_family, format_host_rta(ifa->ifa_family,
RTA_PAYLOAD(rta_tb[IFA_ANYCAST]), rta_tb[IFA_ANYCAST]));
RTA_DATA(rta_tb[IFA_ANYCAST])));
} }
fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1))); fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1)));
if (ifa_flags & IFA_F_SECONDARY) { if (ifa_flags & IFA_F_SECONDARY) {
......
...@@ -75,9 +75,8 @@ int print_addrlabel(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg ...@@ -75,9 +75,8 @@ int print_addrlabel(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg
if (tb[IFAL_ADDRESS]) { if (tb[IFAL_ADDRESS]) {
fprintf(fp, "prefix %s/%u ", fprintf(fp, "prefix %s/%u ",
format_host(ifal->ifal_family, format_host_rta(ifal->ifal_family,
RTA_PAYLOAD(tb[IFAL_ADDRESS]), tb[IFAL_ADDRESS]),
RTA_DATA(tb[IFAL_ADDRESS])),
ifal->ifal_prefixlen); ifal->ifal_prefixlen);
} }
......
...@@ -278,9 +278,7 @@ int print_neigh(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) ...@@ -278,9 +278,7 @@ int print_neigh(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
fprintf(fp, "miss "); fprintf(fp, "miss ");
if (tb[NDA_DST]) { if (tb[NDA_DST]) {
fprintf(fp, "%s ", fprintf(fp, "%s ",
format_host(r->ndm_family, format_host_rta(r->ndm_family, tb[NDA_DST]));
RTA_PAYLOAD(tb[NDA_DST]),
RTA_DATA(tb[NDA_DST])));
} }
if (!filter.index && r->ndm_ifindex) if (!filter.index && r->ndm_ifindex)
fprintf(fp, "dev %s ", ll_index_to_name(r->ndm_ifindex)); fprintf(fp, "dev %s ", ll_index_to_name(r->ndm_ifindex));
......
...@@ -376,10 +376,8 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) ...@@ -376,10 +376,8 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
r->rtm_dst_len r->rtm_dst_len
); );
} else { } else {
fprintf(fp, "%s ", format_host(r->rtm_family, fprintf(fp, "%s ",
RTA_PAYLOAD(tb[RTA_DST]), format_host_rta(r->rtm_family, tb[RTA_DST]));
RTA_DATA(tb[RTA_DST]))
);
} }
} else if (r->rtm_dst_len) { } else if (r->rtm_dst_len) {
fprintf(fp, "0/%d ", r->rtm_dst_len); fprintf(fp, "0/%d ", r->rtm_dst_len);
...@@ -394,19 +392,15 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) ...@@ -394,19 +392,15 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
r->rtm_src_len r->rtm_src_len
); );
} else { } else {
fprintf(fp, "from %s ", format_host(r->rtm_family, fprintf(fp, "from %s ",
RTA_PAYLOAD(tb[RTA_SRC]), format_host_rta(r->rtm_family, tb[RTA_SRC]));
RTA_DATA(tb[RTA_SRC]))
);
} }
} else if (r->rtm_src_len) { } else if (r->rtm_src_len) {
fprintf(fp, "from 0/%u ", r->rtm_src_len); fprintf(fp, "from 0/%u ", r->rtm_src_len);
} }
if (tb[RTA_NEWDST]) { if (tb[RTA_NEWDST]) {
fprintf(fp, "as to %s ", format_host(r->rtm_family, fprintf(fp, "as to %s ",
RTA_PAYLOAD(tb[RTA_NEWDST]), format_host_rta(r->rtm_family, tb[RTA_NEWDST]));
RTA_DATA(tb[RTA_NEWDST]))
);
} }
if (tb[RTA_ENCAP]) if (tb[RTA_ENCAP])
...@@ -419,9 +413,7 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) ...@@ -419,9 +413,7 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
if (tb[RTA_GATEWAY] && filter.rvia.bitlen != host_len) { if (tb[RTA_GATEWAY] && filter.rvia.bitlen != host_len) {
fprintf(fp, "via %s ", fprintf(fp, "via %s ",
format_host(r->rtm_family, format_host_rta(r->rtm_family, tb[RTA_GATEWAY]));
RTA_PAYLOAD(tb[RTA_GATEWAY]),
RTA_DATA(tb[RTA_GATEWAY])));
} }
if (tb[RTA_VIA]) { if (tb[RTA_VIA]) {
size_t len = RTA_PAYLOAD(tb[RTA_VIA]) - 2; size_t len = RTA_PAYLOAD(tb[RTA_VIA]) - 2;
...@@ -653,16 +645,13 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) ...@@ -653,16 +645,13 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
tb[RTA_ENCAP]); tb[RTA_ENCAP]);
if (tb[RTA_NEWDST]) { if (tb[RTA_NEWDST]) {
fprintf(fp, " as to %s ", fprintf(fp, " as to %s ",
format_host(r->rtm_family, format_host_rta(r->rtm_family,
RTA_PAYLOAD(tb[RTA_NEWDST]), tb[RTA_NEWDST]));
RTA_DATA(tb[RTA_NEWDST]),
abuf, sizeof(abuf)));
} }
if (tb[RTA_GATEWAY]) { if (tb[RTA_GATEWAY]) {
fprintf(fp, " via %s ", fprintf(fp, " via %s ",
format_host(r->rtm_family, format_host_rta(r->rtm_family,
RTA_PAYLOAD(tb[RTA_GATEWAY]), tb[RTA_GATEWAY]));
RTA_DATA(tb[RTA_GATEWAY])));
} }
if (tb[RTA_VIA]) { if (tb[RTA_VIA]) {
size_t len = RTA_PAYLOAD(tb[RTA_VIA]) - 2; size_t len = RTA_PAYLOAD(tb[RTA_VIA]) - 2;
......
...@@ -62,9 +62,8 @@ static void print_encap_mpls(FILE *fp, struct rtattr *encap) ...@@ -62,9 +62,8 @@ static void print_encap_mpls(FILE *fp, struct rtattr *encap)
parse_rtattr_nested(tb, MPLS_IPTUNNEL_MAX, encap); parse_rtattr_nested(tb, MPLS_IPTUNNEL_MAX, encap);
if (tb[MPLS_IPTUNNEL_DST]) if (tb[MPLS_IPTUNNEL_DST])
fprintf(fp, " %s ", format_host(AF_MPLS, fprintf(fp, " %s ",
RTA_PAYLOAD(tb[MPLS_IPTUNNEL_DST]), format_host_rta(AF_MPLS, tb[MPLS_IPTUNNEL_DST]));
RTA_DATA(tb[MPLS_IPTUNNEL_DST])));
} }
static void print_encap_ip(FILE *fp, struct rtattr *encap) static void print_encap_ip(FILE *fp, struct rtattr *encap)
......
...@@ -89,10 +89,8 @@ int print_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) ...@@ -89,10 +89,8 @@ int print_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
r->rtm_src_len r->rtm_src_len
); );
} else { } else {
fprintf(fp, "from %s ", format_host(r->rtm_family, fprintf(fp, "from %s ",
RTA_PAYLOAD(tb[FRA_SRC]), format_host_rta(r->rtm_family, tb[FRA_SRC]));
RTA_DATA(tb[FRA_SRC]))
);
} }
} else if (r->rtm_src_len) { } else if (r->rtm_src_len) {
fprintf(fp, "from 0/%d ", r->rtm_src_len); fprintf(fp, "from 0/%d ", r->rtm_src_len);
...@@ -108,9 +106,8 @@ int print_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) ...@@ -108,9 +106,8 @@ int print_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
r->rtm_dst_len r->rtm_dst_len
); );
} else { } else {
fprintf(fp, "to %s ", format_host(r->rtm_family, fprintf(fp, "to %s ",
RTA_PAYLOAD(tb[FRA_DST]), format_host_rta(r->rtm_family, tb[FRA_DST]));
RTA_DATA(tb[FRA_DST])));
} }
} else if (r->rtm_dst_len) { } else if (r->rtm_dst_len) {
fprintf(fp, "to 0/%d ", r->rtm_dst_len); fprintf(fp, "to 0/%d ", r->rtm_dst_len);
...@@ -183,9 +180,8 @@ int print_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) ...@@ -183,9 +180,8 @@ int print_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
if (r->rtm_type == RTN_NAT) { if (r->rtm_type == RTN_NAT) {
if (tb[RTA_GATEWAY]) { if (tb[RTA_GATEWAY]) {
fprintf(fp, "map-to %s ", fprintf(fp, "map-to %s ",
format_host(r->rtm_family, format_host_rta(r->rtm_family,
RTA_PAYLOAD(tb[RTA_GATEWAY]), tb[RTA_GATEWAY]));
RTA_DATA(tb[RTA_GATEWAY])));
} else } else
fprintf(fp, "masquerade"); fprintf(fp, "masquerade");
} else if (r->rtm_type == FR_ACT_GOTO) { } else if (r->rtm_type == FR_ACT_GOTO) {
......
...@@ -78,12 +78,9 @@ static int print_token(const struct sockaddr_nl *who, struct nlmsghdr *n, void * ...@@ -78,12 +78,9 @@ static int print_token(const struct sockaddr_nl *who, struct nlmsghdr *n, void *
return -1; return -1;
} }
fprintf(fp, "token %s ", fprintf(fp, "token %s dev %s\n",
format_host(ifi->ifi_family, format_host_rta(ifi->ifi_family, ltb[IFLA_INET6_TOKEN]),
RTA_PAYLOAD(ltb[IFLA_INET6_TOKEN]), ll_index_to_name(ifi->ifi_index));
RTA_DATA(ltb[IFLA_INET6_TOKEN])));
fprintf(fp, "dev %s ", ll_index_to_name(ifi->ifi_index));
fprintf(fp, "\n");
fflush(fp); fflush(fp);
return 0; return 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment