2 Copyright (c) 2011, Network RADIUS SARL
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.
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.
32 * \brief Functions for name to number, and number to name mappings.
35 const DICT_ATTR *nr_dict_attr_byvalue(unsigned int attr, unsigned int vendor)
39 if (!vendor && (attr > 0) && (attr < 256)) {
40 if (nr_dict_attrs[attr].name) {
41 return &nr_dict_attrs[attr];
46 if (!vendor) return NULL; /* no "non-protocol" attributes */
48 start = 256; /* first 256 entries are "standard" ones */
49 end = nr_dict_num_attrs;
52 half = (start + end) / 2;
54 if ((nr_dict_attrs[half].vendor == vendor) &&
55 (nr_dict_attrs[half].attr == attr)) {
56 return &nr_dict_attrs[half];
59 if ((vendor >= nr_dict_attrs[half].vendor) &&
60 (attr > nr_dict_attrs[half].attr)) {
66 } while (start <= end);
71 const DICT_ATTR *nr_dict_attr_byname(const char *name)
76 end = nr_dict_num_names;
78 if (!name || !*name) return NULL;
83 half = (start + end) / 2;
85 rcode = strcasecmp(name, nr_dict_attr_names[half]->name);
86 if (rcode == 0) return nr_dict_attr_names[half];
95 } while (start <= end);
100 int nr_dict_attr_2struct(DICT_ATTR *da, unsigned int attr, unsigned int vendor,
101 char *buffer, size_t bufsize)
103 if (!da || !buffer) return -RSE_INVAL;
106 if (attr > 256) return -RSE_INVAL;
108 } else if (vendor > (1 << 24)) {
112 memset(da, 0, sizeof(*da));
114 da->flags.unknown = 1;
115 da->type = RS_TYPE_OCTETS;
119 snprintf(buffer, bufsize, "Attr-26.%u.%u",
122 snprintf(buffer, bufsize, "Attr-%u", attr);
130 const DICT_VALUE *nr_dict_value_byattr(UNUSED unsigned int attr,
131 UNUSED unsigned int vendor,
137 const DICT_VALUE *nr_dict_value_byname(UNUSED unsigned int attr,
138 UNUSED unsigned int vendor,
139 UNUSED const char *name)
144 int nr_dict_vendor_byname(const char *name)
146 const DICT_VENDOR *dv;
148 if (!name || !*name) return 0;
153 for (dv = &nr_dict_vendors[0]; dv->name != NULL; dv++) {
154 if (strcasecmp(dv->name, name) == 0) return dv->vendor;
160 const DICT_VENDOR *nr_dict_vendor_byvalue(unsigned int vendor)
162 const DICT_VENDOR *dv;
167 for (dv = &nr_dict_vendors[0]; dv->name != NULL; dv++) {
168 if (dv->vendor == vendor) return dv;