Move lib to the root.
[libradsec.git] / radius / convert.pl
1 #!/usr/bin/env perl
2 ######################################################################
3 # Copyright (c) 2011, Network RADIUS SARL
4 # All rights reserved.
5 #
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.
16
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 ######################################################################
28 #
29 #  Converts dictionaries to C structures.  Does not yet do "VALUE"s.
30 #
31 #  Usage: ./convert.pl dictionary ...
32 #
33 #  Reads input dictionaries, and outputs "radius.h" and "dictionaries.c"
34 #
35 #  $Id$
36 #
37 require "common.pl";
38
39 #
40 #  Read all of the dictionaries
41 #
42 while (@ARGV) {
43     $filename = shift;
44     do_file($filename);
45 }
46
47 #
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.
51 #
52 foreach $attr_val (0..255) {
53     next if defined $attributes{$attr_val};
54     
55     $attributes{$attr_val}{'raw'} = 1;
56 }
57
58 if (scalar keys %attributes == 0) {
59     die "No attributes were defined\n";
60 }
61
62
63 open DICT, ">dictionaries.c" or die "Failed creating dictionaries.c: $!\n";
64
65 #
66 #  Print out the data structues for the vendors.
67 #
68 if (scalar keys %vendor > 0) {
69     print DICT "const DICT_VENDOR nr_dict_vendors[] = {\n";
70     foreach $v (sort keys %vendor) {
71         print DICT "  { \n";
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";
76         print DICT "  },\n";
77     }
78     print DICT "  { \n";
79     print DICT "    0,\n";
80     print DICT "    0,\n";
81     print DICT "    0,\n";
82     print DICT "    NULL\n";
83     print DICT "  },\n";
84     print DICT "};\n\n";
85 }
86
87 # needed for later.
88 $vendor{""}{'pec'} = 0;
89
90 sub printAttrFlag
91 {
92     my $tmp = $attributes{$attr_val}{'flags'}{$_[0]};
93
94     if (!$tmp) {
95         $tmp = 0;
96     }
97
98     print DICT $tmp . ", ";
99 }
100
101 #
102 #  Print DICT out the attributes sorted by number.
103 #
104 my $offset = 0;
105 my $num_names = 0;
106 print DICT "const DICT_ATTR nr_dict_attrs[] = {\n";
107 foreach $attr_val (sort {$a <=> $b} keys %attributes) {
108     print DICT "  { /* $offset */ \n";
109
110     if (defined $attributes{$attr_val}{'raw'}) {
111         print DICT "    0\n",
112     } else {
113         print DICT "    ", $attributes{$attr_val}{'value'}, ", \n";
114         print DICT "    ", $attributes{$attr_val}{'type'}, ", \n";
115         print DICT "    ", $attributes{$attr_val}{'vendor'}, ", \n";
116         print DICT "    { ";
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');
126         print DICT "},\n";
127         print DICT "    \"", $attributes{$attr_val}{'name'}, "\", \n";
128         $num_names++;
129     }
130
131     $attributes{$attr_val}{'offset'} = $offset++;
132     
133     print DICT "  },\n";
134     
135 }
136 print DICT "};\n\n";
137
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";
140
141 my $offset = 0;
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'});
145     
146     print DICT "    &nr_dict_attrs[", $attributes{$attr_val}{'offset'}, "], /* ", $attributes{$attr_val}{'name'}, " */\n";
147 }
148
149 print DICT "};\n\n";
150 close DICT;
151
152 open HDR, ">../include/radsec/radius.h" or die "Failed creating radius.c: $!\n";
153
154 print HDR "/* Automatically generated file.  Do not edit */\n\n";
155
156 foreach $v (sort keys %vendor) {
157     next if ($v eq "");
158
159     $name = $v;
160     $name =~ tr/a-z/A-Z/;               # uppercase
161     $name =~ tr/A-Z0-9/_/c;     # any ELSE becomes _
162
163     print HDR "#define VENDORPEC_", $name, " ", $vendor{$v}{'pec'}, "\n";
164 }
165 print HDR "\n";
166
167 $begin_vendor = -1;
168 foreach $attr_val (sort {$a <=> $b} keys %attributes) {
169     next if (defined $attributes{$attr_val}{'raw'});
170
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'};
174     }
175
176     $name = $attributes{$attr_val}{'name'};
177     $name =~ tr/a-z/A-Z/;
178     $name =~ tr/A-Z0-9/_/c;
179
180     print HDR "#define PW_", $name, " ", $attributes{$attr_val}{'value'}, "\n";
181 }
182 print HDR "\n";
183
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'});
187
188     $name = $attributes{$attr_val}{'name'};
189     $name =~ tr/a-z/A-Z/;
190     $name =~ tr/-/_/;
191
192     print HDR "#define RS_DA_$name (&nr_dict_attrs[$attributes{$attr_val}{'offset'}])\n";
193 }
194
195 print HDR "/* Automatically generated file.  Do not edit */\n";
196
197 close HDR;