summaryrefslogtreecommitdiffstats
path: root/contrib/ipfilter/test/h4to6
blob: e31f7c4224b61b8f0d66d32eac73ad149814087a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
@P=();
$line = 0;
while (<>) {
	s/\=192.168.1.188/\=c0a8:0100::bc/g;
	s/\=192.168.1.188/\=c0a8:0100::bc/g;
	@F = split;
	if (/^#/) {
		@P[$nline++] = join(" ",@F);
		next;
	}
	$line = 0 if (/^\[/);
	if ($line == 1) {
		$len = hex($F[1]) - 20;
		$pr = hex($F[4]) & 0xff;
		$pr = 58 if ($pr == 1);
		$ttl = hex($F[4]) >> 8;
		&replaceip($_, $len, $pr, $ttl);
		$ipline = $nline;
		$err = 0;
	} elsif ($line == 2) {
		if ($pr == 58) {
			#
			# Map the ICMP type codes from IPv4 to IPv6
			# and update the checksum to compensate.
			#
			if ($F[0] =~ /^0800/) {
				$F[0] =~ s/^0800/8000/;
				$d = 0x7800;
			}
			if ($F[0] =~ /^0000/) {
				$F[0] =~ s/^0000/8100/;
				$d = 0x8100;
			}
			if ($F[0] =~ /^0304/) {
				$F[0] =~ s/^0304/0200/;
				$d = 0xfefc;
				$err = 1;
			}
			if ($F[0] =~ /^03/) {
				$F[0] =~ s/^03/01/;
				$d = 0xfe00;
				$err = 1;
			}
			if ($F[0] =~ /^0b/) {
				$F[0] =~ s/^0b/03/;
				$d = 0xf800;
				$err = 1;
			}
			if ($F[0] =~ /^0c/) {
				$F[0] =~ s/^0c/04/;
				$d = 0xf800;
				$err = 1;
			}
			$F[1] = sprintf "%04x", hex($F[1]) - $d;
		}
		@P[$nline++] = join(" ",@F);
	} elsif ($line == 3) {
		if ($pr == 58 && $err == 1 && $F[0] =~ /^45/) {
			local($l) = hex($F[1]) - 20;
			local($p) = hex($F[4]) & 0xff;
			$p = 58 if ($p == 1);
			local($t) = hex($F[4]) >> 8;
			&replaceip(join(" ", @F), $l, $p, $t);
			@H = split(/ /, $P[$ipline]);
			$H[2] += 20;
			$P[$ipline] = join(" ",@H);
		} else {
			@P[$nline++] = join(" ",@F);
		}
	} else {
		@P[$nline++] = join(" ",@F);
	}
	$line++;
}

for ($li = 0; $li < $nline; $li++) {
	print "$P[$li]\n";
}

exit(0);

sub replaceip {
	local(@G) = split(/\s/,$_[0]);
	local($p) = "";

	$p = sprintf "6000 0000 %04x %02x%02x", $_[1], $_[2], $_[3];
	if ($G[6] =~ /^c0a8/) {
		$fmt = " %02x%02x %02x00 0000 0000 0000 0000 0000 00%02x";
	} else {
		if ($G[6] =~ /^0[4a]../) {
			$fmt = " 00%02x 00%02x 00%02x 0000 0000 0000 0000 00%02x";
		} else {
			$fmt = " 00%02x 0000 0000 0000 0000 00%02x 00%02x 00%02x";
		}
		if ($G[6] =~ /^0a/) {
			$G[6] =~ s/^0a/10/;
		}
		if ($G[6] =~ /0a$/) {
			$G[6] =~ s/0a$/10/;
		}
		if ($G[7] =~ /^0a/) {
			$G[7] =~ s/^0a/10/;
		}
		if ($G[7] =~ /0a$/) {
			$G[7] =~ s/0a$/10/;
		}
	}
	$p = $p.sprintf $fmt, hex($G[6]) >> 8, hex($G[6]) & 0xff,
		     hex($G[7]) >> 8, hex($G[7]) & 0xff;

	if ($G[6] =~ /^c0a8/) {
		$fmt = " %02x%02x %02x00 0000 0000 0000 0000 0000 00%02x";
	} else {
		if ($G[8] =~ /^0[4a]../) {
			$fmt = " 00%02x 00%02x 00%02x 0000 0000 0000 0000 00%02x";
		} else {
			$fmt = " 00%02x 0000 0000 0000 0000 00%02x 00%02x 00%02x";
		}
		if ($G[8] =~ /^0a/) {
			$G[8] =~ s/^0a/10/;
		}
		if ($G[8] =~ /0a$/) {
			$G[8] =~ s/0a$/10/;
		}
		if ($G[9] =~ /^0a/) {
			$G[9] =~ s/^0a/10/;
		}
		if ($G[9] =~ /0a$/) {
			$G[9] =~ s/0a$/10/;
		}
	}
	$p = $p.sprintf $fmt, hex($G[8]) >> 8, hex($G[8]) & 0xff,
		     hex($G[9]) >> 8, hex($G[9]) & 0xff;
	$P[$nline++] = $p;
}
OpenPOWER on IntegriCloud