]>
git.saurik.com Git - apple/network_cmds.git/blob - routed.tproj/trace.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright (c) 1983, 1988, 1993
26 * The Regents of the University of California. All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgment:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * @(#)defs.h 8.1 (Berkeley) 6/5/93
61 * Routing Table Management Daemon
66 #include <sys/signal.h>
69 #include "pathnames.h"
71 #define NRECORDS 50 /* size of circular trace buffer */
73 FILE *ftrace
= stdout
;
76 static struct timeval lastlog
;
77 static char *savetracename
;
80 register struct interface
*ifp
;
82 static int iftraceinit();
84 if (iftraceinit(ifp
, &ifp
->int_input
) &&
85 iftraceinit(ifp
, &ifp
->int_output
))
88 fprintf(stderr
, "traceinit: can't init %s\n", ifp
->int_name
);
93 struct interface
*ifp
;
94 register struct ifdebug
*ifd
;
96 register struct iftrace
*t
;
99 (struct iftrace
*)malloc(NRECORDS
* sizeof (struct iftrace
));
100 if (ifd
->ifd_records
== 0)
102 ifd
->ifd_front
= ifd
->ifd_records
;
104 for (t
= ifd
->ifd_records
; t
< ifd
->ifd_records
+ NRECORDS
; t
++) {
119 if (stat(file
, &stbuf
) >= 0 && (stbuf
.st_mode
& S_IFMT
) != S_IFREG
)
121 savetracename
= file
;
122 (void) gettimeofday(&now
, (struct timezone
*)NULL
);
123 ftrace
= fopen(file
, "a");
126 dup2(fileno(ftrace
), 1);
127 dup2(fileno(ftrace
), 2);
129 fprintf(ftrace
, "Tracing enabled %s\n", ctime((time_t *)&now
.tv_sec
));
136 if (ftrace
!= NULL
) {
137 int fd
= open(_PATH_DEVNULL
, O_RDWR
);
139 fprintf(ftrace
, "Tracing disabled %s\n",
140 ctime((time_t *)&now
.tv_sec
));
161 else if (ftrace
== NULL
&& savetracename
)
162 traceon(savetracename
);
168 * Move to next higher level of tracing when -t option processed or
169 * SIGUSR1 is received. Successive levels are:
171 * traceactions + tracepackets
172 * traceactions + tracehistory (packets and contents after change)
173 * traceactions + tracepackets + tracecontents
178 (void) gettimeofday(&now
, (struct timezone
*)NULL
);
179 if (traceactions
== 0) {
182 fprintf(ftrace
, "Tracing actions started %s\n",
183 ctime((time_t *)&now
.tv_sec
));
184 } else if (tracepackets
== 0) {
189 fprintf(ftrace
, "Tracing packets started %s\n",
190 ctime((time_t *)&now
.tv_sec
));
191 } else if (tracehistory
== 0) {
194 fprintf(ftrace
, "Tracing history started %s\n",
195 ctime((time_t *)&now
.tv_sec
));
201 fprintf(ftrace
, "Tracing packet contents started %s\n",
202 ctime((time_t *)&now
.tv_sec
));
208 trace(ifd
, who
, p
, len
, m
)
209 register struct ifdebug
*ifd
;
210 struct sockaddr
*who
;
214 register struct iftrace
*t
;
216 if (ifd
->ifd_records
== 0)
218 t
= ifd
->ifd_front
++;
219 if (ifd
->ifd_front
>= ifd
->ifd_records
+ NRECORDS
)
220 ifd
->ifd_front
= ifd
->ifd_records
;
221 if (ifd
->ifd_count
< NRECORDS
)
223 if (t
->ift_size
> 0 && t
->ift_size
< len
&& t
->ift_packet
) {
229 if (len
> 0 && t
->ift_packet
== 0) {
230 t
->ift_packet
= malloc(len
);
231 if (t
->ift_packet
== 0)
235 memmove(t
->ift_packet
, p
, len
);
240 traceaction(fd
, action
, rt
)
245 struct sockaddr_in
*dst
, *gate
;
251 { RTF_GATEWAY
, "GATEWAY" },
252 { RTF_HOST
, "HOST" },
255 { RTS_PASSIVE
, "PASSIVE" },
256 { RTS_REMOTE
, "REMOTE" },
257 { RTS_INTERFACE
,"INTERFACE" },
258 { RTS_CHANGED
, "CHANGED" },
259 { RTS_INTERNAL
, "INTERNAL" },
260 { RTS_EXTERNAL
, "EXTERNAL" },
261 { RTS_SUBNET
, "SUBNET" },
264 register struct bits
*p
;
267 struct interface
*ifp
;
271 if (lastlog
.tv_sec
!= now
.tv_sec
|| lastlog
.tv_usec
!= now
.tv_usec
) {
272 fprintf(fd
, "\n%.19s:\n", ctime((time_t *)&now
.tv_sec
));
275 fprintf(fd
, "%s ", action
);
276 dst
= (struct sockaddr_in
*)&rt
->rt_dst
;
277 gate
= (struct sockaddr_in
*)&rt
->rt_router
;
278 fprintf(fd
, "dst %s, ", inet_ntoa(dst
->sin_addr
));
279 fprintf(fd
, "router %s, metric %d, flags",
280 inet_ntoa(gate
->sin_addr
), rt
->rt_metric
);
282 for (first
= 1, p
= flagbits
; p
->t_bits
> 0; p
++) {
283 if ((rt
->rt_flags
& p
->t_bits
) == 0)
285 fprintf(fd
, cp
, p
->t_name
);
291 fprintf(fd
, " state");
293 for (first
= 1, p
= statebits
; p
->t_bits
> 0; p
++) {
294 if ((rt
->rt_state
& p
->t_bits
) == 0)
296 fprintf(fd
, cp
, p
->t_name
);
302 fprintf(fd
, " timer %d\n", rt
->rt_timer
);
303 if (tracehistory
&& !tracepackets
&&
304 (rt
->rt_state
& RTS_PASSIVE
) == 0 && rt
->rt_ifp
)
305 dumpif(fd
, rt
->rt_ifp
);
311 tracenewmetric(fd
, rt
, newmetric
)
316 struct sockaddr_in
*dst
, *gate
;
320 if (lastlog
.tv_sec
!= now
.tv_sec
|| lastlog
.tv_usec
!= now
.tv_usec
) {
321 fprintf(fd
, "\n%.19s:\n", ctime((time_t *)&now
.tv_sec
));
324 dst
= (struct sockaddr_in
*)&rt
->rt_dst
;
325 gate
= (struct sockaddr_in
*)&rt
->rt_router
;
326 fprintf(fd
, "CHANGE metric dst %s, ", inet_ntoa(dst
->sin_addr
));
327 fprintf(fd
, "router %s, from %d to %d\n",
328 inet_ntoa(gate
->sin_addr
), rt
->rt_metric
, newmetric
);
336 register struct interface
*ifp
;
338 if (ifp
->int_input
.ifd_count
|| ifp
->int_output
.ifd_count
) {
339 fprintf(fd
, "*** Packet history for interface %s ***\n",
342 dumptrace(fd
, "to", &ifp
->int_output
);
344 dumptrace(fd
, "from", &ifp
->int_input
);
345 fprintf(fd
, "*** end packet history ***\n");
349 dumptrace(fd
, dir
, ifd
)
352 register struct ifdebug
*ifd
;
354 register struct iftrace
*t
;
355 char *cp
= !strcmp(dir
, "to") ? "Output" : "Input";
357 if (ifd
->ifd_front
== ifd
->ifd_records
&&
358 ifd
->ifd_front
->ift_size
== 0) {
359 fprintf(fd
, "%s: no packets.\n", cp
);
363 fprintf(fd
, "%s trace:\n", cp
);
364 t
= ifd
->ifd_front
- ifd
->ifd_count
;
365 if (t
< ifd
->ifd_records
)
367 for ( ; ifd
->ifd_count
; ifd
->ifd_count
--, t
++) {
368 if (t
>= ifd
->ifd_records
+ NRECORDS
)
369 t
= ifd
->ifd_records
;
370 if (t
->ift_size
== 0)
372 dumppacket(fd
, dir
, &t
->ift_who
, t
->ift_packet
, t
->ift_size
,
377 dumppacket(fd
, dir
, who
, cp
, size
, stamp
)
379 struct sockaddr_in
*who
; /* should be sockaddr */
382 struct timeval
*stamp
;
384 register struct rip
*msg
= (struct rip
*)cp
;
385 register struct netinfo
*n
;
389 if (msg
->rip_cmd
&& msg
->rip_cmd
< RIPCMD_MAX
)
390 fprintf(fd
, "%s %s %s.%d %.19s:\n", ripcmds
[msg
->rip_cmd
],
391 dir
, inet_ntoa(who
->sin_addr
), ntohs(who
->sin_port
),
392 ctime((time_t *)&stamp
->tv_sec
));
394 fprintf(fd
, "Bad cmd 0x%x %s %x.%d %.19s\n", msg
->rip_cmd
,
395 dir
, inet_ntoa(who
->sin_addr
), ntohs(who
->sin_port
));
396 fprintf(fd
, "size=%d cp=%x packet=%x\n", size
, cp
, packet
,
397 ctime((time_t *)&stamp
->tv_sec
));
401 if (tracepackets
&& tracecontents
== 0) {
405 switch (msg
->rip_cmd
) {
408 case RIPCMD_RESPONSE
:
409 size
-= 4 * sizeof (char);
411 for (; size
> 0; n
++, size
-= sizeof (struct netinfo
)) {
412 if (size
< sizeof (struct netinfo
)) {
413 fprintf(fd
, "(truncated record, len %d)\n",
417 if (sizeof(n
->rip_dst
.sa_family
) > 1)
418 n
->rip_dst
.sa_family
= ntohs(n
->rip_dst
.sa_family
);
420 switch ((int)n
->rip_dst
.sa_family
) {
423 fprintf(fd
, "\tdst %s metric %d\n",
424 #define satosin(sa) ((struct sockaddr_in *)&sa)
425 inet_ntoa(satosin(n
->rip_dst
)->sin_addr
),
426 ntohl(n
->rip_metric
));
430 fprintf(fd
, "\taf %d? metric %d\n",
431 n
->rip_dst
.sa_family
,
432 ntohl(n
->rip_metric
));
439 fprintf(fd
, "\tfile=%*s\n", size
, msg
->rip_tracefile
);
442 case RIPCMD_TRACEOFF
: