xref: /illumos-gate/usr/src/cmd/ipf/lib/printhash_live.c (revision c43efa7f6b109f90d7f4962df8c0e1a94008d2d1)
1 /*
2  * Copyright (C) 2002 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  *
6  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
7  * Use is subject to license terms.
8  */
9 
10 #include <sys/ioctl.h>
11 #include "ipf.h"
12 #include "netinet/ipl.h"
13 
14 #define	PRINTF	(void)printf
15 #define	FPRINTF	(void)fprintf
16 
17 
18 iphtable_t *printhash_live(hp, fd, name, opts)
19 iphtable_t *hp;
20 int fd;
21 char *name;
22 int opts;
23 {
24 	iphtent_t entry, *top, *node;
25 	ipflookupiter_t iter;
26 	int i, printed = 0, last;
27 	ipfobj_t obj;
28 
29 	if ((name != NULL) && strncmp(name, hp->iph_name, FR_GROUPLEN))
30 		return hp->iph_next;
31 
32 	printhashdata(hp, opts);
33 
34 	if ((opts & OPT_DEBUG) == 0)
35 		PRINTF("\t{");
36 
37 	obj.ipfo_rev = IPFILTER_VERSION;
38 	obj.ipfo_type = IPFOBJ_LOOKUPITER;
39 	obj.ipfo_ptr = &iter;
40 	obj.ipfo_size = sizeof(iter);
41 
42 	iter.ili_data = &entry;
43 	iter.ili_type = IPLT_HASH;
44 	iter.ili_otype = IPFLOOKUPITER_NODE;
45 	iter.ili_ival = IPFGENITER_LOOKUP;
46 	iter.ili_unit = hp->iph_unit;
47 	strncpy(iter.ili_name, hp->iph_name, FR_GROUPLEN);
48 
49 	last = 0;
50 	top = NULL;
51 
52 	while (!last && (ioctl(fd, SIOCLOOKUPITER, &obj) == 0)) {
53 		if (entry.ipe_snext == NULL)
54 			last = 1;
55 		entry.ipe_snext = top;
56 		top = malloc(sizeof(*top));
57 		if (top == NULL)
58 			break;
59 		bcopy(&entry, top, sizeof(entry));
60 	}
61 
62 	while (top != NULL) {
63 		node = top;
64 		(void) printhashnode(hp, node, bcopywrap, opts);
65 		top = node->ipe_snext;
66 		free(node);
67 		printed++;
68 
69                 if ((opts & OPT_DEBUG) == 0)
70                         putchar(';');
71 	}
72 
73 	if (printed == 0)
74 		putchar(';');
75 
76 	if ((opts & OPT_DEBUG) == 0)
77 		PRINTF(" };\n");
78 	return hp->iph_next;
79 }
80