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
|
/*
* Copyright (c) 1985, 1993
* The Regents of the University of California. All rights reserved.
*
* This file includes significant work done at Cornell University by
* Bill Nesheim. That work included by permission.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char sccsid[] = "@(#)trace.c 8.1 (Berkeley) 6/5/93";
#endif /* not lint */
/*
* Routing Table Management Daemon
*/
#define RIPCMDS
#include <stdlib.h>
#include "defs.h"
#define NRECORDS 50 /* size of circular trace buffer */
#ifdef DEBUG
FILE *ftrace = stdout;
int tracing = 1;
#else DEBUG
FILE *ftrace = NULL;
int tracing = 0;
#endif
char *xns_ntoa();
traceinit(ifp)
register struct interface *ifp;
{
static int iftraceinit();
if (iftraceinit(ifp, &ifp->int_input) &&
iftraceinit(ifp, &ifp->int_output))
return;
tracing = 0;
syslog(LOG_ERR, "traceinit: can't init %s\n", ifp->int_name);
}
static
iftraceinit(ifp, ifd)
struct interface *ifp;
register struct ifdebug *ifd;
{
register struct iftrace *t;
ifd->ifd_records =
(struct iftrace *)malloc(NRECORDS * sizeof (struct iftrace));
if (ifd->ifd_records == 0)
return (0);
ifd->ifd_front = ifd->ifd_records;
ifd->ifd_count = 0;
for (t = ifd->ifd_records; t < ifd->ifd_records + NRECORDS; t++) {
t->ift_size = 0;
t->ift_packet = 0;
}
ifd->ifd_if = ifp;
return (1);
}
traceon(file)
char *file;
{
if (ftrace != NULL)
return;
ftrace = fopen(file, "a");
if (ftrace == NULL)
return;
dup2(fileno(ftrace), 1);
dup2(fileno(ftrace), 2);
tracing = 1;
}
traceoff()
{
if (!tracing)
return;
if (ftrace != NULL)
fclose(ftrace);
ftrace = NULL;
tracing = 0;
}
trace(ifd, who, p, len, m)
register struct ifdebug *ifd;
struct sockaddr *who;
char *p;
int len, m;
{
register struct iftrace *t;
if (ifd->ifd_records == 0)
return;
t = ifd->ifd_front++;
if (ifd->ifd_front >= ifd->ifd_records + NRECORDS)
ifd->ifd_front = ifd->ifd_records;
if (ifd->ifd_count < NRECORDS)
ifd->ifd_count++;
if (t->ift_size > 0 && t->ift_packet)
free(t->ift_packet);
t->ift_packet = 0;
t->ift_stamp = time(0);
t->ift_who = *who;
if (len > 0) {
t->ift_packet = malloc(len);
if (t->ift_packet)
bcopy(p, t->ift_packet, len);
else
len = 0;
}
t->ift_size = len;
t->ift_metric = m;
}
traceaction(fd, action, rt)
FILE *fd;
char *action;
struct rt_entry *rt;
{
struct sockaddr_ns *dst, *gate;
static struct bits {
int t_bits;
char *t_name;
} flagbits[] = {
{ RTF_UP, "UP" },
{ RTF_GATEWAY, "GATEWAY" },
{ RTF_HOST, "HOST" },
{ 0 }
}, statebits[] = {
{ RTS_PASSIVE, "PASSIVE" },
{ RTS_REMOTE, "REMOTE" },
{ RTS_INTERFACE,"INTERFACE" },
{ RTS_CHANGED, "CHANGED" },
{ 0 }
};
register struct bits *p;
register int first;
char *cp;
struct interface *ifp;
if (fd == NULL)
return;
fprintf(fd, "%s ", action);
dst = (struct sockaddr_ns *)&rt->rt_dst;
gate = (struct sockaddr_ns *)&rt->rt_router;
fprintf(fd, "dst %s, ", xns_ntoa(&dst->sns_addr));
fprintf(fd, "router %s, metric %d, flags",
xns_ntoa(&gate->sns_addr), rt->rt_metric);
cp = " %s";
for (first = 1, p = flagbits; p->t_bits > 0; p++) {
if ((rt->rt_flags & p->t_bits) == 0)
continue;
fprintf(fd, cp, p->t_name);
if (first) {
cp = "|%s";
first = 0;
}
}
fprintf(fd, " state");
cp = " %s";
for (first = 1, p = statebits; p->t_bits > 0; p++) {
if ((rt->rt_state & p->t_bits) == 0)
continue;
fprintf(fd, cp, p->t_name);
if (first) {
cp = "|%s";
first = 0;
}
}
putc('\n', fd);
if (!tracepackets && (rt->rt_state & RTS_PASSIVE) == 0 && rt->rt_ifp)
dumpif(fd, rt->rt_ifp);
fflush(fd);
}
dumpif(fd, ifp)
register struct interface *ifp;
FILE *fd;
{
if (ifp->int_input.ifd_count || ifp->int_output.ifd_count) {
fprintf(fd, "*** Packet history for interface %s ***\n",
ifp->int_name);
dumptrace(fd, "to", &ifp->int_output);
dumptrace(fd, "from", &ifp->int_input);
fprintf(fd, "*** end packet history ***\n");
}
}
dumptrace(fd, dir, ifd)
FILE *fd;
char *dir;
register struct ifdebug *ifd;
{
register struct iftrace *t;
char *cp = !strcmp(dir, "to") ? "Output" : "Input";
if (ifd->ifd_front == ifd->ifd_records &&
ifd->ifd_front->ift_size == 0) {
fprintf(fd, "%s: no packets.\n", cp);
return;
}
fprintf(fd, "%s trace:\n", cp);
t = ifd->ifd_front - ifd->ifd_count;
if (t < ifd->ifd_records)
t += NRECORDS;
for ( ; ifd->ifd_count; ifd->ifd_count--, t++) {
if (t >= ifd->ifd_records + NRECORDS)
t = ifd->ifd_records;
if (t->ift_size == 0)
continue;
fprintf(fd, "%.24s: metric=%d\n", ctime(&t->ift_stamp),
t->ift_metric);
dumppacket(fd, dir, &t->ift_who, t->ift_packet, t->ift_size);
}
}
dumppacket(fd, dir, who, cp, size)
FILE *fd;
struct sockaddr_ns *who; /* should be sockaddr */
char *dir, *cp;
register int size;
{
register struct rip *msg = (struct rip *)cp;
register struct netinfo *n;
char *xns_nettoa();
if (msg->rip_cmd && ntohs(msg->rip_cmd) < RIPCMD_MAX)
fprintf(fd, "%s %s %s#%x", ripcmds[ntohs(msg->rip_cmd)],
dir, xns_ntoa(&who->sns_addr), ntohs(who->sns_addr.x_port));
else {
fprintf(fd, "Bad cmd 0x%x %s %s#%x\n", ntohs(msg->rip_cmd),
dir, xns_ntoa(&who->sns_addr), ntohs(who->sns_addr.x_port));
fprintf(fd, "size=%d cp=%x packet=%x\n", size, cp, packet);
return;
}
switch (ntohs(msg->rip_cmd)) {
case RIPCMD_REQUEST:
case RIPCMD_RESPONSE:
fprintf(fd, ":\n");
size -= sizeof (u_short);
n = msg->rip_nets;
for (; size > 0; n++, size -= sizeof (struct netinfo)) {
if (size < sizeof (struct netinfo))
break;
fprintf(fd, "\tnet %s metric %d\n",
xns_nettoa(n->rip_dst),
ntohs(n->rip_metric));
}
break;
}
}
union ns_net_u net;
char *
xns_nettoa(val)
union ns_net val;
{
static char buf[100];
net.net_e = val;
(void)sprintf(buf, "%lx", ntohl(net.long_e));
return (buf);
}
char *
xns_ntoa(addr)
struct ns_addr *addr;
{
static char buf[100];
(void)sprintf(buf, "%s#%x:%x:%x:%x:%x:%x",
xns_nettoa(addr->x_net),
addr->x_host.c_host[0], addr->x_host.c_host[1],
addr->x_host.c_host[2], addr->x_host.c_host[3],
addr->x_host.c_host[4], addr->x_host.c_host[5]);
return(buf);
}
|