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