-indent, doxygen
[oweals/gnunet.git] / src / psyc / psyc_api.c
1 /*
2  * This file is part of GNUnet
3  * (C) 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., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /**
22  * @file psyc/psyc_api.c
23  * @brief PSYC service; high-level access to the PSYC protocol
24  *        note that clients of this API are NOT expected to
25  *        understand the PSYC message format, only the semantics!
26  *        Parsing (and serializing) the PSYC stream format is done
27  *        within the implementation of the libgnunetpsyc library,
28  *        and this API deliberately exposes as little as possible
29  *        of the actual data stream format to the application!
30  * @author Gabor X Toth
31  */
32
33 #include <inttypes.h>
34
35 #include "platform.h"
36 #include "gnunet_util_lib.h"
37 #include "gnunet_env_lib.h"
38 #include "gnunet_multicast_service.h"
39 #include "gnunet_psyc_service.h"
40 #include "gnunet_psyc_util_lib.h"
41 #include "psyc.h"
42
43 #define LOG(kind,...) GNUNET_log_from (kind, "psyc-api",__VA_ARGS__)
44
45
46 /**
47  * Handle to access PSYC channel operations for both the master and slaves.
48  */
49 struct GNUNET_PSYC_Channel
50 {
51   /**
52    * Configuration to use.
53    */
54   const struct GNUNET_CONFIGURATION_Handle *cfg;
55
56   /**
57    * Client connection to the service.
58    */
59   struct GNUNET_CLIENT_MANAGER_Connection *client;
60
61   /**
62    * Transmission handle;
63    */
64   struct GNUNET_PSYC_TransmitHandle *tmit;
65
66   /**
67    * Receipt handle;
68    */
69   struct GNUNET_PSYC_ReceiveHandle *recv;
70
71   /**
72    * Message to send on reconnect.
73    */
74   struct GNUNET_MessageHeader *connect_msg;
75
76   /**
77    * Are we polling for incoming messages right now?
78    */
79   uint8_t in_receive;
80
81   /**
82    * Is this a master or slave channel?
83    */
84   uint8_t is_master;
85 };
86
87
88 /**
89  * Handle for the master of a PSYC channel.
90  */
91 struct GNUNET_PSYC_Master
92 {
93   struct GNUNET_PSYC_Channel chn;
94
95   GNUNET_PSYC_MasterStartCallback start_cb;
96
97   /**
98    * Join request callback.
99    */
100   GNUNET_PSYC_JoinRequestCallback join_req_cb;
101
102   /**
103    * Closure for the callbacks.
104    */
105   void *cb_cls;
106 };
107
108
109 /**
110  * Handle for a PSYC channel slave.
111  */
112 struct GNUNET_PSYC_Slave
113 {
114   struct GNUNET_PSYC_Channel chn;
115
116   GNUNET_PSYC_SlaveConnectCallback connect_cb;
117
118   GNUNET_PSYC_JoinDecisionCallback join_dcsn_cb;
119
120   /**
121    * Closure for the callbacks.
122    */
123   void *cb_cls;
124 };
125
126
127 /**
128  * Handle that identifies a join request.
129  *
130  * Used to match calls to #GNUNET_PSYC_JoinRequestCallback to the
131  * corresponding calls to GNUNET_PSYC_join_decision().
132  */
133 struct GNUNET_PSYC_JoinHandle
134 {
135   struct GNUNET_PSYC_Master *mst;
136   struct GNUNET_CRYPTO_EddsaPublicKey slave_key;
137 };
138
139
140 /**
141  * Handle for a pending PSYC transmission operation.
142  */
143 struct GNUNET_PSYC_SlaveTransmitHandle
144 {
145
146 };
147
148
149 /**
150  * Handle to a story telling operation.
151  */
152 struct GNUNET_PSYC_Story
153 {
154
155 };
156
157
158 /**
159  * Handle for a state query operation.
160  */
161 struct GNUNET_PSYC_StateQuery
162 {
163
164 };
165
166
167 static void
168 channel_send_connect_msg (struct GNUNET_PSYC_Channel *chn)
169 {
170   uint16_t cmsg_size = ntohs (chn->connect_msg->size);
171   struct GNUNET_MessageHeader * cmsg = GNUNET_malloc (cmsg_size);
172   memcpy (cmsg, chn->connect_msg, cmsg_size);
173   GNUNET_CLIENT_MANAGER_transmit_now (chn->client, cmsg);
174 }
175
176
177 static void
178 channel_recv_disconnect (void *cls,
179                          struct GNUNET_CLIENT_MANAGER_Connection *client,
180                          const struct GNUNET_MessageHeader *msg)
181 {
182   struct GNUNET_PSYC_Channel *
183     chn = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*chn));
184   GNUNET_CLIENT_MANAGER_reconnect (client);
185   channel_send_connect_msg (chn);
186 }
187
188
189 static void
190 channel_recv_message (void *cls,
191                       struct GNUNET_CLIENT_MANAGER_Connection *client,
192                       const struct GNUNET_MessageHeader *msg)
193 {
194   struct GNUNET_PSYC_Channel *
195     chn = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*chn));
196   GNUNET_PSYC_receive_message (chn->recv,
197                                (const struct GNUNET_PSYC_MessageHeader *) msg);
198 }
199
200
201 static void
202 channel_recv_message_ack (void *cls,
203                           struct GNUNET_CLIENT_MANAGER_Connection *client,
204                           const struct GNUNET_MessageHeader *msg)
205 {
206   struct GNUNET_PSYC_Channel *
207     chn = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*chn));
208   GNUNET_PSYC_transmit_got_ack (chn->tmit);
209 }
210
211
212 static void
213 master_recv_start_ack (void *cls,
214                        struct GNUNET_CLIENT_MANAGER_Connection *client,
215                        const struct GNUNET_MessageHeader *msg)
216 {
217   struct GNUNET_PSYC_Master *
218     mst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
219                                                    sizeof (struct GNUNET_PSYC_Channel));
220
221   struct CountersResult *cres = (struct CountersResult *) msg;
222   if (NULL != mst->start_cb)
223     mst->start_cb (mst->cb_cls, GNUNET_ntohll (cres->max_message_id));
224 }
225
226
227 static void
228 master_recv_join_request (void *cls,
229                           struct GNUNET_CLIENT_MANAGER_Connection *client,
230                           const struct GNUNET_MessageHeader *msg)
231 {
232   struct GNUNET_PSYC_Master *
233     mst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
234                                                    sizeof (struct GNUNET_PSYC_Channel));
235
236   const struct MasterJoinRequest *req = (const struct MasterJoinRequest *) msg;
237
238   struct GNUNET_PSYC_MessageHeader *pmsg = NULL;
239   if (ntohs (req->header.size) <= sizeof (*req) + sizeof (*pmsg))
240     pmsg = (struct GNUNET_PSYC_MessageHeader *) &req[1];
241
242   struct GNUNET_PSYC_JoinHandle *jh = GNUNET_malloc (sizeof (*jh));
243   jh->mst = mst;
244   jh->slave_key = req->slave_key;
245
246   if (NULL != mst->join_req_cb)
247     mst->join_req_cb (mst->cb_cls, &req->slave_key, pmsg, jh);
248 }
249
250
251 static void
252 slave_recv_join_ack (void *cls,
253                      struct GNUNET_CLIENT_MANAGER_Connection *client,
254                      const struct GNUNET_MessageHeader *msg)
255 {
256   struct GNUNET_PSYC_Slave *
257     slv = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
258                                                    sizeof (struct GNUNET_PSYC_Channel));
259   struct CountersResult *cres = (struct CountersResult *) msg;
260   if (NULL != slv->connect_cb)
261     slv->connect_cb (slv->cb_cls, GNUNET_ntohll (cres->max_message_id));
262 }
263
264
265 static void
266 slave_recv_join_decision (void *cls,
267                           struct GNUNET_CLIENT_MANAGER_Connection *client,
268                           const struct GNUNET_MessageHeader *msg)
269 {
270   struct GNUNET_PSYC_Slave *
271     slv = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
272                                                    sizeof (struct GNUNET_PSYC_Channel));
273   const struct SlaveJoinDecision *
274     dcsn = (const struct SlaveJoinDecision *) msg;
275
276   struct GNUNET_PSYC_MessageHeader *pmsg = NULL;
277   if (ntohs (dcsn->header.size) <= sizeof (*dcsn) + sizeof (*pmsg))
278     pmsg = (struct GNUNET_PSYC_MessageHeader *) &dcsn[1];
279
280   struct GNUNET_PSYC_JoinHandle *jh = GNUNET_malloc (sizeof (*jh));
281   if (NULL != slv->join_dcsn_cb)
282     slv->join_dcsn_cb (slv->cb_cls, ntohl (dcsn->is_admitted), pmsg);
283 }
284
285
286 static struct GNUNET_CLIENT_MANAGER_MessageHandler master_handlers[] =
287 {
288   { &channel_recv_disconnect, NULL, 0, 0, GNUNET_NO },
289
290   { &channel_recv_message, NULL,
291     GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
292     sizeof (struct GNUNET_PSYC_MessageHeader), GNUNET_YES },
293
294   { &channel_recv_message_ack, NULL,
295     GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
296     sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
297
298   { &master_recv_start_ack, NULL,
299     GNUNET_MESSAGE_TYPE_PSYC_MASTER_START_ACK,
300     sizeof (struct CountersResult), GNUNET_NO },
301
302   { &master_recv_join_request, NULL,
303     GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
304     sizeof (struct MasterJoinRequest), GNUNET_YES },
305
306   { NULL, NULL, 0, 0, GNUNET_NO }
307 };
308
309
310 static struct GNUNET_CLIENT_MANAGER_MessageHandler slave_handlers[] =
311 {
312   { &channel_recv_disconnect, NULL, 0, 0, GNUNET_NO },
313
314   { &channel_recv_message, NULL,
315     GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
316     sizeof (struct GNUNET_PSYC_MessageHeader), GNUNET_YES },
317
318   { &channel_recv_message_ack, NULL,
319     GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
320     sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
321
322   { &slave_recv_join_ack, NULL,
323     GNUNET_MESSAGE_TYPE_PSYC_SLAVE_JOIN_ACK,
324     sizeof (struct CountersResult), GNUNET_NO },
325
326   { &slave_recv_join_decision, NULL,
327     GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION,
328     sizeof (struct SlaveJoinDecision), GNUNET_YES },
329
330   { NULL, NULL, 0, 0, GNUNET_NO }
331 };
332
333
334 /**
335  * Start a PSYC master channel.
336  *
337  * Will start a multicast group identified by the given ECC key.  Messages
338  * received from group members will be given to the respective handler methods.
339  * If a new member wants to join a group, the "join" method handler will be
340  * invoked; the join handler must then generate a "join" message to approve the
341  * joining of the new member.  The channel can also change group membership
342  * without explicit requests.  Note that PSYC doesn't itself "understand" join
343  * or part messages, the respective methods must call other PSYC functions to
344  * inform PSYC about the meaning of the respective events.
345  *
346  * @param cfg  Configuration to use (to connect to PSYC service).
347  * @param channel_key  ECC key that will be used to sign messages for this
348  *        PSYC session. The public key is used to identify the PSYC channel.
349  *        Note that end-users will usually not use the private key directly, but
350  *        rather look it up in GNS for places managed by other users, or select
351  *        a file with the private key(s) when setting up their own channels
352  *        FIXME: we'll likely want to use NOT the p521 curve here, but a cheaper
353  *        one in the future.
354  * @param policy  Channel policy specifying join and history restrictions.
355  *        Used to automate join decisions.
356  * @param message_cb  Function to invoke on message parts received from slaves.
357  * @param join_request_cb  Function to invoke when a slave wants to join.
358  * @param master_start_cb  Function to invoke after the channel master started.
359  * @param cls  Closure for @a method and @a join_cb.
360  *
361  * @return Handle for the channel master, NULL on error.
362  */
363 struct GNUNET_PSYC_Master *
364 GNUNET_PSYC_master_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
365                           const struct GNUNET_CRYPTO_EddsaPrivateKey *channel_key,
366                           enum GNUNET_PSYC_Policy policy,
367                           GNUNET_PSYC_MasterStartCallback start_cb,
368                           GNUNET_PSYC_JoinRequestCallback join_request_cb,
369                           GNUNET_PSYC_MessageCallback message_cb,
370                           void *cls)
371 {
372   struct GNUNET_PSYC_Master *mst = GNUNET_malloc (sizeof (*mst));
373   struct GNUNET_PSYC_Channel *chn = &mst->chn;
374
375   struct MasterStartRequest *req = GNUNET_malloc (sizeof (*req));
376   req->header.size = htons (sizeof (*req));
377   req->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_MASTER_START);
378   req->channel_key = *channel_key;
379   req->policy = policy;
380
381   chn->connect_msg = (struct GNUNET_MessageHeader *) req;
382   chn->cfg = cfg;
383   chn->is_master = GNUNET_YES;
384
385   mst->start_cb = start_cb;
386   mst->join_req_cb = join_request_cb;
387   mst->cb_cls = cls;
388
389   chn->client = GNUNET_CLIENT_MANAGER_connect (cfg, "psyc", master_handlers);
390   GNUNET_CLIENT_MANAGER_set_user_context_ (chn->client, mst, sizeof (*chn));
391
392   chn->tmit = GNUNET_PSYC_transmit_create (chn->client);
393   chn->recv = GNUNET_PSYC_receive_create (message_cb, message_cb, cls);
394
395   channel_send_connect_msg (chn);
396   return mst;
397 }
398
399
400 /**
401  * Stop a PSYC master channel.
402  *
403  * @param master PSYC channel master to stop.
404  * @param keep_active  FIXME
405  */
406 void
407 GNUNET_PSYC_master_stop (struct GNUNET_PSYC_Master *mst)
408 {
409   GNUNET_CLIENT_MANAGER_disconnect (mst->chn.client, GNUNET_YES);
410   GNUNET_free (mst);
411 }
412
413
414 /**
415  * Function to call with the decision made for a join request.
416  *
417  * Must be called once and only once in response to an invocation of the
418  * #GNUNET_PSYC_JoinCallback.
419  *
420  * @param jh Join request handle.
421  * @param is_admitted  #GNUNET_YES    if the join is approved,
422  *                     #GNUNET_NO     if it is disapproved,
423  *                     #GNUNET_SYSERR if we cannot answer the request.
424  * @param relay_count Number of relays given.
425  * @param relays Array of suggested peers that might be useful relays to use
426  *        when joining the multicast group (essentially a list of peers that
427  *        are already part of the multicast group and might thus be willing
428  *        to help with routing).  If empty, only this local peer (which must
429  *        be the multicast origin) is a good candidate for building the
430  *        multicast tree.  Note that it is unnecessary to specify our own
431  *        peer identity in this array.
432  * @param join_resp  Application-dependent join response message.
433  *
434  * @return #GNUNET_OK on success,
435  *         #GNUNET_SYSERR if the message is too large.
436  */
437 int
438 GNUNET_PSYC_join_decision (struct GNUNET_PSYC_JoinHandle *jh,
439                            int is_admitted,
440                            uint32_t relay_count,
441                            const struct GNUNET_PeerIdentity *relays,
442                            const struct GNUNET_PSYC_MessageHeader *join_resp)
443 {
444   struct GNUNET_PSYC_Channel *chn = &jh->mst->chn;
445   struct MasterJoinDecision *dcsn;
446   uint16_t join_resp_size
447     = (NULL != join_resp) ? ntohs (join_resp->header.size) : 0;
448   uint16_t relay_size = relay_count * sizeof (*relays);
449
450   if (GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD
451       < sizeof (*dcsn) + relay_size + join_resp_size)
452     return GNUNET_SYSERR;
453
454   dcsn = GNUNET_malloc (sizeof (*dcsn) + relay_size + join_resp_size);
455   dcsn->header.size = htons (sizeof (*dcsn) + relay_size + join_resp_size);
456   dcsn->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION);
457   dcsn->is_admitted = htonl (is_admitted);
458   dcsn->slave_key = jh->slave_key;
459
460   if (0 < join_resp_size)
461     memcpy (&dcsn[1], join_resp, join_resp_size);
462
463   GNUNET_CLIENT_MANAGER_transmit (chn->client, &dcsn->header);
464   return GNUNET_OK;
465 }
466
467
468 /**
469  * Send a message to call a method to all members in the PSYC channel.
470  *
471  * @param master Handle to the PSYC channel.
472  * @param method_name Which method should be invoked.
473  * @param notify_mod Function to call to obtain modifiers.
474  * @param notify_data Function to call to obtain fragments of the data.
475  * @param notify_cls Closure for @a notify_mod and @a notify_data.
476  * @param flags Flags for the message being transmitted.
477  *
478  * @return Transmission handle, NULL on error (i.e. more than one request queued).
479  */
480 struct GNUNET_PSYC_MasterTransmitHandle *
481 GNUNET_PSYC_master_transmit (struct GNUNET_PSYC_Master *mst,
482                              const char *method_name,
483                              GNUNET_PSYC_TransmitNotifyModifier notify_mod,
484                              GNUNET_PSYC_TransmitNotifyData notify_data,
485                              void *notify_cls,
486                              enum GNUNET_PSYC_MasterTransmitFlags flags)
487 {
488   if (GNUNET_OK
489       == GNUNET_PSYC_transmit_message (mst->chn.tmit, method_name, NULL,
490                                        notify_mod, notify_data, notify_cls,
491                                        flags))
492     return (struct GNUNET_PSYC_MasterTransmitHandle *) mst->chn.tmit;
493   else
494     return NULL;
495 }
496
497
498 /**
499  * Resume transmission to the channel.
500  *
501  * @param tmit  Handle of the request that is being resumed.
502  */
503 void
504 GNUNET_PSYC_master_transmit_resume (struct GNUNET_PSYC_MasterTransmitHandle *tmit)
505 {
506   GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) tmit);
507 }
508
509
510 /**
511  * Abort transmission request to the channel.
512  *
513  * @param tmit  Handle of the request that is being aborted.
514  */
515 void
516 GNUNET_PSYC_master_transmit_cancel (struct GNUNET_PSYC_MasterTransmitHandle *tmit)
517 {
518   GNUNET_PSYC_transmit_cancel ((struct GNUNET_PSYC_TransmitHandle *) tmit);
519 }
520
521
522 /**
523  * Convert a channel @a master to a @e channel handle to access the @e channel
524  * APIs.
525  *
526  * @param master Channel master handle.
527  *
528  * @return Channel handle, valid for as long as @a master is valid.
529  */
530 struct GNUNET_PSYC_Channel *
531 GNUNET_PSYC_master_get_channel (struct GNUNET_PSYC_Master *master)
532 {
533   return &master->chn;
534 }
535
536
537 /**
538  * Join a PSYC channel.
539  *
540  * The entity joining is always the local peer.  The user must immediately use
541  * the GNUNET_PSYC_slave_transmit() functions to transmit a @e join_msg to the
542  * channel; if the join request succeeds, the channel state (and @e recent
543  * method calls) will be replayed to the joining member.  There is no explicit
544  * notification on failure (as the channel may simply take days to approve,
545  * and disapproval is simply being ignored).
546  *
547  * @param cfg  Configuration to use.
548  * @param channel_key  ECC public key that identifies the channel we wish to join.
549  * @param slave_key  ECC private-public key pair that identifies the slave, and
550  *        used by multicast to sign the join request and subsequent unicast
551  *        requests sent to the master.
552  * @param origin  Peer identity of the origin.
553  * @param relay_count  Number of peers in the @a relays array.
554  * @param relays  Peer identities of members of the multicast group, which serve
555  *        as relays and used to join the group at.
556  * @param message_cb  Function to invoke on message parts received from the
557  *        channel, typically at least contains method handlers for @e join and
558  *        @e part.
559  * @param slave_connect_cb  Function invoked once we have connected to the
560  *        PSYC service.
561  * @param join_decision_cb  Function invoked once we have received a join
562  *        decision.
563  * @param cls  Closure for @a message_cb and @a slave_joined_cb.
564  * @param method_name  Method name for the join request.
565  * @param env  Environment containing transient variables for the request, or NULL.
566  * @param data  Payload for the join message.
567  * @param data_size  Number of bytes in @a data.
568  *
569  * @return Handle for the slave, NULL on error.
570  */
571 struct GNUNET_PSYC_Slave *
572 GNUNET_PSYC_slave_join (const struct GNUNET_CONFIGURATION_Handle *cfg,
573                         const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
574                         const struct GNUNET_CRYPTO_EddsaPrivateKey *slave_key,
575                         const struct GNUNET_PeerIdentity *origin,
576                         uint32_t relay_count,
577                         const struct GNUNET_PeerIdentity *relays,
578                         GNUNET_PSYC_MessageCallback message_cb,
579                         GNUNET_PSYC_SlaveConnectCallback connect_cb,
580                         GNUNET_PSYC_JoinDecisionCallback join_decision_cb,
581                         void *cls,
582                         const char *method_name,
583                         const struct GNUNET_ENV_Environment *env,
584                         const void *data,
585                         uint16_t data_size)
586 {
587   struct GNUNET_PSYC_Slave *slv = GNUNET_malloc (sizeof (*slv));
588   struct GNUNET_PSYC_Channel *chn = &slv->chn;
589   struct SlaveJoinRequest *req
590     = GNUNET_malloc (sizeof (*req) + relay_count * sizeof (*relays));
591   req->header.size = htons (sizeof (*req)
592                             + relay_count * sizeof (*relays));
593   req->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_SLAVE_JOIN);
594   req->channel_key = *channel_key;
595   req->slave_key = *slave_key;
596   req->origin = *origin;
597   req->relay_count = htonl (relay_count);
598   memcpy (&req[1], relays, relay_count * sizeof (*relays));
599
600   chn->connect_msg = (struct GNUNET_MessageHeader *) req;
601   chn->cfg = cfg;
602   chn->is_master = GNUNET_NO;
603
604   slv->connect_cb = connect_cb;
605   slv->join_dcsn_cb = join_decision_cb;
606   slv->cb_cls = cls;
607
608   chn->client = GNUNET_CLIENT_MANAGER_connect (cfg, "psyc", slave_handlers);
609   GNUNET_CLIENT_MANAGER_set_user_context_ (chn->client, slv, sizeof (*chn));
610
611   chn->recv = GNUNET_PSYC_receive_create (message_cb, message_cb, cls);
612   chn->tmit = GNUNET_PSYC_transmit_create (chn->client);
613
614   channel_send_connect_msg (chn);
615   return slv;
616 }
617
618
619 /**
620  * Part a PSYC channel.
621  *
622  * Will terminate the connection to the PSYC service.  Polite clients should
623  * first explicitly send a part request (via GNUNET_PSYC_slave_transmit()).
624  *
625  * @param slave Slave handle.
626  */
627 void
628 GNUNET_PSYC_slave_part (struct GNUNET_PSYC_Slave *slv)
629 {
630   GNUNET_CLIENT_MANAGER_disconnect (slv->chn.client, GNUNET_YES);
631   GNUNET_free (slv);
632 }
633
634
635 /**
636  * Request a message to be sent to the channel master.
637  *
638  * @param slave Slave handle.
639  * @param method_name Which (PSYC) method should be invoked (on host).
640  * @param notify_mod Function to call to obtain modifiers.
641  * @param notify_data Function to call to obtain fragments of the data.
642  * @param notify_cls Closure for @a notify.
643  * @param flags Flags for the message being transmitted.
644  *
645  * @return Transmission handle, NULL on error (i.e. more than one request
646  *         queued).
647  */
648 struct GNUNET_PSYC_SlaveTransmitHandle *
649 GNUNET_PSYC_slave_transmit (struct GNUNET_PSYC_Slave *slv,
650                             const char *method_name,
651                             GNUNET_PSYC_TransmitNotifyModifier notify_mod,
652                             GNUNET_PSYC_TransmitNotifyData notify_data,
653                             void *notify_cls,
654                             enum GNUNET_PSYC_SlaveTransmitFlags flags)
655
656 {
657   if (GNUNET_OK
658       == GNUNET_PSYC_transmit_message (slv->chn.tmit, method_name, NULL,
659                                        notify_mod, notify_data, notify_cls,
660                                        flags))
661     return (struct GNUNET_PSYC_SlaveTransmitHandle *) slv->chn.tmit;
662   else
663     return NULL;
664 }
665
666
667 /**
668  * Resume transmission to the master.
669  *
670  * @param tmit Handle of the request that is being resumed.
671  */
672 void
673 GNUNET_PSYC_slave_transmit_resume (struct GNUNET_PSYC_SlaveTransmitHandle *tmit)
674 {
675   GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) tmit);
676 }
677
678
679 /**
680  * Abort transmission request to master.
681  *
682  * @param tmit Handle of the request that is being aborted.
683  */
684 void
685 GNUNET_PSYC_slave_transmit_cancel (struct GNUNET_PSYC_SlaveTransmitHandle *tmit)
686 {
687   GNUNET_PSYC_transmit_cancel ((struct GNUNET_PSYC_TransmitHandle *) tmit);
688 }
689
690
691 /**
692  * Convert @a slave to a @e channel handle to access the @e channel APIs.
693  *
694  * @param slv Slave handle.
695  *
696  * @return Channel handle, valid for as long as @a slave is valid.
697  */
698 struct GNUNET_PSYC_Channel *
699 GNUNET_PSYC_slave_get_channel (struct GNUNET_PSYC_Slave *slv)
700 {
701   return &slv->chn;
702 }
703
704
705 /**
706  * Add a slave to the channel's membership list.
707  *
708  * Note that this will NOT generate any PSYC traffic, it will merely update the
709  * local database to modify how we react to <em>membership test</em> queries.
710  * The channel master still needs to explicitly transmit a @e join message to
711  * notify other channel members and they then also must still call this function
712  * in their respective methods handling the @e join message.  This way, how @e
713  * join and @e part operations are exactly implemented is still up to the
714  * application; for example, there might be a @e part_all method to kick out
715  * everyone.
716  *
717  * Note that channel slaves are explicitly trusted to execute such methods
718  * correctly; not doing so correctly will result in either denying other slaves
719  * access or offering access to channel data to non-members.
720  *
721  * @param channel Channel handle.
722  * @param slave_key Identity of channel slave to add.
723  * @param announced_at ID of the message that announced the membership change.
724  * @param effective_since Addition of slave is in effect since this message ID.
725  */
726 void
727 GNUNET_PSYC_channel_slave_add (struct GNUNET_PSYC_Channel *chn,
728                                const struct GNUNET_CRYPTO_EddsaPublicKey *slave_key,
729                                uint64_t announced_at,
730                                uint64_t effective_since)
731 {
732   struct ChannelSlaveAdd *add = GNUNET_malloc (sizeof (*add));
733   add->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_CHANNEL_SLAVE_ADD);
734   add->header.size = htons (sizeof (*add));
735   add->announced_at = GNUNET_htonll (announced_at);
736   add->effective_since = GNUNET_htonll (effective_since);
737   GNUNET_CLIENT_MANAGER_transmit (chn->client, &add->header);
738 }
739
740
741 /**
742  * Remove a slave from the channel's membership list.
743  *
744  * Note that this will NOT generate any PSYC traffic, it will merely update the
745  * local database to modify how we react to <em>membership test</em> queries.
746  * The channel master still needs to explicitly transmit a @e part message to
747  * notify other channel members and they then also must still call this function
748  * in their respective methods handling the @e part message.  This way, how
749  * @e join and @e part operations are exactly implemented is still up to the
750  * application; for example, there might be a @e part_all message to kick out
751  * everyone.
752  *
753  * Note that channel members are explicitly trusted to perform these
754  * operations correctly; not doing so correctly will result in either
755  * denying members access or offering access to channel data to
756  * non-members.
757  *
758  * @param channel Channel handle.
759  * @param slave_key Identity of channel slave to remove.
760  * @param announced_at ID of the message that announced the membership change.
761  */
762 void
763 GNUNET_PSYC_channel_slave_remove (struct GNUNET_PSYC_Channel *chn,
764                                   const struct GNUNET_CRYPTO_EddsaPublicKey *slave_key,
765                                   uint64_t announced_at)
766 {
767   struct ChannelSlaveRemove *rm = GNUNET_malloc (sizeof (*rm));
768   rm->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_CHANNEL_SLAVE_RM);
769   rm->header.size = htons (sizeof (*rm));
770   rm->announced_at = GNUNET_htonll (announced_at);
771   GNUNET_CLIENT_MANAGER_transmit (chn->client, &rm->header);
772 }
773
774
775 /**
776  * Request to be told the message history of the channel.
777  *
778  * Historic messages (but NOT the state at the time) will be replayed (given to
779  * the normal method handlers) if available and if access is permitted.
780  *
781  * To get the latest message, use 0 for both the start and end message ID.
782  *
783  * @param channel Which channel should be replayed?
784  * @param start_message_id Earliest interesting point in history.
785  * @param end_message_id Last (exclusive) interesting point in history.
786  * @param message_cb Function to invoke on message parts received from the story.
787  * @param finish_cb Function to call when the requested story has been fully
788  *        told (counting message IDs might not suffice, as some messages
789  *        might be secret and thus the listener would not know the story is
790  *        finished without being told explicitly) once this function
791  *        has been called, the client must not call
792  *        GNUNET_PSYC_channel_story_tell_cancel() anymore.
793  * @param cls Closure for the callbacks.
794  *
795  * @return Handle to cancel story telling operation.
796  */
797 struct GNUNET_PSYC_Story *
798 GNUNET_PSYC_channel_story_tell (struct GNUNET_PSYC_Channel *channel,
799                                 uint64_t start_message_id,
800                                 uint64_t end_message_id,
801                                 GNUNET_PSYC_MessageCallback message_cb,
802                                 GNUNET_PSYC_FinishCallback finish_cb,
803                                 void *cls)
804 {
805   return NULL;
806 }
807
808
809 /**
810  * Abort story telling.
811  *
812  * This function must not be called from within method handlers (as given to
813  * GNUNET_PSYC_slave_join()) of the slave.
814  *
815  * @param story Story telling operation to stop.
816  */
817 void
818 GNUNET_PSYC_channel_story_tell_cancel (struct GNUNET_PSYC_Story *story)
819 {
820
821 }
822
823
824 /**
825  * Retrieve the best matching channel state variable.
826  *
827  * If the requested variable name is not present in the state, the nearest
828  * less-specific name is matched; for example, requesting "_a_b" will match "_a"
829  * if "_a_b" does not exist.
830  *
831  * @param channel Channel handle.
832  * @param full_name Full name of the requested variable, the actual variable
833  *        returned might have a shorter name..
834  * @param cb Function called once when a matching state variable is found.
835  *        Not called if there's no matching state variable.
836  * @param cb_cls Closure for the callbacks.
837  *
838  * @return Handle that can be used to cancel the query operation.
839  */
840 struct GNUNET_PSYC_StateQuery *
841 GNUNET_PSYC_channel_state_get (struct GNUNET_PSYC_Channel *channel,
842                                const char *full_name,
843                                GNUNET_PSYC_StateCallback cb,
844                                void *cb_cls)
845 {
846   return NULL;
847 }
848
849
850 /**
851  * Return all channel state variables whose name matches a given prefix.
852  *
853  * A name matches if it starts with the given @a name_prefix, thus requesting
854  * the empty prefix ("") will match all values; requesting "_a_b" will also
855  * return values stored under "_a_b_c".
856  *
857  * The @a state_cb is invoked on all matching state variables asynchronously, as
858  * the state is stored in and retrieved from the PSYCstore,
859  *
860  * @param channel Channel handle.
861  * @param name_prefix Prefix of the state variable name to match.
862  * @param cb Function to call with the matching state variables.
863  * @param cb_cls Closure for the callbacks.
864  *
865  * @return Handle that can be used to cancel the query operation.
866  */
867 struct GNUNET_PSYC_StateQuery *
868 GNUNET_PSYC_channel_state_get_prefix (struct GNUNET_PSYC_Channel *channel,
869                                       const char *name_prefix,
870                                       GNUNET_PSYC_StateCallback cb,
871                                       void *cb_cls)
872 {
873   return NULL;
874 }
875
876
877 /**
878  * Cancel a state query operation.
879  *
880  * @param query Handle for the operation to cancel.
881  */
882 void
883 GNUNET_PSYC_channel_state_get_cancel (struct GNUNET_PSYC_StateQuery *query)
884 {
885
886 }
887
888
889 /* end of psyc_api.c */