1 /**
2    r_time.c
3 
4 
5    Copyright (C) 2002-2003, Network Resonance, Inc.
6    Copyright (C) 2006, Network Resonance, Inc.
7    All Rights Reserved
8 
9    Redistribution and use in source and binary forms, with or without
10    modification, are permitted provided that the following conditions
11    are met:
12 
13    1. Redistributions of source code must retain the above copyright
14       notice, this list of conditions and the following disclaimer.
15    2. Redistributions in binary form must reproduce the above copyright
16       notice, this list of conditions and the following disclaimer in the
17       documentation and/or other materials provided with the distribution.
18    3. Neither the name of Network Resonance, Inc. nor the name of any
19       contributors to this software may be used to endorse or promote
20       products derived from this software without specific prior written
21       permission.
22 
23    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
24    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26    ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33    POSSIBILITY OF SUCH DAMAGE.
34 
35 
36  */
37 
38 /**
39    r_time.c
40 
41 
42    Copyright (C) 1999-2000 RTFM, Inc.
43    All Rights Reserved
44 
45    This package is a SSLv3/TLS protocol analyzer written by Eric Rescorla
46    <ekr@rtfm.com> and licensed by RTFM, Inc.
47 
48    Redistribution and use in source and binary forms, with or without
49    modification, are permitted provided that the following conditions
50    are met:
51    1. Redistributions of source code must retain the above copyright
52       notice, this list of conditions and the following disclaimer.
53    2. Redistributions in binary form must reproduce the above copyright
54       notice, this list of conditions and the following disclaimer in the
55       documentation and/or other materials provided with the distribution.
56    3. All advertising materials mentioning features or use of this software
57       must display the following acknowledgement:
58 
59       This product includes software developed by Eric Rescorla for
60       RTFM, Inc.
61 
62    4. Neither the name of RTFM, Inc. nor the name of Eric Rescorla may be
63       used to endorse or promote products derived from this
64       software without specific prior written permission.
65 
66    THIS SOFTWARE IS PROVIDED BY ERIC RESCORLA AND RTFM, INC. ``AS IS'' AND
67    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
68    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
69    ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
70    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
71    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
72    OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
73    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
74    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
75    OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY SUCH DAMAGE.
76 
77    $Id: r_time.c,v 1.5 2008/11/26 03:22:02 adamcain Exp $
78 
79    ekr@rtfm.com  Thu Mar  4 08:43:46 1999
80  */
81 
82 #include <r_common.h>
83 #include <r_time.h>
84 
85 /*Note that t1 must be > t0 */
r_timeval_diff(t1,t0,diff)86 int r_timeval_diff(t1,t0,diff)
87   struct timeval *t1;
88   struct timeval *t0;
89   struct timeval *diff;
90   {
91     long d;
92 
93     if(t0->tv_sec > t1->tv_sec)
94       ERETURN(R_BAD_ARGS);
95     if((t0->tv_sec == t1->tv_sec) && (t0->tv_usec > t1->tv_usec))
96       ERETURN(R_BAD_ARGS);
97 
98     /*Easy case*/
99     if(t0->tv_usec <= t1->tv_usec){
100       diff->tv_sec=t1->tv_sec - t0->tv_sec;
101       diff->tv_usec=t1->tv_usec - t0->tv_usec;
102       return(0);
103     }
104 
105     /*Hard case*/
106     d=t0->tv_usec - t1->tv_usec;
107     if(t1->tv_sec < (t0->tv_sec + 1))
108       ERETURN(R_BAD_ARGS);
109     diff->tv_sec=t1->tv_sec - (t0->tv_sec + 1);
110     diff->tv_usec=1000000 - d;
111 
112     return(0);
113   }
114 
r_timeval_add(t1,t2,sum)115 int r_timeval_add(t1,t2,sum)
116   struct timeval *t1;
117   struct timeval *t2;
118   struct timeval *sum;
119   {
120     long tv_sec,tv_usec,d;
121 
122     tv_sec=t1->tv_sec + t2->tv_sec;
123 
124     d=t1->tv_usec + t2->tv_usec;
125     if(d>1000000){
126       tv_sec++;
127       tv_usec=d-1000000;
128     }
129     else{
130       tv_usec=d;
131     }
132 
133     sum->tv_sec=tv_sec;
134     sum->tv_usec=tv_usec;
135 
136     return(0);
137   }
138 
r_timeval_cmp(t1,t2)139 int r_timeval_cmp(t1,t2)
140   struct timeval *t1;
141   struct timeval *t2;
142   {
143     if(t1->tv_sec>t2->tv_sec)
144       return(1);
145     if(t1->tv_sec<t2->tv_sec)
146       return(-1);
147     if(t1->tv_usec>t2->tv_usec)
148       return(1);
149     if(t1->tv_usec<t2->tv_usec)
150       return(-1);
151     return(0);
152   }
153 
154 
r_timeval2int(tv)155 UINT8 r_timeval2int(tv)
156   struct timeval *tv;
157   {
158     UINT8 r=0;
159 
160     r=(tv->tv_sec);
161     r*=1000000;
162     r+=tv->tv_usec;
163 
164     return r;
165   }
166 
r_int2timeval(UINT8 t,struct timeval * tv)167 int r_int2timeval(UINT8 t,struct timeval *tv)
168   {
169     tv->tv_sec=t/1000000;
170     tv->tv_usec=t%1000000;
171 
172     return(0);
173   }
174 
r_gettimeint()175 UINT8 r_gettimeint()
176   {
177     struct timeval tv;
178 
179     gettimeofday(&tv,0);
180 
181     return r_timeval2int(&tv);
182   }
183 
184 /* t1-t0 in microseconds */
r_timeval_diff_usec(struct timeval * t1,struct timeval * t0,INT8 * diff)185 int r_timeval_diff_usec(struct timeval *t1, struct timeval *t0, INT8 *diff)
186   {
187     int r,_status;
188     int sign;
189     struct timeval tmp;
190 
191     sign = 1;
192     if (r=r_timeval_diff(t1, t0, &tmp)) {
193         if (r == R_BAD_ARGS) {
194             sign = -1;
195             if (r=r_timeval_diff(t0, t1, &tmp))
196                 ABORT(r);
197         }
198     }
199 
200     /* 1 second = 1000 milliseconds
201      * 1 milliseconds = 1000 microseconds */
202 
203     *diff = ((tmp.tv_sec * (1000*1000)) + tmp.tv_usec) * sign;
204 
205     _status = 0;
206   abort:
207     return(_status);
208   }
209 
210 /* t1-t0 in milliseconds */
r_timeval_diff_ms(struct timeval * t1,struct timeval * t0,INT8 * diff)211 int r_timeval_diff_ms(struct timeval *t1, struct timeval *t0, INT8 *diff)
212   {
213     int r,_status;
214     int sign;
215     struct timeval tmp;
216 
217     sign = 1;
218     if (r=r_timeval_diff(t1, t0, &tmp)) {
219         if (r == R_BAD_ARGS) {
220             sign = -1;
221             if (r=r_timeval_diff(t0, t1, &tmp))
222                 ABORT(r);
223         }
224     }
225 
226     /* 1 second = 1000 milliseconds
227      * 1 milliseconds = 1000 microseconds */
228 
229     *diff = ((tmp.tv_sec * 1000) + (tmp.tv_usec / 1000)) * sign;
230 
231     _status = 0;
232   abort:
233     return(_status);
234   }
235 
236