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