Skip to content

Commit

Permalink
Implement reverse rap for system()
Browse files Browse the repository at this point in the history
  • Loading branch information
radare committed Jun 8, 2015
1 parent 62e9ea5 commit 03aa69b
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 11 deletions.
8 changes: 7 additions & 1 deletion libr/core/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ static int core_cmd_callback (void *user, const char *cmd) {
return r_core_cmd0 (core, cmd);
}

static char *core_cmdstr_callback (void *user, const char *cmd) {
RCore *core = (RCore *)user;
return r_core_cmd_str (core, cmd);
}

static ut64 getref (RCore *core, int n, char t, int type) {
RAnalFunction *fcn = r_anal_get_fcn_in (core->anal, core->offset, 0);
RListIter *iter;
Expand Down Expand Up @@ -884,7 +889,8 @@ R_API int r_core_init(RCore *core) {
core->io = r_io_new ();
core->io->ff = 1;
core->io->user = (void *)core;
core->io->core_cmd_cb = core_cmd_callback;
core->io->cb_core_cmd = core_cmd_callback;
core->io->cb_core_cmdstr = core_cmdstr_callback;
core->sign = r_sign_new ();
core->search = r_search_new (R_SEARCH_KEYWORD);
r_io_undo_enable (core->io, 1, 0); // TODO: configurable via eval
Expand Down
4 changes: 2 additions & 2 deletions libr/core/graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -874,8 +874,8 @@ R_API int r_core_visual_graph(RCore *core, RAnalFunction *_fcn) {
" TAB - select previous node\n"
" t/f - follow true/false edges\n"
" e - toggle edge-lines style (diagonal/square)\n"
" n - toggle mini-graph\n"
" O - toggle disasm mode\n"
" p - toggle mini-graph\n"
" u - select previous node\n"
" V - toggle basicblock / call graphs\n"
" x/X - jump to xref/ref\n"
Expand Down Expand Up @@ -957,7 +957,7 @@ R_API int r_core_visual_graph(RCore *core, RAnalFunction *_fcn) {
case 'e':
can->linemode = !!!can->linemode;
break;
case 'n':
case 'p':
graph_toggle_small_nodes(g);
break;
case 'u':
Expand Down
7 changes: 4 additions & 3 deletions libr/include/r_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,15 @@ typedef struct r_io_t {
RList *files;
RList *cache;
int zeromap;
//XXX: Need by rap
void *user;
int (*core_cmd_cb)(void *user, const char *str);
RCache *buffer;
int buffer_enabled;
int ff;
int autofd;
char *runprofile;
/* Core Callbacks (used by rap) */
void *user;
int (*cb_core_cmd)(void *user, const char *str);
char* (*cb_core_cmdstr)(void *user, const char *str);
} RIO;

typedef struct r_io_plugin_t {
Expand Down
7 changes: 5 additions & 2 deletions libr/io/io.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2008-2014 - pancake */
/* radare - LGPL - Copyright 2008-2015 - pancake */

#include "r_io.h"
#include "r_util.h"
Expand Down Expand Up @@ -931,14 +931,17 @@ static ut8 * r_io_desc_read (RIO *io, RIODesc * desc, ut64 *out_sz) {
off = io->off;

if (*out_sz == UT64_MAX) return buf;
if (*out_sz > 0xffffff) {
return buf;
}

buf = malloc (*out_sz);

if (desc->plugin && desc->plugin->read) {
if (!buf || !desc->plugin->read (io, desc, buf, *out_sz)) {
free (buf);
io->off = off;
return R_FALSE;
return NULL;
}
}
io->off = off;
Expand Down
45 changes: 42 additions & 3 deletions libr/io/p/io_rap.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2011-2014 - pancake */
/* radare - LGPL - Copyright 2011-2015 - pancake */

// TODO: implement the rap API in r_socket ?
#include "r_io.h"
Expand Down Expand Up @@ -269,9 +269,44 @@ static int rap__system(RIO *io, RIODesc *fd, const char *command) {
r_socket_write (s, buf, i+5);
r_socket_flush (s);

/* read reverse cmds */
for (;;) {
ret = r_socket_read_block (s, buf, 1);
if (ret != 1) {
return -1;
}
/* system back in the middle */
/* TODO: all pkt handlers should check for reverse queries */
if (buf[0] == RMT_SYSTEM) {
char *res, *str;
ut32 reslen = 0, cmdlen = 0;
// run io->cmdstr
// return back the string
buf[0] |= RMT_REPLY;
ret = r_socket_read_block (s, buf+1, 4);
r_mem_copyendian ((ut8*)&cmdlen, buf+1, 4, ENDIAN);
if (cmdlen+1==0) // check overflow
cmdlen = 0;
str = calloc (1, cmdlen+1);
ret = r_socket_read_block (s, (ut8*)str, cmdlen);
//eprintf ("RUN CMD(%s)\n", str);
res = io->cb_core_cmdstr (io->user, str);
reslen = strlen (res);
free (str);
r_mem_copyendian ((ut8*)buf+1, (const ut8*)&reslen,
sizeof(ut32), ENDIAN);
free (res);
memcpy (buf+5, res, reslen);
r_socket_write (s, buf, 5+reslen);
r_socket_flush (s);
} else {
break;
}
}

// read
ret = r_socket_read_block (s, buf, 5);
if (ret != 5)
ret = r_socket_read_block (s, buf+1, 4);
if (ret != 4)
return -1;
if (buf[0] != (op | RMT_REPLY)) {
eprintf ("Unexpected system reply\n");
Expand All @@ -280,6 +315,10 @@ static int rap__system(RIO *io, RIODesc *fd, const char *command) {

r_mem_copyendian ((ut8*)&i, buf+1, 4, ENDIAN);
ret = 0;
if (i>0xffff) {
eprintf ("Invalid length\n");
return -1;
}
ptr = (char *)malloc (i+1);
if (ptr) {
int ir;
Expand Down
Binary file added media/images/r2emoji.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/images/r2sticker.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 03aa69b

Please sign in to comment.