xref: /netbsd-src/external/bsd/ipf/dist/lib/load_poolnode.c (revision 13885a665959c62f13a82b3caedf986eaa17aa31)
1*13885a66Sdarrenr /*	$NetBSD: load_poolnode.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: load_poolnode.c,v 1.1.1.2 2012/07/22 13:44:39 darrenr Exp $
9bc4097aaSchristos  */
10bc4097aaSchristos 
11bc4097aaSchristos #include <fcntl.h>
12bc4097aaSchristos #include <sys/ioctl.h>
13bc4097aaSchristos #include "ipf.h"
14bc4097aaSchristos #include "netinet/ip_lookup.h"
15bc4097aaSchristos #include "netinet/ip_pool.h"
16bc4097aaSchristos 
17bc4097aaSchristos 
18bc4097aaSchristos int
load_poolnode(role,name,node,ttl,iocfunc)19bc4097aaSchristos load_poolnode(role, name, node, ttl, iocfunc)
20bc4097aaSchristos 	int role;
21bc4097aaSchristos 	char *name;
22bc4097aaSchristos 	ip_pool_node_t *node;
23bc4097aaSchristos 	int ttl;
24bc4097aaSchristos 	ioctlfunc_t iocfunc;
25bc4097aaSchristos {
26bc4097aaSchristos 	ip_pool_node_t pn;
27bc4097aaSchristos 	iplookupop_t op;
28c9d5dc6cSdarrenr 	char *what;
29bc4097aaSchristos 	int err;
30bc4097aaSchristos 
31bc4097aaSchristos 	if (pool_open() == -1)
32bc4097aaSchristos 		return -1;
33bc4097aaSchristos 
34bc4097aaSchristos 	op.iplo_unit = role;
35bc4097aaSchristos 	op.iplo_type = IPLT_POOL;
36bc4097aaSchristos 	op.iplo_arg = 0;
37bc4097aaSchristos 	op.iplo_struct = &pn;
38bc4097aaSchristos 	op.iplo_size = sizeof(pn);
39bc4097aaSchristos 	strncpy(op.iplo_name, name, sizeof(op.iplo_name));
40bc4097aaSchristos 
41bc4097aaSchristos 	bzero((char *)&pn, sizeof(pn));
42bc4097aaSchristos 	bcopy((char *)&node->ipn_addr, (char *)&pn.ipn_addr,
43bc4097aaSchristos 	      sizeof(pn.ipn_addr));
44bc4097aaSchristos 	bcopy((char *)&node->ipn_mask, (char *)&pn.ipn_mask,
45bc4097aaSchristos 	      sizeof(pn.ipn_mask));
46bc4097aaSchristos 	pn.ipn_info = node->ipn_info;
47bc4097aaSchristos 	pn.ipn_die = ttl;
48bc4097aaSchristos 	strncpy(pn.ipn_name, node->ipn_name, sizeof(pn.ipn_name));
49bc4097aaSchristos 
50c9d5dc6cSdarrenr 	if ((opts & OPT_REMOVE) == 0) {
51c9d5dc6cSdarrenr 		what = "add";
52bc4097aaSchristos 		err = pool_ioctl(iocfunc, SIOCLOOKUPADDNODE, &op);
53c9d5dc6cSdarrenr 	} else {
54c9d5dc6cSdarrenr 		what = "delete";
55bc4097aaSchristos 		err = pool_ioctl(iocfunc, SIOCLOOKUPDELNODE, &op);
56c9d5dc6cSdarrenr 	}
57bc4097aaSchristos 
58bc4097aaSchristos 	if (err != 0) {
59bc4097aaSchristos 		if ((opts & OPT_DONOTHING) == 0) {
60c9d5dc6cSdarrenr 			char msg[80];
61c9d5dc6cSdarrenr 
62c9d5dc6cSdarrenr 			sprintf(msg, "%s pool node(%s/", what,
63bc4097aaSchristos 				inet_ntoa(pn.ipn_addr.adf_addr.in4));
64c9d5dc6cSdarrenr 			strcat(msg, inet_ntoa(pn.ipn_mask.adf_addr.in4));
65c9d5dc6cSdarrenr 			return ipf_perror_fd(pool_fd(), iocfunc, msg);
66bc4097aaSchristos 		}
67bc4097aaSchristos 	}
68bc4097aaSchristos 
69bc4097aaSchristos 	return 0;
70bc4097aaSchristos }
71