aboutsummaryrefslogtreecommitdiff
path: root/src/datastore/datastore_api.c
blob: 72f7faed76c37ac944a097b2310eb25db0202686 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
/*
     This file is part of GNUnet
     (C) 2004, 2005, 2006, 2007, 2009, 2010 Christian Grothoff (and other contributing authors)

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 2, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/

/**
 * @file datastore/datastore_api.c
 * @brief Management for the datastore for files stored on a GNUnet node
 * @author Christian Grothoff
 */
#include "platform.h"
#include "gnunet_arm_service.h"
#include "gnunet_datastore_service.h"
#include "datastore.h"

/**
 * Entry in our priority queue.
 */
struct QueueEntry
{

  /**
   * This is a linked list.
   */
  struct QueueEntry *next;

  /**
   * This is a linked list.
   */
  struct QueueEntry *prev;

  /**
   * Handle to the master context.
   */
  struct GNUNET_DATASTORE_Handle *h;

  /**
   * Task for timeout signalling.
   */
  GNUNET_SCHEDULER_TaskIdentifier task;

  /**
   * Timeout for the current operation.
   */
  struct GNUNET_TIME_Absolute timeout;

  /**
   * Priority in the queue.
   */
  unsigned int priority;

  /**
   * Maximum allowed length of queue (otherwise
   * this request should be discarded).
   */
  unsigned int max_queue;

  /**
   * Number of bytes in the request message following
   * this struct.
   */
  uint16_t message_size;

  /**
   * Has this message been transmitted to the service?
   * Only ever GNUNET_YES for the head of the queue.
   */
  int16_t was_transmitted;

  /**
   * Response processor (NULL if we are not waiting for a response).
   * This struct should be used for the closure, function-specific
   * arguments can be passed via 'client_ctx'.
   */
  GNUNET_CLIENT_MessageHandler response_proc;
  
  /**
   * Specific context (variable argument that
   * can be used by the response processor).
   */
  void *client_ctx;

};

/**
 * Handle to the datastore service.  Followed
 * by 65536 bytes used for storing messages.
 */
struct GNUNET_DATASTORE_Handle
{

  /**
   * Our configuration.
   */
  const struct GNUNET_CONFIGURATION_Handle *cfg;

  /**
   * Our scheduler.
   */
  struct GNUNET_SCHEDULER_Handle *sched;

  /**
   * Current connection to the datastore service.
   */
  struct GNUNET_CLIENT_Connection *client;

  /**
   * Current head of priority queue.
   */
  struct QueueEntry *queue_head;

  /**
   * Current tail of priority queue.
   */
  struct QueueEntry *queue_tail;

  /**
   * Number of entries in the queue.
   */
  unsigned int queue_size;

};



/**
 * Connect to the datastore service.
 *
 * @param cfg configuration to use
 * @param sched scheduler to use
 * @return handle to use to access the service
 */
struct GNUNET_DATASTORE_Handle *
GNUNET_DATASTORE_connect (const struct
			  GNUNET_CONFIGURATION_Handle
			  *cfg,
			  struct
			  GNUNET_SCHEDULER_Handle
			  *sched)
{
  struct GNUNET_CLIENT_Connection *c;
  struct GNUNET_DATASTORE_Handle *h;
  
  c = GNUNET_CLIENT_connect (sched, "datastore", cfg);
  if (c == NULL)
    return NULL; /* oops */
  h = GNUNET_malloc (sizeof(struct GNUNET_DATASTORE_Handle) + 
		     GNUNET_SERVER_MAX_MESSAGE_SIZE);
  h->client = c;
  h->cfg = cfg;
  h->sched = sched;
  return h;
}


/**
 * Transmit DROP message to datastore service.
 *
 * @param cls the 'struct GNUNET_DATASTORE_Handle'
 * @param size number of bytes that can be copied to buf
 * @param buf where to copy the drop message
 * @return number of bytes written to buf
 */
static size_t
transmit_drop (void *cls,
	       size_t size, 
	       void *buf)
{
  struct GNUNET_DATASTORE_Handle *h = cls;
  struct GNUNET_MessageHeader *hdr;
  
  if (buf == NULL)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
		  _("Failed to transmit request to drop database.\n"));
      GNUNET_DATASTORE_disconnect (h, GNUNET_NO);
      return 0;
    }
  GNUNET_assert (size >= sizeof(struct GNUNET_MessageHeader));
  hdr = buf;
  hdr->size = htons(sizeof(struct GNUNET_MessageHeader));
  hdr->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DROP);
  GNUNET_DATASTORE_disconnect (h, GNUNET_NO);
  return sizeof(struct GNUNET_MessageHeader);
}


/**
 * Disconnect from the datastore service (and free
 * associated resources).
 *
 * @param h handle to the datastore
 * @param drop set to GNUNET_YES to delete all data in datastore (!)
 */
void GNUNET_DATASTORE_disconnect (struct GNUNET_DATASTORE_Handle *h,
				  int drop)
{
  struct QueueEntry *qe;

  if (h->client != NULL)
    GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
  h->client = NULL;
  while (NULL != (qe = h->queue_head))
    {
      GNUNET_CONTAINER_DLL_remove (h->queue_head,
				   h->queue_tail,
				   qe);
      if (NULL != qe->response_proc)
	qe->response_proc (qe, NULL);
      GNUNET_free (qe);
    }
  if (GNUNET_YES == drop) 
    {
      h->client = GNUNET_CLIENT_connect (h->sched, "datastore", h->cfg);
      if (h->client != NULL)
	{
	  if (NULL != 
	      GNUNET_CLIENT_notify_transmit_ready (h->client,
						   sizeof(struct GNUNET_MessageHeader),
						   GNUNET_TIME_UNIT_MINUTES,
						   GNUNET_YES,
						   &transmit_drop,
						   h))
	    return;
	  GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
	}
      GNUNET_break (0);
    }
  GNUNET_free (h);
}


#if 0
/**
 * Type of a function to call when we receive a message
 * from the service.  This specific function is used
 * to handle messages of type "struct StatusMessage".
 *
 * @param cls closure
 * @param msg message received, NULL on timeout or fatal error
 */
static void 
with_status_response_handler (void *cls,
			      const struct
			      GNUNET_MessageHeader * msg)
{
  struct GNUNET_DATASTORE_Handle *h = cls;
  GNUNET_DATASTORE_ContinuationWithStatus cont = h->response_proc;
  const struct StatusMessage *sm;
  const char *emsg;
  int status;

  h->message_size = 0;
  if (msg == NULL)
    {
      h->response_proc = NULL;
      GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
      h->client = GNUNET_CLIENT_connect (h->sched, "datastore", h->cfg);
      cont (h->response_proc_cls, 
	    GNUNET_SYSERR,
	    _("Timeout trying to read response from datastore service"));
      return;
    }
  if ( (ntohs(msg->size) < sizeof(struct StatusMessage)) ||
       (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_STATUS) ) 
    {
      GNUNET_break (0);
      h->response_proc = NULL;
      GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
      h->client = GNUNET_CLIENT_connect (h->sched, "datastore", h->cfg);
      cont (h->response_proc_cls, 
	    GNUNET_SYSERR,
	    _("Error reading response from datastore service"));
      return;
    }
  sm = (const struct StatusMessage*) msg;
  status = ntohl(sm->status);
  emsg = NULL;
  if (ntohs(msg->size) > sizeof(struct StatusMessage))
    {
      emsg = (const char*) &sm[1];
      if (emsg[ntohs(msg->size) - sizeof(struct StatusMessage) - 1] != '\0')
	{
	  GNUNET_break (0);
	  emsg = _("Invalid error message received from datastore service");
	}
    }  
  if ( (status == GNUNET_SYSERR) &&
       (emsg == NULL) )
    {
      GNUNET_break (0);
      emsg = _("Invalid error message received from datastore service");
    }
  h->response_proc = NULL;
#if DEBUG_DATASTORE
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
	      "Received status %d/%s\n",
	      status,
	      emsg);
#endif
  cont (h->response_proc_cls, 
	status,
	emsg);
}


/**
 * Helper function that will initiate the
 * transmission of a message to the datastore
 * service.  The message must already be prepared
 * and stored in the buffer at the end of the
 * handle.  The message must be of a type that
 * expects a "StatusMessage" in response.
 *
 * @param h handle to the service with prepared message
 * @param cont function to call with result
 * @param cont_cls closure
 * @param timeout timeout for the operation
 */
static void
transmit_for_status (struct GNUNET_DATASTORE_Handle *h,
		     GNUNET_DATASTORE_ContinuationWithStatus cont,
		     void *cont_cls,
		     struct GNUNET_TIME_Relative timeout)
{
  const struct GNUNET_MessageHeader *hdr;
  uint16_t msize;

  GNUNET_assert (cont != NULL);
  hdr = (const struct GNUNET_MessageHeader*) &h[1];
  msize = ntohs(hdr->size);
#if DEBUG_DATASTORE
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
	      "Transmitting %u byte message of type %u to datastore service\n",
	      msize,
	      ntohs(hdr->type));
#endif
  GNUNET_assert (h->response_proc == NULL);
  h->response_proc = cont;
  h->response_proc_cls = cont_cls;
  h->timeout = GNUNET_TIME_relative_to_absolute (timeout);
  h->message_size = msize;
  if (GNUNET_OK !=
      GNUNET_CLIENT_transmit_and_get_response (h->client,
					       hdr,					       
					       timeout,
					       GNUNET_YES,
					       &with_status_response_handler,					       
					       h))
    {
      GNUNET_break (0);
      h->response_proc = NULL;
      h->message_size = 0;
      cont (cont_cls,
	    GNUNET_SYSERR,
	    _("Not ready to transmit request to datastore service"));
    }
}


/**
 * Store an item in the datastore.  If the item is already present,
 * the priorities are summed up and the higher expiration time and
 * lower anonymity level is used.
 *
 * @param h handle to the datastore
 * @param rid reservation ID to use (from "reserve"); use 0 if no
 *            prior reservation was made
 * @param key key for the value
 * @param size number of bytes in data
 * @param data content stored
 * @param type type of the content
 * @param priority priority of the content
 * @param anonymity anonymity-level for the content
 * @param expiration expiration time for the content
 * @param timeout timeout for the operation
 * @param cont continuation to call when done
 * @param cont_cls closure for cont
 */
void
GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
		      int rid,
                      const GNUNET_HashCode * key,
                      uint32_t size,
                      const void *data,
                      enum GNUNET_BLOCK_Type type,
                      uint32_t priority,
                      uint32_t anonymity,
                      struct GNUNET_TIME_Absolute expiration,
                      struct GNUNET_TIME_Relative timeout,
		      GNUNET_DATASTORE_ContinuationWithStatus cont,
		      void *cont_cls)
{
  struct DataMessage *dm;
  size_t msize;

#if DEBUG_DATASTORE
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
	      "Asked to put %u bytes of data under key `%s'\n",
	      size,
	      GNUNET_h2s (key));
#endif
  msize = sizeof(struct DataMessage) + size;
  GNUNET_assert (msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
  dm = (struct DataMessage*) &h[1];
  dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_PUT);
  dm->header.size = htons(msize);
  dm->rid = htonl(rid);
  dm->size = htonl(size);
  dm->type = htonl(type);
  dm->priority = htonl(priority);
  dm->anonymity = htonl(anonymity);
  dm->uid = GNUNET_htonll(0);
  dm->expiration = GNUNET_TIME_absolute_hton(expiration);
  dm->key = *key;
  memcpy (&dm[1], data, size);
  transmit_for_status (h, cont, cont_cls, timeout);
}


/**
 * Reserve space in the datastore.  This function should be used
 * to avoid "out of space" failures during a longer sequence of "put"
 * operations (for example, when a file is being inserted).
 *
 * @param h handle to the datastore
 * @param amount how much space (in bytes) should be reserved (for content only)
 * @param entries how many entries will be created (to calculate per-entry overhead)
 * @param cont continuation to call when done; "success" will be set to
 *             a positive reservation value if space could be reserved.
 * @param cont_cls closure for cont
 * @param timeout how long to wait at most for a response
 */
void
GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h,
			  uint64_t amount,
			  uint32_t entries,
			  GNUNET_DATASTORE_ContinuationWithStatus cont,
			  void *cont_cls,
			  struct GNUNET_TIME_Relative timeout)
{
  struct ReserveMessage *rm;

  rm = (struct ReserveMessage*) &h[1];
  rm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE);
  rm->header.size = htons(sizeof (struct ReserveMessage));
  rm->entries = htonl(entries);
  rm->amount = GNUNET_htonll(amount);
  transmit_for_status (h, cont, cont_cls, timeout);
}


/**
 * Signal that all of the data for which a reservation was made has
 * been stored and that whatever excess space might have been reserved
 * can now be released.
 *
 * @param h handle to the datastore
 * @param rid reservation ID (value of "success" in original continuation
 *        from the "reserve" function).
 * @param cont continuation to call when done
 * @param cont_cls closure for cont
 * @param timeout how long to wait at most for a response
 */
void
GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
				  int rid,
				  GNUNET_DATASTORE_ContinuationWithStatus cont,
				  void *cont_cls,
				  struct GNUNET_TIME_Relative timeout)
{
  struct ReleaseReserveMessage *rrm;

  rrm = (struct ReleaseReserveMessage*) &h[1];
  rrm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE);
  rrm->header.size = htons(sizeof (struct ReleaseReserveMessage));
  rrm->rid = htonl(rid);
  transmit_for_status (h, cont, cont_cls, timeout);
}


/**
 * Update a value in the datastore.
 *
 * @param h handle to the datastore
 * @param uid identifier for the value
 * @param priority how much to increase the priority of the value
 * @param expiration new expiration value should be MAX of existing and this argument
 * @param cont continuation to call when done
 * @param cont_cls closure for cont
 * @param timeout how long to wait at most for a response
 */
void
GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
			 unsigned long long uid,
			 uint32_t priority,
			 struct GNUNET_TIME_Absolute expiration,
			 GNUNET_DATASTORE_ContinuationWithStatus cont,
			 void *cont_cls,
			 struct GNUNET_TIME_Relative timeout)
{
  struct UpdateMessage *um;

  um = (struct UpdateMessage*) &h[1];
  um->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE);
  um->header.size = htons(sizeof (struct UpdateMessage));
  um->priority = htonl(priority);
  um->expiration = GNUNET_TIME_absolute_hton(expiration);
  um->uid = GNUNET_htonll(uid);
  transmit_for_status (h, cont, cont_cls, timeout);
}


/**
 * Helper function that will initiate the transmission of a message to
 * the datastore service.  The message must already be prepared and
 * stored in the buffer at the end of the handle.  The message must be
 * of a type that expects a "DataMessage" in response.
 *
 * @param h handle to the service with prepared message
 * @param cont function to call with result
 * @param cont_cls closure
 * @param timeout timeout for the operation
 */
static void
transmit_for_result (struct GNUNET_DATASTORE_Handle *h,
		     GNUNET_DATASTORE_Iterator cont,
		     void *cont_cls,
		     struct GNUNET_TIME_Relative timeout);


/**
 * Type of a function to call when we receive a message
 * from the service.  This specific function is used
 * to handle messages of type "struct DataMessage".
 *
 * @param cls closure
 * @param msg message received, NULL on timeout or fatal error
 */
static void 
with_result_response_handler (void *cls,
			      const struct
			      GNUNET_MessageHeader * msg)
{
  struct GNUNET_DATASTORE_Handle *h = cls;
  GNUNET_DATASTORE_Iterator cont = h->response_proc;
  const struct DataMessage *dm;
  size_t msize;
  struct GNUNET_TIME_Relative remaining;

  if (msg == NULL)
    {
#if DEBUG_DATASTORE
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
		  "Got disconnected from datastore\n");
#endif
      h->response_proc = NULL;
      GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
      h->client = GNUNET_CLIENT_connect (h->sched, "datastore", h->cfg);
      remaining = GNUNET_TIME_absolute_get_remaining (h->timeout);
      if (remaining.value > 0)
	{
	  transmit_for_result (h,
			       cont,
			       h->response_proc_cls,
			       remaining);
	}
      else
	{
	  h->message_size = 0;
	  cont (h->response_proc_cls, 
		NULL, 0, NULL, 0, 0, 0, 
		GNUNET_TIME_UNIT_ZERO_ABS, 0);
	}
      return;
    }
  h->message_size = 0;
  if (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END) 
    {
      GNUNET_break (ntohs(msg->size) == sizeof(struct GNUNET_MessageHeader));
      h->response_proc = NULL;
#if DEBUG_DATASTORE
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
		  "Received end of result set\n");
#endif
      cont (h->response_proc_cls, 
	    NULL, 0, NULL, 0, 0, 0, 
	    GNUNET_TIME_UNIT_ZERO_ABS, 0);
      return;
    }
  if ( (ntohs(msg->size) < sizeof(struct DataMessage)) ||
       (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_DATA) ) 
    {
      GNUNET_break (0);
      GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
      h->client = GNUNET_CLIENT_connect (h->sched, "datastore", h->cfg);
      h->response_proc = NULL;
      cont (h->response_proc_cls, 
	    NULL, 0, NULL, 0, 0, 0, 
	    GNUNET_TIME_UNIT_ZERO_ABS, 0);
      return;
    }
  dm = (const struct DataMessage*) msg;
  msize = ntohl(dm->size);
  if (ntohs(msg->size) != msize + sizeof(struct DataMessage))
    {
      GNUNET_break (0);
      GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
      h->client = GNUNET_CLIENT_connect (h->sched, "datastore", h->cfg);
      h->response_proc = NULL;
      cont (h->response_proc_cls, 
	    NULL, 0, NULL, 0, 0, 0, 
	    GNUNET_TIME_UNIT_ZERO_ABS, 0);
      return;
    }
#if DEBUG_DATASTORE
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
	      "Received result %llu with type %u and size %u with key %s\n",
	      (unsigned long long) GNUNET_ntohll(dm->uid),
	      ntohl(dm->type),
	      msize,
	      GNUNET_h2s(&dm->key));
#endif
  cont (h->response_proc_cls, 
	&dm->key,
	msize,
	&dm[1],
	ntohl(dm->type),
	ntohl(dm->priority),
	ntohl(dm->anonymity),
	GNUNET_TIME_absolute_ntoh(dm->expiration),	
	GNUNET_ntohll(dm->uid));
}


/**
 * Function called to trigger obtaining the next result
 * from the datastore.
 * 
 * @param h handle to the datastore
 * @param more GNUNET_YES to get moxre results, GNUNET_NO to abort
 *        iteration (with a final call to "iter" with key/data == NULL).
 */
void 
GNUNET_DATASTORE_get_next (struct GNUNET_DATASTORE_Handle *h,
			   int more)
{
  GNUNET_DATASTORE_Iterator cont;

  if (GNUNET_YES == more)
    {
      GNUNET_CLIENT_receive (h->client,
			     &with_result_response_handler,
			     h,
			     GNUNET_TIME_absolute_get_remaining (h->timeout));
      return;
    }
  cont = h->response_proc;
  h->response_proc = NULL;
  GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
  h->client = GNUNET_CLIENT_connect (h->sched, "datastore", h->cfg);
  cont (h->response_proc_cls, 
	NULL, 0, NULL, 0, 0, 0, 
	GNUNET_TIME_UNIT_ZERO_ABS, 0);
}


/**
 * Helper function that will initiate the transmission of a message to
 * the datastore service.  The message must already be prepared and
 * stored in the buffer at the end of the handle.  The message must be
 * of a type that expects a "DataMessage" in response.
 *
 * @param h handle to the service with prepared message
 * @param cont function to call with result
 * @param cont_cls closure
 * @param timeout timeout for the operation
 */
static void
transmit_for_result (struct GNUNET_DATASTORE_Handle *h,
		     GNUNET_DATASTORE_Iterator cont,
		     void *cont_cls,
		     struct GNUNET_TIME_Relative timeout)
{
  const struct GNUNET_MessageHeader *hdr;
  uint16_t msize;

  GNUNET_assert (cont != NULL);
  hdr = (const struct GNUNET_MessageHeader*) &h[1];
  msize = ntohs(hdr->size);
#if DEBUG_DATASTORE
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
	      "Transmitting %u byte message of type %u to datastore service\n",
	      msize,
	      ntohs(hdr->type));
#endif
  GNUNET_assert (h->response_proc == NULL);
  h->response_proc = cont;
  h->response_proc_cls = cont_cls;
  h->timeout = GNUNET_TIME_relative_to_absolute (timeout);
  h->message_size = msize;
  if (GNUNET_OK !=
      GNUNET_CLIENT_transmit_and_get_response (h->client,
					       hdr,
					       timeout,
					       GNUNET_YES,
					       &with_result_response_handler,
					       h))
    {
      GNUNET_break (0);
      h->response_proc = NULL;
      h->message_size = 0;
      cont (h->response_proc_cls, 
	    NULL, 0, NULL, 0, 0, 0, 
	    GNUNET_TIME_UNIT_ZERO_ABS, 0);
    }
}


/**
 * Iterate over the results for a particular key
 * in the datastore.
 *
 * @param h handle to the datastore
 * @param key maybe NULL (to match all entries)
 * @param type desired type, 0 for any
 * @param iter function to call on each matching value;
 *        will be called once with a NULL value at the end
 * @param iter_cls closure for iter
 * @param timeout how long to wait at most for a response
 */
void
GNUNET_DATASTORE_get (struct GNUNET_DATASTORE_Handle *h,
                      const GNUNET_HashCode * key,
                      enum GNUNET_BLOCK_Type type,
                      GNUNET_DATASTORE_Iterator iter, void *iter_cls,
		      struct GNUNET_TIME_Relative timeout)
{
  struct GetMessage *gm;

#if DEBUG_DATASTORE
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
	      "Asked to look for data under key `%s'\n",
	      GNUNET_h2s (key));
#endif
  gm = (struct GetMessage*) &h[1];
  gm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET);
  gm->type = htonl(type);
  if (key != NULL)
    {
      gm->header.size = htons(sizeof (struct GetMessage));
      gm->key = *key;
    }
  else
    {
      gm->header.size = htons(sizeof (struct GetMessage) - sizeof(GNUNET_HashCode));
    }
  GNUNET_assert (h->response_proc == NULL);
  transmit_for_result (h, iter, iter_cls, timeout);
}


/**
 * Get a random value from the datastore.
 *
 * @param h handle to the datastore
 * @param iter function to call on a random value; it
 *        will be called exactly once; if no values
 *        are available, the value will be NULL.
 * @param iter_cls closure for iter
 * @param timeout how long to wait at most for a response
 */
void
GNUNET_DATASTORE_get_random (struct GNUNET_DATASTORE_Handle *h,
                             GNUNET_DATASTORE_Iterator iter, void *iter_cls,
			     struct GNUNET_TIME_Relative timeout)
{
  struct GNUNET_MessageHeader *m;

  m = (struct GNUNET_MessageHeader*) &h[1];
  m->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET_RANDOM);
  m->size = htons(sizeof (struct GNUNET_MessageHeader));
  GNUNET_assert (h->response_proc == NULL);
  transmit_for_result (h, iter, iter_cls, timeout);
}


/**
 * Explicitly remove some content from the database.
 *
 * @param h handle to the datastore
 * @param key key for the value
 * @param size number of bytes in data
 * @param data content stored
 * @param cont continuation to call when done
 * @param cont_cls closure for cont
 * @param timeout how long to wait at most for a response
 */
void
GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
                         const GNUNET_HashCode * key,
                         uint32_t size, const void *data,
			 GNUNET_DATASTORE_ContinuationWithStatus cont,
			 void *cont_cls,
			 struct GNUNET_TIME_Relative timeout)
{
  struct DataMessage *dm;
  size_t msize;

#if DEBUG_DATASTORE
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
	      "Asked to remove %u bytes of data under key `%s'\n",
	      size,
	      GNUNET_h2s (key));
#endif
  msize = sizeof(struct DataMessage) + size;
  GNUNET_assert (msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
  dm = (struct DataMessage*) &h[1];
  dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE);
  dm->header.size = htons(msize);
  dm->rid = htonl(0);
  dm->size = htonl(size);
  dm->type = htonl(0);
  dm->priority = htonl(0);
  dm->anonymity = htonl(0);
  dm->uid = GNUNET_htonll(0);
  dm->expiration = GNUNET_TIME_absolute_hton(GNUNET_TIME_UNIT_ZERO_ABS);
  dm->key = *key;
  memcpy (&dm[1], data, size);
  transmit_for_status (h, cont, cont_cls, timeout);
}
#endif

/* end of datastore_api.c */