trace.c revision 1.14 1 1.14 thorpej /* $NetBSD: trace.c,v 1.14 1996/08/10 01:30:15 thorpej Exp $ */
2 1.10 cgd
3 1.1 cgd /*
4 1.5 mycroft * Copyright (c) 1983, 1988, 1993
5 1.5 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.14 thorpej #if !defined(lint) && !defined(sgi)
37 1.10 cgd #if 0
38 1.10 cgd static char sccsid[] = "@(#)trace.c 8.1 (Berkeley) 6/5/93";
39 1.10 cgd #else
40 1.14 thorpej static char rcsid[] = "$NetBSD: trace.c,v 1.14 1996/08/10 01:30:15 thorpej Exp $";
41 1.10 cgd #endif
42 1.1 cgd #endif /* not lint */
43 1.1 cgd
44 1.1 cgd #define RIPCMDS
45 1.1 cgd #include "defs.h"
46 1.14 thorpej #include "pathnames.h"
47 1.1 cgd #include <sys/stat.h>
48 1.14 thorpej #include <sys/signal.h>
49 1.1 cgd #include <fcntl.h>
50 1.14 thorpej
51 1.14 thorpej
52 1.14 thorpej #ifdef sgi
53 1.14 thorpej /* use *stat64 for files on large filesystems */
54 1.14 thorpej #define stat stat64
55 1.14 thorpej #endif
56 1.1 cgd
57 1.1 cgd #define NRECORDS 50 /* size of circular trace buffer */
58 1.14 thorpej
59 1.14 thorpej u_int tracelevel, new_tracelevel;
60 1.14 thorpej FILE *ftrace = stdout; /* output trace file */
61 1.14 thorpej static char *tracelevel_pat = "%s\n";
62 1.14 thorpej
63 1.14 thorpej char savetracename[MAXPATHLEN+1];
64 1.14 thorpej
65 1.14 thorpej
66 1.14 thorpej /* convert IP address to a string, but not into a single buffer
67 1.14 thorpej */
68 1.14 thorpej char *
69 1.14 thorpej naddr_ntoa(naddr a)
70 1.14 thorpej {
71 1.14 thorpej #define NUM_BUFS 4
72 1.14 thorpej static int i;
73 1.14 thorpej static struct {
74 1.14 thorpej char str[16]; /* xxx.xxx.xxx.xxx\0 */
75 1.14 thorpej } bufs[NUM_BUFS];
76 1.14 thorpej struct in_addr addr;
77 1.14 thorpej char *s;
78 1.14 thorpej
79 1.14 thorpej addr.s_addr = a;
80 1.14 thorpej s = strcpy(bufs[i].str, inet_ntoa(addr));
81 1.14 thorpej i = (i+1) % NUM_BUFS;
82 1.14 thorpej return s;
83 1.14 thorpej }
84 1.14 thorpej
85 1.14 thorpej
86 1.14 thorpej char *
87 1.14 thorpej saddr_ntoa(struct sockaddr *sa)
88 1.14 thorpej {
89 1.14 thorpej return (sa == 0) ? "?" : naddr_ntoa(S_ADDR(sa));
90 1.14 thorpej }
91 1.14 thorpej
92 1.14 thorpej
93 1.14 thorpej static char *
94 1.14 thorpej ts(time_t secs) {
95 1.14 thorpej static char s[20];
96 1.14 thorpej
97 1.14 thorpej secs += epoch.tv_sec;
98 1.14 thorpej #ifdef sgi
99 1.14 thorpej (void)cftime(s, "%T", &secs);
100 1.14 thorpej #else
101 1.14 thorpej bcopy(ctime(&secs)+11, s, 8);
102 1.14 thorpej s[8] = '\0';
103 1.1 cgd #endif
104 1.14 thorpej return s;
105 1.14 thorpej }
106 1.1 cgd
107 1.7 cgd
108 1.14 thorpej /* On each event, display a time stamp.
109 1.14 thorpej * This assumes that 'now' is update once for each event, and
110 1.14 thorpej * that at least now.tv_usec changes.
111 1.14 thorpej */
112 1.7 cgd void
113 1.14 thorpej lastlog(void)
114 1.1 cgd {
115 1.14 thorpej static struct timeval last;
116 1.14 thorpej
117 1.14 thorpej if (last.tv_sec != now.tv_sec
118 1.14 thorpej || last.tv_usec != now.tv_usec) {
119 1.14 thorpej (void)fprintf(ftrace, "-- %s --\n", ts(now.tv_sec));
120 1.14 thorpej last = now;
121 1.14 thorpej }
122 1.1 cgd }
123 1.1 cgd
124 1.14 thorpej
125 1.14 thorpej static void
126 1.14 thorpej tmsg(char *p, ...)
127 1.1 cgd {
128 1.14 thorpej va_list args;
129 1.1 cgd
130 1.14 thorpej if (ftrace != 0) {
131 1.14 thorpej lastlog();
132 1.14 thorpej va_start(args, p);
133 1.14 thorpej vfprintf(ftrace, p, args);
134 1.14 thorpej fflush(ftrace);
135 1.1 cgd }
136 1.1 cgd }
137 1.1 cgd
138 1.14 thorpej
139 1.14 thorpej static void
140 1.14 thorpej trace_close(void)
141 1.1 cgd {
142 1.14 thorpej int fd;
143 1.14 thorpej
144 1.14 thorpej
145 1.14 thorpej fflush(stdout);
146 1.14 thorpej fflush(stderr);
147 1.1 cgd
148 1.14 thorpej if (ftrace != 0
149 1.14 thorpej && savetracename[0] != '\0') {
150 1.14 thorpej fd = open(_PATH_DEVNULL, O_RDWR);
151 1.14 thorpej (void)dup2(fd, STDOUT_FILENO);
152 1.14 thorpej (void)dup2(fd, STDERR_FILENO);
153 1.14 thorpej (void)close(fd);
154 1.14 thorpej fclose(ftrace);
155 1.14 thorpej ftrace = 0;
156 1.14 thorpej }
157 1.1 cgd }
158 1.1 cgd
159 1.14 thorpej
160 1.7 cgd void
161 1.14 thorpej trace_flush(void)
162 1.1 cgd {
163 1.14 thorpej if (ftrace != 0) {
164 1.1 cgd fflush(ftrace);
165 1.14 thorpej if (ferror(ftrace))
166 1.14 thorpej trace_off("tracing off: ", strerror(ferror(ftrace)));
167 1.1 cgd }
168 1.1 cgd }
169 1.1 cgd
170 1.14 thorpej
171 1.1 cgd void
172 1.14 thorpej trace_off(char *p, ...)
173 1.1 cgd {
174 1.14 thorpej va_list args;
175 1.1 cgd
176 1.14 thorpej
177 1.14 thorpej if (ftrace != 0) {
178 1.14 thorpej lastlog();
179 1.14 thorpej va_start(args, p);
180 1.14 thorpej vfprintf(ftrace, p, args);
181 1.14 thorpej fflush(ftrace);
182 1.14 thorpej }
183 1.14 thorpej trace_close();
184 1.14 thorpej
185 1.14 thorpej new_tracelevel = tracelevel = 0;
186 1.1 cgd }
187 1.1 cgd
188 1.14 thorpej
189 1.7 cgd void
190 1.14 thorpej trace_on(char *filename,
191 1.14 thorpej int trusted)
192 1.1 cgd {
193 1.14 thorpej struct stat stbuf;
194 1.14 thorpej FILE *n_ftrace;
195 1.1 cgd
196 1.14 thorpej
197 1.14 thorpej /* Given a null filename when tracing is already on, increase the
198 1.14 thorpej * debugging level and re-open the file in case it has been unlinked.
199 1.14 thorpej */
200 1.14 thorpej if (filename[0] == '\0') {
201 1.14 thorpej if (tracelevel != 0) {
202 1.14 thorpej new_tracelevel++;
203 1.14 thorpej tracelevel_pat = "trace command: %s\n";
204 1.14 thorpej } else if (savetracename[0] == '\0') {
205 1.14 thorpej msglog("missing trace file name");
206 1.14 thorpej return;
207 1.14 thorpej }
208 1.14 thorpej filename = savetracename;
209 1.14 thorpej
210 1.14 thorpej } else if (stat(filename, &stbuf) >= 0) {
211 1.14 thorpej if (!trusted) {
212 1.14 thorpej msglog("trace file \"%s\" already exists");
213 1.14 thorpej return;
214 1.14 thorpej }
215 1.14 thorpej if ((stbuf.st_mode & S_IFMT) != S_IFREG) {
216 1.14 thorpej msglog("wrong type (%#x) of trace file \"%s\"",
217 1.14 thorpej stbuf.st_mode, filename);
218 1.14 thorpej return;
219 1.14 thorpej }
220 1.14 thorpej
221 1.14 thorpej if (!trusted
222 1.14 thorpej && strcmp(filename, savetracename)
223 1.14 thorpej && strncmp(filename, _PATH_TRACE, sizeof(_PATH_TRACE)-1)) {
224 1.14 thorpej msglog("wrong directory for trace file: \"%s\"",
225 1.14 thorpej filename);
226 1.14 thorpej return;
227 1.14 thorpej }
228 1.14 thorpej }
229 1.14 thorpej
230 1.14 thorpej n_ftrace = fopen(filename, "a");
231 1.14 thorpej if (n_ftrace == 0) {
232 1.14 thorpej msglog("failed to open trace file \"%s\" %s",
233 1.14 thorpej filename, strerror(errno));
234 1.14 thorpej return;
235 1.1 cgd }
236 1.14 thorpej
237 1.14 thorpej tmsg("switch to trace file %s\n", filename);
238 1.14 thorpej trace_close();
239 1.14 thorpej if (filename != savetracename)
240 1.14 thorpej strncpy(savetracename, filename, sizeof(savetracename)-1);
241 1.14 thorpej ftrace = n_ftrace;
242 1.14 thorpej
243 1.14 thorpej fflush(stdout);
244 1.14 thorpej fflush(stderr);
245 1.14 thorpej dup2(fileno(ftrace), STDOUT_FILENO);
246 1.14 thorpej dup2(fileno(ftrace), STDERR_FILENO);
247 1.14 thorpej
248 1.14 thorpej if (new_tracelevel == 0)
249 1.14 thorpej new_tracelevel = 1;
250 1.14 thorpej set_tracelevel();
251 1.14 thorpej }
252 1.14 thorpej
253 1.14 thorpej
254 1.14 thorpej /* ARGSUSED */
255 1.14 thorpej void
256 1.14 thorpej sigtrace_on(int s)
257 1.14 thorpej {
258 1.14 thorpej new_tracelevel++;
259 1.14 thorpej tracelevel_pat = "SIGUSR1: %s\n";
260 1.1 cgd }
261 1.1 cgd
262 1.14 thorpej
263 1.14 thorpej /* ARGSUSED */
264 1.7 cgd void
265 1.14 thorpej sigtrace_off(int s)
266 1.14 thorpej {
267 1.14 thorpej new_tracelevel--;
268 1.14 thorpej tracelevel_pat = "SIGUSR2: %s\n";
269 1.1 cgd }
270 1.1 cgd
271 1.14 thorpej
272 1.14 thorpej /* Move to next higher level of tracing when -t option processed or
273 1.14 thorpej * SIGUSR1 is received. Successive levels are:
274 1.14 thorpej * actions
275 1.14 thorpej * actions + packets
276 1.14 thorpej * actions + packets + contents
277 1.14 thorpej */
278 1.7 cgd void
279 1.14 thorpej set_tracelevel(void)
280 1.14 thorpej {
281 1.14 thorpej static char *off_msgs[MAX_TRACELEVEL] = {
282 1.14 thorpej "Tracing actions stopped",
283 1.14 thorpej "Tracing packets stopped",
284 1.14 thorpej "Tracing packet contents stopped",
285 1.1 cgd };
286 1.14 thorpej static char *on_msgs[MAX_TRACELEVEL] = {
287 1.14 thorpej "Tracing actions started",
288 1.14 thorpej "Tracing packets started",
289 1.14 thorpej "Tracing packet contents started",
290 1.14 thorpej };
291 1.14 thorpej
292 1.14 thorpej
293 1.14 thorpej if (new_tracelevel > MAX_TRACELEVEL) {
294 1.14 thorpej new_tracelevel = MAX_TRACELEVEL;
295 1.14 thorpej if (new_tracelevel == tracelevel) {
296 1.14 thorpej tmsg(tracelevel_pat, on_msgs[tracelevel-1]);
297 1.14 thorpej return;
298 1.1 cgd }
299 1.1 cgd }
300 1.14 thorpej while (new_tracelevel != tracelevel) {
301 1.14 thorpej if (new_tracelevel < tracelevel) {
302 1.14 thorpej if (--tracelevel == 0)
303 1.14 thorpej trace_off(tracelevel_pat, off_msgs[0]);
304 1.14 thorpej else
305 1.14 thorpej tmsg(tracelevel_pat, off_msgs[tracelevel]);
306 1.14 thorpej } else {
307 1.14 thorpej if (ftrace == 0) {
308 1.14 thorpej if (savetracename[0] != '\0')
309 1.14 thorpej trace_on(savetracename, 1);
310 1.14 thorpej else
311 1.14 thorpej ftrace = stdout;
312 1.14 thorpej }
313 1.14 thorpej tmsg(tracelevel_pat, on_msgs[tracelevel++]);
314 1.1 cgd }
315 1.1 cgd }
316 1.14 thorpej tracelevel_pat = "%s\n";
317 1.14 thorpej }
318 1.14 thorpej
319 1.14 thorpej
320 1.14 thorpej /* display an address
321 1.14 thorpej */
322 1.14 thorpej char *
323 1.14 thorpej addrname(naddr addr, /* in network byte order */
324 1.14 thorpej naddr mask,
325 1.14 thorpej int force) /* 0=show mask if nonstandard, */
326 1.14 thorpej { /* 1=always show mask, 2=never */
327 1.14 thorpej static char s[15+20];
328 1.14 thorpej char *sp;
329 1.14 thorpej naddr dmask;
330 1.14 thorpej int i;
331 1.14 thorpej
332 1.14 thorpej (void)strcpy(s, naddr_ntoa(addr));
333 1.14 thorpej
334 1.14 thorpej if (force == 1 || (force == 0 && mask != std_mask(addr))) {
335 1.14 thorpej sp = &s[strlen(s)];
336 1.14 thorpej
337 1.14 thorpej dmask = mask & -mask;
338 1.14 thorpej if (mask + dmask == 0) {
339 1.14 thorpej for (i = 0; i != 32 && ((1<<i) & mask) == 0; i++)
340 1.14 thorpej continue;
341 1.14 thorpej (void)sprintf(sp, "/%d", 32-i);
342 1.14 thorpej
343 1.14 thorpej } else {
344 1.14 thorpej (void)sprintf(sp, " (mask %#x)", mask);
345 1.14 thorpej }
346 1.14 thorpej }
347 1.14 thorpej
348 1.14 thorpej return s;
349 1.14 thorpej }
350 1.14 thorpej
351 1.14 thorpej
352 1.14 thorpej /* display a bit-field
353 1.14 thorpej */
354 1.14 thorpej struct bits {
355 1.14 thorpej int bits_mask;
356 1.14 thorpej int bits_clear;
357 1.14 thorpej char *bits_name;
358 1.14 thorpej };
359 1.14 thorpej
360 1.14 thorpej static struct bits if_bits[] = {
361 1.14 thorpej { IFF_LOOPBACK, 0, "LOOPBACK" },
362 1.14 thorpej { IFF_POINTOPOINT, 0, "PT-TO-PT" },
363 1.14 thorpej { 0, 0, 0}
364 1.14 thorpej };
365 1.14 thorpej
366 1.14 thorpej static struct bits is_bits[] = {
367 1.14 thorpej { IS_SUBNET, 0, "" },
368 1.14 thorpej { IS_REMOTE, 0, "REMOTE" },
369 1.14 thorpej { IS_PASSIVE, (IS_NO_RDISC
370 1.14 thorpej | IS_BCAST_RDISC
371 1.14 thorpej | IS_NO_RIP
372 1.14 thorpej | IS_NO_SUPER_AG
373 1.14 thorpej | IS_PM_RDISC
374 1.14 thorpej | IS_NO_AG), "PASSIVE" },
375 1.14 thorpej { IS_EXTERNAL, 0, "EXTERNAL" },
376 1.14 thorpej { IS_CHECKED, 0, "" },
377 1.14 thorpej { IS_ALL_HOSTS, 0, "" },
378 1.14 thorpej { IS_ALL_ROUTERS, 0, "" },
379 1.14 thorpej { IS_RIP_QUERIED, 0, "" },
380 1.14 thorpej { IS_BROKE, IS_SICK, "BROKEN" },
381 1.14 thorpej { IS_SICK, 0, "SICK" },
382 1.14 thorpej { IS_ACTIVE, 0, "ACTIVE" },
383 1.14 thorpej { IS_NEED_NET_SYN, 0, "" },
384 1.14 thorpej { IS_NO_AG, IS_NO_SUPER_AG, "NO_AG" },
385 1.14 thorpej { IS_NO_SUPER_AG, 0, "NO_SUPER_AG" },
386 1.14 thorpej { (IS_NO_RIPV1_IN
387 1.14 thorpej | IS_NO_RIPV2_IN
388 1.14 thorpej | IS_NO_RIPV1_OUT
389 1.14 thorpej | IS_NO_RIPV2_OUT), 0, "NO_RIP" },
390 1.14 thorpej { (IS_NO_RIPV1_IN
391 1.14 thorpej | IS_NO_RIPV1_OUT), 0, "RIPV2" },
392 1.14 thorpej { IS_NO_RIPV1_IN, 0, "NO_RIPV1_IN" },
393 1.14 thorpej { IS_NO_RIPV2_IN, 0, "NO_RIPV2_IN" },
394 1.14 thorpej { IS_NO_RIPV1_OUT, 0, "NO_RIPV1_OUT" },
395 1.14 thorpej { IS_NO_RIPV2_OUT, 0, "NO_RIPV2_OUT" },
396 1.14 thorpej { (IS_NO_ADV_IN
397 1.14 thorpej | IS_NO_SOL_OUT
398 1.14 thorpej | IS_NO_ADV_OUT), IS_BCAST_RDISC, "NO_RDISC" },
399 1.14 thorpej { IS_NO_SOL_OUT, 0, "NO_SOLICIT" },
400 1.14 thorpej { IS_SOL_OUT, 0, "SEND_SOLICIT" },
401 1.14 thorpej { IS_NO_ADV_OUT, IS_BCAST_RDISC, "NO_RDISC_ADV" },
402 1.14 thorpej { IS_ADV_OUT, 0, "RDISC_ADV" },
403 1.14 thorpej { IS_BCAST_RDISC, 0, "BCAST_RDISC" },
404 1.14 thorpej { IS_PM_RDISC, 0, "PM_RDISC" },
405 1.14 thorpej { 0, 0, "%#x"}
406 1.14 thorpej };
407 1.14 thorpej
408 1.14 thorpej static struct bits rs_bits[] = {
409 1.14 thorpej { RS_IF, 0, "IF" },
410 1.14 thorpej { RS_NET_INT, RS_NET_SYN, "NET_INT" },
411 1.14 thorpej { RS_NET_SYN, 0, "NET_SYN" },
412 1.14 thorpej { RS_SUBNET, 0, "" },
413 1.14 thorpej { RS_LOCAL, 0, "LOCAL" },
414 1.14 thorpej { RS_MHOME, 0, "MHOME" },
415 1.14 thorpej { RS_STATIC, 0, "STATIC" },
416 1.14 thorpej { RS_RDISC, 0, "RDISC" },
417 1.14 thorpej { 0, 0, "%#x"}
418 1.14 thorpej };
419 1.14 thorpej
420 1.14 thorpej
421 1.14 thorpej static void
422 1.14 thorpej trace_bits(struct bits *tbl,
423 1.14 thorpej u_int field,
424 1.14 thorpej int force)
425 1.14 thorpej {
426 1.14 thorpej int b;
427 1.14 thorpej char c;
428 1.14 thorpej
429 1.14 thorpej if (force) {
430 1.14 thorpej (void)putc('<', ftrace);
431 1.14 thorpej c = 0;
432 1.14 thorpej } else {
433 1.14 thorpej c = '<';
434 1.14 thorpej }
435 1.14 thorpej
436 1.14 thorpej while (field != 0
437 1.14 thorpej && (b = tbl->bits_mask) != 0) {
438 1.14 thorpej if ((b & field) == b) {
439 1.14 thorpej if (tbl->bits_name[0] != '\0') {
440 1.14 thorpej if (c)
441 1.14 thorpej (void)putc(c, ftrace);
442 1.14 thorpej (void)fprintf(ftrace, "%s", tbl->bits_name);
443 1.14 thorpej c = '|';
444 1.14 thorpej }
445 1.14 thorpej if (0 == (field &= ~(b | tbl->bits_clear)))
446 1.14 thorpej break;
447 1.14 thorpej }
448 1.14 thorpej tbl++;
449 1.14 thorpej }
450 1.14 thorpej if (field != 0 && tbl->bits_name != 0) {
451 1.14 thorpej if (c)
452 1.14 thorpej (void)putc(c, ftrace);
453 1.14 thorpej (void)fprintf(ftrace, tbl->bits_name, field);
454 1.14 thorpej c = '|';
455 1.14 thorpej }
456 1.14 thorpej
457 1.14 thorpej if (c != '<' || force)
458 1.14 thorpej (void)fputs("> ", ftrace);
459 1.14 thorpej }
460 1.14 thorpej
461 1.14 thorpej
462 1.14 thorpej static char *
463 1.14 thorpej trace_pair(naddr dst,
464 1.14 thorpej naddr mask,
465 1.14 thorpej char *gate)
466 1.14 thorpej {
467 1.14 thorpej static char buf[3*4+3+1+2+3 /* "xxx.xxx.xxx.xxx/xx-->" */
468 1.14 thorpej +3*4+3+1]; /* "xxx.xxx.xxx.xxx" */
469 1.14 thorpej int i;
470 1.14 thorpej
471 1.14 thorpej i = sprintf(buf, "%-16s-->", addrname(dst, mask, 0));
472 1.14 thorpej (void)sprintf(&buf[i], "%-*s", 15+20-MAX(20,i), gate);
473 1.14 thorpej return buf;
474 1.14 thorpej }
475 1.14 thorpej
476 1.14 thorpej
477 1.14 thorpej void
478 1.14 thorpej trace_if(char *act,
479 1.14 thorpej struct interface *ifp)
480 1.14 thorpej {
481 1.14 thorpej if (!TRACEACTIONS || ftrace == 0)
482 1.14 thorpej return;
483 1.14 thorpej
484 1.14 thorpej lastlog();
485 1.14 thorpej (void)fprintf(ftrace, "%s interface %-4s ", act, ifp->int_name);
486 1.14 thorpej (void)fprintf(ftrace, "%-15s --> %s ",
487 1.14 thorpej naddr_ntoa(ifp->int_addr),
488 1.14 thorpej ((ifp->int_if_flags & IFF_POINTOPOINT)
489 1.14 thorpej ? naddr_ntoa(ifp->int_dstaddr)
490 1.14 thorpej : addrname(htonl(ifp->int_net), ifp->int_mask, 0)));
491 1.14 thorpej (void)fprintf(ftrace, "metric=%d ", ifp->int_metric);
492 1.14 thorpej trace_bits(if_bits, ifp->int_if_flags, 0);
493 1.14 thorpej trace_bits(is_bits, ifp->int_state, 0);
494 1.14 thorpej (void)fputc('\n',ftrace);
495 1.14 thorpej }
496 1.14 thorpej
497 1.14 thorpej
498 1.14 thorpej void
499 1.14 thorpej trace_upslot(struct rt_entry *rt,
500 1.14 thorpej struct rt_spare *rts,
501 1.14 thorpej naddr gate,
502 1.14 thorpej naddr router,
503 1.14 thorpej struct interface *ifp,
504 1.14 thorpej int metric,
505 1.14 thorpej u_short tag,
506 1.14 thorpej time_t new_time)
507 1.14 thorpej {
508 1.14 thorpej if (!TRACEACTIONS || ftrace == 0)
509 1.14 thorpej return;
510 1.14 thorpej if (rts->rts_gate == gate
511 1.14 thorpej && rts->rts_router == router
512 1.14 thorpej && rts->rts_metric == metric
513 1.14 thorpej && rts->rts_tag == tag)
514 1.14 thorpej return;
515 1.14 thorpej
516 1.14 thorpej lastlog();
517 1.14 thorpej if (rts->rts_gate != RIP_DEFAULT) {
518 1.14 thorpej (void)fprintf(ftrace, "Chg #%d %-35s ",
519 1.14 thorpej rts - rt->rt_spares,
520 1.14 thorpej trace_pair(rt->rt_dst, rt->rt_mask,
521 1.14 thorpej naddr_ntoa(rts->rts_gate)));
522 1.14 thorpej if (rts->rts_gate != rts->rts_gate)
523 1.14 thorpej (void)fprintf(ftrace, "router=%s ",
524 1.14 thorpej naddr_ntoa(rts->rts_gate));
525 1.14 thorpej if (rts->rts_tag != 0)
526 1.14 thorpej (void)fprintf(ftrace, "tag=%#x ", rts->rts_tag);
527 1.14 thorpej (void)fprintf(ftrace, "metric=%-2d ", rts->rts_metric);
528 1.14 thorpej if (rts->rts_ifp != 0)
529 1.14 thorpej (void)fprintf(ftrace, "%s ",
530 1.14 thorpej rts->rts_ifp->int_name);
531 1.14 thorpej (void)fprintf(ftrace, "%s\n", ts(rts->rts_time));
532 1.14 thorpej
533 1.14 thorpej (void)fprintf(ftrace, " %19s%-16s ",
534 1.14 thorpej "",
535 1.14 thorpej gate != rts->rts_gate ? naddr_ntoa(gate) : "");
536 1.14 thorpej if (gate != router)
537 1.14 thorpej (void)fprintf(ftrace,"router=%s ",naddr_ntoa(router));
538 1.14 thorpej if (tag != rts->rts_tag)
539 1.14 thorpej (void)fprintf(ftrace, "tag=%#x ", tag);
540 1.14 thorpej if (metric != rts->rts_metric)
541 1.14 thorpej (void)fprintf(ftrace, "metric=%-2d ", metric);
542 1.14 thorpej if (ifp != rts->rts_ifp && ifp != 0 )
543 1.14 thorpej (void)fprintf(ftrace, "%s ", ifp->int_name);
544 1.14 thorpej (void)fprintf(ftrace, "%s\n",
545 1.14 thorpej new_time != rts->rts_time ? ts(new_time) : "");
546 1.14 thorpej
547 1.14 thorpej } else {
548 1.14 thorpej (void)fprintf(ftrace, "Add #%d %-35s ",
549 1.14 thorpej rts - rt->rt_spares,
550 1.14 thorpej trace_pair(rt->rt_dst, rt->rt_mask,
551 1.14 thorpej naddr_ntoa(gate)));
552 1.14 thorpej if (gate != router)
553 1.14 thorpej (void)fprintf(ftrace, "router=%s ", naddr_ntoa(gate));
554 1.14 thorpej if (tag != 0)
555 1.14 thorpej (void)fprintf(ftrace, "tag=%#x ", tag);
556 1.14 thorpej (void)fprintf(ftrace, "metric=%-2d ", metric);
557 1.14 thorpej if (ifp != 0)
558 1.14 thorpej (void)fprintf(ftrace, "%s ", ifp->int_name);
559 1.14 thorpej (void)fprintf(ftrace, "%s\n", ts(new_time));
560 1.14 thorpej }
561 1.1 cgd }
562 1.1 cgd
563 1.14 thorpej
564 1.14 thorpej /* display a message if tracing actions
565 1.14 thorpej */
566 1.14 thorpej void
567 1.14 thorpej trace_act(char *p, ...)
568 1.14 thorpej {
569 1.14 thorpej va_list args;
570 1.14 thorpej
571 1.14 thorpej if (!TRACEACTIONS || ftrace == 0)
572 1.14 thorpej return;
573 1.14 thorpej
574 1.14 thorpej lastlog();
575 1.14 thorpej va_start(args, p);
576 1.14 thorpej vfprintf(ftrace, p, args);
577 1.14 thorpej }
578 1.14 thorpej
579 1.14 thorpej
580 1.14 thorpej /* display a message if tracing packets
581 1.14 thorpej */
582 1.7 cgd void
583 1.14 thorpej trace_pkt(char *p, ...)
584 1.1 cgd {
585 1.14 thorpej va_list args;
586 1.1 cgd
587 1.14 thorpej if (!TRACEPACKETS || ftrace == 0)
588 1.1 cgd return;
589 1.14 thorpej
590 1.14 thorpej lastlog();
591 1.14 thorpej va_start(args, p);
592 1.14 thorpej vfprintf(ftrace, p, args);
593 1.1 cgd }
594 1.1 cgd
595 1.14 thorpej
596 1.7 cgd void
597 1.14 thorpej trace_change(struct rt_entry *rt,
598 1.14 thorpej u_int state,
599 1.14 thorpej naddr gate, /* forward packets here */
600 1.14 thorpej naddr router, /* on the authority of this router */
601 1.14 thorpej int metric,
602 1.14 thorpej u_short tag,
603 1.14 thorpej struct interface *ifp,
604 1.14 thorpej time_t new_time,
605 1.14 thorpej char *label)
606 1.1 cgd {
607 1.14 thorpej if (ftrace == 0)
608 1.14 thorpej return;
609 1.14 thorpej
610 1.14 thorpej if (rt->rt_metric == metric
611 1.14 thorpej && rt->rt_gate == gate
612 1.14 thorpej && rt->rt_router == router
613 1.14 thorpej && rt->rt_state == state
614 1.14 thorpej && rt->rt_tag == tag)
615 1.14 thorpej return;
616 1.14 thorpej
617 1.14 thorpej lastlog();
618 1.14 thorpej (void)fprintf(ftrace, "%s %-35s metric=%-2d ",
619 1.14 thorpej label,
620 1.14 thorpej trace_pair(rt->rt_dst, rt->rt_mask,
621 1.14 thorpej naddr_ntoa(rt->rt_gate)),
622 1.14 thorpej rt->rt_metric);
623 1.14 thorpej if (rt->rt_router != rt->rt_gate)
624 1.14 thorpej (void)fprintf(ftrace, "router=%s ",
625 1.14 thorpej naddr_ntoa(rt->rt_router));
626 1.14 thorpej if (rt->rt_tag != 0)
627 1.14 thorpej (void)fprintf(ftrace, "tag=%#x ", rt->rt_tag);
628 1.14 thorpej trace_bits(rs_bits, rt->rt_state, rt->rt_state != state);
629 1.14 thorpej (void)fprintf(ftrace, "%s ",
630 1.14 thorpej rt->rt_ifp == 0 ? "?" : rt->rt_ifp->int_name);
631 1.14 thorpej (void)fprintf(ftrace, "%s\n",
632 1.14 thorpej AGE_RT(rt, rt->rt_ifp) ? ts(rt->rt_time) : "");
633 1.14 thorpej
634 1.14 thorpej (void)fprintf(ftrace, "%*s %19s%-16s ",
635 1.14 thorpej strlen(label), "", "",
636 1.14 thorpej rt->rt_gate != gate ? naddr_ntoa(gate) : "");
637 1.14 thorpej if (rt->rt_metric != metric)
638 1.14 thorpej (void)fprintf(ftrace, "metric=%-2d ", metric);
639 1.14 thorpej if (router != gate)
640 1.14 thorpej (void)fprintf(ftrace, "router=%s ", naddr_ntoa(router));
641 1.14 thorpej if (rt->rt_tag != tag)
642 1.14 thorpej (void)fprintf(ftrace, "tag=%#x ", tag);
643 1.14 thorpej if (rt->rt_state != state)
644 1.14 thorpej trace_bits(rs_bits, state, 1);
645 1.14 thorpej if (rt->rt_ifp != ifp)
646 1.14 thorpej (void)fprintf(ftrace, "%s ",
647 1.14 thorpej ifp != 0 ? ifp->int_name : "?");
648 1.14 thorpej (void)fprintf(ftrace, "%s\n",
649 1.14 thorpej ((rt->rt_time == new_time || !AGE_RT(rt, ifp))
650 1.14 thorpej ? "" : ts(new_time)));
651 1.1 cgd }
652 1.1 cgd
653 1.14 thorpej
654 1.7 cgd void
655 1.14 thorpej trace_add_del(char * action, struct rt_entry *rt)
656 1.1 cgd {
657 1.14 thorpej u_int state = rt->rt_state;
658 1.1 cgd
659 1.14 thorpej if (ftrace == 0)
660 1.1 cgd return;
661 1.14 thorpej
662 1.14 thorpej lastlog();
663 1.14 thorpej (void)fprintf(ftrace, "%s %-35s metric=%-2d ",
664 1.14 thorpej action,
665 1.14 thorpej trace_pair(rt->rt_dst, rt->rt_mask,
666 1.14 thorpej naddr_ntoa(rt->rt_gate)),
667 1.14 thorpej rt->rt_metric);
668 1.14 thorpej if (rt->rt_router != rt->rt_gate)
669 1.14 thorpej (void)fprintf(ftrace, "router=%s ",
670 1.14 thorpej naddr_ntoa(rt->rt_router));
671 1.14 thorpej if (rt->rt_tag != 0)
672 1.14 thorpej (void)fprintf(ftrace, "tag=%#x ", rt->rt_tag);
673 1.14 thorpej trace_bits(rs_bits, state, 0);
674 1.14 thorpej (void)fprintf(ftrace, "%s ",
675 1.14 thorpej rt->rt_ifp != 0 ? rt->rt_ifp->int_name : "?");
676 1.14 thorpej (void)fprintf(ftrace, "%s\n", ts(rt->rt_time));
677 1.1 cgd }
678 1.1 cgd
679 1.14 thorpej
680 1.7 cgd void
681 1.14 thorpej trace_rip(char *dir1, char *dir2,
682 1.14 thorpej struct sockaddr_in *who,
683 1.14 thorpej struct interface *ifp,
684 1.14 thorpej struct rip *msg,
685 1.14 thorpej int size) /* total size of message */
686 1.1 cgd {
687 1.14 thorpej struct netinfo *n, *lim;
688 1.14 thorpej struct netauth *a;
689 1.14 thorpej int i;
690 1.1 cgd
691 1.14 thorpej if (!TRACEPACKETS || ftrace == 0)
692 1.1 cgd return;
693 1.14 thorpej
694 1.14 thorpej lastlog();
695 1.14 thorpej if (msg->rip_cmd >= RIPCMD_MAX
696 1.14 thorpej || msg->rip_vers == 0) {
697 1.14 thorpej (void)fprintf(ftrace,
698 1.14 thorpej "%s bad RIPv%d cmd=%d %s %s.%d size=%d msg=%#lx\n",
699 1.14 thorpej dir1, msg->rip_vers, msg->rip_cmd, dir2,
700 1.14 thorpej naddr_ntoa(who->sin_addr.s_addr),
701 1.14 thorpej ntohs(who->sin_port), size, (unsigned long)msg);
702 1.1 cgd return;
703 1.1 cgd }
704 1.14 thorpej
705 1.14 thorpej (void)fprintf(ftrace, "%s RIPv%d %s %s %s.%d%s%s\n",
706 1.14 thorpej dir1, msg->rip_vers, ripcmds[msg->rip_cmd], dir2,
707 1.14 thorpej naddr_ntoa(who->sin_addr.s_addr), ntohs(who->sin_port),
708 1.14 thorpej ifp ? " via " : "", ifp ? ifp->int_name : "");
709 1.14 thorpej if (!TRACECONTENTS)
710 1.1 cgd return;
711 1.14 thorpej
712 1.1 cgd switch (msg->rip_cmd) {
713 1.1 cgd case RIPCMD_REQUEST:
714 1.1 cgd case RIPCMD_RESPONSE:
715 1.1 cgd n = msg->rip_nets;
716 1.14 thorpej lim = (struct netinfo *)((char*)msg + size);
717 1.14 thorpej for (; n < lim; n++) {
718 1.14 thorpej if (n->n_family == RIP_AF_UNSPEC
719 1.14 thorpej && ntohl(n->n_metric) == HOPCNT_INFINITY
720 1.14 thorpej && n+1 == lim
721 1.14 thorpej && n == msg->rip_nets
722 1.14 thorpej && msg->rip_cmd == RIPCMD_REQUEST) {
723 1.14 thorpej (void)fputs("\tQUERY ", ftrace);
724 1.14 thorpej if (n->n_dst != 0)
725 1.14 thorpej (void)fprintf(ftrace, "%s ",
726 1.14 thorpej naddr_ntoa(n->n_dst));
727 1.14 thorpej if (n->n_mask != 0)
728 1.14 thorpej (void)fprintf(ftrace, "mask=%#x ",
729 1.14 thorpej ntohl(n->n_mask));
730 1.14 thorpej if (n->n_nhop != 0)
731 1.14 thorpej (void)fprintf(ftrace, " nhop=%s ",
732 1.14 thorpej naddr_ntoa(n->n_nhop));
733 1.14 thorpej if (n->n_tag != 0)
734 1.14 thorpej (void)fprintf(ftrace, "tag=%#x",
735 1.14 thorpej n->n_tag);
736 1.14 thorpej (void)fputc('\n',ftrace);
737 1.14 thorpej continue;
738 1.1 cgd }
739 1.1 cgd
740 1.14 thorpej if (n->n_family == RIP_AF_AUTH) {
741 1.14 thorpej a = (struct netauth*)n;
742 1.14 thorpej (void)fprintf(ftrace,
743 1.14 thorpej "\tAuthentication type %d: ",
744 1.14 thorpej ntohs(a->a_type));
745 1.14 thorpej for (i = 0;
746 1.14 thorpej i < sizeof(a->au.au_pw);
747 1.14 thorpej i++)
748 1.14 thorpej (void)fprintf(ftrace, "%02x ",
749 1.14 thorpej a->au.au_pw[i]);
750 1.14 thorpej (void)fputc('\n',ftrace);
751 1.14 thorpej continue;
752 1.14 thorpej }
753 1.1 cgd
754 1.14 thorpej if (n->n_family != RIP_AF_INET) {
755 1.14 thorpej (void)fprintf(ftrace,
756 1.14 thorpej "\t(af %d) %-18s mask=%#x",
757 1.14 thorpej ntohs(n->n_family),
758 1.14 thorpej naddr_ntoa(n->n_dst),
759 1.14 thorpej ntohl(n->n_mask));
760 1.14 thorpej } else if (msg->rip_vers == RIPv1) {
761 1.14 thorpej (void)fprintf(ftrace, "\t%-18s ",
762 1.14 thorpej addrname(n->n_dst,
763 1.14 thorpej ntohl(n->n_mask),
764 1.14 thorpej n->n_mask==0 ? 2 : 1));
765 1.14 thorpej } else {
766 1.14 thorpej (void)fprintf(ftrace, "\t%-18s ",
767 1.14 thorpej addrname(n->n_dst,
768 1.14 thorpej ntohl(n->n_mask),
769 1.14 thorpej n->n_mask==0 ? 2 : 0));
770 1.1 cgd }
771 1.14 thorpej (void)fprintf(ftrace, "metric=%-2d ",
772 1.14 thorpej ntohl(n->n_metric));
773 1.14 thorpej if (n->n_nhop != 0)
774 1.14 thorpej (void)fprintf(ftrace, " nhop=%s ",
775 1.14 thorpej naddr_ntoa(n->n_nhop));
776 1.14 thorpej if (n->n_tag != 0)
777 1.14 thorpej (void)fprintf(ftrace, "tag=%#x",
778 1.14 thorpej n->n_tag);
779 1.14 thorpej (void)fputc('\n',ftrace);
780 1.1 cgd }
781 1.14 thorpej if (size != (char *)n - (char *)msg)
782 1.14 thorpej (void)fprintf(ftrace, "truncated record, len %d\n",
783 1.14 thorpej size);
784 1.1 cgd break;
785 1.1 cgd
786 1.1 cgd case RIPCMD_TRACEON:
787 1.14 thorpej fprintf(ftrace, "\tfile=%*s\n", size-4, msg->rip_tracefile);
788 1.1 cgd break;
789 1.1 cgd
790 1.1 cgd case RIPCMD_TRACEOFF:
791 1.1 cgd break;
792 1.1 cgd }
793 1.1 cgd }
794