Commit | Line | Data |
---|---|---|
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 |
5ff67f98 DF |
8 | static char sccsid[] = "@(#)trace.c 5.1 (Berkeley) %G%"; |
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 | |
19 | FILE *ftrace = stdout; | |
20 | int tracing = 1; | |
21 | #endif | |
22 | ||
23 | traceinit(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 | ||
34 | static | |
35 | iftraceinit(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 | ||
55 | traceon(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 | ||
69 | traceoff() | |
70 | { | |
71 | if (!tracing) | |
72 | return; | |
73 | if (ftrace != NULL) | |
74 | fclose(ftrace); | |
75 | ftrace = NULL; | |
76 | tracing = 0; | |
77 | } | |
78 | ||
79 | trace(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++; | |
f036a54d SL |
94 | if (t->ift_size > 0 && t->ift_packet) |
95 | free(t->ift_packet); | |
96 | t->ift_packet = 0; | |
97 | t->ift_stamp = time(0); | |
98 | t->ift_who = *who; | |
99 | if (len > 0) { | |
100 | t->ift_packet = malloc(len); | |
101 | if (t->ift_packet) | |
102 | bcopy(p, t->ift_packet, len); | |
103 | else | |
104 | len = 0; | |
105 | } | |
106 | t->ift_size = len; | |
107 | t->ift_metric = m; | |
108 | } | |
109 | ||
110 | traceaction(fd, action, rt) | |
111 | FILE *fd; | |
112 | char *action; | |
113 | struct rt_entry *rt; | |
114 | { | |
115 | struct sockaddr_in *dst, *gate; | |
116 | static struct bits { | |
117 | int t_bits; | |
118 | char *t_name; | |
119 | } flagbits[] = { | |
120 | { RTF_UP, "UP" }, | |
121 | { RTF_GATEWAY, "GATEWAY" }, | |
122 | { RTF_HOST, "HOST" }, | |
123 | { 0 } | |
124 | }, statebits[] = { | |
125 | { RTS_PASSIVE, "PASSIVE" }, | |
126 | { RTS_REMOTE, "REMOTE" }, | |
127 | { RTS_INTERFACE,"INTERFACE" }, | |
128 | { RTS_CHANGED, "CHANGED" }, | |
129 | { 0 } | |
130 | }; | |
131 | register struct bits *p; | |
132 | register int first; | |
133 | char *cp; | |
134 | struct interface *ifp; | |
135 | ||
136 | if (fd == NULL) | |
137 | return; | |
138 | fprintf(fd, "%s ", action); | |
139 | dst = (struct sockaddr_in *)&rt->rt_dst; | |
140 | gate = (struct sockaddr_in *)&rt->rt_router; | |
2513c24a SL |
141 | fprintf(fd, "dst %s, ", inet_ntoa(dst->sin_addr)); |
142 | fprintf(fd, "router %s, metric %d, flags", | |
143 | inet_ntoa(gate->sin_addr), rt->rt_metric); | |
f036a54d SL |
144 | cp = " %s"; |
145 | for (first = 1, p = flagbits; p->t_bits > 0; p++) { | |
146 | if ((rt->rt_flags & p->t_bits) == 0) | |
147 | continue; | |
148 | fprintf(fd, cp, p->t_name); | |
149 | if (first) { | |
150 | cp = "|%s"; | |
151 | first = 0; | |
152 | } | |
153 | } | |
154 | fprintf(fd, " state"); | |
155 | cp = " %s"; | |
156 | for (first = 1, p = statebits; p->t_bits > 0; p++) { | |
157 | if ((rt->rt_state & p->t_bits) == 0) | |
158 | continue; | |
159 | fprintf(fd, cp, p->t_name); | |
160 | if (first) { | |
161 | cp = "|%s"; | |
162 | first = 0; | |
163 | } | |
164 | } | |
165 | putc('\n', fd); | |
d5568f13 | 166 | if (!tracepackets && (rt->rt_state & RTS_PASSIVE) == 0 && rt->rt_ifp) |
f036a54d SL |
167 | dumpif(fd, rt->rt_ifp); |
168 | fflush(fd); | |
169 | } | |
170 | ||
171 | dumpif(fd, ifp) | |
172 | register struct interface *ifp; | |
173 | { | |
b7e4f8be MK |
174 | if (ifp->int_input.ifd_count || ifp->int_output.ifd_count) { |
175 | fprintf(fd, "*** Packet history for interface %s ***\n", | |
176 | ifp->int_name); | |
177 | dumptrace(fd, "to", &ifp->int_output); | |
178 | dumptrace(fd, "from", &ifp->int_input); | |
179 | fprintf(fd, "*** end packet history ***\n"); | |
180 | } | |
f036a54d SL |
181 | } |
182 | ||
183 | dumptrace(fd, dir, ifd) | |
184 | FILE *fd; | |
185 | char *dir; | |
186 | register struct ifdebug *ifd; | |
187 | { | |
188 | register struct iftrace *t; | |
189 | char *cp = !strcmp(dir, "to") ? "Output" : "Input"; | |
190 | ||
191 | if (ifd->ifd_front == ifd->ifd_records && | |
192 | ifd->ifd_front->ift_size == 0) { | |
193 | fprintf(fd, "%s: no packets.\n", cp); | |
194 | return; | |
195 | } | |
196 | fprintf(fd, "%s trace:\n", cp); | |
b7e4f8be MK |
197 | t = ifd->ifd_front - ifd->ifd_count; |
198 | if (t < ifd->ifd_records) | |
199 | t += NRECORDS; | |
200 | for ( ; ifd->ifd_count; ifd->ifd_count--, t++) { | |
201 | if (t >= ifd->ifd_records + NRECORDS) | |
202 | t = ifd->ifd_records; | |
f036a54d SL |
203 | if (t->ift_size == 0) |
204 | continue; | |
205 | fprintf(fd, "%.24s: metric=%d\n", ctime(&t->ift_stamp), | |
206 | t->ift_metric); | |
207 | dumppacket(fd, dir, &t->ift_who, t->ift_packet, t->ift_size); | |
208 | } | |
209 | } | |
210 | ||
211 | dumppacket(fd, dir, who, cp, size) | |
212 | FILE *fd; | |
213 | struct sockaddr_in *who; /* should be sockaddr */ | |
214 | char *dir, *cp; | |
215 | register int size; | |
216 | { | |
217 | register struct rip *msg = (struct rip *)cp; | |
218 | register struct netinfo *n; | |
219 | ||
220 | if (msg->rip_cmd && msg->rip_cmd < RIPCMD_MAX) | |
2513c24a SL |
221 | fprintf(fd, "%s %s %s.%d", ripcmds[msg->rip_cmd], |
222 | dir, inet_ntoa(who->sin_addr), ntohs(who->sin_port)); | |
f036a54d SL |
223 | else { |
224 | fprintf(fd, "Bad cmd 0x%x %s %x.%d\n", msg->rip_cmd, | |
2513c24a | 225 | dir, inet_ntoa(who->sin_addr), ntohs(who->sin_port)); |
f036a54d SL |
226 | fprintf(fd, "size=%d cp=%x packet=%x\n", size, cp, packet); |
227 | return; | |
228 | } | |
229 | switch (msg->rip_cmd) { | |
230 | ||
231 | case RIPCMD_REQUEST: | |
232 | case RIPCMD_RESPONSE: | |
233 | fprintf(fd, ":\n"); | |
234 | size -= 4 * sizeof (char); | |
235 | n = msg->rip_nets; | |
236 | for (; size > 0; n++, size -= sizeof (struct netinfo)) { | |
237 | if (size < sizeof (struct netinfo)) | |
238 | break; | |
2513c24a SL |
239 | fprintf(fd, "\tdst %s metric %d\n", |
240 | #define satosin(sa) ((struct sockaddr_in *)&sa) | |
241 | inet_ntoa(satosin(n->rip_dst)->sin_addr), | |
242 | ntohl(n->rip_metric)); | |
f036a54d SL |
243 | } |
244 | break; | |
245 | ||
246 | case RIPCMD_TRACEON: | |
247 | fprintf(fd, ", file=%*s\n", size, msg->rip_tracefile); | |
248 | break; | |
249 | ||
250 | case RIPCMD_TRACEOFF: | |
251 | fprintf(fd, "\n"); | |
252 | break; | |
253 | } | |
254 | } |