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