xref: /netbsd-src/external/bsd/ipf/dist/lib/load_dstlistnode.c (revision b757af438b42b93f8c6571f026d8b8ef3eaf5fc9)
1 /*	$NetBSD: load_dstlistnode.c,v 1.1.1.1 2012/03/23 21:20:07 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2009 by Darren Reed.
5  *
6  * See the IPFILTER.LICENCE file for details on licencing.
7  *
8  * Id: load_dstlistnode.c,v 1.1.2.1 2012/01/26 05:44:26 darren_r 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
19 load_dstlistnode(role, name, node, ttl, iocfunc)
20 	int role;
21 	char *name;
22 	ipf_dstnode_t *node;
23 	int ttl;
24 	ioctlfunc_t iocfunc;
25 {
26 	iplookupop_t op;
27 	frdest_t *dst;
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) + node->ipfd_dest.fd_name;
42 	strncpy(op.iplo_name, name, sizeof(op.iplo_name));
43 
44 	dst->fd_addr = node->ipfd_dest.fd_addr;
45 	dst->fd_type = node->ipfd_dest.fd_type;
46 	dst->fd_name = node->ipfd_dest.fd_name;
47 	bcopy(node->ipfd_names, (char *)dst + sizeof(*dst),
48 	      node->ipfd_dest.fd_name);
49 
50 	if ((opts & OPT_REMOVE) == 0)
51 		err = pool_ioctl(iocfunc, SIOCLOOKUPADDNODE, &op);
52 	else
53 		err = pool_ioctl(iocfunc, SIOCLOOKUPDELNODE, &op);
54 
55 	if (err != 0) {
56 		if ((opts & OPT_DONOTHING) == 0) {
57 			perror("load_dstlistnode:SIOCLOOKUP*NODE");
58 			free(dst);
59 			return -1;
60 		}
61 	}
62 	free(dst);
63 
64 	return 0;
65 }
66