fieldset.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 <stdlib.h>
  9. #include <stdint.h>
  10. #include "types.h"
  11. #ifndef FIELDSET_H
  12. #define FIELDSET_H
  13. // maximum number of records that can be stored in a fieldset
  14. #define MAX_FIELDS 128
  15. // types of data that can be stored in a field
  16. #define FS_STRING 0
  17. #define FS_UINT64 1
  18. #define FS_BINARY 2
  19. #define FS_NULL 3
  20. // definition of a field that's provided by a probe module
  21. // these are used so that users can ask at the command-line
  22. // what fields are available for consumption
  23. typedef struct field_def {
  24. const char *name;
  25. const char *type;
  26. const char *desc;
  27. } fielddef_t;
  28. typedef struct fielddef_set {
  29. fielddef_t fielddefs[MAX_FIELDS];
  30. int len;
  31. } fielddefset_t;
  32. typedef union field_val {
  33. void *ptr;
  34. uint64_t num;
  35. } field_val_t;
  36. // the internal field type used by fieldset
  37. typedef struct field {
  38. const char *name;
  39. int type;
  40. int free_;
  41. size_t len;
  42. union field_val value;
  43. } field_t;
  44. // data structure that is populated by the probe module
  45. // and translated into the data structure that's passed
  46. // to the output module
  47. typedef struct fieldset {
  48. int len;
  49. field_t fields[MAX_FIELDS];
  50. } fieldset_t;
  51. // we pass a different fieldset to an output module than
  52. // the probe module generates for us because a user may
  53. // only want certain fields and will expect them in a certain
  54. // order. We generate a translated fieldset that contains
  55. // only the fields we want to export to the output module.
  56. // a translation specifies how to efficiently convert the fs
  57. // povided by the probe module to the fs for the output module.
  58. typedef struct translation {
  59. int len;
  60. int translation[MAX_FIELDS];
  61. } translation_t;
  62. fieldset_t *fs_new_fieldset(void);
  63. char* fs_get_string_by_index(fieldset_t *fs, int index);
  64. int fds_get_index_by_name(fielddefset_t *fds, char *name);
  65. void gen_fielddef_set(fielddefset_t *fds, fielddef_t fs[], int len);
  66. void fs_add_null(fieldset_t *fs, const char *name);
  67. void fs_add_uint64(fieldset_t *fs, const char *name, uint64_t value);
  68. void fs_add_string(fieldset_t *fs, const char *name, char *value, int free_);
  69. void fs_add_binary(fieldset_t *fs, const char *name, size_t len,
  70. void *value, int free_);
  71. // Modify
  72. void fs_modify_null(fieldset_t *fs, const char *name);
  73. void fs_modify_uint64(fieldset_t *fs, const char *name, uint64_t value);
  74. void fs_modify_string(fieldset_t *fs, const char *name, char *value, int free_);
  75. void fs_modify_binary(fieldset_t *fs, const char *name, size_t len,
  76. void *value, int free_);
  77. uint64_t fs_get_uint64_by_index(fieldset_t *fs, int index);
  78. void fs_free(fieldset_t *fs);
  79. void fs_generate_fieldset_translation(translation_t *t,
  80. fielddefset_t *avail, char** req, int reqlen);
  81. fieldset_t *translate_fieldset(fieldset_t *fs, translation_t *t);
  82. void fs_generate_full_fieldset_translation(translation_t *t, fielddefset_t *avail);
  83. #endif // FIELDSET_H