psyc,psycstore,social fixes
[oweals/gnunet.git] / src / psycstore / psycstore_api.c
1 /*
2  * This file is part of GNUnet
3  * Copyright (C) 2013 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 /**
22  * @file psycstore/psycstore_api.c
23  * @brief API to interact with the PSYCstore service
24  * @author Gabor X Toth
25  * @author Christian Grothoff
26  */
27
28 #include <inttypes.h>
29
30 #include "platform.h"
31 #include "gnunet_util_lib.h"
32 #include "gnunet_constants.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_psycstore_service.h"
35 #include "gnunet_multicast_service.h"
36 #include "psycstore.h"
37
38 #define LOG(kind,...) GNUNET_log_from (kind, "psycstore-api",__VA_ARGS__)
39
40 /**
41  * Handle for an operation with the PSYCstore service.
42  */
43 struct GNUNET_PSYCSTORE_OperationHandle
44 {
45
46   /**
47    * Main PSYCstore handle.
48    */
49   struct GNUNET_PSYCSTORE_Handle *h;
50
51   /**
52    * Data callbacks.
53    */
54   union {
55     GNUNET_PSYCSTORE_FragmentCallback fragment_cb;
56     GNUNET_PSYCSTORE_CountersCallback counters_cb;
57     GNUNET_PSYCSTORE_StateCallback state_cb;
58   };
59
60   /**
61    * Closure for callbacks.
62    */
63   void *cls;
64
65   /**
66    * Message envelope.
67    */
68   struct GNUNET_MQ_Envelope *env;
69
70   /**
71    * Operation ID.
72    */
73   uint64_t op_id;
74 };
75
76
77 /**
78  * Handle for the service.
79  */
80 struct GNUNET_PSYCSTORE_Handle
81 {
82   /**
83    * Configuration to use.
84    */
85   const struct GNUNET_CONFIGURATION_Handle *cfg;
86
87   /**
88    * Client connection.
89    */
90   struct GNUNET_MQ_Handle *mq;
91
92   /**
93    * Async operations.
94    */
95   struct GNUNET_OP_Handle *op;
96
97   /**
98    * Task doing exponential back-off trying to reconnect.
99    */
100   struct GNUNET_SCHEDULER_Task *reconnect_task;
101
102   /**
103    * Delay for next connect retry.
104    */
105   struct GNUNET_TIME_Relative reconnect_delay;
106
107
108   GNUNET_PSYCSTORE_FragmentCallback *fragment_cb;
109
110   GNUNET_PSYCSTORE_CountersCallback *counters_cb;
111
112   GNUNET_PSYCSTORE_StateCallback *state_cb;
113   /**
114    * Closure for callbacks.
115    */
116   void *cb_cls;
117 };
118
119
120 static int
121 check_result_code (void *cls, const struct OperationResult *opres)
122 {
123   uint16_t size = ntohs (opres->header.size);
124   const char *str = (const char *) &opres[1];
125   if ( (sizeof (*opres) < size) &&
126        ('\0' != str[size - sizeof (*opres) - 1]) )
127   {
128     GNUNET_break (0);
129     return GNUNET_SYSERR;
130   }
131
132   return GNUNET_OK;
133 }
134
135
136 static void
137 handle_result_code (void *cls, const struct OperationResult *opres)
138 {
139   struct GNUNET_PSYCSTORE_Handle *h = cls;
140   struct GNUNET_PSYCSTORE_OperationHandle *op = NULL;
141   uint16_t size = ntohs (opres->header.size);
142
143   const char *
144     str = (sizeof (*opres) < size) ? (const char *) &opres[1] : "";
145
146   if (GNUNET_YES == GNUNET_OP_result (h->op, GNUNET_ntohll (opres->op_id),
147                                       GNUNET_ntohll (opres->result_code) + INT64_MIN,
148                                       str, size - sizeof (*opres), (void **) &op))
149   {
150     LOG (GNUNET_ERROR_TYPE_DEBUG,
151          "handle_result_code: Received result message with operation ID: %" PRIu64 "\n",
152          GNUNET_ntohll (opres->op_id));
153     GNUNET_free (op);
154   }
155   else
156   {
157     LOG (GNUNET_ERROR_TYPE_DEBUG,
158          "handle_result_code: No callback registered for operation with ID %" PRIu64 ".\n",
159          GNUNET_ntohll (opres->op_id));
160   }
161   h->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS;
162 }
163
164
165 static void
166 handle_result_counters (void *cls, const struct CountersResult *cres)
167 {
168   struct GNUNET_PSYCSTORE_Handle *h = cls;
169   struct GNUNET_PSYCSTORE_OperationHandle *op = NULL;
170
171   if (GNUNET_YES == GNUNET_OP_get (h->op, GNUNET_ntohll (cres->op_id),
172                                    NULL, NULL, (void **) &op))
173   {
174     GNUNET_assert (NULL != op);
175     if (NULL != op->counters_cb)
176     {
177       op->counters_cb (op->cls,
178                        ntohl (cres->result_code),
179                        GNUNET_ntohll (cres->max_fragment_id),
180                        GNUNET_ntohll (cres->max_message_id),
181                        GNUNET_ntohll (cres->max_group_generation),
182                        GNUNET_ntohll (cres->max_state_message_id));
183     }
184     GNUNET_OP_remove (h->op, GNUNET_ntohll (cres->op_id));
185     GNUNET_free (op);
186   }
187   else
188   {
189     LOG (GNUNET_ERROR_TYPE_DEBUG,
190          "handle_result_counters: No callback registered for operation with ID %" PRIu64 ".\n",
191          GNUNET_ntohll (cres->op_id));
192   }
193   h->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS;
194 }
195
196
197 static int
198 check_result_fragment (void *cls, const struct FragmentResult *fres)
199 {
200   uint16_t size = ntohs (fres->header.size);
201   struct GNUNET_MULTICAST_MessageHeader *mmsg =
202     (struct GNUNET_MULTICAST_MessageHeader *) &fres[1];
203   if (sizeof (*fres) + sizeof (*mmsg) < size
204       && sizeof (*fres) + ntohs (mmsg->header.size) != size)
205   {
206     LOG (GNUNET_ERROR_TYPE_ERROR,
207          "check_result_fragment: Received message with invalid length %lu bytes.\n",
208          size, sizeof (*fres));
209     GNUNET_break (0);
210     return GNUNET_SYSERR;
211   }
212   return GNUNET_OK;
213 }
214
215
216 static void
217 handle_result_fragment (void *cls, const struct FragmentResult *fres)
218 {
219   struct GNUNET_PSYCSTORE_Handle *h = cls;
220   struct GNUNET_PSYCSTORE_OperationHandle *op = NULL;
221
222   if (GNUNET_YES == GNUNET_OP_get (h->op, GNUNET_ntohll (fres->op_id),
223                                    NULL, NULL, (void **) &op))
224   {
225     GNUNET_assert (NULL != op);
226     if (NULL != op->fragment_cb)
227       op->fragment_cb (op->cls,
228                        (struct GNUNET_MULTICAST_MessageHeader *) &fres[1],
229                        ntohl (fres->psycstore_flags));
230     //GNUNET_OP_remove (h->op, GNUNET_ntohll (fres->op_id));
231     //GNUNET_free (op);
232   }
233   else
234   {
235     LOG (GNUNET_ERROR_TYPE_DEBUG,
236          "handle_result_fragment: No callback registered for operation with ID %" PRIu64 ".\n",
237          GNUNET_ntohll (fres->op_id));
238   }
239   h->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS;
240 }
241
242
243 static int
244 check_result_state (void *cls, const struct StateResult *sres)
245 {
246   const char *name = (const char *) &sres[1];
247   uint16_t size = ntohs (sres->header.size);
248   uint16_t name_size = ntohs (sres->name_size);
249
250   if (name_size <= 2
251       || size - sizeof (*sres) < name_size
252       || '\0' != name[name_size - 1])
253   {
254     LOG (GNUNET_ERROR_TYPE_ERROR,
255          "check_result_state: Received state result message with invalid name.\n");
256     GNUNET_break (0);
257     return GNUNET_SYSERR;
258   }
259   return GNUNET_OK;
260 }
261
262
263 static void
264 handle_result_state (void *cls, const struct StateResult *sres)
265 {
266   struct GNUNET_PSYCSTORE_Handle *h = cls;
267   struct GNUNET_PSYCSTORE_OperationHandle *op = NULL;
268
269   const char *name = (const char *) &sres[1];
270   uint16_t name_size = ntohs (sres->name_size);
271
272   if (GNUNET_YES == GNUNET_OP_get (h->op, GNUNET_ntohll (sres->op_id),
273                                    NULL, NULL, (void **) &op))
274   {
275     GNUNET_assert (NULL != op);
276     if (NULL != op->state_cb)
277        op->state_cb (op->cls, name, (char *) &sres[1] + name_size,
278                      ntohs (sres->header.size) - sizeof (*sres) - name_size);
279     //GNUNET_OP_remove (h->op, GNUNET_ntohll (sres->op_id));
280     //GNUNET_free (op);
281   }
282   else
283   {
284     LOG (GNUNET_ERROR_TYPE_DEBUG,
285          "handle_result_state: No callback registered for operation with ID %" PRIu64 ".\n",
286          GNUNET_ntohll (sres->op_id));
287   }
288   h->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS;
289 }
290
291
292 static void
293 reconnect (void *cls);
294
295
296 /**
297  * Client disconnected from service.
298  *
299  * Reconnect after backoff period.=
300  */
301 static void
302 disconnected (void *cls, enum GNUNET_MQ_Error error)
303 {
304   struct GNUNET_PSYCSTORE_Handle *h = cls;
305
306   LOG (GNUNET_ERROR_TYPE_DEBUG,
307        "Origin client disconnected (%d), re-connecting\n",
308        (int) error);
309   if (NULL != h->mq)
310   {
311     GNUNET_MQ_destroy (h->mq);
312     GNUNET_OP_destroy (h->op);
313     h->mq = NULL;
314     h->op = NULL;
315   }
316
317   h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
318                                                     &reconnect, h);
319   h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
320 }
321
322
323 static void
324 do_connect (struct GNUNET_PSYCSTORE_Handle *h)
325 {
326   LOG (GNUNET_ERROR_TYPE_DEBUG,
327        "Connecting to PSYCstore service.\n");
328
329   struct GNUNET_MQ_MessageHandler handlers[] = {
330     GNUNET_MQ_hd_var_size (result_code,
331                            GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_CODE,
332                            struct OperationResult,
333                            h),
334     GNUNET_MQ_hd_fixed_size (result_counters,
335                              GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_COUNTERS,
336                              struct CountersResult,
337                              h),
338     GNUNET_MQ_hd_var_size (result_fragment,
339                            GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_FRAGMENT,
340                            struct FragmentResult,
341                            h),
342     GNUNET_MQ_hd_var_size (result_state,
343                            GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_STATE,
344                            struct StateResult,
345                            h),
346     GNUNET_MQ_handler_end ()
347   };
348
349   h->op = GNUNET_OP_create ();
350   GNUNET_assert (NULL == h->mq);
351   h->mq = GNUNET_CLIENT_connecT (h->cfg, "psycstore",
352                                  handlers, disconnected, h);
353   GNUNET_assert (NULL != h->mq);
354 }
355
356
357 /**
358  * Try again to connect to the PSYCstore service.
359  *
360  * @param cls Handle to the PSYCstore service.
361  */
362 static void
363 reconnect (void *cls)
364 {
365   do_connect (cls);
366 }
367
368
369 /**
370  * Connect to the PSYCstore service.
371  *
372  * @param cfg The configuration to use
373  * @return Handle to use
374  */
375 struct GNUNET_PSYCSTORE_Handle *
376 GNUNET_PSYCSTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
377 {
378   struct GNUNET_PSYCSTORE_Handle *h
379     = GNUNET_new (struct GNUNET_PSYCSTORE_Handle);
380   h->cfg = cfg;
381   h->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS;
382   do_connect (h);
383   return h;
384 }
385
386
387 /**
388  * Disconnect from PSYCstore service
389  *
390  * @param h Handle to destroy
391  */
392 void
393 GNUNET_PSYCSTORE_disconnect (struct GNUNET_PSYCSTORE_Handle *h)
394 {
395   GNUNET_assert (NULL != h);
396   if (h->reconnect_task != NULL)
397   {
398     GNUNET_SCHEDULER_cancel (h->reconnect_task);
399     h->reconnect_task = NULL;
400   }
401   if (NULL != h->mq)
402   {
403     // FIXME: free data structures for pending operations
404     GNUNET_MQ_destroy (h->mq);
405     h->mq = NULL;
406   }
407   GNUNET_free (h);
408 }
409
410
411 /**
412  * Message sent notification.
413  *
414  * Remove invalidated envelope pointer.
415  */
416 static void
417 message_sent (void *cls)
418 {
419   struct GNUNET_PSYCSTORE_OperationHandle *op = cls;
420   op->env = NULL;
421 }
422
423
424 /**
425  * Create a new operation.
426  */
427 static struct GNUNET_PSYCSTORE_OperationHandle *
428 op_create (struct GNUNET_PSYCSTORE_Handle *h,
429            struct GNUNET_OP_Handle *hop,
430            GNUNET_PSYCSTORE_ResultCallback result_cb,
431            void *cls)
432 {
433   struct GNUNET_PSYCSTORE_OperationHandle *
434     op = GNUNET_malloc (sizeof (*op));
435   op->h = h;
436   op->op_id = GNUNET_OP_add (hop,
437                              (GNUNET_ResultCallback) result_cb,
438                              cls, op);
439   return op;
440 }
441
442
443 /**
444  * Send a message associated with an operation.
445  *
446  * @param h
447  *        PSYCstore handle.
448  * @param op
449  *        Operation handle.
450  * @param env
451  *        Message envelope to send.
452  * @param[out] op_id
453  *        Operation ID to write in network byte order. NULL if not needed.
454  *
455  * @return Operation handle.
456  *
457  */
458 static struct GNUNET_PSYCSTORE_OperationHandle *
459 op_send (struct GNUNET_PSYCSTORE_Handle *h,
460          struct GNUNET_PSYCSTORE_OperationHandle *op,
461          struct GNUNET_MQ_Envelope *env,
462          uint64_t *op_id)
463 {
464   op->env = env;
465   if (NULL != op_id)
466     *op_id = GNUNET_htonll (op->op_id);
467
468   GNUNET_MQ_notify_sent (env, message_sent, op);
469   GNUNET_MQ_send (h->mq, env);
470   return op;
471 }
472
473
474 /**
475  * Cancel a PSYCstore operation. Note that the operation MAY still
476  * be executed; this merely cancels the continuation; if the request
477  * was already transmitted, the service may still choose to complete
478  * the operation.
479  *
480  * @param op Operation to cancel.
481  *
482  * @return #GNUNET_YES if message was not sent yet and got discarded,
483  *         #GNUNET_NO  if it was already sent, and only the callbacks got cancelled.
484  */
485 int
486 GNUNET_PSYCSTORE_operation_cancel (struct GNUNET_PSYCSTORE_OperationHandle *op)
487 {
488   struct GNUNET_PSYCSTORE_Handle *h = op->h;
489   int ret = GNUNET_NO;
490
491   if (NULL != op->env)
492   {
493     GNUNET_MQ_send_cancel (op->env);
494     ret = GNUNET_YES;
495   }
496
497   GNUNET_OP_remove (h->op, op->op_id);
498   GNUNET_free (op);
499
500   return ret;
501 }
502
503
504 /**
505  * Store join/leave events for a PSYC channel in order to be able to answer
506  * membership test queries later.
507  *
508  * @param h
509  *        Handle for the PSYCstore.
510  * @param channel_key
511  *        The channel where the event happened.
512  * @param slave_key
513  *        Public key of joining/leaving slave.
514  * @param did_join
515  *        #GNUNET_YES on join, #GNUNET_NO on part.
516  * @param announced_at
517  *        ID of the message that announced the membership change.
518  * @param effective_since
519  *        Message ID this membership change is in effect since.
520  *        For joins it is <= announced_at, for parts it is always 0.
521  * @param group_generation
522  *        In case of a part, the last group generation the slave has access to.
523  *        It has relevance when a larger message have fragments with different
524  *        group generations.
525  * @param result_cb
526  *        Callback to call with the result of the storage operation.
527  * @param cls
528  *        Closure for the callback.
529  *
530  * @return Operation handle that can be used to cancel the operation.
531  */
532 struct GNUNET_PSYCSTORE_OperationHandle *
533 GNUNET_PSYCSTORE_membership_store (struct GNUNET_PSYCSTORE_Handle *h,
534                                    const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
535                                    const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
536                                    int did_join,
537                                    uint64_t announced_at,
538                                    uint64_t effective_since,
539                                    uint64_t group_generation,
540                                    GNUNET_PSYCSTORE_ResultCallback result_cb,
541                                    void *cls)
542 {
543   GNUNET_assert (NULL != h);
544   GNUNET_assert (NULL != channel_key);
545   GNUNET_assert (NULL != slave_key);
546   GNUNET_assert (GNUNET_YES == did_join || GNUNET_NO == did_join);
547   GNUNET_assert (did_join
548                  ? effective_since <= announced_at
549                  : effective_since == 0);
550
551   struct MembershipStoreRequest *req;
552   struct GNUNET_MQ_Envelope *
553     env = GNUNET_MQ_msg (req, GNUNET_MESSAGE_TYPE_PSYCSTORE_MEMBERSHIP_STORE);
554   req->channel_key = *channel_key;
555   req->slave_key = *slave_key;
556   req->did_join = did_join;
557   req->announced_at = GNUNET_htonll (announced_at);
558   req->effective_since = GNUNET_htonll (effective_since);
559   req->group_generation = GNUNET_htonll (group_generation);
560
561   return
562     op_send (h, op_create (h, h->op, result_cb, cls),
563              env, &req->op_id);
564 }
565
566
567 /**
568  * Test if a member was admitted to the channel at the given message ID.
569  *
570  * This is useful when relaying and replaying messages to check if a particular
571  * slave has access to the message fragment with a given group generation.  It
572  * is also used when handling join requests to determine whether the slave is
573  * currently admitted to the channel.
574  *
575  * @param h
576  *        Handle for the PSYCstore.
577  * @param channel_key
578  *        The channel we are interested in.
579  * @param slave_key
580  *        Public key of slave whose membership to check.
581  * @param message_id
582  *        Message ID for which to do the membership test.
583  * @param group_generation
584  *        Group generation of the fragment of the message to test.
585  *        It has relevance if the message consists of multiple fragments with
586  *        different group generations.
587  * @param result_cb
588  *        Callback to call with the test result.
589  * @param cls
590  *        Closure for the callback.
591  *
592  * @return Operation handle that can be used to cancel the operation.
593  */
594 struct GNUNET_PSYCSTORE_OperationHandle *
595 GNUNET_PSYCSTORE_membership_test (struct GNUNET_PSYCSTORE_Handle *h,
596                                   const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
597                                   const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
598                                   uint64_t message_id,
599                                   uint64_t group_generation,
600                                   GNUNET_PSYCSTORE_ResultCallback result_cb,
601                                   void *cls)
602 {
603   struct MembershipTestRequest *req;
604   struct GNUNET_MQ_Envelope *
605     env = GNUNET_MQ_msg (req, GNUNET_MESSAGE_TYPE_PSYCSTORE_MEMBERSHIP_TEST);
606   req->channel_key = *channel_key;
607   req->slave_key = *slave_key;
608   req->message_id = GNUNET_htonll (message_id);
609   req->group_generation = GNUNET_htonll (group_generation);
610
611   return
612     op_send (h, op_create (h, h->op, result_cb, cls),
613              env, &req->op_id);
614 }
615
616
617 /**
618  * Store a message fragment sent to a channel.
619  *
620  * @param h Handle for the PSYCstore.
621  * @param channel_key The channel the message belongs to.
622  * @param message Message to store.
623  * @param psycstore_flags Flags indicating whether the PSYC message contains
624  *        state modifiers.
625  * @param result_cb Callback to call with the result of the operation.
626  * @param cls Closure for the callback.
627  *
628  * @return Handle that can be used to cancel the operation.
629  */
630 struct GNUNET_PSYCSTORE_OperationHandle *
631 GNUNET_PSYCSTORE_fragment_store (struct GNUNET_PSYCSTORE_Handle *h,
632                                  const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
633                                  const struct GNUNET_MULTICAST_MessageHeader *msg,
634                                  enum GNUNET_PSYCSTORE_MessageFlags psycstore_flags,
635                                  GNUNET_PSYCSTORE_ResultCallback result_cb,
636                                  void *cls)
637 {
638   uint16_t size = ntohs (msg->header.size);
639   struct FragmentStoreRequest *req;
640   struct GNUNET_MQ_Envelope *
641     env = GNUNET_MQ_msg_extra (req, size,
642                                GNUNET_MESSAGE_TYPE_PSYCSTORE_FRAGMENT_STORE);
643   req->channel_key = *channel_key;
644   req->psycstore_flags = htonl (psycstore_flags);
645   GNUNET_memcpy (&req[1], msg, size);
646
647   return
648     op_send (h, op_create (h, h->op, result_cb, cls),
649              env, &req->op_id);
650 }
651
652
653 /**
654  * Retrieve message fragments by fragment ID range.
655  *
656  * @param h
657  *        Handle for the PSYCstore.
658  * @param channel_key
659  *        The channel we are interested in.
660  * @param slave_key
661  *        The slave requesting the fragment.  If not NULL, a membership test is
662  *        performed first and the fragment is only returned if the slave has
663  *        access to it.
664  * @param first_fragment_id
665  *        First fragment ID to retrieve.
666  *        Use 0 to get the latest message fragment.
667  * @param last_fragment_id
668  *        Last consecutive fragment ID to retrieve.
669  *        Use 0 to get the latest message fragment.
670  * @param fragment_limit
671  *        Maximum number of fragments to retrieve.
672  * @param fragment_cb
673  *        Callback to call with the retrieved fragments.
674  * @param result_cb
675  *        Callback to call with the result of the operation.
676  * @param cls
677  *        Closure for the callbacks.
678  *
679  * @return Handle that can be used to cancel the operation.
680  */
681 struct GNUNET_PSYCSTORE_OperationHandle *
682 GNUNET_PSYCSTORE_fragment_get (struct GNUNET_PSYCSTORE_Handle *h,
683                                const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
684                                const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
685                                uint64_t first_fragment_id,
686                                uint64_t last_fragment_id,
687                                GNUNET_PSYCSTORE_FragmentCallback fragment_cb,
688                                GNUNET_PSYCSTORE_ResultCallback result_cb,
689                                void *cls)
690 {
691   struct FragmentGetRequest *req;
692   struct GNUNET_MQ_Envelope *
693     env = GNUNET_MQ_msg (req, GNUNET_MESSAGE_TYPE_PSYCSTORE_FRAGMENT_GET);
694   req->channel_key = *channel_key;
695   req->first_fragment_id = GNUNET_htonll (first_fragment_id);
696   req->last_fragment_id = GNUNET_htonll (last_fragment_id);
697   if (NULL != slave_key)
698   {
699     req->slave_key = *slave_key;
700     req->do_membership_test = GNUNET_YES;
701   }
702
703   struct GNUNET_PSYCSTORE_OperationHandle *
704     op = op_create (h, h->op, result_cb, cls);
705   op->fragment_cb = fragment_cb;
706   op->cls = cls;
707   return op_send (h, op, env, &req->op_id);
708 }
709
710
711 /**
712  * Retrieve latest message fragments.
713  *
714  * @param h
715  *        Handle for the PSYCstore.
716  * @param channel_key
717  *        The channel we are interested in.
718  * @param slave_key
719  *        The slave requesting the fragment.  If not NULL, a membership test is
720  *        performed first and the fragment is only returned if the slave has
721  *        access to it.
722  * @param first_fragment_id
723  *        First fragment ID to retrieve.
724  *        Use 0 to get the latest message fragment.
725  * @param last_fragment_id
726  *        Last consecutive fragment ID to retrieve.
727  *        Use 0 to get the latest message fragment.
728  * @param fragment_limit
729  *        Maximum number of fragments to retrieve.
730  * @param fragment_cb
731  *        Callback to call with the retrieved fragments.
732  * @param result_cb
733  *        Callback to call with the result of the operation.
734  * @param cls
735  *        Closure for the callbacks.
736  *
737  * @return Handle that can be used to cancel the operation.
738  */
739 struct GNUNET_PSYCSTORE_OperationHandle *
740 GNUNET_PSYCSTORE_fragment_get_latest (struct GNUNET_PSYCSTORE_Handle *h,
741                                       const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
742                                       const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
743                                       uint64_t fragment_limit,
744                                       GNUNET_PSYCSTORE_FragmentCallback fragment_cb,
745                                       GNUNET_PSYCSTORE_ResultCallback result_cb,
746                                       void *cls)
747 {
748   struct FragmentGetRequest *req;
749   struct GNUNET_MQ_Envelope *
750     env = GNUNET_MQ_msg (req, GNUNET_MESSAGE_TYPE_PSYCSTORE_FRAGMENT_GET);
751   req->channel_key = *channel_key;
752   req->fragment_limit = GNUNET_ntohll (fragment_limit);
753   if (NULL != slave_key)
754   {
755     req->slave_key = *slave_key;
756     req->do_membership_test = GNUNET_YES;
757   }
758
759   struct GNUNET_PSYCSTORE_OperationHandle *
760     op = op_create (h, h->op, result_cb, cls);
761   op->fragment_cb = fragment_cb;
762   op->cls = cls;
763   return op_send (h, op, env, &req->op_id);
764 }
765
766
767 /**
768  * Retrieve all fragments of messages in a message ID range.
769  *
770  * @param h
771  *        Handle for the PSYCstore.
772  * @param channel_key
773  *        The channel we are interested in.
774  * @param slave_key
775  *        The slave requesting the message.
776  *        If not NULL, a membership test is performed first
777  *        and the message is only returned if the slave has access to it.
778  * @param first_message_id
779  *        First message ID to retrieve.
780  * @param last_message_id
781  *        Last consecutive message ID to retrieve.
782  * @param fragment_limit
783  *        Maximum number of fragments to retrieve.
784  * @param method_prefix
785  *        Retrieve only messages with a matching method prefix.
786  * @todo Implement method_prefix query.
787  * @param fragment_cb
788  *        Callback to call with the retrieved fragments.
789  * @param result_cb
790  *        Callback to call with the result of the operation.
791  * @param cls
792  *        Closure for the callbacks.
793  *
794  * @return Handle that can be used to cancel the operation.
795  */
796 struct GNUNET_PSYCSTORE_OperationHandle *
797 GNUNET_PSYCSTORE_message_get (struct GNUNET_PSYCSTORE_Handle *h,
798                               const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
799                               const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
800                               uint64_t first_message_id,
801                               uint64_t last_message_id,
802                               uint64_t fragment_limit,
803                               const char *method_prefix,
804                               GNUNET_PSYCSTORE_FragmentCallback fragment_cb,
805                               GNUNET_PSYCSTORE_ResultCallback result_cb,
806                               void *cls)
807 {
808   struct MessageGetRequest *req;
809   if (NULL == method_prefix)
810     method_prefix = "";
811   uint16_t method_size = strnlen (method_prefix,
812                                   GNUNET_SERVER_MAX_MESSAGE_SIZE
813                                   - sizeof (*req)) + 1;
814
815   struct GNUNET_MQ_Envelope *
816     env = GNUNET_MQ_msg_extra (req, method_size,
817                                GNUNET_MESSAGE_TYPE_PSYCSTORE_MESSAGE_GET);
818   req->channel_key = *channel_key;
819   req->first_message_id = GNUNET_htonll (first_message_id);
820   req->last_message_id = GNUNET_htonll (last_message_id);
821   req->fragment_limit = GNUNET_htonll (fragment_limit);
822   if (NULL != slave_key)
823   {
824     req->slave_key = *slave_key;
825     req->do_membership_test = GNUNET_YES;
826   }
827   GNUNET_memcpy (&req[1], method_prefix, method_size);
828   ((char *) &req[1])[method_size - 1] = '\0';
829
830   struct GNUNET_PSYCSTORE_OperationHandle *
831     op = op_create (h, h->op, result_cb, cls);
832   op->fragment_cb = fragment_cb;
833   op->cls = cls;
834   return op_send (h, op, env, &req->op_id);
835 }
836
837
838 /**
839  * Retrieve all fragments of the latest messages.
840  *
841  * @param h
842  *        Handle for the PSYCstore.
843  * @param channel_key
844  *        The channel we are interested in.
845  * @param slave_key
846  *        The slave requesting the message.
847  *        If not NULL, a membership test is performed first
848  *        and the message is only returned if the slave has access to it.
849  * @param message_limit
850  *        Maximum number of messages to retrieve.
851  * @param method_prefix
852  *        Retrieve only messages with a matching method prefix.
853  * @todo Implement method_prefix query.
854  * @param fragment_cb
855  *        Callback to call with the retrieved fragments.
856  * @param result_cb
857  *        Callback to call with the result of the operation.
858  * @param cls
859  *        Closure for the callbacks.
860  *
861  * @return Handle that can be used to cancel the operation.
862  */
863 struct GNUNET_PSYCSTORE_OperationHandle *
864 GNUNET_PSYCSTORE_message_get_latest (struct GNUNET_PSYCSTORE_Handle *h,
865                                      const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
866                                      const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
867                                      uint64_t message_limit,
868                                      const char *method_prefix,
869                                      GNUNET_PSYCSTORE_FragmentCallback fragment_cb,
870                                      GNUNET_PSYCSTORE_ResultCallback result_cb,
871                                      void *cls)
872 {
873   struct MessageGetRequest *req;
874
875   if (NULL == method_prefix)
876     method_prefix = "";
877   uint16_t method_size = strnlen (method_prefix,
878                                   GNUNET_SERVER_MAX_MESSAGE_SIZE
879                                   - sizeof (*req)) + 1;
880   GNUNET_assert ('\0' == method_prefix[method_size - 1]);
881
882   struct GNUNET_MQ_Envelope *
883     env = GNUNET_MQ_msg_extra (req, method_size,
884                                GNUNET_MESSAGE_TYPE_PSYCSTORE_MESSAGE_GET);
885   req->channel_key = *channel_key;
886   req->message_limit = GNUNET_ntohll (message_limit);
887   if (NULL != slave_key)
888   {
889     req->slave_key = *slave_key;
890     req->do_membership_test = GNUNET_YES;
891   }
892   GNUNET_memcpy (&req[1], method_prefix, method_size);
893
894   struct GNUNET_PSYCSTORE_OperationHandle *
895     op = op_create (h, h->op, result_cb, cls);
896   op->fragment_cb = fragment_cb;
897   op->cls = cls;
898   return op_send (h, op, env, &req->op_id);
899 }
900
901
902 /**
903  * Retrieve a fragment of message specified by its message ID and fragment
904  * offset.
905  *
906  * @param h
907  *        Handle for the PSYCstore.
908  * @param channel_key
909  *        The channel we are interested in.
910  * @param slave_key
911  *        The slave requesting the message fragment.  If not NULL, a membership
912  *        test is performed first and the message fragment is only returned
913  *        if the slave has access to it.
914  * @param message_id
915  *        Message ID to retrieve.  Use 0 to get the latest message.
916  * @param fragment_offset
917  *        Offset of the fragment to retrieve.
918  * @param fragment_cb
919  *        Callback to call with the retrieved fragments.
920  * @param result_cb
921  *        Callback to call with the result of the operation.
922  * @param cls
923  *        Closure for the callbacks.
924  *
925  * @return Handle that can be used to cancel the operation.
926  */
927 struct GNUNET_PSYCSTORE_OperationHandle *
928 GNUNET_PSYCSTORE_message_get_fragment (struct GNUNET_PSYCSTORE_Handle *h,
929                                        const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
930                                        const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
931                                        uint64_t message_id,
932                                        uint64_t fragment_offset,
933                                        GNUNET_PSYCSTORE_FragmentCallback fragment_cb,
934                                        GNUNET_PSYCSTORE_ResultCallback result_cb,
935                                        void *cls)
936 {
937   struct MessageGetFragmentRequest *req;
938   struct GNUNET_MQ_Envelope *
939     env = GNUNET_MQ_msg (req, GNUNET_MESSAGE_TYPE_PSYCSTORE_MESSAGE_GET_FRAGMENT);
940
941   req->channel_key = *channel_key;
942   req->message_id = GNUNET_htonll (message_id);
943   req->fragment_offset = GNUNET_htonll (fragment_offset);
944   if (NULL != slave_key)
945   {
946     req->slave_key = *slave_key;
947     req->do_membership_test = GNUNET_YES;
948   }
949
950   struct GNUNET_PSYCSTORE_OperationHandle *
951     op = op_create (h, h->op, result_cb, cls);
952   op->fragment_cb = fragment_cb;
953   op->cls = cls;
954   return op_send (h, op, env, &req->op_id);
955 }
956
957
958 /**
959  * Retrieve latest values of counters for a channel master.
960  *
961  * The current value of counters are needed when a channel master is restarted,
962  * so that it can continue incrementing the counters from their last value.
963  *
964  * @param h
965  *        Handle for the PSYCstore.
966  * @param channel_key
967  *        Public key that identifies the channel.
968  * @param ccb
969  *        Callback to call with the result.
970  * @param ccb_cls
971  *        Closure for the @a ccb callback.
972  *
973  * @return Handle that can be used to cancel the operation.
974  */
975 struct GNUNET_PSYCSTORE_OperationHandle *
976 GNUNET_PSYCSTORE_counters_get (struct GNUNET_PSYCSTORE_Handle *h,
977                                struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
978                                GNUNET_PSYCSTORE_CountersCallback counters_cb,
979                                void *cls)
980 {
981   struct OperationRequest *req;
982   struct GNUNET_MQ_Envelope *
983     env = GNUNET_MQ_msg (req, GNUNET_MESSAGE_TYPE_PSYCSTORE_COUNTERS_GET);
984   req->channel_key = *channel_key;
985
986   struct GNUNET_PSYCSTORE_OperationHandle *
987     op = op_create (h, h->op, NULL, NULL);
988   op->counters_cb = counters_cb;
989   op->cls = cls;
990   return op_send (h, op, env, &req->op_id);
991 }
992
993
994 /**
995  * Apply modifiers of a message to the current channel state.
996  *
997  * An error is returned if there are missing messages containing state
998  * operations before the current one.
999  *
1000  * @param h
1001  *        Handle for the PSYCstore.
1002  * @param channel_key
1003  *        The channel we are interested in.
1004  * @param message_id
1005  *        ID of the message that contains the @a modifiers.
1006  * @param state_delta
1007  *        Value of the _state_delta PSYC header variable of the message.
1008  * @param result_cb
1009  *        Callback to call with the result of the operation.
1010  * @param cls
1011  *        Closure for @a result_cb.
1012  *
1013  * @return Handle that can be used to cancel the operation.
1014  */
1015 struct GNUNET_PSYCSTORE_OperationHandle *
1016 GNUNET_PSYCSTORE_state_modify (struct GNUNET_PSYCSTORE_Handle *h,
1017                                const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1018                                uint64_t message_id,
1019                                uint64_t state_delta,
1020                                GNUNET_PSYCSTORE_ResultCallback result_cb,
1021                                void *cls)
1022 {
1023   struct StateModifyRequest *req;
1024   struct GNUNET_MQ_Envelope *
1025     env = GNUNET_MQ_msg (req, GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_MODIFY);
1026   req->channel_key = *channel_key;
1027   req->message_id = GNUNET_htonll (message_id);
1028   req->state_delta = GNUNET_htonll (state_delta);
1029
1030   return op_send (h, op_create (h, h->op, result_cb, cls),
1031                   env, &req->op_id);
1032 }
1033
1034
1035 struct StateSyncClosure
1036 {
1037   GNUNET_PSYCSTORE_ResultCallback result_cb;
1038   void *cls;
1039   uint8_t last;
1040 };
1041
1042
1043 static void
1044 state_sync_result (void *cls, int64_t result,
1045                    const char *err_msg, uint16_t err_msg_size)
1046 {
1047   struct StateSyncClosure *ssc = cls;
1048   if (GNUNET_OK != result || ssc->last)
1049     ssc->result_cb (ssc->cls, result, err_msg, err_msg_size);
1050   GNUNET_free (ssc);
1051 }
1052
1053
1054 /**
1055  * Store synchronized state.
1056  *
1057  * @param h
1058  *        Handle for the PSYCstore.
1059  * @param channel_key
1060  *        The channel we are interested in.
1061  * @param max_state_message_id
1062  *        ID of the last stateful message before @a state_hash_message_id.
1063  * @param state_hash_message_id
1064  *        ID of the message that contains the state_hash PSYC header variable.
1065  * @param modifier_count
1066  *        Number of elements in the @a modifiers array.
1067  * @param modifiers
1068  *        Full state to store.
1069  * @param result_cb
1070  *        Callback to call with the result of the operation.
1071  * @param cls
1072  *        Closure for the callback.
1073  *
1074  * @return Handle that can be used to cancel the operation.
1075  */
1076 struct GNUNET_PSYCSTORE_OperationHandle *
1077 GNUNET_PSYCSTORE_state_sync (struct GNUNET_PSYCSTORE_Handle *h,
1078                              const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1079                              uint64_t max_state_message_id,
1080                              uint64_t state_hash_message_id,
1081                              size_t modifier_count,
1082                              const struct GNUNET_PSYC_Modifier *modifiers,
1083                              GNUNET_PSYCSTORE_ResultCallback result_cb,
1084                              void *cls)
1085 {
1086   struct GNUNET_PSYCSTORE_OperationHandle *op = NULL;
1087   size_t i;
1088
1089   for (i = 0; i < modifier_count; i++) {
1090     struct StateSyncRequest *req;
1091     uint16_t name_size = strlen (modifiers[i].name) + 1;
1092
1093     struct GNUNET_MQ_Envelope *
1094       env = GNUNET_MQ_msg_extra (req,
1095                                  sizeof (*req) + name_size + modifiers[i].value_size,
1096                                  GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_SYNC);
1097
1098     req->header.type = htons (GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_SYNC);
1099     req->header.size = htons (sizeof (*req) + name_size
1100                               + modifiers[i].value_size);
1101     req->channel_key = *channel_key;
1102     req->max_state_message_id = GNUNET_htonll (max_state_message_id);
1103     req->state_hash_message_id = GNUNET_htonll (state_hash_message_id);
1104     req->name_size = htons (name_size);
1105     req->flags
1106       = (0 == i)
1107       ? STATE_OP_FIRST
1108       : (modifier_count - 1 == i)
1109       ? STATE_OP_LAST
1110       : 0;
1111
1112     GNUNET_memcpy (&req[1], modifiers[i].name, name_size);
1113     GNUNET_memcpy ((char *) &req[1] + name_size, modifiers[i].value, modifiers[i].value_size);
1114
1115     struct StateSyncClosure *ssc = GNUNET_malloc (sizeof (*ssc));
1116     ssc->last = (req->flags & STATE_OP_LAST);
1117     ssc->result_cb = result_cb;
1118     ssc->cls = cls;
1119
1120     op_send (h, op_create (h, h->op, state_sync_result, ssc),
1121              env, &req->op_id);
1122   }
1123   // FIXME: only one operation is returned,
1124   //        add pointers to other operations and make all cancellable.
1125   return op;
1126 }
1127
1128
1129 /**
1130  * Reset the state of a channel.
1131  *
1132  * Delete all state variables stored for the given channel.
1133  *
1134  * @param h
1135  *        Handle for the PSYCstore.
1136  * @param channel_key
1137  *        The channel we are interested in.
1138  * @param result_cb
1139  *        Callback to call with the result of the operation.
1140  * @param cls
1141  *        Closure for the callback.
1142  *
1143  * @return Handle that can be used to cancel the operation.
1144  */
1145 struct GNUNET_PSYCSTORE_OperationHandle *
1146 GNUNET_PSYCSTORE_state_reset (struct GNUNET_PSYCSTORE_Handle *h,
1147                               const struct GNUNET_CRYPTO_EddsaPublicKey
1148                               *channel_key,
1149                               GNUNET_PSYCSTORE_ResultCallback result_cb,
1150                               void *cls)
1151 {
1152   struct OperationRequest *req;
1153   struct GNUNET_MQ_Envelope *
1154     env = GNUNET_MQ_msg (req, GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_RESET);
1155   req->channel_key = *channel_key;
1156
1157   return
1158     op_send (h, op_create (h, h->op, result_cb, cls),
1159              env, &req->op_id);
1160 }
1161
1162
1163 /**
1164  * Update signed values of state variables in the state store.
1165  *
1166  * @param h
1167  *        Handle for the PSYCstore.
1168  * @param channel_key
1169  *        The channel we are interested in.
1170  * @param message_id
1171  *        Message ID that contained the state @a hash.
1172  * @param hash
1173  *        Hash of the serialized full state.
1174  * @param result_cb
1175  *        Callback to call with the result of the operation.
1176  * @param cls
1177  *        Closure for the callback.
1178  */
1179 struct GNUNET_PSYCSTORE_OperationHandle *
1180 GNUNET_PSYCSTORE_state_hash_update (struct GNUNET_PSYCSTORE_Handle *h,
1181                                     const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1182                                     uint64_t message_id,
1183                                     const struct GNUNET_HashCode *hash,
1184                                     GNUNET_PSYCSTORE_ResultCallback result_cb,
1185                                     void *cls)
1186 {
1187   struct StateHashUpdateRequest *req;
1188   struct GNUNET_MQ_Envelope *
1189     env = GNUNET_MQ_msg (req, GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_HASH_UPDATE);
1190   req->channel_key = *channel_key;
1191   req->hash = *hash;
1192
1193   return
1194     op_send (h, op_create (h, h->op, result_cb, cls),
1195              env, &req->op_id);
1196 }
1197
1198
1199 /**
1200  * Retrieve the best matching state variable.
1201  *
1202  * @param h
1203  *        Handle for the PSYCstore.
1204  * @param channel_key
1205  *        The channel we are interested in.
1206  * @param name
1207  *        Name of variable to match, the returned variable might be less specific.
1208  * @param state_cb
1209  *        Callback to return the matching state variable.
1210  * @param result_cb
1211  *        Callback to call with the result of the operation.
1212  * @param cls
1213  *        Closure for the callbacks.
1214  *
1215  * @return Handle that can be used to cancel the operation.
1216  */
1217 struct GNUNET_PSYCSTORE_OperationHandle *
1218 GNUNET_PSYCSTORE_state_get (struct GNUNET_PSYCSTORE_Handle *h,
1219                             const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1220                             const char *name,
1221                             GNUNET_PSYCSTORE_StateCallback state_cb,
1222                             GNUNET_PSYCSTORE_ResultCallback result_cb,
1223                             void *cls)
1224 {
1225   size_t name_size = strlen (name) + 1;
1226   struct OperationRequest *req;
1227   struct GNUNET_MQ_Envelope *
1228     env = GNUNET_MQ_msg_extra (req, name_size,
1229                                GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_GET);
1230   req->channel_key = *channel_key;
1231   GNUNET_memcpy (&req[1], name, name_size);
1232
1233   struct GNUNET_PSYCSTORE_OperationHandle *
1234     op = op_create (h, h->op, result_cb, cls);
1235   op->state_cb = state_cb;
1236   op->cls = cls;
1237   return op_send (h, op, env, &req->op_id);
1238 }
1239
1240
1241 /**
1242  * Retrieve all state variables for a channel with the given prefix.
1243  *
1244  * @param h
1245  *        Handle for the PSYCstore.
1246  * @param channel_key
1247  *        The channel we are interested in.
1248  * @param name_prefix
1249  *        Prefix of state variable names to match.
1250  * @param state_cb
1251  *        Callback to return matching state variables.
1252  * @param result_cb
1253  *        Callback to call with the result of the operation.
1254  * @param cls
1255  *        Closure for the callbacks.
1256  *
1257  * @return Handle that can be used to cancel the operation.
1258  */
1259 struct GNUNET_PSYCSTORE_OperationHandle *
1260 GNUNET_PSYCSTORE_state_get_prefix (struct GNUNET_PSYCSTORE_Handle *h,
1261                                    const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1262                                    const char *name_prefix,
1263                                    GNUNET_PSYCSTORE_StateCallback state_cb,
1264                                    GNUNET_PSYCSTORE_ResultCallback result_cb,
1265                                    void *cls)
1266 {
1267   size_t name_size = strlen (name_prefix) + 1;
1268   struct OperationRequest *req;
1269   struct GNUNET_MQ_Envelope *
1270     env = GNUNET_MQ_msg_extra (req, name_size,
1271                                GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_GET_PREFIX);
1272   req->channel_key = *channel_key;
1273   GNUNET_memcpy (&req[1], name_prefix, name_size);
1274
1275   struct GNUNET_PSYCSTORE_OperationHandle *
1276     op = op_create (h, h->op, result_cb, cls);
1277   op->state_cb = state_cb;
1278   op->cls = cls;
1279   return op_send (h, op, env, &req->op_id);
1280 }
1281
1282 /* end of psycstore_api.c */