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