]> git.ipfire.org Git - thirdparty/bird.git/blob - conf/conf.h
Filter: Add more tests for append operator
[thirdparty/bird.git] / conf / conf.h
1 /*
2 * BIRD Internet Routing Daemon -- Configuration File Handling
3 *
4 * (c) 1998--2000 Martin Mares <mj@ucw.cz>
5 *
6 * Can be freely distributed and used under the terms of the GNU GPL.
7 */
8
9 #ifndef _BIRD_CONF_H_
10 #define _BIRD_CONF_H_
11
12 #include "sysdep/config.h"
13 #include "lib/ip.h"
14 #include "lib/hash.h"
15 #include "lib/resource.h"
16 #include "lib/timer.h"
17 #include "lib/tlists.h"
18
19 /* Configuration structure */
20 struct config {
21 pool *pool; /* Pool the configuration is stored in */
22 linpool *mem; /* Linear pool containing configuration data */
23 list protos; /* Configured protocol instances (struct proto_config) */
24 list tables; /* Configured routing tables (struct rtable_config) */
25 list mpls_domains; /* Configured MPLS domains (struct mpls_domain_config) */
26 list logfiles; /* Configured log files (sysdep) */
27 list tests; /* Configured unit tests (f_bt_test_suite) */
28 list symbols; /* Configured symbols in config order */
29 TLIST_STRUCT_DEF(cli_config, struct cli_config) cli; /* Configured CLI sockets */
30
31 int mrtdump_file; /* Configured MRTDump file (sysdep, fd in unix) */
32 const char *syslog_name; /* Name used for syslog (NULL -> no syslog) */
33 struct rtable_config *def_tables[NET_MAX]; /* Default routing tables for each network */
34 struct iface_patt *router_id_from; /* Configured list of router ID iface patterns */
35
36 u32 router_id; /* Our Router ID */
37 const char *hostname; /* Hostname */
38 u32 proto_default_debug; /* Default protocol debug mask */
39 u32 proto_default_mrtdump; /* Default protocol mrtdump mask */
40 u32 channel_default_debug; /* Default channel debug mask */
41 u32 table_default_debug; /* Default table debug mask */
42 struct timeformat tf_route; /* Time format for 'show route' */
43 struct timeformat tf_proto; /* Time format for 'show protocol' */
44 struct timeformat tf_log; /* Time format for the logfile */
45 struct timeformat tf_base; /* Time format for other purposes */
46 u32 gr_wait; /* Graceful restart wait timeout (sec) */
47
48 int cli_debug; /* Tracing of CLI connections and commands */
49 int latency_debug; /* I/O loop tracks duration of each event */
50 u32 latency_limit; /* Events with longer duration are logged (us) */
51 u32 watchdog_warning; /* I/O loop watchdog limit for warning (us) */
52 u32 watchdog_timeout; /* Watchdog timeout (in seconds, 0 = disabled) */
53 char *err_msg; /* Parser error message */
54 int err_lino; /* Line containing error */
55 int err_chno; /* Character where the parser stopped */
56 char *err_file_name; /* File name containing error */
57 char *file_name; /* Name of main configuration file */
58 int file_fd; /* File descriptor of main configuration file */
59
60 struct sym_scope *root_scope; /* Scope for root symbols */
61 struct sym_scope *current_scope; /* Current scope where we are actually in while parsing */
62 int obstacle_count; /* Number of items blocking freeing of this config */
63 int shutdown; /* This is a pseudo-config for daemon shutdown */
64 int gr_down; /* This is a pseudo-config for graceful restart */
65 btime load_time; /* When we've got this configuration */
66 };
67
68 /* Please don't use these variables in protocols. Use proto_config->global instead. */
69 extern struct config *config; /* Currently active configuration */
70 extern struct config *new_config; /* Configuration being parsed */
71
72 struct config *config_alloc(const char *name);
73 int config_parse(struct config *);
74 int cli_parse(struct config *);
75 void config_free(struct config *);
76 void config_free_old(void);
77 int config_commit(struct config *, int type, uint timeout);
78 int config_confirm(void);
79 int config_undo(void);
80 int config_status(void);
81 btime config_timer_status(void);
82 void config_init(void);
83 void cf_error(const char *msg, ...) NORET;
84 #define cf_warn(msg, args...) log(L_WARN "%s:%d:%d: " msg, ifs->file_name, ifs->lino, ifs->chno - ifs->toklen + 1, ##args)
85 void config_add_obstacle(struct config *);
86 void config_del_obstacle(struct config *);
87 void order_shutdown(int gr);
88
89 #define RECONFIG_NONE 0
90 #define RECONFIG_HARD 1
91 #define RECONFIG_SOFT 2
92 #define RECONFIG_UNDO 3
93
94 #define CONF_DONE 0
95 #define CONF_PROGRESS 1
96 #define CONF_QUEUED 2
97 #define CONF_UNQUEUED 3
98 #define CONF_CONFIRM 4
99 #define CONF_SHUTDOWN -1
100 #define CONF_NOTHING -2
101
102
103 /* Pools */
104 extern pool *config_pool;
105 extern linpool *cfg_mem;
106
107 #define cfg_alloc(size) lp_alloc(cfg_mem, size)
108 #define cfg_allocu(size) lp_allocu(cfg_mem, size)
109 #define cfg_allocz(size) lp_allocz(cfg_mem, size)
110 char *cfg_strdup(const char *c);
111 void cfg_copy_list(list *dest, list *src, unsigned node_size);
112
113 /* Lexer */
114
115 extern int (*cf_read_hook)(byte *buf, uint max, int fd);
116
117 struct keyword {
118 byte *name;
119 int value;
120 };
121
122 struct symbol {
123 node n; /* In list of symbols in config */
124 struct symbol *next;
125 struct sym_scope *scope;
126 int class; /* SYM_* */
127 uint flags; /* SYM_FLAG_* */
128
129 union {
130 struct proto_config *proto; /* For SYM_PROTO and SYM_TEMPLATE */
131 const struct f_line *function; /* For SYM_FUNCTION */
132 const struct filter *filter; /* For SYM_FILTER */
133 struct rtable_config *table; /* For SYM_TABLE */
134 struct f_dynamic_attr *attribute; /* For SYM_ATTRIBUTE */
135 struct mpls_domain_config *mpls_domain; /* For SYM_MPLS_DOMAIN */
136 struct mpls_range_config *mpls_range; /* For SYM_MPLS_RANGE */
137 struct f_val *val; /* For SYM_CONSTANT */
138 uint offset; /* For SYM_VARIABLE */
139 const struct keyword *keyword; /* For SYM_KEYWORD */
140 const struct f_method *method; /* For SYM_METHOD */
141 };
142
143 char name[0];
144 };
145
146 struct sym_scope {
147 struct sym_scope *next; /* Next on scope stack */
148 struct symbol *name; /* Name of this scope */
149
150 HASH(struct symbol) hash; /* Local symbol hash */
151
152 uint slots; /* Variable slots */
153 byte soft_scopes; /* Number of soft scopes above */
154 byte active:1; /* Currently entered */
155 byte block:1; /* No independent stack frame */
156 byte readonly:1; /* Do not add new symbols */
157 };
158
159 extern struct sym_scope *global_root_scope;
160 extern pool *global_root_scope_pool;
161 extern linpool *global_root_scope_linpool;
162
163
164 #define SYM_MAX_LEN 64
165
166 /* Remember to update cf_symbol_class_name() */
167 #define SYM_VOID 0
168 #define SYM_PROTO 1
169 #define SYM_TEMPLATE 2
170 #define SYM_FUNCTION 3
171 #define SYM_FILTER 4
172 #define SYM_TABLE 5
173 #define SYM_ATTRIBUTE 6
174 #define SYM_KEYWORD 7
175 #define SYM_METHOD 8
176 #define SYM_MPLS_DOMAIN 9
177 #define SYM_MPLS_RANGE 10
178
179 #define SYM_VARIABLE 0x100 /* 0x100-0x1ff are variable types */
180 #define SYM_VARIABLE_RANGE SYM_VARIABLE ... (SYM_VARIABLE | 0xff)
181 #define SYM_CONSTANT 0x200 /* 0x200-0x2ff are variable types */
182 #define SYM_CONSTANT_RANGE SYM_CONSTANT ... (SYM_CONSTANT | 0xff)
183
184 #define SYM_TYPE(s) ((s)->val->type)
185 #define SYM_VAL(s) ((s)->val->val)
186
187 /* Symbol flags */
188 #define SYM_FLAG_SAME 0x1 /* For SYM_FUNCTION and SYM_FILTER */
189
190 struct include_file_stack {
191 void *buffer; /* Internal lexer state */
192 char *file_name; /* File name */
193 int fd; /* File descriptor */
194 int lino; /* Current line num */
195 int chno; /* Current char num (on current line) */
196 int toklen; /* Current token length */
197 int depth; /* Include depth, 0 = cannot include */
198
199 struct include_file_stack *prev; /* Previous record in stack */
200 struct include_file_stack *up; /* Parent (who included this file) */
201 };
202
203 extern struct include_file_stack *ifs;
204
205 int cf_lex(void);
206 void cf_lex_init(int is_cli, struct config *c);
207 void cf_lex_unwind(void);
208
209 struct symbol *cf_find_symbol_scope(const struct sym_scope *scope, const byte *c);
210 static inline struct symbol *cf_find_symbol_cfg(const struct config *cfg, const byte *c)
211 { return cf_find_symbol_scope(cfg->root_scope, c); }
212
213 #define cf_find_symbol(where, what) _Generic(*(where), \
214 struct config: cf_find_symbol_cfg, \
215 struct sym_scope: cf_find_symbol_scope \
216 )((where), (what))
217
218 struct symbol *cf_get_symbol(struct config *conf, const byte *c);
219 struct symbol *cf_default_name(struct config *conf, char *template, int *counter);
220 struct symbol *cf_localize_symbol(struct config *conf, struct symbol *sym);
221
222 static inline int cf_symbol_is_local(struct config *conf, struct symbol *sym)
223 { return (sym->scope == conf->current_scope) && !conf->current_scope->soft_scopes; }
224
225 /* internal */
226 struct symbol *cf_new_symbol(struct sym_scope *scope, pool *p, struct linpool *lp, const byte *c);
227
228 /**
229 * cf_define_symbol - define meaning of a symbol
230 * @sym: symbol to be defined
231 * @type: symbol class to assign
232 * @def: class dependent data
233 *
234 * Defines new meaning of a symbol. If the symbol is an undefined
235 * one (%SYM_VOID), it's just re-defined to the new type. If it's defined
236 * in different scope, a new symbol in current scope is created and the
237 * meaning is assigned to it. If it's already defined in the current scope,
238 * an error is reported via cf_error().
239 *
240 * Result: Pointer to the newly defined symbol. If we are in the top-level
241 * scope, it's the same @sym as passed to the function.
242 */
243 #define cf_define_symbol(conf_, osym_, type_, var_, def_) ({ \
244 struct symbol *sym_ = cf_localize_symbol(conf_, osym_); \
245 sym_->class = type_; \
246 sym_->var_ = def_; \
247 sym_; })
248
249 #define cf_create_symbol(conf_, name_, type_, var_, def_) \
250 cf_define_symbol(conf_, cf_get_symbol(conf_, name_), type_, var_, def_)
251
252 void cf_push_scope(struct config *, struct symbol *);
253 void cf_pop_scope(struct config *);
254 void cf_push_soft_scope(struct config *);
255 void cf_pop_soft_scope(struct config *);
256
257 static inline void cf_push_block_scope(struct config *conf)
258 { cf_push_scope(conf, NULL); conf->current_scope->block = 1; }
259
260 static inline void cf_pop_block_scope(struct config *conf)
261 { ASSERT(conf->current_scope->block); cf_pop_scope(conf); }
262
263 char *cf_symbol_class_name(struct symbol *sym);
264
265 /* Parser */
266
267 extern char *cf_text;
268 int cf_parse(void);
269
270 /* Sysdep hooks */
271
272 void sysdep_preconfig(struct config *);
273 int sysdep_commit(struct config *, struct config *);
274 void sysdep_shutdown_done(void);
275
276 #endif