📄 ipaddress.c
字号:
/* vi: set sw=4 ts=4: *//* * ipaddress.c "ip address". * * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. * * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> * * Changes: * Laszlo Valko <valko@linux.karinthy.hu> 990223: address label must be zero terminated */#include <fnmatch.h>#include <net/if.h>#include <net/if_arp.h>#include "ip_common.h" /* #include "libbb.h" is inside */#include "rt_names.h"#include "utils.h"#ifndef IFF_LOWER_UP/* from linux/if.h */#define IFF_LOWER_UP 0x10000 /* driver signals L1 up*/#endiftypedef struct filter_t { char *label; char *flushb; struct rtnl_handle *rth; int scope, scopemask; int flags, flagmask; int flushp; int flushe; int ifindex; family_t family; smallint showqueue; smallint oneline; smallint up; smallint flushed; inet_prefix pfx;} filter_t;#define filter (*(filter_t*)&bb_common_bufsiz1)static void print_link_flags(unsigned flags, unsigned mdown){ static const int flag_masks[] = { IFF_LOOPBACK, IFF_BROADCAST, IFF_POINTOPOINT, IFF_MULTICAST, IFF_NOARP, IFF_UP, IFF_LOWER_UP }; static const char flag_labels[] ALIGN1 = "LOOPBACK\0""BROADCAST\0""POINTOPOINT\0" "MULTICAST\0""NOARP\0""UP\0""LOWER_UP\0"; bb_putchar('<'); flags &= ~IFF_RUNNING;#if 0 _PF(ALLMULTI); _PF(PROMISC); _PF(MASTER); _PF(SLAVE); _PF(DEBUG); _PF(DYNAMIC); _PF(AUTOMEDIA); _PF(PORTSEL); _PF(NOTRAILERS);#endif flags = print_flags_separated(flag_masks, flag_labels, flags, ","); if (flags) printf("%x", flags); if (mdown) printf(",M-DOWN"); printf("> ");}static void print_queuelen(char *name){ struct ifreq ifr; int s; s = socket(AF_INET, SOCK_STREAM, 0); if (s < 0) return; memset(&ifr, 0, sizeof(ifr)); strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); if (ioctl_or_warn(s, SIOCGIFTXQLEN, &ifr) < 0) { close(s); return; } close(s); if (ifr.ifr_qlen) printf("qlen %d", ifr.ifr_qlen);}static int print_linkinfo(const struct nlmsghdr *n){ struct ifinfomsg *ifi = NLMSG_DATA(n); struct rtattr * tb[IFLA_MAX+1]; int len = n->nlmsg_len; unsigned m_flag = 0; if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK) return 0; len -= NLMSG_LENGTH(sizeof(*ifi)); if (len < 0) return -1; if (filter.ifindex && ifi->ifi_index != filter.ifindex) return 0; if (filter.up && !(ifi->ifi_flags & IFF_UP)) return 0; memset(tb, 0, sizeof(tb)); parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len); if (tb[IFLA_IFNAME] == NULL) { bb_error_msg("nil ifname"); return -1; } if (filter.label && (!filter.family || filter.family == AF_PACKET) && fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0) ) { return 0; } if (n->nlmsg_type == RTM_DELLINK) printf("Deleted "); 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) printf("@NONE: "); else { printf("@%s: ", ll_idx_n2a(iflink, b1)); m_flag = ll_index_to_flags(iflink); m_flag = !(m_flag & IFF_UP); } } else { printf(": "); } print_link_flags(ifi->ifi_flags, m_flag); if (tb[IFLA_MTU]) printf("mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU])); if (tb[IFLA_QDISC]) printf("qdisc %s ", (char*)RTA_DATA(tb[IFLA_QDISC]));#ifdef IFLA_MASTER if (tb[IFLA_MASTER]) { SPRINT_BUF(b1); printf("master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1)); }#endif if (filter.showqueue) print_queuelen((char*)RTA_DATA(tb[IFLA_IFNAME])); if (!filter.family || filter.family == AF_PACKET) { SPRINT_BUF(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)), stdout); } if (tb[IFLA_BROADCAST]) { if (ifi->ifi_flags & IFF_POINTOPOINT) printf(" peer "); else printf(" brd "); fputs(ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]), RTA_PAYLOAD(tb[IFLA_BROADCAST]), ifi->ifi_type, b1, sizeof(b1)), stdout); } } bb_putchar('\n'); /*fflush(stdout);*/ return 0;}static int flush_update(void){ if (rtnl_send(filter.rth, filter.flushb, filter.flushp) < 0) { bb_perror_msg("failed to send flush request"); return -1; } filter.flushp = 0; return 0;}static int print_addrinfo(const struct sockaddr_nl *who UNUSED_PARAM, struct nlmsghdr *n, void *arg UNUSED_PARAM){ struct ifaddrmsg *ifa = NLMSG_DATA(n); int len = n->nlmsg_len; struct rtattr * rta_tb[IFA_MAX+1]; char abuf[256]; SPRINT_BUF(b1); if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR) return 0; len -= NLMSG_LENGTH(sizeof(*ifa)); if (len < 0) { bb_error_msg("wrong nlmsg len %d", len); return -1; } if (filter.flushb && n->nlmsg_type != RTM_NEWADDR) return 0; memset(rta_tb, 0, sizeof(rta_tb)); parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa))); if (!rta_tb[IFA_LOCAL]) rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS]; if (!rta_tb[IFA_ADDRESS]) rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL]; if (filter.ifindex && filter.ifindex != ifa->ifa_index) return 0; if ((filter.scope ^ ifa->ifa_scope) & filter.scopemask) return 0; if ((filter.flags ^ ifa->ifa_flags) & filter.flagmask) return 0; if (filter.label) { const char *label; if (rta_tb[IFA_LABEL]) label = RTA_DATA(rta_tb[IFA_LABEL]); else label = ll_idx_n2a(ifa->ifa_index, b1); if (fnmatch(filter.label, label, 0) != 0) return 0; } if (filter.pfx.family) { if (rta_tb[IFA_LOCAL]) { inet_prefix dst; memset(&dst, 0, sizeof(dst)); dst.family = ifa->ifa_family; memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL])); if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen)) return 0; } } if (filter.flushb) { struct nlmsghdr *fn; if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) { if (flush_update()) return -1; } fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp)); memcpy(fn, n, n->nlmsg_len); fn->nlmsg_type = RTM_DELADDR; fn->nlmsg_flags = NLM_F_REQUEST; fn->nlmsg_seq = ++filter.rth->seq; filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb; filter.flushed = 1; return 0; } if (n->nlmsg_type == RTM_DELADDR) printf("Deleted "); if (filter.oneline) printf("%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index)); if (ifa->ifa_family == AF_INET) printf(" inet "); else if (ifa->ifa_family == AF_INET6) printf(" inet6 "); else 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)), stdout); if (rta_tb[IFA_ADDRESS] == NULL || memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0) { printf("/%d ", ifa->ifa_prefixlen); } else { printf(" peer %s/%d ", rt_addr_n2a(ifa->ifa_family, RTA_PAYLOAD(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_ADDRESS]), abuf, sizeof(abuf)), ifa->ifa_prefixlen); } } if (rta_tb[IFA_BROADCAST]) { 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]) { printf("any %s ", rt_addr_n2a(ifa->ifa_family, RTA_PAYLOAD(rta_tb[IFA_ANYCAST]), RTA_DATA(rta_tb[IFA_ANYCAST]), abuf, sizeof(abuf))); } 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; printf("secondary "); } if (ifa->ifa_flags & IFA_F_TENTATIVE) { ifa->ifa_flags &= ~IFA_F_TENTATIVE; printf("tentative "); } if (ifa->ifa_flags & IFA_F_DEPRECATED) { ifa->ifa_flags &= ~IFA_F_DEPRECATED; printf("deprecated "); } if (!(ifa->ifa_flags & IFA_F_PERMANENT)) { printf("dynamic "); } else ifa->ifa_flags &= ~IFA_F_PERMANENT; if (ifa->ifa_flags) printf("flags %02x ", ifa->ifa_flags); if (rta_tb[IFA_LABEL]) 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]; bb_putchar(_SL_); if (ci->ifa_valid == 0xFFFFFFFFU) sprintf(buf, "valid_lft forever"); else sprintf(buf, "valid_lft %dsec", ci->ifa_valid); if (ci->ifa_prefered == 0xFFFFFFFFU) sprintf(buf+strlen(buf), " preferred_lft forever"); else sprintf(buf+strlen(buf), " preferred_lft %dsec", ci->ifa_prefered); printf(" %s", buf); } bb_putchar('\n'); /*fflush(stdout);*/ return 0;}struct nlmsg_list{ struct nlmsg_list *next; struct nlmsghdr h;};static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo){ for (; ainfo; ainfo = ainfo->next) { struct nlmsghdr *n = &ainfo->h; struct ifaddrmsg *ifa = NLMSG_DATA(n); if (n->nlmsg_type != RTM_NEWADDR) continue; if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa))) return -1; if (ifa->ifa_index != ifindex || (filter.family && filter.family != ifa->ifa_family)) continue; print_addrinfo(NULL, n, NULL); } return 0;}static int store_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg){ struct nlmsg_list **linfo = (struct nlmsg_list**)arg; struct nlmsg_list *h; struct nlmsg_list **lp; h = malloc(n->nlmsg_len+sizeof(void*)); if (h == NULL) return -1; memcpy(&h->h, n, n->nlmsg_len);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -