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