GNUnet  0.10.x
gnunet-service-namecache.c
Go to the documentation of this file.
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 
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_dnsparser_lib.h"
33 #include "gnunet_signatures.h"
34 #include "namecache.h"
35 
36 #define LOG_STRERROR_FILE(kind, syscall, filename) GNUNET_log_from_strerror_file(kind, "util", syscall, filename)
37 
38 
47 
52 };
53 
54 
58 static const struct GNUNET_CONFIGURATION_Handle *GSN_cfg;
59 
64 
69 
73 static char *db_lib_name;
74 
75 
81 static void
83 {
85  "Stopping namecache service\n");
86  GNUNET_break(NULL ==
88  GSN_database));
90  db_lib_name = NULL;
91  if (NULL != statistics)
92  {
93  GNUNET_STATISTICS_destroy(statistics,
94  GNUNET_NO);
95  statistics = NULL;
96  }
97 }
98 
99 
108 static void
111  void *app_ctx)
112 {
113  struct NamecacheClient *nc = app_ctx;
114 
116  "Client %p disconnected\n",
117  client);
118  GNUNET_free(nc);
119 }
120 
121 
130 static void *
133  struct GNUNET_MQ_Handle *mq)
134 {
135  struct NamecacheClient *nc;
136 
138  "Client %p connected\n",
139  client);
140  nc = GNUNET_new(struct NamecacheClient);
141  nc->client = client;
142  nc->mq = mq;
143  return nc;
144 }
145 
146 
156 
160  uint32_t request_id;
161 
165  int status;
166 };
167 
168 
175 static void
177  const struct GNUNET_GNSRECORD_Block *block)
178 {
179  struct LookupBlockContext *lnc = cls;
180  struct GNUNET_MQ_Envelope *env;
181  struct LookupBlockResponseMessage *r;
182  size_t esize;
183  size_t bsize;
184 
185  bsize = ntohl(block->purpose.size);
186  if (bsize <
187  (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose) + sizeof(struct GNUNET_TIME_AbsoluteNBO)))
188  {
190  "Malformed block.");
191  lnc->status = GNUNET_SYSERR;
192  return;
193  }
194  esize = ntohl(block->purpose.size)
195  - sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)
196  - sizeof(struct GNUNET_TIME_AbsoluteNBO);
197  env = GNUNET_MQ_msg_extra(r,
198  esize,
200  r->gns_header.r_id = htonl(lnc->request_id);
201  r->expire = block->expiration_time;
202  r->signature = block->signature;
203  r->derived_key = block->derived_key;
204  GNUNET_memcpy(&r[1],
205  &block[1],
206  esize);
207  GNUNET_STATISTICS_update(statistics,
208  "blocks found in cache",
209  1,
210  GNUNET_NO);
212  "Sending NAMECACHE_LOOKUP_BLOCK_RESPONSE message with expiration time %s\n",
214  GNUNET_MQ_send(lnc->nc->mq,
215  env);
216 }
217 
218 
225 static void
227  const struct LookupBlockMessage *ln_msg)
228 {
229  struct NamecacheClient *nc = cls;
230  struct GNUNET_MQ_Envelope *env;
231  struct LookupBlockContext lnc;
232  struct LookupBlockResponseMessage *zir_end;
233  int ret;
234 
236  "Received NAMECACHE_LOOKUP_BLOCK message\n");
237  GNUNET_STATISTICS_update(statistics,
238  "blocks looked up",
239  1,
240  GNUNET_NO);
241  lnc.request_id = ntohl(ln_msg->gns_header.r_id);
242  lnc.nc = nc;
243  lnc.status = GNUNET_OK;
244  if (GNUNET_SYSERR ==
245  (ret = GSN_database->lookup_block(GSN_database->cls,
246  &ln_msg->query,
248  &lnc)))
249  {
250  /* 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
252  might also be false... */
253  GNUNET_break(0);
255  return;
256  }
257  if ((0 == ret) || (GNUNET_SYSERR == lnc.status))
258  {
259  /* no records match at all, generate empty response */
261  "Sending empty NAMECACHE_LOOKUP_BLOCK_RESPONSE message\n");
262  env = GNUNET_MQ_msg(zir_end,
264  zir_end->gns_header.r_id = ln_msg->gns_header.r_id;
265  GNUNET_MQ_send(nc->mq,
266  env);
267  }
269 }
270 
271 
279 static int
281  const struct BlockCacheMessage *rp_msg)
282 {
283  return GNUNET_OK;
284 }
285 
286 
293 static void
295  const struct BlockCacheMessage *rp_msg)
296 {
297  struct NamecacheClient *nc = cls;
298  struct GNUNET_MQ_Envelope *env;
299  struct BlockCacheResponseMessage *rpr_msg;
300  struct GNUNET_GNSRECORD_Block *block;
301  size_t esize;
302  int res;
303 
304  GNUNET_STATISTICS_update(statistics,
305  "blocks cached",
306  1,
307  GNUNET_NO);
308  esize = ntohs(rp_msg->gns_header.header.size) - sizeof(struct BlockCacheMessage);
309  block = GNUNET_malloc(sizeof(struct GNUNET_GNSRECORD_Block) + esize);
310  block->signature = rp_msg->signature;
311  block->derived_key = rp_msg->derived_key;
312  block->purpose.size = htonl(sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose) +
313  sizeof(struct GNUNET_TIME_AbsoluteNBO) +
314  esize);
315  block->expiration_time = rp_msg->expire;
317  "Received NAMECACHE_BLOCK_CACHE message with expiration time %s\n",
319  GNUNET_memcpy(&block[1],
320  &rp_msg[1],
321  esize);
322  res = GSN_database->cache_block(GSN_database->cls,
323  block);
324  GNUNET_free(block);
325  env = GNUNET_MQ_msg(rpr_msg,
327  rpr_msg->gns_header.r_id = rp_msg->gns_header.r_id;
328  rpr_msg->op_result = htonl(res);
329  GNUNET_MQ_send(nc->mq,
330  env);
332 }
333 
334 
342 static void
343 run(void *cls,
344  const struct GNUNET_CONFIGURATION_Handle *cfg,
346 {
347  char *database;
348 
350  "Starting namecache service\n");
351  GSN_cfg = cfg;
352 
353  /* Loading database plugin */
354  if (GNUNET_OK !=
356  "namecache",
357  "database",
358  &database))
360  "No database backend configured\n");
361 
363  "libgnunet_plugin_namecache_%s",
364  database);
365  GSN_database = GNUNET_PLUGIN_load(db_lib_name,
366  (void *)GSN_cfg);
367  GNUNET_free(database);
368  if (NULL == GSN_database)
369  {
371  "Could not load database backend `%s'\n",
372  db_lib_name);
374  NULL);
375  return;
376  }
377  statistics = GNUNET_STATISTICS_create("namecache",
378  cfg);
379 
380  /* Configuring server handles */
382  NULL);
383 }
384 
385 
390  ("namecache",
392  &run,
395  NULL,
396  GNUNET_MQ_hd_fixed_size(lookup_block,
398  struct LookupBlockMessage,
399  NULL),
400  GNUNET_MQ_hd_var_size(block_cache,
402  struct BlockCacheMessage,
403  NULL),
405 
406 
407 /* end of gnunet-service-namecache.c */
#define GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE
Service to client: result of block lookup.
#define GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE_RESPONSE
Service to client: result of block cache request.
Lookup a block in the namecache.
Definition: namecache.h:57
struct GNUNET_TIME_AbsoluteNBO expiration_time
Expiration time of the block.
static struct GNUNET_SERVICE_Handle * service
Handle to our service instance.
#define GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE
Client to service: cache a block.
void * cls
Closure to pass to all plugin functions.
struct GNUNET_CRYPTO_EcdsaSignature signature
Signature.
Definition: namecache.h:87
Lookup response.
Definition: namecache.h:73
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh(struct GNUNET_TIME_AbsoluteNBO a)
Convert absolute time from network byte order.
Definition: time.c:671
static void * client_connect_cb(void *cls, struct GNUNET_SERVICE_Client *client, struct GNUNET_MQ_Handle *mq)
Add a client to our list of active clients.
struct GNUNET_MQ_Handle * mq
The message queue to talk to client.
static void client_disconnect_cb(void *cls, struct GNUNET_SERVICE_Client *client, void *app_ctx)
Called whenever a client is disconnected.
struct GNUNET_CRYPTO_EcdsaPublicKey derived_key
Derived public key.
Definition: namecache.h:120
Handle to a service.
Definition: service.c:114
Context for name lookups passed from handle_lookup_block to handle_lookup_block_it as closure...
Response to a request to cache a block.
Definition: namecache.h:129
struct GNUNET_MessageHeader header
header.type will be GNUNET_MESSAGE_TYPE_NAMECACHE_* header.size will be message size ...
Definition: namecache.h:45
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received, or when GNUNET_SCHEDULER_shutdown() is being invoked.
Definition: scheduler.c:1284
struct GNUNET_STATISTICS_Handle * GNUNET_STATISTICS_create(const char *subsystem, const struct GNUNET_CONFIGURATION_Handle *cfg)
Get handle for the statistics service.
struct GNUNET_NAMECACHE_Header gns_header
Type will be GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK.
Definition: namecache.h:61
static void handle_lookup_block_it(void *cls, const struct GNUNET_GNSRECORD_Block *block)
A GNUNET_NAMECACHE_BlockCallback for name lookups in handle_lookup_block.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_MQ_hd_fixed_size(name, code, str, ctx)
static void handle_block_cache(void *cls, const struct BlockCacheMessage *rp_msg)
Handles a GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE message.
#define GNUNET_MQ_msg(mvar, type)
Allocate a GNUNET_MQ_Envelope.
Definition: gnunet_mq_lib.h:67
#define GNUNET_NO
Definition: gnunet_common.h:78
static char * db_lib_name
Name of the database plugin.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_new(type)
Allocate a struct or union of the given type.
struct GNUNET_SERVICE_Client * client
The client.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
Information we have in an encrypted block with record data (i.e.
void GNUNET_STATISTICS_destroy(struct GNUNET_STATISTICS_Handle *h, int sync_first)
Destroy a handle (free all state associated with it).
void GNUNET_STATISTICS_update(struct GNUNET_STATISTICS_Handle *handle, const char *name, int64_t delta, int make_persistent)
Set statistic value for the peer.
static int ret
Final status code.
Definition: gnunet-arm.c:89
Handle for the service.
Time for absolute time used by GNUnet, in microseconds and in network byte order. ...
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
uint32_t request_id
Operation id for the name lookup.
#define GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK
Client to service: lookup block.
void * GNUNET_PLUGIN_unload(const char *library_name, void *arg)
Unload plugin (runs the "done" callback and returns whatever "done" returned).
Definition: plugin.c:255
Handle to a client that is connected to a service.
Definition: service.c:246
header of what an ECC signature signs this must be followed by "size - 8" bytes of the actual signed ...
int(* cache_block)(void *cls, const struct GNUNET_GNSRECORD_Block *block)
Cache a block in the datastore.
A namecache client.
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
#define GNUNET_MQ_msg_extra(mvar, esize, type)
Allocate an envelope, with extra space allocated after the space needed by the message struct...
Definition: gnunet_mq_lib.h:52
Cache a record in the namecache.
Definition: namecache.h:101
static struct GNUNET_STATISTICS_Handle * statistics
Handle to the statistics service.
static struct GNUNET_NAMECACHE_PluginFunctions * GSN_database
Database handle.
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
int(* lookup_block)(void *cls, const struct GNUNET_HashCode *query, GNUNET_NAMECACHE_BlockCallback iter, void *iter_cls)
Get the block for a particular zone and label in the datastore.
static void handle_lookup_block(void *cls, const struct LookupBlockMessage *ln_msg)
Handles a GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK message.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run as soon as possible.
Definition: scheduler.c:1264
uint32_t size
How many bytes does this signature sign? (including this purpose header); in network byte order (!)...
static unsigned int bsize
void GNUNET_SERVICE_client_drop(struct GNUNET_SERVICE_Client *c)
Ask the server to disconnect from the given client.
Definition: service.c:2315
static int res
struct GNUNET_NAMECACHE_Header gns_header
Type will be GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE_RESPONSE.
Definition: namecache.h:133
int GNUNET_CONFIGURATION_get_value_string(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be a string.
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:104
struct GNUNET_NAMECACHE_Header gns_header
Type will be GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE.
Definition: namecache.h:77
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
static struct GNUNET_PEERINFO_NotifyContext * nc
Iterator context.
struct GNUNET_TIME_AbsoluteNBO expire
Expiration time.
Definition: namecache.h:110
static int check_block_cache(void *cls, const struct BlockCacheMessage *rp_msg)
Check a GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE message.
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
struct returned by the initialization function of the plugin
Handle to a message queue.
Definition: mq.c:84
struct GNUNET_CRYPTO_EcdsaSignature signature
Signature of the block.
struct GNUNET_HashCode query
The query.
Definition: namecache.h:66
static const struct GNUNET_CONFIGURATION_Handle * GSN_cfg
Configuration handle.
configuration data
Definition: configuration.c:83
struct GNUNET_CRYPTO_EcdsaPublicKey derived_key
Derived public key.
Definition: namecache.h:92
uint32_t r_id
Request ID in NBO.
Definition: namecache.h:50
static void cleanup_task(void *cls)
Task run during shutdown.
#define GNUNET_log(kind,...)
GNUNET_SERVICE_MAIN("namecache", GNUNET_SERVICE_OPTION_NONE, &run, &client_connect_cb, &client_disconnect_cb, NULL, GNUNET_MQ_hd_fixed_size(lookup_block, GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK, struct LookupBlockMessage, NULL), GNUNET_MQ_hd_var_size(block_cache, GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE, struct BlockCacheMessage, NULL), GNUNET_MQ_handler_end())
Define "main" method using service macro.
struct GNUNET_TIME_AbsoluteNBO expire
Expiration time.
Definition: namecache.h:82
void * GNUNET_PLUGIN_load(const char *library_name, void *arg)
Setup plugin (runs the "init" callback and returns whatever "init" returned).
Definition: plugin.c:206
void GNUNET_MQ_send(struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
Send a message with the given message queue.
Definition: mq.c:351
const char * GNUNET_STRINGS_absolute_time_to_string(struct GNUNET_TIME_Absolute t)
Like asctime, except for GNUnet time.
Definition: strings.c:741
struct GNUNET_CRYPTO_EcdsaSignature signature
Signature.
Definition: namecache.h:115
static void run(void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg, struct GNUNET_SERVICE_Handle *service)
Process namecache requests.
int32_t op_result
GNUNET_OK on success, GNUNET_SYSERR error
Definition: namecache.h:138
struct GNUNET_CRYPTO_EccSignaturePurpose purpose
Number of bytes signed; also specifies the number of bytes of encrypted data that follow...
struct NamecacheClient * nc
The client to send the response to.
common internal definitions for namecache service
struct GNUNET_CRYPTO_EcdsaPublicKey derived_key
Derived key used for signing; hash of this is the query.
void GNUNET_SERVICE_client_continue(struct GNUNET_SERVICE_Client *c)
Continue receiving further messages from the given client.
Definition: service.c:2234
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
struct GNUNET_NAMECACHE_Header gns_header
Type will be GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE.
Definition: namecache.h:105