ifchd: Better handle incomplete command buffers.

Before the handling would constantly acculmulate a prefix of previous
incomplete commands.  Now it still has a latent defect where the entire
buffer will be discarded given a spurious command, but ndhc shouldn't
generate such commands so it shouldn't matter.
This commit is contained in:
Nicholas J. Kain 2021-04-25 04:34:37 -04:00
parent eb463ab086
commit d61902cad4

View File

@ -130,6 +130,7 @@ static int perform_ip4set(const char buf[static 1], size_t len)
action Dispatch { action Dispatch {
int pr = 0; int pr = 0;
cmd_start = p + 1;
switch (cl.state) { switch (cl.state) {
case STATE_IP4SET: pr = perform_ip4set(tb, arg_len); break; case STATE_IP4SET: pr = perform_ip4set(tb, arg_len); break;
case STATE_TIMEZONE: pr = perform_timezone( tb, arg_len); break; case STATE_TIMEZONE: pr = perform_timezone( tb, arg_len); break;
@ -197,6 +198,7 @@ int execute_buffer(const char newbuf[static 1])
int cmdf = 0; int cmdf = 0;
ssize_t buflen = snprintf(buf, sizeof buf, "%s%s", cl.ibuf, newbuf); ssize_t buflen = snprintf(buf, sizeof buf, "%s%s", cl.ibuf, newbuf);
memset(cl.ibuf, 0, sizeof cl.ibuf);
if (buflen < 0) { if (buflen < 0) {
log_line("%s: (%s) snprintf1 failed; your system is broken?", log_line("%s: (%s) snprintf1 failed; your system is broken?",
client_config.interface, __func__); client_config.interface, __func__);
@ -211,14 +213,21 @@ int execute_buffer(const char newbuf[static 1])
const char *p = buf; const char *p = buf;
const char *pe = p + strlen(buf); const char *pe = p + strlen(buf);
const char *arg_start = p; const char *arg_start = p;
const char *cmd_start = p;
size_t arg_len = 0; size_t arg_len = 0;
int cs = 0; int cs = 0;
%% write init; %% write init;
%% write exec; %% write exec;
if (p != pe) { if (cs == ifchd_parser_error) {
ssize_t ilen = snprintf(cl.ibuf, sizeof cl.ibuf, "%s", p); log_line("%s: (%s) ifch received invalid commands",
client_config.interface, __func__);
return -99;
}
if (cmd_start != pe) {
ssize_t ilen = snprintf(cl.ibuf, sizeof cl.ibuf, "%s", cmd_start);
if (ilen < 0) { if (ilen < 0) {
log_line("%s: (%s) snprintf2 failed; your system is broken?", log_line("%s: (%s) snprintf2 failed; your system is broken?",
client_config.interface, __func__); client_config.interface, __func__);
@ -231,11 +240,6 @@ int execute_buffer(const char newbuf[static 1])
} }
} }
if (cs < ifchd_parser_first_final) {
log_line("%s: ifch received invalid commands",
client_config.interface);
return -99;
}
return !cmdf ? 0 : -1; return !cmdf ? 0 : -1;
} }