fondly remembering ciphertext_send... all the pieces are there, but still doesn't...
[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 Not 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
40
41 struct PendingMessages
42 {
43   /**
44    * Linked list of pending messages
45    */
46   struct PendingMessages *next;
47
48   /**
49    * Message that is pending
50    */
51   struct GNUNET_DV_SendMessage *msg;
52
53   /**
54    * Timeout for this message
55    */
56   struct GNUNET_TIME_Absolute timeout;
57
58 };
59
60 /**
61  * Handle for the service.
62  */
63 struct GNUNET_DV_Handle
64 {
65   /**
66    * Our scheduler.
67    */
68   struct GNUNET_SCHEDULER_Handle *sched;
69
70   /**
71    * Configuration to use.
72    */
73   const struct GNUNET_CONFIGURATION_Handle *cfg;
74
75   /**
76    * Socket (if available).
77    */
78   struct GNUNET_CLIENT_Connection *client;
79
80   /**
81    * Currently pending transmission request.
82    */
83   struct GNUNET_CLIENT_TransmitHandle *th;
84
85   /**
86    * List of the currently pending messages for the DV service.
87    */
88   struct PendingMessages *pending_list;
89
90   /**
91    * Message we are currently sending.
92    */
93   struct PendingMessages *current;
94
95   /**
96    * Kill off the connection and any pending messages.
97    */
98   int do_destroy;
99
100   /**
101    * Handler for messages we receive from the DV service
102    */
103   GNUNET_DV_MessageReceivedHandler receive_handler;
104
105   /**
106    * Closure for the receive handler
107    */
108   void *receive_cls;
109
110 };
111
112
113 /**
114  * Try to (re)connect to the dv service.
115  *
116  * @return GNUNET_YES on success, GNUNET_NO on failure.
117  */
118 static int
119 try_connect (struct GNUNET_DV_Handle *ret)
120 {
121   if (ret->client != NULL)
122     return GNUNET_OK;
123   ret->client = GNUNET_CLIENT_connect (ret->sched, "dv", ret->cfg);
124   if (ret->client != NULL)
125     return GNUNET_YES;
126 #if DEBUG_STATISTICS
127   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
128               _("Failed to connect to the dv service!\n"));
129 #endif
130   return GNUNET_NO;
131 }
132
133 static void process_pending_message(struct GNUNET_DV_Handle *handle);
134
135 /**
136  * Send complete, schedule next
137  */
138 static void
139 finish (struct GNUNET_DV_Handle *handle, int code)
140 {
141   struct PendingMessages *pos = handle->current;
142   handle->current = NULL;
143   process_pending_message (handle);
144
145   GNUNET_free (pos);
146 }
147
148
149 static size_t
150 transmit_pending (void *cls, size_t size, void *buf)
151 {
152   struct GNUNET_DV_Handle *handle = cls;
153   size_t ret;
154   size_t tsize;
155
156   if (buf == NULL)
157     {
158       finish(handle, GNUNET_SYSERR);
159       return 0;
160     }
161   handle->th = NULL;
162
163   ret = 0;
164
165   if (handle->current != NULL)
166   {
167     tsize = ntohs(handle->current->msg->header.size);
168     if (size >= tsize)
169     {
170       memcpy(buf, handle->current->msg, tsize);
171     }
172     else
173     {
174       return ret;
175     }
176   }
177
178   return ret;
179 }
180
181 /**
182  * Try to send messages from list of messages to send
183  */
184 static void process_pending_message(struct GNUNET_DV_Handle *handle)
185 {
186   struct GNUNET_TIME_Relative timeout;
187
188   if (handle->current != NULL)
189     return;                     /* action already pending */
190   if (GNUNET_YES != try_connect (handle))
191     {
192       finish (handle, GNUNET_SYSERR);
193       return;
194     }
195
196   /* schedule next action */
197   handle->current = handle->pending_list;
198   if (NULL == handle->current)
199     {
200       if (handle->do_destroy)
201         {
202           handle->do_destroy = GNUNET_NO;
203           //GNUNET_DV_disconnect (handle); /* FIXME: replace with proper disconnect stuffs */
204         }
205       return;
206     }
207   handle->pending_list = handle->pending_list->next;
208   handle->current->next = NULL;
209
210   timeout = GNUNET_TIME_absolute_get_remaining (handle->current->timeout);
211   if (NULL ==
212       (handle->th = GNUNET_CLIENT_notify_transmit_ready (handle->client,
213                                                     ntohs(handle->current->msg->msgbuf_size),
214                                                     timeout,
215                                                     GNUNET_YES,
216                                                     &transmit_pending, handle)))
217     {
218 #if DEBUG_STATISTICS
219       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
220                   "Failed to transmit request to dv service.\n");
221 #endif
222       finish (handle, GNUNET_SYSERR);
223     }
224 }
225
226 /**
227  * Add a pending message to the linked list
228  *
229  * @param handle handle to the specified DV api
230  * @param msg the message to add to the list
231  */
232 static void add_pending(struct GNUNET_DV_Handle *handle, struct GNUNET_DV_SendMessage *msg)
233 {
234   struct PendingMessages *new_message;
235   struct PendingMessages *pos;
236   struct PendingMessages *last;
237
238   new_message = GNUNET_malloc(sizeof(struct PendingMessages));
239   new_message->msg = msg;
240
241   if (handle->pending_list != NULL)
242     {
243       pos = handle->pending_list;
244       while(pos != NULL)
245         {
246           last = pos;
247           pos = pos->next;
248         }
249       new_message->next = last->next; /* Should always be null */
250       last->next = new_message;
251     }
252   else
253     {
254       new_message->next = handle->pending_list; /* Will always be null */
255       handle->pending_list = new_message;
256     }
257
258   process_pending_message(handle);
259 }
260
261
262
263
264 void handle_message_receipt (void *cls,
265                              const struct GNUNET_MessageHeader * msg)
266 {
267   struct GNUNET_DV_Handle *handle = cls;
268   struct GNUNET_DV_MessageReceived *received_msg;
269   size_t packed_msg_len;
270   size_t sender_address_len;
271   char *sender_address;
272   char *packed_msg;
273
274   if (msg == NULL)
275   {
276     return; /* Connection closed? */
277   }
278
279   GNUNET_assert(ntohs(msg->type) == GNUNET_MESSAGE_TYPE_TRANSPORT_DV_RECEIVE);
280
281   if (ntohs(msg->size) < sizeof(struct GNUNET_DV_MessageReceived))
282     return;
283
284   received_msg = (struct GNUNET_DV_MessageReceived *)msg;
285   packed_msg_len = ntohs(received_msg->msg_len);
286   sender_address_len = ntohs(received_msg->sender_address_len);
287   GNUNET_assert(ntohs(msg->size) == (sizeof(struct GNUNET_DV_MessageReceived) + packed_msg_len + sender_address_len));
288
289   sender_address = GNUNET_malloc(sender_address_len);
290   memcpy(sender_address, &received_msg[1], sender_address_len);
291   packed_msg = GNUNET_malloc(packed_msg_len);
292   memcpy(packed_msg, &received_msg[1 + sender_address_len], packed_msg_len);
293
294   handle->receive_handler(handle->receive_cls,
295                           &received_msg->sender,
296                           packed_msg,
297                           packed_msg_len,
298                           ntohl(received_msg->distance),
299                           sender_address,
300                           sender_address_len);
301
302   GNUNET_free(sender_address);
303
304   GNUNET_CLIENT_receive (handle->client,
305                          &handle_message_receipt,
306                          handle, GNUNET_TIME_UNIT_FOREVER_REL);
307 }
308
309 /**
310  * Send a message from the plugin to the DV service indicating that
311  * a message should be sent via DV to some peer.
312  *
313  * @param dv_handle the handle to the DV api
314  * @param target the final target of the message
315  * @param msgbuf the msg(s) to send
316  * @param msgbuf_size the size of msgbuf
317  * @param priority priority to pass on to core when sending the message
318  * @param timeout how long can this message be delayed (pass through to core)
319  * @param addr the address of this peer (internally known to DV)
320  * @param addrlen the length of the peer address
321  *
322  */
323 int GNUNET_DV_send (struct GNUNET_DV_Handle *dv_handle,
324                     const struct GNUNET_PeerIdentity *target,
325                     const char *msgbuf,
326                     size_t msgbuf_size,
327                     unsigned int priority,
328                     struct GNUNET_TIME_Relative timeout,
329                     const void *addr,
330                     size_t addrlen)
331 {
332   struct GNUNET_DV_SendMessage *msg;
333
334   msg = GNUNET_malloc(sizeof(struct GNUNET_DV_SendMessage) + msgbuf_size + addrlen);
335   msg->header.size = htons(sizeof(struct GNUNET_DV_SendMessage) + msgbuf_size + addrlen);
336   msg->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND);
337   memcpy(&msg->target, target, sizeof(struct GNUNET_PeerIdentity));
338   msg->msgbuf = GNUNET_malloc(msgbuf_size);
339   memcpy(msg->msgbuf, msgbuf, msgbuf_size);
340   msg->msgbuf_size = htons(msgbuf_size);
341   msg->priority = htonl(priority);
342   msg->timeout = timeout;
343   msg->addrlen = htons(addrlen);
344   memcpy(&msg[1], addr, addrlen);
345
346   add_pending(dv_handle, msg);
347
348   return GNUNET_OK;
349 }
350
351 /**
352  * Connect to the DV service
353  *
354  * @param sched the scheduler to use
355  * @param cfg the configuration to use
356  * @param receive_handler method call when on receipt from the service
357  * @param receive_handler_cls closure for receive_handler
358  *
359  * @return handle to the DV service
360  */
361 struct GNUNET_DV_Handle *
362 GNUNET_DV_connect (struct GNUNET_SCHEDULER_Handle *sched,
363                   const struct GNUNET_CONFIGURATION_Handle *cfg,
364                   GNUNET_DV_MessageReceivedHandler receive_handler,
365                   void *receive_handler_cls)
366 {
367   struct GNUNET_DV_Handle *handle;
368
369   handle = GNUNET_malloc(sizeof(struct GNUNET_DV_Handle));
370
371   handle->cfg = cfg;
372   handle->sched = sched;
373   handle->pending_list = NULL;
374   handle->current = NULL;
375   handle->do_destroy = GNUNET_NO;
376   handle->th = NULL;
377   handle->client = GNUNET_CLIENT_connect(sched, "dv", cfg);
378   handle->receive_handler = receive_handler;
379   handle->receive_cls = receive_handler_cls;
380
381   if (handle->client == NULL)
382     return NULL;
383
384   GNUNET_CLIENT_receive (handle->client,
385                          &handle_message_receipt,
386                          handle, GNUNET_TIME_UNIT_FOREVER_REL);
387
388   return handle;
389 }
390
391 /**
392  * Disconnect from the DV service
393  *
394  * @param handle the current handle to the service to disconnect
395  */
396 void GNUNET_DV_disconnect(struct GNUNET_DV_Handle *handle)
397 {
398   struct PendingMessages *pos;
399
400   GNUNET_assert(handle != NULL);
401
402   if (handle->th != NULL) /* We have a live transmit request in the Aether */
403     {
404       GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
405       handle->th = NULL;
406     }
407   if (handle->current != NULL) /* We are trying to send something now, clean it up */
408     GNUNET_free(handle->current);
409   while (NULL != (pos = handle->pending_list)) /* Remove all pending sends from the list */
410     {
411       handle->pending_list = pos->next;
412       GNUNET_free(pos);
413     }
414   if (handle->client != NULL) /* Finally, disconnect from the service */
415     {
416       GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
417       handle->client = NULL;
418     }
419
420   GNUNET_free (handle);
421 }
422
423 /* end of dv_api.c */