Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F111591862
D9563.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
11 KB
Referenced Files
None
Subscribers
None
D9563.diff
View Options
Index: head/usr.sbin/arp/Makefile
===================================================================
--- head/usr.sbin/arp/Makefile
+++ head/usr.sbin/arp/Makefile
@@ -4,6 +4,8 @@
PROG= arp
MAN= arp.4 arp.8
+LIBADD= xo
+
WARNS?= 3
.include <bsd.prog.mk>
Index: head/usr.sbin/arp/arp.8
===================================================================
--- head/usr.sbin/arp/arp.8
+++ head/usr.sbin/arp/arp.8
@@ -28,7 +28,7 @@
.\" @(#)arp.8 8.1 (Berkeley) 6/6/93
.\" $FreeBSD$
.\"
-.Dd January 31, 2013
+.Dd April 6, 2017
.Dt ARP 8
.Os
.Sh NAME
@@ -36,10 +36,12 @@
.Nd address resolution display and control
.Sh SYNOPSIS
.Nm
+.Op Fl -libxo Ar options
.Op Fl n
.Op Fl i Ar interface
.Ar hostname
.Nm
+.Op Fl -libxo Ar options
.Op Fl n
.Op Fl i Ar interface
.Fl a
@@ -77,6 +79,13 @@
.Pp
Available options:
.Bl -tag -width indent
+.It Fl -libxo
+Generate output via
+.Xr libxo 3
+in a selection of different human and machine readable formats.
+See
+.Xr xo_parse_args 3
+for details on command line arguments.
.It Fl a
The program displays or deletes all of the current
.Tn ARP
@@ -182,6 +191,8 @@
.El
.Sh SEE ALSO
.Xr inet 3 ,
+.Xr libxo 3 ,
+.Xr xo_parse_args 3 ,
.Xr arp 4 ,
.Xr ifconfig 8 ,
.Xr ndp 8
Index: head/usr.sbin/arp/arp.c
===================================================================
--- head/usr.sbin/arp/arp.c
+++ head/usr.sbin/arp/arp.c
@@ -78,6 +78,7 @@
#include <string.h>
#include <strings.h>
#include <unistd.h>
+#include <libxo/xo.h>
typedef void (action_fn)(struct sockaddr_dl *sdl, struct sockaddr_in *s_in,
struct rt_msghdr *rtm);
@@ -114,6 +115,8 @@
#define SETFUNC(f) { if (func) usage(); func = (f); }
+#define ARP_XO_VERSION "1"
+
int
main(int argc, char *argv[])
{
@@ -121,6 +124,10 @@
int rtn = 0;
int aflag = 0; /* do it for all entries */
+ argc = xo_parse_args(argc, argv);
+ if (argc < 0)
+ exit(1);
+
while ((ch = getopt(argc, argv, "andfsSi:")) != -1)
switch(ch) {
case 'a':
@@ -155,12 +162,13 @@
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) {
@@ -168,7 +176,16 @@
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();
@@ -218,7 +235,7 @@
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];
@@ -234,7 +251,7 @@
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;
}
@@ -262,7 +279,7 @@
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,
@@ -327,7 +344,7 @@
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;
@@ -343,18 +360,18 @@
}
} 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++;
@@ -362,7 +379,7 @@
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);
}
@@ -371,7 +388,7 @@
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;
@@ -389,7 +406,7 @@
*/
rtm = rtmsg(RTM_GET, dst, &sdl_m);
if (rtm == NULL) {
- warn("%s", host);
+ xo_warn("%s", host);
return (1);
}
addr = (struct sockaddr_in *)(rtm + 1);
@@ -398,7 +415,7 @@
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;
@@ -413,19 +430,31 @@
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);
}
/*
@@ -460,7 +489,7 @@
for (;;) { /* try twice */
rtm = rtmsg(RTM_GET, dst, &sdl_m);
if (rtm == NULL) {
- warn("%s", host);
+ xo_warn("%s", host);
return (1);
}
addr = (struct sockaddr_in *)(rtm + 1);
@@ -486,7 +515,7 @@
* 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);
}
@@ -527,21 +556,21 @@
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;
@@ -577,7 +606,9 @@
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),
@@ -591,82 +622,87 @@
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 {
static struct timespec tp;
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");
}
/*
@@ -720,7 +756,7 @@
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));
@@ -740,7 +776,7 @@
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;
@@ -773,7 +809,7 @@
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);
}
}
@@ -781,7 +817,7 @@
l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
} while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
if (l < 0)
- warn("read from routing socket");
+ xo_warn("read from routing socket");
return (rtm);
}
@@ -805,12 +841,12 @@
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;
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Thu, Mar 6, 5:27 PM (12 h, 37 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
17017438
Default Alt Text
D9563.diff (11 KB)
Attached To
Mode
D9563: Introduce libxo to arp(8)
Attached
Detach File
Event Timeline
Log In to Comment