Line data Source code
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 :
83 : static char *RCSSTRING __UNUSED__ ="$Id: r_time.c,v 1.5 2008/11/26 03:22:02 adamcain Exp $";
84 :
85 : #include <r_common.h>
86 : #include <r_time.h>
87 :
88 : /*Note that t1 must be > t0 */
89 0 : int r_timeval_diff(t1,t0,diff)
90 : struct timeval *t1;
91 : struct timeval *t0;
92 : struct timeval *diff;
93 : {
94 : long d;
95 :
96 0 : if(t0->tv_sec > t1->tv_sec)
97 0 : ERETURN(R_BAD_ARGS);
98 0 : if((t0->tv_sec == t1->tv_sec) && (t0->tv_usec > t1->tv_usec))
99 0 : ERETURN(R_BAD_ARGS);
100 :
101 : /*Easy case*/
102 0 : if(t0->tv_usec <= t1->tv_usec){
103 0 : diff->tv_sec=t1->tv_sec - t0->tv_sec;
104 0 : diff->tv_usec=t1->tv_usec - t0->tv_usec;
105 0 : return(0);
106 : }
107 :
108 : /*Hard case*/
109 0 : d=t0->tv_usec - t1->tv_usec;
110 0 : if(t1->tv_sec < (t0->tv_sec + 1))
111 0 : ERETURN(R_BAD_ARGS);
112 0 : diff->tv_sec=t1->tv_sec - (t0->tv_sec + 1);
113 0 : diff->tv_usec=1000000 - d;
114 :
115 0 : return(0);
116 : }
117 :
118 0 : int r_timeval_add(t1,t2,sum)
119 : struct timeval *t1;
120 : struct timeval *t2;
121 : struct timeval *sum;
122 : {
123 : long tv_sec,tv_usec,d;
124 :
125 0 : tv_sec=t1->tv_sec + t2->tv_sec;
126 :
127 0 : d=t1->tv_usec + t2->tv_usec;
128 0 : if(d>1000000){
129 0 : tv_sec++;
130 0 : tv_usec=d-1000000;
131 : }
132 : else{
133 0 : tv_usec=d;
134 : }
135 :
136 0 : sum->tv_sec=tv_sec;
137 0 : sum->tv_usec=tv_usec;
138 :
139 0 : return(0);
140 : }
141 :
142 0 : int r_timeval_cmp(t1,t2)
143 : struct timeval *t1;
144 : struct timeval *t2;
145 : {
146 0 : if(t1->tv_sec>t2->tv_sec)
147 0 : return(1);
148 0 : if(t1->tv_sec<t2->tv_sec)
149 0 : return(-1);
150 0 : if(t1->tv_usec>t2->tv_usec)
151 0 : return(1);
152 0 : if(t1->tv_usec<t2->tv_usec)
153 0 : return(-1);
154 0 : return(0);
155 : }
156 :
157 :
158 0 : UINT8 r_timeval2int(tv)
159 : struct timeval *tv;
160 : {
161 0 : UINT8 r=0;
162 :
163 0 : r=(tv->tv_sec);
164 0 : r*=1000000;
165 0 : r+=tv->tv_usec;
166 :
167 0 : return r;
168 : }
169 :
170 0 : int r_int2timeval(UINT8 t,struct timeval *tv)
171 : {
172 0 : tv->tv_sec=t/1000000;
173 0 : tv->tv_usec=t%1000000;
174 :
175 0 : return(0);
176 : }
177 :
178 0 : UINT8 r_gettimeint()
179 : {
180 : struct timeval tv;
181 :
182 0 : gettimeofday(&tv,0);
183 :
184 0 : return r_timeval2int(&tv);
185 : }
186 :
187 : /* t1-t0 in microseconds */
188 0 : int r_timeval_diff_usec(struct timeval *t1, struct timeval *t0, INT8 *diff)
189 : {
190 : int r,_status;
191 : int sign;
192 : struct timeval tmp;
193 :
194 0 : sign = 1;
195 0 : if (r=r_timeval_diff(t1, t0, &tmp)) {
196 0 : if (r == R_BAD_ARGS) {
197 0 : sign = -1;
198 0 : if (r=r_timeval_diff(t0, t1, &tmp))
199 0 : ABORT(r);
200 : }
201 : }
202 :
203 : /* 1 second = 1000 milliseconds
204 : * 1 milliseconds = 1000 microseconds */
205 :
206 0 : *diff = ((tmp.tv_sec * (1000*1000)) + tmp.tv_usec) * sign;
207 :
208 0 : _status = 0;
209 : abort:
210 0 : return(_status);
211 : }
212 :
213 : /* t1-t0 in milliseconds */
214 0 : int r_timeval_diff_ms(struct timeval *t1, struct timeval *t0, INT8 *diff)
215 : {
216 : int r,_status;
217 : int sign;
218 : struct timeval tmp;
219 :
220 0 : sign = 1;
221 0 : if (r=r_timeval_diff(t1, t0, &tmp)) {
222 0 : if (r == R_BAD_ARGS) {
223 0 : sign = -1;
224 0 : if (r=r_timeval_diff(t0, t1, &tmp))
225 0 : ABORT(r);
226 : }
227 : }
228 :
229 : /* 1 second = 1000 milliseconds
230 : * 1 milliseconds = 1000 microseconds */
231 :
232 0 : *diff = ((tmp.tv_sec * 1000) + (tmp.tv_usec / 1000)) * sign;
233 :
234 0 : _status = 0;
235 : abort:
236 0 : return(_status);
237 : }
238 :
|