|
|
| version 1.4.12.2, 2016/08/11 16:23:43 | version 1.5, 2016/08/18 09:06:31 |
|---|---|
| Line 1 | Line 1 |
| #ifdef __linux__ | |
| int main() | |
| { | |
| return 0; | |
| } | |
| #else | |
| #include <stdio.h> | #include <stdio.h> |
| #include <stdlib.h> | #include <stdlib.h> |
| #include <fcntl.h> | #include <fcntl.h> |
| Line 17 int main() | Line 10 int main() |
| #include <assert.h> | #include <assert.h> |
| #include <sys/types.h> | #include <sys/types.h> |
| #include <sys/param.h> | #include <sys/param.h> |
| #include <sys/limits.h> | |
| #include <sys/socket.h> | #include <sys/socket.h> |
| #include <sys/ioctl.h> | #include <sys/ioctl.h> |
| #include <sys/mman.h> | #include <sys/mman.h> |
| #include <net/if.h> | #include <net/if.h> |
| #ifndef __linux__ | |
| #include <sys/limits.h> | |
| #include <net/bpf.h> | #include <net/bpf.h> |
| #include <machine/atomic.h> | #include <machine/atomic.h> |
| #else | |
| #include <linux/if_ether.h> | |
| #endif | |
| #ifdef __FreeBSD__ | #ifdef __FreeBSD__ |
| #include <net/ethernet.h> | #include <net/ethernet.h> |
| Line 68 static void * | Line 65 static void * |
| ShowPkt(void *buffer) | ShowPkt(void *buffer) |
| { | { |
| char Proto = 0, szStr[BUFSIZ], szLine[BUFSIZ], szWrk[BUFSIZ], szShow[USHRT_MAX] = { 0 }; | char Proto = 0, szStr[BUFSIZ], szLine[BUFSIZ], szWrk[BUFSIZ], szShow[USHRT_MAX] = { 0 }; |
| #ifndef __linux__ | |
| #ifdef __FreeBSD__ | #ifdef __FreeBSD__ |
| struct bpf_zbuf_header *bzh = buffer; | struct bpf_zbuf_header *bzh = buffer; |
| struct icmphdr *icmp; | struct icmphdr *icmp; |
| Line 75 ShowPkt(void *buffer) | Line 73 ShowPkt(void *buffer) |
| #ifdef __OpenBSD__ | #ifdef __OpenBSD__ |
| struct icmp *icmp; | struct icmp *icmp; |
| #endif | #endif |
| struct ether_header *eth; | struct ether_header *eth; |
| #else | |
| struct icmphdr *icmp; | |
| struct ethhdr *eth; | |
| #endif | |
| struct ip *ip; | struct ip *ip; |
| struct ip6_hdr *ipv6; | struct ip6_hdr *ipv6; |
| struct arphdr *arp; | struct arphdr *arp; |
| Line 84 ShowPkt(void *buffer) | Line 87 ShowPkt(void *buffer) |
| assert(buffer); | assert(buffer); |
| #ifndef __linux__ | |
| eth = (struct ether_header *) buffer; | eth = (struct ether_header *) buffer; |
| switch (ntohs(eth->ether_type)) { | switch (ntohs(eth->ether_type)) { |
| #else | |
| #define ETHERTYPE_ARP 0x0806 | |
| #define ETHERTYPE_IP 0x0800 | |
| #define ETHERTYPE_IPV6 0x86dd | |
| #define ETHER_HDR_LEN sizeof(struct ethhdr) | |
| eth = (struct ethhdr *) buffer; | |
| switch (ntohs(eth->h_proto)) { | |
| #endif | |
| case ETHERTYPE_ARP: | case ETHERTYPE_ARP: |
| strlcpy(szWrk, "(ARP)", sizeof szWrk); | strlcpy(szWrk, "(ARP)", sizeof szWrk); |
| break; | break; |
| Line 99 ShowPkt(void *buffer) | Line 113 ShowPkt(void *buffer) |
| default: | default: |
| memset(szWrk, 0, sizeof szWrk); | memset(szWrk, 0, sizeof szWrk); |
| } | } |
| #ifndef __linux__ | |
| snprintf(szStr, BUFSIZ, "%02x:%02x:%02x:%02x:%02x:%02x", eth->ether_shost[0], eth->ether_shost[1], | snprintf(szStr, BUFSIZ, "%02x:%02x:%02x:%02x:%02x:%02x", eth->ether_shost[0], eth->ether_shost[1], |
| eth->ether_shost[2], eth->ether_shost[3], eth->ether_shost[4], eth->ether_shost[5]); | eth->ether_shost[2], eth->ether_shost[3], eth->ether_shost[4], eth->ether_shost[5]); |
| snprintf(szLine, BUFSIZ, "ether_type: %04x%s, src_mac: %s, ", ntohs(eth->ether_type), szWrk, szStr); | snprintf(szLine, BUFSIZ, "ether_type: %04x%s, src_mac: %s, ", ntohs(eth->ether_type), szWrk, szStr); |
| strlcat(szShow, szLine, USHRT_MAX); | strlcat(szShow, szLine, USHRT_MAX); |
| snprintf(szStr, BUFSIZ, "%02x:%02x:%02x:%02x:%02x:%02x", eth->ether_dhost[0], eth->ether_dhost[1], | snprintf(szStr, BUFSIZ, "%02x:%02x:%02x:%02x:%02x:%02x", eth->ether_dhost[0], eth->ether_dhost[1], |
| eth->ether_dhost[2], eth->ether_dhost[3], eth->ether_dhost[4], eth->ether_dhost[5]); | eth->ether_dhost[2], eth->ether_dhost[3], eth->ether_dhost[4], eth->ether_dhost[5]); |
| #else | |
| snprintf(szStr, BUFSIZ, "%02x:%02x:%02x:%02x:%02x:%02x", eth->h_source[0], eth->h_source[1], | |
| eth->h_source[2], eth->h_source[3], eth->h_source[4], eth->h_source[5]); | |
| snprintf(szLine, BUFSIZ, "ether_type: %04x%s, src_mac: %s, ", ntohs(eth->h_proto), szWrk, szStr); | |
| strlcat(szShow, szLine, USHRT_MAX); | |
| snprintf(szStr, BUFSIZ, "%02x:%02x:%02x:%02x:%02x:%02x", eth->h_dest[0], eth->h_dest[1], | |
| eth->h_dest[2], eth->h_dest[3], eth->h_dest[4], eth->h_dest[5]); | |
| #endif | |
| snprintf(szLine, BUFSIZ, "dst_mac: %s\n", szStr); | snprintf(szLine, BUFSIZ, "dst_mac: %s\n", szStr); |
| strlcat(szShow, szLine, USHRT_MAX); | strlcat(szShow, szLine, USHRT_MAX); |
| #ifndef __linux__ | |
| switch (ntohs(eth->ether_type)) { | switch (ntohs(eth->ether_type)) { |
| #else | |
| #define ARPOP_REVREQUEST 3 | |
| #define ARPOP_REVREPLY 4 | |
| #define ARPOP_INVREQUEST 8 | |
| #define ARPOP_INVREPLY 9 | |
| #define IPV6_FLOWLABEL_MASK 0x000fffff | |
| switch (ntohs(eth->h_proto)) { | |
| #endif | |
| case ETHERTYPE_ARP: | case ETHERTYPE_ARP: |
| arp = (struct arphdr*) (((caddr_t) eth) + ETHER_HDR_LEN); | arp = (struct arphdr*) (((caddr_t) eth) + ETHER_HDR_LEN); |
| strlcat(szShow, "\t>>> ARP ...\n", USHRT_MAX); | strlcat(szShow, "\t>>> ARP ...\n", USHRT_MAX); |
| Line 177 ShowPkt(void *buffer) | Line 210 ShowPkt(void *buffer) |
| break; | break; |
| } | } |
| #ifndef __linux__ | |
| if (Proto && (ntohs(eth->ether_type) == ETHERTYPE_IP || ntohs(eth->ether_type) == ETHERTYPE_IPV6)) | if (Proto && (ntohs(eth->ether_type) == ETHERTYPE_IP || ntohs(eth->ether_type) == ETHERTYPE_IPV6)) |
| #else | |
| if (Proto && (ntohs(eth->h_proto) == ETHERTYPE_IP || ntohs(eth->h_proto) == ETHERTYPE_IPV6)) | |
| #endif | |
| switch (Proto) { | switch (Proto) { |
| case IPPROTO_TCP: | case IPPROTO_TCP: |
| strlcat(szShow, "\t\t>>> TCP ...\n", USHRT_MAX); | strlcat(szShow, "\t\t>>> TCP ...\n", USHRT_MAX); |
| #ifndef __linux__ | |
| if (ntohs(eth->ether_type) == ETHERTYPE_IPV6) | if (ntohs(eth->ether_type) == ETHERTYPE_IPV6) |
| #else | |
| if (ntohs(eth->h_proto) == ETHERTYPE_IPV6) | |
| #endif | |
| tcp = (struct tcphdr*) (((caddr_t) ipv6) + sizeof(struct ip6_hdr)); | tcp = (struct tcphdr*) (((caddr_t) ipv6) + sizeof(struct ip6_hdr)); |
| else | else |
| tcp = (struct tcphdr*) (((caddr_t) ip) + sizeof(struct ip)); | tcp = (struct tcphdr*) (((caddr_t) ip) + sizeof(struct ip)); |
| #ifndef __linux__ | |
| snprintf(szLine, BUFSIZ, "\t\tsrc_port: %d, dst_port: %d, seq: %u, ack: %u\n", | snprintf(szLine, BUFSIZ, "\t\tsrc_port: %d, dst_port: %d, seq: %u, ack: %u\n", |
| ntohs(tcp->th_sport), ntohs(tcp->th_dport), tcp->th_seq, tcp->th_ack); | ntohs(tcp->th_sport), ntohs(tcp->th_dport), tcp->th_seq, tcp->th_ack); |
| strlcat(szShow, szLine, USHRT_MAX); | strlcat(szShow, szLine, USHRT_MAX); |
| Line 208 ShowPkt(void *buffer) | Line 250 ShowPkt(void *buffer) |
| strlcat(szWrk, "|CWR", sizeof szWrk); | strlcat(szWrk, "|CWR", sizeof szWrk); |
| snprintf(szLine, BUFSIZ, "\t\toff: %s, win: %d, urp: %04x, cksum: %04x\n", | snprintf(szLine, BUFSIZ, "\t\toff: %s, win: %d, urp: %04x, cksum: %04x\n", |
| szWrk, ntohs(tcp->th_win), ntohs(tcp->th_urp), ntohs(tcp->th_sum)); | szWrk, ntohs(tcp->th_win), ntohs(tcp->th_urp), ntohs(tcp->th_sum)); |
| #else | |
| snprintf(szLine, BUFSIZ, "\t\tsrc_port: %d, dst_port: %d, seq: %u, ack: %u\n", | |
| ntohs(tcp->source), ntohs(tcp->dest), tcp->seq, tcp->ack); | |
| strlcat(szShow, szLine, USHRT_MAX); | strlcat(szShow, szLine, USHRT_MAX); |
| snprintf(szWrk, BUFSIZ, "%d", (u_char) tcp->doff); | |
| if (tcp->fin) | |
| strlcat(szWrk, "|FIN", sizeof szWrk); | |
| if (tcp->syn) | |
| strlcat(szWrk, "|SYN", sizeof szWrk); | |
| if (tcp->rst) | |
| strlcat(szWrk, "|RST", sizeof szWrk); | |
| if (tcp->psh) | |
| strlcat(szWrk, "|PUSH", sizeof szWrk); | |
| if (tcp->ack) | |
| strlcat(szWrk, "|ACK", sizeof szWrk); | |
| if (tcp->urg) | |
| strlcat(szWrk, "|URG", sizeof szWrk); | |
| /* | |
| if (tcp->ece) | |
| strlcat(szWrk, "|ECE", sizeof szWrk); | |
| if (tcp->cwr) | |
| strlcat(szWrk, "|CWR", sizeof szWrk); | |
| */ | |
| snprintf(szLine, BUFSIZ, "\t\toff: %s, win: %d, urp: %04x, cksum: %04x\n", | |
| szWrk, ntohs(tcp->window), ntohs(tcp->urg_ptr), ntohs(tcp->check)); | |
| #endif | |
| strlcat(szShow, szLine, USHRT_MAX); | |
| break; | break; |
| case IPPROTO_UDP: | case IPPROTO_UDP: |
| strlcat(szShow, "\t\t>>> UDP ...\n", USHRT_MAX); | strlcat(szShow, "\t\t>>> UDP ...\n", USHRT_MAX); |
| #ifndef __linux__ | |
| if (ntohs(eth->ether_type) == ETHERTYPE_IPV6) | if (ntohs(eth->ether_type) == ETHERTYPE_IPV6) |
| #else | |
| if (ntohs(eth->h_proto) == ETHERTYPE_IPV6) | |
| #endif | |
| udp = (struct udphdr*) (((caddr_t) ipv6) + sizeof(struct ip6_hdr)); | udp = (struct udphdr*) (((caddr_t) ipv6) + sizeof(struct ip6_hdr)); |
| else | else |
| udp = (struct udphdr*) (((caddr_t) ip) + sizeof(struct ip)); | udp = (struct udphdr*) (((caddr_t) ip) + sizeof(struct ip)); |
| #ifndef __linux__ | |
| snprintf(szLine, BUFSIZ, "\t\tsrc_port: %d, dst_port: %d, len: %d, cksum: %04x\n", | snprintf(szLine, BUFSIZ, "\t\tsrc_port: %d, dst_port: %d, len: %d, cksum: %04x\n", |
| ntohs(udp->uh_sport), ntohs(udp->uh_dport), | ntohs(udp->uh_sport), ntohs(udp->uh_dport), |
| ntohs(udp->uh_ulen), ntohs(udp->uh_sum)); | ntohs(udp->uh_ulen), ntohs(udp->uh_sum)); |
| #else | |
| snprintf(szLine, BUFSIZ, "\t\tsrc_port: %d, dst_port: %d, len: %d, cksum: %04x\n", | |
| ntohs(udp->source), ntohs(udp->dest), | |
| ntohs(udp->len), ntohs(udp->check)); | |
| #endif | |
| strlcat(szShow, szLine, USHRT_MAX); | strlcat(szShow, szLine, USHRT_MAX); |
| break; | break; |
| case IPPROTO_ICMP: | case IPPROTO_ICMP: |
| Line 237 ShowPkt(void *buffer) | Line 315 ShowPkt(void *buffer) |
| icmp = (struct icmp*) (((caddr_t) ip) + sizeof(struct ip)); | icmp = (struct icmp*) (((caddr_t) ip) + sizeof(struct ip)); |
| #endif | #endif |
| #ifndef __linux__ | |
| snprintf(szLine, BUFSIZ, "\t\ttype: %d, code: %d: cksum: %04x\n", | snprintf(szLine, BUFSIZ, "\t\ttype: %d, code: %d: cksum: %04x\n", |
| icmp->icmp_type, icmp->icmp_code, ntohs(icmp->icmp_cksum)); | icmp->icmp_type, icmp->icmp_code, ntohs(icmp->icmp_cksum)); |
| #else | |
| snprintf(szLine, BUFSIZ, "\t\ttype: %d, code: %d: cksum: %04x\n", | |
| icmp->type, icmp->code, ntohs(icmp->checksum)); | |
| #endif | |
| strlcat(szShow, szLine, USHRT_MAX); | strlcat(szShow, szLine, USHRT_MAX); |
| break; | break; |
| } | } |
| Line 298 main(int argc, char **argv) | Line 381 main(int argc, char **argv) |
| else | else |
| strlcpy(szStr, *argv, sizeof szStr); | strlcpy(szStr, *argv, sizeof szStr); |
| #ifndef __linux__ | |
| #ifdef __FreeBSD__ | #ifdef __FreeBSD__ |
| dev = io_etherOpen(szStr, O_RDWR | O_NONBLOCK, 42, 0, (u_int*) &siz, (flg) ? &bz : NULL); | dev = io_etherOpen(szStr, O_RDWR | O_NONBLOCK, 42, 0, (u_int*) &siz, (flg) ? &bz : NULL); |
| if (dev == -1) | if (dev == -1) |
| Line 305 main(int argc, char **argv) | Line 389 main(int argc, char **argv) |
| #else | #else |
| dev = io_etherOpen(szStr, O_RDWR, 42, 0, (u_int*) &siz, NULL); | dev = io_etherOpen(szStr, O_RDWR, 42, 0, (u_int*) &siz, NULL); |
| #endif | #endif |
| #else | |
| dev = io_etherOpen(szStr, 0, 42, 0, (u_int*) &siz, NULL); | |
| #endif | |
| if (dev == -1) { | if (dev == -1) { |
| printf("Error:: #%d - %s\n", io_GetErrno(), io_GetError()); | printf("Error:: #%d - %s\n", io_GetErrno(), io_GetError()); |
| return 1; | return 1; |
| } else | } else |
| printf("dev=%d(%s)\n", dev, szStr); | printf("dev=%d(%s)\n", dev, szStr); |
| #ifndef __linux__ | |
| if (ioctl(dev, BIOCGBLEN, &siz) == -1) { | if (ioctl(dev, BIOCGBLEN, &siz) == -1) { |
| perror("ioctl(BIOCGBLEN)"); | perror("ioctl(BIOCGBLEN)"); |
| io_etherClose(dev, &bz); | io_etherClose(dev, &bz); |
| return 1; | return 1; |
| } else | } else |
| printf("BPF buffer len=%d\n", siz); | printf("BPF buffer len=%d\n", siz); |
| #endif | |
| if (*szMap) { | if (*szMap) { |
| fd = open(szMap, O_RDWR); | fd = open(szMap, O_RDWR); |
| Line 328 main(int argc, char **argv) | Line 417 main(int argc, char **argv) |
| buffer = mmap(NULL, siz, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); | buffer = mmap(NULL, siz, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); |
| close(fd); | close(fd); |
| } else | } else |
| buffer = mmap(NULL, siz, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0); | buffer = mmap(NULL, siz, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); |
| if (buffer == MAP_FAILED) { | if (buffer == MAP_FAILED) { |
| perror("mmap()"); | perror("mmap()"); |
| io_etherClose(dev, &bz); | io_etherClose(dev, &bz); |
| Line 378 main(int argc, char **argv) | Line 467 main(int argc, char **argv) |
| printf("0.%09lu per/iteration\n", ts_end.tv_sec + ts_end.tv_nsec / i); | printf("0.%09lu per/iteration\n", ts_end.tv_sec + ts_end.tv_nsec / i); |
| return 0; | return 0; |
| } | } |
| #endif |