Refactoring gnunet time
[oweals/gnunet.git] / src / dht / dht_api_get_put.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 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 dht/dht_api_get_put.c
23  * @brief library to perform DHT gets and puts
24  * @author Christian Grothoff
25  * @author Nathan Evans
26  */
27
28 #include "platform.h"
29 #include "gnunet_constants.h"
30 #include "gnunet_arm_service.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_util_lib.h"
33 #include "gnunet_dht_service.h"
34 #include "dht.h"
35
36
37 /**
38  * Perform a PUT operation storing data in the DHT.
39  *
40  * @param handle handle to DHT service
41  * @param key the key to store under
42  * @param options routing options for this message
43  * @param type type of the value
44  * @param size number of bytes in data; must be less than 64k
45  * @param data the data to store
46  * @param exp desired expiration time for the value
47  * @param timeout how long to wait for transmission of this request
48  * @param cont continuation to call when done (transmitting request to service)
49  * @param cont_cls closure for cont
50  * @return GNUNET_YES if put message is queued for transmission
51  */
52 void
53 GNUNET_DHT_put (struct GNUNET_DHT_Handle *handle,
54                 const GNUNET_HashCode * key,
55                 enum GNUNET_DHT_RouteOption options,
56                 enum GNUNET_BLOCK_Type type,
57                 size_t size,
58                 const char *data,
59                 struct GNUNET_TIME_Absolute exp,
60                 struct GNUNET_TIME_Relative timeout,
61                 GNUNET_SCHEDULER_Task cont,
62                 void *cont_cls)
63 {
64   char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
65   struct GNUNET_DHT_PutMessage *put_msg;
66
67   if (size >= sizeof (buf) - sizeof (struct GNUNET_DHT_PutMessage))
68     {
69       GNUNET_break (0);
70       return;
71     }
72   put_msg = (struct GNUNET_DHT_PutMessage*) buf;
73   put_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_PUT);
74   put_msg->header.size = htons (sizeof (struct GNUNET_DHT_PutMessage) + size);
75   put_msg->type = htonl ((uint32_t)type);
76   put_msg->expiration = GNUNET_TIME_absolute_hton (exp);
77   memcpy (&put_msg[1], data, size);
78   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
79               "Starting route for %u byte `%s' message of type %u \n",
80               (unsigned int) (sizeof (struct GNUNET_DHT_PutMessage) + size),
81               "PUT", type);
82   GNUNET_break (NULL ==
83                 GNUNET_DHT_route_start (handle, 
84                                         key, 
85                                         DEFAULT_PUT_REPLICATION, options,
86                                         &put_msg->header, 
87                                         timeout, 
88                                         NULL, NULL,
89                                         cont, cont_cls));
90 }
91
92
93
94 /**
95  * Handle to control a get operation.
96  */
97 struct GNUNET_DHT_GetHandle
98 {
99   /**
100    * Handle to the actual route operation for the get
101    */
102   struct GNUNET_DHT_RouteHandle *route_handle;
103
104   /**
105    * Iterator to call on data receipt
106    */
107   GNUNET_DHT_GetIterator iter;
108
109   /**
110    * Closure for the iterator callback
111    */
112   void *iter_cls;
113
114 };
115
116
117
118 /**
119  * Iterator called on each result obtained from a generic route
120  * operation
121  *
122  * @param cls the 'struct GNUNET_DHT_GetHandle'
123  * @param key key that was used
124  * @param get_path NULL-terminated array of pointers
125  *                 to the peers on reverse GET path (or NULL if not recorded)
126  * @param put_path NULL-terminated array of pointers
127  *                 to the peers on the PUT path (or NULL if not recorded)
128  * @param reply response
129  */
130 static void
131 get_reply_iterator (void *cls, 
132                     const GNUNET_HashCode *key,
133                     const struct GNUNET_PeerIdentity * const *get_path,
134                     const struct GNUNET_PeerIdentity * const *put_path,
135                     const struct GNUNET_MessageHeader *reply)
136 {
137   struct GNUNET_DHT_GetHandle *get_handle = cls;
138   const struct GNUNET_DHT_GetResultMessage *result;
139   size_t payload;
140
141   if (ntohs (reply->type) != GNUNET_MESSAGE_TYPE_DHT_GET_RESULT)
142     {
143       GNUNET_break (0);
144       return;
145     }
146
147   GNUNET_assert (ntohs (reply->size) >=
148                  sizeof (struct GNUNET_DHT_GetResultMessage));
149   result = (const struct GNUNET_DHT_GetResultMessage *) reply;
150   payload = ntohs (reply->size) - sizeof(struct GNUNET_DHT_GetResultMessage);
151   get_handle->iter (get_handle->iter_cls,
152                     GNUNET_TIME_absolute_ntoh (result->expiration),
153                     key,
154                     get_path,
155                     put_path,
156                     ntohs (result->type), 
157                     payload,
158                     &result[1]);
159 }
160
161
162
163 /**
164  * Perform an asynchronous GET operation on the DHT identified. See
165  * also "GNUNET_BLOCK_evaluate".
166  *
167  * @param handle handle to the DHT service
168  * @param timeout how long to wait for transmission of this request to the service
169  * @param type expected type of the response object
170  * @param key the key to look up
171  * @param options routing options for this message
172  * @param bf bloom filter associated with query (can be NULL)
173  * @param bf_mutator mutation value for bf
174  * @param xquery extrended query data (can be NULL, depending on type)
175  * @param xquery_size number of bytes in xquery
176  * @param iter function to call on each result
177  * @param iter_cls closure for iter
178  *
179  * @return handle to stop the async get
180  */
181 struct GNUNET_DHT_GetHandle *
182 GNUNET_DHT_get_start (struct GNUNET_DHT_Handle *handle,
183                       struct GNUNET_TIME_Relative timeout,
184                       enum GNUNET_BLOCK_Type type,
185                       const GNUNET_HashCode * key,
186                       enum GNUNET_DHT_RouteOption options,
187                       const struct GNUNET_CONTAINER_BloomFilter *bf,
188                       int32_t bf_mutator,
189                       const void *xquery,
190                       size_t xquery_size,
191                       GNUNET_DHT_GetIterator iter,
192                       void *iter_cls)
193 {
194   struct GNUNET_DHT_GetHandle *get_handle;
195   char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
196   struct GNUNET_DHT_GetMessage *get_msg;
197   size_t bf_size;
198     
199   bf_size = GNUNET_CONTAINER_bloomfilter_get_size (bf);
200   if ( (sizeof (buf) <= 
201         sizeof (struct GNUNET_DHT_GetMessage) + xquery_size + bf_size) ||
202        (sizeof (buf) <= bf_size))
203     {
204       GNUNET_break (0);
205       return NULL;
206     } 
207   get_handle = GNUNET_malloc (sizeof (struct GNUNET_DHT_GetHandle));
208   get_handle->iter = iter;
209   get_handle->iter_cls = iter_cls;
210   get_msg = (struct GNUNET_DHT_GetMessage*) buf;
211   get_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_GET);
212   get_msg->header.size = htons (sizeof (struct GNUNET_DHT_GetMessage) + 
213                                 xquery_size + 
214                                 bf_size);
215   get_msg->type = htonl ((uint32_t) type);
216   get_msg->bf_mutator = bf_mutator;
217   get_msg->xquery_size = htons ((uint16_t) xquery_size);
218   get_msg->bf_size = htons (bf_size);
219   if (xquery != NULL)
220     memcpy (&buf[sizeof(struct GNUNET_DHT_GetMessage)],
221             xquery,
222             xquery_size);
223   else
224     GNUNET_assert (xquery_size == 0);
225   (void) GNUNET_CONTAINER_bloomfilter_get_raw_data (bf,
226                                                     &buf[sizeof(struct GNUNET_DHT_GetMessage) + xquery_size],
227                                                     bf_size);  
228   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
229               "Starting route for %u byte `%s' message\n",
230               (unsigned int) (sizeof (struct GNUNET_DHT_GetMessage) + xquery_size + bf_size) ,
231               "GET");
232   get_handle->route_handle =
233     GNUNET_DHT_route_start (handle,
234                             key, 
235                             DEFAULT_GET_REPLICATION,
236                             options,
237                             &get_msg->header, 
238                             timeout,
239                             &get_reply_iterator, get_handle,
240                             NULL, NULL);
241   GNUNET_break (NULL != get_handle->route_handle);
242   return get_handle;
243 }
244
245
246 /**
247  * Stop async DHT-get.
248  *
249  * @param get_handle handle to the GET operation to stop
250  */
251 void
252 GNUNET_DHT_get_stop (struct GNUNET_DHT_GetHandle *get_handle)
253 {
254   GNUNET_DHT_route_stop (get_handle->route_handle);
255   GNUNET_free (get_handle);
256 }
257
258
259 /* end of dht_api_get_put.c */