From d53f58f56812adb68e8c48c0aa3d3cb9390fdc50 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Tue, 28 May 2019 17:15:37 +0200 Subject: [PATCH] Add capdiff to see differences between captures --- CMakeLists.txt | 6 +++- src/capdiff.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 src/capdiff.c diff --git a/CMakeLists.txt b/CMakeLists.txt index caeef5f..1182bb3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,6 +41,7 @@ add_executable(donar ${CSOURCES} src/donar.c) add_executable(measlat ${CSOURCES} src/meas_lat.c) add_executable(udpecho ${CSOURCES} src/udp_echo.c) add_executable(torecho ${CSOURCES} src/tor_echo.c) +add_executable(capdiff ${CSOURCES} src/capdiff.c) find_package(PkgConfig REQUIRED) pkg_search_module(GLIB REQUIRED glib-2.0) @@ -57,6 +58,9 @@ target_link_libraries(udpecho ${GLIB_LDFLAGS}) target_include_directories(torecho PRIVATE ${GLIB_INCLUDE_DIRS}) target_link_libraries(torecho ${GLIB_LDFLAGS}) -install(TARGETS donar measlat udpecho torecho +target_include_directories(capdiff PRIVATE ${GLIB_INCLUDE_DIRS}) +target_link_libraries(capdiff ${GLIB_LDFLAGS}) + +install(TARGETS donar measlat udpecho torecho capdiff RUNTIME DESTINATION bin LIBRARY DESTINATION lib) diff --git a/src/capdiff.c b/src/capdiff.c new file mode 100644 index 0000000..0912c23 --- /dev/null +++ b/src/capdiff.c @@ -0,0 +1,87 @@ +#include +#include +#include "packet.h" + +int main(int argc, char** argv) { + if (argc != 3) { + fprintf(stderr, "Usage %s ./cap.0 ./cap.1\n", argv[0]); + exit(EXIT_FAILURE); + } + uint8_t verbose = 0; + + FILE *fd[2]; size_t sz[2]; + for (int i = 0; i < 2; i++) { + if ((fd[i] = fopen(argv[i+1], "r")) == NULL) { + perror("Unable to open file"); + exit(EXIT_FAILURE); + } + + fseek(fd[i], 0, SEEK_END); + sz[i] = ftell(fd[i]); + fseek(fd[i], 0, SEEK_SET); + } + + if (sz[0] != sz[1]) { + printf("[!!] %s has %ld entries, %s has %ld entries\n", + argv[1], + sz[0]/sizeof(struct buffer_packet), + argv[2], + sz[1]/sizeof(struct buffer_packet)); + } else if (verbose) { + printf("[OK] %s and %s have %ld entries\n", + argv[1], + argv[2], + sz[0]/sizeof(struct buffer_packet)); + } + + size_t to_read = sz[0] < sz[1] ? sz[0] : sz[1]; + size_t to_read_obj = to_read / sizeof(struct buffer_packet); + uint16_t expected_id = 0; + struct buffer_packet bpread[2]; + for (size_t c = 0; c < to_read_obj; c++) { + for (int i = 0; i < 2; i++) fread(&bpread[i], sizeof(struct buffer_packet), 1, fd[i]); + /*if (expected_id != bpread[0].ip.ap.fmt.content.clear.id || expected_id != bpread[1].ip.ap.fmt.content.clear.id) { + printf("[!!] expected id is %d, %s has id %d, %s has id %d\n", + expected_id, + argv[1], + bpread[0].ip.ap.fmt.content.clear.id, + argv[2], + bpread[1].ip.ap.fmt.content.clear.id); + }*/ + + uint8_t is_same_size = bpread[0].ip.ap.fmt.headers.size == bpread[1].ip.ap.fmt.headers.size; + if (!is_same_size) { + printf("[!!] %s packet has size %d, %s packet has size %d for expected id %d\n", + argv[1], + bpread[0].ip.ap.fmt.headers.size, + argv[2], + bpread[1].ip.ap.fmt.headers.size, + expected_id); + } else if (verbose) { + printf("[OK] %s and %s packets for expected id %d have size %d\n", + argv[1], + argv[2], + expected_id, + bpread[0].ip.ap.fmt.headers.size); + } + + size_t s1 = bpread[0].ip.ap.fmt.headers.size, s2 = bpread[1].ip.ap.fmt.headers.size; + size_t max_size = s1 > s2 ? s1 : s2; + for (size_t idx = 0; idx < max_size; idx++) { + char e1 = (&bpread[0].ip.ap.raw)[idx], e2 = (&bpread[0].ip.ap.raw)[idx]; + if (e1 != e2) { + printf("[!!] for expected id %d, byte 0x%04x is different: 0x%02x vs 0x%02x\n", expected_id, (uint16_t)idx, (uint8_t)e1, (uint8_t)e2); + } + } + + expected_id++; + } + + + for (int i = 0; i < 2; i++) { + fclose(fd[i]); + } + + printf("done\n"); + return 0; +}