load_pool.c revision 153881
1/*	$FreeBSD: head/contrib/ipfilter/lib/load_pool.c 153881 2005-12-30 11:52:26Z guido $	*/
2
3/*
4 * Copyright (C) 2002 by Darren Reed.
5 *
6 * See the IPFILTER.LICENCE file for details on licencing.
7 *
8 * $Id: load_pool.c,v 1.14.2.3 2005/11/13 15:41:13 darrenr Exp $
9 */
10
11#include <fcntl.h>
12#include <sys/ioctl.h>
13#include "ipf.h"
14#include "netinet/ip_lookup.h"
15#include "netinet/ip_pool.h"
16
17static int poolfd = -1;
18
19
20int load_pool(plp, iocfunc)
21ip_pool_t *plp;
22ioctlfunc_t iocfunc;
23{
24	iplookupop_t op;
25	ip_pool_node_t *a;
26	ip_pool_t pool;
27
28	if ((poolfd == -1) && ((opts & OPT_DONOTHING) == 0))
29		poolfd = open(IPLOOKUP_NAME, O_RDWR);
30	if ((poolfd == -1) && ((opts & OPT_DONOTHING) == 0))
31		return -1;
32
33	op.iplo_unit = plp->ipo_unit;
34	op.iplo_type = IPLT_POOL;
35	op.iplo_arg = 0;
36	strncpy(op.iplo_name, plp->ipo_name, sizeof(op.iplo_name));
37	op.iplo_size = sizeof(pool);
38	op.iplo_struct = &pool;
39	bzero((char *)&pool, sizeof(pool));
40	strncpy(pool.ipo_name, plp->ipo_name, sizeof(pool.ipo_name));
41	if (plp->ipo_name[0] == '\0')
42		op.iplo_arg |= IPOOL_ANON;
43
44	if ((opts & OPT_REMOVE) == 0) {
45		if ((*iocfunc)(poolfd, SIOCLOOKUPADDTABLE, &op))
46			if ((opts & OPT_DONOTHING) == 0) {
47				perror("load_pool:SIOCLOOKUPADDTABLE");
48				return -1;
49			}
50	}
51
52	if (op.iplo_arg & IPOOL_ANON)
53		strncpy(pool.ipo_name, op.iplo_name, sizeof(pool.ipo_name));
54
55	if ((opts & OPT_VERBOSE) != 0) {
56		pool.ipo_list = plp->ipo_list;
57		printpool(&pool, bcopywrap, pool.ipo_name, opts);
58		pool.ipo_list = NULL;
59	}
60
61	for (a = plp->ipo_list; a != NULL; a = a->ipn_next)
62		load_poolnode(plp->ipo_unit, pool.ipo_name, a, iocfunc);
63
64	if ((opts & OPT_REMOVE) != 0) {
65		if ((*iocfunc)(poolfd, SIOCLOOKUPDELTABLE, &op))
66			if ((opts & OPT_DONOTHING) == 0) {
67				perror("load_pool:SIOCLOOKUPDELTABLE");
68				return -1;
69			}
70	}
71	return 0;
72}
73