Moved parsing of at_times rules to a separate function
This was done in order to make the parse_rule function smaller and easier to comprehend.
This commit is contained in:
@ -255,6 +255,7 @@ typedef struct
|
|||||||
|
|
||||||
static int hashkeyfun(void* key);
|
static int hashkeyfun(void* key);
|
||||||
static int hashcmpfun(void *, void *);
|
static int hashcmpfun(void *, void *);
|
||||||
|
bool parse_at_times(char** tok, char** saveptr, RULE* ruledef);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hashtable key hashing function. Uses a simple string hashing algorithm.
|
* Hashtable key hashing function. Uses a simple string hashing algorithm.
|
||||||
@ -539,6 +540,8 @@ static TIMERANGE* parse_time(const char* str)
|
|||||||
struct tm start, end;
|
struct tm start, end;
|
||||||
TIMERANGE* tr = NULL;
|
TIMERANGE* tr = NULL;
|
||||||
|
|
||||||
|
memset(&start, 0, sizeof(start));
|
||||||
|
memset(&end, 0, sizeof(end));
|
||||||
strcpy(strbuf, str);
|
strcpy(strbuf, str);
|
||||||
|
|
||||||
if ((separator = strchr(strbuf, '-')))
|
if ((separator = strchr(strbuf, '-')))
|
||||||
@ -577,8 +580,6 @@ TIMERANGE* split_reverse_time(TIMERANGE* tr)
|
|||||||
{
|
{
|
||||||
TIMERANGE* tmp = NULL;
|
TIMERANGE* tmp = NULL;
|
||||||
|
|
||||||
if (IS_RVRS_TIME(tr))
|
|
||||||
{
|
|
||||||
tmp = (TIMERANGE*) calloc(1, sizeof(TIMERANGE));
|
tmp = (TIMERANGE*) calloc(1, sizeof(TIMERANGE));
|
||||||
tmp->next = tr;
|
tmp->next = tr;
|
||||||
tmp->start.tm_hour = 0;
|
tmp->start.tm_hour = 0;
|
||||||
@ -588,8 +589,6 @@ TIMERANGE* split_reverse_time(TIMERANGE* tr)
|
|||||||
tr->end.tm_hour = 23;
|
tr->end.tm_hour = 23;
|
||||||
tr->end.tm_min = 59;
|
tr->end.tm_min = 59;
|
||||||
tr->end.tm_sec = 59;
|
tr->end.tm_sec = 59;
|
||||||
}
|
|
||||||
|
|
||||||
return tmp;
|
return tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1056,39 +1055,12 @@ reparse_rule:
|
|||||||
tok = strtok_r(NULL, " ,", &saveptr);
|
tok = strtok_r(NULL, " ,", &saveptr);
|
||||||
TIMERANGE *tr = NULL;
|
TIMERANGE *tr = NULL;
|
||||||
|
|
||||||
while (tok)
|
if (!parse_at_times(&tok, &saveptr, ruledef))
|
||||||
{
|
{
|
||||||
if (strcmp(tok, "on_queries") == 0)
|
rval = false;
|
||||||
break;
|
break;
|
||||||
if (!check_time(tok))
|
|
||||||
{
|
|
||||||
MXS_ERROR("dbfwfilter: Rule parsing failed, malformed time definition: %s", tok);
|
|
||||||
rval = false;
|
|
||||||
goto retblock;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TIMERANGE *tmp = parse_time(tok);
|
|
||||||
|
|
||||||
if (tmp == NULL)
|
|
||||||
{
|
|
||||||
MXS_ERROR("dbfwfilter: Rule parsing failed, unexpected characters after time definition.");
|
|
||||||
rval = false;
|
|
||||||
tr_free(tr);
|
|
||||||
goto retblock;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (IS_RVRS_TIME(tmp))
|
|
||||||
{
|
|
||||||
tmp = split_reverse_time(tmp);
|
|
||||||
}
|
|
||||||
|
|
||||||
tmp->next = tr;
|
|
||||||
tr = tmp;
|
|
||||||
tok = strtok_r(NULL, " ,", &saveptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
ruledef->active = tr;
|
|
||||||
|
|
||||||
if (tok && strcmp(tok, "on_queries") == 0)
|
if (tok && strcmp(tok, "on_queries") == 0)
|
||||||
{
|
{
|
||||||
goto reparse_rule;
|
goto reparse_rule;
|
||||||
@ -2270,6 +2242,56 @@ diagnostic(FILTER *instance, void *fsession, DCB *dcb)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse at_times rule.
|
||||||
|
* @param tok Pointer to last token, should be a valid timerange
|
||||||
|
* @param saveptr Pointer to the beginning of next token
|
||||||
|
* @param ruledef The rule definition to which this at_times rule is applied
|
||||||
|
* @return True if parsing was successful, false if an error occurred
|
||||||
|
*/
|
||||||
|
bool parse_at_times(char** tok, char** saveptr, RULE* ruledef)
|
||||||
|
{
|
||||||
|
TIMERANGE *tr = NULL;
|
||||||
|
bool success = true;
|
||||||
|
char token[FW_TOKENBUFFER_SIZE];
|
||||||
|
|
||||||
|
while (*tok && strcmp(*tok, "on_queries") != 0)
|
||||||
|
{
|
||||||
|
if (!check_time(*tok))
|
||||||
|
{
|
||||||
|
MXS_ERROR("dbfwfilter: Rule parsing failed, malformed time definition: %s", *tok);
|
||||||
|
success = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
TIMERANGE *tmp = parse_time(*tok);
|
||||||
|
|
||||||
|
if (tmp == NULL)
|
||||||
|
{
|
||||||
|
MXS_ERROR("dbfwfilter: Rule parsing failed, unexpected characters after time definition.");
|
||||||
|
success = false;
|
||||||
|
tr_free(tr);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (IS_RVRS_TIME(tmp))
|
||||||
|
{
|
||||||
|
tmp = split_reverse_time(tmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
tmp->next = tr;
|
||||||
|
tr = tmp;
|
||||||
|
*tok = fw_get_token(NULL, saveptr, token, sizeof(token));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (success)
|
||||||
|
{
|
||||||
|
ruledef->active = tr;
|
||||||
|
}
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef BUILD_RULE_PARSER
|
#ifdef BUILD_RULE_PARSER
|
||||||
#include <test_utils.h>
|
#include <test_utils.h>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user