npf_var.c revision 1.2 1 /* $NetBSD: npf_var.c,v 1.2 2012/01/12 20:41:33 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2011-2012 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: npf_var.c,v 1.2 2012/01/12 20:41:33 christos Exp $");
34
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38
39 #define _NPFVAR_PRIVATE
40 #include "npfctl.h"
41
42 typedef struct npf_element {
43 void * e_data;
44 struct npf_element *e_next;
45 } npf_element_t;
46
47 struct npfvar {
48 char * v_key;
49 npf_element_t * v_elements;
50 npf_element_t * v_last;
51 int v_type;
52 size_t v_count;
53 void * v_next;
54 };
55
56 static npfvar_t * var_list = NULL;
57 static size_t var_num = 0;
58
59 npfvar_t *
60 npfvar_create(const char *name)
61 {
62 npfvar_t *vp = zalloc(sizeof(*vp));
63 vp->v_key = xstrdup(name);
64 return vp;
65 }
66
67 npfvar_t *
68 npfvar_lookup(const char *key)
69 {
70 for (npfvar_t *it = var_list; it != NULL; it = it->v_next)
71 if (strcmp(it->v_key, key) == 0)
72 return it;
73 return NULL;
74 }
75
76 const char *
77 npfvar_type(size_t t)
78 {
79 if (t >= __arraycount(npfvar_types)) {
80 return "unknown";
81 }
82 return npfvar_types[t];
83 }
84
85 void
86 npfvar_add(npfvar_t *vp)
87 {
88 vp->v_next = var_list;
89 var_list = vp;
90 var_num++;
91 }
92
93 npfvar_t *
94 npfvar_add_element(npfvar_t *vp, int type, const void *data, size_t len)
95 {
96 npf_element_t *el;
97
98 if (vp->v_count == 0) {
99 vp->v_type = type;
100 } else if (NPFVAR_TYPE(vp->v_type) != NPFVAR_TYPE(type)) {
101 yyerror("element type '%s' does not match variable type '%s'",
102 npfvar_type(type), npfvar_type(vp->v_type));
103 return NULL;
104 }
105 vp->v_count++;
106 el = zalloc(sizeof(*el));
107 el->e_data = zalloc(len);
108 memcpy(el->e_data, data, len);
109
110 /* Preserve order of insertion. */
111 if (vp->v_elements == NULL) {
112 vp->v_elements = el;
113 } else {
114 vp->v_last->e_next = el;
115 }
116 vp->v_last = el;
117 return vp;
118 }
119
120 npfvar_t *
121 npfvar_add_elements(npfvar_t *vp, npfvar_t *vp2)
122 {
123 if (vp2 == NULL)
124 return vp;
125 if (vp == NULL)
126 return vp2;
127
128 if (vp->v_elements == NULL) {
129 if (vp2->v_elements) {
130 vp->v_type = vp2->v_type;
131 vp->v_elements = vp2->v_elements;
132 }
133 } else if (vp2->v_elements) {
134 if (NPFVAR_TYPE(vp->v_type) != NPFVAR_TYPE(vp2->v_type)) {
135 yyerror("variable '%s' type '%s' does not match "
136 "variable '%s' type '%s'", vp->v_key,
137 npfvar_type(vp->v_type),
138 vp2->v_key, npfvar_type(vp2->v_type));
139 return NULL;
140 }
141 vp->v_last->e_next = vp2->v_elements;
142 }
143 if (vp2->v_elements) {
144 vp->v_last = vp2->v_last;
145 vp->v_count += vp2->v_count;
146 vp2->v_elements = NULL;
147 vp2->v_count = 0;
148 vp2->v_last = NULL;
149 }
150 npfvar_destroy(vp2);
151 return vp;
152 }
153
154 static void
155 npfvar_free_elements(npf_element_t *el)
156 {
157 if (el == NULL)
158 return;
159 npfvar_free_elements(el->e_next);
160 free(el->e_data);
161 free(el);
162 }
163
164 void
165 npfvar_destroy(npfvar_t *vp)
166 {
167 npfvar_free_elements(vp->v_elements);
168 free(vp->v_key);
169 free(vp);
170 }
171
172 char *
173 npfvar_expand_string(const npfvar_t *vp)
174 {
175 return npfvar_get_data(vp, NPFVAR_STRING, 0);
176 }
177
178 size_t
179 npfvar_get_count(const npfvar_t *vp)
180 {
181 return vp ? vp->v_count : 0;
182 }
183
184 int
185 npfvar_get_type(const npfvar_t *vp)
186 {
187 return vp ? vp->v_type : -1;
188 }
189
190 static void *
191 npfvar_get_data1(const npfvar_t *vp, int type, size_t idx, size_t level)
192 {
193 npf_element_t *el;
194
195 if (level >= var_num) {
196 yyerror("variable loop for '%s'", vp->v_key);
197 return NULL;
198 }
199
200 if (vp == NULL)
201 return NULL;
202
203 if (NPFVAR_TYPE(vp->v_type) != NPFVAR_TYPE(type)) {
204 yyerror("variable '%s' is of type '%s' not '%s'", vp->v_key,
205 npfvar_type(vp->v_type), npfvar_type(type));
206 return NULL;
207 }
208
209 if (vp->v_count <= idx) {
210 yyerror("variable '%s' has only %zu elements, requested %zu",
211 vp->v_key, vp->v_count, idx);
212 return NULL;
213 }
214
215 el = vp->v_elements;
216 while (idx--) {
217 el = el->e_next;
218 }
219
220 if (vp->v_type == NPFVAR_VAR_ID)
221 return npfvar_get_data1(npfvar_lookup(el->e_data), type, 0,
222 level + 1);
223 return el->e_data;
224 }
225
226 void *
227 npfvar_get_data(const npfvar_t *vp, int type, size_t idx)
228 {
229 return npfvar_get_data1(vp, type, idx, 0);
230 }
231