Initial port of io.winedbg using the new r_socket_spawn() api

This commit is contained in:
pancake 2017-09-27 03:12:00 +02:00
parent b4a1a595c9
commit 97911069b3
6 changed files with 50 additions and 9 deletions

View File

@ -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);

View File

@ -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];

View File

@ -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

View File

@ -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)

View File

@ -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) {

View File

@ -221,6 +221,7 @@ io.shm
io.w32
io.w32dbg
io.windbg
io.winedbg
io.zip
io.r2k
io.ar