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"
41 struct PendingMessages
44 * Linked list of pending messages
46 struct PendingMessages *next;
49 * Message that is pending
51 struct GNUNET_DV_SendMessage *msg;
54 * Timeout for this message
56 struct GNUNET_TIME_Absolute timeout;
63 * Handle for the service.
65 struct GNUNET_DV_Handle
70 struct GNUNET_SCHEDULER_Handle *sched;
73 * Configuration to use.
75 const struct GNUNET_CONFIGURATION_Handle *cfg;
78 * Socket (if available).
80 struct GNUNET_CLIENT_Connection *client;
83 * Currently pending transmission request.
85 struct GNUNET_CLIENT_TransmitHandle *th;
88 * List of the currently pending messages for the DV service.
90 struct PendingMessages *pending_list;
93 * Message we are currently sending.
95 struct PendingMessages *current;
98 * Kill off the connection and any pending messages.
103 * Handler for messages we receive from the DV service
105 GNUNET_DV_MessageReceivedHandler receive_handler;
108 * Closure for the receive handler
121 struct GNUNET_MessageHeader *message;
124 * Handle to service, in case of timeout
126 struct GNUNET_DV_Handle *handle;
131 * Try to (re)connect to the dv service.
133 * @return GNUNET_YES on success, GNUNET_NO on failure.
136 try_connect (struct GNUNET_DV_Handle *ret)
138 if (ret->client != NULL)
140 ret->client = GNUNET_CLIENT_connect (ret->sched, "dv", ret->cfg);
141 if (ret->client != NULL)
144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
145 _("Failed to connect to the dv service!\n"));
150 static void process_pending_message(struct GNUNET_DV_Handle *handle);
153 * Send complete, schedule next
156 finish (struct GNUNET_DV_Handle *handle, int code)
158 struct PendingMessages *pos = handle->current;
159 handle->current = NULL;
160 process_pending_message (handle);
167 transmit_pending (void *cls, size_t size, void *buf)
169 struct GNUNET_DV_Handle *handle = cls;
174 if (handle->current != NULL)
175 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV API: Transmit pending called with message type %d\n", ntohs(handle->current->msg->header.type));
181 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV API: Transmit pending FAILED!\n\n\n");
183 finish(handle, GNUNET_SYSERR);
190 if (handle->current != NULL)
192 tsize = ntohs(handle->current->msg->header.size);
195 memcpy(buf, handle->current->msg, tsize);
197 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV API: Copied %d bytes into buffer!\n\n\n", tsize);
199 finish(handle, GNUNET_OK);
209 * Try to send messages from list of messages to send
211 static void process_pending_message(struct GNUNET_DV_Handle *handle)
214 if (handle->current != NULL)
215 return; /* action already pending */
216 if (GNUNET_YES != try_connect (handle))
218 finish (handle, GNUNET_SYSERR);
222 /* schedule next action */
223 handle->current = handle->pending_list;
224 if (NULL == handle->current)
226 if (handle->do_destroy)
228 handle->do_destroy = GNUNET_NO;
229 //GNUNET_DV_disconnect (handle); /* FIXME: replace with proper disconnect stuffs */
233 handle->pending_list = handle->pending_list->next;
234 handle->current->next = NULL;
237 (handle->th = GNUNET_CLIENT_notify_transmit_ready (handle->client,
238 ntohl(handle->current->msg->msgbuf_size),
239 handle->current->msg->timeout,
241 &transmit_pending, handle)))
244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
245 "Failed to transmit request to dv service.\n");
247 finish (handle, GNUNET_SYSERR);
252 * Add a pending message to the linked list
254 * @param handle handle to the specified DV api
255 * @param msg the message to add to the list
257 static void add_pending(struct GNUNET_DV_Handle *handle, struct GNUNET_DV_SendMessage *msg)
259 struct PendingMessages *new_message;
260 struct PendingMessages *pos;
261 struct PendingMessages *last;
263 new_message = GNUNET_malloc(sizeof(struct PendingMessages));
264 new_message->msg = msg;
266 if (handle->pending_list != NULL)
268 pos = handle->pending_list;
274 new_message->next = last->next; /* Should always be null */
275 last->next = new_message;
279 new_message->next = handle->pending_list; /* Will always be null */
280 handle->pending_list = new_message;
283 process_pending_message(handle);
287 void handle_message_receipt (void *cls,
288 const struct GNUNET_MessageHeader * msg)
290 struct GNUNET_DV_Handle *handle = cls;
291 struct GNUNET_DV_MessageReceived *received_msg;
292 size_t packed_msg_len;
293 size_t sender_address_len;
294 char *sender_address;
296 char *packed_msg_start;
300 return; /* Connection closed? */
303 GNUNET_assert(ntohs(msg->type) == GNUNET_MESSAGE_TYPE_TRANSPORT_DV_RECEIVE);
305 if (ntohs(msg->size) < sizeof(struct GNUNET_DV_MessageReceived))
308 received_msg = (struct GNUNET_DV_MessageReceived *)msg;
309 packed_msg_len = ntohl(received_msg->msg_len);
310 sender_address_len = ntohl(received_msg->sender_address_len);
312 GNUNET_assert(ntohs(msg->size) == (sizeof(struct GNUNET_DV_MessageReceived) + packed_msg_len + sender_address_len));
314 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "dv api receives message, size checks out!\n");
316 sender_address = GNUNET_malloc(sender_address_len);
317 memcpy(sender_address, &received_msg[1], sender_address_len);
318 packed_msg_start = (char *)&received_msg[1];
319 packed_msg = GNUNET_malloc(packed_msg_len);
320 memcpy(packed_msg, &packed_msg_start[sender_address_len], packed_msg_len);
323 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);
324 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV_API receive: message sender reported as %s\n", GNUNET_i2s(&received_msg->sender));
325 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV_API receive: distance is %u\n", ntohl(received_msg->distance));
328 handle->receive_handler(handle->receive_cls,
329 &received_msg->sender,
332 ntohl(received_msg->distance),
336 GNUNET_free(sender_address);
338 GNUNET_CLIENT_receive (handle->client,
339 &handle_message_receipt,
340 handle, GNUNET_TIME_UNIT_FOREVER_REL);
344 * Send a message from the plugin to the DV service indicating that
345 * a message should be sent via DV to some peer.
347 * @param dv_handle the handle to the DV api
348 * @param target the final target of the message
349 * @param msgbuf the msg(s) to send
350 * @param msgbuf_size the size of msgbuf
351 * @param priority priority to pass on to core when sending the message
352 * @param timeout how long can this message be delayed (pass through to core)
353 * @param addr the address of this peer (internally known to DV)
354 * @param addrlen the length of the peer address
357 int GNUNET_DV_send (struct GNUNET_DV_Handle *dv_handle,
358 const struct GNUNET_PeerIdentity *target,
361 unsigned int priority,
362 struct GNUNET_TIME_Relative timeout,
366 struct GNUNET_DV_SendMessage *msg;
367 char *end_of_message;
368 /* FIXME: Copy message to end of thingy, can't just allocate dummy! */
370 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);
372 msg = GNUNET_malloc(sizeof(struct GNUNET_DV_SendMessage) + addrlen + msgbuf_size);
373 msg->header.size = htons(sizeof(struct GNUNET_DV_SendMessage) + addrlen + msgbuf_size);
374 msg->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND);
375 memcpy(&msg->target, target, sizeof(struct GNUNET_PeerIdentity));
376 msg->msgbuf_size = htonl(msgbuf_size);
377 msg->priority = htonl(priority);
378 msg->timeout = timeout;
379 msg->addrlen = htonl(addrlen);
380 memcpy(&msg[1], addr, addrlen);
381 end_of_message = (char *)&msg[1];
382 end_of_message = &end_of_message[addrlen];
383 memcpy(end_of_message, msgbuf, msgbuf_size);
384 add_pending(dv_handle, msg);
389 /* Forward declaration */
390 void GNUNET_DV_disconnect(struct GNUNET_DV_Handle *handle);
393 transmit_start (void *cls, size_t size, void *buf)
395 struct StartContext *start_context = cls;
396 struct GNUNET_DV_Handle *handle = start_context->handle;
401 GNUNET_free(start_context->message);
402 GNUNET_free(start_context);
403 GNUNET_DV_disconnect(handle);
407 tsize = ntohs(start_context->message->size);
410 memcpy(buf, start_context->message, tsize);
418 * Connect to the DV service
420 * @param sched the scheduler to use
421 * @param cfg the configuration to use
422 * @param receive_handler method call when on receipt from the service
423 * @param receive_handler_cls closure for receive_handler
425 * @return handle to the DV service
427 struct GNUNET_DV_Handle *
428 GNUNET_DV_connect (struct GNUNET_SCHEDULER_Handle *sched,
429 const struct GNUNET_CONFIGURATION_Handle *cfg,
430 GNUNET_DV_MessageReceivedHandler receive_handler,
431 void *receive_handler_cls)
433 struct GNUNET_DV_Handle *handle;
434 struct GNUNET_MessageHeader *start_message;
435 struct StartContext *start_context;
436 handle = GNUNET_malloc(sizeof(struct GNUNET_DV_Handle));
439 handle->sched = sched;
440 handle->pending_list = NULL;
441 handle->current = NULL;
442 handle->do_destroy = GNUNET_NO;
444 handle->client = GNUNET_CLIENT_connect(sched, "dv", cfg);
445 handle->receive_handler = receive_handler;
446 handle->receive_cls = receive_handler_cls;
448 if (handle->client == NULL)
454 start_message = GNUNET_malloc(sizeof(struct GNUNET_MessageHeader));
455 start_message->size = htons(sizeof(struct GNUNET_MessageHeader));
456 start_message->type = htons(GNUNET_MESSAGE_TYPE_DV_START);
458 start_context = GNUNET_malloc(sizeof(struct StartContext));
459 start_context->handle = handle;
460 start_context->message = start_message;
461 GNUNET_CLIENT_notify_transmit_ready (handle->client,
462 sizeof(struct GNUNET_MessageHeader),
463 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 60),
465 &transmit_start, start_context);
467 GNUNET_CLIENT_receive (handle->client,
468 &handle_message_receipt,
469 handle, GNUNET_TIME_UNIT_FOREVER_REL);
475 * Disconnect from the DV service
477 * @param handle the current handle to the service to disconnect
479 void GNUNET_DV_disconnect(struct GNUNET_DV_Handle *handle)
481 struct PendingMessages *pos;
483 GNUNET_assert(handle != NULL);
485 if (handle->th != NULL) /* We have a live transmit request in the Aether */
487 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
490 if (handle->current != NULL) /* We are trying to send something now, clean it up */
491 GNUNET_free(handle->current);
492 while (NULL != (pos = handle->pending_list)) /* Remove all pending sends from the list */
494 handle->pending_list = pos->next;
497 if (handle->client != NULL) /* Finally, disconnect from the service */
499 GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
500 handle->client = NULL;
503 GNUNET_free (handle);
506 /* end of dv_api.c */