fixes
[unix-history] / usr / src / sbin / routed / trace.c
CommitLineData
5ff67f98
DF
1/*
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
f036a54d 7#ifndef lint
9bd44906 8static char sccsid[] = "@(#)trace.c 5.2 (Berkeley) %G%";
5ff67f98 9#endif not lint
f036a54d
SL
10
11/*
12 * Routing Table Management Daemon
13 */
14#define RIPCMDS
7fe7fe74 15#include "defs.h"
f036a54d
SL
16
17#define NRECORDS 50 /* size of circular trace buffer */
18#ifdef DEBUG
19FILE *ftrace = stdout;
20int tracing = 1;
21#endif
22
23traceinit(ifp)
24 register struct interface *ifp;
25{
26
27 if (iftraceinit(ifp, &ifp->int_input) &&
28 iftraceinit(ifp, &ifp->int_output))
29 return;
30 tracing = 0;
31 fprintf(stderr, "traceinit: can't init %s\n", ifp->int_name);
32}
33
34static
35iftraceinit(ifp, ifd)
36 struct interface *ifp;
37 register struct ifdebug *ifd;
38{
39 register struct iftrace *t;
40
41 ifd->ifd_records =
42 (struct iftrace *)malloc(NRECORDS * sizeof (struct iftrace));
43 if (ifd->ifd_records == 0)
44 return (0);
45 ifd->ifd_front = ifd->ifd_records;
b7e4f8be 46 ifd->ifd_count = 0;
f036a54d
SL
47 for (t = ifd->ifd_records; t < ifd->ifd_records + NRECORDS; t++) {
48 t->ift_size = 0;
49 t->ift_packet = 0;
50 }
51 ifd->ifd_if = ifp;
52 return (1);
53}
54
55traceon(file)
56 char *file;
57{
58
59 if (ftrace != NULL)
60 return;
61 ftrace = fopen(file, "a");
62 if (ftrace == NULL)
63 return;
64 dup2(fileno(ftrace), 1);
65 dup2(fileno(ftrace), 2);
66 tracing = 1;
67}
68
69traceoff()
70{
71 if (!tracing)
72 return;
73 if (ftrace != NULL)
74 fclose(ftrace);
75 ftrace = NULL;
76 tracing = 0;
77}
78
79trace(ifd, who, p, len, m)
80 register struct ifdebug *ifd;
81 struct sockaddr *who;
82 char *p;
83 int len, m;
84{
85 register struct iftrace *t;
86
87 if (ifd->ifd_records == 0)
88 return;
89 t = ifd->ifd_front++;
90 if (ifd->ifd_front >= ifd->ifd_records + NRECORDS)
91 ifd->ifd_front = ifd->ifd_records;
b7e4f8be
MK
92 if (ifd->ifd_count < NRECORDS)
93 ifd->ifd_count++;
9bd44906 94 if (t->ift_size > 0 && t->ift_size < len && t->ift_packet) {
f036a54d 95 free(t->ift_packet);
9bd44906
MK
96 t->ift_packet = 0;
97 }
f036a54d
SL
98 t->ift_stamp = time(0);
99 t->ift_who = *who;
9bd44906 100 if (len > 0 && t->ift_packet == 0) {
f036a54d 101 t->ift_packet = malloc(len);
9bd44906 102 if (t->ift_packet == 0)
f036a54d
SL
103 len = 0;
104 }
9bd44906
MK
105 if (len > 0)
106 bcopy(p, t->ift_packet, len);
f036a54d
SL
107 t->ift_size = len;
108 t->ift_metric = m;
109}
110
111traceaction(fd, action, rt)
112 FILE *fd;
113 char *action;
114 struct rt_entry *rt;
115{
116 struct sockaddr_in *dst, *gate;
117 static struct bits {
118 int t_bits;
119 char *t_name;
120 } flagbits[] = {
121 { RTF_UP, "UP" },
122 { RTF_GATEWAY, "GATEWAY" },
123 { RTF_HOST, "HOST" },
124 { 0 }
125 }, statebits[] = {
126 { RTS_PASSIVE, "PASSIVE" },
127 { RTS_REMOTE, "REMOTE" },
128 { RTS_INTERFACE,"INTERFACE" },
129 { RTS_CHANGED, "CHANGED" },
130 { 0 }
131 };
132 register struct bits *p;
133 register int first;
134 char *cp;
135 struct interface *ifp;
136
137 if (fd == NULL)
138 return;
139 fprintf(fd, "%s ", action);
140 dst = (struct sockaddr_in *)&rt->rt_dst;
141 gate = (struct sockaddr_in *)&rt->rt_router;
2513c24a
SL
142 fprintf(fd, "dst %s, ", inet_ntoa(dst->sin_addr));
143 fprintf(fd, "router %s, metric %d, flags",
144 inet_ntoa(gate->sin_addr), rt->rt_metric);
f036a54d
SL
145 cp = " %s";
146 for (first = 1, p = flagbits; p->t_bits > 0; p++) {
147 if ((rt->rt_flags & p->t_bits) == 0)
148 continue;
149 fprintf(fd, cp, p->t_name);
150 if (first) {
151 cp = "|%s";
152 first = 0;
153 }
154 }
155 fprintf(fd, " state");
156 cp = " %s";
157 for (first = 1, p = statebits; p->t_bits > 0; p++) {
158 if ((rt->rt_state & p->t_bits) == 0)
159 continue;
160 fprintf(fd, cp, p->t_name);
161 if (first) {
162 cp = "|%s";
163 first = 0;
164 }
165 }
166 putc('\n', fd);
d5568f13 167 if (!tracepackets && (rt->rt_state & RTS_PASSIVE) == 0 && rt->rt_ifp)
f036a54d
SL
168 dumpif(fd, rt->rt_ifp);
169 fflush(fd);
170}
171
172dumpif(fd, ifp)
173 register struct interface *ifp;
174{
b7e4f8be
MK
175 if (ifp->int_input.ifd_count || ifp->int_output.ifd_count) {
176 fprintf(fd, "*** Packet history for interface %s ***\n",
177 ifp->int_name);
178 dumptrace(fd, "to", &ifp->int_output);
179 dumptrace(fd, "from", &ifp->int_input);
180 fprintf(fd, "*** end packet history ***\n");
181 }
9bd44906 182 fflush(fd);
f036a54d
SL
183}
184
185dumptrace(fd, dir, ifd)
186 FILE *fd;
187 char *dir;
188 register struct ifdebug *ifd;
189{
190 register struct iftrace *t;
191 char *cp = !strcmp(dir, "to") ? "Output" : "Input";
192
193 if (ifd->ifd_front == ifd->ifd_records &&
194 ifd->ifd_front->ift_size == 0) {
195 fprintf(fd, "%s: no packets.\n", cp);
9bd44906 196 fflush(fd);
f036a54d
SL
197 return;
198 }
199 fprintf(fd, "%s trace:\n", cp);
b7e4f8be
MK
200 t = ifd->ifd_front - ifd->ifd_count;
201 if (t < ifd->ifd_records)
202 t += NRECORDS;
203 for ( ; ifd->ifd_count; ifd->ifd_count--, t++) {
204 if (t >= ifd->ifd_records + NRECORDS)
205 t = ifd->ifd_records;
f036a54d
SL
206 if (t->ift_size == 0)
207 continue;
208 fprintf(fd, "%.24s: metric=%d\n", ctime(&t->ift_stamp),
209 t->ift_metric);
210 dumppacket(fd, dir, &t->ift_who, t->ift_packet, t->ift_size);
211 }
212}
213
214dumppacket(fd, dir, who, cp, size)
215 FILE *fd;
216 struct sockaddr_in *who; /* should be sockaddr */
217 char *dir, *cp;
218 register int size;
219{
220 register struct rip *msg = (struct rip *)cp;
221 register struct netinfo *n;
222
223 if (msg->rip_cmd && msg->rip_cmd < RIPCMD_MAX)
2513c24a
SL
224 fprintf(fd, "%s %s %s.%d", ripcmds[msg->rip_cmd],
225 dir, inet_ntoa(who->sin_addr), ntohs(who->sin_port));
f036a54d
SL
226 else {
227 fprintf(fd, "Bad cmd 0x%x %s %x.%d\n", msg->rip_cmd,
2513c24a 228 dir, inet_ntoa(who->sin_addr), ntohs(who->sin_port));
f036a54d 229 fprintf(fd, "size=%d cp=%x packet=%x\n", size, cp, packet);
9bd44906 230 fflush(fd);
f036a54d
SL
231 return;
232 }
233 switch (msg->rip_cmd) {
234
235 case RIPCMD_REQUEST:
236 case RIPCMD_RESPONSE:
237 fprintf(fd, ":\n");
238 size -= 4 * sizeof (char);
239 n = msg->rip_nets;
240 for (; size > 0; n++, size -= sizeof (struct netinfo)) {
241 if (size < sizeof (struct netinfo))
242 break;
2513c24a
SL
243 fprintf(fd, "\tdst %s metric %d\n",
244#define satosin(sa) ((struct sockaddr_in *)&sa)
245 inet_ntoa(satosin(n->rip_dst)->sin_addr),
246 ntohl(n->rip_metric));
f036a54d
SL
247 }
248 break;
249
250 case RIPCMD_TRACEON:
251 fprintf(fd, ", file=%*s\n", size, msg->rip_tracefile);
252 break;
253
254 case RIPCMD_TRACEOFF:
255 fprintf(fd, "\n");
256 break;
257 }
9bd44906 258 fflush(fd);
f036a54d 259}