This is an automated email from Gerrit.

Steven Stallion ([email protected]) just uploaded a new patch set to 
Gerrit, which you can find at http://openocd.zylin.com/3534

-- gerrit

commit e00802bdd0b7ef0e749bef5c6aaed1e6d529e647
Author: Steven Stallion <[email protected]>
Date:   Wed Jun 29 23:15:31 2016 -0500

    server: Add support for listening on loopback
    
    Some installations of OpenOCD are used in restricted environments that
    do not permit binding to public interfaces.
    
    This patch does not affect the default behavior to listen on all
    interfaces, however it does give the option to restrict services to the
    loopback interface by way of a new command.
    
    Change-Id: Id51bd64b376a8c62dd47b08b4d834872925e6af2
    Signed-off-by: Steven Stallion <[email protected]>

diff --git a/doc/openocd.texi b/doc/openocd.texi
index 94f1f31..13564d8 100644
--- a/doc/openocd.texi
+++ b/doc/openocd.texi
@@ -6731,6 +6731,11 @@ the initial log output channel is stderr.
 Add @var{directory} to the file/script search path.
 @end deffn
 
+@deffn Command loopback [on|off]
+Restrict TCP/IP services to the loopback interface.
+By default, services will listen on all available interfaces.
+@end deffn
+
 @anchor{targetstatehandling}
 @section Target State handling
 @cindex reset
diff --git a/src/server/server.c b/src/server/server.c
index cdb6285..ffebd28 100644
--- a/src/server/server.c
+++ b/src/server/server.c
@@ -52,6 +52,9 @@ static int last_signal;
 /* set the polling period to 100ms */
 static int polling_period = 100;
 
+/* determines if services should be restricted to loopback */
+static bool restrict_loopback;
+
 static int add_connection(struct service *service, struct command_context 
*cmd_ctx)
 {
        socklen_t address_size;
@@ -222,6 +225,11 @@ int add_service(char *name,
        }
 
        if (c->type == CONNECTION_TCP) {
+               if (restrict_loopback)
+                       c->address = INADDR_LOOPBACK;
+               else
+                       c->address = INADDR_ANY;
+
                c->max_connections = max_connections;
 
                c->fd = socket(AF_INET, SOCK_STREAM, 0);
@@ -240,11 +248,11 @@ int add_service(char *name,
 
                memset(&c->sin, 0, sizeof(c->sin));
                c->sin.sin_family = AF_INET;
-               c->sin.sin_addr.s_addr = INADDR_ANY;
+               c->sin.sin_addr.s_addr = htonl(c->address);
                c->sin.sin_port = htons(c->portnumber);
 
                if (bind(c->fd, (struct sockaddr *)&c->sin, sizeof(c->sin)) == 
-1) {
-                       LOG_ERROR("couldn't bind to socket: %s", 
strerror(errno));
+                       LOG_ERROR("couldn't bind %s to socket: %s", name, 
strerror(errno));
                        exit(-1);
                }
 
@@ -632,6 +640,22 @@ COMMAND_HANDLER(handle_poll_period_command)
        return ERROR_OK;
 }
 
+COMMAND_HANDLER(handle_loopback_command)
+{
+       switch (CMD_ARGC) {
+               case 0:
+                       command_print(CMD_CTX, "restrict loopback: %s",
+                           restrict_loopback ? "on" : "off");
+                       break;
+               case 1:
+                       COMMAND_PARSE_ON_OFF(CMD_ARGV[0], restrict_loopback);
+                       break;
+               default:
+                       return ERROR_COMMAND_SYNTAX_ERROR;
+       }
+       return ERROR_OK;
+}
+
 static const struct command_registration server_command_handlers[] = {
        {
                .name = "shutdown",
@@ -647,6 +671,14 @@ static const struct command_registration 
server_command_handlers[] = {
                .usage = "",
                .help = "set the servers polling period",
        },
+       {
+               .name = "loopback",
+               .handler = &handle_loopback_command,
+               .mode = COMMAND_ANY,
+               .help = "Restrict TCP/IP services to the loopback interface. "
+                   "By default, services will listen on all available 
interfaces.",
+               .usage = "[on|off]",
+       },
        COMMAND_REGISTRATION_DONE
 };
 
diff --git a/src/server/server.h b/src/server/server.h
index 68ad16d..368fdc8 100644
--- a/src/server/server.h
+++ b/src/server/server.h
@@ -58,6 +58,7 @@ struct service {
        char *name;
        enum connection_type type;
        char *port;
+       in_addr_t address;
        unsigned short portnumber;
        int fd;
        struct sockaddr_in sin;

-- 

------------------------------------------------------------------------------
Attend Shape: An AT&T Tech Expo July 15-16. Meet us at AT&T Park in San
Francisco, CA to explore cutting-edge tech and listen to tech luminaries
present their vision of the future. This family event has something for
everyone, including kids. Get more information and register today.
http://sdm.link/attshape
_______________________________________________
OpenOCD-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openocd-devel

Reply via email to