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