aboutsummaryrefslogtreecommitdiff
path: root/src/namecache
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2016-10-21 21:48:19 +0000
committerChristian Grothoff <christian@grothoff.org>2016-10-21 21:48:19 +0000
commit51baa79fb2c3f05b79d012db22b8d47cdcacf976 (patch)
tree19e1b4dd80528d6371dc81489edb756b8ac37c72 /src/namecache
parentf4ab28b29fc1b4ea87f35c43611f84add4af32d7 (diff)
downloadgnunet-51baa79fb2c3f05b79d012db22b8d47cdcacf976.tar.gz
gnunet-51baa79fb2c3f05b79d012db22b8d47cdcacf976.zip
convert namecache to new service ApI
Diffstat (limited to 'src/namecache')
-rw-r--r--src/namecache/gnunet-service-namecache.c284
1 files changed, 117 insertions, 167 deletions
diff --git a/src/namecache/gnunet-service-namecache.c b/src/namecache/gnunet-service-namecache.c
index 2cd0c161c..f20d664a2 100644
--- a/src/namecache/gnunet-service-namecache.c
+++ b/src/namecache/gnunet-service-namecache.c
@@ -40,21 +40,17 @@
40 */ 40 */
41struct NamecacheClient 41struct NamecacheClient
42{ 42{
43 /**
44 * Next element in the DLL
45 */
46 struct NamecacheClient *next;
47 43
48 /** 44 /**
49 * Previous element in the DLL 45 * The client
50 */ 46 */
51 struct NamecacheClient *prev; 47 struct GNUNET_SERVICE_Client *client;
52 48
53 /** 49 /**
54 * The client 50 * The message queue to talk to @e client.
55 */ 51 */
56 struct GNUNET_SERVER_Client *client; 52 struct GNUNET_MQ_Handle *mq;
57 53
58}; 54};
59 55
60 56
@@ -73,27 +69,6 @@ static struct GNUNET_NAMECACHE_PluginFunctions *GSN_database;
73 */ 69 */
74static char *db_lib_name; 70static char *db_lib_name;
75 71
76/**
77 * Our notification context.
78 */
79static struct GNUNET_SERVER_NotificationContext *snc;
80
81/**
82 * Head of the Client DLL
83 */
84static struct NamecacheClient *client_head;
85
86/**
87 * Tail of the Client DLL
88 */
89static struct NamecacheClient *client_tail;
90
91/**
92 * Notification context shared by all monitors.
93 */
94static struct GNUNET_SERVER_NotificationContext *monitor_nc;
95
96
97 72
98/** 73/**
99 * Task run during shutdown. 74 * Task run during shutdown.
@@ -103,29 +78,13 @@ static struct GNUNET_SERVER_NotificationContext *monitor_nc;
103static void 78static void
104cleanup_task (void *cls) 79cleanup_task (void *cls)
105{ 80{
106 struct NamecacheClient *nc;
107
108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 81 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
109 "Stopping namecache service\n"); 82 "Stopping namecache service\n");
110 if (NULL != snc) 83 GNUNET_break (NULL ==
111 { 84 GNUNET_PLUGIN_unload (db_lib_name,
112 GNUNET_SERVER_notification_context_destroy (snc); 85 GSN_database));
113 snc = NULL;
114 }
115 while (NULL != (nc = client_head))
116 {
117 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
118 GNUNET_SERVER_client_set_user_context (nc->client, NULL);
119 GNUNET_free (nc);
120 }
121 GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, GSN_database));
122 GNUNET_free (db_lib_name); 86 GNUNET_free (db_lib_name);
123 db_lib_name = NULL; 87 db_lib_name = NULL;
124 if (NULL != monitor_nc)
125 {
126 GNUNET_SERVER_notification_context_destroy (monitor_nc);
127 monitor_nc = NULL;
128 }
129} 88}
130 89
131 90
@@ -135,48 +94,43 @@ cleanup_task (void *cls)
135 * 94 *
136 * @param cls closure 95 * @param cls closure
137 * @param client identification of the client 96 * @param client identification of the client
97 * @param app_ctx the `struct NamecacheClient` for this @a client
138 */ 98 */
139static void 99static void
140client_disconnect_notification (void *cls, 100client_disconnect_cb (void *cls,
141 struct GNUNET_SERVER_Client *client) 101 struct GNUNET_SERVICE_Client *client,
102 void *app_ctx)
142{ 103{
143 struct NamecacheClient *nc; 104 struct NamecacheClient *nc = app_ctx;
144 105
145 if (NULL == client)
146 return;
147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
148 "Client %p disconnected\n", 107 "Client %p disconnected\n",
149 client); 108 client);
150 if (NULL == (nc = GNUNET_SERVER_client_get_user_context (client, struct NamecacheClient)))
151 return;
152 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
153 GNUNET_free (nc); 109 GNUNET_free (nc);
154} 110}
155 111
156 112
157/** 113/**
158 * Add a client to our list of active clients, if it is not yet 114 * Add a client to our list of active clients.
159 * in there.
160 * 115 *
116 * @param cls NULL
161 * @param client client to add 117 * @param client client to add
118 * @param mq queue to talk to @a client
162 * @return internal namecache client structure for this client 119 * @return internal namecache client structure for this client
163 */ 120 */
164static struct NamecacheClient * 121static void *
165client_lookup (struct GNUNET_SERVER_Client *client) 122client_connect_cb (void *cls,
123 struct GNUNET_SERVICE_Client *client,
124 struct GNUNET_MQ_Handle *mq)
166{ 125{
167 struct NamecacheClient *nc; 126 struct NamecacheClient *nc;
168 127
169 nc = GNUNET_SERVER_client_get_user_context (client, struct NamecacheClient);
170 if (NULL != nc)
171 return nc;
172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
173 "Client %p connected\n", 129 "Client %p connected\n",
174 client); 130 client);
175 nc = GNUNET_new (struct NamecacheClient); 131 nc = GNUNET_new (struct NamecacheClient);
176 nc->client = client; 132 nc->client = client;
177 GNUNET_SERVER_notification_context_add (snc, client); 133 nc->mq = mq;
178 GNUNET_CONTAINER_DLL_insert (client_head, client_tail, nc);
179 GNUNET_SERVER_client_set_user_context (client, nc);
180 return nc; 134 return nc;
181} 135}
182 136
@@ -211,116 +165,112 @@ handle_lookup_block_it (void *cls,
211 const struct GNUNET_GNSRECORD_Block *block) 165 const struct GNUNET_GNSRECORD_Block *block)
212{ 166{
213 struct LookupBlockContext *lnc = cls; 167 struct LookupBlockContext *lnc = cls;
168 struct GNUNET_MQ_Envelope *env;
214 struct LookupBlockResponseMessage *r; 169 struct LookupBlockResponseMessage *r;
215 size_t esize; 170 size_t esize;
216 171
217 esize = ntohl (block->purpose.size) 172 esize = ntohl (block->purpose.size)
218 - sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) 173 - sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose)
219 - sizeof (struct GNUNET_TIME_AbsoluteNBO); 174 - sizeof (struct GNUNET_TIME_AbsoluteNBO);
220 r = GNUNET_malloc (sizeof (struct LookupBlockResponseMessage) + esize); 175 env = GNUNET_MQ_msg_extra (r,
221 r->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE); 176 esize,
222 r->gns_header.header.size = htons (sizeof (struct LookupBlockResponseMessage) + esize); 177 GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE);
223 r->gns_header.r_id = htonl (lnc->request_id); 178 r->gns_header.r_id = htonl (lnc->request_id);
224 r->expire = block->expiration_time; 179 r->expire = block->expiration_time;
225 r->signature = block->signature; 180 r->signature = block->signature;
226 r->derived_key = block->derived_key; 181 r->derived_key = block->derived_key;
227 GNUNET_memcpy (&r[1], &block[1], esize); 182 GNUNET_memcpy (&r[1],
183 &block[1],
184 esize);
228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
229 "Sending `%s' message with expiration time %s\n", 186 "Sending NAMECACHE_LOOKUP_BLOCK_RESPONSE message with expiration time %s\n",
230 "NAMECACHE_LOOKUP_BLOCK_RESPONSE",
231 GNUNET_STRINGS_absolute_time_to_string (GNUNET_TIME_absolute_ntoh (r->expire))); 187 GNUNET_STRINGS_absolute_time_to_string (GNUNET_TIME_absolute_ntoh (r->expire)));
232 GNUNET_SERVER_notification_context_unicast (snc, 188 GNUNET_MQ_send (lnc->nc->mq,
233 lnc->nc->client, 189 env);
234 &r->gns_header.header,
235 GNUNET_NO);
236 GNUNET_free (r);
237} 190}
238 191
239 192
240/** 193/**
241 * Handles a #GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK message 194 * Handles a #GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK message
242 * 195 *
243 * @param cls unused 196 * @param cls a `struct NamecacheClient *`
244 * @param client client sending the message 197 * @param the inbound message
245 * @param message message of type 'struct LookupNameMessage'
246 */ 198 */
247static void 199static void
248handle_lookup_block (void *cls, 200handle_lookup_block (void *cls,
249 struct GNUNET_SERVER_Client *client, 201 const struct LookupBlockMessage *ln_msg)
250 const struct GNUNET_MessageHeader *message)
251{ 202{
252 const struct LookupBlockMessage *ln_msg; 203 struct NamecacheClient *nc = cls;
204 struct GNUNET_MQ_Envelope *env;
253 struct LookupBlockContext lnc; 205 struct LookupBlockContext lnc;
254 struct NamecacheClient *nc; 206 struct LookupBlockResponseMessage *zir_end;
255 struct LookupBlockResponseMessage zir_end;
256 int ret; 207 int ret;
257 208
258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
259 "Received `%s' message\n", 210 "Received NAMECACHE_LOOKUP_BLOCK message\n");
260 "NAMECACHE_LOOKUP_BLOCK"); 211
261 nc = client_lookup(client);
262 ln_msg = (const struct LookupBlockMessage *) message;
263 lnc.request_id = ntohl (ln_msg->gns_header.r_id); 212 lnc.request_id = ntohl (ln_msg->gns_header.r_id);
264 lnc.nc = nc; 213 lnc.nc = nc;
265 if (GNUNET_SYSERR == 214 if (GNUNET_SYSERR ==
266 (ret = GSN_database->lookup_block (GSN_database->cls, 215 (ret = GSN_database->lookup_block (GSN_database->cls,
267 &ln_msg->query, 216 &ln_msg->query,
268 &handle_lookup_block_it, &lnc))) 217 &handle_lookup_block_it,
218 &lnc)))
269 { 219 {
270 /* internal error (in database plugin); might be best to just hang up on 220 /* internal error (in database plugin); might be best to just hang up on
271 plugin rather than to signal that there are 'no' results, which 221 plugin rather than to signal that there are 'no' results, which
272 might also be false... */ 222 might also be false... */
273 GNUNET_break (0); 223 GNUNET_break (0);
274 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 224 GNUNET_SERVICE_client_drop (nc->client);
275 return; 225 return;
276 } 226 }
277 if (0 == ret) 227 if (0 == ret)
278 { 228 {
279 /* no records match at all, generate empty response */ 229 /* no records match at all, generate empty response */
280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
281 "Sending empty `%s' message\n", 231 "Sending empty NAMECACHE_LOOKUP_BLOCK_RESPONSE message\n");
282 "NAMECACHE_LOOKUP_BLOCK_RESPONSE"); 232 env = GNUNET_MQ_msg (zir_end,
283 memset (&zir_end, 0, sizeof (zir_end)); 233 GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE);
284 zir_end.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE); 234 zir_end->gns_header.r_id = ln_msg->gns_header.r_id;
285 zir_end.gns_header.header.size = htons (sizeof (struct LookupBlockResponseMessage)); 235 GNUNET_MQ_send (nc->mq,
286 zir_end.gns_header.r_id = ln_msg->gns_header.r_id; 236 env);
287 GNUNET_SERVER_notification_context_unicast (snc,
288 client,
289 &zir_end.gns_header.header,
290 GNUNET_NO);
291
292 } 237 }
293 GNUNET_SERVER_receive_done (client, GNUNET_OK); 238 GNUNET_SERVICE_client_continue (nc->client);
239}
240
241
242/**
243 * Check a #GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE message
244 *
245 * @param cls our `struct NamecacheClient`
246 * @param rp_msg message to process
247 * @return #GNUNET_OK (always fine)
248 */
249static int
250check_block_cache (void *cls,
251 const struct BlockCacheMessage *rp_msg)
252{
253 return GNUNET_OK;
294} 254}
295 255
296 256
297/** 257/**
298 * Handles a #GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE message 258 * Handles a #GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE message
299 * 259 *
300 * @param cls unused 260 * @param cls our `struct NamecacheClient`
301 * @param client client sending the message 261 * @param rp_msg message to process
302 * @param message message of type 'struct BlockCacheMessage'
303 */ 262 */
304static void 263static void
305handle_block_cache (void *cls, 264handle_block_cache (void *cls,
306 struct GNUNET_SERVER_Client *client, 265 const struct BlockCacheMessage *rp_msg)
307 const struct GNUNET_MessageHeader *message)
308{ 266{
309 struct NamecacheClient *nc; 267 struct NamecacheClient *nc = cls;
310 const struct BlockCacheMessage *rp_msg; 268 struct GNUNET_MQ_Envelope *env;
311 struct BlockCacheResponseMessage rpr_msg; 269 struct BlockCacheResponseMessage *rpr_msg;
312 struct GNUNET_GNSRECORD_Block *block; 270 struct GNUNET_GNSRECORD_Block *block;
313 size_t esize; 271 size_t esize;
314 int res; 272 int res;
315 273
316 nc = client_lookup (client);
317 if (ntohs (message->size) < sizeof (struct BlockCacheMessage))
318 {
319 GNUNET_break (0);
320 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
321 return;
322 }
323 rp_msg = (const struct BlockCacheMessage *) message;
324 esize = ntohs (rp_msg->gns_header.header.size) - sizeof (struct BlockCacheMessage); 274 esize = ntohs (rp_msg->gns_header.header.size) - sizeof (struct BlockCacheMessage);
325 block = GNUNET_malloc (sizeof (struct GNUNET_GNSRECORD_Block) + esize); 275 block = GNUNET_malloc (sizeof (struct GNUNET_GNSRECORD_Block) + esize);
326 block->signature = rp_msg->signature; 276 block->signature = rp_msg->signature;
@@ -330,23 +280,21 @@ handle_block_cache (void *cls,
330 esize); 280 esize);
331 block->expiration_time = rp_msg->expire; 281 block->expiration_time = rp_msg->expire;
332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
333 "Received `%s' message with expiration time %s\n", 283 "Received NAMECACHE_BLOCK_CACHE message with expiration time %s\n",
334 "NAMECACHE_BLOCK_CACHE",
335 GNUNET_STRINGS_absolute_time_to_string (GNUNET_TIME_absolute_ntoh (block->expiration_time))); 284 GNUNET_STRINGS_absolute_time_to_string (GNUNET_TIME_absolute_ntoh (block->expiration_time)));
336 GNUNET_memcpy (&block[1], &rp_msg[1], esize); 285 GNUNET_memcpy (&block[1],
286 &rp_msg[1],
287 esize);
337 res = GSN_database->cache_block (GSN_database->cls, 288 res = GSN_database->cache_block (GSN_database->cls,
338 block); 289 block);
339 GNUNET_free (block); 290 GNUNET_free (block);
340 291 env = GNUNET_MQ_msg (rpr_msg,
341 rpr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE_RESPONSE); 292 GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE_RESPONSE);
342 rpr_msg.gns_header.header.size = htons (sizeof (struct BlockCacheResponseMessage)); 293 rpr_msg->gns_header.r_id = rp_msg->gns_header.r_id;
343 rpr_msg.gns_header.r_id = rp_msg->gns_header.r_id; 294 rpr_msg->op_result = htonl (res);
344 rpr_msg.op_result = htonl (res); 295 GNUNET_MQ_send (nc->mq,
345 GNUNET_SERVER_notification_context_unicast (snc, 296 env);
346 nc->client, 297 GNUNET_SERVICE_client_continue (nc->client);
347 &rpr_msg.gns_header.header,
348 GNUNET_NO);
349 GNUNET_SERVER_receive_done (client, GNUNET_OK);
350} 298}
351 299
352 300
@@ -354,68 +302,70 @@ handle_block_cache (void *cls,
354 * Process namecache requests. 302 * Process namecache requests.
355 * 303 *
356 * @param cls closure 304 * @param cls closure
357 * @param server the initialized server
358 * @param cfg configuration to use 305 * @param cfg configuration to use
306 * @param service the initialized service
359 */ 307 */
360static void 308static void
361run (void *cls, struct GNUNET_SERVER_Handle *server, 309run (void *cls,
362 const struct GNUNET_CONFIGURATION_Handle *cfg) 310 const struct GNUNET_CONFIGURATION_Handle *cfg,
311 struct GNUNET_SERVICE_Handle *service)
363{ 312{
364 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
365 {&handle_lookup_block, NULL,
366 GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK, sizeof (struct LookupBlockMessage)},
367 {&handle_block_cache, NULL,
368 GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE, 0},
369 {NULL, NULL, 0, 0}
370 };
371 char *database; 313 char *database;
372 314
373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting namecache service\n"); 315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
316 "Starting namecache service\n");
374 GSN_cfg = cfg; 317 GSN_cfg = cfg;
375 monitor_nc = GNUNET_SERVER_notification_context_create (server, 1);
376 318
377 /* Loading database plugin */ 319 /* Loading database plugin */
378 if (GNUNET_OK != 320 if (GNUNET_OK !=
379 GNUNET_CONFIGURATION_get_value_string (cfg, "namecache", "database", 321 GNUNET_CONFIGURATION_get_value_string (cfg,
322 "namecache",
323 "database",
380 &database)) 324 &database))
381 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n"); 325 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
326 "No database backend configured\n");
382 327
383 GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_namecache_%s", database); 328 GNUNET_asprintf (&db_lib_name,
384 GSN_database = GNUNET_PLUGIN_load (db_lib_name, (void *) GSN_cfg); 329 "libgnunet_plugin_namecache_%s",
330 database);
331 GSN_database = GNUNET_PLUGIN_load (db_lib_name,
332 (void *) GSN_cfg);
385 GNUNET_free (database); 333 GNUNET_free (database);
386 if (NULL == GSN_database) 334 if (NULL == GSN_database)
387 { 335 {
388 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 336 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
389 "Could not load database backend `%s'\n", 337 "Could not load database backend `%s'\n",
390 db_lib_name); 338 db_lib_name);
391 GNUNET_SCHEDULER_add_now (&cleanup_task, NULL); 339 GNUNET_SCHEDULER_add_now (&cleanup_task,
340 NULL);
392 return; 341 return;
393 } 342 }
394 343
395 /* Configuring server handles */ 344 /* Configuring server handles */
396 GNUNET_SERVER_add_handlers (server, handlers);
397 snc = GNUNET_SERVER_notification_context_create (server, 16);
398 GNUNET_SERVER_disconnect_notify (server,
399 &client_disconnect_notification,
400 NULL);
401 GNUNET_SCHEDULER_add_shutdown (&cleanup_task, 345 GNUNET_SCHEDULER_add_shutdown (&cleanup_task,
402 NULL); 346 NULL);
403} 347}
404 348
405 349
406/** 350/**
407 * The main function for the template service. 351 * Define "main" method using service macro.
408 *
409 * @param argc number of arguments from the command line
410 * @param argv command line arguments
411 * @return 0 ok, 1 on error
412 */ 352 */
413int 353GNUNET_SERVICE_MAIN
414main (int argc, char *const *argv) 354("namecache",
415{ 355 GNUNET_SERVICE_OPTION_NONE,
416 return (GNUNET_OK == 356 &run,
417 GNUNET_SERVICE_run (argc, argv, "namecache", 357 &client_connect_cb,
418 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1; 358 &client_disconnect_cb,
419} 359 NULL,
360 GNUNET_MQ_hd_fixed_size (lookup_block,
361 GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK,
362 struct LookupBlockMessage,
363 NULL),
364 GNUNET_MQ_hd_var_size (block_cache,
365 GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE,
366 struct BlockCacheMessage,
367 NULL),
368 GNUNET_MQ_handler_end ());
369
420 370
421/* end of gnunet-service-namecache.c */ 371/* end of gnunet-service-namecache.c */