93e82f77f68570b41a301667e4e2f6e20048f00d
[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  * Store ready to send messages
43  */
44 struct PendingMessages
45 {
46   /**
47    * Linked list of pending messages
48    */
49   struct PendingMessages *next;
50
51   /**
52    * Message that is pending
53    */
54   struct GNUNET_DV_SendMessage *msg;
55
56   /**
57    * Timeout for this message
58    */
59   struct GNUNET_TIME_Absolute timeout;
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    * Handler for messages we receive from the DV service
100    */
101   GNUNET_DV_MessageReceivedHandler receive_handler;
102
103   /**
104    * Closure for the receive handler
105    */
106   void *receive_cls;
107
108   /**
109    * Current unique ID
110    */
111   uint32_t uid_gen;
112
113   /**
114    * Hashmap containing outstanding send requests awaiting confirmation.
115    */
116   struct GNUNET_CONTAINER_MultiHashMap *send_callbacks;
117
118 };
119
120
121 struct StartContext
122 {
123   /**
124    * Start message
125    */
126   struct GNUNET_MessageHeader *message;
127
128   /**
129    * Handle to service, in case of timeout
130    */
131   struct GNUNET_DV_Handle *handle;
132 };
133
134 struct SendCallbackContext
135 {
136   /**
137    * The continuation to call once a message is confirmed sent (or failed)
138    */
139   GNUNET_TRANSPORT_TransmitContinuation cont;
140
141   /**
142    * Closure to call with send continuation.
143    */
144   void *cont_cls;
145
146   /**
147    * Target of the message.
148    */
149   struct GNUNET_PeerIdentity target;
150 };
151
152 /**
153  * Convert unique ID to hash code.
154  *
155  * @param uid unique ID to convert
156  * @param hash set to uid (extended with zeros)
157  */
158 static void
159 hash_from_uid (uint32_t uid,
160                GNUNET_HashCode *hash)
161 {
162   memset (hash, 0, sizeof(GNUNET_HashCode));
163   *((uint32_t*)hash) = uid;
164 }
165
166 /**
167  * Try to (re)connect to the dv service.
168  *
169  * @param ret handle to the (disconnected) dv service
170  *
171  * @return GNUNET_YES on success, GNUNET_NO on failure.
172  */
173 static int
174 try_connect (struct GNUNET_DV_Handle *ret)
175 {
176   if (ret->client != NULL)
177     return GNUNET_OK;
178   ret->client = GNUNET_CLIENT_connect (ret->sched, "dv", ret->cfg);
179   if (ret->client != NULL)
180     return GNUNET_YES;
181 #if DEBUG_DV_MESSAGES
182   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
183               _("Failed to connect to the dv service!\n"));
184 #endif
185   return GNUNET_NO;
186 }
187
188 static void process_pending_message(struct GNUNET_DV_Handle *handle);
189
190 /**
191  * Send complete, schedule next
192  *
193  * @param handle handle to the dv service
194  * @param code return code for send (unused)
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->msg);
204   GNUNET_free (pos);
205 }
206
207 /**
208  * Notification that we can send data
209  *
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
213  *
214  * @return how many bytes we copied to buf
215  */
216 static size_t
217 transmit_pending (void *cls, size_t size, void *buf)
218 {
219   struct GNUNET_DV_Handle *handle = cls;
220   size_t ret;
221   size_t tsize;
222
223 #if DEBUG_DV
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));
226 #endif
227
228   if (buf == NULL)
229     {
230 #if DEBUG_DV
231       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV API: Transmit pending FAILED!\n\n\n");
232 #endif
233       finish(handle, GNUNET_SYSERR);
234       return 0;
235     }
236   handle->th = NULL;
237
238   ret = 0;
239
240   if (handle->current != NULL)
241   {
242     tsize = ntohs(handle->current->msg->header.size);
243     if (size >= tsize)
244     {
245       memcpy(buf, handle->current->msg, tsize);
246 #if DEBUG_DV
247       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV API: Copied %d bytes into buffer!\n\n\n", tsize);
248 #endif
249       finish(handle, GNUNET_OK);
250       return tsize;
251     }
252
253   }
254
255   return ret;
256 }
257
258 /**
259  * Try to send messages from list of messages to send
260  *
261  * @param handle handle to the distance vector service
262  */
263 static void process_pending_message(struct GNUNET_DV_Handle *handle)
264 {
265
266   if (handle->current != NULL)
267     return;                     /* action already pending */
268   if (GNUNET_YES != try_connect (handle))
269     {
270       finish (handle, GNUNET_SYSERR);
271       return;
272     }
273
274   /* schedule next action */
275   handle->current = handle->pending_list;
276   if (NULL == handle->current)
277     {
278       return;
279     }
280   handle->pending_list = handle->pending_list->next;
281   handle->current->next = NULL;
282
283   if (NULL ==
284       (handle->th = GNUNET_CLIENT_notify_transmit_ready (handle->client,
285                                                          ntohs(handle->current->msg->header.size),
286                                                          handle->current->msg->timeout,
287                                                          GNUNET_YES,
288                                                          &transmit_pending, handle)))
289     {
290 #if DEBUG_DV
291       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
292                   "Failed to transmit request to dv service.\n");
293 #endif
294       finish (handle, GNUNET_SYSERR);
295     }
296 }
297
298 /**
299  * Add a pending message to the linked list
300  *
301  * @param handle handle to the specified DV api
302  * @param msg the message to add to the list
303  */
304 static void add_pending(struct GNUNET_DV_Handle *handle, struct GNUNET_DV_SendMessage *msg)
305 {
306   struct PendingMessages *new_message;
307   struct PendingMessages *pos;
308   struct PendingMessages *last;
309
310   new_message = GNUNET_malloc(sizeof(struct PendingMessages));
311   new_message->msg = msg;
312
313   if (handle->pending_list != NULL)
314     {
315       pos = handle->pending_list;
316       while(pos != NULL)
317         {
318           last = pos;
319           pos = pos->next;
320         }
321       last->next = new_message;
322     }
323   else
324     {
325       handle->pending_list = new_message;
326     }
327
328   process_pending_message(handle);
329 }
330
331 /**
332  * Handles a message sent from the DV service to us.
333  * Parse it out and give it to the plugin.
334  *
335  * @param cls the handle to the DV API
336  * @param msg the message that was received
337  */
338 void handle_message_receipt (void *cls,
339                              const struct GNUNET_MessageHeader * msg)
340 {
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;
347   char *packed_msg;
348   char *packed_msg_start;
349   GNUNET_HashCode uidhash;
350   struct SendCallbackContext *send_ctx;
351
352   if (msg == NULL)
353   {
354 #if DEBUG_DV_MESSAGES
355     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV_API receive: connection closed\n");
356 #endif
357     return; /* Connection closed? */
358   }
359
360   GNUNET_assert((ntohs(msg->type) == GNUNET_MESSAGE_TYPE_TRANSPORT_DV_RECEIVE) || (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND_RESULT));
361
362   switch (ntohs(msg->type))
363   {
364   case GNUNET_MESSAGE_TYPE_TRANSPORT_DV_RECEIVE:
365     if (ntohs(msg->size) < sizeof(struct GNUNET_DV_MessageReceived))
366       return;
367
368     received_msg = (struct GNUNET_DV_MessageReceived *)msg;
369     packed_msg_len = ntohl(received_msg->msg_len);
370     sender_address_len = ntohl(received_msg->sender_address_len);
371
372     GNUNET_assert(ntohs(msg->size) == (sizeof(struct GNUNET_DV_MessageReceived) + packed_msg_len + sender_address_len));
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);
378
379 #if DEBUG_DV_MESSAGES
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));
383 #endif
384
385     handle->receive_handler(handle->receive_cls,
386                             &received_msg->sender,
387                             packed_msg,
388                             packed_msg_len,
389                             ntohl(received_msg->distance),
390                             sender_address,
391                             sender_address_len);
392
393     GNUNET_free(sender_address);
394     break;
395   case GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND_RESULT:
396     if (ntohs(msg->size) < sizeof(struct GNUNET_DV_SendResultMessage))
397       return;
398
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
403     if ((send_ctx != NULL) && (send_ctx->cont != NULL))
404       {
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  * @param cont continuation to call once the message has been sent (or failed)
437  * @param cont_cls closure for continuation
438  *
439  */
440 int GNUNET_DV_send (struct GNUNET_DV_Handle *dv_handle,
441                     const struct GNUNET_PeerIdentity *target,
442                     const char *msgbuf,
443                     size_t msgbuf_size,
444                     unsigned int priority,
445                     struct GNUNET_TIME_Relative timeout,
446                     const void *addr,
447                     size_t addrlen,
448                     GNUNET_TRANSPORT_TransmitContinuation
449                     cont, void *cont_cls)
450 {
451   struct GNUNET_DV_SendMessage *msg;
452   struct SendCallbackContext *send_ctx;
453   char *end_of_message;
454   GNUNET_HashCode uidhash;
455   int msize;
456 #if DEBUG_DV_MESSAGES
457   dv_handle->uid_gen = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, UINT32_MAX);
458 #else
459   dv_handle->uid_gen++;
460 #endif
461
462   msize = sizeof(struct GNUNET_DV_SendMessage) + addrlen + msgbuf_size;
463   msg = GNUNET_malloc(msize);
464   msg->header.size = htons(msize);
465   msg->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND);
466   memcpy(&msg->target, target, sizeof(struct GNUNET_PeerIdentity));
467   msg->priority = htonl(priority);
468   msg->timeout = timeout;
469   msg->addrlen = htonl(addrlen);
470   msg->uid = htonl(dv_handle->uid_gen);
471   memcpy(&msg[1], addr, addrlen);
472   end_of_message = (char *)&msg[1];
473   end_of_message = &end_of_message[addrlen];
474   memcpy(end_of_message, msgbuf, msgbuf_size);
475   add_pending(dv_handle, msg);
476   send_ctx = GNUNET_malloc(sizeof(struct SendCallbackContext));
477   send_ctx->cont = cont;
478   send_ctx->cont_cls = cont_cls;
479   memcpy(&send_ctx->target, target, sizeof(struct GNUNET_PeerIdentity));
480   hash_from_uid(dv_handle->uid_gen, &uidhash);
481   GNUNET_CONTAINER_multihashmap_put(dv_handle->send_callbacks, &uidhash, send_ctx, GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
482
483   return GNUNET_OK;
484 }
485
486 /**
487  * Callback to transmit a start message to
488  * the DV service, once we can send
489  *
490  * @param cls struct StartContext
491  * @param size how much can we send
492  * @param buf where to copy the message
493  *
494  * @return number of bytes copied to buf
495  */
496 static size_t
497 transmit_start (void *cls, size_t size, void *buf)
498 {
499   struct StartContext *start_context = cls;
500   struct GNUNET_DV_Handle *handle = start_context->handle;
501   size_t tsize;
502 #if DEBUG_DV
503   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV API: sending start request to service\n");
504 #endif
505   if (buf == NULL)
506     {
507       GNUNET_free(start_context->message);
508       GNUNET_free(start_context);
509       GNUNET_DV_disconnect(handle);
510       return 0;
511     }
512
513   tsize = ntohs(start_context->message->size);
514   if (size >= tsize)
515   {
516     memcpy(buf, start_context->message, tsize);
517     GNUNET_free(start_context->message);
518     GNUNET_free(start_context);
519     GNUNET_CLIENT_receive (handle->client,
520                            &handle_message_receipt,
521                            handle, GNUNET_TIME_UNIT_FOREVER_REL);
522
523
524     return tsize;
525   }
526
527   return 0;
528 }
529
530 /**
531  * Connect to the DV service
532  *
533  * @param sched the scheduler to use
534  * @param cfg the configuration to use
535  * @param receive_handler method call when on receipt from the service
536  * @param receive_handler_cls closure for receive_handler
537  *
538  * @return handle to the DV service
539  */
540 struct GNUNET_DV_Handle *
541 GNUNET_DV_connect (struct GNUNET_SCHEDULER_Handle *sched,
542                   const struct GNUNET_CONFIGURATION_Handle *cfg,
543                   GNUNET_DV_MessageReceivedHandler receive_handler,
544                   void *receive_handler_cls)
545 {
546   struct GNUNET_DV_Handle *handle;
547   struct GNUNET_MessageHeader *start_message;
548   struct StartContext *start_context;
549   handle = GNUNET_malloc(sizeof(struct GNUNET_DV_Handle));
550
551   handle->cfg = cfg;
552   handle->sched = sched;
553   handle->pending_list = NULL;
554   handle->current = NULL;
555   handle->th = NULL;
556   handle->client = GNUNET_CLIENT_connect(sched, "dv", cfg);
557   handle->receive_handler = receive_handler;
558   handle->receive_cls = receive_handler_cls;
559
560   if (handle->client == NULL)
561     {
562       GNUNET_free(handle);
563       return NULL;
564     }
565
566   start_message = GNUNET_malloc(sizeof(struct GNUNET_MessageHeader));
567   start_message->size = htons(sizeof(struct GNUNET_MessageHeader));
568   start_message->type = htons(GNUNET_MESSAGE_TYPE_DV_START);
569
570   start_context = GNUNET_malloc(sizeof(struct StartContext));
571   start_context->handle = handle;
572   start_context->message = start_message;
573   GNUNET_CLIENT_notify_transmit_ready (handle->client,
574                                        sizeof(struct GNUNET_MessageHeader),
575                                        GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 60),
576                                        GNUNET_YES,
577                                        &transmit_start, start_context);
578
579   handle->send_callbacks = GNUNET_CONTAINER_multihashmap_create(100);
580
581   return handle;
582 }
583
584 /**
585  * Disconnect from the DV service
586  *
587  * @param handle the current handle to the service to disconnect
588  */
589 void GNUNET_DV_disconnect(struct GNUNET_DV_Handle *handle)
590 {
591   struct PendingMessages *pos;
592
593   GNUNET_assert(handle != NULL);
594
595   if (handle->th != NULL) /* We have a live transmit request in the Aether */
596     {
597       GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
598       handle->th = NULL;
599     }
600   if (handle->current != NULL) /* We are trying to send something now, clean it up */
601     GNUNET_free(handle->current);
602   while (NULL != (pos = handle->pending_list)) /* Remove all pending sends from the list */
603     {
604       handle->pending_list = pos->next;
605       GNUNET_free(pos);
606     }
607   if (handle->client != NULL) /* Finally, disconnect from the service */
608     {
609       GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
610       handle->client = NULL;
611     }
612
613   GNUNET_free (handle);
614 }
615
616 /* end of dv_api.c */