xref: /freebsd/sbin/ipf/libipf/addipopt.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
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 int
14 addipopt(char *op, struct ipopt_names *io, int len, char *class)
15 {
16 	int olen = len;
17 	struct in_addr ipadr;
18 	u_short val;
19 	u_char lvl;
20 	char *s;
21 
22 	if ((len + io->on_siz) > 48) {
23 		fprintf(stderr, "options too long\n");
24 		return (0);
25 	}
26 	len += io->on_siz;
27 	*op++ = io->on_value;
28 	if (io->on_siz > 1) {
29 		s = op;
30 		*op++ = io->on_siz;
31 		*op++ = IPOPT_MINOFF;
32 
33 		if (class) {
34 			switch (io->on_value)
35 			{
36 			case IPOPT_SECURITY :
37 				lvl = seclevel(class);
38 				*(op - 1) = lvl;
39 				break;
40 			case IPOPT_RR :
41 			case IPOPT_TS :
42 				s[IPOPT_OLEN] = IPOPT_MINOFF - 1 + 4;
43 				break;
44 			case IPOPT_LSRR :
45 			case IPOPT_SSRR :
46 				ipadr.s_addr = inet_addr(class);
47 				s[IPOPT_OLEN] = IPOPT_MINOFF - 1 + 4;
48 				bcopy((char *)&ipadr, op, sizeof(ipadr));
49 				break;
50 			case IPOPT_SATID :
51 				val = atoi(class);
52 				bcopy((char *)&val, op, 2);
53 				break;
54 			}
55 		}
56 	}
57 	if (opts & OPT_DEBUG)
58 		fprintf(stderr, "bo: %s %d %#x: %d\n",
59 			io->on_name, io->on_value, io->on_bit, len);
60 	return (len - olen);
61 }
62