R2Pipe

Struct R2Pipe 

Source
pub struct R2Pipe(/* private fields */);
Expand description

Provides abstraction between the three invocation methods.

Implementations§

Source§

impl R2Pipe

Source

pub fn load_native<T: AsRef<str>>(path: T) -> Result<R2Pipe>

Source

pub fn open() -> Result<R2Pipe>

Examples found in repository?
examples/main.rs (line 23)
15fn main() -> Result<()> {
16    test_trim()?;
17
18    let opts = R2PipeSpawnOptions {
19        exepath: "radare2".to_owned(),
20        ..Default::default()
21    };
22    let mut r2p = match R2Pipe::in_session() {
23        Some(_) => R2Pipe::open()?,
24        None => R2Pipe::spawn("/bin/ls".to_owned(), Some(opts))?,
25    };
26
27    println!("{}", r2p.cmd("?e Hello World")?);
28
29    let json = r2p.cmdj("ij")?;
30    println!("{}", serde_json::to_string_pretty(&json)?);
31    println!("ARCH {}", json["bin"]["arch"]);
32    println!("BITS {}", json["bin"]["bits"]);
33    println!("Disasm:\n{}", r2p.cmd("pd 20")?);
34    println!("Hexdump:\n{}", r2p.cmd("px 64")?);
35    r2p.close();
36
37    Ok(())
38}
More examples
Hide additional examples
examples/async.rs (line 46)
41    pub fn mainloop(mut self) {
42        let child_rx = self.rx;
43        let child_tx = self.tx2.clone();
44        let child = thread::spawn(move || {
45            let mut r2p = match R2Pipe::in_session() {
46                Some(_) => R2Pipe::open(),
47                None => R2Pipe::spawn(FILENAME.to_owned(), None),
48            }
49            .unwrap();
50            loop {
51                let msg = child_rx.recv().unwrap();
52                if msg == "q" {
53                    // push a result without callback
54                    child_tx.send("".to_owned()).unwrap();
55                    drop(child_tx);
56                    break;
57                }
58                let res = r2p.cmd(&msg).unwrap();
59                child_tx.send(res).unwrap();
60            }
61            r2p.close();
62        });
63
64        // main loop
65        loop {
66            let msg = self.rx2.recv();
67            if msg.is_ok() {
68                let res = msg.unwrap();
69                if let Some(cb) = self.cbs.pop() {
70                    cb(res.trim().to_string());
71                } else {
72                    break;
73                }
74            } else {
75                break;
76            }
77        }
78        child.join().unwrap();
79    }
Source

pub fn cmd(&mut self, cmd: &str) -> Result<String>

Examples found in repository?
examples/main.rs (line 8)
6fn test_trim() -> Result<()> {
7    let mut ns = R2Pipe::spawn("/bin/ls".to_owned(), None)?;
8    println!("(({}))", ns.cmd("\n\n?e hello world\n\n")?);
9    println!("(({}))", ns.cmd("\n\n?e hello world\n\n")?);
10    println!("(({}))", ns.cmd("\n\n?e hello world\n\n")?);
11    ns.close();
12    Ok(())
13}
14
15fn main() -> Result<()> {
16    test_trim()?;
17
18    let opts = R2PipeSpawnOptions {
19        exepath: "radare2".to_owned(),
20        ..Default::default()
21    };
22    let mut r2p = match R2Pipe::in_session() {
23        Some(_) => R2Pipe::open()?,
24        None => R2Pipe::spawn("/bin/ls".to_owned(), Some(opts))?,
25    };
26
27    println!("{}", r2p.cmd("?e Hello World")?);
28
29    let json = r2p.cmdj("ij")?;
30    println!("{}", serde_json::to_string_pretty(&json)?);
31    println!("ARCH {}", json["bin"]["arch"]);
32    println!("BITS {}", json["bin"]["bits"]);
33    println!("Disasm:\n{}", r2p.cmd("pd 20")?);
34    println!("Hexdump:\n{}", r2p.cmd("px 64")?);
35    r2p.close();
36
37    Ok(())
38}
More examples
Hide additional examples
examples/tcp.rs (line 8)
5fn main() -> Result<()> {
6    let mut r2p = R2Pipe::tcp("localhost:9080")?;
7
8    println!("{}", r2p.cmd("?e Hello World")?);
9
10    let json = r2p.cmdj("ij")?;
11    println!("{}", serde_json::to_string_pretty(&json)?);
12    println!("ARCH {}", json["bin"]["arch"]);
13    println!("BITS {}", json["bin"]["bits"]);
14    println!("Disasm:\n{}", r2p.cmd("pd 20")?);
15    println!("Hexdump:\n{}", r2p.cmd("px 64")?);
16    r2p.close();
17
18    Ok(())
19}
examples/quit.rs (line 5)
3fn main() -> Result<()> {
4    let mut r2p = R2Pipe::spawn("/bin/ls".to_owned(), None)?;
5    println!("{}", r2p.cmd("?e Hello")?);
6    if let Err(_) = r2p.cmd("q") {
7        // !killall r2") {
8        println!("Quit happens!");
9    } else {
10        println!("Quit failed/ignored!");
11        if let Ok(msg) = r2p.cmd("?e World") {
12            println!("{}", msg);
13            r2p.close();
14        } else {
15            println!("FAIL");
16        }
17    }
18    println!("Byebye");
19    Ok(())
20}
examples/async.rs (line 58)
41    pub fn mainloop(mut self) {
42        let child_rx = self.rx;
43        let child_tx = self.tx2.clone();
44        let child = thread::spawn(move || {
45            let mut r2p = match R2Pipe::in_session() {
46                Some(_) => R2Pipe::open(),
47                None => R2Pipe::spawn(FILENAME.to_owned(), None),
48            }
49            .unwrap();
50            loop {
51                let msg = child_rx.recv().unwrap();
52                if msg == "q" {
53                    // push a result without callback
54                    child_tx.send("".to_owned()).unwrap();
55                    drop(child_tx);
56                    break;
57                }
58                let res = r2p.cmd(&msg).unwrap();
59                child_tx.send(res).unwrap();
60            }
61            r2p.close();
62        });
63
64        // main loop
65        loop {
66            let msg = self.rx2.recv();
67            if msg.is_ok() {
68                let res = msg.unwrap();
69                if let Some(cb) = self.cbs.pop() {
70                    cb(res.trim().to_string());
71                } else {
72                    break;
73                }
74            } else {
75                break;
76            }
77        }
78        child.join().unwrap();
79    }
Source

pub fn cmdj(&mut self, cmd: &str) -> Result<Value>

Examples found in repository?
examples/tcp.rs (line 10)
5fn main() -> Result<()> {
6    let mut r2p = R2Pipe::tcp("localhost:9080")?;
7
8    println!("{}", r2p.cmd("?e Hello World")?);
9
10    let json = r2p.cmdj("ij")?;
11    println!("{}", serde_json::to_string_pretty(&json)?);
12    println!("ARCH {}", json["bin"]["arch"]);
13    println!("BITS {}", json["bin"]["bits"]);
14    println!("Disasm:\n{}", r2p.cmd("pd 20")?);
15    println!("Hexdump:\n{}", r2p.cmd("px 64")?);
16    r2p.close();
17
18    Ok(())
19}
More examples
Hide additional examples
examples/main.rs (line 29)
15fn main() -> Result<()> {
16    test_trim()?;
17
18    let opts = R2PipeSpawnOptions {
19        exepath: "radare2".to_owned(),
20        ..Default::default()
21    };
22    let mut r2p = match R2Pipe::in_session() {
23        Some(_) => R2Pipe::open()?,
24        None => R2Pipe::spawn("/bin/ls".to_owned(), Some(opts))?,
25    };
26
27    println!("{}", r2p.cmd("?e Hello World")?);
28
29    let json = r2p.cmdj("ij")?;
30    println!("{}", serde_json::to_string_pretty(&json)?);
31    println!("ARCH {}", json["bin"]["arch"]);
32    println!("BITS {}", json["bin"]["bits"]);
33    println!("Disasm:\n{}", r2p.cmd("pd 20")?);
34    println!("Hexdump:\n{}", r2p.cmd("px 64")?);
35    r2p.close();
36
37    Ok(())
38}
Source

pub fn close(&mut self)

Examples found in repository?
examples/main.rs (line 11)
6fn test_trim() -> Result<()> {
7    let mut ns = R2Pipe::spawn("/bin/ls".to_owned(), None)?;
8    println!("(({}))", ns.cmd("\n\n?e hello world\n\n")?);
9    println!("(({}))", ns.cmd("\n\n?e hello world\n\n")?);
10    println!("(({}))", ns.cmd("\n\n?e hello world\n\n")?);
11    ns.close();
12    Ok(())
13}
14
15fn main() -> Result<()> {
16    test_trim()?;
17
18    let opts = R2PipeSpawnOptions {
19        exepath: "radare2".to_owned(),
20        ..Default::default()
21    };
22    let mut r2p = match R2Pipe::in_session() {
23        Some(_) => R2Pipe::open()?,
24        None => R2Pipe::spawn("/bin/ls".to_owned(), Some(opts))?,
25    };
26
27    println!("{}", r2p.cmd("?e Hello World")?);
28
29    let json = r2p.cmdj("ij")?;
30    println!("{}", serde_json::to_string_pretty(&json)?);
31    println!("ARCH {}", json["bin"]["arch"]);
32    println!("BITS {}", json["bin"]["bits"]);
33    println!("Disasm:\n{}", r2p.cmd("pd 20")?);
34    println!("Hexdump:\n{}", r2p.cmd("px 64")?);
35    r2p.close();
36
37    Ok(())
38}
More examples
Hide additional examples
examples/tcp.rs (line 16)
5fn main() -> Result<()> {
6    let mut r2p = R2Pipe::tcp("localhost:9080")?;
7
8    println!("{}", r2p.cmd("?e Hello World")?);
9
10    let json = r2p.cmdj("ij")?;
11    println!("{}", serde_json::to_string_pretty(&json)?);
12    println!("ARCH {}", json["bin"]["arch"]);
13    println!("BITS {}", json["bin"]["bits"]);
14    println!("Disasm:\n{}", r2p.cmd("pd 20")?);
15    println!("Hexdump:\n{}", r2p.cmd("px 64")?);
16    r2p.close();
17
18    Ok(())
19}
examples/quit.rs (line 13)
3fn main() -> Result<()> {
4    let mut r2p = R2Pipe::spawn("/bin/ls".to_owned(), None)?;
5    println!("{}", r2p.cmd("?e Hello")?);
6    if let Err(_) = r2p.cmd("q") {
7        // !killall r2") {
8        println!("Quit happens!");
9    } else {
10        println!("Quit failed/ignored!");
11        if let Ok(msg) = r2p.cmd("?e World") {
12            println!("{}", msg);
13            r2p.close();
14        } else {
15            println!("FAIL");
16        }
17    }
18    println!("Byebye");
19    Ok(())
20}
examples/async.rs (line 61)
41    pub fn mainloop(mut self) {
42        let child_rx = self.rx;
43        let child_tx = self.tx2.clone();
44        let child = thread::spawn(move || {
45            let mut r2p = match R2Pipe::in_session() {
46                Some(_) => R2Pipe::open(),
47                None => R2Pipe::spawn(FILENAME.to_owned(), None),
48            }
49            .unwrap();
50            loop {
51                let msg = child_rx.recv().unwrap();
52                if msg == "q" {
53                    // push a result without callback
54                    child_tx.send("".to_owned()).unwrap();
55                    drop(child_tx);
56                    break;
57                }
58                let res = r2p.cmd(&msg).unwrap();
59                child_tx.send(res).unwrap();
60            }
61            r2p.close();
62        });
63
64        // main loop
65        loop {
66            let msg = self.rx2.recv();
67            if msg.is_ok() {
68                let res = msg.unwrap();
69                if let Some(cb) = self.cbs.pop() {
70                    cb(res.trim().to_string());
71                } else {
72                    break;
73                }
74            } else {
75                break;
76            }
77        }
78        child.join().unwrap();
79    }
Source

pub fn call(&mut self, cmd: &str) -> Result<String>

Escape the command before executing, valid only as of r2 v.5.8.0 “icebucket”

Source

pub fn callj(&mut self, cmd: &str) -> Result<Value>

Escape the command before executing and convert it to a json value, valid only as of r2 v.5.8.0 “icebucket”

Source

pub fn in_session() -> Option<(i32, i32)>

Examples found in repository?
examples/main.rs (line 22)
15fn main() -> Result<()> {
16    test_trim()?;
17
18    let opts = R2PipeSpawnOptions {
19        exepath: "radare2".to_owned(),
20        ..Default::default()
21    };
22    let mut r2p = match R2Pipe::in_session() {
23        Some(_) => R2Pipe::open()?,
24        None => R2Pipe::spawn("/bin/ls".to_owned(), Some(opts))?,
25    };
26
27    println!("{}", r2p.cmd("?e Hello World")?);
28
29    let json = r2p.cmdj("ij")?;
30    println!("{}", serde_json::to_string_pretty(&json)?);
31    println!("ARCH {}", json["bin"]["arch"]);
32    println!("BITS {}", json["bin"]["bits"]);
33    println!("Disasm:\n{}", r2p.cmd("pd 20")?);
34    println!("Hexdump:\n{}", r2p.cmd("px 64")?);
35    r2p.close();
36
37    Ok(())
38}
More examples
Hide additional examples
examples/async.rs (line 45)
41    pub fn mainloop(mut self) {
42        let child_rx = self.rx;
43        let child_tx = self.tx2.clone();
44        let child = thread::spawn(move || {
45            let mut r2p = match R2Pipe::in_session() {
46                Some(_) => R2Pipe::open(),
47                None => R2Pipe::spawn(FILENAME.to_owned(), None),
48            }
49            .unwrap();
50            loop {
51                let msg = child_rx.recv().unwrap();
52                if msg == "q" {
53                    // push a result without callback
54                    child_tx.send("".to_owned()).unwrap();
55                    drop(child_tx);
56                    break;
57                }
58                let res = r2p.cmd(&msg).unwrap();
59                child_tx.send(res).unwrap();
60            }
61            r2p.close();
62        });
63
64        // main loop
65        loop {
66            let msg = self.rx2.recv();
67            if msg.is_ok() {
68                let res = msg.unwrap();
69                if let Some(cb) = self.cbs.pop() {
70                    cb(res.trim().to_string());
71                } else {
72                    break;
73                }
74            } else {
75                break;
76            }
77        }
78        child.join().unwrap();
79    }
Source

pub fn spawn<T: AsRef<str>>( name: T, opts: Option<R2PipeSpawnOptions>, ) -> Result<R2Pipe>

Creates a new R2PipeSpawn.

Examples found in repository?
examples/main.rs (line 7)
6fn test_trim() -> Result<()> {
7    let mut ns = R2Pipe::spawn("/bin/ls".to_owned(), None)?;
8    println!("(({}))", ns.cmd("\n\n?e hello world\n\n")?);
9    println!("(({}))", ns.cmd("\n\n?e hello world\n\n")?);
10    println!("(({}))", ns.cmd("\n\n?e hello world\n\n")?);
11    ns.close();
12    Ok(())
13}
14
15fn main() -> Result<()> {
16    test_trim()?;
17
18    let opts = R2PipeSpawnOptions {
19        exepath: "radare2".to_owned(),
20        ..Default::default()
21    };
22    let mut r2p = match R2Pipe::in_session() {
23        Some(_) => R2Pipe::open()?,
24        None => R2Pipe::spawn("/bin/ls".to_owned(), Some(opts))?,
25    };
26
27    println!("{}", r2p.cmd("?e Hello World")?);
28
29    let json = r2p.cmdj("ij")?;
30    println!("{}", serde_json::to_string_pretty(&json)?);
31    println!("ARCH {}", json["bin"]["arch"]);
32    println!("BITS {}", json["bin"]["bits"]);
33    println!("Disasm:\n{}", r2p.cmd("pd 20")?);
34    println!("Hexdump:\n{}", r2p.cmd("px 64")?);
35    r2p.close();
36
37    Ok(())
38}
More examples
Hide additional examples
examples/quit.rs (line 4)
3fn main() -> Result<()> {
4    let mut r2p = R2Pipe::spawn("/bin/ls".to_owned(), None)?;
5    println!("{}", r2p.cmd("?e Hello")?);
6    if let Err(_) = r2p.cmd("q") {
7        // !killall r2") {
8        println!("Quit happens!");
9    } else {
10        println!("Quit failed/ignored!");
11        if let Ok(msg) = r2p.cmd("?e World") {
12            println!("{}", msg);
13            r2p.close();
14        } else {
15            println!("FAIL");
16        }
17    }
18    println!("Byebye");
19    Ok(())
20}
examples/async.rs (line 47)
41    pub fn mainloop(mut self) {
42        let child_rx = self.rx;
43        let child_tx = self.tx2.clone();
44        let child = thread::spawn(move || {
45            let mut r2p = match R2Pipe::in_session() {
46                Some(_) => R2Pipe::open(),
47                None => R2Pipe::spawn(FILENAME.to_owned(), None),
48            }
49            .unwrap();
50            loop {
51                let msg = child_rx.recv().unwrap();
52                if msg == "q" {
53                    // push a result without callback
54                    child_tx.send("".to_owned()).unwrap();
55                    drop(child_tx);
56                    break;
57                }
58                let res = r2p.cmd(&msg).unwrap();
59                child_tx.send(res).unwrap();
60            }
61            r2p.close();
62        });
63
64        // main loop
65        loop {
66            let msg = self.rx2.recv();
67            if msg.is_ok() {
68                let res = msg.unwrap();
69                if let Some(cb) = self.cbs.pop() {
70                    cb(res.trim().to_string());
71                } else {
72                    break;
73                }
74            } else {
75                break;
76            }
77        }
78        child.join().unwrap();
79    }
Source

pub fn tcp<A: ToSocketAddrs>(addr: A) -> Result<R2Pipe>

Creates a new R2PipeTcp

Examples found in repository?
examples/tcp.rs (line 6)
5fn main() -> Result<()> {
6    let mut r2p = R2Pipe::tcp("localhost:9080")?;
7
8    println!("{}", r2p.cmd("?e Hello World")?);
9
10    let json = r2p.cmdj("ij")?;
11    println!("{}", serde_json::to_string_pretty(&json)?);
12    println!("ARCH {}", json["bin"]["arch"]);
13    println!("BITS {}", json["bin"]["bits"]);
14    println!("Disasm:\n{}", r2p.cmd("pd 20")?);
15    println!("Hexdump:\n{}", r2p.cmd("px 64")?);
16    r2p.close();
17
18    Ok(())
19}
Source

pub fn http(host: &str) -> R2Pipe

Creates a new R2PipeHttp

Source

pub fn threads( names: Vec<&'static str>, opts: Vec<Option<R2PipeSpawnOptions>>, callback: Option<Arc<dyn Fn(u16, String) + Sync + Send>>, ) -> Result<Vec<R2PipeThread>>

Creates new pipe threads First two arguments for R2Pipe::threads() are the same as for R2Pipe::spawn() but inside vectors Third and last argument is an option to a callback function The callback function takes two Arguments: Thread ID and r2pipe output

Examples found in repository?
examples/threads.rs (lines 7-11)
3fn main() -> Result<()> {
4    // Lets spawn some r2pipes to open some binaries
5    // First two arguments for R2Pipe::threads() are the same as for R2Pipe::spawn() but inside vectors
6    // Third and last argument is an option of a callback function
7    let pipes = match R2Pipe::threads(
8        vec!["/bin/ls", "/bin/id", "/bin/cat"],
9        vec![None, None, None],
10        None,
11    ) {
12        Ok(p) => p,
13        Err(e) => {
14            println!("Error spawning Pipes: {}", e);
15            return Ok(());
16        }
17    };
18
19    // At this point we can iter through all of our r2pipes and send some commands
20    for p in pipes.iter() {
21        if let Ok(_) = p.send("ij".to_string()) {};
22    }
23
24    // do_other_stuff_here();
25
26    // Lets iter again and see what the pipes got
27    for p in pipes.iter() {
28        // this will block, do "p.recv(false)" for non-blocking receive inside a loop
29        if let Ok(msg) = p.recv(true) {
30            println!("Pipe #{} says: {}", p.id, msg);
31        }
32    }
33
34    // Finally properly close all pipes
35    // Note: For "join()" we need to borrow so pipes.iter() won't work for this
36    for p in pipes {
37        if let Ok(_) = p.send("q".to_string()) {};
38        p.handle.join().unwrap()?;
39    }
40
41    Ok(())
42}
More examples
Hide additional examples
examples/threads_callback.rs (lines 14-18)
4fn main() -> Result<()> {
5    // First we define a callback. It doesn't block and gets called after a thread receives output from r2pipe
6    // Note: First argument to the callback is the thread id, second one the r2pipe output
7    let callback = Arc::new(|id, result| {
8        println!("Pipe #{} says: {}", id, result);
9    });
10
11    // First two arguments for R2Pipe::threads() are the same as for R2Pipe::spawn() but inside vectors
12    // Third and last argument is an option to a callback function
13    // The callback function takes two Arguments: Thread ID and r2pipe output
14    let pipes = match R2Pipe::threads(
15        vec!["/bin/ls", "/bin/id", "/bin/cat"],
16        vec![None, None, None],
17        Some(callback),
18    ) {
19        Ok(p) => p,
20        Err(e) => {
21            println!("Error spawning Pipes: {}", e);
22            return Ok(());
23        }
24    };
25
26    // At this point we can iter through all of our r2pipes and send some commands
27    for p in pipes.iter() {
28        if let Ok(_) = p.send("ij".to_string()) {};
29    }
30
31    // Meanwhile: Expecting callbacks
32    std::thread::sleep(std::time::Duration::from_millis(1000));
33
34    // Finally properly close all pipes
35    // Note: For "join()" we need to borrow so pipes.iter() won't work for this
36    for p in pipes {
37        if let Ok(_) = p.send("q".to_string()) {};
38        p.handle.join().unwrap()?;
39    }
40
41    Ok(())
42}

Auto Trait Implementations§

§

impl Freeze for R2Pipe

§

impl !RefUnwindSafe for R2Pipe

§

impl !Send for R2Pipe

§

impl !Sync for R2Pipe

§

impl Unpin for R2Pipe

§

impl !UnwindSafe for R2Pipe

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.