79
79
80
80
use aquatic_udp_protocol:: ConnectionId as Cookie ;
81
81
use cookie_builder:: { assemble, decode, disassemble, encode} ;
82
+ use tracing:: instrument;
82
83
use zerocopy:: AsBytes ;
83
84
84
85
use super :: error:: Error ;
@@ -94,9 +95,12 @@ use crate::shared::crypto::keys::CipherArrayBlowfish;
94
95
///
95
96
/// It would panic if the cookie is not exactly 8 bytes is size.
96
97
///
98
+ #[ instrument( err) ]
97
99
pub fn make ( fingerprint : u64 , issue_at : f64 ) -> Result < Cookie , Error > {
98
100
if !issue_at. is_normal ( ) {
99
- return Err ( Error :: InvalidCookieIssueTime { invalid_value : issue_at } ) ;
101
+ return Err ( Error :: CookieValueNotNormal {
102
+ not_normal_value : issue_at,
103
+ } ) ;
100
104
}
101
105
102
106
let cookie = assemble ( fingerprint, issue_at) ;
@@ -117,6 +121,7 @@ use std::ops::Range;
117
121
/// # Panics
118
122
///
119
123
/// It would panic if the range start is not smaller than it's end.
124
+ #[ instrument( err) ]
120
125
pub fn check ( cookie : & Cookie , fingerprint : u64 , valid_range : Range < f64 > ) -> Result < f64 , Error > {
121
126
assert ! ( valid_range. start <= valid_range. end, "range start is larger than range end" ) ;
122
127
@@ -126,20 +131,20 @@ pub fn check(cookie: &Cookie, fingerprint: u64, valid_range: Range<f64>) -> Resu
126
131
let issue_time = disassemble ( fingerprint, cookie_bytes) ;
127
132
128
133
if !issue_time. is_normal ( ) {
129
- return Err ( Error :: ConnectionIdNotNormal {
134
+ return Err ( Error :: CookieValueNotNormal {
130
135
not_normal_value : issue_time,
131
136
} ) ;
132
137
}
133
138
134
139
if issue_time < valid_range. start {
135
- return Err ( Error :: ConnectionIdExpired {
140
+ return Err ( Error :: CookieValueExpired {
136
141
expired_value : issue_time,
137
142
min_value : valid_range. start ,
138
143
} ) ;
139
144
}
140
145
141
146
if issue_time > valid_range. end {
142
- return Err ( Error :: ConnectionIdFromFuture {
147
+ return Err ( Error :: CookieValueFromFuture {
143
148
future_value : issue_time,
144
149
max_value : valid_range. end ,
145
150
} ) ;
@@ -150,15 +155,15 @@ pub fn check(cookie: &Cookie, fingerprint: u64, valid_range: Range<f64>) -> Resu
150
155
151
156
mod cookie_builder {
152
157
use cipher:: { BlockDecrypt , BlockEncrypt } ;
153
- use tracing:: { instrument, Level } ;
158
+ use tracing:: instrument;
154
159
use zerocopy:: { byteorder, AsBytes as _, NativeEndian } ;
155
160
156
161
pub type CookiePlainText = CipherArrayBlowfish ;
157
162
pub type CookieCipherText = CipherArrayBlowfish ;
158
163
159
164
use crate :: shared:: crypto:: keys:: { CipherArrayBlowfish , Current , Keeper } ;
160
165
161
- #[ instrument( ret ( level = Level :: TRACE ) ) ]
166
+ #[ instrument( ) ]
162
167
pub ( super ) fn assemble ( fingerprint : u64 , issue_at : f64 ) -> CookiePlainText {
163
168
let issue_at: byteorder:: I64 < NativeEndian > =
164
169
* zerocopy:: FromBytes :: ref_from ( & issue_at. to_ne_bytes ( ) ) . expect ( "it should be aligned" ) ;
@@ -172,7 +177,7 @@ mod cookie_builder {
172
177
* CipherArrayBlowfish :: from_slice ( cookie. as_bytes ( ) )
173
178
}
174
179
175
- #[ instrument( ret ( level = Level :: TRACE ) ) ]
180
+ #[ instrument( ) ]
176
181
pub ( super ) fn disassemble ( fingerprint : u64 , cookie : CookiePlainText ) -> f64 {
177
182
let fingerprint: byteorder:: I64 < NativeEndian > =
178
183
* zerocopy:: FromBytes :: ref_from ( & fingerprint. to_ne_bytes ( ) ) . expect ( "it should be aligned" ) ;
@@ -189,7 +194,7 @@ mod cookie_builder {
189
194
issue_time. get ( )
190
195
}
191
196
192
- #[ instrument( ret ( level = Level :: TRACE ) ) ]
197
+ #[ instrument( ) ]
193
198
pub ( super ) fn encode ( mut cookie : CookiePlainText ) -> CookieCipherText {
194
199
let cipher = Current :: get_cipher_blowfish ( ) ;
195
200
@@ -198,7 +203,7 @@ mod cookie_builder {
198
203
cookie
199
204
}
200
205
201
- #[ instrument( ret ( level = Level :: TRACE ) ) ]
206
+ #[ instrument( ) ]
202
207
pub ( super ) fn decode ( mut cookie : CookieCipherText ) -> CookiePlainText {
203
208
let cipher = Current :: get_cipher_blowfish ( ) ;
204
209
@@ -282,7 +287,7 @@ mod tests {
282
287
let result = check ( & cookie, fingerprint, min..max) . unwrap_err ( ) ;
283
288
284
289
match result {
285
- Error :: ConnectionIdExpired { .. } => { } // Expected error
290
+ Error :: CookieValueExpired { .. } => { } // Expected error
286
291
_ => panic ! ( "Expected ConnectionIdExpired error" ) ,
287
292
}
288
293
}
@@ -300,7 +305,7 @@ mod tests {
300
305
let result = check ( & cookie, fingerprint, min..max) . unwrap_err ( ) ;
301
306
302
307
match result {
303
- Error :: ConnectionIdFromFuture { .. } => { } // Expected error
308
+ Error :: CookieValueFromFuture { .. } => { } // Expected error
304
309
_ => panic ! ( "Expected ConnectionIdFromFuture error" ) ,
305
310
}
306
311
}
0 commit comments