1 | /* Tool that compares two traces and outputs any packets that do not match |
---|
2 | * between the two |
---|
3 | * |
---|
4 | * Author: Shane Alcock |
---|
5 | */ |
---|
6 | |
---|
7 | #include "libtrace.h" |
---|
8 | #include <stdio.h> |
---|
9 | #include <assert.h> |
---|
10 | #include <getopt.h> |
---|
11 | #include <string.h> |
---|
12 | #include <stdlib.h> |
---|
13 | |
---|
14 | #include "libpacketdump.h" |
---|
15 | |
---|
16 | uint32_t max_diff = 0; |
---|
17 | uint32_t dumped_diff = 0; |
---|
18 | |
---|
19 | /* Compares the two provided packets. If the packets differ in any fashion, |
---|
20 | * both will be dumped to standard output using libpacketdump followed by a |
---|
21 | * line of asterisks. |
---|
22 | * |
---|
23 | * Note that only the contents of the packet are compared; the framing provided |
---|
24 | * by the trace format, e.g. the ERF or PCAP header, is not examined. |
---|
25 | */ |
---|
26 | void per_packet(libtrace_packet_t *a, libtrace_packet_t *b) |
---|
27 | { |
---|
28 | char *buf_a, *buf_b; |
---|
29 | libtrace_linktype_t lt; |
---|
30 | uint32_t rem_a, rem_b; |
---|
31 | |
---|
32 | |
---|
33 | buf_a = trace_get_packet_buffer(a, <, &rem_a); |
---|
34 | buf_b = trace_get_packet_buffer(b, <, &rem_b); |
---|
35 | |
---|
36 | if (rem_a > trace_get_wire_length(a)) |
---|
37 | rem_a = trace_get_wire_length(a); |
---|
38 | if (rem_b > trace_get_wire_length(b)) |
---|
39 | rem_b = trace_get_wire_length(b); |
---|
40 | |
---|
41 | |
---|
42 | if (!buf_a && !buf_b) |
---|
43 | return; |
---|
44 | |
---|
45 | if (!buf_a || !buf_b) { |
---|
46 | trace_dump_packet(a); |
---|
47 | trace_dump_packet(b); |
---|
48 | printf("****************\n"); |
---|
49 | dumped_diff ++; |
---|
50 | return; |
---|
51 | } |
---|
52 | |
---|
53 | |
---|
54 | if (rem_a == 0 || rem_b == 0) |
---|
55 | return; |
---|
56 | |
---|
57 | if (rem_a != rem_b) { |
---|
58 | trace_dump_packet(a); |
---|
59 | trace_dump_packet(b); |
---|
60 | printf("****************\n"); |
---|
61 | dumped_diff ++; |
---|
62 | return; |
---|
63 | } |
---|
64 | |
---|
65 | /* This is not exactly going to be snappy, but it's the easiest way |
---|
66 | * to look for differences */ |
---|
67 | if (memcmp(buf_a, buf_b, rem_a) != 0) { |
---|
68 | trace_dump_packet(a); |
---|
69 | trace_dump_packet(b); |
---|
70 | printf("****************\n"); |
---|
71 | dumped_diff ++; |
---|
72 | } |
---|
73 | |
---|
74 | } |
---|
75 | |
---|
76 | void usage(char *prog) { |
---|
77 | printf("Usage instructions for %s\n\n", prog); |
---|
78 | printf("\t%s [options] traceA traceB\n\n", prog); |
---|
79 | printf("Supported options:\n"); |
---|
80 | printf("\t-m <max> Stop after <max> differences have been reported\n"); |
---|
81 | |
---|
82 | |
---|
83 | return; |
---|
84 | |
---|
85 | } |
---|
86 | |
---|
87 | int main(int argc, char *argv[]) |
---|
88 | { |
---|
89 | libtrace_t *trace[2]; |
---|
90 | libtrace_packet_t *packet[2]; |
---|
91 | int opt, i; |
---|
92 | |
---|
93 | if (argc<2) { |
---|
94 | usage(argv[0]); |
---|
95 | return -1; |
---|
96 | } |
---|
97 | |
---|
98 | while ((opt = getopt(argc, argv, "m:")) != EOF) { |
---|
99 | switch (opt) { |
---|
100 | case 'm': |
---|
101 | if (atoi(optarg) < 0) { |
---|
102 | fprintf(stderr, "-m option must not be negative - ignoring\n"); |
---|
103 | } else { |
---|
104 | max_diff = (uint32_t) atoi(optarg); |
---|
105 | } |
---|
106 | break; |
---|
107 | default: |
---|
108 | usage(argv[0]); |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | if (optind + 2 > argc) { |
---|
113 | usage(argv[0]); |
---|
114 | return -1; |
---|
115 | } |
---|
116 | packet[0] = trace_create_packet(); |
---|
117 | packet[1] = trace_create_packet(); |
---|
118 | |
---|
119 | trace[0] = trace_create(argv[optind++]); |
---|
120 | |
---|
121 | if (trace_is_err(trace[0])) { |
---|
122 | trace_perror(trace[0],"Opening trace file"); |
---|
123 | return -1; |
---|
124 | } |
---|
125 | |
---|
126 | if (trace_start(trace[0])) { |
---|
127 | trace_perror(trace[0],"Starting trace"); |
---|
128 | trace_destroy(trace[0]); |
---|
129 | return -1; |
---|
130 | } |
---|
131 | |
---|
132 | trace[1] = trace_create(argv[optind++]); |
---|
133 | |
---|
134 | if (trace_is_err(trace[1])) { |
---|
135 | trace_perror(trace[1],"Opening trace file"); |
---|
136 | return -1; |
---|
137 | } |
---|
138 | |
---|
139 | if (trace_start(trace[1])) { |
---|
140 | trace_perror(trace[1],"Starting trace"); |
---|
141 | trace_destroy(trace[1]); |
---|
142 | return -1; |
---|
143 | } |
---|
144 | |
---|
145 | while (trace_read_packet(trace[0], packet[0]) > 0 && |
---|
146 | trace_read_packet(trace[1], packet[1]) > 0) { |
---|
147 | |
---|
148 | per_packet(packet[0], packet[1]); |
---|
149 | |
---|
150 | if (max_diff > 0 && dumped_diff >= max_diff) |
---|
151 | break; |
---|
152 | |
---|
153 | } |
---|
154 | |
---|
155 | if (trace_is_err(trace[0])) { |
---|
156 | trace_perror(trace[0],"Reading packets"); |
---|
157 | trace_destroy(trace[0]); |
---|
158 | return -1; |
---|
159 | } |
---|
160 | |
---|
161 | if (trace_is_err(trace[1])) { |
---|
162 | trace_perror(trace[1],"Reading packets"); |
---|
163 | trace_destroy(trace[1]); |
---|
164 | return -1; |
---|
165 | } |
---|
166 | |
---|
167 | trace_destroy(trace[0]); |
---|
168 | trace_destroy(trace[1]); |
---|
169 | |
---|
170 | trace_destroy_packet(packet[0]); |
---|
171 | trace_destroy_packet(packet[1]); |
---|
172 | |
---|
173 | if (dumped_diff == 0) |
---|
174 | return 0; |
---|
175 | else |
---|
176 | return 1; |
---|
177 | } |
---|