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_stub_api.c
23 * @brief stub library to access the NAMESTORE service
24 * @author Martin Schanzenbach
28 #include "gnunet_util_lib.h"
29 #include "gnunet_constants.h"
30 #include "gnunet_arm_service.h"
31 #include "gnunet_namestore_service.h"
33 #define DEBUG_GNS_API GNUNET_EXTRA_LOGGING
35 #define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__)
40 struct GNUNET_NAMESTORE_QueueEntry
42 char *data; /*stub data pointer*/
46 * Connection to the NAMESTORE service.
48 struct GNUNET_NAMESTORE_Handle
52 * Configuration to use.
54 const struct GNUNET_CONFIGURATION_Handle *cfg;
57 * Socket (if available).
59 struct GNUNET_CLIENT_Connection *client;
62 * Currently pending transmission request (or NULL).
64 struct GNUNET_CLIENT_TransmitHandle *th;
66 /* dll to use for records */
67 struct GNUNET_NAMESTORE_SimpleRecord * records_head;
68 struct GNUNET_NAMESTORE_SimpleRecord * records_tail;
74 struct GNUNET_NAMESTORE_ZoneIterator
76 struct GNUNET_NAMESTORE_Handle *handle;
77 GNUNET_NAMESTORE_RecordProcessor proc;
79 const GNUNET_HashCode * zone;
82 struct GNUNET_NAMESTORE_Handle *h;
83 struct GNUNET_NAMESTORE_SimpleRecord *sr;
86 struct GNUNET_NAMESTORE_SimpleRecord
91 struct GNUNET_NAMESTORE_SimpleRecord *next;
96 struct GNUNET_NAMESTORE_SimpleRecord *prev;
99 const GNUNET_HashCode *zone;
100 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key;
102 struct GNUNET_NAMESTORE_RecordData rd[100];
107 * Initialize the connection with the NAMESTORE service.
109 * @param cfg configuration to use
110 * @return handle to the GNS service, or NULL on error
112 struct GNUNET_NAMESTORE_Handle *
113 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
115 struct GNUNET_NAMESTORE_Handle *handle;
117 handle = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Handle));
119 handle->records_head = NULL;
120 handle->records_tail = NULL;
125 * Shutdown connection with the NAMESTORE service.
127 * @param handle handle of the NAMESTORE connection to stop
130 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *handle, int drop)
136 * Store an item in the namestore. If the item is already present,
137 * the expiration time is updated to the max of the existing time and
138 * the new time. The operation must fail if there is no matching
139 * entry in the signature tree.
141 * @param h handle to the namestore
142 * @param zone hash of the public key of the zone
143 * @param name name that is being mapped (at most 255 characters long)
144 * @param record_type type of the record (A, AAAA, PKEY, etc.)
145 * @param expiration expiration time for the content
146 * @param flags flags for the content
147 * @param data_size number of bytes in data
148 * @param data value, semantics depend on 'record_type' (see RFCs for DNS and
149 * GNS specification for GNS extensions)
150 * @param cont continuation to call when done
151 * @param cont_cls closure for cont
152 * @return handle to abort the request
154 struct GNUNET_NAMESTORE_QueueEntry *
155 GNUNET_NAMESTORE_record_put (struct GNUNET_NAMESTORE_Handle *h,
156 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
158 struct GNUNET_TIME_Absolute expiration,
159 unsigned int rd_count,
160 const struct GNUNET_NAMESTORE_RecordData *rd,
161 const struct GNUNET_CRYPTO_RsaSignature *signature,
162 GNUNET_NAMESTORE_ContinuationWithStatus cont,
165 struct GNUNET_NAMESTORE_QueueEntry *qe;
166 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
167 struct GNUNET_NAMESTORE_SimpleRecord* sr;
168 GNUNET_HashCode *zone;
171 zone = GNUNET_malloc(sizeof(GNUNET_HashCode));
172 GNUNET_CRYPTO_hash(public_key,
173 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
176 sr = h->records_head;
177 for (; sr != NULL; sr = sr->next)
179 if (GNUNET_CRYPTO_hash_cmp(zone, sr->zone) == 0)
181 sr->rd_count = rd_count;
182 for (i=0; i<rd_count; i++)
186 //Expiration, Signature etc
190 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "new records for %s\n", name);
192 sr = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_SimpleRecord));
193 sr->rd_count = rd_count;
194 sr->name = GNUNET_malloc(strlen(name));
196 sr->zone_key = public_key; //pkey FIXME;
199 strcpy((char*)sr->name, name);
201 for (i=0; i<rd_count; i++)
204 if (h->records_head == NULL && h->records_tail == NULL)
206 h->records_head = sr;
207 h->records_tail = sr;
211 GNUNET_CONTAINER_DLL_insert(h->records_head, h->records_tail, sr);
218 GNUNET_NAMESTORE_verify_signature (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
220 unsigned int rd_count,
221 const struct GNUNET_NAMESTORE_RecordData *rd,
222 const struct GNUNET_CRYPTO_RsaSignature *signature)
227 struct GNUNET_NAMESTORE_QueueEntry *
228 GNUNET_NAMESTORE_record_create (struct GNUNET_NAMESTORE_Handle *h,
229 const struct GNUNET_CRYPTO_RsaPrivateKey *key,
231 const struct GNUNET_NAMESTORE_RecordData *rd,
232 GNUNET_NAMESTORE_ContinuationWithStatus cont,
235 struct GNUNET_NAMESTORE_QueueEntry *qe;
236 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
237 struct GNUNET_NAMESTORE_SimpleRecord* sr;
239 GNUNET_HashCode *zone_hash;
241 //memleakage.. but only stub so w/e
242 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey;
243 pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
244 GNUNET_CRYPTO_rsa_key_get_public (key, pkey);
246 zone_hash = GNUNET_malloc(sizeof(GNUNET_HashCode));
248 GNUNET_CRYPTO_hash(pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
251 sr = h->records_head;
252 for (; sr != NULL; sr = sr->next)
254 if ((strcmp(sr->name, name) == 0) &&
255 (0 == GNUNET_CRYPTO_hash_cmp(sr->zone, zone_hash)))
258 memcpy (&(sr->rd[sr->rd_count-1]), rd,
259 sizeof(struct GNUNET_NAMESTORE_RecordData));
266 sr = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_SimpleRecord));
269 sr->name = GNUNET_malloc(strlen(name));
270 sr->zone = zone_hash;
274 strcpy((char*)sr->name, name);
276 memcpy (&(sr->rd), rd,
277 sizeof(struct GNUNET_NAMESTORE_RecordData));
278 if (h->records_head == NULL && h->records_tail == NULL)
280 h->records_head = sr;
281 h->records_tail = sr;
285 GNUNET_CONTAINER_DLL_insert(h->records_head, h->records_tail, sr);
292 * Explicitly remove some content from the database. The
293 * "cont"inuation will be called with status "GNUNET_OK" if content
294 * was removed, "GNUNET_NO" if no matching entry was found and
295 * "GNUNET_SYSERR" on all other types of errors.
297 * @param h handle to the namestore
298 * @param zone hash of the public key of the zone
299 * @param name name that is being mapped (at most 255 characters long)
300 * @param record_type type of the record (A, AAAA, PKEY, etc.)
301 * @param size number of bytes in data
302 * @param data content stored
303 * @param cont continuation to call when done
304 * @param cont_cls closure for cont
305 * @return handle to abort the request
307 struct GNUNET_NAMESTORE_QueueEntry *
308 GNUNET_NAMESTORE_record_remove (struct GNUNET_NAMESTORE_Handle *h,
309 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
311 const struct GNUNET_NAMESTORE_RecordData *rd,
312 GNUNET_NAMESTORE_ContinuationWithStatus cont,
315 struct GNUNET_NAMESTORE_QueueEntry *qe;
316 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
323 * Get a result for a particular key from the namestore. The processor
324 * will only be called once.
326 * @param h handle to the namestore
327 * @param zone zone to look up a record from
328 * @param name name to look up
329 * @param record_type desired record type
330 * @param proc function to call on each matching value;
331 * will be called once with a NULL value at the end
332 * @param proc_cls closure for proc
333 * @return a handle that can be used to
336 struct GNUNET_NAMESTORE_QueueEntry *
337 GNUNET_NAMESTORE_lookup_record (struct GNUNET_NAMESTORE_Handle *h,
338 const GNUNET_HashCode *zone,
340 uint32_t record_type,
341 GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls)
343 struct GNUNET_NAMESTORE_QueueEntry *qe;
344 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
345 struct GNUNET_NAMESTORE_SimpleRecord *sr;
346 struct GNUNET_CRYPTO_HashAsciiEncoded zone_string, zone_string_ex;
348 GNUNET_CRYPTO_hash_to_enc (zone, &zone_string);
349 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Looking up %s in %s\n", name, (char*)&zone_string);
350 sr = h->records_head;
351 for (; sr != NULL; sr = sr->next)
353 GNUNET_CRYPTO_hash_to_enc (sr->zone, &zone_string_ex);
354 if ((strcmp(sr->name, name) == 0) &&
355 (0 == (GNUNET_CRYPTO_hash_cmp(sr->zone, zone))))
357 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
358 "Found match for %s in %s with %d entries\n",
359 sr->name, (char*)&zone_string_ex, sr->rd_count);
360 //Simply always return all records
361 proc(proc_cls, sr->zone_key, GNUNET_TIME_UNIT_FOREVER_ABS, //FIXME
362 name, sr->rd_count, sr->rd, NULL);
365 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No match\n");
367 proc(proc_cls, NULL, GNUNET_TIME_UNIT_ZERO_ABS, name, 0, NULL, NULL);
372 struct GNUNET_NAMESTORE_ZoneIterator *
373 GNUNET_NAMESTORE_zone_iteration_start(struct GNUNET_NAMESTORE_Handle *h,
374 const GNUNET_HashCode *zone,
375 enum GNUNET_NAMESTORE_RecordFlags must_have_flags,
376 enum GNUNET_NAMESTORE_RecordFlags must_not_have_flags,
377 GNUNET_NAMESTORE_RecordProcessor proc,
380 struct GNUNET_NAMESTORE_ZoneIterator *it;
382 it = GNUNET_malloc(sizeof(struct GNUNET_NAMESTORE_ZoneIterator));
384 it->sr = h->records_head;
386 it->proc_cls = proc_cls;
388 it->no_flags = must_not_have_flags;
389 it->flags = must_have_flags;
390 GNUNET_NAMESTORE_zone_iterator_next(it);
395 GNUNET_NAMESTORE_zone_iterator_next(struct GNUNET_NAMESTORE_ZoneIterator *it)
398 if (it->h->locked == 0)
402 it->proc(it->proc_cls, NULL, GNUNET_TIME_UNIT_ZERO_ABS,
403 NULL, 0, NULL, NULL);
406 if (GNUNET_CRYPTO_hash_cmp(it->sr->zone, it->zone) == 0)
408 //Simply always return all records
410 it->proc(it->proc_cls, it->sr->zone_key, GNUNET_TIME_UNIT_FOREVER_ABS,
411 it->sr->name, it->sr->rd_count, it->sr->rd, NULL);
413 it->sr = it->sr->next;
417 GNUNET_NAMESTORE_zone_iteration_stop(struct GNUNET_NAMESTORE_ZoneIterator *it)
423 * Cancel a namestore operation. The final callback from the
424 * operation must not have been done yet.
426 * @param qe operation to cancel
429 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
438 /* end of namestore_stub_api.c */