1
2/*
3 * Copyright (C) 2012 by Darren Reed.
4 *
5 * See the IPFILTER.LICENCE file for details on licencing.
6 *
7 * $Id$
8 */
9
10#include "ipf.h"
11#include "netinet/ipl.h"
12#include <sys/ioctl.h>
13
14void ipf_dotuning(int fd, char *tuneargs, ioctlfunc_t iocfn)
15{
16	ipfobj_t obj;
17	ipftune_t tu;
18	char *s, *t;
19
20	bzero((char *)&tu, sizeof(tu));
21	obj.ipfo_rev = IPFILTER_VERSION;
22	obj.ipfo_size = sizeof(tu);
23	obj.ipfo_ptr = (void *)&tu;
24	obj.ipfo_type = IPFOBJ_TUNEABLE;
25
26	for (s = strtok(tuneargs, ","); s != NULL; s = strtok(NULL, ",")) {
27		if (!strcmp(s, "list")) {
28			while (1) {
29				if ((*iocfn)(fd, SIOCIPFGETNEXT, &obj) == -1) {
30					ipf_perror_fd(fd, iocfn,
31						      "ioctl(SIOCIPFGETNEXT)");
32					break;
33				}
34				if (tu.ipft_cookie == NULL)
35					break;
36
37				tu.ipft_name[sizeof(tu.ipft_name) - 1] = '\0';
38				printtunable(&tu);
39			}
40		} else if ((t = strchr(s, '=')) != NULL) {
41			tu.ipft_cookie = NULL;
42			*t++ = '\0';
43			strncpy(tu.ipft_name, s, sizeof(tu.ipft_name));
44			if (sscanf(t, "%lu", &tu.ipft_vlong) == 1) {
45				if ((*iocfn)(fd, SIOCIPFSET, &obj) == -1) {
46					ipf_perror_fd(fd, iocfn,
47						      "ioctl(SIOCIPFSET)");
48					return;
49				}
50			} else {
51				fprintf(stderr, "invalid value '%s'\n", s);
52				return;
53			}
54		} else {
55			tu.ipft_cookie = NULL;
56			strncpy(tu.ipft_name, s, sizeof(tu.ipft_name));
57			if ((*iocfn)(fd, SIOCIPFGET, &obj) == -1) {
58				ipf_perror_fd(fd, iocfn, "ioctl(SIOCIPFGET)");
59				return;
60			}
61			if (tu.ipft_cookie == NULL) {
62				fprintf(stderr, "Null cookie for %s\n", s);
63				return;
64			}
65
66			tu.ipft_name[sizeof(tu.ipft_name) - 1] = '\0';
67			printtunable(&tu);
68		}
69	}
70}
71