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