xref: /illumos-gate/usr/src/cmd/ipf/lib/printpool_live.c (revision 8119dad84d6416f13557b0ba8e2aaf9064cbcfd3)
1 /*
2  * Copyright (C) 2002 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  *
6  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
7  * Use is subject to license terms.
8  */
9 
10 #include <sys/ioctl.h>
11 #include "ipf.h"
12 #include "netinet/ipl.h"
13 
14 #define	PRINTF	(void)printf
15 #define	FPRINTF	(void)fprintf
16 
17 
18 ip_pool_t *printpool_live(pool, fd, name, opts)
19 ip_pool_t *pool;
20 int fd;
21 char *name;
22 int opts;
23 {
24 	ip_pool_node_t entry, *top, *node;
25 	ipflookupiter_t iter;
26 	int i, printed = 0, last;
27 	ipfobj_t obj;
28 
29 	if ((name != NULL) && strncmp(name, pool->ipo_name, FR_GROUPLEN))
30 		return pool->ipo_next;
31 
32 	printpooldata(pool, opts);
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_POOL;
44 	iter.ili_otype = IPFLOOKUPITER_NODE;
45 	iter.ili_ival = IPFGENITER_LOOKUP;
46 	iter.ili_unit = pool->ipo_unit;
47 	strncpy(iter.ili_name, pool->ipo_name, FR_GROUPLEN);
48 
49 	last = 0;
50 	top = NULL;
51 
52 	while (!last && (ioctl(fd, SIOCLOOKUPITER, &obj) == 0)) {
53 		if (entry.ipn_next == NULL)
54 			last = 1;
55 		entry.ipn_next = top;
56 		top = malloc(sizeof(*top));
57 		if (top == NULL)
58 			break;
59 		bcopy(&entry, top, sizeof(entry));
60 	}
61 
62 	while (top != NULL) {
63 		node = top;
64 		(void) printpoolnode(node, opts);
65 		top = node->ipn_next;
66 		free(node);
67 		printed++;
68 
69 		if ((opts & OPT_DEBUG) == 0)
70 			putchar(';');
71 	}
72 
73 	if (printed == 0)
74 		putchar(';');
75 
76 	if ((opts & OPT_DEBUG) == 0)
77 		PRINTF(" };\n");
78 	return pool->ipo_next;
79 }
80