summaryrefslogtreecommitdiff
path: root/src/filter.c
blob: 5c31f30cec76c0a2dbb85d1f56b0cc48a16c4993 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 * ZMap Copyright 2013 Regents of the University of Michigan
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy
 * of the License at http://www.apache.org/licenses/LICENSE-2.0
 */

#include "filter.h"

#include <string.h>

#include "lexer.h"
#include "parser.h"
#include "state.h"

#include "../lib/logger.h"

extern int yyparse();

node_t *zfilter;

static int validate_node(node_t *node, fielddefset_t *fields) {
    int index, found = 0;
    if (node->type == OP) {
        // These end up getting validated later
        if (node->value.op == AND || node->value.op == OR) {
            return 1;
        }
        // Comparison node (=, >, <, etc.)
        // Validate that the field (left child) exists in the fieldset
        for (index = 0; index < fields->len; index++) {
            if (fields->fielddefs[index].name) {
                if (strcmp(fields->fielddefs[index].name,
                           node->left_child->value.field.fieldname) == 0) {
                    node->left_child->value.field.index = index;
                    found                               = 1;
                    break;
                }
            }
        }
        if (!found) {
            fprintf(stderr, "Field '%s' does not exist\n",
                    node->left_child->value.field.fieldname);
            return 0;
        }
        // Fieldname is fine, match the type.
        switch (node->right_child->type) {
        case STRING:
            if (strcmp(fields->fielddefs[index].type, "string") == 0) {
                return 1;
            } else {
                fprintf(stderr, "Field '%s' is not of type 'string'\n",
                        fields->fielddefs[index].name);
                return 0;
            }
        case INT:
            if (strcmp(fields->fielddefs[index].type, "int") == 0 ||
                strcmp(fields->fielddefs[index].type, "bool") == 0) {
                return 1;
            } else {
                fprintf(stderr, "Field '%s' is not of type 'int'\n",
                        fields->fielddefs[index].name);
                return 0;
            }
        default:
            return 0;
        }
    } else {
        // All non-op nodes are valid
        return 1;
    }

    // Didn't validate
    return 0;
}

int parse_filter_string(char *filter) {
    YY_BUFFER_STATE buffer_state = yy_scan_string(filter);
    int             status       = yyparse();
    yy_delete_buffer(buffer_state);
    if (status) {
        // Error
        log_error("xmap", "Unable to parse filter string: '%s'", filter);
        return 0;
    }
    xconf.filter.expression = zfilter;

    return 1;
}

/*
 * 0     Valid
 * -1    Invalid Field Name
 * -2    Type Mismatch
 */
int validate_filter(node_t *root, fielddefset_t *fields) {
    int valid;
    if (!root) {
        return 1;
    }
    valid = validate_node(root, fields);
    if (!valid) {
        return 0;
    }

    return (validate_filter(root->left_child, fields) &&
            validate_filter(root->right_child, fields));
}