--- embedaddon/choparp/choparp.c 2012/02/21 16:49:51 1.1.1.1 +++ embedaddon/choparp/choparp.c 2013/07/22 01:01:52 1.1.1.1.2.1 @@ -42,7 +42,6 @@ #include #include #include -#include #include #include #include @@ -76,7 +75,6 @@ struct cidr { struct cidr *targets = NULL, *excludes = NULL; u_char target_mac[ETHER_ADDR_LEN]; /* target MAC address */ -int verbose = 0; /* ARP filter program @@ -241,16 +239,6 @@ checkarp(char *arpbuf){ fprintf(stderr,"checkarp: WARNING: received unknown type ARP request.\n"); return(0); } - if (ntohl(*(u_int32_t *)(arp->arp_tpa)) == ntohl(*(u_int32_t *)(arp->arp_spa))) { - if (verbose != 0) - fprintf(stderr,"checkarp: WARNING: sender equal dest.\n"); - return(0); - } - if (0 == ntohl(*(u_int32_t *)(arp->arp_spa))) { - if (verbose != 0) - fprintf(stderr,"checkarp: WARNING: zero sender address.\n"); - return(0); - } target_ip = ntohl(*(u_int32_t *)(arp->arp_tpa)); return match(target_ip, targets) && !match(target_ip, excludes); } @@ -292,22 +280,13 @@ loop(int fd, char *buf, size_t buflen){ char *rframe; char *sframe; size_t frame_len; - int kq; - struct kevent kev; + fd_set fdset; - if ((kq = kqueue()) < 0) { - perror("kqueue"); - return; - } + FD_ZERO(&fdset); + FD_SET(fd,&fdset); - EV_SET(&kev, fd, EVFILT_READ, EV_ADD, 0, 0, NULL); - if (kevent(kq, &kev, 1, NULL, 0, NULL) < 0 ) { - perror("kevent"); - return; - } - for(;;){ - int r = kevent(kq, NULL, 0, &kev, 1, NULL); + int r = select(fd+1,&fdset, 0, 0, 0); if (r < 0) { if (errno == EINTR) @@ -316,7 +295,7 @@ loop(int fd, char *buf, size_t buflen){ return; } - rlen = read(kev.ident, buf, buflen); + rlen = read(fd, buf, buflen); if (rlen < 0) { if (errno == EINTR) continue; @@ -328,7 +307,7 @@ loop(int fd, char *buf, size_t buflen){ while((rframe = getarp(p, rlen, &nextp, &nextlen)) != NULL){ if (checkarp(rframe)){ sframe = gen_arpreply(rframe, &frame_len); - write(kev.ident, sframe, frame_len); + write(fd, sframe, frame_len); } p = nextp; rlen = nextlen; @@ -383,13 +362,13 @@ atoip(char *buf, u_int32_t *ip_addr){ void usage(void){ - fprintf(stderr,"usage: choparp [-v] if_name mac_addr [-]addr/mask...\n"); + fprintf(stderr,"usage: choparp if_name mac_addr [-]addr/mask...\n"); exit(-1); } int main(int argc, char **argv){ - int ch, fd; + int fd; char *buf, *ifname; struct cidr **targets_tail = &targets, **excludes_tail = &excludes; #define APPEND(LIST,ADDR,MASK) \ @@ -402,24 +381,13 @@ main(int argc, char **argv){ } while (0) size_t buflen; - while ((ch = getopt(argc, argv, "v")) != -1) - switch (ch) { - case 'v': - verbose++; - break; - default: - usage(); - } - argc -= optind; - argv += optind; - - if (argc < 3) + if (argc < 4) usage(); - ifname = argv[0]; - if (setmac(argv[1], ifname)) + ifname = argv[1]; + if (setmac(argv[2], ifname)) usage(); - argv += 2; argc -= 2; + argv += 3; argc -= 3; while (argc > 0) { u_int32_t addr, mask = ~0; @@ -469,9 +437,6 @@ main(int argc, char **argv){ #endif if ((fd = openbpf(ifname, &buf, &buflen)) < 0) return(-1); - #ifndef DEBUG - daemon(0, 0); - #endif loop(fd, buf, buflen); return(-1); }