aboutsummaryrefslogtreecommitdiff
path: root/src/dht/dht_api_get_put.c
blob: 5b5baa531874bd82a2faa1af222f7112029e8d9f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
     This file is part of GNUnet.
     (C) 2009, 2010 Christian Grothoff (and other contributing authors)

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/

/**
 * @file dht/dht_api_get_put.c
 * @brief library to perform DHT gets and puts
 * @author Christian Grothoff
 * @author Nathan Evans
 */

#include "platform.h"
#include "gnunet_constants.h"
#include "gnunet_arm_service.h"
#include "gnunet_protocols.h"
#include "gnunet_util_lib.h"
#include "gnunet_dht_service.h"
#include "dht.h"


/**
 * Perform a PUT operation storing data in the DHT.
 *
 * @param handle handle to DHT service
 * @param key the key to store under
 * @param desired_replication_level estimate of how many
 *                nearest peers this request should reach
 * @param options routing options for this message
 * @param type type of the value
 * @param size number of bytes in data; must be less than 64k
 * @param data the data to store
 * @param exp desired expiration time for the value
 * @param timeout how long to wait for transmission of this request
 * @param cont continuation to call when done (transmitting request to service)
 * @param cont_cls closure for cont
 * @return GNUNET_YES if put message is queued for transmission
 */
void
GNUNET_DHT_put (struct GNUNET_DHT_Handle *handle,
                const GNUNET_HashCode * key,
                uint32_t desired_replication_level,
		enum GNUNET_DHT_RouteOption options,
                enum GNUNET_BLOCK_Type type,
                size_t size,
                const char *data,
                struct GNUNET_TIME_Absolute exp,
                struct GNUNET_TIME_Relative timeout,
		GNUNET_SCHEDULER_Task cont,
		void *cont_cls)
{
  char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
  struct GNUNET_DHT_PutMessage *put_msg;

  if (size >= sizeof (buf) - sizeof (struct GNUNET_DHT_PutMessage))
    {
      GNUNET_break (0);
      return;
    }
  put_msg = (struct GNUNET_DHT_PutMessage*) buf;
  put_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_PUT);
  put_msg->header.size = htons (sizeof (struct GNUNET_DHT_PutMessage) + size);
  put_msg->type = htonl ((uint32_t)type);
  put_msg->expiration = GNUNET_TIME_absolute_hton (exp);
  memcpy (&put_msg[1], data, size);
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
	      "Starting route for %u byte `%s' message of type %u \n",
	      (unsigned int) (sizeof (struct GNUNET_DHT_PutMessage) + size),
	      "PUT", type);
  GNUNET_break (NULL ==
		GNUNET_DHT_route_start (handle, 
					key, 
					desired_replication_level, options,
					&put_msg->header, 
					timeout, 
					NULL, NULL,
					cont, cont_cls));
}



/**
 * Handle to control a get operation.
 */
struct GNUNET_DHT_GetHandle
{
  /**
   * Handle to the actual route operation for the get
   */
  struct GNUNET_DHT_RouteHandle *route_handle;

  /**
   * Iterator to call on data receipt
   */
  GNUNET_DHT_GetIterator iter;

  /**
   * Closure for the iterator callback
   */
  void *iter_cls;

};



/**
 * Iterator called on each result obtained from a generic route
 * operation
 *
 * @param cls the 'struct GNUNET_DHT_GetHandle'
 * @param key key that was used
 * @param outgoing_path path of the message from this peer
 *                      to the target
 * @param reply response
 */
static void
get_reply_iterator (void *cls, 
		    const GNUNET_HashCode *key,
		    const struct GNUNET_PeerIdentity * const *outgoing_path,
		    const struct GNUNET_MessageHeader *reply)
{
  struct GNUNET_DHT_GetHandle *get_handle = cls;
  const struct GNUNET_DHT_GetResultMessage *result;
  const struct GNUNET_PeerIdentity **put_path;
  size_t payload;
  char *path_offset;
  const struct GNUNET_PeerIdentity *pos;
  unsigned int i;
  uint16_t put_path_length;
  uint16_t data_size;

  if (ntohs (reply->type) != GNUNET_MESSAGE_TYPE_DHT_GET_RESULT)
    {
      GNUNET_break (0);
      return;
    }

  GNUNET_assert (ntohs (reply->size) >=
                 sizeof (struct GNUNET_DHT_GetResultMessage));
  result = (const struct GNUNET_DHT_GetResultMessage *) reply;

  put_path = NULL;
  put_path_length = ntohs(result->put_path_length);
  if (put_path_length > 0)
    {
      data_size = ntohs(result->header.size) - (put_path_length * sizeof(struct GNUNET_PeerIdentity)) - sizeof(struct GNUNET_DHT_GetResultMessage);
      path_offset = (char *)&result[1];
      //GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "In get_reply_iterator, result->header.size is %d, put_path_length %d, offset is %d, data_size is %d\n", ntohs(result->header.size), put_path_length, ntohs(result->header.size) - (put_path_length * sizeof(struct GNUNET_PeerIdentity)), data_size);
      path_offset += data_size;
      pos = (const struct GNUNET_PeerIdentity *)path_offset;
      //GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Found put peer %s\n", GNUNET_i2s((const struct GNUNET_PeerIdentity *)path_offset));
      put_path = GNUNET_malloc ((put_path_length + 1) * sizeof (struct GNUNET_PeerIdentity*));
      for (i = 0; i < put_path_length; i++)
        {
          put_path[i] = pos;
          pos++;
        }
      put_path[put_path_length] = NULL;
    }

  payload = ntohs (reply->size) - sizeof(struct GNUNET_DHT_GetResultMessage);
  get_handle->iter (get_handle->iter_cls,
		    GNUNET_TIME_absolute_ntoh (result->expiration),
		    key,
		    outgoing_path,
		    put_path,
		    ntohs (result->type), 
		    payload,
		    &result[1]);
  GNUNET_free_non_null(put_path);
}



/**
 * Perform an asynchronous GET operation on the DHT identified. See
 * also "GNUNET_BLOCK_evaluate".
 *
 * @param handle handle to the DHT service
 * @param timeout how long to wait for transmission of this request to the service
 * @param type expected type of the response object
 * @param key the key to look up
 * @param desired_replication_level estimate of how many
                  nearest peers this request should reach
 * @param options routing options for this message
 * @param bf bloom filter associated with query (can be NULL)
 * @param bf_mutator mutation value for bf
 * @param xquery extended query data (can be NULL, depending on type)
 * @param xquery_size number of bytes in xquery
 * @param iter function to call on each result
 * @param iter_cls closure for iter
 *
 * @return handle to stop the async get
 */
struct GNUNET_DHT_GetHandle *
GNUNET_DHT_get_start (struct GNUNET_DHT_Handle *handle,
                      struct GNUNET_TIME_Relative timeout,
                      enum GNUNET_BLOCK_Type type,
                      const GNUNET_HashCode * key,
                      uint32_t desired_replication_level,
		      enum GNUNET_DHT_RouteOption options,
		      const struct GNUNET_CONTAINER_BloomFilter *bf,
		      int32_t bf_mutator,
		      const void *xquery,
		      size_t xquery_size,
                      GNUNET_DHT_GetIterator iter,
                      void *iter_cls)
{
  struct GNUNET_DHT_GetHandle *get_handle;
  char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
  struct GNUNET_DHT_GetMessage *get_msg;
  size_t bf_size;
    
  bf_size = GNUNET_CONTAINER_bloomfilter_get_size (bf);
  if ( (sizeof (buf) <= 
	sizeof (struct GNUNET_DHT_GetMessage) + xquery_size + bf_size) ||
       (sizeof (buf) <= bf_size))
    {
      GNUNET_break (0);
      return NULL;
    } 
  get_handle = GNUNET_malloc (sizeof (struct GNUNET_DHT_GetHandle));
  get_handle->iter = iter;
  get_handle->iter_cls = iter_cls;
  get_msg = (struct GNUNET_DHT_GetMessage*) buf;
  get_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_GET);
  get_msg->header.size = htons (sizeof (struct GNUNET_DHT_GetMessage) + 
				xquery_size + 
				bf_size);
  get_msg->type = htonl ((uint32_t) type);
  get_msg->bf_mutator = bf_mutator;
  get_msg->xquery_size = htons ((uint16_t) xquery_size);
  get_msg->bf_size = htons (bf_size);
  if (xquery != NULL)
    memcpy (&buf[sizeof(struct GNUNET_DHT_GetMessage)],
	    xquery,
	    xquery_size);
  else
    GNUNET_assert (xquery_size == 0);
  (void) GNUNET_CONTAINER_bloomfilter_get_raw_data (bf,
						    &buf[sizeof(struct GNUNET_DHT_GetMessage) + xquery_size],
						    bf_size);  
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
	      "Starting route for %u byte `%s' message\n",
	      (unsigned int) (sizeof (struct GNUNET_DHT_GetMessage) + xquery_size + bf_size) ,
	      "GET");
  get_handle->route_handle =
    GNUNET_DHT_route_start (handle,
			    key, 
			    desired_replication_level,
			    options,
			    &get_msg->header, 
			    timeout,
                            &get_reply_iterator, get_handle,
			    NULL, NULL);
  GNUNET_break (NULL != get_handle->route_handle);
  return get_handle;
}


/**
 * Stop async DHT-get.
 *
 * @param get_handle handle to the GET operation to stop
 *
 * On return get_handle will no longer be valid, caller
 * must not use again!!!
 */
void
GNUNET_DHT_get_stop (struct GNUNET_DHT_GetHandle *get_handle)
{
  GNUNET_DHT_route_stop (get_handle->route_handle);
  GNUNET_free (get_handle);
}


/* end of dht_api_get_put.c */