[ee6e802] | 1 | /* |
---|
| 2 | * |
---|
| 3 | * Copyright (c) 2007-2016 The University of Waikato, Hamilton, New Zealand. |
---|
| 4 | * All rights reserved. |
---|
| 5 | * |
---|
| 6 | * This file is part of libtrace. |
---|
| 7 | * |
---|
| 8 | * This code has been developed by the University of Waikato WAND |
---|
| 9 | * research group. For further information please see http://www.wand.net.nz/ |
---|
| 10 | * |
---|
| 11 | * libtrace is free software; you can redistribute it and/or modify |
---|
| 12 | * it under the terms of the GNU Lesser General Public License as published by |
---|
| 13 | * the Free Software Foundation; either version 3 of the License, or |
---|
| 14 | * (at your option) any later version. |
---|
| 15 | * |
---|
| 16 | * libtrace is distributed in the hope that it will be useful, |
---|
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 19 | * GNU Lesser General Public License for more details. |
---|
| 20 | * |
---|
| 21 | * You should have received a copy of the GNU Lesser General Public License |
---|
| 22 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
| 23 | * |
---|
| 24 | * |
---|
| 25 | */ |
---|
| 26 | |
---|
| 27 | |
---|
[7da2676] | 28 | /* Tool that compares two traces and outputs any packets that do not match |
---|
| 29 | * between the two |
---|
| 30 | * |
---|
| 31 | * Author: Shane Alcock |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | #include "libtrace.h" |
---|
| 35 | #include <stdio.h> |
---|
| 36 | #include <assert.h> |
---|
| 37 | #include <getopt.h> |
---|
| 38 | #include <string.h> |
---|
| 39 | #include <stdlib.h> |
---|
| 40 | |
---|
| 41 | #include "libpacketdump.h" |
---|
| 42 | |
---|
| 43 | uint32_t max_diff = 0; |
---|
| 44 | uint32_t dumped_diff = 0; |
---|
| 45 | |
---|
| 46 | /* Compares the two provided packets. If the packets differ in any fashion, |
---|
| 47 | * both will be dumped to standard output using libpacketdump followed by a |
---|
| 48 | * line of asterisks. |
---|
| 49 | * |
---|
| 50 | * Note that only the contents of the packet are compared; the framing provided |
---|
| 51 | * by the trace format, e.g. the ERF or PCAP header, is not examined. |
---|
| 52 | */ |
---|
[d7e8e67] | 53 | static void per_packet(libtrace_packet_t *a, libtrace_packet_t *b) |
---|
[7da2676] | 54 | { |
---|
| 55 | char *buf_a, *buf_b; |
---|
| 56 | libtrace_linktype_t lt; |
---|
| 57 | uint32_t rem_a, rem_b; |
---|
| 58 | |
---|
| 59 | |
---|
| 60 | buf_a = trace_get_packet_buffer(a, <, &rem_a); |
---|
| 61 | buf_b = trace_get_packet_buffer(b, <, &rem_b); |
---|
| 62 | |
---|
| 63 | if (rem_a > trace_get_wire_length(a)) |
---|
| 64 | rem_a = trace_get_wire_length(a); |
---|
| 65 | if (rem_b > trace_get_wire_length(b)) |
---|
| 66 | rem_b = trace_get_wire_length(b); |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | if (!buf_a && !buf_b) |
---|
| 70 | return; |
---|
| 71 | |
---|
| 72 | if (!buf_a || !buf_b) { |
---|
| 73 | trace_dump_packet(a); |
---|
| 74 | trace_dump_packet(b); |
---|
| 75 | printf("****************\n"); |
---|
| 76 | dumped_diff ++; |
---|
| 77 | return; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | |
---|
| 81 | if (rem_a == 0 || rem_b == 0) |
---|
| 82 | return; |
---|
| 83 | |
---|
| 84 | if (rem_a != rem_b) { |
---|
| 85 | trace_dump_packet(a); |
---|
| 86 | trace_dump_packet(b); |
---|
| 87 | printf("****************\n"); |
---|
| 88 | dumped_diff ++; |
---|
| 89 | return; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | /* This is not exactly going to be snappy, but it's the easiest way |
---|
| 93 | * to look for differences */ |
---|
| 94 | if (memcmp(buf_a, buf_b, rem_a) != 0) { |
---|
| 95 | trace_dump_packet(a); |
---|
| 96 | trace_dump_packet(b); |
---|
| 97 | printf("****************\n"); |
---|
| 98 | dumped_diff ++; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | } |
---|
| 102 | |
---|
[d7e8e67] | 103 | static void usage(char *prog) { |
---|
[7da2676] | 104 | printf("Usage instructions for %s\n\n", prog); |
---|
| 105 | printf("\t%s [options] traceA traceB\n\n", prog); |
---|
| 106 | printf("Supported options:\n"); |
---|
| 107 | printf("\t-m <max> Stop after <max> differences have been reported\n"); |
---|
| 108 | |
---|
| 109 | |
---|
| 110 | return; |
---|
| 111 | |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | int main(int argc, char *argv[]) |
---|
| 115 | { |
---|
| 116 | libtrace_t *trace[2]; |
---|
| 117 | libtrace_packet_t *packet[2]; |
---|
[d7e8e67] | 118 | int opt; |
---|
[7da2676] | 119 | |
---|
| 120 | if (argc<2) { |
---|
| 121 | usage(argv[0]); |
---|
| 122 | return -1; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | while ((opt = getopt(argc, argv, "m:")) != EOF) { |
---|
| 126 | switch (opt) { |
---|
| 127 | case 'm': |
---|
| 128 | if (atoi(optarg) < 0) { |
---|
| 129 | fprintf(stderr, "-m option must not be negative - ignoring\n"); |
---|
| 130 | } else { |
---|
| 131 | max_diff = (uint32_t) atoi(optarg); |
---|
| 132 | } |
---|
| 133 | break; |
---|
| 134 | default: |
---|
| 135 | usage(argv[0]); |
---|
| 136 | } |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | if (optind + 2 > argc) { |
---|
| 140 | usage(argv[0]); |
---|
| 141 | return -1; |
---|
| 142 | } |
---|
| 143 | packet[0] = trace_create_packet(); |
---|
| 144 | packet[1] = trace_create_packet(); |
---|
| 145 | |
---|
| 146 | trace[0] = trace_create(argv[optind++]); |
---|
| 147 | |
---|
| 148 | if (trace_is_err(trace[0])) { |
---|
| 149 | trace_perror(trace[0],"Opening trace file"); |
---|
| 150 | return -1; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | if (trace_start(trace[0])) { |
---|
| 154 | trace_perror(trace[0],"Starting trace"); |
---|
| 155 | trace_destroy(trace[0]); |
---|
| 156 | return -1; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | trace[1] = trace_create(argv[optind++]); |
---|
| 160 | |
---|
| 161 | if (trace_is_err(trace[1])) { |
---|
| 162 | trace_perror(trace[1],"Opening trace file"); |
---|
| 163 | return -1; |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | if (trace_start(trace[1])) { |
---|
| 167 | trace_perror(trace[1],"Starting trace"); |
---|
| 168 | trace_destroy(trace[1]); |
---|
| 169 | return -1; |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | while (trace_read_packet(trace[0], packet[0]) > 0 && |
---|
| 173 | trace_read_packet(trace[1], packet[1]) > 0) { |
---|
| 174 | |
---|
| 175 | per_packet(packet[0], packet[1]); |
---|
| 176 | |
---|
| 177 | if (max_diff > 0 && dumped_diff >= max_diff) |
---|
| 178 | break; |
---|
| 179 | |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | if (trace_is_err(trace[0])) { |
---|
| 183 | trace_perror(trace[0],"Reading packets"); |
---|
| 184 | trace_destroy(trace[0]); |
---|
| 185 | return -1; |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | if (trace_is_err(trace[1])) { |
---|
| 189 | trace_perror(trace[1],"Reading packets"); |
---|
| 190 | trace_destroy(trace[1]); |
---|
| 191 | return -1; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | trace_destroy(trace[0]); |
---|
| 195 | trace_destroy(trace[1]); |
---|
| 196 | |
---|
| 197 | trace_destroy_packet(packet[0]); |
---|
| 198 | trace_destroy_packet(packet[1]); |
---|
| 199 | |
---|
| 200 | if (dumped_diff == 0) |
---|
| 201 | return 0; |
---|
| 202 | else |
---|
| 203 | return 1; |
---|
| 204 | } |
---|