summaryrefslogtreecommitdiff
path: root/sysutils/pftop/patches/patch-ab
blob: e9a582cc08c7b69f2d872d40937ba80c7000dcb6 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
$NetBSD: patch-ab,v 1.3 2005/12/07 14:32:07 joerg Exp $

--- pftop.c.orig	2003-08-05 01:20:39.000000000 +0000
+++ pftop.c
@@ -31,6 +31,11 @@
  */
 #include "config.h"
 
+#ifdef __NetBSD__
+#include <sys/param.h>
+#include <sys/syslimits.h>
+#endif
+
 #include <sys/types.h>
 #include <sys/ioctl.h>
 #include <sys/socket.h>
@@ -54,7 +59,9 @@
 #include <err.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <limits.h>
 #include <netdb.h>
+#include <poll.h>
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -192,9 +199,6 @@ int cachestates = 0;
 
 volatile sig_atomic_t gotsig_close = 0;
 volatile sig_atomic_t gotsig_resize = 0;
-volatile sig_atomic_t gotsig_alarm = 0;
-int need_update = 0;
-int need_sort = 0;
 
 #define FLD_ALIGN_LEFT   0
 #define FLD_ALIGN_RIGHT  1
@@ -506,7 +510,6 @@ tb_end(void)
 
 int
 tbprintf(char *format, ...)
-	GCC_PRINTFLIKE(1,2)       /* defined in curses.h */
 {
 	int len;
 	va_list arg;
@@ -543,6 +546,10 @@ move_horiz(int offset)
 	}
 }
 
+#ifndef MIN
+#define MIN(x,y) ((x) < (y) ? (x) : (y))
+#endif
+
 void
 print_str(int len, const char *str)
 {
@@ -758,7 +765,6 @@ set_curr_view(field_view *v)
 	}
 
 	if ((curr_view != NULL) && (curr_view->mgr != v->mgr)) {
-		gotsig_alarm = 1;
 		if (v->mgr != NULL && v->mgr->select_fn != NULL)
 			v->mgr->select_fn();
 	}
@@ -766,7 +772,6 @@ set_curr_view(field_view *v)
 	curr_view = v;
 	curr_mgr = v->mgr;
 	field_setup();
-	need_update = 1;
 }
 
 void
@@ -849,11 +854,9 @@ update_cache()
 	if (cachestates) {
 		show_field(FLD_SI);
 		show_field(FLD_SP);
-		gotsig_alarm = 1;
 	} else {
 		hide_field(FLD_SI);
 		hide_field(FLD_SP);
-		need_update = 1;
 	}
 	field_setup();
 }
@@ -1212,7 +1215,7 @@ read_states(int dev)
 		ps.ps_buf = (char *) state_buf;
 
 		if (ioctl(dev, DIOCGETSTATES, &ps) < 0) {
-			errx(1, "DIOCGETSTATES");
+			err(1, "DIOCGETSTATES");
 		}
 		num_states = ps.ps_len / sizeof(struct pf_state);
 
@@ -1303,7 +1306,7 @@ print_header(struct pf_status *status)
 		tbprintf(" PAUSED");
 
 	if (rawmode)
-		printf("\n\n%s\n", tmp_buf);
+		printf("\n%s", tmp_buf);
 	else
 		mvprintw(0, 0, "%s", tmp_buf);
 
@@ -1319,7 +1322,10 @@ print_header(struct pf_status *status)
 		len = columns - strlen(tmp_buf);
 		if (len < 0)
 			len = 0;
-		mvprintw(0, len, "%s", tmp_buf);
+		if (rawmode)
+			printf(" %s\n", tmp_buf);
+		else
+			mvprintw(0, len, "%s", tmp_buf);
 	}
 
 	tb_end();
@@ -1645,7 +1651,7 @@ disp_update(int dev)
 		dispstart = 0;
 
 	if (ioctl(dev, DIOCGETSTATUS, &status)) {
-		warnx("DIOCGETSTATUS");
+		warn("DIOCGETSTATUS");
 		return (-1);
 	}
 
@@ -1681,7 +1687,7 @@ read_rules(int dev)
 
 	memset(&pr, 0, sizeof(pr));
 	if (ioctl(dev, DIOCGETRULES, &pr)) {
-		warnx("DIOCGETRULES");
+		warn("DIOCGETRULES");
 		return (-1);
 	}
 
@@ -1704,7 +1710,7 @@ read_rules(int dev)
 	for (nr = 0; nr < num_rules; ++nr) {
 		pr.nr = nr;
 		if (ioctl(dev, DIOCGETRULE, &pr)) {
-			warnx("DIOCGETRULE");
+			warn("DIOCGETRULE");
 			return (-1);
 		}
 		rules[nr] = pr.rule;
@@ -1802,7 +1808,11 @@ tb_print_fromto(struct pf_rule_addr *src
 			 PF_AZERO(PT_MASK(src), AF_INET6))
 			tbprintf("any ");
 		else {
+#if OS_LEVEL > 35
+			if (src->neg)
+#else		
 			if (src->not)
+#endif
 				tbprintf("! ");
 #ifdef HAVE_ADDR_WRAP
 			tb_print_addrw(&src->addr, PT_MASK(src), af);
@@ -1823,7 +1833,11 @@ tb_print_fromto(struct pf_rule_addr *src
 			 PF_AZERO(PT_MASK(dst), AF_INET6))
 			tbprintf("any ");
 		else {
+#if OS_LEVEL > 35
+			if (dst->neg)
+#else
 			if (dst->not)
+#endif
 				tbprintf("! ");
 #ifdef HAVE_ADDR_WRAP
 			tb_print_addrw(&dst->addr, PT_MASK(dst), af);
@@ -1890,8 +1904,13 @@ print_rule(struct pf_rule *pr)
 #ifdef HAVE_RULE_STATES
 	print_fld_size(FLD_STATS, pr->states);
 #endif
+#if OS_LEVEL > 37
+	print_fld_size(FLD_PKTS, pr->packets[0] + pr->packets[1]);
+	print_fld_size(FLD_BYTES, pr->bytes[0] + pr->bytes[1]);
+#else
 	print_fld_size(FLD_PKTS, pr->packets);
 	print_fld_size(FLD_BYTES, pr->bytes);
+#endif
 	print_fld_uint(FLD_RULE, pr->nr);
 	print_fld_str(FLD_DIR, pr->direction == PF_OUT ? "Out" : "In");
 	if (pr->quick)
@@ -2486,12 +2505,6 @@ sig_resize(int signal)
 }
 
 void
-sig_alarm(int signal)
-{
-	gotsig_alarm = 1;
-}
-
-void
 usage()
 {
 	extern char *__progname;
@@ -2544,7 +2557,9 @@ show_help(void)
 	line++;
 	mvprintw(line++, 6, "press any key to continue ...");
 
+	timeout(-1);
 	while (getch() == ERR);
+	timeout(0);
 }
 
 void
@@ -2577,8 +2592,9 @@ setup_term(int maxstates)
 		keypad(stdscr, TRUE);
 		intrflush(stdscr, FALSE);
 
-		halfdelay(10);
+		cbreak();
 		noecho();
+		timeout(0);
 	}
 
 	if (maxstates == 0)
@@ -2597,7 +2613,6 @@ command_set(struct command *cmd)
 		cmdbuf[0] = 0;
 	}
 	curr_cmd = cmd;
-	need_update = 1;
 	return prev;
 }
 
@@ -2618,10 +2633,8 @@ cmd_delay(void)
 {
 	int del;
 	del = atoi(cmdbuf);
-	if (del > 0) {
+	if (del > 0)
 		delay = del;
-		gotsig_alarm = 1;
-	}
 }
 
 void
@@ -2646,7 +2659,6 @@ cmd_keyboard(int ch)
 		if (cmd_len < sizeof(cmdbuf) - 1) {
 			cmdbuf[cmd_len++] = ch;
 			cmdbuf[cmd_len] = 0;
-			need_update = 1;
 		} else
 			beep();
 	}
@@ -2663,10 +2675,9 @@ cmd_keyboard(int ch)
 	case KEY_BACKSPACE:
 	case KEY_DC:
 	case CTRL_H:
-		if (cmd_len > 0) {
+		if (cmd_len > 0)
 			cmdbuf[--cmd_len] = 0;
-			need_update = 1;
-		} else
+		else
 			beep();
 		break;
 	default:
@@ -2687,9 +2698,6 @@ keyboard(void)
 	}
 
 	switch (ch) {
-	case ' ':
-		gotsig_alarm = 1;
-		break;
 	case 'c':
 		cachestates = !cachestates;
 		update_cache();
@@ -2698,25 +2706,21 @@ keyboard(void)
 		/* FALLTHROUGH */
 	case 'h':
 		show_help();
-		need_update = 1;
 		break;
 	case 'n':
 		command_set(&cm_count);
 		break;
 	case 'o':
 		next_order();
-		need_sort = 1;
 		break;
 	case 'p':
 		paused = !paused;
-		gotsig_alarm = 1;
 		break;
 	case 'q':
 		gotsig_close = 1;
 		break;
 	case 'r':
 		sortdir *= -1;
-		need_sort = 1;
 		break;
 	case 's':
 		command_set(&cm_delay);
@@ -2737,49 +2741,40 @@ keyboard(void)
 		/* FALLTHROUGH */
 	case CTRL_N:
 		dispstart++;
-		need_update = 1;
 		break;
 	case KEY_UP:
 		/* FALLTHROUGH */
 	case CTRL_P:
 		dispstart--;
-		need_update = 1;
 		break;
 	case KEY_NPAGE:
 		/* FALLTHROUGH */
 	case CTRL_V:
 		dispstart += maxprint;
-		need_update = 1;
 		break;
 	case KEY_PPAGE:
 		/* FALLTHROUGH */
 	case META_V:
 		dispstart -= maxprint;
-		need_update = 1;
 		break;
 	case KEY_HOME:
 		/* FALLTHROUGH */
 	case CTRL_A:
 		dispstart = 0;
-		need_update = 1;
 		break;
 	case KEY_END:
 		/* FALLTHROUGH */
 	case CTRL_E:
 		dispstart = num_disp;
-		need_update = 1;
 		break;
 	case CTRL_L:
 		clear();
-		need_update = 1;
 		break;
 	default:
 		break;
 	}
 
-	if (set_order_hotkey(ch))
-		need_sort = 1;
-	else
+	if (!set_order_hotkey(ch))
 		set_view_hotkey(ch);
 }
 
@@ -2790,6 +2785,7 @@ main(int argc, char *argv[])
 	extern int optind;
 
 	struct pf_status status;
+	struct pollfd set[1];
 
 	char *orderstr = NULL;
 	char *viewstr = NULL;
@@ -2872,7 +2868,6 @@ main(int argc, char *argv[])
 	signal(SIGINT, sig_close);
 	signal(SIGQUIT, sig_close);
 	signal(SIGWINCH, sig_resize);
-	signal(SIGALRM, sig_alarm);
 
 #ifdef HAVE_DEVICE_RO
 	dev = open("/dev/pf", O_RDONLY);
@@ -2884,7 +2879,7 @@ main(int argc, char *argv[])
 
 	/* preallocate existing states if possible */
 	if (ioctl(dev, DIOCGETSTATUS, &status)) {
-		warnx("DIOCGETSTATUS");
+		warn("DIOCGETSTATUS");
 		alloc_buf(0);
 	} else
 		alloc_buf(status.states);
@@ -2910,36 +2905,7 @@ main(int argc, char *argv[])
 	if (rawmode && countmax == 0)
 		countmax = 1;
 
-	gotsig_alarm = 1;
 	for (;;) {
-		if (gotsig_alarm) {
-			read_view(dev);
-			need_sort = 1;
-			gotsig_alarm = 0;
-			alarm(delay);
-		}
-
-		if (need_sort) {
-			sort_view();
-			need_sort = 0;
-			need_update = 1;
-			
-			/* XXX if sort took too long */
-			if (gotsig_alarm) {
-				gotsig_alarm = 0;
-				alarm(delay);
-			}
-		}
-
-		if (need_update) {
-			erase();
-			disp_update(dev);
-			end_page();
-			need_update = 0;
-			if (countmax && ++count >= countmax)
-				break;
-		}
-
 		if (gotsig_close)
 			break;
 		if (gotsig_resize) {
@@ -2947,12 +2913,30 @@ main(int argc, char *argv[])
 				endwin();
 			setup_term(maxstates);
 			gotsig_resize = 0;
-			need_update = 1;
 		}
 
-		if (interactive && need_update == 0)
-			keyboard();
-		else if (interactive == 0)
+		read_view(dev);
+		sort_view();
+
+		if (rawmode == 0)
+			erase();
+		disp_update(dev);
+		end_page();
+
+		if (countmax && ++count >= countmax)
+			break;
+
+		if (interactive) {
+			set[0].fd = STDIN_FILENO;
+			set[0].events = POLLIN;
+			if (poll(set, 1, delay * 1000) == -1) {
+				if (errno == EINTR)
+					continue;
+				break;
+			}
+			if ((set[0].revents & POLLIN) != 0)
+				keyboard();
+		} else
 			sleep(delay);
 	}