From: Vasil Velichkov Date: Mon, 29 Oct 2018 22:44:16 +0000 (+0200) Subject: text2pcap: Fix -i when IPv6 (-6) is specified X-Git-Url: http://git.samba.org/?p=metze%2Fwireshark%2Fwip.git;a=commitdiff_plain;h=5359a97d4383ca75606b6011006f44b040d5b92b text2pcap: Fix -i when IPv6 (-6) is specified When the -i option is specified the hdr_ip was always set to TRUE which resulted in a wrong header length when the IPv6 (-6) option is specified as well. To resolve this set hdr_ip only when -i is specified without -4 or -6 options. Change-Id: I21898f27ceaad603b9275ab6878ff4bd8f9586cd Reviewed-on: https://code.wireshark.org/review/30411 Reviewed-by: Peter Wu Petri-Dish: Peter Wu Tested-by: Petri Dish Buildbot Reviewed-by: Michael Mann --- diff --git a/test/suite_text2pcap.py b/test/suite_text2pcap.py index 63a7899c04..dc6412fa3c 100644 --- a/test/suite_text2pcap.py +++ b/test/suite_text2pcap.py @@ -484,3 +484,48 @@ class case_text2pcap_ipv6(subprocesstest.SubprocessTestCase): 'ipv6': {'plen': '36', 'plen_tree': None}}, self.run_text2pcap_ipv6("0000: 01 00 03 03 00 00 00 08\n", ("-S", "2905,2905,3"))) + +class case_text2pcap_i_proto(subprocesstest.SubprocessTestCase): + '''Test -i for IPv4 and IPv6''' + maxDiff = None + + def test_text2pcap_i_icmp(self): + '''Test -i without -4 or -6''' + self.assertEqual({'encapsulation': 'Ethernet', 'packets': 1, + 'datasize': 98, 'expert': ''}, + run_text2pcap_capinfos_tshark(self, + "0000 08 00 bb b3 d7 3b 00 00 51 a7 d6 7d 00 04 51 e4\n" + + "0010 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17\n" + + "0020 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27\n" + + "0030 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37\n", + ("-i", "1"))) + + def test_text2pcap_i_icmp_ipv4(self): + '''Test -i with IPv4 (-4) header''' + self.assertEqual({'encapsulation': 'Ethernet', 'packets': 1, + 'datasize': 98, 'expert': ''}, + run_text2pcap_capinfos_tshark(self, + "0000 08 00 bb b3 d7 3b 00 00 51 a7 d6 7d 00 04 51 e4\n" + + "0010 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17\n" + + "0020 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27\n" + + "0030 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37\n", + ("-i", "1", "-4", "127.0.0.1,127.0.0.1"))) + + def test_text2pcap_i_icmpv6_ipv6(self): + '''Test -i with IPv6 (-6) header''' + self.assertEqual({'encapsulation': 'Ethernet', 'packets': 1, + 'datasize': 86, 'expert': ''}, + run_text2pcap_capinfos_tshark(self, + "0000 87 00 f2 62 00 00 00 00 fe 80 00 00 00 00 00 00\n" + + "0010 00 00 00 00 00 00 00 02 01 01 52 54 00 12 34 56\n", + ("-i", "58", "-6", "::1,::1"))) + + def test_text2pcap_i_sctp_ipv6(self): + '''Test -i with IPv6 (-6) header''' + self.assertEqual({'encapsulation': 'Ethernet', 'packets': 1, + 'datasize': 90, 'expert': ''}, + run_text2pcap_capinfos_tshark(self, + "0000 0b 59 0b 59 00 00 00 00 26 98 58 51 00 03 00 18\n" + + "0010 00 00 00 00 00 00 00 00 00 00 00 03 01 00 03 03\n" + + "0020 00 00 00 08\n", + ("-i", "132", "-6", "::1,::1"))) diff --git a/text2pcap.c b/text2pcap.c index f492059b16..3793ac5295 100644 --- a/text2pcap.c +++ b/text2pcap.c @@ -152,7 +152,7 @@ static guint32 hdr_ethernet_proto = 0; /* Dummy IP header */ static int hdr_ip = FALSE; static int hdr_ipv6 = FALSE; -static long hdr_ip_proto = 0; +static long hdr_ip_proto = -1; /* Destination and source addresses for IP header */ static guint32 hdr_ip_dest_addr = 0; @@ -1512,7 +1512,6 @@ parse_options (int argc, char *argv[]) break; case 'i': - hdr_ip = TRUE; hdr_ip_proto = strtol(optarg, &p, 10); if (p == optarg || *p != '\0' || hdr_ip_proto < 0 || hdr_ip_proto > 255) { @@ -1830,6 +1829,11 @@ parse_options (int argc, char *argv[]) timecode_default = *now_tm; timecode_default.tm_isdst = -1; /* Unknown for now, depends on time given to the strptime() function */ + if (hdr_ip_proto != -1 && !(hdr_ip || hdr_ipv6)) { + /* If -i option is specified without -4 or -6 then add the default IPv4 header */ + hdr_ip = TRUE; + } + if ((hdr_tcp || hdr_udp || hdr_sctp) && !(hdr_ip || hdr_ipv6)) { /* * If TCP (-T), UDP (-u) or SCTP (-s/-S) header options are specified