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.
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 "gnunet_transport_plugin.h"
41 #define LOG(kind,...) GNUNET_log_from (kind, "dv-api",__VA_ARGS__)
44 * Store ready to send messages
46 struct PendingMessages
49 * Linked list of pending messages
51 struct PendingMessages *next;
54 * Message that is pending
56 struct GNUNET_DV_SendMessage *msg;
59 * Timeout for this message
61 struct GNUNET_TIME_Absolute timeout;
66 * Handle for the service.
68 struct GNUNET_DV_Handle
72 * Configuration to use.
74 const struct GNUNET_CONFIGURATION_Handle *cfg;
77 * Socket (if available).
79 struct GNUNET_CLIENT_Connection *client;
82 * Currently pending transmission request.
84 struct GNUNET_CLIENT_TransmitHandle *th;
87 * List of the currently pending messages for the DV service.
89 struct PendingMessages *pending_list;
92 * Message we are currently sending.
94 struct PendingMessages *current;
97 * Handler for messages we receive from the DV service
99 GNUNET_DV_MessageReceivedHandler receive_handler;
102 * Closure for the receive handler
112 * Hashmap containing outstanding send requests awaiting confirmation.
114 struct GNUNET_CONTAINER_MultiHashMap *send_callbacks;
124 struct GNUNET_MessageHeader *message;
127 * Handle to service, in case of timeout
129 struct GNUNET_DV_Handle *handle;
132 struct SendCallbackContext
135 * The continuation to call once a message is confirmed sent (or failed)
137 GNUNET_TRANSPORT_TransmitContinuation cont;
140 * Closure to call with send continuation.
145 * Target of the message.
147 struct GNUNET_PeerIdentity target;
151 * Convert unique ID to hash code.
153 * @param uid unique ID to convert
154 * @param hash set to uid (extended with zeros)
157 hash_from_uid (uint32_t uid, GNUNET_HashCode * hash)
159 memset (hash, 0, sizeof (GNUNET_HashCode));
160 *((uint32_t *) hash) = uid;
164 * Try to (re)connect to the dv service.
166 * @param ret handle to the (disconnected) dv service
168 * @return GNUNET_YES on success, GNUNET_NO on failure.
171 try_connect (struct GNUNET_DV_Handle *ret)
173 if (ret->client != NULL)
175 ret->client = GNUNET_CLIENT_connect ("dv", ret->cfg);
176 if (ret->client != NULL)
178 #if DEBUG_DV_MESSAGES
179 LOG (GNUNET_ERROR_TYPE_DEBUG, _("Failed to connect to the dv service!\n"));
185 process_pending_message (struct GNUNET_DV_Handle *handle);
188 * Send complete, schedule next
190 * @param handle handle to the dv service
191 * @param code return code for send (unused)
194 finish (struct GNUNET_DV_Handle *handle, int code)
196 struct PendingMessages *pos = handle->current;
198 handle->current = NULL;
199 process_pending_message (handle);
201 GNUNET_free (pos->msg);
206 * Notification that we can send data
208 * @param cls handle to the dv service (struct GNUNET_DV_Handle)
209 * @param size how many bytes can we send
210 * @param buf where to copy the message to send
212 * @return how many bytes we copied to buf
215 transmit_pending (void *cls, size_t size, void *buf)
217 struct GNUNET_DV_Handle *handle = cls;
222 if (handle->current != NULL)
223 LOG (GNUNET_ERROR_TYPE_DEBUG,
224 "DV API: Transmit pending called with message type %d\n",
225 ntohs (handle->current->msg->header.type));
231 LOG (GNUNET_ERROR_TYPE_DEBUG, "DV API: Transmit pending FAILED!\n\n\n");
233 finish (handle, GNUNET_SYSERR);
240 if (handle->current != NULL)
242 tsize = ntohs (handle->current->msg->header.size);
245 memcpy (buf, handle->current->msg, tsize);
247 LOG (GNUNET_ERROR_TYPE_DEBUG,
248 "DV API: Copied %d bytes into buffer!\n\n\n", tsize);
250 finish (handle, GNUNET_OK);
260 * Try to send messages from list of messages to send
262 * @param handle handle to the distance vector service
265 process_pending_message (struct GNUNET_DV_Handle *handle)
268 if (handle->current != NULL)
269 return; /* action already pending */
270 if (GNUNET_YES != try_connect (handle))
272 finish (handle, GNUNET_SYSERR);
276 /* schedule next action */
277 handle->current = handle->pending_list;
278 if (NULL == handle->current)
282 handle->pending_list = handle->pending_list->next;
283 handle->current->next = NULL;
287 GNUNET_CLIENT_notify_transmit_ready (handle->client,
288 ntohs (handle->current->msg->
290 handle->current->msg->timeout,
291 GNUNET_YES, &transmit_pending,
295 LOG (GNUNET_ERROR_TYPE_DEBUG,
296 "Failed to transmit request to dv service.\n");
298 finish (handle, GNUNET_SYSERR);
303 * Add a pending message to the linked list
305 * @param handle handle to the specified DV api
306 * @param msg the message to add to the list
309 add_pending (struct GNUNET_DV_Handle *handle, struct GNUNET_DV_SendMessage *msg)
311 struct PendingMessages *new_message;
312 struct PendingMessages *pos;
313 struct PendingMessages *last;
315 new_message = GNUNET_malloc (sizeof (struct PendingMessages));
316 new_message->msg = msg;
318 if (handle->pending_list != NULL)
320 pos = handle->pending_list;
326 last->next = new_message;
330 handle->pending_list = new_message;
333 process_pending_message (handle);
337 * Handles a message sent from the DV service to us.
338 * Parse it out and give it to the plugin.
340 * @param cls the handle to the DV API
341 * @param msg the message that was received
344 handle_message_receipt (void *cls, const struct GNUNET_MessageHeader *msg)
346 struct GNUNET_DV_Handle *handle = cls;
347 struct GNUNET_DV_MessageReceived *received_msg;
348 struct GNUNET_DV_SendResultMessage *send_result_msg;
349 size_t packed_msg_len;
350 size_t sender_address_len;
351 char *sender_address;
353 char *packed_msg_start;
354 GNUNET_HashCode uidhash;
355 struct SendCallbackContext *send_ctx;
359 #if DEBUG_DV_MESSAGES
360 LOG (GNUNET_ERROR_TYPE_DEBUG, "DV_API receive: connection closed\n");
362 return; /* Connection closed? */
365 GNUNET_assert ((ntohs (msg->type) == GNUNET_MESSAGE_TYPE_TRANSPORT_DV_RECEIVE)
366 || (ntohs (msg->type) ==
367 GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND_RESULT));
369 switch (ntohs (msg->type))
371 case GNUNET_MESSAGE_TYPE_TRANSPORT_DV_RECEIVE:
372 if (ntohs (msg->size) < sizeof (struct GNUNET_DV_MessageReceived))
375 received_msg = (struct GNUNET_DV_MessageReceived *) msg;
376 packed_msg_len = ntohl (received_msg->msg_len);
378 ntohs (msg->size) - packed_msg_len -
379 sizeof (struct GNUNET_DV_MessageReceived);
380 GNUNET_assert (sender_address_len > 0);
381 sender_address = GNUNET_malloc (sender_address_len);
382 memcpy (sender_address, &received_msg[1], sender_address_len);
383 packed_msg_start = (char *) &received_msg[1];
384 packed_msg = GNUNET_malloc (packed_msg_len);
385 memcpy (packed_msg, &packed_msg_start[sender_address_len], packed_msg_len);
387 #if DEBUG_DV_MESSAGES
388 LOG (GNUNET_ERROR_TYPE_DEBUG,
389 "DV_API receive: packed message type: %d or %d\n",
390 ntohs (((struct GNUNET_MessageHeader *) packed_msg)->type),
391 ((struct GNUNET_MessageHeader *) packed_msg)->type);
392 LOG (GNUNET_ERROR_TYPE_DEBUG,
393 "DV_API receive: message sender reported as %s\n",
394 GNUNET_i2s (&received_msg->sender));
395 LOG (GNUNET_ERROR_TYPE_DEBUG, "DV_API receive: distance is %u\n",
396 ntohl (received_msg->distance));
399 handle->receive_handler (handle->receive_cls, &received_msg->sender,
400 packed_msg, packed_msg_len,
401 ntohl (received_msg->distance), sender_address,
404 GNUNET_free (sender_address);
406 case GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND_RESULT:
407 if (ntohs (msg->size) < sizeof (struct GNUNET_DV_SendResultMessage))
410 send_result_msg = (struct GNUNET_DV_SendResultMessage *) msg;
411 hash_from_uid (ntohl (send_result_msg->uid), &uidhash);
413 GNUNET_CONTAINER_multihashmap_get (handle->send_callbacks, &uidhash);
415 if ((send_ctx != NULL) && (send_ctx->cont != NULL))
417 if (ntohl (send_result_msg->result) == 0)
419 send_ctx->cont (send_ctx->cont_cls, &send_ctx->target, GNUNET_OK);
423 send_ctx->cont (send_ctx->cont_cls, &send_ctx->target, GNUNET_SYSERR);
426 GNUNET_free_non_null (send_ctx);
431 GNUNET_CLIENT_receive (handle->client, &handle_message_receipt, handle,
432 GNUNET_TIME_UNIT_FOREVER_REL);
436 * Send a message from the plugin to the DV service indicating that
437 * a message should be sent via DV to some peer.
439 * @param dv_handle the handle to the DV api
440 * @param target the final target of the message
441 * @param msgbuf the msg(s) to send
442 * @param msgbuf_size the size of msgbuf
443 * @param priority priority to pass on to core when sending the message
444 * @param timeout how long can this message be delayed (pass through to core)
445 * @param addr the address of this peer (internally known to DV)
446 * @param addrlen the length of the peer address
447 * @param cont continuation to call once the message has been sent (or failed)
448 * @param cont_cls closure for continuation
452 GNUNET_DV_send (struct GNUNET_DV_Handle *dv_handle,
453 const struct GNUNET_PeerIdentity *target, const char *msgbuf,
454 size_t msgbuf_size, unsigned int priority,
455 struct GNUNET_TIME_Relative timeout, const void *addr,
456 size_t addrlen, GNUNET_TRANSPORT_TransmitContinuation cont,
459 struct GNUNET_DV_SendMessage *msg;
460 struct SendCallbackContext *send_ctx;
461 char *end_of_message;
462 GNUNET_HashCode uidhash;
465 #if DEBUG_DV_MESSAGES
467 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, UINT32_MAX);
469 dv_handle->uid_gen++;
472 msize = sizeof (struct GNUNET_DV_SendMessage) + addrlen + msgbuf_size;
473 msg = GNUNET_malloc (msize);
474 msg->header.size = htons (msize);
475 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND);
476 memcpy (&msg->target, target, sizeof (struct GNUNET_PeerIdentity));
477 msg->priority = htonl (priority);
478 msg->timeout = timeout;
479 msg->addrlen = htonl (addrlen);
480 msg->uid = htonl (dv_handle->uid_gen);
481 memcpy (&msg[1], addr, addrlen);
482 end_of_message = (char *) &msg[1];
483 end_of_message = &end_of_message[addrlen];
484 memcpy (end_of_message, msgbuf, msgbuf_size);
485 add_pending (dv_handle, msg);
486 send_ctx = GNUNET_malloc (sizeof (struct SendCallbackContext));
487 send_ctx->cont = cont;
488 send_ctx->cont_cls = cont_cls;
489 memcpy (&send_ctx->target, target, sizeof (struct GNUNET_PeerIdentity));
490 hash_from_uid (dv_handle->uid_gen, &uidhash);
491 GNUNET_CONTAINER_multihashmap_put (dv_handle->send_callbacks, &uidhash,
493 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
499 * Callback to transmit a start message to
500 * the DV service, once we can send
502 * @param cls struct StartContext
503 * @param size how much can we send
504 * @param buf where to copy the message
506 * @return number of bytes copied to buf
509 transmit_start (void *cls, size_t size, void *buf)
511 struct StartContext *start_context = cls;
512 struct GNUNET_DV_Handle *handle = start_context->handle;
516 LOG (GNUNET_ERROR_TYPE_DEBUG, "DV API: sending start request to service\n");
520 GNUNET_free (start_context->message);
521 GNUNET_free (start_context);
522 GNUNET_DV_disconnect (handle);
526 tsize = ntohs (start_context->message->size);
529 memcpy (buf, start_context->message, tsize);
530 GNUNET_free (start_context->message);
531 GNUNET_free (start_context);
532 GNUNET_CLIENT_receive (handle->client, &handle_message_receipt, handle,
533 GNUNET_TIME_UNIT_FOREVER_REL);
543 * Connect to the DV service
545 * @param cfg the configuration to use
546 * @param receive_handler method call when on receipt from the service
547 * @param receive_handler_cls closure for receive_handler
549 * @return handle to the DV service
551 struct GNUNET_DV_Handle *
552 GNUNET_DV_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
553 GNUNET_DV_MessageReceivedHandler receive_handler,
554 void *receive_handler_cls)
556 struct GNUNET_DV_Handle *handle;
557 struct GNUNET_MessageHeader *start_message;
558 struct StartContext *start_context;
560 handle = GNUNET_malloc (sizeof (struct GNUNET_DV_Handle));
563 handle->pending_list = NULL;
564 handle->current = NULL;
566 handle->client = GNUNET_CLIENT_connect ("dv", cfg);
567 handle->receive_handler = receive_handler;
568 handle->receive_cls = receive_handler_cls;
570 if (handle->client == NULL)
572 GNUNET_free (handle);
576 start_message = GNUNET_malloc (sizeof (struct GNUNET_MessageHeader));
577 start_message->size = htons (sizeof (struct GNUNET_MessageHeader));
578 start_message->type = htons (GNUNET_MESSAGE_TYPE_DV_START);
580 start_context = GNUNET_malloc (sizeof (struct StartContext));
581 start_context->handle = handle;
582 start_context->message = start_message;
583 GNUNET_CLIENT_notify_transmit_ready (handle->client,
584 sizeof (struct GNUNET_MessageHeader),
585 GNUNET_TIME_relative_multiply
586 (GNUNET_TIME_UNIT_SECONDS, 60),
587 GNUNET_YES, &transmit_start,
590 handle->send_callbacks = GNUNET_CONTAINER_multihashmap_create (100);
596 * Disconnect from the DV service
598 * @param handle the current handle to the service to disconnect
601 GNUNET_DV_disconnect (struct GNUNET_DV_Handle *handle)
603 struct PendingMessages *pos;
605 GNUNET_assert (handle != NULL);
607 if (handle->th != NULL) /* We have a live transmit request in the Aether */
609 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
612 if (handle->current != NULL) /* We are trying to send something now, clean it up */
613 GNUNET_free (handle->current);
614 while (NULL != (pos = handle->pending_list)) /* Remove all pending sends from the list */
616 handle->pending_list = pos->next;
619 if (handle->client != NULL) /* Finally, disconnect from the service */
621 GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
622 handle->client = NULL;
625 GNUNET_free (handle);
628 /* end of dv_api.c */