xref: /freebsd/sbin/ipf/libipf/buildopts.c (revision efeb8bffe34422937c7f8df836afb5b817366d16)
141edb306SCy Schubert /*	$FreeBSD$	*/
241edb306SCy Schubert 
341edb306SCy Schubert /*
441edb306SCy Schubert  * Copyright (C) 2012 by Darren Reed.
541edb306SCy Schubert  *
641edb306SCy Schubert  * See the IPFILTER.LICENCE file for details on licencing.
741edb306SCy Schubert  *
841edb306SCy Schubert  * $Id$
941edb306SCy Schubert  */
1041edb306SCy Schubert 
1141edb306SCy Schubert #include "ipf.h"
1241edb306SCy Schubert 
1341edb306SCy Schubert 
14*efeb8bffSCy Schubert u_32_t
15*efeb8bffSCy Schubert buildopts(char *cp, char *op, int len)
1641edb306SCy Schubert {
1741edb306SCy Schubert 	struct ipopt_names *io;
1841edb306SCy Schubert 	u_32_t msk = 0;
1941edb306SCy Schubert 	char *s, *t;
2041edb306SCy Schubert 	int inc;
2141edb306SCy Schubert 
2241edb306SCy Schubert 	for (s = strtok(cp, ","); s; s = strtok(NULL, ",")) {
2341edb306SCy Schubert 		if ((t = strchr(s, '=')))
2441edb306SCy Schubert 			*t++ = '\0';
2541edb306SCy Schubert 		else
2641edb306SCy Schubert 			t = "";
2741edb306SCy Schubert 		for (io = ionames; io->on_name; io++) {
2841edb306SCy Schubert 			if (strcasecmp(s, io->on_name) || (msk & io->on_bit))
2941edb306SCy Schubert 				continue;
3041edb306SCy Schubert 			if ((inc = addipopt(op, io, len, t))) {
3141edb306SCy Schubert 				op += inc;
3241edb306SCy Schubert 				len += inc;
3341edb306SCy Schubert 			}
3441edb306SCy Schubert 			msk |= io->on_bit;
3541edb306SCy Schubert 			break;
3641edb306SCy Schubert 		}
3741edb306SCy Schubert 		if (!io->on_name) {
3841edb306SCy Schubert 			fprintf(stderr, "unknown IP option name %s\n", s);
3941edb306SCy Schubert 			return 0;
4041edb306SCy Schubert 		}
4141edb306SCy Schubert 	}
4241edb306SCy Schubert 	while ((len & 3) != 3) {
4341edb306SCy Schubert 		*op++ = IPOPT_NOP;
4441edb306SCy Schubert 		len++;
4541edb306SCy Schubert 	}
4641edb306SCy Schubert 	*op++ = IPOPT_EOL;
4741edb306SCy Schubert 	len++;
4841edb306SCy Schubert 	return len;
4941edb306SCy Schubert }
50