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 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "records for %s already present ow\n",
183 sr->rd_count = rd_count;
184 for (i=0; i<rd_count; i++)
188 //Expiration, Signature etc
192 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "new records for %s\n", name);
194 sr = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_SimpleRecord));
195 sr->rd_count = rd_count;
196 sr->name = GNUNET_malloc(strlen(name));
198 sr->zone_key = public_key; //pkey FIXME;
201 strcpy((char*)sr->name, name);
203 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "copying \n");
204 for (i=0; i<rd_count; i++)
206 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "done \n");
208 if (h->records_head == NULL && h->records_tail == NULL)
210 h->records_head = sr;
211 h->records_tail = sr;
215 GNUNET_CONTAINER_DLL_insert(h->records_head, h->records_tail, sr);
222 GNUNET_NAMESTORE_verify_signature (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
224 unsigned int rd_count,
225 const struct GNUNET_NAMESTORE_RecordData *rd,
226 const struct GNUNET_CRYPTO_RsaSignature *signature)
231 struct GNUNET_NAMESTORE_QueueEntry *
232 GNUNET_NAMESTORE_record_create (struct GNUNET_NAMESTORE_Handle *h,
233 const struct GNUNET_CRYPTO_RsaPrivateKey *key,
235 const struct GNUNET_NAMESTORE_RecordData *rd,
236 GNUNET_NAMESTORE_ContinuationWithStatus cont,
239 struct GNUNET_NAMESTORE_QueueEntry *qe;
240 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
241 struct GNUNET_NAMESTORE_SimpleRecord* sr;
243 GNUNET_HashCode *zone_hash;
245 //memleakage.. but only stub so w/e
246 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey;
247 pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
248 GNUNET_CRYPTO_rsa_key_get_public (key, pkey);
250 zone_hash = GNUNET_malloc(sizeof(GNUNET_HashCode));
252 GNUNET_CRYPTO_hash(pkey, GNUNET_CRYPTO_RSA_KEY_LENGTH, zone_hash);
254 sr = h->records_head;
255 for (; sr != NULL; sr = sr->next)
257 if (strcmp(sr->name, name) && (sr->zone == zone_hash))
260 memcpy (&(sr->rd[sr->rd_count-1]), rd,
261 sizeof(struct GNUNET_NAMESTORE_RecordData));
268 sr = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_SimpleRecord));
271 sr->name = GNUNET_malloc(strlen(name));
272 sr->zone = zone_hash;
276 strcpy((char*)sr->name, name);
278 memcpy (&(sr->rd), rd,
279 sizeof(struct GNUNET_NAMESTORE_RecordData));
280 if (h->records_head == NULL && h->records_tail == NULL)
282 h->records_head = sr;
283 h->records_tail = sr;
287 GNUNET_CONTAINER_DLL_insert(h->records_head, h->records_tail, sr);
294 * Explicitly remove some content from the database. The
295 * "cont"inuation will be called with status "GNUNET_OK" if content
296 * was removed, "GNUNET_NO" if no matching entry was found and
297 * "GNUNET_SYSERR" on all other types of errors.
299 * @param h handle to the namestore
300 * @param zone hash of the public key of the zone
301 * @param name name that is being mapped (at most 255 characters long)
302 * @param record_type type of the record (A, AAAA, PKEY, etc.)
303 * @param size number of bytes in data
304 * @param data content stored
305 * @param cont continuation to call when done
306 * @param cont_cls closure for cont
307 * @return handle to abort the request
309 struct GNUNET_NAMESTORE_QueueEntry *
310 GNUNET_NAMESTORE_record_remove (struct GNUNET_NAMESTORE_Handle *h,
311 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
313 const struct GNUNET_NAMESTORE_RecordData *rd,
314 GNUNET_NAMESTORE_ContinuationWithStatus cont,
317 struct GNUNET_NAMESTORE_QueueEntry *qe;
318 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
325 * Get a result for a particular key from the namestore. The processor
326 * will only be called once.
328 * @param h handle to the namestore
329 * @param zone zone to look up a record from
330 * @param name name to look up
331 * @param record_type desired record type
332 * @param proc function to call on each matching value;
333 * will be called once with a NULL value at the end
334 * @param proc_cls closure for proc
335 * @return a handle that can be used to
338 struct GNUNET_NAMESTORE_QueueEntry *
339 GNUNET_NAMESTORE_lookup_record (struct GNUNET_NAMESTORE_Handle *h,
340 const GNUNET_HashCode *zone,
342 uint32_t record_type,
343 GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls)
345 struct GNUNET_NAMESTORE_QueueEntry *qe;
346 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
347 struct GNUNET_NAMESTORE_SimpleRecord *sr;
349 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Looking up %s\n", name);
350 sr = h->records_head;
351 for (; sr != NULL; sr = sr->next)
353 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Got %s\n", sr->name);
354 if ((strcmp(sr->name, name) == 0) &&
355 (0 == (GNUNET_CRYPTO_hash_cmp(sr->zone, zone))))
357 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
358 "Found match for %s with %d entries\n",
359 sr->name, 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_INFO, "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);
407 if (GNUNET_CRYPTO_hash_cmp(it->sr->zone, it->zone))
409 //Simply always return all records
411 it->proc(it->proc_cls, it->sr->zone_key, GNUNET_TIME_UNIT_FOREVER_ABS,
412 it->sr->name, it->sr->rd_count, it->sr->rd, NULL);
414 it->sr = it->sr->next;
418 GNUNET_NAMESTORE_zone_iteration_stop(struct GNUNET_NAMESTORE_ZoneIterator *it)
424 * Cancel a namestore operation. The final callback from the
425 * operation must not have been done yet.
427 * @param qe operation to cancel
430 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
439 /* end of namestore_stub_api.c */