1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
|
/* Copyright 2011 PADL Software Pty Ltd. All rights reserved.
See the file COPYING for licensing information. */
#if defined HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
#include <radsec/radsec.h>
#include <radius/client.h>
#define RS_ERR(err) ((err) < 0 ? -err : RSE_OK)
void
rs_avp_free (rs_avp **vps)
{
nr_vp_free (vps);
}
size_t
rs_avp_length (rs_const_avp *vp)
{
assert (vp != NULL);
return vp->length;
}
rs_attr_type_t
rs_avp_typeof (rs_const_avp *vp)
{
return vp ? vp->da->type : RS_TYPE_INVALID;
}
void
rs_avp_attrid (rs_const_avp *vp,
unsigned int *attr,
unsigned int *vendor)
{
*attr = vp->da->attr;
*vendor = vp->da->vendor;
}
const char *
rs_avp_name (rs_const_avp *vp)
{
return vp ? vp->da->name : NULL;
}
void
rs_avp_append (rs_avp **head, rs_avp *tail)
{
nr_vps_append (head, tail);
}
rs_avp *
rs_avp_find (rs_avp *vp, unsigned int attr, unsigned int vendor)
{
if (vp == NULL)
return NULL;
return nr_vps_find (vp, attr, vendor);
}
rs_const_avp *
rs_avp_find_const (rs_const_avp *vp,
unsigned int attr, unsigned int vendor)
{
if (vp == NULL)
return NULL;
return nr_vps_find ((rs_avp *)vp, attr, vendor);
}
rs_avp *
rs_avp_alloc (unsigned int attr, unsigned int vendor)
{
const DICT_ATTR *da;
VALUE_PAIR *vp;
da = nr_dict_attr_byvalue (attr, vendor);
if (da == NULL) {
vp = nr_vp_alloc_raw (attr, vendor);
} else {
vp = nr_vp_alloc (da);
}
if (vp == NULL)
return NULL;
return vp;
}
rs_avp *
rs_avp_dup (rs_const_avp *vp)
{
rs_avp *vp2;
vp2 = nr_vp_alloc (vp->da);
if (vp2 == NULL)
return NULL;
vp2->length = vp->length;
vp2->tag = vp->tag;
vp2->next = NULL;
#ifdef RS_TYPE_TLV
if (rs_avp_is_tlv (vp)) {
vp2->vp_tlv = malloc (vp->length);
if (vp2->vp_tlv == NULL) {
rs_avp_free (vp2);
return NULL;
}
memcpy (vp2->vp_tlv, vp->vp_tlv, vp->length);
return vp2;
}
#endif
memcpy (vp2->vp_strvalue, vp->vp_strvalue, vp->length);
if (rs_avp_is_string (vp))
vp2->vp_strvalue[vp->length] = '\0';
return vp2;
}
rs_avp *
rs_avp_next (rs_avp *avp)
{
return avp ? avp->next : NULL;
}
rs_const_avp *
rs_avp_next_const (rs_const_avp *avp)
{
return avp ? avp->next : NULL;
}
int
rs_avp_delete (rs_avp **first,
unsigned int attr, unsigned int vendor)
{
int found = 0;
rs_avp **p;
for (p = first; *p != NULL; p++) {
if ((*p)->da->attr == attr &&
(*p)->da->vendor == vendor) {
rs_avp *next = (*p)->next;
(*p)->next = NULL;
rs_avp_free (p);
*p = next;
found++;
}
}
return found ? RSE_OK : RSE_ATTR_UNKNOWN;
}
const char *
rs_avp_string_value (rs_const_avp *vp)
{
if (!rs_avp_is_string (vp))
return NULL;
return vp->vp_strvalue;
}
int
rs_avp_string_set (rs_avp *vp, const char *str)
{
int err;
if (vp == NULL)
return RSE_INVAL;
if (!rs_avp_is_string (vp))
return RSE_ATTR_INVALID;
err = nr_vp_set_data (vp, str, strlen (str));
return RS_ERR(err);
}
uint32_t
rs_avp_integer_value (rs_const_avp *vp)
{
if (!rs_avp_is_integer (vp))
return 0;
return vp->vp_integer;
}
int
rs_avp_integer_set (rs_avp *vp, uint32_t val)
{
int err;
if (vp == NULL)
return RSE_INVAL;
if (!rs_avp_is_integer (vp))
return RSE_ATTR_INVALID;
err = nr_vp_set_data (vp, &val, sizeof (val));
return RS_ERR(err);
}
uint32_t
rs_avp_ipaddr_value (rs_const_avp *vp)
{
if (!rs_avp_is_ipaddr (vp))
return 0;
return vp->vp_ipaddr;
}
int
rs_avp_ipaddr_set (rs_avp *vp, struct in_addr in)
{
int err;
if (vp == NULL)
return RSE_INVAL;
if (!rs_avp_is_ipaddr (vp))
return RSE_ATTR_INVALID;
err = nr_vp_set_data (vp, &in, sizeof (in));
return RS_ERR(err);
}
time_t
rs_avp_date_value (rs_const_avp *vp)
{
if (!rs_avp_is_date (vp))
return 0;
return vp->vp_date;
}
int
rs_avp_date_set (rs_avp *vp, time_t date)
{
uint32_t date32;
int err;
if (vp == NULL)
return RSE_INVAL;
if (!rs_avp_is_date (vp))
return RSE_ATTR_INVALID;
if (date > 0xFFFFFFFF)
return RSE_ATTR_INVALID;
date32 = (uint32_t)date;
err = nr_vp_set_data (vp, &date32, sizeof (date32));
return RS_ERR(err);
}
const unsigned char *
rs_avp_octets_value_const_ptr (rs_const_avp *vp)
{
return rs_avp_octets_value_ptr ((rs_avp *)vp);
}
unsigned char *
rs_avp_octets_value_ptr (rs_avp *vp)
{
if (vp == NULL)
return NULL;
#ifdef RS_TYPE_TLV
if (rs_avp_is_tlv (vp))
return vp->vp_tlv;
#endif
return vp->vp_octets;
}
int
rs_avp_octets_value_byref (rs_avp *vp,
unsigned char **p,
size_t *len)
{
if (vp == NULL)
return RSE_INVAL;
*len = vp->length;
*p = (unsigned char *)rs_avp_octets_value_ptr (vp);
return RSE_OK;
}
int
rs_avp_octets_value (rs_const_avp *vp,
unsigned char *buf,
size_t *len)
{
if (vp == NULL)
return RSE_INVAL;
if (vp->length > *len) {
*len = vp->length;
return RSE_ATTR_TOO_SMALL;
}
*len = vp->length;
#ifdef RS_TYPE_TLV
if (rs_avp_is_tlv (vp))
memcpy (buf, vp->vp_tlv, vp->length);
else
#endif
memcpy (buf, vp->vp_octets, vp->length);
return RSE_OK;
}
int
rs_avp_fragmented_value (rs_const_avp *vps,
unsigned char *buf,
size_t *len)
{
size_t total_len = 0;
unsigned char *p;
rs_const_avp *vp;
if (vps == NULL)
return RSE_INVAL;
if (!rs_avp_is_octets (vps) &&
!rs_avp_is_string (vps))
return RSE_ATTR_INVALID;
for (vp = vps;
vp != NULL;
vp = rs_avp_find_const (vp->next, vp->da->attr, vp->da->vendor))
total_len += vp->length;
if (*len < total_len) {
*len = total_len;
return RSE_ATTR_TOO_SMALL;
}
for (vp = vps, p = buf;
vp != NULL;
vp = rs_avp_find_const (vp->next, vp->da->attr, vp->da->vendor)) {
memcpy (p, vp->vp_octets, vp->length);
p += vp->length;
}
*len = total_len;
return RSE_OK;
}
int
rs_avp_octets_set (rs_avp *vp,
const unsigned char *buf,
size_t len)
{
int err;
if (!rs_avp_is_octets (vp))
return RSE_ATTR_INVALID;
err = nr_vp_set_data (vp, buf, len);
return RS_ERR(err);
}
int
rs_avp_ifid_value (rs_const_avp *vp, uint8_t val[8])
{
if (!rs_avp_is_ifid (vp))
return RSE_ATTR_INVALID;
memcpy (val, vp->vp_ifid, 8);
return RSE_OK;
}
int
rs_avp_ifid_set (rs_avp *vp, const uint8_t val[8])
{
int err;
if (!rs_avp_is_ifid (vp))
return RSE_ATTR_INVALID;
err = nr_vp_set_data (vp, val, 8);
return RS_ERR(err);
}
uint8_t
rs_avp_byte_value (rs_const_avp *vp)
{
if (!rs_avp_is_byte (vp))
return 0;
return vp->vp_integer;
}
int
rs_avp_byte_set (rs_avp *vp, uint8_t val)
{
int err;
if (!rs_avp_is_byte (vp))
return RSE_ATTR_INVALID;
err = nr_vp_set_data (vp, &val, sizeof (val));
return RS_ERR(err);
}
uint16_t
rs_avp_short_value (rs_const_avp *vp)
{
if (!rs_avp_is_short (vp))
return 0;
return vp->vp_integer;
}
int
rs_avp_short_set (rs_avp *vp, uint16_t val)
{
int err;
if (!rs_avp_is_short (vp))
return RSE_ATTR_INVALID;
err = nr_vp_set_data (vp, &val, sizeof (val));
return RS_ERR(err);
}
int
rs_attr_find (const char *name,
unsigned int *attr,
unsigned int *vendor)
{
const DICT_ATTR *da;
da = nr_dict_attr_byname (name);
if (da == NULL)
return RSE_ATTR_UNKNOWN;
*attr = da->attr;
*vendor = da->vendor;
return RSE_OK;
}
size_t
rs_avp_display_value (rs_const_avp *vp,
char *buffer,
size_t buflen)
{
return nr_vp_snprintf_value (buffer, buflen, vp);
}
|