xref: /freebsd/sbin/ipf/libipf/printpool_live.c (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
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 void
13 printpool_live(ip_pool_t *pool, int fd, char *name, int opts,
14 	wordtab_t *fields)
15 {
16 	ip_pool_node_t entry;
17 	ipflookupiter_t iter;
18 	int printed, last;
19 	ipfobj_t obj;
20 
21 	if ((name != NULL) && strncmp(name, pool->ipo_name, FR_GROUPLEN))
22 		return;
23 
24 	if (fields == NULL)
25 		printpooldata(pool, opts);
26 
27 	if ((pool->ipo_flags & IPOOL_DELETE) != 0)
28 		PRINTF("# ");
29 	if (opts & OPT_SAVEOUT)
30 		PRINTF("{\n");
31 	else if ((opts & OPT_DEBUG) == 0)
32 		PRINTF("\t{");
33 
34 	obj.ipfo_rev = IPFILTER_VERSION;
35 	obj.ipfo_type = IPFOBJ_LOOKUPITER;
36 	obj.ipfo_ptr = &iter;
37 	obj.ipfo_size = sizeof(iter);
38 
39 	iter.ili_data = &entry;
40 	iter.ili_type = IPLT_POOL;
41 	iter.ili_otype = IPFLOOKUPITER_NODE;
42 	iter.ili_ival = IPFGENITER_LOOKUP;
43 	iter.ili_unit = pool->ipo_unit;
44 	strncpy(iter.ili_name, pool->ipo_name, FR_GROUPLEN);
45 
46 	last = 0;
47 	printed = 0;
48 
49 	if (pool->ipo_list != NULL) {
50 		while (!last && (ioctl(fd, SIOCLOOKUPITER, &obj) == 0)) {
51 			if (entry.ipn_next == NULL)
52 				last = 1;
53 			if (opts & OPT_SAVEOUT)
54 				PRINTF("\t");
55 			(void) printpoolnode(&entry, opts, fields);
56 			if ((opts & OPT_DEBUG) == 0)
57 				putchar(';');
58 			if (opts & OPT_SAVEOUT)
59 				PRINTF("\n");
60 			printed++;
61 		}
62 	}
63 
64 	if (printed == 0)
65 		putchar(';');
66 
67 	if (opts & OPT_SAVEOUT)
68 		PRINTF("};\n");
69 	else if ((opts & OPT_DEBUG) == 0)
70 		PRINTF(" };\n");
71 
72 	(void) ioctl(fd,SIOCIPFDELTOK, &iter.ili_key);
73 
74 	return;
75 }
76