xref: /freebsd/sbin/ipf/libipf/buildopts.c (revision 911f0260390e18cf85f3dbf2c719b593efdc1e3c)
1 /*	$FreeBSD$	*/
2 
3 /*
4  * Copyright (C) 2012 by Darren Reed.
5  *
6  * See the IPFILTER.LICENCE file for details on licencing.
7  *
8  * $Id$
9  */
10 
11 #include "ipf.h"
12 
13 
14 u_32_t
15 buildopts(char *cp, char *op, int len)
16 {
17 	struct ipopt_names *io;
18 	u_32_t msk = 0;
19 	char *s, *t;
20 	int inc;
21 
22 	for (s = strtok(cp, ","); s; s = strtok(NULL, ",")) {
23 		if ((t = strchr(s, '=')))
24 			*t++ = '\0';
25 		else
26 			t = "";
27 		for (io = ionames; io->on_name; io++) {
28 			if (strcasecmp(s, io->on_name) || (msk & io->on_bit))
29 				continue;
30 			if ((inc = addipopt(op, io, len, t))) {
31 				op += inc;
32 				len += inc;
33 			}
34 			msk |= io->on_bit;
35 			break;
36 		}
37 		if (!io->on_name) {
38 			fprintf(stderr, "unknown IP option name %s\n", s);
39 			return (0);
40 		}
41 	}
42 	while ((len & 3) != 3) {
43 		*op++ = IPOPT_NOP;
44 		len++;
45 	}
46 	*op++ = IPOPT_EOL;
47 	len++;
48 	return (len);
49 }
50