xref: /openbsd-src/usr.sbin/tcpdump/print-timed.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: print-timed.c,v 1.4 2007/10/07 16:41:05 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 #ifndef lint
29 static const char rcsid[] =
30     "@(#) $Id: print-timed.c,v 1.4 2007/10/07 16:41:05 deraadt 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 const 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 	TCHECK(tsp->tsp_type);
66 	if (tsp->tsp_type < TSPTYPENUMBER)
67 		printf("TSP_%s", tsptype[tsp->tsp_type]);
68 	else
69 		printf("(tsp_type %#x)", tsp->tsp_type);
70 
71 	TCHECK(tsp->tsp_vers);
72 	printf(" vers %d", tsp->tsp_vers);
73 
74 	TCHECK(tsp->tsp_seq);
75 	printf(" seq %d", tsp->tsp_seq);
76 
77 	if (tsp->tsp_type == TSP_LOOP) {
78 		TCHECK(tsp->tsp_hopcnt);
79 		printf(" hopcnt %d", tsp->tsp_hopcnt);
80 	} else if (tsp->tsp_type == TSP_SETTIME ||
81 	    tsp->tsp_type == TSP_ADJTIME ||
82 	    tsp->tsp_type == TSP_SETDATE ||
83 	    tsp->tsp_type == TSP_SETDATEREQ) {
84 		TCHECK(tsp->tsp_time);
85 		sec = EXTRACT_32BITS(&tsp->tsp_time.tv_sec);
86 		usec = EXTRACT_32BITS(&tsp->tsp_time.tv_usec);
87 		if (usec < 0)
88 			/* corrupt, skip the rest of the packet */
89 			return;
90 		fputs(" time ", stdout);
91 		if (sec < 0 && usec != 0) {
92 			sec++;
93 			if (sec == 0)
94 				fputc('-', stdout);
95 			usec = 1000000 - usec;
96 		}
97 		printf("%ld.%06ld", sec, usec);
98 	}
99 
100 	end = endof(tsp->tsp_name) > snapend ? snapend : endof(tsp->tsp_name);
101 	fputs(" name ", stdout);
102 	if (fn_print(tsp->tsp_name, end))
103 		goto trunc;
104 	return;
105 trunc:
106 	fputs(" [|timed]", stdout);
107 }
108