diff options
author | Thomas Habets <thomas@habets.pp.se> | 2004-08-25 13:26:02 +0000 |
---|---|---|
committer | Thomas Habets <thomas@habets.pp.se> | 2004-08-25 13:26:02 +0000 |
commit | 61548aff79e61cb678ea80971a2281ffb2cb4468 (patch) | |
tree | 99ca0e51a2ff1a9addadf0ccaec9d299c04158d8 | |
parent | 754e7d16e6a77810a234d382b56272add58c0b8a (diff) | |
download | arping-61548aff79e61cb678ea80971a2281ffb2cb4468.tar.gz |
windows-related stuff mac/ip recognice bugfix -u switch libnet-not-
inited bugfix
-rw-r--r-- | arping-2/arping.c | 183 | ||||
-rw-r--r-- | arping.8 | 5 | ||||
-rw-r--r-- | arping.c | 66 | ||||
-rw-r--r-- | arping.yodl | 6 |
4 files changed, 222 insertions, 38 deletions
diff --git a/arping-2/arping.c b/arping-2/arping.c index 1db9a0c..1a66cbf 100644 --- a/arping-2/arping.c +++ b/arping-2/arping.c @@ -12,7 +12,7 @@ * * Also finds out IP of specified MAC * - * $Id: arping.c 1116 2004-08-05 02:36:34Z marvin $ + * $Id: arping.c 1123 2004-08-25 13:26:02Z marvin $ */ /* * Copyright (C) 2000-2002 Thomas Habets <thomas@habets.pp.se> @@ -34,6 +34,7 @@ //#include "config.h" #include <stdio.h> #include <stdlib.h> +#ifndef WIN32 #include <unistd.h> // NOTE: try un-commenting this //#include <stdint.h> @@ -44,10 +45,20 @@ #include <netinet/in.h> #include <arpa/inet.h> - #include <libnet.h> +#endif + +#ifdef WIN32 +#include <win32/libnet.h> +#endif #include <pcap.h> +#if defined(WIN32) +#define HAVE_ESIZE_TYPES 1 +#include "win32.h" +#include "win32/getopt.h" +#endif + #if defined(linux) #define HAVE_ESIZE_TYPES 1 #define FINDIF 1 @@ -78,7 +89,7 @@ #define IP_ALEN 4 #endif -const float version = 2.03; +const float version = 2.04f; static libnet_t *libnet = 0; @@ -88,6 +99,7 @@ static u_int32_t srcip,dstip; static int beep = 0; static int verbose = 0; +static int alsototal = 0; /*static int pingmac = 0; */ static int finddup = 0; static unsigned int numsent = 0; @@ -134,17 +146,28 @@ static void do_libnet_init(const char *ifname) /* * */ -const char *arping_lookupdev_default(u_int32_t srcip, u_int32_t dstip, +static const char *arping_lookupdev_default(u_int32_t srcip, u_int32_t dstip, char *ebuf) { +#ifdef WIN32 + WCHAR buf[LIBNET_ERRBUF_SIZE + PCAP_ERRBUF_SIZE]; + WCHAR* ret = (WCHAR*)pcap_lookupdev((char*)buf); + if (ret != NULL) { + wcstombs(ebuf, ret, LIBNET_ERRBUF_SIZE + PCAP_ERRBUF_SIZE); + return ebuf; + } + return NULL; +#else return pcap_lookupdev(ebuf); +#endif } #if defined(FINDIF) && defined(linux) /* * */ -const char *arping_lookupdev(u_int32_t srcip, u_int32_t dstip, char *ebuf) +static const char *arping_lookupdev(u_int32_t srcip, u_int32_t dstip, + char *ebuf) { FILE *f; static char buf[1024]; @@ -153,6 +176,7 @@ const char *arping_lookupdev(u_int32_t srcip, u_int32_t dstip, char *ebuf) char *p,*p2; int n; + do_libnet_init(NULL); libnet_addr2name4_r(dstip,0,buf2); libnet_addr2name4_r(srcip,0,buf1); @@ -197,12 +221,36 @@ const char *arping_lookupdev(u_int32_t srcip, u_int32_t dstip, char *ebuf) /* * */ -const char *arping_lookupdev(u_int32_t srcip, u_int32_t dstip, char *ebuf) +static const char *arping_lookupdev(u_int32_t srcip, u_int32_t dstip, + char *ebuf) { return arping_lookupdev_default(srcip,dstip,ebuf); } #endif + +#ifdef WIN32 +static BOOL WINAPI arping_console_ctrl_handler(DWORD dwCtrlType ) +{ + if(verbose) { + printf("arping_console_ctrl_handler( %d )\n", dwCtrlType ); + } + time_to_die = 1; + +#if 0 + /* if SetConsoleCtrlHandler() does what I think, this isn't needed */ + if (display == NORMAL) { + printf("\n--- %s statistics ---\n" + "%d packets transmitted, %d packets received, %3.0f%% " + "unanswered\n",target,numsent,numrecvd, + 100.0 - 100.0 * (float)(numrecvd)/(float)numsent); + } +#endif + return TRUE; +} +#endif + + /* * */ @@ -218,7 +266,7 @@ static void usage(int ret) { printf("ARPing %1.2f, by Thomas Habets <thomas@habets.pp.se>\n", version); - printf("usage: arping [ -0aAbdFpqrRv ] [ -w <us> ] [ -S <host/ip> ] " + printf("usage: arping [ -0aAbdFpqrRuv ] [ -w <us> ] [ -S <host/ip> ] " "[ -T <host/ip ]\n" " [ -s <MAC> ] [ -t <MAC> ] [ -c <count> ] " "[ -i <interface> ]\n" @@ -227,15 +275,46 @@ static void usage(int ret) } /* - * + * It was unclear from msdn.microsoft.com if their scanf() supported + * [0-9a-fA-F], so I'll stay away from it. */ static int is_mac_addr(const char *p) { - unsigned int n[6]; - if(6==sscanf(p, "%2x%2x.%2x%2x.%2x%2x", - &n[0],&n[1],&n[2],&n[3],&n[4],&n[5])){ + /* cisco-style */ + if (3*5-1 == strlen(p)) { + int c; + for (c = 0; c < strlen(p); c++) { + if ((c % 5) == 4) { + if ('.' != p[c]) { + goto checkcolon; + } + } else { + if (!isxdigit(p[c])) { + goto checkcolon; + } + } + } + return 1; + } + /* windows-style */ + if (6*3-1 == strlen(p)) { + int c; + for (c = 0; c < strlen(p); c++) { + if ((c % 3) == 2) { + if ('-' != p[c]) { + goto checkcolon; + } + } else { + if (!isxdigit(p[c])) { + goto checkcolon; + } + } + } return 1; } + + checkcolon: + /* unix */ return strchr(p, ':') ? 1 : 0; } @@ -254,6 +333,8 @@ static int get_mac_addr(const char *in, return 1; } else if(6 == sscanf(in, "%2x%x.%2x%x.%2x%x",n0,n1,n2,n3,n4,n5)) { return 1; + } else if(6 == sscanf(in, "%x-%x-%x-%x-%x-%x",n0,n1,n2,n3,n4,n5)) { + return 1; } return 0; } @@ -467,9 +548,13 @@ static void pingip_recv(const char *unused, struct pcap_pkthdr *h, (c<5)?':':' '); } - printf("(%s): index=%d time=%s", + printf("(%s): index=%d", libnet_addr2name4(ip,0), - numrecvd, + numrecvd); + if (alsototal) { + printf("/%u", numsent-1); + } + printf(" time=%s", tv2str(&lastpacketsent, &arrival,buf)); break; } @@ -550,7 +635,7 @@ static void pingmac_recv(const char *unused, struct pcap_pkthdr *h, (c<5)?':':')'); } printf(": icmp_seq=%d time=%s", - hicmp->icmp_seq,tv2str(&lastpacketsent, + htons(hicmp->icmp_seq),tv2str(&lastpacketsent, &arrival,buf)); break; } case RAW: @@ -586,17 +671,55 @@ static void pingmac_recv(const char *unused, struct pcap_pkthdr *h, */ static void ping_recv(pcap_t *pcap,u_int32_t packetwait, pcap_handler func) { - struct timeval tv,tv2; - char done = 0; - fd_set fds; + struct timeval tv,tv2; + char done = 0; +#ifndef WIN32 + fd_set fds; +#endif + if(verbose>3) { + printf("arping: receiving packets...\n"); + } + +#ifdef WIN32 + /* windows won't let us do select() */ + if (-1 == gettimeofday(&tv2,NULL)) { + fprintf(stderr, "arping: gettimeofday(): %s\n", + strerror(errno)); + sigint(0); + } + while (!done && !time_to_die) { + struct pcap_pkthdr *pkt_header; + u_char *pkt_data; + if (pcap_next_ex(pcap, &pkt_header, &pkt_data) == 1) { + func(pcap, pkt_header, pkt_data); + } + if (-1 == gettimeofday(&tv,NULL)) { + fprintf(stderr, "arping: " + "gettimeofday(): %s\n", + strerror(errno)); + sigint(0); + } + /* + * setup next timeval, not very exact + */ + tv.tv_sec = (packetwait / 1000000) + - (tv.tv_sec - tv2.tv_sec); + tv.tv_usec = (packetwait % 1000000) + - (tv.tv_usec - tv2.tv_usec); + while (tv.tv_usec < 0) { + tv.tv_sec--; + tv.tv_usec += 1000000; + } + usleep(10); + if (tv.tv_sec < 0) { + done=1; + } + } +#else tv.tv_sec = packetwait / 1000000; tv.tv_usec = packetwait % 1000000; - if(verbose>3) { - printf("arping: receiving packets...\n"); - } - for (;!done;) { int sr; FD_ZERO(&fds); @@ -675,6 +798,7 @@ static void ping_recv(pcap_t *pcap,u_int32_t packetwait, pcap_handler func) fprintf(stderr, "arping: select(delay): %s\n",strerror(errno)); sigint(0); } +#endif } /* @@ -706,7 +830,7 @@ int main(int argc, char **argv) memset(dstmac, 0xff, ETH_ALEN); memset(ethxmas, 0xff, ETH_ALEN); - while (EOF!=(c = getopt(argc, argv, "0aAbBc:dFhi:pqrRs:S:t:T:vw:"))) { + while (EOF!=(c=getopt(argc, argv, "0aAbBc:dFhi:I:pqrRs:S:t:T:uvw:"))) { switch(c) { case '0': srcip = 0; @@ -741,10 +865,13 @@ int main(int argc, char **argv) if (strchr(optarg, ':')) { fprintf(stderr, "arping: If you're trying to " "feed me an interface alias then you " - "don't really\nknow what this program " - "does, do you?\n"); + "don't really\nknow what this programs" + " does, do you?\nUse -I if you really" + " mean it (undocumented on " + "purpose)\n"); exit(1); } + case 'I': /* FALL THROUGH */ ifname = optarg; break; case 'p': @@ -823,6 +950,9 @@ int main(int argc, char **argv) } mode = PINGMAC; break; + case 'u': + alsototal = 1; + break; case 'v': verbose++; break; @@ -1001,7 +1131,12 @@ int main(int argc, char **argv) exit(1); } } +#ifdef WIN32 + SetConsoleCtrlHandler(NULL, TRUE); + SetConsoleCtrlHandler(arping_console_ctrl_handler, TRUE); +#else signal(SIGINT, sigint); +#endif if (verbose) { printf("This box: Interface: %s IP: %s MAC address: ", @@ -4,7 +4,7 @@ arping \- sends arp and/or ip pings to a given host .PP .SH "SYNOPSIS" -\fBarping\fP [-hqavrRd0bp] [-S \fIhost/ip\fP] [-T \fIhost/ip\fP] [-s \fIMAC\fP] [-t \fIMAC\fP] [-c \fIcount\fP] [-i \fIinterface\fP] <\fIhost\fP | -B> +\fBarping\fP [-abhpqrRd0uv] [-S \fIhost/ip\fP] [-T \fIhost/ip\fP] [-s \fIMAC\fP] [-t \fIMAC\fP] [-c \fIcount\fP] [-i \fIinterface\fP] [ -w \fIus\fP ] <\fIhost\fP | -B> .PP .SH "DESCRIPTION" The \fIarping\fP utility sends \fBARP\fP and/or \fBICMP\fP requests to the specified \fIhost\fP and displays the replies\&. The \fIhost\fP may be specified by its \fBhostname\fP, its \fBIP\fP address, or its \fBMAC\fP address\&. @@ -89,6 +89,9 @@ $ arping -S <IP-B> -s <MAC-B> -p <MAC-A> .IP "-p" Turn on promiscious mode on interface, use this if you don\&'t "own" the MAC address you are using\&. +.IP "-u" +Show index=received/sent instead of just index=received when +pinging MACs\&. .IP "-v" Verbose output\&. Use twice for more messages\&. .IP "-w" @@ -18,7 +18,7 @@ * * Also finds out IP of specified MAC * - * $Id: arping.c 984 2003-08-07 20:11:36Z marvin $ + * $Id: arping.c 1123 2004-08-25 13:26:02Z marvin $ */ /* * Copyright (C) 2000-2003 Thomas Habets <thomas@habets.pp.se> @@ -48,6 +48,7 @@ * arping -a mac audiable pongs * arping -A host nothing * arping -A mac nothing + * arping -u host pongs, index n/m * arping -t cmac -A host pongs * arping -t mac -A host pongs * arping -T ip -A mac pongs @@ -65,6 +66,8 @@ * Linux/x86 test with debian package libnet0-dev and libnet1-dev * Linux/sparc * Linux/hppa + * Linux/x86-64 + * IRIX/mips Need IRIX install CD to test this. * Solaris/sparc * NetBSD/alpha (is libnet or arping unaligned? -- libnet I think) * OpenBSD/sparc64 (libnet a bit buggy here) @@ -117,7 +120,7 @@ #define DEBUG(a) #endif -const float version = 1.08; +const float version = 1.09; struct ether_addr *mymac; static u_char eth_xmas[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; @@ -149,12 +152,41 @@ static unsigned int searchmac = 0; static unsigned int finddup = 0; static unsigned int maxcount = -1; static unsigned int rawoutput = 0; +static unsigned int alsototal = 0; static unsigned int quiet = 0; static unsigned int nullip = 0; static unsigned int is_promisc = 0; static unsigned int addr_must_be_same = 0; + +/* + * It was unclear from msdn.microsoft.com if their scanf() supported + * [0-9a-fA-F], so I'll stay away from it. + */ +static int is_mac_addr(const char *p) +{ + if (4+1+4+1+4 == strlen(p)) { + int c; + for (c = 0; c < strlen(p); c++) { + if ((9 == c) || (4 == c)) { + if (!strchr(".-", p[c])) { + goto checkcolon; + } + } else { + if (!isxdigit(p[c])) { + goto checkcolon; + } + } + } + return 1; + } + + checkcolon: + return strchr(p, ':') ? 1 : 0; +} + + const char *arping_lookupdev_default(u_int32_t srcip, u_int32_t dstip, char *ebuf) { @@ -251,7 +283,7 @@ static void usage(int ret) { printf("ARPing %1.2f, by Thomas Habets <thomas@habets.pp.se>\n", version); - printf("usage: arping [ -0aAbdFpqrRv ] [ -S <host/ip> ] " + printf("usage: arping [ -0aAbdFpqrRuv ] [ -S <host/ip> ] " "[ -T <host/ip ] [ -s <MAC> ]\n" " [ -t <MAC> ] [ -c <count> ] [ -i <interface> ] " "<host/ip/MAC | -B>\n"); @@ -455,9 +487,13 @@ static void handlepacket(const char *unused, struct pcap_pkthdr *h, printf("%.2x", *cp); } if (!rawoutput) { - printf(" (%s): index=%d time=%s", + printf(" (%s): index=%d", libnet_host_lookup(ip,0), - numrecvd, + numrecvd); + if (alsototal) { + printf("/%u",numsent-1); + } + printf(" time=%s", tvtoda(&lastpacketsent, &recvtime)); } @@ -500,7 +536,7 @@ int main(int argc, char **argv) memcpy(eth_target, eth_xmas, ETH_ALEN); - while ((c = getopt(argc, argv, "aAbBc:dF0S:T:hi:rRqs:t:pv")) != EOF) { + while ((c = getopt(argc, argv, "0aAbBc:dFhi:I:pqrRs:S:t:T:uv")) != EOF) { switch (c) { case 'A': addr_must_be_same = 1; @@ -521,6 +557,9 @@ int main(int argc, char **argv) #endif dont_use_arping_lookupdev=1; break; + case 'u': + alsototal=1; + break; case 'v': verbose++; break; @@ -531,9 +570,12 @@ int main(int argc, char **argv) fprintf(stderr, "arping: If you're trying to " "feed me an interface alias then you " "don't really\nknow what this programs" - " does, do you?\n"); + " does, do you?\nUse -I if you really" + " mean it (undocumented on " + "purpose)\n"); exit(1); } + case 'I': /* FALL THROUGH */ ifname = optarg; break; case 'r': @@ -584,7 +626,7 @@ int main(int argc, char **argv) &n[5] ) == 6) { ; - } else if(sscanf(optarg, "%2x%x.%2x%x.%2x%x", + } else if(sscanf(optarg, "%2x%2x.%2x%2x.%2x%2x", &n[0], &n[1], &n[2], @@ -614,7 +656,7 @@ int main(int argc, char **argv) &n[5] ) == 6) { ; - } else if(sscanf(optarg, "%2x%x.%2x%x.%2x%x", + } else if(sscanf(optarg, "%2x%2x.%2x%2x.%2x%2x", &n[0], &n[1], &n[2], @@ -655,7 +697,8 @@ int main(int argc, char **argv) exit(1); } - if (sscanf(argv[optind], "%x:%x:%x:%x:%x:%x", + if (is_mac_addr(argv[optind]) + && sscanf(argv[optind], "%x:%x:%x:%x:%x:%x", &n[0], &n[1], &n[2], @@ -664,7 +707,8 @@ int main(int argc, char **argv) &n[5] ) == 6) { searchmac = 1; - } else if(sscanf(argv[optind], "%2x%x.%2x%x.%2x%x", + } else if(is_mac_addr(argv[optind]) + && sscanf(argv[optind], "%2x%2x.%2x%2x.%2x%2x", &n[0], &n[1], &n[2], diff --git a/arping.yodl b/arping.yodl index 7e48c86..74906ca 100644 --- a/arping.yodl +++ b/arping.yodl @@ -3,8 +3,8 @@ manpage(arping)(8)(21th June, 2003)(arping)() manpagename(arping)(sends arp and/or ip pings to a given host) manpagesynopsis() - bf(arping) [-hqavrRd0bp] [-S em(host/ip)] [-T em(host/ip)] [-s em(MAC)] \ -[-t em(MAC)] [-c em(count)] [-i em(interface)] <em(host) | -B> + bf(arping) [-abhpqrRd0uv] [-S em(host/ip)] [-T em(host/ip)] [-s em(MAC)] \ +[-t em(MAC)] [-c em(count)] [-i em(interface)] [ -w em(us) ] <em(host) | -B> manpagedescription() The em(arping) utility sends bf(ARP) and/or bf(ICMP) requests to the specified em(host) and displays the replies. The em(host) may be specified by its bf(hostname), its bf(IP) address, or its bf(MAC) address. @@ -73,6 +73,8 @@ em(Example): \ $ arping -S <IP-B> -s <MAC-B> -p <MAC-A> dit(-p) Turn on promiscious mode on interface, use this if you don't "own" the MAC address you are using. + dit(-u) Show index=received/sent instead of just index=received when + pinging MACs. dit(-v) Verbose output. Use twice for more messages. dit(-w) (arping 2.x only) Time to wait between pings, in microseconds. enddit() |