libiproute: use stdout directly instead of passing it as a parameter

function                                             old     new   delta
iprule_list                                           86      82      -4
print_rule                                           860     791     -69
ipaddr_list_or_flush                                2484    2384    -100
print_addrinfo                                      1498    1292    -206
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/4 up/down: 0/-379)           Total: -379 bytes
This commit is contained in:
Denis Vlasenko 2008-06-05 05:00:24 +00:00
parent b6fe10091d
commit bedfabd500
2 changed files with 75 additions and 79 deletions

View File

@ -39,13 +39,13 @@ typedef struct filter_t {
#define filter (*(filter_t*)&bb_common_bufsiz1)
static void print_link_flags(FILE *fp, unsigned flags, unsigned mdown)
static void print_link_flags(unsigned flags, unsigned mdown)
{
fprintf(fp, "<");
printf("<");
flags &= ~IFF_RUNNING;
#define _PF(f) if (flags & IFF_##f) { \
flags &= ~IFF_##f; \
fprintf(fp, #f "%s", flags ? "," : ""); }
printf(#f "%s", flags ? "," : ""); }
_PF(LOOPBACK);
_PF(BROADCAST);
_PF(POINTOPOINT);
@ -65,10 +65,10 @@ static void print_link_flags(FILE *fp, unsigned flags, unsigned mdown)
_PF(UP);
#undef _PF
if (flags)
fprintf(fp, "%x", flags);
printf("%x", flags);
if (mdown)
fprintf(fp, ",M-DOWN");
fprintf(fp, "> ");
printf(",M-DOWN");
printf("> ");
}
static void print_queuelen(char *name)
@ -92,10 +92,8 @@ static void print_queuelen(char *name)
printf("qlen %d", ifr.ifr_qlen);
}
static int print_linkinfo(struct sockaddr_nl ATTRIBUTE_UNUSED *who,
const struct nlmsghdr *n, void ATTRIBUTE_UNUSED *arg)
static int print_linkinfo(const struct nlmsghdr *n)
{
FILE *fp = (FILE*)arg;
struct ifinfomsg *ifi = NLMSG_DATA(n);
struct rtattr * tb[IFLA_MAX+1];
int len = n->nlmsg_len;
@ -127,34 +125,34 @@ static int print_linkinfo(struct sockaddr_nl ATTRIBUTE_UNUSED *who,
}
if (n->nlmsg_type == RTM_DELLINK)
fprintf(fp, "Deleted ");
printf("Deleted ");
fprintf(fp, "%d: %s", ifi->ifi_index,
printf("%d: %s", ifi->ifi_index,
tb[IFLA_IFNAME] ? (char*)RTA_DATA(tb[IFLA_IFNAME]) : "<nil>");
if (tb[IFLA_LINK]) {
SPRINT_BUF(b1);
int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]);
if (iflink == 0)
fprintf(fp, "@NONE: ");
printf("@NONE: ");
else {
fprintf(fp, "@%s: ", ll_idx_n2a(iflink, b1));
printf("@%s: ", ll_idx_n2a(iflink, b1));
m_flag = ll_index_to_flags(iflink);
m_flag = !(m_flag & IFF_UP);
}
} else {
fprintf(fp, ": ");
printf(": ");
}
print_link_flags(fp, ifi->ifi_flags, m_flag);
print_link_flags(ifi->ifi_flags, m_flag);
if (tb[IFLA_MTU])
fprintf(fp, "mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU]));
printf("mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU]));
if (tb[IFLA_QDISC])
fprintf(fp, "qdisc %s ", (char*)RTA_DATA(tb[IFLA_QDISC]));
printf("qdisc %s ", (char*)RTA_DATA(tb[IFLA_QDISC]));
#ifdef IFLA_MASTER
if (tb[IFLA_MASTER]) {
SPRINT_BUF(b1);
fprintf(fp, "master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1));
printf("master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1));
}
#endif
if (filter.showqueue)
@ -162,27 +160,27 @@ static int print_linkinfo(struct sockaddr_nl ATTRIBUTE_UNUSED *who,
if (!filter.family || filter.family == AF_PACKET) {
SPRINT_BUF(b1);
fprintf(fp, "%c link/%s ", _SL_, ll_type_n2a(ifi->ifi_type, b1, sizeof(b1)));
printf("%c link/%s ", _SL_, ll_type_n2a(ifi->ifi_type, b1, sizeof(b1)));
if (tb[IFLA_ADDRESS]) {
fputs(ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
RTA_PAYLOAD(tb[IFLA_ADDRESS]),
ifi->ifi_type,
b1, sizeof(b1)), fp);
b1, sizeof(b1)), stdout);
}
if (tb[IFLA_BROADCAST]) {
if (ifi->ifi_flags & IFF_POINTOPOINT)
fprintf(fp, " peer ");
printf(" peer ");
else
fprintf(fp, " brd ");
printf(" brd ");
fputs(ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]),
RTA_PAYLOAD(tb[IFLA_BROADCAST]),
ifi->ifi_type,
b1, sizeof(b1)), fp);
b1, sizeof(b1)), stdout);
}
}
fputc('\n', fp);
fflush(fp);
bb_putchar('\n');
/*fflush(stdout);*/
return 0;
}
@ -196,10 +194,9 @@ static int flush_update(void)
return 0;
}
static int print_addrinfo(struct sockaddr_nl ATTRIBUTE_UNUSED *who,
struct nlmsghdr *n, void ATTRIBUTE_UNUSED *arg)
static int print_addrinfo(struct sockaddr_nl *who ATTRIBUTE_UNUSED,
struct nlmsghdr *n, void *arg ATTRIBUTE_UNUSED)
{
FILE *fp = (FILE*)arg;
struct ifaddrmsg *ifa = NLMSG_DATA(n);
int len = n->nlmsg_len;
struct rtattr * rta_tb[IFA_MAX+1];
@ -268,28 +265,28 @@ static int print_addrinfo(struct sockaddr_nl ATTRIBUTE_UNUSED *who,
}
if (n->nlmsg_type == RTM_DELADDR)
fprintf(fp, "Deleted ");
printf("Deleted ");
if (filter.oneline)
fprintf(fp, "%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index));
printf("%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index));
if (ifa->ifa_family == AF_INET)
fprintf(fp, " inet ");
printf(" inet ");
else if (ifa->ifa_family == AF_INET6)
fprintf(fp, " inet6 ");
printf(" inet6 ");
else
fprintf(fp, " family %d ", ifa->ifa_family);
printf(" family %d ", ifa->ifa_family);
if (rta_tb[IFA_LOCAL]) {
fputs(rt_addr_n2a(ifa->ifa_family,
RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
RTA_DATA(rta_tb[IFA_LOCAL]),
abuf, sizeof(abuf)), fp);
abuf, sizeof(abuf)), stdout);
if (rta_tb[IFA_ADDRESS] == NULL ||
memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0) {
fprintf(fp, "/%d ", ifa->ifa_prefixlen);
printf("/%d ", ifa->ifa_prefixlen);
} else {
fprintf(fp, " peer %s/%d ",
printf(" peer %s/%d ",
rt_addr_n2a(ifa->ifa_family,
RTA_PAYLOAD(rta_tb[IFA_ADDRESS]),
RTA_DATA(rta_tb[IFA_ADDRESS]),
@ -299,44 +296,44 @@ static int print_addrinfo(struct sockaddr_nl ATTRIBUTE_UNUSED *who,
}
if (rta_tb[IFA_BROADCAST]) {
fprintf(fp, "brd %s ",
printf("brd %s ",
rt_addr_n2a(ifa->ifa_family,
RTA_PAYLOAD(rta_tb[IFA_BROADCAST]),
RTA_DATA(rta_tb[IFA_BROADCAST]),
abuf, sizeof(abuf)));
}
if (rta_tb[IFA_ANYCAST]) {
fprintf(fp, "any %s ",
printf("any %s ",
rt_addr_n2a(ifa->ifa_family,
RTA_PAYLOAD(rta_tb[IFA_ANYCAST]),
RTA_DATA(rta_tb[IFA_ANYCAST]),
abuf, sizeof(abuf)));
}
fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1)));
printf("scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1)));
if (ifa->ifa_flags & IFA_F_SECONDARY) {
ifa->ifa_flags &= ~IFA_F_SECONDARY;
fprintf(fp, "secondary ");
printf("secondary ");
}
if (ifa->ifa_flags & IFA_F_TENTATIVE) {
ifa->ifa_flags &= ~IFA_F_TENTATIVE;
fprintf(fp, "tentative ");
printf("tentative ");
}
if (ifa->ifa_flags & IFA_F_DEPRECATED) {
ifa->ifa_flags &= ~IFA_F_DEPRECATED;
fprintf(fp, "deprecated ");
printf("deprecated ");
}
if (!(ifa->ifa_flags & IFA_F_PERMANENT)) {
fprintf(fp, "dynamic ");
printf("dynamic ");
} else
ifa->ifa_flags &= ~IFA_F_PERMANENT;
if (ifa->ifa_flags)
fprintf(fp, "flags %02x ", ifa->ifa_flags);
printf("flags %02x ", ifa->ifa_flags);
if (rta_tb[IFA_LABEL])
fputs((char*)RTA_DATA(rta_tb[IFA_LABEL]), fp);
fputs((char*)RTA_DATA(rta_tb[IFA_LABEL]), stdout);
if (rta_tb[IFA_CACHEINFO]) {
struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
char buf[128];
fputc(_SL_, fp);
bb_putchar(_SL_);
if (ci->ifa_valid == 0xFFFFFFFFU)
sprintf(buf, "valid_lft forever");
else
@ -345,10 +342,10 @@ static int print_addrinfo(struct sockaddr_nl ATTRIBUTE_UNUSED *who,
sprintf(buf+strlen(buf), " preferred_lft forever");
else
sprintf(buf+strlen(buf), " preferred_lft %dsec", ci->ifa_prefered);
fprintf(fp, " %s", buf);
printf(" %s", buf);
}
fputc('\n', fp);
fflush(fp);
bb_putchar('\n');
/*fflush(stdout);*/
return 0;
}
@ -359,7 +356,7 @@ struct nlmsg_list
struct nlmsghdr h;
};
static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo, FILE *fp)
static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo)
{
for (; ainfo; ainfo = ainfo->next) {
struct nlmsghdr *n = &ainfo->h;
@ -375,7 +372,7 @@ static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo, FILE *
(filter.family && filter.family != ifa->ifa_family))
continue;
print_addrinfo(NULL, n, fp);
print_addrinfo(NULL, n, NULL);
}
return 0;
}
@ -498,7 +495,7 @@ int ipaddr_list_or_flush(char **argv, int flush)
for (;;) {
xrtnl_wilddump_request(&rth, filter.family, RTM_GETADDR);
filter.flushed = 0;
xrtnl_dump_filter(&rth, print_addrinfo, stdout);
xrtnl_dump_filter(&rth, print_addrinfo, NULL);
if (filter.flushed == 0) {
return 0;
}
@ -574,10 +571,10 @@ int ipaddr_list_or_flush(char **argv, int flush)
}
for (l = linfo; l; l = l->next) {
if (no_link || print_linkinfo(NULL, &l->h, stdout) == 0) {
if (no_link || print_linkinfo(&l->h) == 0) {
struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
if (filter.family != AF_PACKET)
print_selected_addrinfo(ifi->ifi_index, ainfo, stdout);
print_selected_addrinfo(ifi->ifi_index, ainfo);
}
}

View File

@ -41,9 +41,8 @@ static void usage(void)
*/
static int print_rule(struct sockaddr_nl *who ATTRIBUTE_UNUSED,
struct nlmsghdr *n, void *arg)
struct nlmsghdr *n, void *arg ATTRIBUTE_UNUSED)
{
FILE *fp = (FILE*)arg;
struct rtmsg *r = NLMSG_DATA(n);
int len = n->nlmsg_len;
int host_len = -1;
@ -71,14 +70,14 @@ static int print_rule(struct sockaddr_nl *who ATTRIBUTE_UNUSED,
host_len = 80;
*/
if (tb[RTA_PRIORITY])
fprintf(fp, "%u:\t", *(unsigned*)RTA_DATA(tb[RTA_PRIORITY]));
printf("%u:\t", *(unsigned*)RTA_DATA(tb[RTA_PRIORITY]));
else
fprintf(fp, "0:\t");
printf("0:\t");
fprintf(fp, "from ");
printf("from ");
if (tb[RTA_SRC]) {
if (r->rtm_src_len != host_len) {
fprintf(fp, "%s/%u", rt_addr_n2a(r->rtm_family,
printf("%s/%u", rt_addr_n2a(r->rtm_family,
RTA_PAYLOAD(tb[RTA_SRC]),
RTA_DATA(tb[RTA_SRC]),
abuf, sizeof(abuf)),
@ -88,73 +87,73 @@ static int print_rule(struct sockaddr_nl *who ATTRIBUTE_UNUSED,
fputs(format_host(r->rtm_family,
RTA_PAYLOAD(tb[RTA_SRC]),
RTA_DATA(tb[RTA_SRC]),
abuf, sizeof(abuf)), fp);
abuf, sizeof(abuf)), stdout);
}
} else if (r->rtm_src_len) {
fprintf(fp, "0/%d", r->rtm_src_len);
printf("0/%d", r->rtm_src_len);
} else {
fprintf(fp, "all");
printf("all");
}
fprintf(fp, " ");
bb_putchar(' ');
if (tb[RTA_DST]) {
if (r->rtm_dst_len != host_len) {
fprintf(fp, "to %s/%u ", rt_addr_n2a(r->rtm_family,
printf("to %s/%u ", rt_addr_n2a(r->rtm_family,
RTA_PAYLOAD(tb[RTA_DST]),
RTA_DATA(tb[RTA_DST]),
abuf, sizeof(abuf)),
r->rtm_dst_len
);
} else {
fprintf(fp, "to %s ", format_host(r->rtm_family,
printf("to %s ", format_host(r->rtm_family,
RTA_PAYLOAD(tb[RTA_DST]),
RTA_DATA(tb[RTA_DST]),
abuf, sizeof(abuf)));
}
} else if (r->rtm_dst_len) {
fprintf(fp, "to 0/%d ", r->rtm_dst_len);
printf("to 0/%d ", r->rtm_dst_len);
}
if (r->rtm_tos) {
fprintf(fp, "tos %s ", rtnl_dsfield_n2a(r->rtm_tos, b1, sizeof(b1)));
printf("tos %s ", rtnl_dsfield_n2a(r->rtm_tos, b1, sizeof(b1)));
}
if (tb[RTA_PROTOINFO]) {
fprintf(fp, "fwmark %#x ", *(uint32_t*)RTA_DATA(tb[RTA_PROTOINFO]));
printf("fwmark %#x ", *(uint32_t*)RTA_DATA(tb[RTA_PROTOINFO]));
}
if (tb[RTA_IIF]) {
fprintf(fp, "iif %s ", (char*)RTA_DATA(tb[RTA_IIF]));
printf("iif %s ", (char*)RTA_DATA(tb[RTA_IIF]));
}
if (r->rtm_table)
fprintf(fp, "lookup %s ", rtnl_rttable_n2a(r->rtm_table, b1, sizeof(b1)));
printf("lookup %s ", rtnl_rttable_n2a(r->rtm_table, b1, sizeof(b1)));
if (tb[RTA_FLOW]) {
uint32_t to = *(uint32_t*)RTA_DATA(tb[RTA_FLOW]);
uint32_t from = to>>16;
to &= 0xFFFF;
if (from) {
fprintf(fp, "realms %s/",
printf("realms %s/",
rtnl_rtrealm_n2a(from, b1, sizeof(b1)));
}
fprintf(fp, "%s ",
printf("%s ",
rtnl_rtrealm_n2a(to, b1, sizeof(b1)));
}
if (r->rtm_type == RTN_NAT) {
if (tb[RTA_GATEWAY]) {
fprintf(fp, "map-to %s ",
printf("map-to %s ",
format_host(r->rtm_family,
RTA_PAYLOAD(tb[RTA_GATEWAY]),
RTA_DATA(tb[RTA_GATEWAY]),
abuf, sizeof(abuf)));
} else
fprintf(fp, "masquerade");
printf("masquerade");
} else if (r->rtm_type != RTN_UNICAST)
fputs(rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1)), fp);
fputs(rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1)), stdout);
fputc('\n', fp);
fflush(fp);
bb_putchar('\n');
/*fflush(stdout);*/
return 0;
}
@ -176,7 +175,7 @@ static int iprule_list(char **argv)
xrtnl_open(&rth);
xrtnl_wilddump_request(&rth, af, RTM_GETRULE);
xrtnl_dump_filter(&rth, print_rule, stdout);
xrtnl_dump_filter(&rth, print_rule, NULL);
return 0;
}