ipf_dotuning.c revision 147547
1/*	$FreeBSD: head/contrib/ipfilter/lib/ipf_dotuning.c 147547 2005-06-23 14:19:02Z darrenr $	*/
2
3#include "ipf.h"
4#include "netinet/ipl.h"
5#include <sys/ioctl.h>
6
7void ipf_dotuning(fd, tuneargs, iocfn)
8int fd;
9char *tuneargs;
10ioctlfunc_t iocfn;
11{
12	ipfobj_t obj;
13	ipftune_t tu;
14	char *s, *t;
15
16	bzero((char *)&tu, sizeof(tu));
17	obj.ipfo_rev = IPFILTER_VERSION;
18	obj.ipfo_size = sizeof(tu);;
19	obj.ipfo_ptr = (void *)&tu;
20	obj.ipfo_type = IPFOBJ_TUNEABLE;
21
22	for (s = strtok(tuneargs, ","); s != NULL; s = strtok(NULL, ",")) {
23		if (!strcmp(s, "list")) {
24			while (1) {
25				if ((*iocfn)(fd, SIOCIPFGETNEXT, &obj) == -1) {
26					perror("ioctl(SIOCIPFGETNEXT)");
27					break;
28				}
29				if (tu.ipft_cookie == NULL)
30					break;
31
32				tu.ipft_name[sizeof(tu.ipft_name) - 1] = '\0';
33				printtunable(&tu);
34			}
35		} else if ((t = strchr(s, '=')) != NULL) {
36			tu.ipft_cookie = NULL;
37			*t++ = '\0';
38			strncpy(tu.ipft_name, s, sizeof(tu.ipft_name));
39			if (sscanf(t, "%lu", &tu.ipft_vlong) == 1) {
40				if ((*iocfn)(fd, SIOCIPFSET, &obj) == -1) {
41					perror("ioctl(SIOCIPFSET)");
42					return;
43				}
44			} else {
45				fprintf(stderr, "invalid value '%s'\n", s);
46				return;
47			}
48		} else {
49			tu.ipft_cookie = NULL;
50			strncpy(tu.ipft_name, s, sizeof(tu.ipft_name));
51			if ((*iocfn)(fd, SIOCIPFGET, &obj) == -1) {
52				perror("ioctl(SIOCIPFGET)");
53				return;
54			}
55			if (tu.ipft_cookie == NULL) {
56				fprintf(stderr, "Null cookie for %s\n", s);
57				return;
58			}
59
60			tu.ipft_name[sizeof(tu.ipft_name) - 1] = '\0';
61			printtunable(&tu);
62		}
63	}
64}
65