6 #include <netinet/ip.h>
15 groupstats() : count(0) {}
18 struct timeval last_seen;
20 map<uint32_t, groupstats> stats;
24 void callback(u_char *user, const struct pcap_pkthdr *h, const u_char *bytes)
28 const iphdr *ip = (const iphdr *)(bytes + 16);
29 if ((ntohl(ip->daddr) & 0xe0000000) != (224u << 24) ||
30 (ntohl(ip->daddr) & 0xffffff00) == (224u << 24) ||
31 ip->daddr == 0xffffffffu ||
36 ++stats[ip->daddr].count;
37 stats[ip->daddr].last_seen = h->ts;
40 double tdiff(const timeval &a, const timeval &b)
42 return (b.tv_sec - a.tv_sec) +
43 1e-6 * (b.tv_usec - a.tv_usec);
48 for (auto it = stats.begin(); it != stats.end(); ) {
49 if (tdiff(it->second.last_seen, now) < 1.0) {
55 ip.s_addr = it->first;
56 printf("%s %d\n", inet_ntoa(ip), it->second.count);
64 char errbuf[PCAP_ERRBUF_SIZE];
65 pcap_t *pcap = pcap_create("any", NULL);
67 fprintf(stderr, "pcap_create(): %s\n", errbuf);
74 pcap_dispatch(pcap, 10000, callback, NULL);