2007-01-08 01:05:11 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* arp.c - Manipulate the system ARP cache
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Author: Fred N. van Kempen, <waltje at uwalt.nl.mugnet.org>
|
|
|
|
* Busybox port: Paul van Gool <pvangool at mimotech.com>
|
|
|
|
*
|
|
|
|
* modified for getopt32 by Arne Bernin <arne [at] alamut.de>
|
|
|
|
*/
|
|
|
|
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage:#define arp_trivial_usage
|
|
|
|
//usage: "\n[-vn] [-H HWTYPE] [-i IF] -a [HOSTNAME]"
|
|
|
|
//usage: "\n[-v] [-i IF] -d HOSTNAME [pub]"
|
|
|
|
//usage: "\n[-v] [-H HWTYPE] [-i IF] -s HOSTNAME HWADDR [temp]"
|
|
|
|
//usage: "\n[-v] [-H HWTYPE] [-i IF] -s HOSTNAME HWADDR [netmask MASK] pub"
|
|
|
|
//usage: "\n[-v] [-H HWTYPE] [-i IF] -Ds HOSTNAME IFACE [netmask MASK] pub"
|
|
|
|
//usage:#define arp_full_usage "\n\n"
|
|
|
|
//usage: "Manipulate ARP cache\n"
|
|
|
|
//usage: "\n -a Display (all) hosts"
|
2013-02-04 20:48:58 +05:30
|
|
|
//usage: "\n -d Delete ARP entry"
|
|
|
|
//usage: "\n -s Set new entry"
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage: "\n -v Verbose"
|
|
|
|
//usage: "\n -n Don't resolve names"
|
|
|
|
//usage: "\n -i IF Network interface"
|
2013-02-04 20:48:58 +05:30
|
|
|
//usage: "\n -D Read HWADDR from IFACE"
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage: "\n -A,-p AF Protocol family"
|
|
|
|
//usage: "\n -H HWTYPE Hardware address type"
|
|
|
|
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2016-04-21 19:56:30 +05:30
|
|
|
#include "common_bufsiz.h"
|
2007-01-08 01:05:11 +05:30
|
|
|
#include "inet_common.h"
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/if_arp.h>
|
|
|
|
#include <netinet/ether.h>
|
|
|
|
#include <netpacket/packet.h>
|
|
|
|
|
|
|
|
#define DEBUG 0
|
|
|
|
|
|
|
|
#define DFLT_AF "inet"
|
|
|
|
#define DFLT_HW "ether"
|
|
|
|
|
2008-11-13 05:35:17 +05:30
|
|
|
enum {
|
|
|
|
ARP_OPT_A = (1 << 0),
|
|
|
|
ARP_OPT_p = (1 << 1),
|
|
|
|
ARP_OPT_H = (1 << 2),
|
|
|
|
ARP_OPT_t = (1 << 3),
|
|
|
|
ARP_OPT_i = (1 << 4),
|
|
|
|
ARP_OPT_a = (1 << 5),
|
|
|
|
ARP_OPT_d = (1 << 6),
|
|
|
|
ARP_OPT_n = (1 << 7), /* do not resolve addresses */
|
|
|
|
ARP_OPT_D = (1 << 8), /* HW-address is devicename */
|
|
|
|
ARP_OPT_s = (1 << 9),
|
|
|
|
ARP_OPT_v = (1 << 10) * DEBUG, /* debugging output flag */
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
sockfd = 3, /* active socket descriptor */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct globals {
|
|
|
|
const struct aftype *ap; /* current address family */
|
|
|
|
const struct hwtype *hw; /* current hardware type */
|
|
|
|
const char *device; /* current device */
|
|
|
|
smallint hw_set; /* flag if hw-type was set (-H) */
|
2010-02-04 19:30:15 +05:30
|
|
|
} FIX_ALIASING;
|
2016-04-21 19:56:30 +05:30
|
|
|
#define G (*(struct globals*)bb_common_bufsiz1)
|
2008-11-13 05:35:17 +05:30
|
|
|
#define ap (G.ap )
|
|
|
|
#define hw (G.hw )
|
|
|
|
#define device (G.device )
|
|
|
|
#define hw_set (G.hw_set )
|
|
|
|
#define INIT_G() do { \
|
|
|
|
device = ""; \
|
|
|
|
} while (0)
|
|
|
|
|
2007-01-08 01:05:11 +05:30
|
|
|
|
2007-08-13 02:28:27 +05:30
|
|
|
static const char options[] ALIGN1 =
|
2007-07-24 21:24:42 +05:30
|
|
|
"pub\0"
|
|
|
|
"priv\0"
|
|
|
|
"temp\0"
|
|
|
|
"trail\0"
|
|
|
|
"dontpub\0"
|
|
|
|
"auto\0"
|
|
|
|
"dev\0"
|
|
|
|
"netmask\0";
|
2007-01-08 01:05:11 +05:30
|
|
|
|
|
|
|
/* Delete an entry from the ARP cache. */
|
|
|
|
/* Called only from main, once */
|
|
|
|
static int arp_del(char **args)
|
|
|
|
{
|
2007-03-15 03:41:20 +05:30
|
|
|
char *host;
|
2007-01-08 01:05:11 +05:30
|
|
|
struct arpreq req;
|
|
|
|
struct sockaddr sa;
|
|
|
|
int flags = 0;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
|
|
|
|
/* Resolve the host name. */
|
2007-03-15 03:41:20 +05:30
|
|
|
host = *args;
|
|
|
|
if (ap->input(host, &sa) < 0) {
|
2007-01-08 01:05:11 +05:30
|
|
|
bb_herror_msg_and_die("%s", host);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If a host has more than one address, use the correct one! */
|
|
|
|
memcpy(&req.arp_pa, &sa, sizeof(struct sockaddr));
|
|
|
|
|
|
|
|
if (hw_set)
|
|
|
|
req.arp_ha.sa_family = hw->type;
|
|
|
|
|
|
|
|
req.arp_flags = ATF_PERM;
|
|
|
|
args++;
|
|
|
|
while (*args != NULL) {
|
2007-07-24 21:24:42 +05:30
|
|
|
switch (index_in_strings(options, *args)) {
|
2007-01-08 03:42:35 +05:30
|
|
|
case 0: /* "pub" */
|
2007-01-08 01:05:11 +05:30
|
|
|
flags |= 1;
|
|
|
|
args++;
|
|
|
|
break;
|
2007-01-08 03:42:35 +05:30
|
|
|
case 1: /* "priv" */
|
2007-01-08 01:05:11 +05:30
|
|
|
flags |= 2;
|
|
|
|
args++;
|
|
|
|
break;
|
2007-01-08 03:42:35 +05:30
|
|
|
case 2: /* "temp" */
|
2007-01-08 01:05:11 +05:30
|
|
|
req.arp_flags &= ~ATF_PERM;
|
|
|
|
args++;
|
|
|
|
break;
|
2007-01-08 03:42:35 +05:30
|
|
|
case 3: /* "trail" */
|
2007-01-08 01:05:11 +05:30
|
|
|
req.arp_flags |= ATF_USETRAILERS;
|
|
|
|
args++;
|
|
|
|
break;
|
2007-01-08 03:42:35 +05:30
|
|
|
case 4: /* "dontpub" */
|
2007-01-08 01:05:11 +05:30
|
|
|
#ifdef HAVE_ATF_DONTPUB
|
|
|
|
req.arp_flags |= ATF_DONTPUB;
|
|
|
|
#else
|
|
|
|
bb_error_msg("feature ATF_DONTPUB is not supported");
|
|
|
|
#endif
|
|
|
|
args++;
|
|
|
|
break;
|
2007-01-08 03:42:35 +05:30
|
|
|
case 5: /* "auto" */
|
2007-01-08 01:05:11 +05:30
|
|
|
#ifdef HAVE_ATF_MAGIC
|
|
|
|
req.arp_flags |= ATF_MAGIC;
|
|
|
|
#else
|
|
|
|
bb_error_msg("feature ATF_MAGIC is not supported");
|
|
|
|
#endif
|
|
|
|
args++;
|
|
|
|
break;
|
2007-01-08 03:42:35 +05:30
|
|
|
case 6: /* "dev" */
|
2007-01-08 01:05:11 +05:30
|
|
|
if (*++args == NULL)
|
|
|
|
bb_show_usage();
|
|
|
|
device = *args;
|
|
|
|
args++;
|
|
|
|
break;
|
2007-01-08 03:42:35 +05:30
|
|
|
case 7: /* "netmask" */
|
2007-01-08 01:05:11 +05:30
|
|
|
if (*++args == NULL)
|
|
|
|
bb_show_usage();
|
|
|
|
if (strcmp(*args, "255.255.255.255") != 0) {
|
2007-03-15 03:41:20 +05:30
|
|
|
host = *args;
|
|
|
|
if (ap->input(host, &sa) < 0) {
|
2007-01-08 03:42:35 +05:30
|
|
|
bb_herror_msg_and_die("%s", host);
|
2007-01-08 01:05:11 +05:30
|
|
|
}
|
|
|
|
memcpy(&req.arp_netmask, &sa, sizeof(struct sockaddr));
|
|
|
|
req.arp_flags |= ATF_NETMASK;
|
|
|
|
}
|
|
|
|
args++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
bb_show_usage();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (flags == 0)
|
|
|
|
flags = 3;
|
|
|
|
|
|
|
|
strncpy(req.arp_dev, device, sizeof(req.arp_dev));
|
|
|
|
|
|
|
|
err = -1;
|
|
|
|
|
|
|
|
/* Call the kernel. */
|
|
|
|
if (flags & 2) {
|
|
|
|
if (option_mask32 & ARP_OPT_v)
|
|
|
|
bb_error_msg("SIOCDARP(nopub)");
|
|
|
|
err = ioctl(sockfd, SIOCDARP, &req);
|
|
|
|
if (err < 0) {
|
|
|
|
if (errno == ENXIO) {
|
|
|
|
if (flags & 1)
|
|
|
|
goto nopub;
|
|
|
|
printf("No ARP entry for %s\n", host);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
bb_perror_msg_and_die("SIOCDARP(priv)");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((flags & 1) && err) {
|
|
|
|
nopub:
|
|
|
|
req.arp_flags |= ATF_PUBL;
|
|
|
|
if (option_mask32 & ARP_OPT_v)
|
|
|
|
bb_error_msg("SIOCDARP(pub)");
|
|
|
|
if (ioctl(sockfd, SIOCDARP, &req) < 0) {
|
|
|
|
if (errno == ENXIO) {
|
|
|
|
printf("No ARP entry for %s\n", host);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
bb_perror_msg_and_die("SIOCDARP(pub)");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the hardware address to a specified interface name */
|
2013-02-04 20:48:58 +05:30
|
|
|
static void arp_getdevhw(char *ifname, struct sockaddr *sa)
|
2007-01-08 01:05:11 +05:30
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
|
|
|
const struct hwtype *xhw;
|
|
|
|
|
|
|
|
strcpy(ifr.ifr_name, ifname);
|
2007-07-15 03:37:14 +05:30
|
|
|
ioctl_or_perror_and_die(sockfd, SIOCGIFHWADDR, &ifr,
|
2013-02-04 20:48:58 +05:30
|
|
|
"can't get HW-Address for '%s'", ifname);
|
|
|
|
if (hw_set && (ifr.ifr_hwaddr.sa_family != hw->type)) {
|
2007-01-08 01:05:11 +05:30
|
|
|
bb_error_msg_and_die("protocol type mismatch");
|
|
|
|
}
|
|
|
|
memcpy(sa, &(ifr.ifr_hwaddr), sizeof(struct sockaddr));
|
|
|
|
|
|
|
|
if (option_mask32 & ARP_OPT_v) {
|
|
|
|
xhw = get_hwntype(ifr.ifr_hwaddr.sa_family);
|
|
|
|
if (!xhw || !xhw->print) {
|
|
|
|
xhw = get_hwntype(-1);
|
|
|
|
}
|
|
|
|
bb_error_msg("device '%s' has HW address %s '%s'",
|
2013-02-04 20:48:58 +05:30
|
|
|
ifname, xhw->name,
|
|
|
|
xhw->print((unsigned char *) &ifr.ifr_hwaddr.sa_data));
|
2007-01-08 01:05:11 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set an entry in the ARP cache. */
|
|
|
|
/* Called only from main, once */
|
|
|
|
static int arp_set(char **args)
|
|
|
|
{
|
2007-03-15 03:41:20 +05:30
|
|
|
char *host;
|
2007-01-08 01:05:11 +05:30
|
|
|
struct arpreq req;
|
|
|
|
struct sockaddr sa;
|
|
|
|
int flags;
|
2007-01-08 03:42:35 +05:30
|
|
|
|
2007-01-08 01:05:11 +05:30
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
|
2007-03-15 03:41:20 +05:30
|
|
|
host = *args++;
|
|
|
|
if (ap->input(host, &sa) < 0) {
|
2007-01-08 01:05:11 +05:30
|
|
|
bb_herror_msg_and_die("%s", host);
|
|
|
|
}
|
|
|
|
/* If a host has more than one address, use the correct one! */
|
|
|
|
memcpy(&req.arp_pa, &sa, sizeof(struct sockaddr));
|
|
|
|
|
|
|
|
/* Fetch the hardware address. */
|
|
|
|
if (*args == NULL) {
|
|
|
|
bb_error_msg_and_die("need hardware address");
|
|
|
|
}
|
|
|
|
if (option_mask32 & ARP_OPT_D) {
|
2013-02-04 20:48:58 +05:30
|
|
|
arp_getdevhw(*args++, &req.arp_ha);
|
2007-01-08 01:05:11 +05:30
|
|
|
} else {
|
|
|
|
if (hw->input(*args++, &req.arp_ha) < 0) {
|
|
|
|
bb_error_msg_and_die("invalid hardware address");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check out any modifiers. */
|
|
|
|
flags = ATF_PERM | ATF_COM;
|
|
|
|
while (*args != NULL) {
|
2007-07-24 21:24:42 +05:30
|
|
|
switch (index_in_strings(options, *args)) {
|
2007-01-08 03:42:35 +05:30
|
|
|
case 0: /* "pub" */
|
2007-01-08 01:05:11 +05:30
|
|
|
flags |= ATF_PUBL;
|
|
|
|
args++;
|
|
|
|
break;
|
2007-01-08 03:42:35 +05:30
|
|
|
case 1: /* "priv" */
|
2007-01-08 01:05:11 +05:30
|
|
|
flags &= ~ATF_PUBL;
|
|
|
|
args++;
|
|
|
|
break;
|
2007-01-08 03:42:35 +05:30
|
|
|
case 2: /* "temp" */
|
2007-01-08 01:05:11 +05:30
|
|
|
flags &= ~ATF_PERM;
|
|
|
|
args++;
|
|
|
|
break;
|
2007-01-08 03:42:35 +05:30
|
|
|
case 3: /* "trail" */
|
2007-01-08 01:05:11 +05:30
|
|
|
flags |= ATF_USETRAILERS;
|
|
|
|
args++;
|
|
|
|
break;
|
2007-01-08 03:42:35 +05:30
|
|
|
case 4: /* "dontpub" */
|
2007-01-08 01:05:11 +05:30
|
|
|
#ifdef HAVE_ATF_DONTPUB
|
|
|
|
flags |= ATF_DONTPUB;
|
|
|
|
#else
|
|
|
|
bb_error_msg("feature ATF_DONTPUB is not supported");
|
|
|
|
#endif
|
|
|
|
args++;
|
|
|
|
break;
|
2007-01-08 03:42:35 +05:30
|
|
|
case 5: /* "auto" */
|
2007-01-08 01:05:11 +05:30
|
|
|
#ifdef HAVE_ATF_MAGIC
|
|
|
|
flags |= ATF_MAGIC;
|
|
|
|
#else
|
|
|
|
bb_error_msg("feature ATF_MAGIC is not supported");
|
|
|
|
#endif
|
|
|
|
args++;
|
|
|
|
break;
|
2007-01-08 03:42:35 +05:30
|
|
|
case 6: /* "dev" */
|
2007-01-08 01:05:11 +05:30
|
|
|
if (*++args == NULL)
|
|
|
|
bb_show_usage();
|
|
|
|
device = *args;
|
|
|
|
args++;
|
|
|
|
break;
|
2007-01-08 03:42:35 +05:30
|
|
|
case 7: /* "netmask" */
|
2007-01-08 01:05:11 +05:30
|
|
|
if (*++args == NULL)
|
|
|
|
bb_show_usage();
|
|
|
|
if (strcmp(*args, "255.255.255.255") != 0) {
|
2007-03-15 03:41:20 +05:30
|
|
|
host = *args;
|
|
|
|
if (ap->input(host, &sa) < 0) {
|
2007-01-08 01:05:11 +05:30
|
|
|
bb_herror_msg_and_die("%s", host);
|
|
|
|
}
|
|
|
|
memcpy(&req.arp_netmask, &sa, sizeof(struct sockaddr));
|
|
|
|
flags |= ATF_NETMASK;
|
|
|
|
}
|
|
|
|
args++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
bb_show_usage();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fill in the remainder of the request. */
|
|
|
|
req.arp_flags = flags;
|
|
|
|
|
|
|
|
strncpy(req.arp_dev, device, sizeof(req.arp_dev));
|
|
|
|
|
|
|
|
/* Call the kernel. */
|
|
|
|
if (option_mask32 & ARP_OPT_v)
|
|
|
|
bb_error_msg("SIOCSARP()");
|
2007-07-15 03:37:14 +05:30
|
|
|
xioctl(sockfd, SIOCSARP, &req);
|
2007-01-08 01:05:11 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Print the contents of an ARP request block. */
|
|
|
|
static void
|
2007-01-30 04:21:58 +05:30
|
|
|
arp_disp(const char *name, char *ip, int type, int arp_flags,
|
2013-01-14 06:04:48 +05:30
|
|
|
char *hwa, char *mask, char *dev)
|
2007-01-08 01:05:11 +05:30
|
|
|
{
|
2008-06-07 20:40:29 +05:30
|
|
|
static const int arp_masks[] = {
|
2008-06-16 09:39:25 +05:30
|
|
|
ATF_PERM, ATF_PUBL,
|
2008-06-07 20:40:29 +05:30
|
|
|
#ifdef HAVE_ATF_MAGIC
|
|
|
|
ATF_MAGIC,
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ATF_DONTPUB
|
|
|
|
ATF_DONTPUB,
|
|
|
|
#endif
|
|
|
|
ATF_USETRAILERS,
|
|
|
|
};
|
|
|
|
static const char arp_labels[] ALIGN1 = "PERM\0""PUP\0"
|
|
|
|
#ifdef HAVE_ATF_MAGIC
|
|
|
|
"AUTO\0"
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ATF_DONTPUB
|
|
|
|
"DONTPUB\0"
|
|
|
|
#endif
|
|
|
|
"TRAIL\0"
|
|
|
|
;
|
|
|
|
|
2007-01-08 01:05:11 +05:30
|
|
|
const struct hwtype *xhw;
|
|
|
|
|
|
|
|
xhw = get_hwntype(type);
|
|
|
|
if (xhw == NULL)
|
|
|
|
xhw = get_hwtype(DFLT_HW);
|
|
|
|
|
|
|
|
printf("%s (%s) at ", name, ip);
|
|
|
|
|
|
|
|
if (!(arp_flags & ATF_COM)) {
|
|
|
|
if (arp_flags & ATF_PUBL)
|
|
|
|
printf("* ");
|
|
|
|
else
|
|
|
|
printf("<incomplete> ");
|
|
|
|
} else {
|
|
|
|
printf("%s [%s] ", hwa, xhw->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arp_flags & ATF_NETMASK)
|
|
|
|
printf("netmask %s ", mask);
|
|
|
|
|
2008-06-07 20:40:29 +05:30
|
|
|
print_flags_separated(arp_masks, arp_labels, arp_flags, " ");
|
|
|
|
printf(" on %s\n", dev);
|
2007-01-08 01:05:11 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Display the contents of the ARP cache in the kernel. */
|
|
|
|
/* Called only from main, once */
|
|
|
|
static int arp_show(char *name)
|
|
|
|
{
|
2007-03-15 03:41:20 +05:30
|
|
|
const char *host;
|
|
|
|
const char *hostname;
|
|
|
|
FILE *fp;
|
2007-01-08 01:05:11 +05:30
|
|
|
struct sockaddr sa;
|
|
|
|
int type, flags;
|
2007-01-08 03:42:35 +05:30
|
|
|
int num;
|
|
|
|
unsigned entries = 0, shown = 0;
|
2007-03-15 03:41:20 +05:30
|
|
|
char ip[128];
|
|
|
|
char hwa[128];
|
|
|
|
char mask[128];
|
|
|
|
char line[128];
|
|
|
|
char dev[128];
|
2007-01-08 01:05:11 +05:30
|
|
|
|
2007-03-15 03:41:20 +05:30
|
|
|
host = NULL;
|
2007-01-08 01:05:11 +05:30
|
|
|
if (name != NULL) {
|
|
|
|
/* Resolve the host name. */
|
2007-03-15 03:41:20 +05:30
|
|
|
if (ap->input(name, &sa) < 0) {
|
|
|
|
bb_herror_msg_and_die("%s", name);
|
2007-01-08 01:05:11 +05:30
|
|
|
}
|
2007-03-15 03:41:20 +05:30
|
|
|
host = xstrdup(ap->sprint(&sa, 1));
|
2007-01-08 01:05:11 +05:30
|
|
|
}
|
2008-07-22 04:35:26 +05:30
|
|
|
fp = xfopen_for_read("/proc/net/arp");
|
2007-03-15 03:41:20 +05:30
|
|
|
/* Bypass header -- read one line */
|
|
|
|
fgets(line, sizeof(line), fp);
|
|
|
|
|
|
|
|
/* Read the ARP cache entries. */
|
|
|
|
while (fgets(line, sizeof(line), fp)) {
|
|
|
|
|
2007-01-08 01:05:11 +05:30
|
|
|
mask[0] = '-'; mask[1] = '\0';
|
|
|
|
dev[0] = '-'; dev[1] = '\0';
|
2007-03-15 03:41:20 +05:30
|
|
|
/* All these strings can't overflow
|
|
|
|
* because fgets above reads limited amount of data */
|
|
|
|
num = sscanf(line, "%s 0x%x 0x%x %s %s %s\n",
|
2013-01-14 06:04:48 +05:30
|
|
|
ip, &type, &flags, hwa, mask, dev);
|
2007-03-15 03:41:20 +05:30
|
|
|
if (num < 4)
|
|
|
|
break;
|
2007-01-08 01:05:11 +05:30
|
|
|
|
2007-03-15 03:41:20 +05:30
|
|
|
entries++;
|
|
|
|
/* if the user specified hw-type differs, skip it */
|
|
|
|
if (hw_set && (type != hw->type))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* if the user specified address differs, skip it */
|
|
|
|
if (host && strcmp(ip, host) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* if the user specified device differs, skip it */
|
|
|
|
if (device[0] && strcmp(dev, device) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
shown++;
|
|
|
|
/* This IS ugly but it works -be */
|
|
|
|
hostname = "?";
|
|
|
|
if (!(option_mask32 & ARP_OPT_n)) {
|
|
|
|
if (ap->input(ip, &sa) < 0)
|
|
|
|
hostname = ip;
|
|
|
|
else
|
|
|
|
hostname = ap->sprint(&sa, (option_mask32 & ARP_OPT_n) | 0x8000);
|
|
|
|
if (strcmp(hostname, ip) == 0)
|
|
|
|
hostname = "?";
|
2007-01-08 01:05:11 +05:30
|
|
|
}
|
2007-03-15 03:41:20 +05:30
|
|
|
|
|
|
|
arp_disp(hostname, ip, type, flags, hwa, mask, dev);
|
2007-01-08 01:05:11 +05:30
|
|
|
}
|
|
|
|
if (option_mask32 & ARP_OPT_v)
|
2013-11-29 21:15:45 +05:30
|
|
|
printf("Entries: %u\tSkipped: %u\tFound: %u\n",
|
2013-01-14 06:04:48 +05:30
|
|
|
entries, entries - shown, shown);
|
2007-01-08 01:05:11 +05:30
|
|
|
|
|
|
|
if (!shown) {
|
2007-03-15 03:41:20 +05:30
|
|
|
if (hw_set || host || device[0])
|
2013-11-29 21:15:45 +05:30
|
|
|
printf("No match found in %u entries\n", entries);
|
2007-01-08 01:05:11 +05:30
|
|
|
}
|
2007-03-15 03:41:20 +05:30
|
|
|
if (ENABLE_FEATURE_CLEAN_UP) {
|
|
|
|
free((char*)host);
|
|
|
|
fclose(fp);
|
|
|
|
}
|
2007-01-08 01:05:11 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int arp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 14:48:54 +05:30
|
|
|
int arp_main(int argc UNUSED_PARAM, char **argv)
|
2007-01-08 01:05:11 +05:30
|
|
|
{
|
2013-02-04 19:44:20 +05:30
|
|
|
const char *hw_type;
|
2008-08-16 01:44:23 +05:30
|
|
|
const char *protocol;
|
2008-11-13 05:35:17 +05:30
|
|
|
unsigned opts;
|
|
|
|
|
|
|
|
INIT_G();
|
2007-01-08 01:05:11 +05:30
|
|
|
|
2008-11-13 05:35:17 +05:30
|
|
|
xmove_fd(xsocket(AF_INET, SOCK_DGRAM, 0), sockfd);
|
2013-02-04 19:44:20 +05:30
|
|
|
|
2007-01-08 01:05:11 +05:30
|
|
|
ap = get_aftype(DFLT_AF);
|
2013-02-04 19:44:20 +05:30
|
|
|
/* Defaults are always supported */
|
|
|
|
//if (!ap)
|
|
|
|
// bb_error_msg_and_die("%s: %s not supported", DFLT_AF, "address family");
|
|
|
|
hw = get_hwtype(DFLT_HW);
|
|
|
|
//if (!hw)
|
|
|
|
// bb_error_msg_and_die("%s: %s not supported", DFLT_HW, "hardware type");
|
2007-01-08 01:05:11 +05:30
|
|
|
|
2008-11-13 05:35:17 +05:30
|
|
|
opts = getopt32(argv, "A:p:H:t:i:adnDsv", &protocol, &protocol,
|
2007-01-08 01:05:11 +05:30
|
|
|
&hw_type, &hw_type, &device);
|
|
|
|
argv += optind;
|
2008-11-13 05:35:17 +05:30
|
|
|
if (opts & (ARP_OPT_A | ARP_OPT_p)) {
|
2007-01-08 01:05:11 +05:30
|
|
|
ap = get_aftype(protocol);
|
2013-02-04 19:44:20 +05:30
|
|
|
if (!ap)
|
2007-01-08 01:05:11 +05:30
|
|
|
bb_error_msg_and_die("%s: unknown %s", protocol, "address family");
|
|
|
|
}
|
2013-02-04 19:44:20 +05:30
|
|
|
if (opts & (ARP_OPT_H | ARP_OPT_t)) {
|
2007-01-08 01:05:11 +05:30
|
|
|
hw = get_hwtype(hw_type);
|
2013-02-04 19:44:20 +05:30
|
|
|
if (!hw)
|
2007-01-08 01:05:11 +05:30
|
|
|
bb_error_msg_and_die("%s: unknown %s", hw_type, "hardware type");
|
|
|
|
hw_set = 1;
|
|
|
|
}
|
2008-11-13 05:35:17 +05:30
|
|
|
//if (opts & ARP_OPT_i)... -i
|
2007-01-08 01:05:11 +05:30
|
|
|
|
|
|
|
if (ap->af != AF_INET) {
|
|
|
|
bb_error_msg_and_die("%s: kernel only supports 'inet'", ap->name);
|
|
|
|
}
|
|
|
|
if (hw->alen <= 0) {
|
|
|
|
bb_error_msg_and_die("%s: %s without ARP support",
|
2013-01-14 06:04:48 +05:30
|
|
|
hw->name, "hardware type");
|
2007-01-08 01:05:11 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Now see what we have to do here... */
|
2008-11-13 05:35:17 +05:30
|
|
|
if (opts & (ARP_OPT_d | ARP_OPT_s)) {
|
2007-01-08 01:05:11 +05:30
|
|
|
if (argv[0] == NULL)
|
|
|
|
bb_error_msg_and_die("need host name");
|
2008-11-13 05:35:17 +05:30
|
|
|
if (opts & ARP_OPT_s)
|
2007-01-08 01:05:11 +05:30
|
|
|
return arp_set(argv);
|
|
|
|
return arp_del(argv);
|
|
|
|
}
|
2013-02-04 19:44:20 +05:30
|
|
|
|
2008-11-13 05:35:17 +05:30
|
|
|
//if (opts & ARP_OPT_a) - default
|
2007-01-08 01:05:11 +05:30
|
|
|
return arp_show(argv[0]);
|
|
|
|
}
|