changing heap remove node api to not pass heap; more fs hacking
[oweals/gnunet.git] / src / mesh / mesh_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 mesh/mesh_api.c
23  * @brief mesh service; API for the Mesh. This is used to talk to arbitrary peers
24  *        as of 2011-01Jan-06 this is a mockup.
25  * @author Philipp Tölke
26  */
27 #include <platform.h>
28 #include <gnunet_constants.h>
29 #include <gnunet_mesh_service.h>
30 #include <gnunet_core_service.h>
31 #include <gnunet_container_lib.h>
32
33 struct tunnel_id
34 {
35   uint32_t id GNUNET_PACKED;
36   struct GNUNET_PeerIdentity initiator;
37   struct GNUNET_PeerIdentity target;
38 };
39
40 static uint32_t current_id = 0;
41
42 struct tunnel_message
43 {
44   struct GNUNET_MessageHeader hdr;
45   struct tunnel_id id;
46   /* followed by another GNUNET_MessageHeader */
47 };
48
49 struct notify_cls
50 {
51   void* notify_cls;
52   GNUNET_CONNECTION_TransmitReadyNotify notify;
53   struct GNUNET_MESH_Tunnel *tunnel;
54 };
55
56 struct GNUNET_MESH_Tunnel
57 {
58   /* The other peer this tunnel leads to; just unicast for the moment! */
59   struct GNUNET_PeerIdentity peer;
60
61   struct tunnel_id id;
62
63   /* The handlers and cls for outbound tunnels. Are NULL for inbound tunnels. */
64   GNUNET_MESH_TunnelDisconnectHandler disconnect_handler;
65   GNUNET_MESH_TunnelConnectHandler connect_handler;
66   void *handler_cls;
67
68   struct GNUNET_MESH_Handle* handle;
69
70   /* The context of the receive-function. */
71   void *ctx;
72 };
73
74 struct tunnel_list_element
75 {
76   struct GNUNET_MESH_Tunnel tunnel;
77   struct tunnel_list_element *next, *prev;
78 };
79
80 struct tunnel_list
81 {
82   struct tunnel_list_element *head, *tail;
83 };
84
85 struct peer_list_element
86 {
87   struct GNUNET_PeerIdentity peer;
88   struct GNUNET_TRANSPORT_ATS_Information atsi;
89   struct peer_list_element *next, *prev;
90 };
91
92 struct peer_list
93 {
94   struct peer_list_element *head, *tail;
95 };
96
97 struct GNUNET_MESH_Handle
98 {
99   struct GNUNET_CORE_Handle *core;
100   struct GNUNET_MESH_MessageHandler *handlers;
101   struct GNUNET_PeerIdentity myself;
102   unsigned int connected_to_core;
103   struct peer_list connected_peers;
104   struct tunnel_list established_tunnels;
105   struct tunnel_list pending_tunnels;
106   void *cls;
107   GNUNET_MESH_TunnelEndHandler *cleaner;
108 };
109
110 static void
111 send_end_connect(void* cls,
112                      const struct GNUNET_SCHEDULER_TaskContext* tc)
113 {
114   struct GNUNET_MESH_Tunnel* tunnel = cls;
115
116   tunnel->connect_handler(tunnel->handler_cls, NULL, NULL);
117 }
118
119 static void
120 send_self_connect(void* cls,
121                         const struct GNUNET_SCHEDULER_TaskContext* tc)
122 {
123   struct GNUNET_MESH_Tunnel* tunnel = cls;
124
125   tunnel->connect_handler(tunnel->handler_cls, &tunnel->handle->myself, NULL);
126   GNUNET_SCHEDULER_add_now(send_end_connect, tunnel);
127 }
128
129 static void
130 core_startup (void *cls,
131               struct GNUNET_CORE_Handle *core,
132               const struct GNUNET_PeerIdentity *my_identity,
133               const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
134 {
135   struct GNUNET_MESH_Handle *handle = cls;
136   memcpy (&handle->myself, my_identity, sizeof (struct GNUNET_PeerIdentity));
137   handle->connected_to_core = GNUNET_YES;
138 }
139
140 /**
141  * Core calls this if we are connected to a new peer.
142  *
143  * If core tells us that we are connected to ourself, we ignore it. Otherwise, the
144  * peer is added to the connected_peers-list.
145  *
146  */
147 static void
148 core_connect (void *cls,
149               const struct GNUNET_PeerIdentity *peer,
150               const struct GNUNET_TRANSPORT_ATS_Information *atsi)
151 {
152   struct GNUNET_MESH_Handle *handle = cls;
153   /* Check for connect-to-self-message, which we ignore */
154   if (0 ==
155       memcmp (peer, &handle->myself, sizeof (struct GNUNET_PeerIdentity)))
156     return;
157
158
159   /* put the new peer into the list of connected peers */
160   struct peer_list_element *element =
161     GNUNET_malloc (sizeof (struct peer_list_element));
162   memcpy (&element->peer, peer, sizeof (struct GNUNET_PeerIdentity));
163   memcpy (&element->atsi, atsi,
164           sizeof (struct GNUNET_TRANSPORT_ATS_Information));
165
166   GNUNET_CONTAINER_DLL_insert_after (handle->connected_peers.head,
167                                      handle->connected_peers.tail,
168                                      handle->connected_peers.tail, element);
169
170   struct tunnel_list_element *tunnel = handle->pending_tunnels.head;
171   while (tunnel != NULL)
172     {
173       if (0 ==
174           memcmp (&tunnel->tunnel.peer, peer,
175                   sizeof (struct GNUNET_PeerIdentity)))
176         {
177           struct tunnel_list_element *next = tunnel->next;
178           GNUNET_CONTAINER_DLL_remove (handle->pending_tunnels.head,
179                                        handle->pending_tunnels.tail, tunnel);
180           GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
181                                              handle->established_tunnels.tail,
182                                              handle->established_tunnels.tail,
183                                              tunnel);
184           tunnel->tunnel.connect_handler (tunnel->tunnel.handler_cls,
185                                           peer, atsi);
186           GNUNET_SCHEDULER_add_now(send_end_connect, tunnel);
187           tunnel = next;
188         }
189       else
190         tunnel = tunnel->next;
191     }
192 }
193
194 /**
195  * Core calls this if we disconnect a peer
196  *
197  * Remove this peer from the list of connected peers
198  * Close all tunnels this peer belongs to
199  */
200 static void
201 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
202 {
203   struct GNUNET_MESH_Handle *handle = cls;
204
205   struct peer_list_element *element = handle->connected_peers.head;
206   while (element != NULL)
207     {
208       if (0 ==
209           memcmp (&element->peer, peer, sizeof (struct GNUNET_PeerIdentity)))
210         break;
211       element = element->next;
212     }
213   if (element != NULL)
214     {
215       GNUNET_CONTAINER_DLL_remove (handle->connected_peers.head,
216                                    handle->connected_peers.tail, element);
217       GNUNET_free (element);
218     }
219
220   struct tunnel_list_element *telement = handle->established_tunnels.head;
221   while (telement != NULL)
222     {
223       if (0 ==
224           memcmp (&telement->tunnel.peer, peer,
225                   sizeof (struct GNUNET_PeerIdentity)))
226         {
227           /* disconnect tunnels */
228           /* outbound tunnels */
229           if (telement->tunnel.connect_handler != NULL && NULL != telement->tunnel.disconnect_handler)
230             telement->tunnel.disconnect_handler (telement->tunnel.handler_cls,
231                                                  peer);
232           /* inbound tunnels */
233           else if (NULL != handle->cleaner)
234             handle->cleaner (handle->cls, &telement->tunnel,
235                              &telement->tunnel.ctx);
236
237           struct tunnel_list_element *next = telement->next;
238           GNUNET_CONTAINER_DLL_remove (handle->established_tunnels.head,
239                                        handle->established_tunnels.tail,
240                                        telement);
241           GNUNET_free (telement);
242           telement = next;
243         }
244       else
245         {
246           telement = telement->next;
247         }
248     }
249 }
250
251 /**
252  * Receive a message from core.
253  */
254 static int
255 core_receive (void *cls,
256               const struct GNUNET_PeerIdentity *other,
257               const struct GNUNET_MessageHeader *message,
258               const struct GNUNET_TRANSPORT_ATS_Information *atsi)
259 {
260   struct GNUNET_MESH_Handle *handle = cls;
261   struct tunnel_message *tmessage = (struct tunnel_message *) message;
262   struct GNUNET_MessageHeader *rmessage =
263     (struct GNUNET_MessageHeader *) (tmessage + 1);
264
265   struct GNUNET_MESH_MessageHandler *handler;
266
267   for (handler = handle->handlers; handler->callback != NULL; handler++)
268     {
269       if ( (ntohs (rmessage->type) == handler->type)
270            && ( (handler->expected_size == 0)
271                 || (handler->expected_size == ntohs (rmessage->size))) )
272         {
273           break;
274         }
275     }
276
277   /* handler->callback handles this message */
278
279   /* If no handler was found, drop the message but keep the channel open */
280   if (handler->callback == NULL)
281     return GNUNET_OK;
282
283   struct tunnel_list_element *tunnel = handle->established_tunnels.head;
284
285   while (tunnel != NULL)
286     {
287       if (tunnel->tunnel.id.id == tmessage->id.id &&
288           (0 ==
289            memcmp (&tmessage->id.initiator, &tunnel->tunnel.id.initiator,
290                    sizeof (struct GNUNET_PeerIdentity)))
291           && (0 ==
292               memcmp (&tmessage->id.target, &tunnel->tunnel.id.target,
293                       sizeof (struct GNUNET_PeerIdentity))))
294         break;
295       tunnel = tunnel->next;
296     }
297
298   /* if no tunnel was found: create a new inbound tunnel */
299   if (tunnel == NULL)
300     {
301       tunnel = GNUNET_malloc (sizeof (struct tunnel_list_element));
302       tunnel->tunnel.connect_handler = NULL;
303       tunnel->tunnel.disconnect_handler = NULL;
304       tunnel->tunnel.handler_cls = NULL;
305       tunnel->tunnel.ctx = NULL;
306       tunnel->tunnel.handle = handle;
307       memcpy (&tunnel->tunnel.peer, other,
308               sizeof (struct GNUNET_PeerIdentity));
309       memcpy (&tunnel->tunnel.id, &tmessage->id, sizeof (struct tunnel_id));
310
311       GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
312                                          handle->established_tunnels.tail,
313                                          handle->established_tunnels.tail,
314                                          tunnel);
315     }
316
317   return handler->callback (handle->cls, &tunnel->tunnel,
318                             &tunnel->tunnel.ctx, other, rmessage, atsi);
319 }
320
321
322 struct GNUNET_MESH_Tunnel *
323 GNUNET_MESH_peer_request_connect_all (struct GNUNET_MESH_Handle *handle,
324                                       struct GNUNET_TIME_Relative timeout,
325                                       unsigned int num_peers,
326                                       const struct GNUNET_PeerIdentity *peers,
327                                       GNUNET_MESH_TunnelConnectHandler
328                                       connect_handler,
329                                       GNUNET_MESH_TunnelDisconnectHandler
330                                       disconnect_handler, void *handler_cls)
331 {
332   if (num_peers != 1)
333     return NULL;
334
335   struct tunnel_list_element *tunnel =
336     GNUNET_malloc (sizeof (struct tunnel_list_element));
337
338   tunnel->tunnel.connect_handler = connect_handler;
339   tunnel->tunnel.disconnect_handler = disconnect_handler;
340   tunnel->tunnel.handler_cls = handler_cls;
341   tunnel->tunnel.ctx = NULL;
342   tunnel->tunnel.handle = handle;
343   memcpy (&tunnel->tunnel.id.initiator, &handle->myself,
344           sizeof (struct GNUNET_PeerIdentity));
345   memcpy (&tunnel->tunnel.id.target, peers,
346           sizeof (struct GNUNET_PeerIdentity));
347   tunnel->tunnel.id.id = current_id++;
348   memcpy (&tunnel->tunnel.peer, peers, sizeof(struct GNUNET_PeerIdentity));
349
350   struct peer_list_element *element = handle->connected_peers.head;
351   while (element != NULL)
352     {
353       if (0 ==
354           memcmp (&element->peer, peers, sizeof (struct GNUNET_PeerIdentity)))
355         break;
356       element = element->next;
357     }
358
359   if (element != NULL)
360     {
361       /* we are connected to this peer */
362       GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
363                                          handle->established_tunnels.tail,
364                                          handle->established_tunnels.tail,
365                                          tunnel);
366       connect_handler (handler_cls, &element->peer, &element->atsi);
367     }
368   else if (0 ==
369            memcmp (peers, &handle->myself,
370                    sizeof (struct GNUNET_PeerIdentity)))
371     {
372       /* we are the peer */
373       GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
374                                          handle->established_tunnels.tail,
375                                          handle->established_tunnels.tail,
376                                          tunnel);
377       GNUNET_SCHEDULER_add_now(send_self_connect, tunnel);
378     }
379   else
380     {
381       /* we are not connected to this peer */
382       GNUNET_CONTAINER_DLL_insert_after (handle->pending_tunnels.head,
383                                          handle->pending_tunnels.tail,
384                                          handle->pending_tunnels.tail,
385                                          tunnel);
386       (void) GNUNET_CORE_peer_request_connect (handle->core,
387                                                timeout,
388                                                peers,
389                                                NULL, NULL);                                     
390     }
391
392   return &tunnel->tunnel;
393 }
394
395 const struct GNUNET_PeerIdentity*
396 GNUNET_MESH_get_peer(const struct GNUNET_MESH_Tunnel* tunnel)
397 {
398   return &tunnel->peer;
399 }
400
401 static size_t
402 core_notify(void* cls, size_t size, void* buf)
403 {
404   struct notify_cls *ncls = cls;
405   struct GNUNET_MESH_Tunnel *tunnel = ncls->tunnel;
406   struct tunnel_message* message = buf;
407   void* cbuf = (void*) &message[1];
408   GNUNET_assert(NULL != ncls->notify);
409
410   size_t sent = ncls->notify(ncls->notify_cls, size - sizeof(struct tunnel_message), cbuf);
411
412   GNUNET_free(ncls);
413
414   if (0 == sent) return 0;
415
416   sent += sizeof(struct tunnel_message);
417
418   message->hdr.type = htons(GNUNET_MESSAGE_TYPE_MESH);
419   message->hdr.size = htons(sent);
420   memcpy(&message->id, &tunnel->id, sizeof(struct tunnel_id));
421   return sent;
422 }
423
424 struct GNUNET_MESH_TransmitHandle *
425 GNUNET_MESH_notify_transmit_ready (struct
426                                    GNUNET_MESH_Tunnel
427                                    *tunnel,
428                                    int cork,
429                                    uint32_t priority,
430                                    struct
431                                    GNUNET_TIME_Relative
432                                    maxdelay,
433                                    size_t
434                                    notify_size,
435                                    GNUNET_CONNECTION_TransmitReadyNotify
436                                    notify, void *notify_cls)
437 {
438   struct notify_cls *cls = GNUNET_malloc(sizeof(struct notify_cls));
439   cls->notify_cls = notify_cls;
440   GNUNET_assert(NULL != notify);
441   cls->notify = notify;
442   cls->tunnel = tunnel;
443   GNUNET_CORE_notify_transmit_ready(tunnel->handle->core,
444                                     cork,
445                                     priority,
446                                     maxdelay,
447                                     &tunnel->peer,
448                                     notify_size + sizeof(struct tunnel_message),
449                                     &core_notify,
450                                     (void*)cls);
451
452   /* aborting is not implemented yet */
453   return (struct GNUNET_MESH_TransmitHandle*) 1;
454 }
455
456
457 struct GNUNET_MESH_Handle *
458 GNUNET_MESH_connect (const struct
459                      GNUNET_CONFIGURATION_Handle
460                      *cfg, void *cls,
461                      GNUNET_MESH_TunnelEndHandler
462                      cleaner,
463                      const struct GNUNET_MESH_MessageHandler *handlers)
464 {
465   struct GNUNET_MESH_Handle *ret =
466     GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
467
468   ret->connected_to_core = GNUNET_NO;
469   ret->connected_peers.head = NULL;
470   ret->connected_peers.tail = NULL;
471   ret->cleaner = cleaner;
472   ret->cls = cls;
473
474   const struct GNUNET_MESH_MessageHandler *it;
475   unsigned int len = 1;
476   for (it = handlers; it->callback != NULL; it++)
477     {
478       len++;
479     }
480
481   ret->handlers =
482     GNUNET_malloc (len * sizeof (struct GNUNET_MESH_MessageHandler));
483   memset(ret->handlers, 0, len * sizeof(struct GNUNET_MESH_MessageHandler));
484   memcpy (ret->handlers, handlers,
485           len * sizeof (struct GNUNET_MESH_MessageHandler));
486
487   const static struct GNUNET_CORE_MessageHandler core_handlers[] = {
488     {&core_receive, GNUNET_MESSAGE_TYPE_MESH, 0},
489     {NULL, 0, 0}
490   };
491
492   ret->core = GNUNET_CORE_connect (cfg,
493                                    42,
494                                    ret,
495                                    &core_startup,
496                                    &core_connect,
497                                    &core_disconnect,
498                                    NULL,
499                                    NULL,
500                                    GNUNET_NO, NULL, GNUNET_NO, core_handlers);
501   return ret;
502 }
503
504 void
505 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
506 {
507   GNUNET_free (handle->handlers);
508   GNUNET_CORE_disconnect (handle->core);
509
510   struct peer_list_element *element = handle->connected_peers.head;
511   while (element != NULL)
512     {
513       struct peer_list_element *next = element->next;
514       GNUNET_free (element);
515       element = next;
516     }
517
518   struct tunnel_list_element *tunnel = handle->pending_tunnels.head;
519   while (tunnel != NULL)
520     {
521       struct tunnel_list_element *next = tunnel->next;
522       GNUNET_free (tunnel);
523       tunnel = next;
524     }
525   tunnel = handle->established_tunnels.head;;
526   while (tunnel != NULL)
527     {
528       struct tunnel_list_element *next = tunnel->next;
529       GNUNET_free (tunnel);
530       tunnel = next;
531     }
532
533   GNUNET_free (handle);
534 }
535
536 /* end of mesh_api.c */