f1f9b17ab8086bc7658f6784fd379b41612b731d
[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;
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     }
382
383   return &tunnel->tunnel;
384 }
385
386 const struct GNUNET_PeerIdentity*
387 GNUNET_MESH_get_peer(const struct GNUNET_MESH_Tunnel* tunnel)
388 {
389   return &tunnel->peer;
390 }
391
392 static size_t
393 core_notify(void* cls, size_t size, void* buf)
394 {
395   struct GNUNET_MESH_Tunnel *tunnel = cls;
396   struct tunnel_message* message = buf;
397   void* cbuf = (void*)(message + 1);
398
399   size_t sent = tunnel->notify(tunnel->notify_cls, size - sizeof(struct tunnel_message), cbuf);
400
401   tunnel->notify = NULL;
402   tunnel->notify_cls = NULL;
403
404   sent += sizeof(struct tunnel_message);
405
406   message->hdr.type = htons(GNUNET_MESSAGE_TYPE_MESH);
407   message->hdr.size = htons(sent);
408   memcpy(&message->id, &tunnel->id, sizeof(struct tunnel_id));
409   return sent;
410 }
411
412 struct GNUNET_MESH_TransmitHandle *
413 GNUNET_MESH_notify_transmit_ready (struct
414                                    GNUNET_MESH_Tunnel
415                                    *tunnel,
416                                    int cork,
417                                    uint32_t priority,
418                                    struct
419                                    GNUNET_TIME_Relative
420                                    maxdelay,
421                                    size_t
422                                    notify_size,
423                                    GNUNET_CONNECTION_TransmitReadyNotify
424                                    notify, void *notify_cls)
425 {
426   tunnel->notify_cls = notify_cls;
427   tunnel->notify = notify;
428   GNUNET_CORE_notify_transmit_ready(tunnel->handle->core,
429                                     priority,
430                                     maxdelay,
431                                     &tunnel->peer,
432                                     notify_size + sizeof(struct tunnel_message),
433                                     core_notify,
434                                     (void*)tunnel);
435
436   /* aborting is not implemented yet */
437   return (struct GNUNET_MESH_TransmitHandle*)1;
438 }
439
440
441 struct GNUNET_MESH_Handle *
442 GNUNET_MESH_connect (const struct
443                      GNUNET_CONFIGURATION_Handle
444                      *cfg, void *cls,
445                      GNUNET_MESH_TunnelEndHandler
446                      cleaner,
447                      const struct GNUNET_MESH_MessageHandler *handlers)
448 {
449   struct GNUNET_MESH_Handle *ret =
450     GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
451
452   ret->connected_to_core = GNUNET_NO;
453   ret->connected_peers.head = NULL;
454   ret->connected_peers.tail = NULL;
455   ret->cleaner = cleaner;
456   ret->cls = cls;
457
458   const struct GNUNET_MESH_MessageHandler *it;
459   unsigned int len = 1;
460   for (it = handlers; it->callback != NULL; it++)
461     {
462       len++;
463     }
464
465   ret->handlers =
466     GNUNET_malloc (len * sizeof (struct GNUNET_MESH_MessageHandler));
467   memcpy (ret->handlers, handlers,
468           len * sizeof (struct GNUNET_MESH_MessageHandler));
469
470   const static struct GNUNET_CORE_MessageHandler core_handlers[] = {
471     {core_receive, GNUNET_MESSAGE_TYPE_MESH, 0},
472     {NULL, 0, 0}
473   };
474
475   ret->core = GNUNET_CORE_connect (cfg,
476                                    42,
477                                    ret,
478                                    core_startup,
479                                    core_connect,
480                                    core_disconnect,
481                                    NULL,
482                                    NULL,
483                                    GNUNET_NO, NULL, GNUNET_NO, core_handlers);
484   return ret;
485 }
486
487 void
488 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
489 {
490   GNUNET_free (handle->handlers);
491   GNUNET_CORE_disconnect (handle->core);
492
493   struct peer_list_element *element = handle->connected_peers.head;
494   while (element != NULL)
495     {
496       struct peer_list_element *next = element->next;
497       GNUNET_free (element);
498       element = next;
499     }
500
501   struct tunnel_list_element *tunnel = handle->pending_tunnels.head;
502   while (tunnel != NULL)
503     {
504       struct tunnel_list_element *next = tunnel->next;
505       GNUNET_free (tunnel);
506       tunnel = next;
507     }
508   tunnel = handle->established_tunnels.head;;
509   while (tunnel != NULL)
510     {
511       struct tunnel_list_element *next = tunnel->next;
512       GNUNET_free (tunnel);
513       tunnel = next;
514     }
515
516   GNUNET_free (handle);
517 }
518
519 /* end of core_api.c */