printhash.c revision 145510
172445Sassar/*	$NetBSD$	*/
272445Sassar
372445Sassar/*
472445Sassar * Copyright (C) 2002 by Darren Reed.
572445Sassar *
672445Sassar * See the IPFILTER.LICENCE file for details on licencing.
772445Sassar */
872445Sassar
972445Sassar#include "ipf.h"
1072445Sassar
1172445Sassar#define	PRINTF	(void)printf
1272445Sassar#define	FPRINTF	(void)fprintf
1372445Sassar
1472445Sassar
15178825Sdfriphtable_t *printhash(hp, copyfunc, name, opts)
1672445Sassariphtable_t *hp;
1772445Sassarcopyfunc_t copyfunc;
1872445Sassarchar *name;
1972445Sassarint opts;
2072445Sassar{
2172445Sassar	iphtent_t *ipep, **table;
2272445Sassar	iphtable_t iph;
2372445Sassar	int i, printed;
2472445Sassar	size_t sz;
2572445Sassar
2672445Sassar	if ((*copyfunc)((char *)hp, (char *)&iph, sizeof(iph)))
2772445Sassar		return NULL;
2872445Sassar
2972445Sassar	if ((name != NULL) && strncmp(name, iph.iph_name, FR_GROUPLEN))
3072445Sassar		return iph.iph_next;
3172445Sassar
3272445Sassar	if ((opts & OPT_DEBUG) == 0) {
3372445Sassar		if ((iph.iph_type & IPHASH_ANON) == IPHASH_ANON)
3472445Sassar			PRINTF("# 'anonymous' table\n");
3572445Sassar		switch (iph.iph_type & ~IPHASH_ANON)
3672445Sassar		{
37178825Sdfr		case IPHASH_LOOKUP :
38178825Sdfr			PRINTF("table");
39233294Sstas			break;
40233294Sstas		case IPHASH_GROUPMAP :
41178825Sdfr			PRINTF("group-map");
42178825Sdfr			if (iph.iph_flags & FR_INQUE)
43233294Sstas				PRINTF(" in");
44178825Sdfr			else if (iph.iph_flags & FR_OUTQUE)
45178825Sdfr				PRINTF(" out");
46178825Sdfr			else
47178825Sdfr				PRINTF(" ???");
48178825Sdfr			break;
49178825Sdfr		default :
50178825Sdfr			PRINTF("%#x", iph.iph_type);
5172445Sassar			break;
5272445Sassar		}
5372445Sassar		PRINTF(" role = ");
5472445Sassar	} else {
5572445Sassar		PRINTF("Hash Table Number: %s", iph.iph_name);
5672445Sassar		if ((iph.iph_type & IPHASH_ANON) == IPHASH_ANON)
57178825Sdfr			PRINTF("(anon)");
58178825Sdfr		putchar(' ');
5972445Sassar		PRINTF("Role: ");
60178825Sdfr	}
61178825Sdfr
62178825Sdfr	switch (iph.iph_unit)
63178825Sdfr	{
64178825Sdfr	case IPL_LOGNAT :
65178825Sdfr		PRINTF("nat");
6672445Sassar		break;
67	case IPL_LOGIPF :
68		PRINTF("ipf");
69		break;
70	case IPL_LOGAUTH :
71		PRINTF("auth");
72		break;
73	case IPL_LOGCOUNT :
74		PRINTF("count");
75		break;
76	default :
77		PRINTF("#%d", iph.iph_unit);
78		break;
79	}
80
81	if ((opts & OPT_DEBUG) == 0) {
82		if ((iph.iph_type & ~IPHASH_ANON) == IPHASH_LOOKUP)
83			PRINTF(" type = hash");
84		PRINTF(" number = %s size = %lu",
85			iph.iph_name, (u_long)iph.iph_size);
86		if (iph.iph_seed != 0)
87			PRINTF(" seed = %lu", iph.iph_seed);
88		putchar('\n');
89	} else {
90		PRINTF(" Type: ");
91		switch (iph.iph_type & ~IPHASH_ANON)
92		{
93		case IPHASH_LOOKUP :
94			PRINTF("lookup");
95			break;
96		case IPHASH_GROUPMAP :
97			PRINTF("groupmap Group. %s", iph.iph_name);
98			break;
99		default :
100			break;
101		}
102
103		putchar('\n');
104		PRINTF("\t\tSize: %lu\tSeed: %lu",
105			(u_long)iph.iph_size, iph.iph_seed);
106		PRINTF("\tRef. Count: %d\tMasks: %#x\n", iph.iph_ref,
107			iph.iph_masks);
108	}
109
110	if ((opts & OPT_DEBUG) != 0) {
111		struct in_addr m;
112
113		for (i = 0; i < 32; i++) {
114			if ((1 << i) & iph.iph_masks) {
115				ntomask(4, i, &m.s_addr);
116				PRINTF("\t\tMask: %s\n", inet_ntoa(m));
117			}
118		}
119	}
120
121	if ((opts & OPT_DEBUG) == 0)
122		PRINTF("\t{");
123
124	sz = iph.iph_size * sizeof(*table);
125	table = malloc(sz);
126	if ((*copyfunc)((char *)iph.iph_table, (char *)table, sz))
127		return NULL;
128
129	for (i = 0, printed = 0; i < iph.iph_size; i++) {
130		for (ipep = table[i]; ipep != NULL; ) {
131			ipep = printhashnode(&iph, ipep, copyfunc, opts);
132			printed++;
133		}
134	}
135	if (printed == 0)
136		putchar(';');
137
138	free(table);
139
140	if ((opts & OPT_DEBUG) == 0)
141		PRINTF(" };\n");
142
143	return iph.iph_next;
144}
145