From 61a10eb2aa7c0e8980e1c633c24c35404d87ad4c Mon Sep 17 00:00:00 2001 From: pancake Date: Fri, 13 Jul 2012 02:03:36 +0200 Subject: [PATCH] rarun2 now supports connect/listen with r_socket Use SO_REUSEADDR in r_socket_listen () --- TODO | 1 - binr/rarun2/Makefile | 2 +- binr/rarun2/rarun2.c | 46 ++++++++++++++++++++++++++++++++++++++++++++ libr/socket/socket.c | 2 ++ 4 files changed, 49 insertions(+), 2 deletions(-) diff --git a/TODO b/TODO index 4a533ac8f6..9621ed082e 100644 --- a/TODO +++ b/TODO @@ -10,7 +10,6 @@ * Colorize multiple ranges of chars in hexdump * Source debugging or gtfo - integration with rabin2 -d -* sC not working * get cparse ftw * show analized functions in 'aa' -> discuss diff --git a/binr/rarun2/Makefile b/binr/rarun2/Makefile index 86de931113..1b3ceeeb46 100644 --- a/binr/rarun2/Makefile +++ b/binr/rarun2/Makefile @@ -1,4 +1,4 @@ BIN=rarun2 -BINDEPS=r_util +BINDEPS=r_util r_socket include ../binr.mk diff --git a/binr/rarun2/rarun2.c b/binr/rarun2/rarun2.c index 6a7735968c..aee8ef762c 100644 --- a/binr/rarun2/rarun2.c +++ b/binr/rarun2/rarun2.c @@ -5,6 +5,7 @@ #include #include #include +#include #if __UNIX__ #include #endif @@ -24,6 +25,8 @@ static char *_seteuid = NULL; static char *_setgid = NULL; static char *_setegid = NULL; static char *_input = NULL; +static char *_connect = NULL; +static char *_listen = NULL; static int _timeout = 0; static void parseline (char *b) { @@ -34,6 +37,8 @@ static void parseline (char *b) { if (*e=='$') e = r_sys_getenv (e); if (e == NULL) return; if (!strcmp (b, "program")) _args[0] = _program = strdup (e); + else if (!strcmp (b, "connect")) _connect = strdup (e); + else if (!strcmp (b, "listen")) _listen = strdup (e); else if (!strcmp (b, "stdout")) _stdout = strdup (e); else if (!strcmp (b, "stdin")) _stdin = strdup (e); else if (!strcmp (b, "input")) _input = strdup (e); @@ -91,6 +96,45 @@ static int runfile () { close (2); dup2 (f, 2); } + + if (_connect) { + char *p = strchr (_connect, ':'); + if (p) { + RSocket *fd = r_socket_new (0); + *p=0; + if (!r_socket_connect_tcp (fd, _connect, p+1)) { + eprintf ("Cannot connect\n"); + return 1; + } + eprintf ("connected\n"); + close (0); + close (1); + close (2); + dup2 (fd->fd, 0); + dup2 (fd->fd, 1); + dup2 (fd->fd, 2); + } else { + eprintf ("Invalid format for connect. missing ':'\n"); + return 1; + } + } + if (_listen) { + RSocket *child, *fd = r_socket_new (0); + if (!r_socket_listen (fd, _listen, NULL)) { + eprintf ("Cannot listen\n"); + return 1; + } + child = r_socket_accept (fd); + if (child) { + eprintf ("connected\n"); + close (0); + close (1); + close (2); + dup2 (child->fd, 0); + dup2 (child->fd, 1); + dup2 (child->fd, 2); + } + } if (_chgdir) chdir (_chgdir); if (_chroot) chdir (_chroot); #if __UNIX__ @@ -143,6 +187,8 @@ int main(int argc, char **argv) { "# arg#=...\n" "setenv=FOO=BAR\n" "timeout=3\n" + "# connect=localhost:8080\n" + "# listen=8080\n" "# stdout=foo.txt\n" "# stdin=input.txt\n" "# input=input.txt\n" diff --git a/libr/socket/socket.c b/libr/socket/socket.c index 10b29052f6..4857d2d6a4 100644 --- a/libr/socket/socket.c +++ b/libr/socket/socket.c @@ -206,6 +206,7 @@ R_API int r_socket_free (RSocket *s) { } R_API int r_socket_listen (RSocket *s, const char *port, const char *certfile) { + int optval = 1; struct sockaddr_in sa; struct linger linger = { 0 }; @@ -214,6 +215,7 @@ R_API int r_socket_listen (RSocket *s, const char *port, const char *certfile) { linger.l_onoff = 1; linger.l_linger = 1; setsockopt (s->fd, SOL_SOCKET, SO_LINGER, (const char *)&linger, sizeof (linger)); + setsockopt(s->fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval); memset (&sa, 0, sizeof (sa)); sa.sin_family = AF_INET; sa.sin_addr.s_addr = htonl (INADDR_ANY);