confparse.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file confparse.h
  8. *
  9. * \brief Header for confparse.c.
  10. */
  11. #ifndef TOR_CONFPARSE_H
  12. #define TOR_CONFPARSE_H
  13. /** Enumeration of types which option values can take */
  14. typedef enum config_type_t {
  15. CONFIG_TYPE_STRING = 0, /**< An arbitrary string. */
  16. CONFIG_TYPE_FILENAME, /**< A filename: some prefixes get expanded. */
  17. CONFIG_TYPE_UINT, /**< A non-negative integer less than MAX_INT */
  18. CONFIG_TYPE_INT, /**< Any integer. */
  19. CONFIG_TYPE_PORT, /**< A port from 1...65535, 0 for "not set", or
  20. * "auto". */
  21. CONFIG_TYPE_INTERVAL, /**< A number of seconds, with optional units*/
  22. CONFIG_TYPE_MSEC_INTERVAL,/**< A number of milliseconds, with optional
  23. * units */
  24. CONFIG_TYPE_MEMUNIT, /**< A number of bytes, with optional units*/
  25. CONFIG_TYPE_DOUBLE, /**< A floating-point value */
  26. CONFIG_TYPE_BOOL, /**< A boolean value, expressed as 0 or 1. */
  27. CONFIG_TYPE_AUTOBOOL, /**< A boolean+auto value, expressed 0 for false,
  28. * 1 for true, and -1 for auto */
  29. CONFIG_TYPE_ISOTIME, /**< An ISO-formatted time relative to UTC. */
  30. CONFIG_TYPE_CSV, /**< A list of strings, separated by commas and
  31. * optional whitespace. */
  32. CONFIG_TYPE_CSV_INTERVAL, /**< A list of strings, separated by commas and
  33. * optional whitespace, representing intervals in
  34. * seconds, with optional units. We allow
  35. * multiple values here for legacy reasons, but
  36. * ignore every value after the first. */
  37. CONFIG_TYPE_LINELIST, /**< Uninterpreted config lines */
  38. CONFIG_TYPE_LINELIST_S, /**< Uninterpreted, context-sensitive config lines,
  39. * mixed with other keywords. */
  40. CONFIG_TYPE_LINELIST_V, /**< Catch-all "virtual" option to summarize
  41. * context-sensitive config lines when fetching.
  42. */
  43. CONFIG_TYPE_ROUTERSET, /**< A list of router names, addrs, and fps,
  44. * parsed into a routerset_t. */
  45. CONFIG_TYPE_OBSOLETE, /**< Obsolete (ignored) option. */
  46. } config_type_t;
  47. #ifdef TOR_UNIT_TESTS
  48. /**
  49. * Union used when building in test mode typechecking the members of a type
  50. * used with confparse.c. See CONF_CHECK_VAR_TYPE for a description of how
  51. * it is used. */
  52. typedef union {
  53. char **STRING;
  54. char **FILENAME;
  55. int *UINT; /* yes, really: Even though the confparse type is called
  56. * "UINT", it still uses the C int type -- it just enforces that
  57. * the values are in range [0,INT_MAX].
  58. */
  59. int *INT;
  60. int *PORT;
  61. int *INTERVAL;
  62. int *MSEC_INTERVAL;
  63. uint64_t *MEMUNIT;
  64. double *DOUBLE;
  65. int *BOOL;
  66. int *AUTOBOOL;
  67. time_t *ISOTIME;
  68. smartlist_t **CSV;
  69. int *CSV_INTERVAL;
  70. struct config_line_t **LINELIST;
  71. struct config_line_t **LINELIST_S;
  72. struct config_line_t **LINELIST_V;
  73. routerset_t **ROUTERSET;
  74. } confparse_dummy_values_t;
  75. #endif /* defined(TOR_UNIT_TESTS) */
  76. /** An abbreviation for a configuration option allowed on the command line. */
  77. typedef struct config_abbrev_t {
  78. const char *abbreviated;
  79. const char *full;
  80. int commandline_only;
  81. int warn;
  82. } config_abbrev_t;
  83. typedef struct config_deprecation_t {
  84. const char *name;
  85. const char *why_deprecated;
  86. } config_deprecation_t;
  87. /* Handy macro for declaring "In the config file or on the command line,
  88. * you can abbreviate <b>tok</b>s as <b>tok</b>". */
  89. #define PLURAL(tok) { #tok, #tok "s", 0, 0 }
  90. /** A variable allowed in the configuration file or on the command line. */
  91. typedef struct config_var_t {
  92. const char *name; /**< The full keyword (case insensitive). */
  93. config_type_t type; /**< How to interpret the type and turn it into a
  94. * value. */
  95. off_t var_offset; /**< Offset of the corresponding member of or_options_t. */
  96. const char *initvalue; /**< String (or null) describing initial value. */
  97. #ifdef TOR_UNIT_TESTS
  98. /** Used for compiler-magic to typecheck the corresponding field in the
  99. * corresponding struct. Only used in unit test mode, at compile-time. */
  100. confparse_dummy_values_t var_ptr_dummy;
  101. #endif
  102. } config_var_t;
  103. /* Macros to define extra members inside config_var_t fields, and at the
  104. * end of a list of them.
  105. */
  106. #ifdef TOR_UNIT_TESTS
  107. /* This is a somewhat magic type-checking macro for users of confparse.c.
  108. * It initializes a union member "confparse_dummy_values_t.conftype" with
  109. * the address of a static member "tp_dummy.member". This
  110. * will give a compiler warning unless the member field is of the correct
  111. * type.
  112. *
  113. * (This warning is mandatory, because a type mismatch here violates the type
  114. * compatibility constraint for simple assignment, and requires a diagnostic,
  115. * according to the C spec.)
  116. *
  117. * For example, suppose you say:
  118. * "CONF_CHECK_VAR_TYPE(or_options_t, STRING, Address)".
  119. * Then this macro will evaluate to:
  120. * { .STRING = &or_options_t_dummy.Address }
  121. * And since confparse_dummy_values_t.STRING has type "char **", that
  122. * expression will create a warning unless or_options_t.Address also
  123. * has type "char *".
  124. */
  125. #define CONF_CHECK_VAR_TYPE(tp, conftype, member) \
  126. { . conftype = &tp ## _dummy . member }
  127. #define CONF_TEST_MEMBERS(tp, conftype, member) \
  128. , CONF_CHECK_VAR_TYPE(tp, conftype, member)
  129. #define END_OF_CONFIG_VARS \
  130. { NULL, CONFIG_TYPE_OBSOLETE, 0, NULL, { .INT=NULL } }
  131. #define DUMMY_TYPECHECK_INSTANCE(tp) \
  132. static tp tp ## _dummy
  133. #else /* !(defined(TOR_UNIT_TESTS)) */
  134. #define CONF_TEST_MEMBERS(tp, conftype, member)
  135. #define END_OF_CONFIG_VARS { NULL, CONFIG_TYPE_OBSOLETE, 0, NULL }
  136. /* Repeatedly declarable incomplete struct to absorb redundant semicolons */
  137. #define DUMMY_TYPECHECK_INSTANCE(tp) \
  138. struct tor_semicolon_eater
  139. #endif /* defined(TOR_UNIT_TESTS) */
  140. /** Type of a callback to validate whether a given configuration is
  141. * well-formed and consistent. See options_trial_assign() for documentation
  142. * of arguments. */
  143. typedef int (*validate_fn_t)(void*,void*,void*,int,char**);
  144. /** Information on the keys, value types, key-to-struct-member mappings,
  145. * variable descriptions, validation functions, and abbreviations for a
  146. * configuration or storage format. */
  147. typedef struct config_format_t {
  148. size_t size; /**< Size of the struct that everything gets parsed into. */
  149. uint32_t magic; /**< Required 'magic value' to make sure we have a struct
  150. * of the right type. */
  151. off_t magic_offset; /**< Offset of the magic value within the struct. */
  152. config_abbrev_t *abbrevs; /**< List of abbreviations that we expand when
  153. * parsing this format. */
  154. const config_deprecation_t *deprecations; /** List of deprecated options */
  155. config_var_t *vars; /**< List of variables we recognize, their default
  156. * values, and where we stick them in the structure. */
  157. validate_fn_t validate_fn; /**< Function to validate config. */
  158. /** If present, extra is a LINELIST variable for unrecognized
  159. * lines. Otherwise, unrecognized lines are an error. */
  160. config_var_t *extra;
  161. } config_format_t;
  162. /** Macro: assert that <b>cfg</b> has the right magic field for format
  163. * <b>fmt</b>. */
  164. #define CONFIG_CHECK(fmt, cfg) STMT_BEGIN \
  165. tor_assert(fmt && cfg); \
  166. tor_assert((fmt)->magic == \
  167. *(uint32_t*)STRUCT_VAR_P(cfg,fmt->magic_offset)); \
  168. STMT_END
  169. #define CAL_USE_DEFAULTS (1u<<0)
  170. #define CAL_CLEAR_FIRST (1u<<1)
  171. #define CAL_WARN_DEPRECATIONS (1u<<2)
  172. void *config_new(const config_format_t *fmt);
  173. void config_free_(const config_format_t *fmt, void *options);
  174. #define config_free(fmt, options) do { \
  175. config_free_((fmt), (options)); \
  176. (options) = NULL; \
  177. } while (0)
  178. struct config_line_t *config_get_assigned_option(const config_format_t *fmt,
  179. const void *options, const char *key,
  180. int escape_val);
  181. int config_is_same(const config_format_t *fmt,
  182. const void *o1, const void *o2,
  183. const char *name);
  184. void config_init(const config_format_t *fmt, void *options);
  185. void *config_dup(const config_format_t *fmt, const void *old);
  186. char *config_dump(const config_format_t *fmt, const void *default_options,
  187. const void *options, int minimal,
  188. int comment_defaults);
  189. int config_assign(const config_format_t *fmt, void *options,
  190. struct config_line_t *list,
  191. unsigned flags, char **msg);
  192. config_var_t *config_find_option_mutable(config_format_t *fmt,
  193. const char *key);
  194. const char *config_find_deprecation(const config_format_t *fmt,
  195. const char *key);
  196. const config_var_t *config_find_option(const config_format_t *fmt,
  197. const char *key);
  198. const char *config_expand_abbrev(const config_format_t *fmt,
  199. const char *option,
  200. int command_line, int warn_obsolete);
  201. void warn_deprecated_option(const char *what, const char *why);
  202. /* Helper macros to compare an option across two configuration objects */
  203. #define CFG_EQ_BOOL(a,b,opt) ((a)->opt == (b)->opt)
  204. #define CFG_EQ_INT(a,b,opt) ((a)->opt == (b)->opt)
  205. #define CFG_EQ_STRING(a,b,opt) (!strcmp_opt((a)->opt, (b)->opt))
  206. #define CFG_EQ_SMARTLIST(a,b,opt) smartlist_strings_eq((a)->opt, (b)->opt)
  207. #define CFG_EQ_LINELIST(a,b,opt) config_lines_eq((a)->opt, (b)->opt)
  208. #define CFG_EQ_ROUTERSET(a,b,opt) routerset_equal((a)->opt, (b)->opt)
  209. #endif /* !defined(TOR_CONFPARSE_H) */