1
1
use enr:: { CombinedKey , Enr } ;
2
2
use rlp:: { DecoderError , RlpStream } ;
3
- use std:: net:: { IpAddr , Ipv6Addr } ;
3
+ use std:: {
4
+ convert:: TryInto ,
5
+ net:: { IpAddr , Ipv6Addr } ,
6
+ num:: NonZeroU16 ,
7
+ } ;
4
8
use tracing:: { debug, warn} ;
5
9
6
10
/// Type to manage the request IDs.
@@ -89,7 +93,7 @@ pub enum ResponseBody {
89
93
/// Our external IP address as observed by the responder.
90
94
ip : IpAddr ,
91
95
/// Our external UDP port as observed by the responder.
92
- port : u16 ,
96
+ port : NonZeroU16 ,
93
97
} ,
94
98
/// A NODES response.
95
99
Nodes {
@@ -186,7 +190,7 @@ impl Response {
186
190
IpAddr :: V4 ( addr) => s. append ( & ( & addr. octets ( ) as & [ u8 ] ) ) ,
187
191
IpAddr :: V6 ( addr) => s. append ( & ( & addr. octets ( ) as & [ u8 ] ) ) ,
188
192
} ;
189
- s. append ( & port) ;
193
+ s. append ( & port. get ( ) ) ;
190
194
buf. extend_from_slice ( & s. out ( ) ) ;
191
195
buf
192
196
}
@@ -378,15 +382,20 @@ impl Message {
378
382
return Err ( DecoderError :: RlpIncorrectListLen ) ;
379
383
}
380
384
} ;
381
- let port = rlp. val_at :: < u16 > ( 3 ) ?;
382
- Message :: Response ( Response {
383
- id,
384
- body : ResponseBody :: Pong {
385
- enr_seq : rlp. val_at :: < u64 > ( 1 ) ?,
386
- ip,
387
- port,
388
- } ,
389
- } )
385
+ let raw_port = rlp. val_at :: < u16 > ( 3 ) ?;
386
+ if let Ok ( port) = raw_port. try_into ( ) {
387
+ Message :: Response ( Response {
388
+ id,
389
+ body : ResponseBody :: Pong {
390
+ enr_seq : rlp. val_at :: < u64 > ( 1 ) ?,
391
+ ip,
392
+ port,
393
+ } ,
394
+ } )
395
+ } else {
396
+ debug ! ( "The port number should be non zero: {raw_port}" ) ;
397
+ return Err ( DecoderError :: Custom ( "PONG response port number invalid" ) ) ;
398
+ }
390
399
}
391
400
3 => {
392
401
// FindNodeRequest
@@ -530,7 +539,11 @@ mod tests {
530
539
let port = 5000 ;
531
540
let message = Message :: Response ( Response {
532
541
id,
533
- body : ResponseBody :: Pong { enr_seq, ip, port } ,
542
+ body : ResponseBody :: Pong {
543
+ enr_seq,
544
+ ip,
545
+ port : port. try_into ( ) . unwrap ( ) ,
546
+ } ,
534
547
} ) ;
535
548
536
549
// expected hex output
@@ -647,7 +660,7 @@ mod tests {
647
660
body : ResponseBody :: Pong {
648
661
enr_seq : 15 ,
649
662
ip : "127.0.0.1" . parse ( ) . unwrap ( ) ,
650
- port : 80 ,
663
+ port : 80 . try_into ( ) . unwrap ( ) ,
651
664
} ,
652
665
} ) ;
653
666
@@ -665,7 +678,7 @@ mod tests {
665
678
body : ResponseBody :: Pong {
666
679
enr_seq : 15 ,
667
680
ip : IpAddr :: V6 ( Ipv4Addr :: new ( 192 , 0 , 2 , 1 ) . to_ipv6_mapped ( ) ) ,
668
- port : 80 ,
681
+ port : 80 . try_into ( ) . unwrap ( ) ,
669
682
} ,
670
683
} ) ;
671
684
@@ -676,7 +689,7 @@ mod tests {
676
689
body : ResponseBody :: Pong {
677
690
enr_seq : 15 ,
678
691
ip : IpAddr :: V4 ( Ipv4Addr :: new ( 192 , 0 , 2 , 1 ) ) ,
679
- port : 80 ,
692
+ port : 80 . try_into ( ) . unwrap ( ) ,
680
693
} ,
681
694
} ) ;
682
695
@@ -691,7 +704,7 @@ mod tests {
691
704
body : ResponseBody :: Pong {
692
705
enr_seq : 15 ,
693
706
ip : IpAddr :: V6 ( Ipv6Addr :: LOCALHOST ) ,
694
- port : 80 ,
707
+ port : 80 . try_into ( ) . unwrap ( ) ,
695
708
} ,
696
709
} ) ;
697
710
0 commit comments