diff options
Diffstat (limited to 'src/plugin/dns/plugin_block_dns.c')
-rw-r--r-- | src/plugin/dns/plugin_block_dns.c | 290 |
1 files changed, 290 insertions, 0 deletions
diff --git a/src/plugin/dns/plugin_block_dns.c b/src/plugin/dns/plugin_block_dns.c new file mode 100644 index 000000000..1bbd7f750 --- /dev/null +++ b/src/plugin/dns/plugin_block_dns.c | |||
@@ -0,0 +1,290 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2013, 2017 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your 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 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file dns/plugin_block_dns.c | ||
23 | * @brief block plugin for advertising a DNS exit service | ||
24 | * @author Christian Grothoff | ||
25 | * | ||
26 | * Note that this plugin might more belong with EXIT and PT | ||
27 | * as those two are using this type of block. Still, this | ||
28 | * might be a natural enough place for people to find the code... | ||
29 | */ | ||
30 | #include "platform.h" | ||
31 | #include "gnunet_block_plugin.h" | ||
32 | #include "block_dns.h" | ||
33 | #include "gnunet_signatures.h" | ||
34 | #include "gnunet_block_group_lib.h" | ||
35 | |||
36 | |||
37 | /** | ||
38 | * Number of bits we set per entry in the bloomfilter. | ||
39 | * Do not change! | ||
40 | */ | ||
41 | #define BLOOMFILTER_K 16 | ||
42 | |||
43 | |||
44 | /** | ||
45 | * Create a new block group. | ||
46 | * | ||
47 | * @param ctx block context in which the block group is created | ||
48 | * @param type type of the block for which we are creating the group | ||
49 | * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh | ||
50 | * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh | ||
51 | * @param va variable arguments specific to @a type | ||
52 | * @return block group handle, NULL if block groups are not supported | ||
53 | * by this @a type of block (this is not an error) | ||
54 | */ | ||
55 | static struct GNUNET_BLOCK_Group * | ||
56 | block_plugin_dns_create_group (void *cls, | ||
57 | enum GNUNET_BLOCK_Type type, | ||
58 | const void *raw_data, | ||
59 | size_t raw_data_size, | ||
60 | va_list va) | ||
61 | { | ||
62 | unsigned int bf_size; | ||
63 | const char *guard; | ||
64 | |||
65 | guard = va_arg (va, const char *); | ||
66 | if (0 == strcmp (guard, | ||
67 | "seen-set-size")) | ||
68 | bf_size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size (va_arg (va, unsigned | ||
69 | int), | ||
70 | BLOOMFILTER_K); | ||
71 | else if (0 == strcmp (guard, | ||
72 | "filter-size")) | ||
73 | bf_size = va_arg (va, unsigned int); | ||
74 | else | ||
75 | { | ||
76 | GNUNET_break (0); | ||
77 | bf_size = 8; | ||
78 | } | ||
79 | GNUNET_break (NULL == va_arg (va, const char *)); | ||
80 | return GNUNET_BLOCK_GROUP_bf_create (cls, | ||
81 | bf_size, | ||
82 | BLOOMFILTER_K, | ||
83 | type, | ||
84 | raw_data, | ||
85 | raw_data_size); | ||
86 | } | ||
87 | |||
88 | |||
89 | /** | ||
90 | * Function called to validate a query. | ||
91 | * | ||
92 | * @param cls closure | ||
93 | * @param type block type | ||
94 | * @param query original query (hash) | ||
95 | * @param xquery extrended query data (can be NULL, depending on type) | ||
96 | * @param xquery_size number of bytes in @a xquery | ||
97 | * @return #GNUNET_OK if the query is fine, #GNUNET_NO if not | ||
98 | */ | ||
99 | static enum GNUNET_GenericReturnValue | ||
100 | block_plugin_dns_check_query (void *cls, | ||
101 | enum GNUNET_BLOCK_Type type, | ||
102 | const struct GNUNET_HashCode *query, | ||
103 | const void *xquery, | ||
104 | size_t xquery_size) | ||
105 | { | ||
106 | switch (type) | ||
107 | { | ||
108 | case GNUNET_BLOCK_TYPE_DNS: | ||
109 | if (0 != xquery_size) | ||
110 | { | ||
111 | GNUNET_break_op (0); | ||
112 | return GNUNET_NO; | ||
113 | } | ||
114 | return GNUNET_OK; | ||
115 | default: | ||
116 | GNUNET_break (0); | ||
117 | return GNUNET_SYSERR; | ||
118 | } | ||
119 | } | ||
120 | |||
121 | |||
122 | /** | ||
123 | * Function called to validate a block for storage. | ||
124 | * | ||
125 | * @param cls closure | ||
126 | * @param type block type | ||
127 | * @param block block data to validate | ||
128 | * @param block_size number of bytes in @a block | ||
129 | * @return #GNUNET_OK if the block is fine, #GNUNET_NO if not | ||
130 | */ | ||
131 | static enum GNUNET_GenericReturnValue | ||
132 | block_plugin_dns_check_block (void *cls, | ||
133 | enum GNUNET_BLOCK_Type type, | ||
134 | const void *block, | ||
135 | size_t block_size) | ||
136 | { | ||
137 | const struct GNUNET_DNS_Advertisement *ad; | ||
138 | |||
139 | switch (type) | ||
140 | { | ||
141 | case GNUNET_BLOCK_TYPE_DNS: | ||
142 | if (sizeof(struct GNUNET_DNS_Advertisement) != block_size) | ||
143 | { | ||
144 | GNUNET_break_op (0); | ||
145 | return GNUNET_NO; | ||
146 | } | ||
147 | ad = block; | ||
148 | |||
149 | if (ntohl (ad->purpose.size) != | ||
150 | sizeof(struct GNUNET_DNS_Advertisement) | ||
151 | - sizeof(struct GNUNET_CRYPTO_EddsaSignature)) | ||
152 | { | ||
153 | GNUNET_break_op (0); | ||
154 | return GNUNET_NO; | ||
155 | } | ||
156 | if (GNUNET_TIME_absolute_is_past ( | ||
157 | GNUNET_TIME_absolute_ntoh (ad->expiration_time))) | ||
158 | { | ||
159 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
160 | "DNS advertisement has expired\n"); | ||
161 | return GNUNET_NO; | ||
162 | } | ||
163 | if (GNUNET_OK != | ||
164 | GNUNET_CRYPTO_eddsa_verify_ (GNUNET_SIGNATURE_PURPOSE_DNS_RECORD, | ||
165 | &ad->purpose, | ||
166 | &ad->signature, | ||
167 | &ad->peer.public_key)) | ||
168 | { | ||
169 | GNUNET_break_op (0); | ||
170 | return GNUNET_NO; | ||
171 | } | ||
172 | return GNUNET_OK; | ||
173 | default: | ||
174 | GNUNET_break (0); | ||
175 | return GNUNET_SYSERR; | ||
176 | } | ||
177 | } | ||
178 | |||
179 | |||
180 | /** | ||
181 | * Function called to validate a reply to a request. Note that it is assumed | ||
182 | * that the reply has already been matched to the key (and signatures checked) | ||
183 | * as it would be done with the GetKeyFunction and the | ||
184 | * BlockEvaluationFunction. | ||
185 | * | ||
186 | * @param cls closure | ||
187 | * @param type block type | ||
188 | * @param group which block group to use for evaluation | ||
189 | * @param query original query (hash) | ||
190 | * @param xquery extrended query data (can be NULL, depending on type) | ||
191 | * @param xquery_size number of bytes in @a xquery | ||
192 | * @param reply_block response to validate | ||
193 | * @param reply_block_size number of bytes in @a reply_block | ||
194 | * @return characterization of result | ||
195 | */ | ||
196 | static enum GNUNET_BLOCK_ReplyEvaluationResult | ||
197 | block_plugin_dns_check_reply ( | ||
198 | void *cls, | ||
199 | enum GNUNET_BLOCK_Type type, | ||
200 | struct GNUNET_BLOCK_Group *group, | ||
201 | const struct GNUNET_HashCode *query, | ||
202 | const void *xquery, | ||
203 | size_t xquery_size, | ||
204 | const void *reply_block, | ||
205 | size_t reply_block_size) | ||
206 | { | ||
207 | struct GNUNET_HashCode phash; | ||
208 | |||
209 | switch (type) | ||
210 | { | ||
211 | case GNUNET_BLOCK_TYPE_DNS: | ||
212 | GNUNET_CRYPTO_hash (reply_block, | ||
213 | reply_block_size, | ||
214 | &phash); | ||
215 | if (GNUNET_YES == | ||
216 | GNUNET_BLOCK_GROUP_bf_test_and_set (group, | ||
217 | &phash)) | ||
218 | return GNUNET_BLOCK_REPLY_OK_DUPLICATE; | ||
219 | return GNUNET_BLOCK_REPLY_OK_MORE; | ||
220 | default: | ||
221 | GNUNET_break (0); | ||
222 | return GNUNET_BLOCK_REPLY_TYPE_NOT_SUPPORTED; | ||
223 | } | ||
224 | } | ||
225 | |||
226 | |||
227 | /** | ||
228 | * Function called to obtain the key for a block. | ||
229 | * | ||
230 | * @param cls closure | ||
231 | * @param type block type | ||
232 | * @param block block to get the key for | ||
233 | * @param block_size number of bytes in @a block | ||
234 | * @param key set to the key (query) for the given block | ||
235 | * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported | ||
236 | * (or if extracting a key from a block of this type does not work) | ||
237 | */ | ||
238 | static enum GNUNET_GenericReturnValue | ||
239 | block_plugin_dns_get_key (void *cls, | ||
240 | enum GNUNET_BLOCK_Type type, | ||
241 | const void *block, | ||
242 | size_t block_size, | ||
243 | struct GNUNET_HashCode *key) | ||
244 | { | ||
245 | if (GNUNET_BLOCK_TYPE_DNS != type) | ||
246 | { | ||
247 | GNUNET_break (0); | ||
248 | return GNUNET_SYSERR; | ||
249 | } | ||
250 | return GNUNET_NO; | ||
251 | } | ||
252 | |||
253 | |||
254 | /** | ||
255 | * Entry point for the plugin. | ||
256 | */ | ||
257 | void * | ||
258 | libgnunet_plugin_block_dns_init (void *cls) | ||
259 | { | ||
260 | static enum GNUNET_BLOCK_Type types[] = { | ||
261 | GNUNET_BLOCK_TYPE_DNS, | ||
262 | GNUNET_BLOCK_TYPE_ANY /* end of list */ | ||
263 | }; | ||
264 | struct GNUNET_BLOCK_PluginFunctions *api; | ||
265 | |||
266 | api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions); | ||
267 | api->get_key = &block_plugin_dns_get_key; | ||
268 | api->check_query = &block_plugin_dns_check_query; | ||
269 | api->check_block = &block_plugin_dns_check_block; | ||
270 | api->check_reply = &block_plugin_dns_check_reply; | ||
271 | api->create_group = &block_plugin_dns_create_group; | ||
272 | api->types = types; | ||
273 | return api; | ||
274 | } | ||
275 | |||
276 | |||
277 | /** | ||
278 | * Exit point from the plugin. | ||
279 | */ | ||
280 | void * | ||
281 | libgnunet_plugin_block_dns_done (void *cls) | ||
282 | { | ||
283 | struct GNUNET_BLOCK_PluginFunctions *api = cls; | ||
284 | |||
285 | GNUNET_free (api); | ||
286 | return NULL; | ||
287 | } | ||
288 | |||
289 | |||
290 | /* end of plugin_block_dns.c */ | ||