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