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