diff options
author | Thomas Habets <thomas@habets.se> | 2011-11-15 23:35:53 +0000 |
---|---|---|
committer | Thomas Habets <thomas@habets.se> | 2011-11-15 23:35:53 +0000 |
commit | 99e16682dc73aa56382cbe77e03365cbb0769011 (patch) | |
tree | 7e08b1845f071736d2b8b523c942a112b7589c6a | |
parent | 68af09288192ab84af52e6b86597479fb8db3275 (diff) | |
download | arping-99e16682dc73aa56382cbe77e03365cbb0769011.tar.gz |
Cleaned up interface-finding code.
-rw-r--r-- | src/arping.c | 69 | ||||
-rw-r--r-- | src/arping.h | 5 | ||||
-rw-r--r-- | src/findif_bsdroute.c | 7 | ||||
-rw-r--r-- | src/findif_getifaddrs.c | 17 | ||||
-rw-r--r-- | src/findif_linux.c | 7 | ||||
-rw-r--r-- | src/findif_other.c | 7 | ||||
-rw-r--r-- | src/findif_sysctl.c | 5 |
7 files changed, 53 insertions, 64 deletions
diff --git a/src/arping.c b/src/arping.c index cc396dc..54c2d0e 100644 --- a/src/arping.c +++ b/src/arping.c @@ -108,8 +108,7 @@ * OS-specific interface finding using routing table. See findif_*.c */ const char * -arping_lookupdev(const char *ifname, - uint32_t srcip, uint32_t dstip,char *ebuf); +arping_lookupdev(uint32_t srcip, uint32_t dstip,char *ebuf); static const char *version = VERSION; /* from autoconf */ @@ -154,7 +153,7 @@ static enum { NORMAL, QUIET, RAW, RRAW, RAWRAW, DOT } display = NORMAL; static const uint8_t ethnull[ETH_ALEN] = {0, 0, 0, 0, 0, 0}; static const uint8_t ethxmas[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; -int verbose = 0; +int verbose = 0; /* Increase with -v */ /* Doesn't really need to be volatile. */ static volatile sig_atomic_t time_to_die = 0; @@ -173,7 +172,7 @@ count_missing_dots() /** * - */ + */ void do_libnet_init(const char *ifname) { @@ -205,8 +204,7 @@ do_libnet_init(const char *ifname) * */ const char * -arping_lookupdev_default(const char *ifname, - uint32_t srcip, uint32_t dstip, +arping_lookupdev_default(int32_t srcip, uint32_t dstip, char *ebuf) { #if WIN32 @@ -253,23 +251,6 @@ static void sigint(int i) } /** - * idiot-proof gettimeofday() wrapper. - */ -static void -gettv(struct timespec *ts) -{ - struct timeval tv; - if (-1 == gettimeofday(&tv, NULL)) { - fprintf(stderr, "arping: " - "gettimeofday(): %s\n", - strerror(errno)); - sigint(0); - } - ts->tv_sec = tv.tv_sec; - ts->tv_nsec = tv.tv_usec * 1000; -} - -/** * idiot-proof clock_gettime() wrapper */ static void @@ -283,7 +264,14 @@ getclock(struct timespec *ts) sigint(0); } #else - gettv(ts); + struct timeval tv; + if (-1 == gettimeofday(&tv, NULL)) { + fprintf(stderr, "arping: gettimeofday(): %s\n", + strerror(errno)); + sigint(0); + } + ts->tv_sec = tv.tv_sec; + ts->tv_nsec = tv.tv_usec * 1000; #endif } @@ -574,7 +562,7 @@ pingip_send() ARPOP_REQUEST, srcmac, (uint8_t*)&srcip, - ethnull, + (uint8_t*)ethnull, (uint8_t*)&dstip, NULL, 0, @@ -1261,15 +1249,24 @@ int main(int argc, char **argv) * Get some good iface. */ if (!ifname) { - if (dont_use_arping_lookupdev) { - ifname = arping_lookupdev_default(ifname, - srcip,dstip,ebuf); - } else { - ifname = arping_lookupdev(ifname,srcip,dstip,ebuf); + if (!dont_use_arping_lookupdev) { + ifname = arping_lookupdev(srcip, dstip, ebuf); + } + if (!ifname) { + ifname = arping_lookupdev_default(srcip, dstip, ebuf); + if (!dont_use_arping_lookupdev) { + fprintf(stderr, + "arping: Unable to automatically find " + "interface to use. Is it on the local " + "LAN?\n" + "arping: Use -i to manually " + "specify interface. " + "Guessing interface %s.\n", ifname); + } } if (!ifname) { - fprintf(stderr, "arping: arping_lookupdev(): %s\n", - ebuf); + fprintf(stderr, "arping: Gave up looking for interface" + " to use: %s\n", ebuf); exit(1); } /* FIXME: check for other probably-not interfaces */ @@ -1347,8 +1344,12 @@ int main(int argc, char **argv) } if (!srcip_given) { if (-1 == (srcip = libnet_get_ipaddr4(libnet))) { - fprintf(stderr, "arping: libnet_get_ipaddr4(libnet): " - "%s\n", libnet_geterror(libnet)); + fprintf(stderr, + "arping: Unable to get the IPv4 address of " + "interface %s:\narping: %s" + "arping: " + "Use -S to specify address manually.\n", + ifname, libnet_geterror(libnet)); exit(1); } } diff --git a/src/arping.h b/src/arping.h index deda061..54c5961 100644 --- a/src/arping.h +++ b/src/arping.h @@ -11,6 +11,5 @@ extern uint32_t srcip,dstip; extern int verbose; void do_libnet_init(const char *ifname); -const char *arping_lookupdev_default(const char *ifname, - uint32_t srcip, uint32_t dstip, - char *ebuf); +const char *arping_lookupdev_default(uint32_t srcip, uint32_t dstip, + char *ebuf); diff --git a/src/findif_bsdroute.c b/src/findif_bsdroute.c index a458164..55b6e38 100644 --- a/src/findif_bsdroute.c +++ b/src/findif_bsdroute.c @@ -32,8 +32,7 @@ * */ const char * -arping_lookupdev(const char *ifname, - uint32_t srcip, +arping_lookupdev(uint32_t srcip, uint32_t dstip, char *ebuf) { @@ -43,7 +42,7 @@ arping_lookupdev(const char *ifname, char *p,*p2; int n; - do_libnet_init(ifname); + do_libnet_init(NULL); libnet_addr2name4_r(dstip,0,buf1, 1024); /* @@ -81,5 +80,5 @@ arping_lookupdev(const char *ifname, *p2 = 0; return p; failed: - return arping_lookupdev_default(ifname,srcip,dstip,ebuf); + return NULL; } diff --git a/src/findif_getifaddrs.c b/src/findif_getifaddrs.c index 8da77dd..b0f3d50 100644 --- a/src/findif_getifaddrs.c +++ b/src/findif_getifaddrs.c @@ -36,14 +36,13 @@ #include "arping.h" const char * -arping_lookupdev(const char *ifname_unused, - uint32_t srcip, +arping_lookupdev(uint32_t srcip, uint32_t dstip, char *ebuf) { struct ifaddrs *ifa = NULL; struct ifaddrs *cur; - const char *ret; + const char *ret = NULL; int match_count = 0; /* Matching interfaces */ /* best match */ @@ -57,7 +56,7 @@ arping_lookupdev(const char *ifname_unused, if (verbose) { printf("getifaddrs(): %s\n", strerror(errno)); } - goto failed; + goto out; } for (cur = ifa; cur; cur = cur->ifa_next) { in_addr_t addr, mask; @@ -81,22 +80,18 @@ arping_lookupdev(const char *ifname_unused, best_mask = mask; } } - if (!match_count) { + if (match_count) { + ret = ifname; + } else { if (verbose) { printf("Failed to find iface using getifaddrs().\n"); } - goto failed; } - ret = ifname; out: if (ifa) { freeifaddrs(ifa); } return ret; - - failed: - ret = arping_lookupdev_default(ifname_unused, srcip, dstip, ebuf); - goto out; } /* ---- Emacs Variables ---- * Local Variables: diff --git a/src/findif_linux.c b/src/findif_linux.c index 585329a..6213014 100644 --- a/src/findif_linux.c +++ b/src/findif_linux.c @@ -29,8 +29,7 @@ * WARNING: non-reentrant */ const char * -arping_lookupdev(const char *ifname, - uint32_t srcip, +arping_lookupdev(uint32_t srcip, uint32_t dstip, char *ebuf) { @@ -41,7 +40,7 @@ arping_lookupdev(const char *ifname, char *p,*p2; int n; - do_libnet_init(ifname); + do_libnet_init(NULL); libnet_addr2name4_r(dstip,0,buf2,1024); libnet_addr2name4_r(srcip,0,buf1,1024); @@ -80,5 +79,5 @@ arping_lookupdev(const char *ifname, *p2 = 0; return p; failed: - return arping_lookupdev_default(ifname,srcip,dstip,ebuf); + return NULL; } diff --git a/src/findif_other.c b/src/findif_other.c index 1952d05..83ab775 100644 --- a/src/findif_other.c +++ b/src/findif_other.c @@ -29,10 +29,7 @@ * */ const char * -arping_lookupdev(const char *ifname, - uint32_t srcip, - uint32_t dstip, - char *ebuf) +arping_lookupdev(uint32_t srcip, uint32_t dstip, char *ebuf) { - return arping_lookupdev_default(ifname,srcip,dstip,ebuf); + return NULL; } diff --git a/src/findif_sysctl.c b/src/findif_sysctl.c index ed10c7d..77d42e7 100644 --- a/src/findif_sysctl.c +++ b/src/findif_sysctl.c @@ -57,8 +57,7 @@ * */ const char * -arping_lookupdev(const char *ifname, - uint32_t srcip, +arping_lookupdev(uint32_t srcip, uint32_t dstip, char *ebuf) { @@ -232,7 +231,7 @@ arping_lookupdev(const char *ifname, return ifName; failed: - return arping_lookupdev_default(ifname, srcip, dstip, ebuf); + return NULL; } /* ---- Emacs Variables ---- |