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