xref: /netbsd-src/external/bsd/ipf/dist/lib/printhash.c (revision c9d5dc6c77aa32fd07899a7a63638e95ffa433dd)
1*c9d5dc6cSdarrenr /*	$NetBSD: printhash.c,v 1.1.1.2 2012/07/22 13:44:40 darrenr Exp $	*/
2bc4097aaSchristos 
3bc4097aaSchristos /*
4*c9d5dc6cSdarrenr  * Copyright (C) 2012 by Darren Reed.
5bc4097aaSchristos  *
6bc4097aaSchristos  * See the IPFILTER.LICENCE file for details on licencing.
7bc4097aaSchristos  */
8bc4097aaSchristos 
9bc4097aaSchristos #include "ipf.h"
10bc4097aaSchristos 
11bc4097aaSchristos 
12bc4097aaSchristos iphtable_t *
printhash(hp,copyfunc,name,opts,fields)13bc4097aaSchristos printhash(hp, copyfunc, name, opts, fields)
14bc4097aaSchristos 	iphtable_t *hp;
15bc4097aaSchristos 	copyfunc_t copyfunc;
16bc4097aaSchristos 	char *name;
17bc4097aaSchristos 	int opts;
18bc4097aaSchristos 	wordtab_t *fields;
19bc4097aaSchristos {
20bc4097aaSchristos 	iphtent_t *ipep, **table;
21bc4097aaSchristos 	iphtable_t iph;
22bc4097aaSchristos 	int printed;
23bc4097aaSchristos 	size_t sz;
24bc4097aaSchristos 
25bc4097aaSchristos 	if ((*copyfunc)((char *)hp, (char *)&iph, sizeof(iph)))
26bc4097aaSchristos 		return NULL;
27bc4097aaSchristos 
28bc4097aaSchristos 	if ((name != NULL) && strncmp(name, iph.iph_name, FR_GROUPLEN))
29bc4097aaSchristos 		return iph.iph_next;
30bc4097aaSchristos 
31bc4097aaSchristos 	if (fields == NULL)
32bc4097aaSchristos 		printhashdata(hp, opts);
33bc4097aaSchristos 
34bc4097aaSchristos 	if ((hp->iph_flags & IPHASH_DELETE) != 0)
35bc4097aaSchristos 		PRINTF("# ");
36bc4097aaSchristos 
37bc4097aaSchristos 	if ((opts & OPT_DEBUG) == 0)
38bc4097aaSchristos 		PRINTF("\t{");
39bc4097aaSchristos 
40bc4097aaSchristos 	sz = iph.iph_size * sizeof(*table);
41bc4097aaSchristos 	table = malloc(sz);
42bc4097aaSchristos 	if ((*copyfunc)((char *)iph.iph_table, (char *)table, sz))
43bc4097aaSchristos 		return NULL;
44bc4097aaSchristos 
45bc4097aaSchristos 	for (printed = 0, ipep = iph.iph_list; ipep != NULL; ) {
46bc4097aaSchristos 		ipep = printhashnode(&iph, ipep, copyfunc, opts, fields);
47bc4097aaSchristos 		printed++;
48bc4097aaSchristos 	}
49bc4097aaSchristos 	if (printed == 0)
50bc4097aaSchristos 		putchar(';');
51bc4097aaSchristos 
52bc4097aaSchristos 	free(table);
53bc4097aaSchristos 
54bc4097aaSchristos 	if ((opts & OPT_DEBUG) == 0)
55bc4097aaSchristos 		PRINTF(" };\n");
56bc4097aaSchristos 
57bc4097aaSchristos 	return iph.iph_next;
58bc4097aaSchristos }
59