xref: /freebsd/sbin/ipf/libipf/printdstl_live.c (revision 963f5dc7a30624e95d72fb7f87b8892651164e46)
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 /*
13  * Because the ipf_dstnode_t can vary in size because of the interface name,
14  * the size may be larger than just sizeof().
15  */
16 ippool_dst_t *
17 printdstl_live(d, fd, name, opts, fields)
18 	ippool_dst_t *d;
19 	int fd;
20 	char *name;
21 	int opts;
22 	wordtab_t *fields;
23 {
24 	ipf_dstnode_t *entry, *zero;
25 	ipflookupiter_t iter;
26 	int printed, last;
27 	ipfobj_t obj;
28 
29 	if ((name != NULL) && strncmp(name, d->ipld_name, FR_GROUPLEN))
30 		return d->ipld_next;
31 
32 	entry = calloc(1, sizeof(*entry) + 64);
33 	if (entry == NULL)
34 		return d->ipld_next;
35 	zero = calloc(1, sizeof(*zero) + 64);
36 	if (zero == NULL) {
37 		free(entry);
38 		return d->ipld_next;
39 	}
40 
41 	if (fields == NULL)
42 		printdstlistdata(d, opts);
43 
44 	if ((d->ipld_flags & IPHASH_DELETE) != 0)
45 		PRINTF("# ");
46 
47 	if ((opts & OPT_DEBUG) == 0)
48 		PRINTF("\t{");
49 
50 	obj.ipfo_rev = IPFILTER_VERSION;
51 	obj.ipfo_type = IPFOBJ_LOOKUPITER;
52 	obj.ipfo_ptr = &iter;
53 	obj.ipfo_size = sizeof(iter);
54 
55 	iter.ili_data = entry;
56 	iter.ili_type = IPLT_DSTLIST;
57 	iter.ili_otype = IPFLOOKUPITER_NODE;
58 	iter.ili_ival = IPFGENITER_LOOKUP;
59 	iter.ili_unit = d->ipld_unit;
60 	strncpy(iter.ili_name, d->ipld_name, FR_GROUPLEN);
61 
62 	last = 0;
63 	printed = 0;
64 
65 	while (!last && (ioctl(fd, SIOCLOOKUPITER, &obj) == 0)) {
66 		if (entry->ipfd_next == NULL)
67 			last = 1;
68 		if (bcmp((char *)zero, (char *)entry, sizeof(*zero)) == 0)
69 			break;
70 		(void) printdstlistnode(entry, bcopywrap, opts, fields);
71 		printed++;
72 	}
73 
74 	(void) ioctl(fd, SIOCIPFDELTOK, &iter.ili_key);
75 	free(entry);
76 	free(zero);
77 
78 	if (printed == 0)
79 		putchar(';');
80 
81 	if ((opts & OPT_DEBUG) == 0)
82 		PRINTF(" };\n");
83 	return d->ipld_next;
84 }
85