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"
21 #include <netinet/in.h>
25 void die(byte *msg, ...)
30 fputs("netgrind: ", stderr);
31 vfprintf(stderr, msg, args);
36 static volatile int stop_signal;
38 static void sigint_handler(int arg UNUSED)
43 /*** MANUAL MULTIPLEXER ***/
45 static void mux_open(struct flow *f, u64 when)
47 u32 saddr = ntohl(f->saddr);
48 u32 daddr = ntohl(f->daddr);
49 uns sport = ntohs(f->sport);
50 uns dport = ntohs(f->dport);
51 struct appl_hooks *appl = &appl_sink;
53 if (dport == 80 || dport == 8080 || dport == 8081 || dport == 3128)
59 struct appl_hooks appl_mux = {
65 /*** PCAP INTERFACE ***/
67 static void (*link_handler)(struct pkt *);
68 static struct pkt_stats stat_pcap_incomplete, stat_pcap_in;
70 static uns in_count, start_sec;
71 static u64 last_timestamp;
72 static jmp_buf stop_jump;
74 static int link_setup_handler(int dlt)
78 case DLT_EN10MB: link_handler = link_eth_got_packet; return 1;
83 static void got_pcap_packet(u_char *userdata UNUSED, const struct pcap_pkthdr *hdr, const u_char *pkt)
86 longjmp(stop_jump, 1);
87 histogram_step((uns) hdr->ts.tv_sec);
88 stat_pcap_in.packets++;
89 stat_pcap_in.bytes += hdr->len;
90 if (hdr->caplen != hdr->len)
92 stat_pcap_incomplete.packets++;
93 stat_pcap_incomplete.bytes += hdr->len - hdr->caplen;
96 if (!(in_count % 1024))
99 start_sec = hdr->ts.tv_sec;
100 fprintf(stderr, "%d packets, %d seconds, %d conns (%d open)\r",
101 in_count, (int)hdr->ts.tv_sec - start_sec,
102 cnt_tcp_flows, tcp_num_flows);
106 struct pkt *p = pkt_new(0, hdr->len);
107 memcpy(pkt_append(p, hdr->len), pkt, hdr->len);
108 p->timestamp = (u64)hdr->ts.tv_sec * 1000000 + hdr->ts.tv_usec;
109 last_timestamp = p->timestamp;
115 static void usage(void)
117 fprintf(stderr, "Usage: netgrind [<switches>] <capture-files>\n\
119 -a TCP: Record arrival times instead of processing times\n\
120 -c <count> Stop after processing <count> packets\n\
121 -d <dir> Dump connections to a given directory\n\
122 -D <dir> Dump connections with more details\n\
123 -f <filter> Apply filter expression\n\
124 -h <file> Write packet histogram to <file>\n\
125 -s Dump connection summary\n\
126 -t Calculate statistics only\n\
127 -w TCP: Wait for ACK before processing packets\n\
132 static int max_packets = -1;
133 static byte *filter = NULL;
135 int main(int argc, char **argv)
137 char errbuf[PCAP_ERRBUF_SIZE];
140 struct bpf_program filter_prog;
141 byte *histogram = NULL;
143 tcp_default_appl = &appl_mux;
144 while ((c = getopt(argc, argv, "ac:d:D:f:h:stw")) >= 0)
148 tcp_arrival_times = 1;
151 max_packets = atol(optarg);
154 tcp_default_appl = &appl_save;
158 tcp_default_appl = &appl_asave;
168 tcp_wait_for_ack = 1;
171 tcp_default_appl = &appl_summary;
174 tcp_default_appl = &appl_sink;
185 histogram_init(histogram);
186 histogram_add_stat("PcapIn", &stat_pcap_in);
187 histogram_add_stat("PcapIncomp", &stat_pcap_incomplete);
188 histogram_add_stat("LinkIn", &stat_link_in);
189 histogram_add_stat("LinkDwarf", &stat_link_dwarf);
190 histogram_add_stat("LinkUnkn", &stat_link_unknown);
191 histogram_add_stat("LinkArp", &stat_link_arp);
192 histogram_add_stat("IPIn", &stat_ip_in);
193 histogram_add_stat("IPBad", &stat_ip_invalid);
194 histogram_add_stat("IPUnint", &stat_ip_uninteresting);
195 histogram_add_stat("IPFrag", &stat_ip_fragmented);
196 histogram_add_stat("IPBadSum", &stat_ip_badsum);
197 histogram_add_stat("TCPIn", &stat_tcp_in);
198 histogram_add_stat("TCPBad", &stat_tcp_invalid);
199 histogram_add_stat("TCPBadSum", &stat_tcp_badsum);
200 histogram_add_stat("TCPUnmatch", &stat_tcp_unmatched);
201 histogram_add_stat("TCPOnClosed", &stat_tcp_on_closed);
202 histogram_add_stat("TCPBadState", &stat_tcp_bad_state);
203 histogram_add_int("FlowsTotal", &cnt_tcp_flows);
204 histogram_add_int("FlowsClosed", &cnt_tcp_causes[CAUSE_CLOSE]);
205 histogram_add_int("FlowsReset", &cnt_tcp_causes[CAUSE_RESET]);
206 histogram_add_int("FlowsTimeout", &cnt_tcp_causes[CAUSE_TIMEOUT]);
207 histogram_add_int("FlowsDoomsday", &cnt_tcp_causes[CAUSE_DOOMSDAY]);
208 histogram_add_int("FlowsBad", &cnt_tcp_causes[CAUSE_CORRUPT]);
210 signal(SIGINT, sigint_handler);
212 while (optind < argc)
214 fprintf(stderr, "Processing %s...\n", argv[optind]);
215 if (!(pcap = pcap_open_offline(argv[optind], errbuf)))
216 die("Unable to open %s", errbuf);
217 dlt = pcap_datalink(pcap);
218 if (!link_setup_handler(dlt))
219 die("Don't know how to handle data link type %d", dlt);
222 if (pcap_compile(pcap, &filter_prog, filter, 1, 0) < 0)
223 die("Error compiling filter: %s", pcap_geterr(pcap));
224 pcap_setfilter(pcap, &filter_prog);
225 pcap_freecode(&filter_prog);
227 if (!setjmp(stop_jump))
229 if (pcap_loop(pcap, max_packets, got_pcap_packet, NULL) < 0)
231 fprintf(stderr, "Capture failed: %s\n", pcap_geterr(pcap));
237 fprintf(stderr, "Interrupted\n");
243 tcp_cleanup(last_timestamp);
245 printf("# Netgrind statistics:\n");
246 printf("# Pcap: %Ld(%Ld) in, %Ld(%Ld) incomplete\n",
247 stat_pcap_in.packets, stat_pcap_in.bytes,
248 stat_pcap_incomplete.packets, stat_pcap_incomplete.bytes);
249 printf("# Link: %Ld(%Ld) in, %Ld(%Ld) dwarves, %Ld(%Ld) strangers, %Ld(%Ld) ARPs\n",
250 stat_link_in.packets, stat_link_in.bytes,
251 stat_link_dwarf.packets, stat_link_dwarf.bytes,
252 stat_link_unknown.packets, stat_link_unknown.bytes,
253 stat_link_arp.packets, stat_link_arp.bytes);
254 printf("# IP: %Ld(%Ld) in, %Ld(%Ld) invalid, %Ld(%Ld) boring, %Ld(%Ld) fragmented, %Ld(%Ld) bad checksum\n",
255 stat_ip_in.packets, stat_ip_in.bytes,
256 stat_ip_invalid.packets, stat_ip_invalid.bytes,
257 stat_ip_uninteresting.packets, stat_ip_uninteresting.bytes,
258 stat_ip_fragmented.packets, stat_ip_fragmented.bytes,
259 stat_ip_badsum.packets, stat_ip_badsum.bytes);
260 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",
261 stat_tcp_in.packets, stat_tcp_in.bytes,
262 stat_tcp_invalid.packets, stat_tcp_invalid.bytes,
263 stat_tcp_badsum.packets, stat_tcp_badsum.bytes,
264 stat_tcp_unmatched.packets, stat_tcp_unmatched.bytes,
265 stat_tcp_on_closed.packets, stat_tcp_on_closed.bytes,
266 stat_tcp_bad_state.packets, stat_tcp_bad_state.bytes);
267 printf("# Flows: %d total: %d closed, %d reset, %d timed out, %d overlap end, %d corrupted\n",
268 cnt_tcp_flows, cnt_tcp_causes[CAUSE_CLOSE], cnt_tcp_causes[CAUSE_RESET],
269 cnt_tcp_causes[CAUSE_TIMEOUT], cnt_tcp_causes[CAUSE_DOOMSDAY], cnt_tcp_causes[CAUSE_CORRUPT]);