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 2, 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.
23 * @brief library to access the DV service
24 * @author Christian Grothoff
25 * @author Nathan Evans
28 #include "gnunet_bandwidth_lib.h"
29 #include "gnunet_client_lib.h"
30 #include "gnunet_constants.h"
31 #include "gnunet_container_lib.h"
32 #include "gnunet_arm_service.h"
33 #include "gnunet_hello_lib.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_server_lib.h"
36 #include "gnunet_time_lib.h"
37 #include "gnunet_dv_service.h"
39 #include "../transport/plugin_transport.h"
42 struct PendingMessages
45 * Linked list of pending messages
47 struct PendingMessages *next;
50 * Message that is pending
52 struct GNUNET_DV_SendMessage *msg;
55 * Timeout for this message
57 struct GNUNET_TIME_Absolute timeout;
64 * Handle for the service.
66 struct GNUNET_DV_Handle
71 struct GNUNET_SCHEDULER_Handle *sched;
74 * Configuration to use.
76 const struct GNUNET_CONFIGURATION_Handle *cfg;
79 * Socket (if available).
81 struct GNUNET_CLIENT_Connection *client;
84 * Currently pending transmission request.
86 struct GNUNET_CLIENT_TransmitHandle *th;
89 * List of the currently pending messages for the DV service.
91 struct PendingMessages *pending_list;
94 * Message we are currently sending.
96 struct PendingMessages *current;
99 * Kill off the connection and any pending messages.
104 * Handler for messages we receive from the DV service
106 GNUNET_DV_MessageReceivedHandler receive_handler;
109 * Closure for the receive handler
119 * Hashmap containing outstanding send requests awaiting confirmation.
121 struct GNUNET_CONTAINER_MultiHashMap *send_callbacks;
131 struct GNUNET_MessageHeader *message;
134 * Handle to service, in case of timeout
136 struct GNUNET_DV_Handle *handle;
139 struct SendCallbackContext
142 * The continuation to call once a message is confirmed sent (or failed)
144 GNUNET_TRANSPORT_TransmitContinuation cont;
147 * Closure to call with send continuation.
152 * Target of the message.
154 struct GNUNET_PeerIdentity target;
158 * Convert unique ID to hash code.
160 * @param uid unique ID to convert
161 * @param hash set to uid (extended with zeros)
164 hash_from_uid (uint32_t uid,
165 GNUNET_HashCode *hash)
167 memset (hash, 0, sizeof(GNUNET_HashCode));
168 *((uint32_t*)hash) = uid;
172 * Try to (re)connect to the dv service.
174 * @return GNUNET_YES on success, GNUNET_NO on failure.
177 try_connect (struct GNUNET_DV_Handle *ret)
179 if (ret->client != NULL)
181 ret->client = GNUNET_CLIENT_connect (ret->sched, "dv", ret->cfg);
182 if (ret->client != NULL)
185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
186 _("Failed to connect to the dv service!\n"));
191 static void process_pending_message(struct GNUNET_DV_Handle *handle);
194 * Send complete, schedule next
197 finish (struct GNUNET_DV_Handle *handle, int code)
199 struct PendingMessages *pos = handle->current;
200 handle->current = NULL;
201 process_pending_message (handle);
208 transmit_pending (void *cls, size_t size, void *buf)
210 struct GNUNET_DV_Handle *handle = cls;
215 if (handle->current != NULL)
216 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV API: Transmit pending called with message type %d\n", ntohs(handle->current->msg->header.type));
222 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV API: Transmit pending FAILED!\n\n\n");
224 finish(handle, GNUNET_SYSERR);
231 if (handle->current != NULL)
233 tsize = ntohs(handle->current->msg->header.size);
236 memcpy(buf, handle->current->msg, tsize);
238 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV API: Copied %d bytes into buffer!\n\n\n", tsize);
240 finish(handle, GNUNET_OK);
250 * Try to send messages from list of messages to send
252 static void process_pending_message(struct GNUNET_DV_Handle *handle)
255 if (handle->current != NULL)
256 return; /* action already pending */
257 if (GNUNET_YES != try_connect (handle))
259 finish (handle, GNUNET_SYSERR);
263 /* schedule next action */
264 handle->current = handle->pending_list;
265 if (NULL == handle->current)
267 if (handle->do_destroy)
269 handle->do_destroy = GNUNET_NO;
270 //GNUNET_DV_disconnect (handle); /* FIXME: replace with proper disconnect stuffs */
274 handle->pending_list = handle->pending_list->next;
275 handle->current->next = NULL;
278 (handle->th = GNUNET_CLIENT_notify_transmit_ready (handle->client,
279 ntohl(handle->current->msg->msgbuf_size),
280 handle->current->msg->timeout,
282 &transmit_pending, handle)))
285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
286 "Failed to transmit request to dv service.\n");
288 finish (handle, GNUNET_SYSERR);
293 * Add a pending message to the linked list
295 * @param handle handle to the specified DV api
296 * @param msg the message to add to the list
298 static void add_pending(struct GNUNET_DV_Handle *handle, struct GNUNET_DV_SendMessage *msg)
300 struct PendingMessages *new_message;
301 struct PendingMessages *pos;
302 struct PendingMessages *last;
304 new_message = GNUNET_malloc(sizeof(struct PendingMessages));
305 new_message->msg = msg;
307 if (handle->pending_list != NULL)
309 pos = handle->pending_list;
315 new_message->next = last->next; /* Should always be null */
316 last->next = new_message;
320 new_message->next = handle->pending_list; /* Will always be null */
321 handle->pending_list = new_message;
324 process_pending_message(handle);
328 * Handles a message sent from the DV service to us.
329 * Parse it out and give it to the plugin.
331 * @param cls the handle to the DV API
332 * @param msg the message that was received
334 void handle_message_receipt (void *cls,
335 const struct GNUNET_MessageHeader * msg)
337 struct GNUNET_DV_Handle *handle = cls;
338 struct GNUNET_DV_MessageReceived *received_msg;
339 struct GNUNET_DV_SendResultMessage *send_result_msg;
340 size_t packed_msg_len;
341 size_t sender_address_len;
342 char *sender_address;
344 char *packed_msg_start;
345 GNUNET_HashCode uidhash;
346 struct SendCallbackContext *send_ctx;
349 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "dv api receives message!\n");
354 return; /* Connection closed? */
357 GNUNET_assert((ntohs(msg->type) == GNUNET_MESSAGE_TYPE_TRANSPORT_DV_RECEIVE) || (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND_RESULT));
359 switch (ntohs(msg->type))
361 case GNUNET_MESSAGE_TYPE_TRANSPORT_DV_RECEIVE:
362 if (ntohs(msg->size) < sizeof(struct GNUNET_DV_MessageReceived))
365 received_msg = (struct GNUNET_DV_MessageReceived *)msg;
366 packed_msg_len = ntohl(received_msg->msg_len);
367 sender_address_len = ntohl(received_msg->sender_address_len);
369 GNUNET_assert(ntohs(msg->size) == (sizeof(struct GNUNET_DV_MessageReceived) + packed_msg_len + sender_address_len));
371 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "dv api receives message, size checks out!\n");
373 sender_address = GNUNET_malloc(sender_address_len);
374 memcpy(sender_address, &received_msg[1], sender_address_len);
375 packed_msg_start = (char *)&received_msg[1];
376 packed_msg = GNUNET_malloc(packed_msg_len);
377 memcpy(packed_msg, &packed_msg_start[sender_address_len], packed_msg_len);
380 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV_API receive: packed message type: %d or %d\n", ntohs(((struct GNUNET_MessageHeader *)packed_msg)->type), ((struct GNUNET_MessageHeader *)packed_msg)->type);
381 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV_API receive: message sender reported as %s\n", GNUNET_i2s(&received_msg->sender));
382 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV_API receive: distance is %u\n", ntohl(received_msg->distance));
385 handle->receive_handler(handle->receive_cls,
386 &received_msg->sender,
389 ntohl(received_msg->distance),
393 GNUNET_free(sender_address);
395 case GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND_RESULT:
396 if (ntohs(msg->size) < sizeof(struct GNUNET_DV_SendResultMessage))
399 send_result_msg = (struct GNUNET_DV_SendResultMessage *)msg;
400 hash_from_uid(ntohl(send_result_msg->uid), &uidhash);
401 send_ctx = GNUNET_CONTAINER_multihashmap_get(handle->send_callbacks, &uidhash);
402 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "got uid of %u or %u, hash of %s !!!!\n", ntohl(send_result_msg->uid), send_result_msg->uid, GNUNET_h2s(&uidhash));
404 if ((send_ctx != NULL) && (send_ctx->cont != NULL))
407 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "dv api notifies transport of send result (%u)!\n", ntohl(send_result_msg->result));
409 if (ntohl(send_result_msg->result) == 0)
411 send_ctx->cont(send_ctx->cont_cls, &send_ctx->target, GNUNET_OK);
415 send_ctx->cont(send_ctx->cont_cls, &send_ctx->target, GNUNET_SYSERR);
418 GNUNET_free_non_null(send_ctx);
423 GNUNET_CLIENT_receive (handle->client,
424 &handle_message_receipt,
425 handle, GNUNET_TIME_UNIT_FOREVER_REL);
429 * Send a message from the plugin to the DV service indicating that
430 * a message should be sent via DV to some peer.
432 * @param dv_handle the handle to the DV api
433 * @param target the final target of the message
434 * @param msgbuf the msg(s) to send
435 * @param msgbuf_size the size of msgbuf
436 * @param priority priority to pass on to core when sending the message
437 * @param timeout how long can this message be delayed (pass through to core)
438 * @param addr the address of this peer (internally known to DV)
439 * @param addrlen the length of the peer address
440 * @param cont continuation to call once the message has been sent (or failed)
441 * @param cont_cls closure for continuation
444 int GNUNET_DV_send (struct GNUNET_DV_Handle *dv_handle,
445 const struct GNUNET_PeerIdentity *target,
448 unsigned int priority,
449 struct GNUNET_TIME_Relative timeout,
452 GNUNET_TRANSPORT_TransmitContinuation
453 cont, void *cont_cls)
455 struct GNUNET_DV_SendMessage *msg;
456 struct SendCallbackContext *send_ctx;
457 char *end_of_message;
458 GNUNET_HashCode uidhash;
460 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV SEND called with message of size %d, address size %d, total size to send is %d\n", msgbuf_size, addrlen, sizeof(struct GNUNET_DV_SendMessage) + msgbuf_size + addrlen);
462 dv_handle->uid_gen++;
463 msg = GNUNET_malloc(sizeof(struct GNUNET_DV_SendMessage) + addrlen + msgbuf_size);
464 msg->header.size = htons(sizeof(struct GNUNET_DV_SendMessage) + addrlen + msgbuf_size);
465 msg->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND);
466 memcpy(&msg->target, target, sizeof(struct GNUNET_PeerIdentity));
467 msg->msgbuf_size = htonl(msgbuf_size);
468 msg->priority = htonl(priority);
469 msg->timeout = timeout;
470 msg->addrlen = htonl(addrlen);
471 msg->uid = htonl(dv_handle->uid_gen);
472 memcpy(&msg[1], addr, addrlen);
473 end_of_message = (char *)&msg[1];
474 end_of_message = &end_of_message[addrlen];
475 memcpy(end_of_message, msgbuf, msgbuf_size);
476 add_pending(dv_handle, msg);
478 send_ctx = GNUNET_malloc(sizeof(struct SendCallbackContext));
480 send_ctx->cont = cont;
482 send_ctx->cont_cls = cont_cls;
483 memcpy(&send_ctx->target, target, sizeof(struct GNUNET_PeerIdentity));
485 hash_from_uid(dv_handle->uid_gen, &uidhash);
487 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "set uid of %u or %u, hash of %s !!!!\n", dv_handle->uid_gen, htonl(dv_handle->uid_gen), GNUNET_h2s(&uidhash));
488 GNUNET_CONTAINER_multihashmap_put(dv_handle->send_callbacks, &uidhash, send_ctx, GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
493 /* Forward declaration */
494 void GNUNET_DV_disconnect(struct GNUNET_DV_Handle *handle);
497 transmit_start (void *cls, size_t size, void *buf)
499 struct StartContext *start_context = cls;
500 struct GNUNET_DV_Handle *handle = start_context->handle;
505 GNUNET_free(start_context->message);
506 GNUNET_free(start_context);
507 GNUNET_DV_disconnect(handle);
511 tsize = ntohs(start_context->message->size);
514 memcpy(buf, start_context->message, tsize);
522 * Connect to the DV service
524 * @param sched the scheduler to use
525 * @param cfg the configuration to use
526 * @param receive_handler method call when on receipt from the service
527 * @param receive_handler_cls closure for receive_handler
529 * @return handle to the DV service
531 struct GNUNET_DV_Handle *
532 GNUNET_DV_connect (struct GNUNET_SCHEDULER_Handle *sched,
533 const struct GNUNET_CONFIGURATION_Handle *cfg,
534 GNUNET_DV_MessageReceivedHandler receive_handler,
535 void *receive_handler_cls)
537 struct GNUNET_DV_Handle *handle;
538 struct GNUNET_MessageHeader *start_message;
539 struct StartContext *start_context;
540 handle = GNUNET_malloc(sizeof(struct GNUNET_DV_Handle));
543 handle->sched = sched;
544 handle->pending_list = NULL;
545 handle->current = NULL;
546 handle->do_destroy = GNUNET_NO;
548 handle->client = GNUNET_CLIENT_connect(sched, "dv", cfg);
549 handle->receive_handler = receive_handler;
550 handle->receive_cls = receive_handler_cls;
552 if (handle->client == NULL)
558 start_message = GNUNET_malloc(sizeof(struct GNUNET_MessageHeader));
559 start_message->size = htons(sizeof(struct GNUNET_MessageHeader));
560 start_message->type = htons(GNUNET_MESSAGE_TYPE_DV_START);
562 start_context = GNUNET_malloc(sizeof(struct StartContext));
563 start_context->handle = handle;
564 start_context->message = start_message;
565 GNUNET_CLIENT_notify_transmit_ready (handle->client,
566 sizeof(struct GNUNET_MessageHeader),
567 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 60),
569 &transmit_start, start_context);
571 handle->send_callbacks = GNUNET_CONTAINER_multihashmap_create(100);
573 GNUNET_CLIENT_receive (handle->client,
574 &handle_message_receipt,
575 handle, GNUNET_TIME_UNIT_FOREVER_REL);
581 * Disconnect from the DV service
583 * @param handle the current handle to the service to disconnect
585 void GNUNET_DV_disconnect(struct GNUNET_DV_Handle *handle)
587 struct PendingMessages *pos;
589 GNUNET_assert(handle != NULL);
591 if (handle->th != NULL) /* We have a live transmit request in the Aether */
593 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
596 if (handle->current != NULL) /* We are trying to send something now, clean it up */
597 GNUNET_free(handle->current);
598 while (NULL != (pos = handle->pending_list)) /* Remove all pending sends from the list */
600 handle->pending_list = pos->next;
603 if (handle->client != NULL) /* Finally, disconnect from the service */
605 GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
606 handle->client = NULL;
609 GNUNET_free (handle);
612 /* end of dv_api.c */