]> git.ipfire.org Git - people/ms/network.git/blob - src/networkd/config.h
Makefile: Fix typo in localstatedir
[people/ms/network.git] / src / networkd / config.h
1 /*#############################################################################
2 # #
3 # IPFire.org - A linux based firewall #
4 # Copyright (C) 2023 IPFire Network Development Team #
5 # #
6 # This program is free software: you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation, either version 3 of the License, or #
9 # (at your option) any later version. #
10 # #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
15 # #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
18 # #
19 #############################################################################*/
20
21 #ifndef NETWORKD_CONFIG_H
22 #define NETWORKD_CONFIG_H
23
24 #include <dirent.h>
25 #include <stdio.h>
26
27 #define NETWORK_CONFIG_KEY_MAX_LENGTH 128
28 #define NETWORK_CONFIG_VALUE_MAX_LENGTH 2048
29
30 typedef struct nw_config nw_config;
31
32 int nw_config_create(nw_config** config, FILE* f);
33 int nw_config_open(nw_config** config, const char* path);
34
35 nw_config* nw_config_ref(nw_config* config);
36 nw_config* nw_config_unref(nw_config* config);
37
38 int nw_config_copy(nw_config* config, nw_config** copy);
39
40 int nw_config_flush(nw_config* config);
41
42 int nw_config_read(nw_config* config, FILE* f);
43 int nw_config_write(nw_config* config, FILE* f);
44
45 int nw_config_del(nw_config* config, const char* key);
46
47 const char* nw_config_get(nw_config* config, const char* key);
48 int nw_config_set(nw_config* config, const char* key, const char* value);
49
50 int nw_config_get_int(nw_config* config, const char* key, const int __default);
51 int nw_config_set_int(nw_config* config, const char* key, const int value);
52
53 int nw_config_get_bool(nw_config* config, const char* key);
54 int nw_config_set_bool(nw_config* config, const char* key, const int value);
55
56 /*
57 Directory
58 */
59
60 typedef struct nw_configd nw_configd;
61
62 int nw_configd_create(nw_configd** dir, const char* path);
63
64 nw_configd* nw_configd_ref(nw_configd* dir);
65 nw_configd* nw_configd_unref(nw_configd* dir);
66
67 FILE* nw_configd_fopen(nw_configd* dir, const char* path, const char* mode);
68 int nw_configd_open_config(nw_config** config, nw_configd* dir, const char* path);
69 int nw_configd_unlink(nw_configd* dir, const char* path, int flags);
70
71 nw_configd* nw_configd_descend(nw_configd* dir, const char* path);
72
73 typedef int (*nw_configd_walk_callback)(struct dirent* entry, FILE* f, void* data);
74
75 int nw_configd_walk(nw_configd* dir, nw_configd_walk_callback callback, void* data);
76
77 /*
78 Options
79 */
80
81 int nw_config_options_read(nw_config* config);
82 int nw_config_options_write(nw_config* config);
83
84 typedef int (*nw_config_option_read_callback_t)
85 (nw_config* config, const char* key, void* value, const size_t length, void* data);
86 typedef int (*nw_config_option_write_callback_t)
87 (nw_config* config, const char* key, const void* value, const size_t length, void* data);
88
89 int nw_config_option_add(nw_config* config, const char* key, void* value, const size_t length,
90 nw_config_option_read_callback_t read_callback,
91 nw_config_option_write_callback_t write_callback, void* data);
92
93 #define NW_CONFIG_OPTION(config, key, value, length, read_callback, write_callback, data) \
94 nw_config_option_add(config, key, value, length, read_callback, write_callback, data)
95
96 // String
97
98 #define NW_CONFIG_OPTION_STRING(config, key, value) \
99 nw_config_option_add(config, key, value, nw_config_read_string, nw_config_write_string, NULL)
100
101 int nw_config_read_string(nw_config* config,
102 const char* key, void* value, const size_t length, void* data);
103 int nw_config_write_string(nw_config* config,
104 const char* key, const void* value, const size_t length, void* data);
105
106 #define NW_CONFIG_OPTION_STRING_BUFFER(config, key, value) \
107 nw_config_option_add(config, key, value, sizeof(value), \
108 nw_config_read_string_buffer, nw_config_write_string_buffer, NULL)
109
110 int nw_config_read_string_buffer(nw_config* config,
111 const char* key, void* value, const size_t length, void* data);
112 #define nw_config_write_string_buffer nw_config_write_string
113
114 // String Table
115
116 #define NW_CONFIG_OPTION_STRING_TABLE(config, key, value, table) \
117 nw_config_option_add(config, key, value, 0, \
118 nw_config_read_string_table, nw_config_write_string_table, (void*)table)
119
120 int nw_config_read_string_table(nw_config* config,
121 const char* key, void* value, const size_t length, void* data);
122 int nw_config_write_string_table(nw_config* config,
123 const char* key, const void* value, const size_t length, void* data);
124
125 // Integer
126
127 #define NW_CONFIG_OPTION_INT(config, key, value) \
128 nw_config_option_add(config, key, value, 0, nw_config_read_int, nw_config_write_int, NULL)
129
130 int nw_config_read_int(nw_config* config,
131 const char* key, void* value, const size_t length, void* data);
132 int nw_config_write_int(nw_config* config,
133 const char* key, const void* value, const size_t length, void* data);
134
135 // Address
136
137 #define NW_CONFIG_OPTION_ADDRESS(config, key, value) \
138 nw_config_option_add(config, key, value, 0, nw_config_read_address, nw_config_write_address, NULL)
139
140 int nw_config_read_address(nw_config* config,
141 const char* key, void* value, const size_t length, void* data);
142 int nw_config_write_address(nw_config* config,
143 const char* key, const void* value, const size_t length, void* data);
144
145 #endif /* NETWORKD_CONFIG_H */