7979
8080use aquatic_udp_protocol:: ConnectionId as Cookie ;
8181use cookie_builder:: { assemble, decode, disassemble, encode} ;
82+ use tracing:: instrument;
8283use zerocopy:: AsBytes ;
8384
8485use super :: error:: Error ;
@@ -94,9 +95,12 @@ use crate::shared::crypto::keys::CipherArrayBlowfish;
9495///
9596/// It would panic if the cookie is not exactly 8 bytes is size.
9697///
98+ #[ instrument( err) ]
9799pub fn make ( fingerprint : u64 , issue_at : f64 ) -> Result < Cookie , Error > {
98100 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+ } ) ;
100104 }
101105
102106 let cookie = assemble ( fingerprint, issue_at) ;
@@ -117,6 +121,7 @@ use std::ops::Range;
117121/// # Panics
118122///
119123/// It would panic if the range start is not smaller than it's end.
124+ #[ instrument( err) ]
120125pub fn check ( cookie : & Cookie , fingerprint : u64 , valid_range : Range < f64 > ) -> Result < f64 , Error > {
121126 assert ! ( valid_range. start <= valid_range. end, "range start is larger than range end" ) ;
122127
@@ -126,20 +131,20 @@ pub fn check(cookie: &Cookie, fingerprint: u64, valid_range: Range<f64>) -> Resu
126131 let issue_time = disassemble ( fingerprint, cookie_bytes) ;
127132
128133 if !issue_time. is_normal ( ) {
129- return Err ( Error :: ConnectionIdNotNormal {
134+ return Err ( Error :: CookieValueNotNormal {
130135 not_normal_value : issue_time,
131136 } ) ;
132137 }
133138
134139 if issue_time < valid_range. start {
135- return Err ( Error :: ConnectionIdExpired {
140+ return Err ( Error :: CookieValueExpired {
136141 expired_value : issue_time,
137142 min_value : valid_range. start ,
138143 } ) ;
139144 }
140145
141146 if issue_time > valid_range. end {
142- return Err ( Error :: ConnectionIdFromFuture {
147+ return Err ( Error :: CookieValueFromFuture {
143148 future_value : issue_time,
144149 max_value : valid_range. end ,
145150 } ) ;
@@ -150,15 +155,15 @@ pub fn check(cookie: &Cookie, fingerprint: u64, valid_range: Range<f64>) -> Resu
150155
151156mod cookie_builder {
152157 use cipher:: { BlockDecrypt , BlockEncrypt } ;
153- use tracing:: { instrument, Level } ;
158+ use tracing:: instrument;
154159 use zerocopy:: { byteorder, AsBytes as _, NativeEndian } ;
155160
156161 pub type CookiePlainText = CipherArrayBlowfish ;
157162 pub type CookieCipherText = CipherArrayBlowfish ;
158163
159164 use crate :: shared:: crypto:: keys:: { CipherArrayBlowfish , Current , Keeper } ;
160165
161- #[ instrument( ret ( level = Level :: TRACE ) ) ]
166+ #[ instrument( ) ]
162167 pub ( super ) fn assemble ( fingerprint : u64 , issue_at : f64 ) -> CookiePlainText {
163168 let issue_at: byteorder:: I64 < NativeEndian > =
164169 * zerocopy:: FromBytes :: ref_from ( & issue_at. to_ne_bytes ( ) ) . expect ( "it should be aligned" ) ;
@@ -172,7 +177,7 @@ mod cookie_builder {
172177 * CipherArrayBlowfish :: from_slice ( cookie. as_bytes ( ) )
173178 }
174179
175- #[ instrument( ret ( level = Level :: TRACE ) ) ]
180+ #[ instrument( ) ]
176181 pub ( super ) fn disassemble ( fingerprint : u64 , cookie : CookiePlainText ) -> f64 {
177182 let fingerprint: byteorder:: I64 < NativeEndian > =
178183 * zerocopy:: FromBytes :: ref_from ( & fingerprint. to_ne_bytes ( ) ) . expect ( "it should be aligned" ) ;
@@ -189,7 +194,7 @@ mod cookie_builder {
189194 issue_time. get ( )
190195 }
191196
192- #[ instrument( ret ( level = Level :: TRACE ) ) ]
197+ #[ instrument( ) ]
193198 pub ( super ) fn encode ( mut cookie : CookiePlainText ) -> CookieCipherText {
194199 let cipher = Current :: get_cipher_blowfish ( ) ;
195200
@@ -198,7 +203,7 @@ mod cookie_builder {
198203 cookie
199204 }
200205
201- #[ instrument( ret ( level = Level :: TRACE ) ) ]
206+ #[ instrument( ) ]
202207 pub ( super ) fn decode ( mut cookie : CookieCipherText ) -> CookiePlainText {
203208 let cipher = Current :: get_cipher_blowfish ( ) ;
204209
@@ -282,7 +287,7 @@ mod tests {
282287 let result = check ( & cookie, fingerprint, min..max) . unwrap_err ( ) ;
283288
284289 match result {
285- Error :: ConnectionIdExpired { .. } => { } // Expected error
290+ Error :: CookieValueExpired { .. } => { } // Expected error
286291 _ => panic ! ( "Expected ConnectionIdExpired error" ) ,
287292 }
288293 }
@@ -300,7 +305,7 @@ mod tests {
300305 let result = check ( & cookie, fingerprint, min..max) . unwrap_err ( ) ;
301306
302307 match result {
303- Error :: ConnectionIdFromFuture { .. } => { } // Expected error
308+ Error :: CookieValueFromFuture { .. } => { } // Expected error
304309 _ => panic ! ( "Expected ConnectionIdFromFuture error" ) ,
305310 }
306311 }
0 commit comments