Extract the argument-splitting part of control.c's parser

This is preliminary work for fixing 29984; no behavior has changed.
This commit is contained in:
Nick Mathewson 2019-04-02 10:29:07 -04:00
parent de70eebc65
commit f18b7dc473
2 changed files with 33 additions and 14 deletions

View File

@ -33,6 +33,7 @@
**/ **/
#define CONTROL_MODULE_PRIVATE #define CONTROL_MODULE_PRIVATE
#define CONTROL_PRIVATE
#include "core/or/or.h" #include "core/or/or.h"
#include "app/config/config.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); 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[] = static const char CONTROLPORT_IS_NOT_AN_HTTP_PROXY_MSG[] =
"HTTP/1.0 501 Tor ControlPort is not an HTTP proxy" "HTTP/1.0 501 Tor ControlPort is not an HTTP proxy"
"\r\nContent-Type: text/html; charset=iso-8859-1\r\n\r\n" "\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; size_t data_len;
uint32_t cmd_data_len; uint32_t cmd_data_len;
int cmd_len;
char *args; char *args;
tor_assert(conn); tor_assert(conn);
@ -400,22 +425,11 @@ connection_control_process_inbuf(control_connection_t *conn)
/* Otherwise, read another line. */ /* Otherwise, read another line. */
} }
data_len = conn->incoming_cmd_cur_len; data_len = conn->incoming_cmd_cur_len;
/* Okay, we now have a command sitting on conn->incoming_cmd. See if we /* Okay, we now have a command sitting on conn->incoming_cmd. See if we
* recognize it. * recognize it.
*/ */
cmd_len = 0; args = control_split_incoming_command(conn->incoming_cmd, &data_len);
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;
}
/* If the connection is already closing, ignore further commands */ /* If the connection is already closing, ignore further commands */
if (TO_CONN(conn)->marked_for_close) { if (TO_CONN(conn)->marked_for_close) {

View File

@ -60,4 +60,9 @@ int get_cached_network_liveness(void);
void set_cached_network_liveness(int liveness); void set_cached_network_liveness(int liveness);
#endif /* defined(CONTROL_MODULE_PRIVATE) */ #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) */ #endif /* !defined(TOR_CONTROL_H) */