diff options
author | Thomas Habets <thomas@habets.pp.se> | 2003-08-03 22:57:59 +0000 |
---|---|---|
committer | Thomas Habets <thomas@habets.pp.se> | 2003-08-03 22:57:59 +0000 |
commit | 6c10760e09012f5b5f103926c33c61a798bbcc4a (patch) | |
tree | f8b810148a8efe3c92b114df6dfba8876f3b1da9 | |
parent | b6a2bdccf5c80c360cbb9983ab561059f1fdce9f (diff) | |
download | arping-6c10760e09012f5b5f103926c33c61a798bbcc4a.tar.gz |
.
-rw-r--r-- | arping-2/arping.c | 111 | ||||
-rwxr-xr-x | arping-scan-net.sh | 4 | ||||
-rw-r--r-- | arping.c | 198 |
3 files changed, 184 insertions, 129 deletions
diff --git a/arping-2/arping.c b/arping-2/arping.c index 8d38a7d..7ba9dc4 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 923 2003-06-21 16:28:11Z marvin $ + * $Id: arping.c 974 2003-08-03 22:57:59Z marvin $ */ /* * Copyright (C) 2000-2002 Thomas Habets <thomas@habets.pp.se> @@ -184,6 +184,38 @@ static void usage(int ret) } /* + * + */ +static int is_mac_addr(const char *p) +{ + unsigned int n[6]; + if(6==sscanf(p, "%2x%x.%2x%x.%2x%x", + &n[0],&n[1],&n[2],&n[3],&n[4],&n[5])){ + return 1; + } + return strchr(p, ':') ? 1 : 0; +} + +/* + * lots of parms since C arrays suck + */ +static int get_mac_addr(const char *in, + unsigned int *n0, + unsigned int *n1, + unsigned int *n2, + unsigned int *n3, + unsigned int *n4, + unsigned int *n5) +{ + if (6 == sscanf(in, "%x:%x:%x:%x:%x:%x",n0,n1,n2,n3,n4,n5)) { + return 1; + } else if(6 == sscanf(in, "%2x%x.%2x%x.%2x%x",n0,n1,n2,n3,n4,n5)) { + return 1; + } + return 0; +} + +/* * as always, the answer is 42 * * in this case the question is how many bytes buf needs to be. @@ -285,7 +317,7 @@ static void pingmac_send(u_int8_t *srcmac, u_int8_t *dstmac, printf("arping: sending packet\n"); } if (-1 == (c = libnet_write(libnet))) { - fprintf(stderr, "libnet_write(): %s\n", + fprintf(stderr, "arping: libnet_write(): %s\n", libnet_geterror(libnet)); sigint(0); } @@ -317,7 +349,7 @@ static void pingip_send(u_int8_t *srcmac, u_int8_t *dstmac, 0, libnet, arp))) { - fprintf(stderr, "libnet_build_arp(): %s\n", + fprintf(stderr, "arping: libnet_build_arp(): %s\n", libnet_geterror(libnet)); sigint(0); } @@ -328,7 +360,7 @@ static void pingip_send(u_int8_t *srcmac, u_int8_t *dstmac, 0, libnet, eth))) { - fprintf(stderr, "libnet_build_ethernet(): %s\n", + fprintf(stderr, "arping: libnet_build_ethernet(): %s\n", libnet_geterror(libnet)); sigint(0); } @@ -392,7 +424,7 @@ static void pingip_recv(const char *unused, struct pcap_pkthdr *h, (c<5)?':':' '); } - printf("(%s): index=%d time=%s\n", + printf("(%s): index=%d time=%s", libnet_addr2name4(ip,0), numrecvd, tv2str(&lastpacketsent, @@ -405,20 +437,21 @@ static void pingip_recv(const char *unused, struct pcap_pkthdr *h, printf("%.2x%c", heth->_802_3_shost[c], (c<5)?':':' '); } - printf("%s\n", libnet_addr2name4(ip,0)); + printf("%s", libnet_addr2name4(ip,0)); break; case RRAW: - printf("%s\n", libnet_addr2name4(ip,0)); + printf("%s", libnet_addr2name4(ip,0)); break; case RAW: for (c = 0; c < 6; c++) { - printf("%.2x%c", heth->_802_3_shost[c], - (c<5)?':':'\n'); + printf("%.2x%s", heth->_802_3_shost[c], + (c<5)?":":""); } break; default: fprintf(stderr, "arping: can't happen!\n"); } + printf(beep?"\a\n":"\n"); numrecvd++; } } @@ -473,18 +506,18 @@ static void pingmac_recv(const char *unused, struct pcap_pkthdr *h, printf("%.2x%c", heth->_802_3_shost[c], (c<5)?':':')'); } - printf(": icmp_seq=%d time=%s\n", + printf(": icmp_seq=%d time=%s", hicmp->icmp_seq,tv2str(&lastpacketsent, &arrival,buf)); break; } case RAW: - printf("%s\n", + printf("%s", libnet_addr2name4(hip->ip_src.s_addr, 0)); break; case RRAW: for (c = 0; c < 6; c++) { - printf("%.2x%c", heth->_802_3_shost[c], - (c<5)?':':'\n'); + printf("%.2x%s", heth->_802_3_shost[c], + (c<5)?":":""); } break; case RAWRAW: @@ -492,13 +525,14 @@ static void pingmac_recv(const char *unused, struct pcap_pkthdr *h, printf("%.2x%c", heth->_802_3_shost[c], (c<5)?':':' '); } - printf("%s\n", + printf("%s", libnet_addr2name4(hip->ip_src.s_addr, 0)); break; default: fprintf(stderr, "arping: can't-happen-bug\n"); sigint(0); } + printf(beep?"\a\n":"\n"); numrecvd++; } } @@ -681,15 +715,9 @@ int main(int argc, char **argv) break; case 's': {// spoof source MAC unsigned int n[6]; - - if (sscanf(optarg, "%x:%x:%x:%x:%x:%x", - &n[0], - &n[1], - &n[2], - &n[3], - &n[4], - &n[5] - ) != 6) { + if (!get_mac_addr(optarg, + &n[0],&n[1],&n[2], + &n[3],&n[4],&n[5])){ fprintf(stderr, "arping: Weird MAC addr %s\n", optarg); exit(1); @@ -717,15 +745,9 @@ int main(int argc, char **argv) "in IP ping mode\n"); exit(1); } - - if (sscanf(optarg, "%x:%x:%x:%x:%x:%x", - &n[0], - &n[1], - &n[2], - &n[3], - &n[4], - &n[5] - ) != 6) { + if (!get_mac_addr(optarg, + &n[0],&n[1],&n[2], + &n[3],&n[4],&n[5])){ fprintf(stderr, "Illegal MAC addr %s\n", optarg); exit(1); @@ -786,8 +808,9 @@ int main(int argc, char **argv) * Handle dstip_given instead of ip address after parms (-B really) */ if (mode == NONE) { + unsigned char n[6]; if (optind + 1 == argc) { - mode = strchr(parm, ':')?PINGMAC:PINGIP; + mode = is_mac_addr(parm)?PINGMAC:PINGIP; } else if (dstip_given) { mode = PINGIP; parm = strdup(libnet_addr2name4(dstip, @@ -803,7 +826,7 @@ int main(int argc, char **argv) * Make sure dstip and parm like eachother */ if (mode == PINGIP && (!dstip_given)) { - if (strchr(parm, ':')) { + if (is_mac_addr(parm)) { fprintf(stderr, "arping: Options given only apply to " "IP ping, but MAC address given as argument" "\n"); @@ -822,25 +845,21 @@ int main(int argc, char **argv) * parse parm into dstmac */ if (mode == PINGMAC) { - unsigned char n[6]; + unsigned int n[6]; if (optind + 1 != argc) { usage(1); } - if (!strchr(parm, ':')) { + if (!is_mac_addr(parm)) { fprintf(stderr, "arping: Options given only apply to " "MAC ping, but no MAC address given as " "argument\n"); exit(1); } - if (sscanf(argv[optind], "%x:%x:%x:%x:%x:%x", - &n[0], - &n[1], - &n[2], - &n[3], - &n[4], - &n[5] - ) != 6) { - fprintf(stderr, "Illegal mac addr %s\n", argv[optind]); + if (!get_mac_addr(argv[optind], + &n[0],&n[1],&n[2], + &n[3],&n[4],&n[5])) { + fprintf(stderr, "arping: Illegal mac addr %s\n", + argv[optind]); return 1; } for (c = 0; c < 6; c++) { @@ -920,7 +939,7 @@ int main(int argc, char **argv) */ if (!srcmac_given) { if (!(cp = (char*)libnet_get_hwaddr(libnet))) { - fprintf(stderr, "libnet_get_hwaddr(): %s\n", + fprintf(stderr, "arping: libnet_get_hwaddr(): %s\n", libnet_geterror(libnet)); exit(1); } diff --git a/arping-scan-net.sh b/arping-scan-net.sh index cbd340e..d88e5d1 100755 --- a/arping-scan-net.sh +++ b/arping-scan-net.sh @@ -1,5 +1,5 @@ #!/bin/sh -# $Id: arping-scan-net.sh 922 2003-06-21 16:26:53Z marvin $ +# $Id: arping-scan-net.sh 974 2003-08-03 22:57:59Z marvin $ # # Copyright (C) 2002 Thomas Habets <thomas@habets.pp.se> # @@ -43,7 +43,7 @@ seq() { echo "for (i=$1; i<=$2; i++) i;" | bc | tr "\012" " "; } # for a in $(seq 192 192); do for b in $(seq 168 168); do - for c in $(seq 0 0); do + for c in $(seq 42 42); do for d in $(seq 0 255); do sh -c "arping -A -q -c 1 -T $a.$b.$c.$d $TARGET_MAC if [ \$? = 0 ]; then @@ -12,7 +12,7 @@ * * Also finds out IP of specified MAC * - * $Id: arping.c 925 2003-06-21 16:42:02Z marvin $ + * $Id: arping.c 974 2003-08-03 22:57:59Z marvin $ */ /* * Copyright (C) 2000-2003 Thomas Habets <thomas@habets.pp.se> @@ -33,30 +33,34 @@ */ /* * Note to self: - * Test checklist: + * Test checklist: (cmac = mac in cisco format (0000.0000.0000) * command expected response * arping host pongs * arping -a host audiable pongs * arping mac pongs + * arping cmac pongs * arping -a mac audiable pongs * arping -A host nothing * arping -A mac nothing - * arping -t mac -A host nothing - * arping -T ip -A mac nothing + * arping -t cmac -A host pongs + * arping -t mac -A host pongs + * arping -T ip -A mac pongs + * arping -T ip -A cmac pongs * arping -r host mac * arping -R host ip * arping -r mac ip * arping -R mac mac * arping -rR mac mac ip + * arping -rR ip mac ip * ./arping-scan-net.sh mac ip * * Arch checklist: + * Linux/x86 test with debian package libnet0-dev and libnet1-dev + * Linux/sparc + * Linux/hppa * Solaris/sparc * NetBSD/alpha (is libnet or arping unaligned? -- libnet I think) * OpenBSD/sparc64 (libnet a bit buggy here) - * Linux/x86 - * Linux/sparc - * Linux/hppa * */ #include <stdio.h> @@ -445,8 +449,10 @@ static void handlepacket(const char *unused, struct pcap_pkthdr *h, } if (!rawoutput) { printf(" (%s): index=%d time=%s", - libnet_host_lookup(ip, 0), - numrecvd, tvtoda(&lastpacketsent, &recvtime)); + libnet_host_lookup(ip,0), + numrecvd, + tvtoda(&lastpacketsent, + &recvtime)); } if (beep) { printf("\a"); @@ -477,6 +483,7 @@ int main(int argc, char **argv) struct bpf_program bp; char must_be_pingip = 0; char have_eth_source = 0; + unsigned int n[6]; int dont_use_arping_lookupdev=0; DEBUG(printf("main()\n")); @@ -503,7 +510,7 @@ int main(int argc, char **argv) case 'F': #if !defined(FINDIF) fprintf(stderr, "arping: find-interface support not " - "compiled in\n"; + "compiled in\n"); #endif dont_use_arping_lookupdev=1; break; @@ -532,9 +539,8 @@ int main(int argc, char **argv) quiet = rawoutput = 1; break; case 'S': - if ((myip = libnet_name_resolve(optarg, - LIBNET_RESOLVE)) - == -1) { + if (-1==(myip = libnet_name_resolve(optarg, + LIBNET_RESOLVE))){ fprintf(stderr,"arping: Can't to resolve %s\n", optarg); exit(1); @@ -544,8 +550,12 @@ int main(int argc, char **argv) } break; case 'T': // destination IP in mac ping (default: 0xffffffff) - dip = libnet_name_resolve(optarg, - LIBNET_RESOLVE); + if (-1 == (dip = libnet_name_resolve(optarg, + LIBNET_RESOLVE))){ + fprintf(stderr, "arping: can't resolve: %s\n", + optarg); + exit(1); + } searchmac = 1; break; case 'b': @@ -558,47 +568,62 @@ int main(int argc, char **argv) nullip = 1; break; case 's': // spoofed source MAC - { - unsigned int n[6]; - - if (sscanf(optarg, "%x:%x:%x:%x:%x:%x", - &n[0], - &n[1], - &n[2], - &n[3], - &n[4], - &n[5] - ) != 6) { - fprintf(stderr, "Illegal MAC addr %s\n", - optarg); - exit(1); - } - for (c = 0; c < 6; c++) { - eth_source[c] = n[c] & 0xff; - } - have_eth_source = 1; + if (sscanf(optarg, "%x:%x:%x:%x:%x:%x", + &n[0], + &n[1], + &n[2], + &n[3], + &n[4], + &n[5] + ) == 6) { + ; + } else if(sscanf(optarg, "%2x%x.%2x%x.%2x%x", + &n[0], + &n[1], + &n[2], + &n[3], + &n[4], + &n[5] + ) == 6) { + ; + } else { + fprintf(stderr, "arping: Illegal MAC addr " + "%s\n", optarg); + exit(1); + } + for (c = 0; c < 6; c++) { + eth_source[c] = n[c] & 0xff; } + have_eth_source = 1; break; case 't': must_be_pingip = 1; - { - unsigned int n[6]; - - if (sscanf(optarg, "%x:%x:%x:%x:%x:%x", - &n[0], - &n[1], - &n[2], - &n[3], - &n[4], - &n[5] - ) != 6) { - fprintf(stderr, "Illegal MAC addr %s\n", - optarg); - exit(1); - } - for (c = 0; c < 6; c++) { - eth_target[c] = n[c] & 0xff; - } + if (sscanf(optarg, "%x:%x:%x:%x:%x:%x", + &n[0], + &n[1], + &n[2], + &n[3], + &n[4], + &n[5] + ) == 6) { + ; + } else if(sscanf(optarg, "%2x%x.%2x%x.%2x%x", + &n[0], + &n[1], + &n[2], + &n[3], + &n[4], + &n[5] + ) == 6) { + ; + } else { + fprintf(stderr,"arping: Illegal MAC addr %s\n", + optarg); + exit(1); + } + + for (c = 0; c < 6; c++) { + eth_target[c] = n[c] & 0xff; } break; case 'p': @@ -623,41 +648,52 @@ int main(int argc, char **argv) exit(1); } - if (searchmac || (!dip && strchr(argv[optind], ':'))) { - unsigned int n[6]; - if (!dip) { - dip = ip_xmas; - } - - if (must_be_pingip) { - fprintf(stderr, "Specified switch can't be used in " - "MAC-ping mode\n"); - exit(1); - } - - if (sscanf(argv[optind], "%x:%x:%x:%x:%x:%x", - &n[0], - &n[1], - &n[2], - &n[3], - &n[4], - &n[5] - ) != 6) { - fprintf(stderr, "Illegal mac addr %s\n", argv[optind]); - return 1; - } - for (c = 0; c < 6; c++) { - eth_target[c] = n[c] & 0xff; - } + if (sscanf(argv[optind], "%x:%x:%x:%x:%x:%x", + &n[0], + &n[1], + &n[2], + &n[3], + &n[4], + &n[5] + ) == 6) { + searchmac = 1; + } else if(sscanf(argv[optind], "%2x%x.%2x%x.%2x%x", + &n[0], + &n[1], + &n[2], + &n[3], + &n[4], + &n[5] + ) == 6) { searchmac = 1; } else if (!dip) { - - if (-1 == (dip = libnet_name_resolve((u_char*)argv[optind], - LIBNET_RESOLVE))) { + if (-1 == (dip=libnet_name_resolve((u_char*)argv[optind], + LIBNET_RESOLVE))) { fprintf(stderr, "arping: Can't resolve %s\n", argv[optind]); exit(1); } + } else if (must_be_pingip) { + fprintf(stderr, "arping: Illegal IP %s\n", argv[optind]); + exit(1); + } else { + fprintf(stderr, "arping: Illegal mac %s\n", argv[optind]); + exit(1); + } + if (searchmac && !dip) { + dip = ip_xmas; + } + + if (searchmac && must_be_pingip) { + fprintf(stderr, "arping: Specified switch can't be used in " + "MAC-ping mode\n"); + exit(1); + } + + if (searchmac) { + for (c = 0; c < 6; c++) { + eth_target[c] = n[c] & 0xff; + } } if (finddup && maxcount == -1) { maxcount = 3; |