port to new RADIUS client library
[radsecproxy.git] / lib / 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 "    .name = \"", $v, "\", \n";
73         print DICT "    .vendor = ", $vendor{$v}{'pec'}, ", \n";
74         print DICT "    .type = ", $vendor{$v}{'type'}, ",\n";
75         print DICT "    .length = ", $vendor{$v}{'length'}, ",\n";
76         print DICT "  },\n";
77     }
78     print DICT "\n  { .name = NULL, }\n";
79     print DICT "};\n\n";
80 }
81
82 # needed for later.
83 $vendor{""}{'pec'} = 0;
84
85 #
86 #  Print DICT out the attributes sorted by number.
87 #
88 my $offset = 0;
89 my $num_names = 0;
90 print DICT "const DICT_ATTR nr_dict_attrs[] = {\n";
91 foreach $attr_val (sort {$a <=> $b} keys %attributes) {
92     print DICT "  { /* $offset */ \n";
93
94     if (defined $attributes{$attr_val}{'raw'}) {
95         print DICT "    .name = NULL, \n";
96     } else {
97         print DICT "    .name = \"", $attributes{$attr_val}{'name'}, "\", \n";
98         if ($attributes{$attr_val}{'vendor'}) {
99             print DICT "    .vendor = ", $attributes{$attr_val}{'vendor'}, ", \n";
100         }
101
102         print DICT "    .attr = ", $attributes{$attr_val}{'value'}, ", \n";
103         print DICT "    .type = ", $attributes{$attr_val}{'type'}, ", \n";
104         
105         if ($attributes{$attr_val}{'flags'}) {
106             print DICT "    .flags = {\n";
107             foreach $flag (keys %{$attributes{$attr_val}{'flags'}}) {
108                 print DICT "      .$flag = $attributes{$attr_val}{'flags'}{$flag},\n";
109             }
110             print DICT "    },\n";
111         }
112
113         $num_names++;
114     }
115
116     $attributes{$attr_val}{'offset'} = $offset++;
117     
118     print DICT "  },\n";
119     
120 }
121 print DICT "};\n\n";
122
123 print DICT "const int nr_dict_num_attrs = ", $offset - 1, ";\n\n";
124 print DICT "const int nr_dict_num_names = ", $num_names - 1, ";\n\n";
125
126 my $offset = 0;
127 print DICT "const DICT_ATTR *nr_dict_attr_names[] = {\n";
128 foreach $attr_val (sort {lc($attributes{$a}{'name'}) cmp lc($attributes{$b}{'name'})} keys %attributes) {
129     next if (defined $attributes{$attr_val}{'raw'});
130     
131     print DICT "    &nr_dict_attrs[", $attributes{$attr_val}{'offset'}, "], /* ", $attributes{$attr_val}{'name'}, " */\n";
132 }
133
134 print DICT "};\n\n";
135 close DICT;
136
137 open HDR, ">../include/radsec/radius.h" or die "Failed creating radius.c: $!\n";
138
139 print HDR "/* Automatically generated file.  Do not edit */\n\n";
140
141 foreach $v (sort keys %vendor) {
142     next if ($v eq "");
143
144     $name = $v;
145     $name =~ tr/a-z/A-Z/;               # uppercase
146     $name =~ tr/A-Z0-9/_/c;     # any ELSE becomes _
147
148     print HDR "#define VENDORPEC_", $name, " ", $vendor{$v}{'pec'}, "\n";
149 }
150 print HDR "\n";
151
152 $begin_vendor = -1;
153 foreach $attr_val (sort {$a <=> $b} keys %attributes) {
154     next if (defined $attributes{$attr_val}{'raw'});
155
156     if ($attributes{$attr_val}{'vendor'} != $begin_vendor) {
157         print HDR "\n/* ", $vendorpec{$attributes{$attr_val}{'vendor'}}, " */\n";
158         $begin_vendor = $attributes{$attr_val}{'vendor'};
159     }
160
161     $name = $attributes{$attr_val}{'name'};
162     $name =~ tr/a-z/A-Z/;
163     $name =~ tr/A-Z0-9/_/c;
164
165     print HDR "#define PW_", $name, " ", $attributes{$attr_val}{'value'}, "\n";
166 }
167 print HDR "\n";
168
169 print HDR "/* Fixed offsets to dictionary definitions of attributes */\n";
170 foreach $attr_val (sort {$a <=> $b} keys %attributes) {
171     next if (defined $attributes{$attr_val}{'raw'});
172
173     $name = $attributes{$attr_val}{'name'};
174     $name =~ tr/a-z/A-Z/;
175     $name =~ tr/-/_/;
176
177     print HDR "#define RS_DA_$name (&nr_dict_attrs[$attributes{$attr_val}{'offset'}])\n";
178 }
179
180 print HDR "/* Automatically generated file.  Do not edit */\n";
181
182 close HDR;