diff options
Diffstat (limited to 'src/dht/dht_api_get_put.c')
-rw-r--r-- | src/dht/dht_api_get_put.c | 163 |
1 files changed, 78 insertions, 85 deletions
diff --git a/src/dht/dht_api_get_put.c b/src/dht/dht_api_get_put.c index 5b5baa531..30b73e1f1 100644 --- a/src/dht/dht_api_get_put.c +++ b/src/dht/dht_api_get_put.c | |||
@@ -55,41 +55,38 @@ void | |||
55 | GNUNET_DHT_put (struct GNUNET_DHT_Handle *handle, | 55 | GNUNET_DHT_put (struct GNUNET_DHT_Handle *handle, |
56 | const GNUNET_HashCode * key, | 56 | const GNUNET_HashCode * key, |
57 | uint32_t desired_replication_level, | 57 | uint32_t desired_replication_level, |
58 | enum GNUNET_DHT_RouteOption options, | 58 | enum GNUNET_DHT_RouteOption options, |
59 | enum GNUNET_BLOCK_Type type, | 59 | enum GNUNET_BLOCK_Type type, |
60 | size_t size, | 60 | size_t size, |
61 | const char *data, | 61 | const char *data, |
62 | struct GNUNET_TIME_Absolute exp, | 62 | struct GNUNET_TIME_Absolute exp, |
63 | struct GNUNET_TIME_Relative timeout, | 63 | struct GNUNET_TIME_Relative timeout, |
64 | GNUNET_SCHEDULER_Task cont, | 64 | GNUNET_SCHEDULER_Task cont, void *cont_cls) |
65 | void *cont_cls) | ||
66 | { | 65 | { |
67 | char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE]; | 66 | char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE]; |
68 | struct GNUNET_DHT_PutMessage *put_msg; | 67 | struct GNUNET_DHT_PutMessage *put_msg; |
69 | 68 | ||
70 | if (size >= sizeof (buf) - sizeof (struct GNUNET_DHT_PutMessage)) | 69 | if (size >= sizeof (buf) - sizeof (struct GNUNET_DHT_PutMessage)) |
71 | { | 70 | { |
72 | GNUNET_break (0); | 71 | GNUNET_break (0); |
73 | return; | 72 | return; |
74 | } | 73 | } |
75 | put_msg = (struct GNUNET_DHT_PutMessage*) buf; | 74 | put_msg = (struct GNUNET_DHT_PutMessage *) buf; |
76 | put_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_PUT); | 75 | put_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_PUT); |
77 | put_msg->header.size = htons (sizeof (struct GNUNET_DHT_PutMessage) + size); | 76 | put_msg->header.size = htons (sizeof (struct GNUNET_DHT_PutMessage) + size); |
78 | put_msg->type = htonl ((uint32_t)type); | 77 | put_msg->type = htonl ((uint32_t) type); |
79 | put_msg->expiration = GNUNET_TIME_absolute_hton (exp); | 78 | put_msg->expiration = GNUNET_TIME_absolute_hton (exp); |
80 | memcpy (&put_msg[1], data, size); | 79 | memcpy (&put_msg[1], data, size); |
81 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 80 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
82 | "Starting route for %u byte `%s' message of type %u \n", | 81 | "Starting route for %u byte `%s' message of type %u \n", |
83 | (unsigned int) (sizeof (struct GNUNET_DHT_PutMessage) + size), | 82 | (unsigned int) (sizeof (struct GNUNET_DHT_PutMessage) + size), |
84 | "PUT", type); | 83 | "PUT", type); |
85 | GNUNET_break (NULL == | 84 | GNUNET_break (NULL == |
86 | GNUNET_DHT_route_start (handle, | 85 | GNUNET_DHT_route_start (handle, |
87 | key, | 86 | key, |
88 | desired_replication_level, options, | 87 | desired_replication_level, options, |
89 | &put_msg->header, | 88 | &put_msg->header, |
90 | timeout, | 89 | timeout, NULL, NULL, cont, cont_cls)); |
91 | NULL, NULL, | ||
92 | cont, cont_cls)); | ||
93 | } | 90 | } |
94 | 91 | ||
95 | 92 | ||
@@ -129,10 +126,10 @@ struct GNUNET_DHT_GetHandle | |||
129 | * @param reply response | 126 | * @param reply response |
130 | */ | 127 | */ |
131 | static void | 128 | static void |
132 | get_reply_iterator (void *cls, | 129 | get_reply_iterator (void *cls, |
133 | const GNUNET_HashCode *key, | 130 | const GNUNET_HashCode * key, |
134 | const struct GNUNET_PeerIdentity * const *outgoing_path, | 131 | const struct GNUNET_PeerIdentity *const *outgoing_path, |
135 | const struct GNUNET_MessageHeader *reply) | 132 | const struct GNUNET_MessageHeader *reply) |
136 | { | 133 | { |
137 | struct GNUNET_DHT_GetHandle *get_handle = cls; | 134 | struct GNUNET_DHT_GetHandle *get_handle = cls; |
138 | const struct GNUNET_DHT_GetResultMessage *result; | 135 | const struct GNUNET_DHT_GetResultMessage *result; |
@@ -145,44 +142,46 @@ get_reply_iterator (void *cls, | |||
145 | uint16_t data_size; | 142 | uint16_t data_size; |
146 | 143 | ||
147 | if (ntohs (reply->type) != GNUNET_MESSAGE_TYPE_DHT_GET_RESULT) | 144 | if (ntohs (reply->type) != GNUNET_MESSAGE_TYPE_DHT_GET_RESULT) |
148 | { | 145 | { |
149 | GNUNET_break (0); | 146 | GNUNET_break (0); |
150 | return; | 147 | return; |
151 | } | 148 | } |
152 | 149 | ||
153 | GNUNET_assert (ntohs (reply->size) >= | 150 | GNUNET_assert (ntohs (reply->size) >= |
154 | sizeof (struct GNUNET_DHT_GetResultMessage)); | 151 | sizeof (struct GNUNET_DHT_GetResultMessage)); |
155 | result = (const struct GNUNET_DHT_GetResultMessage *) reply; | 152 | result = (const struct GNUNET_DHT_GetResultMessage *) reply; |
156 | 153 | ||
157 | put_path = NULL; | 154 | put_path = NULL; |
158 | put_path_length = ntohs(result->put_path_length); | 155 | put_path_length = ntohs (result->put_path_length); |
159 | if (put_path_length > 0) | 156 | if (put_path_length > 0) |
157 | { | ||
158 | data_size = | ||
159 | ntohs (result->header.size) - | ||
160 | (put_path_length * sizeof (struct GNUNET_PeerIdentity)) - | ||
161 | sizeof (struct GNUNET_DHT_GetResultMessage); | ||
162 | path_offset = (char *) &result[1]; | ||
163 | //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); | ||
164 | path_offset += data_size; | ||
165 | pos = (const struct GNUNET_PeerIdentity *) path_offset; | ||
166 | //GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Found put peer %s\n", GNUNET_i2s((const struct GNUNET_PeerIdentity *)path_offset)); | ||
167 | put_path = | ||
168 | GNUNET_malloc ((put_path_length + | ||
169 | 1) * sizeof (struct GNUNET_PeerIdentity *)); | ||
170 | for (i = 0; i < put_path_length; i++) | ||
160 | { | 171 | { |
161 | data_size = ntohs(result->header.size) - (put_path_length * sizeof(struct GNUNET_PeerIdentity)) - sizeof(struct GNUNET_DHT_GetResultMessage); | 172 | put_path[i] = pos; |
162 | path_offset = (char *)&result[1]; | 173 | pos++; |
163 | //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); | ||
164 | path_offset += data_size; | ||
165 | pos = (const struct GNUNET_PeerIdentity *)path_offset; | ||
166 | //GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Found put peer %s\n", GNUNET_i2s((const struct GNUNET_PeerIdentity *)path_offset)); | ||
167 | put_path = GNUNET_malloc ((put_path_length + 1) * sizeof (struct GNUNET_PeerIdentity*)); | ||
168 | for (i = 0; i < put_path_length; i++) | ||
169 | { | ||
170 | put_path[i] = pos; | ||
171 | pos++; | ||
172 | } | ||
173 | put_path[put_path_length] = NULL; | ||
174 | } | 174 | } |
175 | put_path[put_path_length] = NULL; | ||
176 | } | ||
175 | 177 | ||
176 | payload = ntohs (reply->size) - sizeof(struct GNUNET_DHT_GetResultMessage); | 178 | payload = ntohs (reply->size) - sizeof (struct GNUNET_DHT_GetResultMessage); |
177 | get_handle->iter (get_handle->iter_cls, | 179 | get_handle->iter (get_handle->iter_cls, |
178 | GNUNET_TIME_absolute_ntoh (result->expiration), | 180 | GNUNET_TIME_absolute_ntoh (result->expiration), |
179 | key, | 181 | key, |
180 | outgoing_path, | 182 | outgoing_path, |
181 | put_path, | 183 | put_path, ntohs (result->type), payload, &result[1]); |
182 | ntohs (result->type), | 184 | GNUNET_free_non_null (put_path); |
183 | payload, | ||
184 | &result[1]); | ||
185 | GNUNET_free_non_null(put_path); | ||
186 | } | 185 | } |
187 | 186 | ||
188 | 187 | ||
@@ -213,61 +212,55 @@ GNUNET_DHT_get_start (struct GNUNET_DHT_Handle *handle, | |||
213 | enum GNUNET_BLOCK_Type type, | 212 | enum GNUNET_BLOCK_Type type, |
214 | const GNUNET_HashCode * key, | 213 | const GNUNET_HashCode * key, |
215 | uint32_t desired_replication_level, | 214 | uint32_t desired_replication_level, |
216 | enum GNUNET_DHT_RouteOption options, | 215 | enum GNUNET_DHT_RouteOption options, |
217 | const struct GNUNET_CONTAINER_BloomFilter *bf, | 216 | const struct GNUNET_CONTAINER_BloomFilter *bf, |
218 | int32_t bf_mutator, | 217 | int32_t bf_mutator, |
219 | const void *xquery, | 218 | const void *xquery, |
220 | size_t xquery_size, | 219 | size_t xquery_size, |
221 | GNUNET_DHT_GetIterator iter, | 220 | GNUNET_DHT_GetIterator iter, void *iter_cls) |
222 | void *iter_cls) | ||
223 | { | 221 | { |
224 | struct GNUNET_DHT_GetHandle *get_handle; | 222 | struct GNUNET_DHT_GetHandle *get_handle; |
225 | char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1]; | 223 | char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1]; |
226 | struct GNUNET_DHT_GetMessage *get_msg; | 224 | struct GNUNET_DHT_GetMessage *get_msg; |
227 | size_t bf_size; | 225 | size_t bf_size; |
228 | 226 | ||
229 | bf_size = GNUNET_CONTAINER_bloomfilter_get_size (bf); | 227 | bf_size = GNUNET_CONTAINER_bloomfilter_get_size (bf); |
230 | if ( (sizeof (buf) <= | 228 | if ((sizeof (buf) <= |
231 | sizeof (struct GNUNET_DHT_GetMessage) + xquery_size + bf_size) || | 229 | sizeof (struct GNUNET_DHT_GetMessage) + xquery_size + bf_size) || |
232 | (sizeof (buf) <= bf_size)) | 230 | (sizeof (buf) <= bf_size)) |
233 | { | 231 | { |
234 | GNUNET_break (0); | 232 | GNUNET_break (0); |
235 | return NULL; | 233 | return NULL; |
236 | } | 234 | } |
237 | get_handle = GNUNET_malloc (sizeof (struct GNUNET_DHT_GetHandle)); | 235 | get_handle = GNUNET_malloc (sizeof (struct GNUNET_DHT_GetHandle)); |
238 | get_handle->iter = iter; | 236 | get_handle->iter = iter; |
239 | get_handle->iter_cls = iter_cls; | 237 | get_handle->iter_cls = iter_cls; |
240 | get_msg = (struct GNUNET_DHT_GetMessage*) buf; | 238 | get_msg = (struct GNUNET_DHT_GetMessage *) buf; |
241 | get_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_GET); | 239 | get_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_GET); |
242 | get_msg->header.size = htons (sizeof (struct GNUNET_DHT_GetMessage) + | 240 | get_msg->header.size = htons (sizeof (struct GNUNET_DHT_GetMessage) + |
243 | xquery_size + | 241 | xquery_size + bf_size); |
244 | bf_size); | ||
245 | get_msg->type = htonl ((uint32_t) type); | 242 | get_msg->type = htonl ((uint32_t) type); |
246 | get_msg->bf_mutator = bf_mutator; | 243 | get_msg->bf_mutator = bf_mutator; |
247 | get_msg->xquery_size = htons ((uint16_t) xquery_size); | 244 | get_msg->xquery_size = htons ((uint16_t) xquery_size); |
248 | get_msg->bf_size = htons (bf_size); | 245 | get_msg->bf_size = htons (bf_size); |
249 | if (xquery != NULL) | 246 | if (xquery != NULL) |
250 | memcpy (&buf[sizeof(struct GNUNET_DHT_GetMessage)], | 247 | memcpy (&buf[sizeof (struct GNUNET_DHT_GetMessage)], xquery, xquery_size); |
251 | xquery, | ||
252 | xquery_size); | ||
253 | else | 248 | else |
254 | GNUNET_assert (xquery_size == 0); | 249 | GNUNET_assert (xquery_size == 0); |
255 | (void) GNUNET_CONTAINER_bloomfilter_get_raw_data (bf, | 250 | (void) GNUNET_CONTAINER_bloomfilter_get_raw_data (bf, |
256 | &buf[sizeof(struct GNUNET_DHT_GetMessage) + xquery_size], | 251 | &buf[sizeof |
257 | bf_size); | 252 | (struct |
253 | GNUNET_DHT_GetMessage) | ||
254 | + xquery_size], | ||
255 | bf_size); | ||
258 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 256 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
259 | "Starting route for %u byte `%s' message\n", | 257 | "Starting route for %u byte `%s' message\n", |
260 | (unsigned int) (sizeof (struct GNUNET_DHT_GetMessage) + xquery_size + bf_size) , | 258 | (unsigned int) (sizeof (struct GNUNET_DHT_GetMessage) + |
261 | "GET"); | 259 | xquery_size + bf_size), "GET"); |
262 | get_handle->route_handle = | 260 | get_handle->route_handle = |
263 | GNUNET_DHT_route_start (handle, | 261 | GNUNET_DHT_route_start (handle, key, desired_replication_level, options, |
264 | key, | 262 | &get_msg->header, timeout, &get_reply_iterator, |
265 | desired_replication_level, | 263 | get_handle, NULL, NULL); |
266 | options, | ||
267 | &get_msg->header, | ||
268 | timeout, | ||
269 | &get_reply_iterator, get_handle, | ||
270 | NULL, NULL); | ||
271 | GNUNET_break (NULL != get_handle->route_handle); | 264 | GNUNET_break (NULL != get_handle->route_handle); |
272 | return get_handle; | 265 | return get_handle; |
273 | } | 266 | } |