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"
19 #include <netinet/in.h>
23 void die(byte *msg, ...)
28 fputs("netgrind: ", stderr);
29 vfprintf(stderr, msg, args);
34 /*** MANUAL MULTIPLEXER ***/
36 static void mux_open(struct flow *f, u64 when)
38 u32 saddr = ntohl(f->saddr);
39 u32 daddr = ntohl(f->daddr);
40 uns sport = ntohs(f->sport);
41 uns dport = ntohs(f->dport);
42 struct appl_hooks *appl = &appl_sink;
44 if (dport == 80 || dport == 8080 || dport == 8081 || dport == 3128)
53 struct appl_hooks appl_mux = {
59 /*** PCAP INTERFACE ***/
61 static void (*link_handler)(struct pkt *);
62 static struct pkt_stats stat_pcap_incomplete, stat_pcap_in;
64 static uns in_count, start_sec;
65 static u64 last_timestamp;
67 static int link_setup_handler(int dlt)
71 case DLT_EN10MB: link_handler = link_eth_got_packet; return 1;
76 static void got_pcap_packet(u_char *userdata UNUSED, const struct pcap_pkthdr *hdr, const u_char *pkt)
78 stat_pcap_in.packets++;
79 stat_pcap_in.bytes += hdr->len;
80 if (hdr->caplen != hdr->len)
82 stat_pcap_incomplete.packets++;
83 stat_pcap_incomplete.bytes += hdr->len - hdr->caplen;
86 if (!(in_count % 1024))
89 start_sec = hdr->ts.tv_sec;
90 fprintf(stderr, "%d packets, %d seconds, %d conns (%d open)\r",
91 in_count, (int)hdr->ts.tv_sec - start_sec,
92 cnt_tcp_flows, tcp_num_flows);
96 struct pkt *p = pkt_new(0, hdr->len);
97 memcpy(pkt_append(p, hdr->len), pkt, hdr->len);
98 p->timestamp = (u64)hdr->ts.tv_sec * 1000000 + hdr->ts.tv_usec;
99 last_timestamp = p->timestamp;
105 static void usage(void)
107 fprintf(stderr, "Usage: netgrind [<switches>] <capture-files>\n\
109 -a TCP: Record arrival times instead of processing times\n\
110 -c <count> Stop after processing <count> packets\n\
111 -d <dir> Dump connections to a given directory\n\
112 -D <dir> Dump connections with more details\n\
113 -f <filter> Apply filter expression\n\
114 -s Dump connection summary\n\
115 -t Calculate statistics only\n\
116 -w TCP: Wait for ACK before processing packets\n\
121 int main(int argc, char **argv)
123 char errbuf[PCAP_ERRBUF_SIZE];
126 int max_packets = -1;
128 struct bpf_program filter_prog;
130 tcp_default_appl = &appl_mux;
131 while ((c = getopt(argc, argv, "ac:d:D:f:stw")) >= 0)
135 tcp_arrival_times = 1;
138 max_packets = atol(optarg);
141 tcp_default_appl = &appl_save;
145 tcp_default_appl = &appl_asave;
152 tcp_wait_for_ack = 1;
155 tcp_default_appl = &appl_summary;
158 tcp_default_appl = &appl_sink;
168 while (optind < argc)
170 fprintf(stderr, "Processing %s...\n", argv[optind]);
171 if (!(pcap = pcap_open_offline(argv[optind], errbuf)))
172 die("Unable to open %s", errbuf);
173 dlt = pcap_datalink(pcap);
174 if (!link_setup_handler(dlt))
175 die("Don't know how to handle data link type %d", dlt);
178 if (pcap_compile(pcap, &filter_prog, filter, 1, 0) < 0)
179 die("Error compiling filter: %s", pcap_geterr(pcap));
180 pcap_setfilter(pcap, &filter_prog);
181 pcap_freecode(&filter_prog);
183 if (pcap_loop(pcap, max_packets, got_pcap_packet, NULL) < 0)
184 die("Capture failed: %s", pcap_geterr(pcap));
188 tcp_cleanup(last_timestamp);
189 printf("# Netgrind statistics:\n");
190 printf("# Pcap: %Ld(%Ld) in, %Ld(%Ld) incomplete\n",
191 stat_pcap_in.packets, stat_pcap_in.bytes,
192 stat_pcap_incomplete.packets, stat_pcap_incomplete.bytes);
193 printf("# Link: %Ld(%Ld) in, %Ld(%Ld) dwarves, %Ld(%Ld) strangers, %Ld(%Ld) ARPs\n",
194 stat_link_in.packets, stat_link_in.bytes,
195 stat_link_dwarf.packets, stat_link_dwarf.bytes,
196 stat_link_unknown.packets, stat_link_unknown.bytes,
197 stat_link_arp.packets, stat_link_arp.bytes);
198 printf("# IP: %Ld(%Ld) in, %Ld(%Ld) invalid, %Ld(%Ld) boring, %Ld(%Ld) fragmented, %Ld(%Ld) bad checksum\n",
199 stat_ip_in.packets, stat_ip_in.bytes,
200 stat_ip_invalid.packets, stat_ip_invalid.bytes,
201 stat_ip_uninteresting.packets, stat_ip_uninteresting.bytes,
202 stat_ip_fragmented.packets, stat_ip_fragmented.bytes,
203 stat_ip_badsum.packets, stat_ip_badsum.bytes);
204 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",
205 stat_tcp_in.packets, stat_tcp_in.bytes,
206 stat_tcp_invalid.packets, stat_tcp_invalid.bytes,
207 stat_tcp_badsum.packets, stat_tcp_badsum.bytes,
208 stat_tcp_unmatched.packets, stat_tcp_unmatched.bytes,
209 stat_tcp_on_closed.packets, stat_tcp_on_closed.bytes,
210 stat_tcp_bad_state.packets, stat_tcp_bad_state.bytes);
211 printf("# Flows: %d total: %d closed, %d reset, %d timed out, %d overlap end, %d corrupted\n",
212 cnt_tcp_flows, cnt_tcp_causes[CAUSE_CLOSE], cnt_tcp_causes[CAUSE_RESET],
213 cnt_tcp_causes[CAUSE_TIMEOUT], cnt_tcp_causes[CAUSE_DOOMSDAY], cnt_tcp_causes[CAUSE_CORRUPT]);