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