xref: /netbsd-src/external/bsd/tcpdump/dist/ntp.c (revision c41df9f6167ea7cd2f761f0a97783c8267cb8847)
1*d881c474Schristos /*
2*d881c474Schristos  * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3*d881c474Schristos  *	The Regents of the University of California.  All rights reserved.
4*d881c474Schristos  *
5*d881c474Schristos  * Redistribution and use in source and binary forms, with or without
6*d881c474Schristos  * modification, are permitted provided that: (1) source code distributions
7*d881c474Schristos  * retain the above copyright notice and this paragraph in its entirety, (2)
8*d881c474Schristos  * distributions including binary code include the above copyright notice and
9*d881c474Schristos  * this paragraph in its entirety in the documentation or other materials
10*d881c474Schristos  * provided with the distribution, and (3) all advertising materials mentioning
11*d881c474Schristos  * features or use of this software display the following acknowledgement:
12*d881c474Schristos  * ``This product includes software developed by the University of California,
13*d881c474Schristos  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14*d881c474Schristos  * the University nor the names of its contributors may be used to endorse
15*d881c474Schristos  * or promote products derived from this software without specific prior
16*d881c474Schristos  * written permission.
17*d881c474Schristos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18*d881c474Schristos  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19*d881c474Schristos  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20*d881c474Schristos  *
21*d881c474Schristos  */
22*d881c474Schristos 
23*d881c474Schristos #include <config.h>
24*d881c474Schristos 
25*d881c474Schristos #include "ntp.h"
26*d881c474Schristos 
27*d881c474Schristos #include "extract.h"
28*d881c474Schristos 
29*d881c474Schristos #define	JAN_1970	INT64_T_CONSTANT(2208988800)	/* 1970 - 1900 in seconds */
30*d881c474Schristos 
31*d881c474Schristos void
32*d881c474Schristos p_ntp_time(netdissect_options *ndo,
33*d881c474Schristos 	   const struct l_fixedpt *lfp)
34*d881c474Schristos {
35*d881c474Schristos 	uint32_t i;
36*d881c474Schristos 	uint32_t uf;
37*d881c474Schristos 	uint32_t f;
38*d881c474Schristos 	double ff;
39*d881c474Schristos 
40*d881c474Schristos 	i = GET_BE_U_4(lfp->int_part);
41*d881c474Schristos 	uf = GET_BE_U_4(lfp->fraction);
42*d881c474Schristos 	ff = uf;
43*d881c474Schristos 	if (ff < 0.0)		/* some compilers are buggy */
44*d881c474Schristos 		ff += FMAXINT;
45*d881c474Schristos 	ff = ff / FMAXINT;			/* shift radix point by 32 bits */
46*d881c474Schristos 	f = (uint32_t)(ff * 1000000000.0);	/* treat fraction as parts per billion */
47*d881c474Schristos 	ND_PRINT("%u.%09u", i, f);
48*d881c474Schristos 
49*d881c474Schristos 	/*
50*d881c474Schristos 	 * print the UTC time in human-readable format.
51*d881c474Schristos 	 */
52*d881c474Schristos 	if (i) {
53*d881c474Schristos 	    int64_t seconds_64bit = (int64_t)i - JAN_1970;
54*d881c474Schristos 	    time_t seconds;
55*d881c474Schristos 	    char time_buf[128];
56*d881c474Schristos 	    const char *time_string;
57*d881c474Schristos 
58*d881c474Schristos 	    seconds = (time_t)seconds_64bit;
59*d881c474Schristos 	    if (seconds != seconds_64bit) {
60*d881c474Schristos 		/*
61*d881c474Schristos 		 * It doesn't fit into a time_t, so we can't hand it
62*d881c474Schristos 		 * to gmtime.
63*d881c474Schristos 		 */
64*d881c474Schristos 		time_string = "[Time is too large to fit into a time_t]";
65*d881c474Schristos 	    } else {
66*d881c474Schristos 		/* use ISO 8601 (RFC3339) format */
67*d881c474Schristos 		time_string = nd_format_time(time_buf, sizeof (time_buf),
68*d881c474Schristos 		  "%Y-%m-%dT%H:%M:%SZ", gmtime(&seconds));
69*d881c474Schristos 	    }
70*d881c474Schristos 	    ND_PRINT(" (%s)", time_string);
71*d881c474Schristos 	}
72*d881c474Schristos }
73