dv changes. add propagation of disconnect messages, dv updates on peer direct connect...
[oweals/gnunet.git] / src / dv / dv_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file dv/dv_api.c
23  * @brief library to access the DV service
24  * @author Christian Grothoff
25  * @author Nathan Evans
26  */
27 #include "platform.h"
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"
38 #include "dv.h"
39 #include "../transport/plugin_transport.h"
40
41
42 struct PendingMessages
43 {
44   /**
45    * Linked list of pending messages
46    */
47   struct PendingMessages *next;
48
49   /**
50    * Message that is pending
51    */
52   struct GNUNET_DV_SendMessage *msg;
53
54   /**
55    * Timeout for this message
56    */
57   struct GNUNET_TIME_Absolute timeout;
58
59 };
60
61
62
63 /**
64  * Handle for the service.
65  */
66 struct GNUNET_DV_Handle
67 {
68   /**
69    * Our scheduler.
70    */
71   struct GNUNET_SCHEDULER_Handle *sched;
72
73   /**
74    * Configuration to use.
75    */
76   const struct GNUNET_CONFIGURATION_Handle *cfg;
77
78   /**
79    * Socket (if available).
80    */
81   struct GNUNET_CLIENT_Connection *client;
82
83   /**
84    * Currently pending transmission request.
85    */
86   struct GNUNET_CLIENT_TransmitHandle *th;
87
88   /**
89    * List of the currently pending messages for the DV service.
90    */
91   struct PendingMessages *pending_list;
92
93   /**
94    * Message we are currently sending.
95    */
96   struct PendingMessages *current;
97
98   /**
99    * Kill off the connection and any pending messages.
100    */
101   int do_destroy;
102
103   /**
104    * Handler for messages we receive from the DV service
105    */
106   GNUNET_DV_MessageReceivedHandler receive_handler;
107
108   /**
109    * Closure for the receive handler
110    */
111   void *receive_cls;
112
113   /**
114    * Current unique ID
115    */
116   uint32_t uid_gen;
117
118   /**
119    * Hashmap containing outstanding send requests awaiting confirmation.
120    */
121   struct GNUNET_CONTAINER_MultiHashMap *send_callbacks;
122
123 };
124
125
126 struct StartContext
127 {
128   /**
129    * Start message
130    */
131   struct GNUNET_MessageHeader *message;
132
133   /**
134    * Handle to service, in case of timeout
135    */
136   struct GNUNET_DV_Handle *handle;
137 };
138
139 struct SendCallbackContext
140 {
141   /**
142    * The continuation to call once a message is confirmed sent (or failed)
143    */
144   GNUNET_TRANSPORT_TransmitContinuation cont;
145
146   /**
147    * Closure to call with send continuation.
148    */
149   void *cont_cls;
150
151   /**
152    * Target of the message.
153    */
154   struct GNUNET_PeerIdentity target;
155 };
156
157 /**
158  * Convert unique ID to hash code.
159  *
160  * @param uid unique ID to convert
161  * @param hash set to uid (extended with zeros)
162  */
163 static void
164 hash_from_uid (uint32_t uid,
165                GNUNET_HashCode *hash)
166 {
167   memset (hash, 0, sizeof(GNUNET_HashCode));
168   *((uint32_t*)hash) = uid;
169 }
170
171 /**
172  * Try to (re)connect to the dv service.
173  *
174  * @return GNUNET_YES on success, GNUNET_NO on failure.
175  */
176 static int
177 try_connect (struct GNUNET_DV_Handle *ret)
178 {
179   if (ret->client != NULL)
180     return GNUNET_OK;
181   ret->client = GNUNET_CLIENT_connect (ret->sched, "dv", ret->cfg);
182   if (ret->client != NULL)
183     return GNUNET_YES;
184 #if DEBUG_STATISTICS
185   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
186               _("Failed to connect to the dv service!\n"));
187 #endif
188   return GNUNET_NO;
189 }
190
191 static void process_pending_message(struct GNUNET_DV_Handle *handle);
192
193 /**
194  * Send complete, schedule next
195  */
196 static void
197 finish (struct GNUNET_DV_Handle *handle, int code)
198 {
199   struct PendingMessages *pos = handle->current;
200   handle->current = NULL;
201   process_pending_message (handle);
202
203   GNUNET_free (pos);
204 }
205
206
207 static size_t
208 transmit_pending (void *cls, size_t size, void *buf)
209 {
210   struct GNUNET_DV_Handle *handle = cls;
211   size_t ret;
212   size_t tsize;
213
214 #if DEBUG_DV
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));
217 #endif
218
219   if (buf == NULL)
220     {
221 #if DEBUG_DV
222       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV API: Transmit pending FAILED!\n\n\n");
223 #endif
224       finish(handle, GNUNET_SYSERR);
225       return 0;
226     }
227   handle->th = NULL;
228
229   ret = 0;
230
231   if (handle->current != NULL)
232   {
233     tsize = ntohs(handle->current->msg->header.size);
234     if (size >= tsize)
235     {
236       memcpy(buf, handle->current->msg, tsize);
237 #if DEBUG_DV
238       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV API: Copied %d bytes into buffer!\n\n\n", tsize);
239 #endif
240       finish(handle, GNUNET_OK);
241       return tsize;
242     }
243
244   }
245
246   return ret;
247 }
248
249 /**
250  * Try to send messages from list of messages to send
251  */
252 static void process_pending_message(struct GNUNET_DV_Handle *handle)
253 {
254
255   if (handle->current != NULL)
256     return;                     /* action already pending */
257   if (GNUNET_YES != try_connect (handle))
258     {
259       finish (handle, GNUNET_SYSERR);
260       return;
261     }
262
263   /* schedule next action */
264   handle->current = handle->pending_list;
265   if (NULL == handle->current)
266     {
267       if (handle->do_destroy)
268         {
269           handle->do_destroy = GNUNET_NO;
270           //GNUNET_DV_disconnect (handle); /* FIXME: replace with proper disconnect stuffs */
271         }
272       return;
273     }
274   handle->pending_list = handle->pending_list->next;
275   handle->current->next = NULL;
276
277   if (NULL ==
278       (handle->th = GNUNET_CLIENT_notify_transmit_ready (handle->client,
279                                                     ntohl(handle->current->msg->msgbuf_size),
280                                                     handle->current->msg->timeout,
281                                                     GNUNET_YES,
282                                                     &transmit_pending, handle)))
283     {
284 #if DEBUG_DV
285       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
286                   "Failed to transmit request to dv service.\n");
287 #endif
288       finish (handle, GNUNET_SYSERR);
289     }
290 }
291
292 /**
293  * Add a pending message to the linked list
294  *
295  * @param handle handle to the specified DV api
296  * @param msg the message to add to the list
297  */
298 static void add_pending(struct GNUNET_DV_Handle *handle, struct GNUNET_DV_SendMessage *msg)
299 {
300   struct PendingMessages *new_message;
301   struct PendingMessages *pos;
302   struct PendingMessages *last;
303
304   new_message = GNUNET_malloc(sizeof(struct PendingMessages));
305   new_message->msg = msg;
306
307   if (handle->pending_list != NULL)
308     {
309       pos = handle->pending_list;
310       while(pos != NULL)
311         {
312           last = pos;
313           pos = pos->next;
314         }
315       new_message->next = last->next; /* Should always be null */
316       last->next = new_message;
317     }
318   else
319     {
320       new_message->next = handle->pending_list; /* Will always be null */
321       handle->pending_list = new_message;
322     }
323
324   process_pending_message(handle);
325 }
326
327 /**
328  * Handles a message sent from the DV service to us.
329  * Parse it out and give it to the plugin.
330  *
331  * @param cls the handle to the DV API
332  * @param msg the message that was received
333  */
334 void handle_message_receipt (void *cls,
335                              const struct GNUNET_MessageHeader * msg)
336 {
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;
343   char *packed_msg;
344   char *packed_msg_start;
345   GNUNET_HashCode uidhash;
346   struct SendCallbackContext *send_ctx;
347
348   if (msg == NULL)
349   {
350     return; /* Connection closed? */
351   }
352
353   GNUNET_assert((ntohs(msg->type) == GNUNET_MESSAGE_TYPE_TRANSPORT_DV_RECEIVE) || (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND_RESULT));
354
355   switch (ntohs(msg->type))
356   {
357   case GNUNET_MESSAGE_TYPE_TRANSPORT_DV_RECEIVE:
358     if (ntohs(msg->size) < sizeof(struct GNUNET_DV_MessageReceived))
359       return;
360
361     received_msg = (struct GNUNET_DV_MessageReceived *)msg;
362     packed_msg_len = ntohl(received_msg->msg_len);
363     sender_address_len = ntohl(received_msg->sender_address_len);
364
365     GNUNET_assert(ntohs(msg->size) == (sizeof(struct GNUNET_DV_MessageReceived) + packed_msg_len + sender_address_len));
366 #if DEBUG_DV
367     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "dv api receives message, size checks out!\n");
368 #endif
369     sender_address = GNUNET_malloc(sender_address_len);
370     memcpy(sender_address, &received_msg[1], sender_address_len);
371     packed_msg_start = (char *)&received_msg[1];
372     packed_msg = GNUNET_malloc(packed_msg_len);
373     memcpy(packed_msg, &packed_msg_start[sender_address_len], packed_msg_len);
374
375 #if DEBUG_DV
376     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);
377     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV_API receive: message sender reported as %s\n", GNUNET_i2s(&received_msg->sender));
378     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV_API receive: distance is %u\n", ntohl(received_msg->distance));
379 #endif
380
381     handle->receive_handler(handle->receive_cls,
382                             &received_msg->sender,
383                             packed_msg,
384                             packed_msg_len,
385                             ntohl(received_msg->distance),
386                             sender_address,
387                             sender_address_len);
388
389     GNUNET_free(sender_address);
390     break;
391   case GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND_RESULT:
392     if (ntohs(msg->size) < sizeof(struct GNUNET_DV_SendResultMessage))
393       return;
394
395     send_result_msg = (struct GNUNET_DV_SendResultMessage *)msg;
396     hash_from_uid(ntohl(send_result_msg->uid), &uidhash);
397     send_ctx = GNUNET_CONTAINER_multihashmap_get(handle->send_callbacks, &uidhash);
398     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));
399
400     if ((send_ctx != NULL) && (send_ctx->cont != NULL))
401       {
402 #if DEBUG_DV
403         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "dv api notifies transport of send result (%u)!\n", ntohl(send_result_msg->result));
404 #endif
405         if (ntohl(send_result_msg->result) == 0)
406           {
407             send_ctx->cont(send_ctx->cont_cls, &send_ctx->target, GNUNET_OK);
408           }
409         else
410           {
411             send_ctx->cont(send_ctx->cont_cls, &send_ctx->target, GNUNET_SYSERR);
412           }
413       }
414     GNUNET_free_non_null(send_ctx);
415     break;
416   default:
417     break;
418   }
419   GNUNET_CLIENT_receive (handle->client,
420                          &handle_message_receipt,
421                          handle, GNUNET_TIME_UNIT_FOREVER_REL);
422 }
423
424 /**
425  * Send a message from the plugin to the DV service indicating that
426  * a message should be sent via DV to some peer.
427  *
428  * @param dv_handle the handle to the DV api
429  * @param target the final target of the message
430  * @param msgbuf the msg(s) to send
431  * @param msgbuf_size the size of msgbuf
432  * @param priority priority to pass on to core when sending the message
433  * @param timeout how long can this message be delayed (pass through to core)
434  * @param addr the address of this peer (internally known to DV)
435  * @param addrlen the length of the peer address
436  *
437  */
438 int GNUNET_DV_send (struct GNUNET_DV_Handle *dv_handle,
439                     const struct GNUNET_PeerIdentity *target,
440                     const char *msgbuf,
441                     size_t msgbuf_size,
442                     unsigned int priority,
443                     struct GNUNET_TIME_Relative timeout,
444                     const void *addr,
445                     size_t addrlen,
446                     GNUNET_TRANSPORT_TransmitContinuation
447                     cont, void *cont_cls)
448 {
449   struct GNUNET_DV_SendMessage *msg;
450   struct SendCallbackContext *send_ctx;
451   char *end_of_message;
452   GNUNET_HashCode uidhash;
453 #if DEBUG_DV
454   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);
455 #endif
456   dv_handle->uid_gen++;
457   msg = GNUNET_malloc(sizeof(struct GNUNET_DV_SendMessage) + addrlen + msgbuf_size);
458   msg->header.size = htons(sizeof(struct GNUNET_DV_SendMessage) + addrlen + msgbuf_size);
459   msg->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND);
460   memcpy(&msg->target, target, sizeof(struct GNUNET_PeerIdentity));
461   msg->msgbuf_size = htonl(msgbuf_size);
462   msg->priority = htonl(priority);
463   msg->timeout = timeout;
464   msg->addrlen = htonl(addrlen);
465   msg->uid = htonl(dv_handle->uid_gen);
466   memcpy(&msg[1], addr, addrlen);
467   end_of_message = (char *)&msg[1];
468   end_of_message = &end_of_message[addrlen];
469   memcpy(end_of_message, msgbuf, msgbuf_size);
470   add_pending(dv_handle, msg);
471
472   send_ctx = GNUNET_malloc(sizeof(struct SendCallbackContext));
473
474   send_ctx->cont = cont;
475   if (cont == NULL)
476     fprintf(stderr, "DV_SEND called with null continuation!\n");
477   send_ctx->cont_cls = cont_cls;
478   memcpy(&send_ctx->target, target, sizeof(struct GNUNET_PeerIdentity));
479
480   hash_from_uid(dv_handle->uid_gen, &uidhash);
481
482   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));
483   GNUNET_CONTAINER_multihashmap_put(dv_handle->send_callbacks, &uidhash, send_ctx, GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
484
485   return GNUNET_OK;
486 }
487
488 /* Forward declaration */
489 void GNUNET_DV_disconnect(struct GNUNET_DV_Handle *handle);
490
491 static size_t
492 transmit_start (void *cls, size_t size, void *buf)
493 {
494   struct StartContext *start_context = cls;
495   struct GNUNET_DV_Handle *handle = start_context->handle;
496   size_t tsize;
497
498   if (buf == NULL)
499     {
500       GNUNET_free(start_context->message);
501       GNUNET_free(start_context);
502       GNUNET_DV_disconnect(handle);
503       return 0;
504     }
505
506   tsize = ntohs(start_context->message->size);
507   if (size >= tsize)
508   {
509     memcpy(buf, start_context->message, tsize);
510     return tsize;
511   }
512
513   return 0;
514 }
515
516 /**
517  * Connect to the DV service
518  *
519  * @param sched the scheduler to use
520  * @param cfg the configuration to use
521  * @param receive_handler method call when on receipt from the service
522  * @param receive_handler_cls closure for receive_handler
523  *
524  * @return handle to the DV service
525  */
526 struct GNUNET_DV_Handle *
527 GNUNET_DV_connect (struct GNUNET_SCHEDULER_Handle *sched,
528                   const struct GNUNET_CONFIGURATION_Handle *cfg,
529                   GNUNET_DV_MessageReceivedHandler receive_handler,
530                   void *receive_handler_cls)
531 {
532   struct GNUNET_DV_Handle *handle;
533   struct GNUNET_MessageHeader *start_message;
534   struct StartContext *start_context;
535   handle = GNUNET_malloc(sizeof(struct GNUNET_DV_Handle));
536
537   handle->cfg = cfg;
538   handle->sched = sched;
539   handle->pending_list = NULL;
540   handle->current = NULL;
541   handle->do_destroy = GNUNET_NO;
542   handle->th = NULL;
543   handle->client = GNUNET_CLIENT_connect(sched, "dv", cfg);
544   handle->receive_handler = receive_handler;
545   handle->receive_cls = receive_handler_cls;
546
547   if (handle->client == NULL)
548     {
549       GNUNET_free(handle);
550       return NULL;
551     }
552
553   start_message = GNUNET_malloc(sizeof(struct GNUNET_MessageHeader));
554   start_message->size = htons(sizeof(struct GNUNET_MessageHeader));
555   start_message->type = htons(GNUNET_MESSAGE_TYPE_DV_START);
556
557   start_context = GNUNET_malloc(sizeof(struct StartContext));
558   start_context->handle = handle;
559   start_context->message = start_message;
560   GNUNET_CLIENT_notify_transmit_ready (handle->client,
561                                        sizeof(struct GNUNET_MessageHeader),
562                                        GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 60),
563                                        GNUNET_YES,
564                                        &transmit_start, start_context);
565
566   handle->send_callbacks = GNUNET_CONTAINER_multihashmap_create(100);
567
568   GNUNET_CLIENT_receive (handle->client,
569                          &handle_message_receipt,
570                          handle, GNUNET_TIME_UNIT_FOREVER_REL);
571
572   return handle;
573 }
574
575 /**
576  * Disconnect from the DV service
577  *
578  * @param handle the current handle to the service to disconnect
579  */
580 void GNUNET_DV_disconnect(struct GNUNET_DV_Handle *handle)
581 {
582   struct PendingMessages *pos;
583
584   GNUNET_assert(handle != NULL);
585
586   if (handle->th != NULL) /* We have a live transmit request in the Aether */
587     {
588       GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
589       handle->th = NULL;
590     }
591   if (handle->current != NULL) /* We are trying to send something now, clean it up */
592     GNUNET_free(handle->current);
593   while (NULL != (pos = handle->pending_list)) /* Remove all pending sends from the list */
594     {
595       handle->pending_list = pos->next;
596       GNUNET_free(pos);
597     }
598   if (handle->client != NULL) /* Finally, disconnect from the service */
599     {
600       GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
601       handle->client = NULL;
602     }
603
604   GNUNET_free (handle);
605 }
606
607 /* end of dv_api.c */