2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file gns/namestore_api.c
23 * @brief API to access the NAMESTORE service
24 * @author Martin Schanzenbach
25 * @author Matthias Wachs
29 #include "gnunet_util_lib.h"
30 #include "gnunet_constants.h"
31 #include "gnunet_arm_service.h"
32 #include "gnunet_namestore_service.h"
33 #include "namestore.h"
34 #define DEBUG_GNS_API GNUNET_EXTRA_LOGGING
36 #define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__)
41 struct GNUNET_NAMESTORE_QueueEntry
43 char *data; /*stub data pointer*/
48 * Message in linked list we should send to the service. The
49 * actual binary message follows this struct.
57 struct PendingMessage *next;
62 struct PendingMessage *prev;
65 * Size of the message.
70 * Is this the 'START' message?
77 * Connection to the NAMESTORE service.
79 struct GNUNET_NAMESTORE_Handle
83 * Configuration to use.
85 const struct GNUNET_CONFIGURATION_Handle *cfg;
88 * Socket (if available).
90 struct GNUNET_CLIENT_Connection *client;
93 * Currently pending transmission request (or NULL).
95 struct GNUNET_CLIENT_TransmitHandle *th;
100 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
103 * Pending messages to send to the service
106 struct PendingMessage * pending_head;
107 struct PendingMessage * pending_tail;
110 * Should we reconnect to service due to some serious error?
115 struct GNUNET_NAMESTORE_SimpleRecord
120 struct GNUNET_NAMESTORE_SimpleRecord *next;
125 struct GNUNET_NAMESTORE_SimpleRecord *prev;
128 const GNUNET_HashCode *zone;
129 uint32_t record_type;
130 struct GNUNET_TIME_Absolute expiration;
131 enum GNUNET_NAMESTORE_RecordFlags flags;
137 * Disconnect from service and then reconnect.
139 * @param nsh our handle
142 force_reconnect (struct GNUNET_NAMESTORE_Handle *nsh);
146 * Type of a function to call when we receive a message
149 * @param cls the 'struct GNUNET_NAMESTORE_SchedulingHandle'
150 * @param msg message received, NULL on timeout or fatal error
153 process_namestore_message (void *cls, const struct GNUNET_MessageHeader *msg)
155 struct GNUNET_NAMESTORE_Handle *nsh = cls;
161 force_reconnect (nsh);
165 size = ntohs (msg->size);
166 type = ntohs (msg->type);
169 case GNUNET_MESSAGE_TYPE_TEST:
170 /* handle message here */
175 size++; // FIXME: just working around compiler warning here...
176 GNUNET_CLIENT_receive (nsh->client, &process_namestore_message, nsh,
177 GNUNET_TIME_UNIT_FOREVER_REL);
179 if (GNUNET_YES == nsh->reconnect)
180 force_reconnect (nsh);
185 * Transmit messages from the message queue to the service
186 * (if there are any, and if we are not already trying).
188 * @param nsh handle to use
191 do_transmit (struct GNUNET_NAMESTORE_Handle *nsh);
195 * We can now transmit a message to NAMESTORE. Do it.
197 * @param cls the 'struct GNUNET_NAMESTORE_Handle'
198 * @param size number of bytes we can transmit
199 * @param buf where to copy the messages
200 * @return number of bytes copied into buf
203 transmit_message_to_namestore (void *cls, size_t size, void *buf)
205 struct GNUNET_NAMESTORE_Handle *nsh = cls;
206 struct PendingMessage *p;
211 if ((size == 0) || (buf == NULL))
213 force_reconnect (nsh);
218 while ((NULL != (p = nsh->pending_head)) && (p->size <= size))
220 memcpy (&cbuf[ret], &p[1], p->size);
223 GNUNET_CONTAINER_DLL_remove (nsh->pending_head, nsh->pending_tail, p);
224 if (GNUNET_YES == p->is_init)
225 GNUNET_CLIENT_receive (nsh->client, &process_namestore_message, nsh,
226 GNUNET_TIME_UNIT_FOREVER_REL);
235 * Transmit messages from the message queue to the service
236 * (if there are any, and if we are not already trying).
238 * @param nsh handle to use
241 do_transmit (struct GNUNET_NAMESTORE_Handle *nsh)
243 struct PendingMessage *p;
247 if (NULL == (p = nsh->pending_head))
249 if (NULL == nsh->client)
250 return; /* currently reconnecting */
252 nsh->th = GNUNET_CLIENT_notify_transmit_ready (nsh->client, p->size,
253 GNUNET_TIME_UNIT_FOREVER_REL,
254 GNUNET_NO, &transmit_message_to_namestore,
260 * Try again to connect to namestore service.
262 * @param cls the handle to the namestore service
263 * @param tc scheduler context
266 reconnect (struct GNUNET_NAMESTORE_Handle *nsh)
268 struct PendingMessage *p;
269 struct StartMessage *init;
271 GNUNET_assert (NULL == nsh->client);
272 nsh->client = GNUNET_CLIENT_connect ("namestore", nsh->cfg);
273 GNUNET_assert (NULL != nsh->client);
275 if ((NULL == (p = nsh->pending_head)) || (GNUNET_YES != p->is_init))
277 p = GNUNET_malloc (sizeof (struct PendingMessage) +
278 sizeof (struct StartMessage));
279 p->size = sizeof (struct StartMessage);
280 p->is_init = GNUNET_YES;
281 init = (struct StartMessage *) &p[1];
282 init->header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_START);
283 init->header.size = htons (sizeof (struct StartMessage));
284 GNUNET_CONTAINER_DLL_insert (nsh->pending_head, nsh->pending_tail, p);
290 * Re-establish the connection to the service.
292 * @param cls handle to use to re-connect.
293 * @param tc scheduler context
296 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
298 struct GNUNET_NAMESTORE_Handle *nsh = cls;
300 nsh->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
306 * Disconnect from service and then reconnect.
308 * @param nsh our handle
311 force_reconnect (struct GNUNET_NAMESTORE_Handle *nsh)
313 nsh->reconnect = GNUNET_NO;
314 GNUNET_CLIENT_disconnect (nsh->client, GNUNET_NO);
316 nsh->reconnect_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
324 * Initialize the connection with the NAMESTORE service.
326 * @param cfg configuration to use
327 * @return handle to the GNS service, or NULL on error
329 struct GNUNET_NAMESTORE_Handle *
330 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
332 struct GNUNET_NAMESTORE_Handle *nsh;
334 nsh = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Handle));
336 nsh->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect_task, nsh);
342 * Shutdown connection with the NAMESTORE service.
344 * @param handle handle of the NAMESTORE connection to stop
347 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *nsh, int drop)
349 struct PendingMessage *p;
351 while (NULL != (p = nsh->pending_head))
353 GNUNET_CONTAINER_DLL_remove (nsh->pending_head, nsh->pending_tail, p);
356 if (NULL != nsh->client)
358 GNUNET_CLIENT_disconnect (nsh->client, GNUNET_NO);
361 if (GNUNET_SCHEDULER_NO_TASK != nsh->reconnect_task)
363 GNUNET_SCHEDULER_cancel (nsh->reconnect_task);
364 nsh->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
372 * Store an item in the namestore. If the item is already present,
373 * the expiration time is updated to the max of the existing time and
374 * the new time. This API is used when we cache signatures from other
377 * @param h handle to the namestore
378 * @param zone hash of the public key of the zone
379 * @param name name that is being mapped (at most 255 characters long)
380 * @param expire when does the corresponding block in the DHT expire (until
381 * when should we never do a DHT lookup for the same name again)?
382 * @param rd_count number of entries in 'rd' array
383 * @param rd array of records with data to store
384 * @param signature signature for all the records in the zone under the given name
385 * @param cont continuation to call when done
386 * @param cont_cls closure for cont
387 * @return handle to abort the request
389 struct GNUNET_NAMESTORE_QueueEntry *
390 GNUNET_NAMESTORE_record_put (struct GNUNET_NAMESTORE_Handle *h,
391 const GNUNET_HashCode *zone,
393 struct GNUNET_TIME_Absolute expire,
394 unsigned int rd_count,
395 const struct GNUNET_NAMESTORE_RecordData *rd,
396 const struct GNUNET_CRYPTO_RsaSignature *signature,
397 GNUNET_NAMESTORE_ContinuationWithStatus cont,
400 struct GNUNET_NAMESTORE_QueueEntry *qe;
401 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
403 struct GNUNET_NAMESTORE_SimpleRecord *sr;
404 sr = GNUNET_malloc(sizeof(struct GNUNET_NAMESTORE_SimpleRecord));
406 sr->record_type = record_type;
407 sr->expiration = expiration;
409 sr->data_size = data_size;
411 GNUNET_CONTAINER_DLL_insert(h->records_head, h->records_tail, sr);
418 * Check if a signature is valid. This API is used by the GNS Block
419 * to validate signatures received from the network.
421 * @param public_key public key of the zone
422 * @param name name that is being mapped (at most 255 characters long)
423 * @param rd_count number of entries in 'rd' array
424 * @param rd array of records with data to store
425 * @param signature signature for all the records in the zone under the given name
426 * @return GNUNET_OK if the signature is valid
429 GNUNET_NAMESTORE_verify_signature (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
431 unsigned int rd_count,
432 const struct GNUNET_NAMESTORE_RecordData *rd,
433 const struct GNUNET_CRYPTO_RsaSignature *signature)
435 return GNUNET_SYSERR;
439 * Store an item in the namestore. If the item is already present,
440 * the expiration time is updated to the max of the existing time and
441 * the new time. This API is used by the authority of a zone.
443 * @param h handle to the namestore
444 * @param pkey private key of the zone
445 * @param name name that is being mapped (at most 255 characters long)
446 * @param rd record data to store
447 * @param cont continuation to call when done
448 * @param cont_cls closure for cont
449 * @return handle to abort the request
451 struct GNUNET_NAMESTORE_QueueEntry *
452 GNUNET_NAMESTORE_record_create (struct GNUNET_NAMESTORE_Handle *h,
453 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
455 const struct GNUNET_NAMESTORE_RecordData *rd,
456 GNUNET_NAMESTORE_ContinuationWithStatus cont,
464 * Explicitly remove some content from the database. The
465 * "cont"inuation will be called with status "GNUNET_OK" if content
466 * was removed, "GNUNET_NO" if no matching entry was found and
467 * "GNUNET_SYSERR" on all other types of errors.
468 * This API is used by the authority of a zone.
470 * @param h handle to the namestore
471 * @param pkey private key of the zone
472 * @param name name that is being mapped (at most 255 characters long)
473 * @param rd record data
474 * @param cont continuation to call when done
475 * @param cont_cls closure for cont
476 * @return handle to abort the request
478 struct GNUNET_NAMESTORE_QueueEntry *
479 GNUNET_NAMESTORE_record_remove (struct GNUNET_NAMESTORE_Handle *h,
480 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
482 const struct GNUNET_NAMESTORE_RecordData *rd,
483 GNUNET_NAMESTORE_ContinuationWithStatus cont,
486 struct GNUNET_NAMESTORE_QueueEntry *qe;
487 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
489 struct GNUNET_NAMESTORE_SimpleRecord *iter;
490 for (iter=h->records_head; iter != NULL; iter=iter->next)
492 if (strcmp ( iter->name, name ) &&
493 iter->record_type == record_type &&
494 GNUNET_CRYPTO_hash_cmp (iter->zone, zone))
498 GNUNET_CONTAINER_DLL_remove(h->records_head,
507 * Get a result for a particular key from the namestore. The processor
508 * will only be called once.
510 * @param h handle to the namestore
511 * @param zone zone to look up a record from
512 * @param name name to look up
513 * @param record_type desired record type, 0 for all
514 * @param proc function to call on the matching records, or with
515 * NULL (rd_count == 0) if there are no matching records
516 * @param proc_cls closure for proc
517 * @return a handle that can be used to
520 struct GNUNET_NAMESTORE_QueueEntry *
521 GNUNET_NAMESTORE_lookup_record (struct GNUNET_NAMESTORE_Handle *h,
522 const GNUNET_HashCode *zone,
524 uint32_t record_type,
525 GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls)
527 struct GNUNET_NAMESTORE_QueueEntry *qe;
528 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
530 struct GNUNET_NAMESTORE_SimpleRecord *iter;
531 for (iter=h->records_head; iter != NULL; iter=iter->next)
533 proc(proc_cls, iter->zone, iter->name, iter->record_type,
537 iter->data_size /*size*/,
538 iter->data /* data */);
540 proc(proc_cls, zone, name, record_type,
541 GNUNET_TIME_absolute_get_forever(), 0, NULL, 0, NULL); /*TERMINATE*/
544 GNUNET_assert (NULL != h);
546 struct PendingMessage * p;
547 struct LookupNameMessage * msg;
548 size_t msg_len = sizeof (struct LookupNameMessage);
550 p = GNUNET_malloc (sizeof (struct PendingMessage) + msg_len);
552 p->is_init = GNUNET_NO;
553 msg = (struct LookupNameMessage *) &p[1];
554 msg->header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME);
555 msg->header.size = htons (msg_len);
556 GNUNET_CONTAINER_DLL_insert (h->pending_head, h->pending_tail, p);
565 * Starts a new zone iteration (used to periodically PUT all of our
566 * records into our DHT). This MUST lock the GNUNET_NAMESTORE_Handle
567 * for any other calls than GNUNET_NAMESTORE_zone_iterator_next and
568 * GNUNET_NAMESTORE_zone_iteration_stop. "proc" will be called once
569 * immediately, and then again after
570 * "GNUNET_NAMESTORE_zone_iterator_next" is invoked.
572 * @param h handle to the namestore
573 * @param zone zone to access, NULL for all zones
574 * @param must_have_flags flags that must be set for the record to be returned
575 * @param must_not_have_flags flags that must NOT be set for the record to be returned
576 * @param proc function to call on each name from the zone; it
577 * will be called repeatedly with a value (if available)
578 * and always once at the end with a name of NULL.
579 * @param proc_cls closure for proc
580 * @return an iterator handle to use for iteration
582 struct GNUNET_NAMESTORE_ZoneIterator *
583 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
584 const GNUNET_HashCode *zone,
585 enum GNUNET_NAMESTORE_RecordFlags must_have_flags,
586 enum GNUNET_NAMESTORE_RecordFlags must_not_have_flags,
587 GNUNET_NAMESTORE_RecordProcessor proc,
595 * Calls the record processor specified in GNUNET_NAMESTORE_zone_iteration_start
596 * for the next record.
598 * @param it the iterator
601 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it)
607 * Stops iteration and releases the namestore handle for further calls.
609 * @param it the iterator
612 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it)
618 * Cancel a namestore operation. The final callback from the
619 * operation must not have been done yet.
621 * @param qe operation to cancel
624 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
630 /* end of namestore_api.c */