psycutil reorg: message, env, slicer
[oweals/gnunet.git] / src / include / gnunet_psyc_service.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012, 2013 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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @author Gabor X Toth
23  * @author Christian Grothoff
24  *
25  * @file
26  * PSYC service
27  *
28  * @defgroup psyc  PSYC service
29  * Send/receive messages in PSYC channels and access the PSYC Store.
30  *
31  * Note that clients of this API are NOT expected to understand the PSYC message
32  * format, only the semantics!  Parsing (and serializing) the PSYC stream format
33  * is done within the implementation of the libgnunetpsyc library, and this API
34  * deliberately exposes as little as possible of the actual data stream format
35  * to the application!
36  *
37  * NOTE:
38  * - this API does not know about PSYC's "root" and "places";
39  *   there is no 'root' in GNUnet-PSYC as we're decentralized;
40  *   'places' and 'persons' are combined within the same
41  *   abstraction, that of a "channel".  Channels are identified
42  *   and accessed in this API using a public/private key.
43  *   Higher-level applications should use NAMES within GNS
44  *   to obtain public keys, and the distinction between
45  *   'places' and 'persons' can then be made with the help
46  *   of the naming system (and/or conventions).
47  *   Channels are (as in PSYC) organized into a hierarchy; each
48  *   channel master (the one with the private key) is then
49  *   the operator of the multicast group (its Origin in
50  *   the terminology of the multicast API).
51  * - The API supports passing large amounts of data using
52  *   'streaming' for the argument passed to a method.  State
53  *   and variables must fit into memory and cannot be streamed
54  *   (thus, no passing of 4 GB of data in a variable;
55  *   once we implement this, we might want to create a
56  *   @c \#define for the maximum size of a variable).
57  * - PSYC defines standard variables, methods, etc.  This
58  *   library deliberately abstracts over all of these; a
59  *   higher-level API should combine the naming system (GNS)
60  *   and standard methods (message, join, part, warn,
61  *   fail, error) and variables (action, color, time,
62  *   tag, etc.).  However, this API does take over the
63  *   routing variables, specifically 'context' (channel),
64  *   and 'source'.  We only kind-of support 'target', as
65  *   the target is either everyone in the group or the
66  *   origin, and never just a single member of the group;
67  *   for such individual messages, an application needs to
68  *   construct an 'inbox' channel where the master (only)
69  *   receives messages (but never forwards; private responses
70  *   would be transmitted by joining the senders 'inbox'
71  *   channel -- or a inbox#bob subchannel).  The
72  *   goal for all of this is to keep the abstractions in this
73  *   API minimal: interaction with multicast, try \& slice,
74  *   state/variable/channel management.  Higher-level
75  *   operations belong elsewhere (so maybe this API should
76  *   be called 'PSYC-low', whereas a higher-level API
77  *   implementing defaults for standard methods and
78  *   variables might be called 'PSYC-std' or 'PSYC-high'.
79  *
80  * @{
81  */
82
83 #ifndef GNUNET_PSYC_SERVICE_H
84 #define GNUNET_PSYC_SERVICE_H
85
86 #ifdef __cplusplus
87 extern "C"
88 {
89 #if 0                           /* keep Emacsens' auto-indent happy */
90 }
91 #endif
92 #endif
93
94 #include "gnunet_util_lib.h"
95 #include "gnunet_multicast_service.h"
96 //Mingw work around
97 #ifdef MINGW
98     # ifndef  UINT64_MAX
99     # define  UINT64_MAX 0xffffffffffffffffULL
100     # endif
101 #endif
102
103 /**
104  * Version number of GNUnet-PSYC API.
105  */
106 #define GNUNET_PSYC_VERSION 0x00000000
107
108
109 /**
110  * Policy flags for a channel.
111  */
112 enum GNUNET_PSYC_ChannelFlags
113 {
114   /**
115    * Admission must be confirmed by the master.
116    */
117   GNUNET_PSYC_CHANNEL_ADMISSION_CONTROL = 1 << 0,
118
119   /**
120    * Past messages are only available to slaves who were admitted at the time
121    * they were sent to the channel.
122    */
123   GNUNET_PSYC_CHANNEL_RESTRICTED_HISTORY = 1 << 1
124 };
125
126
127 /**
128  * PSYC channel policies.
129  */
130 enum GNUNET_PSYC_Policy
131 {
132   /**
133    * Anyone can join the channel, without announcing his presence;
134    * all messages are always public and can be distributed freely.
135    * Joins may be announced, but this is not required.
136    */
137   GNUNET_PSYC_CHANNEL_ANONYMOUS = 0,
138
139   /**
140    * The master must approve membership to the channel, messages must only be
141    * distributed to current channel slaves.  This includes the channel
142    * state as well as transient messages.
143    */
144   GNUNET_PSYC_CHANNEL_PRIVATE
145     = GNUNET_PSYC_CHANNEL_ADMISSION_CONTROL
146     | GNUNET_PSYC_CHANNEL_RESTRICTED_HISTORY
147
148 #if IDEAS_FOR_FUTURE
149   /**
150    * Anyone can freely join the channel (no approval required);
151    * however, messages must only be distributed to current channel
152    * slaves, so the master must still acknowledge that the slave
153    * joined before transient messages are delivered.  As approval is
154    * guaranteed, the presistent channel state can be synchronized freely
155    * immediately, prior to master confirmation.
156    */
157   GNUNET_PSYC_CHANNEL_OPEN
158     = GNUNET_PSYC_CHANNEL_RESTRICTED_HISTORY,
159
160   /**
161    * The master must approve joins to the channel, but past messages can be
162    * freely distributed to slaves.
163    */
164   GNUNET_PSYC_CHANNEL_CLOSED
165     = GNUNET_PSYC_CHANNEL_ADMISSION_CONTROL,
166 #endif
167 };
168
169
170 enum GNUNET_PSYC_MessageFlags
171 {
172   /**
173    * Default / no flags.
174    */
175   GNUNET_PSYC_MESSAGE_DEFAULT = 0,
176
177   /**
178    * Historic message, retrieved from PSYCstore.
179    */
180   GNUNET_PSYC_MESSAGE_HISTORIC = 1 << 0,
181
182   /**
183    * Request from slave to master.
184    */
185   GNUNET_PSYC_MESSAGE_REQUEST = 1 << 1,
186
187   /**
188    * Message can be delivered out of order.
189    */
190   GNUNET_PSYC_MESSAGE_ORDER_ANY = 1 << 2
191 };
192
193
194 /**
195  * Values for the @a state_delta field of GNUNET_PSYC_MessageHeader.
196  */
197 enum GNUNET_PSYC_StateDeltaValues
198 {
199   GNUNET_PSYC_STATE_RESET = 0,
200
201   GNUNET_PSYC_STATE_NOT_MODIFIED = UINT64_MAX
202 };
203
204
205 GNUNET_NETWORK_STRUCT_BEGIN
206
207 /**
208  * A PSYC message.
209  *
210  * Used for single-fragment messages e.g. in a join request or response.
211  */
212 struct GNUNET_PSYC_Message
213 {
214   /**
215    * Message header with size and type information.
216    */
217   struct GNUNET_MessageHeader header;
218
219   /* Followed by concatenated PSYC message parts:
220    * messages with GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_* types
221    */
222 };
223
224
225 /**
226  * Header of a PSYC message.
227  *
228  * The PSYC service adds this when delivering the message to local clients,
229  * not present on the multicast layer.
230  */
231 struct GNUNET_PSYC_MessageHeader
232 {
233   /**
234    * Generic message header with size and type information.
235    */
236   struct GNUNET_MessageHeader header;
237
238   /**
239    * Flags for this message fragment.
240    *
241    * @see enum GNUNET_PSYC_MessageFlags
242    */
243   uint32_t flags GNUNET_PACKED;
244
245   /**
246    * Number of the message this message part belongs to.
247    * Monotonically increasing from 1.
248    */
249   uint64_t message_id GNUNET_PACKED;
250
251   /**
252    * Byte offset of this @e fragment of the @e message.
253    * FIXME: use data_offset instead
254    */
255   uint64_t fragment_offset GNUNET_PACKED;
256
257   /**
258    * Sending slave's public key.
259    * Not set if the message is from the master.
260    */
261   struct GNUNET_CRYPTO_EcdsaPublicKey slave_key;
262
263   /* Followed by concatenated PSYC message parts:
264    * messages with GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_* types
265    */
266 };
267
268
269 /**
270  * The method of a message.
271  */
272 struct GNUNET_PSYC_MessageMethod
273 {
274   /**
275    * Type: GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD
276    */
277   struct GNUNET_MessageHeader header;
278
279   /**
280    * OR'ed GNUNET_PSYC_MasterTransmitFlags
281    */
282   uint32_t flags GNUNET_PACKED;
283
284   /**
285    * Number of message IDs since the last message that contained state
286    * operations. @see enum GNUNET_PSYC_StateDeltaValues
287    */
288   uint64_t state_delta GNUNET_PACKED;
289
290   /* Followed by NUL-terminated method name. */
291 };
292
293
294 /**
295  * A modifier of a message.
296  */
297 struct GNUNET_PSYC_MessageModifier
298 {
299   /**
300    * Type: GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER
301    */
302   struct GNUNET_MessageHeader header;
303
304   /**
305    * Size of value.
306    */
307   uint32_t value_size GNUNET_PACKED;
308
309   /**
310    * Size of name, including NUL terminator.
311    */
312   uint16_t name_size GNUNET_PACKED;
313
314   /**
315    * enum GNUNET_ENV_Operator
316    */
317   uint8_t oper;
318
319   /* Followed by NUL-terminated name, then the value. */
320 };
321
322
323 struct GNUNET_PSYC_CountersResultMessage
324 {
325   /**
326    * Type: GNUNET_MESSAGE_TYPE_PSYC_RESULT_COUNTERS
327    */
328   struct GNUNET_MessageHeader header;
329
330   /**
331    * Status code for the operation.
332    */
333   uint32_t result_code GNUNET_PACKED;
334
335   /**
336    * Last message ID sent to the channel.
337    */
338   uint64_t max_message_id GNUNET_PACKED;
339 };
340
341
342 /**
343  * Join request sent to a PSYC master.
344  */
345 struct GNUNET_PSYC_JoinRequestMessage
346 {
347   /**
348    * Type: GNUNET_MESSAGE_TYPE_PSYC_MASTER_JOIN_REQUEST
349    */
350   struct GNUNET_MessageHeader header;
351   /**
352    * Public key of the joining slave.
353    */
354   struct GNUNET_CRYPTO_EcdsaPublicKey slave_key;
355
356   /* Followed by struct GNUNET_MessageHeader join_request */
357 };
358
359
360 /**
361  * Join decision sent in reply to a join request.
362  */
363 struct GNUNET_PSYC_JoinDecisionMessage
364 {
365   /**
366    * Type: GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION
367    */
368   struct GNUNET_MessageHeader header;
369
370   /**
371    * #GNUNET_YES if the slave was admitted.
372    */
373   int32_t is_admitted;
374
375   /**
376    * Public key of the joining slave.
377    * Only set when the master is sending the decision,
378    * not set when a slave is receiving it.
379    */
380   struct GNUNET_CRYPTO_EcdsaPublicKey slave_key;
381
382   /* Followed by struct GNUNET_MessageHeader join_response */
383 };
384
385
386 enum GNUNET_PSYC_HistoryReplayFlags
387 {
388   /**
389    * Replay locally available messages.
390    */
391   GNUNET_PSYC_HISTORY_REPLAY_LOCAL  = 0,
392
393   /**
394    * Replay messages from remote peers if not found locally.
395    */
396   GNUNET_PSYC_HISTORY_REPLAY_REMOTE = 1,
397 };
398
399
400 struct GNUNET_PSYC_HistoryRequestMessage
401 {
402   /**
403    * Type: GNUNET_MESSAGE_TYPE_PSYC_CHANNEL_HISTORY_REPLAY
404    */
405   struct GNUNET_MessageHeader header;
406
407   /**
408    * @see enum GNUNET_PSYC_HistoryReplayFlags
409    */
410   uint32_t flags GNUNET_PACKED;
411
412   /**
413    * ID for this operation.
414    */
415   uint64_t op_id GNUNET_PACKED;
416
417   uint64_t start_message_id GNUNET_PACKED;
418
419   uint64_t end_message_id GNUNET_PACKED;
420
421   uint64_t message_limit GNUNET_PACKED;
422
423   /* Followed by NUL-terminated method name prefix. */
424 };
425
426
427 struct GNUNET_PSYC_StateRequestMessage
428 {
429   /**
430    * Types:
431    * - GNUNET_MESSAGE_TYPE_PSYC_CHANNEL_STATE_GET
432    * - GNUNET_MESSAGE_TYPE_PSYC_CHANNEL_STATE_GET_PREFIX
433    */
434   struct GNUNET_MessageHeader header;
435
436   uint32_t reserved GNUNET_PACKED;
437
438   /**
439    * ID for this operation.
440    */
441   uint64_t op_id GNUNET_PACKED;
442
443   /* Followed by NUL-terminated name. */
444 };
445
446
447 /**** service -> library ****/
448
449
450 /**
451  * Answer from service to client about last operation.
452  */
453 struct GNUNET_PSYC_OperationResultMessage
454 {
455   /**
456    * Types:
457    * - GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE
458    * - GNUNET_MESSAGE_TYPE_PSYC_CHANNEL_STATE_RESULT
459    */
460   struct GNUNET_MessageHeader header;
461
462   uint32_t reserved GNUNET_PACKED;
463
464   /**
465    * Operation ID.
466    */
467   uint64_t op_id GNUNET_PACKED;
468
469   /**
470    * Status code for the operation.
471    */
472   uint64_t result_code GNUNET_PACKED;
473
474   /* Followed by:
475    * - on error: NUL-terminated error message
476    * - on success: one of the following message types
477    *
478    *   For a STATE_RESULT, one of:
479    *   - GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER
480    *   - GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT
481    *   - GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END
482    */
483 };
484
485 GNUNET_NETWORK_STRUCT_END
486
487
488 #define GNUNET_PSYC_MODIFIER_MAX_PAYLOAD        \
489   GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD         \
490   - sizeof (struct GNUNET_PSYC_MessageModifier)
491
492 #define GNUNET_PSYC_MOD_CONT_MAX_PAYLOAD        \
493   GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD         \
494   - sizeof (struct GNUNET_MessageHeader)
495
496 #define GNUNET_PSYC_DATA_MAX_PAYLOAD            \
497   GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD         \
498   - sizeof (struct GNUNET_MessageHeader)
499
500
501 /**
502  * PSYC message part processing states.
503  */
504 enum GNUNET_PSYC_MessageState
505 {
506   GNUNET_PSYC_MESSAGE_STATE_START    = 0,
507   GNUNET_PSYC_MESSAGE_STATE_HEADER   = 1,
508   GNUNET_PSYC_MESSAGE_STATE_METHOD   = 2,
509   GNUNET_PSYC_MESSAGE_STATE_MODIFIER = 3,
510   GNUNET_PSYC_MESSAGE_STATE_MOD_CONT = 4,
511   GNUNET_PSYC_MESSAGE_STATE_DATA     = 5,
512   GNUNET_PSYC_MESSAGE_STATE_END      = 6,
513   GNUNET_PSYC_MESSAGE_STATE_CANCEL   = 7,
514   GNUNET_PSYC_MESSAGE_STATE_ERROR    = 8,
515 };
516
517
518 /**
519  * Handle that identifies a join request.
520  *
521  * Used to match calls to #GNUNET_PSYC_JoinCallback to the
522  * corresponding calls to GNUNET_PSYC_join_decision().
523  */
524 struct GNUNET_PSYC_JoinHandle;
525
526
527 /**
528  * Method called from PSYC upon receiving a message.
529  *
530  * @param cls  Closure.
531  * @param message_id  Sequence number of the message.
532  * @param flags  OR'ed GNUNET_PSYC_MessageFlags
533  * @param msg  Message part, one of the following types:
534  */
535 typedef void
536 (*GNUNET_PSYC_MessageCallback) (void *cls,
537                                 uint64_t message_id,
538                                 uint32_t flags,
539                                 const struct GNUNET_PSYC_MessageHeader *msg);
540
541
542 /**
543  * Method called from PSYC upon receiving part of a message.
544  *
545  * @param cls
546  *        Closure.
547  * @param slave_key
548  *        Public key of the slave sending the message.
549  *        Only set for channel master.
550  * @param message_id
551  *        Sequence number of the message.
552  * @param flags
553  *        OR'ed GNUNET_PSYC_MessageFlags
554  * @param data_offset
555  *        Byte offset of data, only set if @a msg has a type
556  *        #GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA
557  * @param msg  Message part, one of the following types:
558  * - #GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_HEADER
559  * - #GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD
560  * - #GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER
561  * - #GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT
562  * - #GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA
563  * or NULL if an error occurred while receiving a message.
564  */
565 typedef void
566 (*GNUNET_PSYC_MessagePartCallback) (void *cls,
567                                     const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
568                                     uint64_t message_id,
569                                     uint32_t flags,
570                                     uint64_t data_offset,
571                                     const struct GNUNET_MessageHeader *msg);
572
573
574 /**
575  * Method called from PSYC upon receiving a join request.
576  *
577  * @param cls
578  *        Closure.
579  * @param slave_key
580  *        Public key of the slave requesting join.
581  * @param join_msg
582  *        Join message sent along with the request.
583  * @param jh
584  *        Join handle to use with GNUNET_PSYC_join_decision()
585  */
586 typedef void
587 (*GNUNET_PSYC_JoinRequestCallback) (void *cls,
588                                     const struct GNUNET_PSYC_JoinRequestMessage *req,
589                                     const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
590                                     const struct GNUNET_PSYC_Message *join_msg,
591                                     struct GNUNET_PSYC_JoinHandle *jh);
592
593
594 /**
595  * Function to call with the decision made for a join request.
596  *
597  * Must be called once and only once in response to an invocation of the
598  * #GNUNET_PSYC_JoinCallback.
599  *
600  * @param jh  Join request handle.
601  * @param is_admitted
602  *   #GNUNET_YES    if the join is approved,
603  *   #GNUNET_NO     if it is disapproved,
604  *   #GNUNET_SYSERR if we cannot answer the request.
605  * @param relay_count  Number of relays given.
606  * @param relays  Array of suggested peers that might be useful relays to use
607  *        when joining the multicast group (essentially a list of peers that
608  *        are already part of the multicast group and might thus be willing
609  *        to help with routing).  If empty, only this local peer (which must
610  *        be the multicast origin) is a good candidate for building the
611  *        multicast tree.  Note that it is unnecessary to specify our own
612  *        peer identity in this array.
613  * @param join_resp  Application-dependent join response message to send along
614  *        with the decision.
615  *
616  * @return #GNUNET_OK on success,
617  *         #GNUNET_SYSERR if @a join_resp is too large.
618  */
619 int
620 GNUNET_PSYC_join_decision (struct GNUNET_PSYC_JoinHandle *jh,
621                            int is_admitted,
622                            uint32_t relay_count,
623                            const struct GNUNET_PeerIdentity *relays,
624                            const struct GNUNET_PSYC_Message *join_resp);
625
626
627 /**
628  * Handle for the master of a PSYC channel.
629  */
630 struct GNUNET_PSYC_Master;
631
632
633 /**
634  * Function called after connected to the PSYC service
635  * and the channel master started.
636  *
637  * Also called when reconnected to the service
638  * after the connection closed unexpectedly.
639  *
640  * @param cls
641  *        Closure.
642  * @param result
643  *        #GNUNET_YES if there were already messages sent to the channel,
644  *        #GNUNET_NO  if the message history is empty,
645  *        #GNUNET_SYSERR on error.
646  * @param max_message_id
647  *        Last message ID sent to the channel.
648  */
649 typedef void
650 (*GNUNET_PSYC_MasterStartCallback) (void *cls, int result,
651                                     uint64_t max_message_id);
652
653
654 /**
655  * Start a PSYC master channel.
656  *
657  * Will start a multicast group identified by the given ECC key.  Messages
658  * received from group members will be given to the respective handler methods.
659  * If a new member wants to join a group, the "join" method handler will be
660  * invoked; the join handler must then generate a "join" message to approve the
661  * joining of the new member.  The channel can also change group membership
662  * without explicit requests.  Note that PSYC doesn't itself "understand" join
663  * or part messages, the respective methods must call other PSYC functions to
664  * inform PSYC about the meaning of the respective events.
665  *
666  * @param cfg  Configuration to use (to connect to PSYC service).
667  * @param channel_key  ECC key that will be used to sign messages for this
668  *        PSYC session. The public key is used to identify the PSYC channel.
669  *        Note that end-users will usually not use the private key directly, but
670  *        rather look it up in GNS for places managed by other users, or select
671  *        a file with the private key(s) when setting up their own channels
672  *        FIXME: we'll likely want to use NOT the p521 curve here, but a cheaper
673  *        one in the future.
674  * @param policy  Channel policy specifying join and history restrictions.
675  *        Used to automate join decisions.
676  * @param master_start_cb  Function to invoke after the channel master started.
677  * @param join_request_cb  Function to invoke when a slave wants to join.
678  * @param message_cb  Function to invoke on message parts sent to the channel
679  *        and received from slaves
680  * @param cls  Closure for @a method and @a join_cb.
681  *
682  * @return Handle for the channel master, NULL on error.
683  */
684 struct GNUNET_PSYC_Master *
685 GNUNET_PSYC_master_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
686                           const struct GNUNET_CRYPTO_EddsaPrivateKey *channel_key,
687                           enum GNUNET_PSYC_Policy policy,
688                           GNUNET_PSYC_MasterStartCallback master_start_cb,
689                           GNUNET_PSYC_JoinRequestCallback join_request_cb,
690                           GNUNET_PSYC_MessageCallback message_cb,
691                           GNUNET_PSYC_MessagePartCallback message_part_cb,
692                           void *cls);
693
694
695 /**
696  * Function called to provide data for a transmission via PSYC.
697  *
698  * Note that returning #GNUNET_YES or #GNUNET_SYSERR (but not #GNUNET_NO)
699  * invalidates the respective transmission handle.
700  *
701  * @param cls Closure.
702  * @param[in,out] data_size Initially set to the number of bytes available in
703  *        @a data, should be set to the number of bytes written to data.
704  * @param[out] data Where to write the body of the message to give to the
705  *         method. The function must copy at most @a data_size bytes to @a data.
706  * @return #GNUNET_SYSERR on error (fatal, aborts transmission)
707  *         #GNUNET_NO on success, if more data is to be transmitted later.
708  *         Should be used if @a data_size was not big enough to take all the
709  *         data.  If 0 is returned in @a data_size the transmission is paused,
710  *         and can be resumed with GNUNET_PSYC_master_transmit_resume().
711  *         #GNUNET_YES if this completes the transmission (all data supplied)
712  */
713 typedef int
714 (*GNUNET_PSYC_TransmitNotifyData) (void *cls,
715                                    uint16_t *data_size,
716                                    void *data);
717
718 /**
719  * Function called to provide a modifier for a transmission via PSYC.
720  *
721  * Note that returning #GNUNET_YES or #GNUNET_SYSERR (but not #GNUNET_NO)
722  * invalidates the respective transmission handle.
723  *
724  * @param cls Closure.
725  * @param[in,out] data_size  Initially set to the number of bytes available in
726  *         @a data, should be set to the number of bytes written to data.
727  * @param[out] data  Where to write the modifier's name and value.
728  *         The function must copy at most @a data_size bytes to @a data.
729  *         When this callback is first called for a modifier, @a data should
730  *         contain: "name\0value".  If the whole value does not fit, subsequent
731  *         calls to this function should write continuations of the value to
732  *         @a data.
733  * @param[out] oper  Where to write the operator of the modifier.
734  *         Only needed during the first call to this callback at the beginning
735  *         of the modifier.  In case of subsequent calls asking for value
736  *         continuations @a oper is set to #NULL.
737  * @param[out] full_value_size  Where to write the full size of the value.
738  *         Only needed during the first call to this callback at the beginning
739  *         of the modifier.  In case of subsequent calls asking for value
740  *         continuations @a value_size is set to #NULL.
741  * @return #GNUNET_SYSERR on error (fatal, aborts transmission)
742  *         #GNUNET_NO on success, if more data is to be transmitted later.
743  *         Should be used if @a data_size was not big enough to take all the
744  *         data for the modifier's value (the name must be always returned
745  *         during the first call to this callback).
746  *         If 0 is returned in @a data_size the transmission is paused,
747  *         and can be resumed with GNUNET_PSYC_master_transmit_resume().
748  *         #GNUNET_YES if this completes the modifier (the whole value is supplied).
749  */
750 typedef int
751 (*GNUNET_PSYC_TransmitNotifyModifier) (void *cls,
752                                        uint16_t *data_size,
753                                        void *data,
754                                        uint8_t *oper,
755                                        uint32_t *full_value_size);
756
757 /**
758  * Flags for transmitting messages to a channel by the master.
759  */
760 enum GNUNET_PSYC_MasterTransmitFlags
761 {
762   GNUNET_PSYC_MASTER_TRANSMIT_NONE = 0,
763
764   /**
765    * Whether this message should reset the channel state,
766    * i.e. remove all previously stored state variables.
767    */
768
769   GNUNET_PSYC_MASTER_TRANSMIT_STATE_RESET = 1 << 0,
770
771   /**
772    * Whether this message contains any state modifiers.
773    */
774   GNUNET_PSYC_MASTER_TRANSMIT_STATE_MODIFY = 1 << 1,
775
776   /**
777    * Add PSYC header variable with the hash of the current channel state.
778    */
779   GNUNET_PSYC_MASTER_TRANSMIT_STATE_HASH = 1 << 2,
780
781   /**
782    * Whether we need to increment the group generation counter after
783    * transmitting this message.
784    */
785   GNUNET_PSYC_MASTER_TRANSMIT_INC_GROUP_GEN = 1 << 3
786 };
787
788
789 /**
790  * Handle for a pending PSYC transmission operation.
791  */
792 struct GNUNET_PSYC_MasterTransmitHandle;
793
794
795 /**
796  * Send a message to call a method to all members in the PSYC channel.
797  *
798  * @param master Handle to the PSYC channel.
799  * @param method_name Which method should be invoked.
800  * @param notify_mod Function to call to obtain modifiers.
801  * @param notify_data Function to call to obtain fragments of the data.
802  * @param notify_cls Closure for @a notify_mod and @a notify_data.
803  * @param flags Flags for the message being transmitted.
804  * @return Transmission handle, NULL on error (i.e. more than one request queued).
805  */
806 struct GNUNET_PSYC_MasterTransmitHandle *
807 GNUNET_PSYC_master_transmit (struct GNUNET_PSYC_Master *master,
808                              const char *method_name,
809                              GNUNET_PSYC_TransmitNotifyModifier notify_mod,
810                              GNUNET_PSYC_TransmitNotifyData notify_data,
811                              void *notify_cls,
812                              enum GNUNET_PSYC_MasterTransmitFlags flags);
813
814
815 /**
816  * Resume transmission to the channel.
817  *
818  * @param th Handle of the request that is being resumed.
819  */
820 void
821 GNUNET_PSYC_master_transmit_resume (struct GNUNET_PSYC_MasterTransmitHandle *th);
822
823
824 /**
825  * Abort transmission request to channel.
826  *
827  * @param th Handle of the request that is being aborted.
828  */
829 void
830 GNUNET_PSYC_master_transmit_cancel (struct GNUNET_PSYC_MasterTransmitHandle *th);
831
832
833 /**
834  * Stop a PSYC master channel.
835  *
836  * @param master
837  *        PSYC channel master to stop.
838  * @param keep_active
839  *        Keep place active after last application disconnected.
840  * @param stop_cb
841  *        Function called after the master stopped
842  *        and disconnected from the psyc service.
843  * @param stop_cls
844  *        Closure for @a part_cb.
845  */
846 void
847 GNUNET_PSYC_master_stop (struct GNUNET_PSYC_Master *master,
848                          int keep_active,
849                          GNUNET_ContinuationCallback stop_cb,
850                          void *stop_cls);
851
852
853 /**
854  * Handle for a PSYC channel slave.
855  */
856 struct GNUNET_PSYC_Slave;
857
858
859 /**
860  * Function called after the slave connected to the PSYC service.
861  *
862  * Also called when reconnected to the service
863  * after the connection closed unexpectedly.
864  *
865  * @param cls
866  *        Closure.
867  * @param result
868  *        #GNUNET_YES if there were already messages sent to the channel,
869  *        #GNUNET_NO  if the message history is empty,
870  *        #GNUNET_SYSERR on error.
871  * @param max_message_id
872  *        Last message ID sent to the channel.
873  */
874 typedef void
875 (*GNUNET_PSYC_SlaveConnectCallback) (void *cls, int result,
876                                      uint64_t max_message_id);
877
878
879 /**
880  * Method called to inform about the decision in response to a join request.
881  *
882  * If @a is_admitted is not #GNUNET_YES, then sending messages to the channel is
883  * not possible, but earlier history can be still queried.
884  *
885  * @param cls  Closure.
886  * @param is_admitted  #GNUNET_YES or #GNUNET_NO or #GNUNET_SYSERR
887  * @param join_msg  Application-dependent join message from the origin.
888  */
889 typedef void
890 (*GNUNET_PSYC_JoinDecisionCallback) (void *cls,
891                                      const struct GNUNET_PSYC_JoinDecisionMessage *dcsn,
892                                      int is_admitted,
893                                      const struct GNUNET_PSYC_Message *join_msg);
894
895 /**
896  * Flags for GNUNET_PSYC_slave_join()
897  */
898 enum GNUNET_PSYC_SlaveJoinFlags
899 {
900   GNUNET_PSYC_SLAVE_JOIN_NONE   = 0,
901
902   /**
903    * Local join for history access, no network connection is established.
904    */
905   GNUNET_PSYC_SLAVE_JOIN_LOCAL  = 1,
906 };
907
908
909 /**
910  * Join a PSYC channel.
911  *
912  * The entity joining is always the local peer.  The user must immediately use
913  * the GNUNET_PSYC_slave_transmit() functions to transmit a @e join_msg to the
914  * channel; if the join request succeeds, the channel state (and @e recent
915  * method calls) will be replayed to the joining member.  There is no explicit
916  * notification on failure (as the channel may simply take days to approve,
917  * and disapproval is simply being ignored).
918  *
919  * @param cfg
920  *        Configuration to use.
921  * @param channel_pub_key
922  *        ECC public key that identifies the channel we wish to join.
923  * @param slave_key
924  *        ECC private-public key pair that identifies the slave, and
925  *        used by multicast to sign the join request and subsequent unicast
926  *        requests sent to the master.
927  * @param flags
928  *        Join flags.
929  * @param origin
930  *        Peer identity of the origin.
931  * @param relay_count
932  *        Number of peers in the @a relays array.
933  * @param relays
934  *        Peer identities of members of the multicast group, which serve
935  *        as relays and used to join the group at.
936  * @param message_cb
937  *        Function to invoke on message fragments received from the channel.
938  * @param message_part_cb
939  *        Function to invoke on message parts received from the channel.
940  * @param slave_connect_cb
941  *        Function invoked once we have connected to the PSYC service.
942  * @param join_decision_cb
943  *        Function invoked once we have received a join decision.
944  * @param cls
945  *        Closure for @a message_cb and @a slave_joined_cb.
946  * @param join_msg
947  *        Join message.
948  *
949  * @return Handle for the slave, NULL on error.
950  */
951 struct GNUNET_PSYC_Slave *
952 GNUNET_PSYC_slave_join (const struct GNUNET_CONFIGURATION_Handle *cfg,
953                         const struct GNUNET_CRYPTO_EddsaPublicKey *channel_pub_key,
954                         const struct GNUNET_CRYPTO_EcdsaPrivateKey *slave_key,
955                         enum GNUNET_PSYC_SlaveJoinFlags flags,
956                         const struct GNUNET_PeerIdentity *origin,
957                         uint32_t relay_count,
958                         const struct GNUNET_PeerIdentity *relays,
959                         GNUNET_PSYC_MessageCallback message_cb,
960                         GNUNET_PSYC_MessagePartCallback message_part_cb,
961                         GNUNET_PSYC_SlaveConnectCallback slave_connect_cb,
962                         GNUNET_PSYC_JoinDecisionCallback join_decision_cb,
963                         void *cls,
964                         const struct GNUNET_PSYC_Message *join_msg);
965
966
967 /**
968  * Part a PSYC channel.
969  *
970  * Will terminate the connection to the PSYC service.  Polite clients should
971  * first explicitly send a part request (via GNUNET_PSYC_slave_transmit()).
972  *
973  * @param slave
974  *        Slave handle.
975  * @param keep_active
976  *        Keep place active after last application disconnected.
977  * @param part_cb
978  *        Function called after the slave parted the channel
979  *        and disconnected from the psyc service.
980  * @param part_cls
981  *        Closure for @a part_cb.
982  */
983 void
984 GNUNET_PSYC_slave_part (struct GNUNET_PSYC_Slave *slave,
985                         int keep_active,
986                         GNUNET_ContinuationCallback part_cb,
987                         void *part_cls);
988
989
990 /**
991  * Flags for transmitting messages to the channel master by a slave.
992  */
993 enum GNUNET_PSYC_SlaveTransmitFlags
994 {
995   GNUNET_PSYC_SLAVE_TRANSMIT_NONE = 0
996 };
997
998
999 /**
1000  * Handle for a pending PSYC transmission operation.
1001  */
1002 struct GNUNET_PSYC_SlaveTransmitHandle;
1003
1004
1005 /**
1006  * Request a message to be sent to the channel master.
1007  *
1008  * @param slave Slave handle.
1009  * @param method_name Which (PSYC) method should be invoked (on host).
1010  * @param notify_mod Function to call to obtain modifiers.
1011  * @param notify_data Function to call to obtain fragments of the data.
1012  * @param notify_cls Closure for @a notify.
1013  * @param flags Flags for the message being transmitted.
1014  * @return Transmission handle, NULL on error (i.e. more than one request queued).
1015  */
1016 struct GNUNET_PSYC_SlaveTransmitHandle *
1017 GNUNET_PSYC_slave_transmit (struct GNUNET_PSYC_Slave *slave,
1018                             const char *method_name,
1019                             GNUNET_PSYC_TransmitNotifyModifier notify_mod,
1020                             GNUNET_PSYC_TransmitNotifyData notify_data,
1021                             void *notify_cls,
1022                             enum GNUNET_PSYC_SlaveTransmitFlags flags);
1023
1024
1025 /**
1026  * Resume transmission to the master.
1027  *
1028  * @param th Handle of the request that is being resumed.
1029  */
1030 void
1031 GNUNET_PSYC_slave_transmit_resume (struct GNUNET_PSYC_SlaveTransmitHandle *th);
1032
1033
1034 /**
1035  * Abort transmission request to master.
1036  *
1037  * @param th Handle of the request that is being aborted.
1038  */
1039 void
1040 GNUNET_PSYC_slave_transmit_cancel (struct GNUNET_PSYC_SlaveTransmitHandle *th);
1041
1042
1043 /**
1044  * Handle to access PSYC channel operations for both the master and slaves.
1045  */
1046 struct GNUNET_PSYC_Channel;
1047
1048
1049 /**
1050  * Convert a channel @a master to a @e channel handle to access the @e channel
1051  * APIs.
1052  *
1053  * @param master Channel master handle.
1054  * @return Channel handle, valid for as long as @a master is valid.
1055  */
1056 struct GNUNET_PSYC_Channel *
1057 GNUNET_PSYC_master_get_channel (struct GNUNET_PSYC_Master *master);
1058
1059
1060 /**
1061  * Convert @a slave to a @e channel handle to access the @e channel APIs.
1062  *
1063  * @param slave Slave handle.
1064  * @return Channel handle, valid for as long as @a slave is valid.
1065  */
1066 struct GNUNET_PSYC_Channel *
1067 GNUNET_PSYC_slave_get_channel (struct GNUNET_PSYC_Slave *slave);
1068
1069
1070 /**
1071  * Add a slave to the channel's membership list.
1072  *
1073  * Note that this will NOT generate any PSYC traffic, it will merely update the
1074  * local database to modify how we react to <em>membership test</em> queries.
1075  * The channel master still needs to explicitly transmit a @e join message to
1076  * notify other channel members and they then also must still call this function
1077  * in their respective methods handling the @e join message.  This way, how @e
1078  * join and @e part operations are exactly implemented is still up to the
1079  * application; for example, there might be a @e part_all method to kick out
1080  * everyone.
1081  *
1082  * Note that channel slaves are explicitly trusted to execute such methods
1083  * correctly; not doing so correctly will result in either denying other slaves
1084  * access or offering access to channel data to non-members.
1085  *
1086  * @param channel
1087  *        Channel handle.
1088  * @param slave_key
1089  *        Identity of channel slave to add.
1090  * @param announced_at
1091  *        ID of the message that announced the membership change.
1092  * @param effective_since
1093  *        Addition of slave is in effect since this message ID.
1094  * @param result_cb
1095  *        Function to call with the result of the operation.
1096  *        The @e result_code argument is #GNUNET_OK on success, or
1097  *        #GNUNET_SYSERR on error.  In case of an error, the @e data argument
1098  *        can contain an optional error message.
1099  * @param cls
1100  *        Closure for @a result_cb.
1101  */
1102 void
1103 GNUNET_PSYC_channel_slave_add (struct GNUNET_PSYC_Channel *channel,
1104                                const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
1105                                uint64_t announced_at,
1106                                uint64_t effective_since,
1107                                GNUNET_ResultCallback result_cb,
1108                                void *cls);
1109
1110
1111 /**
1112  * Remove a slave from the channel's membership list.
1113  *
1114  * Note that this will NOT generate any PSYC traffic, it will merely update the
1115  * local database to modify how we react to <em>membership test</em> queries.
1116  * The channel master still needs to explicitly transmit a @e part message to
1117  * notify other channel members and they then also must still call this function
1118  * in their respective methods handling the @e part message.  This way, how
1119  * @e join and @e part operations are exactly implemented is still up to the
1120  * application; for example, there might be a @e part_all message to kick out
1121  * everyone.
1122  *
1123  * Note that channel members are explicitly trusted to perform these
1124  * operations correctly; not doing so correctly will result in either
1125  * denying members access or offering access to channel data to
1126  * non-members.
1127  *
1128  * @param channel
1129  *        Channel handle.
1130  * @param slave_key
1131  *        Identity of channel slave to remove.
1132  * @param announced_at
1133  *        ID of the message that announced the membership change.
1134  * @param result_cb
1135  *        Function to call with the result of the operation.
1136  *        The @e result_code argument is #GNUNET_OK on success, or
1137  *        #GNUNET_SYSERR on error.  In case of an error, the @e data argument
1138  *        can contain an optional error message.
1139  * @param cls
1140  *        Closure for @a result_cb.
1141  */
1142 void
1143 GNUNET_PSYC_channel_slave_remove (struct GNUNET_PSYC_Channel *channel,
1144                                   const struct GNUNET_CRYPTO_EcdsaPublicKey
1145                                   *slave_key,
1146                                   uint64_t announced_at,
1147                                   GNUNET_ResultCallback result_cb,
1148                                   void *cls);
1149
1150
1151 /**
1152  * History request handle.
1153  */
1154 struct GNUNET_PSYC_HistoryRequest;
1155
1156
1157 /**
1158  * Request to replay a part of the message history of the channel.
1159  *
1160  * Historic messages (but NOT the state at the time) will be replayed (given to
1161  * the normal method handlers) if available and if access is permitted.
1162  *
1163  * @param channel
1164  *        Which channel should be replayed?
1165  * @param start_message_id
1166  *        Earliest interesting point in history.
1167  * @param end_message_id
1168  *        Last (inclusive) interesting point in history.
1169  * @param method_prefix
1170  *        Retrieve only messages with a matching method prefix.
1171  * @param flags
1172  *        OR'ed enum GNUNET_PSYC_HistoryReplayFlags
1173  * @param result_cb
1174  *        Function to call when the requested history has been fully replayed.
1175  *        Once this function has been called, the client must not call
1176  *        GNUNET_PSYC_channel_history_replay_cancel() anymore.
1177  * @param cls
1178  *        Closure for the callbacks.
1179  *
1180  * @return Handle to cancel history replay operation.
1181  */
1182 struct GNUNET_PSYC_HistoryRequest *
1183 GNUNET_PSYC_channel_history_replay (struct GNUNET_PSYC_Channel *channel,
1184                                     uint64_t start_message_id,
1185                                     uint64_t end_message_id,
1186                                     const char *method_prefix,
1187                                     uint32_t flags,
1188                                     GNUNET_PSYC_MessageCallback message_cb,
1189                                     GNUNET_PSYC_MessagePartCallback message_part_cb,
1190                                     GNUNET_ResultCallback result_cb,
1191                                     void *cls);
1192
1193
1194 /**
1195  * Request to replay the latest messages from the message history of the channel.
1196  *
1197  * Historic messages (but NOT the state at the time) will be replayed (given to
1198  * the normal method handlers) if available and if access is permitted.
1199  *
1200  * @param channel
1201  *        Which channel should be replayed?
1202  * @param message_limit
1203  *        Maximum number of messages to replay.
1204  * @param flags
1205  *        OR'ed enum GNUNET_PSYC_HistoryReplayFlags
1206  * @param finish_cb
1207  *        Function to call when the requested history has been fully replayed
1208  *        (counting message IDs might not suffice, as some messages might be
1209  *        secret and thus the listener would not know the story is finished
1210  *        without being told explicitly)o once this function has been called, the
1211  *        client must not call GNUNET_PSYC_channel_history_replay_cancel() anymore.
1212  * @param cls
1213  *        Closure for the callbacks.
1214  *
1215  * @return Handle to cancel history replay operation.
1216  */
1217 struct GNUNET_PSYC_HistoryRequest *
1218 GNUNET_PSYC_channel_history_replay_latest (struct GNUNET_PSYC_Channel *channel,
1219                                            uint64_t message_limit,
1220                                            const char *method_prefix,
1221                                            uint32_t flags,
1222                                            GNUNET_PSYC_MessageCallback message_cb,
1223                                            GNUNET_PSYC_MessagePartCallback message_part_cb,
1224                                            GNUNET_ResultCallback result_cb,
1225                                            void *cls);
1226
1227
1228 void
1229 GNUNET_PSYC_channel_history_replay_cancel (struct GNUNET_PSYC_Channel *channel,
1230                                            struct GNUNET_PSYC_HistoryRequest *hr);
1231
1232
1233 /**
1234  * Function called to inform a member about stored state values for a channel.
1235  *
1236  * If @a full_value_size > value_size then this function is called multiple
1237  * times until the whole value arrived.
1238  *
1239  * @param cls
1240  *        Closure.
1241  * @param name
1242  *        Name of the state variable.
1243  *        NULL if there are no more state variables to be returned.
1244  * @param value
1245  *        Value of the state variable.
1246  * @param value_size
1247  *        Number of bytes in @a value.
1248  * @param full_value_size
1249  *        Number of bytes in the full value, including continuations.
1250  *        Only set for the first part of a variable,
1251  *        in case of a continuation it is 0.
1252  */
1253 typedef void
1254 (*GNUNET_PSYC_StateVarCallback) (void *cls,
1255                                  const struct GNUNET_MessageHeader *mod,
1256                                  const char *name,
1257                                  const void *value,
1258                                  uint32_t value_size,
1259                                  uint32_t full_value_size);
1260
1261
1262 /**
1263  * State request handle.
1264  */
1265 struct GNUNET_PSYC_StateRequest;
1266
1267
1268 /**
1269  * Retrieve the best matching channel state variable.
1270  *
1271  * If the requested variable name is not present in the state, the nearest
1272  * less-specific name is matched; for example, requesting "_a_b" will match "_a"
1273  * if "_a_b" does not exist.
1274  *
1275  * @param channel
1276  *        Channel handle.
1277  * @param full_name
1278  *        Full name of the requested variable.
1279  *        The actual variable returned might have a shorter name.
1280  * @param var_cb
1281  *        Function called once when a matching state variable is found.
1282  *        Not called if there's no matching state variable.
1283  * @param result_cb
1284  *        Function called after the operation finished.
1285  *        (i.e. all state variables have been returned via @a state_cb)
1286  * @param cls
1287  *        Closure for the callbacks.
1288  */
1289 struct GNUNET_PSYC_StateRequest *
1290 GNUNET_PSYC_channel_state_get (struct GNUNET_PSYC_Channel *channel,
1291                                const char *full_name,
1292                                GNUNET_PSYC_StateVarCallback var_cb,
1293                                GNUNET_ResultCallback result_cb,
1294                                void *cls);
1295
1296
1297 /**
1298  * Return all channel state variables whose name matches a given prefix.
1299  *
1300  * A name matches if it starts with the given @a name_prefix, thus requesting
1301  * the empty prefix ("") will match all values; requesting "_a_b" will also
1302  * return values stored under "_a_b_c".
1303  *
1304  * The @a state_cb is invoked on all matching state variables asynchronously, as
1305  * the state is stored in and retrieved from the PSYCstore,
1306  *
1307  * @param channel
1308  *        Channel handle.
1309  * @param name_prefix
1310  *        Prefix of the state variable name to match.
1311  * @param var_cb
1312  *        Function called once when a matching state variable is found.
1313  *        Not called if there's no matching state variable.
1314  * @param result_cb
1315  *        Function called after the operation finished.
1316  *        (i.e. all state variables have been returned via @a state_cb)
1317  * @param cls
1318  *        Closure for the callbacks.
1319  */
1320 struct GNUNET_PSYC_StateRequest *
1321 GNUNET_PSYC_channel_state_get_prefix (struct GNUNET_PSYC_Channel *channel,
1322                                       const char *name_prefix,
1323                                       GNUNET_PSYC_StateVarCallback var_cb,
1324                                       GNUNET_ResultCallback result_cb,
1325                                       void *cls);
1326
1327 /**
1328  * Cancel a state request operation.
1329  *
1330  * @param sr
1331  *        Handle for the operation to cancel.
1332  */
1333 void
1334 GNUNET_PSYC_channel_state_get_cancel (struct GNUNET_PSYC_StateRequest *sr);
1335
1336
1337
1338 #if 0                           /* keep Emacsens' auto-indent happy */
1339 {
1340 #endif
1341 #ifdef __cplusplus
1342 }
1343 #endif
1344
1345 /* ifndef GNUNET_PSYC_SERVICE_H */
1346 #endif
1347
1348 /** @} */  /* end of group */