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