printhash.c revision 145510
134198Sjdp/*	$NetBSD$	*/
260698Sjdp
334198Sjdp/*
434198Sjdp * Copyright (C) 2002 by Darren Reed.
534198Sjdp *
634198Sjdp * See the IPFILTER.LICENCE file for details on licencing.
734198Sjdp */
834198Sjdp
934198Sjdp#include "ipf.h"
1034198Sjdp
1134198Sjdp#define	PRINTF	(void)printf
1234198Sjdp#define	FPRINTF	(void)fprintf
1334198Sjdp
1434198Sjdp
1534198Sjdpiphtable_t *printhash(hp, copyfunc, name, opts)
1634198Sjdpiphtable_t *hp;
1734198Sjdpcopyfunc_t copyfunc;
1834198Sjdpchar *name;
1934198Sjdpint opts;
2034198Sjdp{
2134198Sjdp	iphtent_t *ipep, **table;
2234198Sjdp	iphtable_t iph;
2334198Sjdp	int i, printed;
2434198Sjdp	size_t sz;
2534198Sjdp
26216338Sdim	if ((*copyfunc)((char *)hp, (char *)&iph, sizeof(iph)))
27216338Sdim		return NULL;
28216338Sdim
2967811Sobrien	if ((name != NULL) && strncmp(name, iph.iph_name, FR_GROUPLEN))
3067811Sobrien		return iph.iph_next;
3167811Sobrien
3267811Sobrien	if ((opts & OPT_DEBUG) == 0) {
3367811Sobrien		if ((iph.iph_type & IPHASH_ANON) == IPHASH_ANON)
34127252Speter			PRINTF("# 'anonymous' table\n");
3567811Sobrien		switch (iph.iph_type & ~IPHASH_ANON)
3667811Sobrien		{
3767811Sobrien		case IPHASH_LOOKUP :
3867811Sobrien			PRINTF("table");
3967811Sobrien			break;
4067811Sobrien		case IPHASH_GROUPMAP :
41127252Speter			PRINTF("group-map");
42			if (iph.iph_flags & FR_INQUE)
43				PRINTF(" in");
44			else if (iph.iph_flags & FR_OUTQUE)
45				PRINTF(" out");
46			else
47				PRINTF(" ???");
48			break;
49		default :
50			PRINTF("%#x", iph.iph_type);
51			break;
52		}
53		PRINTF(" role = ");
54	} else {
55		PRINTF("Hash Table Number: %s", iph.iph_name);
56		if ((iph.iph_type & IPHASH_ANON) == IPHASH_ANON)
57			PRINTF("(anon)");
58		putchar(' ');
59		PRINTF("Role: ");
60	}
61
62	switch (iph.iph_unit)
63	{
64	case IPL_LOGNAT :
65		PRINTF("nat");
66		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