]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/ipfilter/lib/printipfexpr.c
Copy head (r256279) to stable/10 as part of the 10.0-RELEASE cycle.
[FreeBSD/stable/10.git] / contrib / ipfilter / lib / printipfexpr.c
1 #include "ipf.h"
2
3 static void printport __P((int *));
4 static void printhosts __P((int *));
5 static void printsingle __P((int *));
6 static void printhostsv6 __P((int *));
7
8 void
9 printipfexpr(array)
10         int *array;
11 {
12         int i, nelems, j, not;
13         ipfexp_t *ipfe;
14
15         nelems = array[0];
16
17         for (i = 1; i < nelems; ) {
18                 ipfe = (ipfexp_t *)(array + i);
19                 if (ipfe->ipfe_cmd == IPF_EXP_END)
20                         break;
21
22                 not = ipfe->ipfe_not;
23
24                 switch (ipfe->ipfe_cmd)
25                 {
26                 case IPF_EXP_IP_ADDR :
27                         PRINTF("ip.addr %s= ", not ? "!" : "");
28                         printhosts(array + i);
29                         break;
30
31                 case IPF_EXP_IP_PR :
32                         PRINTF("ip.p %s= ", not ? "!" : "");
33                         printsingle(array + i);
34                         break;
35
36                 case IPF_EXP_IP_SRCADDR :
37                         PRINTF("ip.src %s= ", not ? "!" : "");
38                         printhosts(array + i);
39                         break;
40
41                 case IPF_EXP_IP_DSTADDR :
42                         PRINTF("ip.dst %s= ", not ? "!" : "");
43                         printhosts(array + i);
44                         break;
45
46                 case IPF_EXP_TCP_PORT :
47                         PRINTF("tcp.port %s= ", not ? "!" : "");
48                         printport(array + i);
49                         break;
50
51                 case IPF_EXP_TCP_DPORT :
52                         PRINTF("tcp.dport %s= ", not ? "!" : "");
53                         printport(array + i);
54                         break;
55
56                 case IPF_EXP_TCP_SPORT :
57                         PRINTF("tcp.sport %s= ", not ? "!" : "");
58                         printport(array + i);
59                         break;
60
61                 case IPF_EXP_TCP_FLAGS :
62                         PRINTF("tcp.flags %s= ", not ? "!" : "");
63
64                         for (j = 0; j < ipfe->ipfe_narg; ) {
65                                 printtcpflags(array[i + 4], array[i + 5]);
66                                 j += 2;
67                                 if (j < array[4])
68                                         putchar(',');
69                         }
70                         break;
71
72                 case IPF_EXP_UDP_PORT :
73                         PRINTF("udp.port %s= ", not ? "!" : "");
74                         printport(array + i);
75                         break;
76
77                 case IPF_EXP_UDP_DPORT :
78                         PRINTF("udp.dport %s= ", not ? "!" : "");
79                         printport(array + i);
80                         break;
81
82                 case IPF_EXP_UDP_SPORT :
83                         PRINTF("udp.sport %s= ", not ? "!" : "");
84                         printport(array + i);
85                         break;
86
87                 case IPF_EXP_IDLE_GT :
88                         PRINTF("idle-gt %s= ", not ? "!" : "");
89                         printsingle(array + i);
90                         break;
91
92                 case IPF_EXP_TCP_STATE :
93                         PRINTF("tcp-state %s= ", not ? "!" : "");
94                         printsingle(array + i);
95                         break;
96
97 #ifdef USE_INET6
98                 case IPF_EXP_IP6_ADDR :
99                         PRINTF("ip6.addr %s= ", not ? "!" : "");
100                         printhostsv6(array + i);
101                         break;
102
103                 case IPF_EXP_IP6_SRCADDR :
104                         PRINTF("ip6.src %s= ", not ? "!" : "");
105                         printhostsv6(array + i);
106                         break;
107
108                 case IPF_EXP_IP6_DSTADDR :
109                         PRINTF("ip6.dst %s= ", not ? "!" : "");
110                         printhostsv6(array + i);
111                         break;
112 #endif
113
114                 case IPF_EXP_END :
115                         break;
116
117                 default :
118                         PRINTF("#%#x,len=%d;",
119                                ipfe->ipfe_cmd, ipfe->ipfe_narg);
120                 }
121
122                 if (array[i] != IPF_EXP_END)
123                         putchar(';');
124
125                 i += ipfe->ipfe_size;
126                 if (array[i] != IPF_EXP_END)
127                         putchar(' ');
128         }
129 }
130
131
132 static void
133 printsingle(array)
134         int *array;
135 {
136         ipfexp_t *ipfe = (ipfexp_t *)array;
137         int i;
138
139         for (i = 0; i < ipfe->ipfe_narg; ) {
140                 PRINTF("%d", array[i + 4]);
141                 i++;
142                 if (i < ipfe->ipfe_narg)
143                         putchar(',');
144         }
145 }
146
147
148 static void
149 printport(array)
150         int *array;
151 {
152         ipfexp_t *ipfe = (ipfexp_t *)array;
153         int i;
154
155         for (i = 0; i < ipfe->ipfe_narg; ) {
156                 PRINTF("%d", ntohs(array[i + 4]));
157                 i++;
158                 if (i < ipfe->ipfe_narg)
159                         putchar(',');
160         }
161 }
162
163
164 static void
165 printhosts(array)
166         int *array;
167 {
168         ipfexp_t *ipfe = (ipfexp_t *)array;
169         int i, j;
170
171         for (i = 0, j = 0; i < ipfe->ipfe_narg; j++) {
172                 printhostmask(AF_INET, (u_32_t *)ipfe->ipfe_arg0 + j * 2,
173                               (u_32_t *)ipfe->ipfe_arg0 + j * 2 + 1);
174                 i += 2;
175                 if (i < ipfe->ipfe_narg)
176                         putchar(',');
177         }
178 }
179
180
181 #ifdef USE_INET6
182 static void
183 printhostsv6(array)
184         int *array;
185 {
186         ipfexp_t *ipfe = (ipfexp_t *)array;
187         int i, j;
188
189         for (i = 4, j= 0; i < ipfe->ipfe_size; j++) {
190                 printhostmask(AF_INET6, (u_32_t *)ipfe->ipfe_arg0 + j * 8,
191                               (u_32_t *)ipfe->ipfe_arg0 + j * 8 + 4);
192                 i += 8;
193                 if (i < ipfe->ipfe_size)
194                         putchar(',');
195         }
196 }
197 #endif