diff options
Diffstat (limited to 'freebsd/usr.sbin/arp/arp.c')
-rw-r--r-- | freebsd/usr.sbin/arp/arp.c | 159 |
1 files changed, 93 insertions, 66 deletions
diff --git a/freebsd/usr.sbin/arp/arp.c b/freebsd/usr.sbin/arp/arp.c index e3f28b97..75853e5e 100644 --- a/freebsd/usr.sbin/arp/arp.c +++ b/freebsd/usr.sbin/arp/arp.c @@ -90,6 +90,7 @@ __FBSDID("$FreeBSD$"); #include <string.h> #include <strings.h> #include <unistd.h> +#include <libxo/xo.h> #ifdef __rtems__ #include "rtems-bsd-arp-arp-data.h" static struct timespec tp; @@ -133,6 +134,8 @@ static struct if_nameindex *ifnameindex; #define SETFUNC(f) { if (func) usage(); func = (f); } +#define ARP_XO_VERSION "1" + #ifdef __rtems__ static int main(int argc, char *argv[]); @@ -172,6 +175,10 @@ main(int argc, char *argv[]) #define getopt(argc, argv, opt) getopt_r(argc, argv, "+" opt, &getopt_data) #endif /* __rtems__ */ + argc = xo_parse_args(argc, argv); + if (argc < 0) + exit(1); + while ((ch = getopt(argc, argv, "andfsSi:")) != -1) switch(ch) { case 'a': @@ -206,12 +213,13 @@ main(int argc, char *argv[]) func = F_GET; if (rifname) { if (func != F_GET && !(func == F_DELETE && aflag)) - errx(1, "-i not applicable to this operation"); + xo_errx(1, "-i not applicable to this operation"); if (if_nametoindex(rifname) == 0) { if (errno == ENXIO) - errx(1, "interface %s does not exist", rifname); + xo_errx(1, "interface %s does not exist", + rifname); else - err(1, "if_nametoindex(%s)", rifname); + xo_err(1, "if_nametoindex(%s)", rifname); } } switch (func) { @@ -219,7 +227,16 @@ main(int argc, char *argv[]) if (aflag) { if (argc != 0) usage(); + + xo_set_version(ARP_XO_VERSION); + xo_open_container("arp"); + xo_open_list("arp-cache"); + search(0, print_entry); + + xo_close_list("arp-cache"); + xo_close_container("arp"); + xo_finish(); } else { if (argc != 1) usage(); @@ -269,7 +286,7 @@ file(char *name) char line[100], arg[5][50], *args[5], *p; if ((fp = fopen(name, "r")) == NULL) - err(1, "cannot open %s", name); + xo_err(1, "cannot open %s", name); args[0] = &arg[0][0]; args[1] = &arg[1][0]; args[2] = &arg[2][0]; @@ -285,7 +302,7 @@ file(char *name) i = sscanf(p, "%49s %49s %49s %49s %49s", arg[0], arg[1], arg[2], arg[3], arg[4]); if (i < 2) { - warnx("bad line: %s", line); + xo_warnx("bad line: %s", line); retval = 1; continue; } @@ -313,7 +330,7 @@ getaddr(char *host) reply.sin_addr.s_addr = inet_addr(host); if (reply.sin_addr.s_addr == INADDR_NONE) { if (!(hp = gethostbyname(host))) { - warnx("%s: %s", host, hstrerror(h_errno)); + xo_warnx("%s: %s", host, hstrerror(h_errno)); return (NULL); } bcopy((char *)hp->h_addr, (char *)&reply.sin_addr, @@ -378,7 +395,7 @@ set(int argc, char **argv) clock_gettime(CLOCK_MONOTONIC, &tp); if (sysctlbyname("net.link.ether.inet.max_age", &max_age, &len, NULL, 0) != 0) - err(1, "sysctlbyname"); + xo_err(1, "sysctlbyname"); expire_time = tp.tv_sec + max_age; } else if (strcmp(argv[0], "pub") == 0) { flags |= RTF_ANNOUNCE; @@ -394,18 +411,18 @@ set(int argc, char **argv) } } else if (strcmp(argv[0], "blackhole") == 0) { if (flags & RTF_REJECT) { - errx(1, "Choose one of blackhole or reject, " + xo_errx(1, "Choose one of blackhole or reject, " "not both."); } flags |= RTF_BLACKHOLE; } else if (strcmp(argv[0], "reject") == 0) { if (flags & RTF_BLACKHOLE) { - errx(1, "Choose one of blackhole or reject, " + xo_errx(1, "Choose one of blackhole or reject, " "not both."); } flags |= RTF_REJECT; } else { - warnx("Invalid parameter '%s'", argv[0]); + xo_warnx("Invalid parameter '%s'", argv[0]); usage(); } argv++; @@ -413,7 +430,7 @@ set(int argc, char **argv) ea = (struct ether_addr *)LLADDR(&sdl_m); if (doing_proxy && !strcmp(eaddr, "auto")) { if (!get_ether_addr(dst->sin_addr.s_addr, ea)) { - warnx("no interface found for %s", + xo_warnx("no interface found for %s", inet_ntoa(dst->sin_addr)); return (1); } @@ -422,7 +439,7 @@ set(int argc, char **argv) struct ether_addr *ea1 = ether_aton(eaddr); if (ea1 == NULL) { - warnx("invalid Ethernet address '%s'", eaddr); + xo_warnx("invalid Ethernet address '%s'", eaddr); return (1); } else { *ea = *ea1; @@ -438,9 +455,9 @@ set(int argc, char **argv) * the prefix route covering the local end of the * PPP link should be returned, on which ARP applies. */ - rtm = rtmsg(RTM_GET, dst, &sdl_m); + rtm = rtmsg(RTM_GET, dst, NULL); if (rtm == NULL) { - warn("%s", host); + xo_warn("%s", host); return (1); } addr = (struct sockaddr_in *)(rtm + 1); @@ -449,7 +466,7 @@ set(int argc, char **argv) if ((sdl->sdl_family != AF_LINK) || (rtm->rtm_flags & RTF_GATEWAY) || !valid_type(sdl->sdl_type)) { - warnx("cannot intuit interface index and type for %s", host); + xo_warnx("cannot intuit interface index and type for %s", host); return (1); } sdl_m.sdl_type = sdl->sdl_type; @@ -464,19 +481,31 @@ static int get(char *host) { struct sockaddr_in *addr; + int found; addr = getaddr(host); if (addr == NULL) return (1); - if (0 == search(addr->sin_addr.s_addr, print_entry)) { - printf("%s (%s) -- no entry", + + xo_set_version(ARP_XO_VERSION); + xo_open_container("arp"); + xo_open_list("arp-cache"); + + found = search(addr->sin_addr.s_addr, print_entry); + + if (found == 0) { + xo_emit("{d:hostname/%s} ({d:ip-address/%s}) -- no entry", host, inet_ntoa(addr->sin_addr)); if (rifname) - printf(" on %s", rifname); - printf("\n"); - return (1); + xo_emit(" on {d:interface/%s}", rifname); + xo_emit("\n"); } - return (0); + + xo_close_list("arp-cache"); + xo_close_container("arp"); + xo_finish(); + + return (found == 0); } /* @@ -488,7 +517,6 @@ delete(char *host) struct sockaddr_in *addr, *dst; struct rt_msghdr *rtm; struct sockaddr_dl *sdl; - struct sockaddr_dl sdl_m; dst = getaddr(host); if (dst == NULL) @@ -499,19 +527,10 @@ delete(char *host) */ flags &= ~RTF_ANNOUNCE; - /* - * setup the data structure to notify the kernel - * it is the ARP entry the RTM_GET is interested - * in - */ - bzero(&sdl_m, sizeof(sdl_m)); - sdl_m.sdl_len = sizeof(sdl_m); - sdl_m.sdl_family = AF_LINK; - for (;;) { /* try twice */ - rtm = rtmsg(RTM_GET, dst, &sdl_m); + rtm = rtmsg(RTM_GET, dst, NULL); if (rtm == NULL) { - warn("%s", host); + xo_warn("%s", host); return (1); } addr = (struct sockaddr_in *)(rtm + 1); @@ -533,11 +552,11 @@ delete(char *host) } /* - * Regualar entry delete failed, now check if there + * Regular entry delete failed, now check if there * is a proxy-arp entry to remove. */ if (flags & RTF_ANNOUNCE) { - warnx("delete: cannot locate %s", host); + xo_warnx("delete: cannot locate %s", host); return (1); } @@ -578,21 +597,21 @@ search(u_long addr, action_fn *action) mib[5] = 0; #endif if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) - err(1, "route-sysctl-estimate"); + xo_err(1, "route-sysctl-estimate"); if (needed == 0) /* empty table */ return 0; buf = NULL; for (;;) { buf = reallocf(buf, needed); if (buf == NULL) - errx(1, "could not reallocate memory"); + xo_errx(1, "could not reallocate memory"); st = sysctl(mib, 6, buf, &needed, NULL, 0); if (st == 0 || errno != ENOMEM) break; needed += needed / 8; } if (st == -1) - err(1, "actual retrieval of routing table"); + xo_err(1, "actual retrieval of routing table"); lim = buf + needed; for (next = buf; next < lim; next += rtm->rtm_msglen) { rtm = (struct rt_msghdr *)next; @@ -628,7 +647,9 @@ print_entry(struct sockaddr_dl *sdl, if (ifnameindex == NULL) if ((ifnameindex = if_nameindex()) == NULL) - err(1, "cannot retrieve interface names"); + xo_err(1, "cannot retrieve interface names"); + + xo_open_instance("arp-cache"); if (nflag == 0) hp = gethostbyaddr((caddr_t)&(addr->sin_addr), @@ -642,32 +663,33 @@ print_entry(struct sockaddr_dl *sdl, if (h_errno == TRY_AGAIN) nflag = 1; } - printf("%s (%s) at ", host, inet_ntoa(addr->sin_addr)); + xo_emit("{:hostname/%s} ({:ip-address/%s}) at ", host, + inet_ntoa(addr->sin_addr)); if (sdl->sdl_alen) { if ((sdl->sdl_type == IFT_ETHER || sdl->sdl_type == IFT_L2VLAN || sdl->sdl_type == IFT_BRIDGE) && sdl->sdl_alen == ETHER_ADDR_LEN) - printf("%s", + xo_emit("{:mac-address/%s}", ether_ntoa((struct ether_addr *)LLADDR(sdl))); else { int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0; - printf("%s", link_ntoa(sdl) + n); + xo_emit("{:mac-address/%s}", link_ntoa(sdl) + n); } } else - printf("(incomplete)"); + xo_emit("{d:/(incomplete)}{en:incomplete/true}"); for (p = ifnameindex; p && ifnameindex->if_index && ifnameindex->if_name; p++) { if (p->if_index == sdl->sdl_index) { - printf(" on %s", p->if_name); + xo_emit(" on {:interface/%s}", p->if_name); break; } } if (rtm->rtm_rmx.rmx_expire == 0) - printf(" permanent"); + xo_emit("{d:/ permanent}{en:permanent/true}"); else { #ifndef __rtems__ static struct timespec tp; @@ -675,51 +697,55 @@ print_entry(struct sockaddr_dl *sdl, if (tp.tv_sec == 0) clock_gettime(CLOCK_MONOTONIC, &tp); if ((expire_time = rtm->rtm_rmx.rmx_expire - tp.tv_sec) > 0) - printf(" expires in %d seconds", (int)expire_time); + xo_emit(" expires in {:expires/%d} seconds", + (int)expire_time); else - printf(" expired"); + xo_emit("{d:/ expired}{en:expired/true}"); } + if (rtm->rtm_flags & RTF_ANNOUNCE) - printf(" published"); + xo_emit("{d:/ published}{en:published/true}"); + switch(sdl->sdl_type) { case IFT_ETHER: - printf(" [ethernet]"); + xo_emit(" [{:type/ethernet}]"); break; case IFT_ISO88025: - printf(" [token-ring]"); + xo_emit(" [{:type/token-ring}]"); trld = SDL_ISO88025(sdl); if (trld->trld_rcf != 0) { - printf(" rt=%x", ntohs(trld->trld_rcf)); + xo_emit(" rt=%x", ntohs(trld->trld_rcf)); for (seg = 0; seg < ((TR_RCF_RIFLEN(trld->trld_rcf) - 2 ) / 2); seg++) - printf(":%x", ntohs(*(trld->trld_route[seg]))); + xo_emit(":%x", ntohs(*(trld->trld_route[seg]))); } break; case IFT_FDDI: - printf(" [fddi]"); + xo_emit(" [{:type/fddi}]"); break; case IFT_ATM: - printf(" [atm]"); + xo_emit(" [{:type/atm}]"); break; case IFT_L2VLAN: - printf(" [vlan]"); + xo_emit(" [{:type/vlan}]"); break; case IFT_IEEE1394: - printf(" [firewire]"); + xo_emit(" [{:type/firewire}]"); break; case IFT_BRIDGE: - printf(" [bridge]"); + xo_emit(" [{:type/bridge}]"); break; case IFT_INFINIBAND: - printf(" [infiniband]"); + xo_emit(" [{:type/infiniband}]"); break; default: break; } - printf("\n"); + xo_emit("\n"); + xo_close_instance("arp-cache"); } /* @@ -777,7 +803,7 @@ rtmsg(int cmd, struct sockaddr_in *dst, struct sockaddr_dl *sdl) if (s < 0) { /* first time: open socket, get pid */ s = socket(PF_ROUTE, SOCK_RAW, 0); if (s < 0) - err(1, "socket"); + xo_err(1, "socket"); pid = getpid(); } bzero(&so_mask, sizeof(so_mask)); @@ -797,7 +823,7 @@ rtmsg(int cmd, struct sockaddr_in *dst, struct sockaddr_dl *sdl) switch (cmd) { default: - errx(1, "internal wrong cmd"); + xo_errx(1, "internal wrong cmd"); case RTM_ADD: rtm->rtm_addrs |= RTA_GATEWAY; rtm->rtm_rmx.rmx_expire = expire_time; @@ -830,15 +856,16 @@ doit: rtm->rtm_type = cmd; if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) { if (errno != ESRCH || cmd != RTM_DELETE) { - warn("writing to routing socket"); + xo_warn("writing to routing socket"); return (NULL); } } do { l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg)); - } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid)); + } while (l > 0 && (rtm->rtm_type != cmd || rtm->rtm_seq != seq || + rtm->rtm_pid != pid)); if (l < 0) - warn("read from routing socket"); + xo_warn("read from routing socket"); return (rtm); } @@ -862,12 +889,12 @@ get_ether_addr(in_addr_t ipaddr, struct ether_addr *hwaddr) sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock < 0) - err(1, "socket"); + xo_err(1, "socket"); ifc.ifc_len = sizeof(ifs); ifc.ifc_req = ifs; if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) { - warnx("ioctl(SIOCGIFCONF)"); + xo_warnx("ioctl(SIOCGIFCONF)"); goto done; } |