xref: /openbsd-src/usr.sbin/tcpdump/print-timed.c (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1 /*	$OpenBSD: print-timed.c,v 1.5 2009/10/27 23:59:57 deraadt 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 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31 
32 #include <sys/param.h>
33 #include <sys/time.h>
34 
35 #include <netinet/in.h>
36 
37 #include <protocols/timed.h>
38 
39 #include <stdio.h>
40 #include <string.h>
41 
42 #include "interface.h"
43 #include "addrtoname.h"
44 #include "extract.h"                    /* must come after interface.h */
45 
46 static const char *tsptype[TSPTYPENUMBER] =
47   { "ANY", "ADJTIME", "ACK", "MASTERREQ", "MASTERACK", "SETTIME", "MASTERUP",
48   "SLAVEUP", "ELECTION", "ACCEPT", "REFUSE", "CONFLICT", "RESOLVE", "QUIT",
49   "DATE", "DATEREQ", "DATEACK", "TRACEON", "TRACEOFF", "MSITE", "MSITEREQ",
50   "TEST", "SETDATE", "SETDATEREQ", "LOOP" };
51 
52 void
53 timed_print(register const u_char *bp, u_int length)
54 {
55 #define endof(x) ((u_char *)&(x) + sizeof (x))
56 	struct tsp *tsp = (struct tsp *)bp;
57 	long sec, usec;
58 	const u_char *end;
59 
60 	TCHECK(tsp->tsp_type);
61 	if (tsp->tsp_type < TSPTYPENUMBER)
62 		printf("TSP_%s", tsptype[tsp->tsp_type]);
63 	else
64 		printf("(tsp_type %#x)", tsp->tsp_type);
65 
66 	TCHECK(tsp->tsp_vers);
67 	printf(" vers %d", tsp->tsp_vers);
68 
69 	TCHECK(tsp->tsp_seq);
70 	printf(" seq %d", tsp->tsp_seq);
71 
72 	if (tsp->tsp_type == TSP_LOOP) {
73 		TCHECK(tsp->tsp_hopcnt);
74 		printf(" hopcnt %d", tsp->tsp_hopcnt);
75 	} else if (tsp->tsp_type == TSP_SETTIME ||
76 	    tsp->tsp_type == TSP_ADJTIME ||
77 	    tsp->tsp_type == TSP_SETDATE ||
78 	    tsp->tsp_type == TSP_SETDATEREQ) {
79 		TCHECK(tsp->tsp_time);
80 		sec = EXTRACT_32BITS(&tsp->tsp_time.tv_sec);
81 		usec = EXTRACT_32BITS(&tsp->tsp_time.tv_usec);
82 		if (usec < 0)
83 			/* corrupt, skip the rest of the packet */
84 			return;
85 		fputs(" time ", stdout);
86 		if (sec < 0 && usec != 0) {
87 			sec++;
88 			if (sec == 0)
89 				fputc('-', stdout);
90 			usec = 1000000 - usec;
91 		}
92 		printf("%ld.%06ld", sec, usec);
93 	}
94 
95 	end = endof(tsp->tsp_name) > snapend ? snapend : endof(tsp->tsp_name);
96 	fputs(" name ", stdout);
97 	if (fn_print(tsp->tsp_name, end))
98 		goto trunc;
99 	return;
100 trunc:
101 	fputs(" [|timed]", stdout);
102 }
103