xref: /openbsd-src/usr.sbin/tcpdump/print-timed.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: print-timed.c,v 1.1 2000/12/07 22:52:00 mickey Exp $	*/
2 
3 /*
4  * Copyright (c) 2000 Ben Smithurst <ben@scientia.demon.co.uk>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that: (1) source code distributions
9  * retain the above copyright notice and this paragraph in its entirety, (2)
10  * distributions including binary code include the above copyright notice and
11  * this paragraph in its entirety in the documentation or other materials
12  * provided with the distribution, and (3) all advertising materials mentioning
13  * features or use of this software display the following acknowledgement:
14  * ``This product includes software developed by the University of California,
15  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16  * the University nor the names of its contributors may be used to endorse
17  * or promote products derived from this software without specific prior
18  * written permission.
19  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22  *
23  * from tcpdump.org:
24  * Header: /tcpdump/master/tcpdump/print-timed.c,v 1.1 2000/10/06 05:35:37 guy Exp
25  *
26  */
27 
28 #ifndef lint
29 static const char rcsid[] =
30     "@(#) $Header: /home/cvs/src/usr.sbin/tcpdump/print-timed.c,v 1.1 2000/12/07 22:52:00 mickey Exp $";
31 #endif
32 
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36 
37 #include <sys/param.h>
38 #include <sys/time.h>
39 
40 #include <netinet/in.h>
41 
42 #include <protocols/timed.h>
43 
44 #include <stdio.h>
45 #include <string.h>
46 
47 #include "interface.h"
48 #include "addrtoname.h"
49 #include "extract.h"                    /* must come after interface.h */
50 
51 static char *tsptype[TSPTYPENUMBER] =
52   { "ANY", "ADJTIME", "ACK", "MASTERREQ", "MASTERACK", "SETTIME", "MASTERUP",
53   "SLAVEUP", "ELECTION", "ACCEPT", "REFUSE", "CONFLICT", "RESOLVE", "QUIT",
54   "DATE", "DATEREQ", "DATEACK", "TRACEON", "TRACEOFF", "MSITE", "MSITEREQ",
55   "TEST", "SETDATE", "SETDATEREQ", "LOOP" };
56 
57 void
58 timed_print(register const u_char *bp, u_int length)
59 {
60 #define endof(x) ((u_char *)&(x) + sizeof (x))
61 	struct tsp *tsp = (struct tsp *)bp;
62 	long sec, usec;
63 	const u_char *end;
64 
65 	if (endof(tsp->tsp_type) > snapend) {
66 		fputs("[|timed]", stdout);
67 		return;
68 	}
69 	if (tsp->tsp_type < TSPTYPENUMBER)
70 		printf("%s", tsptype[tsp->tsp_type]);
71 	else
72 		printf("(tsp_type %#x)", tsp->tsp_type);
73 
74 	if (endof(tsp->tsp_vers) > snapend) {
75 		fputs(" [|timed]", stdout);
76 		return;
77 	}
78 	printf(" vers %d", tsp->tsp_vers);
79 
80 	if (endof(tsp->tsp_seq) > snapend) {
81 		fputs(" [|timed]", stdout);
82 		return;
83 	}
84 	printf(" seq %d", tsp->tsp_seq);
85 
86 	if (tsp->tsp_type == TSP_LOOP) {
87 		if (endof(tsp->tsp_hopcnt) > snapend) {
88 			fputs(" [|timed]", stdout);
89 			return;
90 		}
91 		printf(" hopcnt %d", tsp->tsp_hopcnt);
92 	} else if (tsp->tsp_type == TSP_SETTIME ||
93 	    tsp->tsp_type == TSP_ADJTIME ||
94 	    tsp->tsp_type == TSP_SETDATE ||
95 	    tsp->tsp_type == TSP_SETDATEREQ) {
96 		if (endof(tsp->tsp_time) > snapend) {
97 			fputs(" [|timed]", stdout);
98 			return;
99 		}
100 		sec = ntohl((long)tsp->tsp_time.tv_sec);
101 		usec = ntohl((long)tsp->tsp_time.tv_usec);
102 		if (usec < 0)
103 			/* corrupt, skip the rest of the packet */
104 			return;
105 		fputs(" time ", stdout);
106 		if (sec < 0 && usec != 0) {
107 			sec++;
108 			if (sec == 0)
109 				fputc('-', stdout);
110 			usec = 1000000 - usec;
111 		}
112 		printf("%ld.%06ld", sec, usec);
113 	}
114 
115 	end = memchr(tsp->tsp_name, '\0', snapend - (u_char *)tsp->tsp_name);
116 	if (end == NULL)
117 		fputs(" [|timed]", stdout);
118 	else {
119 		fputs(" name ", stdout);
120 		fwrite(tsp->tsp_name, end - (u_char *)tsp->tsp_name, 1, stdout);
121 	}
122 }
123