Home | History | Annotate | Line # | Download | only in npfctl
npf_var.c revision 1.9.2.1
      1  1.9.2.1  pgoyette /*	$NetBSD: npf_var.c,v 1.9.2.1 2017/03/20 06:58:08 pgoyette 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.9.2.1  pgoyette __RCSID("$NetBSD: npf_var.c,v 1.9.2.1 2017/03/20 06:58:08 pgoyette 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.4  christos 	int		e_type;
     45      1.1     rmind 	struct npf_element *e_next;
     46      1.1     rmind } npf_element_t;
     47      1.1     rmind 
     48      1.1     rmind struct npfvar {
     49      1.1     rmind 	char *		v_key;
     50      1.1     rmind 	npf_element_t *	v_elements;
     51      1.1     rmind 	npf_element_t *	v_last;
     52      1.1     rmind 	int		v_type;
     53      1.1     rmind 	size_t		v_count;
     54      1.1     rmind 	void *		v_next;
     55      1.1     rmind };
     56      1.1     rmind 
     57      1.1     rmind static npfvar_t *	var_list = NULL;
     58      1.2  christos static size_t		var_num = 0;
     59      1.1     rmind 
     60      1.1     rmind npfvar_t *
     61      1.8     rmind npfvar_create(void)
     62      1.1     rmind {
     63      1.7     rmind 	npfvar_t *vp = ecalloc(1, sizeof(*vp));
     64      1.5     rmind 	var_num++;
     65      1.1     rmind 	return vp;
     66      1.1     rmind }
     67      1.1     rmind 
     68      1.1     rmind npfvar_t *
     69      1.1     rmind npfvar_lookup(const char *key)
     70      1.1     rmind {
     71      1.1     rmind 	for (npfvar_t *it = var_list; it != NULL; it = it->v_next)
     72      1.1     rmind 		if (strcmp(it->v_key, key) == 0)
     73      1.1     rmind 			return it;
     74      1.1     rmind 	return NULL;
     75      1.1     rmind }
     76      1.1     rmind 
     77      1.1     rmind const char *
     78      1.1     rmind npfvar_type(size_t t)
     79      1.1     rmind {
     80      1.1     rmind 	if (t >= __arraycount(npfvar_types)) {
     81      1.1     rmind 		return "unknown";
     82      1.1     rmind 	}
     83      1.1     rmind 	return npfvar_types[t];
     84      1.1     rmind }
     85      1.1     rmind 
     86      1.1     rmind void
     87      1.8     rmind npfvar_add(npfvar_t *vp, const char *name)
     88      1.1     rmind {
     89      1.8     rmind 	vp->v_key = estrdup(name);
     90      1.1     rmind 	vp->v_next = var_list;
     91      1.1     rmind 	var_list = vp;
     92      1.1     rmind }
     93      1.1     rmind 
     94      1.1     rmind npfvar_t *
     95      1.8     rmind npfvar_create_element(int type, const void *data, size_t len)
     96      1.8     rmind {
     97      1.8     rmind 	npfvar_t *vp = npfvar_create();
     98      1.8     rmind 	return npfvar_add_element(vp, type, data, len);
     99      1.8     rmind }
    100      1.8     rmind 
    101      1.8     rmind npfvar_t *
    102      1.8     rmind npfvar_create_from_string(int type, const char *string)
    103      1.8     rmind {
    104      1.8     rmind 	return npfvar_create_element(type, string, strlen(string) + 1);
    105      1.8     rmind }
    106      1.8     rmind 
    107      1.8     rmind npfvar_t *
    108      1.1     rmind npfvar_add_element(npfvar_t *vp, int type, const void *data, size_t len)
    109      1.1     rmind {
    110      1.1     rmind 	npf_element_t *el;
    111      1.1     rmind 
    112      1.1     rmind 	if (vp->v_count == 0) {
    113      1.1     rmind 		vp->v_type = type;
    114      1.1     rmind 	} else if (NPFVAR_TYPE(vp->v_type) != NPFVAR_TYPE(type)) {
    115      1.1     rmind 		yyerror("element type '%s' does not match variable type '%s'",
    116      1.1     rmind 		    npfvar_type(type), npfvar_type(vp->v_type));
    117      1.1     rmind 		return NULL;
    118      1.1     rmind 	}
    119      1.1     rmind 	vp->v_count++;
    120      1.7     rmind 	el = ecalloc(1, sizeof(*el));
    121      1.7     rmind 	el->e_data = ecalloc(1, len);
    122      1.4  christos 	el->e_type = type;
    123      1.1     rmind 	memcpy(el->e_data, data, len);
    124      1.1     rmind 
    125      1.1     rmind 	/* Preserve order of insertion. */
    126      1.1     rmind 	if (vp->v_elements == NULL) {
    127      1.1     rmind 		vp->v_elements = el;
    128      1.1     rmind 	} else {
    129      1.1     rmind 		vp->v_last->e_next = el;
    130      1.1     rmind 	}
    131      1.1     rmind 	vp->v_last = el;
    132      1.1     rmind 	return vp;
    133      1.1     rmind }
    134      1.1     rmind 
    135      1.1     rmind npfvar_t *
    136      1.1     rmind npfvar_add_elements(npfvar_t *vp, npfvar_t *vp2)
    137      1.1     rmind {
    138      1.1     rmind 	if (vp2 == NULL)
    139      1.1     rmind 		return vp;
    140      1.1     rmind 	if (vp == NULL)
    141      1.1     rmind 		return vp2;
    142      1.1     rmind 
    143      1.1     rmind 	if (vp->v_elements == NULL) {
    144      1.1     rmind 		if (vp2->v_elements) {
    145      1.1     rmind 			vp->v_type = vp2->v_type;
    146      1.1     rmind 			vp->v_elements = vp2->v_elements;
    147      1.1     rmind 		}
    148      1.1     rmind 	} else if (vp2->v_elements) {
    149      1.1     rmind 		if (NPFVAR_TYPE(vp->v_type) != NPFVAR_TYPE(vp2->v_type)) {
    150      1.1     rmind 			yyerror("variable '%s' type '%s' does not match "
    151      1.1     rmind 			    "variable '%s' type '%s'", vp->v_key,
    152      1.1     rmind 			    npfvar_type(vp->v_type),
    153      1.1     rmind 			    vp2->v_key, npfvar_type(vp2->v_type));
    154      1.1     rmind 			return NULL;
    155      1.1     rmind 		}
    156      1.1     rmind 		vp->v_last->e_next = vp2->v_elements;
    157      1.1     rmind 	}
    158      1.1     rmind 	if (vp2->v_elements) {
    159      1.1     rmind 		vp->v_last = vp2->v_last;
    160      1.1     rmind 		vp->v_count += vp2->v_count;
    161      1.1     rmind 		vp2->v_elements = NULL;
    162      1.1     rmind 		vp2->v_count = 0;
    163      1.1     rmind 		vp2->v_last = NULL;
    164      1.1     rmind 	}
    165      1.1     rmind 	npfvar_destroy(vp2);
    166      1.1     rmind 	return vp;
    167      1.1     rmind }
    168      1.1     rmind 
    169      1.1     rmind static void
    170      1.1     rmind npfvar_free_elements(npf_element_t *el)
    171      1.1     rmind {
    172      1.1     rmind 	if (el == NULL)
    173      1.1     rmind 		return;
    174      1.1     rmind 	npfvar_free_elements(el->e_next);
    175      1.1     rmind 	free(el->e_data);
    176      1.1     rmind 	free(el);
    177      1.1     rmind }
    178      1.1     rmind 
    179      1.1     rmind void
    180      1.1     rmind npfvar_destroy(npfvar_t *vp)
    181      1.1     rmind {
    182      1.1     rmind 	npfvar_free_elements(vp->v_elements);
    183      1.1     rmind 	free(vp->v_key);
    184      1.1     rmind 	free(vp);
    185      1.5     rmind 	var_num--;
    186      1.1     rmind }
    187      1.1     rmind 
    188      1.1     rmind char *
    189      1.1     rmind npfvar_expand_string(const npfvar_t *vp)
    190      1.1     rmind {
    191  1.9.2.1  pgoyette 	if (npfvar_get_count(vp) != 1)
    192  1.9.2.1  pgoyette 		yyerror("variable '%s' type '%s' has %zu elements", vp->v_key,
    193  1.9.2.1  pgoyette 		    npfvar_type(vp->v_type), npfvar_get_count(vp));
    194  1.9.2.1  pgoyette 
    195      1.1     rmind 	return npfvar_get_data(vp, NPFVAR_STRING, 0);
    196      1.1     rmind }
    197      1.1     rmind 
    198      1.1     rmind size_t
    199      1.1     rmind npfvar_get_count(const npfvar_t *vp)
    200      1.1     rmind {
    201      1.1     rmind 	return vp ? vp->v_count : 0;
    202      1.1     rmind }
    203      1.1     rmind 
    204      1.2  christos static void *
    205      1.2  christos npfvar_get_data1(const npfvar_t *vp, int type, size_t idx, size_t level)
    206      1.1     rmind {
    207      1.1     rmind 	npf_element_t *el;
    208      1.1     rmind 
    209      1.2  christos 	if (level >= var_num) {
    210      1.2  christos 		yyerror("variable loop for '%s'", vp->v_key);
    211      1.2  christos 		return NULL;
    212      1.2  christos 	}
    213      1.2  christos 
    214      1.1     rmind 	if (vp == NULL)
    215      1.1     rmind 		return NULL;
    216      1.1     rmind 
    217      1.1     rmind 	if (NPFVAR_TYPE(vp->v_type) != NPFVAR_TYPE(type)) {
    218      1.1     rmind 		yyerror("variable '%s' is of type '%s' not '%s'", vp->v_key,
    219      1.1     rmind 		    npfvar_type(vp->v_type), npfvar_type(type));
    220      1.1     rmind 		return NULL;
    221      1.1     rmind 	}
    222      1.1     rmind 
    223      1.1     rmind 	if (vp->v_count <= idx) {
    224      1.1     rmind 		yyerror("variable '%s' has only %zu elements, requested %zu",
    225      1.1     rmind 		    vp->v_key, vp->v_count, idx);
    226      1.1     rmind 		return NULL;
    227      1.1     rmind 	}
    228      1.1     rmind 
    229      1.1     rmind 	el = vp->v_elements;
    230      1.1     rmind 	while (idx--) {
    231      1.1     rmind 		el = el->e_next;
    232      1.1     rmind 	}
    233      1.2  christos 
    234      1.3     rmind 	if (vp->v_type == NPFVAR_VAR_ID) {
    235      1.3     rmind 		npfvar_t *rvp = npfvar_lookup(el->e_data);
    236      1.3     rmind 		return npfvar_get_data1(rvp, type, 0, level + 1);
    237      1.3     rmind 	}
    238      1.1     rmind 	return el->e_data;
    239      1.1     rmind }
    240      1.2  christos 
    241      1.4  christos static int
    242      1.4  christos npfvar_get_type1(const npfvar_t *vp, size_t idx, size_t level)
    243      1.4  christos {
    244      1.4  christos 	npf_element_t *el;
    245      1.4  christos 
    246      1.9     rmind 	if (vp == NULL)
    247      1.9     rmind 		return -1;
    248      1.9     rmind 
    249      1.4  christos 	if (level >= var_num) {
    250      1.4  christos 		yyerror("variable loop for '%s'", vp->v_key);
    251      1.4  christos 		return -1;
    252      1.4  christos 	}
    253      1.4  christos 
    254      1.4  christos 	if (vp->v_count <= idx) {
    255      1.4  christos 		yyerror("variable '%s' has only %zu elements, requested %zu",
    256      1.4  christos 		    vp->v_key, vp->v_count, idx);
    257      1.4  christos 		return -1;
    258      1.4  christos 	}
    259      1.4  christos 
    260      1.4  christos 	el = vp->v_elements;
    261      1.4  christos 	while (idx--) {
    262      1.4  christos 		el = el->e_next;
    263      1.4  christos 	}
    264      1.4  christos 
    265      1.4  christos 	if (vp->v_type == NPFVAR_VAR_ID) {
    266      1.4  christos 		npfvar_t *rvp = npfvar_lookup(el->e_data);
    267      1.4  christos 		return npfvar_get_type1(rvp, 0, level + 1);
    268      1.4  christos 	}
    269      1.4  christos 	return el->e_type;
    270      1.4  christos }
    271      1.4  christos 
    272      1.4  christos int
    273      1.4  christos npfvar_get_type(const npfvar_t *vp, size_t idx)
    274      1.4  christos {
    275      1.4  christos 	return npfvar_get_type1(vp, idx, 0);
    276      1.4  christos }
    277      1.4  christos 
    278      1.2  christos void *
    279      1.2  christos npfvar_get_data(const npfvar_t *vp, int type, size_t idx)
    280      1.2  christos {
    281      1.2  christos 	return npfvar_get_data1(vp, type, idx, 0);
    282      1.2  christos }
    283