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 "../transport/plugin_transport.h"
42 * Store ready to send messages
44 struct PendingMessages
47 * Linked list of pending messages
49 struct PendingMessages *next;
52 * Message that is pending
54 struct GNUNET_DV_SendMessage *msg;
57 * Timeout for this message
59 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 * Handler for messages we receive from the DV service
101 GNUNET_DV_MessageReceivedHandler receive_handler;
104 * Closure for the receive handler
114 * Hashmap containing outstanding send requests awaiting confirmation.
116 struct GNUNET_CONTAINER_MultiHashMap *send_callbacks;
126 struct GNUNET_MessageHeader *message;
129 * Handle to service, in case of timeout
131 struct GNUNET_DV_Handle *handle;
134 struct SendCallbackContext
137 * The continuation to call once a message is confirmed sent (or failed)
139 GNUNET_TRANSPORT_TransmitContinuation cont;
142 * Closure to call with send continuation.
147 * Target of the message.
149 struct GNUNET_PeerIdentity target;
153 * Convert unique ID to hash code.
155 * @param uid unique ID to convert
156 * @param hash set to uid (extended with zeros)
159 hash_from_uid (uint32_t uid,
160 GNUNET_HashCode *hash)
162 memset (hash, 0, sizeof(GNUNET_HashCode));
163 *((uint32_t*)hash) = uid;
167 * Try to (re)connect to the dv service.
169 * @param ret handle to the (disconnected) dv service
171 * @return GNUNET_YES on success, GNUNET_NO on failure.
174 try_connect (struct GNUNET_DV_Handle *ret)
176 if (ret->client != NULL)
178 ret->client = GNUNET_CLIENT_connect (ret->sched, "dv", ret->cfg);
179 if (ret->client != NULL)
181 #if DEBUG_DV_MESSAGES
182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
183 _("Failed to connect to the dv service!\n"));
188 static void process_pending_message(struct GNUNET_DV_Handle *handle);
191 * Send complete, schedule next
193 * @param handle handle to the dv service
194 * @param code return code for send (unused)
197 finish (struct GNUNET_DV_Handle *handle, int code)
199 struct PendingMessages *pos = handle->current;
200 handle->current = NULL;
201 process_pending_message (handle);
203 GNUNET_free(pos->msg);
208 * Notification that we can send data
210 * @param cls handle to the dv service (struct GNUNET_DV_Handle)
211 * @param size how many bytes can we send
212 * @param buf where to copy the message to send
214 * @return how many bytes we copied to buf
217 transmit_pending (void *cls, size_t size, void *buf)
219 struct GNUNET_DV_Handle *handle = cls;
224 if (handle->current != NULL)
225 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV API: Transmit pending called with message type %d\n", ntohs(handle->current->msg->header.type));
231 GNUNET_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 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV API: Copied %d bytes into buffer!\n\n\n", tsize);
249 finish(handle, GNUNET_OK);
259 * Try to send messages from list of messages to send
261 * @param handle handle to the distance vector service
263 static void process_pending_message(struct GNUNET_DV_Handle *handle)
266 if (handle->current != NULL)
267 return; /* action already pending */
268 if (GNUNET_YES != try_connect (handle))
270 finish (handle, GNUNET_SYSERR);
274 /* schedule next action */
275 handle->current = handle->pending_list;
276 if (NULL == handle->current)
280 handle->pending_list = handle->pending_list->next;
281 handle->current->next = NULL;
284 (handle->th = GNUNET_CLIENT_notify_transmit_ready (handle->client,
285 ntohs(handle->current->msg->header.size),
286 handle->current->msg->timeout,
288 &transmit_pending, handle)))
291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
292 "Failed to transmit request to dv service.\n");
294 finish (handle, GNUNET_SYSERR);
299 * Add a pending message to the linked list
301 * @param handle handle to the specified DV api
302 * @param msg the message to add to the list
304 static void add_pending(struct GNUNET_DV_Handle *handle, struct GNUNET_DV_SendMessage *msg)
306 struct PendingMessages *new_message;
307 struct PendingMessages *pos;
308 struct PendingMessages *last;
310 new_message = GNUNET_malloc(sizeof(struct PendingMessages));
311 new_message->msg = msg;
313 if (handle->pending_list != NULL)
315 pos = handle->pending_list;
321 last->next = new_message;
325 handle->pending_list = new_message;
328 process_pending_message(handle);
332 * Handles a message sent from the DV service to us.
333 * Parse it out and give it to the plugin.
335 * @param cls the handle to the DV API
336 * @param msg the message that was received
338 void handle_message_receipt (void *cls,
339 const struct GNUNET_MessageHeader * msg)
341 struct GNUNET_DV_Handle *handle = cls;
342 struct GNUNET_DV_MessageReceived *received_msg;
343 struct GNUNET_DV_SendResultMessage *send_result_msg;
344 size_t packed_msg_len;
345 size_t sender_address_len;
346 char *sender_address;
348 char *packed_msg_start;
349 GNUNET_HashCode uidhash;
350 struct SendCallbackContext *send_ctx;
354 #if DEBUG_DV_MESSAGES
355 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV_API receive: connection closed\n");
357 return; /* Connection closed? */
360 GNUNET_assert((ntohs(msg->type) == GNUNET_MESSAGE_TYPE_TRANSPORT_DV_RECEIVE) || (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND_RESULT));
362 switch (ntohs(msg->type))
364 case GNUNET_MESSAGE_TYPE_TRANSPORT_DV_RECEIVE:
365 if (ntohs(msg->size) < sizeof(struct GNUNET_DV_MessageReceived))
368 received_msg = (struct GNUNET_DV_MessageReceived *)msg;
369 packed_msg_len = ntohl(received_msg->msg_len);
370 sender_address_len = ntohs(msg->size) - packed_msg_len - sizeof(struct GNUNET_DV_MessageReceived);
371 GNUNET_assert(sender_address_len > 0);
372 sender_address = GNUNET_malloc(sender_address_len);
373 memcpy(sender_address, &received_msg[1], sender_address_len);
374 packed_msg_start = (char *)&received_msg[1];
375 packed_msg = GNUNET_malloc(packed_msg_len);
376 memcpy(packed_msg, &packed_msg_start[sender_address_len], packed_msg_len);
378 #if DEBUG_DV_MESSAGES
379 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);
380 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV_API receive: message sender reported as %s\n", GNUNET_i2s(&received_msg->sender));
381 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV_API receive: distance is %u\n", ntohl(received_msg->distance));
384 handle->receive_handler(handle->receive_cls,
385 &received_msg->sender,
388 ntohl(received_msg->distance),
392 GNUNET_free(sender_address);
394 case GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND_RESULT:
395 if (ntohs(msg->size) < sizeof(struct GNUNET_DV_SendResultMessage))
398 send_result_msg = (struct GNUNET_DV_SendResultMessage *)msg;
399 hash_from_uid(ntohl(send_result_msg->uid), &uidhash);
400 send_ctx = GNUNET_CONTAINER_multihashmap_get(handle->send_callbacks, &uidhash);
402 if ((send_ctx != NULL) && (send_ctx->cont != NULL))
404 if (ntohl(send_result_msg->result) == 0)
406 send_ctx->cont(send_ctx->cont_cls, &send_ctx->target, GNUNET_OK);
410 send_ctx->cont(send_ctx->cont_cls, &send_ctx->target, GNUNET_SYSERR);
413 GNUNET_free_non_null(send_ctx);
418 GNUNET_CLIENT_receive (handle->client,
419 &handle_message_receipt,
420 handle, GNUNET_TIME_UNIT_FOREVER_REL);
424 * Send a message from the plugin to the DV service indicating that
425 * a message should be sent via DV to some peer.
427 * @param dv_handle the handle to the DV api
428 * @param target the final target of the message
429 * @param msgbuf the msg(s) to send
430 * @param msgbuf_size the size of msgbuf
431 * @param priority priority to pass on to core when sending the message
432 * @param timeout how long can this message be delayed (pass through to core)
433 * @param addr the address of this peer (internally known to DV)
434 * @param addrlen the length of the peer address
435 * @param cont continuation to call once the message has been sent (or failed)
436 * @param cont_cls closure for continuation
439 int GNUNET_DV_send (struct GNUNET_DV_Handle *dv_handle,
440 const struct GNUNET_PeerIdentity *target,
443 unsigned int priority,
444 struct GNUNET_TIME_Relative timeout,
447 GNUNET_TRANSPORT_TransmitContinuation
448 cont, void *cont_cls)
450 struct GNUNET_DV_SendMessage *msg;
451 struct SendCallbackContext *send_ctx;
452 char *end_of_message;
453 GNUNET_HashCode uidhash;
455 #if DEBUG_DV_MESSAGES
456 dv_handle->uid_gen = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, UINT32_MAX);
458 dv_handle->uid_gen++;
461 msize = sizeof(struct GNUNET_DV_SendMessage) + addrlen + msgbuf_size;
462 msg = GNUNET_malloc(msize);
463 msg->header.size = htons(msize);
464 msg->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND);
465 memcpy(&msg->target, target, sizeof(struct GNUNET_PeerIdentity));
466 msg->priority = htonl(priority);
467 msg->timeout = timeout;
468 msg->addrlen = htonl(addrlen);
469 msg->uid = htonl(dv_handle->uid_gen);
470 memcpy(&msg[1], addr, addrlen);
471 end_of_message = (char *)&msg[1];
472 end_of_message = &end_of_message[addrlen];
473 memcpy(end_of_message, msgbuf, msgbuf_size);
474 add_pending(dv_handle, msg);
475 send_ctx = GNUNET_malloc(sizeof(struct SendCallbackContext));
476 send_ctx->cont = cont;
477 send_ctx->cont_cls = cont_cls;
478 memcpy(&send_ctx->target, target, sizeof(struct GNUNET_PeerIdentity));
479 hash_from_uid(dv_handle->uid_gen, &uidhash);
480 GNUNET_CONTAINER_multihashmap_put(dv_handle->send_callbacks, &uidhash, send_ctx, GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
486 * Callback to transmit a start message to
487 * the DV service, once we can send
489 * @param cls struct StartContext
490 * @param size how much can we send
491 * @param buf where to copy the message
493 * @return number of bytes copied to buf
496 transmit_start (void *cls, size_t size, void *buf)
498 struct StartContext *start_context = cls;
499 struct GNUNET_DV_Handle *handle = start_context->handle;
502 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV API: sending start request to service\n");
506 GNUNET_free(start_context->message);
507 GNUNET_free(start_context);
508 GNUNET_DV_disconnect(handle);
512 tsize = ntohs(start_context->message->size);
515 memcpy(buf, start_context->message, tsize);
516 GNUNET_free(start_context->message);
517 GNUNET_free(start_context);
518 GNUNET_CLIENT_receive (handle->client,
519 &handle_message_receipt,
520 handle, GNUNET_TIME_UNIT_FOREVER_REL);
530 * Connect to the DV service
532 * @param sched the scheduler to use
533 * @param cfg the configuration to use
534 * @param receive_handler method call when on receipt from the service
535 * @param receive_handler_cls closure for receive_handler
537 * @return handle to the DV service
539 struct GNUNET_DV_Handle *
540 GNUNET_DV_connect (struct GNUNET_SCHEDULER_Handle *sched,
541 const struct GNUNET_CONFIGURATION_Handle *cfg,
542 GNUNET_DV_MessageReceivedHandler receive_handler,
543 void *receive_handler_cls)
545 struct GNUNET_DV_Handle *handle;
546 struct GNUNET_MessageHeader *start_message;
547 struct StartContext *start_context;
548 handle = GNUNET_malloc(sizeof(struct GNUNET_DV_Handle));
551 handle->sched = sched;
552 handle->pending_list = NULL;
553 handle->current = NULL;
555 handle->client = GNUNET_CLIENT_connect(sched, "dv", cfg);
556 handle->receive_handler = receive_handler;
557 handle->receive_cls = receive_handler_cls;
559 if (handle->client == NULL)
565 start_message = GNUNET_malloc(sizeof(struct GNUNET_MessageHeader));
566 start_message->size = htons(sizeof(struct GNUNET_MessageHeader));
567 start_message->type = htons(GNUNET_MESSAGE_TYPE_DV_START);
569 start_context = GNUNET_malloc(sizeof(struct StartContext));
570 start_context->handle = handle;
571 start_context->message = start_message;
572 GNUNET_CLIENT_notify_transmit_ready (handle->client,
573 sizeof(struct GNUNET_MessageHeader),
574 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 60),
576 &transmit_start, start_context);
578 handle->send_callbacks = GNUNET_CONTAINER_multihashmap_create(100);
584 * Disconnect from the DV service
586 * @param handle the current handle to the service to disconnect
588 void GNUNET_DV_disconnect(struct GNUNET_DV_Handle *handle)
590 struct PendingMessages *pos;
592 GNUNET_assert(handle != NULL);
594 if (handle->th != NULL) /* We have a live transmit request in the Aether */
596 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
599 if (handle->current != NULL) /* We are trying to send something now, clean it up */
600 GNUNET_free(handle->current);
601 while (NULL != (pos = handle->pending_list)) /* Remove all pending sends from the list */
603 handle->pending_list = pos->next;
606 if (handle->client != NULL) /* Finally, disconnect from the service */
608 GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
609 handle->client = NULL;
612 GNUNET_free (handle);
615 /* end of dv_api.c */