Commit | Line | Data |
---|---|---|
e1ffbff4 | 1 | /* |
3565c602 KB |
2 | * Copyright (c) 1985 The Regents of the University of California. |
3 | * All rights reserved. | |
e1ffbff4 | 4 | * |
3565c602 KB |
5 | * This file includes significant work done at Cornell University by |
6 | * Bill Nesheim. That work included by permission. | |
7 | * | |
af359dea C |
8 | * Redistribution and use in source and binary forms, with or without |
9 | * modification, are permitted provided that the following conditions | |
10 | * are met: | |
11 | * 1. Redistributions of source code must retain the above copyright | |
12 | * notice, this list of conditions and the following disclaimer. | |
13 | * 2. Redistributions in binary form must reproduce the above copyright | |
14 | * notice, this list of conditions and the following disclaimer in the | |
15 | * documentation and/or other materials provided with the distribution. | |
16 | * 3. All advertising materials mentioning features or use of this software | |
17 | * must display the following acknowledgement: | |
18 | * This product includes software developed by the University of | |
19 | * California, Berkeley and its contributors. | |
20 | * 4. Neither the name of the University nor the names of its contributors | |
21 | * may be used to endorse or promote products derived from this software | |
22 | * without specific prior written permission. | |
23 | * | |
24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
34 | * SUCH DAMAGE. | |
e1ffbff4 KS |
35 | */ |
36 | ||
809d6545 | 37 | #ifndef lint |
af359dea | 38 | static char sccsid[] = "@(#)trace.c 5.11 (Berkeley) 2/26/91"; |
3565c602 | 39 | #endif /* not lint */ |
809d6545 KS |
40 | |
41 | /* | |
42 | * Routing Table Management Daemon | |
43 | */ | |
44 | #define RIPCMDS | |
cd8d2522 | 45 | #include <stdlib.h> |
809d6545 KS |
46 | #include "defs.h" |
47 | ||
48 | #define NRECORDS 50 /* size of circular trace buffer */ | |
49 | #ifdef DEBUG | |
50 | FILE *ftrace = stdout; | |
51 | int tracing = 1; | |
610b111d KS |
52 | #else DEBUG |
53 | FILE *ftrace = NULL; | |
54 | int tracing = 0; | |
809d6545 KS |
55 | #endif |
56 | ||
57 | char *xns_ntoa(); | |
58 | ||
59 | traceinit(ifp) | |
60 | register struct interface *ifp; | |
61 | { | |
3bafd13e | 62 | static int iftraceinit(); |
809d6545 KS |
63 | |
64 | if (iftraceinit(ifp, &ifp->int_input) && | |
65 | iftraceinit(ifp, &ifp->int_output)) | |
66 | return; | |
67 | tracing = 0; | |
610b111d | 68 | syslog(LOG_ERR, "traceinit: can't init %s\n", ifp->int_name); |
809d6545 KS |
69 | } |
70 | ||
71 | static | |
72 | iftraceinit(ifp, ifd) | |
73 | struct interface *ifp; | |
74 | register struct ifdebug *ifd; | |
75 | { | |
76 | register struct iftrace *t; | |
77 | ||
78 | ifd->ifd_records = | |
79 | (struct iftrace *)malloc(NRECORDS * sizeof (struct iftrace)); | |
80 | if (ifd->ifd_records == 0) | |
81 | return (0); | |
82 | ifd->ifd_front = ifd->ifd_records; | |
83 | ifd->ifd_count = 0; | |
84 | for (t = ifd->ifd_records; t < ifd->ifd_records + NRECORDS; t++) { | |
85 | t->ift_size = 0; | |
86 | t->ift_packet = 0; | |
87 | } | |
88 | ifd->ifd_if = ifp; | |
89 | return (1); | |
90 | } | |
91 | ||
92 | traceon(file) | |
93 | char *file; | |
94 | { | |
95 | ||
96 | if (ftrace != NULL) | |
97 | return; | |
98 | ftrace = fopen(file, "a"); | |
99 | if (ftrace == NULL) | |
100 | return; | |
101 | dup2(fileno(ftrace), 1); | |
102 | dup2(fileno(ftrace), 2); | |
103 | tracing = 1; | |
104 | } | |
105 | ||
106 | traceoff() | |
107 | { | |
108 | if (!tracing) | |
109 | return; | |
110 | if (ftrace != NULL) | |
111 | fclose(ftrace); | |
112 | ftrace = NULL; | |
113 | tracing = 0; | |
114 | } | |
115 | ||
116 | trace(ifd, who, p, len, m) | |
117 | register struct ifdebug *ifd; | |
118 | struct sockaddr *who; | |
119 | char *p; | |
120 | int len, m; | |
121 | { | |
122 | register struct iftrace *t; | |
123 | ||
124 | if (ifd->ifd_records == 0) | |
125 | return; | |
126 | t = ifd->ifd_front++; | |
127 | if (ifd->ifd_front >= ifd->ifd_records + NRECORDS) | |
128 | ifd->ifd_front = ifd->ifd_records; | |
129 | if (ifd->ifd_count < NRECORDS) | |
130 | ifd->ifd_count++; | |
131 | if (t->ift_size > 0 && t->ift_packet) | |
132 | free(t->ift_packet); | |
133 | t->ift_packet = 0; | |
134 | t->ift_stamp = time(0); | |
135 | t->ift_who = *who; | |
136 | if (len > 0) { | |
137 | t->ift_packet = malloc(len); | |
138 | if (t->ift_packet) | |
139 | bcopy(p, t->ift_packet, len); | |
140 | else | |
141 | len = 0; | |
142 | } | |
143 | t->ift_size = len; | |
144 | t->ift_metric = m; | |
145 | } | |
146 | ||
147 | traceaction(fd, action, rt) | |
148 | FILE *fd; | |
149 | char *action; | |
150 | struct rt_entry *rt; | |
151 | { | |
48681466 | 152 | struct sockaddr_ns *dst, *gate; |
809d6545 KS |
153 | static struct bits { |
154 | int t_bits; | |
155 | char *t_name; | |
156 | } flagbits[] = { | |
157 | { RTF_UP, "UP" }, | |
158 | { RTF_GATEWAY, "GATEWAY" }, | |
159 | { RTF_HOST, "HOST" }, | |
160 | { 0 } | |
161 | }, statebits[] = { | |
162 | { RTS_PASSIVE, "PASSIVE" }, | |
163 | { RTS_REMOTE, "REMOTE" }, | |
164 | { RTS_INTERFACE,"INTERFACE" }, | |
165 | { RTS_CHANGED, "CHANGED" }, | |
166 | { 0 } | |
167 | }; | |
168 | register struct bits *p; | |
169 | register int first; | |
170 | char *cp; | |
171 | struct interface *ifp; | |
172 | ||
173 | if (fd == NULL) | |
174 | return; | |
175 | fprintf(fd, "%s ", action); | |
48681466 KS |
176 | dst = (struct sockaddr_ns *)&rt->rt_dst; |
177 | gate = (struct sockaddr_ns *)&rt->rt_router; | |
178 | fprintf(fd, "dst %s, ", xns_ntoa(&dst->sns_addr)); | |
809d6545 | 179 | fprintf(fd, "router %s, metric %d, flags", |
48681466 | 180 | xns_ntoa(&gate->sns_addr), rt->rt_metric); |
809d6545 KS |
181 | cp = " %s"; |
182 | for (first = 1, p = flagbits; p->t_bits > 0; p++) { | |
183 | if ((rt->rt_flags & p->t_bits) == 0) | |
184 | continue; | |
185 | fprintf(fd, cp, p->t_name); | |
186 | if (first) { | |
187 | cp = "|%s"; | |
188 | first = 0; | |
189 | } | |
190 | } | |
191 | fprintf(fd, " state"); | |
192 | cp = " %s"; | |
193 | for (first = 1, p = statebits; p->t_bits > 0; p++) { | |
194 | if ((rt->rt_state & p->t_bits) == 0) | |
195 | continue; | |
196 | fprintf(fd, cp, p->t_name); | |
197 | if (first) { | |
198 | cp = "|%s"; | |
199 | first = 0; | |
200 | } | |
201 | } | |
202 | putc('\n', fd); | |
203 | if (!tracepackets && (rt->rt_state & RTS_PASSIVE) == 0 && rt->rt_ifp) | |
204 | dumpif(fd, rt->rt_ifp); | |
205 | fflush(fd); | |
206 | } | |
207 | ||
208 | dumpif(fd, ifp) | |
209 | register struct interface *ifp; | |
610b111d | 210 | FILE *fd; |
809d6545 KS |
211 | { |
212 | if (ifp->int_input.ifd_count || ifp->int_output.ifd_count) { | |
213 | fprintf(fd, "*** Packet history for interface %s ***\n", | |
214 | ifp->int_name); | |
215 | dumptrace(fd, "to", &ifp->int_output); | |
216 | dumptrace(fd, "from", &ifp->int_input); | |
217 | fprintf(fd, "*** end packet history ***\n"); | |
218 | } | |
219 | } | |
220 | ||
221 | dumptrace(fd, dir, ifd) | |
222 | FILE *fd; | |
223 | char *dir; | |
224 | register struct ifdebug *ifd; | |
225 | { | |
226 | register struct iftrace *t; | |
227 | char *cp = !strcmp(dir, "to") ? "Output" : "Input"; | |
228 | ||
229 | if (ifd->ifd_front == ifd->ifd_records && | |
230 | ifd->ifd_front->ift_size == 0) { | |
231 | fprintf(fd, "%s: no packets.\n", cp); | |
232 | return; | |
233 | } | |
234 | fprintf(fd, "%s trace:\n", cp); | |
235 | t = ifd->ifd_front - ifd->ifd_count; | |
236 | if (t < ifd->ifd_records) | |
237 | t += NRECORDS; | |
238 | for ( ; ifd->ifd_count; ifd->ifd_count--, t++) { | |
239 | if (t >= ifd->ifd_records + NRECORDS) | |
240 | t = ifd->ifd_records; | |
241 | if (t->ift_size == 0) | |
242 | continue; | |
243 | fprintf(fd, "%.24s: metric=%d\n", ctime(&t->ift_stamp), | |
244 | t->ift_metric); | |
245 | dumppacket(fd, dir, &t->ift_who, t->ift_packet, t->ift_size); | |
246 | } | |
247 | } | |
248 | ||
249 | dumppacket(fd, dir, who, cp, size) | |
250 | FILE *fd; | |
48681466 | 251 | struct sockaddr_ns *who; /* should be sockaddr */ |
809d6545 KS |
252 | char *dir, *cp; |
253 | register int size; | |
254 | { | |
255 | register struct rip *msg = (struct rip *)cp; | |
256 | register struct netinfo *n; | |
0a5735f6 | 257 | char *xns_nettoa(); |
809d6545 KS |
258 | |
259 | if (msg->rip_cmd && ntohs(msg->rip_cmd) < RIPCMD_MAX) | |
260 | fprintf(fd, "%s %s %s#%x", ripcmds[ntohs(msg->rip_cmd)], | |
48681466 | 261 | dir, xns_ntoa(&who->sns_addr), ntohs(who->sns_addr.x_port)); |
809d6545 KS |
262 | else { |
263 | fprintf(fd, "Bad cmd 0x%x %s %s#%x\n", ntohs(msg->rip_cmd), | |
48681466 | 264 | dir, xns_ntoa(&who->sns_addr), ntohs(who->sns_addr.x_port)); |
809d6545 KS |
265 | fprintf(fd, "size=%d cp=%x packet=%x\n", size, cp, packet); |
266 | return; | |
267 | } | |
268 | switch (ntohs(msg->rip_cmd)) { | |
269 | ||
270 | case RIPCMD_REQUEST: | |
271 | case RIPCMD_RESPONSE: | |
272 | fprintf(fd, ":\n"); | |
273 | size -= sizeof (u_short); | |
274 | n = msg->rip_nets; | |
275 | for (; size > 0; n++, size -= sizeof (struct netinfo)) { | |
276 | if (size < sizeof (struct netinfo)) | |
277 | break; | |
0a5735f6 KS |
278 | fprintf(fd, "\tnet %s metric %d\n", |
279 | xns_nettoa(n->rip_dst), | |
809d6545 KS |
280 | ntohs(n->rip_metric)); |
281 | } | |
282 | break; | |
283 | ||
284 | } | |
285 | } | |
286 | ||
0a5735f6 KS |
287 | union ns_net_u net; |
288 | ||
289 | char * | |
290 | xns_nettoa(val) | |
291 | union ns_net val; | |
292 | { | |
293 | static char buf[100]; | |
294 | net.net_e = val; | |
9bd38ba8 | 295 | (void)sprintf(buf, "%lx", ntohl(net.long_e)); |
0a5735f6 KS |
296 | return (buf); |
297 | } | |
298 | ||
299 | ||
809d6545 KS |
300 | char * |
301 | xns_ntoa(addr) | |
48681466 | 302 | struct ns_addr *addr; |
809d6545 KS |
303 | { |
304 | static char buf[100]; | |
305 | ||
9bd38ba8 | 306 | (void)sprintf(buf, "%s#%x:%x:%x:%x:%x:%x", |
0a5735f6 | 307 | xns_nettoa(addr->x_net), |
48681466 KS |
308 | addr->x_host.c_host[0], addr->x_host.c_host[1], |
309 | addr->x_host.c_host[2], addr->x_host.c_host[3], | |
310 | addr->x_host.c_host[4], addr->x_host.c_host[5]); | |
809d6545 KS |
311 | |
312 | return(buf); | |
313 | } |