From 97911069b326f893e188fabbfa4f25376ec317ad Mon Sep 17 00:00:00 2001 From: pancake Date: Wed, 27 Sep 2017 03:12:00 +0200 Subject: [PATCH] Initial port of io.winedbg using the new r_socket_spawn() api --- binr/radare2/radare2.c | 4 ++-- libr/debug/p/debug_io.c | 6 +++++- libr/include/r_io.h | 1 + libr/include/r_socket.h | 1 + libr/socket/socket.c | 46 +++++++++++++++++++++++++++++++++++------ plugins.def.cfg | 1 + 6 files changed, 50 insertions(+), 9 deletions(-) diff --git a/binr/radare2/radare2.c b/binr/radare2/radare2.c index 0c3a2e5e3f..9dab348ace 100644 --- a/binr/radare2/radare2.c +++ b/binr/radare2/radare2.c @@ -813,13 +813,13 @@ int main(int argc, char **argv, char **envp) { ut8 *buf = (ut8 *)r_stdin_slurp (&sz); close (0); if (buf && sz > 0) { - char path[1024]; + char path[128]; snprintf (path, sizeof (path) - 1, "malloc://%d", sz); fh = r_core_file_open (&r, path, perms, mapaddr); if (!fh) { r_cons_flush (); free (buf); - eprintf ("Cannot open %s\n", path); + eprintf ("Cannot open '%s'\n", path); return 1; } r_io_write_at (r.io, 0, buf, sz); diff --git a/libr/debug/p/debug_io.c b/libr/debug/p/debug_io.c index c9411e5590..9239f51ee0 100644 --- a/libr/debug/p/debug_io.c +++ b/libr/debug/p/debug_io.c @@ -19,7 +19,11 @@ static int __io_step_over(RDebug *dbg) { static RList *__io_maps(RDebug *dbg) { RList *list = r_list_new (); dbg->iob.system (dbg->iob.io, "dm"); - char *ostr, *str = strdup (r_cons_get_buffer ()); + const char *consdata = r_cons_get_buffer (); + if (!consdata) { + return NULL; + } + char *ostr, *str = strdup (consdata); ut64 map_start, map_end; char perm[32]; char name[512]; diff --git a/libr/include/r_io.h b/libr/include/r_io.h index 2b17e4bba8..2289cf2383 100644 --- a/libr/include/r_io.h +++ b/libr/include/r_io.h @@ -520,5 +520,6 @@ extern RIOPlugin r_io_plugin_bochs; extern RIOPlugin r_io_plugin_null; extern RIOPlugin r_io_plugin_ar; extern RIOPlugin r_io_plugin_rbuf; +extern RIOPlugin r_io_plugin_winedbg; #endif diff --git a/libr/include/r_socket.h b/libr/include/r_socket.h index bcf2f64e46..6da1aa60ce 100644 --- a/libr/include/r_socket.h +++ b/libr/include/r_socket.h @@ -78,6 +78,7 @@ typedef struct r_socket_t { R_API RSocket *r_socket_new_from_fd(int fd); R_API RSocket *r_socket_new(int is_ssl); R_API bool r_socket_connect(RSocket *s, const char *host, const char *port, int proto, unsigned int timeout); +R_API int 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); #define r_socket_connect_tcp(a, b, c, d) r_socket_connect (a, b, c, R_SOCKET_PROTO_TCP, d) #define r_socket_connect_udp(a, b, c, d) r_socket_connect (a, b, c, R_SOCKET_PROTO_UDP, d) diff --git a/libr/socket/socket.c b/libr/socket/socket.c index a0fa3f125a..74b5b31e94 100644 --- a/libr/socket/socket.c +++ b/libr/socket/socket.c @@ -43,6 +43,9 @@ R_API int r_socket_unix_listen (RSocket *s, const char *file) { R_API bool r_socket_connect (RSocket *s, const char *host, const char *port, int proto, unsigned int timeout) { return false; } +R_API int r_socket_spawn (RSocket *s, const char *cmd, unsigned int timeout) { + return -1; +} R_API int r_socket_close_fd (RSocket *s) { return -1; } @@ -211,6 +214,35 @@ R_API RSocket *r_socket_new (int is_ssl) { return s; } +R_API int r_socket_spawn (RSocket *s, const char *cmd, unsigned int timeout) { + // XXX TODO: dont use sockets, we can achieve the same with pipes + const int port = 2000 + r_num_rand (2000); + int childPid = r_sys_fork(); + if (childPid == 0) { + char *a = r_str_replace (strdup (cmd), "\\", "\\\\", true); + r_sys_cmdf ("rarun2 system=\"%s\" listen=%d", a, port); + free (a); +#if 0 + // TODO: use the api + char *profile = r_str_newf ( + "system=%s\n" + "listen=%d\n", cmd, port); + RRunProfile *rp = r_run_new (profile); + r_run_start (rp); + r_run_free (rp); + free (profile); +#endif + eprintf ("r_socket_spawn: %s is dead\n", cmd); + exit (0); + } + sleep (1); + r_sys_usleep (timeout); + char aport[32]; + sprintf (aport, "%d", port); + // redirect stdin/stdout/stderr + return r_socket_connect (s, "127.0.0.1", aport, R_SOCKET_PROTO_TCP, 2000); +} + R_API bool r_socket_connect (RSocket *s, const char *host, const char *port, int proto, unsigned int timeout) { #if __WINDOWS__ && !defined(__CYGWIN__) //&& !defined(__MINGW64__) struct sockaddr_in sa; @@ -620,8 +652,9 @@ R_API int r_socket_ready(RSocket *s, int secs, int usecs) { FD_SET (s->fd, &rfds); tv.tv_sec = secs; tv.tv_usec = usecs; - if (select (s->fd+1, &rfds, NULL, NULL, &tv) == -1) + if (select (s->fd + 1, &rfds, NULL, NULL, &tv) == -1) { return -1; + } return FD_ISSET (0, &rfds); #endif #else @@ -675,15 +708,16 @@ R_API int r_socket_write(RSocket *s, void *buf, int len) { ret = send (s->fd, (char *)buf+delta, b, 0); } //if (ret == 0) return -1; - if (ret<1) break; - if (ret == len) + if (ret < 1) { + break; + } + if (ret == len) { return len; + } delta += ret; len -= ret; } - if (ret == -1) - return -1; - return delta; + return (ret == -1)? -1 : delta; } R_API int r_socket_puts(RSocket *s, char *buf) { diff --git a/plugins.def.cfg b/plugins.def.cfg index 6c853ba763..fe60914238 100644 --- a/plugins.def.cfg +++ b/plugins.def.cfg @@ -221,6 +221,7 @@ io.shm io.w32 io.w32dbg io.windbg +io.winedbg io.zip io.r2k io.ar