xref: /netbsd-src/external/bsd/ipf/dist/lib/load_poolnode.c (revision bc4097aacfdd9307c19b7947c13c6ad6982527a9)
1 /*	$NetBSD: load_poolnode.c,v 1.1.1.1 2012/03/23 21:20:08 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2011 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 	int err;
29 
30 	if (pool_open() == -1)
31 		return -1;
32 
33 	op.iplo_unit = role;
34 	op.iplo_type = IPLT_POOL;
35 	op.iplo_arg = 0;
36 	op.iplo_struct = &pn;
37 	op.iplo_size = sizeof(pn);
38 	strncpy(op.iplo_name, name, sizeof(op.iplo_name));
39 
40 	bzero((char *)&pn, sizeof(pn));
41 	bcopy((char *)&node->ipn_addr, (char *)&pn.ipn_addr,
42 	      sizeof(pn.ipn_addr));
43 	bcopy((char *)&node->ipn_mask, (char *)&pn.ipn_mask,
44 	      sizeof(pn.ipn_mask));
45 	pn.ipn_info = node->ipn_info;
46 	pn.ipn_die = ttl;
47 	strncpy(pn.ipn_name, node->ipn_name, sizeof(pn.ipn_name));
48 
49 	if ((opts & OPT_REMOVE) == 0)
50 		err = pool_ioctl(iocfunc, SIOCLOOKUPADDNODE, &op);
51 	else
52 		err = pool_ioctl(iocfunc, SIOCLOOKUPDELNODE, &op);
53 
54 	if (err != 0) {
55 		if ((opts & OPT_DONOTHING) == 0) {
56 			fprintf(stderr, "load_loopnode(%s/",
57 				inet_ntoa(pn.ipn_addr.adf_addr.in4));
58 			fprintf(stderr, "%s",
59 				inet_ntoa(pn.ipn_mask.adf_addr.in4));
60 			perror(":SIOCLOOKUP*NODE");
61 			return -1;
62 		}
63 	}
64 
65 	return 0;
66 }
67