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