xref: /netbsd-src/external/bsd/ipf/dist/lib/load_dstlistnode.c (revision 13885a665959c62f13a82b3caedf986eaa17aa31)
1 /*	$NetBSD: load_dstlistnode.c,v 1.2 2012/07/22 14:27:36 darrenr Exp $	*/
2 
3 /*
4  * Copyright (C) 2012 by Darren Reed.
5  *
6  * See the IPFILTER.LICENCE file for details on licencing.
7  *
8  * Id: load_dstlistnode.c,v 1.1.1.2 2012/07/22 13:44:39 darrenr Exp $
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
load_dstlistnode(role,name,node,iocfunc)19 load_dstlistnode(role, name, node, iocfunc)
20 	int role;
21 	char *name;
22 	ipf_dstnode_t *node;
23 	ioctlfunc_t iocfunc;
24 {
25 	iplookupop_t op;
26 	frdest_t *dst;
27 	char *what;
28 	int err;
29 
30 	if (pool_open() == -1)
31 		return -1;
32 
33 	dst = calloc(1, sizeof(*dst) + node->ipfd_dest.fd_name);
34 	if (dst == NULL)
35 		return -1;
36 
37 	op.iplo_unit = role;
38 	op.iplo_type = IPLT_DSTLIST;
39 	op.iplo_arg = 0;
40 	op.iplo_struct = dst;
41 	op.iplo_size = sizeof(*dst);
42 	if (node->ipfd_dest.fd_name >= 0)
43 		op.iplo_size += node->ipfd_dest.fd_name;
44 	(void) strncpy(op.iplo_name, name, sizeof(op.iplo_name));
45 
46 	dst->fd_addr = node->ipfd_dest.fd_addr;
47 	dst->fd_type = node->ipfd_dest.fd_type;
48 	dst->fd_name = node->ipfd_dest.fd_name;
49 	if (node->ipfd_dest.fd_name >= 0)
50 		bcopy(node->ipfd_names, (char *)dst + sizeof(*dst),
51 		      node->ipfd_dest.fd_name);
52 
53 	if ((opts & OPT_REMOVE) == 0) {
54 		what = "add";
55 		err = pool_ioctl(iocfunc, SIOCLOOKUPADDNODE, &op);
56 	} else {
57 		what = "delete";
58 		err = pool_ioctl(iocfunc, SIOCLOOKUPDELNODE, &op);
59 	}
60 	free(dst);
61 
62 	if (err != 0) {
63 		if ((opts & OPT_DONOTHING) == 0) {
64 			char msg[80];
65 
66 			(void) sprintf(msg, "%s lookup node", what);
67 			return ipf_perror_fd(pool_fd(), iocfunc, msg);
68 		}
69 	}
70 
71 	return 0;
72 }
73