printdstl_live.c revision 1.1.1.1 1 /* $NetBSD: printdstl_live.c,v 1.1.1.1 2012/03/23 21:20:09 christos Exp $ */
2
3 /*
4 * Copyright (C) 2010 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 *
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, *top, *node;
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
38 if (fields == NULL)
39 printdstlistdata(d, opts);
40
41 if ((d->ipld_flags & IPHASH_DELETE) != 0)
42 PRINTF("# ");
43
44 if ((opts & OPT_DEBUG) == 0)
45 PRINTF("\t{");
46
47 obj.ipfo_rev = IPFILTER_VERSION;
48 obj.ipfo_type = IPFOBJ_LOOKUPITER;
49 obj.ipfo_ptr = &iter;
50 obj.ipfo_size = sizeof(iter);
51
52 iter.ili_data = entry;
53 iter.ili_type = IPLT_DSTLIST;
54 iter.ili_otype = IPFLOOKUPITER_NODE;
55 iter.ili_ival = IPFGENITER_LOOKUP;
56 iter.ili_unit = d->ipld_unit;
57 strncpy(iter.ili_name, d->ipld_name, FR_GROUPLEN);
58
59 last = 0;
60 top = NULL;
61 printed = 0;
62 entry = NULL;
63
64 if (d->ipld_nodes > 0) {
65 while (!last && (ioctl(fd, SIOCLOOKUPITER, &obj) == 0)) {
66 if (entry->ipfd_next == NULL)
67 last = 1;
68 entry->ipfd_next = top;
69 top = malloc(entry->ipfd_size);
70 if (top == NULL)
71 break;
72 bcopy(entry, top, entry->ipfd_size);
73 }
74 }
75
76 while (top != NULL) {
77 node = top;
78 (void) printdstlistnode(node, bcopywrap, opts, fields);
79 top = node->ipfd_next;
80 free(node);
81 printed++;
82 }
83
84 if (entry != NULL)
85 free(entry);
86
87 if (printed == 0)
88 putchar(';');
89
90 if ((opts & OPT_DEBUG) == 0)
91 PRINTF(" };\n");
92 return d->ipld_next;
93 }
94