When debugging network packets in Click, usually the packets are printed to screen as hex string:

c2ef0a00 00918843 e138a72b 08004500 003cd373 40004006 58ac0202 020a0a00
0091aae6 0016b446 7f860000 0000a002 39082c11 00000204 05b40402 080acf40
26400000 00000103 0307

It’s time-consuming and error-prone to analyze the hex-string by hand. A very good tool to parse the hex packets is Wireshark. According to Wireshark Manual,

Wireshark understands a hexdump of the form generated by od -Ax -tx1 -v. In other words, each byte is individually displayed and surrounded with a space. Each line begins with an offset describing the position in the file. The offset is a hex number (can also be octal or decimal), of more than two hex digits.

To make Wireshark understands the above hex string, it should be formated into:

000000 c2 ef 0a 00 00 91 88 43  e1 38 a7 2b 08 00 45 00
000010 00 3c d3 73 40 00 40 06  58 ac 02 02 02 0a 0a 00
000020 00 91 aa e6 00 16 b4 46  7f 86 00 00 00 00 a0 02
000030 39 08 2c 11 00 00 02 04  05 b4 04 02 08 0a cf 40
000040 26 40 00 00 00 00 01 03  03 07

Once got the hexdump file, it can be parsed by Wireshark using the “File->Import from Hex Dump” dialog box.

import_from_hex_dump

To automatically generate the hexdump file, a C++ tool gen_hexdump is developed. The C++ source code can be compiled by command

g++ -Wall -std=c++0x -o gen_hexdump gen_hexdump.cc

Usage:

gen_hexdump [-i input_file] [-n] [-s hex_str] -o out_file

Examples:

  1. format a single hex string:

    gen_hexdump -o <output> -s <hex_string>

  2. format a single packet in a file:

    gen_hexdump -n -i <input> -o <output>

  3. format multiple packets from a file(one packet per line):

    gen_hexdump -i <input> -o <output>

Reference:

  • https://www.wireshark.org/docs/wsug_html_chunked/ChIOImportSection.html