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