printpool_live.c revision 1.2
1/*	$NetBSD: printpool_live.c,v 1.2 2012/02/15 17:55:07 riz Exp $	*/
2
3/*
4 * Copyright (C) 2002 by Darren Reed.
5 *
6 * See the IPFILTER.LICENCE file for details on licencing.
7 */
8
9#include <sys/ioctl.h>
10#include "ipf.h"
11#include "netinet/ipl.h"
12
13#define	PRINTF	(void)printf
14#define	FPRINTF	(void)fprintf
15
16
17ip_pool_t *printpool_live(pool, fd, name, opts)
18ip_pool_t *pool;
19int fd;
20char *name;
21int opts;
22{
23	ip_pool_node_t entry, *top, *node;
24	ipflookupiter_t iter;
25	int printed, last;
26	ipfobj_t obj;
27
28	if ((name != NULL) && strncmp(name, pool->ipo_name, FR_GROUPLEN))
29		return pool->ipo_next;
30
31	printpooldata(pool, opts);
32
33	if ((pool->ipo_flags & IPOOL_DELETE) != 0)
34		PRINTF("# ");
35	if ((opts & OPT_DEBUG) == 0)
36		PRINTF("\t{");
37
38	obj.ipfo_rev = IPFILTER_VERSION;
39	obj.ipfo_type = IPFOBJ_LOOKUPITER;
40	obj.ipfo_ptr = &iter;
41	obj.ipfo_size = sizeof(iter);
42
43	iter.ili_data = &entry;
44	iter.ili_type = IPLT_POOL;
45	iter.ili_otype = IPFLOOKUPITER_NODE;
46	iter.ili_ival = IPFGENITER_LOOKUP;
47	iter.ili_unit = pool->ipo_unit;
48	strncpy(iter.ili_name, pool->ipo_name, FR_GROUPLEN);
49
50	last = 0;
51	top = NULL;
52	printed = 0;
53
54	while (!last && (ioctl(fd, SIOCLOOKUPITER, &obj) == 0)) {
55		if (entry.ipn_next == NULL)
56			last = 1;
57		node = malloc(sizeof(*top));
58		if (node == NULL)
59			break;
60		bcopy(&entry, node, sizeof(entry));
61		node->ipn_next = top;
62		top = node;
63	}
64
65	while (top != NULL) {
66		node = top;
67		(void) printpoolnode(node, opts);
68		if ((opts & OPT_DEBUG) == 0)
69			putchar(';');
70		top = node->ipn_next;
71		free(node);
72		printed++;
73	}
74
75	if (printed == 0)
76		putchar(';');
77
78	if ((opts & OPT_DEBUG) == 0)
79		PRINTF(" };\n");
80
81	if (ioctl(fd, SIOCIPFDELTOK, &iter.ili_key) != 0)
82		perror("SIOCIPFDELTOK");
83
84	return pool->ipo_next;
85}
86