npfext_normalize.c revision 1.1
11.1Schristos/*	$NetBSD: npfext_normalize.c,v 1.1 2013/03/10 21:49:26 christos Exp $	*/
21.1Schristos
31.1Schristos/*-
41.1Schristos * Copyright (c) 2012 The NetBSD Foundation, Inc.
51.1Schristos * All rights reserved.
61.1Schristos *
71.1Schristos * Redistribution and use in source and binary forms, with or without
81.1Schristos * modification, are permitted provided that the following conditions
91.1Schristos * are met:
101.1Schristos * 1. Redistributions of source code must retain the above copyright
111.1Schristos *    notice, this list of conditions and the following disclaimer.
121.1Schristos * 2. Redistributions in binary form must reproduce the above copyright
131.1Schristos *    notice, this list of conditions and the following disclaimer in the
141.1Schristos *    documentation and/or other materials provided with the distribution.
151.1Schristos *
161.1Schristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
171.1Schristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
181.1Schristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
191.1Schristos * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
201.1Schristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
211.1Schristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
221.1Schristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
231.1Schristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
241.1Schristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
251.1Schristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
261.1Schristos * POSSIBILITY OF SUCH DAMAGE.
271.1Schristos */
281.1Schristos
291.1Schristos#include <sys/cdefs.h>
301.1Schristos__RCSID("$NetBSD: npfext_normalize.c,v 1.1 2013/03/10 21:49:26 christos Exp $");
311.1Schristos
321.1Schristos#include <stdlib.h>
331.1Schristos#include <string.h>
341.1Schristos#include <assert.h>
351.1Schristos#include <errno.h>
361.1Schristos
371.1Schristos#include <npf.h>
381.1Schristos
391.1Schristosint		npfext_normalize_init(void);
401.1Schristosnl_ext_t *	npfext_normalize_construct(const char *);
411.1Schristosint		npfext_normalize_param(nl_ext_t *, const char *, const char *);
421.1Schristos
431.1Schristosint
441.1Schristosnpfext_normalize_init(void)
451.1Schristos{
461.1Schristos	/* Nothing to initialize. */
471.1Schristos	return 0;
481.1Schristos}
491.1Schristos
501.1Schristosnl_ext_t *
511.1Schristosnpfext_normalize_construct(const char *name)
521.1Schristos{
531.1Schristos	assert(strcmp(name, "normalize") == 0);
541.1Schristos	return npf_ext_construct(name);
551.1Schristos}
561.1Schristos
571.1Schristosint
581.1Schristosnpfext_normalize_param(nl_ext_t *ext, const char *param, const char *val)
591.1Schristos{
601.1Schristos	enum ptype {
611.1Schristos		PARAM_BOOL,
621.1Schristos		PARAM_U32
631.1Schristos	};
641.1Schristos	static const struct param {
651.1Schristos		const char *	name;
661.1Schristos		enum ptype	type;
671.1Schristos		bool		reqval;
681.1Schristos	} params[] = {
691.1Schristos		{ "random-id",	PARAM_BOOL,	false	},
701.1Schristos		{ "no-df",	PARAM_BOOL,	false	},
711.1Schristos		{ "min-ttl",	PARAM_U32,	true	},
721.1Schristos		{ "max-mss",	PARAM_U32,	true	},
731.1Schristos	};
741.1Schristos
751.1Schristos	for (unsigned i = 0; i < __arraycount(params); i++) {
761.1Schristos		const char *name = params[i].name;
771.1Schristos
781.1Schristos		if (strcmp(name, param) != 0) {
791.1Schristos			continue;
801.1Schristos		}
811.1Schristos		if (val == NULL && params[i].reqval) {
821.1Schristos			return EINVAL;
831.1Schristos		}
841.1Schristos
851.1Schristos		switch (params[i].type) {
861.1Schristos		case PARAM_BOOL:
871.1Schristos			npf_ext_param_bool(ext, name, true);
881.1Schristos			break;
891.1Schristos		case PARAM_U32:
901.1Schristos			npf_ext_param_u32(ext, name, atol(val));
911.1Schristos			break;
921.1Schristos		default:
931.1Schristos			assert(false);
941.1Schristos		}
951.1Schristos		return 0;
961.1Schristos	}
971.1Schristos
981.1Schristos	/* Invalid parameter, if not found. */
991.1Schristos	return EINVAL;
1001.1Schristos}
101