first batch of license fixes (boring)
[oweals/gnunet.git] / src / consensus / plugin_block_consensus.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2017 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU 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
16 /**
17  * @file consensus/plugin_block_consensus.c
18  * @brief consensus block, either nested block or marker
19  * @author Christian Grothoff
20  */
21
22 #include "platform.h"
23 #include "consensus_protocol.h"
24 #include "gnunet_block_plugin.h"
25 #include "gnunet_block_group_lib.h"
26
27
28 /**
29  * Function called to validate a reply or a request.  For
30  * request evaluation, simply pass "NULL" for the reply_block.
31  *
32  * @param cls closure
33  * @param ctx context
34  * @param type block type
35  * @param group block group to use
36  * @param eo control flags
37  * @param query original query (hash)
38  * @param xquery extrended query data (can be NULL, depending on type)
39  * @param xquery_size number of bytes in xquery
40  * @param reply_block response to validate
41  * @param reply_block_size number of bytes in reply block
42  * @return characterization of result
43  */
44 static enum GNUNET_BLOCK_EvaluationResult
45 block_plugin_consensus_evaluate (void *cls,
46                                  struct GNUNET_BLOCK_Context *ctx,
47                                  enum GNUNET_BLOCK_Type type,
48                                  struct GNUNET_BLOCK_Group *group,
49                                  enum GNUNET_BLOCK_EvaluationOptions eo,
50                                  const struct GNUNET_HashCode *query,
51                                  const void *xquery,
52                                  size_t xquery_size,
53                                  const void *reply_block,
54                                  size_t reply_block_size)
55 {
56   if (reply_block_size < sizeof (struct ConsensusElement))
57     return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
58
59   const struct ConsensusElement *ce = reply_block;
60
61   if ( (0 != ce->marker) ||
62        (0 == ce->payload_type ) )
63     return GNUNET_BLOCK_EVALUATION_OK_MORE;
64
65   return GNUNET_BLOCK_evaluate (ctx,
66                                 type,
67                                 group,
68                                 eo,
69                                 query,
70                                 xquery,
71                                 xquery_size,
72                                 &ce[1],
73                                 reply_block_size - sizeof (struct ConsensusElement));
74 }
75
76
77 /**
78  * Function called to obtain the key for a block.
79  *
80  * @param cls closure
81  * @param type block type
82  * @param block block to get the key for
83  * @param block_size number of bytes in block
84  * @param key set to the key (query) for the given block
85  * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
86  *         (or if extracting a key from a block of this type does not work)
87  */
88 static int
89 block_plugin_consensus_get_key (void *cls,
90                                enum GNUNET_BLOCK_Type type,
91                                const void *block,
92                                size_t block_size,
93                                struct GNUNET_HashCode *key)
94 {
95   return GNUNET_SYSERR;
96 }
97
98
99 /**
100  * Entry point for the plugin.
101  */
102 void *
103 libgnunet_plugin_block_consensus_init (void *cls)
104 {
105   static enum GNUNET_BLOCK_Type types[] =
106   {
107     GNUNET_BLOCK_TYPE_CONSENSUS_ELEMENT,
108     GNUNET_BLOCK_TYPE_ANY       /* end of list */
109   };
110   struct GNUNET_BLOCK_PluginFunctions *api;
111
112   api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
113   api->evaluate = &block_plugin_consensus_evaluate;
114   api->get_key = &block_plugin_consensus_get_key;
115   api->types = types;
116   return api;
117 }
118
119
120 /**
121  * Exit point from the plugin.
122  */
123 void *
124 libgnunet_plugin_block_consensus_done (void *cls)
125 {
126   struct GNUNET_BLOCK_PluginFunctions *api = cls;
127
128   GNUNET_free (api);
129   return NULL;
130 }
131
132 /* end of plugin_block_consensus.c */