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.
29 * \brief Functions to manipulate C structure versions of RADIUS attributes.
34 void nr_vp_free(VALUE_PAIR **head)
36 VALUE_PAIR *next, *vp;
38 if (!head || !*head) return;
42 if (vp) next = vp->next;
43 if (vp->da->flags.encrypt) {
44 memset(vp, 0, sizeof(vp));
54 VALUE_PAIR *nr_vp_init(VALUE_PAIR *vp, const DICT_ATTR *da)
56 memset(vp, 0, sizeof(*vp));
59 vp->length = da->flags.length;
65 VALUE_PAIR *nr_vp_alloc(const DICT_ATTR *da)
67 VALUE_PAIR *vp = NULL;
70 nr_strerror_printf("Unknown attribute");
74 vp = malloc(sizeof(*vp));
76 nr_strerror_printf("Out of memory");
80 return nr_vp_init(vp, da);
83 VALUE_PAIR *nr_vp_alloc_raw(unsigned int attr, unsigned int vendor)
85 VALUE_PAIR *vp = NULL;
88 vp = malloc(sizeof(*vp) + sizeof(*da) + 64);
90 nr_strerror_printf("Out of memory");
93 memset(vp, 0, sizeof(*vp));
95 da = (DICT_ATTR *) (vp + 1);
97 if (nr_dict_attr_2struct(da, attr, vendor, (char *) (da + 1), 64) < 0) {
107 int nr_vp_set_data(VALUE_PAIR *vp, const void *data, size_t sizeof_data)
109 int rcode = 1; /* OK */
111 if (!vp || !data || (sizeof_data == 0)) return -RSE_INVAL;
113 switch (vp->da->type) {
115 vp->vp_integer = *(const uint8_t *) data;
119 vp->vp_integer = *(const uint16_t *) data;
122 case RS_TYPE_INTEGER:
125 vp->vp_integer = *(const uint32_t *) data;
129 if (sizeof_data >= sizeof(vp->vp_strvalue)) {
130 sizeof_data = sizeof(vp->vp_strvalue) - 1;
131 rcode = 0; /* truncated */
134 memcpy(vp->vp_strvalue, (const char *) data, sizeof_data);
135 vp->vp_strvalue[sizeof_data + 1] = '\0';
136 vp->length = sizeof_data;
140 if (sizeof_data > sizeof(vp->vp_octets)) {
141 sizeof_data = sizeof(vp->vp_octets);
142 rcode = 0; /* truncated */
144 memcpy(vp->vp_octets, data, sizeof_data);
145 vp->length = sizeof_data;
149 return -RSE_ATTR_TYPE_UNKNOWN;
155 VALUE_PAIR *nr_vp_create(int attr, int vendor, const void *data, size_t data_len)
160 da = nr_dict_attr_byvalue(attr, vendor);
161 if (!da) return NULL;
163 vp = nr_vp_alloc(da);
164 if (!vp) return NULL;
166 if (nr_vp_set_data(vp, data, data_len) < 0) {
174 void nr_vps_append(VALUE_PAIR **head, VALUE_PAIR *tail)
179 head = &((*head)->next);
185 VALUE_PAIR *nr_vps_find(VALUE_PAIR *head,
186 unsigned int attr, unsigned int vendor)
189 if ((head->da->attr == attr) &&
190 (head->da->vendor == vendor)) return head;