- FIX: GNUNET_SET_STATUS_HALF_DONE is never called only GNUNET_SET_STATUS_DONE
[oweals/gnunet.git] / src / revocation / gnunet-service-revocation.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 Licerevocation 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 Licerevocation for more details.
14
15   You should have received a copy of the GNU General Public Licerevocation
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 revocation/gnunet-service-revocation.c
23  * @brief key revocation service
24  * @author Christian Grothoff
25  *
26  * The purpose of this service is to allow users to permanently revoke
27  * (compromised) keys.  This is done by flooding the network with the
28  * revocation requests.  To reduce the attack potential offered by such
29  * flooding, revocations must include a proof of work.  We use the
30  * set service for efficiently computing the union of revocations of
31  * peers that connect.
32  *
33  * TODO:
34  * - optimization: avoid sending revocation back to peer that we got it from;
35  * - optimization: have randomized delay in sending revocations to other peers
36  *                 to make it rare to traverse each link twice (NSE-style)
37  */
38 #include "platform.h"
39 #include <math.h>
40 #include "gnunet_util_lib.h"
41 #include "gnunet_constants.h"
42 #include "gnunet_protocols.h"
43 #include "gnunet_signatures.h"
44 #include "gnunet_statistics_service.h"
45 #include "gnunet_core_service.h"
46 #include "gnunet_revocation_service.h"
47 #include "gnunet_set_service.h"
48 #include "revocation.h"
49 #include <gcrypt.h>
50
51
52 /**
53  * Per-peer information.
54  */
55 struct PeerEntry
56 {
57
58   /**
59    * Queue for sending messages to this peer.
60    */
61   struct GNUNET_MQ_Handle *mq;
62
63   /**
64    * What is the identity of the peer?
65    */
66   struct GNUNET_PeerIdentity id;
67
68   /**
69    * Tasked used to trigger the set union operation.
70    */
71   GNUNET_SCHEDULER_TaskIdentifier transmit_task;
72
73   /**
74    * Handle to active set union operation (over revocation sets).
75    */
76   struct GNUNET_SET_OperationHandle *so;
77
78 };
79
80
81 /**
82  * Set from all revocations known to us.
83  */
84 static struct GNUNET_SET_Handle *revocation_set;
85
86 /**
87  * Hash map with all revoked keys, maps the hash of the public key
88  * to the respective `struct RevokeMessage`.
89  */
90 static struct GNUNET_CONTAINER_MultiHashMap *revocation_map;
91
92 /**
93  * Handle to our current configuration.
94  */
95 static const struct GNUNET_CONFIGURATION_Handle *cfg;
96
97 /**
98  * Handle to the statistics service.
99  */
100 static struct GNUNET_STATISTICS_Handle *stats;
101
102 /**
103  * Handle to the core service (for flooding)
104  */
105 static struct GNUNET_CORE_Handle *core_api;
106
107 /**
108  * Map of all connected peers.
109  */
110 static struct GNUNET_CONTAINER_MultiPeerMap *peers;
111
112 /**
113  * The peer identity of this peer.
114  */
115 static struct GNUNET_PeerIdentity my_identity;
116
117 /**
118  * Handle to this serivce's server.
119  */
120 static struct GNUNET_SERVER_Handle *srv;
121
122 /**
123  * Notification context for convenient sending of replies to the clients.
124  */
125 static struct GNUNET_SERVER_NotificationContext *nc;
126
127 /**
128  * File handle for the revocation database.
129  */
130 static struct GNUNET_DISK_FileHandle *revocation_db;
131
132 /**
133  * Handle for us listening to incoming revocation set union requests.
134  */
135 static struct GNUNET_SET_ListenHandle *revocation_union_listen_handle;
136
137 /**
138  * Amount of work required (W-bit collisions) for REVOCATION proofs, in collision-bits.
139  */
140 static unsigned long long revocation_work_required;
141
142 /**
143  * Our application ID for set union operations.  Must be the
144  * same for all (compatible) peers.
145  */
146 static struct GNUNET_HashCode revocation_set_union_app_id;
147
148
149
150 /**
151  * An revoke message has been received, check that it is well-formed.
152  *
153  * @param rm the message to verify
154  * @return #GNUNET_YES if the message is verified
155  *         #GNUNET_NO if the key/signature don't verify
156  */
157 static int
158 verify_revoke_message (const struct RevokeMessage *rm)
159 {
160   if (GNUNET_YES !=
161       GNUNET_REVOCATION_check_pow (&rm->public_key,
162                                    rm->proof_of_work,
163                                    (unsigned int) revocation_work_required))
164   {
165     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
166                 "Proof of work invalid!\n");
167     GNUNET_break_op (0);
168     return GNUNET_NO;
169   }
170   if (GNUNET_OK !=
171       GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_REVOCATION,
172                                 &rm->purpose,
173                                 &rm->signature,
174                                 &rm->public_key))
175   {
176     GNUNET_break_op (0);
177     return GNUNET_NO;
178   }
179   return GNUNET_YES;
180 }
181
182
183 /**
184  * Handle QUERY message from client.
185  *
186  * @param cls unused
187  * @param client who sent the message
188  * @param message the message received
189  */
190 static void
191 handle_query_message (void *cls,
192                       struct GNUNET_SERVER_Client *client,
193                       const struct GNUNET_MessageHeader *message)
194 {
195   const struct QueryMessage *qm = (const struct QueryMessage *) message;
196   struct QueryResponseMessage qrm;
197   struct GNUNET_HashCode hc;
198   int res;
199
200   GNUNET_CRYPTO_hash (&qm->key,
201                       sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey),
202                       &hc);
203   res = GNUNET_CONTAINER_multihashmap_contains (revocation_map, &hc);
204   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
205               (GNUNET_NO == res)
206               ? "Received revocation check for valid key `%s' from client\n"
207               : "Received revocation check for revoked key `%s' from client\n",
208               GNUNET_h2s (&hc));
209   qrm.header.size = htons (sizeof (struct QueryResponseMessage));
210   qrm.header.type = htons (GNUNET_MESSAGE_TYPE_REVOCATION_QUERY_RESPONSE);
211   qrm.is_valid = htonl ((GNUNET_YES == res) ? GNUNET_NO : GNUNET_YES);
212   GNUNET_SERVER_notification_context_add (nc,
213                                           client);
214   GNUNET_SERVER_notification_context_unicast (nc,
215                                               client,
216                                               &qrm.header,
217                                               GNUNET_NO);
218   GNUNET_SERVER_receive_done (client, GNUNET_OK);
219 }
220
221
222 /**
223  * Flood the given revocation message to all neighbours.
224  *
225  * @param cls the `struct RevokeMessage` to flood
226  * @param target a neighbour
227  * @param value our `struct PeerEntry` for the neighbour
228  * @return #GNUNET_OK (continue to iterate)
229  */
230 static int
231 do_flood (void *cls,
232           const struct GNUNET_PeerIdentity *target,
233           void *value)
234 {
235   const struct RevokeMessage *rm = cls;
236   struct PeerEntry *pe = value;
237   struct GNUNET_MQ_Envelope *e;
238   struct RevokeMessage *cp;
239
240   e = GNUNET_MQ_msg (cp, GNUNET_MESSAGE_TYPE_REVOCATION_REVOKE);
241   *cp = *rm;
242   GNUNET_MQ_send (pe->mq, e);
243   return GNUNET_OK;
244 }
245
246
247 /**
248  * Publicize revocation message.   Stores the message locally in the
249  * database and passes it to all connected neighbours (and adds it to
250  * the set for future connections).
251  *
252  * @param rm message to publicize
253  * @return #GNUNET_OK on success, #GNUNET_NO if we encountered an error,
254  *         #GNUNET_SYSERR if the message was malformed
255  */
256 static int
257 publicize_rm (const struct RevokeMessage *rm)
258 {
259   struct RevokeMessage *cp;
260   struct GNUNET_HashCode hc;
261   struct GNUNET_SET_Element e;
262
263   GNUNET_CRYPTO_hash (&rm->public_key,
264                       sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey),
265                       &hc);
266   if (GNUNET_YES ==
267       GNUNET_CONTAINER_multihashmap_contains (revocation_map,
268                                               &hc))
269   {
270     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
271                 _("Duplicate revocation received from peer. Ignored.\n"));
272     return GNUNET_OK;
273   }
274   if (GNUNET_OK !=
275       verify_revoke_message (rm))
276   {
277     GNUNET_break_op (0);
278     return GNUNET_SYSERR;
279   }
280   /* write to disk */
281   if (sizeof (struct RevokeMessage) !=
282       GNUNET_DISK_file_write (revocation_db,
283                               rm,
284                               sizeof (struct RevokeMessage)))
285   {
286     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
287                          "write");
288     return GNUNET_NO;
289   }
290   if (GNUNET_OK !=
291       GNUNET_DISK_file_sync (revocation_db))
292   {
293     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
294                          "sync");
295     return GNUNET_NO;
296   }
297   /* keep copy in memory */
298   cp = (struct RevokeMessage *) GNUNET_copy_message (&rm->header);
299   GNUNET_break (GNUNET_OK ==
300                 GNUNET_CONTAINER_multihashmap_put (revocation_map,
301                                                    &hc,
302                                                    cp,
303                                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
304   /* add to set for future connections */
305   e.size = htons (rm->header.size);
306   e.type = 0;
307   e.data = rm;
308   if (GNUNET_OK !=
309       GNUNET_SET_add_element (revocation_set,
310                               &e,
311                               NULL, NULL))
312   {
313     GNUNET_break (0);
314     return GNUNET_OK;
315   }
316   /* flood to neighbours */
317   GNUNET_CONTAINER_multipeermap_iterate (peers,
318                                          &do_flood,
319                                          cp);
320   return GNUNET_OK;
321 }
322
323
324 /**
325  * Handle REVOKE message from client.
326  *
327  * @param cls unused
328  * @param client who sent the message
329  * @param message the message received
330  */
331 static void
332 handle_revoke_message (void *cls,
333                        struct GNUNET_SERVER_Client *client,
334                        const struct GNUNET_MessageHeader *message)
335 {
336   const struct RevokeMessage *rm;
337   struct RevocationResponseMessage rrm;
338   int ret;
339
340   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
341               "Received REVOKE message from client\n");
342   rm = (const struct RevokeMessage *) message;
343   if (GNUNET_SYSERR == (ret = publicize_rm (rm)))
344   {
345     GNUNET_break_op (0);
346     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
347     return;
348   }
349   rrm.header.size = htons (sizeof (struct RevocationResponseMessage));
350   rrm.header.type = htons (GNUNET_MESSAGE_TYPE_REVOCATION_REVOKE_RESPONSE);
351   rrm.is_valid = htonl ((GNUNET_OK == ret) ? GNUNET_NO : GNUNET_YES);
352   GNUNET_SERVER_notification_context_add (nc,
353                                           client);
354   GNUNET_SERVER_notification_context_unicast (nc,
355                                               client,
356                                               &rrm.header,
357                                               GNUNET_NO);
358   GNUNET_SERVER_receive_done (client, GNUNET_OK);
359 }
360
361
362 /**
363  * Core handler for flooded revocation messages.
364  *
365  * @param cls closure unused
366  * @param message message
367  * @param peer peer identity this message is from (ignored)
368  */
369 static int
370 handle_p2p_revoke_message (void *cls,
371                            const struct GNUNET_PeerIdentity *peer,
372                            const struct GNUNET_MessageHeader *message)
373 {
374   const struct RevokeMessage *rm;
375
376   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
377               "Received REVOKE message from peer\n");
378   rm = (const struct RevokeMessage *) message;
379   GNUNET_break_op (GNUNET_SYSERR != publicize_rm (rm));
380   return GNUNET_OK;
381 }
382
383
384
385 /**
386  * Callback for set operation results. Called for each element in the
387  * result set.  Each element contains a revocation, which we should
388  * validate and then add to our revocation list (and set).
389  *
390  * @param cls closure
391  * @param element a result element, only valid if status is #GNUNET_SET_STATUS_OK
392  * @param status see `enum GNUNET_SET_Status`
393  */
394 static void
395 add_revocation (void *cls,
396                 const struct GNUNET_SET_Element *element,
397                 enum GNUNET_SET_Status status)
398 {
399   struct PeerEntry *peer_entry = cls;
400   const struct RevokeMessage *rm;
401
402   switch (status)
403   {
404   case GNUNET_SET_STATUS_OK:
405     if (element->size != sizeof (struct RevokeMessage))
406     {
407       GNUNET_break_op (0);
408       return;
409     }
410     if (0 != element->type)
411     {
412       GNUNET_STATISTICS_update (stats,
413                                 "# unsupported revocations received via set union",
414                                 1, GNUNET_NO);
415       return;
416     }
417     rm = element->data;
418     (void) handle_p2p_revoke_message (NULL,
419                                       &peer_entry->id,
420                                       &rm->header);
421     GNUNET_STATISTICS_update (stats,
422                               "# revocation messages received via set union",
423                               1, GNUNET_NO);
424     break;
425   case GNUNET_SET_STATUS_TIMEOUT:
426   case GNUNET_SET_STATUS_FAILURE:
427     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
428                 _("Error computing revocation set union with %s\n"),
429                 GNUNET_i2s (&peer_entry->id));
430     peer_entry->so = NULL;
431     GNUNET_STATISTICS_update (stats, "# revocation set unions failed",
432                               1, GNUNET_NO);
433     break;
434   case GNUNET_SET_STATUS_HALF_DONE:
435     break;
436   case GNUNET_SET_STATUS_DONE:
437     peer_entry->so = NULL;
438     GNUNET_STATISTICS_update (stats, "# revocation set unions completed",
439                               1, GNUNET_NO);
440     break;
441   default:
442     GNUNET_break (0);
443     break;
444  }
445 }
446
447
448 /**
449  * The timeout for performing the set union has expired,
450  * run the set operation on the revocation certificates.
451  *
452  * @param cls NULL
453  * @param tc scheduler context (unused)
454  */
455 static void
456 transmit_task_cb (void *cls,
457                   const struct GNUNET_SCHEDULER_TaskContext *tc)
458 {
459   struct PeerEntry *peer_entry = cls;
460   uint16_t salt;
461
462   salt = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
463                                               UINT16_MAX);
464   peer_entry->transmit_task = GNUNET_SCHEDULER_NO_TASK;
465   peer_entry->so = GNUNET_SET_prepare (&peer_entry->id,
466                                        &revocation_set_union_app_id,
467                                        NULL, salt,
468                                        GNUNET_SET_RESULT_ADDED,
469                                        &add_revocation,
470                                        peer_entry);
471   if (GNUNET_OK !=
472       GNUNET_SET_commit (peer_entry->so,
473                          revocation_set))
474   {
475     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
476                 _("SET service crashed, terminating revocation service\n"));
477     GNUNET_SCHEDULER_shutdown ();
478     return;
479   }
480 }
481
482
483 /**
484  * Method called whenever a peer connects. Sets up the PeerEntry and
485  * schedules the initial revocation set exchange with this peer.
486  *
487  * @param cls closure
488  * @param peer peer identity this notification is about
489  */
490 static void
491 handle_core_connect (void *cls,
492                      const struct GNUNET_PeerIdentity *peer)
493 {
494   struct PeerEntry *peer_entry;
495   struct GNUNET_HashCode my_hash;
496   struct GNUNET_HashCode peer_hash;
497
498   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
499               "Peer `%s' connected to us\n",
500               GNUNET_i2s (peer));
501   peer_entry = GNUNET_CONTAINER_multipeermap_get (peers,
502                                                   peer);
503   if (NULL == peer_entry)
504   {
505     peer_entry = GNUNET_new (struct PeerEntry);
506     peer_entry->id = *peer;
507     GNUNET_assert (GNUNET_OK ==
508                    GNUNET_CONTAINER_multipeermap_put (peers, peer,
509                                                       peer_entry,
510                                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
511   }
512   else
513   {
514     GNUNET_assert (NULL == peer_entry->mq);
515   }
516   peer_entry->mq = GNUNET_CORE_mq_create (core_api, peer);
517   GNUNET_CRYPTO_hash (&my_identity, sizeof (my_identity), &my_hash);
518   GNUNET_CRYPTO_hash (peer, sizeof (*peer), &peer_hash);
519   if (0 < GNUNET_CRYPTO_hash_cmp (&my_hash,
520                                   &peer_hash))
521   {
522     peer_entry->transmit_task =
523       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
524                                     &transmit_task_cb,
525                                     peer_entry);
526   }
527   GNUNET_STATISTICS_update (stats, "# peers connected", 1, GNUNET_NO);
528 }
529
530
531 /**
532  * Method called whenever a peer disconnects. Deletes the PeerEntry and cancels
533  * any pending transmission requests to that peer.
534  *
535  * @param cls closure
536  * @param peer peer identity this notification is about
537  */
538 static void
539 handle_core_disconnect (void *cls,
540                         const struct GNUNET_PeerIdentity *peer)
541 {
542   struct PeerEntry *pos;
543
544   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
545               "Peer `%s' disconnected from us\n",
546               GNUNET_i2s (peer));
547   pos = GNUNET_CONTAINER_multipeermap_get (peers, peer);
548   if (NULL == pos)
549   {
550     GNUNET_break (0);
551     return;
552   }
553   GNUNET_assert (GNUNET_YES ==
554                  GNUNET_CONTAINER_multipeermap_remove (peers, peer,
555                                                        pos));
556   GNUNET_MQ_destroy (pos->mq);
557   if (pos->transmit_task != GNUNET_SCHEDULER_NO_TASK)
558   {
559     GNUNET_SCHEDULER_cancel (pos->transmit_task);
560     pos->transmit_task = GNUNET_SCHEDULER_NO_TASK;
561   }
562   if (NULL != pos->so)
563   {
564     GNUNET_SET_operation_cancel (pos->so);
565     pos->so = NULL;
566   }
567   GNUNET_free (pos);
568   GNUNET_STATISTICS_update (stats, "# peers connected", -1, GNUNET_NO);
569 }
570
571
572 /**
573  * Free all values in a hash map.
574  *
575  * @param cls NULL
576  * @param key the key
577  * @param value value to free
578  * @return #GNUNET_OK (continue to iterate)
579  */
580 static int
581 free_entry (void *cls,
582             const struct GNUNET_HashCode *key,
583             void *value)
584 {
585   GNUNET_free (value);
586   return GNUNET_OK;
587 }
588
589
590 /**
591  * Task run during shutdown.
592  *
593  * @param cls unused
594  * @param tc unused
595  */
596 static void
597 shutdown_task (void *cls,
598                const struct GNUNET_SCHEDULER_TaskContext *tc)
599 {
600   if (NULL != revocation_set)
601   {
602     GNUNET_SET_destroy (revocation_set);
603     revocation_set = NULL;
604   }
605   if (NULL != revocation_union_listen_handle)
606   {
607     GNUNET_SET_listen_cancel (revocation_union_listen_handle);
608     revocation_union_listen_handle = NULL;
609   }
610   if (NULL != core_api)
611   {
612     GNUNET_CORE_disconnect (core_api);
613     core_api = NULL;
614   }
615   if (NULL != stats)
616   {
617     GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
618     stats = NULL;
619   }
620   if (NULL != peers)
621   {
622     GNUNET_CONTAINER_multipeermap_destroy (peers);
623     peers = NULL;
624   }
625   if (NULL != nc)
626   {
627     GNUNET_SERVER_notification_context_destroy (nc);
628     nc = NULL;
629   }
630   if (NULL != revocation_db)
631   {
632     GNUNET_DISK_file_close (revocation_db);
633     revocation_db = NULL;
634   }
635   GNUNET_CONTAINER_multihashmap_iterate (revocation_map,
636                                          &free_entry,
637                                          NULL);
638   GNUNET_CONTAINER_multihashmap_destroy (revocation_map);
639 }
640
641
642 /**
643  * Called on core init/fail.
644  *
645  * @param cls service closure
646  * @param identity the public identity of this peer
647  */
648 static void
649 core_init (void *cls,
650            const struct GNUNET_PeerIdentity *identity)
651 {
652   if (NULL == identity)
653   {
654     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
655                 "Connection to core FAILED!\n");
656     GNUNET_SCHEDULER_shutdown ();
657     return;
658   }
659   my_identity = *identity;
660 }
661
662
663 /**
664  * Called when another peer wants to do a set operation with the
665  * local peer. If a listen error occurs, the 'request' is NULL.
666  *
667  * @param cls closure
668  * @param other_peer the other peer
669  * @param context_msg message with application specific information from
670  *        the other peer
671  * @param request request from the other peer (never NULL), use GNUNET_SET_accept()
672  *        to accept it, otherwise the request will be refused
673  *        Note that we can't just return value from the listen callback,
674  *        as it is also necessary to specify the set we want to do the
675  *        operation with, whith sometimes can be derived from the context
676  *        message. It's necessary to specify the timeout.
677  */
678 static void
679 handle_revocation_union_request (void *cls,
680                                  const struct GNUNET_PeerIdentity *other_peer,
681                                  const struct GNUNET_MessageHeader *context_msg,
682                                  struct GNUNET_SET_Request *request)
683 {
684   struct PeerEntry *peer_entry;
685
686   if (NULL == request)
687   {
688     GNUNET_break (0);
689     return;
690   }
691   peer_entry = GNUNET_CONTAINER_multipeermap_get (peers,
692                                                   other_peer);
693   if (NULL == peer_entry)
694   {
695     peer_entry = GNUNET_new (struct PeerEntry);
696     peer_entry->id = *other_peer;
697     GNUNET_assert (GNUNET_OK ==
698                    GNUNET_CONTAINER_multipeermap_put (peers, other_peer,
699                                                       peer_entry,
700                                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
701   }
702   peer_entry->so = GNUNET_SET_accept (request,
703                                       GNUNET_SET_RESULT_ADDED,
704                                       &add_revocation,
705                                       peer_entry);
706 }
707
708
709 /**
710  * Handle network size estimate clients.
711  *
712  * @param cls closure
713  * @param server the initialized server
714  * @param c configuration to use
715  */
716 static void
717 run (void *cls,
718      struct GNUNET_SERVER_Handle *server,
719      const struct GNUNET_CONFIGURATION_Handle *c)
720 {
721   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
722     {&handle_query_message, NULL, GNUNET_MESSAGE_TYPE_REVOCATION_QUERY,
723      sizeof (struct QueryMessage)},
724     {&handle_revoke_message, NULL, GNUNET_MESSAGE_TYPE_REVOCATION_REVOKE,
725      sizeof (struct RevokeMessage)},
726     {NULL, NULL, 0, 0}
727   };
728   static const struct GNUNET_CORE_MessageHandler core_handlers[] = {
729     {&handle_p2p_revoke_message, GNUNET_MESSAGE_TYPE_REVOCATION_REVOKE,
730      sizeof (struct RevokeMessage)},
731     {NULL, 0, 0}
732   };
733   char *fn;
734   uint64_t left;
735   struct RevokeMessage *rm;
736   struct GNUNET_HashCode hc;
737
738   if (GNUNET_OK !=
739       GNUNET_CONFIGURATION_get_value_filename (c,
740                                                "REVOCATION",
741                                                "DATABASE",
742                                                &fn))
743   {
744     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
745                                "REVOCATION",
746                                "DATABASE");
747     GNUNET_SCHEDULER_shutdown ();
748     return;
749   }
750   cfg = c;
751   srv = server;
752   revocation_map = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO);
753   nc = GNUNET_SERVER_notification_context_create (server, 1);
754   if (GNUNET_OK !=
755       GNUNET_CONFIGURATION_get_value_number (cfg, "REVOCATION", "WORKBITS",
756                                              &revocation_work_required))
757   {
758     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
759                                "REVOCATION",
760                                "WORKBITS");
761     GNUNET_SCHEDULER_shutdown ();
762     GNUNET_free (fn);
763     return;
764   }
765   if (revocation_work_required >= sizeof (struct GNUNET_HashCode) * 8)
766   {
767     GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
768                                "REVOCATION",
769                                "WORKBITS",
770                                _("Value is too large.\n"));
771     GNUNET_SCHEDULER_shutdown ();
772     GNUNET_free (fn);
773     return;
774   }
775   revocation_set = GNUNET_SET_create (cfg,
776                                       GNUNET_SET_OPERATION_UNION);
777   revocation_union_listen_handle = GNUNET_SET_listen (cfg,
778                                                       GNUNET_SET_OPERATION_UNION,
779                                                       &revocation_set_union_app_id,
780                                                       &handle_revocation_union_request,
781                                                       NULL);
782   revocation_db = GNUNET_DISK_file_open (fn,
783                                          GNUNET_DISK_OPEN_READWRITE |
784                                          GNUNET_DISK_OPEN_CREATE,
785                                          GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE |
786                                          GNUNET_DISK_PERM_GROUP_READ |
787                                          GNUNET_DISK_PERM_OTHER_READ);
788   if (NULL == revocation_db)
789   {
790     GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
791                                "REVOCATION",
792                                "DATABASE",
793                                _("Could not open revocation database file!"));
794     GNUNET_SCHEDULER_shutdown ();
795     GNUNET_free (fn);
796     return;
797   }
798   if (GNUNET_OK !=
799       GNUNET_DISK_file_size (fn, &left, GNUNET_YES, GNUNET_YES))
800     left = 0;
801   while (left > sizeof (struct RevokeMessage))
802   {
803     rm = GNUNET_new (struct RevokeMessage);
804     if (sizeof (struct RevokeMessage) !=
805         GNUNET_DISK_file_read (revocation_db,
806                                rm,
807                                sizeof (struct RevokeMessage)))
808     {
809       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
810                                 "read",
811                                 fn);
812       GNUNET_free (rm);
813       GNUNET_SCHEDULER_shutdown ();
814       GNUNET_free (fn);
815       return;
816     }
817     GNUNET_break (0 == ntohl (rm->reserved));
818     GNUNET_CRYPTO_hash (&rm->public_key,
819                         sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey),
820                         &hc);
821     GNUNET_break (GNUNET_OK ==
822                   GNUNET_CONTAINER_multihashmap_put (revocation_map,
823                                                      &hc,
824                                                      rm,
825                                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
826   }
827   GNUNET_free (fn);
828
829   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
830                                 NULL);
831   peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
832   GNUNET_SERVER_add_handlers (srv, handlers);
833    /* Connect to core service and register core handlers */
834   core_api = GNUNET_CORE_connect (cfg,   /* Main configuration */
835                                  NULL,       /* Closure passed to functions */
836                                  &core_init,    /* Call core_init once connected */
837                                  &handle_core_connect,  /* Handle connects */
838                                  &handle_core_disconnect,       /* Handle disconnects */
839                                  NULL,  /* Don't want notified about all incoming messages */
840                                  GNUNET_NO,     /* For header only inbound notification */
841                                  NULL,  /* Don't want notified about all outbound messages */
842                                  GNUNET_NO,     /* For header only outbound notification */
843                                  core_handlers);        /* Register these handlers */
844   if (NULL == core_api)
845   {
846     GNUNET_SCHEDULER_shutdown ();
847     return;
848   }
849   stats = GNUNET_STATISTICS_create ("revocation", cfg);
850 }
851
852
853 /**
854  * The main function for the network size estimation service.
855  *
856  * @param argc number of arguments from the command line
857  * @param argv command line arguments
858  * @return 0 ok, 1 on error
859  */
860 int
861 main (int argc,
862       char *const *argv)
863 {
864   GNUNET_CRYPTO_hash ("revocation-set-union-application-id",
865                       strlen ("revocation-set-union-application-id"),
866                       &revocation_set_union_app_id);
867   return (GNUNET_OK ==
868           GNUNET_SERVICE_run (argc, argv, "revocation", GNUNET_SERVICE_OPTION_NONE,
869                               &run, NULL)) ? 0 : 1;
870 }
871
872
873 #ifdef LINUX
874 #include <malloc.h>
875
876 /**
877  * MINIMIZE heap size (way below 128k) since this process doesn't need much.
878  */
879 void __attribute__ ((constructor))
880 GNUNET_ARM_memory_init ()
881 {
882   mallopt (M_TRIM_THRESHOLD, 4 * 1024);
883   mallopt (M_TOP_PAD, 1 * 1024);
884   malloc_trim (0);
885 }
886 #endif
887
888
889
890 /* end of gnunet-service-revocation.c */