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;
85 struct GNUNET_NAMESTORE_SimpleRecord
90 struct GNUNET_NAMESTORE_SimpleRecord *next;
95 struct GNUNET_NAMESTORE_SimpleRecord *prev;
98 const GNUNET_HashCode *zone;
99 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key;
101 struct GNUNET_NAMESTORE_RecordData rd[100];
106 * Initialize the connection with the NAMESTORE service.
108 * @param cfg configuration to use
109 * @return handle to the GNS service, or NULL on error
111 struct GNUNET_NAMESTORE_Handle *
112 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
114 struct GNUNET_NAMESTORE_Handle *handle;
116 handle = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Handle));
118 handle->records_head = NULL;
119 handle->records_tail = NULL;
124 * Shutdown connection with the NAMESTORE service.
126 * @param handle handle of the NAMESTORE connection to stop
129 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *handle, int drop)
135 * Store an item in the namestore. If the item is already present,
136 * the expiration time is updated to the max of the existing time and
137 * the new time. The operation must fail if there is no matching
138 * entry in the signature tree.
140 * @param h handle to the namestore
141 * @param zone hash of the public key of the zone
142 * @param name name that is being mapped (at most 255 characters long)
143 * @param record_type type of the record (A, AAAA, PKEY, etc.)
144 * @param expiration expiration time for the content
145 * @param flags flags for the content
146 * @param data_size number of bytes in data
147 * @param data value, semantics depend on 'record_type' (see RFCs for DNS and
148 * GNS specification for GNS extensions)
149 * @param cont continuation to call when done
150 * @param cont_cls closure for cont
151 * @return handle to abort the request
153 struct GNUNET_NAMESTORE_QueueEntry *
154 GNUNET_NAMESTORE_record_put (struct GNUNET_NAMESTORE_Handle *h,
155 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
157 struct GNUNET_TIME_Absolute expiration,
158 unsigned int rd_count,
159 const struct GNUNET_NAMESTORE_RecordData *rd,
160 const struct GNUNET_CRYPTO_RsaSignature *signature,
161 GNUNET_NAMESTORE_ContinuationWithStatus cont,
164 struct GNUNET_NAMESTORE_QueueEntry *qe;
165 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
166 struct GNUNET_NAMESTORE_SimpleRecord* sr;
167 GNUNET_HashCode *zone;
170 zone = GNUNET_malloc(sizeof(GNUNET_HashCode));
171 GNUNET_CRYPTO_hash(public_key,
172 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
175 sr = h->records_head;
176 for (; sr != NULL; sr = sr->next)
178 if (GNUNET_CRYPTO_hash_cmp(zone, sr->zone) == 0)
180 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "records for %s already present ow\n",
182 sr->rd_count = rd_count;
183 for (i=0; i<rd_count; i++)
187 //Expiration, Signature etc
191 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "new records for %s\n", name);
193 sr = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_SimpleRecord));
194 sr->rd_count = rd_count;
195 sr->name = GNUNET_malloc(strlen(name));
197 sr->zone_key = public_key; //pkey FIXME;
200 strcpy((char*)sr->name, name);
202 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "copying \n");
203 for (i=0; i<rd_count; i++)
205 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "done \n");
207 if (h->records_head == NULL && h->records_tail == NULL)
209 h->records_head = sr;
210 h->records_tail = sr;
214 GNUNET_CONTAINER_DLL_insert(h->records_head, h->records_tail, sr);
221 GNUNET_NAMESTORE_verify_signature (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
223 unsigned int rd_count,
224 const struct GNUNET_NAMESTORE_RecordData *rd,
225 const struct GNUNET_CRYPTO_RsaSignature *signature)
230 struct GNUNET_NAMESTORE_QueueEntry *
231 GNUNET_NAMESTORE_record_create (struct GNUNET_NAMESTORE_Handle *h,
232 const struct GNUNET_CRYPTO_RsaPrivateKey *key,
234 const struct GNUNET_NAMESTORE_RecordData *rd,
235 GNUNET_NAMESTORE_ContinuationWithStatus cont,
238 struct GNUNET_NAMESTORE_QueueEntry *qe;
239 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
240 struct GNUNET_NAMESTORE_SimpleRecord* sr;
242 GNUNET_HashCode *zone_hash;
244 //memleakage.. but only stub so w/e
245 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey;
246 pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
247 GNUNET_CRYPTO_rsa_key_get_public (key, pkey);
249 zone_hash = GNUNET_malloc(sizeof(GNUNET_HashCode));
251 GNUNET_CRYPTO_hash(pkey, GNUNET_CRYPTO_RSA_KEY_LENGTH, zone_hash);
253 sr = h->records_head;
254 for (; sr != NULL; sr = sr->next)
256 if (strcmp(sr->name, name) && (sr->zone == zone_hash))
259 memcpy (&(sr->rd[sr->rd_count-1]), rd,
260 sizeof(struct GNUNET_NAMESTORE_RecordData));
267 sr = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_SimpleRecord));
270 sr->name = GNUNET_malloc(strlen(name));
271 sr->zone = zone_hash;
275 strcpy((char*)sr->name, name);
277 memcpy (&(sr->rd), rd,
278 sizeof(struct GNUNET_NAMESTORE_RecordData));
279 if (h->records_head == NULL && h->records_tail == NULL)
281 h->records_head = sr;
282 h->records_tail = sr;
286 GNUNET_CONTAINER_DLL_insert(h->records_head, h->records_tail, sr);
293 * Explicitly remove some content from the database. The
294 * "cont"inuation will be called with status "GNUNET_OK" if content
295 * was removed, "GNUNET_NO" if no matching entry was found and
296 * "GNUNET_SYSERR" on all other types of errors.
298 * @param h handle to the namestore
299 * @param zone hash of the public key of the zone
300 * @param name name that is being mapped (at most 255 characters long)
301 * @param record_type type of the record (A, AAAA, PKEY, etc.)
302 * @param size number of bytes in data
303 * @param data content stored
304 * @param cont continuation to call when done
305 * @param cont_cls closure for cont
306 * @return handle to abort the request
308 struct GNUNET_NAMESTORE_QueueEntry *
309 GNUNET_NAMESTORE_record_remove (struct GNUNET_NAMESTORE_Handle *h,
310 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
312 const struct GNUNET_NAMESTORE_RecordData *rd,
313 GNUNET_NAMESTORE_ContinuationWithStatus cont,
316 struct GNUNET_NAMESTORE_QueueEntry *qe;
317 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
324 * Get a result for a particular key from the namestore. The processor
325 * will only be called once.
327 * @param h handle to the namestore
328 * @param zone zone to look up a record from
329 * @param name name to look up
330 * @param record_type desired record type
331 * @param proc function to call on each matching value;
332 * will be called once with a NULL value at the end
333 * @param proc_cls closure for proc
334 * @return a handle that can be used to
337 struct GNUNET_NAMESTORE_QueueEntry *
338 GNUNET_NAMESTORE_lookup_record (struct GNUNET_NAMESTORE_Handle *h,
339 const GNUNET_HashCode *zone,
341 uint32_t record_type,
342 GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls)
344 struct GNUNET_NAMESTORE_QueueEntry *qe;
345 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
346 struct GNUNET_NAMESTORE_SimpleRecord *sr;
348 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Looking up %s\n", name);
349 sr = h->records_head;
350 for (; sr != NULL; sr = sr->next)
352 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Got %s\n", sr->name);
353 if ((strcmp(sr->name, name) == 0) &&
354 (GNUNET_CRYPTO_hash_cmp(sr->zone, zone)))
356 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
357 "Found match for %s with %d entries\n",
358 sr->name, sr->rd_count);
359 //Simply always return all records
360 proc(proc_cls, sr->zone_key, GNUNET_TIME_UNIT_FOREVER_ABS, //FIXME
361 name, sr->rd_count, sr->rd, NULL);
364 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "No match\n");
366 proc(proc_cls, NULL, GNUNET_TIME_UNIT_ZERO_ABS, name, 0, NULL, NULL);
371 struct GNUNET_NAMESTORE_ZoneIterator *
372 GNUNET_NAMESTORE_zone_iteration_start(struct GNUNET_NAMESTORE_Handle *h,
373 const GNUNET_HashCode *zone,
374 enum GNUNET_NAMESTORE_RecordFlags must_have_flags,
375 enum GNUNET_NAMESTORE_RecordFlags must_not_have_flags,
376 GNUNET_NAMESTORE_RecordProcessor proc,
379 struct GNUNET_NAMESTORE_ZoneIterator *it;
381 it = GNUNET_malloc(sizeof(struct GNUNET_NAMESTORE_ZoneIterator));
384 it->proc_cls = proc_cls;
386 it->no_flags = must_not_have_flags;
387 it->flags = must_have_flags;
388 GNUNET_NAMESTORE_zone_iterator_next(it);
393 GNUNET_NAMESTORE_zone_iterator_next(struct GNUNET_NAMESTORE_ZoneIterator *it)
395 struct GNUNET_NAMESTORE_SimpleRecord *sr;
397 if (it->h->locked == 0)
400 sr = it->h->records_head;
401 for (; sr != NULL; sr = sr->next)
403 if (GNUNET_CRYPTO_hash_cmp(sr->zone, it->zone))
405 //Simply always return all records
407 it->proc(it->proc_cls, sr->zone_key, GNUNET_TIME_UNIT_FOREVER_ABS, //FIXME
408 sr->name, sr->rd_count, sr->rd, NULL);
411 it->proc(it->proc_cls, NULL, GNUNET_TIME_UNIT_ZERO_ABS, NULL, 0, NULL, NULL);
415 GNUNET_NAMESTORE_zone_iteration_stop(struct GNUNET_NAMESTORE_ZoneIterator *it)
422 * Cancel a namestore operation. The final callback from the
423 * operation must not have been done yet.
425 * @param qe operation to cancel
428 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
437 /* end of namestore_stub_api.c */