diff options
author | Nick Mathewson <nickm@torproject.org> | 2019-04-02 10:29:07 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2019-04-25 14:13:03 -0400 |
commit | f18b7dc4731bcb853db92a0faaa4ec03d6ef5586 (patch) | |
tree | 9c4fe19191358faa9980ef287f946136fb6bab75 /src/feature/control | |
parent | de70eebc65d40d50f877b0f82df4d05ce670faa5 (diff) | |
download | tor-f18b7dc4731bcb853db92a0faaa4ec03d6ef5586.tar.gz tor-f18b7dc4731bcb853db92a0faaa4ec03d6ef5586.zip |
Extract the argument-splitting part of control.c's parser
This is preliminary work for fixing 29984; no behavior has changed.
Diffstat (limited to 'src/feature/control')
-rw-r--r-- | src/feature/control/control.c | 42 | ||||
-rw-r--r-- | src/feature/control/control.h | 5 |
2 files changed, 33 insertions, 14 deletions
diff --git a/src/feature/control/control.c b/src/feature/control/control.c index 41e21c0a14..308f1936b9 100644 --- a/src/feature/control/control.c +++ b/src/feature/control/control.c @@ -33,6 +33,7 @@ **/ #define CONTROL_MODULE_PRIVATE +#define CONTROL_PRIVATE #include "core/or/or.h" #include "app/config/config.h" @@ -274,6 +275,31 @@ peek_connection_has_http_command(connection_t *conn) return peek_buf_has_http_command(conn->inbuf); } +/** + * Helper: take a nul-terminated command of given length, and find where + * the command starts and the argument begins. Separate them with a NUL, + * and return a pointer to the arguments. + **/ +STATIC char * +control_split_incoming_command(char *incoming_cmd, size_t *data_len) +{ + size_t cmd_len = 0; + while (cmd_len < *data_len + && !TOR_ISSPACE(incoming_cmd[cmd_len])) + ++cmd_len; + + incoming_cmd[cmd_len]='\0'; + char *args = incoming_cmd+cmd_len+1; + tor_assert(*data_len>cmd_len); + *data_len -= (cmd_len+1); /* skip the command and NUL we added after it */ + while (TOR_ISSPACE(*args)) { + ++args; + --*data_len; + } + + return args; +} + static const char CONTROLPORT_IS_NOT_AN_HTTP_PROXY_MSG[] = "HTTP/1.0 501 Tor ControlPort is not an HTTP proxy" "\r\nContent-Type: text/html; charset=iso-8859-1\r\n\r\n" @@ -308,7 +334,6 @@ connection_control_process_inbuf(control_connection_t *conn) { size_t data_len; uint32_t cmd_data_len; - int cmd_len; char *args; tor_assert(conn); @@ -400,22 +425,11 @@ connection_control_process_inbuf(control_connection_t *conn) /* Otherwise, read another line. */ } data_len = conn->incoming_cmd_cur_len; + /* Okay, we now have a command sitting on conn->incoming_cmd. See if we * recognize it. */ - cmd_len = 0; - while ((size_t)cmd_len < data_len - && !TOR_ISSPACE(conn->incoming_cmd[cmd_len])) - ++cmd_len; - - conn->incoming_cmd[cmd_len]='\0'; - args = conn->incoming_cmd+cmd_len+1; - tor_assert(data_len>(size_t)cmd_len); - data_len -= (cmd_len+1); /* skip the command and NUL we added after it */ - while (TOR_ISSPACE(*args)) { - ++args; - --data_len; - } + args = control_split_incoming_command(conn->incoming_cmd, &data_len); /* If the connection is already closing, ignore further commands */ if (TO_CONN(conn)->marked_for_close) { diff --git a/src/feature/control/control.h b/src/feature/control/control.h index 3083837931..6fc1c40cac 100644 --- a/src/feature/control/control.h +++ b/src/feature/control/control.h @@ -60,4 +60,9 @@ int get_cached_network_liveness(void); void set_cached_network_liveness(int liveness); #endif /* defined(CONTROL_MODULE_PRIVATE) */ +#ifdef CONTROL_PRIVATE +STATIC char *control_split_incoming_command(char *incoming_cmd, + size_t *data_len); +#endif + #endif /* !defined(TOR_CONTROL_H) */ |