filter.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * ZMap Copyright 2013 Regents of the University of Michigan
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy
  6. * of the License at http://www.apache.org/licenses/LICENSE-2.0
  7. */
  8. #include "filter.h"
  9. #include "state.h"
  10. #include "lexer.h"
  11. #include "parser.h"
  12. #include "expression.h"
  13. #include "../lib/logger.h"
  14. #include <string.h>
  15. extern int yyparse();
  16. node_t *zfilter;
  17. static int validate_node(node_t *node, fielddefset_t *fields)
  18. {
  19. int index, found = 0;
  20. if (node->type == OP) {
  21. // These end up getting validated later
  22. if (node->value.op == AND || node->value.op == OR) {
  23. return 1;
  24. }
  25. // Comparison node (=, >, <, etc.)
  26. // Validate that the field (left child) exists in the fieldset
  27. for (index = 0; index < fields->len; index++) {
  28. if (fields->fielddefs[index].name) {
  29. if (strcmp(fields->fielddefs[index].name,
  30. node->left_child->value.field.fieldname) == 0) {
  31. node->left_child->value.field.index = index;
  32. found = 1;
  33. break;
  34. }
  35. }
  36. }
  37. if (!found) {
  38. fprintf(stderr, "Field '%s' does not exist\n",
  39. node->left_child->value.field.fieldname);
  40. return 0;
  41. }
  42. // Fieldname is fine, match the type.
  43. switch (node->right_child->type) {
  44. case STRING:
  45. if (strcmp(fields->fielddefs[index].type, "string") == 0) {
  46. return 1;
  47. } else {
  48. fprintf(stderr, "Field '%s' is not of type 'string'\n",
  49. fields->fielddefs[index].name);
  50. return 0;
  51. }
  52. case INT:
  53. if (strcmp(fields->fielddefs[index].type, "int") == 0) {
  54. return 1;
  55. } else {
  56. fprintf(stderr, "Field '%s' is not of type 'int'\n",
  57. fields->fielddefs[index].name);
  58. return 0;
  59. }
  60. default:
  61. return 0;
  62. }
  63. } else {
  64. // All non-op nodes are valid
  65. return 1;
  66. }
  67. // Didn't validate
  68. return 0;
  69. }
  70. int parse_filter_string(char *filter)
  71. {
  72. YY_BUFFER_STATE buffer_state = yy_scan_string(filter);
  73. int status = yyparse();
  74. yy_delete_buffer(buffer_state);
  75. if (status) {
  76. // Error
  77. log_error("zmap", "Unable to parse filter string: '%s'", filter);
  78. return 0;
  79. }
  80. zconf.filter.expression = zfilter;
  81. return 1;
  82. }
  83. /*
  84. * 0 Valid
  85. * -1 Invalid Field Name
  86. * -2 Type Mismatch
  87. */
  88. int validate_filter(node_t *root, fielddefset_t *fields)
  89. {
  90. int valid;
  91. if (!root) {
  92. return 1;
  93. }
  94. valid = validate_node(root, fields);
  95. if (!valid) {
  96. return 0;
  97. }
  98. return (validate_filter(root->left_child, fields) && validate_filter(root->right_child, fields));
  99. }