summaryrefslogtreecommitdiff
path: root/src/namecache/gnunet-service-namecache.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/namecache/gnunet-service-namecache.c')
-rw-r--r--src/namecache/gnunet-service-namecache.c319
1 files changed, 164 insertions, 155 deletions
diff --git a/src/namecache/gnunet-service-namecache.c b/src/namecache/gnunet-service-namecache.c
index e9f45a4fc..7cf216ce3 100644
--- a/src/namecache/gnunet-service-namecache.c
+++ b/src/namecache/gnunet-service-namecache.c
@@ -33,13 +33,17 @@
33#include "gnunet_signatures.h" 33#include "gnunet_signatures.h"
34#include "namecache.h" 34#include "namecache.h"
35 35
36#define LOG_STRERROR_FILE(kind, syscall, filename) GNUNET_log_from_strerror_file(kind, "util", syscall, filename) 36#define LOG_STRERROR_FILE(kind, syscall, \
37 filename) GNUNET_log_from_strerror_file (kind, "util", \
38 syscall, \
39 filename)
37 40
38 41
39/** 42/**
40 * A namecache client 43 * A namecache client
41 */ 44 */
42struct NamecacheClient { 45struct NamecacheClient
46{
43 /** 47 /**
44 * The client 48 * The client
45 */ 49 */
@@ -79,21 +83,21 @@ static char *db_lib_name;
79 * @param cls unused 83 * @param cls unused
80 */ 84 */
81static void 85static void
82cleanup_task(void *cls) 86cleanup_task (void *cls)
83{ 87{
84 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 88 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
85 "Stopping namecache service\n"); 89 "Stopping namecache service\n");
86 GNUNET_break(NULL == 90 GNUNET_break (NULL ==
87 GNUNET_PLUGIN_unload(db_lib_name, 91 GNUNET_PLUGIN_unload (db_lib_name,
88 GSN_database)); 92 GSN_database));
89 GNUNET_free(db_lib_name); 93 GNUNET_free (db_lib_name);
90 db_lib_name = NULL; 94 db_lib_name = NULL;
91 if (NULL != statistics) 95 if (NULL != statistics)
92 { 96 {
93 GNUNET_STATISTICS_destroy(statistics, 97 GNUNET_STATISTICS_destroy (statistics,
94 GNUNET_NO); 98 GNUNET_NO);
95 statistics = NULL; 99 statistics = NULL;
96 } 100 }
97} 101}
98 102
99 103
@@ -106,16 +110,16 @@ cleanup_task(void *cls)
106 * @param app_ctx the `struct NamecacheClient` for this @a client 110 * @param app_ctx the `struct NamecacheClient` for this @a client
107 */ 111 */
108static void 112static void
109client_disconnect_cb(void *cls, 113client_disconnect_cb (void *cls,
110 struct GNUNET_SERVICE_Client *client, 114 struct GNUNET_SERVICE_Client *client,
111 void *app_ctx) 115 void *app_ctx)
112{ 116{
113 struct NamecacheClient *nc = app_ctx; 117 struct NamecacheClient *nc = app_ctx;
114 118
115 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
116 "Client %p disconnected\n", 120 "Client %p disconnected\n",
117 client); 121 client);
118 GNUNET_free(nc); 122 GNUNET_free (nc);
119} 123}
120 124
121 125
@@ -128,16 +132,16 @@ client_disconnect_cb(void *cls,
128 * @return internal namecache client structure for this client 132 * @return internal namecache client structure for this client
129 */ 133 */
130static void * 134static void *
131client_connect_cb(void *cls, 135client_connect_cb (void *cls,
132 struct GNUNET_SERVICE_Client *client, 136 struct GNUNET_SERVICE_Client *client,
133 struct GNUNET_MQ_Handle *mq) 137 struct GNUNET_MQ_Handle *mq)
134{ 138{
135 struct NamecacheClient *nc; 139 struct NamecacheClient *nc;
136 140
137 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
138 "Client %p connected\n", 142 "Client %p connected\n",
139 client); 143 client);
140 nc = GNUNET_new(struct NamecacheClient); 144 nc = GNUNET_new (struct NamecacheClient);
141 nc->client = client; 145 nc->client = client;
142 nc->mq = mq; 146 nc->mq = mq;
143 return nc; 147 return nc;
@@ -148,7 +152,8 @@ client_connect_cb(void *cls,
148 * Context for name lookups passed from #handle_lookup_block to 152 * Context for name lookups passed from #handle_lookup_block to
149 * #handle_lookup_block_it as closure 153 * #handle_lookup_block_it as closure
150 */ 154 */
151struct LookupBlockContext { 155struct LookupBlockContext
156{
152 /** 157 /**
153 * The client to send the response to 158 * The client to send the response to
154 */ 159 */
@@ -173,8 +178,8 @@ struct LookupBlockContext {
173 * @param block the block 178 * @param block the block
174 */ 179 */
175static void 180static void
176handle_lookup_block_it(void *cls, 181handle_lookup_block_it (void *cls,
177 const struct GNUNET_GNSRECORD_Block *block) 182 const struct GNUNET_GNSRECORD_Block *block)
178{ 183{
179 struct LookupBlockContext *lnc = cls; 184 struct LookupBlockContext *lnc = cls;
180 struct GNUNET_MQ_Envelope *env; 185 struct GNUNET_MQ_Envelope *env;
@@ -182,37 +187,39 @@ handle_lookup_block_it(void *cls,
182 size_t esize; 187 size_t esize;
183 size_t bsize; 188 size_t bsize;
184 189
185 bsize = ntohl(block->purpose.size); 190 bsize = ntohl (block->purpose.size);
186 if (bsize < 191 if (bsize <
187 (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose) + sizeof(struct GNUNET_TIME_AbsoluteNBO))) 192 (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose) + sizeof(struct
188 { 193 GNUNET_TIME_AbsoluteNBO)))
189 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 194 {
190 "Malformed block."); 195 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
191 lnc->status = GNUNET_SYSERR; 196 "Malformed block.");
192 return; 197 lnc->status = GNUNET_SYSERR;
193 } 198 return;
194 esize = ntohl(block->purpose.size) 199 }
200 esize = ntohl (block->purpose.size)
195 - sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose) 201 - sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)
196 - sizeof(struct GNUNET_TIME_AbsoluteNBO); 202 - sizeof(struct GNUNET_TIME_AbsoluteNBO);
197 env = GNUNET_MQ_msg_extra(r, 203 env = GNUNET_MQ_msg_extra (r,
198 esize, 204 esize,
199 GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE); 205 GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE);
200 r->gns_header.r_id = htonl(lnc->request_id); 206 r->gns_header.r_id = htonl (lnc->request_id);
201 r->expire = block->expiration_time; 207 r->expire = block->expiration_time;
202 r->signature = block->signature; 208 r->signature = block->signature;
203 r->derived_key = block->derived_key; 209 r->derived_key = block->derived_key;
204 GNUNET_memcpy(&r[1], 210 GNUNET_memcpy (&r[1],
205 &block[1], 211 &block[1],
206 esize); 212 esize);
207 GNUNET_STATISTICS_update(statistics, 213 GNUNET_STATISTICS_update (statistics,
208 "blocks found in cache", 214 "blocks found in cache",
209 1, 215 1,
210 GNUNET_NO); 216 GNUNET_NO);
211 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
212 "Sending NAMECACHE_LOOKUP_BLOCK_RESPONSE message with expiration time %s\n", 218 "Sending NAMECACHE_LOOKUP_BLOCK_RESPONSE message with expiration time %s\n",
213 GNUNET_STRINGS_absolute_time_to_string(GNUNET_TIME_absolute_ntoh(r->expire))); 219 GNUNET_STRINGS_absolute_time_to_string (
214 GNUNET_MQ_send(lnc->nc->mq, 220 GNUNET_TIME_absolute_ntoh (r->expire)));
215 env); 221 GNUNET_MQ_send (lnc->nc->mq,
222 env);
216} 223}
217 224
218 225
@@ -223,8 +230,8 @@ handle_lookup_block_it(void *cls,
223 * @param the inbound message 230 * @param the inbound message
224 */ 231 */
225static void 232static void
226handle_lookup_block(void *cls, 233handle_lookup_block (void *cls,
227 const struct LookupBlockMessage *ln_msg) 234 const struct LookupBlockMessage *ln_msg)
228{ 235{
229 struct NamecacheClient *nc = cls; 236 struct NamecacheClient *nc = cls;
230 struct GNUNET_MQ_Envelope *env; 237 struct GNUNET_MQ_Envelope *env;
@@ -232,40 +239,40 @@ handle_lookup_block(void *cls,
232 struct LookupBlockResponseMessage *zir_end; 239 struct LookupBlockResponseMessage *zir_end;
233 int ret; 240 int ret;
234 241
235 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
236 "Received NAMECACHE_LOOKUP_BLOCK message\n"); 243 "Received NAMECACHE_LOOKUP_BLOCK message\n");
237 GNUNET_STATISTICS_update(statistics, 244 GNUNET_STATISTICS_update (statistics,
238 "blocks looked up", 245 "blocks looked up",
239 1, 246 1,
240 GNUNET_NO); 247 GNUNET_NO);
241 lnc.request_id = ntohl(ln_msg->gns_header.r_id); 248 lnc.request_id = ntohl (ln_msg->gns_header.r_id);
242 lnc.nc = nc; 249 lnc.nc = nc;
243 lnc.status = GNUNET_OK; 250 lnc.status = GNUNET_OK;
244 if (GNUNET_SYSERR == 251 if (GNUNET_SYSERR ==
245 (ret = GSN_database->lookup_block(GSN_database->cls, 252 (ret = GSN_database->lookup_block (GSN_database->cls,
246 &ln_msg->query, 253 &ln_msg->query,
247 &handle_lookup_block_it, 254 &handle_lookup_block_it,
248 &lnc))) 255 &lnc)))
249 { 256 {
250 /* internal error (in database plugin); might be best to just hang up on 257 /* internal error (in database plugin); might be best to just hang up on
251 plugin rather than to signal that there are 'no' results, which 258 plugin rather than to signal that there are 'no' results, which
252 might also be false... */ 259 might also be false... */
253 GNUNET_break(0); 260 GNUNET_break (0);
254 GNUNET_SERVICE_client_drop(nc->client); 261 GNUNET_SERVICE_client_drop (nc->client);
255 return; 262 return;
256 } 263 }
257 if ((0 == ret) || (GNUNET_SYSERR == lnc.status)) 264 if ((0 == ret) || (GNUNET_SYSERR == lnc.status))
258 { 265 {
259 /* no records match at all, generate empty response */ 266 /* no records match at all, generate empty response */
260 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
261 "Sending empty NAMECACHE_LOOKUP_BLOCK_RESPONSE message\n"); 268 "Sending empty NAMECACHE_LOOKUP_BLOCK_RESPONSE message\n");
262 env = GNUNET_MQ_msg(zir_end, 269 env = GNUNET_MQ_msg (zir_end,
263 GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE); 270 GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE);
264 zir_end->gns_header.r_id = ln_msg->gns_header.r_id; 271 zir_end->gns_header.r_id = ln_msg->gns_header.r_id;
265 GNUNET_MQ_send(nc->mq, 272 GNUNET_MQ_send (nc->mq,
266 env); 273 env);
267 } 274 }
268 GNUNET_SERVICE_client_continue(nc->client); 275 GNUNET_SERVICE_client_continue (nc->client);
269} 276}
270 277
271 278
@@ -277,8 +284,8 @@ handle_lookup_block(void *cls,
277 * @return #GNUNET_OK (always fine) 284 * @return #GNUNET_OK (always fine)
278 */ 285 */
279static int 286static int
280check_block_cache(void *cls, 287check_block_cache (void *cls,
281 const struct BlockCacheMessage *rp_msg) 288 const struct BlockCacheMessage *rp_msg)
282{ 289{
283 return GNUNET_OK; 290 return GNUNET_OK;
284} 291}
@@ -291,8 +298,8 @@ check_block_cache(void *cls,
291 * @param rp_msg message to process 298 * @param rp_msg message to process
292 */ 299 */
293static void 300static void
294handle_block_cache(void *cls, 301handle_block_cache (void *cls,
295 const struct BlockCacheMessage *rp_msg) 302 const struct BlockCacheMessage *rp_msg)
296{ 303{
297 struct NamecacheClient *nc = cls; 304 struct NamecacheClient *nc = cls;
298 struct GNUNET_MQ_Envelope *env; 305 struct GNUNET_MQ_Envelope *env;
@@ -301,34 +308,36 @@ handle_block_cache(void *cls,
301 size_t esize; 308 size_t esize;
302 int res; 309 int res;
303 310
304 GNUNET_STATISTICS_update(statistics, 311 GNUNET_STATISTICS_update (statistics,
305 "blocks cached", 312 "blocks cached",
306 1, 313 1,
307 GNUNET_NO); 314 GNUNET_NO);
308 esize = ntohs(rp_msg->gns_header.header.size) - sizeof(struct BlockCacheMessage); 315 esize = ntohs (rp_msg->gns_header.header.size) - sizeof(struct
309 block = GNUNET_malloc(sizeof(struct GNUNET_GNSRECORD_Block) + esize); 316 BlockCacheMessage);
317 block = GNUNET_malloc (sizeof(struct GNUNET_GNSRECORD_Block) + esize);
310 block->signature = rp_msg->signature; 318 block->signature = rp_msg->signature;
311 block->derived_key = rp_msg->derived_key; 319 block->derived_key = rp_msg->derived_key;
312 block->purpose.size = htonl(sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose) + 320 block->purpose.size = htonl (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)
313 sizeof(struct GNUNET_TIME_AbsoluteNBO) + 321 + sizeof(struct GNUNET_TIME_AbsoluteNBO)
314 esize); 322 + esize);
315 block->expiration_time = rp_msg->expire; 323 block->expiration_time = rp_msg->expire;
316 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
317 "Received NAMECACHE_BLOCK_CACHE message with expiration time %s\n", 325 "Received NAMECACHE_BLOCK_CACHE message with expiration time %s\n",
318 GNUNET_STRINGS_absolute_time_to_string(GNUNET_TIME_absolute_ntoh(block->expiration_time))); 326 GNUNET_STRINGS_absolute_time_to_string (
319 GNUNET_memcpy(&block[1], 327 GNUNET_TIME_absolute_ntoh (block->expiration_time)));
320 &rp_msg[1], 328 GNUNET_memcpy (&block[1],
321 esize); 329 &rp_msg[1],
322 res = GSN_database->cache_block(GSN_database->cls, 330 esize);
323 block); 331 res = GSN_database->cache_block (GSN_database->cls,
324 GNUNET_free(block); 332 block);
325 env = GNUNET_MQ_msg(rpr_msg, 333 GNUNET_free (block);
326 GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE_RESPONSE); 334 env = GNUNET_MQ_msg (rpr_msg,
335 GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE_RESPONSE);
327 rpr_msg->gns_header.r_id = rp_msg->gns_header.r_id; 336 rpr_msg->gns_header.r_id = rp_msg->gns_header.r_id;
328 rpr_msg->op_result = htonl(res); 337 rpr_msg->op_result = htonl (res);
329 GNUNET_MQ_send(nc->mq, 338 GNUNET_MQ_send (nc->mq,
330 env); 339 env);
331 GNUNET_SERVICE_client_continue(nc->client); 340 GNUNET_SERVICE_client_continue (nc->client);
332} 341}
333 342
334 343
@@ -340,46 +349,46 @@ handle_block_cache(void *cls,
340 * @param service the initialized service 349 * @param service the initialized service
341 */ 350 */
342static void 351static void
343run(void *cls, 352run (void *cls,
344 const struct GNUNET_CONFIGURATION_Handle *cfg, 353 const struct GNUNET_CONFIGURATION_Handle *cfg,
345 struct GNUNET_SERVICE_Handle *service) 354 struct GNUNET_SERVICE_Handle *service)
346{ 355{
347 char *database; 356 char *database;
348 357
349 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
350 "Starting namecache service\n"); 359 "Starting namecache service\n");
351 GSN_cfg = cfg; 360 GSN_cfg = cfg;
352 361
353 /* Loading database plugin */ 362 /* Loading database plugin */
354 if (GNUNET_OK != 363 if (GNUNET_OK !=
355 GNUNET_CONFIGURATION_get_value_string(cfg, 364 GNUNET_CONFIGURATION_get_value_string (cfg,
356 "namecache", 365 "namecache",
357 "database", 366 "database",
358 &database)) 367 &database))
359 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 368 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
360 "No database backend configured\n"); 369 "No database backend configured\n");
361 370
362 GNUNET_asprintf(&db_lib_name, 371 GNUNET_asprintf (&db_lib_name,
363 "libgnunet_plugin_namecache_%s", 372 "libgnunet_plugin_namecache_%s",
364 database); 373 database);
365 GSN_database = GNUNET_PLUGIN_load(db_lib_name, 374 GSN_database = GNUNET_PLUGIN_load (db_lib_name,
366 (void *)GSN_cfg); 375 (void *) GSN_cfg);
367 GNUNET_free(database); 376 GNUNET_free (database);
368 if (NULL == GSN_database) 377 if (NULL == GSN_database)
369 { 378 {
370 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 379 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
371 "Could not load database backend `%s'\n", 380 "Could not load database backend `%s'\n",
372 db_lib_name); 381 db_lib_name);
373 GNUNET_SCHEDULER_add_now(&cleanup_task, 382 GNUNET_SCHEDULER_add_now (&cleanup_task,
374 NULL); 383 NULL);
375 return; 384 return;
376 } 385 }
377 statistics = GNUNET_STATISTICS_create("namecache", 386 statistics = GNUNET_STATISTICS_create ("namecache",
378 cfg); 387 cfg);
379 388
380 /* Configuring server handles */ 389 /* Configuring server handles */
381 GNUNET_SCHEDULER_add_shutdown(&cleanup_task, 390 GNUNET_SCHEDULER_add_shutdown (&cleanup_task,
382 NULL); 391 NULL);
383} 392}
384 393
385 394
@@ -393,15 +402,15 @@ GNUNET_SERVICE_MAIN
393 &client_connect_cb, 402 &client_connect_cb,
394 &client_disconnect_cb, 403 &client_disconnect_cb,
395 NULL, 404 NULL,
396 GNUNET_MQ_hd_fixed_size(lookup_block, 405 GNUNET_MQ_hd_fixed_size (lookup_block,
397 GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK, 406 GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK,
398 struct LookupBlockMessage, 407 struct LookupBlockMessage,
399 NULL), 408 NULL),
400 GNUNET_MQ_hd_var_size(block_cache, 409 GNUNET_MQ_hd_var_size (block_cache,
401 GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE, 410 GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE,
402 struct BlockCacheMessage, 411 struct BlockCacheMessage,
403 NULL), 412 NULL),
404 GNUNET_MQ_handler_end()); 413 GNUNET_MQ_handler_end ());
405 414
406 415
407/* end of gnunet-service-namecache.c */ 416/* end of gnunet-service-namecache.c */