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