Improve rap:// code, r2p_ is now r2pipe_ ##io (#12699)

This commit is contained in:
radare 2019-01-09 02:47:35 +01:00 committed by GitHub
parent 25913f4745
commit 32e494a696
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 410 additions and 406 deletions

View File

@ -3200,7 +3200,7 @@ R_API int r_core_config_init(RCore *core) {
SETI ("magic.depth", 100, "Recursivity depth in magic description strings");
/* rap */
SETPREF ("rap.loop", "true", "Run rap as a forever-listening daemon");
SETPREF ("rap.loop", "true", "Run rap as a forever-listening daemon (=:9090)");
/* nkeys */
SETPREF ("key.s", "", "override step into action");

View File

@ -135,24 +135,25 @@ static const char *help_msg_dot[] = {
};
static const char *help_msg_equal[] = {
"Usage:", " =[:!+-=hH] [...]", " # radare remote command execution protocol",
"\nrap commands:", "", "",
"Usage:", " =[:!+-=ghH] [...]", " # connect with other instances of r2",
"\nremote commands:", "", "",
"=", "", "list all open connections",
"=<", "[fd] cmd", "send output of local command to remote fd",
"=<", "[fd] cmd", "send output of local command to remote fd", // XXX may not be a special char
"=", "[fd] cmd", "exec cmd at remote 'fd' (last open is default one)",
"=!", " cmd", "run command via r_io_system",
"=+", " [proto://]host", "add host (default=rap://, tcp://, udp://)",
"=+", " [proto://]host:port", "connect to remote host:port (*rap://, raps://, tcp://, udp://, http://)",
"=-", "[fd]", "remove all hosts or host 'fd'",
"==", "[fd]", "open remote session with host 'fd', 'q' to quit",
"=!=", "", "disable remote cmd mode",
"!=!", "", "enable remote cmd mode",
"\nrap server:","","",
"=", ":port", "listen on given port using rap protocol (o rap://9999)",
"\nrap server - radare binary protocol:","","",
"=", ":port", "start the rap server (o rap://9999)",
"=&", ":port", "start rap server in background",
"=", ":host:port cmd", "run 'cmd' command on remote server",
"\nother servers:","","",
"=h", "[?]", "listen for http connections",
"=g", "[?]", "using gdbserver",
"\nother:","","",
"=g", "[?]", "start the gdbserver",
"=h", "[?]", "start the http webserver",
"=H", "[?]", "start the http webserver (and laungh the web browser)",
NULL
};

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2009-2018 - pancake */
/* radare - LGPL - Copyright 2009-2019 - pancake */
#include <stddef.h>
#include <math.h> // required for signbit
@ -34,41 +34,41 @@ static const char *help_msg_percent[] = {
// command.. we should only expose the most essential and
// unidirectional ones.
static const char *help_msg_env[] = {
"\nEnvironment:", "", "",
"R2_FILE", "", "file name",
"R2_OFFSET", "", "10base offset 64bit value",
"R2_BYTES", "", "TODO: variable with bytes in curblock",
"R2_XOFFSET", "", "same as above, but in 16 base",
"R2_BSIZE", "", "block size",
"R2_ENDIAN", "", "'big' or 'little'",
"R2_IOVA", "", "is io.va true? virtual addressing (1,0)",
"R2_DEBUG", "", "debug mode enabled? (1,0)",
"R2_BLOCK", "", "TODO: dump current block to tmp file",
"R2_SIZE", "","file size",
"R2_ARCH", "", "value of asm.arch",
"R2_BITS", "", "arch reg size (8, 16, 32, 64)",
"RABIN2_LANG", "", "assume this lang to demangle",
"RABIN2_DEMANGLE", "", "demangle or not",
"RABIN2_PDBSERVER", "", "e pdb.server",
NULL
"\nEnvironment:", "", "",
"R2_FILE", "", "file name",
"R2_OFFSET", "", "10base offset 64bit value",
"R2_BYTES", "", "TODO: variable with bytes in curblock",
"R2_XOFFSET", "", "same as above, but in 16 base",
"R2_BSIZE", "", "block size",
"R2_ENDIAN", "", "'big' or 'little'",
"R2_IOVA", "", "is io.va true? virtual addressing (1,0)",
"R2_DEBUG", "", "debug mode enabled? (1,0)",
"R2_BLOCK", "", "TODO: dump current block to tmp file",
"R2_SIZE", "","file size",
"R2_ARCH", "", "value of asm.arch",
"R2_BITS", "", "arch reg size (8, 16, 32, 64)",
"RABIN2_LANG", "", "assume this lang to demangle",
"RABIN2_DEMANGLE", "", "demangle or not",
"RABIN2_PDBSERVER", "", "e pdb.server",
NULL
};
static const char *help_msg_exclamation[] = {
"Usage:", "!<cmd>", " Run given command as in system(3)",
"!", "", "list all historic commands",
"!", "ls", "execute 'ls' in shell",
"!!", "", "save command history to hist file",
"!!", "ls~txt", "print output of 'ls' and grep for 'txt'",
"!!!", "cmd [args|$type]", "adds the autocomplete value",
"!!!-", "cmd [args]", "removes the autocomplete value",
".!", "rabin2 -rpsei ${FILE}", "run each output line as a r2 cmd",
"!", "echo $SIZE", "display file size",
"!-", "", "clear history in current session",
"!-*", "", "clear and save empty history log",
"!=!", "", "enable remotecmd mode",
"=!=", "", "disable remotecmd mode",
NULL
};
"Usage:", "!<cmd>", " Run given command as in system(3)",
"!", "", "list all historic commands",
"!", "ls", "execute 'ls' in shell",
"!!", "", "save command history to hist file",
"!!", "ls~txt", "print output of 'ls' and grep for 'txt'",
"!!!", "cmd [args|$type]", "adds the autocomplete value",
"!!!-", "cmd [args]", "removes the autocomplete value",
".!", "rabin2 -rpsei ${FILE}", "run each output line as a r2 cmd",
"!", "echo $SIZE", "display file size",
"!-", "", "clear history in current session",
"!-*", "", "clear and save empty history log",
"!=!", "", "enable remotecmd mode",
"=!=", "", "disable remotecmd mode",
NULL
};
static const char *help_msg_root[] = {
"%var", "=value", "alias for 'env' command",
@ -76,7 +76,7 @@ static const char *help_msg_root[] = {
"(macro arg0 arg1)", "", "manage scripting macros",
".", "[?] [-|(m)|f|!sh|cmd]", "Define macro or load r2, cparse or rlang file",
"_", "[?]", "Print last output",
"=","[?] [cmd]", "send/listen for remote commands (rap://, http://, <fd>)",
"=","[?] [cmd]", "send/listen for remote commands (rap://, raps://, udp://, http://, <fd>)",
"<","[...]", "push escaped string into the RCons.readChar buffer",
"/","[?]", "search for bytes, regexps, patterns, ..",
"!","[?] [cmd]", "run given command as in system(3)",

View File

@ -1218,6 +1218,7 @@ static int cmd_open(void *data, const char *input) {
}
{
const char *argv0 = argv ? argv[0] : ptr;
void *bed = r_cons_sleep_begin ();
if ((file = r_core_file_open (core, argv0, perms, addr))) {
fd = file->fd;
if (!silence) {
@ -1228,6 +1229,7 @@ static int cmd_open(void *data, const char *input) {
eprintf ("cannot open file %s\n", argv0);
}
r_str_argv_free (argv);
r_cons_sleep_end (bed);
}
r_core_block_read (core);
return 0;

View File

@ -1371,7 +1371,7 @@ R_API void r_core_rtr_pushout(RCore *core, const char *input) {
}
if (!rtr_host[rtr_n].fd || !rtr_host[rtr_n].fd->fd) {
eprintf("Error: Unknown host\n");
eprintf ("Error: Unknown host\n");
return;
}
@ -1381,14 +1381,19 @@ R_API void r_core_rtr_pushout(RCore *core, const char *input) {
}
switch (rtr_host[rtr_n].proto) {
case RTR_PROT_RAP:
case RTR_PROTOCOL_RAP:
eprintf ("Error: Cannot use '=<' to a rap connection.\n");
break;
case RTR_PROT_TCP:
case RTR_PROT_UDP:
default:
case RTR_PROTOCOL_HTTP:
eprintf ("TODO\n");
break;
case RTR_PROTOCOL_TCP:
case RTR_PROTOCOL_UDP:
r_socket_write (rtr_host[rtr_n].fd, str, strlen (str));
break;
default:
eprintf ("Unknown protocol\n");
break;
}
free (str);
}
@ -1399,64 +1404,75 @@ R_API void r_core_rtr_list(RCore *core) {
if (!rtr_host[i].fd) {
continue;
}
r_cons_printf ("%i - ", rtr_host[i].fd->fd);
const char *proto = "rap";
switch (rtr_host[i].proto) {
case RTR_PROT_HTTP: r_cons_printf ( "http://"); break;
case RTR_PROT_TCP: r_cons_printf ("tcp://"); break;
case RTR_PROT_UDP: r_cons_printf ("udp://"); break;
default: r_cons_printf ("rap://"); break;
case RTR_PROTOCOL_HTTP: proto = "http"; break;
case RTR_PROTOCOL_TCP: proto = "tcp"; break;
case RTR_PROTOCOL_UDP: proto = "udp"; break;
}
r_cons_printf ("%s:%i/%s\n", rtr_host[i].host,
r_cons_printf ("%i - %s://%s:%i/%s\n",
rtr_host[i].fd->fd, proto, rtr_host[i].host,
rtr_host[i].port, rtr_host[i].file);
}
}
R_API void r_core_rtr_add(RCore *core, const char *_input) {
char *port, input[1024], *host = NULL, *file = NULL, *ptr = NULL, buf[1024];
int proto, i, timeout, ret;
char *port, input[1024], *file = NULL, *ptr = NULL, buf[1024];
int i, timeout, ret;
RSocket *fd;
timeout = r_config_get_i (core->config, "http.timeout");
strncpy (input, _input, sizeof (input) - 4);
input[sizeof(input)-4] = '\0';
/* Parse uri */
if ((ptr = strstr (input, "tcp://"))) {
proto = RTR_PROT_TCP;
host = ptr + 6;
} else if ((ptr = strstr(input, "http://"))) {
proto = RTR_PROT_HTTP;
host = ptr + 7;
} else if ((ptr = strstr(input, "udp://"))) {
proto = RTR_PROT_UDP;
host = ptr + 6;
} else if ((ptr = strstr(input, "rap://"))) {
proto = RTR_PROT_RAP;
host = ptr + 6;
} else {
proto = RTR_PROT_RAP;
host = input;
input[sizeof (input)-4] = '\0';
int proto = RTR_PROTOCOL_RAP;
char *host = (char *)r_str_trim_ro (input);
char *pikaboo = strstr (host, "://");
if (pikaboo) {
struct {
const char *name;
int protocol;
} uris[5] = {
{"tcp", RTR_PROTOCOL_TCP},
{"udp", RTR_PROTOCOL_UDP},
{"rap", RTR_PROTOCOL_RAP},
{"http", RTR_PROTOCOL_HTTP},
{NULL, 0}
};
char *s = r_str_ndup (input, pikaboo - input);
//int nlen = pikaboo - input;
for (i = 0; uris[i].name; i++) {
if (r_str_endswith (s, uris[i].name)) {
proto = uris[i].protocol;
host = pikaboo + 3;
break;
}
}
free (s);
}
while (*host && IS_WHITECHAR (*host)) {
host++;
if (host) {
if (!(ptr = strchr (host, ':'))) {
ptr = host;
port = "80";
} else {
*ptr++ = '\0';
port = ptr;
}
}
file = strchr (ptr, '/');
if (file) {
*file = 0;
file = (char *)r_str_trim_ro (file + 1);
} else {
if (*host == ':' || strstr (host, "://:")) { // listen
// it's fine to listen without serving a file
} else {
eprintf ("Error: Missing '/'\n");
return;
}
}
if (!(ptr = strchr (host, ':'))) {
ptr = host;
port = "80";
} else {
*ptr++ = '\0';
port = ptr;
}
if (!(file = strchr (ptr, '/'))) {
eprintf ("Error: Missing '/'\n");
return;
}
*file++ = 0;
port = r_str_trim (port);
while (*file == ' ') {
file++;
}
if (r_sandbox_enable (0)) {
eprintf ("sandbox: connect disabled\n");
return;
@ -1468,7 +1484,7 @@ R_API void r_core_rtr_add(RCore *core, const char *_input) {
return;
}
switch (proto) {
case RTR_PROT_HTTP:
case RTR_PROTOCOL_HTTP:
{
char prompt[64], prompt2[64], *str, *ptr;
int len, flen = strlen (file);
@ -1577,7 +1593,7 @@ R_API void r_core_rtr_add(RCore *core, const char *_input) {
return;
}
break;
case RTR_PROT_RAP:
case RTR_PROTOCOL_RAP:
if (!r_socket_connect_tcp (fd, host, port, timeout)) { //TODO: Use rap.ssl
eprintf ("Error: Cannot connect to '%s' (%s)\n", host, port);
r_socket_free (fd);
@ -1602,7 +1618,7 @@ R_API void r_core_rtr_add(RCore *core, const char *_input) {
}
eprintf ("ok\n");
break;
case RTR_PROT_TCP:
case RTR_PROTOCOL_TCP:
if (!r_socket_connect_tcp (fd, host, port, timeout)) { //TODO: Use rap.ssl
core->num->value = 1;
eprintf ("Error: Cannot connect to '%s' (%s)\n", host, port);
@ -1612,7 +1628,7 @@ R_API void r_core_rtr_add(RCore *core, const char *_input) {
core->num->value = 0;
eprintf ("Connected to: %s at port %s\n", host, port);
break;
case RTR_PROT_UDP:
case RTR_PROTOCOL_UDP:
if (!r_socket_connect_udp (fd, host, port, timeout)) { //TODO: Use rap.ssl
core->num->value = 1;
eprintf ("Error: Cannot connect to '%s' (%s)\n", host, port);
@ -1757,17 +1773,18 @@ R_API void r_core_rtr_cmd(RCore *core, const char *input) {
// "=:"
if (*input == ':' && !strchr (input + 1, ':')) {
void *bed = r_cons_sleep_begin ();
r_core_rtr_rap_run (core, input);
r_cons_sleep_end (bed);
return;
}
if (*input == '&') { // "=h&"
r_core_cmd0 (core, "& =h");
#if 0
if (*input == '&') { // "=h&" "=&:9090"
if (rapthread) {
eprintf ("RAP Thread is already running\n");
eprintf ("This is experimental and probably buggy. Use at your own risk\n");
} else {
// TODO: use tasks
RapThread *RT = R_NEW0 (RapThread);
if (RT) {
RT->core = core;
@ -1778,7 +1795,6 @@ R_API void r_core_rtr_cmd(RCore *core, const char *input) {
eprintf ("Background rap server started.\n");
}
}
#endif
return;
}
@ -1801,7 +1817,7 @@ R_API void r_core_rtr_cmd(RCore *core, const char *input) {
return;
}
if (rtr_host[rtr_n].proto != RTR_PROT_RAP) {
if (rtr_host[rtr_n].proto != RTR_PROTOCOL_RAP) {
eprintf ("Error: Not a rap:// host\n");
return;
}

View File

@ -1,34 +1,34 @@
/* radare - LGPL - Copyright 2011-2012 - pancake */
/* radare - LGPL - Copyright 2011-2019 - pancake */
#include <r_asm.h>
#include <r_cons.h>
#include <r_debug.h>
static int r_debug_rap_step(RDebug *dbg) {
static int __rap_step(RDebug *dbg) {
r_io_system (dbg->iob.io, "ds");
return true;
}
static int r_debug_rap_reg_read(RDebug *dbg, int type, ut8 *buf, int size) {
static int __rap_reg_read(RDebug *dbg, int type, ut8 *buf, int size) {
r_io_system (dbg->iob.io, "dr");
return 0;
}
static int r_debug_rap_reg_write(RDebug *dbg, int type, const ut8 *buf, int size) {
static int __rap_reg_write(RDebug *dbg, int type, const ut8 *buf, int size) {
return false; // XXX Error check
}
static int r_debug_rap_continue(RDebug *dbg, int pid, int tid, int sig) {
static int __rap_continue(RDebug *dbg, int pid, int tid, int sig) {
r_io_system (dbg->iob.io, "dc");
return true;
}
static int r_debug_rap_wait(RDebug *dbg, int pid) {
static int __rap_wait(RDebug *dbg, int pid) {
/* do nothing */
return true;
}
static int r_debug_rap_attach(RDebug *dbg, int pid) {
static int __rap_attach(RDebug *dbg, int pid) {
// XXX TODO PID must be a socket here !!1
RIODesc *d = dbg->iob.io->desc;
if (d && d->plugin && d->plugin->name) {
@ -41,14 +41,14 @@ static int r_debug_rap_attach(RDebug *dbg, int pid) {
return true;
}
static int r_debug_rap_detach(RDebug *dbg, int pid) {
static int __rap_detach(RDebug *dbg, int pid) {
// XXX TODO PID must be a socket here !!1
// close (pid);
//XXX Maybe we should continue here?
return true;
}
static char *r_debug_rap_reg_profile(RDebug *dbg) {
static char *__rap_reg_profile(RDebug *dbg) {
char *out, *tf = r_file_temp ("rap.XXXXXX");
int fd = r_cons_pipe_open (tf, 1, 0);
r_io_system (dbg->iob.io, "drp");
@ -59,7 +59,7 @@ static char *r_debug_rap_reg_profile(RDebug *dbg) {
return out;
}
static int r_debug_rap_breakpoint (RBreakpoint *bp, RBreakpointItem *b, bool set) {
static int __rap_breakpoint (RBreakpoint *bp, RBreakpointItem *b, bool set) {
//r_io_system (dbg->iob.io, "db");
return false;
}
@ -69,17 +69,17 @@ RDebugPlugin r_debug_plugin_rap = {
.license = "LGPL3",
.arch = "any",
.bits = R_SYS_BITS_32,
.step = r_debug_rap_step,
.cont = r_debug_rap_continue,
.attach = &r_debug_rap_attach,
.detach = &r_debug_rap_detach,
.wait = &r_debug_rap_wait,
.breakpoint = r_debug_rap_breakpoint,
.reg_read = &r_debug_rap_reg_read,
.reg_write = &r_debug_rap_reg_write,
.reg_profile = (void *)r_debug_rap_reg_profile,
//.bp_write = &r_debug_rap_bp_write,
//.bp_read = &r_debug_rap_bp_read,
.step = __rap_step,
.cont = __rap_continue,
.attach = &__rap_attach,
.detach = &__rap_detach,
.wait = &__rap_wait,
.breakpoint = __rap_breakpoint,
.reg_read = &__rap_reg_read,
.reg_write = &__rap_reg_write,
.reg_profile = (void *)__rap_reg_profile,
//.bp_write = &__rap_bp_write,
//.bp_read = &__rap_bp_read,
};
#ifndef CORELIB

View File

@ -7,10 +7,6 @@ LDFLAGS+=-lsocket
endif
OBJ_RAP=debug_rap.o
#librapwrap/rapwrapper.o
#librapwrap/rapwrapper.o:
# ${CC} -c ${CFLAGS} ${LDFLAGS} -o p/librapwrap/rapwrapper.o p/librapwrap/rapwrapper.c
STATIC_OBJ+=${OBJ_RAP}
TARGET_RAP=debug_rap.${EXT_SO}

View File

@ -57,10 +57,10 @@ R_LIB_VERSION_HEADER(r_core);
#define R_CONS_COLOR(x) R_CONS_COLOR_DEF (x, "")
/* rtr */
#define RTR_PROT_RAP 0
#define RTR_PROT_TCP 1
#define RTR_PROT_UDP 2
#define RTR_PROT_HTTP 3
#define RTR_PROTOCOL_RAP 0
#define RTR_PROTOCOL_TCP 1
#define RTR_PROTOCOL_UDP 2
#define RTR_PROTOCOL_HTTP 3
#define RTR_RAP_OPEN 0x01
#define RTR_RAP_CMD 0x07

View File

@ -1,4 +1,4 @@
/* radare2 - LGPL - Copyright 2017-2018 - condret, pancake, alvaro */
/* radare2 - LGPL - Copyright 2017-2019 - condret, pancake, alvaro */
#ifndef R2_IO_H
#define R2_IO_H

View File

@ -60,7 +60,7 @@ typedef struct r_socket_t {
#else
int fd;
#endif
int is_ssl;
bool is_ssl;
int local; // TODO: merge ssl with local -> flags/options
int port;
struct sockaddr_in sa;
@ -77,7 +77,7 @@ typedef struct r_socket_t {
#ifdef R_API
R_API RSocket *r_socket_new_from_fd(int fd);
R_API RSocket *r_socket_new(int is_ssl);
R_API RSocket *r_socket_new(bool is_ssl);
R_API bool r_socket_connect(RSocket *s, const char *host, const char *port, int proto, unsigned int timeout);
R_API bool r_socket_spawn (RSocket *s, const char *cmd, unsigned int timeout);
R_API int r_socket_connect_serial(RSocket *sock, const char *path, int speed, int parity);
@ -151,19 +151,20 @@ typedef char *(*rap_server_cmd)(void *user, const char *command);
typedef int (*rap_server_close)(void *user, int fd);
enum {
RAP_RMT_OPEN = 0x01,
RAP_RMT_READ,
RAP_RMT_WRITE,
RAP_RMT_SEEK,
RAP_RMT_CLOSE,
RAP_RMT_CMD = 0x07,
RAP_RMT_OPEN = 1,
RAP_RMT_READ = 2,
RAP_RMT_WRITE = 3,
RAP_RMT_SEEK = 4,
RAP_RMT_CLOSE = 5,
// system was deprecated in slot 6,
RAP_RMT_CMD = 7,
RAP_RMT_REPLY = 0x80,
RAP_RMT_MAX = 4096
};
typedef struct r_socket_rap_server_t {
RSocket *fd;
char port[5];
char *port;
ut8 buf[RAP_RMT_MAX + 32]; // This should be used as a static buffer for everything done by the server
rap_server_open open;
rap_server_seek seek;
@ -235,16 +236,16 @@ R_API int r_run_start(RRunProfile *p);
R_API void r_run_reset(RRunProfile *p);
R_API int r_run_parsefile(RRunProfile *p, const char *b);
/* r2pipe */
R_API R2Pipe *r2p_open(const char *cmd);
R_API R2Pipe *r2p_open_corebind(RCoreBind *coreb);
R_API int r2p_close(R2Pipe *r2p);
/* rapipe */
R_API R2Pipe *rap_open(const char *cmd);
R_API R2Pipe *rap_open_corebind(RCoreBind *coreb);
R_API int rap_close(R2Pipe *rap);
R_API char *r2p_cmd(R2Pipe *r2p, const char *str);
R_API char *r2p_cmdf(R2Pipe *r2p, const char *fmt, ...);
R_API char *rap_cmd(R2Pipe *rap, const char *str);
R_API char *rap_cmdf(R2Pipe *rap, const char *fmt, ...);
R_API int r2p_write(R2Pipe *r2p, const char *str);
R_API char *r2p_read(R2Pipe *r2p);
R_API int rap_write(R2Pipe *rap, const char *str);
R_API char *rap_read(R2Pipe *rap);
#endif
#ifdef __cplusplus

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2015-2016 - pancake */
/* radare - LGPL - Copyright 2015-2019 - pancake */
#include "r_io.h"
#include "r_lib.h"
@ -10,7 +10,7 @@
/* --------------------------------------------------------- */
#define R2P(x) ((R2Pipe*)(x)->data)
// TODO: add r2p_assert
// TODO: add r2pipe_assert
static int __write(RIO *io, RIODesc *fd, const ut8 *buf, int count) {
char fmt[4096];
@ -31,15 +31,15 @@ static int __write(RIO *io, RIODesc *fd, const ut8 *buf, int count) {
"{\"op\":\"write\",\"address\":%" PFMT64d ",\"data\":[%s]}",
io->off, bufnum);
if (len >= sizeof (fmt)) {
eprintf ("r2p_write: error, fmt string has been truncated\n");
eprintf ("r2pipe_write: error, fmt string has been truncated\n");
return -1;
}
rv = r2p_write (R2P (fd), fmt);
rv = r2pipe_write (R2P (fd), fmt);
if (rv < 1) {
eprintf ("r2p_write: error\n");
eprintf ("r2pipe_write: error\n");
return -1;
}
res = r2p_read (R2P (fd));
res = r2pipe_read (R2P (fd));
/* TODO: parse json back */
r = strstr (res, "result");
if (r) {
@ -63,12 +63,12 @@ static int __read(RIO *io, RIODesc *fd, ut8 *buf, int count) {
snprintf (fmt, sizeof (fmt),
"{\"op\":\"read\",\"address\":%"PFMT64d",\"count\":%d}",
io->off, count);
rv = r2p_write (R2P (fd), fmt);
rv = r2pipe_write (R2P (fd), fmt);
if (rv < 1) {
eprintf ("r2p_write: error\n");
eprintf ("r2pipe_write: error\n");
return -1;
}
res = r2p_read (R2P (fd));
res = r2pipe_read (R2P (fd));
/* TODO: parse json back */
r = strstr (res, "result");
@ -124,7 +124,7 @@ static int __close(RIODesc *fd) {
if (!fd || !fd->data) {
return -1;
}
r2p_close (fd->data);
r2pipe_close (fd->data);
fd->data = NULL;
return 0;
}
@ -145,7 +145,7 @@ static bool __check(RIO *io, const char *pathname, bool many) {
static RIODesc *__open(RIO *io, const char *pathname, int rw, int mode) {
R2Pipe *r2p = NULL;
if (__check (io, pathname, 0)) {
r2p = r2p_open (pathname + 9);
r2p = r2pipe_open (pathname + 9);
}
return r2p? r_io_desc_new (io, &r_io_plugin_r2pipe,
pathname, rw, mode, r2p): NULL;
@ -159,12 +159,12 @@ static char *__system(RIO *io, RIODesc *fd, const char *msg) {
return NULL;
}
snprintf (fmt, sizeof (fmt), "{\"op\":\"system\",\"cmd\":\"%s\"}", msg);
rv = r2p_write (R2P (fd), fmt);
rv = r2pipe_write (R2P (fd), fmt);
if (rv < 1) {
eprintf ("r2p_write: error\n");
eprintf ("r2pipe_write: error\n");
return NULL;
}
res = r2p_read (R2P (fd));
res = r2pipe_read (R2P (fd));
//eprintf ("%s\n", res);
/* TODO: parse json back */
r = strstr (res, "result");

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2011-2017 - pancake */
/* radare - MIT - Copyright 2011-2019 - pancake */
#include <r_io.h>
#include <r_lib.h>
@ -11,7 +11,7 @@
#define RIORAP_IS_LISTEN(x) (((RIORap*)((x)->data))->listener)
#define RIORAP_IS_VALID(x) ((x) && ((x)->data) && ((x)->plugin == &r_io_plugin_rap))
static int rap__write(RIO *io, RIODesc *fd, const ut8 *buf, int count) {
static int __rap_write(RIO *io, RIODesc *fd, const ut8 *buf, int count) {
RSocket *s = RIORAP_FD (fd);
ut8 *tmp;
int ret;
@ -24,7 +24,7 @@ static int rap__write(RIO *io, RIODesc *fd, const ut8 *buf, int count) {
count = RMT_MAX;
}
if (!(tmp = (ut8 *)malloc (count + 5))) {
eprintf ("rap__write: malloc failed\n");
eprintf ("__rap_write: malloc failed\n");
return -1;
}
tmp[0] = RMT_WRITE;
@ -34,7 +34,7 @@ static int rap__write(RIO *io, RIODesc *fd, const ut8 *buf, int count) {
(void)r_socket_write (s, tmp, count + 5);
r_socket_flush (s);
if (r_socket_read (s, tmp, 5) != 5) { // TODO read_block?
eprintf ("rap__write: error\n");
eprintf ("__rap_write: error\n");
ret = -1;
} else {
ret = r_read_be32 (tmp + 1);
@ -46,7 +46,7 @@ static int rap__write(RIO *io, RIODesc *fd, const ut8 *buf, int count) {
return ret;
}
static bool rap__accept(RIO *io, RIODesc *desc, int fd) {
static bool __rap_accept(RIO *io, RIODesc *desc, int fd) {
RIORap *rap = desc->data;
if (rap) {
rap->client = r_socket_new_from_fd (fd);
@ -55,7 +55,7 @@ static bool rap__accept(RIO *io, RIODesc *desc, int fd) {
return false;
}
static int rap__read(RIO *io, RIODesc *fd, ut8 *buf, int count) {
static int __rap_read(RIO *io, RIODesc *fd, ut8 *buf, int count) {
RSocket *s = RIORAP_FD (fd);
int ret, i = (int)count;
ut8 tmp[5];
@ -72,21 +72,21 @@ static int rap__read(RIO *io, RIODesc *fd, ut8 *buf, int count) {
// recv
ret = r_socket_read_block (s, tmp, 5);
if (ret != 5 || tmp[0] != (RMT_READ | RMT_REPLY)) {
eprintf ("rap__read: Unexpected rap read reply "
eprintf ("__rap_read: Unexpected rap read reply "
"(%d=0x%02x) expected (%d=0x%02x)\n",
ret, tmp[0], 2, (RMT_READ | RMT_REPLY));
return -1;
}
i = r_read_at_be32 (tmp, 1);
if (i >count) {
eprintf ("rap__read: Unexpected data size %d\n", i);
eprintf ("__rap_read: Unexpected data size %d\n", i);
return -1;
}
r_socket_read_block (s, buf, i);
return count;
}
static int rap__close(RIODesc *fd) {
static int __rap_close(RIODesc *fd) {
int ret = -1;
if (RIORAP_IS_VALID (fd)) {
if (RIORAP_FD (fd) != NULL) {
@ -96,12 +96,12 @@ static int rap__close(RIODesc *fd) {
R_FREE (fd->data);
}
} else {
eprintf ("rap__close: fdesc is not a r_io_rap plugin\n");
eprintf ("__rap_close: fdesc is not a r_io_rap plugin\n");
}
return ret;
}
static ut64 rap__lseek(RIO *io, RIODesc *fd, ut64 offset, int whence) {
static ut64 __rap_lseek(RIO *io, RIODesc *fd, ut64 offset, int whence) {
RSocket *s = RIORAP_FD (fd);
ut8 tmp[10];
int ret;
@ -124,12 +124,12 @@ static ut64 rap__lseek(RIO *io, RIODesc *fd, ut64 offset, int whence) {
return offset;
}
static bool rap__plugin_open(RIO *io, const char *pathname, bool many) {
static bool __rap_plugin_open(RIO *io, const char *pathname, bool many) {
return (!strncmp (pathname, "rap://", 6)) \
|| (!strncmp (pathname, "raps://", 7));
}
static RIODesc *rap__open(RIO *io, const char *pathname, int rw, int mode) {
static RIODesc *__rap_open(RIO *io, const char *pathname, int rw, int mode) {
int i, p, listenmode;
char *file, *port;
const char *ptr;
@ -137,7 +137,7 @@ static RIODesc *rap__open(RIO *io, const char *pathname, int rw, int mode) {
char buf[1024];
RIORap *rior;
if (!rap__plugin_open (io, pathname, 0)) {
if (!__rap_plugin_open (io, pathname, 0)) {
return NULL;
}
bool is_ssl = (!strncmp (pathname, "raps://", 7));
@ -230,6 +230,9 @@ static RIODesc *rap__open(RIO *io, const char *pathname, int rw, int mode) {
if (i > 0) {
eprintf ("ok\n");
}
io->cb_core_cmd (io->user, "e io.va=0");
io->cb_core_cmd (io->user, ".=!f*");
io->cb_core_cmd (io->user, ".=!om*");
#if 0
/* Read meta info */
r_socket_read (rap_fd, (ut8 *)&buf, 4);
@ -255,11 +258,11 @@ static RIODesc *rap__open(RIO *io, const char *pathname, int rw, int mode) {
pathname, rw, mode, rior);
}
static int rap__listener(RIODesc *fd) {
static int __rap_listener(RIODesc *fd) {
return (RIORAP_IS_VALID (fd))? RIORAP_IS_LISTEN (fd): 0; // -1 ?
}
static char *rap__system(RIO *io, RIODesc *fd, const char *command) {
static char *__rap_system(RIO *io, RIODesc *fd, const char *command) {
int ret, reslen = 0, cmdlen = 0;
RSocket *s = RIORAP_FD (fd);
unsigned int i;
@ -366,17 +369,17 @@ static char *rap__system(RIO *io, RIODesc *fd, const char *command) {
RIOPlugin r_io_plugin_rap = {
.name = "rap",
.desc = "radare network protocol (rap://:port rap://host:port/file)",
.license = "LGPL3",
.listener = rap__listener,
.open = rap__open,
.close = rap__close,
.read = rap__read,
.check = rap__plugin_open,
.lseek = rap__lseek,
.system = rap__system,
.write = rap__write,
.accept = rap__accept,
.desc = "remote binary protocol (rap://:port rap://host:port/file)",
.license = "MIT",
.listener = __rap_listener,
.open = __rap_open,
.close = __rap_close,
.read = __rap_read,
.check = __rap_plugin_open,
.lseek = __rap_lseek,
.system = __rap_system,
.write = __rap_write,
.accept = __rap_accept,
};
#ifndef CORELIB

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2011-2015 pancake */
/* radare - LGPL - Copyright 2011-2019 pancake */
/* vala extension for libr (radare2) */
// TODO: add cache directory (~/.r2/cache)
@ -24,7 +24,7 @@ static int lang_cpipe_file(RLang *lang, const char *file) {
if (a) {
*a = 0;
libpath = name;
libname = a+1;
libname = a + 1;
} else {
libpath = ".";
libname = name;
@ -59,23 +59,25 @@ static int lang_cpipe_init(void *user) {
static int lang_cpipe_run(RLang *lang, const char *code, int len) {
FILE *fd = fopen (".tmp.c", "w");
if (fd) {
fputs ("#include <r_socket.h>\n\n"
"#define R2P(x,y...) r2p_cmdf(r2p,x,##y)\n"
"int main() {\n"
"R2Pipe *r2p = r2p_open(NULL);", fd);
fputs (code, fd);
fputs ("\n}\n", fd);
fclose (fd);
lang_cpipe_file (lang, ".tmp.c");
r_file_rm (".tmp.c");
} else eprintf ("Cannot open .tmp.c\n");
eprintf ("Cannot open .tmp.c\n");
return false;
}
fputs ("#include <r_socket.h>\n\n"
"#define R2P(x,y...) r2pipe_cmdf(r2p,x,##y)\n"
"int main() {\n"
" R2Pipe *r2p = r2pipe_open(NULL);", fd);
fputs (code, fd);
fputs ("\n}\n", fd);
fclose (fd);
lang_cpipe_file (lang, ".tmp.c");
r_file_rm (".tmp.c");
return true;
}
static RLangPlugin r_lang_plugin_cpipe = {
.name = "cpipe",
.ext = "c2",
.desc = "C r2pipe scripting",
.desc = "r2pipe scripting in C",
.license = "LGPL",
.run = lang_cpipe_run,
.init = (void*)lang_cpipe_init,

View File

@ -7,11 +7,11 @@ Usage Example:
RCoreBind rcb;
RCore *core = r_core_new ();
r_core_bind (core, &rcb);
r2p_open_corebind (&rcb);
char *clippy = r2p_cmd ("?E hello");
r2pipe_open_corebind (&rcb);
char *clippy = r2pipe_cmd ("?E hello");
eprintf ("%s\n", clippy);
free (clippy);
r2p_close (r2p);
r2pipe_close (r2pipe);
r_core_free (core);
}
*/
@ -32,10 +32,10 @@ static void env(const char *s, int f) {
}
#endif
R_API int r2p_write(R2Pipe *r2p, const char *str) {
R_API int r2pipe_write(R2Pipe *r2pipe, const char *str) {
char *cmd;
int ret, len;
if (!r2p || !str) {
if (!r2pipe || !str) {
return -1;
}
len = strlen (str) + 2; /* include \n\x00 */
@ -47,20 +47,20 @@ R_API int r2p_write(R2Pipe *r2p, const char *str) {
strcpy (cmd + len - 2, "\n");
#if __WINDOWS__ && !defined(__CYGWIN__)
DWORD dwWritten = -1;
WriteFile (r2p->pipe, cmd, len, &dwWritten, NULL);
WriteFile (r2pipe->pipe, cmd, len, &dwWritten, NULL);
ret = (dwWritten == len);
#else
ret = (write (r2p->input[1], cmd, len) == len);
ret = (write (r2pipe->input[1], cmd, len) == len);
#endif
free (cmd);
return ret;
}
/* TODO: add timeout here ? */
R_API char *r2p_read(R2Pipe *r2p) {
R_API char *r2pipe_read(R2Pipe *r2pipe) {
int bufsz = 0;
char *buf = NULL;
if (!r2p) {
if (!r2pipe) {
return NULL;
}
bufsz = 4096;
@ -72,7 +72,7 @@ R_API char *r2p_read(R2Pipe *r2p) {
BOOL bSuccess = FALSE;
DWORD dwRead = 0;
// TODO: handle > 4096 buffers here
bSuccess = ReadFile (r2p->pipe, buf, bufsz, &dwRead, NULL);
bSuccess = ReadFile (r2pipe->pipe, buf, bufsz, &dwRead, NULL);
if (!bSuccess || !buf[0]) {
return NULL;
}
@ -84,7 +84,7 @@ R_API char *r2p_read(R2Pipe *r2p) {
char *newbuf;
int i, rv;
for (i = 0; i < bufsz; i++) {
rv = read (r2p->output[0], buf + i, 1);
rv = read (r2pipe->output[0], buf + i, 1);
if (i + 2 >= bufsz) {
bufsz += 4096;
newbuf = realloc (buf, bufsz);
@ -106,52 +106,52 @@ R_API char *r2p_read(R2Pipe *r2p) {
return buf;
}
R_API int r2p_close(R2Pipe *r2p) {
if (!r2p) {
R_API int r2pipe_close(R2Pipe *r2pipe) {
if (!r2pipe) {
return 0;
}
#if __WINDOWS__ && !defined(__CYGWIN__)
if (r2p->pipe) {
CloseHandle (r2p->pipe);
r2p->pipe = NULL;
if (r2pipe->pipe) {
CloseHandle (r2pipe->pipe);
r2pipe->pipe = NULL;
}
#else
if (r2p->input[0] != -1) {
close (r2p->input[0]);
r2p->input[0] = -1;
if (r2pipe->input[0] != -1) {
close (r2pipe->input[0]);
r2pipe->input[0] = -1;
}
if (r2p->input[1] != -1) {
close (r2p->input[1]);
r2p->input[1] = -1;
if (r2pipe->input[1] != -1) {
close (r2pipe->input[1]);
r2pipe->input[1] = -1;
}
if (r2p->output[0] != -1) {
close (r2p->output[0]);
r2p->output[0] = -1;
if (r2pipe->output[0] != -1) {
close (r2pipe->output[0]);
r2pipe->output[0] = -1;
}
if (r2p->output[1] != -1) {
close (r2p->output[1]);
r2p->output[1] = -1;
if (r2pipe->output[1] != -1) {
close (r2pipe->output[1]);
r2pipe->output[1] = -1;
}
if (r2p->child != -1) {
kill (r2p->child, SIGTERM);
waitpid (r2p->child, NULL, 0);
r2p->child = -1;
if (r2pipe->child != -1) {
kill (r2pipe->child, SIGTERM);
waitpid (r2pipe->child, NULL, 0);
r2pipe->child = -1;
}
#endif
free (r2p);
free (r2pipe);
return 0;
}
#if __WINDOWS__ && !defined(__CYGWIN__)
static int w32_createPipe(R2Pipe *r2p, const char *cmd) {
static int w32_createPipe(R2Pipe *r2pipe, const char *cmd) {
CHAR buf[1024];
r2p->pipe = CreateNamedPipe (TEXT ("\\\\.\\pipe\\R2PIPE_IN"),
r2pipe->pipe = CreateNamedPipe (TEXT ("\\\\.\\pipe\\R2PIPE_IN"),
PIPE_ACCESS_DUPLEX,PIPE_TYPE_MESSAGE | \
PIPE_READMODE_MESSAGE | \
PIPE_WAIT, PIPE_UNLIMITED_INSTANCES,
sizeof (buf), sizeof (buf), 0, NULL);
if (r_sys_create_child_proc_w32 (cmd, NULL)) {
if (ConnectNamedPipe (r2p->pipe, NULL)) {
if (ConnectNamedPipe (r2pipe->pipe, NULL)) {
return true;
}
}
@ -159,7 +159,7 @@ static int w32_createPipe(R2Pipe *r2p, const char *cmd) {
}
#endif
static R2Pipe* r2p_open_spawn(R2Pipe* r2p) {
static R2Pipe* r2pipe_open_spawn(R2Pipe* r2pipe) {
#if __UNIX__ || defined(__CYGWIN__)
char *out = r_sys_getenv ("R2PIPE_IN");
char *in = r_sys_getenv ("R2PIPE_OUT");
@ -168,132 +168,132 @@ static R2Pipe* r2p_open_spawn(R2Pipe* r2p) {
int i_in = atoi (in);
int i_out = atoi (out);
if (i_in >= 0 && i_out >= 0) {
r2p->input[0] = r2p->input[1] = i_in;
r2p->output[0] = r2p->output[1] = i_out;
r2pipe->input[0] = r2pipe->input[1] = i_in;
r2pipe->output[0] = r2pipe->output[1] = i_out;
done = true;
}
}
if (!done) {
eprintf ("Cannot find R2PIPE_IN or R2PIPE_OUT environment\n");
R_FREE (r2p);
R_FREE (r2pipe);
}
free (in);
free (out);
return r2p;
return r2pipe;
#else
eprintf ("r2p_open(NULL) not supported on windows\n");
eprintf ("r2pipe_open(NULL) not supported on windows\n");
return NULL;
#endif
}
static R2Pipe *r2p_new() {
R2Pipe *r2p = R_NEW0 (R2Pipe);
if (r2p) {
static R2Pipe *r2pipe_new() {
R2Pipe *r2pipe = R_NEW0 (R2Pipe);
if (r2pipe) {
#if __UNIX__ || defined(__CYGWIN__)
r2p->input[0] = r2p->input[1] = -1;
r2p->output[0] = r2p->output[1] = -1;
r2pipe->input[0] = r2pipe->input[1] = -1;
r2pipe->output[0] = r2pipe->output[1] = -1;
#endif
r2p->child = -1;
r2pipe->child = -1;
}
return r2p;
return r2pipe;
}
R_API R2Pipe *r2p_open_corebind(RCoreBind *coreb) {
R2Pipe *r2p = r2p_new ();
if (r2p) {
memcpy (&r2p->coreb, coreb, sizeof (RCoreBind));
R_API R2Pipe *r2pipe_open_corebind(RCoreBind *coreb) {
R2Pipe *r2pipe = r2pipe_new ();
if (r2pipe) {
memcpy (&r2pipe->coreb, coreb, sizeof (RCoreBind));
}
return r2p;
return r2pipe;
}
R_API R2Pipe *r2p_open(const char *cmd) {
R2Pipe *r2p = r2p_new ();
if (!r2p) {
R_API R2Pipe *r2pipe_open(const char *cmd) {
R2Pipe *r2pipe = r2pipe_new ();
if (!r2pipe) {
return NULL;
}
if (!cmd) {
r2p->child = -1;
return r2p_open_spawn (r2p);
r2pipe->child = -1;
return r2pipe_open_spawn (r2pipe);
}
#if __WINDOWS__ && !defined(__CYGWIN__)
w32_createPipe (r2p, cmd);
r2p->child = (int)(r2p->pipe);
w32_createPipe (r2pipe, cmd);
r2pipe->child = (int)(r2pipe->pipe);
#else
int r = pipe (r2p->input);
int r = pipe (r2pipe->input);
if (r != 0) {
eprintf ("pipe failed on input\n");
r2p_close (r2p);
r2pipe_close (r2pipe);
return NULL;
}
r = pipe (r2p->output);
r = pipe (r2pipe->output);
if (r != 0) {
eprintf ("pipe failed on output\n");
r2p_close (r2p);
r2pipe_close (r2pipe);
return NULL;
}
#if LIBC_HAVE_FORK
r2p->child = fork ();
r2pipe->child = fork ();
#else
r2p->child = -1;
r2pipe->child = -1;
#endif
if (r2p->child == -1) {
r2p_close (r2p);
if (r2pipe->child == -1) {
r2pipe_close (r2pipe);
return NULL;
}
env ("R2PIPE_IN", r2p->input[0]);
env ("R2PIPE_OUT", r2p->output[1]);
env ("R2PIPE_IN", r2pipe->input[0]);
env ("R2PIPE_OUT", r2pipe->output[1]);
if (r2p->child) {
if (r2pipe->child) {
char ch = 1;
// eprintf ("[+] r2pipe child is %d\n", r2p->child);
if (read (r2p->output[0], &ch, 1) != 1) {
// eprintf ("[+] r2pipeipe child is %d\n", r2pipe->child);
if (read (r2pipe->output[0], &ch, 1) != 1) {
eprintf ("Failed to read 1 byte\n");
r2p_close (r2p);
r2pipe_close (r2pipe);
return NULL;
}
if (ch) {
eprintf ("[+] r2pipe-io link failed. Expected two null bytes.\n");
r2p_close (r2p);
eprintf ("[+] r2pipeipe-io link failed. Expected two null bytes.\n");
r2pipe_close (r2pipe);
return NULL;
}
// Close parent's end of pipes
close (r2p->input[0]);
close (r2p->output[1]);
r2p->input[0] = -1;
r2p->output[1] = -1;
close (r2pipe->input[0]);
close (r2pipe->output[1]);
r2pipe->input[0] = -1;
r2pipe->output[1] = -1;
} else {
int rc = 0;
if (cmd && *cmd) {
close (0);
close (1);
dup2 (r2p->input[0], 0);
dup2 (r2p->output[1], 1);
close (r2p->input[1]);
close (r2p->output[0]);
r2p->input[1] = -1;
r2p->output[0] = -1;
dup2 (r2pipe->input[0], 0);
dup2 (r2pipe->output[1], 1);
close (r2pipe->input[1]);
close (r2pipe->output[0]);
r2pipe->input[1] = -1;
r2pipe->output[0] = -1;
rc = r_sandbox_system (cmd, 0);
}
r2p_close (r2p);
r2pipe_close (r2pipe);
exit (rc);
return NULL;
}
#endif
return r2p;
return r2pipe;
}
R_API char *r2p_cmd(R2Pipe *r2p, const char *str) {
if (r2p->coreb.core) {
return r2p->coreb.cmdstr (r2p->coreb.core, str);
R_API char *r2pipe_cmd(R2Pipe *r2pipe, const char *str) {
if (r2pipe->coreb.core) {
return r2pipe->coreb.cmdstr (r2pipe->coreb.core, str);
}
if (!r2p_write (r2p, str)) {
perror ("r2p_write");
if (!r2pipe_write (r2pipe, str)) {
perror ("r2pipe_write");
return NULL;
}
return r2p_read (r2p);
return r2pipe_read (r2pipe);
}
R_API char *r2p_cmdf(R2Pipe *r2p, const char *fmt, ...) {
R_API char *r2pipe_cmdf(R2Pipe *r2pipe, const char *fmt, ...) {
int ret, ret2;
char *p, string[1024];
va_list ap, ap2;
@ -314,10 +314,10 @@ R_API char *r2p_cmdf(R2Pipe *r2p, const char *fmt, ...) {
va_end (ap);
return NULL;
}
fmt = r2p_cmd (r2p, p);
fmt = r2pipe_cmd (r2pipe, p);
free (p);
} else {
fmt = r2p_cmd (r2p, string);
fmt = r2pipe_cmd (r2pipe, string);
}
va_end (ap2);
va_end (ap);

View File

@ -1,150 +1,135 @@
/* radare - LGPL - Copyright 2014-2016 - condret */
/* radare - LGPL - Copyright 2014-2019 - pancake, condret */
#include <r_socket.h>
#include <string.h>
#include <r_util.h>
R_API RSocketRapServer *r_socket_rap_server_new (int is_ssl, const char *port) {
RSocketRapServer *rap_s;
if (!port) {
return NULL;
R_API RSocketRapServer *r_socket_server_new (bool use_ssl, const char *port) {
r_return_val_if_fail (port, NULL);
RSocketRapServer *s = R_NEW0 (RSocketRapServer);
if (s) {
s->port = strdup (port);
s->fd = r_socket_new (use_ssl);
if (s->fd) {
return s;
}
r_socket_free (s->fd);
free (s);
}
rap_s = R_NEW0 (RSocketRapServer);
if (!rap_s) {
return NULL;
}
rap_s->fd = r_socket_new (is_ssl);
memcpy (rap_s->port, port, 4);
if (rap_s->fd) {
return rap_s;
}
free (rap_s);
return NULL;
}
R_API RSocketRapServer *r_socket_rap_server_create (const char *pathname) {
const char *port = NULL;
int is_ssl;
if (!pathname) {
return NULL;
}
R_API RSocketRapServer *r_socket_server_create (const char *pathname) {
r_return_val_if_fail (pathname, NULL);
if (strlen (pathname) < 11) {
return NULL;
}
if (strncmp (pathname, "rap", 3)) {
return NULL;
}
is_ssl = (pathname[3] == 's');
port = &pathname[7 + is_ssl];
return r_socket_rap_server_new (is_ssl, port);
bool is_ssl = (pathname[3] == 's');
const char *port = &pathname[7 + is_ssl];
return r_socket_server_new (is_ssl, port);
}
R_API void r_socket_rap_server_free (RSocketRapServer *rap_s) {
if (rap_s) {
r_socket_free (rap_s->fd);
R_API void r_socket_server_free (RSocketRapServer *s) {
if (s) {
r_socket_free (s->fd);
free (s);
}
free (rap_s);
}
R_API int r_socket_rap_server_listen (RSocketRapServer *rap_s, const char *certfile) {
if (!rap_s || !*rap_s->port) {
return false;
}
return r_socket_listen (rap_s->fd, rap_s->port, certfile);
R_API bool r_socket_server_listen (RSocketRapServer *s, const char *certfile) {
r_return_val_if_fail (s && s->port && *s->port, false);
return r_socket_listen (s->fd, s->port, certfile);
}
R_API RSocket* r_socket_rap_server_accept (RSocketRapServer *rap_s) {
if (!rap_s || !rap_s->fd) {
eprint ("error: r_socket_rap_server_accept");
return NULL;
}
return r_socket_accept (rap_s->fd);
R_API RSocket* r_socket_server_accept (RSocketRapServer *s) {
r_return_val_if_fail (s && s->fd, NULL);
return r_socket_accept (s->fd);
}
R_API bool r_socket_rap_server_continue (RSocketRapServer *rap_s) {
int i, whence, ret = true;
ut64 offset;
R_API bool r_socket_server_continue (RSocketRapServer *s) {
r_return_val_if_fail (s && s->fd, false);
int i;
char *ptr = NULL;
if (!rap_s || !rap_s->fd) {
if (!r_socket_is_connected (s->fd)) {
return false;
}
if (!r_socket_is_connected (rap_s->fd)) {
return false;
}
r_socket_read_block (rap_s->fd, rap_s->buf, 1);
ret = rap_s->buf[0];
switch (ret) {
r_socket_read_block (s->fd, s->buf, 1);
switch (s->buf[0]) {
case RAP_RMT_OPEN:
r_socket_read_block (rap_s->fd, &rap_s->buf[1], 2);
r_socket_read_block (rap_s->fd, &rap_s->buf[3], (int)rap_s->buf[2]);
rap_s->open (rap_s->user, (const char *)&rap_s->buf[3], (int)rap_s->buf[1], 0);
rap_s->buf[0] = RAP_RMT_OPEN | RAP_RMT_REPLY;
r_socket_write (rap_s->fd, rap_s->buf, 5);
r_socket_flush (rap_s->fd);
r_socket_read_block (s->fd, &s->buf[1], 2);
r_socket_read_block (s->fd, &s->buf[3], (int)s->buf[2]);
s->open (s->user, (const char *)&s->buf[3], (int)s->buf[1], 0);
s->buf[0] = RAP_RMT_OPEN | RAP_RMT_REPLY;
r_socket_write (s->fd, s->buf, 5);
r_socket_flush (s->fd);
break;
case RAP_RMT_READ:
r_socket_read_block (rap_s->fd, &rap_s->buf[1], 4);
i = r_read_be32 (&rap_s->buf[1]);
r_socket_read_block (s->fd, &s->buf[1], 4);
i = r_read_be32 (&s->buf[1]);
if (i > RAP_RMT_MAX || i < 0) {
i = RAP_RMT_MAX;
}
rap_s->read (rap_s->user, &rap_s->buf[5], i);
rap_s->buf[0] = RAP_RMT_READ | RAP_RMT_REPLY;
r_socket_write (rap_s->fd, rap_s->buf, i + 5);
r_socket_flush (rap_s->fd);
s->read (s->user, &s->buf[5], i);
s->buf[0] = RAP_RMT_READ | RAP_RMT_REPLY;
r_socket_write (s->fd, s->buf, i + 5);
r_socket_flush (s->fd);
break;
case RAP_RMT_WRITE:
r_socket_read_block (rap_s->fd, rap_s->buf + 1, 4);
i = r_read_be32 (rap_s->buf + 1);
r_socket_read_block (s->fd, s->buf + 1, 4);
i = r_read_be32 (s->buf + 1);
if (i > RAP_RMT_MAX || i < 0) {
i = RAP_RMT_MAX;
}
r_socket_read_block (rap_s->fd, rap_s->buf + 5, i);
int ret = rap_s->write (rap_s->user, rap_s->buf + 5, i);
r_write_be32 (rap_s->buf + 1, ret);
rap_s->buf[0] = RAP_RMT_WRITE | RAP_RMT_REPLY;
r_socket_write (rap_s->fd, rap_s->buf, 5);
r_socket_flush (rap_s->fd);
r_socket_read_block (s->fd, s->buf + 5, i);
r_write_be32 (s->buf + 1, s->write (s->user, s->buf + 5, i));
s->buf[0] = RAP_RMT_WRITE | RAP_RMT_REPLY;
r_socket_write (s->fd, s->buf, 5);
r_socket_flush (s->fd);
break;
case RAP_RMT_SEEK:
r_socket_read_block (rap_s->fd, &rap_s->buf[1], 9);
whence = rap_s->buf[1];
offset = r_read_be64 (rap_s->buf + 2);
offset = rap_s->seek (rap_s->user, offset, whence);
{
r_socket_read_block (s->fd, &s->buf[1], 9);
int whence = s->buf[1];
ut64 offset = r_read_be64 (s->buf + 2);
offset = s->seek (s->user, offset, whence);
/* prepare reply */
rap_s->buf[0] = RAP_RMT_SEEK | RAP_RMT_REPLY;
r_write_be64 (rap_s->buf + 1, offset);
r_socket_write (rap_s->fd, rap_s->buf, 9);
r_socket_flush (rap_s->fd);
s->buf[0] = RAP_RMT_SEEK | RAP_RMT_REPLY;
r_write_be64 (s->buf + 1, offset);
r_socket_write (s->fd, s->buf, 9);
r_socket_flush (s->fd);
}
break;
case RAP_RMT_CMD:
r_socket_read_block (rap_s->fd, &rap_s->buf[1], 4);
i = r_read_be32 (&rap_s->buf[1]);
r_socket_read_block (rap_s->fd, &rap_s->buf[5], i);
ptr = rap_s->cmd (rap_s->user, (const char *)&rap_s->buf[5]);
r_socket_read_block (s->fd, &s->buf[1], 4);
i = r_read_be32 (&s->buf[1]);
r_socket_read_block (s->fd, &s->buf[5], i);
ptr = s->cmd (s->user, (const char *)&s->buf[5]);
i = (ptr)? strlen (ptr) + 1: 0;
r_write_be32 (&rap_s->buf[1], i);
rap_s->buf[0] = RAP_RMT_CMD | RAP_RMT_REPLY;
r_socket_write (rap_s->fd, rap_s->buf, 5);
r_write_be32 (&s->buf[1], i);
s->buf[0] = RAP_RMT_CMD | RAP_RMT_REPLY;
r_socket_write (s->fd, s->buf, 5);
if (i) {
r_socket_write (rap_s->fd, ptr, i);
r_socket_write (s->fd, ptr, i);
}
r_socket_flush (rap_s->fd);
r_socket_flush (s->fd);
R_FREE (ptr);
break;
case RAP_RMT_CLOSE:
r_socket_read_block (rap_s->fd, &rap_s->buf[1], 4);
i = r_read_be32 (&rap_s->buf[1]);
rap_s->close (rap_s->user, i);
rap_s->buf[0] = RAP_RMT_CLOSE | RAP_RMT_REPLY;
r_socket_write (rap_s->fd, rap_s->buf, 5);
r_socket_flush (rap_s->fd);
r_socket_read_block (s->fd, &s->buf[1], 4);
i = r_read_be32 (&s->buf[1]);
s->close (s->user, i);
s->buf[0] = RAP_RMT_CLOSE | RAP_RMT_REPLY;
r_socket_write (s->fd, s->buf, 5);
r_socket_flush (s->fd);
break;
default:
eprintf ("unknown command 0x%02x\n", (ut8)(rap_s->buf[0] & 0xff));
r_socket_close (rap_s->fd);
ret = false;
break;
eprintf ("unknown command 0x%02x\n", (ut8)(s->buf[0] & 0xff));
r_socket_close (s->fd);
return false;
}
return ret;
return true;
}

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2006-2018 - pancake */
/* radare - LGPL - Copyright 2006-2019 - pancake */
/* must be included first because of winsock2.h and windows.h */
#include <r_socket.h>
@ -183,7 +183,7 @@ R_API int r_socket_unix_listen (RSocket *s, const char *file) {
}
#endif
R_API RSocket *r_socket_new (int is_ssl) {
R_API RSocket *r_socket_new (bool is_ssl) {
RSocket *s = R_NEW0 (RSocket);
if (!s) {
return NULL;
@ -280,11 +280,12 @@ R_API bool r_socket_connect (RSocket *s, const char *host, const char *port, int
}
s->fd = socket (AF_INET, SOCK_STREAM, 0);
#ifdef _MSC_VER
if (s->fd == INVALID_SOCKET)
if (s->fd == INVALID_SOCKET) {
#else
if (s->fd == -1)
if (s->fd == -1) {
#endif
return false;
}
unsigned long iMode = 1;
int iResult = ioctlsocket (s->fd, FIONBIO, &iMode);
@ -486,10 +487,7 @@ R_API int r_socket_free (RSocket *s) {
R_API int r_socket_port_by_name(const char *name) {
struct servent *p = getservbyname (name, "tcp");
if (p && p->s_port) {
return ntohs (p->s_port);
}
return r_num_get (NULL, name);
return (p && p->s_port) ? ntohs (p->s_port) : r_num_get (NULL, name);
}
R_API bool r_socket_listen (RSocket *s, const char *port, const char *certfile) {