1254219Scy/*
2254219Scy * Copyright (C) 2012 by Darren Reed.
3254219Scy *
4254219Scy * See the IPFILTER.LICENCE file for details on licencing.
5254219Scy */
6254219Scy
7254219Scy#include <sys/ioctl.h>
8254219Scy#include "ipf.h"
9254219Scy#include "netinet/ipl.h"
10254219Scy
11254219Scy
12254219Scy/*
13254219Scy * Because the ipf_dstnode_t can vary in size because of the interface name,
14254219Scy * the size may be larger than just sizeof().
15254219Scy */
16254219Scyippool_dst_t *
17254219Scyprintdstl_live(d, fd, name, opts, fields)
18254219Scy	ippool_dst_t *d;
19254219Scy	int fd;
20254219Scy	char *name;
21254219Scy	int opts;
22254219Scy	wordtab_t *fields;
23254219Scy{
24254219Scy	ipf_dstnode_t *entry, *zero;
25254219Scy	ipflookupiter_t iter;
26254219Scy	int printed, last;
27254219Scy	ipfobj_t obj;
28254219Scy
29254219Scy	if ((name != NULL) && strncmp(name, d->ipld_name, FR_GROUPLEN))
30254219Scy		return d->ipld_next;
31254219Scy
32254219Scy	entry = calloc(1, sizeof(*entry) + 64);
33254219Scy	if (entry == NULL)
34254219Scy		return d->ipld_next;
35254219Scy	zero = calloc(1, sizeof(*zero) + 64);
36254219Scy	if (zero == NULL) {
37254219Scy		free(entry);
38254219Scy		return d->ipld_next;
39254219Scy	}
40254219Scy
41254219Scy	if (fields == NULL)
42254219Scy		printdstlistdata(d, opts);
43254219Scy
44254219Scy	if ((d->ipld_flags & IPHASH_DELETE) != 0)
45254219Scy		PRINTF("# ");
46254219Scy
47254219Scy	if ((opts & OPT_DEBUG) == 0)
48254219Scy		PRINTF("\t{");
49254219Scy
50254219Scy	obj.ipfo_rev = IPFILTER_VERSION;
51254219Scy	obj.ipfo_type = IPFOBJ_LOOKUPITER;
52254219Scy	obj.ipfo_ptr = &iter;
53254219Scy	obj.ipfo_size = sizeof(iter);
54254219Scy
55254219Scy	iter.ili_data = entry;
56254219Scy	iter.ili_type = IPLT_DSTLIST;
57254219Scy	iter.ili_otype = IPFLOOKUPITER_NODE;
58254219Scy	iter.ili_ival = IPFGENITER_LOOKUP;
59254219Scy	iter.ili_unit = d->ipld_unit;
60254219Scy	strncpy(iter.ili_name, d->ipld_name, FR_GROUPLEN);
61254219Scy
62254219Scy	last = 0;
63254219Scy	printed = 0;
64254219Scy
65254219Scy	while (!last && (ioctl(fd, SIOCLOOKUPITER, &obj) == 0)) {
66254219Scy		if (entry->ipfd_next == NULL)
67254219Scy			last = 1;
68254219Scy		if (bcmp((char *)zero, (char *)entry, sizeof(*zero)) == 0)
69254219Scy			break;
70254219Scy		(void) printdstlistnode(entry, bcopywrap, opts, fields);
71254219Scy		printed++;
72254219Scy	}
73254219Scy
74254219Scy	(void) ioctl(fd, SIOCIPFDELTOK, &iter.ili_key);
75254219Scy	free(entry);
76254219Scy	free(zero);
77254219Scy
78254219Scy	if (printed == 0)
79254219Scy		putchar(';');
80254219Scy
81254219Scy	if ((opts & OPT_DEBUG) == 0)
82254219Scy		PRINTF(" };\n");
83254219Scy	return d->ipld_next;
84254219Scy}
85