diff options
Diffstat (limited to 'src/namecache/gnunet-service-namecache.c')
-rw-r--r-- | src/namecache/gnunet-service-namecache.c | 391 |
1 files changed, 0 insertions, 391 deletions
diff --git a/src/namecache/gnunet-service-namecache.c b/src/namecache/gnunet-service-namecache.c deleted file mode 100644 index 07bf30de9..000000000 --- a/src/namecache/gnunet-service-namecache.c +++ /dev/null | |||
@@ -1,391 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2012, 2013 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 namecache/gnunet-service-namecache.c | ||
23 | * @brief namecache for the GNUnet naming system | ||
24 | * @author Matthias Wachs | ||
25 | * @author Christian Grothoff | ||
26 | */ | ||
27 | #include "platform.h" | ||
28 | #include "gnunet_util_lib.h" | ||
29 | #include "gnunet_dnsparser_lib.h" | ||
30 | #include "gnunet_statistics_service.h" | ||
31 | #include "gnunet_namecache_service.h" | ||
32 | #include "gnunet_namecache_plugin.h" | ||
33 | #include "gnunet_signatures.h" | ||
34 | #include "namecache.h" | ||
35 | |||
36 | #define LOG_STRERROR_FILE(kind, syscall, \ | ||
37 | filename) GNUNET_log_from_strerror_file (kind, "util", \ | ||
38 | syscall, \ | ||
39 | filename) | ||
40 | |||
41 | |||
42 | /** | ||
43 | * A namecache client | ||
44 | */ | ||
45 | struct NamecacheClient | ||
46 | { | ||
47 | /** | ||
48 | * The client | ||
49 | */ | ||
50 | struct GNUNET_SERVICE_Client *client; | ||
51 | |||
52 | /** | ||
53 | * The message queue to talk to @e client. | ||
54 | */ | ||
55 | struct GNUNET_MQ_Handle *mq; | ||
56 | }; | ||
57 | |||
58 | |||
59 | /** | ||
60 | * Configuration handle. | ||
61 | */ | ||
62 | static const struct GNUNET_CONFIGURATION_Handle *GSN_cfg; | ||
63 | |||
64 | /** | ||
65 | * Handle to the statistics service | ||
66 | */ | ||
67 | static struct GNUNET_STATISTICS_Handle *statistics; | ||
68 | |||
69 | /** | ||
70 | * Database handle | ||
71 | */ | ||
72 | static struct GNUNET_NAMECACHE_PluginFunctions *GSN_database; | ||
73 | |||
74 | /** | ||
75 | * Name of the database plugin | ||
76 | */ | ||
77 | static char *db_lib_name; | ||
78 | |||
79 | |||
80 | /** | ||
81 | * Task run during shutdown. | ||
82 | * | ||
83 | * @param cls unused | ||
84 | */ | ||
85 | static void | ||
86 | cleanup_task (void *cls) | ||
87 | { | ||
88 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
89 | "Stopping namecache service\n"); | ||
90 | GNUNET_break (NULL == | ||
91 | GNUNET_PLUGIN_unload (db_lib_name, | ||
92 | GSN_database)); | ||
93 | GNUNET_free (db_lib_name); | ||
94 | db_lib_name = NULL; | ||
95 | if (NULL != statistics) | ||
96 | { | ||
97 | GNUNET_STATISTICS_destroy (statistics, | ||
98 | GNUNET_NO); | ||
99 | statistics = NULL; | ||
100 | } | ||
101 | } | ||
102 | |||
103 | |||
104 | /** | ||
105 | * Called whenever a client is disconnected. | ||
106 | * Frees our resources associated with that client. | ||
107 | * | ||
108 | * @param cls closure | ||
109 | * @param client identification of the client | ||
110 | * @param app_ctx the `struct NamecacheClient` for this @a client | ||
111 | */ | ||
112 | static void | ||
113 | client_disconnect_cb (void *cls, | ||
114 | struct GNUNET_SERVICE_Client *client, | ||
115 | void *app_ctx) | ||
116 | { | ||
117 | struct NamecacheClient *nc = app_ctx; | ||
118 | |||
119 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
120 | "Client %p disconnected\n", | ||
121 | client); | ||
122 | GNUNET_free (nc); | ||
123 | } | ||
124 | |||
125 | |||
126 | /** | ||
127 | * Add a client to our list of active clients. | ||
128 | * | ||
129 | * @param cls NULL | ||
130 | * @param client client to add | ||
131 | * @param mq queue to talk to @a client | ||
132 | * @return internal namecache client structure for this client | ||
133 | */ | ||
134 | static void * | ||
135 | client_connect_cb (void *cls, | ||
136 | struct GNUNET_SERVICE_Client *client, | ||
137 | struct GNUNET_MQ_Handle *mq) | ||
138 | { | ||
139 | struct NamecacheClient *nc; | ||
140 | |||
141 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
142 | "Client %p connected\n", | ||
143 | client); | ||
144 | nc = GNUNET_new (struct NamecacheClient); | ||
145 | nc->client = client; | ||
146 | nc->mq = mq; | ||
147 | return nc; | ||
148 | } | ||
149 | |||
150 | |||
151 | /** | ||
152 | * Context for name lookups passed from #handle_lookup_block to | ||
153 | * #handle_lookup_block_it as closure | ||
154 | */ | ||
155 | struct LookupBlockContext | ||
156 | { | ||
157 | /** | ||
158 | * The client to send the response to | ||
159 | */ | ||
160 | struct NamecacheClient *nc; | ||
161 | |||
162 | /** | ||
163 | * Operation id for the name lookup | ||
164 | */ | ||
165 | uint32_t request_id; | ||
166 | |||
167 | /** | ||
168 | * Lookup status | ||
169 | */ | ||
170 | int status; | ||
171 | }; | ||
172 | |||
173 | |||
174 | /** | ||
175 | * A #GNUNET_NAMECACHE_BlockCallback for name lookups in #handle_lookup_block | ||
176 | * | ||
177 | * @param cls a `struct LookupNameContext *` with information about the request | ||
178 | * @param block the block | ||
179 | */ | ||
180 | static void | ||
181 | handle_lookup_block_it (void *cls, | ||
182 | const struct GNUNET_GNSRECORD_Block *block) | ||
183 | { | ||
184 | struct LookupBlockContext *lnc = cls; | ||
185 | struct GNUNET_MQ_Envelope *env; | ||
186 | struct LookupBlockResponseMessage *r; | ||
187 | size_t bsize; | ||
188 | |||
189 | bsize = GNUNET_GNSRECORD_block_get_size (block); | ||
190 | env = GNUNET_MQ_msg_extra (r, | ||
191 | bsize, | ||
192 | GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE); | ||
193 | r->gns_header.r_id = htonl (lnc->request_id); | ||
194 | GNUNET_memcpy (&r[1], | ||
195 | block, | ||
196 | bsize); | ||
197 | GNUNET_STATISTICS_update (statistics, | ||
198 | "blocks found in cache", | ||
199 | 1, | ||
200 | GNUNET_NO); | ||
201 | r->expire = GNUNET_TIME_absolute_hton ( | ||
202 | GNUNET_GNSRECORD_block_get_expiration (block)); | ||
203 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
204 | "Sending NAMECACHE_LOOKUP_BLOCK_RESPONSE message\n"); | ||
205 | GNUNET_MQ_send (lnc->nc->mq, | ||
206 | env); | ||
207 | } | ||
208 | |||
209 | |||
210 | /** | ||
211 | * Handles a #GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK message | ||
212 | * | ||
213 | * @param cls a `struct NamecacheClient *` | ||
214 | * @param the inbound message | ||
215 | */ | ||
216 | static void | ||
217 | handle_lookup_block (void *cls, | ||
218 | const struct LookupBlockMessage *ln_msg) | ||
219 | { | ||
220 | struct NamecacheClient *nc = cls; | ||
221 | struct GNUNET_MQ_Envelope *env; | ||
222 | struct LookupBlockContext lnc; | ||
223 | struct LookupBlockResponseMessage *zir_end; | ||
224 | int ret; | ||
225 | |||
226 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
227 | "Received NAMECACHE_LOOKUP_BLOCK message\n"); | ||
228 | GNUNET_STATISTICS_update (statistics, | ||
229 | "blocks looked up", | ||
230 | 1, | ||
231 | GNUNET_NO); | ||
232 | lnc.request_id = ntohl (ln_msg->gns_header.r_id); | ||
233 | lnc.nc = nc; | ||
234 | lnc.status = GNUNET_OK; | ||
235 | if (GNUNET_SYSERR == | ||
236 | (ret = GSN_database->lookup_block (GSN_database->cls, | ||
237 | &ln_msg->query, | ||
238 | &handle_lookup_block_it, | ||
239 | &lnc))) | ||
240 | { | ||
241 | /* internal error (in database plugin); might be best to just hang up on | ||
242 | plugin rather than to signal that there are 'no' results, which | ||
243 | might also be false... */ | ||
244 | GNUNET_break (0); | ||
245 | GNUNET_SERVICE_client_drop (nc->client); | ||
246 | return; | ||
247 | } | ||
248 | if ((0 == ret) || (GNUNET_SYSERR == lnc.status)) | ||
249 | { | ||
250 | /* no records match at all, generate empty response */ | ||
251 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
252 | "Sending empty NAMECACHE_LOOKUP_BLOCK_RESPONSE message\n"); | ||
253 | env = GNUNET_MQ_msg (zir_end, | ||
254 | GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE); | ||
255 | zir_end->gns_header.r_id = ln_msg->gns_header.r_id; | ||
256 | GNUNET_MQ_send (nc->mq, | ||
257 | env); | ||
258 | } | ||
259 | GNUNET_SERVICE_client_continue (nc->client); | ||
260 | } | ||
261 | |||
262 | |||
263 | /** | ||
264 | * Check a #GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE message | ||
265 | * | ||
266 | * @param cls our `struct NamecacheClient` | ||
267 | * @param rp_msg message to process | ||
268 | * @return #GNUNET_OK (always fine) | ||
269 | */ | ||
270 | static int | ||
271 | check_block_cache (void *cls, | ||
272 | const struct BlockCacheMessage *rp_msg) | ||
273 | { | ||
274 | return GNUNET_OK; | ||
275 | } | ||
276 | |||
277 | |||
278 | /** | ||
279 | * Handles a #GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE message | ||
280 | * | ||
281 | * @param cls our `struct NamecacheClient` | ||
282 | * @param rp_msg message to process | ||
283 | */ | ||
284 | static void | ||
285 | handle_block_cache (void *cls, | ||
286 | const struct BlockCacheMessage *rp_msg) | ||
287 | { | ||
288 | struct NamecacheClient *nc = cls; | ||
289 | struct GNUNET_MQ_Envelope *env; | ||
290 | struct BlockCacheResponseMessage *rpr_msg; | ||
291 | struct GNUNET_GNSRECORD_Block *block; | ||
292 | size_t esize; | ||
293 | int res; | ||
294 | |||
295 | GNUNET_STATISTICS_update (statistics, | ||
296 | "blocks cached", | ||
297 | 1, | ||
298 | GNUNET_NO); | ||
299 | esize = ntohs (rp_msg->gns_header.header.size) - sizeof(struct | ||
300 | BlockCacheMessage); | ||
301 | block = GNUNET_malloc (esize); | ||
302 | memcpy (block, &rp_msg[1], esize); | ||
303 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
304 | "Received NAMECACHE_BLOCK_CACHE message with type %u\n", | ||
305 | htonl (block->type)); | ||
306 | res = GSN_database->cache_block (GSN_database->cls, | ||
307 | block); | ||
308 | GNUNET_free (block); | ||
309 | env = GNUNET_MQ_msg (rpr_msg, | ||
310 | GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE_RESPONSE); | ||
311 | rpr_msg->gns_header.r_id = rp_msg->gns_header.r_id; | ||
312 | rpr_msg->op_result = htonl (res); | ||
313 | GNUNET_MQ_send (nc->mq, | ||
314 | env); | ||
315 | GNUNET_SERVICE_client_continue (nc->client); | ||
316 | } | ||
317 | |||
318 | |||
319 | /** | ||
320 | * Process namecache requests. | ||
321 | * | ||
322 | * @param cls closure | ||
323 | * @param cfg configuration to use | ||
324 | * @param service the initialized service | ||
325 | */ | ||
326 | static void | ||
327 | run (void *cls, | ||
328 | const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
329 | struct GNUNET_SERVICE_Handle *service) | ||
330 | { | ||
331 | char *database; | ||
332 | |||
333 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
334 | "Starting namecache service\n"); | ||
335 | GSN_cfg = cfg; | ||
336 | |||
337 | /* Loading database plugin */ | ||
338 | if (GNUNET_OK != | ||
339 | GNUNET_CONFIGURATION_get_value_string (cfg, | ||
340 | "namecache", | ||
341 | "database", | ||
342 | &database)) | ||
343 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
344 | "No database backend configured\n"); | ||
345 | |||
346 | GNUNET_asprintf (&db_lib_name, | ||
347 | "libgnunet_plugin_namecache_%s", | ||
348 | database); | ||
349 | GSN_database = GNUNET_PLUGIN_load (db_lib_name, | ||
350 | (void *) GSN_cfg); | ||
351 | GNUNET_free (database); | ||
352 | if (NULL == GSN_database) | ||
353 | { | ||
354 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
355 | "Could not load database backend `%s'\n", | ||
356 | db_lib_name); | ||
357 | GNUNET_SCHEDULER_add_now (&cleanup_task, | ||
358 | NULL); | ||
359 | return; | ||
360 | } | ||
361 | statistics = GNUNET_STATISTICS_create ("namecache", | ||
362 | cfg); | ||
363 | |||
364 | /* Configuring server handles */ | ||
365 | GNUNET_SCHEDULER_add_shutdown (&cleanup_task, | ||
366 | NULL); | ||
367 | } | ||
368 | |||
369 | |||
370 | /** | ||
371 | * Define "main" method using service macro. | ||
372 | */ | ||
373 | GNUNET_SERVICE_MAIN | ||
374 | ("namecache", | ||
375 | GNUNET_SERVICE_OPTION_NONE, | ||
376 | &run, | ||
377 | &client_connect_cb, | ||
378 | &client_disconnect_cb, | ||
379 | NULL, | ||
380 | GNUNET_MQ_hd_fixed_size (lookup_block, | ||
381 | GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK, | ||
382 | struct LookupBlockMessage, | ||
383 | NULL), | ||
384 | GNUNET_MQ_hd_var_size (block_cache, | ||
385 | GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE, | ||
386 | struct BlockCacheMessage, | ||
387 | NULL), | ||
388 | GNUNET_MQ_handler_end ()); | ||
389 | |||
390 | |||
391 | /* end of gnunet-service-namecache.c */ | ||