diff options
Diffstat (limited to 'src/zklaim/gnunet-service-zklaim.c')
-rw-r--r-- | src/zklaim/gnunet-service-zklaim.c | 417 |
1 files changed, 417 insertions, 0 deletions
diff --git a/src/zklaim/gnunet-service-zklaim.c b/src/zklaim/gnunet-service-zklaim.c new file mode 100644 index 000000000..ac8455364 --- /dev/null +++ b/src/zklaim/gnunet-service-zklaim.c | |||
@@ -0,0 +1,417 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2012-2015 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 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | */ | ||
18 | /** | ||
19 | * @author Martin Schanzenbach | ||
20 | * @file src/reclaim/gnunet-service-reclaim.c | ||
21 | * @brief reclaim Service | ||
22 | * | ||
23 | */ | ||
24 | #include "platform.h" | ||
25 | #include "gnunet_util_lib.h" | ||
26 | #include "gnunet_constants.h" | ||
27 | #include "gnunet_protocols.h" | ||
28 | #include "gnunet_gnsrecord_lib.h" | ||
29 | #include "gnunet_gns_service.h" | ||
30 | #include "gnunet_statistics_service.h" | ||
31 | #include "gnunet_namestore_service.h" | ||
32 | #include "zklaim_api.h" | ||
33 | #include "zklaim/zklaim.h" | ||
34 | |||
35 | /** | ||
36 | * Namestore handle | ||
37 | */ | ||
38 | static struct GNUNET_NAMESTORE_Handle *ns_handle; | ||
39 | |||
40 | /** | ||
41 | * GNS handle | ||
42 | */ | ||
43 | static struct GNUNET_GNS_Handle *gns_handle; | ||
44 | |||
45 | /** | ||
46 | * Handle to the statistics service. | ||
47 | */ | ||
48 | static struct GNUNET_STATISTICS_Handle *stats; | ||
49 | |||
50 | /** | ||
51 | * Our configuration. | ||
52 | */ | ||
53 | static const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
54 | |||
55 | /** | ||
56 | * An idp client | ||
57 | */ | ||
58 | struct ZkClient | ||
59 | { | ||
60 | |||
61 | /** | ||
62 | * The client | ||
63 | */ | ||
64 | struct GNUNET_SERVICE_Client *client; | ||
65 | |||
66 | /** | ||
67 | * Message queue for transmission to @e client | ||
68 | */ | ||
69 | struct GNUNET_MQ_Handle *mq; | ||
70 | |||
71 | /** | ||
72 | * Head of DLL of context create ops | ||
73 | */ | ||
74 | struct CreateContextHandle *create_op_head; | ||
75 | |||
76 | /** | ||
77 | * Tail of DLL of attribute store ops | ||
78 | */ | ||
79 | struct CreateContextHandle *create_op_tail; | ||
80 | |||
81 | }; | ||
82 | |||
83 | struct CreateContextHandle | ||
84 | { | ||
85 | /** | ||
86 | * DLL | ||
87 | */ | ||
88 | struct CreateContextHandle *next; | ||
89 | |||
90 | /** | ||
91 | * DLL | ||
92 | */ | ||
93 | struct CreateContextHandle *prev; | ||
94 | |||
95 | /** | ||
96 | * Client connection | ||
97 | */ | ||
98 | struct ZkClient *client; | ||
99 | |||
100 | /** | ||
101 | * Issuer private key | ||
102 | */ | ||
103 | struct GNUNET_CRYPTO_EcdsaPrivateKey private_key; | ||
104 | |||
105 | /** | ||
106 | * Issuer public key | ||
107 | */ | ||
108 | struct GNUNET_CRYPTO_EcdsaPublicKey public_key; | ||
109 | |||
110 | /** | ||
111 | * QueueEntry | ||
112 | */ | ||
113 | struct GNUNET_NAMESTORE_QueueEntry *ns_qe; | ||
114 | |||
115 | /** | ||
116 | * The context name | ||
117 | */ | ||
118 | char *name; | ||
119 | |||
120 | /** | ||
121 | * The attributes to support | ||
122 | */ | ||
123 | char *attrs; | ||
124 | |||
125 | }; | ||
126 | |||
127 | /** | ||
128 | * Cleanup task | ||
129 | */ | ||
130 | static void | ||
131 | cleanup() | ||
132 | { | ||
133 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
134 | "Cleaning up\n"); | ||
135 | |||
136 | if (NULL != stats) | ||
137 | { | ||
138 | GNUNET_STATISTICS_destroy (stats, GNUNET_NO); | ||
139 | stats = NULL; | ||
140 | } | ||
141 | if (NULL != gns_handle) | ||
142 | GNUNET_GNS_disconnect (gns_handle); | ||
143 | if (NULL != ns_handle) | ||
144 | GNUNET_NAMESTORE_disconnect (ns_handle); | ||
145 | } | ||
146 | |||
147 | /** | ||
148 | * Shutdown task | ||
149 | * | ||
150 | * @param cls NULL | ||
151 | */ | ||
152 | static void | ||
153 | do_shutdown (void *cls) | ||
154 | { | ||
155 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
156 | "Shutting down...\n"); | ||
157 | cleanup(); | ||
158 | } | ||
159 | |||
160 | |||
161 | |||
162 | /** | ||
163 | * Cleanup attribute store handle | ||
164 | * | ||
165 | * @param handle handle to clean up | ||
166 | */ | ||
167 | static void | ||
168 | cleanup_create_handle (struct CreateContextHandle *handle) | ||
169 | { | ||
170 | if (NULL != handle->ns_qe) | ||
171 | GNUNET_NAMESTORE_cancel (handle->ns_qe); | ||
172 | if (NULL != handle->name) | ||
173 | GNUNET_free (handle->name); | ||
174 | GNUNET_free (handle); | ||
175 | } | ||
176 | |||
177 | static void | ||
178 | send_result (int32_t status, | ||
179 | struct CreateContextHandle *cch) | ||
180 | { | ||
181 | struct GNUNET_MQ_Envelope *env; | ||
182 | struct ResultCodeMessage *r_msg; | ||
183 | |||
184 | |||
185 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
186 | "Sending RESULT_CODE message\n"); | ||
187 | env = GNUNET_MQ_msg (r_msg, | ||
188 | GNUNET_MESSAGE_TYPE_ZKLAIM_RESULT_CODE); | ||
189 | r_msg->result_code = htonl (status); | ||
190 | GNUNET_MQ_send (cch->client->mq, | ||
191 | env); | ||
192 | cleanup_create_handle (cch); | ||
193 | |||
194 | } | ||
195 | |||
196 | static void | ||
197 | context_store_cont (void *cls, | ||
198 | int32_t success, | ||
199 | const char *emsg) | ||
200 | { | ||
201 | struct CreateContextHandle *cch = cls; | ||
202 | |||
203 | cch->ns_qe = NULL; | ||
204 | GNUNET_CONTAINER_DLL_remove (cch->client->create_op_head, | ||
205 | cch->client->create_op_tail, | ||
206 | cch); | ||
207 | |||
208 | if (GNUNET_SYSERR == success) | ||
209 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
210 | "Failed to create context %s\n", | ||
211 | emsg); | ||
212 | |||
213 | send_result (success, cch); | ||
214 | } | ||
215 | |||
216 | |||
217 | |||
218 | static int | ||
219 | check_create_context_message(void *cls, | ||
220 | const struct CreateRequestMessage *crm) | ||
221 | { | ||
222 | uint16_t size; | ||
223 | |||
224 | size = ntohs (crm->header.size); | ||
225 | if (size <= sizeof (struct CreateRequestMessage)) | ||
226 | { | ||
227 | GNUNET_break (0); | ||
228 | return GNUNET_SYSERR; | ||
229 | } | ||
230 | return GNUNET_OK; | ||
231 | } | ||
232 | |||
233 | |||
234 | static void | ||
235 | handle_create_context_message (void *cls, | ||
236 | const struct CreateRequestMessage *crm) | ||
237 | { | ||
238 | struct CreateContextHandle *cch; | ||
239 | struct ZkClient *zkc = cls; | ||
240 | size_t data_len; | ||
241 | char *tmp; | ||
242 | char *pos; | ||
243 | unsigned char *data; | ||
244 | int num_attrs; | ||
245 | int num_pl; | ||
246 | int i; | ||
247 | zklaim_ctx *ctx; | ||
248 | |||
249 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
250 | "Received CREATE_REQUEST message\n"); | ||
251 | |||
252 | data_len = ntohs (crm->name_len); | ||
253 | |||
254 | cch = GNUNET_new (struct CreateContextHandle); | ||
255 | cch->name = GNUNET_strndup ((char*)&crm[1], data_len-1); | ||
256 | data_len = ntohs(crm->attrs_len); | ||
257 | cch->attrs = GNUNET_strndup (((char*)&crm[1]) + strlen (cch->name) + 1, | ||
258 | data_len-1); | ||
259 | cch->private_key = crm->private_key; | ||
260 | GNUNET_CRYPTO_ecdsa_key_get_public (&crm->private_key, | ||
261 | &cch->public_key); | ||
262 | |||
263 | GNUNET_SERVICE_client_continue (zkc->client); | ||
264 | cch->client = zkc; | ||
265 | GNUNET_CONTAINER_DLL_insert (zkc->create_op_head, | ||
266 | zkc->create_op_tail, | ||
267 | cch); | ||
268 | |||
269 | tmp = GNUNET_strdup (cch->attrs); | ||
270 | pos = strtok(tmp, ","); | ||
271 | num_attrs = 0; | ||
272 | if (NULL == pos) | ||
273 | { | ||
274 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
275 | "No attributes given.\n"); | ||
276 | send_result(GNUNET_SYSERR, cch); | ||
277 | GNUNET_free (tmp); | ||
278 | return; | ||
279 | } | ||
280 | while (NULL != pos) | ||
281 | { | ||
282 | num_attrs++; | ||
283 | pos = strtok(NULL, ","); | ||
284 | } | ||
285 | GNUNET_free (tmp); | ||
286 | num_pl = num_attrs / 5; | ||
287 | zklaim_payload pl[num_pl]; | ||
288 | ctx = zklaim_context_new (); | ||
289 | for (i = 0; i < num_pl; i++) | ||
290 | zklaim_add_pl (ctx, pl[i]); | ||
291 | zklaim_hash_ctx (ctx); | ||
292 | if (0 != zklaim_trusted_setup (ctx)) | ||
293 | { | ||
294 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
295 | "Trusted Setup failed.\n"); | ||
296 | send_result(GNUNET_SYSERR, cch); | ||
297 | zklaim_ctx_free (ctx); | ||
298 | return; | ||
299 | } | ||
300 | if (0 != zklaim_ctx_serialize (ctx, &data)) | ||
301 | { | ||
302 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
303 | "Context serialization failed.\n"); | ||
304 | send_result(GNUNET_SYSERR, cch); | ||
305 | zklaim_ctx_free (ctx); | ||
306 | return; | ||
307 | } | ||
308 | zklaim_ctx_free (ctx); | ||
309 | // store ctx,attrs as GNS record | ||
310 | } | ||
311 | |||
312 | |||
313 | |||
314 | /** | ||
315 | * Main function that will be run | ||
316 | * | ||
317 | * @param cls closure | ||
318 | * @param c the configuration used | ||
319 | * @param server the service handle | ||
320 | */ | ||
321 | static void | ||
322 | run (void *cls, | ||
323 | const struct GNUNET_CONFIGURATION_Handle *c, | ||
324 | struct GNUNET_SERVICE_Handle *server) | ||
325 | { | ||
326 | cfg = c; | ||
327 | |||
328 | stats = GNUNET_STATISTICS_create ("zklaim", cfg); | ||
329 | |||
330 | //Connect to services | ||
331 | ns_handle = GNUNET_NAMESTORE_connect (cfg); | ||
332 | if (NULL == ns_handle) | ||
333 | { | ||
334 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "error connecting to namestore"); | ||
335 | } | ||
336 | |||
337 | gns_handle = GNUNET_GNS_connect (cfg); | ||
338 | if (NULL == gns_handle) | ||
339 | { | ||
340 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "error connecting to gns"); | ||
341 | } | ||
342 | GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL); | ||
343 | } | ||
344 | |||
345 | |||
346 | /** | ||
347 | * Called whenever a client is disconnected. | ||
348 | * | ||
349 | * @param cls closure | ||
350 | * @param client identification of the client | ||
351 | * @param app_ctx @a client | ||
352 | */ | ||
353 | static void | ||
354 | client_disconnect_cb (void *cls, | ||
355 | struct GNUNET_SERVICE_Client *client, | ||
356 | void *app_ctx) | ||
357 | { | ||
358 | struct ZkClient *zkc = app_ctx; | ||
359 | struct CreateContextHandle *cch; | ||
360 | |||
361 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
362 | "Client %p disconnected\n", | ||
363 | client); | ||
364 | |||
365 | while (NULL != (cch = zkc->create_op_head)) | ||
366 | { | ||
367 | GNUNET_CONTAINER_DLL_remove (zkc->create_op_head, | ||
368 | zkc->create_op_tail, | ||
369 | cch); | ||
370 | cleanup_create_handle (cch); | ||
371 | } | ||
372 | GNUNET_free (zkc); | ||
373 | } | ||
374 | |||
375 | |||
376 | |||
377 | /** | ||
378 | * Add a client to our list of active clients. | ||
379 | * | ||
380 | * @param cls NULL | ||
381 | * @param client client to add | ||
382 | * @param mq message queue for @a client | ||
383 | * @return internal namestore client structure for this client | ||
384 | */ | ||
385 | static void * | ||
386 | client_connect_cb (void *cls, | ||
387 | struct GNUNET_SERVICE_Client *client, | ||
388 | struct GNUNET_MQ_Handle *mq) | ||
389 | { | ||
390 | struct ZkClient *zkc; | ||
391 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
392 | "Client %p connected\n", | ||
393 | client); | ||
394 | zkc = GNUNET_new (struct ZkClient); | ||
395 | zkc->client = client; | ||
396 | zkc->mq = mq; | ||
397 | return zkc; | ||
398 | } | ||
399 | |||
400 | |||
401 | |||
402 | /** | ||
403 | * Define "main" method using service macro. | ||
404 | */ | ||
405 | GNUNET_SERVICE_MAIN | ||
406 | ("zklaim", | ||
407 | GNUNET_SERVICE_OPTION_NONE, | ||
408 | &run, | ||
409 | &client_connect_cb, | ||
410 | &client_disconnect_cb, | ||
411 | NULL, | ||
412 | GNUNET_MQ_hd_var_size (create_context_message, | ||
413 | GNUNET_MESSAGE_TYPE_ZKLAIM_CREATE, | ||
414 | struct CreateRequestMessage, | ||
415 | NULL), | ||
416 | GNUNET_MQ_handler_end()); | ||
417 | /* end of gnunet-service-zklaim.c */ | ||