messages for inter-controller overlay connect
[oweals/gnunet.git] / src / include / gnunet_configuration_lib.h
1 /*
2      This file is part of GNUnet.
3      (C) 2006, 2008, 2009 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 2, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file include/gnunet_configuration_lib.h
23  * @brief configuration API
24  *
25  * @author Christian Grothoff
26  */
27
28 #ifndef GNUNET_CONFIGURATION_LIB_H
29 #define GNUNET_CONFIGURATION_LIB_H
30
31
32 #ifdef __cplusplus
33 extern "C"
34 {
35 #if 0                           /* keep Emacsens' auto-indent happy */
36 }
37 #endif
38 #endif
39
40 #include "gnunet_common.h"
41 #include "gnunet_time_lib.h"
42
43 /**
44  * A configuration object.
45  */
46 struct GNUNET_CONFIGURATION_Handle;
47
48 /**
49  * Create a new configuration object.
50  * @return fresh configuration object
51  */
52 struct GNUNET_CONFIGURATION_Handle *
53 GNUNET_CONFIGURATION_create (void);
54
55
56 /**
57  * Duplicate an existing configuration object.
58  *
59  * @param cfg configuration to duplicate
60  * @return duplicate configuration
61  */
62 struct GNUNET_CONFIGURATION_Handle *
63 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg);
64
65
66 /**
67  * Destroy configuration object.
68  *
69  * @param cfg configuration to destroy
70  */
71 void
72 GNUNET_CONFIGURATION_destroy (struct GNUNET_CONFIGURATION_Handle *cfg);
73
74
75 /**
76  * Load configuration.  This function will first parse the
77  * defaults and then parse the specific configuration file
78  * to overwrite the defaults.
79  *
80  * @param cfg configuration to update
81  * @param filename name of the configuration file, NULL to load defaults
82  * @return GNUNET_OK on success, GNUNET_SYSERR on error
83  */
84 int
85 GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
86                            const char *filename);
87
88
89 /**
90  * Load default configuration.  This function will parse the
91  * defaults from the given defaults_d directory.
92  *
93  * @param cfg configuration to update
94  * @param defaults_d directory with the defaults
95  * @return GNUNET_OK on success, GNUNET_SYSERR on error
96  */
97 int
98 GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg,
99                                 const char *defaults_d);
100
101
102 /**
103  * Parse a configuration file, add all of the options in the
104  * file to the configuration environment.
105  *
106  * @param cfg configuration to update
107  * @param filename name of the configuration file
108  * @return GNUNET_OK on success, GNUNET_SYSERR on error
109  */
110 int
111 GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
112                             const char *filename);
113
114
115 /**
116  * Serializes the given configuration.
117  *
118  * @param cfg configuration to serialize
119  * @param size will be set to the size of the serialized memory block
120  * @return the memory block where the serialized configuration is
121  *           present. This memory should be freed by the caller
122  */
123 char *
124 GNUNET_CONFIGURATION_serialize (const struct GNUNET_CONFIGURATION_Handle *cfg,
125                                 size_t *size);
126
127
128 /**
129  * De-serializes configuration
130  *
131  * @param cfg configuration to update
132  * @param mem the memory block of serialized configuration
133  * @param size the size of the memory block
134  * @param allow_inline set to GNUNET_YES if we recursively load configuration
135  *          from inlined configurations; GNUNET_NO if not and raise warnings
136  *          when we come across them
137  * @return GNUNET_OK on success, GNUNET_ERROR on error
138  */
139 int
140 GNUNET_CONFIGURATION_deserialize (struct GNUNET_CONFIGURATION_Handle *cfg,
141                                   const char *mem,
142                                   const size_t size,
143                                   int allow_inline);
144
145
146 /**
147  * Write configuration file.
148  *
149  * @param cfg configuration to write
150  * @param filename where to write the configuration
151  * @return GNUNET_OK on success, GNUNET_SYSERR on error
152  */
153 int
154 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
155                             const char *filename);
156
157 /**
158  * Write only configuration entries that have been changed to configuration file
159  * @param cfgDefault default configuration
160  * @param cfgNew new configuration
161  * @param filename where to write the configuration diff between default and new
162  * @return GNUNET_OK on success, GNUNET_SYSERR on error
163  */
164 int
165 GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
166                                   *cfgDefault,
167                                   const struct GNUNET_CONFIGURATION_Handle
168                                   *cfgNew, const char *filename);
169
170 /**
171  * Test if there are configuration options that were
172  * changed since the last save.
173  *
174  * @param cfg configuration to inspect
175  * @return GNUNET_NO if clean, GNUNET_YES if dirty, GNUNET_SYSERR on error (i.e. last save failed)
176  */
177 int
178 GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg);
179
180
181 /**
182  * Function to iterate over options.
183  *
184  * @param cls closure
185  * @param section name of the section
186  * @param option name of the option
187  * @param value value of the option
188  */
189 typedef void (*GNUNET_CONFIGURATION_Iterator) (void *cls, const char *section,
190                                                const char *option,
191                                                const char *value);
192
193
194 /**
195  * Function to iterate over section.
196  *
197  * @param cls closure
198  * @param section name of the section
199  */
200 typedef void (*GNUNET_CONFIGURATION_Section_Iterator) (void *cls,
201                                                        const char *section);
202
203
204 /**
205  * Iterate over all options in the configuration.
206  *
207  * @param cfg configuration to inspect
208  * @param iter function to call on each option
209  * @param iter_cls closure for iter
210  */
211 void
212 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
213                               GNUNET_CONFIGURATION_Iterator iter,
214                               void *iter_cls);
215
216
217 /**
218  * Iterate over all sections in the configuration.
219  *
220  * @param cfg configuration to inspect
221  * @param iter function to call on each section
222  * @param iter_cls closure for iter
223  */
224 void
225 GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle
226                                        *cfg,
227                                        GNUNET_CONFIGURATION_Section_Iterator
228                                        iter, void *iter_cls);
229
230
231 /**
232  * Remove the given section and all options in it.
233  *
234  * @param cfg configuration to inspect
235  * @param section name of the section to remove
236  */
237 void
238 GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
239                                      const char *section);
240
241 /**
242  * Get a configuration value that should be a number.
243  *
244  * @param cfg configuration to inspect
245  * @param section section of interest
246  * @param option option of interest
247  * @param number where to store the numeric value of the option
248  * @return GNUNET_OK on success, GNUNET_SYSERR on error
249  */
250 int
251 GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
252                                        *cfg, const char *section,
253                                        const char *option,
254                                        unsigned long long *number);
255
256
257 /**
258  * Get a configuration value that should be a relative time.
259  *
260  * @param cfg configuration to inspect
261  * @param section section of interest
262  * @param option option of interest
263  * @param time set to the time value stored in the configuration
264  * @return GNUNET_OK on success, GNUNET_SYSERR on error
265  */
266 int
267 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
268                                      *cfg, const char *section,
269                                      const char *option,
270                                      struct GNUNET_TIME_Relative *time);
271
272
273
274 /**
275  * Get a configuration value that should be a size in bytes.
276  *
277  * @param cfg configuration to inspect
278  * @param section section of interest
279  * @param option option of interest
280  * @param size set to the size in bytes as stored in the configuration
281  * @return GNUNET_OK on success, GNUNET_SYSERR on error
282  */
283 int
284 GNUNET_CONFIGURATION_get_value_size (const struct GNUNET_CONFIGURATION_Handle
285                                      *cfg, const char *section,
286                                      const char *option,
287                                      unsigned long long *size);
288
289
290 /**
291  * Test if we have a value for a particular option
292  *
293  * @param cfg configuration to inspect
294  * @param section section of interest
295  * @param option option of interest
296  * @return GNUNET_YES if so, GNUNET_NO if not.
297  */
298 int
299 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
300                                  const char *section, const char *option);
301
302
303 /**
304  * Get a configuration value that should be a string.
305  *
306  * @param cfg configuration to inspect
307  * @param section section of interest
308  * @param option option of interest
309  * @param value will be set to a freshly allocated configuration
310  *        value, or NULL if option is not specified
311  * @return GNUNET_OK on success, GNUNET_SYSERR on error
312  */
313 int
314 GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle
315                                        *cfg, const char *section,
316                                        const char *option, char **value);
317
318
319 /**
320  * Get a configuration value that should be the name of a file
321  * or directory.
322  *
323  * @param cfg configuration to inspect
324  * @param section section of interest
325  * @param option option of interest
326  * @param value will be set to a freshly allocated configuration
327  *        value, or NULL if option is not specified
328  * @return GNUNET_OK on success, GNUNET_SYSERR on error
329  */
330 int
331 GNUNET_CONFIGURATION_get_value_filename (const struct
332                                          GNUNET_CONFIGURATION_Handle *cfg,
333                                          const char *section,
334                                          const char *option, char **value);
335
336 /**
337  * Iterate over the set of filenames stored in a configuration value.
338  *
339  * @param cfg configuration to inspect
340  * @param section section of interest
341  * @param option option of interest
342  * @param cb function to call on each filename
343  * @param cb_cls closure for cb
344  * @return number of filenames iterated over, -1 on error
345  */
346 int
347 GNUNET_CONFIGURATION_iterate_value_filenames (const struct
348                                               GNUNET_CONFIGURATION_Handle *cfg,
349                                               const char *section,
350                                               const char *option,
351                                               GNUNET_FileNameCallback cb,
352                                               void *cb_cls);
353
354 /**
355  * Iterate over values of a section in the configuration.
356  *
357  * @param cfg configuration to inspect
358  * @param section the section
359  * @param iter function to call on each option
360  * @param iter_cls closure for iter
361  */
362 void
363 GNUNET_CONFIGURATION_iterate_section_values (const struct
364                                              GNUNET_CONFIGURATION_Handle *cfg,
365                                              const char *section,
366                                              GNUNET_CONFIGURATION_Iterator iter,
367                                              void *iter_cls);
368
369 /**
370  * Get a configuration value that should be in a set of
371  * predefined strings
372  *
373  * @param cfg configuration to inspect
374  * @param section section of interest
375  * @param option option of interest
376  * @param choices NULL-terminated list of legal values
377  * @param value will be set to an entry in the legal list,
378  *        or NULL if option is not specified and no default given
379  * @return GNUNET_OK on success, GNUNET_SYSERR on error
380  */
381 int
382 GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle
383                                        *cfg, const char *section,
384                                        const char *option, const char **choices,
385                                        const char **value);
386
387 /**
388  * Get a configuration value that should be in a set of
389  * "YES" or "NO".
390  *
391  * @param cfg configuration to inspect
392  * @param section section of interest
393  * @param option option of interest
394  * @return GNUNET_YES, GNUNET_NO or if option has no valid value, GNUNET_SYSERR
395  */
396 int
397 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle
398                                       *cfg, const char *section,
399                                       const char *option);
400
401
402 /**
403  * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
404  * where either in the "PATHS" section or the environtment
405  * "FOO" is set to "DIRECTORY".
406  *
407  * @param cfg configuration to use for path expansion
408  * @param orig string to $-expand (will be freed!)
409  * @return $-expanded string
410  */
411 char *
412 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle
413                                     *cfg, char *orig);
414
415
416 /**
417  * Set a configuration value that should be a number.
418  *
419  * @param cfg configuration to update
420  * @param section section of interest
421  * @param option option of interest
422  * @param number value to set
423  */
424 void
425 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
426                                        const char *section, const char *option,
427                                        unsigned long long number);
428
429
430 /**
431  * Set a configuration value that should be a string.
432  *
433  * @param cfg configuration to update
434  * @param section section of interest
435  * @param option option of interest
436  * @param value value to set
437  */
438 void
439 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
440                                        const char *section, const char *option,
441                                        const char *value);
442
443
444 /**
445  * Remove a filename from a configuration value that
446  * represents a list of filenames
447  *
448  * @param cfg configuration to update
449  * @param section section of interest
450  * @param option option of interest
451  * @param value filename to remove
452  * @return GNUNET_OK on success,
453  *         GNUNET_SYSERR if the filename is not in the list
454  */
455 int
456 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
457                                             *cfg, const char *section,
458                                             const char *option,
459                                             const char *value);
460
461 /**
462  * Append a filename to a configuration value that
463  * represents a list of filenames
464  *
465  * @param cfg configuration to update
466  * @param section section of interest
467  * @param option option of interest
468  * @param value filename to append
469  * @return GNUNET_OK on success,
470  *         GNUNET_SYSERR if the filename already in the list
471  */
472 int
473 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
474                                             *cfg, const char *section,
475                                             const char *option,
476                                             const char *value);
477
478
479 #if 0                           /* keep Emacsens' auto-indent happy */
480 {
481 #endif
482 #ifdef __cplusplus
483 }
484 #endif
485
486 #endif