diff options
author | Nathan S. Evans <evans@in.tum.de> | 2010-04-04 14:14:15 +0000 |
---|---|---|
committer | Nathan S. Evans <evans@in.tum.de> | 2010-04-04 14:14:15 +0000 |
commit | 90c0d0137a71dc752066ae2922ae0dedf6f8b63e (patch) | |
tree | 7a92db44e539cd98ac239e70591c77507a6ec924 /src/dht/gnunet-service-dht.c | |
parent | f5ee95ef07151a06778fa02a6d74689dcbb0bdf5 (diff) | |
download | gnunet-90c0d0137a71dc752066ae2922ae0dedf6f8b63e.tar.gz gnunet-90c0d0137a71dc752066ae2922ae0dedf6f8b63e.zip |
service does simple put and get into datacache, test case verifies it works
Diffstat (limited to 'src/dht/gnunet-service-dht.c')
-rw-r--r-- | src/dht/gnunet-service-dht.c | 433 |
1 files changed, 309 insertions, 124 deletions
diff --git a/src/dht/gnunet-service-dht.c b/src/dht/gnunet-service-dht.c index c6ddb0ab9..f03cb379b 100644 --- a/src/dht/gnunet-service-dht.c +++ b/src/dht/gnunet-service-dht.c | |||
@@ -40,7 +40,7 @@ | |||
40 | /** | 40 | /** |
41 | * Handle to the datacache service (for inserting/retrieving data) | 41 | * Handle to the datacache service (for inserting/retrieving data) |
42 | */ | 42 | */ |
43 | struct GNUNET_DATACACHE_Handle *datacache; | 43 | struct GNUNET_DATACACHE_Handle *datacache; |
44 | 44 | ||
45 | /** | 45 | /** |
46 | * The main scheduler to use for the DHT service | 46 | * The main scheduler to use for the DHT service |
@@ -72,28 +72,60 @@ static struct GNUNET_PeerIdentity my_identity; | |||
72 | */ | 72 | */ |
73 | static GNUNET_SCHEDULER_TaskIdentifier cleanup_task; | 73 | static GNUNET_SCHEDULER_TaskIdentifier cleanup_task; |
74 | 74 | ||
75 | struct ClientList | 75 | /** |
76 | * Context for handling results from a get request. | ||
77 | */ | ||
78 | struct DatacacheGetContext | ||
76 | { | 79 | { |
77 | /** | 80 | /** |
78 | * This is a linked list | 81 | * The client to send the result to. |
82 | */ | ||
83 | struct GNUNET_SERVER_Client *client; | ||
84 | |||
85 | /** | ||
86 | * The unique id of this request | ||
79 | */ | 87 | */ |
80 | struct ClientList *next; | 88 | unsigned long long unique_id; |
89 | }; | ||
90 | |||
81 | 91 | ||
92 | struct DHT_MessageContext | ||
93 | { | ||
82 | /** | 94 | /** |
83 | * The client in question | 95 | * The client this request was received from. |
84 | */ | 96 | */ |
85 | struct GNUNET_SERVER_Client *client; | 97 | struct GNUNET_SERVER_Client *client; |
98 | |||
99 | /** | ||
100 | * The key this request was about | ||
101 | */ | ||
102 | GNUNET_HashCode *key; | ||
103 | |||
104 | /** | ||
105 | * The unique identifier of this request | ||
106 | */ | ||
107 | unsigned long long unique_id; | ||
108 | |||
109 | /** | ||
110 | * Desired replication level | ||
111 | */ | ||
112 | size_t replication; | ||
113 | |||
114 | /** | ||
115 | * Any message options for this request | ||
116 | */ | ||
117 | size_t msg_options; | ||
86 | }; | 118 | }; |
87 | 119 | ||
88 | /** | 120 | /** |
89 | * Server handler for handling locally received dht requests | 121 | * Server handler for handling locally received dht requests |
90 | */ | 122 | */ |
91 | static void | 123 | static void |
92 | handle_dht_start_message(void *cls, struct GNUNET_SERVER_Client * client, | 124 | handle_dht_start_message (void *cls, struct GNUNET_SERVER_Client *client, |
93 | const struct GNUNET_MessageHeader *message); | 125 | const struct GNUNET_MessageHeader *message); |
94 | 126 | ||
95 | static void | 127 | static void |
96 | handle_dht_stop_message(void *cls, struct GNUNET_SERVER_Client * client, | 128 | handle_dht_stop_message (void *cls, struct GNUNET_SERVER_Client *client, |
97 | const struct GNUNET_MessageHeader *message); | 129 | const struct GNUNET_MessageHeader *message); |
98 | 130 | ||
99 | static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = { | 131 | static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = { |
@@ -107,28 +139,29 @@ static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = { | |||
107 | * Core handler for p2p dht get requests. | 139 | * Core handler for p2p dht get requests. |
108 | */ | 140 | */ |
109 | static int handle_dht_p2p_get (void *cls, | 141 | static int handle_dht_p2p_get (void *cls, |
110 | const struct GNUNET_PeerIdentity * peer, | 142 | const struct GNUNET_PeerIdentity *peer, |
111 | const struct GNUNET_MessageHeader * message, | 143 | const struct GNUNET_MessageHeader *message, |
112 | struct GNUNET_TIME_Relative latency, | 144 | struct GNUNET_TIME_Relative latency, |
113 | uint32_t distance); | 145 | uint32_t distance); |
114 | 146 | ||
115 | /** | 147 | /** |
116 | * Core handler for p2p dht put requests. | 148 | * Core handler for p2p dht put requests. |
117 | */ | 149 | */ |
118 | static int handle_dht_p2p_put (void *cls, | 150 | static int handle_dht_p2p_put (void *cls, |
119 | const struct GNUNET_PeerIdentity * peer, | 151 | const struct GNUNET_PeerIdentity *peer, |
120 | const struct GNUNET_MessageHeader * message, | 152 | const struct GNUNET_MessageHeader *message, |
121 | struct GNUNET_TIME_Relative latency, | 153 | struct GNUNET_TIME_Relative latency, |
122 | uint32_t distance); | 154 | uint32_t distance); |
123 | 155 | ||
124 | /** | 156 | /** |
125 | * Core handler for p2p dht find peer requests. | 157 | * Core handler for p2p dht find peer requests. |
126 | */ | 158 | */ |
127 | static int handle_dht_p2p_find_peer (void *cls, | 159 | static int handle_dht_p2p_find_peer (void *cls, |
128 | const struct GNUNET_PeerIdentity * peer, | 160 | const struct GNUNET_PeerIdentity *peer, |
129 | const struct GNUNET_MessageHeader * message, | 161 | const struct GNUNET_MessageHeader |
130 | struct GNUNET_TIME_Relative latency, | 162 | *message, |
131 | uint32_t distance); | 163 | struct GNUNET_TIME_Relative latency, |
164 | uint32_t distance); | ||
132 | 165 | ||
133 | static struct GNUNET_CORE_MessageHandler core_handlers[] = { | 166 | static struct GNUNET_CORE_MessageHandler core_handlers[] = { |
134 | {&handle_dht_p2p_get, GNUNET_MESSAGE_TYPE_DHT_GET, 0}, | 167 | {&handle_dht_p2p_get, GNUNET_MESSAGE_TYPE_DHT_GET, 0}, |
@@ -138,25 +171,131 @@ static struct GNUNET_CORE_MessageHandler core_handlers[] = { | |||
138 | }; | 171 | }; |
139 | 172 | ||
140 | 173 | ||
174 | static size_t | ||
175 | send_reply (void *cls, size_t size, void *buf) | ||
176 | { | ||
177 | struct GNUNET_DHT_Message *reply = cls; | ||
178 | |||
179 | if (buf == NULL) /* Message timed out, that's crappy... */ | ||
180 | { | ||
181 | GNUNET_free (reply); | ||
182 | return 0; | ||
183 | } | ||
184 | |||
185 | if (size >= ntohs (reply->header.size)) | ||
186 | { | ||
187 | memcpy (buf, reply, ntohs (reply->header.size)); | ||
188 | return ntohs (reply->header.size); | ||
189 | } | ||
190 | else | ||
191 | return 0; | ||
192 | } | ||
193 | |||
194 | |||
195 | static void | ||
196 | send_reply_to_client (struct GNUNET_SERVER_Client *client, | ||
197 | struct GNUNET_MessageHeader *message, | ||
198 | unsigned long long uid) | ||
199 | { | ||
200 | struct GNUNET_DHT_Message *reply; | ||
201 | size_t msize; | ||
202 | size_t tsize; | ||
203 | #if DEBUG_DHT | ||
204 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
205 | "`%s': Sending reply to client.\n", "DHT"); | ||
206 | #endif | ||
207 | msize = ntohs (message->size); | ||
208 | tsize = sizeof (struct GNUNET_DHT_Message) + msize; | ||
209 | reply = GNUNET_malloc (tsize); | ||
210 | reply->header.type = htons (GNUNET_MESSAGE_TYPE_DHT); | ||
211 | reply->header.size = htons (tsize); | ||
212 | if (uid != 0) | ||
213 | reply->unique = htons (GNUNET_YES); | ||
214 | reply->unique_id = GNUNET_htonll (uid); | ||
215 | memcpy (&reply[1], message, msize); | ||
216 | |||
217 | GNUNET_SERVER_notify_transmit_ready (client, | ||
218 | tsize, | ||
219 | GNUNET_TIME_relative_multiply | ||
220 | (GNUNET_TIME_UNIT_SECONDS, 5), | ||
221 | &send_reply, reply); | ||
222 | |||
223 | } | ||
224 | |||
225 | |||
226 | /** | ||
227 | * Iterator for local get request results, return | ||
228 | * GNUNET_OK to continue iteration, anything else | ||
229 | * to stop iteration. | ||
230 | */ | ||
231 | static int | ||
232 | datacache_get_iterator (void *cls, | ||
233 | struct GNUNET_TIME_Absolute exp, | ||
234 | const GNUNET_HashCode * key, | ||
235 | uint32_t size, const char *data, uint32_t type) | ||
236 | { | ||
237 | struct DatacacheGetContext *datacache_get_ctx = cls; | ||
238 | struct GNUNET_DHT_GetResultMessage *get_result; | ||
239 | |||
240 | get_result = | ||
241 | GNUNET_malloc (sizeof (struct GNUNET_DHT_GetResultMessage) + size); | ||
242 | get_result->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_GET_RESULT); | ||
243 | get_result->header.size = | ||
244 | htons (sizeof (struct GNUNET_DHT_GetResultMessage) + size); | ||
245 | get_result->data_size = htons (size); | ||
246 | get_result->expiration = exp; | ||
247 | memcpy (&get_result->key, key, sizeof (GNUNET_HashCode)); | ||
248 | get_result->type = htons (type); | ||
249 | memcpy (&get_result[1], data, size); | ||
250 | |||
251 | send_reply_to_client (datacache_get_ctx->client, &get_result->header, | ||
252 | datacache_get_ctx->unique_id); | ||
253 | |||
254 | GNUNET_free (get_result); | ||
255 | return GNUNET_OK; | ||
256 | } | ||
141 | 257 | ||
142 | /** | 258 | /** |
143 | * Server handler for initiating local dht get requests | 259 | * Server handler for initiating local dht get requests |
144 | */ | 260 | */ |
145 | static void handle_dht_get (void *cls, struct GNUNET_DHT_GetMessage *get_msg, GNUNET_HashCode *key) | 261 | static void |
262 | handle_dht_get (void *cls, struct GNUNET_DHT_GetMessage *get_msg, | ||
263 | struct DHT_MessageContext *message_context) | ||
146 | { | 264 | { |
147 | #if DEBUG_DHT | 265 | #if DEBUG_DHT |
148 | GNUNET_HashCode get_key; | 266 | GNUNET_HashCode get_key; |
149 | #endif | 267 | #endif |
150 | size_t get_type; | 268 | size_t get_type; |
269 | unsigned int results; | ||
270 | struct DatacacheGetContext *datacache_get_context; | ||
151 | 271 | ||
152 | GNUNET_assert(ntohs(get_msg->header.size) >= sizeof(struct GNUNET_DHT_GetMessage)); | 272 | GNUNET_assert (ntohs (get_msg->header.size) >= |
153 | get_type = ntohs(get_msg->type); | 273 | sizeof (struct GNUNET_DHT_GetMessage)); |
274 | get_type = ntohs (get_msg->type); | ||
154 | 275 | ||
155 | #if DEBUG_DHT | 276 | #if DEBUG_DHT |
156 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 277 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
157 | "`%s': Received `%s' request from client, message type %d, key %s\n", "DHT", "GET", get_type, GNUNET_h2s(&get_key)); | 278 | "`%s': Received `%s' request from client, message type %d, key %s, uid %llu\n", |
279 | "DHT", "GET", get_type, GNUNET_h2s (&get_key), | ||
280 | message_context->unique_id); | ||
158 | #endif | 281 | #endif |
159 | 282 | ||
283 | datacache_get_context = GNUNET_malloc (sizeof (struct DatacacheGetContext)); | ||
284 | datacache_get_context->client = message_context->client; | ||
285 | datacache_get_context->unique_id = message_context->unique_id; | ||
286 | |||
287 | results = 0; | ||
288 | if (datacache != NULL) | ||
289 | results = | ||
290 | GNUNET_DATACACHE_get (datacache, message_context->key, get_type, | ||
291 | datacache_get_iterator, datacache_get_context); | ||
292 | |||
293 | #if DEBUG_DHT | ||
294 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
295 | "`%s': Found %d results for local `%s' request\n", "DHT", | ||
296 | results, "GET"); | ||
297 | #endif | ||
298 | GNUNET_free (datacache_get_context); | ||
160 | /* FIXME: Implement get functionality here */ | 299 | /* FIXME: Implement get functionality here */ |
161 | } | 300 | } |
162 | 301 | ||
@@ -164,14 +303,20 @@ static void handle_dht_get (void *cls, struct GNUNET_DHT_GetMessage *get_msg, GN | |||
164 | /** | 303 | /** |
165 | * Server handler for initiating local dht find peer requests | 304 | * Server handler for initiating local dht find peer requests |
166 | */ | 305 | */ |
167 | static void handle_dht_find_peer (void *cls, struct GNUNET_DHT_FindPeerMessage *find_msg, GNUNET_HashCode *key) | 306 | static void |
307 | handle_dht_find_peer (void *cls, struct GNUNET_DHT_FindPeerMessage *find_msg, | ||
308 | struct DHT_MessageContext *message_context) | ||
168 | { | 309 | { |
169 | #if DEBUG_DHT | 310 | #if DEBUG_DHT |
170 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 311 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
171 | "`%s': Received `%s' request from client, key %s (msg size %d, we expected %d)\n", "DHT", "FIND PEER", GNUNET_h2s(key), ntohs(find_msg->header.size), sizeof(struct GNUNET_DHT_FindPeerMessage)); | 312 | "`%s': Received `%s' request from client, key %s (msg size %d, we expected %d)\n", |
313 | "DHT", "FIND PEER", GNUNET_h2s (message_context->key), | ||
314 | ntohs (find_msg->header.size), | ||
315 | sizeof (struct GNUNET_DHT_FindPeerMessage)); | ||
172 | #endif | 316 | #endif |
173 | 317 | ||
174 | GNUNET_assert(ntohs(find_msg->header.size) >= sizeof(struct GNUNET_DHT_FindPeerMessage)); | 318 | GNUNET_assert (ntohs (find_msg->header.size) >= |
319 | sizeof (struct GNUNET_DHT_FindPeerMessage)); | ||
175 | 320 | ||
176 | /* FIXME: Implement find peer functionality here */ | 321 | /* FIXME: Implement find peer functionality here */ |
177 | } | 322 | } |
@@ -180,35 +325,44 @@ static void handle_dht_find_peer (void *cls, struct GNUNET_DHT_FindPeerMessage * | |||
180 | /** | 325 | /** |
181 | * Server handler for initiating local dht put requests | 326 | * Server handler for initiating local dht put requests |
182 | */ | 327 | */ |
183 | static void handle_dht_put (void *cls, struct GNUNET_DHT_PutMessage *put_msg, GNUNET_HashCode *key) | 328 | static void |
329 | handle_dht_put (void *cls, struct GNUNET_DHT_PutMessage *put_msg, | ||
330 | struct DHT_MessageContext *message_context) | ||
184 | { | 331 | { |
185 | size_t put_type; | 332 | size_t put_type; |
186 | size_t data_size; | 333 | size_t data_size; |
187 | char *data; | ||
188 | 334 | ||
189 | GNUNET_assert(ntohs(put_msg->header.size) >= sizeof(struct GNUNET_DHT_PutMessage)); | 335 | GNUNET_assert (ntohs (put_msg->header.size) >= |
336 | sizeof (struct GNUNET_DHT_PutMessage)); | ||
190 | 337 | ||
191 | put_type = ntohs(put_msg->type); | 338 | put_type = ntohs (put_msg->type); |
192 | data_size = ntohs(put_msg->data_size); | 339 | data_size = ntohs (put_msg->data_size); |
193 | #if DEBUG_DHT | 340 | #if DEBUG_DHT |
194 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 341 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
195 | "`%s': %s msg total size is %d, data size %d, struct size %d\n", "DHT", "PUT", ntohs(put_msg->header.size), data_size, sizeof(struct GNUNET_DHT_PutMessage)); | 342 | "`%s': %s msg total size is %d, data size %d, struct size %d\n", |
343 | "DHT", "PUT", ntohs (put_msg->header.size), data_size, | ||
344 | sizeof (struct GNUNET_DHT_PutMessage)); | ||
196 | #endif | 345 | #endif |
197 | GNUNET_assert(ntohs(put_msg->header.size) == sizeof(struct GNUNET_DHT_PutMessage) + data_size); | 346 | GNUNET_assert (ntohs (put_msg->header.size) == |
198 | data = GNUNET_malloc(data_size); | 347 | sizeof (struct GNUNET_DHT_PutMessage) + data_size); |
199 | memcpy(data, &put_msg[1], data_size); | ||
200 | 348 | ||
201 | #if DEBUG_DHT | 349 | #if DEBUG_DHT |
202 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 350 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
203 | "`%s': Received `%s' request from client, message type %d, key %s\n", "DHT", "PUT", put_type, GNUNET_h2s(key)); | 351 | "`%s': Received `%s' request from client, message type %d, key %s\n", |
352 | "DHT", "PUT", put_type, GNUNET_h2s (message_context->key)); | ||
204 | #endif | 353 | #endif |
205 | 354 | ||
206 | 355 | /** | |
356 | * Simplest DHT functionality, store any message we receive a put request for. | ||
357 | */ | ||
358 | if (datacache != NULL) | ||
359 | GNUNET_DATACACHE_put (datacache, message_context->key, data_size, | ||
360 | (char *) &put_msg[1], put_type, | ||
361 | put_msg->expiration); | ||
207 | /** | 362 | /** |
208 | * FIXME: Implement dht put request functionality here! | 363 | * FIXME: Implement dht put request functionality here! |
209 | */ | 364 | */ |
210 | 365 | ||
211 | GNUNET_free(data); | ||
212 | } | 366 | } |
213 | 367 | ||
214 | /** | 368 | /** |
@@ -224,117 +378,145 @@ struct SendConfirmationContext | |||
224 | /** | 378 | /** |
225 | * Transmit handle. | 379 | * Transmit handle. |
226 | */ | 380 | */ |
227 | struct GNUNET_CONNECTION_TransmitHandle * transmit_handle; | 381 | struct GNUNET_CONNECTION_TransmitHandle *transmit_handle; |
228 | }; | 382 | }; |
229 | 383 | ||
230 | static size_t send_confirmation (void *cls, | 384 | static size_t |
231 | size_t size, void *buf) | 385 | send_confirmation (void *cls, size_t size, void *buf) |
232 | { | 386 | { |
233 | struct GNUNET_DHT_StopMessage *confirmation_message = cls; | 387 | struct GNUNET_DHT_StopMessage *confirmation_message = cls; |
234 | 388 | ||
235 | if (buf == NULL) /* Message timed out, that's crappy... */ | 389 | if (buf == NULL) /* Message timed out, that's crappy... */ |
236 | { | 390 | { |
237 | GNUNET_free(confirmation_message); | 391 | GNUNET_free (confirmation_message); |
238 | return 0; | 392 | return 0; |
239 | } | 393 | } |
240 | 394 | ||
241 | if (size >= ntohs(confirmation_message->header.size)) | 395 | if (size >= ntohs (confirmation_message->header.size)) |
242 | { | 396 | { |
243 | memcpy(buf, confirmation_message, ntohs(confirmation_message->header.size)); | 397 | memcpy (buf, confirmation_message, |
244 | return ntohs(confirmation_message->header.size); | 398 | ntohs (confirmation_message->header.size)); |
245 | } | 399 | return ntohs (confirmation_message->header.size); |
400 | } | ||
246 | else | 401 | else |
247 | return 0; | 402 | return 0; |
248 | } | 403 | } |
249 | 404 | ||
405 | |||
250 | static void | 406 | static void |
251 | send_client_receipt_confirmation(struct GNUNET_SERVER_Client *client, uint64_t uid) | 407 | send_client_receipt_confirmation (struct GNUNET_SERVER_Client *client, |
408 | uint64_t uid) | ||
252 | { | 409 | { |
253 | struct GNUNET_DHT_StopMessage *confirm_message; | 410 | struct GNUNET_DHT_StopMessage *confirm_message; |
254 | 411 | ||
255 | #if DEBUG_DHT | 412 | #if DEBUG_DHT |
256 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 413 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
257 | "`%s': Sending receipt confirmation for uid %llu\n", "DHT", uid); | 414 | "`%s': Sending receipt confirmation for uid %llu\n", "DHT", |
415 | uid); | ||
258 | #endif | 416 | #endif |
259 | confirm_message = GNUNET_malloc(sizeof(struct GNUNET_DHT_StopMessage)); | 417 | confirm_message = GNUNET_malloc (sizeof (struct GNUNET_DHT_StopMessage)); |
260 | confirm_message->header.type = htons(GNUNET_MESSAGE_TYPE_DHT_STOP); | 418 | confirm_message->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_STOP); |
261 | confirm_message->header.size = htons(sizeof(struct GNUNET_DHT_StopMessage)); | 419 | confirm_message->header.size = |
262 | confirm_message->unique_id = GNUNET_htonll(uid); | 420 | htons (sizeof (struct GNUNET_DHT_StopMessage)); |
421 | confirm_message->unique_id = GNUNET_htonll (uid); | ||
263 | 422 | ||
264 | GNUNET_SERVER_notify_transmit_ready (client, | 423 | GNUNET_SERVER_notify_transmit_ready (client, |
265 | sizeof(struct GNUNET_DHT_StopMessage), | 424 | sizeof (struct GNUNET_DHT_StopMessage), |
266 | GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5), | 425 | GNUNET_TIME_relative_multiply |
426 | (GNUNET_TIME_UNIT_SECONDS, 5), | ||
267 | &send_confirmation, confirm_message); | 427 | &send_confirmation, confirm_message); |
268 | 428 | ||
269 | } | 429 | } |
270 | 430 | ||
271 | static void | 431 | static void |
272 | handle_dht_start_message(void *cls, struct GNUNET_SERVER_Client * client, | 432 | handle_dht_start_message (void *cls, struct GNUNET_SERVER_Client *client, |
273 | const struct GNUNET_MessageHeader *message) | 433 | const struct GNUNET_MessageHeader *message) |
274 | { | 434 | { |
275 | struct GNUNET_DHT_Message *dht_msg = (struct GNUNET_DHT_Message *)message; | 435 | struct GNUNET_DHT_Message *dht_msg = (struct GNUNET_DHT_Message *) message; |
276 | struct GNUNET_MessageHeader *enc_msg; | 436 | struct GNUNET_MessageHeader *enc_msg; |
437 | struct DHT_MessageContext *message_context; | ||
277 | size_t enc_type; | 438 | size_t enc_type; |
278 | 439 | ||
279 | enc_msg = (struct GNUNET_MessageHeader *)&dht_msg[1]; | 440 | enc_msg = (struct GNUNET_MessageHeader *) &dht_msg[1]; |
280 | enc_type = ntohs(enc_msg->type); | 441 | enc_type = ntohs (enc_msg->type); |
281 | 442 | ||
282 | 443 | ||
283 | #if DEBUG_DHT | 444 | #if DEBUG_DHT |
284 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 445 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
285 | "`%s': Received `%s' request from client, message type %d, key %s, uid %llu\n", "DHT", "GENERIC", enc_type, GNUNET_h2s(&dht_msg->key), GNUNET_ntohll(dht_msg->unique_id)); | 446 | "`%s': Received `%s' request from client, message type %d, key %s, uid %llu\n", |
447 | "DHT", "GENERIC", enc_type, GNUNET_h2s (&dht_msg->key), | ||
448 | GNUNET_ntohll (dht_msg->unique_id)); | ||
286 | #endif | 449 | #endif |
287 | 450 | ||
288 | /* FIXME: Implement demultiplexing functionality here */ | 451 | message_context = GNUNET_malloc (sizeof (struct DHT_MessageContext)); |
452 | message_context->client = client; | ||
453 | message_context->key = &dht_msg->key; | ||
454 | message_context->unique_id = GNUNET_ntohll (dht_msg->unique_id); | ||
455 | message_context->replication = ntohs (dht_msg->desired_replication_level); | ||
456 | message_context->msg_options = ntohs (dht_msg->options); | ||
457 | |||
289 | switch (enc_type) | 458 | switch (enc_type) |
290 | { | 459 | { |
291 | case GNUNET_MESSAGE_TYPE_DHT_GET: | 460 | case GNUNET_MESSAGE_TYPE_DHT_GET: |
292 | handle_dht_get(cls, (struct GNUNET_DHT_GetMessage *)enc_msg, &dht_msg->key); | 461 | handle_dht_get (cls, (struct GNUNET_DHT_GetMessage *) enc_msg, |
462 | message_context); | ||
293 | break; | 463 | break; |
294 | case GNUNET_MESSAGE_TYPE_DHT_PUT: | 464 | case GNUNET_MESSAGE_TYPE_DHT_PUT: |
295 | handle_dht_put(cls, (struct GNUNET_DHT_PutMessage *)enc_msg, &dht_msg->key); | 465 | handle_dht_put (cls, (struct GNUNET_DHT_PutMessage *) enc_msg, |
296 | send_client_receipt_confirmation(client, GNUNET_ntohll(dht_msg->unique_id)); | 466 | message_context); |
467 | send_client_receipt_confirmation (client, | ||
468 | GNUNET_ntohll (dht_msg->unique_id)); | ||
297 | break; | 469 | break; |
298 | case GNUNET_MESSAGE_TYPE_DHT_FIND_PEER: | 470 | case GNUNET_MESSAGE_TYPE_DHT_FIND_PEER: |
299 | handle_dht_find_peer(cls, (struct GNUNET_DHT_FindPeerMessage *)enc_msg, &dht_msg->key); | 471 | handle_dht_find_peer (cls, |
472 | (struct GNUNET_DHT_FindPeerMessage *) enc_msg, | ||
473 | message_context); | ||
300 | break; | 474 | break; |
301 | default: | 475 | default: |
302 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | 476 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, |
303 | "`%s': Message type (%d) not handled\n", "DHT", enc_type); | 477 | "`%s': Message type (%d) not handled\n", "DHT", enc_type); |
304 | } | 478 | } |
305 | 479 | ||
306 | GNUNET_SERVER_receive_done(client, GNUNET_OK); | 480 | GNUNET_free (message_context); |
481 | GNUNET_SERVER_receive_done (client, GNUNET_OK); | ||
307 | 482 | ||
308 | } | 483 | } |
309 | 484 | ||
310 | 485 | ||
311 | static void | 486 | static void |
312 | handle_dht_stop_message(void *cls, struct GNUNET_SERVER_Client * client, | 487 | handle_dht_stop_message (void *cls, struct GNUNET_SERVER_Client *client, |
313 | const struct GNUNET_MessageHeader *message) | 488 | const struct GNUNET_MessageHeader *message) |
314 | { | 489 | { |
315 | struct GNUNET_DHT_StopMessage * dht_stop_msg = (struct GNUNET_DHT_StopMessage *)message; | 490 | struct GNUNET_DHT_StopMessage *dht_stop_msg = |
491 | (struct GNUNET_DHT_StopMessage *) message; | ||
316 | 492 | ||
317 | #if DEBUG_DHT | 493 | #if DEBUG_DHT |
318 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 494 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
319 | "`%s': Received `%s' request from client, uid %llu\n", "DHT", "GENERIC STOP", GNUNET_ntohll(dht_stop_msg->unique_id)); | 495 | "`%s': Received `%s' request from client, uid %llu\n", "DHT", |
496 | "GENERIC STOP", GNUNET_ntohll (dht_stop_msg->unique_id)); | ||
320 | #endif | 497 | #endif |
321 | send_client_receipt_confirmation(client, GNUNET_ntohll(dht_stop_msg->unique_id)); | 498 | |
322 | GNUNET_SERVER_receive_done(client, GNUNET_OK); | 499 | /* TODO: Put in demultiplexing here */ |
500 | |||
501 | send_client_receipt_confirmation (client, | ||
502 | GNUNET_ntohll (dht_stop_msg->unique_id)); | ||
503 | GNUNET_SERVER_receive_done (client, GNUNET_OK); | ||
323 | } | 504 | } |
324 | 505 | ||
325 | 506 | ||
326 | /** | 507 | /** |
327 | * Core handler for p2p dht get requests. | 508 | * Core handler for p2p dht get requests. |
328 | */ | 509 | */ |
329 | static int handle_dht_p2p_get (void *cls, | 510 | static int |
330 | const struct GNUNET_PeerIdentity * peer, | 511 | handle_dht_p2p_get (void *cls, |
331 | const struct GNUNET_MessageHeader * message, | 512 | const struct GNUNET_PeerIdentity *peer, |
332 | struct GNUNET_TIME_Relative latency, | 513 | const struct GNUNET_MessageHeader *message, |
333 | uint32_t distance) | 514 | struct GNUNET_TIME_Relative latency, uint32_t distance) |
334 | { | 515 | { |
335 | #if DEBUG_DHT | 516 | #if DEBUG_DHT |
336 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 517 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
337 | "`%s': Received `%s' request from another peer\n", "DHT", "GET"); | 518 | "`%s': Received `%s' request from another peer\n", "DHT", |
519 | "GET"); | ||
338 | #endif | 520 | #endif |
339 | 521 | ||
340 | return GNUNET_YES; | 522 | return GNUNET_YES; |
@@ -343,15 +525,16 @@ static int handle_dht_p2p_get (void *cls, | |||
343 | /** | 525 | /** |
344 | * Core handler for p2p dht put requests. | 526 | * Core handler for p2p dht put requests. |
345 | */ | 527 | */ |
346 | static int handle_dht_p2p_put (void *cls, | 528 | static int |
347 | const struct GNUNET_PeerIdentity * peer, | 529 | handle_dht_p2p_put (void *cls, |
348 | const struct GNUNET_MessageHeader * message, | 530 | const struct GNUNET_PeerIdentity *peer, |
349 | struct GNUNET_TIME_Relative latency, | 531 | const struct GNUNET_MessageHeader *message, |
350 | uint32_t distance) | 532 | struct GNUNET_TIME_Relative latency, uint32_t distance) |
351 | { | 533 | { |
352 | #if DEBUG_DHT | 534 | #if DEBUG_DHT |
353 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 535 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
354 | "`%s': Received `%s' request from another peer\n", "DHT", "PUT"); | 536 | "`%s': Received `%s' request from another peer\n", "DHT", |
537 | "PUT"); | ||
355 | #endif | 538 | #endif |
356 | 539 | ||
357 | return GNUNET_YES; | 540 | return GNUNET_YES; |
@@ -360,15 +543,17 @@ static int handle_dht_p2p_put (void *cls, | |||
360 | /** | 543 | /** |
361 | * Core handler for p2p dht find peer requests. | 544 | * Core handler for p2p dht find peer requests. |
362 | */ | 545 | */ |
363 | static int handle_dht_p2p_find_peer (void *cls, | 546 | static int |
364 | const struct GNUNET_PeerIdentity * peer, | 547 | handle_dht_p2p_find_peer (void *cls, |
365 | const struct GNUNET_MessageHeader * message, | 548 | const struct GNUNET_PeerIdentity *peer, |
366 | struct GNUNET_TIME_Relative latency, | 549 | const struct GNUNET_MessageHeader *message, |
367 | uint32_t distance) | 550 | struct GNUNET_TIME_Relative latency, |
551 | uint32_t distance) | ||
368 | { | 552 | { |
369 | #if DEBUG_DHT | 553 | #if DEBUG_DHT |
370 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 554 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
371 | "`%s': Received `%s' request from another peer\n", "DHT", "FIND PEER"); | 555 | "`%s': Received `%s' request from another peer\n", "DHT", |
556 | "FIND PEER"); | ||
372 | #endif | 557 | #endif |
373 | 558 | ||
374 | return GNUNET_YES; | 559 | return GNUNET_YES; |
@@ -381,8 +566,7 @@ static int handle_dht_p2p_find_peer (void *cls, | |||
381 | * @param tc unused | 566 | * @param tc unused |
382 | */ | 567 | */ |
383 | static void | 568 | static void |
384 | shutdown_task (void *cls, | 569 | shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) |
385 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
386 | { | 570 | { |
387 | GNUNET_CORE_disconnect (coreAPI); | 571 | GNUNET_CORE_disconnect (coreAPI); |
388 | } | 572 | } |
@@ -390,23 +574,25 @@ shutdown_task (void *cls, | |||
390 | /** | 574 | /** |
391 | * To be called on core init/fail. | 575 | * To be called on core init/fail. |
392 | */ | 576 | */ |
393 | void core_init (void *cls, | 577 | void |
394 | struct GNUNET_CORE_Handle * server, | 578 | core_init (void *cls, |
395 | const struct GNUNET_PeerIdentity *identity, | 579 | struct GNUNET_CORE_Handle *server, |
396 | const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded * publicKey) | 580 | const struct GNUNET_PeerIdentity *identity, |
581 | const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey) | ||
397 | { | 582 | { |
398 | 583 | ||
399 | if (server == NULL) | 584 | if (server == NULL) |
400 | { | 585 | { |
401 | GNUNET_SCHEDULER_cancel(sched, cleanup_task); | 586 | GNUNET_SCHEDULER_cancel (sched, cleanup_task); |
402 | GNUNET_SCHEDULER_add_now(sched, &shutdown_task, NULL); | 587 | GNUNET_SCHEDULER_add_now (sched, &shutdown_task, NULL); |
403 | return; | 588 | return; |
404 | } | 589 | } |
405 | #if DEBUG_DHT | 590 | #if DEBUG_DHT |
406 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 591 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
407 | "%s: Core connection initialized, I am peer: %s\n", "dht", GNUNET_i2s(identity)); | 592 | "%s: Core connection initialized, I am peer: %s\n", "dht", |
593 | GNUNET_i2s (identity)); | ||
408 | #endif | 594 | #endif |
409 | memcpy(&my_identity, identity, sizeof(struct GNUNET_PeerIdentity)); | 595 | memcpy (&my_identity, identity, sizeof (struct GNUNET_PeerIdentity)); |
410 | coreAPI = server; | 596 | coreAPI = server; |
411 | } | 597 | } |
412 | 598 | ||
@@ -429,32 +615,31 @@ run (void *cls, | |||
429 | 615 | ||
430 | datacache = GNUNET_DATACACHE_create (sched, cfg, "dhtcache"); | 616 | datacache = GNUNET_DATACACHE_create (sched, cfg, "dhtcache"); |
431 | 617 | ||
432 | client_transmit_timeout = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5); | 618 | client_transmit_timeout = |
619 | GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5); | ||
433 | GNUNET_SERVER_add_handlers (server, plugin_handlers); | 620 | GNUNET_SERVER_add_handlers (server, plugin_handlers); |
434 | 621 | ||
435 | coreAPI = | 622 | coreAPI = GNUNET_CORE_connect (sched, /* Main scheduler */ |
436 | GNUNET_CORE_connect (sched, /* Main scheduler */ | 623 | cfg, /* Main configuration */ |
437 | cfg, /* Main configuration */ | 624 | client_transmit_timeout, /* Delay for connecting */ |
438 | client_transmit_timeout, /* Delay for connecting */ | 625 | NULL, /* FIXME: anything we want to pass around? */ |
439 | NULL, /* FIXME: anything we want to pass around? */ | 626 | &core_init, /* Call core_init once connected */ |
440 | &core_init, /* Call core_init once connected */ | 627 | NULL, /* Don't care about pre-connects */ |
441 | NULL, /* Don't care about pre-connects */ | 628 | NULL, /* Don't care about connects */ |
442 | NULL, /* Don't care about connects */ | 629 | NULL, /* Don't care about disconnects */ |
443 | NULL, /* Don't care about disconnects */ | 630 | NULL, /* Don't want notified about all incoming messages */ |
444 | NULL, /* Don't want notified about all incoming messages */ | 631 | GNUNET_NO, /* For header only inbound notification */ |
445 | GNUNET_NO, /* For header only inbound notification */ | 632 | NULL, /* Don't want notified about all outbound messages */ |
446 | NULL, /* Don't want notified about all outbound messages */ | 633 | GNUNET_NO, /* For header only outbound notification */ |
447 | GNUNET_NO, /* For header only outbound notification */ | 634 | core_handlers); /* Register these handlers */ |
448 | core_handlers); /* Register these handlers */ | ||
449 | 635 | ||
450 | if (coreAPI == NULL) | 636 | if (coreAPI == NULL) |
451 | return; | 637 | return; |
452 | 638 | ||
453 | /* Scheduled the task to clean up when shutdown is called */ | 639 | /* Scheduled the task to clean up when shutdown is called */ |
454 | cleanup_task = GNUNET_SCHEDULER_add_delayed (sched, | 640 | cleanup_task = GNUNET_SCHEDULER_add_delayed (sched, |
455 | GNUNET_TIME_UNIT_FOREVER_REL, | 641 | GNUNET_TIME_UNIT_FOREVER_REL, |
456 | &shutdown_task, | 642 | &shutdown_task, NULL); |
457 | NULL); | ||
458 | } | 643 | } |
459 | 644 | ||
460 | 645 | ||