xref: /freebsd/sbin/ipf/libipf/printhash_live.c (revision 02e9120893770924227138ba49df1edb3896112a)
1 /*
2  * Copyright (C) 2012 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  */
6 
7 #include <sys/ioctl.h>
8 #include "ipf.h"
9 #include "netinet/ipl.h"
10 
11 
12 iphtable_t *
13 printhash_live(iphtable_t *hp, int fd, char *name, int opts, wordtab_t *fields)
14 {
15 	iphtent_t entry, zero;
16 	ipflookupiter_t iter;
17 	int last, printed;
18 	ipfobj_t obj;
19 
20 	if ((name != NULL) && strncmp(name, hp->iph_name, FR_GROUPLEN))
21 		return (hp->iph_next);
22 
23 	if (fields == NULL)
24 		printhashdata(hp, opts);
25 
26 	if ((hp->iph_flags & IPHASH_DELETE) != 0)
27 		PRINTF("# ");
28 
29 	if ((opts & OPT_DEBUG) == 0)
30 		PRINTF("\t{");
31 
32 	obj.ipfo_rev = IPFILTER_VERSION;
33 	obj.ipfo_type = IPFOBJ_LOOKUPITER;
34 	obj.ipfo_ptr = &iter;
35 	obj.ipfo_size = sizeof(iter);
36 
37 	iter.ili_data = &entry;
38 	iter.ili_type = IPLT_HASH;
39 	iter.ili_otype = IPFLOOKUPITER_NODE;
40 	iter.ili_ival = IPFGENITER_LOOKUP;
41 	iter.ili_unit = hp->iph_unit;
42 	strncpy(iter.ili_name, hp->iph_name, FR_GROUPLEN);
43 
44 	last = 0;
45 	printed = 0;
46 	bzero((char *)&zero, sizeof(zero));
47 
48 	while (!last && (ioctl(fd, SIOCLOOKUPITER, &obj) == 0)) {
49 		if (entry.ipe_next == NULL)
50 			last = 1;
51 		if (bcmp(&zero, &entry, sizeof(zero)) == 0)
52 			break;
53 		(void) printhashnode(hp, &entry, bcopywrap, opts, fields);
54 		printed++;
55 	}
56 	if (last == 0)
57 		ipferror(fd, "walking hash nodes");
58 
59 	if (printed == 0)
60 		putchar(';');
61 
62 	if ((opts & OPT_DEBUG) == 0)
63 		PRINTF(" };\n");
64 	return (hp->iph_next);
65 }
66