1 /*        $NetBSD: load_hashnode.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_hashnode.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_htable.h"
16 
17 
18 int
load_hashnode(unit,name,node,ttl,iocfunc)19 load_hashnode(unit, name, node, ttl, iocfunc)
20           int unit;
21           char *name;
22           iphtent_t *node;
23           int ttl;
24           ioctlfunc_t iocfunc;
25 {
26           iplookupop_t op;
27           iphtent_t ipe;
28           char *what;
29           int err;
30 
31           if (pool_open() == -1)
32                     return -1;
33 
34           op.iplo_type = IPLT_HASH;
35           op.iplo_unit = unit;
36           op.iplo_arg = 0;
37           op.iplo_size = sizeof(ipe);
38           op.iplo_struct = &ipe;
39           strncpy(op.iplo_name, name, sizeof(op.iplo_name));
40 
41           bzero((char *)&ipe, sizeof(ipe));
42           ipe.ipe_family = node->ipe_family;
43           ipe.ipe_die = ttl;
44           bcopy((char *)&node->ipe_addr, (char *)&ipe.ipe_addr,
45                 sizeof(ipe.ipe_addr));
46           bcopy((char *)&node->ipe_mask, (char *)&ipe.ipe_mask,
47                 sizeof(ipe.ipe_mask));
48           bcopy((char *)&node->ipe_group, (char *)&ipe.ipe_group,
49                 sizeof(ipe.ipe_group));
50 
51           if ((opts & OPT_REMOVE) == 0) {
52                     what = "add";
53                     err = pool_ioctl(iocfunc, SIOCLOOKUPADDNODE, &op);
54           } else {
55                     what = "delete";
56                     err = pool_ioctl(iocfunc, SIOCLOOKUPDELNODE, &op);
57           }
58 
59           if (err != 0)
60                     if (!(opts & OPT_DONOTHING)) {
61                               char msg[80];
62 
63                               sprintf(msg, "%s node from lookup hash table", what);
64                               return ipf_perror_fd(pool_fd(), iocfunc, msg);
65                     }
66           return 0;
67 }
68