- Changes for long churn (test with 10 peers)
[oweals/gnunet.git] / src / gns / namestore_stub_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 Christian Grothoff (and other contributing authors)
4
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.
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      General Public License for more details.
14
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.
19 */
20
21 /**
22  * @file gns/namestore_stub_api.c
23  * @brief stub library to access the NAMESTORE service
24  * @author Martin Schanzenbach
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_constants.h"
30 #include "gnunet_arm_service.h"
31 #include "gnunet_namestore_service.h"
32
33 #define DEBUG_GNS_API GNUNET_EXTRA_LOGGING
34
35 #define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__)
36
37 /**
38  * A QueueEntry.
39  */
40 struct GNUNET_NAMESTORE_QueueEntry
41 {
42   char *data; /*stub data pointer*/
43 };
44
45 /**
46  * Connection to the NAMESTORE service.
47  */
48 struct GNUNET_NAMESTORE_Handle
49 {
50
51   /**
52    * Configuration to use.
53    */
54   const struct GNUNET_CONFIGURATION_Handle *cfg;
55
56   /**
57    * Socket (if available).
58    */
59   struct GNUNET_CLIENT_Connection *client;
60
61   /**
62    * Currently pending transmission request (or NULL).
63    */
64   struct GNUNET_CLIENT_TransmitHandle *th;
65
66   /* dll to use for records */
67   struct GNUNET_NAMESTORE_SimpleRecord * records_head;
68   struct GNUNET_NAMESTORE_SimpleRecord * records_tail;
69
70 };
71
72 struct GNUNET_NAMESTORE_ZoneIterator
73 {
74   struct GNUNET_NAMESTORE_Handle *handle;
75 };
76
77 struct GNUNET_NAMESTORE_SimpleRecord
78 {
79   /**
80    * DLL
81    */
82   struct GNUNET_NAMESTORE_SimpleRecord *next;
83
84   /**
85    * DLL
86    */
87   struct GNUNET_NAMESTORE_SimpleRecord *prev;
88   
89   const char *name;
90   const GNUNET_HashCode *zone;
91   uint32_t record_type;
92   struct GNUNET_TIME_Absolute expiration;
93   enum GNUNET_NAMESTORE_RecordFlags flags;
94   size_t data_size;
95   const void *data;
96 };
97
98 /**
99  * Initialize the connection with the NAMESTORE service.
100  *
101  * @param cfg configuration to use
102  * @return handle to the GNS service, or NULL on error
103  */
104 struct GNUNET_NAMESTORE_Handle *
105 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
106 {
107   struct GNUNET_NAMESTORE_Handle *handle;
108
109   handle = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Handle));
110   handle->cfg = cfg;
111   return handle;
112 }
113
114
115 /**
116  * Shutdown connection with the NAMESTORE service.
117  *
118  * @param handle handle of the NAMESTORE connection to stop
119  */
120 void
121 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *handle, int drop)
122 {
123   GNUNET_free(handle);
124 }
125
126 /**
127  * Store an item in the namestore.  If the item is already present,
128  * the expiration time is updated to the max of the existing time and
129  * the new time.  The operation must fail if there is no matching
130  * entry in the signature tree.
131  *
132  * @param h handle to the namestore
133  * @param zone hash of the public key of the zone
134  * @param name name that is being mapped (at most 255 characters long)
135  * @param record_type type of the record (A, AAAA, PKEY, etc.)
136  * @param expiration expiration time for the content
137  * @param flags flags for the content
138  * @param data_size number of bytes in data
139  * @param data value, semantics depend on 'record_type' (see RFCs for DNS and 
140  *             GNS specification for GNS extensions)
141  * @param cont continuation to call when done
142  * @param cont_cls closure for cont
143  * @return handle to abort the request
144  */
145 struct GNUNET_NAMESTORE_QueueEntry *
146 GNUNET_NAMESTORE_record_put (struct GNUNET_NAMESTORE_Handle *h,
147                              const GNUNET_HashCode *zone,
148                              const char *name,
149                              uint32_t record_type,
150                              struct GNUNET_TIME_Absolute expiration,
151                              enum GNUNET_NAMESTORE_RecordFlags flags,
152                              size_t data_size,
153                              const void *data, 
154                              GNUNET_NAMESTORE_ContinuationWithStatus cont,
155                              void *cont_cls)
156 {
157   struct GNUNET_NAMESTORE_QueueEntry *qe;
158   qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
159
160   struct GNUNET_NAMESTORE_SimpleRecord *sr;
161   sr = GNUNET_malloc(sizeof(struct GNUNET_NAMESTORE_SimpleRecord));
162   sr->name = name;
163   sr->record_type = record_type;
164   sr->expiration = expiration;
165   sr->flags = flags;
166   sr->data_size = data_size;
167   sr->data = data;
168   GNUNET_CONTAINER_DLL_insert(h->records_head, h->records_tail, sr);
169   return qe;
170 }
171
172 /**
173  * Store a signature in the namestore. 
174  *
175  * @param h handle to the namestore
176  * @param zone hash of the public key of the zone
177  * @param name name that is being mapped (at most 255 characters long)
178  * @param record_type type of the record (A, AAAA, PKEY, etc.)
179  * @param expiration expiration time for the content
180  * @param flags flags for the content
181  * @param data_size number of bytes in data
182  * @param data value, semantics depend on 'record_type' (see RFCs for DNS and 
183  *             GNS specification for GNS extensions)
184  * @param cont continuation to call when done
185  * @param cont_cls closure for cont
186  * @return handle to abort the request
187  */
188 struct GNUNET_NAMESTORE_QueueEntry *
189 GNUNET_NAMESTORE_signature_put (struct GNUNET_NAMESTORE_Handle *h,
190                              const GNUNET_HashCode *zone,
191                              const char *name,
192            struct GNUNET_CRYPTO_RsaSignature sig,
193                              GNUNET_NAMESTORE_ContinuationWithStatus cont,
194                              void *cont_cls)
195 {
196   struct GNUNET_NAMESTORE_QueueEntry *qe;
197   qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
198
199   return qe;
200 }
201
202 /**
203  * Explicitly remove some content from the database.  The
204  * "cont"inuation will be called with status "GNUNET_OK" if content
205  * was removed, "GNUNET_NO" if no matching entry was found and
206  * "GNUNET_SYSERR" on all other types of errors.
207  *
208  * @param h handle to the namestore
209  * @param zone hash of the public key of the zone
210  * @param name name that is being mapped (at most 255 characters long)
211  * @param record_type type of the record (A, AAAA, PKEY, etc.)
212  * @param size number of bytes in data
213  * @param data content stored
214  * @param cont continuation to call when done
215  * @param cont_cls closure for cont
216  * @return handle to abort the request
217  */
218 struct GNUNET_NAMESTORE_QueueEntry *
219 GNUNET_NAMESTORE_record_remove (struct GNUNET_NAMESTORE_Handle *h,
220                                 const GNUNET_HashCode *zone, 
221                                 const char *name,
222                                 uint32_t record_type,
223                                 size_t size,
224                                 const void *data, 
225                                 GNUNET_NAMESTORE_ContinuationWithStatus cont,
226                                 void *cont_cls)
227 {
228   struct GNUNET_NAMESTORE_QueueEntry *qe;
229   qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
230   
231   struct GNUNET_NAMESTORE_SimpleRecord *iter;
232   for (iter=h->records_head; iter != NULL; iter=iter->next)
233   {
234     if (strcmp ( iter->name, name ) &&
235         iter->record_type == record_type &&
236         GNUNET_CRYPTO_hash_cmp (iter->zone, zone))
237       break;
238   }
239   if (iter)
240     GNUNET_CONTAINER_DLL_remove(h->records_head,
241                                 h->records_tail,
242                                 iter);
243   
244   return qe;
245 }
246
247 /**
248  * Get a result for a particular key from the namestore.  The processor
249  * will only be called once.
250  *
251  * @param h handle to the namestore
252  * @param zone zone to look up a record from
253  * @param name name to look up
254  * @param record_type desired record type
255  * @param proc function to call on each matching value;
256  *        will be called once with a NULL value at the end
257  * @param proc_cls closure for proc
258  * @return a handle that can be used to
259  *         cancel
260  */
261 struct GNUNET_NAMESTORE_QueueEntry *
262 GNUNET_NAMESTORE_lookup_record (struct GNUNET_NAMESTORE_Handle *h, 
263                               const GNUNET_HashCode *zone,
264                               const char *name,
265                               uint32_t record_type,
266                               GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls)
267 {
268   struct GNUNET_NAMESTORE_QueueEntry *qe;
269   qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
270
271   struct GNUNET_NAMESTORE_SimpleRecord *iter;
272   for (iter=h->records_head; iter != NULL; iter=iter->next)
273   {
274     if (strcmp(iter->name, name))
275       continue;
276
277     if (iter->record_type != record_type)
278       continue;
279
280     proc(proc_cls, iter->zone, iter->name, iter->record_type,
281        iter->expiration,
282        iter->flags,
283        iter->data_size /*size*/,
284        iter->data /* data */);
285   }
286   proc(proc_cls, zone, name, record_type,
287        GNUNET_TIME_absolute_get_forever(), 0, 0, NULL); /*TERMINATE*/
288
289   return qe;
290 }
291
292 struct GNUNET_NAMESTORE_QueueEntry *
293 GNUNET_NAMESTORE_lookup_signature (struct GNUNET_NAMESTORE_Handle *h,
294                                    const GNUNET_HashCode *zone,
295                                    const char* name,
296                                    GNUNET_NAMESTORE_SignatureProcessor proc,
297                                    void *proc_cls)
298 {
299   return NULL;
300 }
301
302
303 /**
304  * Get the hash of a record (what will be signed in the Stree for
305  * the record).
306  *
307  * @param zone hash of the public key of the zone
308  * @param name name that is being mapped (at most 255 characters long)
309  * @param record_type type of the record (A, AAAA, PKEY, etc.)
310  * @param expiration expiration time for the content
311  * @param flags flags for the content
312  * @param data_size number of bytes in data
313  * @param data value, semantics depend on 'record_type' (see RFCs for DNS and.
314  *             GNS specification for GNS extensions)
315  * @param record_hash hash of the record (set)
316  */
317 void
318 GNUNET_NAMESTORE_record_hash (struct GNUNET_NAMESTORE_Handle *h,
319                               const GNUNET_HashCode *zone,
320                               const char *name,
321                               uint32_t record_type,
322                               struct GNUNET_TIME_Absolute expiration,
323                               enum GNUNET_NAMESTORE_RecordFlags flags,
324                               size_t data_size,
325                               const void *data,
326                               GNUNET_HashCode *record_hash)
327 {
328   char* teststring = "namestore-stub";
329   GNUNET_CRYPTO_hash(teststring, strlen(teststring), record_hash);
330 }
331
332
333 /**
334  * Get all records of a zone.
335  *
336  * @param h handle to the namestore
337  * @param zone zone to access
338  * @param proc function to call on a random value; it
339  *        will be called repeatedly with a value (if available)
340  *        and always once at the end with a zone and name of NULL.
341  * @param proc_cls closure for proc
342  * @return a handle that can be used to
343  *         cancel
344  */
345 struct GNUNET_NAMESTORE_QueueEntry *
346 GNUNET_NAMESTORE_zone_transfer (struct GNUNET_NAMESTORE_Handle *h,
347                                 const GNUNET_HashCode *zone,
348                                 GNUNET_NAMESTORE_RecordProcessor proc,
349                                 void *proc_cls)
350 {
351   struct GNUNET_NAMESTORE_QueueEntry *qe;
352   qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
353   return qe;
354 }
355
356 struct GNUNET_NAMESTORE_ZoneIterator *
357 GNUNET_NAMESTORE_zone_iteration_start(struct GNUNET_NAMESTORE_Handle *h,
358                                       const GNUNET_HashCode *zone,
359                                       GNUNET_NAMESTORE_RecordProcessor proc,
360                                       void *proc_cls);
361
362 int
363 GNUNET_NAMESTORE_zone_iterator_next(struct GNUNET_NAMESTORE_ZoneIterator *it);
364
365 void
366 GNUNET_NAMESTORE_zone_iteration_stop(struct GNUNET_NAMESTORE_ZoneIterator *it);
367
368 void
369 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe);
370
371
372 /**
373  * Cancel a namestore operation.  The final callback from the
374  * operation must not have been done yet.
375  *
376  * @param qe operation to cancel
377  */
378 void
379 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
380 {
381   if (qe)
382     GNUNET_free(qe);
383 }
384
385
386
387
388 /* end of namestore_stub_api.c */