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