@@ -81,7 +81,7 @@ use aquatic_udp_protocol::ConnectionId as Cookie;
81
81
use cookie_builder:: { assemble, decode, disassemble, encode} ;
82
82
use zerocopy:: AsBytes ;
83
83
84
- use super :: error:: { self , Error } ;
84
+ use super :: error:: Error ;
85
85
use crate :: shared:: crypto:: keys:: CipherArrayBlowfish ;
86
86
87
87
/// Generates a new connection cookie.
@@ -106,6 +106,8 @@ pub fn make(fingerprint: u64, issue_at: f64) -> Result<Cookie, Error> {
106
106
Ok ( zerocopy:: FromBytes :: read_from ( cookie. as_slice ( ) ) . expect ( "it should be the same size" ) )
107
107
}
108
108
109
+ use std:: ops:: Range ;
110
+
109
111
/// Checks if the supplied `connection_cookie` is valid.
110
112
///
111
113
/// # Errors
@@ -114,32 +116,32 @@ pub fn make(fingerprint: u64, issue_at: f64) -> Result<Cookie, Error> {
114
116
///
115
117
/// # Panics
116
118
///
117
- /// It would panic if cookie min value is larger than the max value .
118
- pub fn check ( cookie : & Cookie , fingerprint : u64 , min : f64 , max : f64 ) -> Result < f64 , Error > {
119
- assert ! ( min < max , "min is larger than max " ) ;
119
+ /// It would panic if the range start is not smaller than it's end .
120
+ pub fn check ( cookie : & Cookie , fingerprint : u64 , valid_range : Range < f64 > ) -> Result < f64 , Error > {
121
+ assert ! ( valid_range . start <= valid_range . end , "range start is larger than range end " ) ;
120
122
121
123
let cookie_bytes = CipherArrayBlowfish :: from_slice ( cookie. 0 . as_bytes ( ) ) ;
122
124
let cookie_bytes = decode ( * cookie_bytes) ;
123
125
124
126
let issue_time = disassemble ( fingerprint, cookie_bytes) ;
125
127
126
128
if !issue_time. is_normal ( ) {
127
- return Err ( Error :: InvalidConnectionId {
128
- bad_id : error :: ConnectionCookie ( * cookie ) ,
129
+ return Err ( Error :: ConnectionIdNotNormal {
130
+ not_normal_value : issue_time ,
129
131
} ) ;
130
132
}
131
133
132
- if issue_time < min {
134
+ if issue_time < valid_range . start {
133
135
return Err ( Error :: ConnectionIdExpired {
134
- bad_age : issue_time,
135
- min_age : min ,
136
+ expired_value : issue_time,
137
+ min_value : valid_range . start ,
136
138
} ) ;
137
139
}
138
140
139
- if issue_time > max {
141
+ if issue_time > valid_range . end {
140
142
return Err ( Error :: ConnectionIdFromFuture {
141
- future_age : issue_time,
142
- max_age : max ,
143
+ future_value : issue_time,
144
+ max_value : valid_range . end ,
143
145
} ) ;
144
146
}
145
147
@@ -262,7 +264,7 @@ mod tests {
262
264
let min = issue_at - 10.0 ;
263
265
let max = issue_at + 10.0 ;
264
266
265
- let result = check ( & cookie, fingerprint, min, max) . unwrap ( ) ;
267
+ let result = check ( & cookie, fingerprint, min.. max) . unwrap ( ) ;
266
268
267
269
// we should have exactly the same bytes returned
268
270
assert_eq ! ( result. to_ne_bytes( ) , issue_at. to_ne_bytes( ) ) ;
@@ -277,7 +279,7 @@ mod tests {
277
279
let min = issue_at + 10.0 ;
278
280
let max = issue_at + 20.0 ;
279
281
280
- let result = check ( & cookie, fingerprint, min, max) . unwrap_err ( ) ;
282
+ let result = check ( & cookie, fingerprint, min.. max) . unwrap_err ( ) ;
281
283
282
284
match result {
283
285
Error :: ConnectionIdExpired { .. } => { } // Expected error
@@ -295,7 +297,7 @@ mod tests {
295
297
let min = issue_at - 20.0 ;
296
298
let max = issue_at - 10.0 ;
297
299
298
- let result = check ( & cookie, fingerprint, min, max) . unwrap_err ( ) ;
300
+ let result = check ( & cookie, fingerprint, min.. max) . unwrap_err ( ) ;
299
301
300
302
match result {
301
303
Error :: ConnectionIdFromFuture { .. } => { } // Expected error
0 commit comments