fc04ee2dbd67f87828bd96dc51f1038e7e9ce929
[libradsec.git] / lib / radius / dict.c
1 /*
2 Copyright (c) 2011, Network RADIUS SARL
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7     * Redistributions of source code must retain the above copyright
8       notice, this list of conditions and the following disclaimer.
9     * Redistributions in binary form must reproduce the above copyright
10       notice, this list of conditions and the following disclaimer in the
11       documentation and/or other materials provided with the distribution.
12     * Neither the name of the <organization> nor the
13       names of its contributors may be used to endorse or promote products
14       derived from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include "client.h"
29 #include <ctype.h>
30
31 /** \file dict.c
32  *  \brief Functions for name to number, and number to name mappings.
33  */
34
35 const DICT_ATTR *nr_dict_attr_byvalue(unsigned int attr, unsigned int vendor)
36 {
37         int start, half, end;
38
39         if (!vendor && (attr > 0) && (attr < 256)) {
40                 if (nr_dict_attrs[attr].name) {
41                         return &nr_dict_attrs[attr];
42                 }
43                 return NULL;
44         }
45
46         if (!vendor) return NULL; /* no "non-protocol" attributes */
47
48         start = 256;            /* first 256 entries are "standard" ones */
49         end = nr_dict_num_attrs;
50
51         do {
52                 half = (start + end) / 2;
53
54                 if ((nr_dict_attrs[half].vendor == vendor) &&
55                     (nr_dict_attrs[half].attr == attr)) {
56                         return &nr_dict_attrs[half];
57                 }
58
59                 if ((vendor >= nr_dict_attrs[half].vendor) &&
60                     (attr > nr_dict_attrs[half].attr)) {
61                         start = half + 1;
62                 } else {
63                         end = half - 1;
64                 }
65
66         } while (start <= end);
67
68         return NULL;
69 }
70
71 const DICT_ATTR *nr_dict_attr_byname(const char *name)
72 {
73         int start, half, end;
74
75         start = 1;
76         end = nr_dict_num_names;
77
78         if (!name || !*name) return NULL;
79
80         do {
81                 int rcode;
82
83                 half = (start + end) / 2;
84
85                 rcode = strcasecmp(name, nr_dict_attr_names[half]->name);
86                 if (rcode == 0) return nr_dict_attr_names[half];
87
88                 if (rcode > 0) {
89                         start = half + 1;
90                 } else {
91                         end = half - 1;
92                 }
93
94
95         } while (start <= end);
96
97         return NULL;
98 }
99
100 int nr_dict_attr_2struct(DICT_ATTR *da, unsigned int attr, unsigned int vendor,
101                          char *buffer, size_t bufsize)
102 {
103         if (!da || !buffer) return -RSE_INVAL;
104
105         if (!vendor) {
106                 if (attr > 256) return -RSE_INVAL;
107
108         } else if (vendor > (1 << 24)) {
109                 return -RSE_INVAL;
110         }
111
112         memset(da, 0, sizeof(*da));
113         da->attr = attr;
114         da->flags.unknown = 1;
115         da->type = RS_TYPE_OCTETS;
116         da->vendor = vendor;
117
118         if (da->vendor) {
119                 snprintf(buffer, bufsize, "Attr-26.%u.%u",
120                          vendor, attr);
121         } else {
122                 snprintf(buffer, bufsize, "Attr-%u", attr);
123         }
124         da->name = buffer;
125
126         return 0;
127 }
128
129
130 const DICT_VALUE *nr_dict_value_byattr(UNUSED unsigned int attr,
131                                  UNUSED unsigned int vendor,
132                                  UNUSED int value)
133 {
134         return NULL;
135 }
136
137 const DICT_VALUE *nr_dict_value_byname(UNUSED unsigned int attr,
138                                  UNUSED unsigned int vendor,
139                                  UNUSED const char *name)
140 {
141         return NULL;
142 }
143
144 int nr_dict_vendor_byname(const char *name)
145 {
146         const DICT_VENDOR *dv;
147
148         if (!name || !*name) return 0;
149
150         /*
151          *      O(n) lookup.
152          */
153         for (dv = &nr_dict_vendors[0]; dv->name != NULL; dv++) {
154                 if (strcasecmp(dv->name, name) == 0) return dv->vendor;
155         }
156
157         return 0;
158 }
159
160 const DICT_VENDOR *nr_dict_vendor_byvalue(unsigned int vendor)
161 {
162         const DICT_VENDOR *dv;
163
164         /*
165          *      O(n) lookup.
166          */
167         for (dv = &nr_dict_vendors[0]; dv->name != NULL; dv++) {
168                 if (dv->vendor == vendor) return dv;
169         }
170
171         return NULL;
172 }