xref: /netbsd-src/external/bsd/ipf/dist/lib/printhash_live.c (revision c9d5dc6c77aa32fd07899a7a63638e95ffa433dd)
1*c9d5dc6cSdarrenr /*	$NetBSD: printhash_live.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 <sys/ioctl.h>
10bc4097aaSchristos #include "ipf.h"
11bc4097aaSchristos #include "netinet/ipl.h"
12bc4097aaSchristos 
13bc4097aaSchristos 
14bc4097aaSchristos iphtable_t *
printhash_live(hp,fd,name,opts,fields)15bc4097aaSchristos printhash_live(hp, fd, name, opts, fields)
16bc4097aaSchristos 	iphtable_t *hp;
17bc4097aaSchristos 	int fd;
18bc4097aaSchristos 	char *name;
19bc4097aaSchristos 	int opts;
20bc4097aaSchristos 	wordtab_t *fields;
21bc4097aaSchristos {
22*c9d5dc6cSdarrenr 	iphtent_t entry, zero;
23bc4097aaSchristos 	ipflookupiter_t iter;
24*c9d5dc6cSdarrenr 	int last, printed;
25bc4097aaSchristos 	ipfobj_t obj;
26bc4097aaSchristos 
27bc4097aaSchristos 	if ((name != NULL) && strncmp(name, hp->iph_name, FR_GROUPLEN))
28bc4097aaSchristos 		return hp->iph_next;
29bc4097aaSchristos 
30bc4097aaSchristos 	if (fields == NULL)
31bc4097aaSchristos 		printhashdata(hp, opts);
32bc4097aaSchristos 
33bc4097aaSchristos 	if ((hp->iph_flags & IPHASH_DELETE) != 0)
34bc4097aaSchristos 		PRINTF("# ");
35bc4097aaSchristos 
36bc4097aaSchristos 	if ((opts & OPT_DEBUG) == 0)
37bc4097aaSchristos 		PRINTF("\t{");
38bc4097aaSchristos 
39bc4097aaSchristos 	obj.ipfo_rev = IPFILTER_VERSION;
40bc4097aaSchristos 	obj.ipfo_type = IPFOBJ_LOOKUPITER;
41bc4097aaSchristos 	obj.ipfo_ptr = &iter;
42bc4097aaSchristos 	obj.ipfo_size = sizeof(iter);
43bc4097aaSchristos 
44bc4097aaSchristos 	iter.ili_data = &entry;
45bc4097aaSchristos 	iter.ili_type = IPLT_HASH;
46bc4097aaSchristos 	iter.ili_otype = IPFLOOKUPITER_NODE;
47bc4097aaSchristos 	iter.ili_ival = IPFGENITER_LOOKUP;
48bc4097aaSchristos 	iter.ili_unit = hp->iph_unit;
49bc4097aaSchristos 	strncpy(iter.ili_name, hp->iph_name, FR_GROUPLEN);
50bc4097aaSchristos 
51bc4097aaSchristos 	last = 0;
52bc4097aaSchristos 	printed = 0;
53*c9d5dc6cSdarrenr 	bzero((char *)&zero, sizeof(zero));
54bc4097aaSchristos 
55bc4097aaSchristos 	while (!last && (ioctl(fd, SIOCLOOKUPITER, &obj) == 0)) {
56bc4097aaSchristos 		if (entry.ipe_next == NULL)
57bc4097aaSchristos 			last = 1;
58*c9d5dc6cSdarrenr 		if (bcmp(&zero, &entry, sizeof(zero)) == 0)
59bc4097aaSchristos 			break;
60*c9d5dc6cSdarrenr 		(void) printhashnode(hp, &entry, bcopywrap, opts, fields);
61bc4097aaSchristos 		printed++;
62bc4097aaSchristos 	}
63*c9d5dc6cSdarrenr 	if (last == 0)
64*c9d5dc6cSdarrenr 		ipferror(fd, "walking hash nodes:");
65bc4097aaSchristos 
66bc4097aaSchristos 	if (printed == 0)
67bc4097aaSchristos 		putchar(';');
68bc4097aaSchristos 
69bc4097aaSchristos 	if ((opts & OPT_DEBUG) == 0)
70bc4097aaSchristos 		PRINTF(" };\n");
71bc4097aaSchristos 	return hp->iph_next;
72bc4097aaSchristos }
73