Fix for #4553
[oweals/gnunet.git] / src / fs / gnunet-service-fs_put.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2011 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file fs/gnunet-service-fs_put.c
23  * @brief API to PUT zero-anonymity index data from our datastore into the DHT
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet-service-fs.h"
28 #include "gnunet-service-fs_put.h"
29
30
31 /**
32  * How often do we at most PUT content into the DHT?
33  */
34 #define MAX_DHT_PUT_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
35
36 /**
37  * How many replicas do we try to create per PUT?
38  */
39 #define DEFAULT_PUT_REPLICATION 5
40
41
42 /**
43  * Context for each zero-anonymity iterator.
44  */
45 struct PutOperator
46 {
47
48   /**
49    * Request to datastore for DHT PUTs (or NULL).
50    */
51   struct GNUNET_DATASTORE_QueueEntry *dht_qe;
52
53   /**
54    * Type we request from the datastore.
55    */
56   enum GNUNET_BLOCK_Type dht_put_type;
57
58   /**
59    * Handle to PUT operation.
60    */
61   struct GNUNET_DHT_PutHandle *dht_put;
62
63   /**
64    * ID of task that collects blocks for DHT PUTs.
65    */
66   struct GNUNET_SCHEDULER_Task * dht_task;
67
68   /**
69    * How many entires with zero anonymity of our type do we currently
70    * estimate to have in the database?
71    */
72   uint64_t zero_anonymity_count_estimate;
73
74   /**
75    * Current offset when iterating the database.
76    */
77   uint64_t current_offset;
78 };
79
80
81 /**
82  * ANY-terminated list of our operators (one per type
83  * of block that we're putting into the DHT).
84  */
85 static struct PutOperator operators[] = {
86   {NULL, GNUNET_BLOCK_TYPE_FS_UBLOCK, 0, 0, 0},
87   {NULL, GNUNET_BLOCK_TYPE_ANY, 0, 0, 0}
88 };
89
90
91 /**
92  * Task that is run periodically to obtain blocks for DHT PUTs.
93  *
94  * @param cls type of blocks to gather
95  * @param tc scheduler context (unused)
96  */
97 static void
98 gather_dht_put_blocks (void *cls);
99
100
101 /**
102  * Calculate when to run the next PUT operation and schedule it.
103  *
104  * @param po put operator to schedule
105  */
106 static void
107 schedule_next_put (struct PutOperator *po)
108 {
109   struct GNUNET_TIME_Relative delay;
110
111   if (po->zero_anonymity_count_estimate > 0)
112   {
113     delay =
114         GNUNET_TIME_relative_divide (GNUNET_DHT_DEFAULT_REPUBLISH_FREQUENCY,
115                                      po->zero_anonymity_count_estimate);
116     delay = GNUNET_TIME_relative_min (delay, MAX_DHT_PUT_FREQ);
117   }
118   else
119   {
120     /* if we have NO zero-anonymity content yet, wait 5 minutes for some to
121      * (hopefully) appear */
122     delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5);
123   }
124   po->dht_task =
125       GNUNET_SCHEDULER_add_delayed (delay, &gather_dht_put_blocks, po);
126 }
127
128
129 /**
130  * Continuation called after DHT PUT operation has finished.
131  *
132  * @param cls type of blocks to gather
133  * @param success GNUNET_OK if the PUT was transmitted,
134  *                GNUNET_NO on timeout,
135  *                GNUNET_SYSERR on disconnect from service
136  *                after the PUT message was transmitted
137  *                (so we don't know if it was received or not)
138  */
139 static void
140 delay_dht_put_blocks (void *cls, int success)
141 {
142   struct PutOperator *po = cls;
143
144   po->dht_put = NULL;
145   schedule_next_put (po);
146 }
147
148
149 /**
150  * Task that is run periodically to obtain blocks for DHT PUTs.
151  *
152  * @param cls type of blocks to gather
153  */
154 static void
155 delay_dht_put_task (void *cls)
156 {
157   struct PutOperator *po = cls;
158
159   po->dht_task = NULL;
160   schedule_next_put (po);
161 }
162
163
164 /**
165  * Store content in DHT.
166  *
167  * @param cls closure
168  * @param key key for the content
169  * @param size number of bytes in data
170  * @param data content stored
171  * @param type type of the content
172  * @param priority priority of the content
173  * @param anonymity anonymity-level for the content
174  * @param expiration expiration time for the content
175  * @param uid unique identifier for the datum;
176  *        maybe 0 if no unique identifier is available
177  */
178 static void
179 process_dht_put_content (void *cls,
180                          const struct GNUNET_HashCode * key,
181                          size_t size,
182                          const void *data,
183                          enum GNUNET_BLOCK_Type type,
184                          uint32_t priority, uint32_t anonymity,
185                          struct GNUNET_TIME_Absolute expiration, uint64_t uid)
186 {
187   struct PutOperator *po = cls;
188
189   po->dht_qe = NULL;
190   if (key == NULL)
191   {
192     po->zero_anonymity_count_estimate = po->current_offset - 1;
193     po->current_offset = 0;
194     po->dht_task = GNUNET_SCHEDULER_add_now (&delay_dht_put_task, po);
195     return;
196   }
197   po->zero_anonymity_count_estimate =
198       GNUNET_MAX (po->current_offset, po->zero_anonymity_count_estimate);
199   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
200               "Retrieved block `%s' of type %u for DHT PUT\n", GNUNET_h2s (key),
201               type);
202   po->dht_put = GNUNET_DHT_put (GSF_dht, key, DEFAULT_PUT_REPLICATION,
203                                 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, type, size, data,
204                                 expiration, GNUNET_TIME_UNIT_FOREVER_REL,
205                                 &delay_dht_put_blocks, po);
206 }
207
208
209 /**
210  * Task that is run periodically to obtain blocks for DHT PUTs.
211  *
212  * @param cls type of blocks to gather
213  */
214 static void
215 gather_dht_put_blocks (void *cls)
216 {
217   struct PutOperator *po = cls;
218
219   po->dht_task = NULL;
220   po->dht_qe =
221       GNUNET_DATASTORE_get_zero_anonymity (GSF_dsh, po->current_offset++, 0,
222                                            UINT_MAX,
223                                            GNUNET_TIME_UNIT_FOREVER_REL,
224                                            po->dht_put_type,
225                                            &process_dht_put_content, po);
226   if (NULL == po->dht_qe)
227     po->dht_task = GNUNET_SCHEDULER_add_now (&delay_dht_put_task, po);
228 }
229
230
231 /**
232  * Setup the module.
233  */
234 void
235 GSF_put_init_ ()
236 {
237   unsigned int i;
238
239   i = 0;
240   while (operators[i].dht_put_type != GNUNET_BLOCK_TYPE_ANY)
241   {
242     operators[i].dht_task =
243         GNUNET_SCHEDULER_add_now (&gather_dht_put_blocks, &operators[i]);
244     i++;
245   }
246 }
247
248
249 /**
250  * Shutdown the module.
251  */
252 void
253 GSF_put_done_ ()
254 {
255   struct PutOperator *po;
256   unsigned int i;
257
258   i = 0;
259   while ((po = &operators[i])->dht_put_type != GNUNET_BLOCK_TYPE_ANY)
260   {
261     if (NULL != po->dht_task)
262     {
263       GNUNET_SCHEDULER_cancel (po->dht_task);
264       po->dht_task = NULL;
265     }
266     if (NULL != po->dht_put)
267     {
268       GNUNET_DHT_put_cancel (po->dht_put);
269       po->dht_put = NULL;
270     }
271     if (NULL != po->dht_qe)
272     {
273       GNUNET_DATASTORE_cancel (po->dht_qe);
274       po->dht_qe = NULL;
275     }
276     i++;
277   }
278 }
279
280 /* end of gnunet-service-fs_put.c */