1*c50c2f6fSdarrenr /*	$NetBSD: printdstl_live.c,v 1.1.1.2 2012/07/22 13:44:40 darrenr Exp $	*/
226945a25Schristos 
326945a25Schristos /*
4*c50c2f6fSdarrenr  * Copyright (C) 2012 by Darren Reed.
526945a25Schristos  *
626945a25Schristos  * See the IPFILTER.LICENCE file for details on licencing.
726945a25Schristos  */
826945a25Schristos 
926945a25Schristos #include <sys/ioctl.h>
1026945a25Schristos #include "ipf.h"
1126945a25Schristos #include "netinet/ipl.h"
1226945a25Schristos 
1326945a25Schristos 
1426945a25Schristos /*
1526945a25Schristos  * Because the ipf_dstnode_t can vary in size because of the interface name,
1626945a25Schristos  * the size may be larger than just sizeof().
1726945a25Schristos  */
1826945a25Schristos ippool_dst_t *
printdstl_live(d,fd,name,opts,fields)1926945a25Schristos printdstl_live(d, fd, name, opts, fields)
2026945a25Schristos 	ippool_dst_t *d;
2126945a25Schristos 	int fd;
2226945a25Schristos 	char *name;
2326945a25Schristos 	int opts;
2426945a25Schristos 	wordtab_t *fields;
2526945a25Schristos {
26*c50c2f6fSdarrenr 	ipf_dstnode_t *entry, *zero;
2726945a25Schristos 	ipflookupiter_t iter;
2826945a25Schristos 	int printed, last;
2926945a25Schristos 	ipfobj_t obj;
3026945a25Schristos 
3126945a25Schristos 	if ((name != NULL) && strncmp(name, d->ipld_name, FR_GROUPLEN))
3226945a25Schristos 		return d->ipld_next;
3326945a25Schristos 
3426945a25Schristos 	entry = calloc(1, sizeof(*entry) + 64);
3526945a25Schristos 	if (entry == NULL)
3626945a25Schristos 		return d->ipld_next;
37*c50c2f6fSdarrenr 	zero = calloc(1, sizeof(*zero) + 64);
38*c50c2f6fSdarrenr 	if (zero == NULL) {
39*c50c2f6fSdarrenr 		free(entry);
40*c50c2f6fSdarrenr 		return d->ipld_next;
41*c50c2f6fSdarrenr 	}
4226945a25Schristos 
4326945a25Schristos 	if (fields == NULL)
4426945a25Schristos 		printdstlistdata(d, opts);
4526945a25Schristos 
4626945a25Schristos 	if ((d->ipld_flags & IPHASH_DELETE) != 0)
4726945a25Schristos 		PRINTF("# ");
4826945a25Schristos 
4926945a25Schristos 	if ((opts & OPT_DEBUG) == 0)
5026945a25Schristos 		PRINTF("\t{");
5126945a25Schristos 
5226945a25Schristos 	obj.ipfo_rev = IPFILTER_VERSION;
5326945a25Schristos 	obj.ipfo_type = IPFOBJ_LOOKUPITER;
5426945a25Schristos 	obj.ipfo_ptr = &iter;
5526945a25Schristos 	obj.ipfo_size = sizeof(iter);
5626945a25Schristos 
5726945a25Schristos 	iter.ili_data = entry;
5826945a25Schristos 	iter.ili_type = IPLT_DSTLIST;
5926945a25Schristos 	iter.ili_otype = IPFLOOKUPITER_NODE;
6026945a25Schristos 	iter.ili_ival = IPFGENITER_LOOKUP;
6126945a25Schristos 	iter.ili_unit = d->ipld_unit;
6226945a25Schristos 	strncpy(iter.ili_name, d->ipld_name, FR_GROUPLEN);
6326945a25Schristos 
6426945a25Schristos 	last = 0;
6526945a25Schristos 	printed = 0;
6626945a25Schristos 
6726945a25Schristos 	while (!last && (ioctl(fd, SIOCLOOKUPITER, &obj) == 0)) {
6826945a25Schristos 		if (entry->ipfd_next == NULL)
6926945a25Schristos 			last = 1;
70*c50c2f6fSdarrenr 		if (bcmp((char *)zero, (char *)entry, sizeof(*zero)) == 0)
7126945a25Schristos 			break;
72*c50c2f6fSdarrenr 		(void) printdstlistnode(entry, bcopywrap, opts, fields);
7326945a25Schristos 		printed++;
7426945a25Schristos 	}
7526945a25Schristos 
76*c50c2f6fSdarrenr 	(void) ioctl(fd, SIOCIPFDELTOK, &iter.ili_key);
7726945a25Schristos 	free(entry);
78*c50c2f6fSdarrenr 	free(zero);
7926945a25Schristos 
8026945a25Schristos 	if (printed == 0)
8126945a25Schristos 		putchar(';');
8226945a25Schristos 
8326945a25Schristos 	if ((opts & OPT_DEBUG) == 0)
8426945a25Schristos 		PRINTF(" };\n");
8526945a25Schristos 	return d->ipld_next;
8626945a25Schristos }
87