Home | History | Annotate | Line # | Download | only in lib
printdstl_live.c revision 1.1
      1  1.1  christos /*	$NetBSD: printdstl_live.c,v 1.1 2012/03/23 21:20:09 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (C) 2010 by Darren Reed.
      5  1.1  christos  *
      6  1.1  christos  * See the IPFILTER.LICENCE file for details on licencing.
      7  1.1  christos  */
      8  1.1  christos 
      9  1.1  christos #include <sys/ioctl.h>
     10  1.1  christos #include "ipf.h"
     11  1.1  christos #include "netinet/ipl.h"
     12  1.1  christos 
     13  1.1  christos 
     14  1.1  christos /*
     15  1.1  christos  * Because the ipf_dstnode_t can vary in size because of the interface name,
     16  1.1  christos  * the size may be larger than just sizeof().
     17  1.1  christos  */
     18  1.1  christos ippool_dst_t *
     19  1.1  christos printdstl_live(d, fd, name, opts, fields)
     20  1.1  christos 	ippool_dst_t *d;
     21  1.1  christos 	int fd;
     22  1.1  christos 	char *name;
     23  1.1  christos 	int opts;
     24  1.1  christos 	wordtab_t *fields;
     25  1.1  christos {
     26  1.1  christos 	ipf_dstnode_t *entry, *top, *node;
     27  1.1  christos 	ipflookupiter_t iter;
     28  1.1  christos 	int printed, last;
     29  1.1  christos 	ipfobj_t obj;
     30  1.1  christos 
     31  1.1  christos 	if ((name != NULL) && strncmp(name, d->ipld_name, FR_GROUPLEN))
     32  1.1  christos 		return d->ipld_next;
     33  1.1  christos 
     34  1.1  christos 	entry = calloc(1, sizeof(*entry) + 64);
     35  1.1  christos 	if (entry == NULL)
     36  1.1  christos 		return d->ipld_next;
     37  1.1  christos 
     38  1.1  christos 	if (fields == NULL)
     39  1.1  christos 		printdstlistdata(d, opts);
     40  1.1  christos 
     41  1.1  christos 	if ((d->ipld_flags & IPHASH_DELETE) != 0)
     42  1.1  christos 		PRINTF("# ");
     43  1.1  christos 
     44  1.1  christos 	if ((opts & OPT_DEBUG) == 0)
     45  1.1  christos 		PRINTF("\t{");
     46  1.1  christos 
     47  1.1  christos 	obj.ipfo_rev = IPFILTER_VERSION;
     48  1.1  christos 	obj.ipfo_type = IPFOBJ_LOOKUPITER;
     49  1.1  christos 	obj.ipfo_ptr = &iter;
     50  1.1  christos 	obj.ipfo_size = sizeof(iter);
     51  1.1  christos 
     52  1.1  christos 	iter.ili_data = entry;
     53  1.1  christos 	iter.ili_type = IPLT_DSTLIST;
     54  1.1  christos 	iter.ili_otype = IPFLOOKUPITER_NODE;
     55  1.1  christos 	iter.ili_ival = IPFGENITER_LOOKUP;
     56  1.1  christos 	iter.ili_unit = d->ipld_unit;
     57  1.1  christos 	strncpy(iter.ili_name, d->ipld_name, FR_GROUPLEN);
     58  1.1  christos 
     59  1.1  christos 	last = 0;
     60  1.1  christos 	top = NULL;
     61  1.1  christos 	printed = 0;
     62  1.1  christos 	entry = NULL;
     63  1.1  christos 
     64  1.1  christos 	if (d->ipld_nodes > 0) {
     65  1.1  christos 		while (!last && (ioctl(fd, SIOCLOOKUPITER, &obj) == 0)) {
     66  1.1  christos 			if (entry->ipfd_next == NULL)
     67  1.1  christos 				last = 1;
     68  1.1  christos 			entry->ipfd_next = top;
     69  1.1  christos 			top = malloc(entry->ipfd_size);
     70  1.1  christos 			if (top == NULL)
     71  1.1  christos 				break;
     72  1.1  christos 			bcopy(entry, top, entry->ipfd_size);
     73  1.1  christos 		}
     74  1.1  christos 	}
     75  1.1  christos 
     76  1.1  christos 	while (top != NULL) {
     77  1.1  christos 		node = top;
     78  1.1  christos 		(void) printdstlistnode(node, bcopywrap, opts, fields);
     79  1.1  christos 		top = node->ipfd_next;
     80  1.1  christos 		free(node);
     81  1.1  christos 		printed++;
     82  1.1  christos 	}
     83  1.1  christos 
     84  1.1  christos 	if (entry != NULL)
     85  1.1  christos 		free(entry);
     86  1.1  christos 
     87  1.1  christos 	if (printed == 0)
     88  1.1  christos 		putchar(';');
     89  1.1  christos 
     90  1.1  christos 	if ((opts & OPT_DEBUG) == 0)
     91  1.1  christos 		PRINTF(" };\n");
     92  1.1  christos 	return d->ipld_next;
     93  1.1  christos }
     94