xref: /freebsd/sbin/ipf/libipf/printpool_live.c (revision 18054d0220cfc8df9c9568c437bd6fbb59d53c3c)
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 ip_pool_t *
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 (pool->ipo_next);
23 
24 	if (fields == NULL)
25 		printpooldata(pool, opts);
26 
27 	if ((pool->ipo_flags & IPOOL_DELETE) != 0)
28 		PRINTF("# ");
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_POOL;
39 	iter.ili_otype = IPFLOOKUPITER_NODE;
40 	iter.ili_ival = IPFGENITER_LOOKUP;
41 	iter.ili_unit = pool->ipo_unit;
42 	strncpy(iter.ili_name, pool->ipo_name, FR_GROUPLEN);
43 
44 	last = 0;
45 	printed = 0;
46 
47 	if (pool->ipo_list != NULL) {
48 		while (!last && (ioctl(fd, SIOCLOOKUPITER, &obj) == 0)) {
49 			if (entry.ipn_next == NULL)
50 				last = 1;
51 			(void) printpoolnode(&entry, opts, fields);
52 			if ((opts & OPT_DEBUG) == 0)
53 				putchar(';');
54 			printed++;
55 		}
56 	}
57 
58 	if (printed == 0)
59 		putchar(';');
60 
61 	if ((opts & OPT_DEBUG) == 0)
62 		PRINTF(" };\n");
63 
64 	(void) ioctl(fd,SIOCIPFDELTOK, &iter.ili_key);
65 
66 	return (pool->ipo_next);
67 }
68