xref: /illumos-gate/usr/src/cmd/ipf/lib/common/printhash.c (revision ab25eeb5)
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  *
6*ab25eeb5Syz  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
77663b816Sml  * Use is subject to license terms.
87c478bd9Sstevel@tonic-gate  */
97663b816Sml 
107663b816Sml #pragma ident	"%Z%%M%	%I%	%E% SMI"
117c478bd9Sstevel@tonic-gate 
127c478bd9Sstevel@tonic-gate #include "ipf.h"
137c478bd9Sstevel@tonic-gate 
147c478bd9Sstevel@tonic-gate #define	PRINTF	(void)printf
157c478bd9Sstevel@tonic-gate #define	FPRINTF	(void)fprintf
167c478bd9Sstevel@tonic-gate 
17*ab25eeb5Syz 
printhash(hp,copyfunc,name,opts)18*ab25eeb5Syz iphtable_t *printhash(hp, copyfunc, name, opts)
197c478bd9Sstevel@tonic-gate iphtable_t *hp;
207c478bd9Sstevel@tonic-gate copyfunc_t copyfunc;
21*ab25eeb5Syz char *name;
227c478bd9Sstevel@tonic-gate int opts;
237c478bd9Sstevel@tonic-gate {
247663b816Sml 	iphtent_t *ipep, **table;
257c478bd9Sstevel@tonic-gate 	iphtable_t iph;
26*ab25eeb5Syz 	int i, printed;
277663b816Sml 	size_t sz;
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate 	if ((*copyfunc)((char *)hp, (char *)&iph, sizeof(iph)))
307c478bd9Sstevel@tonic-gate 		return NULL;
317c478bd9Sstevel@tonic-gate 
32*ab25eeb5Syz 	if ((name != NULL) && strncmp(name, iph.iph_name, FR_GROUPLEN))
33*ab25eeb5Syz 		return iph.iph_next;
34*ab25eeb5Syz 
357c478bd9Sstevel@tonic-gate 	if ((opts & OPT_DEBUG) == 0) {
367c478bd9Sstevel@tonic-gate 		if ((iph.iph_type & IPHASH_ANON) == IPHASH_ANON)
377c478bd9Sstevel@tonic-gate 			PRINTF("# 'anonymous' table\n");
387c478bd9Sstevel@tonic-gate 		switch (iph.iph_type & ~IPHASH_ANON)
397c478bd9Sstevel@tonic-gate 		{
407c478bd9Sstevel@tonic-gate 		case IPHASH_LOOKUP :
417c478bd9Sstevel@tonic-gate 			PRINTF("table");
427c478bd9Sstevel@tonic-gate 			break;
437c478bd9Sstevel@tonic-gate 		case IPHASH_GROUPMAP :
447c478bd9Sstevel@tonic-gate 			PRINTF("group-map");
457c478bd9Sstevel@tonic-gate 			if (iph.iph_flags & FR_INQUE)
467c478bd9Sstevel@tonic-gate 				PRINTF(" in");
477c478bd9Sstevel@tonic-gate 			else if (iph.iph_flags & FR_OUTQUE)
487c478bd9Sstevel@tonic-gate 				PRINTF(" out");
497c478bd9Sstevel@tonic-gate 			else
507c478bd9Sstevel@tonic-gate 				PRINTF(" ???");
517c478bd9Sstevel@tonic-gate 			break;
527c478bd9Sstevel@tonic-gate 		default :
537c478bd9Sstevel@tonic-gate 			PRINTF("%#x", iph.iph_type);
547c478bd9Sstevel@tonic-gate 			break;
557c478bd9Sstevel@tonic-gate 		}
567c478bd9Sstevel@tonic-gate 		PRINTF(" role = ");
577c478bd9Sstevel@tonic-gate 	} else {
587c478bd9Sstevel@tonic-gate 		PRINTF("Hash Table Number: %s", iph.iph_name);
597c478bd9Sstevel@tonic-gate 		if ((iph.iph_type & IPHASH_ANON) == IPHASH_ANON)
607c478bd9Sstevel@tonic-gate 			PRINTF("(anon)");
617c478bd9Sstevel@tonic-gate 		putchar(' ');
627c478bd9Sstevel@tonic-gate 		PRINTF("Role: ");
637c478bd9Sstevel@tonic-gate 	}
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	switch (iph.iph_unit)
667c478bd9Sstevel@tonic-gate 	{
677c478bd9Sstevel@tonic-gate 	case IPL_LOGNAT :
687c478bd9Sstevel@tonic-gate 		PRINTF("nat");
697c478bd9Sstevel@tonic-gate 		break;
707c478bd9Sstevel@tonic-gate 	case IPL_LOGIPF :
717c478bd9Sstevel@tonic-gate 		PRINTF("ipf");
727c478bd9Sstevel@tonic-gate 		break;
737c478bd9Sstevel@tonic-gate 	case IPL_LOGAUTH :
747c478bd9Sstevel@tonic-gate 		PRINTF("auth");
757c478bd9Sstevel@tonic-gate 		break;
767c478bd9Sstevel@tonic-gate 	case IPL_LOGCOUNT :
777c478bd9Sstevel@tonic-gate 		PRINTF("count");
787c478bd9Sstevel@tonic-gate 		break;
797c478bd9Sstevel@tonic-gate 	default :
807c478bd9Sstevel@tonic-gate 		PRINTF("#%d", iph.iph_unit);
817c478bd9Sstevel@tonic-gate 		break;
827c478bd9Sstevel@tonic-gate 	}
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	if ((opts & OPT_DEBUG) == 0) {
857c478bd9Sstevel@tonic-gate 		if ((iph.iph_type & ~IPHASH_ANON) == IPHASH_LOOKUP)
867c478bd9Sstevel@tonic-gate 			PRINTF(" type = hash");
877663b816Sml 		PRINTF(" number = %s size = %lu",
887663b816Sml 			iph.iph_name, (u_long)iph.iph_size);
897c478bd9Sstevel@tonic-gate 		if (iph.iph_seed != 0)
907c478bd9Sstevel@tonic-gate 			PRINTF(" seed = %lu", iph.iph_seed);
917c478bd9Sstevel@tonic-gate 		putchar('\n');
927c478bd9Sstevel@tonic-gate 	} else {
937c478bd9Sstevel@tonic-gate 		PRINTF(" Type: ");
947c478bd9Sstevel@tonic-gate 		switch (iph.iph_type & ~IPHASH_ANON)
957c478bd9Sstevel@tonic-gate 		{
967c478bd9Sstevel@tonic-gate 		case IPHASH_LOOKUP :
977c478bd9Sstevel@tonic-gate 			PRINTF("lookup");
987c478bd9Sstevel@tonic-gate 			break;
997c478bd9Sstevel@tonic-gate 		case IPHASH_GROUPMAP :
1007c478bd9Sstevel@tonic-gate 			PRINTF("groupmap Group. %s", iph.iph_name);
1017c478bd9Sstevel@tonic-gate 			break;
1027c478bd9Sstevel@tonic-gate 		default :
1037c478bd9Sstevel@tonic-gate 			break;
1047c478bd9Sstevel@tonic-gate 		}
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 		putchar('\n');
1077663b816Sml 		PRINTF("\t\tSize: %lu\tSeed: %lu",
1087663b816Sml 			(u_long)iph.iph_size, iph.iph_seed);
1097663b816Sml 		PRINTF("\tRef. Count: %d\tMasks: %#x\n", iph.iph_ref,
1107663b816Sml 			iph.iph_masks[3]);
1117c478bd9Sstevel@tonic-gate 	}
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	if ((opts & OPT_DEBUG) != 0) {
1147663b816Sml 		struct in_addr m;
1157c478bd9Sstevel@tonic-gate 
1167663b816Sml 		for (i = 0; i < 32; i++) {
1177663b816Sml 			if ((1 << i) & iph.iph_masks[3]) {
1187663b816Sml 				ntomask(4, i, &m.s_addr);
1197663b816Sml 				PRINTF("\t\tMask: %s\n", inet_ntoa(m));
1207663b816Sml 			}
1217c478bd9Sstevel@tonic-gate 		}
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate 
1247663b816Sml 	if ((opts & OPT_DEBUG) == 0)
1257663b816Sml 		PRINTF("\t{");
1267663b816Sml 
1277663b816Sml 	sz = iph.iph_size * sizeof(*table);
1287663b816Sml 	table = malloc(sz);
1297663b816Sml 	if ((*copyfunc)((char *)iph.iph_table, (char *)table, sz))
1307663b816Sml 		return NULL;
1317c478bd9Sstevel@tonic-gate 
132*ab25eeb5Syz 	for (i = 0, printed = 0; i < iph.iph_size; i++) {
1337663b816Sml 		for (ipep = table[i]; ipep != NULL; ) {
1347c478bd9Sstevel@tonic-gate 			ipep = printhashnode(&iph, ipep, copyfunc, opts);
135*ab25eeb5Syz 			printed++;
1367663b816Sml 		}
1377663b816Sml 	}
138*ab25eeb5Syz 	if (printed == 0)
139*ab25eeb5Syz 		putchar(';');
1407663b816Sml 
1417663b816Sml 	free(table);
1427c478bd9Sstevel@tonic-gate 
1437663b816Sml 	if ((opts & OPT_DEBUG) == 0)
1447663b816Sml 		PRINTF(" };\n");
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	return iph.iph_next;
1477c478bd9Sstevel@tonic-gate }
148