-fix #3233
[oweals/gnunet.git] / src / secretsharing / secretsharing_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012 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 secretsharing/secretsharing_api.c
23  * @brief
24  * @author Florian Dold
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_secretsharing_service.h"
29 #include "secretsharing.h"
30
31
32 #define LOG(kind,...) GNUNET_log_from (kind, "secretsharing-api",__VA_ARGS__)
33
34
35
36 /**
37  * Session that will eventually establish a shared secred between
38  * the involved peers and allow encryption and cooperative decryption.
39  */
40 struct GNUNET_SECRETSHARING_Session
41 {
42   /**
43    * Client connected to the secretsharing service.
44    */
45   struct GNUNET_CLIENT_Connection *client;
46
47   /**
48    * Message queue for 'client'.
49    */
50   struct GNUNET_MQ_Handle *mq;
51
52   /**
53    * Called when the secret sharing is done.
54    */
55   GNUNET_SECRETSHARING_SecretReadyCallback secret_ready_cb;
56
57   /**
58    * Closure for 'secret_ready_cb'.
59    */
60   void *secret_ready_cls;
61 };
62
63
64 static void
65 handle_session_client_error (void *cls, enum GNUNET_MQ_Error error)
66 {
67   struct GNUNET_SECRETSHARING_Session *s = cls;
68
69   s->secret_ready_cb (s->secret_ready_cls, NULL, NULL, 0, NULL);
70 }
71
72 static void
73 handle_secret_ready (void *cls, const struct GNUNET_MessageHeader *msg)
74 {
75   struct GNUNET_SECRETSHARING_Session *s = cls;
76   const struct GNUNET_SECRETSHARING_SecretReadyMessage *m = (const void *) msg;
77
78   s->secret_ready_cb (s->secret_ready_cls,
79                       NULL,
80                       &m->public_key,
81                       ntohs (m->num_secret_peers),
82                       (struct GNUNET_PeerIdentity *) &m[1]);
83
84 }
85
86
87 struct GNUNET_SECRETSHARING_Session *
88 GNUNET_SECRETSHARING_create_session (const struct GNUNET_CONFIGURATION_Handle *cfg,
89                                      unsigned int num_peers,
90                                      const struct GNUNET_PeerIdentity *peers,
91                                      const struct GNUNET_HashCode *session_id,
92                                      struct GNUNET_TIME_Absolute deadline,
93                                      unsigned int threshold,
94                                      GNUNET_SECRETSHARING_SecretReadyCallback cb,
95                                      void *cls)
96 {
97   struct GNUNET_SECRETSHARING_Session *s;
98   struct GNUNET_MQ_Envelope *ev;
99   struct GNUNET_SECRETSHARING_CreateMessage *msg;
100   static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
101     {handle_secret_ready, GNUNET_MESSAGE_TYPE_SECRETSHARING_CLIENT_SECRET_READY, 0},
102     GNUNET_MQ_HANDLERS_END
103   };
104
105
106   s = GNUNET_new (struct GNUNET_SECRETSHARING_Session);
107   s->client = GNUNET_CLIENT_connect ("secretsharing", cfg);
108   s->secret_ready_cb = cb;
109   s->secret_ready_cls = cls;
110   GNUNET_assert (NULL != s->client);
111
112   s->mq = GNUNET_MQ_queue_for_connection_client (s->client, mq_handlers,
113                                                    handle_session_client_error, s);
114   GNUNET_assert (NULL != s->mq);
115
116   ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SECRETSHARING_CLIENT_GENERATE);
117   msg->threshold = htons (threshold);
118   GNUNET_MQ_send (s->mq, ev);
119
120   LOG (GNUNET_ERROR_TYPE_DEBUG, "secretsharing session created\n");
121   return s;
122
123 }
124
125
126