]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/ipfilter/lib/load_hashnode.c
Import IPFilter 4.1.28
[FreeBSD/FreeBSD.git] / contrib / ipfilter / lib / load_hashnode.c
1 /*
2  * Copyright (C) 2003-2005 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  *
6  * $Id: load_hashnode.c,v 1.2.4.2 2006/06/16 17:21:05 darrenr Exp $
7  */
8
9 #include <fcntl.h>
10 #include <sys/ioctl.h>
11 #include "ipf.h"
12 #include "netinet/ip_lookup.h"
13 #include "netinet/ip_htable.h"
14
15 static int hashfd = -1;
16
17
18 int load_hashnode(unit, name, node, iocfunc)
19 int unit;
20 char *name;
21 iphtent_t *node;
22 ioctlfunc_t iocfunc;
23 {
24         iplookupop_t op;
25         iphtent_t ipe;
26         int err;
27
28         if ((hashfd == -1) && ((opts & OPT_DONOTHING) == 0))
29                 hashfd = open(IPLOOKUP_NAME, O_RDWR);
30         if ((hashfd == -1) && ((opts & OPT_DONOTHING) == 0))
31                 return -1;
32
33         op.iplo_type = IPLT_HASH;
34         op.iplo_unit = unit;
35         op.iplo_arg = 0;
36         op.iplo_size = sizeof(ipe);
37         op.iplo_struct = &ipe;
38         strncpy(op.iplo_name, name, sizeof(op.iplo_name));
39
40         bzero((char *)&ipe, sizeof(ipe));
41         bcopy((char *)&node->ipe_addr, (char *)&ipe.ipe_addr,
42               sizeof(ipe.ipe_addr));
43         bcopy((char *)&node->ipe_mask, (char *)&ipe.ipe_mask,
44               sizeof(ipe.ipe_mask));
45         bcopy((char *)&node->ipe_group, (char *)&ipe.ipe_group,
46               sizeof(ipe.ipe_group));
47
48         if ((opts & OPT_REMOVE) == 0)
49                 err = (*iocfunc)(hashfd, SIOCLOOKUPADDNODE, &op);
50         else
51                 err = (*iocfunc)(hashfd, SIOCLOOKUPDELNODE, &op);
52
53         if (err != 0)
54                 if (!(opts & OPT_DONOTHING)) {
55                         perror("load_hash:SIOCLOOKUP*NODE");
56                         return -1;
57                 }
58         return 0;
59 }