1
2
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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//! Provides functionality to connect with radare2.
//!
//! Please check crate level documentation for more details and example.

use crate::dlfcn;
use crate::{Error, Result};

use std::env;
use std::fs::File;
use std::io::prelude::*;
use std::io::BufReader;
use std::net::{SocketAddr, TcpStream, ToSocketAddrs};
use std::path::Path;
use std::process;
use std::process::Command;
use std::process::Stdio;
use std::str;
use std::sync::mpsc;
use std::sync::Arc;
use std::thread;

use serde_json::Value;

/// File descriptors to the parent r2 process.
pub struct R2PipeLang {
    read: BufReader<File>,
    write: File,
}

/// Stores descriptors to the spawned r2 process.
pub struct R2PipeSpawn {
    read: BufReader<process::ChildStdout>,
    write: process::ChildStdin,
    child: Option<process::Child>,
}

/// Stores the socket address of the r2 process.
pub struct R2PipeTcp {
    socket_addr: SocketAddr,
}

pub struct R2PipeHttp {
    host: String,
}

/// Stores thread metadata
/// It stores both a sending and receiving end to the thread, allowing convenient interaction
/// So we can send commands using R2PipeThread::send() and fetch outputs using R2PipeThread::recv()
pub struct R2PipeThread {
    r2recv: mpsc::Receiver<String>,
    r2send: mpsc::Sender<String>,
    pub id: u16,
    pub handle: thread::JoinHandle<Result<()>>,
}

#[derive(Default, Clone)]
pub struct R2PipeSpawnOptions {
    pub exepath: String,
    pub args: Vec<&'static str>,
}

/// Provides abstraction between the three invocation methods.
pub struct R2Pipe(Box<dyn Pipe>);
pub trait Pipe {
    fn cmd(&mut self, cmd: &str) -> Result<String>;
    fn cmdj(&mut self, cmd: &str) -> Result<Value> {
        let result = self.cmd(cmd)?;
        if result.is_empty() {
            return Err(Error::EmptyResponse);
        }
        Ok(serde_json::from_str(&result)?)
    }
    /// Escape the command before executing, valid only as of r2 v.5.8.0 "icebucket"
    fn call(&mut self, cmd: &str) -> Result<String> {
        self.cmd(&format!("\"\"{}", cmd))
    }
    /// Escape the command before executing and convert it to a json value,
    /// valid only as of r2 v.5.8.0 "icebucket"
    fn callj(&mut self, cmd: &str) -> Result<Value> {
        self.cmdj(&format!("\"\"{}", cmd))
    }
    fn close(&mut self) {}
}
fn getenv(k: &str) -> Option<i32> {
    match env::var(k) {
        Ok(val) => val.parse::<i32>().ok(),
        Err(_) => None,
    }
}

fn process_result(res: Vec<u8>) -> Result<String> {
    let len = res.len();
    if len == 0 {
        Err(Error::EmptyResponse)
    } else {
        Ok(str::from_utf8(&res[..len - 1])?.to_string())
    }
}

#[macro_export]
macro_rules! open_pipe {
    () => {
        R2Pipe::open(),
    };
        ($x: expr) => {
            match $x {
                Some(path) => R2Pipe::load_native(&path.clone()).or_else(|_| R2Pipe::spawn(path, None)),
                None => R2Pipe::open(),
            }
        };
        ($x: expr, $y: expr) => {
            match $x $y {
                Some(path, opts) => R2Pipe::spawn(path, opts),
                (None, None) => R2Pipe::open(),
            }
    }
}

impl R2Pipe {
    pub fn load_native<T: AsRef<str>>(path: T) -> Result<R2Pipe> {
        Ok(R2Pipe(Box::new(R2PipeNative::open(path.as_ref())?)))
    }
    #[cfg(not(windows))]
    pub fn open() -> Result<R2Pipe> {
        use std::os::unix::io::FromRawFd;

        let (f_in, f_out) = R2Pipe::in_session().ok_or(Error::NoSession)?;

        let res = unsafe {
            // dup file descriptors to avoid from_raw_fd ownership issue
            let (d_in, d_out) = (libc::dup(f_in), libc::dup(f_out));
            R2PipeLang {
                read: BufReader::new(File::from_raw_fd(d_in)),
                write: File::from_raw_fd(d_out),
            }
        };
        Ok(R2Pipe(Box::new(res)))
    }

    #[cfg(windows)]
    pub fn open() -> Result<R2Pipe> {
        unimplemented!()
    }
    pub fn cmd(&mut self, cmd: &str) -> Result<String> {
        self.0.cmd(cmd.trim())
    }

    pub fn cmdj(&mut self, cmd: &str) -> Result<Value> {
        self.0.cmdj(cmd.trim())
    }

    pub fn close(&mut self) {
        self.0.close();
    }
    /// Escape the command before executing, valid only as of r2 v.5.8.0 "icebucket"
    pub fn call(&mut self, cmd: &str) -> Result<String> {
        self.0.call(cmd)
    }
    /// Escape the command before executing and convert it to a json value,
    /// valid only as of r2 v.5.8.0 "icebucket"
    pub fn callj(&mut self, cmd: &str) -> Result<Value> {
        self.0.callj(cmd)
    }

    pub fn in_session() -> Option<(i32, i32)> {
        let f_in = getenv("R2PIPE_IN")?;
        let f_out = getenv("R2PIPE_OUT")?;
        Some((f_in, f_out))
    }

    #[cfg(windows)]
    pub fn in_windows_session() -> Option<String> {
        match env::var("R2PIPE_PATH") {
            Ok(val) => Some(format!("\\\\.\\pipe\\{}", val)),
            Err(_) => None,
        }
    }

    /// Creates a new R2PipeSpawn.
    pub fn spawn<T: AsRef<str>>(name: T, opts: Option<R2PipeSpawnOptions>) -> Result<R2Pipe> {
        if name.as_ref() == "" && R2Pipe::in_session().is_some() {
            return R2Pipe::open();
        }

        let exepath = match opts {
            Some(ref opt) => opt.exepath.clone(),
            _ => {
                if cfg!(windows) {
                    "radare2.exe"
                } else {
                    "r2"
                }
            }
            .to_owned(),
        };
        let args = match opts {
            Some(ref opt) => opt.args.clone(),
            _ => vec![],
        };
        let path = Path::new(name.as_ref());
        let mut child = Command::new(exepath)
            .arg("-q0")
            .args(&args)
            .arg(path)
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .spawn()?;

        // If stdin/stdout is not available, hard error
        let sin = child.stdin.take().unwrap();
        let mut sout = child.stdout.take().unwrap();

        // flush out the initial null byte.
        let mut w = [0; 1];
        sout.read_exact(&mut w)?;

        let res = R2PipeSpawn {
            read: BufReader::new(sout),
            write: sin,
            child: Some(child),
        };

        Ok(R2Pipe(Box::new(res)))
    }

    /// Creates a new R2PipeTcp
    pub fn tcp<A: ToSocketAddrs>(addr: A) -> Result<R2Pipe> {
        // use `connect` to figure out which socket address works
        let stream = TcpStream::connect(addr)?;
        let addr = stream.peer_addr()?;
        Ok(R2Pipe(Box::new(R2PipeTcp { socket_addr: addr })))
    }

    /// Creates a new R2PipeHttp
    pub fn http(host: &str) -> R2Pipe {
        R2Pipe(Box::new(R2PipeHttp {
            host: host.to_string(),
        }))
    }

    /// 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
    pub fn threads(
        names: Vec<&'static str>,
        opts: Vec<Option<R2PipeSpawnOptions>>,
        callback: Option<Arc<dyn Fn(u16, String) + Sync + Send>>,
    ) -> Result<Vec<R2PipeThread>> {
        if names.len() != opts.len() {
            return Err(Error::ArgumentMismatch);
        }

        let mut pipes = Vec::new();

        for n in 0..names.len() {
            let (htx, rx) = mpsc::channel();
            let (tx, hrx) = mpsc::channel();
            let name = names[n];
            let opt = opts[n].clone();
            let cb = callback.clone();
            let t = thread::spawn(move || -> Result<()> {
                let mut r2 = R2Pipe::spawn(name, opt)?;
                loop {
                    let cmd: String = hrx.recv()?;
                    if cmd == "q" {
                        break;
                    }
                    let res = r2.cmdj(&cmd)?.to_string();
                    htx.send(res.clone())?;
                    if let Some(cbs) = cb.clone() {
                        thread::spawn(move || {
                            cbs(n as u16, res);
                        });
                    };
                }
                Ok(())
            });
            pipes.push(R2PipeThread {
                r2recv: rx,
                r2send: tx,
                id: n as u16,
                handle: t,
            });
        }
        Ok(pipes)
    }
}

impl R2PipeThread {
    pub fn send(&self, cmd: String) -> Result<()> {
        Ok(self.r2send.send(cmd)?)
    }

    pub fn recv(&self, block: bool) -> Result<String> {
        if block {
            Ok(self.r2recv.recv()?)
        } else {
            Ok(self.r2recv.try_recv()?)
        }
    }
}

impl Pipe for R2PipeSpawn {
    fn cmd(&mut self, cmd: &str) -> Result<String> {
        let cmd = cmd.to_owned() + "\n";
        self.write.write_all(cmd.as_bytes())?;

        let mut res: Vec<u8> = Vec::new();
        self.read.read_until(0u8, &mut res)?;
        process_result(res)
    }

    fn close(&mut self) {
        let _ = self.cmd("q!");
        if let Some(child) = &mut self.child {
            let _ = child.wait();
        }
    }
}

impl R2PipeSpawn {
    /// Attempts to take the pipes underlying child process handle.
    /// On success the handle is returned.
    /// If `None` is returned the child handle was already taken previously.
    /// By using this method you take over the responsibility to `wait()` the child process in order to free all of it's resources.
    pub fn take_child(&mut self) -> Option<process::Child> {
        self.child.take()
    }
}

impl Pipe for R2PipeLang {
    fn cmd(&mut self, cmd: &str) -> Result<String> {
        self.write.write_all(cmd.as_bytes())?;
        let mut res: Vec<u8> = Vec::new();
        self.read.read_until(0u8, &mut res)?;
        process_result(res)
    }
}

impl Pipe for R2PipeHttp {
    fn cmd(&mut self, cmd: &str) -> Result<String> {
        let host = if self.host.starts_with("http://") {
            &self.host[7..]
        } else {
            &self.host
        };
        let mut stream = TcpStream::connect(host)?;
        let req = format!("GET /cmd/{} HTTP/1.1\r\n", cmd);
        let mut resp = Vec::with_capacity(1024);
        stream.write_all(req.as_bytes())?;
        stream.read_to_end(&mut resp)?;

        // index of the start of response body
        let index = resp
            .windows(4)
            .position(|w| w == "\r\n\r\n".as_bytes())
            .map(|i| i + 4)
            .unwrap_or(0);

        Ok(str::from_utf8(&resp[index..]).map(|s| s.to_string())?)
    }
}

impl Pipe for R2PipeTcp {
    fn cmd(&mut self, cmd: &str) -> Result<String> {
        let mut stream = TcpStream::connect(self.socket_addr)?;
        stream.write_all(cmd.as_bytes())?;
        let mut res: Vec<u8> = Vec::new();
        stream.read_to_end(&mut res)?;
        res.push(0);
        process_result(res)
    }
}

pub struct R2PipeNative {
    lib: dlfcn::LibHandle,
    r_core: std::sync::Mutex<*mut libc::c_void>,
    r_core_cmd_str_handle: fn(*mut libc::c_void, *const libc::c_char) -> *mut libc::c_char,
}

impl R2PipeNative {
    pub fn open(file: &str) -> Result<R2PipeNative> {
        let mut lib = dlfcn::LibHandle::new("libr_core", None)?;
        let r_core_new: fn() -> *mut libc::c_void = unsafe { lib.load_sym("r_core_new")? };
        let r_core_cmd_str_handle = unsafe { lib.load_sym("r_core_cmd_str")? };
        let r_core = r_core_new();
        if r_core.is_null() {
            Err(Error::SharedLibraryLoadError)
        } else {
            let mut ret = R2PipeNative {
                lib,
                r_core: std::sync::Mutex::new(r_core),
                r_core_cmd_str_handle,
            };
            ret.cmd(&format!("o {}", file))?;
            Ok(ret)
        }
    }
}

impl Pipe for R2PipeNative {
    fn cmd(&mut self, cmd: &str) -> Result<String> {
        let r_core = *self.r_core.lock().unwrap();
        let cmd = dlfcn::to_cstr(cmd)?;
        let res = (self.r_core_cmd_str_handle)(r_core, cmd);
        if res.is_null() {
            Err(Error::EmptyResponse)
        } else {
            Ok(unsafe { std::ffi::CStr::from_ptr(res).to_str()?.to_string() })
        }
    }
}

impl Drop for R2PipeNative {
    fn drop(&mut self) {
        let r_core = *self.r_core.lock().unwrap();
        if let Ok(r_core_free) =
            unsafe { self.lib.load_sym::<fn(*mut libc::c_void)>("r_core_free") }
        {
            r_core_free(r_core);
        }
    }
}

#[cfg(test)]
mod test {
    use super::Pipe;
    use super::R2PipeNative;
    use crate::R2Pipe;

    #[test]
    fn spawn_test() {
        let mut pipe = R2Pipe::spawn("/bin/ls", None).unwrap();
        assert_eq!(pipe.cmd("echo test").unwrap(), "test\n");
    }

    #[test]
    fn native_test() {
        let mut r2p = R2PipeNative::open("/bin/ls").unwrap();
        assert_eq!("a\n", r2p.cmd("echo a").unwrap());
    }
}