Sync usage with man page.
[netbsd-mini2440.git] / dist / ipf / lib / load_hash.c
blobbbe9bd7735e19059624d985a8d6dbca34b6392a0
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2002-2005 by Darren Reed.
6 * See the IPFILTER.LICENCE file for details on licencing.
8 * Id: load_hash.c,v 1.11.2.6 2009/01/01 03:48:21 darrenr Exp
9 */
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"
17 static int hashfd = -1;
20 int load_hash(iphp, list, iocfunc)
21 iphtable_t *iphp;
22 iphtent_t *list;
23 ioctlfunc_t iocfunc;
25 iplookupop_t op;
26 iphtable_t iph;
27 iphtent_t *a;
28 size_t size;
29 int n;
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;
36 for (n = 0, a = list; a != NULL; a = a->ipe_next)
37 n++;
39 bzero((char *)&iph, sizeof(iph));
40 op.iplo_arg = 0;
41 op.iplo_type = IPLT_HASH;
42 op.iplo_unit = iphp->iph_unit;
43 strncpy(op.iplo_name, iphp->iph_name, sizeof(op.iplo_name));
44 if (*op.iplo_name == '\0')
45 op.iplo_arg = IPHASH_ANON;
46 op.iplo_size = sizeof(iph);
47 op.iplo_struct = &iph;
48 iph.iph_unit = iphp->iph_unit;
49 iph.iph_type = iphp->iph_type;
50 strncpy(iph.iph_name, iphp->iph_name, sizeof(iph.iph_name));
51 iph.iph_flags = iphp->iph_flags;
52 if (n <= 0)
53 n = 1;
54 if (iphp->iph_size == 0)
55 size = n * 2 - 1;
56 else
57 size = iphp->iph_size;
58 if ((list == NULL) && (size == 1)) {
59 fprintf(stderr,
60 "WARNING: empty hash table %s, recommend setting %s\n",
61 iphp->iph_name, "size to match expected use");
63 iph.iph_size = size;
64 iph.iph_seed = iphp->iph_seed;
65 iph.iph_table = NULL;
66 iph.iph_list = NULL;
67 iph.iph_ref = 0;
69 if ((opts & OPT_REMOVE) == 0) {
70 if ((*iocfunc)(hashfd, SIOCLOOKUPADDTABLE, &op))
71 if ((opts & OPT_DONOTHING) == 0) {
72 perror("load_hash:SIOCLOOKUPADDTABLE");
73 return -1;
77 strncpy(iph.iph_name, op.iplo_name, sizeof(op.iplo_name));
78 strncpy(iphp->iph_name, op.iplo_name, sizeof(op.iplo_name));
80 if (opts & OPT_VERBOSE) {
81 for (a = list; a != NULL; a = a->ipe_next) {
82 a->ipe_addr.in4_addr = ntohl(a->ipe_addr.in4_addr);
83 a->ipe_mask.in4_addr = ntohl(a->ipe_mask.in4_addr);
85 iph.iph_table = calloc(size, sizeof(*iph.iph_table));
86 if (iph.iph_table == NULL) {
87 perror("calloc(size, sizeof(*iph.iph_table))");
88 return -1;
90 iph.iph_list = list;
91 printhash(&iph, bcopywrap, iph.iph_name, opts);
92 free(iph.iph_table);
93 iph.iph_list = NULL;
95 for (a = list; a != NULL; a = a->ipe_next) {
96 a->ipe_addr.in4_addr = htonl(a->ipe_addr.in4_addr);
97 a->ipe_mask.in4_addr = htonl(a->ipe_mask.in4_addr);
101 if (opts & OPT_DEBUG)
102 printf("Hash %s:\n", iph.iph_name);
104 for (a = list; a != NULL; a = a->ipe_next)
105 load_hashnode(iphp->iph_unit, iph.iph_name, a, iocfunc);
107 if ((opts & OPT_REMOVE) != 0) {
108 if ((*iocfunc)(hashfd, SIOCLOOKUPDELTABLE, &op))
109 if ((opts & OPT_DONOTHING) == 0) {
110 perror("load_hash:SIOCLOOKUPDELTABLE");
111 return -1;
114 return 0;