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
33 #include <arpa/inet.h>
35 ssize_t nr_vp_sscanf_value(VALUE_PAIR *vp, const char *value)
39 switch (vp->da->type) {
41 strlcpy(vp->vp_strvalue, value, sizeof(vp->vp_strvalue));
42 vp->length = strlen(vp->vp_strvalue);
47 vp->vp_integer = strtoul(value, &end, 10);
48 if ((value == end) || (*end != '\0')) {
49 nr_debug_error("Invalid value");
50 return -RSE_ATTR_VALUE_MALFORMED;
55 if (inet_pton(AF_INET, value, &vp->vp_ipaddr) < 0) {
60 #ifdef RS_TYPE_IPV6ADDR
61 case RS_TYPE_IPV6ADDR:
62 if (inet_pton(AF_INET6, value, &vp-vp>ipv6addr) < 0) {
73 if (sscanf(value, "%02x%02x%02x%02x%02x%02x%02x%02x",
74 &array[0], &array[1], &array[2], &array[3],
75 &array[4], &array[5], &array[6], &array[7]) != 8) {
79 for (i = 0; i < 8; i++) vp->vp_ifid[i] = array[i] & 0xff;
86 nr_debug_error("Invalid type");
87 return -RSE_ATTR_TYPE_UNKNOWN;
93 int nr_vp_sscanf(const char *string, VALUE_PAIR **pvp)
102 if (!string || !pvp) return -RSE_INVAL;
106 while (*p && (*p != ' ') && (*p != '=')) {
112 nr_debug_error("No Attribute name");
113 return -RSE_ATTR_BAD_NAME;
116 da = nr_dict_attr_byname(buffer);
118 nr_debug_error("Unknown attribute \"%s\"", buffer);
119 return -RSE_ATTR_UNKNOWN;
122 while (*p == ' ') p++;
124 nr_debug_error("Unexpected text after attribute name");
125 return -RSE_ATTR_BAD_NAME;
129 while (*p == ' ') p++;
131 vp = nr_vp_alloc(da);
132 if (!vp) return -RSE_NOMEM;
134 rcode = nr_vp_sscanf_value(vp, p);