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