2 ######################################################################
3 # Copyright (c) 2011, Network RADIUS SARL
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
13 # * Neither the name of the <organization> nor the
14 # names of its contributors may be used to endorse or promote products
15 # derived from this software without specific prior written permission.
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 # DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
21 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 ######################################################################
29 # Converts dictionaries to C structures. Does not yet do "VALUE"s.
31 # Usage: ./convert.pl dictionary ...
33 # Reads input dictionaries, and outputs "radius.h" and "dictionaries.c"
40 # Read all of the dictionaries
48 # For speed, the dictionary data structures have the first 256
49 # attributes at fixed offsets in the array. If the user didn't
50 # define them, then we set them here to be "raw" or unknown.
52 foreach $attr_val (0..255) {
53 next if defined $attributes{$attr_val};
55 $attributes{$attr_val}{'raw'} = 1;
58 if (scalar keys %attributes == 0) {
59 die "No attributes were defined\n";
63 open DICT, ">dictionaries.c" or die "Failed creating dictionaries.c: $!\n";
66 # Print out the data structues for the vendors.
68 if (scalar keys %vendor > 0) {
69 print DICT "const DICT_VENDOR nr_dict_vendors[] = {\n";
70 foreach $v (sort keys %vendor) {
72 print DICT " " . $vendor{$v}{'pec'} . ", \n";
73 print DICT " " . $vendor{$v}{'type'} . ",\n";
74 print DICT " " . $vendor{$v}{'length'} . ",\n";
75 print DICT " \"" . $v, "\"\n";
88 $vendor{""}{'pec'} = 0;
92 my $tmp = $attributes{$attr_val}{'flags'}{$_[0]};
98 print DICT $tmp . ", ";
102 # Print DICT out the attributes sorted by number.
106 print DICT "const DICT_ATTR nr_dict_attrs[] = {\n";
107 foreach $attr_val (sort {$a <=> $b} keys %attributes) {
108 print DICT " { /* $offset */ \n";
110 if (defined $attributes{$attr_val}{'raw'}) {
113 print DICT " ", $attributes{$attr_val}{'value'}, ", \n";
114 print DICT " ", $attributes{$attr_val}{'type'}, ", \n";
115 print DICT " ", $attributes{$attr_val}{'vendor'}, ", \n";
117 &printAttrFlag('has_tag');
118 &printAttrFlag('unknown');
119 # &printAttrFlag('has_tlv');
120 # &printAttrFlag('is_tlv');
121 &printAttrFlag('extended');
122 &printAttrFlag('extended_flags');
123 &printAttrFlag('evs');
124 &printAttrFlag('encrypt');
125 &printAttrFlag('length');
127 print DICT " \"", $attributes{$attr_val}{'name'}, "\", \n";
131 $attributes{$attr_val}{'offset'} = $offset++;
138 print DICT "const int nr_dict_num_attrs = ", $offset - 1, ";\n\n";
139 print DICT "const int nr_dict_num_names = ", $num_names - 1, ";\n\n";
142 print DICT "const DICT_ATTR *nr_dict_attr_names[] = {\n";
143 foreach $attr_val (sort {lc($attributes{$a}{'name'}) cmp lc($attributes{$b}{'name'})} keys %attributes) {
144 next if (defined $attributes{$attr_val}{'raw'});
146 print DICT " &nr_dict_attrs[", $attributes{$attr_val}{'offset'}, "], /* ", $attributes{$attr_val}{'name'}, " */\n";
152 open HDR, ">../include/radsec/radius.h" or die "Failed creating radius.c: $!\n";
154 print HDR "/* Automatically generated file. Do not edit */\n\n";
156 foreach $v (sort keys %vendor) {
160 $name =~ tr/a-z/A-Z/; # uppercase
161 $name =~ tr/A-Z0-9/_/c; # any ELSE becomes _
163 print HDR "#define VENDORPEC_", $name, " ", $vendor{$v}{'pec'}, "\n";
168 foreach $attr_val (sort {$a <=> $b} keys %attributes) {
169 next if (defined $attributes{$attr_val}{'raw'});
171 if ($attributes{$attr_val}{'vendor'} != $begin_vendor) {
172 print HDR "\n/* ", $vendorpec{$attributes{$attr_val}{'vendor'}}, " */\n";
173 $begin_vendor = $attributes{$attr_val}{'vendor'};
176 $name = $attributes{$attr_val}{'name'};
177 $name =~ tr/a-z/A-Z/;
178 $name =~ tr/A-Z0-9/_/c;
180 print HDR "#define PW_", $name, " ", $attributes{$attr_val}{'value'}, "\n";
184 print HDR "/* Fixed offsets to dictionary definitions of attributes */\n";
185 foreach $attr_val (sort {$a <=> $b} keys %attributes) {
186 next if (defined $attributes{$attr_val}{'raw'});
188 $name = $attributes{$attr_val}{'name'};
189 $name =~ tr/a-z/A-Z/;
192 print HDR "#define RS_DA_$name (&nr_dict_attrs[$attributes{$attr_val}{'offset'}])\n";
195 print HDR "/* Automatically generated file. Do not edit */\n";