summaryrefslogtreecommitdiffstats
path: root/contrib/ipfilter/lib/printipfexpr.c
blob: 06b987e07575155dd9aafd5981ecbcf2be94ed77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "ipf.h"

static void printport __P((int *));
static void printhosts __P((int *));
static void printsingle __P((int *));
#ifdef USE_INET6
static void printhostsv6 __P((int *));
#endif

void
printipfexpr(array)
	int *array;
{
	int i, nelems, j, not;
	ipfexp_t *ipfe;

	nelems = array[0];

	for (i = 1; i < nelems; ) {
		ipfe = (ipfexp_t *)(array + i);
		if (ipfe->ipfe_cmd == IPF_EXP_END)
			break;

		not = ipfe->ipfe_not;

		switch (ipfe->ipfe_cmd)
		{
		case IPF_EXP_IP_ADDR :
			PRINTF("ip.addr %s= ", not ? "!" : "");
			printhosts(array + i);
			break;

		case IPF_EXP_IP_PR :
			PRINTF("ip.p %s= ", not ? "!" : "");
			printsingle(array + i);
			break;

		case IPF_EXP_IP_SRCADDR :
			PRINTF("ip.src %s= ", not ? "!" : "");
			printhosts(array + i);
			break;

		case IPF_EXP_IP_DSTADDR :
			PRINTF("ip.dst %s= ", not ? "!" : "");
			printhosts(array + i);
			break;

		case IPF_EXP_TCP_PORT :
			PRINTF("tcp.port %s= ", not ? "!" : "");
			printport(array + i);
			break;

		case IPF_EXP_TCP_DPORT :
			PRINTF("tcp.dport %s= ", not ? "!" : "");
			printport(array + i);
			break;

		case IPF_EXP_TCP_SPORT :
			PRINTF("tcp.sport %s= ", not ? "!" : "");
			printport(array + i);
			break;

		case IPF_EXP_TCP_FLAGS :
			PRINTF("tcp.flags %s= ", not ? "!" : "");

			for (j = 0; j < ipfe->ipfe_narg; ) {
				printtcpflags(array[i + 4], array[i + 5]);
				j += 2;
				if (j < array[4])
					putchar(',');
			}
			break;

		case IPF_EXP_UDP_PORT :
			PRINTF("udp.port %s= ", not ? "!" : "");
			printport(array + i);
			break;

		case IPF_EXP_UDP_DPORT :
			PRINTF("udp.dport %s= ", not ? "!" : "");
			printport(array + i);
			break;

		case IPF_EXP_UDP_SPORT :
			PRINTF("udp.sport %s= ", not ? "!" : "");
			printport(array + i);
			break;

		case IPF_EXP_IDLE_GT :
			PRINTF("idle-gt %s= ", not ? "!" : "");
			printsingle(array + i);
			break;

		case IPF_EXP_TCP_STATE :
			PRINTF("tcp-state %s= ", not ? "!" : "");
			printsingle(array + i);
			break;

#ifdef USE_INET6
		case IPF_EXP_IP6_ADDR :
			PRINTF("ip6.addr %s= ", not ? "!" : "");
			printhostsv6(array + i);
			break;

		case IPF_EXP_IP6_SRCADDR :
			PRINTF("ip6.src %s= ", not ? "!" : "");
			printhostsv6(array + i);
			break;

		case IPF_EXP_IP6_DSTADDR :
			PRINTF("ip6.dst %s= ", not ? "!" : "");
			printhostsv6(array + i);
			break;
#endif

		case IPF_EXP_END :
			break;

		default :
			PRINTF("#%#x,len=%d;",
			       ipfe->ipfe_cmd, ipfe->ipfe_narg);
		}

		if (array[i] != IPF_EXP_END)
			putchar(';');

		i += ipfe->ipfe_size;
		if (array[i] != IPF_EXP_END)
			putchar(' ');
	}
}


static void
printsingle(array)
	int *array;
{
	ipfexp_t *ipfe = (ipfexp_t *)array;
	int i;

	for (i = 0; i < ipfe->ipfe_narg; ) {
		PRINTF("%d", array[i + 4]);
		i++;
		if (i < ipfe->ipfe_narg)
			putchar(',');
	}
}


static void
printport(array)
	int *array;
{
	ipfexp_t *ipfe = (ipfexp_t *)array;
	int i;

	for (i = 0; i < ipfe->ipfe_narg; ) {
		PRINTF("%d", ntohs(array[i + 4]));
		i++;
		if (i < ipfe->ipfe_narg)
			putchar(',');
	}
}


static void
printhosts(array)
	int *array;
{
	ipfexp_t *ipfe = (ipfexp_t *)array;
	int i, j;

	for (i = 0, j = 0; i < ipfe->ipfe_narg; j++) {
		printhostmask(AF_INET, (u_32_t *)ipfe->ipfe_arg0 + j * 2,
			      (u_32_t *)ipfe->ipfe_arg0 + j * 2 + 1);
		i += 2;
		if (i < ipfe->ipfe_narg)
			putchar(',');
	}
}


#ifdef USE_INET6
static void
printhostsv6(array)
	int *array;
{
	ipfexp_t *ipfe = (ipfexp_t *)array;
	int i, j;

	for (i = 4, j= 0; i < ipfe->ipfe_size; j++) {
		printhostmask(AF_INET6, (u_32_t *)ipfe->ipfe_arg0 + j * 8,
			      (u_32_t *)ipfe->ipfe_arg0 + j * 8 + 4);
		i += 8;
		if (i < ipfe->ipfe_size)
			putchar(',');
	}
}
#endif
OpenPOWER on IntegriCloud