xref: /freebsd/sbin/ipf/libipf/optname.c (revision c03c5b1c)
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 optname(char ***cp, u_short *sp, int linenum)
16 {
17 	struct ipopt_names *io, *so;
18 	u_long msk = 0;
19 	u_short smsk = 0;
20 	char *s;
21 	int sec = 0;
22 
23 	for (s = strtok(**cp, ","); s; s = strtok(NULL, ",")) {
24 		for (io = ionames; io->on_name; io++)
25 			if (!strcasecmp(s, io->on_name)) {
26 				msk |= io->on_bit;
27 				break;
28 			}
29 		if (!io->on_name) {
30 			fprintf(stderr, "%d: unknown IP option name %s\n",
31 				linenum, s);
32 			return (0);
33 		}
34 		if (!strcasecmp(s, "sec-class"))
35 			sec = 1;
36 	}
37 
38 	if (sec && !*(*cp + 1)) {
39 		fprintf(stderr, "%d: missing security level after sec-class\n",
40 			linenum);
41 		return (0);
42 	}
43 
44 	if (sec) {
45 		(*cp)++;
46 		for (s = strtok(**cp, ","); s; s = strtok(NULL, ",")) {
47 			for (so = secclass; so->on_name; so++)
48 				if (!strcasecmp(s, so->on_name)) {
49 					smsk |= so->on_bit;
50 					break;
51 				}
52 			if (!so->on_name) {
53 				fprintf(stderr,
54 					"%d: no such security level: %s\n",
55 					linenum, s);
56 				return (0);
57 			}
58 		}
59 		if (smsk)
60 			*sp = smsk;
61 	}
62 	return (msk);
63 }
64