1 /* $OpenBSD: print-ntp.c,v 1.12 2005/12/07 19:53:31 otto Exp $ */ 2 3 /* 4 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997 5 * The Regents of the University of California. 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 * Format and print ntp packets. 24 * By Jeffrey Mogul/DECWRL 25 * loosely based on print-bootp.c 26 */ 27 28 #ifndef lint 29 static const char rcsid[] = 30 "@(#) $Header: /home/cvs/src/usr.sbin/tcpdump/print-ntp.c,v 1.12 2005/12/07 19:53:31 otto Exp $ (LBL)"; 31 #endif 32 33 #include <sys/param.h> 34 #include <sys/time.h> 35 #include <sys/socket.h> 36 37 struct mbuf; 38 struct rtentry; 39 #include <net/if.h> 40 41 #include <netinet/in.h> 42 #include <netinet/if_ether.h> 43 44 #include <ctype.h> 45 #include <stdio.h> 46 #include <string.h> 47 48 #include "interface.h" 49 #include "addrtoname.h" 50 #ifdef MODEMASK 51 #undef MODEMASK /* Solaris sucks */ 52 #endif 53 #include "ntp.h" 54 55 static void p_sfix(const struct s_fixedpt *); 56 static void p_ntp_time(const struct l_fixedpt *); 57 static void p_ntp_delta(const struct l_fixedpt *, const struct l_fixedpt *); 58 59 /* 60 * Print ntp requests 61 */ 62 void 63 ntp_print(register const u_char *cp, u_int length) 64 { 65 register const struct ntpdata *bp; 66 int mode, version, leapind; 67 68 bp = (struct ntpdata *)cp; 69 /* Note funny sized packets */ 70 if (length != sizeof(struct ntpdata)) 71 (void)printf(" [len=%d]", length); 72 73 TCHECK(bp->status); 74 75 version = (int)(bp->status & VERSIONMASK) >> 3; 76 printf(" v%d", version); 77 78 leapind = bp->status & LEAPMASK; 79 switch (leapind) { 80 81 case NO_WARNING: 82 break; 83 84 case PLUS_SEC: 85 fputs(" +1s", stdout); 86 break; 87 88 case MINUS_SEC: 89 fputs(" -1s", stdout); 90 break; 91 } 92 93 mode = bp->status & MODEMASK; 94 switch (mode) { 95 96 case MODE_UNSPEC: /* unspecified */ 97 fputs(" unspec", stdout); 98 break; 99 100 case MODE_SYM_ACT: /* symmetric active */ 101 fputs(" sym_act", stdout); 102 break; 103 104 case MODE_SYM_PAS: /* symmetric passive */ 105 fputs(" sym_pas", stdout); 106 break; 107 108 case MODE_CLIENT: /* client */ 109 fputs(" client", stdout); 110 break; 111 112 case MODE_SERVER: /* server */ 113 fputs(" server", stdout); 114 break; 115 116 case MODE_BROADCAST: /* broadcast */ 117 fputs(" bcast", stdout); 118 break; 119 120 case MODE_RES1: /* reserved */ 121 fputs(" res1", stdout); 122 break; 123 124 case MODE_RES2: /* reserved */ 125 fputs(" res2", stdout); 126 break; 127 128 } 129 130 TCHECK(bp->stratum); 131 printf(" strat %d", bp->stratum); 132 133 TCHECK(bp->ppoll); 134 printf(" poll %d", bp->ppoll); 135 136 /* Can't TCHECK bp->precision bitfield so bp->distance + 0 instead */ 137 TCHECK2(bp->distance, 0); 138 printf(" prec %d", bp->precision); 139 140 if (!vflag) 141 return; 142 143 TCHECK(bp->distance); 144 fputs(" dist ", stdout); 145 p_sfix(&bp->distance); 146 147 TCHECK(bp->dispersion); 148 fputs(" disp ", stdout); 149 p_sfix(&bp->dispersion); 150 151 TCHECK(bp->refid); 152 fputs(" ref ", stdout); 153 /* Interpretation depends on stratum */ 154 switch (bp->stratum) { 155 156 case UNSPECIFIED: 157 printf("(unspec)"); 158 break; 159 160 case PRIM_REF: 161 fn_printn((u_char *)&bp->refid, sizeof(bp->refid), NULL); 162 break; 163 164 case INFO_QUERY: 165 printf("%s INFO_QUERY", ipaddr_string(&(bp->refid))); 166 /* this doesn't have more content */ 167 return; 168 169 case INFO_REPLY: 170 printf("%s INFO_REPLY", ipaddr_string(&(bp->refid))); 171 /* this is too complex to be worth printing */ 172 return; 173 174 default: 175 printf("%s", ipaddr_string(&(bp->refid))); 176 break; 177 } 178 179 TCHECK(bp->reftime); 180 putchar('@'); 181 p_ntp_time(&(bp->reftime)); 182 183 TCHECK(bp->org); 184 fputs(" orig ", stdout); 185 p_ntp_time(&(bp->org)); 186 187 TCHECK(bp->rec); 188 fputs(" rec ", stdout); 189 p_ntp_delta(&(bp->org), &(bp->rec)); 190 191 TCHECK(bp->xmt); 192 fputs(" xmt ", stdout); 193 p_ntp_delta(&(bp->org), &(bp->xmt)); 194 195 return; 196 197 trunc: 198 fputs(" [|ntp]", stdout); 199 } 200 201 static void 202 p_sfix(register const struct s_fixedpt *sfp) 203 { 204 register int i; 205 register int f; 206 register float ff; 207 208 i = ntohs(sfp->int_part); 209 f = ntohs(sfp->fraction); 210 ff = f / 65536.0; /* shift radix point by 16 bits */ 211 f = ff * 1000000.0; /* Treat fraction as parts per million */ 212 printf("%d.%06d", i, f); 213 } 214 215 #define FMAXINT (4294967296.0) /* floating point rep. of MAXINT */ 216 217 static void 218 p_ntp_time(register const struct l_fixedpt *lfp) 219 { 220 register int32_t i; 221 register u_int32_t uf; 222 register u_int32_t f; 223 register float ff; 224 225 i = ntohl(lfp->int_part); 226 uf = ntohl(lfp->fraction); 227 ff = uf; 228 if (ff < 0.0) /* some compilers are buggy */ 229 ff += FMAXINT; 230 ff = ff / FMAXINT; /* shift radix point by 32 bits */ 231 f = ff * 1000000000.0; /* treat fraction as parts per billion */ 232 printf("%u.%09d", i, f); 233 } 234 235 /* Prints time difference between *lfp and *olfp */ 236 static void 237 p_ntp_delta(register const struct l_fixedpt *olfp, 238 register const struct l_fixedpt *lfp) 239 { 240 register int32_t i; 241 register u_int32_t uf; 242 register u_int32_t ouf; 243 register u_int32_t f; 244 register float ff; 245 int signbit; 246 247 i = ntohl(lfp->int_part) - ntohl(olfp->int_part); 248 249 uf = ntohl(lfp->fraction); 250 ouf = ntohl(olfp->fraction); 251 252 if (i > 0) { /* new is definitely greater than old */ 253 signbit = 0; 254 f = uf - ouf; 255 if (ouf > uf) /* must borrow from high-order bits */ 256 i -= 1; 257 } else if (i < 0) { /* new is definitely less than old */ 258 signbit = 1; 259 f = ouf - uf; 260 if (uf > ouf) /* must carry into the high-order bits */ 261 i += 1; 262 i = -i; 263 } else { /* int_part is zero */ 264 if (uf > ouf) { 265 signbit = 0; 266 f = uf - ouf; 267 } else { 268 signbit = 1; 269 f = ouf - uf; 270 } 271 } 272 273 ff = f; 274 if (ff < 0.0) /* some compilers are buggy */ 275 ff += FMAXINT; 276 ff = ff / FMAXINT; /* shift radix point by 32 bits */ 277 f = ff * 1000000000.0; /* treat fraction as parts per billion */ 278 if (signbit) 279 putchar('-'); 280 else 281 putchar('+'); 282 printf("%d.%09d", i, f); 283 } 284