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