libiproute: make rt_addr_n2a() and format_host() return auto strings
function old new delta rt_addr_n2a 56 53 -3 print_addrinfo 1227 1178 -49 print_neigh 933 881 -52 print_rule 689 617 -72 print_tunnel 640 560 -80 print_route 1727 1588 -139 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 0/6 up/down: 0/-395) Total: -395 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
@ -214,8 +214,7 @@ static int FAST_FUNC print_addrinfo(const struct sockaddr_nl *who UNUSED_PARAM,
|
|||||||
{
|
{
|
||||||
struct ifaddrmsg *ifa = NLMSG_DATA(n);
|
struct ifaddrmsg *ifa = NLMSG_DATA(n);
|
||||||
int len = n->nlmsg_len;
|
int len = n->nlmsg_len;
|
||||||
struct rtattr * rta_tb[IFA_MAX+1];
|
struct rtattr *rta_tb[IFA_MAX+1];
|
||||||
char abuf[256];
|
|
||||||
|
|
||||||
if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR)
|
if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR)
|
||||||
return 0;
|
return 0;
|
||||||
@ -291,9 +290,9 @@ static int FAST_FUNC print_addrinfo(const struct sockaddr_nl *who UNUSED_PARAM,
|
|||||||
printf(" family %d ", ifa->ifa_family);
|
printf(" family %d ", ifa->ifa_family);
|
||||||
|
|
||||||
if (rta_tb[IFA_LOCAL]) {
|
if (rta_tb[IFA_LOCAL]) {
|
||||||
fputs(rt_addr_n2a(ifa->ifa_family,
|
fputs(rt_addr_n2a(ifa->ifa_family, RTA_DATA(rta_tb[IFA_LOCAL])),
|
||||||
RTA_DATA(rta_tb[IFA_LOCAL]),
|
stdout
|
||||||
abuf, sizeof(abuf)), stdout);
|
);
|
||||||
|
|
||||||
if (rta_tb[IFA_ADDRESS] == NULL
|
if (rta_tb[IFA_ADDRESS] == NULL
|
||||||
|| memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0
|
|| memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0
|
||||||
@ -301,25 +300,22 @@ static int FAST_FUNC print_addrinfo(const struct sockaddr_nl *who UNUSED_PARAM,
|
|||||||
printf("/%d ", ifa->ifa_prefixlen);
|
printf("/%d ", ifa->ifa_prefixlen);
|
||||||
} else {
|
} else {
|
||||||
printf(" peer %s/%d ",
|
printf(" peer %s/%d ",
|
||||||
rt_addr_n2a(ifa->ifa_family,
|
rt_addr_n2a(ifa->ifa_family, RTA_DATA(rta_tb[IFA_ADDRESS])),
|
||||||
RTA_DATA(rta_tb[IFA_ADDRESS]),
|
ifa->ifa_prefixlen
|
||||||
abuf, sizeof(abuf)),
|
);
|
||||||
ifa->ifa_prefixlen);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rta_tb[IFA_BROADCAST]) {
|
if (rta_tb[IFA_BROADCAST]) {
|
||||||
printf("brd %s ",
|
printf("brd %s ",
|
||||||
rt_addr_n2a(ifa->ifa_family,
|
rt_addr_n2a(ifa->ifa_family,
|
||||||
RTA_DATA(rta_tb[IFA_BROADCAST]),
|
RTA_DATA(rta_tb[IFA_BROADCAST]))
|
||||||
abuf, sizeof(abuf))
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (rta_tb[IFA_ANYCAST]) {
|
if (rta_tb[IFA_ANYCAST]) {
|
||||||
printf("any %s ",
|
printf("any %s ",
|
||||||
rt_addr_n2a(ifa->ifa_family,
|
rt_addr_n2a(ifa->ifa_family,
|
||||||
RTA_DATA(rta_tb[IFA_ANYCAST]),
|
RTA_DATA(rta_tb[IFA_ANYCAST]))
|
||||||
abuf, sizeof(abuf))
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
printf("scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope));
|
printf("scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope));
|
||||||
|
@ -91,7 +91,6 @@ static int FAST_FUNC print_neigh(const struct sockaddr_nl *who UNUSED_PARAM,
|
|||||||
struct ndmsg *r = NLMSG_DATA(n);
|
struct ndmsg *r = NLMSG_DATA(n);
|
||||||
int len = n->nlmsg_len;
|
int len = n->nlmsg_len;
|
||||||
struct rtattr *tb[NDA_MAX+1];
|
struct rtattr *tb[NDA_MAX+1];
|
||||||
char abuf[256];
|
|
||||||
|
|
||||||
if (n->nlmsg_type != RTM_NEWNEIGH && n->nlmsg_type != RTM_DELNEIGH) {
|
if (n->nlmsg_type != RTM_NEWNEIGH && n->nlmsg_type != RTM_DELNEIGH) {
|
||||||
bb_error_msg_and_die("not RTM_NEWNEIGH: %08x %08x %08x",
|
bb_error_msg_and_die("not RTM_NEWNEIGH: %08x %08x %08x",
|
||||||
@ -155,8 +154,8 @@ static int FAST_FUNC print_neigh(const struct sockaddr_nl *who UNUSED_PARAM,
|
|||||||
printf("%s ",
|
printf("%s ",
|
||||||
format_host(r->ndm_family,
|
format_host(r->ndm_family,
|
||||||
RTA_PAYLOAD(tb[NDA_DST]),
|
RTA_PAYLOAD(tb[NDA_DST]),
|
||||||
RTA_DATA(tb[NDA_DST]),
|
RTA_DATA(tb[NDA_DST]))
|
||||||
abuf, sizeof(abuf)));
|
);
|
||||||
}
|
}
|
||||||
if (!G_filter.index && r->ndm_ifindex)
|
if (!G_filter.index && r->ndm_ifindex)
|
||||||
printf("dev %s ", ll_index_to_name(r->ndm_ifindex));
|
printf("dev %s ", ll_index_to_name(r->ndm_ifindex));
|
||||||
|
@ -61,7 +61,6 @@ static int FAST_FUNC print_route(const struct sockaddr_nl *who UNUSED_PARAM,
|
|||||||
struct rtmsg *r = NLMSG_DATA(n);
|
struct rtmsg *r = NLMSG_DATA(n);
|
||||||
int len = n->nlmsg_len;
|
int len = n->nlmsg_len;
|
||||||
struct rtattr *tb[RTA_MAX+1];
|
struct rtattr *tb[RTA_MAX+1];
|
||||||
char abuf[256];
|
|
||||||
inet_prefix dst;
|
inet_prefix dst;
|
||||||
inet_prefix src;
|
inet_prefix src;
|
||||||
int host_len = -1;
|
int host_len = -1;
|
||||||
@ -218,17 +217,15 @@ static int FAST_FUNC print_route(const struct sockaddr_nl *who UNUSED_PARAM,
|
|||||||
|
|
||||||
if (tb[RTA_DST]) {
|
if (tb[RTA_DST]) {
|
||||||
if (r->rtm_dst_len != host_len) {
|
if (r->rtm_dst_len != host_len) {
|
||||||
printf("%s/%u ", rt_addr_n2a(r->rtm_family,
|
printf("%s/%u ",
|
||||||
RTA_DATA(tb[RTA_DST]),
|
rt_addr_n2a(r->rtm_family, RTA_DATA(tb[RTA_DST])),
|
||||||
abuf, sizeof(abuf)),
|
r->rtm_dst_len
|
||||||
r->rtm_dst_len
|
);
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
printf("%s ", format_host(r->rtm_family,
|
printf("%s ", format_host(r->rtm_family,
|
||||||
RTA_PAYLOAD(tb[RTA_DST]),
|
RTA_PAYLOAD(tb[RTA_DST]),
|
||||||
RTA_DATA(tb[RTA_DST]),
|
RTA_DATA(tb[RTA_DST]))
|
||||||
abuf, sizeof(abuf))
|
);
|
||||||
);
|
|
||||||
}
|
}
|
||||||
} else if (r->rtm_dst_len) {
|
} else if (r->rtm_dst_len) {
|
||||||
printf("0/%d ", r->rtm_dst_len);
|
printf("0/%d ", r->rtm_dst_len);
|
||||||
@ -237,17 +234,15 @@ static int FAST_FUNC print_route(const struct sockaddr_nl *who UNUSED_PARAM,
|
|||||||
}
|
}
|
||||||
if (tb[RTA_SRC]) {
|
if (tb[RTA_SRC]) {
|
||||||
if (r->rtm_src_len != host_len) {
|
if (r->rtm_src_len != host_len) {
|
||||||
printf("from %s/%u ", rt_addr_n2a(r->rtm_family,
|
printf("from %s/%u ",
|
||||||
RTA_DATA(tb[RTA_SRC]),
|
rt_addr_n2a(r->rtm_family, RTA_DATA(tb[RTA_SRC])),
|
||||||
abuf, sizeof(abuf)),
|
r->rtm_src_len
|
||||||
r->rtm_src_len
|
);
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
printf("from %s ", format_host(r->rtm_family,
|
printf("from %s ", format_host(r->rtm_family,
|
||||||
RTA_PAYLOAD(tb[RTA_SRC]),
|
RTA_PAYLOAD(tb[RTA_SRC]),
|
||||||
RTA_DATA(tb[RTA_SRC]),
|
RTA_DATA(tb[RTA_SRC]))
|
||||||
abuf, sizeof(abuf))
|
);
|
||||||
);
|
|
||||||
}
|
}
|
||||||
} else if (r->rtm_src_len) {
|
} else if (r->rtm_src_len) {
|
||||||
printf("from 0/%u ", r->rtm_src_len);
|
printf("from 0/%u ", r->rtm_src_len);
|
||||||
@ -255,8 +250,8 @@ static int FAST_FUNC print_route(const struct sockaddr_nl *who UNUSED_PARAM,
|
|||||||
if (tb[RTA_GATEWAY] && G_filter.rvia.bitlen != host_len) {
|
if (tb[RTA_GATEWAY] && G_filter.rvia.bitlen != host_len) {
|
||||||
printf("via %s ", format_host(r->rtm_family,
|
printf("via %s ", format_host(r->rtm_family,
|
||||||
RTA_PAYLOAD(tb[RTA_GATEWAY]),
|
RTA_PAYLOAD(tb[RTA_GATEWAY]),
|
||||||
RTA_DATA(tb[RTA_GATEWAY]),
|
RTA_DATA(tb[RTA_GATEWAY]))
|
||||||
abuf, sizeof(abuf)));
|
);
|
||||||
}
|
}
|
||||||
if (tb[RTA_OIF]) {
|
if (tb[RTA_OIF]) {
|
||||||
printf("dev %s ", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_OIF])));
|
printf("dev %s ", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_OIF])));
|
||||||
@ -269,8 +264,7 @@ static int FAST_FUNC print_route(const struct sockaddr_nl *who UNUSED_PARAM,
|
|||||||
and symbolic name will not be useful.
|
and symbolic name will not be useful.
|
||||||
*/
|
*/
|
||||||
printf(" src %s ", rt_addr_n2a(r->rtm_family,
|
printf(" src %s ", rt_addr_n2a(r->rtm_family,
|
||||||
RTA_DATA(tb[RTA_PREFSRC]),
|
RTA_DATA(tb[RTA_PREFSRC])));
|
||||||
abuf, sizeof(abuf)));
|
|
||||||
}
|
}
|
||||||
if (tb[RTA_PRIORITY]) {
|
if (tb[RTA_PRIORITY]) {
|
||||||
printf(" metric %d ", *(uint32_t*)RTA_DATA(tb[RTA_PRIORITY]));
|
printf(" metric %d ", *(uint32_t*)RTA_DATA(tb[RTA_PRIORITY]));
|
||||||
|
@ -44,7 +44,6 @@ static int FAST_FUNC print_rule(const struct sockaddr_nl *who UNUSED_PARAM,
|
|||||||
int len = n->nlmsg_len;
|
int len = n->nlmsg_len;
|
||||||
int host_len = -1;
|
int host_len = -1;
|
||||||
struct rtattr * tb[RTA_MAX+1];
|
struct rtattr * tb[RTA_MAX+1];
|
||||||
char abuf[256];
|
|
||||||
|
|
||||||
if (n->nlmsg_type != RTM_NEWRULE)
|
if (n->nlmsg_type != RTM_NEWRULE)
|
||||||
return 0;
|
return 0;
|
||||||
@ -71,16 +70,14 @@ static int FAST_FUNC print_rule(const struct sockaddr_nl *who UNUSED_PARAM,
|
|||||||
printf("from ");
|
printf("from ");
|
||||||
if (tb[RTA_SRC]) {
|
if (tb[RTA_SRC]) {
|
||||||
if (r->rtm_src_len != host_len) {
|
if (r->rtm_src_len != host_len) {
|
||||||
printf("%s/%u", rt_addr_n2a(r->rtm_family,
|
printf("%s/%u",
|
||||||
RTA_DATA(tb[RTA_SRC]),
|
rt_addr_n2a(r->rtm_family, RTA_DATA(tb[RTA_SRC])),
|
||||||
abuf, sizeof(abuf)),
|
|
||||||
r->rtm_src_len
|
r->rtm_src_len
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
fputs(format_host(r->rtm_family,
|
fputs(format_host(r->rtm_family,
|
||||||
RTA_PAYLOAD(tb[RTA_SRC]),
|
RTA_PAYLOAD(tb[RTA_SRC]),
|
||||||
RTA_DATA(tb[RTA_SRC]),
|
RTA_DATA(tb[RTA_SRC])),
|
||||||
abuf, sizeof(abuf)),
|
|
||||||
stdout
|
stdout
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -94,15 +91,13 @@ static int FAST_FUNC print_rule(const struct sockaddr_nl *who UNUSED_PARAM,
|
|||||||
if (tb[RTA_DST]) {
|
if (tb[RTA_DST]) {
|
||||||
if (r->rtm_dst_len != host_len) {
|
if (r->rtm_dst_len != host_len) {
|
||||||
printf("to %s/%u ", rt_addr_n2a(r->rtm_family,
|
printf("to %s/%u ", rt_addr_n2a(r->rtm_family,
|
||||||
RTA_DATA(tb[RTA_DST]),
|
RTA_DATA(tb[RTA_DST])),
|
||||||
abuf, sizeof(abuf)),
|
|
||||||
r->rtm_dst_len
|
r->rtm_dst_len
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
printf("to %s ", format_host(r->rtm_family,
|
printf("to %s ", format_host(r->rtm_family,
|
||||||
RTA_PAYLOAD(tb[RTA_DST]),
|
RTA_PAYLOAD(tb[RTA_DST]),
|
||||||
RTA_DATA(tb[RTA_DST]),
|
RTA_DATA(tb[RTA_DST])));
|
||||||
abuf, sizeof(abuf)));
|
|
||||||
}
|
}
|
||||||
} else if (r->rtm_dst_len) {
|
} else if (r->rtm_dst_len) {
|
||||||
printf("to 0/%d ", r->rtm_dst_len);
|
printf("to 0/%d ", r->rtm_dst_len);
|
||||||
@ -139,8 +134,8 @@ static int FAST_FUNC print_rule(const struct sockaddr_nl *who UNUSED_PARAM,
|
|||||||
printf("map-to %s ",
|
printf("map-to %s ",
|
||||||
format_host(r->rtm_family,
|
format_host(r->rtm_family,
|
||||||
RTA_PAYLOAD(tb[RTA_GATEWAY]),
|
RTA_PAYLOAD(tb[RTA_GATEWAY]),
|
||||||
RTA_DATA(tb[RTA_GATEWAY]),
|
RTA_DATA(tb[RTA_GATEWAY]))
|
||||||
abuf, sizeof(abuf)));
|
);
|
||||||
} else
|
} else
|
||||||
printf("masquerade");
|
printf("masquerade");
|
||||||
} else if (r->rtm_type != RTN_UNICAST)
|
} else if (r->rtm_type != RTN_UNICAST)
|
||||||
|
@ -404,22 +404,18 @@ static int do_del(char **argv)
|
|||||||
|
|
||||||
static void print_tunnel(struct ip_tunnel_parm *p)
|
static void print_tunnel(struct ip_tunnel_parm *p)
|
||||||
{
|
{
|
||||||
char s1[256];
|
char s3[INET_ADDRSTRLEN];
|
||||||
char s2[256];
|
char s4[INET_ADDRSTRLEN];
|
||||||
char s3[64];
|
|
||||||
char s4[64];
|
|
||||||
|
|
||||||
format_host(AF_INET, 4, &p->iph.daddr, s1, sizeof(s1));
|
|
||||||
format_host(AF_INET, 4, &p->iph.saddr, s2, sizeof(s2));
|
|
||||||
inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3));
|
|
||||||
inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4));
|
|
||||||
|
|
||||||
printf("%s: %s/ip remote %s local %s ",
|
printf("%s: %s/ip remote %s local %s ",
|
||||||
p->name,
|
p->name,
|
||||||
p->iph.protocol == IPPROTO_IPIP ? "ip" :
|
p->iph.protocol == IPPROTO_IPIP ? "ip" :
|
||||||
(p->iph.protocol == IPPROTO_GRE ? "gre" :
|
p->iph.protocol == IPPROTO_GRE ? "gre" :
|
||||||
(p->iph.protocol == IPPROTO_IPV6 ? "ipv6" : "unknown")),
|
p->iph.protocol == IPPROTO_IPV6 ? "ipv6" :
|
||||||
p->iph.daddr ? s1 : "any", p->iph.saddr ? s2 : "any");
|
"unknown",
|
||||||
|
p->iph.daddr ? format_host(AF_INET, 4, &p->iph.daddr) : "any",
|
||||||
|
p->iph.saddr ? format_host(AF_INET, 4, &p->iph.saddr) : "any"
|
||||||
|
);
|
||||||
if (p->link) {
|
if (p->link) {
|
||||||
char *n = do_ioctl_get_ifname(p->link);
|
char *n = do_ioctl_get_ifname(p->link);
|
||||||
if (n) {
|
if (n) {
|
||||||
@ -442,9 +438,11 @@ static void print_tunnel(struct ip_tunnel_parm *p)
|
|||||||
if (!(p->iph.frag_off & htons(IP_DF)))
|
if (!(p->iph.frag_off & htons(IP_DF)))
|
||||||
printf(" nopmtudisc");
|
printf(" nopmtudisc");
|
||||||
|
|
||||||
|
inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3));
|
||||||
|
inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4));
|
||||||
if ((p->i_flags & GRE_KEY) && (p->o_flags & GRE_KEY) && p->o_key == p->i_key)
|
if ((p->i_flags & GRE_KEY) && (p->o_flags & GRE_KEY) && p->o_key == p->i_key)
|
||||||
printf(" key %s", s3);
|
printf(" key %s", s3);
|
||||||
else if ((p->i_flags | p->o_flags) & GRE_KEY) {
|
else {
|
||||||
if (p->i_flags & GRE_KEY)
|
if (p->i_flags & GRE_KEY)
|
||||||
printf(" ikey %s ", s3);
|
printf(" ikey %s ", s3);
|
||||||
if (p->o_flags & GRE_KEY)
|
if (p->o_flags & GRE_KEY)
|
||||||
|
@ -276,20 +276,21 @@ int inet_addr_match(const inet_prefix *a, const inet_prefix *b, int bits)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *rt_addr_n2a(int af,
|
const char *rt_addr_n2a(int af, void *addr)
|
||||||
void *addr, char *buf, int buflen)
|
|
||||||
{
|
{
|
||||||
switch (af) {
|
switch (af) {
|
||||||
case AF_INET:
|
case AF_INET:
|
||||||
case AF_INET6:
|
case AF_INET6:
|
||||||
return inet_ntop(af, addr, buf, buflen);
|
return inet_ntop(af, addr,
|
||||||
|
auto_string(xzalloc(INET6_ADDRSTRLEN)), INET6_ADDRSTRLEN
|
||||||
|
);
|
||||||
default:
|
default:
|
||||||
return "???";
|
return "???";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef RESOLVE_HOSTNAMES
|
#ifdef RESOLVE_HOSTNAMES
|
||||||
const char *format_host(int af, int len, void *addr, char *buf, int buflen)
|
const char *format_host(int af, int len, void *addr)
|
||||||
{
|
{
|
||||||
if (resolve_hosts) {
|
if (resolve_hosts) {
|
||||||
struct hostent *h_ent;
|
struct hostent *h_ent;
|
||||||
@ -308,11 +309,10 @@ const char *format_host(int af, int len, void *addr, char *buf, int buflen)
|
|||||||
if (len > 0) {
|
if (len > 0) {
|
||||||
h_ent = gethostbyaddr(addr, len, af);
|
h_ent = gethostbyaddr(addr, len, af);
|
||||||
if (h_ent != NULL) {
|
if (h_ent != NULL) {
|
||||||
safe_strncpy(buf, h_ent->h_name, buflen);
|
return auto_string(xstrdup(h_ent->h_name));
|
||||||
return buf;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return rt_addr_n2a(af, addr, buf, buflen);
|
return rt_addr_n2a(af, addr);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -66,12 +66,12 @@ extern unsigned get_unsigned(char *arg, const char *errmsg);
|
|||||||
extern uint32_t get_u32(char *arg, const char *errmsg);
|
extern uint32_t get_u32(char *arg, const char *errmsg);
|
||||||
extern uint16_t get_u16(char *arg, const char *errmsg);
|
extern uint16_t get_u16(char *arg, const char *errmsg);
|
||||||
|
|
||||||
extern const char *rt_addr_n2a(int af, void *addr, char *buf, int buflen);
|
extern const char *rt_addr_n2a(int af, void *addr);
|
||||||
#ifdef RESOLVE_HOSTNAMES
|
#ifdef RESOLVE_HOSTNAMES
|
||||||
extern const char *format_host(int af, int len, void *addr, char *buf, int buflen);
|
extern const char *format_host(int af, int len, void *addr);
|
||||||
#else
|
#else
|
||||||
#define format_host(af, len, addr, buf, buflen) \
|
#define format_host(af, len, addr) \
|
||||||
rt_addr_n2a(af, addr, buf, buflen)
|
rt_addr_n2a(af, addr)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void invarg_1_to_2(const char *, const char *) NORETURN;
|
void invarg_1_to_2(const char *, const char *) NORETURN;
|
||||||
|
Reference in New Issue
Block a user