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