]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/ipfilter/lib/load_hash.c
This commit was generated by cvs2svn to compensate for changes in r170242,
[FreeBSD/FreeBSD.git] / contrib / ipfilter / lib / load_hash.c
1 /*      $FreeBSD$       */
2
3 /*
4  * Copyright (C) 2002 by Darren Reed.
5  *
6  * See the IPFILTER.LICENCE file for details on licencing.
7  *
8  * $Id: load_hash.c,v 1.11.2.3 2005/11/13 15:41:12 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 static int hashfd = -1;
18
19
20 int load_hash(iphp, list, iocfunc)
21 iphtable_t *iphp;
22 iphtent_t *list;
23 ioctlfunc_t iocfunc;
24 {
25         iplookupop_t op;
26         iphtable_t iph;
27         iphtent_t *a;
28         size_t size;
29         int n;
30
31         if ((hashfd == -1) && ((opts & OPT_DONOTHING) == 0))
32                 hashfd = open(IPLOOKUP_NAME, O_RDWR);
33         if ((hashfd == -1) && ((opts & OPT_DONOTHING) == 0))
34                 return -1;
35
36         for (n = 0, a = list; a != NULL; a = a->ipe_next)
37                 n++;
38
39         op.iplo_arg = 0;
40         op.iplo_type = IPLT_HASH;
41         op.iplo_unit = iphp->iph_unit;
42         strncpy(op.iplo_name, iphp->iph_name, sizeof(op.iplo_name));
43         if (*op.iplo_name == '\0')
44                 op.iplo_arg = IPHASH_ANON;
45         op.iplo_size = sizeof(iph);
46         op.iplo_struct = &iph;
47         iph.iph_unit = iphp->iph_unit;
48         iph.iph_type = iphp->iph_type;
49         strncpy(iph.iph_name, iphp->iph_name, sizeof(iph.iph_name));
50         iph.iph_flags = iphp->iph_flags;
51         if (n <= 0)
52                 n = 1;
53         if (iphp->iph_size == 0)
54                 size = n * 2 - 1;
55         else
56                 size = iphp->iph_size;
57         if ((list == NULL) && (size == 1)) {
58                 fprintf(stderr,
59                         "WARNING: empty hash table %s, recommend setting %s\n",
60                         iphp->iph_name, "size to match expected use");
61         }
62         iph.iph_size = size;
63         iph.iph_seed = iphp->iph_seed;
64         iph.iph_table = NULL;
65         iph.iph_ref = 0;
66
67         if ((opts & OPT_REMOVE) == 0) {
68                 if ((*iocfunc)(hashfd, SIOCLOOKUPADDTABLE, &op))
69                         if ((opts & OPT_DONOTHING) == 0) {
70                                 perror("load_hash:SIOCLOOKUPADDTABLE");
71                                 return -1;
72                         }
73         }
74
75         strncpy(iph.iph_name, op.iplo_name, sizeof(op.iplo_name));
76         strncpy(iphp->iph_name, op.iplo_name, sizeof(op.iplo_name));
77
78         if (opts & OPT_VERBOSE) {
79                 for (a = list; a != NULL; a = a->ipe_next) {
80                         a->ipe_addr.in4_addr = ntohl(a->ipe_addr.in4_addr);
81                         a->ipe_mask.in4_addr = ntohl(a->ipe_mask.in4_addr);
82                 }
83                 iph.iph_table = calloc(size, sizeof(*iph.iph_table));
84                 if (iph.iph_table == NULL) {
85                         perror("calloc(size, sizeof(*iph.iph_table))");
86                         return -1;
87                 }
88                 iph.iph_table[0] = list;
89                 printhash(&iph, bcopywrap, iph.iph_name, opts);
90                 free(iph.iph_table);
91
92                 for (a = list; a != NULL; a = a->ipe_next) {
93                         a->ipe_addr.in4_addr = htonl(a->ipe_addr.in4_addr);
94                         a->ipe_mask.in4_addr = htonl(a->ipe_mask.in4_addr);
95                 }
96         }
97
98         if (opts & OPT_DEBUG)
99                 printf("Hash %s:\n", iph.iph_name);
100
101         for (a = list; a != NULL; a = a->ipe_next)
102                 load_hashnode(iphp->iph_unit, iph.iph_name, a, iocfunc);
103
104         if ((opts & OPT_REMOVE) != 0) {
105                 if ((*iocfunc)(hashfd, SIOCLOOKUPDELTABLE, &op))
106                         if ((opts & OPT_DONOTHING) == 0) {
107                                 perror("load_hash:SIOCLOOKUPDELTABLE");
108                                 return -1;
109                         }
110         }
111         return 0;
112 }