pub struct R2Pipe(_);
Expand description
Provides abstraction between the three invocation methods.
Implementations§
source§impl R2Pipe
impl R2Pipe
pub fn load_native<T: AsRef<str>>(path: T) -> Result<R2Pipe>
sourcepub fn open() -> Result<R2Pipe>
pub fn open() -> Result<R2Pipe>
Examples found in repository?
examples/main.rs (line 23)
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
fn main() -> Result<()> {
test_trim()?;
let opts = R2PipeSpawnOptions {
exepath: "radare2".to_owned(),
..Default::default()
};
let mut r2p = match R2Pipe::in_session() {
Some(_) => R2Pipe::open()?,
None => R2Pipe::spawn("/bin/ls".to_owned(), Some(opts))?,
};
println!("{}", r2p.cmd("?e Hello World")?);
let json = r2p.cmdj("ij")?;
println!("{}", serde_json::to_string_pretty(&json)?);
println!("ARCH {}", json["bin"]["arch"]);
println!("BITS {}", json["bin"]["bits"]);
println!("Disasm:\n{}", r2p.cmd("pd 20")?);
println!("Hexdump:\n{}", r2p.cmd("px 64")?);
r2p.close();
Ok(())
}
More examples
examples/async.rs (line 46)
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
pub fn mainloop(mut self) {
let child_rx = self.rx;
let child_tx = self.tx2.clone();
let child = thread::spawn(move || {
let mut r2p = match R2Pipe::in_session() {
Some(_) => R2Pipe::open(),
None => R2Pipe::spawn(FILENAME.to_owned(), None),
}
.unwrap();
loop {
let msg = child_rx.recv().unwrap();
if msg == "q" {
// push a result without callback
child_tx.send("".to_owned()).unwrap();
drop(child_tx);
break;
}
let res = r2p.cmd(&msg).unwrap();
child_tx.send(res).unwrap();
}
r2p.close();
});
// main loop
loop {
let msg = self.rx2.recv();
if msg.is_ok() {
let res = msg.unwrap();
if let Some(cb) = self.cbs.pop() {
cb(res.trim().to_string());
} else {
break;
}
} else {
break;
}
}
child.join().unwrap();
}
sourcepub fn cmd(&mut self, cmd: &str) -> Result<String>
pub fn cmd(&mut self, cmd: &str) -> Result<String>
Examples found in repository?
examples/main.rs (line 8)
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
fn test_trim() -> Result<()> {
let mut ns = R2Pipe::spawn("/bin/ls".to_owned(), None)?;
println!("(({}))", ns.cmd("\n\n?e hello world\n\n")?);
println!("(({}))", ns.cmd("\n\n?e hello world\n\n")?);
println!("(({}))", ns.cmd("\n\n?e hello world\n\n")?);
ns.close();
Ok(())
}
fn main() -> Result<()> {
test_trim()?;
let opts = R2PipeSpawnOptions {
exepath: "radare2".to_owned(),
..Default::default()
};
let mut r2p = match R2Pipe::in_session() {
Some(_) => R2Pipe::open()?,
None => R2Pipe::spawn("/bin/ls".to_owned(), Some(opts))?,
};
println!("{}", r2p.cmd("?e Hello World")?);
let json = r2p.cmdj("ij")?;
println!("{}", serde_json::to_string_pretty(&json)?);
println!("ARCH {}", json["bin"]["arch"]);
println!("BITS {}", json["bin"]["bits"]);
println!("Disasm:\n{}", r2p.cmd("pd 20")?);
println!("Hexdump:\n{}", r2p.cmd("px 64")?);
r2p.close();
Ok(())
}
More examples
examples/tcp.rs (line 8)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
fn main() -> Result<()> {
let mut r2p = R2Pipe::tcp("localhost:9080")?;
println!("{}", r2p.cmd("?e Hello World")?);
let json = r2p.cmdj("ij")?;
println!("{}", serde_json::to_string_pretty(&json)?);
println!("ARCH {}", json["bin"]["arch"]);
println!("BITS {}", json["bin"]["bits"]);
println!("Disasm:\n{}", r2p.cmd("pd 20")?);
println!("Hexdump:\n{}", r2p.cmd("px 64")?);
r2p.close();
Ok(())
}
examples/quit.rs (line 5)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
fn main() -> Result<()> {
let mut r2p = R2Pipe::spawn("/bin/ls".to_owned(), None)?;
println!("{}", r2p.cmd("?e Hello")?);
if let Err(_) = r2p.cmd("q") {
// !killall r2") {
println!("Quit happens!");
} else {
println!("Quit failed/ignored!");
if let Ok(msg) = r2p.cmd("?e World") {
println!("{}", msg);
r2p.close();
} else {
println!("FAIL");
}
}
println!("Byebye");
Ok(())
}
examples/async.rs (line 58)
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
pub fn mainloop(mut self) {
let child_rx = self.rx;
let child_tx = self.tx2.clone();
let child = thread::spawn(move || {
let mut r2p = match R2Pipe::in_session() {
Some(_) => R2Pipe::open(),
None => R2Pipe::spawn(FILENAME.to_owned(), None),
}
.unwrap();
loop {
let msg = child_rx.recv().unwrap();
if msg == "q" {
// push a result without callback
child_tx.send("".to_owned()).unwrap();
drop(child_tx);
break;
}
let res = r2p.cmd(&msg).unwrap();
child_tx.send(res).unwrap();
}
r2p.close();
});
// main loop
loop {
let msg = self.rx2.recv();
if msg.is_ok() {
let res = msg.unwrap();
if let Some(cb) = self.cbs.pop() {
cb(res.trim().to_string());
} else {
break;
}
} else {
break;
}
}
child.join().unwrap();
}
sourcepub fn cmdj(&mut self, cmd: &str) -> Result<Value>
pub fn cmdj(&mut self, cmd: &str) -> Result<Value>
Examples found in repository?
examples/tcp.rs (line 10)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
fn main() -> Result<()> {
let mut r2p = R2Pipe::tcp("localhost:9080")?;
println!("{}", r2p.cmd("?e Hello World")?);
let json = r2p.cmdj("ij")?;
println!("{}", serde_json::to_string_pretty(&json)?);
println!("ARCH {}", json["bin"]["arch"]);
println!("BITS {}", json["bin"]["bits"]);
println!("Disasm:\n{}", r2p.cmd("pd 20")?);
println!("Hexdump:\n{}", r2p.cmd("px 64")?);
r2p.close();
Ok(())
}
More examples
examples/main.rs (line 29)
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
fn main() -> Result<()> {
test_trim()?;
let opts = R2PipeSpawnOptions {
exepath: "radare2".to_owned(),
..Default::default()
};
let mut r2p = match R2Pipe::in_session() {
Some(_) => R2Pipe::open()?,
None => R2Pipe::spawn("/bin/ls".to_owned(), Some(opts))?,
};
println!("{}", r2p.cmd("?e Hello World")?);
let json = r2p.cmdj("ij")?;
println!("{}", serde_json::to_string_pretty(&json)?);
println!("ARCH {}", json["bin"]["arch"]);
println!("BITS {}", json["bin"]["bits"]);
println!("Disasm:\n{}", r2p.cmd("pd 20")?);
println!("Hexdump:\n{}", r2p.cmd("px 64")?);
r2p.close();
Ok(())
}
sourcepub fn close(&mut self)
pub fn close(&mut self)
Examples found in repository?
examples/main.rs (line 11)
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
fn test_trim() -> Result<()> {
let mut ns = R2Pipe::spawn("/bin/ls".to_owned(), None)?;
println!("(({}))", ns.cmd("\n\n?e hello world\n\n")?);
println!("(({}))", ns.cmd("\n\n?e hello world\n\n")?);
println!("(({}))", ns.cmd("\n\n?e hello world\n\n")?);
ns.close();
Ok(())
}
fn main() -> Result<()> {
test_trim()?;
let opts = R2PipeSpawnOptions {
exepath: "radare2".to_owned(),
..Default::default()
};
let mut r2p = match R2Pipe::in_session() {
Some(_) => R2Pipe::open()?,
None => R2Pipe::spawn("/bin/ls".to_owned(), Some(opts))?,
};
println!("{}", r2p.cmd("?e Hello World")?);
let json = r2p.cmdj("ij")?;
println!("{}", serde_json::to_string_pretty(&json)?);
println!("ARCH {}", json["bin"]["arch"]);
println!("BITS {}", json["bin"]["bits"]);
println!("Disasm:\n{}", r2p.cmd("pd 20")?);
println!("Hexdump:\n{}", r2p.cmd("px 64")?);
r2p.close();
Ok(())
}
More examples
examples/tcp.rs (line 16)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
fn main() -> Result<()> {
let mut r2p = R2Pipe::tcp("localhost:9080")?;
println!("{}", r2p.cmd("?e Hello World")?);
let json = r2p.cmdj("ij")?;
println!("{}", serde_json::to_string_pretty(&json)?);
println!("ARCH {}", json["bin"]["arch"]);
println!("BITS {}", json["bin"]["bits"]);
println!("Disasm:\n{}", r2p.cmd("pd 20")?);
println!("Hexdump:\n{}", r2p.cmd("px 64")?);
r2p.close();
Ok(())
}
examples/quit.rs (line 13)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
fn main() -> Result<()> {
let mut r2p = R2Pipe::spawn("/bin/ls".to_owned(), None)?;
println!("{}", r2p.cmd("?e Hello")?);
if let Err(_) = r2p.cmd("q") {
// !killall r2") {
println!("Quit happens!");
} else {
println!("Quit failed/ignored!");
if let Ok(msg) = r2p.cmd("?e World") {
println!("{}", msg);
r2p.close();
} else {
println!("FAIL");
}
}
println!("Byebye");
Ok(())
}
examples/async.rs (line 61)
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
pub fn mainloop(mut self) {
let child_rx = self.rx;
let child_tx = self.tx2.clone();
let child = thread::spawn(move || {
let mut r2p = match R2Pipe::in_session() {
Some(_) => R2Pipe::open(),
None => R2Pipe::spawn(FILENAME.to_owned(), None),
}
.unwrap();
loop {
let msg = child_rx.recv().unwrap();
if msg == "q" {
// push a result without callback
child_tx.send("".to_owned()).unwrap();
drop(child_tx);
break;
}
let res = r2p.cmd(&msg).unwrap();
child_tx.send(res).unwrap();
}
r2p.close();
});
// main loop
loop {
let msg = self.rx2.recv();
if msg.is_ok() {
let res = msg.unwrap();
if let Some(cb) = self.cbs.pop() {
cb(res.trim().to_string());
} else {
break;
}
} else {
break;
}
}
child.join().unwrap();
}
sourcepub fn call(&mut self, cmd: &str) -> Result<String>
pub fn call(&mut self, cmd: &str) -> Result<String>
Escape the command before executing, valid only as of r2 v.5.8.0 “icebucket”
sourcepub fn callj(&mut self, cmd: &str) -> Result<Value>
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”
sourcepub fn in_session() -> Option<(i32, i32)>
pub fn in_session() -> Option<(i32, i32)>
Examples found in repository?
examples/main.rs (line 22)
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
fn main() -> Result<()> {
test_trim()?;
let opts = R2PipeSpawnOptions {
exepath: "radare2".to_owned(),
..Default::default()
};
let mut r2p = match R2Pipe::in_session() {
Some(_) => R2Pipe::open()?,
None => R2Pipe::spawn("/bin/ls".to_owned(), Some(opts))?,
};
println!("{}", r2p.cmd("?e Hello World")?);
let json = r2p.cmdj("ij")?;
println!("{}", serde_json::to_string_pretty(&json)?);
println!("ARCH {}", json["bin"]["arch"]);
println!("BITS {}", json["bin"]["bits"]);
println!("Disasm:\n{}", r2p.cmd("pd 20")?);
println!("Hexdump:\n{}", r2p.cmd("px 64")?);
r2p.close();
Ok(())
}
More examples
examples/async.rs (line 45)
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
pub fn mainloop(mut self) {
let child_rx = self.rx;
let child_tx = self.tx2.clone();
let child = thread::spawn(move || {
let mut r2p = match R2Pipe::in_session() {
Some(_) => R2Pipe::open(),
None => R2Pipe::spawn(FILENAME.to_owned(), None),
}
.unwrap();
loop {
let msg = child_rx.recv().unwrap();
if msg == "q" {
// push a result without callback
child_tx.send("".to_owned()).unwrap();
drop(child_tx);
break;
}
let res = r2p.cmd(&msg).unwrap();
child_tx.send(res).unwrap();
}
r2p.close();
});
// main loop
loop {
let msg = self.rx2.recv();
if msg.is_ok() {
let res = msg.unwrap();
if let Some(cb) = self.cbs.pop() {
cb(res.trim().to_string());
} else {
break;
}
} else {
break;
}
}
child.join().unwrap();
}
sourcepub fn spawn<T: AsRef<str>>(
name: T,
opts: Option<R2PipeSpawnOptions>
) -> Result<R2Pipe>
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)
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
fn test_trim() -> Result<()> {
let mut ns = R2Pipe::spawn("/bin/ls".to_owned(), None)?;
println!("(({}))", ns.cmd("\n\n?e hello world\n\n")?);
println!("(({}))", ns.cmd("\n\n?e hello world\n\n")?);
println!("(({}))", ns.cmd("\n\n?e hello world\n\n")?);
ns.close();
Ok(())
}
fn main() -> Result<()> {
test_trim()?;
let opts = R2PipeSpawnOptions {
exepath: "radare2".to_owned(),
..Default::default()
};
let mut r2p = match R2Pipe::in_session() {
Some(_) => R2Pipe::open()?,
None => R2Pipe::spawn("/bin/ls".to_owned(), Some(opts))?,
};
println!("{}", r2p.cmd("?e Hello World")?);
let json = r2p.cmdj("ij")?;
println!("{}", serde_json::to_string_pretty(&json)?);
println!("ARCH {}", json["bin"]["arch"]);
println!("BITS {}", json["bin"]["bits"]);
println!("Disasm:\n{}", r2p.cmd("pd 20")?);
println!("Hexdump:\n{}", r2p.cmd("px 64")?);
r2p.close();
Ok(())
}
More examples
examples/quit.rs (line 4)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
fn main() -> Result<()> {
let mut r2p = R2Pipe::spawn("/bin/ls".to_owned(), None)?;
println!("{}", r2p.cmd("?e Hello")?);
if let Err(_) = r2p.cmd("q") {
// !killall r2") {
println!("Quit happens!");
} else {
println!("Quit failed/ignored!");
if let Ok(msg) = r2p.cmd("?e World") {
println!("{}", msg);
r2p.close();
} else {
println!("FAIL");
}
}
println!("Byebye");
Ok(())
}
examples/async.rs (line 47)
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
pub fn mainloop(mut self) {
let child_rx = self.rx;
let child_tx = self.tx2.clone();
let child = thread::spawn(move || {
let mut r2p = match R2Pipe::in_session() {
Some(_) => R2Pipe::open(),
None => R2Pipe::spawn(FILENAME.to_owned(), None),
}
.unwrap();
loop {
let msg = child_rx.recv().unwrap();
if msg == "q" {
// push a result without callback
child_tx.send("".to_owned()).unwrap();
drop(child_tx);
break;
}
let res = r2p.cmd(&msg).unwrap();
child_tx.send(res).unwrap();
}
r2p.close();
});
// main loop
loop {
let msg = self.rx2.recv();
if msg.is_ok() {
let res = msg.unwrap();
if let Some(cb) = self.cbs.pop() {
cb(res.trim().to_string());
} else {
break;
}
} else {
break;
}
}
child.join().unwrap();
}
sourcepub fn tcp<A: ToSocketAddrs>(addr: A) -> Result<R2Pipe>
pub fn tcp<A: ToSocketAddrs>(addr: A) -> Result<R2Pipe>
Creates a new R2PipeTcp
Examples found in repository?
examples/tcp.rs (line 6)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
fn main() -> Result<()> {
let mut r2p = R2Pipe::tcp("localhost:9080")?;
println!("{}", r2p.cmd("?e Hello World")?);
let json = r2p.cmdj("ij")?;
println!("{}", serde_json::to_string_pretty(&json)?);
println!("ARCH {}", json["bin"]["arch"]);
println!("BITS {}", json["bin"]["bits"]);
println!("Disasm:\n{}", r2p.cmd("pd 20")?);
println!("Hexdump:\n{}", r2p.cmd("px 64")?);
r2p.close();
Ok(())
}
sourcepub fn threads(
names: Vec<&'static str>,
opts: Vec<Option<R2PipeSpawnOptions>>,
callback: Option<Arc<dyn Fn(u16, String) + Sync + Send>>
) -> Result<Vec<R2PipeThread>>
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)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
fn main() -> Result<()> {
// Lets spawn some r2pipes to open some binaries
// First two arguments for R2Pipe::threads() are the same as for R2Pipe::spawn() but inside vectors
// Third and last argument is an option of a callback function
let pipes = match R2Pipe::threads(
vec!["/bin/ls", "/bin/id", "/bin/cat"],
vec![None, None, None],
None,
) {
Ok(p) => p,
Err(e) => {
println!("Error spawning Pipes: {}", e);
return Ok(());
}
};
// At this point we can iter through all of our r2pipes and send some commands
for p in pipes.iter() {
if let Ok(_) = p.send("ij".to_string()) {};
}
// do_other_stuff_here();
// Lets iter again and see what the pipes got
for p in pipes.iter() {
// this will block, do "p.recv(false)" for non-blocking receive inside a loop
if let Ok(msg) = p.recv(true) {
println!("Pipe #{} says: {}", p.id, msg);
}
}
// Finally properly close all pipes
// Note: For "join()" we need to borrow so pipes.iter() won't work for this
for p in pipes {
if let Ok(_) = p.send("q".to_string()) {};
p.handle.join().unwrap()?;
}
Ok(())
}
More examples
examples/threads_callback.rs (lines 14-18)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
fn main() -> Result<()> {
// First we define a callback. It doesn't block and gets called after a thread receives output from r2pipe
// Note: First argument to the callback is the thread id, second one the r2pipe output
let callback = Arc::new(|id, result| {
println!("Pipe #{} says: {}", id, result);
});
// 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
let pipes = match R2Pipe::threads(
vec!["/bin/ls", "/bin/id", "/bin/cat"],
vec![None, None, None],
Some(callback),
) {
Ok(p) => p,
Err(e) => {
println!("Error spawning Pipes: {}", e);
return Ok(());
}
};
// At this point we can iter through all of our r2pipes and send some commands
for p in pipes.iter() {
if let Ok(_) = p.send("ij".to_string()) {};
}
// Meanwhile: Expecting callbacks
std::thread::sleep(std::time::Duration::from_millis(1000));
// Finally properly close all pipes
// Note: For "join()" we need to borrow so pipes.iter() won't work for this
for p in pipes {
if let Ok(_) = p.send("q".to_string()) {};
p.handle.join().unwrap()?;
}
Ok(())
}