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