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