-/* vi:set ts=4:*/
+/* vi: set sw=4 ts=4: */
/*
* arping.c - Ping hosts by ARP requests/replies
*
#include <sys/ioctl.h>
#include <signal.h>
-#include <sys/time.h>
#include <errno.h>
#include <stdlib.h>
advert = 4,
quiet = 8,
quit_on_reply = 16,
- unicasting = 32,
- broadcast_only = 64
+ broadcast_only = 32,
+ unicasting = 64
};
static int cfg;
static int s;
-static int count = -1;
-static int timeout;
+static unsigned count = UINT_MAX;
+static unsigned timeout;
static int sent;
static int brd_sent;
static int received;
#define MS_TDIFF(tv1,tv2) ( ((tv1).tv_sec-(tv2).tv_sec)*1000 + \
((tv1).tv_usec-(tv2).tv_usec)/1000 )
-#if 0
-static void set_signal(int signo, void (*handler) (void))
-{
- struct sigaction sa;
-
- memset(&sa, 0, sizeof(sa));
- sa.sa_handler = (void (*)(int)) handler;
- sa.sa_flags = SA_RESTART;
- sigaction(signo, &sa, NULL);
-}
-#endif
-
static int send_pack(int sock, struct in_addr *src_addr,
struct in_addr *dst_addr, struct sockaddr_ll *ME,
struct sockaddr_ll *HE)
static void finish(void)
{
if (!(cfg&quiet)) {
- printf("Sent %d probes (%d broadcast(s))\n", sent, brd_sent);
- printf("Received %d repl%s", received, (received > 1) ? "ies" : "y");
+ printf("Sent %d probes (%d broadcast(s))\n"
+ "Received %d repl%s",
+ sent, brd_sent,
+ received, (received > 1) ? "ies" : "y");
if (brd_recv || req_recv) {
printf(" (");
if (req_recv)
gettimeofday(&tv, NULL);
- printf("%s ",
- FROM->sll_pkttype == PACKET_HOST ? "Unicast" : "Broadcast");
- printf("%s from ",
- ah->ar_op == htons(ARPOP_REPLY) ? "reply" : "request");
- printf("%s ", inet_ntoa(src_ip));
- printf("[%s]", ether_ntoa((struct ether_addr *) p));
+ printf("%s %s from %s [%s]",
+ FROM->sll_pkttype == PACKET_HOST ? "Unicast" : "Broadcast",
+ ah->ar_op == htons(ARPOP_REPLY) ? "reply" : "request",
+ inet_ntoa(src_ip),
+ ether_ntoa((struct ether_addr *) p));
if (dst_ip.s_addr != src.s_addr) {
printf("for %s ", inet_ntoa(dst_ip));
s_printed = 1;
char *source = NULL;
char *target;
- s = socket(PF_PACKET, SOCK_DGRAM, 0);
- ifindex = errno;
+ s = xsocket(PF_PACKET, SOCK_DGRAM, 0);
- setuid(getuid());
+ // Drop suid root privileges
+ xsetuid(getuid());
{
- unsigned long opt;
- char *_count, *_timeout, *_device;
- opt = bb_getopt_ulflags(argc, argv, "DUAqfbc:w:i:s:",
- &_count, &_timeout, &_device);
- if (opt & 1) { /* Dad */
- cfg |= dad;
- cfg |= quit_on_reply;
- }
- if (opt & 2) /* Unsolicited */
- cfg |= unsolicited;
- if (opt & 4) { /* Advert */
- cfg |= advert;
- cfg |= unsolicited;
- }
- if (opt & 8) /* quiet */
- cfg |= quiet;
- if (opt & 16) /* quit on reply */
- cfg |= quit_on_reply;
- if (opt & 32) /* broadcast only */
- cfg |= broadcast_only;
- if (opt & 64) /* count */
- count = atoi(_count);
- if (opt & 128) /* timeout */
- timeout = atoi(_timeout);
- if (opt & 256) { /* interface */
- if (bb_strlen(_device) > IF_NAMESIZE) {
- bb_error_msg_and_die("Interface name `%s' must be less than %d",
- _device, IF_NAMESIZE);
+ unsigned opt;
+ char *_count, *_timeout;
+
+ /* Dad also sets quit_on_reply.
+ * Advert also sets unsolicited.
+ */
+ opt_complementary = "Df:AU";
+ opt = getopt32(argc, argv, "DUAqfbc:w:i:s:",
+ &_count, &_timeout, &device, &source);
+ cfg |= opt & 0x3f; /* set respective flags */
+ if (opt & 0x40) /* -c: count */
+ count = xatou(_count);
+ if (opt & 0x80) /* -w: timeout */
+ timeout = xatoul_range(_timeout, 0, INT_MAX/2000);
+ if (opt & 0x100) { /* -i: interface */
+ if (strlen(device) > IF_NAMESIZE) {
+ bb_error_msg_and_die("interface name '%s' is too long",
+ device);
}
- device = _device;
}
- if (opt & 512) /* source */
- source = optarg;
+ //if (opt & 0x200) /* -s: source */
}
argc -= optind;
argv += optind;
target = *argv;
-
- if (s < 0) {
- bb_default_error_retval = ifindex;
- bb_perror_msg_and_die("socket");
- }
- bb_default_error_retval = 2;
+ xfunc_error_retval = 2;
{
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, device, IFNAMSIZ - 1);
if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
- bb_error_msg_and_die("Interface %s not found", device);
+ bb_error_msg_and_die("interface %s not found", device);
}
ifindex = ifr.ifr_ifindex;
bb_error_msg_and_die("SIOCGIFFLAGS");
}
if (!(ifr.ifr_flags & IFF_UP)) {
- bb_error_msg_and_die("Interface %s is down", device);
+ bb_error_msg_and_die("interface %s is down", device);
}
if (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)) {
- bb_error_msg("Interface %s is not ARPable", device);
+ bb_error_msg("interface %s is not ARPable", device);
exit(cfg&dad ? 0 : 2);
}
}
if (!(cfg&dad) || src.s_addr) {
struct sockaddr_in saddr;
- int probe_fd = socket(AF_INET, SOCK_DGRAM, 0);
+ int probe_fd = xsocket(AF_INET, SOCK_DGRAM, 0);
- if (probe_fd < 0) {
- bb_error_msg_and_die("socket");
- }
if (device) {
if (setsockopt
(probe_fd, SOL_SOCKET, SO_BINDTODEVICE, device,
strlen(device) + 1) == -1)
- bb_error_msg("WARNING: interface %s is ignored", device);
+ bb_error_msg("warning: interface %s is ignored", device);
}
memset(&saddr, 0, sizeof(saddr));
saddr.sin_family = AF_INET;
if (setsockopt
(probe_fd, SOL_SOCKET, SO_DONTROUTE, (char *) &on,
sizeof(on)) == -1)
- bb_perror_msg("WARNING: setsockopt(SO_DONTROUTE)");
+ bb_perror_msg("warning: setsockopt(SO_DONTROUTE)");
if (connect(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr))
== -1) {
bb_error_msg_and_die("connect");
}
}
if (me.sll_halen == 0) {
- bb_error_msg("Interface \"%s\" is not ARPable (no ll address)", device);
+ bb_error_msg("interface \"%s\" is not ARPable (no ll address)", device);
exit(cfg&dad ? 0 : 2);
}
he = me;
memset(he.sll_addr, -1, he.sll_halen);
if (!(cfg&quiet)) {
- printf("ARPING to %s", inet_ntoa(dst));
- printf(" from %s via %s\n", inet_ntoa(src),
- device ? device : "unknown");
+ printf("ARPING to %s from %s via %s\n",
+ inet_ntoa(dst), inet_ntoa(src),
+ device ? device : "unknown");
}
if (!src.s_addr && !(cfg&dad)) {
if ((cc = recvfrom(s, packet, 4096, 0,
(struct sockaddr *) &from, &alen)) < 0) {
- perror("recvfrom");
+ bb_perror_msg("recvfrom");
continue;
}
sigemptyset(&sset);