2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
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.
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.
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.
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
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"
38 * Perform a PUT operation storing data in the DHT.
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
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,
59 struct GNUNET_TIME_Absolute exp,
60 struct GNUNET_TIME_Relative timeout,
61 GNUNET_SCHEDULER_Task cont,
64 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
65 struct GNUNET_DHT_PutMessage *put_msg;
67 if (size >= sizeof (buf) - sizeof (struct GNUNET_DHT_PutMessage))
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 = htons (type);
76 put_msg->expiration = GNUNET_TIME_absolute_hton (exp);
77 memcpy (&put_msg[1], data, size);
80 GNUNET_DHT_route_start (handle,
82 DEFAULT_PUT_REPLICATION, options,
92 * Handle to control a get operation.
94 struct GNUNET_DHT_GetHandle
97 * Handle to the actual route operation for the get
99 struct GNUNET_DHT_RouteHandle *route_handle;
102 * Iterator to call on data receipt
104 GNUNET_DHT_GetIterator iter;
107 * Closure for the iterator callback
116 * Iterator called on each result obtained from a generic route
119 * @param cls the 'struct GNUNET_DHT_GetHandle'
120 * @param key key that was used
121 * @param reply response
124 get_reply_iterator (void *cls,
125 const GNUNET_HashCode *key,
126 const struct GNUNET_MessageHeader *reply)
128 struct GNUNET_DHT_GetHandle *get_handle = cls;
129 const struct GNUNET_DHT_GetResultMessage *result;
130 const struct GNUNET_PeerIdentity *const*get_path;
131 const struct GNUNET_PeerIdentity *const*put_path;
134 if (ntohs (reply->type) != GNUNET_MESSAGE_TYPE_DHT_GET_RESULT)
137 GNUNET_assert (ntohs (reply->size) >=
138 sizeof (struct GNUNET_DHT_GetResultMessage));
139 result = (const struct GNUNET_DHT_GetResultMessage *) reply;
140 payload = ntohs (reply->size) - sizeof(struct GNUNET_DHT_GetResultMessage);
141 get_path = NULL; // FIXME: parse path info!
142 put_path = NULL; // FIXME: parse path info!
144 get_handle->iter (get_handle->iter_cls,
145 GNUNET_TIME_absolute_ntoh (result->expiration),
149 ntohs (result->type),
157 * Perform an asynchronous GET operation on the DHT identified. See
158 * also "GNUNET_BLOCK_evaluate".
160 * @param handle handle to the DHT service
161 * @param timeout how long to wait for transmission of this request to the service
162 * @param type expected type of the response object
163 * @param key the key to look up
164 * @param options routing options for this message
165 * @param bf bloom filter associated with query (can be NULL)
166 * @param bf_mutator mutation value for bf
167 * @param xquery extrended query data (can be NULL, depending on type)
168 * @param xquery_size number of bytes in xquery
169 * @param iter function to call on each result
170 * @param iter_cls closure for iter
172 * @return handle to stop the async get
174 struct GNUNET_DHT_GetHandle *
175 GNUNET_DHT_get_start (struct GNUNET_DHT_Handle *handle,
176 struct GNUNET_TIME_Relative timeout,
177 enum GNUNET_BLOCK_Type type,
178 const GNUNET_HashCode * key,
179 enum GNUNET_DHT_RouteOption options,
180 const struct GNUNET_CONTAINER_BloomFilter *bf,
184 GNUNET_DHT_GetIterator iter,
187 struct GNUNET_DHT_GetHandle *get_handle;
188 struct GNUNET_DHT_GetMessage get_msg;
190 /* FIXME: transmit bf, mutator, xquery & xquery_size as well... */
191 get_handle = GNUNET_malloc (sizeof (struct GNUNET_DHT_GetHandle));
192 get_handle->iter = iter;
193 get_handle->iter_cls = iter_cls;
194 get_msg.header.type = htons (GNUNET_MESSAGE_TYPE_DHT_GET);
195 get_msg.header.size = htons (sizeof (struct GNUNET_DHT_GetMessage));
196 get_msg.type = htons (type);
197 get_handle->route_handle =
198 GNUNET_DHT_route_start (handle,
200 DEFAULT_GET_REPLICATION,
204 &get_reply_iterator, get_handle,
206 GNUNET_break (NULL != get_handle->route_handle);
212 * Stop async DHT-get.
214 * @param get_handle handle to the GET operation to stop
217 GNUNET_DHT_get_stop (struct GNUNET_DHT_GetHandle *get_handle)
219 GNUNET_DHT_route_stop (get_handle->route_handle);
220 GNUNET_free (get_handle);
224 /* end of dht_api_get_put.c */