Bug: fill in the peer-field of the tunnel
[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 #include <gnunet_applications.h>
33
34 struct tunnel_id
35 {
36   uint32_t id GNUNET_PACKED;
37   struct GNUNET_PeerIdentity initiator;
38   struct GNUNET_PeerIdentity target;
39 };
40
41 static uint32_t current_id = 0;
42
43 struct tunnel_message
44 {
45   struct GNUNET_MessageHeader hdr;
46   struct tunnel_id id;
47   /* followed by another GNUNET_MessageHeader */
48 };
49
50 struct notify_cls
51 {
52   void* notify_cls;
53   GNUNET_CONNECTION_TransmitReadyNotify notify;
54   struct GNUNET_MESH_Tunnel *tunnel;
55 };
56
57 struct GNUNET_MESH_Tunnel
58 {
59   /* The other peer this tunnel leads to; just unicast for the moment! */
60   struct GNUNET_PeerIdentity peer;
61
62   struct tunnel_id id;
63
64   /* The handlers and cls for outbound tunnels. Are NULL for inbound tunnels. */
65   GNUNET_MESH_TunnelDisconnectHandler disconnect_handler;
66   GNUNET_MESH_TunnelConnectHandler connect_handler;
67   void *handler_cls;
68
69   struct GNUNET_MESH_Handle* handle;
70
71   /* The application-type requested for this tunnel. Is only needed for pending
72    * by_tupe-tunnels
73    */
74   uint16_t application_type;
75
76   struct GNUNET_MESH_TransmitHandle* notify_handle;
77
78   /* The context of the receive-function. */
79   void *ctx;
80
81   /* A list, usable by application-code (for queues) */
82   void* app_head;
83   void* app_tail;
84
85   /* A pointer, usable by application-code */
86   void* app_data;
87 };
88
89 struct tunnel_list_element
90 {
91   struct GNUNET_MESH_Tunnel tunnel;
92   struct tunnel_list_element *next, *prev;
93 };
94
95 struct tunnel_list
96 {
97   struct tunnel_list_element *head, *tail;
98 };
99
100 struct type_list_element
101 {
102   GNUNET_MESH_ApplicationType type;
103   struct type_list_element *next, *prev;
104 };
105
106 struct peer_list_element
107 {
108   struct GNUNET_PeerIdentity peer;
109
110   /* list of application-types */
111   struct type_list_element *type_head, *type_tail;
112
113   struct GNUNET_TRANSPORT_ATS_Information atsi;
114   struct peer_list_element *next, *prev;
115 };
116
117 struct peer_list
118 {
119   struct peer_list_element *head, *tail;
120 };
121
122 struct GNUNET_MESH_Handle
123 {
124   struct GNUNET_CORE_Handle *core;
125   struct GNUNET_MESH_MessageHandler *handlers;
126   struct GNUNET_PeerIdentity myself;
127   unsigned int connected_to_core;
128   struct peer_list connected_peers;
129   struct tunnel_list established_tunnels;
130   struct tunnel_list pending_tunnels;
131   struct tunnel_list pending_by_type_tunnels;
132   void *cls;
133   GNUNET_MESH_TunnelEndHandler *cleaner;
134   size_t hello_message_size;
135   uint16_t *hello_message;
136 };
137
138 static void
139 send_end_connect(void* cls,
140                      const struct GNUNET_SCHEDULER_TaskContext* tc)
141 {
142   if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
143     return;
144
145   struct GNUNET_MESH_Tunnel* tunnel = cls;
146
147   tunnel->connect_handler(tunnel->handler_cls, NULL, NULL);
148 }
149
150 static void
151 send_self_connect(void* cls,
152                         const struct GNUNET_SCHEDULER_TaskContext* tc)
153 {
154   if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
155       return;
156
157   struct GNUNET_MESH_Tunnel* tunnel = cls;
158
159   tunnel->connect_handler(tunnel->handler_cls, &tunnel->handle->myself, NULL);
160   GNUNET_SCHEDULER_add_now(send_end_connect, tunnel);
161 }
162
163 static void
164 call_connect_handler (void *cls,
165                       const struct GNUNET_SCHEDULER_TaskContext *tc)
166 {
167   if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
168       return;
169
170   struct GNUNET_MESH_Tunnel *tunnel = cls;
171
172   tunnel->connect_handler (tunnel->handler_cls, &tunnel->peer,
173                            NULL);
174   GNUNET_SCHEDULER_add_now (send_end_connect, tunnel);
175 }
176
177 static void
178 core_startup (void *cls,
179               struct GNUNET_CORE_Handle *core __attribute__((unused)),
180               const struct GNUNET_PeerIdentity *my_identity,
181               const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey __attribute__((unused)))
182 {
183   struct GNUNET_MESH_Handle *handle = cls;
184   memcpy (&handle->myself, my_identity, sizeof (struct GNUNET_PeerIdentity));
185   handle->connected_to_core = GNUNET_YES;
186 }
187
188 static size_t
189 send_hello_message (void *cls, size_t size, void *buf)
190 {
191   if (cls == NULL) return 0;
192
193   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending hello\n");
194
195   struct GNUNET_MESH_Handle *handle = cls;
196   struct GNUNET_MessageHeader *hdr = buf;
197
198   size_t sent = sizeof(struct GNUNET_MessageHeader) + handle->hello_message_size;
199
200   if (sent > size) return 0;
201
202   hdr->type = htons(GNUNET_MESSAGE_TYPE_MESH_HELLO);
203   hdr->size = htons(size);
204
205   memcpy(hdr+1, handle->hello_message, handle->hello_message_size);
206   return sent;
207 }
208
209
210 /**
211  * Core calls this if we are connected to a new peer.
212  *
213  * The peer is added to the connected_peers-list.
214  *
215  */
216 static void
217 core_connect (void *cls,
218               const struct GNUNET_PeerIdentity *peer,
219               const struct GNUNET_TRANSPORT_ATS_Information *atsi)
220 {
221   struct GNUNET_MESH_Handle *handle = cls;
222
223   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Core tells us we are connected to peer %s\n", GNUNET_i2s(peer));
224
225   /* Send a hello to this peer */
226   GNUNET_CORE_notify_transmit_ready(handle->core,
227                                     GNUNET_NO,
228                                     42,
229                                     GNUNET_TIME_UNIT_SECONDS,
230                                     peer,
231                                     sizeof(struct GNUNET_MessageHeader) + handle->hello_message_size,
232                                     &send_hello_message,
233                                     cls);
234
235   /* put the new peer into the list of connected peers */
236   struct peer_list_element *element =
237     GNUNET_malloc (sizeof (struct peer_list_element));
238   memcpy (&element->peer, peer, sizeof (struct GNUNET_PeerIdentity));
239
240   if (NULL != atsi)
241     memcpy (&element->atsi, atsi,
242             sizeof (struct GNUNET_TRANSPORT_ATS_Information));
243
244   GNUNET_CONTAINER_DLL_insert_after (handle->connected_peers.head,
245                                      handle->connected_peers.tail,
246                                      handle->connected_peers.tail, element);
247
248   struct tunnel_list_element *tunnel = handle->pending_tunnels.head;
249   while (tunnel != NULL)
250     {
251       if (0 ==
252           memcmp (&tunnel->tunnel.peer, peer,
253                   sizeof (struct GNUNET_PeerIdentity)))
254         {
255           struct tunnel_list_element *next = tunnel->next;
256           GNUNET_CONTAINER_DLL_remove (handle->pending_tunnels.head,
257                                        handle->pending_tunnels.tail, tunnel);
258           GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
259                                              handle->established_tunnels.tail,
260                                              handle->established_tunnels.tail,
261                                              tunnel);
262           tunnel->tunnel.connect_handler (tunnel->tunnel.handler_cls,
263                                           peer, atsi);
264           GNUNET_SCHEDULER_add_now(send_end_connect, tunnel);
265           tunnel = next;
266         }
267       else
268         tunnel = tunnel->next;
269     }
270 }
271
272 /**
273  * Core calls this if we disconnect a peer
274  *
275  * Remove this peer from the list of connected peers
276  * Close all tunnels this peer belongs to
277  */
278 static void
279 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
280 {
281   struct GNUNET_MESH_Handle *handle = cls;
282
283   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Core tells us we are no longer connected to peer %s\n", GNUNET_i2s(peer));
284
285   struct peer_list_element *element = handle->connected_peers.head;
286   while (element != NULL)
287     {
288       if (0 ==
289           memcmp (&element->peer, peer, sizeof (struct GNUNET_PeerIdentity)))
290         break;
291       element = element->next;
292     }
293   if (element != NULL)
294     {
295       GNUNET_CONTAINER_DLL_remove (handle->connected_peers.head,
296                                    handle->connected_peers.tail, element);
297       while (element->type_head != NULL)
298         {
299           struct type_list_element* tail = element->type_tail;
300           GNUNET_CONTAINER_DLL_remove(element->type_head, element->type_tail, tail);
301           GNUNET_free(tail);
302         }
303       GNUNET_free (element);
304     }
305
306   struct tunnel_list_element *telement = handle->established_tunnels.head;
307   while (telement != NULL)
308     {
309       if (0 ==
310           memcmp (&telement->tunnel.peer, peer,
311                   sizeof (struct GNUNET_PeerIdentity)))
312         {
313           /* disconnect tunnels */
314           /* outbound tunnels */
315           if (telement->tunnel.connect_handler != NULL && NULL != telement->tunnel.disconnect_handler)
316             telement->tunnel.disconnect_handler (telement->tunnel.handler_cls,
317                                                  peer);
318           /* inbound tunnels */
319           else if (NULL != handle->cleaner)
320             handle->cleaner (handle->cls, &telement->tunnel,
321                              &telement->tunnel.ctx);
322
323           struct tunnel_list_element *next = telement->next;
324           GNUNET_CONTAINER_DLL_remove (handle->established_tunnels.head,
325                                        handle->established_tunnels.tail,
326                                        telement);
327           GNUNET_free (telement);
328           telement = next;
329         }
330       else
331         {
332           telement = telement->next;
333         }
334     }
335 }
336
337 /**
338  * Receive a message from core.
339  * This is a hello-message, containing the application-types the other peer can receive
340  */
341 static int
342 receive_hello (void *cls,
343                const struct GNUNET_PeerIdentity *other,
344                const struct GNUNET_MessageHeader *message,
345                const struct GNUNET_TRANSPORT_ATS_Information *atsi)
346 {
347   struct GNUNET_MESH_Handle *handle = cls;
348   uint16_t *num = (uint16_t *) (message + 1);
349   GNUNET_MESH_ApplicationType *ports = (GNUNET_MESH_ApplicationType*) (num + 1);
350   unsigned int i;
351
352   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "The peer %s tells us he supports %d application-types.\n", GNUNET_i2s(other), ntohs(*num));
353
354   struct peer_list_element *element = handle->connected_peers.head;
355   while (element != NULL)
356     {
357       if (0 ==
358           memcmp (&element->peer, other, sizeof (struct GNUNET_PeerIdentity)))
359         break;
360       element = element->next;
361     }
362
363   GNUNET_assert(NULL != element);
364
365   for (i = 0; i < ntohs(*num); i++)
366     {
367       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "The peer %s newly supports the application-type %d\n", GNUNET_i2s(other), ntohs(ports[i]));
368       if (GNUNET_APPLICATION_TYPE_END == ntohs(ports[i])) continue;
369       struct type_list_element* new_type = GNUNET_malloc(sizeof *new_type);
370       new_type->type = (GNUNET_MESH_ApplicationType)ntohs (ports[i]);
371       GNUNET_CONTAINER_DLL_insert(element->type_head, element->type_tail, new_type);
372     }
373
374   struct type_list_element *type;
375   for (type = element->type_head; type != NULL; type = type->next)
376     {
377       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "The peer %s supports the application-type %d\n", GNUNET_i2s(other), type->type);
378     }
379
380   struct tunnel_list_element *tunnel = handle->pending_by_type_tunnels.head;
381   while (tunnel != NULL)
382     {
383       struct tunnel_list_element *next = tunnel->next;
384       for (i = 0; i < ntohs(*num); i++)
385         {
386           if (ntohs (ports[i]) == tunnel->tunnel.application_type)
387             {
388               GNUNET_CONTAINER_DLL_remove (handle->pending_by_type_tunnels.head,
389                                            handle->pending_by_type_tunnels.tail,
390                                            tunnel);
391               GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.
392                                                  head,
393                                                  handle->established_tunnels.
394                                                  tail,
395                                                  handle->established_tunnels.
396                                                  tail, tunnel);
397               memcpy(&tunnel->tunnel.peer, other, sizeof(struct GNUNET_PeerIdentity));
398               tunnel->tunnel.connect_handler (tunnel->tunnel.handler_cls,
399                                               &tunnel->tunnel.peer, atsi);
400               GNUNET_SCHEDULER_add_now (send_end_connect, tunnel);
401               break;
402             }
403         }
404       if (ntohs (ports[i]) == tunnel->tunnel.application_type)
405         tunnel = next;
406       else
407         tunnel = tunnel->next;
408     }
409   return GNUNET_OK;
410 }
411
412 /**
413  * Receive a message from core.
414  */
415 static int
416 core_receive (void *cls,
417               const struct GNUNET_PeerIdentity *other,
418               const struct GNUNET_MessageHeader *message,
419               const struct GNUNET_TRANSPORT_ATS_Information *atsi)
420 {
421   struct GNUNET_MESH_Handle *handle = cls;
422   struct tunnel_message *tmessage = (struct tunnel_message *) message;
423   struct GNUNET_MessageHeader *rmessage =
424     (struct GNUNET_MessageHeader *) (tmessage + 1);
425
426   struct GNUNET_MESH_MessageHandler *handler;
427
428   for (handler = handle->handlers; handler->callback != NULL; handler++)
429     {
430       if ( (ntohs (rmessage->type) == handler->type)
431            && ( (handler->expected_size == 0)
432                 || (handler->expected_size == ntohs (rmessage->size))) )
433         {
434           break;
435         }
436     }
437
438   /* handler->callback handles this message */
439
440   /* If no handler was found, drop the message but keep the channel open */
441   if (handler->callback == NULL)
442     {
443       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received message of type %d from peer %s; dropping it.\n",
444                  ntohs(rmessage->type), GNUNET_i2s(other));
445       return GNUNET_OK;
446     }
447
448   struct tunnel_list_element *tunnel = handle->established_tunnels.head;
449
450   while (tunnel != NULL)
451     {
452       if (tunnel->tunnel.id.id == tmessage->id.id &&
453           (0 ==
454            memcmp (&tmessage->id.initiator, &tunnel->tunnel.id.initiator,
455                    sizeof (struct GNUNET_PeerIdentity)))
456           && (0 ==
457               memcmp (&tmessage->id.target, &tunnel->tunnel.id.target,
458                       sizeof (struct GNUNET_PeerIdentity))))
459         break;
460       tunnel = tunnel->next;
461     }
462
463   /* if no tunnel was found: create a new inbound tunnel */
464   if (tunnel == NULL)
465     {
466       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "New inbound tunnel from peer %s; first message has type %d.\n",
467                  GNUNET_i2s(other), ntohs(rmessage->type));
468       tunnel = GNUNET_malloc (sizeof (struct tunnel_list_element));
469       tunnel->tunnel.connect_handler = NULL;
470       tunnel->tunnel.disconnect_handler = NULL;
471       tunnel->tunnel.handler_cls = NULL;
472       tunnel->tunnel.ctx = NULL;
473       tunnel->tunnel.handle = handle;
474       memcpy (&tunnel->tunnel.peer, other,
475               sizeof (struct GNUNET_PeerIdentity));
476       memcpy (&tunnel->tunnel.id, &tmessage->id, sizeof (struct tunnel_id));
477
478       GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
479                                          handle->established_tunnels.tail,
480                                          handle->established_tunnels.tail,
481                                          tunnel);
482     }
483   else
484     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Inbound message from peer %s; type %d.\n",
485                GNUNET_i2s(other), ntohs(rmessage->type));
486
487   return handler->callback (handle->cls, &tunnel->tunnel,
488                             &tunnel->tunnel.ctx, other, rmessage, atsi);
489 }
490
491 struct GNUNET_MESH_Tunnel *
492 GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Handle *handle,
493                                           struct GNUNET_TIME_Relative timeout,
494                                           GNUNET_MESH_ApplicationType application_type,
495                                           GNUNET_MESH_TunnelConnectHandler
496                                           connect_handler,
497                                           GNUNET_MESH_TunnelDisconnectHandler
498                                           disconnect_handler,
499                                           void *handler_cls)
500 {
501   /* Look in the list of connected peers */
502   struct peer_list_element *element = handle->connected_peers.head;
503   while (element != NULL)
504     {
505       struct type_list_element* i;
506       for (i = element->type_head; i != NULL; i = i->next)
507         if (application_type == i->type)
508           return GNUNET_MESH_peer_request_connect_all (handle, timeout, 1,
509                                                        &element->peer,
510                                                        connect_handler,
511                                                        disconnect_handler,
512                                                        handler_cls);
513       element = element->next;
514     }
515
516   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Trying to connect by tupe %d.\n", application_type);
517
518   /* Put into pending list */
519   struct tunnel_list_element *tunnel =
520     GNUNET_malloc (sizeof (struct tunnel_list_element));
521
522   tunnel->tunnel.connect_handler = connect_handler;
523   tunnel->tunnel.disconnect_handler = disconnect_handler;
524   tunnel->tunnel.handler_cls = handler_cls;
525   tunnel->tunnel.ctx = NULL;
526   tunnel->tunnel.handle = handle;
527   memcpy (&tunnel->tunnel.id.initiator, &handle->myself,
528           sizeof (struct GNUNET_PeerIdentity));
529   tunnel->tunnel.id.id = current_id++;
530   tunnel->tunnel.application_type = application_type;
531
532   GNUNET_CONTAINER_DLL_insert_after (handle->pending_by_type_tunnels.head,
533                                      handle->pending_by_type_tunnels.tail,
534                                      handle->pending_by_type_tunnels.tail,
535                                      tunnel);
536   return &tunnel->tunnel;
537 }
538
539
540
541 struct GNUNET_MESH_Tunnel *
542 GNUNET_MESH_peer_request_connect_all (struct GNUNET_MESH_Handle *handle,
543                                       struct GNUNET_TIME_Relative timeout,
544                                       unsigned int num_peers,
545                                       const struct GNUNET_PeerIdentity *peers,
546                                       GNUNET_MESH_TunnelConnectHandler
547                                       connect_handler,
548                                       GNUNET_MESH_TunnelDisconnectHandler
549                                       disconnect_handler, void *handler_cls)
550 {
551   if (num_peers != 1)
552     return NULL;
553
554   struct tunnel_list_element *tunnel =
555     GNUNET_malloc (sizeof (struct tunnel_list_element));
556
557   tunnel->tunnel.connect_handler = connect_handler;
558   tunnel->tunnel.disconnect_handler = disconnect_handler;
559   tunnel->tunnel.handler_cls = handler_cls;
560   tunnel->tunnel.ctx = NULL;
561   tunnel->tunnel.handle = handle;
562   memcpy (&tunnel->tunnel.id.initiator, &handle->myself,
563           sizeof (struct GNUNET_PeerIdentity));
564   memcpy (&tunnel->tunnel.id.target, peers,
565           sizeof (struct GNUNET_PeerIdentity));
566   tunnel->tunnel.id.id = current_id++;
567   memcpy (&tunnel->tunnel.peer, peers, sizeof(struct GNUNET_PeerIdentity));
568
569   struct peer_list_element *element = handle->connected_peers.head;
570   while (element != NULL)
571     {
572       if (0 ==
573           memcmp (&element->peer, peers, sizeof (struct GNUNET_PeerIdentity)))
574         break;
575       element = element->next;
576     }
577
578   if (element != NULL)
579     {
580       /* we are connected to this peer */
581       GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
582                                          handle->established_tunnels.tail,
583                                          handle->established_tunnels.tail,
584                                          tunnel);
585       GNUNET_SCHEDULER_add_now(call_connect_handler, tunnel);
586     }
587   else if (0 ==
588            memcmp (peers, &handle->myself,
589                    sizeof (struct GNUNET_PeerIdentity)))
590     {
591       /* we are the peer */
592       GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
593                                          handle->established_tunnels.tail,
594                                          handle->established_tunnels.tail,
595                                          tunnel);
596       GNUNET_SCHEDULER_add_now(send_self_connect, tunnel);
597     }
598   else
599     {
600       /* we are not connected to this peer */
601       GNUNET_CONTAINER_DLL_insert_after (handle->pending_tunnels.head,
602                                          handle->pending_tunnels.tail,
603                                          handle->pending_tunnels.tail,
604                                          tunnel);
605       (void) GNUNET_CORE_peer_request_connect (handle->core,
606                                                peers,
607                                                NULL, NULL);
608     }
609
610   return &tunnel->tunnel;
611 }
612
613 const struct GNUNET_PeerIdentity*
614 GNUNET_MESH_get_peer(const struct GNUNET_MESH_Tunnel* tunnel)
615 {
616   return &tunnel->peer;
617 }
618
619 static size_t
620 core_notify(void* cls, size_t size, void* buf)
621 {
622   struct notify_cls *ncls = cls;
623   struct GNUNET_MESH_Tunnel *tunnel = ncls->tunnel;
624   tunnel->notify_handle = NULL;
625   struct tunnel_message* message = buf;
626   void* cbuf = (void*) &message[1];
627   GNUNET_assert(NULL != ncls->notify);
628
629   size_t sent = ncls->notify(ncls->notify_cls, size - sizeof(struct tunnel_message), cbuf);
630
631   GNUNET_free(ncls);
632
633   if (0 == sent) return 0;
634
635   sent += sizeof(struct tunnel_message);
636
637   message->hdr.type = htons(GNUNET_MESSAGE_TYPE_MESH);
638   message->hdr.size = htons(sent);
639   memcpy(&message->id, &tunnel->id, sizeof(struct tunnel_id));
640   return sent;
641 }
642
643
644 /**
645  * Ask the mesh to call "notify" once it is ready to transmit the
646  * given number of bytes to the specified "target".  If we are not yet
647  * connected to the specified peer, a call to this function will cause
648  * us to try to establish a connection.
649  *
650  * @param tunnel tunnel to use for transmission
651  * @param cork is corking allowed for this transmission?
652  * @param priority how important is the message?
653  * @param maxdelay how long can the message wait?
654  * @param target destination for the message, NULL for multicast to all tunnel targets 
655  * @param notify_size how many bytes of buffer space does notify want?
656  * @param notify function to call when buffer space is available;
657  *        will be called with NULL on timeout or if the overall queue
658  *        for this peer is larger than queue_size and this is currently
659  *        the message with the lowest priority
660  * @param notify_cls closure for notify
661  * @return non-NULL if the notify callback was queued,
662  *         NULL if we can not even queue the request (insufficient
663  *         memory); if NULL is returned, "notify" will NOT be called.
664  */
665 struct GNUNET_MESH_TransmitHandle *
666 GNUNET_MESH_notify_transmit_ready (struct
667                                    GNUNET_MESH_Tunnel
668                                    *tunnel,
669                                    int cork,
670                                    uint32_t priority,
671                                    struct
672                                    GNUNET_TIME_Relative
673                                    maxdelay,
674                                    const struct GNUNET_PeerIdentity *target
675                                    __attribute__ ((unused)),
676                                    size_t notify_size,
677                                    GNUNET_CONNECTION_TransmitReadyNotify
678                                    notify, void *notify_cls)
679 {
680   if (NULL != tunnel->notify_handle)
681     {
682       GNUNET_break(0);
683       return NULL;
684     }
685
686   struct notify_cls *cls = GNUNET_malloc (sizeof (struct notify_cls));
687   cls->notify_cls = notify_cls;
688   GNUNET_assert (NULL != notify);
689   cls->notify = notify;
690   cls->tunnel = tunnel;
691
692   tunnel->notify_handle = (struct GNUNET_MESH_TransmitHandle *)
693     GNUNET_CORE_notify_transmit_ready (tunnel->handle->core, cork, priority,
694                                        maxdelay, &tunnel->peer,
695                                        notify_size +
696                                        sizeof (struct tunnel_message),
697                                        &core_notify, (void *) cls);
698
699   return tunnel->notify_handle;
700 }
701
702 void
703 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle
704                                           *th)
705 {
706   GNUNET_CORE_notify_transmit_ready_cancel ((struct GNUNET_CORE_TransmitHandle
707                                              *) th);
708 }
709
710 void
711 GNUNET_MESH_tunnel_set_head (struct GNUNET_MESH_Tunnel *tunnel, void *head)
712 {
713   tunnel->app_head = head;
714 }
715
716 void
717 GNUNET_MESH_tunnel_set_tail (struct GNUNET_MESH_Tunnel *tunnel, void *tail)
718 {
719   tunnel->app_tail = tail;
720 }
721
722 void *
723 GNUNET_MESH_tunnel_get_head (struct GNUNET_MESH_Tunnel *tunnel)
724 {
725   return tunnel->app_head;
726 }
727
728 void *
729 GNUNET_MESH_tunnel_get_tail (struct GNUNET_MESH_Tunnel *tunnel)
730 {
731   return tunnel->app_head;
732 }
733
734 void
735 GNUNET_MESH_tunnel_set_data (struct GNUNET_MESH_Tunnel *tunnel, void *data)
736 {
737   tunnel->app_data = data;
738 }
739
740 void *
741 GNUNET_MESH_tunnel_get_data (struct GNUNET_MESH_Tunnel *tunnel)
742 {
743   return tunnel->app_data;
744 }
745
746
747 void build_hello_message(struct GNUNET_MESH_Handle* handle,
748                          const GNUNET_MESH_ApplicationType *stypes)
749 {
750   int num = 0;
751   const GNUNET_MESH_ApplicationType *t;
752
753   for (t = stypes; *t != GNUNET_APPLICATION_TYPE_END; t++, num++);
754
755   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "I can handle %d app-types.\n", num);
756
757   handle->hello_message_size = sizeof(uint16_t) + /* For the number of types */
758     num * sizeof(GNUNET_MESH_ApplicationType); /* For the types */
759
760   uint16_t *nums = GNUNET_malloc(handle->hello_message_size);
761   GNUNET_MESH_ApplicationType *types = (GNUNET_MESH_ApplicationType*)(nums + 1);
762
763   *nums = htons(num);
764
765   int i;
766   for (i = 0; i < num; i++)
767     {
768       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "I can handle the app-type %d\n", stypes[i]);
769       types[i] = htons(stypes[i]);
770     }
771
772   handle->hello_message = nums;
773 }
774
775
776 struct GNUNET_MESH_Handle *
777 GNUNET_MESH_connect (const struct
778                      GNUNET_CONFIGURATION_Handle
779                      *cfg, void *cls,
780                      GNUNET_MESH_TunnelEndHandler
781                      cleaner,
782                      const struct GNUNET_MESH_MessageHandler *handlers,
783                      const GNUNET_MESH_ApplicationType *stypes)
784 {
785   struct GNUNET_MESH_Handle *ret =
786     GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
787
788   ret->connected_to_core = GNUNET_NO;
789   ret->connected_peers.head = NULL;
790   ret->connected_peers.tail = NULL;
791   ret->cleaner = cleaner;
792   ret->cls = cls;
793
794   const struct GNUNET_MESH_MessageHandler *it;
795   unsigned int len = 1;
796   for (it = handlers; it->callback != NULL; it++)
797     {
798       len++;
799     }
800
801   ret->handlers =
802     GNUNET_malloc (len * sizeof (struct GNUNET_MESH_MessageHandler));
803   memset(ret->handlers, 0, len * sizeof(struct GNUNET_MESH_MessageHandler));
804   memcpy (ret->handlers, handlers,
805           len * sizeof (struct GNUNET_MESH_MessageHandler));
806
807   build_hello_message(ret, stypes);
808
809   static const struct GNUNET_CORE_MessageHandler core_handlers[] = {
810     {&core_receive, GNUNET_MESSAGE_TYPE_MESH, 0},
811     {&receive_hello, GNUNET_MESSAGE_TYPE_MESH_HELLO, 0},
812     {NULL, 0, 0}
813   };
814
815   ret->core = GNUNET_CORE_connect (cfg,
816                                    42,
817                                    ret,
818                                    &core_startup,
819                                    &core_connect,
820                                    &core_disconnect,
821                                    NULL,
822                                    NULL,
823                                    GNUNET_NO, NULL, GNUNET_NO, core_handlers);
824   return ret;
825 }
826
827 void
828 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
829 {
830   GNUNET_free (handle->handlers);
831   GNUNET_free (handle->hello_message);
832   GNUNET_CORE_disconnect (handle->core);
833
834   struct peer_list_element *element = handle->connected_peers.head;
835   while (element != NULL)
836     {
837       struct peer_list_element *next = element->next;
838       while (element->type_head != NULL)
839         {
840           struct type_list_element* tail = element->type_tail;
841           GNUNET_CONTAINER_DLL_remove(element->type_head, element->type_tail, tail);
842           GNUNET_free(tail);
843         }
844       GNUNET_free (element);
845       element = next;
846     }
847
848   struct tunnel_list_element *tunnel = handle->pending_tunnels.head;
849   while (tunnel != NULL)
850     {
851       struct tunnel_list_element *next = tunnel->next;
852       GNUNET_free (tunnel);
853       tunnel = next;
854     }
855   tunnel = handle->established_tunnels.head;;
856   while (tunnel != NULL)
857     {
858       struct tunnel_list_element *next = tunnel->next;
859       GNUNET_free (tunnel);
860       tunnel = next;
861     }
862
863   GNUNET_free (handle);
864 }
865
866 /* end of mesh_api.c */