2 * Netgrind -- The Network Traffic Analyser
4 * (c) 2003 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU General Public License.
11 #include "netgrind/pkt.h"
12 #include "netgrind/netgrind.h"
22 void die(byte *msg, ...)
27 fputs("netgrind: ", stderr);
28 vfprintf(stderr, msg, args);
33 /*** PCAP INTERFACE ***/
35 static void (*link_handler)(struct pkt *);
36 static struct pkt_stats stat_pcap_incomplete;
38 static uns in_count, start_sec;
40 static int link_setup_handler(int dlt)
44 case DLT_EN10MB: link_handler = link_eth_got_packet; return 1;
49 static void got_pcap_packet(u_char *userdata UNUSED, const struct pcap_pkthdr *hdr, const u_char *pkt)
51 if (hdr->caplen != hdr->len)
53 stat_pcap_incomplete.packets++;
54 stat_pcap_incomplete.bytes += hdr->len - hdr->caplen;
57 if (!(in_count % 1024))
60 start_sec = hdr->ts.tv_sec;
61 fprintf(stderr, "%d packets, %d seconds, %d conns (%d still open)\r",
62 in_count, (int)hdr->ts.tv_sec - start_sec,
63 tcp_total_flows, tcp_num_flows);
67 struct pkt *p = pkt_new(0, hdr->len);
68 memcpy(pkt_append(p, hdr->len), pkt, hdr->len);
69 p->timestamp = (u64)hdr->ts.tv_sec * 1000000 + hdr->ts.tv_usec;
73 static void usage(void)
75 fprintf(stderr, "Usage: netgrind [<switches>] <capture-file>\n\
77 -a TCP: Record arrival times instead of processing times\n\
78 -c <count> Stop after processing <count> packets\n\
79 -d <dir> Dump connections to a given directory\n\
80 -D <dir> Dump connections with more details\n\
81 -f <filter> Apply filter expression\n\
82 -w TCP: Wait for ACK before processing packets\n\
87 int main(int argc, char **argv)
89 char errbuf[PCAP_ERRBUF_SIZE];
94 struct bpf_program filter_prog;
96 tcp_default_appl = &appl_sink;
97 while ((c = getopt(argc, argv, "ac:d:D:f:w")) >= 0)
101 tcp_arrival_times = 1;
104 max_packets = atol(optarg);
107 tcp_default_appl = &appl_save;
111 tcp_default_appl = &appl_asave;
118 tcp_wait_for_ack = 1;
123 if (optind != argc - 1)
128 if (!(pcap = pcap_open_offline(argv[optind], errbuf)))
129 die("Unable to open %s", errbuf);
130 dlt = pcap_datalink(pcap);
131 if (!link_setup_handler(dlt))
132 die("Don't know how to handle data link type %d", dlt);
135 if (pcap_compile(pcap, &filter_prog, filter, 1, 0) < 0)
136 die("Error compiling filter: %s", pcap_geterr(pcap));
137 pcap_setfilter(pcap, &filter_prog);
139 if (pcap_loop(pcap, max_packets, got_pcap_packet, NULL) < 0)
140 die("Capture failed: %s", pcap_geterr(pcap));
142 printf("Pcap: %Ld(%Ld) incomplete\n",
143 stat_pcap_incomplete.packets, stat_pcap_incomplete.bytes);
144 printf("Link: %Ld(%Ld) in, %Ld(%Ld) dwarves, %Ld(%Ld) strangers, %Ld(%Ld) ARPs\n",
145 stat_link_in.packets, stat_link_in.bytes,
146 stat_link_dwarf.packets, stat_link_dwarf.bytes,
147 stat_link_unknown.packets, stat_link_unknown.bytes,
148 stat_link_arp.packets, stat_link_arp.bytes);
149 printf("IP: %Ld(%Ld) in, %Ld(%Ld) invalid, %Ld(%Ld) boring, %Ld(%Ld) fragmented, %Ld(%Ld) bad checksum; %d flows\n",
150 stat_ip_in.packets, stat_ip_in.bytes,
151 stat_ip_invalid.packets, stat_ip_invalid.bytes,
152 stat_ip_uninteresting.packets, stat_ip_uninteresting.bytes,
153 stat_ip_fragmented.packets, stat_ip_fragmented.bytes,
154 stat_ip_badsum.packets, stat_ip_badsum.bytes,
156 printf("TCP: %Ld(%Ld) in, %Ld(%Ld) invalid, %Ld(%Ld) bad checksum, %Ld(%Ld) unmatched, %Ld(%Ld) on closed connections, %Ld(%Ld) in unexpected state\n",
157 stat_tcp_in.packets, stat_tcp_in.bytes,
158 stat_tcp_invalid.packets, stat_tcp_invalid.bytes,
159 stat_tcp_badsum.packets, stat_tcp_badsum.bytes,
160 stat_tcp_unmatched.packets, stat_tcp_unmatched.bytes,
161 stat_tcp_on_closed.packets, stat_tcp_on_closed.bytes,
162 stat_tcp_bad_state.packets, stat_tcp_bad_state.bytes);