2019-05-16 12:18:16 +00:00
|
|
|
/* radare - Copyright 2009-2019 - pancake, nibble */
|
2010-08-24 09:58:09 +00:00
|
|
|
|
|
|
|
#include "r_core.h"
|
|
|
|
#include "r_socket.h"
|
2017-05-22 17:37:48 +00:00
|
|
|
#include "gdb/include/libgdbr.h"
|
|
|
|
#include "gdb/include/gdbserver/core.h"
|
2010-08-24 09:58:09 +00:00
|
|
|
|
2018-09-17 10:07:00 +00:00
|
|
|
#if HAVE_LIBUV
|
|
|
|
#include <uv.h>
|
|
|
|
#endif
|
|
|
|
|
2016-05-04 21:32:29 +00:00
|
|
|
#if 0
|
|
|
|
SECURITY IMPLICATIONS
|
|
|
|
=====================
|
|
|
|
- no ssl
|
|
|
|
- no auth
|
|
|
|
- commands can be executed by anyone
|
|
|
|
- default is to listen on localhost
|
|
|
|
- can access full filesystem
|
|
|
|
- follow symlinks
|
|
|
|
#endif
|
|
|
|
|
2010-08-24 09:58:09 +00:00
|
|
|
#define rtr_n core->rtr_n
|
|
|
|
#define rtr_host core->rtr_host
|
|
|
|
|
2012-09-06 01:12:54 +00:00
|
|
|
static RSocket *s = NULL;
|
2016-05-04 21:32:29 +00:00
|
|
|
static RThread *httpthread = NULL;
|
2016-07-29 17:32:27 +00:00
|
|
|
static RThread *rapthread = NULL;
|
2012-11-05 01:00:34 +00:00
|
|
|
static const char *listenport = NULL;
|
2012-09-06 01:12:54 +00:00
|
|
|
|
2014-08-28 02:41:33 +00:00
|
|
|
typedef struct {
|
|
|
|
const char *host;
|
|
|
|
const char *port;
|
|
|
|
const char *file;
|
|
|
|
} TextLog;
|
2016-05-04 21:32:29 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
RCore *core;
|
|
|
|
int launch;
|
2018-07-08 22:19:33 +00:00
|
|
|
int browse;
|
2016-06-08 12:46:24 +00:00
|
|
|
char *path;
|
2016-05-04 21:32:29 +00:00
|
|
|
} HttpThread;
|
2014-08-28 02:41:33 +00:00
|
|
|
|
2016-07-29 17:32:27 +00:00
|
|
|
typedef struct {
|
|
|
|
RCore *core;
|
2017-12-18 12:27:26 +00:00
|
|
|
char* input;
|
2016-07-29 17:32:27 +00:00
|
|
|
} RapThread;
|
|
|
|
|
2017-12-18 12:27:26 +00:00
|
|
|
R_API void r_core_wait(RCore *core) {
|
2018-06-23 10:10:13 +00:00
|
|
|
r_cons_singleton ()->context->breaked = true;
|
2017-12-18 12:27:26 +00:00
|
|
|
r_th_kill (httpthread, true);
|
|
|
|
r_th_kill (rapthread, true);
|
|
|
|
r_th_wait (httpthread);
|
|
|
|
r_th_wait (rapthread);
|
|
|
|
}
|
|
|
|
|
2016-10-15 21:42:12 +00:00
|
|
|
static void http_logf(RCore *core, const char *fmt, ...) {
|
|
|
|
bool http_log_enabled = r_config_get_i (core->config, "http.log");
|
|
|
|
va_list ap;
|
|
|
|
va_start (ap, fmt);
|
|
|
|
if (http_log_enabled) {
|
|
|
|
const char *http_log_file = r_config_get (core->config, "http.logfile");
|
|
|
|
if (http_log_file && *http_log_file) {
|
|
|
|
char * msg = calloc (4096, 1);
|
2018-08-16 02:29:15 +00:00
|
|
|
if (msg) {
|
|
|
|
vsnprintf (msg, 4095, fmt, ap);
|
|
|
|
r_file_dump (http_log_file, (const ut8*)msg, -1, true);
|
|
|
|
free (msg);
|
|
|
|
}
|
2016-10-15 21:42:12 +00:00
|
|
|
} else {
|
|
|
|
vfprintf (stderr, fmt, ap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
va_end (ap);
|
|
|
|
}
|
|
|
|
|
2014-08-28 02:41:33 +00:00
|
|
|
static char *rtrcmd (TextLog T, const char *str) {
|
2016-05-04 21:32:29 +00:00
|
|
|
char *res, *ptr2;
|
|
|
|
char *ptr = r_str_uri_encode (str);
|
|
|
|
char *uri = r_str_newf ("http://%s:%s/%s%s", T.host, T.port, T.file, ptr? ptr: str);
|
2014-08-28 02:41:33 +00:00
|
|
|
int len;
|
2016-05-04 21:32:29 +00:00
|
|
|
free (ptr);
|
2014-08-28 02:41:33 +00:00
|
|
|
ptr2 = r_socket_http_get (uri, NULL, &len);
|
2016-05-04 21:32:29 +00:00
|
|
|
free (uri);
|
2014-08-28 02:41:33 +00:00
|
|
|
if (ptr2) {
|
|
|
|
ptr2[len] = 0;
|
|
|
|
res = strstr (ptr2, "\n\n");
|
2016-10-15 21:42:12 +00:00
|
|
|
if (res) {
|
|
|
|
res = strstr (res + 1, "\n\n");
|
|
|
|
}
|
2016-05-04 21:32:29 +00:00
|
|
|
return res? res + 2: ptr2;
|
2014-08-28 02:41:33 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-09-02 00:20:03 +00:00
|
|
|
static void showcursor(RCore *core, int x) {
|
|
|
|
if (core && core->vmode) {
|
|
|
|
r_cons_show_cursor (x);
|
2016-05-04 21:32:29 +00:00
|
|
|
r_cons_enable_mouse (x? r_config_get_i (core->config, "scr.wheel"): false);
|
2016-10-15 21:42:12 +00:00
|
|
|
} else {
|
|
|
|
r_cons_enable_mouse (false);
|
|
|
|
}
|
2014-09-02 00:20:03 +00:00
|
|
|
r_cons_flush ();
|
|
|
|
}
|
|
|
|
|
2016-05-04 21:32:29 +00:00
|
|
|
// TODO: rename /name to /nick or /so?
|
|
|
|
// clone of textlog_chat () using rtrcmd()
|
|
|
|
static void rtr_textlog_chat (RCore *core, TextLog T) {
|
|
|
|
char prompt[64];
|
|
|
|
char buf[1024];
|
|
|
|
int lastmsg = 0;
|
|
|
|
const char *me = r_config_get (core->config, "cfg.user");
|
|
|
|
char *ret, msg[1024];
|
|
|
|
|
|
|
|
eprintf ("Type '/help' for commands and ^D to quit:\n");
|
|
|
|
char *oldprompt = strdup (r_line_singleton ()->prompt);
|
|
|
|
snprintf (prompt, sizeof (prompt) - 1, "[%s]> ", me);
|
|
|
|
r_line_set_prompt (prompt);
|
|
|
|
ret = rtrcmd (T, msg);
|
|
|
|
for (;;) {
|
|
|
|
if (lastmsg >= 0) {
|
|
|
|
snprintf (msg, sizeof (msg) - 1, "T %d", lastmsg);
|
|
|
|
} else {
|
|
|
|
strcpy (msg, "T");
|
|
|
|
}
|
|
|
|
ret = rtrcmd (T, msg);
|
2016-06-26 04:51:17 +00:00
|
|
|
r_cons_println (ret);
|
2016-05-04 21:32:29 +00:00
|
|
|
free (ret);
|
|
|
|
ret = rtrcmd (T, "Tl");
|
|
|
|
lastmsg = atoi (ret)-1;
|
|
|
|
free (ret);
|
|
|
|
if (r_cons_fgets (buf, sizeof (buf) - 1, 0, NULL) < 0) {
|
|
|
|
goto beach;
|
|
|
|
}
|
2016-10-15 21:42:12 +00:00
|
|
|
if (!*buf) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-05-04 21:32:29 +00:00
|
|
|
if (!strcmp (buf, "/help")) {
|
|
|
|
eprintf ("/quit quit the chat (same as ^D)\n");
|
|
|
|
eprintf ("/nick <nick> set cfg.user nick name\n");
|
|
|
|
eprintf ("/log show full log\n");
|
|
|
|
eprintf ("/clear clear text log messages\n");
|
|
|
|
} else if (!strncmp (buf, "/nick ", 6)) {
|
|
|
|
snprintf (msg, sizeof (msg) - 1, "* '%s' is now known as '%s'", me, buf+6);
|
2016-06-26 04:51:17 +00:00
|
|
|
r_cons_println (msg);
|
2016-05-04 21:32:29 +00:00
|
|
|
r_core_log_add (core, msg);
|
|
|
|
r_config_set (core->config, "cfg.user", buf+6);
|
|
|
|
me = r_config_get (core->config, "cfg.user");
|
|
|
|
snprintf (prompt, sizeof (prompt) - 1, "[%s]> ", me);
|
|
|
|
r_line_set_prompt (prompt);
|
|
|
|
} else if (!strcmp (buf, "/log")) {
|
|
|
|
char *ret = rtrcmd (T, "T");
|
|
|
|
if (ret) {
|
2016-06-26 04:51:17 +00:00
|
|
|
r_cons_println (ret);
|
2016-05-04 21:32:29 +00:00
|
|
|
free (ret);
|
|
|
|
}
|
|
|
|
} else if (!strcmp (buf, "/clear")) {
|
|
|
|
//r_core_log_del (core, 0);
|
|
|
|
free (rtrcmd (T, "T-"));
|
|
|
|
} else if (!strcmp (buf, "/quit")) {
|
|
|
|
goto beach;
|
|
|
|
} else if (*buf=='/') {
|
|
|
|
eprintf ("Unknown command: %s\n", buf);
|
|
|
|
} else {
|
2018-08-31 15:56:37 +00:00
|
|
|
char *cmd = r_str_newf ("T [%s] %s", me, buf);
|
|
|
|
free (rtrcmd (T, cmd));
|
|
|
|
free (cmd);
|
2016-05-04 21:32:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
beach:
|
|
|
|
r_line_set_prompt (oldprompt);
|
|
|
|
free (oldprompt);
|
|
|
|
}
|
|
|
|
|
2016-11-20 18:20:14 +00:00
|
|
|
static bool rtr_visual(RCore *core, TextLog T, const char *cmd) {
|
2016-05-04 21:32:29 +00:00
|
|
|
bool autorefresh = false;
|
2014-08-28 09:38:11 +00:00
|
|
|
if (cmd) {
|
2016-11-20 18:20:14 +00:00
|
|
|
r_cons_break_push (NULL, NULL);
|
2014-08-28 09:38:11 +00:00
|
|
|
for (;;) {
|
2014-10-06 23:58:42 +00:00
|
|
|
char *ret;
|
2014-08-28 09:38:11 +00:00
|
|
|
r_cons_clear00 ();
|
2014-10-06 23:58:42 +00:00
|
|
|
ret = rtrcmd (T, cmd);
|
2016-06-26 04:51:17 +00:00
|
|
|
r_cons_println (ret);
|
2014-10-06 23:58:42 +00:00
|
|
|
free (ret);
|
2014-08-28 09:38:11 +00:00
|
|
|
r_cons_flush ();
|
2016-11-20 18:20:14 +00:00
|
|
|
if (r_cons_is_breaked ()) {
|
2014-08-28 09:38:11 +00:00
|
|
|
break;
|
2016-11-20 18:20:14 +00:00
|
|
|
}
|
2014-08-28 09:38:11 +00:00
|
|
|
r_sys_sleep (1);
|
|
|
|
}
|
2016-11-20 18:20:14 +00:00
|
|
|
r_cons_break_pop ();
|
2014-08-28 09:38:11 +00:00
|
|
|
} else {
|
2015-11-23 15:31:31 +00:00
|
|
|
const char *cmds[] = { "px", "pd", "pxa", "dr", "sr SP;pxa", NULL };
|
2014-08-28 09:38:11 +00:00
|
|
|
int cmdidx = 0;
|
|
|
|
char *ret, ch;
|
2014-10-06 22:56:39 +00:00
|
|
|
free (rtrcmd (T, "e scr.color=true"));
|
|
|
|
free (rtrcmd (T, "e scr.html=false"));
|
2014-08-28 09:38:11 +00:00
|
|
|
for (;;) {
|
|
|
|
r_cons_clear00 ();
|
|
|
|
ret = rtrcmd (T, cmds[cmdidx]);
|
|
|
|
if (ret) {
|
2016-06-26 04:51:17 +00:00
|
|
|
r_cons_println (ret);
|
2014-08-28 09:38:11 +00:00
|
|
|
free (ret);
|
|
|
|
}
|
|
|
|
r_cons_flush ();
|
2014-09-02 00:20:03 +00:00
|
|
|
if (autorefresh) {
|
|
|
|
r_cons_printf ("(auto-refresh)\n");
|
|
|
|
r_cons_flush ();
|
2016-11-20 18:20:14 +00:00
|
|
|
r_cons_break_push (NULL, NULL);
|
2014-09-02 00:20:03 +00:00
|
|
|
r_sys_sleep (1);
|
2016-11-20 18:20:14 +00:00
|
|
|
if (r_cons_is_breaked ()) {
|
2015-09-14 10:35:38 +00:00
|
|
|
autorefresh = false;
|
2014-09-02 00:20:03 +00:00
|
|
|
ch = r_cons_readchar ();
|
|
|
|
} else {
|
2016-11-20 18:20:14 +00:00
|
|
|
r_cons_break_pop ();
|
2014-09-02 00:20:03 +00:00
|
|
|
continue;
|
|
|
|
}
|
2016-11-20 18:20:14 +00:00
|
|
|
r_cons_break_pop ();
|
2014-09-02 00:20:03 +00:00
|
|
|
} else {
|
|
|
|
ch = r_cons_readchar ();
|
|
|
|
}
|
2014-08-28 09:38:11 +00:00
|
|
|
#if 0
|
2015-04-11 09:45:10 +00:00
|
|
|
TODO:
|
2014-08-28 09:38:11 +00:00
|
|
|
i insert hex/string/asm
|
|
|
|
0-9 follow jumps
|
|
|
|
#endif
|
|
|
|
switch (ch) {
|
2015-04-11 09:45:10 +00:00
|
|
|
case '?':
|
2016-05-04 21:32:29 +00:00
|
|
|
r_cons_clear00 ();
|
2014-09-02 21:53:36 +00:00
|
|
|
r_cons_printf ("Remote Visual keys:\n"
|
|
|
|
" hjkl : move\n"
|
2014-10-06 22:56:39 +00:00
|
|
|
" HJKL : move faster\n"
|
2014-09-02 21:53:36 +00:00
|
|
|
" +-*/ : change block size\n"
|
|
|
|
" pP : rotate print modes\n"
|
|
|
|
" T : enter TextLog chat console\n"
|
|
|
|
" @ : enter auto-refresh mode\n"
|
2014-09-02 22:18:33 +00:00
|
|
|
" i : insert hexpair\n"
|
2014-09-02 21:53:36 +00:00
|
|
|
" q : quit this mode and go back to the shell\n"
|
2014-09-02 22:18:33 +00:00
|
|
|
" sS : step / step over\n"
|
2014-09-02 21:53:36 +00:00
|
|
|
" . : seek entry or pc\n");
|
2014-08-28 09:38:11 +00:00
|
|
|
r_cons_flush ();
|
2015-03-05 22:33:28 +00:00
|
|
|
r_cons_any_key (NULL);
|
2014-08-28 09:38:11 +00:00
|
|
|
break;
|
2014-09-02 22:18:33 +00:00
|
|
|
case 'i':
|
|
|
|
{
|
|
|
|
#if __UNIX__
|
2016-05-04 21:32:29 +00:00
|
|
|
#define COLORFLAGS (core->print->flags & R_PRINT_FLAGS_COLOR)
|
2014-09-02 22:18:33 +00:00
|
|
|
#else
|
2016-05-04 21:32:29 +00:00
|
|
|
#define COLORFLAGS 0
|
2014-09-02 22:18:33 +00:00
|
|
|
#endif
|
2016-05-04 21:32:29 +00:00
|
|
|
char buf[1024];
|
|
|
|
if (COLORFLAGS) {
|
2014-09-02 22:18:33 +00:00
|
|
|
r_line_set_prompt (Color_RESET":> ");
|
|
|
|
} else {
|
|
|
|
r_line_set_prompt (":> ");
|
|
|
|
}
|
2015-09-14 10:35:38 +00:00
|
|
|
showcursor (core, true);
|
2016-05-04 21:32:29 +00:00
|
|
|
r_cons_fgets (buf+3, sizeof (buf) - 4, 0, NULL);
|
2014-09-02 22:18:33 +00:00
|
|
|
memcpy (buf, "wx ", 3);
|
|
|
|
if (buf[3]) {
|
|
|
|
char *res = rtrcmd (T, buf);
|
|
|
|
if (res) {
|
2016-06-26 04:51:17 +00:00
|
|
|
r_cons_println (res);
|
2014-09-02 22:18:33 +00:00
|
|
|
free (res);
|
|
|
|
}
|
|
|
|
r_cons_flush ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
free (rtrcmd (T, "ds;.dr*"));
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
free (rtrcmd (T, "dso;.dr*"));
|
|
|
|
break;
|
2014-09-02 21:53:36 +00:00
|
|
|
case '.':
|
2015-10-31 00:57:52 +00:00
|
|
|
free (rtrcmd (T, "s entry0;dr?rip;?? sr PC"));
|
2014-09-02 21:53:36 +00:00
|
|
|
break;
|
2014-09-02 00:20:03 +00:00
|
|
|
case ':':
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
eprintf ("Press <enter> to return to Visual mode.\n");
|
|
|
|
do {
|
|
|
|
char buf[1024];
|
|
|
|
#if __UNIX__
|
|
|
|
r_line_set_prompt (Color_RESET":> ");
|
|
|
|
#else
|
|
|
|
r_line_set_prompt (":> ");
|
|
|
|
#endif
|
2015-09-14 10:35:38 +00:00
|
|
|
showcursor (core, true);
|
2014-09-02 00:20:03 +00:00
|
|
|
r_cons_fgets (buf, sizeof (buf), 0, NULL);
|
|
|
|
if (*buf) {
|
|
|
|
r_line_hist_add (buf);
|
|
|
|
char *res = rtrcmd (T, buf);
|
|
|
|
if (res) {
|
2016-06-26 04:51:17 +00:00
|
|
|
r_cons_println (res);
|
2014-09-02 00:20:03 +00:00
|
|
|
free (res);
|
|
|
|
}
|
|
|
|
r_cons_flush ();
|
2015-09-14 10:35:38 +00:00
|
|
|
ret = true;
|
2014-09-02 00:20:03 +00:00
|
|
|
} else {
|
2015-09-14 10:35:38 +00:00
|
|
|
ret = false;
|
2014-09-02 00:20:03 +00:00
|
|
|
//r_cons_any_key ();
|
|
|
|
r_cons_clear00 ();
|
2015-09-14 10:35:38 +00:00
|
|
|
showcursor (core, false);
|
2014-09-02 00:20:03 +00:00
|
|
|
}
|
|
|
|
} while (ret);
|
|
|
|
}
|
|
|
|
break;
|
2015-09-14 10:35:38 +00:00
|
|
|
case '@': autorefresh = true; break;
|
2015-04-11 09:45:10 +00:00
|
|
|
case 'j':
|
2016-05-04 21:32:29 +00:00
|
|
|
if (cmdidx == 1) {
|
2014-10-06 22:56:39 +00:00
|
|
|
free (rtrcmd (T, "so")); break;
|
|
|
|
} else {
|
|
|
|
free (rtrcmd (T, "s+16")); break;
|
|
|
|
}
|
|
|
|
break;
|
2014-08-28 09:38:11 +00:00
|
|
|
case 'k': free (rtrcmd (T, "s-16")); break;
|
|
|
|
case 'h': free (rtrcmd (T, "s-1")); break;
|
|
|
|
case 'l': free (rtrcmd (T, "s+1")); break;
|
2014-10-06 22:56:39 +00:00
|
|
|
case 'J':
|
2016-10-15 21:42:12 +00:00
|
|
|
if (cmdidx == 1) {
|
2014-10-06 22:56:39 +00:00
|
|
|
free (rtrcmd (T, "4so"));
|
|
|
|
} else {
|
|
|
|
free (rtrcmd (T, "s+32"));
|
2016-10-15 21:42:12 +00:00
|
|
|
}
|
|
|
|
break;
|
2014-10-06 22:56:39 +00:00
|
|
|
case 'K': free (rtrcmd (T, "s-32")); break;
|
|
|
|
case 'H': free (rtrcmd (T, "s-2")); break;
|
|
|
|
case 'L': free (rtrcmd (T, "s+2")); break;
|
2014-09-02 00:20:03 +00:00
|
|
|
case 'T': rtr_textlog_chat (core, T); break;
|
|
|
|
case '+': free (rtrcmd (T, "b+1")); break;
|
|
|
|
case '*': free (rtrcmd (T, "b+16")); break;
|
|
|
|
case '-': free (rtrcmd (T, "b-1")); break;
|
|
|
|
case '/': free (rtrcmd (T, "b-16")); break;
|
2018-09-13 08:17:26 +00:00
|
|
|
case 'p': cmdidx++;
|
|
|
|
if (!cmds[cmdidx]) {
|
|
|
|
cmdidx = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'P': cmdidx--;
|
|
|
|
if (cmdidx < 0) {
|
|
|
|
cmdidx = 2;
|
|
|
|
}
|
|
|
|
break;
|
2016-05-04 21:32:29 +00:00
|
|
|
case 'q': return false;
|
2014-08-28 02:41:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-04 21:32:29 +00:00
|
|
|
return true;
|
2014-08-28 02:41:33 +00:00
|
|
|
}
|
|
|
|
|
2014-04-11 00:22:41 +00:00
|
|
|
R_API int r_core_rtr_http_stop(RCore *u) {
|
2012-09-06 01:12:54 +00:00
|
|
|
RCore *core = (RCore*)u;
|
2016-05-04 21:32:29 +00:00
|
|
|
const int timeout = 1; // 1 second
|
|
|
|
const char *port;
|
|
|
|
RSocket* sock;
|
|
|
|
|
2015-06-03 22:13:13 +00:00
|
|
|
#if __WINDOWS__
|
2018-06-23 10:10:13 +00:00
|
|
|
r_socket_http_server_set_breaked (&r_cons_singleton ()->context->breaked);
|
2015-06-03 22:13:13 +00:00
|
|
|
#endif
|
2016-05-04 21:32:29 +00:00
|
|
|
if (((size_t)u) > 0xff) {
|
2014-04-16 04:26:47 +00:00
|
|
|
port = listenport? listenport: r_config_get (
|
|
|
|
core->config, "http.port");
|
2012-10-25 19:40:11 +00:00
|
|
|
sock = r_socket_new (0);
|
2015-04-11 09:45:10 +00:00
|
|
|
(void)r_socket_connect (sock, "localhost",
|
2014-04-16 04:26:47 +00:00
|
|
|
port, R_SOCKET_PROTO_TCP, timeout);
|
2012-11-05 01:00:34 +00:00
|
|
|
r_socket_free (sock);
|
2012-10-25 19:40:11 +00:00
|
|
|
}
|
2012-09-06 01:12:54 +00:00
|
|
|
r_socket_free (s);
|
2012-09-06 10:47:32 +00:00
|
|
|
s = NULL;
|
2014-04-11 00:22:41 +00:00
|
|
|
return 0;
|
2012-09-06 01:12:54 +00:00
|
|
|
}
|
|
|
|
|
2014-06-22 12:57:54 +00:00
|
|
|
static char *rtr_dir_files (const char *path) {
|
|
|
|
char *ptr = strdup ("<html><body>\n");
|
|
|
|
const char *file;
|
|
|
|
RListIter *iter;
|
|
|
|
// list files
|
|
|
|
RList *files = r_sys_dir (path);
|
|
|
|
eprintf ("Listing directory %s\n", path);
|
|
|
|
r_list_foreach (files, iter, file) {
|
2018-09-13 08:17:26 +00:00
|
|
|
if (file[0] == '.') {
|
|
|
|
continue;
|
|
|
|
}
|
2017-03-16 21:29:49 +00:00
|
|
|
ptr = r_str_appendf (ptr, "<a href=\"%s%s\">%s</a><br />\n",
|
2014-06-22 12:57:54 +00:00
|
|
|
path, file, file);
|
|
|
|
}
|
|
|
|
r_list_free (files);
|
2017-03-16 21:29:49 +00:00
|
|
|
return r_str_append (ptr, "</body></html>\n");
|
2014-06-22 12:57:54 +00:00
|
|
|
}
|
|
|
|
|
2015-01-12 01:52:12 +00:00
|
|
|
#if __UNIX__
|
2014-11-26 15:59:58 +00:00
|
|
|
static void dietime (int sig) {
|
|
|
|
eprintf ("It's Die Time!\n");
|
2015-01-12 01:52:12 +00:00
|
|
|
exit (0);
|
2014-11-26 15:59:58 +00:00
|
|
|
}
|
2015-01-12 01:52:12 +00:00
|
|
|
#endif
|
2014-11-26 15:59:58 +00:00
|
|
|
|
2014-11-26 21:19:55 +00:00
|
|
|
static void activateDieTime (RCore *core) {
|
|
|
|
int dt = r_config_get_i (core->config, "http.dietime");
|
2016-05-04 21:32:29 +00:00
|
|
|
if (dt > 0) {
|
2014-11-26 21:19:55 +00:00
|
|
|
#if __UNIX__
|
|
|
|
signal (SIGALRM, dietime);
|
|
|
|
alarm (dt);
|
|
|
|
#else
|
|
|
|
eprintf ("http.dietime only works on *nix systems\n");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-06 22:56:39 +00:00
|
|
|
// return 1 on error
|
2018-07-08 22:19:33 +00:00
|
|
|
static int r_core_rtr_http_run(RCore *core, int launch, int browse, const char *path) {
|
2016-05-04 21:32:29 +00:00
|
|
|
RConfig *newcfg = NULL, *origcfg = NULL;
|
2017-03-25 01:30:00 +00:00
|
|
|
char headers[128] = R_EMPTY;
|
2016-05-04 21:32:29 +00:00
|
|
|
RSocketHTTPRequest *rs;
|
2013-01-25 02:47:09 +00:00
|
|
|
char buf[32];
|
2016-05-04 21:32:29 +00:00
|
|
|
int ret = 0;
|
2014-09-01 22:56:28 +00:00
|
|
|
RSocket *s;
|
2019-01-10 09:51:51 +00:00
|
|
|
RSocketHTTPOptions so;
|
2016-05-04 21:32:29 +00:00
|
|
|
char *dir;
|
2019-01-10 09:51:51 +00:00
|
|
|
int iport;
|
2015-03-01 21:07:28 +00:00
|
|
|
const char *host = r_config_get (core->config, "http.bind");
|
2016-02-10 23:19:05 +00:00
|
|
|
const char *root = r_config_get (core->config, "http.root");
|
2016-05-15 22:59:29 +00:00
|
|
|
const char *homeroot = r_config_get (core->config, "http.homeroot");
|
2012-09-06 01:12:54 +00:00
|
|
|
const char *port = r_config_get (core->config, "http.port");
|
2015-01-13 01:22:20 +00:00
|
|
|
const char *allow = r_config_get (core->config, "http.allow");
|
|
|
|
const char *httpui = r_config_get (core->config, "http.ui");
|
2019-01-10 09:51:51 +00:00
|
|
|
const char *httpauthfile = r_config_get (core->config, "http.authfile");
|
|
|
|
char *pfile = NULL;
|
2014-08-29 16:45:14 +00:00
|
|
|
|
2016-02-10 23:19:05 +00:00
|
|
|
if (!r_file_is_directory (root)) {
|
2016-05-15 22:59:29 +00:00
|
|
|
if (!r_file_is_directory (homeroot)) {
|
|
|
|
eprintf ("Cannot find http.root (%s) or http.homeroot (%s)\n", root, homeroot);
|
|
|
|
return false;
|
|
|
|
}
|
2016-02-10 23:19:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-01-14 00:09:30 +00:00
|
|
|
if (!path) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-08 22:19:33 +00:00
|
|
|
char *arg = strchr (path, ' ');
|
|
|
|
if (arg) {
|
|
|
|
path = arg + 1;
|
|
|
|
}
|
2014-08-29 16:45:14 +00:00
|
|
|
if (path && atoi (path)) {
|
|
|
|
port = path;
|
2019-03-06 03:43:32 +00:00
|
|
|
r_config_set (core->config, "http.port", port);
|
2014-08-29 16:45:14 +00:00
|
|
|
path = NULL;
|
2015-01-13 01:22:20 +00:00
|
|
|
} else {
|
2016-09-02 18:57:45 +00:00
|
|
|
if (core->file && (!path || !*path)) {
|
2015-05-27 22:59:37 +00:00
|
|
|
if (!strcmp (httpui, "p")
|
2015-08-13 21:58:01 +00:00
|
|
|
|| !strcmp (httpui, "m")
|
|
|
|
|| !strcmp (httpui, "enyo")
|
|
|
|
|| !strcmp (httpui, "t")) {
|
2015-05-27 22:59:37 +00:00
|
|
|
path = httpui;
|
|
|
|
}
|
2015-01-13 01:22:20 +00:00
|
|
|
}
|
2014-08-29 16:45:14 +00:00
|
|
|
}
|
|
|
|
|
2013-01-25 02:47:09 +00:00
|
|
|
if (!strcmp (port, "0")) {
|
|
|
|
r_num_irand ();
|
2016-05-04 21:32:29 +00:00
|
|
|
iport = 1024 + r_num_rand (45256);
|
2013-01-25 02:47:09 +00:00
|
|
|
snprintf (buf, sizeof (buf), "%d", iport);
|
|
|
|
port = buf;
|
|
|
|
}
|
2015-09-14 10:35:38 +00:00
|
|
|
s = r_socket_new (false);
|
2015-03-23 23:47:10 +00:00
|
|
|
{
|
2015-04-13 00:02:07 +00:00
|
|
|
if (host && *host) {
|
|
|
|
if (!strcmp (host, "::1")) {
|
2015-09-14 10:35:38 +00:00
|
|
|
s->local = true;
|
2015-04-13 00:02:07 +00:00
|
|
|
} else if (!strcmp (host, "localhost")) {
|
2015-09-14 10:35:38 +00:00
|
|
|
s->local = true;
|
2015-04-13 00:02:07 +00:00
|
|
|
} else if (!strcmp (host, "127.0.0.1")) {
|
2015-09-14 10:35:38 +00:00
|
|
|
s->local = true;
|
2015-04-13 00:02:07 +00:00
|
|
|
} else if (!strcmp (host, "local")) {
|
2015-09-14 10:35:38 +00:00
|
|
|
s->local = true;
|
2015-03-23 23:47:10 +00:00
|
|
|
r_config_set (core->config, "http.bind", "localhost");
|
2015-04-13 00:02:07 +00:00
|
|
|
} else if (host[0]=='0' || !strcmp (host, "public")) {
|
2015-03-23 23:47:10 +00:00
|
|
|
// public
|
2015-04-17 22:36:24 +00:00
|
|
|
host = "127.0.0.1";
|
2016-05-04 21:32:29 +00:00
|
|
|
r_config_set (core->config, "http.bind", "0.0.0.0");
|
2015-09-14 10:35:38 +00:00
|
|
|
s->local = false;
|
2015-03-23 23:47:10 +00:00
|
|
|
} else {
|
2015-09-14 10:35:38 +00:00
|
|
|
s->local = true;
|
2015-03-23 23:47:10 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-09-14 10:35:38 +00:00
|
|
|
s->local = true;
|
2015-03-23 23:47:10 +00:00
|
|
|
}
|
2019-01-10 09:51:51 +00:00
|
|
|
memset (&so, 0, sizeof (so));
|
2015-03-23 23:47:10 +00:00
|
|
|
}
|
2012-09-06 01:12:54 +00:00
|
|
|
if (!r_socket_listen (s, port, NULL)) {
|
2015-01-24 14:26:17 +00:00
|
|
|
r_socket_free (s);
|
2012-09-06 01:12:54 +00:00
|
|
|
eprintf ("Cannot listen on http.port\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2018-06-17 23:32:24 +00:00
|
|
|
|
2018-07-08 22:19:33 +00:00
|
|
|
if (browse == 'H') {
|
2012-09-06 01:12:54 +00:00
|
|
|
const char *browser = r_config_get (core->config, "http.browser");
|
2015-12-24 14:13:17 +00:00
|
|
|
r_sys_cmdf ("%s http://%s:%d/%s &",
|
2015-03-01 21:07:28 +00:00
|
|
|
browser, host, atoi (port), path? path:"");
|
2012-09-06 01:12:54 +00:00
|
|
|
}
|
2014-09-01 22:56:28 +00:00
|
|
|
|
2019-01-10 09:51:51 +00:00
|
|
|
so.httpauth = r_config_get_i (core->config, "http.auth");
|
|
|
|
|
|
|
|
if (so.httpauth) {
|
|
|
|
if (!httpauthfile) {
|
|
|
|
r_socket_free (s);
|
|
|
|
eprintf ("No user list set for HTTP Authentification\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sz;
|
|
|
|
pfile = r_file_slurp (httpauthfile, &sz);
|
|
|
|
|
|
|
|
if (pfile) {
|
|
|
|
so.authtokens = r_str_split_list (pfile, "\n");
|
|
|
|
} else {
|
|
|
|
r_socket_free (s);
|
|
|
|
eprintf ("Empty list of HTTP users\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
so.timeout = r_config_get_i (core->config, "http.timeout");
|
|
|
|
so.accept_timeout = 1;
|
|
|
|
}
|
|
|
|
|
2014-09-01 22:56:28 +00:00
|
|
|
origcfg = core->config;
|
|
|
|
newcfg = r_config_clone (core->config);
|
|
|
|
core->config = newcfg;
|
|
|
|
|
2018-02-22 16:59:08 +00:00
|
|
|
r_config_set (core->config, "asm.cmt.right", "false");
|
2017-03-19 01:10:53 +00:00
|
|
|
#if 0
|
|
|
|
// WHY
|
2012-09-06 01:12:54 +00:00
|
|
|
r_config_set (core->config, "scr.html", "true");
|
2017-03-19 01:10:53 +00:00
|
|
|
#endif
|
2018-02-26 14:52:47 +00:00
|
|
|
r_config_set_i (core->config, "scr.color", COLOR_MODE_DISABLED);
|
2012-09-06 23:48:09 +00:00
|
|
|
r_config_set (core->config, "asm.bytes", "false");
|
2012-09-25 12:06:09 +00:00
|
|
|
r_config_set (core->config, "scr.interactive", "false");
|
2017-01-09 12:19:08 +00:00
|
|
|
bool restoreSandbox = false;
|
2012-12-03 03:01:55 +00:00
|
|
|
if (r_config_get_i (core->config, "http.sandbox")) {
|
2014-09-02 21:53:36 +00:00
|
|
|
//(void)r_config_get_i (core->config, "cfg.sandbox");
|
2012-12-03 03:01:55 +00:00
|
|
|
r_config_set (core->config, "cfg.sandbox", "true");
|
2017-01-09 12:19:08 +00:00
|
|
|
restoreSandbox = true;
|
2012-12-03 03:01:55 +00:00
|
|
|
}
|
2012-09-06 23:48:09 +00:00
|
|
|
eprintf ("Starting http server...\n");
|
2015-03-01 21:07:28 +00:00
|
|
|
eprintf ("open http://%s:%d/\n", host, atoi (port));
|
|
|
|
eprintf ("r2 -C http://%s:%d/cmd/\n", host, atoi (port));
|
2015-09-14 10:35:38 +00:00
|
|
|
core->http_up = true;
|
2014-09-01 22:56:28 +00:00
|
|
|
|
2014-09-02 21:53:36 +00:00
|
|
|
ut64 newoff, origoff = core->offset;
|
2014-11-26 21:19:55 +00:00
|
|
|
int newblksz, origblksz = core->blocksize;
|
2014-09-02 21:53:36 +00:00
|
|
|
ut8 *newblk, *origblk = core->block;
|
|
|
|
|
|
|
|
newblk = malloc (core->blocksize);
|
2018-08-16 02:29:15 +00:00
|
|
|
if (!newblk) {
|
2018-08-17 15:38:24 +00:00
|
|
|
r_socket_free (s);
|
2019-01-10 09:51:51 +00:00
|
|
|
r_list_free (so.authtokens);
|
|
|
|
free (pfile);
|
2018-08-16 02:29:15 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2014-09-02 21:53:36 +00:00
|
|
|
memcpy (newblk, core->block, core->blocksize);
|
|
|
|
|
|
|
|
core->block = newblk;
|
2014-09-01 22:56:28 +00:00
|
|
|
// TODO: handle mutex lock/unlock here
|
2016-11-20 18:20:14 +00:00
|
|
|
r_cons_break_push ((RConsBreak)r_core_rtr_http_stop, core);
|
|
|
|
while (!r_cons_is_breaked ()) {
|
2014-09-02 21:53:36 +00:00
|
|
|
/* restore environment */
|
2014-09-01 22:56:28 +00:00
|
|
|
core->config = origcfg;
|
|
|
|
r_config_set (origcfg, "scr.html", r_config_get (origcfg, "scr.html"));
|
2018-02-26 14:52:47 +00:00
|
|
|
r_config_set_i (origcfg, "scr.color", r_config_get_i (origcfg, "scr.color"));
|
2014-09-01 22:56:28 +00:00
|
|
|
r_config_set (origcfg, "scr.interactive", r_config_get (origcfg, "scr.interactive"));
|
2014-09-02 21:53:36 +00:00
|
|
|
core->http_up = 0; // DAT IS NOT TRUE AT ALL.. but its the way to enable visual
|
|
|
|
|
|
|
|
newoff = core->offset;
|
|
|
|
newblk = core->block;
|
|
|
|
newblksz = core->blocksize;
|
|
|
|
|
|
|
|
core->offset = origoff;
|
|
|
|
core->block = origblk;
|
|
|
|
core->blocksize = origblksz;
|
|
|
|
|
|
|
|
// backup and restore offset and blocksize
|
2015-04-11 09:45:10 +00:00
|
|
|
|
2014-09-02 21:53:36 +00:00
|
|
|
/* this is blocking */
|
2014-11-26 21:19:55 +00:00
|
|
|
activateDieTime (core);
|
2018-06-20 09:20:55 +00:00
|
|
|
|
2019-01-07 12:24:30 +00:00
|
|
|
void *bed = r_cons_sleep_begin ();
|
2019-01-10 09:51:51 +00:00
|
|
|
rs = r_socket_http_accept (s, &so);
|
2019-01-07 12:24:30 +00:00
|
|
|
r_cons_sleep_end (bed);
|
2014-09-02 21:53:36 +00:00
|
|
|
|
|
|
|
origoff = core->offset;
|
|
|
|
origblk = core->block;
|
|
|
|
origblksz = core->blocksize;
|
|
|
|
core->offset = newoff;
|
|
|
|
core->block = newblk;
|
|
|
|
core->blocksize = newblksz;
|
|
|
|
/* set environment */
|
|
|
|
// backup and restore offset and blocksize
|
|
|
|
core->http_up = 1;
|
2014-09-01 22:56:28 +00:00
|
|
|
core->config = newcfg;
|
|
|
|
r_config_set (newcfg, "scr.html", r_config_get (newcfg, "scr.html"));
|
2018-02-26 14:52:47 +00:00
|
|
|
r_config_set_i (newcfg, "scr.color", r_config_get_i (newcfg, "scr.color"));
|
2014-09-01 22:56:28 +00:00
|
|
|
r_config_set (newcfg, "scr.interactive", r_config_get (newcfg, "scr.interactive"));
|
|
|
|
|
2012-09-06 23:48:09 +00:00
|
|
|
if (!rs) {
|
2019-01-07 12:24:30 +00:00
|
|
|
bed = r_cons_sleep_begin ();
|
2013-02-25 00:47:55 +00:00
|
|
|
r_sys_usleep (100);
|
2019-01-07 12:24:30 +00:00
|
|
|
r_cons_sleep_end (bed);
|
2012-09-06 23:48:09 +00:00
|
|
|
continue;
|
|
|
|
}
|
2013-02-25 00:47:55 +00:00
|
|
|
if (allow && *allow) {
|
2016-10-09 21:56:52 +00:00
|
|
|
bool accepted = false;
|
2015-03-01 21:07:28 +00:00
|
|
|
const char *allows_host;
|
2013-02-25 00:47:55 +00:00
|
|
|
char *p, *peer = r_socket_to_string (rs->s);
|
|
|
|
char *allows = strdup (allow);
|
|
|
|
//eprintf ("Firewall (%s)\n", allows);
|
|
|
|
int i, count = r_str_split (allows, ',');
|
|
|
|
p = strchr (peer, ':');
|
2016-11-20 18:20:14 +00:00
|
|
|
if (p) {
|
|
|
|
*p = 0;
|
|
|
|
}
|
2016-05-04 21:32:29 +00:00
|
|
|
for (i = 0; i < count; i++) {
|
2015-03-01 21:07:28 +00:00
|
|
|
allows_host = r_str_word_get0 (allows, i);
|
2013-02-25 00:47:55 +00:00
|
|
|
//eprintf ("--- (%s) (%s)\n", host, peer);
|
2015-03-01 21:07:28 +00:00
|
|
|
if (!strcmp (allows_host, peer)) {
|
2015-09-14 10:35:38 +00:00
|
|
|
accepted = true;
|
2013-02-25 00:47:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free (peer);
|
|
|
|
free (allows);
|
|
|
|
if (!accepted) {
|
|
|
|
r_socket_http_close (rs);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2012-12-03 03:36:00 +00:00
|
|
|
if (!rs->method || !rs->path) {
|
2016-10-15 21:42:12 +00:00
|
|
|
http_logf (core, "Invalid http headers received from client\n");
|
2012-12-03 03:36:00 +00:00
|
|
|
r_socket_http_close (rs);
|
|
|
|
continue;
|
|
|
|
}
|
2015-01-24 02:08:03 +00:00
|
|
|
dir = NULL;
|
2014-06-22 12:57:54 +00:00
|
|
|
|
2019-01-10 09:51:51 +00:00
|
|
|
if (!rs->auth) {
|
|
|
|
r_socket_http_response (rs, 401, "", 0, NULL);
|
|
|
|
}
|
|
|
|
|
2015-01-24 02:08:03 +00:00
|
|
|
if (r_config_get_i (core->config, "http.verbose")) {
|
|
|
|
char *peer = r_socket_to_string (rs->s);
|
2016-10-15 21:42:12 +00:00
|
|
|
http_logf (core, "[HTTP] %s %s\n", peer, rs->path);
|
2015-01-24 02:08:03 +00:00
|
|
|
free (peer);
|
|
|
|
}
|
2016-11-20 18:20:14 +00:00
|
|
|
if (r_config_get_i (core->config, "http.dirlist")) {
|
|
|
|
if (r_file_is_directory (rs->path)) {
|
2014-06-22 12:57:54 +00:00
|
|
|
dir = strdup (rs->path);
|
2016-11-20 18:20:14 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-11 16:54:35 +00:00
|
|
|
if (r_config_get_i (core->config, "http.cors")) {
|
2016-05-04 21:32:29 +00:00
|
|
|
strcpy (headers, "Access-Control-Allow-Origin: *\n"
|
|
|
|
"Access-Control-Allow-Headers: Origin, "
|
|
|
|
"X-Requested-With, Content-Type, Accept\n");
|
2015-04-11 16:54:35 +00:00
|
|
|
}
|
2015-04-11 09:45:10 +00:00
|
|
|
if (!strcmp (rs->method, "OPTIONS")) {
|
2015-07-09 17:23:59 +00:00
|
|
|
r_socket_http_response (rs, 200, "", 0, headers);
|
2016-10-09 21:56:52 +00:00
|
|
|
} else if (!strcmp (rs->method, "GET")) {
|
2015-01-23 00:18:13 +00:00
|
|
|
if (!strncmp (rs->path, "/up/", 4)) {
|
2013-01-03 00:43:23 +00:00
|
|
|
if (r_config_get_i (core->config, "http.upget")) {
|
2013-01-04 09:16:41 +00:00
|
|
|
const char *uproot = r_config_get (core->config, "http.uproot");
|
2016-05-04 21:32:29 +00:00
|
|
|
if (!rs->path[3] || (rs->path[3]=='/' && !rs->path[4])) {
|
2014-06-22 12:57:54 +00:00
|
|
|
char *ptr = rtr_dir_files (uproot);
|
2015-04-11 16:54:35 +00:00
|
|
|
r_socket_http_response (rs, 200, ptr, 0, headers);
|
2014-06-22 12:57:54 +00:00
|
|
|
free (ptr);
|
2013-01-04 09:16:41 +00:00
|
|
|
} else {
|
|
|
|
char *path = r_file_root (uproot, rs->path + 4);
|
|
|
|
if (r_file_exists (path)) {
|
2014-09-20 12:43:18 +00:00
|
|
|
int sz = 0;
|
2013-01-04 09:16:41 +00:00
|
|
|
char *f = r_file_slurp (path, &sz);
|
|
|
|
if (f) {
|
2015-04-11 16:54:35 +00:00
|
|
|
r_socket_http_response (rs, 200, f, sz, headers);
|
2013-01-04 09:16:41 +00:00
|
|
|
free (f);
|
|
|
|
} else {
|
2015-04-11 16:54:35 +00:00
|
|
|
r_socket_http_response (rs, 403, "Permission denied", 0, headers);
|
2016-10-15 21:42:12 +00:00
|
|
|
http_logf (core, "http: Cannot open '%s'\n", path);
|
2013-01-04 09:16:41 +00:00
|
|
|
}
|
|
|
|
} else {
|
2014-06-22 12:57:54 +00:00
|
|
|
if (dir) {
|
|
|
|
char *resp = rtr_dir_files (dir);
|
2015-04-11 16:54:35 +00:00
|
|
|
r_socket_http_response (rs, 404, resp, 0, headers);
|
2014-06-22 12:57:54 +00:00
|
|
|
free (resp);
|
|
|
|
} else {
|
2016-10-15 21:42:12 +00:00
|
|
|
http_logf (core, "File '%s' not found\n", path);
|
2015-04-11 16:54:35 +00:00
|
|
|
r_socket_http_response (rs, 404, "File not found\n", 0, headers);
|
2014-06-22 12:57:54 +00:00
|
|
|
}
|
2013-01-04 09:16:41 +00:00
|
|
|
}
|
|
|
|
free (path);
|
|
|
|
}
|
2013-01-03 00:43:23 +00:00
|
|
|
} else {
|
2019-01-10 09:51:51 +00:00
|
|
|
r_socket_http_response (rs, 403, "", 0, NULL);
|
2013-01-03 00:43:23 +00:00
|
|
|
}
|
2014-09-18 14:54:21 +00:00
|
|
|
} else if (!strncmp (rs->path, "/cmd/", 5)) {
|
2019-01-08 22:40:19 +00:00
|
|
|
const bool colon = r_config_get_i (core->config, "http.colon");
|
|
|
|
if (colon && rs->path[5] != ':') {
|
|
|
|
r_socket_http_response (rs, 403, "Permission denied", 0, headers);
|
2016-03-28 21:38:45 +00:00
|
|
|
} else {
|
2019-01-08 22:40:19 +00:00
|
|
|
char *cmd = rs->path + 5;
|
|
|
|
const char *httpcmd = r_config_get (core->config, "http.uri");
|
|
|
|
const char *httpref = r_config_get (core->config, "http.referer");
|
|
|
|
const bool httpref_enabled = (httpref && *httpref);
|
|
|
|
char *refstr = NULL;
|
|
|
|
if (httpref_enabled) {
|
|
|
|
if (strstr (httpref, "http")) {
|
|
|
|
refstr = strdup (httpref);
|
2015-04-13 00:02:07 +00:00
|
|
|
} else {
|
2019-01-08 22:40:19 +00:00
|
|
|
refstr = r_str_newf ("http://localhost:%d/", atoi (port));
|
2015-04-13 00:02:07 +00:00
|
|
|
}
|
2019-01-08 22:40:19 +00:00
|
|
|
}
|
2017-11-26 13:28:57 +00:00
|
|
|
|
2019-01-08 22:40:19 +00:00
|
|
|
while (*cmd == '/') {
|
|
|
|
cmd++;
|
|
|
|
}
|
|
|
|
if (httpref_enabled && (!rs->referer || (refstr && !strstr (rs->referer, refstr)))) {
|
|
|
|
r_socket_http_response (rs, 503, "", 0, headers);
|
|
|
|
} else {
|
|
|
|
if (httpcmd && *httpcmd) {
|
|
|
|
int len; // do remote http query and proxy response
|
|
|
|
char *res, *bar = r_str_newf ("%s/%s", httpcmd, cmd);
|
|
|
|
bed = r_cons_sleep_begin ();
|
|
|
|
res = r_socket_http_get (bar, NULL, &len);
|
|
|
|
r_cons_sleep_end (bed);
|
|
|
|
if (res) {
|
|
|
|
res[len] = 0;
|
|
|
|
r_cons_println (res);
|
|
|
|
}
|
|
|
|
free (bar);
|
2016-10-09 21:56:52 +00:00
|
|
|
} else {
|
2019-01-08 22:40:19 +00:00
|
|
|
char *out, *cmd = rs->path + 5;
|
|
|
|
r_str_uri_decode (cmd);
|
|
|
|
r_config_set (core->config, "scr.interactive", "false");
|
|
|
|
|
|
|
|
if (!r_sandbox_enable (0) &&
|
|
|
|
(!strcmp (cmd, "=h*") ||
|
|
|
|
!strcmp (cmd, "=h--"))) {
|
|
|
|
out = NULL;
|
|
|
|
} else if (*cmd == ':') {
|
|
|
|
/* commands in /cmd/: starting with : do not show any output */
|
|
|
|
r_core_cmd0 (core, cmd + 1);
|
|
|
|
out = NULL;
|
|
|
|
} else {
|
|
|
|
out = r_core_cmd_str_pipe (core, cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (out) {
|
|
|
|
char *res = r_str_uri_encode (out);
|
|
|
|
char *newheaders = r_str_newf (
|
|
|
|
"Content-Type: text/plain\n%s", headers);
|
|
|
|
r_socket_http_response (rs, 200, out, 0, newheaders);
|
|
|
|
free (out);
|
|
|
|
free (newheaders);
|
|
|
|
free (res);
|
|
|
|
} else {
|
|
|
|
r_socket_http_response (rs, 200, "", 0, headers);
|
|
|
|
}
|
2017-11-26 13:28:57 +00:00
|
|
|
|
2019-01-08 22:40:19 +00:00
|
|
|
if (!r_sandbox_enable (0)) {
|
|
|
|
if (!strcmp (cmd, "=h*")) {
|
|
|
|
/* do stuff */
|
|
|
|
r_socket_http_close (rs);
|
|
|
|
free (dir);
|
|
|
|
free (refstr);
|
|
|
|
ret = -2;
|
|
|
|
goto the_end;
|
|
|
|
} else if (!strcmp (cmd, "=h--")) {
|
|
|
|
r_socket_http_close (rs);
|
|
|
|
free (dir);
|
|
|
|
free (refstr);
|
|
|
|
ret = 0;
|
|
|
|
goto the_end;
|
|
|
|
}
|
2017-11-26 13:28:57 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-13 00:02:07 +00:00
|
|
|
}
|
2019-01-08 22:40:19 +00:00
|
|
|
free (refstr);
|
2013-02-06 09:35:23 +00:00
|
|
|
}
|
2012-09-06 01:12:54 +00:00
|
|
|
} else {
|
|
|
|
const char *root = r_config_get (core->config, "http.root");
|
2016-05-15 22:59:29 +00:00
|
|
|
const char *homeroot = r_config_get (core->config, "http.homeroot");
|
|
|
|
char *path;
|
2016-05-18 10:03:07 +00:00
|
|
|
if (!strcmp (rs->path, "/")) {
|
|
|
|
free (rs->path);
|
|
|
|
rs->path = strdup ("/index.html");
|
|
|
|
}
|
2016-05-15 22:59:29 +00:00
|
|
|
if (homeroot && *homeroot) {
|
|
|
|
char *homepath = r_file_abspath (homeroot);
|
|
|
|
path = r_file_root (homepath, rs->path);
|
|
|
|
free (homepath);
|
2016-05-18 10:03:07 +00:00
|
|
|
if (!r_file_exists (path) && !r_file_is_directory (path)) {
|
2016-05-15 22:59:29 +00:00
|
|
|
free (path);
|
|
|
|
path = r_file_root (root, rs->path);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
path = r_file_root (root, rs->path);
|
|
|
|
}
|
2014-04-21 10:55:34 +00:00
|
|
|
// FD IS OK HERE
|
2016-05-18 10:03:07 +00:00
|
|
|
if (rs->path [strlen (rs->path) - 1] == '/') {
|
2017-03-16 21:29:49 +00:00
|
|
|
path = r_str_append (path, "index.html");
|
|
|
|
//rs->path = r_str_append (rs->path, "index.html");
|
2012-12-01 01:35:12 +00:00
|
|
|
} else {
|
2013-01-04 09:16:41 +00:00
|
|
|
//snprintf (path, sizeof (path), "%s/%s", root, rs->path);
|
2012-12-01 01:35:12 +00:00
|
|
|
if (r_file_is_directory (path)) {
|
2016-05-04 21:32:29 +00:00
|
|
|
char *res = r_str_newf ("Location: %s/\n%s", rs->path, headers);
|
|
|
|
r_socket_http_response (rs, 302, NULL, 0, res);
|
2012-12-01 01:35:12 +00:00
|
|
|
r_socket_http_close (rs);
|
2013-01-04 09:16:41 +00:00
|
|
|
free (path);
|
2016-05-04 21:32:29 +00:00
|
|
|
free (res);
|
|
|
|
R_FREE (dir);
|
2012-12-01 01:35:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2012-09-06 06:59:13 +00:00
|
|
|
if (r_file_exists (path)) {
|
2014-09-20 12:43:18 +00:00
|
|
|
int sz = 0;
|
2012-09-06 01:12:54 +00:00
|
|
|
char *f = r_file_slurp (path, &sz);
|
|
|
|
if (f) {
|
2016-05-04 21:32:29 +00:00
|
|
|
const char *ct = NULL;
|
2016-11-20 18:20:14 +00:00
|
|
|
if (strstr (path, ".js")) {
|
|
|
|
ct = "Content-Type: application/javascript\n";
|
|
|
|
}
|
|
|
|
if (strstr (path, ".css")) {
|
|
|
|
ct = "Content-Type: text/css\n";
|
|
|
|
}
|
|
|
|
if (strstr (path, ".html")) {
|
|
|
|
ct = "Content-Type: text/html\n";
|
|
|
|
}
|
2016-05-04 21:32:29 +00:00
|
|
|
char *hdr = r_str_newf ("%s%s", ct, headers);
|
|
|
|
r_socket_http_response (rs, 200, f, sz, hdr);
|
|
|
|
free (hdr);
|
2012-09-06 01:12:54 +00:00
|
|
|
free (f);
|
2012-12-03 03:01:55 +00:00
|
|
|
} else {
|
2015-04-11 16:54:35 +00:00
|
|
|
r_socket_http_response (rs, 403, "Permission denied", 0, headers);
|
2016-10-15 21:42:12 +00:00
|
|
|
http_logf (core, "http: Cannot open '%s'\n", path);
|
2012-12-03 03:01:55 +00:00
|
|
|
}
|
2012-09-06 01:12:54 +00:00
|
|
|
} else {
|
2014-06-22 12:57:54 +00:00
|
|
|
if (dir) {
|
|
|
|
char *resp = rtr_dir_files (dir);
|
2016-10-15 21:42:12 +00:00
|
|
|
http_logf (core, "Dirlisting %s\n", dir);
|
2015-04-11 16:54:35 +00:00
|
|
|
r_socket_http_response (rs, 404, resp, 0, headers);
|
2014-06-22 12:57:54 +00:00
|
|
|
free (resp);
|
|
|
|
} else {
|
2016-10-15 21:42:12 +00:00
|
|
|
http_logf (core, "File '%s' not found\n", path);
|
2015-04-11 16:54:35 +00:00
|
|
|
r_socket_http_response (rs, 404, "File not found\n", 0, headers);
|
2014-06-22 12:57:54 +00:00
|
|
|
}
|
2012-09-06 01:12:54 +00:00
|
|
|
}
|
2013-01-04 09:16:41 +00:00
|
|
|
free (path);
|
2012-09-06 01:12:54 +00:00
|
|
|
}
|
2016-10-09 21:56:52 +00:00
|
|
|
} else if (!strcmp (rs->method, "POST")) {
|
2013-01-03 00:43:23 +00:00
|
|
|
ut8 *ret;
|
2013-01-02 17:37:48 +00:00
|
|
|
int retlen;
|
2013-01-02 23:47:58 +00:00
|
|
|
char buf[128];
|
|
|
|
if (r_config_get_i (core->config, "http.upload")) {
|
2016-05-04 21:32:29 +00:00
|
|
|
ret = r_socket_http_handle_upload (rs->data, rs->data_length, &retlen);
|
2013-01-03 00:43:23 +00:00
|
|
|
if (ret) {
|
|
|
|
ut64 size = r_config_get_i (core->config, "http.maxsize");
|
|
|
|
if (size && retlen > size) {
|
2015-04-11 16:54:35 +00:00
|
|
|
r_socket_http_response (rs, 403, "403 File too big\n", 0, headers);
|
2013-01-03 00:43:23 +00:00
|
|
|
} else {
|
|
|
|
char *filename = r_file_root (
|
|
|
|
r_config_get (core->config, "http.uproot"),
|
|
|
|
rs->path + 4);
|
2016-10-15 21:42:12 +00:00
|
|
|
http_logf (core, "UPLOADED '%s'\n", filename);
|
2015-04-03 02:04:46 +00:00
|
|
|
r_file_dump (filename, ret, retlen, 0);
|
2013-01-03 00:43:23 +00:00
|
|
|
free (filename);
|
|
|
|
snprintf (buf, sizeof (buf),
|
2017-12-27 17:33:58 +00:00
|
|
|
"<html><body><h2>uploaded %d byte(s). Thanks</h2>\n", retlen);
|
2015-04-11 16:54:35 +00:00
|
|
|
r_socket_http_response (rs, 200, buf, 0, headers);
|
2013-01-03 00:43:23 +00:00
|
|
|
}
|
|
|
|
free (ret);
|
|
|
|
}
|
2013-01-02 23:47:58 +00:00
|
|
|
} else {
|
2015-04-11 16:54:35 +00:00
|
|
|
r_socket_http_response (rs, 403, "403 Forbidden\n", 0, headers);
|
2013-01-02 23:47:58 +00:00
|
|
|
}
|
2012-09-06 01:12:54 +00:00
|
|
|
} else {
|
2015-04-11 16:54:35 +00:00
|
|
|
r_socket_http_response (rs, 404, "Invalid protocol", 0, headers);
|
2012-09-06 01:12:54 +00:00
|
|
|
}
|
2012-12-12 03:10:44 +00:00
|
|
|
r_socket_http_close (rs);
|
2014-06-22 12:57:54 +00:00
|
|
|
free (dir);
|
2012-09-06 01:12:54 +00:00
|
|
|
}
|
2015-04-13 00:02:07 +00:00
|
|
|
the_end:
|
2016-05-04 21:32:29 +00:00
|
|
|
{
|
|
|
|
int timeout = r_config_get_i (core->config, "http.timeout");
|
|
|
|
const char *host = r_config_get (core->config, "http.bind");
|
|
|
|
const char *port = r_config_get (core->config, "http.port");
|
|
|
|
const char *cors = r_config_get (core->config, "http.cors");
|
|
|
|
const char *allow = r_config_get (core->config, "http.allow");
|
|
|
|
const char *httpui = r_config_get (core->config, "http.ui");
|
|
|
|
core->config = origcfg;
|
|
|
|
r_config_set_i (core->config, "http.timeout", timeout);
|
|
|
|
r_config_set (core->config, "http.bind", host);
|
|
|
|
r_config_set (core->config, "http.port", port);
|
|
|
|
r_config_set (core->config, "http.cors", cors);
|
|
|
|
r_config_set (core->config, "http.allow", allow);
|
|
|
|
r_config_set (core->config, "http.ui", httpui);
|
|
|
|
}
|
2016-11-20 18:20:14 +00:00
|
|
|
r_cons_break_pop ();
|
2015-09-14 10:35:38 +00:00
|
|
|
core->http_up = false;
|
2019-01-10 09:51:51 +00:00
|
|
|
free (pfile);
|
2012-09-06 01:12:54 +00:00
|
|
|
r_socket_free (s);
|
2014-09-01 22:56:28 +00:00
|
|
|
r_config_free (newcfg);
|
2017-01-09 12:19:08 +00:00
|
|
|
if (restoreSandbox) {
|
|
|
|
r_sandbox_disable (true);
|
|
|
|
}
|
2016-10-09 21:56:52 +00:00
|
|
|
/* refresh settings - run callbacks */
|
2014-09-01 22:56:28 +00:00
|
|
|
r_config_set (origcfg, "scr.html", r_config_get (origcfg, "scr.html"));
|
2018-02-26 14:52:47 +00:00
|
|
|
r_config_set_i (origcfg, "scr.color", r_config_get_i (origcfg, "scr.color"));
|
2014-09-01 22:56:28 +00:00
|
|
|
r_config_set (origcfg, "scr.interactive", r_config_get (origcfg, "scr.interactive"));
|
2015-04-13 00:02:07 +00:00
|
|
|
return ret;
|
2012-09-06 01:12:54 +00:00
|
|
|
}
|
|
|
|
|
2019-02-14 09:04:48 +00:00
|
|
|
#if 0
|
2018-08-23 11:49:28 +00:00
|
|
|
static RThreadFunctionRet r_core_rtr_http_thread (RThread *th) {
|
2016-07-29 17:32:27 +00:00
|
|
|
if (!th) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-04 21:32:29 +00:00
|
|
|
HttpThread *ht = th->user;
|
2016-07-29 17:32:27 +00:00
|
|
|
if (!ht || !ht->core) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-08 22:19:33 +00:00
|
|
|
eprintf ("WARNING: Background webserver requires http.sandbox=false to run properly\n");
|
|
|
|
int ret = r_core_rtr_http_run (ht->core, ht->launch, ht->browse, ht->path);
|
2016-06-08 12:46:24 +00:00
|
|
|
R_FREE (ht->path);
|
2017-04-03 21:22:59 +00:00
|
|
|
if (ret) {
|
|
|
|
int p = r_config_get_i (ht->core->config, "http.port");
|
|
|
|
r_config_set_i (ht->core->config, "http.port", p + 1);
|
|
|
|
if (p >= r_config_get_i (ht->core->config, "http.maxport")) {
|
2018-08-23 11:49:28 +00:00
|
|
|
return R_TH_STOP;
|
2017-04-03 21:22:59 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-23 11:49:28 +00:00
|
|
|
return ret ? R_TH_REPEAT : R_TH_STOP;
|
2014-08-28 10:08:46 +00:00
|
|
|
}
|
2019-02-14 09:04:48 +00:00
|
|
|
#endif
|
2014-08-28 10:08:46 +00:00
|
|
|
|
2018-07-08 22:19:33 +00:00
|
|
|
R_API int r_core_rtr_http(RCore *core, int launch, int browse, const char *path) {
|
2015-04-13 00:02:07 +00:00
|
|
|
int ret;
|
2014-08-28 10:08:46 +00:00
|
|
|
if (r_sandbox_enable (0)) {
|
|
|
|
eprintf ("sandbox: connect disabled\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2016-09-02 18:57:45 +00:00
|
|
|
if (launch == '-') {
|
2014-09-01 22:01:56 +00:00
|
|
|
if (httpthread) {
|
|
|
|
eprintf ("Press ^C to stop the webserver\n");
|
2018-08-23 11:49:28 +00:00
|
|
|
r_th_kill_free (httpthread);
|
2014-09-01 22:01:56 +00:00
|
|
|
httpthread = NULL;
|
|
|
|
} else {
|
|
|
|
eprintf ("No webserver running\n");
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2014-08-28 10:08:46 +00:00
|
|
|
if (core->http_up) {
|
|
|
|
eprintf ("http server is already running\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2016-05-04 21:32:29 +00:00
|
|
|
if (launch == '&') {
|
2019-01-14 00:36:30 +00:00
|
|
|
while (*path == '&') path++;
|
|
|
|
return r_core_cmdf (core, "& =h%s", path);
|
|
|
|
}
|
|
|
|
#if 0
|
2014-09-01 22:01:56 +00:00
|
|
|
if (httpthread) {
|
|
|
|
eprintf ("HTTP Thread is already running\n");
|
|
|
|
eprintf ("This is experimental and probably buggy. Use at your own risk\n");
|
|
|
|
eprintf ("TODO: Use different eval environ for scr. for the web\n");
|
|
|
|
eprintf ("TODO: Visual mode should be enabled on local\n");
|
|
|
|
} else {
|
2018-01-08 02:29:29 +00:00
|
|
|
const char *tpath = r_str_trim_ro (path + 1);
|
2017-04-02 23:00:45 +00:00
|
|
|
//HttpThread ht = { core, launch, strdup (tpath) };
|
|
|
|
HttpThread *ht = calloc (sizeof (HttpThread), 1);
|
|
|
|
ht->core = core;
|
|
|
|
ht->launch = launch;
|
2018-07-08 22:19:33 +00:00
|
|
|
ht->browse = browse;
|
2017-04-02 23:00:45 +00:00
|
|
|
ht->path = strdup (tpath);
|
|
|
|
httpthread = r_th_new (r_core_rtr_http_thread, ht, false);
|
2019-03-13 09:47:57 +00:00
|
|
|
if (httpthread) {
|
|
|
|
r_th_setname (httpthread, "httpthread");
|
|
|
|
}
|
2016-07-29 17:32:27 +00:00
|
|
|
r_th_start (httpthread, true);
|
2014-09-01 22:01:56 +00:00
|
|
|
eprintf ("Background http server started.\n");
|
|
|
|
}
|
|
|
|
return 0;
|
2014-08-28 10:08:46 +00:00
|
|
|
}
|
2019-01-14 00:36:30 +00:00
|
|
|
#endif
|
2015-04-13 00:02:07 +00:00
|
|
|
do {
|
2018-07-08 22:19:33 +00:00
|
|
|
ret = r_core_rtr_http_run (core, launch, browse, path);
|
2015-04-13 00:02:07 +00:00
|
|
|
} while (ret == -2);
|
|
|
|
return ret;
|
2014-08-28 10:08:46 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 06:51:30 +00:00
|
|
|
static int write_reg_val(char *buf, ut64 sz, ut64 reg, int regsize, bool bigendian) {
|
|
|
|
if (!bigendian) {
|
|
|
|
switch (regsize) {
|
|
|
|
case 2:
|
|
|
|
reg = r_swap_ut16 (reg);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
reg = r_swap_ut32 (reg);
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
reg = r_swap_ut64 (reg);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
eprintf ("%s: Unsupported reg size: %d\n",
|
|
|
|
__func__, regsize);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return snprintf (buf, sz, regsize == 2 ? "%04"PFMT64x
|
|
|
|
: regsize == 4 ? "%08"PFMT64x : "%016"PFMT64x, reg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int write_big_reg(char *buf, ut64 sz, const utX *val, int regsize, bool bigendian) {
|
|
|
|
switch (regsize) {
|
|
|
|
case 10:
|
|
|
|
if (bigendian) {
|
|
|
|
return snprintf (buf, sz,
|
|
|
|
"%04x%016"PFMT64x, val->v80.High,
|
|
|
|
val->v80.Low);
|
|
|
|
}
|
|
|
|
return snprintf (buf, sz,
|
|
|
|
"%016"PFMT64x"%04x", r_swap_ut64 (val->v80.Low),
|
|
|
|
r_swap_ut16 (val->v80.High));
|
|
|
|
case 12:
|
|
|
|
if (bigendian) {
|
|
|
|
return snprintf (buf, sz,
|
|
|
|
"%08"PFMT32x"%016"PFMT64x, val->v96.High,
|
|
|
|
val->v96.Low);
|
|
|
|
}
|
|
|
|
return snprintf (buf, sz,
|
|
|
|
"%016"PFMT64x"%08"PFMT32x, r_swap_ut64 (val->v96.Low),
|
|
|
|
r_swap_ut32 (val->v96.High));
|
|
|
|
case 16:
|
|
|
|
if (bigendian) {
|
|
|
|
return snprintf (buf, sz,
|
|
|
|
"%016"PFMT64x"%016"PFMT64x, val->v128.High,
|
|
|
|
val->v128.Low);
|
|
|
|
}
|
|
|
|
return snprintf (buf, sz,
|
|
|
|
"%016"PFMT64x"%016"PFMT64x,
|
|
|
|
r_swap_ut64 (val->v128.Low),
|
|
|
|
r_swap_ut64 (val->v128.High));
|
|
|
|
default:
|
2017-12-27 17:33:58 +00:00
|
|
|
eprintf ("%s: big registers (%d byte(s)) not yet supported\n",
|
2017-07-17 06:51:30 +00:00
|
|
|
__func__, regsize);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int swap_big_regs (char *dest, ut64 sz, const char *src, int regsz) {
|
|
|
|
utX val;
|
2017-07-17 10:05:37 +00:00
|
|
|
char sdup[128] = {0};
|
|
|
|
if (!src || !src[0] || !src[1]) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
strncpy (sdup, src + 2, sizeof (sdup) - 1);
|
2017-07-17 06:51:30 +00:00
|
|
|
int len = strlen (sdup);
|
|
|
|
memset (&val, 0, sizeof (val));
|
|
|
|
switch (regsz) {
|
|
|
|
case 10:
|
|
|
|
if (len <= 4) {
|
|
|
|
val.v80.High = (ut16) strtoul (sdup, NULL, 16);
|
|
|
|
} else {
|
|
|
|
val.v80.High = (ut16) strtoul (sdup + (len - 4), NULL, 16);
|
|
|
|
sdup[len - 4] = '\0';
|
|
|
|
val.v80.Low = (ut64) strtoull (sdup, NULL, 16);
|
|
|
|
}
|
|
|
|
return snprintf (dest, sz, "0x%04x%016"PFMT64x,
|
|
|
|
val.v80.High, val.v80.Low);
|
|
|
|
case 12:
|
|
|
|
if (len <= 8) {
|
|
|
|
val.v96.High = (ut32) strtoul (sdup, NULL, 16);
|
|
|
|
} else {
|
|
|
|
val.v96.High = (ut32) strtoul (sdup + (len - 8), NULL, 16);
|
|
|
|
sdup[len - 8] = '\0';
|
|
|
|
val.v96.Low = (ut64) strtoull (sdup, NULL, 16);
|
|
|
|
}
|
|
|
|
return snprintf (dest, sz, "0x%08x%016"PFMT64x,
|
|
|
|
val.v96.High, val.v96.Low);
|
|
|
|
case 16:
|
|
|
|
if (len <= 16) {
|
|
|
|
val.v128.High = (ut64) strtoul (sdup, NULL, 16);
|
|
|
|
} else {
|
|
|
|
val.v128.High = (ut64) strtoul (sdup + (len - 16), NULL, 16);
|
|
|
|
sdup[len - 16] = '\0';
|
|
|
|
val.v128.Low = (ut64) strtoull (sdup, NULL, 16);
|
|
|
|
}
|
|
|
|
return snprintf (dest, sz, "0x%016"PFMT64x"%016"PFMT64x,
|
|
|
|
val.v128.High, val.v128.Low);
|
|
|
|
default:
|
2017-12-27 17:33:58 +00:00
|
|
|
eprintf ("%s: big registers (%d byte(s)) not yet supported\n",
|
2017-07-17 06:51:30 +00:00
|
|
|
__func__, regsz);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int r_core_rtr_gdb_cb(libgdbr_t *g, void *core_ptr, const char *cmd,
|
|
|
|
char *out_buf, size_t max_len) {
|
2017-05-29 19:36:42 +00:00
|
|
|
int ret;
|
|
|
|
RList *list;
|
|
|
|
RListIter *iter;
|
2017-06-14 11:44:41 +00:00
|
|
|
gdb_reg_t *gdb_reg;
|
2017-07-17 06:51:30 +00:00
|
|
|
RRegItem *r;
|
2017-06-14 11:44:41 +00:00
|
|
|
utX val_big;
|
|
|
|
ut64 m_off, reg_val;
|
|
|
|
bool be;
|
2017-06-04 20:22:01 +00:00
|
|
|
RDebugPid *dbgpid;
|
2017-05-29 19:36:42 +00:00
|
|
|
if (!core_ptr || ! cmd) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
RCore *core = (RCore*) core_ptr;
|
|
|
|
switch (cmd[0]) {
|
2017-07-17 06:51:30 +00:00
|
|
|
case '?': // Stop reason
|
|
|
|
if (!out_buf) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// dbg->reason.signum and dbg->reason.tid are not correct for native
|
|
|
|
// debugger. This is a hack
|
|
|
|
switch (core->dbg->reason.type) {
|
|
|
|
case R_DEBUG_REASON_BREAKPOINT:
|
|
|
|
case R_DEBUG_REASON_STEP:
|
|
|
|
case R_DEBUG_REASON_TRAP:
|
2017-07-21 18:06:41 +00:00
|
|
|
default: // remove when possible
|
|
|
|
return snprintf (out_buf, max_len - 1, "T05thread:%x;",
|
2017-07-17 06:51:30 +00:00
|
|
|
core->dbg->tid);
|
|
|
|
}
|
|
|
|
// Fallback for when it's fixed
|
2017-07-21 18:06:41 +00:00
|
|
|
/*
|
2017-07-17 06:51:30 +00:00
|
|
|
return snprintf (out_buf, max_len - 1, "T%02xthread:%x;",
|
|
|
|
core->dbg->reason.type, core->dbg->reason.tid);
|
2017-07-21 18:06:41 +00:00
|
|
|
*/
|
2017-05-29 19:36:42 +00:00
|
|
|
case 'd':
|
|
|
|
switch (cmd[1]) {
|
2017-06-14 06:48:46 +00:00
|
|
|
case 'm': // dm
|
|
|
|
if (snprintf (out_buf, max_len - 1, "%"PFMT64x, r_debug_get_baddr (core->dbg, NULL)) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
2017-05-29 19:36:42 +00:00
|
|
|
case 'p': // dp
|
|
|
|
switch (cmd[2]) {
|
|
|
|
case '\0': // dp
|
|
|
|
// TODO support multiprocess
|
|
|
|
snprintf (out_buf, max_len - 1, "QC%x", core->dbg->tid);
|
|
|
|
return 0;
|
2017-06-04 20:22:01 +00:00
|
|
|
case 't':
|
|
|
|
switch (cmd[3]) {
|
|
|
|
case '\0': // dpt
|
|
|
|
if (!core->dbg->h->threads) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!(list = core->dbg->h->threads(core->dbg, core->dbg->pid))) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
memset (out_buf, 0, max_len);
|
|
|
|
out_buf[0] = 'm';
|
|
|
|
ret = 1;
|
|
|
|
r_list_foreach (list, iter, dbgpid) {
|
|
|
|
// Max length of a hex pid = 8?
|
|
|
|
if (ret >= max_len - 9) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
snprintf (out_buf + ret, max_len - ret - 1, "%x,", dbgpid->pid);
|
|
|
|
ret = strlen (out_buf);
|
|
|
|
}
|
|
|
|
if (ret > 1) {
|
|
|
|
ret--;
|
|
|
|
out_buf[ret] = '\0';
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
case 'r': // dptr -> return current tid as int
|
|
|
|
return core->dbg->tid;
|
|
|
|
default:
|
|
|
|
return r_core_cmd (core, cmd, 0);
|
|
|
|
}
|
2017-05-29 19:36:42 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'r': // dr
|
2017-07-17 06:51:30 +00:00
|
|
|
r_debug_reg_sync (core->dbg, R_REG_TYPE_ALL, false);
|
2017-06-14 11:44:41 +00:00
|
|
|
be = r_config_get_i (core->config, "cfg.bigendian");
|
2018-11-06 12:40:00 +00:00
|
|
|
if (isspace ((ut8)cmd[2])) { // dr reg
|
2017-07-17 06:51:30 +00:00
|
|
|
const char *name, *val_ptr;
|
|
|
|
char new_cmd[128] = { 0 };
|
|
|
|
int off = 0;
|
|
|
|
name = cmd + 3;
|
|
|
|
// Temporarily using new_cmd to store reg name
|
|
|
|
if ((val_ptr = strchr (name, '='))) {
|
2017-07-21 17:03:20 +00:00
|
|
|
strncpy (new_cmd, name, R_MIN (val_ptr - name, sizeof (new_cmd) - 1));
|
2017-07-17 06:51:30 +00:00
|
|
|
} else {
|
2017-07-19 14:45:27 +00:00
|
|
|
strncpy (new_cmd, name, sizeof (new_cmd) - 1);
|
2017-07-17 06:51:30 +00:00
|
|
|
}
|
|
|
|
if (!(r = r_reg_get (core->dbg->reg, new_cmd, -1))) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (val_ptr) { // dr reg=val
|
|
|
|
val_ptr++;
|
|
|
|
off = val_ptr - cmd;
|
|
|
|
if (be) {
|
|
|
|
// We don't need to swap
|
|
|
|
r_core_cmd (core, cmd, 0);
|
|
|
|
}
|
|
|
|
// Previous contents are overwritten, since len(name) < off
|
|
|
|
strncpy (new_cmd, cmd, off);
|
|
|
|
if (r->size <= 64) {
|
|
|
|
reg_val = strtoll (val_ptr, NULL, 16);
|
|
|
|
if (write_reg_val (new_cmd + off, sizeof (new_cmd) - off - 1,
|
|
|
|
reg_val, r->size / 8, be) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return r_core_cmd (core, new_cmd, 0);
|
|
|
|
}
|
|
|
|
// Big registers
|
|
|
|
if (swap_big_regs (new_cmd + off, sizeof (new_cmd) - off - 1,
|
|
|
|
val_ptr, r->size / 8) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return r_core_cmd (core, new_cmd, 0);
|
|
|
|
}
|
|
|
|
if (r->size <= 64) {
|
|
|
|
reg_val = r_reg_get_value (core->dbg->reg, r);
|
|
|
|
return write_reg_val (out_buf, max_len - 1,
|
|
|
|
reg_val, r->size / 8, be);
|
|
|
|
}
|
|
|
|
r_reg_get_value_big (core->dbg->reg,
|
|
|
|
r, &val_big);
|
|
|
|
return write_big_reg (out_buf, max_len - 1,
|
|
|
|
&val_big, r->size / 8, be);
|
|
|
|
}
|
|
|
|
// dr - Print all registers
|
2017-05-29 19:36:42 +00:00
|
|
|
ret = 0;
|
2017-06-14 11:44:41 +00:00
|
|
|
if (!(gdb_reg = g->registers)) {
|
2017-05-29 19:36:42 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2017-06-14 11:44:41 +00:00
|
|
|
while (*gdb_reg->name) {
|
|
|
|
if (ret + gdb_reg->size * 2 >= max_len - 1) {
|
|
|
|
return -1;
|
2017-05-29 19:36:42 +00:00
|
|
|
}
|
2017-06-14 11:44:41 +00:00
|
|
|
if (gdb_reg->size <= 8) {
|
|
|
|
reg_val = r_reg_getv (core->dbg->reg, gdb_reg->name);
|
2017-07-17 06:51:30 +00:00
|
|
|
if (write_reg_val (out_buf + ret,
|
|
|
|
gdb_reg->size * 2 + 1,
|
|
|
|
reg_val, gdb_reg->size, be) < 0) {
|
|
|
|
return -1;
|
2017-05-29 19:36:42 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-06-14 11:44:41 +00:00
|
|
|
r_reg_get_value_big (core->dbg->reg,
|
|
|
|
r_reg_get (core->dbg->reg, gdb_reg->name, -1),
|
|
|
|
&val_big);
|
2017-07-17 06:51:30 +00:00
|
|
|
if (write_big_reg (out_buf + ret, gdb_reg->size * 2 + 1,
|
|
|
|
&val_big, gdb_reg->size, be) < 0) {
|
|
|
|
return -1;
|
2017-05-29 19:36:42 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-14 11:44:41 +00:00
|
|
|
ret += gdb_reg->size * 2;
|
|
|
|
gdb_reg++;
|
2017-05-29 19:36:42 +00:00
|
|
|
}
|
2017-06-14 11:44:41 +00:00
|
|
|
out_buf[ret] = '\0';
|
2017-05-29 19:36:42 +00:00
|
|
|
return ret;
|
2017-06-04 20:22:01 +00:00
|
|
|
default:
|
|
|
|
return r_core_cmd (core, cmd, 0);
|
2017-05-29 19:36:42 +00:00
|
|
|
}
|
|
|
|
break;
|
2017-07-21 18:06:41 +00:00
|
|
|
case 'i':
|
|
|
|
switch (cmd[1]) {
|
|
|
|
case 'f':
|
|
|
|
{
|
|
|
|
ut64 off, len, sz, namelen;
|
2017-08-22 07:42:16 +00:00
|
|
|
RIODesc *desc = core && core->file ? r_io_desc_get (core->io, core->file->fd) : NULL;
|
2017-07-21 18:06:41 +00:00
|
|
|
if (sscanf (cmd + 2, "%"PFMT64x",%"PFMT64x, &off, &len) != 2) {
|
|
|
|
strcpy (out_buf, "E00");
|
|
|
|
return 0;
|
|
|
|
}
|
2017-08-22 07:42:16 +00:00
|
|
|
namelen = desc ? strlen (desc->name) : 0;
|
2017-07-21 18:06:41 +00:00
|
|
|
if (off >= namelen) {
|
|
|
|
out_buf[0] = 'l';
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
sz = R_MIN (max_len, len + 2);
|
2017-08-22 07:42:16 +00:00
|
|
|
len = snprintf (out_buf, sz, "l%s", desc ? (desc->name + off) : "");
|
2017-07-21 18:06:41 +00:00
|
|
|
if (len >= sz) {
|
|
|
|
// There's more left
|
|
|
|
out_buf[0] = 'm';
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2017-05-29 19:36:42 +00:00
|
|
|
case 'm':
|
2017-06-29 15:17:22 +00:00
|
|
|
sscanf (cmd + 1, "%"PFMT64x",%x", &m_off, &ret);
|
2017-10-08 10:04:12 +00:00
|
|
|
if (r_io_read_at (core->io, m_off, (ut8*) out_buf, ret)) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return -1;
|
2017-06-29 15:17:22 +00:00
|
|
|
default:
|
|
|
|
return r_core_cmd (core, cmd, 0);
|
2017-05-29 19:36:42 +00:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-05-22 17:37:48 +00:00
|
|
|
// path = "<port> <file_name>"
|
|
|
|
static int r_core_rtr_gdb_run(RCore *core, int launch, const char *path) {
|
|
|
|
RSocket *sock;
|
2017-05-29 19:36:42 +00:00
|
|
|
int p, ret;
|
2017-07-16 10:01:17 +00:00
|
|
|
bool debug_msg = false;
|
2017-05-22 17:37:48 +00:00
|
|
|
char port[10];
|
2017-06-14 06:48:46 +00:00
|
|
|
char *file = NULL, *args = NULL;
|
2017-05-22 17:37:48 +00:00
|
|
|
libgdbr_t *g;
|
|
|
|
|
2017-05-23 09:24:04 +00:00
|
|
|
if (!core || !path) {
|
|
|
|
return -1;
|
|
|
|
}
|
2017-07-16 10:01:17 +00:00
|
|
|
if (*path == '!') {
|
|
|
|
debug_msg = true;
|
|
|
|
path++;
|
|
|
|
}
|
2018-01-08 02:29:29 +00:00
|
|
|
if (!(path = r_str_trim_ro (path)) || !*path) {
|
2017-05-22 17:37:48 +00:00
|
|
|
eprintf ("gdbserver: Port not specified\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2017-07-26 18:20:07 +00:00
|
|
|
if (!(p = atoi (path)) || p < 0 || p > 65535) {
|
|
|
|
eprintf ("gdbserver: Invalid port: %s\n", port);
|
|
|
|
return -1;
|
2017-05-22 17:37:48 +00:00
|
|
|
}
|
2017-07-26 18:20:07 +00:00
|
|
|
snprintf (port, sizeof (port) - 1, "%d", p);
|
|
|
|
if (!(file = strchr (path, ' '))) {
|
|
|
|
eprintf ("gdbserver: File not specified\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2018-01-08 02:29:29 +00:00
|
|
|
if (!(file = (char *)r_str_trim_ro (file)) || !*file) {
|
2017-07-26 18:20:07 +00:00
|
|
|
eprintf ("gdbserver: File not specified\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
args = strchr (file, ' ');
|
|
|
|
if (args) {
|
|
|
|
*args++ = '\0';
|
2018-01-08 02:29:29 +00:00
|
|
|
if (!(args = (char *)r_str_trim_ro (args))) {
|
2017-06-17 23:11:11 +00:00
|
|
|
args = "";
|
2017-06-14 06:48:46 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
args = "";
|
|
|
|
}
|
2017-05-22 17:37:48 +00:00
|
|
|
|
2018-09-21 00:16:54 +00:00
|
|
|
if (!r_core_file_open (core, file, R_PERM_R, 0)) {
|
2017-05-22 17:37:48 +00:00
|
|
|
eprintf ("Cannot open file (%s)\n", file);
|
|
|
|
return -1;
|
|
|
|
}
|
2017-06-14 06:48:46 +00:00
|
|
|
r_core_file_reopen_debug (core, args);
|
2017-05-22 17:37:48 +00:00
|
|
|
|
|
|
|
if (!(sock = r_socket_new (false))) {
|
|
|
|
eprintf ("gdbserver: Could not open socket for listening\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!r_socket_listen (sock, port, NULL)) {
|
|
|
|
r_socket_free (sock);
|
|
|
|
eprintf ("gdbserver: Cannot listen on port: %s\n", port);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!(g = R_NEW0(libgdbr_t))) {
|
|
|
|
r_socket_free (sock);
|
|
|
|
eprintf ("gdbserver: Cannot alloc libgdbr instance\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2017-06-11 00:12:44 +00:00
|
|
|
gdbr_init (g, true);
|
2017-07-16 10:01:17 +00:00
|
|
|
g->server_debug = debug_msg;
|
2017-06-14 11:44:41 +00:00
|
|
|
gdbr_set_architecture (g, r_config_get (core->config, "asm.arch"), r_config_get_i (core->config, "asm.bits"));
|
2017-05-22 17:37:48 +00:00
|
|
|
core->gdbserver_up = 1;
|
|
|
|
eprintf ("gdbserver started on port: %s, file: %s\n", port, file);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (!(g->sock = r_socket_accept (sock))) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
g->connected = 1;
|
2017-05-29 19:36:42 +00:00
|
|
|
ret = gdbr_server_serve (g, r_core_rtr_gdb_cb, (void*) core);
|
|
|
|
r_socket_close (g->sock);
|
2017-05-22 17:37:48 +00:00
|
|
|
g->connected = 0;
|
2017-05-29 19:36:42 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
break;
|
|
|
|
}
|
2017-05-22 17:37:48 +00:00
|
|
|
}
|
|
|
|
core->gdbserver_up = 0;
|
|
|
|
gdbr_cleanup (g);
|
|
|
|
free (g);
|
|
|
|
r_socket_free (sock);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API int r_core_rtr_gdb(RCore *core, int launch, const char *path) {
|
|
|
|
int ret;
|
|
|
|
if (r_sandbox_enable (0)) {
|
|
|
|
eprintf ("sandbox: connect disabled\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// TODO: do stuff with launch
|
|
|
|
if (core->gdbserver_up) {
|
|
|
|
eprintf ("gdbserver is already running\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ret = r_core_rtr_gdb_run (core, launch, path);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-08-24 09:58:09 +00:00
|
|
|
R_API void r_core_rtr_pushout(RCore *core, const char *input) {
|
2011-09-03 03:09:31 +00:00
|
|
|
int fd = atoi (input);
|
2010-08-24 09:58:09 +00:00
|
|
|
const char *cmd = NULL;
|
|
|
|
char *str = NULL;
|
|
|
|
if (fd) {
|
2017-09-15 14:08:32 +00:00
|
|
|
for (rtr_n = 0; rtr_host[rtr_n].fd && rtr_n < RTR_MAX_HOSTS - 1; rtr_n++) {
|
|
|
|
if (rtr_host[rtr_n].fd->fd != fd) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2010-08-24 09:58:09 +00:00
|
|
|
if (!(cmd = strchr (input, ' '))) {
|
|
|
|
eprintf ("Error\n");
|
|
|
|
return;
|
|
|
|
}
|
2017-09-13 13:19:38 +00:00
|
|
|
} else {
|
|
|
|
cmd = input;
|
|
|
|
}
|
2010-08-24 09:58:09 +00:00
|
|
|
|
2018-12-20 10:50:29 +00:00
|
|
|
if (!rtr_host[rtr_n].fd || !rtr_host[rtr_n].fd->fd) {
|
2019-01-09 01:47:35 +00:00
|
|
|
eprintf ("Error: Unknown host\n");
|
2010-08-24 09:58:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(str = r_core_cmd_str (core, cmd))) {
|
|
|
|
eprintf ("Error: radare_cmd_str returned NULL\n");
|
|
|
|
return;
|
|
|
|
}
|
2012-09-06 01:12:54 +00:00
|
|
|
|
2010-08-24 09:58:09 +00:00
|
|
|
switch (rtr_host[rtr_n].proto) {
|
2019-01-09 01:47:35 +00:00
|
|
|
case RTR_PROTOCOL_RAP:
|
2010-08-24 09:58:09 +00:00
|
|
|
eprintf ("Error: Cannot use '=<' to a rap connection.\n");
|
|
|
|
break;
|
2019-01-09 01:47:35 +00:00
|
|
|
case RTR_PROTOCOL_HTTP:
|
|
|
|
eprintf ("TODO\n");
|
|
|
|
break;
|
|
|
|
case RTR_PROTOCOL_TCP:
|
|
|
|
case RTR_PROTOCOL_UDP:
|
2010-08-24 09:58:09 +00:00
|
|
|
r_socket_write (rtr_host[rtr_n].fd, str, strlen (str));
|
|
|
|
break;
|
2019-01-09 01:47:35 +00:00
|
|
|
default:
|
|
|
|
eprintf ("Unknown protocol\n");
|
|
|
|
break;
|
2010-08-24 09:58:09 +00:00
|
|
|
}
|
|
|
|
free (str);
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API void r_core_rtr_list(RCore *core) {
|
|
|
|
int i;
|
2016-05-04 21:32:29 +00:00
|
|
|
for (i = 0; i < RTR_MAX_HOSTS; i++) {
|
2018-09-13 08:17:26 +00:00
|
|
|
if (!rtr_host[i].fd) {
|
2016-05-04 21:32:29 +00:00
|
|
|
continue;
|
2018-09-13 08:17:26 +00:00
|
|
|
}
|
2019-01-09 01:47:35 +00:00
|
|
|
const char *proto = "rap";
|
2016-05-04 21:32:29 +00:00
|
|
|
switch (rtr_host[i].proto) {
|
2019-01-09 01:47:35 +00:00
|
|
|
case RTR_PROTOCOL_HTTP: proto = "http"; break;
|
|
|
|
case RTR_PROTOCOL_TCP: proto = "tcp"; break;
|
|
|
|
case RTR_PROTOCOL_UDP: proto = "udp"; break;
|
2019-05-06 09:31:32 +00:00
|
|
|
case RTR_PROTOCOL_RAP: proto = "r2p"; break;
|
2016-05-04 21:32:29 +00:00
|
|
|
}
|
2019-01-09 01:47:35 +00:00
|
|
|
r_cons_printf ("%i - %s://%s:%i/%s\n",
|
|
|
|
rtr_host[i].fd->fd, proto, rtr_host[i].host,
|
2016-05-04 21:32:29 +00:00
|
|
|
rtr_host[i].port, rtr_host[i].file);
|
2010-08-24 09:58:09 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-22 22:12:03 +00:00
|
|
|
|
2010-08-24 09:58:09 +00:00
|
|
|
R_API void r_core_rtr_add(RCore *core, const char *_input) {
|
2019-01-09 01:47:35 +00:00
|
|
|
char *port, input[1024], *file = NULL, *ptr = NULL, buf[1024];
|
|
|
|
int i, timeout, ret;
|
2011-03-21 00:47:17 +00:00
|
|
|
RSocket *fd;
|
2010-08-24 09:58:09 +00:00
|
|
|
|
2013-11-29 16:27:46 +00:00
|
|
|
timeout = r_config_get_i (core->config, "http.timeout");
|
2016-05-04 21:32:29 +00:00
|
|
|
strncpy (input, _input, sizeof (input) - 4);
|
2019-01-09 01:47:35 +00:00
|
|
|
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;
|
2019-05-06 09:31:32 +00:00
|
|
|
} uris[6] = {
|
2019-01-09 01:47:35 +00:00
|
|
|
{"tcp", RTR_PROTOCOL_TCP},
|
|
|
|
{"udp", RTR_PROTOCOL_UDP},
|
|
|
|
{"rap", RTR_PROTOCOL_RAP},
|
2019-05-06 09:31:32 +00:00
|
|
|
{"r2p", RTR_PROTOCOL_RAP},
|
2019-01-09 01:47:35 +00:00
|
|
|
{"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);
|
2010-08-24 09:58:09 +00:00
|
|
|
}
|
2019-01-09 01:47:35 +00:00
|
|
|
if (host) {
|
|
|
|
if (!(ptr = strchr (host, ':'))) {
|
|
|
|
ptr = host;
|
|
|
|
port = "80";
|
|
|
|
} else {
|
|
|
|
*ptr++ = '\0';
|
|
|
|
port = ptr;
|
|
|
|
}
|
2018-09-13 08:17:26 +00:00
|
|
|
}
|
2019-01-09 01:47:35 +00:00
|
|
|
file = strchr (ptr, '/');
|
|
|
|
if (file) {
|
|
|
|
*file = 0;
|
|
|
|
file = (char *)r_str_trim_ro (file + 1);
|
2013-03-17 23:38:04 +00:00
|
|
|
} else {
|
2019-01-09 01:47:35 +00:00
|
|
|
if (*host == ':' || strstr (host, "://:")) { // listen
|
|
|
|
// it's fine to listen without serving a file
|
|
|
|
} else {
|
|
|
|
eprintf ("Error: Missing '/'\n");
|
|
|
|
return;
|
|
|
|
}
|
2010-08-24 09:58:09 +00:00
|
|
|
}
|
|
|
|
|
2018-01-08 02:22:26 +00:00
|
|
|
port = r_str_trim (port);
|
2013-11-29 16:27:46 +00:00
|
|
|
if (r_sandbox_enable (0)) {
|
|
|
|
eprintf ("sandbox: connect disabled\n");
|
|
|
|
return;
|
|
|
|
}
|
2010-08-24 09:58:09 +00:00
|
|
|
|
2015-09-14 10:35:38 +00:00
|
|
|
fd = r_socket_new (false);
|
2011-05-05 01:41:57 +00:00
|
|
|
if (!fd) {
|
2011-09-03 03:09:31 +00:00
|
|
|
eprintf ("Error: Cannot create new socket\n");
|
2011-05-05 01:41:57 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-08-24 09:58:09 +00:00
|
|
|
switch (proto) {
|
2019-01-09 01:47:35 +00:00
|
|
|
case RTR_PROTOCOL_HTTP:
|
2013-02-06 09:35:23 +00:00
|
|
|
{
|
2016-05-04 21:32:29 +00:00
|
|
|
char prompt[64], prompt2[64], *str, *ptr;
|
|
|
|
int len, flen = strlen (file);
|
|
|
|
bool is_valid, is_visual;
|
2015-01-30 23:50:05 +00:00
|
|
|
const char* res;
|
2016-05-04 21:32:29 +00:00
|
|
|
|
|
|
|
is_visual = (file[flen - 1] == 'V');
|
|
|
|
is_valid = (file[flen - (is_visual? 2: 1)] == '/');
|
|
|
|
|
2014-10-06 23:58:42 +00:00
|
|
|
if (is_valid) {
|
2014-08-28 09:38:11 +00:00
|
|
|
TextLog T = { host, port, file };
|
2014-10-06 23:58:42 +00:00
|
|
|
if (is_visual) {
|
2016-05-04 21:32:29 +00:00
|
|
|
file[flen - 1] = 0; // remove V from url
|
2014-10-06 23:58:42 +00:00
|
|
|
rtr_visual (core, T, NULL);
|
|
|
|
}
|
2013-02-06 09:35:23 +00:00
|
|
|
snprintf (prompt, sizeof (prompt), "[http://%s:%s/%s]> ",
|
2014-10-06 23:58:42 +00:00
|
|
|
host, port, file);
|
2016-05-04 21:32:29 +00:00
|
|
|
snprintf (prompt2, sizeof (prompt2), "[%s:%s]$ ", host, port);
|
2014-10-06 23:58:42 +00:00
|
|
|
for (;;) {
|
|
|
|
r_line_set_prompt (prompt);
|
2015-01-30 23:50:05 +00:00
|
|
|
res = r_line_readline ();
|
2018-09-13 08:17:26 +00:00
|
|
|
if (!res || !*res) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (*res == 'q') {
|
|
|
|
break;
|
|
|
|
}
|
2015-01-30 23:50:05 +00:00
|
|
|
if (!strcmp (res, "!sh")) {
|
2015-01-27 16:03:06 +00:00
|
|
|
for (;;) {
|
|
|
|
r_line_set_prompt (prompt2);
|
|
|
|
res = r_line_readline ();
|
2015-01-29 11:16:37 +00:00
|
|
|
if (!res || !*res || !strcmp (res, "exit")) {
|
2015-01-27 16:06:01 +00:00
|
|
|
break;
|
2015-01-29 11:16:37 +00:00
|
|
|
}
|
2015-01-27 16:03:06 +00:00
|
|
|
ptr = r_str_uri_encode (res);
|
2016-05-04 21:32:29 +00:00
|
|
|
char *uri = r_str_newf ("http://%s:%s/%s!%s", host, port, file, res);
|
2015-01-27 16:03:06 +00:00
|
|
|
str = r_socket_http_get (uri, NULL, &len);
|
|
|
|
if (str) {
|
|
|
|
str[len] = 0;
|
|
|
|
res = strstr (str, "\n\n");
|
2018-09-13 08:17:26 +00:00
|
|
|
if (res) {
|
|
|
|
res = strstr (res + 1, "\n\n");
|
|
|
|
}
|
2016-05-04 21:32:29 +00:00
|
|
|
res = res? res + 2: str;
|
|
|
|
const char *tail = (res[strlen (res) - 1] == '\n')? "": "\n";
|
|
|
|
printf ("%s%s", res, tail);
|
2015-01-27 16:03:06 +00:00
|
|
|
r_line_hist_add (str);
|
|
|
|
free (str);
|
|
|
|
}
|
2015-01-29 11:16:37 +00:00
|
|
|
free (ptr);
|
2016-05-04 21:32:29 +00:00
|
|
|
free (uri);
|
2015-01-27 16:03:06 +00:00
|
|
|
}
|
2015-01-30 23:50:05 +00:00
|
|
|
} else if (res[0]=='V') {
|
|
|
|
if (res[1]==' ') {
|
|
|
|
rtr_visual (core, T, res+1);
|
2014-08-28 09:38:11 +00:00
|
|
|
} else {
|
|
|
|
rtr_visual (core, T, NULL);
|
|
|
|
}
|
2015-01-30 23:50:05 +00:00
|
|
|
} else if (!strcmp (res, "TT")) {
|
2014-08-28 02:41:33 +00:00
|
|
|
rtr_textlog_chat (core, T);
|
|
|
|
} else {
|
2015-01-30 23:50:05 +00:00
|
|
|
ptr = r_str_uri_encode (res);
|
2018-09-13 08:17:26 +00:00
|
|
|
if (ptr) {
|
|
|
|
res = ptr;
|
|
|
|
}
|
2016-05-04 21:32:29 +00:00
|
|
|
char *uri = r_str_newf ("http://%s:%s/%s%s", host, port, file, res);
|
2018-09-13 08:17:26 +00:00
|
|
|
if (ptr == res) {
|
|
|
|
free (ptr);
|
|
|
|
}
|
2014-10-06 23:58:42 +00:00
|
|
|
str = r_socket_http_get (uri, NULL, &len);
|
2017-09-11 22:28:51 +00:00
|
|
|
if (str && len > 0) {
|
2014-10-06 23:58:42 +00:00
|
|
|
str[len] = 0;
|
|
|
|
res = strstr (str, "\n\n");
|
2018-09-13 08:17:26 +00:00
|
|
|
if (res) {
|
|
|
|
res = strstr (res + 1, "\n\n");
|
|
|
|
}
|
|
|
|
if (res) {
|
|
|
|
res += 2;
|
|
|
|
} else {
|
|
|
|
res = str;
|
|
|
|
}
|
2017-09-11 22:28:51 +00:00
|
|
|
printf ("%s%s", res, (*res && res[strlen (res)-1] == '\n') ? "" : "\n");
|
2014-10-06 23:58:42 +00:00
|
|
|
r_line_hist_add (str);
|
|
|
|
}
|
2017-09-11 22:28:51 +00:00
|
|
|
free (str);
|
2016-05-04 21:32:29 +00:00
|
|
|
free (uri);
|
2013-03-17 23:38:04 +00:00
|
|
|
}
|
2013-02-06 09:35:23 +00:00
|
|
|
}
|
2016-05-04 21:32:29 +00:00
|
|
|
} else {
|
|
|
|
char *http_uri = r_str_newf ("http://%s:%s/%s", host, port, file);
|
|
|
|
str = r_socket_http_get (http_uri, NULL, &len);
|
|
|
|
if (str) {
|
|
|
|
str[len] = 0;
|
|
|
|
res = strstr (str, "\n\n");
|
2018-09-13 08:17:26 +00:00
|
|
|
if (res) {
|
|
|
|
res = strstr (res + 1, "\n\n");
|
|
|
|
}
|
2016-05-04 21:32:29 +00:00
|
|
|
printf ("%s", res? res + 2: str);
|
|
|
|
free (str);
|
2018-09-13 08:17:26 +00:00
|
|
|
} else {
|
|
|
|
eprintf ("HTTP connection has failed\n");
|
|
|
|
}
|
2016-05-04 21:32:29 +00:00
|
|
|
free (http_uri);
|
2013-02-06 09:35:23 +00:00
|
|
|
}
|
2016-05-04 21:32:29 +00:00
|
|
|
r_socket_free (fd);
|
2013-02-06 09:35:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
2019-01-09 01:47:35 +00:00
|
|
|
case RTR_PROTOCOL_RAP:
|
2013-11-29 16:27:46 +00:00
|
|
|
if (!r_socket_connect_tcp (fd, host, port, timeout)) { //TODO: Use rap.ssl
|
2011-09-03 03:09:31 +00:00
|
|
|
eprintf ("Error: Cannot connect to '%s' (%s)\n", host, port);
|
2016-05-04 21:32:29 +00:00
|
|
|
r_socket_free (fd);
|
2010-08-24 09:58:09 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-11-30 01:47:29 +00:00
|
|
|
eprintf ("Connected to %s at port %s\n", host, port);
|
2010-08-24 09:58:09 +00:00
|
|
|
/* send */
|
2016-10-20 13:02:25 +00:00
|
|
|
buf[0] = RTR_RAP_OPEN;
|
|
|
|
buf[1] = 0;
|
|
|
|
buf[2] = (ut8)(strlen (file) + 1);
|
|
|
|
memcpy (buf + 3, file, buf[2]);
|
|
|
|
r_socket_write (fd, buf, 3 + buf[2]);
|
|
|
|
/* read */
|
|
|
|
eprintf ("waiting... ");
|
|
|
|
fflush (stdout);
|
|
|
|
r_socket_read (fd, (ut8*)buf, 5);
|
|
|
|
i = r_read_at_be32 (buf, 1);
|
|
|
|
if (buf[0] != (char)(RTR_RAP_OPEN | RTR_RAP_REPLY) || i <= 0) {
|
|
|
|
eprintf ("Error: Wrong reply\n");
|
|
|
|
r_socket_free (fd);
|
|
|
|
return;
|
2010-08-24 09:58:09 +00:00
|
|
|
}
|
|
|
|
eprintf ("ok\n");
|
|
|
|
break;
|
2019-01-09 01:47:35 +00:00
|
|
|
case RTR_PROTOCOL_TCP:
|
2013-11-29 16:27:46 +00:00
|
|
|
if (!r_socket_connect_tcp (fd, host, port, timeout)) { //TODO: Use rap.ssl
|
|
|
|
core->num->value = 1;
|
2016-05-04 21:32:29 +00:00
|
|
|
eprintf ("Error: Cannot connect to '%s' (%s)\n", host, port);
|
|
|
|
r_socket_free (fd);
|
2010-08-24 09:58:09 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-11-29 16:27:46 +00:00
|
|
|
core->num->value = 0;
|
2011-03-27 15:44:27 +00:00
|
|
|
eprintf ("Connected to: %s at port %s\n", host, port);
|
2010-08-24 09:58:09 +00:00
|
|
|
break;
|
2019-01-09 01:47:35 +00:00
|
|
|
case RTR_PROTOCOL_UDP:
|
2013-11-29 16:27:46 +00:00
|
|
|
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);
|
2016-05-04 21:32:29 +00:00
|
|
|
r_socket_free (fd);
|
2010-08-24 09:58:09 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-11-29 16:27:46 +00:00
|
|
|
core->num->value = 0;
|
2016-05-04 21:32:29 +00:00
|
|
|
eprintf ("Connected to: %s at port %s\n", host, port);
|
2010-08-24 09:58:09 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-11-30 01:47:29 +00:00
|
|
|
ret = core->num->value;
|
2016-05-04 21:32:29 +00:00
|
|
|
for (i = 0; i < RTR_MAX_HOSTS; i++) {
|
2016-10-09 21:56:52 +00:00
|
|
|
if (rtr_host[i].fd) {
|
2016-05-04 21:32:29 +00:00
|
|
|
continue;
|
2016-10-09 21:56:52 +00:00
|
|
|
}
|
2016-05-04 21:32:29 +00:00
|
|
|
rtr_host[i].proto = proto;
|
2018-05-28 09:35:50 +00:00
|
|
|
strncpy (rtr_host[i].host, host, sizeof (rtr_host[i].host)-1);
|
2016-05-04 21:32:29 +00:00
|
|
|
rtr_host[i].port = r_num_get (core->num, port);
|
|
|
|
strncpy (rtr_host[i].file, file, sizeof (rtr_host[i].file)-1);
|
|
|
|
rtr_host[i].fd = fd;
|
|
|
|
rtr_n = i;
|
|
|
|
break;
|
|
|
|
}
|
2013-11-30 01:47:29 +00:00
|
|
|
core->num->value = ret;
|
2015-05-12 21:47:26 +00:00
|
|
|
// double free wtf is freed this here? r_socket_free(fd);
|
2013-02-06 09:35:23 +00:00
|
|
|
//r_core_rtr_list (core);
|
2010-08-24 09:58:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
R_API void r_core_rtr_remove(RCore *core, const char *input) {
|
|
|
|
int fd, i;
|
|
|
|
|
2019-05-06 09:31:32 +00:00
|
|
|
if (IS_DIGIT (input[0])) {
|
2010-08-24 09:58:09 +00:00
|
|
|
fd = r_num_math (core->num, input);
|
2018-09-13 08:17:26 +00:00
|
|
|
for (i = 0; i < RTR_MAX_HOSTS; i++) {
|
2015-04-01 00:05:43 +00:00
|
|
|
if (rtr_host[i].fd && rtr_host[i].fd->fd == fd) {
|
2011-03-21 00:47:17 +00:00
|
|
|
r_socket_free (rtr_host[i].fd);
|
|
|
|
rtr_host[i].fd = NULL;
|
2016-10-09 21:56:52 +00:00
|
|
|
if (rtr_n == i) {
|
2018-09-13 08:17:26 +00:00
|
|
|
for (rtr_n = 0; !rtr_host[rtr_n].fd && rtr_n < RTR_MAX_HOSTS - 1; rtr_n++) {
|
|
|
|
;
|
|
|
|
}
|
2016-10-09 21:56:52 +00:00
|
|
|
}
|
2010-08-24 09:58:09 +00:00
|
|
|
break;
|
2018-09-13 08:17:26 +00:00
|
|
|
}
|
2010-08-24 09:58:09 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-05-12 21:47:26 +00:00
|
|
|
for (i = 0; i < RTR_MAX_HOSTS; i++) {
|
|
|
|
if (rtr_host[i].fd) {
|
2011-03-21 00:47:17 +00:00
|
|
|
r_socket_free (rtr_host[i].fd);
|
2015-05-12 21:47:26 +00:00
|
|
|
rtr_host[i].fd = NULL;
|
|
|
|
}
|
|
|
|
}
|
2016-05-04 21:32:29 +00:00
|
|
|
memset (rtr_host, '\0', RTR_MAX_HOSTS * sizeof (RCoreRtrHost));
|
2010-08-24 09:58:09 +00:00
|
|
|
rtr_n = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API void r_core_rtr_session(RCore *core, const char *input) {
|
2016-05-04 21:32:29 +00:00
|
|
|
char prompt[64], buf[1024];
|
2010-08-24 09:58:09 +00:00
|
|
|
int fd;
|
|
|
|
|
2014-01-18 22:02:53 +00:00
|
|
|
prompt[0] = 0;
|
2017-12-18 12:27:26 +00:00
|
|
|
if (IS_DIGIT (input[0])) {
|
2010-08-24 09:58:09 +00:00
|
|
|
fd = r_num_math (core->num, input);
|
2018-09-13 08:17:26 +00:00
|
|
|
for (rtr_n = 0; rtr_host[rtr_n].fd && rtr_host[rtr_n].fd->fd != fd && rtr_n < RTR_MAX_HOSTS - 1; rtr_n++) {
|
|
|
|
;
|
|
|
|
}
|
2010-08-24 09:58:09 +00:00
|
|
|
}
|
|
|
|
|
2017-12-18 12:27:26 +00:00
|
|
|
while (!r_cons_is_breaked ()) {
|
|
|
|
if (rtr_host[rtr_n].fd) {
|
2011-09-03 03:09:31 +00:00
|
|
|
snprintf (prompt, sizeof (prompt),
|
2017-12-18 23:17:43 +00:00
|
|
|
"fd:%d> ", (int)(size_t)rtr_host[rtr_n].fd->fd);
|
2017-12-18 12:27:26 +00:00
|
|
|
}
|
2012-08-19 01:28:17 +00:00
|
|
|
free (r_line_singleton ()->prompt);
|
|
|
|
r_line_singleton ()->prompt = strdup (prompt);
|
2016-10-09 21:56:52 +00:00
|
|
|
if (r_cons_fgets (buf, sizeof (buf), 0, NULL) < 1) {
|
2016-05-04 21:32:29 +00:00
|
|
|
break;
|
2016-10-09 21:56:52 +00:00
|
|
|
}
|
|
|
|
if (!*buf || *buf == 'q') {
|
2016-05-04 21:32:29 +00:00
|
|
|
break;
|
2016-10-09 21:56:52 +00:00
|
|
|
}
|
2016-05-04 21:32:29 +00:00
|
|
|
if (*buf == 'V') {
|
|
|
|
eprintf ("Visual mode not supported\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
r_core_rtr_cmd (core, buf);
|
|
|
|
r_cons_flush ();
|
2010-08-24 09:58:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-04 23:20:23 +00:00
|
|
|
static ut8 *r_rap_packet(ut8 type, ut32 len) {
|
|
|
|
ut8 *buf = malloc (len + 5);
|
|
|
|
if (buf) {
|
|
|
|
buf[0] = type;
|
|
|
|
r_write_be32 (buf + 1, len);
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void r_rap_packet_fill(ut8 *buf, const ut8* src, int len) {
|
|
|
|
if (buf && src && len > 0) {
|
|
|
|
ut32 curlen = r_read_be32 (buf + 1);
|
|
|
|
memcpy (buf + 5, src, R_MIN (curlen, len));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-18 12:27:26 +00:00
|
|
|
static bool r_core_rtr_rap_run(RCore *core, const char *input) {
|
|
|
|
char *file = r_str_newf ("rap://%s", input);
|
2018-09-21 00:16:54 +00:00
|
|
|
int flags = R_PERM_RW;
|
2017-12-18 12:27:26 +00:00
|
|
|
RIODesc *fd = r_io_open_nomap (core->io, file, flags, 0644);
|
|
|
|
if (fd) {
|
|
|
|
if (r_io_is_listener (core->io)) {
|
|
|
|
if (!r_core_serve (core, fd)) {
|
2019-05-16 12:18:16 +00:00
|
|
|
r_cons_singleton ()->context->breaked = true;
|
2017-12-18 12:27:26 +00:00
|
|
|
}
|
2019-05-16 12:18:16 +00:00
|
|
|
r_io_desc_close (fd);
|
|
|
|
// avoid doble free, we are not the owners of this fd so we cant destroy it
|
|
|
|
//r_io_desc_free (fd);
|
2017-12-18 12:27:26 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-06-23 10:10:13 +00:00
|
|
|
r_cons_singleton ()->context->breaked = true;
|
2017-12-18 12:27:26 +00:00
|
|
|
}
|
2018-06-23 10:10:13 +00:00
|
|
|
return !r_cons_singleton ()->context->breaked;
|
2017-12-18 12:27:26 +00:00
|
|
|
// r_core_cmdf (core, "o rap://%s", input);
|
2016-07-29 17:32:27 +00:00
|
|
|
}
|
|
|
|
|
2018-08-23 11:49:28 +00:00
|
|
|
static RThreadFunctionRet r_core_rtr_rap_thread (RThread *th) {
|
2016-07-29 17:32:27 +00:00
|
|
|
if (!th) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
RapThread *rt = th->user;
|
|
|
|
if (!rt || !rt->core) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-23 11:49:28 +00:00
|
|
|
return r_core_rtr_rap_run (rt->core, rt->input) ? R_TH_REPEAT : R_TH_STOP;
|
2016-07-29 17:32:27 +00:00
|
|
|
}
|
|
|
|
|
2010-08-24 09:58:09 +00:00
|
|
|
R_API void r_core_rtr_cmd(RCore *core, const char *input) {
|
2016-05-04 23:20:23 +00:00
|
|
|
char bufw[1024], bufr[8], *cmd_output = NULL;
|
|
|
|
const char *cmd = NULL;
|
2019-05-06 18:36:41 +00:00
|
|
|
unsigned int cmd_len = 0;
|
2016-12-21 21:13:16 +00:00
|
|
|
int i, fd = atoi (input);
|
2010-08-24 09:58:09 +00:00
|
|
|
|
2016-10-09 21:56:52 +00:00
|
|
|
// "=:"
|
|
|
|
if (*input == ':' && !strchr (input + 1, ':')) {
|
2019-01-09 01:47:35 +00:00
|
|
|
void *bed = r_cons_sleep_begin ();
|
2016-07-29 17:32:27 +00:00
|
|
|
r_core_rtr_rap_run (core, input);
|
2019-01-09 01:47:35 +00:00
|
|
|
r_cons_sleep_end (bed);
|
2012-12-30 22:40:17 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-07-29 17:32:27 +00:00
|
|
|
|
2019-01-09 01:47:35 +00:00
|
|
|
if (*input == '&') { // "=h&" "=&:9090"
|
2016-07-29 17:32:27 +00:00
|
|
|
if (rapthread) {
|
|
|
|
eprintf ("RAP Thread is already running\n");
|
|
|
|
eprintf ("This is experimental and probably buggy. Use at your own risk\n");
|
|
|
|
} else {
|
2019-01-09 01:47:35 +00:00
|
|
|
// TODO: use tasks
|
2017-12-18 12:27:26 +00:00
|
|
|
RapThread *RT = R_NEW0 (RapThread);
|
|
|
|
if (RT) {
|
|
|
|
RT->core = core;
|
|
|
|
RT->input = strdup (input + 1);
|
|
|
|
//RapThread rt = { core, strdup (input + 1) };
|
|
|
|
rapthread = r_th_new (r_core_rtr_rap_thread, RT, false);
|
2019-03-13 09:47:57 +00:00
|
|
|
r_th_setname (rapthread, "rapthread");
|
2017-12-18 12:27:26 +00:00
|
|
|
r_th_start (rapthread, true);
|
|
|
|
eprintf ("Background rap server started.\n");
|
|
|
|
}
|
2016-07-29 17:32:27 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-08-24 09:58:09 +00:00
|
|
|
if (fd != 0) {
|
2016-05-04 23:20:23 +00:00
|
|
|
RSocket *fh = rtr_host[rtr_n].fd;
|
|
|
|
for (rtr_n = 0; fh && fh->fd != fd && rtr_n < RTR_MAX_HOSTS - 1; rtr_n++) {
|
|
|
|
/* do nothing */
|
|
|
|
}
|
2010-08-24 09:58:09 +00:00
|
|
|
if (!(cmd = strchr (input, ' '))) {
|
2011-09-03 03:09:31 +00:00
|
|
|
eprintf ("Error\n");
|
2010-08-24 09:58:09 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-05-04 21:32:29 +00:00
|
|
|
} else {
|
|
|
|
cmd = input;
|
|
|
|
}
|
2010-08-24 09:58:09 +00:00
|
|
|
|
|
|
|
if (!rtr_host[rtr_n].fd){
|
|
|
|
eprintf ("Error: Unknown host\n");
|
2013-11-29 16:27:46 +00:00
|
|
|
core->num->value = 1; // fail
|
2010-08-24 09:58:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-06 09:31:32 +00:00
|
|
|
if (rtr_host[rtr_n].proto == RTR_PROTOCOL_TCP) {
|
|
|
|
RSocket *fh = rtr_host[rtr_n].fd;
|
|
|
|
if (cmd_len < 1 || cmd_len > 16384) {
|
|
|
|
eprintf ("Error: cmd_len is wrong\n");
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
cmd_output = calloc (1, cmd_len + 1);
|
|
|
|
if (!cmd_output) {
|
|
|
|
eprintf ("Error: Allocating cmd output\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
r_socket_read_block (fh, (ut8*)cmd_output, cmd_len);
|
|
|
|
//ensure the termination
|
|
|
|
cmd_output[cmd_len] = 0;
|
|
|
|
r_cons_println (cmd_output);
|
|
|
|
free ((void *)cmd_output);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-09 01:47:35 +00:00
|
|
|
if (rtr_host[rtr_n].proto != RTR_PROTOCOL_RAP) {
|
2010-08-24 09:58:09 +00:00
|
|
|
eprintf ("Error: Not a rap:// host\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-29 16:27:46 +00:00
|
|
|
core->num->value = 0; // that's fine
|
2019-05-06 09:31:32 +00:00
|
|
|
cmd = r_str_trim_ro (cmd);
|
2016-05-04 23:20:23 +00:00
|
|
|
RSocket *fh = rtr_host[rtr_n].fd;
|
2013-11-29 16:27:46 +00:00
|
|
|
if (!strlen (cmd)) {
|
|
|
|
// just check if we can connect
|
2016-05-04 23:20:23 +00:00
|
|
|
r_socket_close (fh);
|
2013-11-29 16:27:46 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-05-04 23:20:23 +00:00
|
|
|
/* send request */
|
2016-05-04 18:08:38 +00:00
|
|
|
bufw[0] = RAP_RMT_CMD;
|
2010-08-24 09:58:09 +00:00
|
|
|
i = strlen (cmd) + 1;
|
2016-05-04 18:08:38 +00:00
|
|
|
r_write_be32 (bufw + 1, i);
|
|
|
|
memcpy (bufw + 5, cmd, i);
|
2016-05-04 23:20:23 +00:00
|
|
|
r_socket_write (fh, bufw, 5 + i);
|
|
|
|
/* read response */
|
|
|
|
r_socket_read (fh, (ut8*)bufr, 5);
|
|
|
|
if (bufr[0] == (char)(RAP_RMT_CMD)) {
|
|
|
|
cmd_len = r_read_at_be32 (bufr, 1);
|
2016-12-19 15:44:51 +00:00
|
|
|
char *rcmd = calloc (1, cmd_len + 1);
|
|
|
|
if (rcmd) {
|
|
|
|
r_socket_read (fh, (ut8*)rcmd, cmd_len);
|
|
|
|
char *res = r_core_cmd_str (core, rcmd);
|
2016-05-04 23:20:23 +00:00
|
|
|
if (res) {
|
|
|
|
int res_len = strlen (res) + 1;
|
|
|
|
ut8 *pkt = r_rap_packet ((RAP_RMT_CMD | RAP_RMT_REPLY), res_len);
|
|
|
|
r_rap_packet_fill (pkt, (const ut8*)res, res_len);
|
|
|
|
r_socket_write (fh, pkt, 5 + res_len);
|
|
|
|
free (res);
|
|
|
|
free (pkt);
|
|
|
|
}
|
2016-12-19 15:44:51 +00:00
|
|
|
free (rcmd);
|
2016-05-04 23:20:23 +00:00
|
|
|
}
|
|
|
|
/* read response */
|
|
|
|
r_socket_read (fh, (ut8*)bufr, 5);
|
|
|
|
}
|
|
|
|
|
2016-05-04 18:08:38 +00:00
|
|
|
if (bufr[0] != (char)(RAP_RMT_CMD | RTR_RAP_REPLY)) {
|
2010-08-24 09:58:09 +00:00
|
|
|
eprintf ("Error: Wrong reply\n");
|
|
|
|
return;
|
|
|
|
}
|
2016-04-26 09:09:15 +00:00
|
|
|
cmd_len = r_read_at_be32 (bufr, 1);
|
2016-06-30 19:24:38 +00:00
|
|
|
if (cmd_len < 1 || cmd_len > 16384) {
|
|
|
|
eprintf ("Error: cmd_len is wrong\n");
|
|
|
|
return;
|
|
|
|
}
|
2016-05-04 23:20:23 +00:00
|
|
|
cmd_output = calloc (1, cmd_len + 1);
|
2010-08-24 09:58:09 +00:00
|
|
|
if (!cmd_output) {
|
2011-09-03 03:09:31 +00:00
|
|
|
eprintf ("Error: Allocating cmd output\n");
|
2010-08-24 09:58:09 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-05-28 09:38:23 +00:00
|
|
|
r_socket_read_block (fh, (ut8*)cmd_output, cmd_len);
|
2016-06-30 21:38:08 +00:00
|
|
|
//ensure the termination
|
|
|
|
cmd_output[cmd_len] = 0;
|
2016-06-26 04:51:17 +00:00
|
|
|
r_cons_println (cmd_output);
|
2010-08-24 09:58:09 +00:00
|
|
|
free ((void *)cmd_output);
|
|
|
|
}
|
2012-11-05 01:00:34 +00:00
|
|
|
|
|
|
|
// TODO: support len for binary data?
|
|
|
|
R_API char *r_core_rtr_cmds_query (RCore *core, const char *host, const char *port, const char *cmd) {
|
|
|
|
RSocket *s = r_socket_new (0);
|
2016-05-04 21:32:29 +00:00
|
|
|
const int timeout = 0;
|
|
|
|
char *rbuf = NULL;
|
|
|
|
int retries = 6;
|
|
|
|
ut8 buf[1024];
|
|
|
|
|
|
|
|
for (; retries > 0; r_sys_usleep (10 * 1000)) {
|
2017-06-12 21:34:51 +00:00
|
|
|
if (r_socket_connect (s, host, port, R_SOCKET_PROTO_TCP, timeout)) {
|
2012-11-05 01:00:34 +00:00
|
|
|
break;
|
2017-06-12 21:34:51 +00:00
|
|
|
}
|
2012-11-05 01:00:34 +00:00
|
|
|
retries--;
|
|
|
|
}
|
2016-05-04 21:32:29 +00:00
|
|
|
if (retries > 0) {
|
2012-11-05 01:00:34 +00:00
|
|
|
rbuf = strdup ("");
|
|
|
|
r_socket_write (s, (void*)cmd, strlen (cmd));
|
|
|
|
//r_socket_write (s, "px\n", 3);
|
|
|
|
for (;;) {
|
|
|
|
int ret = r_socket_read (s, buf, sizeof (buf));
|
2018-09-13 08:17:26 +00:00
|
|
|
if (ret < 1) {
|
|
|
|
break;
|
|
|
|
}
|
2012-11-05 01:00:34 +00:00
|
|
|
buf[ret] = 0;
|
2017-03-16 21:29:49 +00:00
|
|
|
rbuf = r_str_append (rbuf, (const char *)buf);
|
2012-11-05 01:00:34 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
eprintf ("Cannot connect\n");
|
|
|
|
}
|
|
|
|
r_socket_free (s);
|
|
|
|
return rbuf;
|
|
|
|
}
|
|
|
|
|
2018-09-17 10:07:00 +00:00
|
|
|
#if HAVE_LIBUV
|
|
|
|
|
|
|
|
typedef struct rtr_cmds_context_t {
|
|
|
|
uv_tcp_t server;
|
|
|
|
RPVector clients;
|
|
|
|
void *bed;
|
|
|
|
} rtr_cmds_context;
|
|
|
|
|
|
|
|
typedef struct rtr_cmds_client_context_t {
|
|
|
|
RCore *core;
|
|
|
|
char buf[4096];
|
|
|
|
char *res;
|
|
|
|
size_t len;
|
|
|
|
uv_tcp_t *client;
|
|
|
|
} rtr_cmds_client_context;
|
|
|
|
|
|
|
|
static void rtr_cmds_client_close(uv_tcp_t *client, bool remove) {
|
|
|
|
uv_loop_t *loop = client->loop;
|
|
|
|
rtr_cmds_context *context = loop->data;
|
|
|
|
if (remove) {
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < r_pvector_len (&context->clients); i++) {
|
|
|
|
if (r_pvector_at (&context->clients, i) == client) {
|
|
|
|
r_pvector_remove_at (&context->clients, i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rtr_cmds_client_context *client_context = client->data;
|
|
|
|
uv_close ((uv_handle_t *) client, (uv_close_cb) free);
|
|
|
|
free (client_context->res);
|
|
|
|
free (client_context);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rtr_cmds_alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) {
|
|
|
|
rtr_cmds_client_context *context = handle->data;
|
|
|
|
buf->base = context->buf + context->len;
|
|
|
|
buf->len = sizeof (context->buf) - context->len - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rtr_cmds_write(uv_write_t *req, int status) {
|
|
|
|
rtr_cmds_client_context *context = req->data;
|
|
|
|
|
|
|
|
if (status) {
|
|
|
|
eprintf ("Write error: %s\n", uv_strerror (status));
|
|
|
|
}
|
|
|
|
|
|
|
|
free (req);
|
|
|
|
rtr_cmds_client_close (context->client, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rtr_cmds_read(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf) {
|
|
|
|
rtr_cmds_context *context = client->loop->data;
|
|
|
|
rtr_cmds_client_context *client_context = client->data;
|
|
|
|
|
|
|
|
if (nread < 0) {
|
|
|
|
if (nread != UV_EOF) {
|
|
|
|
eprintf ("Failed to read: %s\n", uv_err_name ((int) nread));
|
|
|
|
}
|
|
|
|
rtr_cmds_client_close ((uv_tcp_t *) client, true);
|
|
|
|
return;
|
|
|
|
} else if (nread == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf->base[nread] = '\0';
|
|
|
|
char *end = strchr (buf->base, '\n');
|
|
|
|
if (!end) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
*end = '\0';
|
|
|
|
|
|
|
|
r_cons_sleep_end (context->bed);
|
|
|
|
client_context->res = r_core_cmd_str (client_context->core, (const char *)client_context->buf);
|
|
|
|
context->bed = r_cons_sleep_begin ();
|
|
|
|
|
|
|
|
if (!client_context->res || !*client_context->res) {
|
|
|
|
free (client_context->res);
|
|
|
|
client_context->res = strdup ("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!client_context->res || (!r_config_get_i (client_context->core->config, "scr.prompt") &&
|
|
|
|
!strcmp ((char *)buf, "q!")) ||
|
|
|
|
!strcmp ((char *)buf, ".--")) {
|
|
|
|
rtr_cmds_client_close ((uv_tcp_t *) client, true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uv_write_t *req = R_NEW (uv_write_t);
|
|
|
|
req->data = client_context;
|
|
|
|
uv_buf_t wrbuf = uv_buf_init (client_context->res, (unsigned int) strlen (client_context->res));
|
|
|
|
uv_write (req, client, &wrbuf, 1, rtr_cmds_write);
|
|
|
|
uv_read_stop (client);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rtr_cmds_new_connection(uv_stream_t *server, int status) {
|
|
|
|
if (status < 0) {
|
|
|
|
eprintf ("New connection error: %s\n", uv_strerror (status));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rtr_cmds_context *context = server->loop->data;
|
|
|
|
|
|
|
|
uv_tcp_t *client = R_NEW (uv_tcp_t);
|
|
|
|
if (!client) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uv_tcp_init (server->loop, client);
|
|
|
|
if (uv_accept (server, (uv_stream_t *)client) == 0) {
|
|
|
|
rtr_cmds_client_context *client_context = R_NEW (rtr_cmds_client_context);
|
|
|
|
if (!client_context) {
|
|
|
|
uv_close ((uv_handle_t *)client, NULL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
client_context->core = server->data;
|
|
|
|
client_context->len = 0;
|
|
|
|
client_context->buf[0] = '\0';
|
|
|
|
client_context->res = NULL;
|
|
|
|
client_context->client = client;
|
|
|
|
client->data = client_context;
|
|
|
|
|
|
|
|
uv_read_start ((uv_stream_t *)client, rtr_cmds_alloc_buffer, rtr_cmds_read);
|
|
|
|
|
|
|
|
r_pvector_push (&context->clients, client);
|
|
|
|
} else {
|
|
|
|
uv_close ((uv_handle_t *)client, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rtr_cmds_stop(uv_async_t *handle) {
|
|
|
|
uv_close ((uv_handle_t *) handle, NULL);
|
|
|
|
|
|
|
|
rtr_cmds_context *context = handle->loop->data;
|
|
|
|
|
|
|
|
uv_close ((uv_handle_t *) &context->server, NULL);
|
|
|
|
|
|
|
|
void **it;
|
|
|
|
r_pvector_foreach (&context->clients, it) {
|
|
|
|
uv_tcp_t *client = *it;
|
|
|
|
rtr_cmds_client_close (client, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rtr_cmds_break(uv_async_t *async) {
|
|
|
|
uv_async_send (async);
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API int r_core_rtr_cmds(RCore *core, const char *port) {
|
|
|
|
if (!port || port[0] == '?') {
|
|
|
|
r_cons_printf ("Usage: .:[tcp-port] run r2 commands for clients\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uv_loop_t *loop = R_NEW (uv_loop_t);
|
|
|
|
if (!loop) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
uv_loop_init (loop);
|
|
|
|
|
|
|
|
rtr_cmds_context context;
|
|
|
|
r_pvector_init (&context.clients, NULL);
|
|
|
|
loop->data = &context;
|
|
|
|
|
|
|
|
context.server.data = core;
|
|
|
|
uv_tcp_init (loop, &context.server);
|
|
|
|
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
bool local = (bool) r_config_get_i(core->config, "tcp.islocal");
|
|
|
|
int porti = r_socket_port_by_name (port);
|
|
|
|
uv_ip4_addr (local ? "127.0.0.1" : "0.0.0.0", porti, &addr);
|
|
|
|
|
|
|
|
uv_tcp_bind (&context.server, (const struct sockaddr *) &addr, 0);
|
|
|
|
int r = uv_listen ((uv_stream_t *)&context.server, 32, rtr_cmds_new_connection);
|
|
|
|
if (r) {
|
|
|
|
eprintf ("Failed to listen: %s\n", uv_strerror (r));
|
|
|
|
goto beach;
|
|
|
|
}
|
|
|
|
|
|
|
|
uv_async_t stop_async;
|
|
|
|
uv_async_init (loop, &stop_async, rtr_cmds_stop);
|
|
|
|
|
|
|
|
r_cons_break_push ((RConsBreak) rtr_cmds_break, &stop_async);
|
|
|
|
context.bed = r_cons_sleep_begin ();
|
|
|
|
uv_run (loop, UV_RUN_DEFAULT);
|
|
|
|
r_cons_sleep_end (context.bed);
|
|
|
|
r_cons_break_pop ();
|
|
|
|
|
|
|
|
beach:
|
|
|
|
uv_loop_close (loop);
|
|
|
|
free (loop);
|
|
|
|
r_pvector_clear (&context.clients);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2012-11-05 01:00:34 +00:00
|
|
|
R_API int r_core_rtr_cmds (RCore *core, const char *port) {
|
2014-04-22 22:12:03 +00:00
|
|
|
unsigned char buf[4097];
|
2015-02-17 23:28:53 +00:00
|
|
|
RSocket *ch = NULL;
|
2012-12-22 02:37:50 +00:00
|
|
|
int i, ret;
|
|
|
|
char *str;
|
|
|
|
|
2016-05-04 21:32:29 +00:00
|
|
|
if (!port || port[0] == '?') {
|
2012-12-22 02:37:50 +00:00
|
|
|
r_cons_printf ("Usage: .:[tcp-port] run r2 commands for clients\n");
|
2015-09-14 10:35:38 +00:00
|
|
|
return false;
|
2012-12-22 02:37:50 +00:00
|
|
|
}
|
2012-12-03 03:01:55 +00:00
|
|
|
|
2019-05-06 09:31:32 +00:00
|
|
|
RSocket *s = r_socket_new (0);
|
2019-05-16 12:18:16 +00:00
|
|
|
s->local = r_config_get_i (core->config, "tcp.islocal");
|
2018-02-27 12:11:26 +00:00
|
|
|
|
2012-11-05 01:00:34 +00:00
|
|
|
if (!r_socket_listen (s, port, NULL)) {
|
|
|
|
eprintf ("Error listening on port %s\n", port);
|
2012-12-22 02:37:50 +00:00
|
|
|
r_socket_free (s);
|
2015-09-14 10:35:38 +00:00
|
|
|
return false;
|
2012-11-05 01:00:34 +00:00
|
|
|
}
|
2014-04-22 22:12:03 +00:00
|
|
|
|
2012-11-05 01:00:34 +00:00
|
|
|
eprintf ("Listening for commands on port %s\n", port);
|
|
|
|
listenport = port;
|
2016-11-20 18:20:14 +00:00
|
|
|
r_cons_break_push ((RConsBreak)r_core_rtr_http_stop, core);
|
2012-11-05 01:00:34 +00:00
|
|
|
for (;;) {
|
2016-11-20 18:20:14 +00:00
|
|
|
if (r_cons_is_breaked ()) {
|
|
|
|
break;
|
|
|
|
}
|
2018-08-11 15:24:19 +00:00
|
|
|
void *bed = r_cons_sleep_begin ();
|
2012-11-05 01:00:34 +00:00
|
|
|
ch = r_socket_accept (s);
|
|
|
|
buf[0] = 0;
|
2014-04-22 22:12:03 +00:00
|
|
|
ret = r_socket_read (ch, buf, sizeof (buf) - 1);
|
2018-08-11 15:24:19 +00:00
|
|
|
r_cons_sleep_end (bed);
|
2016-05-04 21:32:29 +00:00
|
|
|
if (ret > 0) {
|
2012-11-05 01:00:34 +00:00
|
|
|
buf[ret] = 0;
|
2016-05-04 21:32:29 +00:00
|
|
|
for (i = 0; buf[i]; i++) {
|
2016-11-29 10:52:24 +00:00
|
|
|
if (buf[i] == '\n') {
|
2016-11-20 18:20:14 +00:00
|
|
|
buf[i] = buf[i + 1]? ';': '\0';
|
2016-11-29 10:52:24 +00:00
|
|
|
}
|
2016-05-04 21:32:29 +00:00
|
|
|
}
|
2017-12-13 11:03:39 +00:00
|
|
|
if ((!r_config_get_i (core->config, "scr.prompt") &&
|
|
|
|
!strcmp ((char *)buf, "q!")) ||
|
|
|
|
!strcmp ((char *)buf, ".--")) {
|
2016-11-29 10:52:24 +00:00
|
|
|
r_socket_close (ch);
|
2012-12-12 22:58:18 +00:00
|
|
|
break;
|
2016-11-29 10:52:24 +00:00
|
|
|
}
|
2012-11-05 01:00:34 +00:00
|
|
|
str = r_core_cmd_str (core, (const char *)buf);
|
2018-08-11 15:24:19 +00:00
|
|
|
bed = r_cons_sleep_begin ();
|
2016-05-04 21:32:29 +00:00
|
|
|
if (str && *str) {
|
|
|
|
r_socket_write (ch, str, strlen (str));
|
|
|
|
} else {
|
|
|
|
r_socket_write (ch, "\n", 1);
|
|
|
|
}
|
2018-08-11 15:24:19 +00:00
|
|
|
r_cons_sleep_end (bed);
|
2012-12-12 22:58:18 +00:00
|
|
|
free (str);
|
2012-11-05 01:00:34 +00:00
|
|
|
}
|
|
|
|
r_socket_close (ch);
|
2017-07-26 06:41:03 +00:00
|
|
|
r_socket_free (ch);
|
2016-11-29 10:52:24 +00:00
|
|
|
ch = NULL;
|
2012-11-05 01:00:34 +00:00
|
|
|
}
|
2016-11-20 18:20:14 +00:00
|
|
|
r_cons_break_pop ();
|
2016-05-04 21:32:29 +00:00
|
|
|
r_socket_free (s);
|
|
|
|
r_socket_free (ch);
|
2012-11-05 01:00:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2018-09-17 10:07:00 +00:00
|
|
|
|
|
|
|
#endif
|