xref: /netbsd-src/external/bsd/ipf/dist/lib/addipopt.c (revision 13885a665959c62f13a82b3caedf986eaa17aa31)
1*13885a66Sdarrenr /*	$NetBSD: addipopt.c,v 1.2 2012/07/22 14:27:36 darrenr Exp $	*/
2bc4097aaSchristos 
3bc4097aaSchristos /*
4c9d5dc6cSdarrenr  * Copyright (C) 2012 by Darren Reed.
5bc4097aaSchristos  *
6bc4097aaSchristos  * See the IPFILTER.LICENCE file for details on licencing.
7bc4097aaSchristos  *
8*13885a66Sdarrenr  * Id: addipopt.c,v 1.1.1.2 2012/07/22 13:44:38 darrenr Exp $
9bc4097aaSchristos  */
10bc4097aaSchristos 
11bc4097aaSchristos #include "ipf.h"
12bc4097aaSchristos 
13bc4097aaSchristos 
addipopt(op,io,len,class)14bc4097aaSchristos int addipopt(op, io, len, class)
15bc4097aaSchristos 	char *op;
16bc4097aaSchristos 	struct ipopt_names *io;
17bc4097aaSchristos 	int len;
18bc4097aaSchristos 	char *class;
19bc4097aaSchristos {
20bc4097aaSchristos 	int olen = len;
21bc4097aaSchristos 	struct in_addr ipadr;
22bc4097aaSchristos 	u_short val;
23bc4097aaSchristos 	u_char lvl;
24bc4097aaSchristos 	char *s;
25bc4097aaSchristos 
26bc4097aaSchristos 	if ((len + io->on_siz) > 48) {
27bc4097aaSchristos 		fprintf(stderr, "options too long\n");
28bc4097aaSchristos 		return 0;
29bc4097aaSchristos 	}
30bc4097aaSchristos 	len += io->on_siz;
31bc4097aaSchristos 	*op++ = io->on_value;
32bc4097aaSchristos 	if (io->on_siz > 1) {
33bc4097aaSchristos 		s = op;
34bc4097aaSchristos 		*op++ = io->on_siz;
35bc4097aaSchristos 		*op++ = IPOPT_MINOFF;
36bc4097aaSchristos 
37bc4097aaSchristos 		if (class) {
38bc4097aaSchristos 			switch (io->on_value)
39bc4097aaSchristos 			{
40bc4097aaSchristos 			case IPOPT_SECURITY :
41bc4097aaSchristos 				lvl = seclevel(class);
42bc4097aaSchristos 				*(op - 1) = lvl;
43bc4097aaSchristos 				break;
44bc4097aaSchristos 			case IPOPT_RR :
45bc4097aaSchristos 			case IPOPT_TS :
46bc4097aaSchristos 				s[IPOPT_OLEN] = IPOPT_MINOFF - 1 + 4;
47bc4097aaSchristos 				break;
48bc4097aaSchristos 			case IPOPT_LSRR :
49bc4097aaSchristos 			case IPOPT_SSRR :
50bc4097aaSchristos 				ipadr.s_addr = inet_addr(class);
51bc4097aaSchristos 				s[IPOPT_OLEN] = IPOPT_MINOFF - 1 + 4;
52bc4097aaSchristos 				bcopy((char *)&ipadr, op, sizeof(ipadr));
53bc4097aaSchristos 				break;
54bc4097aaSchristos 			case IPOPT_SATID :
55bc4097aaSchristos 				val = atoi(class);
56bc4097aaSchristos 				bcopy((char *)&val, op, 2);
57bc4097aaSchristos 				break;
58bc4097aaSchristos 			}
59bc4097aaSchristos 		}
60bc4097aaSchristos 	}
61bc4097aaSchristos 	if (opts & OPT_DEBUG)
62bc4097aaSchristos 		fprintf(stderr, "bo: %s %d %#x: %d\n",
63bc4097aaSchristos 			io->on_name, io->on_value, io->on_bit, len);
64bc4097aaSchristos 	return len - olen;
65bc4097aaSchristos }
66