xref: /illumos-gate/usr/src/cmd/ipf/lib/printhash.c (revision f3ac678143127d4c6c1793fadabb5ded04e127b6)
1*f3ac6781SToomas Soome /*
2*f3ac6781SToomas Soome  * Copyright (C) 2002 by Darren Reed.
3*f3ac6781SToomas Soome  *
4*f3ac6781SToomas Soome  * See the IPFILTER.LICENCE file for details on licencing.
5*f3ac6781SToomas Soome  *
6*f3ac6781SToomas Soome  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
7*f3ac6781SToomas Soome  * Use is subject to license terms.
8*f3ac6781SToomas Soome  */
9*f3ac6781SToomas Soome 
10*f3ac6781SToomas Soome #include "ipf.h"
11*f3ac6781SToomas Soome 
12*f3ac6781SToomas Soome #define	PRINTF	(void)printf
13*f3ac6781SToomas Soome #define	FPRINTF	(void)fprintf
14*f3ac6781SToomas Soome 
15*f3ac6781SToomas Soome 
printhash(hp,copyfunc,name,opts)16*f3ac6781SToomas Soome iphtable_t *printhash(hp, copyfunc, name, opts)
17*f3ac6781SToomas Soome iphtable_t *hp;
18*f3ac6781SToomas Soome copyfunc_t copyfunc;
19*f3ac6781SToomas Soome char *name;
20*f3ac6781SToomas Soome int opts;
21*f3ac6781SToomas Soome {
22*f3ac6781SToomas Soome 	iphtent_t *ipep, **table;
23*f3ac6781SToomas Soome 	iphtable_t iph;
24*f3ac6781SToomas Soome 	int i, printed;
25*f3ac6781SToomas Soome 	size_t sz;
26*f3ac6781SToomas Soome 
27*f3ac6781SToomas Soome 	if ((*copyfunc)((char *)hp, (char *)&iph, sizeof(iph)))
28*f3ac6781SToomas Soome 		return NULL;
29*f3ac6781SToomas Soome 
30*f3ac6781SToomas Soome 	if ((name != NULL) && strncmp(name, iph.iph_name, FR_GROUPLEN))
31*f3ac6781SToomas Soome 		return iph.iph_next;
32*f3ac6781SToomas Soome 
33*f3ac6781SToomas Soome 	if ((opts & OPT_DEBUG) == 0) {
34*f3ac6781SToomas Soome 		if ((iph.iph_type & IPHASH_ANON) == IPHASH_ANON)
35*f3ac6781SToomas Soome 			PRINTF("# 'anonymous' table\n");
36*f3ac6781SToomas Soome 		switch (iph.iph_type & ~IPHASH_ANON)
37*f3ac6781SToomas Soome 		{
38*f3ac6781SToomas Soome 		case IPHASH_LOOKUP :
39*f3ac6781SToomas Soome 			PRINTF("table");
40*f3ac6781SToomas Soome 			break;
41*f3ac6781SToomas Soome 		case IPHASH_GROUPMAP :
42*f3ac6781SToomas Soome 			PRINTF("group-map");
43*f3ac6781SToomas Soome 			if (iph.iph_flags & FR_INQUE)
44*f3ac6781SToomas Soome 				PRINTF(" in");
45*f3ac6781SToomas Soome 			else if (iph.iph_flags & FR_OUTQUE)
46*f3ac6781SToomas Soome 				PRINTF(" out");
47*f3ac6781SToomas Soome 			else
48*f3ac6781SToomas Soome 				PRINTF(" ???");
49*f3ac6781SToomas Soome 			break;
50*f3ac6781SToomas Soome 		default :
51*f3ac6781SToomas Soome 			PRINTF("%#x", iph.iph_type);
52*f3ac6781SToomas Soome 			break;
53*f3ac6781SToomas Soome 		}
54*f3ac6781SToomas Soome 		PRINTF(" role = ");
55*f3ac6781SToomas Soome 	} else {
56*f3ac6781SToomas Soome 		PRINTF("Hash Table Number: %s", iph.iph_name);
57*f3ac6781SToomas Soome 		if ((iph.iph_type & IPHASH_ANON) == IPHASH_ANON)
58*f3ac6781SToomas Soome 			PRINTF("(anon)");
59*f3ac6781SToomas Soome 		putchar(' ');
60*f3ac6781SToomas Soome 		PRINTF("Role: ");
61*f3ac6781SToomas Soome 	}
62*f3ac6781SToomas Soome 
63*f3ac6781SToomas Soome 	switch (iph.iph_unit)
64*f3ac6781SToomas Soome 	{
65*f3ac6781SToomas Soome 	case IPL_LOGNAT :
66*f3ac6781SToomas Soome 		PRINTF("nat");
67*f3ac6781SToomas Soome 		break;
68*f3ac6781SToomas Soome 	case IPL_LOGIPF :
69*f3ac6781SToomas Soome 		PRINTF("ipf");
70*f3ac6781SToomas Soome 		break;
71*f3ac6781SToomas Soome 	case IPL_LOGAUTH :
72*f3ac6781SToomas Soome 		PRINTF("auth");
73*f3ac6781SToomas Soome 		break;
74*f3ac6781SToomas Soome 	case IPL_LOGCOUNT :
75*f3ac6781SToomas Soome 		PRINTF("count");
76*f3ac6781SToomas Soome 		break;
77*f3ac6781SToomas Soome 	default :
78*f3ac6781SToomas Soome 		PRINTF("#%d", iph.iph_unit);
79*f3ac6781SToomas Soome 		break;
80*f3ac6781SToomas Soome 	}
81*f3ac6781SToomas Soome 
82*f3ac6781SToomas Soome 	if ((opts & OPT_DEBUG) == 0) {
83*f3ac6781SToomas Soome 		if ((iph.iph_type & ~IPHASH_ANON) == IPHASH_LOOKUP)
84*f3ac6781SToomas Soome 			PRINTF(" type = hash");
85*f3ac6781SToomas Soome 		PRINTF(" number = %s size = %lu",
86*f3ac6781SToomas Soome 			iph.iph_name, (u_long)iph.iph_size);
87*f3ac6781SToomas Soome 		if (iph.iph_seed != 0)
88*f3ac6781SToomas Soome 			PRINTF(" seed = %lu", iph.iph_seed);
89*f3ac6781SToomas Soome 		putchar('\n');
90*f3ac6781SToomas Soome 	} else {
91*f3ac6781SToomas Soome 		PRINTF(" Type: ");
92*f3ac6781SToomas Soome 		switch (iph.iph_type & ~IPHASH_ANON)
93*f3ac6781SToomas Soome 		{
94*f3ac6781SToomas Soome 		case IPHASH_LOOKUP :
95*f3ac6781SToomas Soome 			PRINTF("lookup");
96*f3ac6781SToomas Soome 			break;
97*f3ac6781SToomas Soome 		case IPHASH_GROUPMAP :
98*f3ac6781SToomas Soome 			PRINTF("groupmap Group. %s", iph.iph_name);
99*f3ac6781SToomas Soome 			break;
100*f3ac6781SToomas Soome 		default :
101*f3ac6781SToomas Soome 			break;
102*f3ac6781SToomas Soome 		}
103*f3ac6781SToomas Soome 
104*f3ac6781SToomas Soome 		putchar('\n');
105*f3ac6781SToomas Soome 		PRINTF("\t\tSize: %lu\tSeed: %lu",
106*f3ac6781SToomas Soome 			(u_long)iph.iph_size, iph.iph_seed);
107*f3ac6781SToomas Soome 		PRINTF("\tRef. Count: %d\tMasks: %#x\n", iph.iph_ref,
108*f3ac6781SToomas Soome 			iph.iph_masks[3]);
109*f3ac6781SToomas Soome 	}
110*f3ac6781SToomas Soome 
111*f3ac6781SToomas Soome 	if ((opts & OPT_DEBUG) != 0) {
112*f3ac6781SToomas Soome 		struct in_addr m;
113*f3ac6781SToomas Soome 
114*f3ac6781SToomas Soome 		for (i = 0; i < 32; i++) {
115*f3ac6781SToomas Soome 			if ((1 << i) & iph.iph_masks[3]) {
116*f3ac6781SToomas Soome 				ntomask(4, i, &m.s_addr);
117*f3ac6781SToomas Soome 				PRINTF("\t\tMask: %s\n", inet_ntoa(m));
118*f3ac6781SToomas Soome 			}
119*f3ac6781SToomas Soome 		}
120*f3ac6781SToomas Soome 	}
121*f3ac6781SToomas Soome 
122*f3ac6781SToomas Soome 	if ((opts & OPT_DEBUG) == 0)
123*f3ac6781SToomas Soome 		PRINTF("\t{");
124*f3ac6781SToomas Soome 
125*f3ac6781SToomas Soome 	sz = iph.iph_size * sizeof(*table);
126*f3ac6781SToomas Soome 	table = malloc(sz);
127*f3ac6781SToomas Soome 	if ((*copyfunc)((char *)iph.iph_table, (char *)table, sz))
128*f3ac6781SToomas Soome 		return NULL;
129*f3ac6781SToomas Soome 
130*f3ac6781SToomas Soome 	for (i = 0, printed = 0; i < iph.iph_size; i++) {
131*f3ac6781SToomas Soome 		for (ipep = table[i]; ipep != NULL; ) {
132*f3ac6781SToomas Soome 			ipep = printhashnode(&iph, ipep, copyfunc, opts);
133*f3ac6781SToomas Soome 			printed++;
134*f3ac6781SToomas Soome 		}
135*f3ac6781SToomas Soome 	}
136*f3ac6781SToomas Soome 	if (printed == 0)
137*f3ac6781SToomas Soome 		putchar(';');
138*f3ac6781SToomas Soome 
139*f3ac6781SToomas Soome 	free(table);
140*f3ac6781SToomas Soome 
141*f3ac6781SToomas Soome 	if ((opts & OPT_DEBUG) == 0)
142*f3ac6781SToomas Soome 		PRINTF(" };\n");
143*f3ac6781SToomas Soome 
144*f3ac6781SToomas Soome 	return iph.iph_next;
145*f3ac6781SToomas Soome }
146