xref: /titanic_52/usr/src/cmd/ipf/lib/common/load_hash.c (revision bdfc6d18da790deeec2e0eb09c625902defe2498)
1 /*
2  * Copyright (C) 2002 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  *
6  * $Id: load_hash.c,v 1.10 2003/04/26 04:55:11 darrenr Exp $
7  */
8 
9 #include <fcntl.h>
10 #include <sys/ioctl.h>
11 #include "ipf.h"
12 #if SOLARIS2 >= 10
13 #include "ip_lookup.h"
14 #include "ip_htable.h"
15 #else
16 #include "netinet/ip_lookup.h"
17 #include "netinet/ip_htable.h"
18 #endif
19 
20 static int hashfd = -1;
21 
22 
23 int load_hash(iphp, list, iocfunc)
24 iphtable_t *iphp;
25 iphtent_t *list;
26 ioctlfunc_t iocfunc;
27 {
28 	iplookupop_t op;
29 	iphtable_t iph;
30 	iphtent_t *a;
31 	size_t size;
32 	int n;
33 
34 	if ((hashfd == -1) && ((opts & OPT_DONOTHING) == 0))
35 		hashfd = open(IPLOOKUP_NAME, O_RDWR);
36 	if ((hashfd == -1) && ((opts & OPT_DONOTHING) == 0))
37 		return -1;
38 	if (list == NULL)
39 		return 0;
40 
41 	for (n = 0, a = list; a != NULL; a = a->ipe_next)
42 		n++;
43 
44 	op.iplo_arg = 0;
45 	op.iplo_type = IPLT_HASH;
46 	op.iplo_unit = iphp->iph_unit;
47 	strncpy(op.iplo_name, iphp->iph_name, sizeof(op.iplo_name));
48 	if (*op.iplo_name == '\0')
49 		op.iplo_arg = IPHASH_ANON;
50 	op.iplo_size = sizeof(iph);
51 	op.iplo_struct = &iph;
52 	iph.iph_unit = iphp->iph_unit;
53 	iph.iph_type = iphp->iph_type;
54 	strncpy(iph.iph_name, iphp->iph_name, sizeof(iph.iph_name));
55 	iph.iph_flags = iphp->iph_flags;
56 	if (iphp->iph_size == 0)
57 		size = n * 2 - 1;
58 	else
59 		size = iphp->iph_size;
60 	iph.iph_size = size;
61 	iph.iph_seed = iphp->iph_seed;
62 	iph.iph_table = NULL;
63 	iph.iph_ref = 0;
64 
65 	if ((*iocfunc)(hashfd, SIOCLOOKUPADDTABLE, &op))
66 		if ((opts & OPT_DONOTHING) == 0) {
67 			perror("load_hash:SIOCLOOKUPADDTABLE");
68 			return -1;
69 		}
70 
71 	strncpy(op.iplo_name, iph.iph_name, sizeof(op.iplo_name));
72 	strncpy(iphp->iph_name, iph.iph_name, sizeof(op.iplo_name));
73 
74 	if (opts & OPT_VERBOSE) {
75 		for (a = list; a != NULL; a = a->ipe_next) {
76 			a->ipe_addr.in4_addr = ntohl(a->ipe_addr.in4_addr);
77 			a->ipe_mask.in4_addr = ntohl(a->ipe_mask.in4_addr);
78 		}
79 		iph.iph_table = calloc(size, sizeof(*iph.iph_table));
80 		if (iph.iph_table == NULL) {
81 			perror("calloc(size, sizeof(*iph.iph_table))");
82 			return -1;
83 		}
84 		iph.iph_table[0] = list;
85 		printhash(&iph, bcopywrap, opts);
86 		free(iph.iph_table);
87 
88 		for (a = list; a != NULL; a = a->ipe_next) {
89 			a->ipe_addr.in4_addr = htonl(a->ipe_addr.in4_addr);
90 			a->ipe_mask.in4_addr = htonl(a->ipe_mask.in4_addr);
91 		}
92 	}
93 
94 	if (opts & OPT_DEBUG)
95 		printf("Hash %s:\n", iph.iph_name);
96 
97 	for (a = list; a != NULL; a = a->ipe_next)
98 		load_hashnode(iphp->iph_unit, iph.iph_name, a, iocfunc);
99 
100 	return 0;
101 }
102