From 5fcee5d712b52d01300b62ab700ac3027714e205 Mon Sep 17 00:00:00 2001 From: pancake Date: Wed, 19 Oct 2016 03:36:59 +0200 Subject: [PATCH] Implement wts to send data to socket --- libr/core/cmd_write.c | 56 +++++++++++++++++++++++++++++++++++++++++-- libr/io/io.c | 5 +++- 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/libr/core/cmd_write.c b/libr/core/cmd_write.c index e7dfe070e3..1b323922a8 100644 --- a/libr/core/cmd_write.c +++ b/libr/core/cmd_write.c @@ -425,7 +425,8 @@ static int cmd_write(void *data, const char *input) { "wp"," -|file","apply radare patch file. See wp? fmi", "wr"," 10","write 10 random bytes", "ws"," pstring","write 1 byte for length and then the string", - "wt"," file [sz]","write to file (from current seek, blocksize or sz bytes)", + "wt[f]"," file [sz]","write to file (from current seek, blocksize or sz bytes)", + "wts"," host:port", "send data to remote host:port via tcp://", "ww"," foobar","write wide string 'f\\x00o\\x00o\\x00b\\x00a\\x00r\\x00'", "wx","[?][fs] 9090","write two intel nops (from wxfile or wxseek)", "wv","[?] eip+34","write 32-64 bit value", @@ -920,13 +921,64 @@ static int cmd_write(void *data, const char *input) { r_core_block_read (core); break; case 't': // "wt" - if (*str == '?' || *str == '\0') { + if (*str == 's') { // "wts" + if (str[1] == ' ') { + eprintf ("Write to server\n"); + st64 sz = r_io_size (core->io); + if (sz > 0) { + ut64 addr = 0; + char *host = str + 2; + char *port = strchr (host, ':'); + if (port) { + *port ++= 0; + char *space = strchr (port, ' '); + if (space) { + *space++ = 0; + sz = r_num_math (core->num, space); + addr = core->offset; + } + ut8 *buf = calloc (1, sz); + if (space) { + (void)r_io_vread (core->io, addr, buf, sz); + } else { + (void)r_io_pread (core->io, addr, buf, sz); + } + RSocket *s = r_socket_new (false); + if (r_socket_connect (s, host, port, R_SOCKET_PROTO_TCP, 0)) { + int done = 0; + eprintf ("Transfering file to the end-point...\n"); + while (done < sz) { + int rc = r_socket_write (s, buf + done, sz - done); + if (rc <1) { + eprintf ("oops\n"); + break; + } + done += rc; + } + } else { + eprintf ("Cannot connect\n"); + } + r_socket_free (s); + free (buf); + } else { + eprintf ("Usage wts host:port [sz]\n"); + } + } else { + eprintf ("Unknown file size\n"); + } + } else { + eprintf ("Usage wts host:port [sz]\n"); + } + } else if (*str == '?' || *str == '\0') { eprintf ("Usage: wt[a] file [size] write 'size' bytes in current block to file\n"); free (ostr); return 0; } else { int append = 0; st64 sz = core->blocksize; + if (*str=='f') { // "wtf" + str++; + } else if (*str=='a') { // "wta" append = 1; str++; diff --git a/libr/io/io.c b/libr/io/io.c index 9309d2574d..ac7a38de7e 100644 --- a/libr/io/io.c +++ b/libr/io/io.c @@ -1002,9 +1002,12 @@ static ut8 *r_io_desc_read(RIO *io, RIODesc *desc, ut64 *out_sz) { ut8 *buf = NULL; ut64 off = 0; - if (!io || !desc || !out_sz) { + if (!io || !out_sz) { return NULL; } + if (!desc) { + desc = io->desc; + } if (*out_sz == UT64_MAX) { *out_sz = r_io_desc_size (io, desc); }