This repository has been archived by the owner on Apr 18, 2019. It is now read-only.
forked from je9000/synfrag
-
Notifications
You must be signed in to change notification settings - Fork 3
/
icmp-xml-to-c.pl
executable file
·143 lines (116 loc) · 5 KB
/
icmp-xml-to-c.pl
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
136
137
138
139
140
141
142
143
#!/usr/local/bin/perl
#
# Copyright (c) 2012, Yahoo! Inc All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer. Redistributions
# in binary form must reproduce the above copyright notice, this list
# of conditions and the following disclaimer in the documentation and/or
# other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
# OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
# THE POSSIBILITY OF SUCH DAMAGE.
#
# Author: John Eaglesham
#
# This file is meant to parse the output of:
# http://www.iana.org/assignments/icmp-parameters/icmp-parameters.xml
# And turn it into a semi-useful C code.
#
use warnings;
use strict;
use XML::XPath;
use Data::Dumper;
# If we get a description that matches one of these regular expressions, we
# will ignore it. That way we avoid bloat in our output code because the
# default description is "Unassigned".
my $treat_as_unassigned_type = qr/unassigned/i;
my $treat_as_unassigned_code = qr/no code/i;
my %icmp_codes;
my $xp = XML::XPath->new(filename => 'icmp-parameters.xml');
my $registry = $xp->find('/registry/registry[@id="icmp-parameters-types"]');
if ( $registry->size() != 1 ) {
die "Don't know how to parse this file.";
}
my $nodeset = $registry->pop()->find('record');
foreach my $node ($nodeset->get_nodelist()) {
my $value = $node->find('value');
if ( $value->size() != 1 ) { next; }
$value = $value->pop()->string_value;
my $descr = $node->find('description');
if ( $descr->size() != 1 ) { next; }
$descr = $descr->pop()->string_value;
if ( $value !~ /^\d+$/ || !$descr || $descr =~ $treat_as_unassigned_type ) { next; }
$descr =~ s/\n+//g;
$descr =~ s/[^[:print:]]//;
$descr =~ s/ +/ /g;
$icmp_codes{$value} = { descr => $descr };
}
undef $nodeset;
$registry = $xp->find('/registry/registry[@id="icmp-parameters-codes"]/registry');
if ( $registry->size() < 1 ) {
die "Don't know how to parse this file.";
}
foreach my $code_node ($registry->get_nodelist()) {
my $id = $code_node->getAttribute('id');
if ( !defined $id || $id !~ /^icmp-parameters-codes-/ ) {
die "Don't know how to parse this file.";
}
# Skip the codes that give a range.
if ( $id !~ /^icmp-parameters-codes-(\d+)$/ ) {
next;
}
$id = $1;
$nodeset = $code_node->find('record');
foreach my $node ($nodeset->get_nodelist()) {
my $value = $node->find('value');
if ( $value->size() != 1 ) { next; }
$value = $value->pop()->string_value;
my $descr = $node->find('description');
if ( $descr->size() != 1 ) { next; }
$descr = $descr->pop()->string_value;
if ( $value !~ /^\d+$/ || !$descr || $descr =~ $treat_as_unassigned_code ) { next; }
$descr =~ s/\n+//g;
$descr =~ s/ +/ /g;
if ( !exists $icmp_codes{$id} ) { next; }
$icmp_codes{$id}->{codes}->{$value} = $descr;
}
}
# It would be possible to create an array, but the array would have a lot of
# "holes", meaning you would have to allocate 255 entries but the vast
# majority would be set to NULL. The problem would be worse for the codes.
#foreach my $type ( sort{ $a <=> $b } ( keys( %icmp_codes ) ) ) {
# my $name = $icmp_codes{$type}->{descr};
# print qq{icmp_type_names[$type] = "$name"\n};
#}
# Instead we generate functions.
print qq#char *icmp_type_to_name( unsigned char type )\n{\n#;
foreach my $type ( sort{ $a <=> $b } ( keys( %icmp_codes ) ) ) {
my $name = $icmp_codes{$type}->{descr};
print qq# if ( type == $type ) return "$name";\n#;
}
print qq# return "Unassigned";\n}\n\n#;
print qq#char *icmp_code_to_name( unsigned char type, unsigned char code )\n{\n#;
foreach my $type ( sort{ $a <=> $b } ( keys( %icmp_codes ) ) ) {
if ( !exists $icmp_codes{$type}->{codes} ) { next; }
print qq# if ( type == $type ) {\n#;
foreach my $code ( sort{ $a <=> $b } ( keys( %{ $icmp_codes{$type}->{codes} } ) ) ) {
my $name = $icmp_codes{$type}->{codes}->{$code};
print qq# if ( code == $code ) return "$name";\n#;
}
print qq# }\n#;
}
print qq# return "Unassigned";\n}\n\n#;
#die Dumper \%icmp_codes;