1: #ifndef lint
   2: static char *RCSid = "$Source: /usr/users/louie/ntp/RCS/ntpsubs.c,v $ $Revision: 3.4.1.3 $ $Date: 89/05/18 18:33:50 $";
   3: #endif	lint
   4: 
   5: /*
   6:  *  $Log:	ntpsubs.c,v $
   7:  * Revision 3.4.1.3  89/05/18  18:33:50  louie
   8:  * Added support few a new type of unsigned long to double compiler brokenness,
   9:  * called GENERIC_UNS_BUG.  If this is defined, then the unsigned long is
  10:  * shifted right one bit, the high-order bit of the result is cleared, then
  11:  * converted to a double.  The double is multiplied by 2.0, and the a 1.0 is
  12:  * optionall added to it if the low order bit of the original unsigned long
  13:  * was set.  Whew!
  14:  *
  15:  * Revision 3.4.1.2  89/03/29  12:46:02  louie
  16:  * Check for success sending query before trying to listen for answers.  Will
  17:  * catch case of no server running and an ICMP port unreachable being returned.
  18:  *
  19:  * Revision 3.4.1.1  89/03/22  18:32:19  louie
  20:  * patch3: Use new RCS headers.
  21:  *
  22:  * Revision 3.4  89/03/17  18:37:18  louie
  23:  * Latest test release.
  24:  *
  25:  * Revision 3.3  89/03/15  14:20:03  louie
  26:  * New baseline for next release.
  27:  *
  28:  * Revision 3.2  89/03/07  18:29:22  louie
  29:  * New version of UNIX NTP daemon based on the 6 March 1989 draft of the new
  30:  * NTP protocol spec.  This module has mostly cosmetic changes.
  31:  *
  32:  * Revision 3.1.1.1  89/02/15  08:59:27  louie
  33:  * *** empty log message ***
  34:  *
  35:  *
  36:  * Revision 3.1  89/01/30  14:43:18  louie
  37:  * Second UNIX NTP test release.
  38:  *
  39:  * Revision 3.0  88/12/12  15:58:59  louie
  40:  * Test release of new UNIX NTP software.  This version should conform to the
  41:  * revised NTP protocol specification.
  42:  *
  43:  */
  44: 
  45: #include <stdio.h>
  46: #include <sys/types.h>
  47: #include <sys/param.h>
  48: #include <sys/time.h>
  49: #include <sys/uio.h>
  50: #include <sys/socket.h>
  51: #include <netinet/in.h>
  52: #include <arpa/inet.h>
  53: #include <errno.h>
  54: #include <syslog.h>
  55: 
  56: #include "ntp.h"
  57: 
  58: extern int errno;
  59: 
  60: #define TRUE    1
  61: #define FALSE   0
  62: 
  63: /*
  64:  *  The nice thing here is that the quantity is NEVER signed.
  65:  */
  66: double
  67: ul_fixed_to_double(t)
  68:     struct l_fixedpt *t;
  69: {
  70:     double a, b;
  71: #ifdef  GENERIC_UNS_BUG
  72:     long i;
  73: 
  74:     i = ntohl(t->fraction);
  75:     a = (long)((i >> 1) & 0x7fffffff);
  76:     a *= 2.0;
  77:     if (i & 1)
  78:         a += 1.0;
  79:     a = a / (4.294967296e9);    /* shift dec point over by 32 bits */
  80:     i = ntohl(t->int_part);
  81:     b = (long)((i >> 1) & 0x7fffffff);
  82:     b *= 2.0;
  83:     if (i & 1)
  84:         b += 1.0;
  85: #else   /* GENERIC_UNS_BUG */
  86:     a = (u_long) ntohl(t->fraction);
  87: #ifdef  VAX_COMPILER_FLT_BUG
  88:     if (a < 0.0) a += 4.294967296e9;
  89: #endif
  90:     a = a / (4.294967296e9);/* shift dec point over by 32 bits */
  91:     b = (u_long) ntohl(t->int_part);
  92: #ifdef  VAX_COMPILER_FLT_BUG
  93:     if (b < 0.0) b += 4.294967296e9;
  94: #endif
  95: #endif	/* GENERIC_UNS_BUG */
  96:     return (a + b);
  97: }
  98: 
  99: /*
 100:  *  Here we have to worry about the high order bit being signed
 101:  */
 102: 
 103: #if 0
 104: double
 105: l_fixed_to_double(t)
 106:     struct l_fixedpt *t;
 107: {
 108:     double a,b;
 109: 
 110:     if (ntohl(t->int_part) & 0x80000000) {
 111:         a = ntohl(~t->fraction);
 112: #ifdef  VAX_COMPILER_FLT_BUG
 113:         if (a < 0.0) a += 4.294967296e9;
 114: #endif
 115:         a = a / (4.294967296e9);
 116:         b = ntohl(~t->int_part);
 117: #ifdef  VAX_COMPILER_FLT_BUG
 118:         if (b < 0.0) b += 4.294967296e9;
 119: #endif
 120:         a += b;
 121:         a = -a;
 122:     } else {
 123:         a = ntohl(t->fraction);
 124: #ifdef  VAX_COMPILER_FLT_BUG
 125:         if (a < 0.0) a += 4.294967296e9;
 126: #endif
 127:         a = a / (4.294967296e9);
 128:         b = ntohl(t->int_part);
 129: #ifdef  VAX_COMPILER_FLT_BUG
 130:         if (b < 0.0) b += 4.294967296e9;
 131: #endif
 132:         a += b;
 133:     }
 134:     return (a);
 135: }
 136: #endif
 137: 
 138: /*
 139:  *  Here we have to worry about the high order bit being signed
 140:  */
 141: double
 142: s_fixed_to_double(t)
 143:     struct s_fixedpt *t;
 144: {
 145:     double a;
 146: 
 147:     if (ntohs(t->int_part) & 0x8000) {
 148:         a = ntohs(~t->fraction & 0xFFFF);
 149:         a = a / 65536.0;    /* shift dec point over by 16 bits */
 150:         a +=  ntohs(~t->int_part & 0xFFFF);
 151:         a = -a;
 152:     } else {
 153:         a = ntohs(t->fraction);
 154:         a = a / 65536.0;    /* shift dec point over by 16 bits */
 155:         a += ntohs(t->int_part);
 156:     }
 157:     return (a);
 158: }
 159: 
 160: void
 161: double_to_l_fixed(t, value)
 162:     struct l_fixedpt *t;
 163:     double value;
 164: {
 165:     double temp;
 166: 
 167:     if (value >= (double) 0.0) {
 168:         t->int_part = value;
 169:         temp = value - t->int_part;
 170:         temp *= 4.294967296e9;
 171:         t->fraction = temp;
 172:         t->int_part = htonl(t->int_part);
 173:         t->fraction = htonl(t->fraction);
 174:     } else {
 175:         value = -value;
 176:         t->int_part = value;
 177:         temp = value - t->int_part;
 178:         temp *= 4.294967296e9;
 179:         t->fraction = temp;
 180:         t->int_part = htonl(~t->int_part);
 181:         t->fraction = htonl(~t->fraction);
 182:     }
 183: }
 184: 
 185: void
 186: double_to_s_fixed(t, value)
 187:     struct s_fixedpt *t;
 188:     double value;
 189: {
 190:     double temp;
 191: 
 192:     if (value >= (double) 0.0) {
 193:         t->int_part = value;
 194:         temp = value - t->int_part;
 195:         temp *= 65536.0;
 196:         t->fraction = temp;
 197:         t->int_part = htons(t->int_part);
 198:         t->fraction = htons(t->fraction);
 199:     } else {
 200:         value = -value;
 201:         t->int_part = value;
 202:         temp = value - t->int_part;
 203:         temp *= 65536.0;
 204:         t->fraction = temp;
 205:         t->int_part = htons(~t->int_part);
 206:         t->fraction = htons(~t->fraction);
 207:     }
 208: }
 209: /*
 210: 	in the sun, trying to assign a float between 2^31 and 2^32
 211: 	results in the value 2^31.  Neither 4.2bsd nor VMS have this
 212: 	problem.  Reported it to Bob O'Brien of SMI
 213: */
 214: #ifdef  SUN_FLT_BUG
 215: tstamp(stampp, tvp)
 216:     struct l_fixedpt *stampp;
 217:     struct timeval *tvp;
 218: {
 219:     long tt;
 220:     double dd;
 221: 
 222:     stampp->int_part = ntohl(JAN_1970 + tvp->tv_sec);
 223:     dd = (float) tvp->tv_usec / 1000000.0;
 224:     tt = dd * 2147483648.0;
 225:     stampp->fraction = ntohl((tt << 1));
 226: }
 227: #else
 228: tstamp(stampp, tvp)
 229:     struct l_fixedpt *stampp;
 230:     struct timeval *tvp;
 231: {
 232:     stampp->int_part = ntohl((u_long) (JAN_1970 + tvp->tv_sec));
 233:     stampp->fraction = ntohl((u_long) ((float) tvp->tv_usec * 4294.967295));
 234: }
 235: #endif
 236: 
 237: /*
 238:  * ntoa is similar to inet_ntoa, but cycles through a set of 8 buffers
 239:  * so it can be invoked several times in a function parameter list.
 240:  */
 241: 
 242: char *
 243: ntoa (in_addr)
 244: struct in_addr in_addr;
 245: {
 246:     static int i = 0;
 247:     static char bufs[8][20];
 248:     unsigned char *p = (unsigned char *) &in_addr.s_addr;
 249: 
 250:     i = (i + 1) % (sizeof bufs / sizeof bufs[0]);
 251:     sprintf (bufs[i], "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
 252:     return bufs[i];
 253: }

Defined functions

double_to_l_fixed defined in line 160; used 1 times
l_fixed_to_double defined in line 104; used 2 times
ul_fixed_to_double defined in line 66; used 12 times

Defined variables

RCSid defined in line 2; never used

Defined macros

FALSE defined in line 61; never used
TRUE defined in line 60; never used
Last modified: 1989-07-25
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 3653
Valid CSS Valid XHTML 1.0 Strict