]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/ntp/libntp/timespecops.c
Fix multiple denial of service in ntpd.
[FreeBSD/FreeBSD.git] / contrib / ntp / libntp / timespecops.c
1 /*
2  * timespecops.c -- calculations on 'struct timespec' values
3  *
4  * Written by Juergen Perlinger (perlinger@ntp.org) for the NTP project.
5  * The contents of 'html/copyright.html' apply.
6  *
7  */
8
9 #include "config.h"
10
11 #include <sys/types.h>
12 #include <stdio.h>
13 #include <math.h>
14
15 #include "ntp.h"
16 #include "timetoa.h"
17 #include "timespecops.h"
18
19
20 /* nanoseconds per second */
21 #define NANOSECONDS 1000000000
22
23 /* conversion between l_fp fractions and nanoseconds */
24 #ifdef HAVE_U_INT64
25 # define FTOTVN(tsf)                                            \
26         ((int32)                                                \
27          (((u_int64)(tsf) * NANOSECONDS + 0x80000000) >> 32))
28 # define TVNTOF(tvu)                                            \
29         ((u_int32)                                              \
30          ((((u_int64)(tvu) << 32) + NANOSECONDS / 2) /          \
31           NANOSECONDS))
32 #else
33 # define NSECFRAC       (FRAC / NANOSECONDS)
34 # define FTOTVN(tsf)                                            \
35         ((int32)((tsf) / NSECFRAC + 0.5))
36 # define TVNTOF(tvu)                                            \
37         ((u_int32)((tvu) * NSECFRAC + 0.5))
38 #endif
39
40
41
42 /* make sure nanoseconds are in nominal range */
43 struct timespec
44 normalize_tspec(
45         struct timespec x
46         )
47 {
48 #if SIZEOF_LONG > 4
49         long    z;
50
51         /* 
52          * tv_nsec is of type 'long', and on a 64-bit machine using only
53          * loops becomes prohibitive once the upper 32 bits get
54          * involved. On the other hand, division by constant should be
55          * fast enough; so we do a division of the nanoseconds in that
56          * case. The floor adjustment step follows with the standard
57          * normalisation loops. And labs() is intentionally not used
58          * here: it has implementation-defined behaviour when applied
59          * to LONG_MIN.
60          */
61         if (x.tv_nsec < -3l * NANOSECONDS ||
62             x.tv_nsec > 3l * NANOSECONDS) {
63                 z = x.tv_nsec / NANOSECONDS;
64                 x.tv_nsec -= z * NANOSECONDS;
65                 x.tv_sec += z;
66         }
67 #endif
68         /* since 10**9 is close to 2**32, we don't divide but do a
69          * normalisation in a loop; this takes 3 steps max, and should
70          * outperform a division even if the mul-by-inverse trick is
71          * employed. */
72         if (x.tv_nsec < 0)
73                 do {
74                         x.tv_nsec += NANOSECONDS;
75                         x.tv_sec--;
76                 } while (x.tv_nsec < 0);
77         else if (x.tv_nsec >= NANOSECONDS)
78                 do {
79                         x.tv_nsec -= NANOSECONDS;
80                         x.tv_sec++;
81                 } while (x.tv_nsec >= NANOSECONDS);
82
83         return x;
84 }
85
86 /* x = abs(a) */
87 struct timespec
88 abs_tspec(
89         struct timespec a
90         )
91 {
92         struct timespec c;
93
94         c = normalize_tspec(a);
95         if (c.tv_sec < 0) {
96                 if (c.tv_nsec != 0) {
97                         c.tv_sec = -c.tv_sec - 1;
98                         c.tv_nsec = NANOSECONDS - c.tv_nsec;
99                 } else {
100                         c.tv_sec = -c.tv_sec;
101                 }
102         }
103
104         return c;
105 }
106
107 /*
108  * compare previously-normalised a and b
109  * return 1 / 0 / -1 if a < / == / > b
110  */
111 int
112 cmp_tspec(
113         struct timespec a,
114         struct timespec b
115         )
116 {
117         int r;
118
119         r = (a.tv_sec > b.tv_sec) - (a.tv_sec < b.tv_sec);
120         if (0 == r)
121                 r = (a.tv_nsec > b.tv_nsec) -
122                     (a.tv_nsec < b.tv_nsec);
123         
124         return r;
125 }
126
127 /*
128  * test previously-normalised a
129  * return 1 / 0 / -1 if a < / == / > 0
130  */
131 int
132 test_tspec(
133         struct timespec a
134         )
135 {
136         int             r;
137
138         r = (a.tv_sec > 0) - (a.tv_sec < 0);
139         if (r == 0)
140                 r = (a.tv_nsec > 0);
141         
142         return r;
143 }
144
145 /*
146  *  convert to l_fp type, relative and absolute
147  */
148
149 /* convert from timespec duration to l_fp duration */
150 l_fp
151 tspec_intv_to_lfp(
152         struct timespec x
153         )
154 {
155         struct timespec v;
156         l_fp            y;
157         
158         v = normalize_tspec(x);
159         y.l_uf = TVNTOF(v.tv_nsec);
160         y.l_i = (int32)v.tv_sec;
161
162         return y;
163 }
164
165 /* convert from l_fp type, relative signed/unsigned and absolute */
166 struct timespec
167 lfp_intv_to_tspec(
168         l_fp            x
169         )
170 {
171         struct timespec out;
172         l_fp            absx;
173         int             neg;
174         
175         neg = L_ISNEG(&x);
176         absx = x;
177         if (neg) {
178                 L_NEG(&absx);   
179         }
180         out.tv_nsec = FTOTVN(absx.l_uf);
181         out.tv_sec = absx.l_i;
182         if (neg) {
183                 out.tv_sec = -out.tv_sec;
184                 out.tv_nsec = -out.tv_nsec;
185                 out = normalize_tspec(out);
186         }
187
188         return out;
189 }
190
191 struct timespec
192 lfp_uintv_to_tspec(
193         l_fp            x
194         )
195 {
196         struct timespec out;
197         
198         out.tv_nsec = FTOTVN(x.l_uf);
199         out.tv_sec = x.l_ui;
200
201         return out;
202 }
203
204 /*
205  * absolute (timestamp) conversion. Input is time in NTP epoch, output
206  * is in UN*X epoch. The NTP time stamp will be expanded around the
207  * pivot time *p or the current time, if p is NULL.
208  */
209 struct timespec
210 lfp_stamp_to_tspec(
211         l_fp            x,
212         const time_t *  p
213         )
214 {
215         struct timespec out;
216         vint64          sec;
217
218         sec = ntpcal_ntp_to_time(x.l_ui, p);
219         out.tv_nsec = FTOTVN(x.l_uf);
220
221         /* copying a vint64 to a time_t needs some care... */
222 #if SIZEOF_TIME_T <= 4
223         out.tv_sec = (time_t)sec.d_s.lo;
224 #elif defined(HAVE_INT64)
225         out.tv_sec = (time_t)sec.q_s;
226 #else
227         out.tv_sec = ((time_t)sec.d_s.hi << 32) | sec.d_s.lo;
228 #endif
229         
230         return out;
231 }
232
233 /* -*-EOF-*- */