xref: /illumos-gate/usr/src/cmd/ipf/lib/printhash.c (revision f3ac6781)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (C) 2002 by Darren Reed.
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * See the IPFILTER.LICENCE file for details on licencing.
57663b816Sml  *
6ab25eeb5Syz  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
77663b816Sml  * Use is subject to license terms.
87c478bd9Sstevel@tonic-gate  */
97c478bd9Sstevel@tonic-gate 
107c478bd9Sstevel@tonic-gate #include "ipf.h"
117c478bd9Sstevel@tonic-gate 
127c478bd9Sstevel@tonic-gate #define	PRINTF	(void)printf
137c478bd9Sstevel@tonic-gate #define	FPRINTF	(void)fprintf
147c478bd9Sstevel@tonic-gate 
15ab25eeb5Syz 
printhash(hp,copyfunc,name,opts)16ab25eeb5Syz iphtable_t *printhash(hp, copyfunc, name, opts)
177c478bd9Sstevel@tonic-gate iphtable_t *hp;
187c478bd9Sstevel@tonic-gate copyfunc_t copyfunc;
19ab25eeb5Syz char *name;
207c478bd9Sstevel@tonic-gate int opts;
217c478bd9Sstevel@tonic-gate {
227663b816Sml 	iphtent_t *ipep, **table;
237c478bd9Sstevel@tonic-gate 	iphtable_t iph;
24ab25eeb5Syz 	int i, printed;
257663b816Sml 	size_t sz;
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate 	if ((*copyfunc)((char *)hp, (char *)&iph, sizeof(iph)))
287c478bd9Sstevel@tonic-gate 		return NULL;
297c478bd9Sstevel@tonic-gate 
30ab25eeb5Syz 	if ((name != NULL) && strncmp(name, iph.iph_name, FR_GROUPLEN))
31ab25eeb5Syz 		return iph.iph_next;
32ab25eeb5Syz 
337c478bd9Sstevel@tonic-gate 	if ((opts & OPT_DEBUG) == 0) {
347c478bd9Sstevel@tonic-gate 		if ((iph.iph_type & IPHASH_ANON) == IPHASH_ANON)
357c478bd9Sstevel@tonic-gate 			PRINTF("# 'anonymous' table\n");
367c478bd9Sstevel@tonic-gate 		switch (iph.iph_type & ~IPHASH_ANON)
377c478bd9Sstevel@tonic-gate 		{
387c478bd9Sstevel@tonic-gate 		case IPHASH_LOOKUP :
397c478bd9Sstevel@tonic-gate 			PRINTF("table");
407c478bd9Sstevel@tonic-gate 			break;
417c478bd9Sstevel@tonic-gate 		case IPHASH_GROUPMAP :
427c478bd9Sstevel@tonic-gate 			PRINTF("group-map");
437c478bd9Sstevel@tonic-gate 			if (iph.iph_flags & FR_INQUE)
447c478bd9Sstevel@tonic-gate 				PRINTF(" in");
457c478bd9Sstevel@tonic-gate 			else if (iph.iph_flags & FR_OUTQUE)
467c478bd9Sstevel@tonic-gate 				PRINTF(" out");
477c478bd9Sstevel@tonic-gate 			else
487c478bd9Sstevel@tonic-gate 				PRINTF(" ???");
497c478bd9Sstevel@tonic-gate 			break;
507c478bd9Sstevel@tonic-gate 		default :
517c478bd9Sstevel@tonic-gate 			PRINTF("%#x", iph.iph_type);
527c478bd9Sstevel@tonic-gate 			break;
537c478bd9Sstevel@tonic-gate 		}
547c478bd9Sstevel@tonic-gate 		PRINTF(" role = ");
557c478bd9Sstevel@tonic-gate 	} else {
567c478bd9Sstevel@tonic-gate 		PRINTF("Hash Table Number: %s", iph.iph_name);
577c478bd9Sstevel@tonic-gate 		if ((iph.iph_type & IPHASH_ANON) == IPHASH_ANON)
587c478bd9Sstevel@tonic-gate 			PRINTF("(anon)");
597c478bd9Sstevel@tonic-gate 		putchar(' ');
607c478bd9Sstevel@tonic-gate 		PRINTF("Role: ");
617c478bd9Sstevel@tonic-gate 	}
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	switch (iph.iph_unit)
647c478bd9Sstevel@tonic-gate 	{
657c478bd9Sstevel@tonic-gate 	case IPL_LOGNAT :
667c478bd9Sstevel@tonic-gate 		PRINTF("nat");
677c478bd9Sstevel@tonic-gate 		break;
687c478bd9Sstevel@tonic-gate 	case IPL_LOGIPF :
697c478bd9Sstevel@tonic-gate 		PRINTF("ipf");
707c478bd9Sstevel@tonic-gate 		break;
717c478bd9Sstevel@tonic-gate 	case IPL_LOGAUTH :
727c478bd9Sstevel@tonic-gate 		PRINTF("auth");
737c478bd9Sstevel@tonic-gate 		break;
747c478bd9Sstevel@tonic-gate 	case IPL_LOGCOUNT :
757c478bd9Sstevel@tonic-gate 		PRINTF("count");
767c478bd9Sstevel@tonic-gate 		break;
777c478bd9Sstevel@tonic-gate 	default :
787c478bd9Sstevel@tonic-gate 		PRINTF("#%d", iph.iph_unit);
797c478bd9Sstevel@tonic-gate 		break;
807c478bd9Sstevel@tonic-gate 	}
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	if ((opts & OPT_DEBUG) == 0) {
837c478bd9Sstevel@tonic-gate 		if ((iph.iph_type & ~IPHASH_ANON) == IPHASH_LOOKUP)
847c478bd9Sstevel@tonic-gate 			PRINTF(" type = hash");
857663b816Sml 		PRINTF(" number = %s size = %lu",
867663b816Sml 			iph.iph_name, (u_long)iph.iph_size);
877c478bd9Sstevel@tonic-gate 		if (iph.iph_seed != 0)
887c478bd9Sstevel@tonic-gate 			PRINTF(" seed = %lu", iph.iph_seed);
897c478bd9Sstevel@tonic-gate 		putchar('\n');
907c478bd9Sstevel@tonic-gate 	} else {
917c478bd9Sstevel@tonic-gate 		PRINTF(" Type: ");
927c478bd9Sstevel@tonic-gate 		switch (iph.iph_type & ~IPHASH_ANON)
937c478bd9Sstevel@tonic-gate 		{
947c478bd9Sstevel@tonic-gate 		case IPHASH_LOOKUP :
957c478bd9Sstevel@tonic-gate 			PRINTF("lookup");
967c478bd9Sstevel@tonic-gate 			break;
977c478bd9Sstevel@tonic-gate 		case IPHASH_GROUPMAP :
987c478bd9Sstevel@tonic-gate 			PRINTF("groupmap Group. %s", iph.iph_name);
997c478bd9Sstevel@tonic-gate 			break;
1007c478bd9Sstevel@tonic-gate 		default :
1017c478bd9Sstevel@tonic-gate 			break;
1027c478bd9Sstevel@tonic-gate 		}
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 		putchar('\n');
1057663b816Sml 		PRINTF("\t\tSize: %lu\tSeed: %lu",
1067663b816Sml 			(u_long)iph.iph_size, iph.iph_seed);
1077663b816Sml 		PRINTF("\tRef. Count: %d\tMasks: %#x\n", iph.iph_ref,
1087663b816Sml 			iph.iph_masks[3]);
1097c478bd9Sstevel@tonic-gate 	}
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	if ((opts & OPT_DEBUG) != 0) {
1127663b816Sml 		struct in_addr m;
1137c478bd9Sstevel@tonic-gate 
1147663b816Sml 		for (i = 0; i < 32; i++) {
1157663b816Sml 			if ((1 << i) & iph.iph_masks[3]) {
1167663b816Sml 				ntomask(4, i, &m.s_addr);
1177663b816Sml 				PRINTF("\t\tMask: %s\n", inet_ntoa(m));
1187663b816Sml 			}
1197c478bd9Sstevel@tonic-gate 		}
1207c478bd9Sstevel@tonic-gate 	}
1217c478bd9Sstevel@tonic-gate 
1227663b816Sml 	if ((opts & OPT_DEBUG) == 0)
1237663b816Sml 		PRINTF("\t{");
1247663b816Sml 
1257663b816Sml 	sz = iph.iph_size * sizeof(*table);
1267663b816Sml 	table = malloc(sz);
1277663b816Sml 	if ((*copyfunc)((char *)iph.iph_table, (char *)table, sz))
1287663b816Sml 		return NULL;
1297c478bd9Sstevel@tonic-gate 
130ab25eeb5Syz 	for (i = 0, printed = 0; i < iph.iph_size; i++) {
1317663b816Sml 		for (ipep = table[i]; ipep != NULL; ) {
1327c478bd9Sstevel@tonic-gate 			ipep = printhashnode(&iph, ipep, copyfunc, opts);
133ab25eeb5Syz 			printed++;
1347663b816Sml 		}
1357663b816Sml 	}
136ab25eeb5Syz 	if (printed == 0)
137ab25eeb5Syz 		putchar(';');
1387663b816Sml 
1397663b816Sml 	free(table);
1407c478bd9Sstevel@tonic-gate 
1417663b816Sml 	if ((opts & OPT_DEBUG) == 0)
1427663b816Sml 		PRINTF(" };\n");
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	return iph.iph_next;
1457c478bd9Sstevel@tonic-gate }
146