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 Routines to parse strings into internal data structures
34 #ifdef HAVE_ARPA_INET_H
35 #include <arpa/inet.h>
38 ssize_t nr_vp_sscanf_value(VALUE_PAIR *vp, const char *value)
42 switch (vp->da->type) {
43 case RS_TYPE_STRING: {
44 size_t len = strlen(value);
46 if (len >= RS_MAX_STRING_LEN)
47 return -RSE_ATTR_TOO_LARGE;
49 memcpy(vp->vp_strvalue, value, len + 1);
50 return (vp->length = len);
54 vp->vp_integer = strtoul(value, &end, 10);
55 if ((value == end) || (*end != '\0')) {
56 nr_debug_error("Invalid value");
57 return -RSE_ATTR_VALUE_MALFORMED;
62 if (inet_pton(AF_INET, value, &vp->vp_ipaddr) < 0) {
67 #ifdef RS_TYPE_IPV6ADDR
68 case RS_TYPE_IPV6ADDR:
69 if (inet_pton(AF_INET6, value, &vp-vp>ipv6addr) < 0) {
80 if (sscanf(value, "%02x%02x%02x%02x%02x%02x%02x%02x",
81 &array[0], &array[1], &array[2], &array[3],
82 &array[4], &array[5], &array[6], &array[7]) != 8) {
86 for (i = 0; i < 8; i++) vp->vp_ifid[i] = array[i] & 0xff;
93 nr_debug_error("Invalid type");
94 return -RSE_ATTR_TYPE_UNKNOWN;
100 int nr_vp_sscanf(const char *string, VALUE_PAIR **pvp)
109 if (!string || !pvp) return -RSE_INVAL;
113 while (*p && (*p != ' ') && (*p != '=')) {
119 nr_debug_error("No Attribute name");
120 return -RSE_ATTR_BAD_NAME;
123 da = nr_dict_attr_byname(buffer);
125 nr_debug_error("Unknown attribute \"%s\"", buffer);
126 return -RSE_ATTR_UNKNOWN;
129 while (*p == ' ') p++;
131 nr_debug_error("Unexpected text after attribute name");
132 return -RSE_ATTR_BAD_NAME;
136 while (*p == ' ') p++;
138 vp = nr_vp_alloc(da);
139 if (!vp) return -RSE_NOMEM;
141 rcode = nr_vp_sscanf_value(vp, p);