6
6
#[ cfg( feature = "proxy" ) ]
7
7
use socks:: Socks5Stream ;
8
8
use std:: io:: { BufRead , BufReader , Read , Write } ;
9
+ #[ cfg( not( fuzzing) ) ]
9
10
use std:: net:: TcpStream ;
10
11
use std:: net:: { SocketAddr , ToSocketAddrs } ;
11
12
use std:: sync:: { Arc , Mutex } ;
@@ -19,6 +20,43 @@ use serde_json;
19
20
use crate :: client:: Transport ;
20
21
use crate :: { Request , Response } ;
21
22
23
+ #[ cfg( fuzzing) ]
24
+ /// Global mutex used by the fuzzing harness to inject data into the read
25
+ /// end of the TCP stream.
26
+ pub static FUZZ_TCP_SOCK : Mutex < Option < io:: Cursor < Vec < u8 > > > > = Mutex :: new ( None ) ;
27
+
28
+ #[ cfg( fuzzing) ]
29
+ #[ derive( Clone , Debug ) ]
30
+ struct TcpStream ;
31
+
32
+ #[ cfg( fuzzing) ]
33
+ mod impls {
34
+ use super :: * ;
35
+ impl Read for TcpStream {
36
+ fn read ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
37
+ match * FUZZ_TCP_SOCK . lock ( ) . unwrap ( ) {
38
+ Some ( ref mut cursor) => io:: Read :: read ( cursor, buf) ,
39
+ None => Ok ( 0 ) ,
40
+ }
41
+ }
42
+ }
43
+ impl Write for TcpStream {
44
+ fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
45
+ io:: sink ( ) . write ( buf)
46
+ }
47
+ fn flush ( & mut self ) -> io:: Result < ( ) > {
48
+ Ok ( ( ) )
49
+ }
50
+ }
51
+
52
+ impl TcpStream {
53
+ pub fn connect_timeout ( _: & SocketAddr , _: Duration ) -> io:: Result < Self > { Ok ( TcpStream ) }
54
+ pub fn set_read_timeout ( & self , _: Option < Duration > ) -> io:: Result < ( ) > { Ok ( ( ) ) }
55
+ pub fn set_write_timeout ( & self , _: Option < Duration > ) -> io:: Result < ( ) > { Ok ( ( ) ) }
56
+ }
57
+ }
58
+
59
+
22
60
/// The default TCP port to use for connections.
23
61
/// Set to 8332, the default RPC port for bitcoind.
24
62
pub const DEFAULT_PORT : u16 = 8332 ;
0 commit comments