2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
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.
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.
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.
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
28 #include <gnunet_constants.h>
29 #include <gnunet_mesh_service.h>
30 #include <gnunet_core_service.h>
31 #include <gnunet_transport_service.h>
32 #include <gnunet_container_lib.h>
33 #include <gnunet_applications.h>
37 uint32_t id GNUNET_PACKED;
38 struct GNUNET_PeerIdentity initiator;
39 struct GNUNET_PeerIdentity target;
42 static uint32_t current_id = 0;
46 struct GNUNET_MessageHeader hdr;
48 /* followed by another GNUNET_MessageHeader */
54 GNUNET_CONNECTION_TransmitReadyNotify notify;
55 struct GNUNET_MESH_Tunnel *tunnel;
58 struct GNUNET_MESH_Tunnel
60 /* The other peer this tunnel leads to; just unicast for the moment! */
61 struct GNUNET_PeerIdentity peer;
65 /* The handlers and cls for outbound tunnels. Are NULL for inbound tunnels. */
66 GNUNET_MESH_TunnelDisconnectHandler disconnect_handler;
67 GNUNET_MESH_TunnelConnectHandler connect_handler;
70 struct GNUNET_MESH_Handle *handle;
72 /* The application-type requested for this tunnel. Is only needed for pending
75 uint16_t application_type;
77 struct GNUNET_MESH_TransmitHandle *notify_handle;
79 /* The context of the receive-function. */
82 /* A list, usable by application-code (for queues) */
86 /* A pointer, usable by application-code */
90 struct tunnel_list_element
92 struct GNUNET_MESH_Tunnel tunnel;
93 struct tunnel_list_element *next, *prev;
98 struct tunnel_list_element *head, *tail;
101 struct type_list_element
103 GNUNET_MESH_ApplicationType type;
104 struct type_list_element *next, *prev;
107 struct peer_list_element
109 struct GNUNET_PeerIdentity peer;
111 /* list of application-types */
112 struct type_list_element *type_head, *type_tail;
114 struct GNUNET_TRANSPORT_ATS_Information atsi;
115 struct peer_list_element *next, *prev;
117 /* The handle that sends the hellos to this peer */
118 struct GNUNET_CORE_TransmitHandle *hello;
120 GNUNET_SCHEDULER_TaskIdentifier sched;
122 struct GNUNET_MESH_Handle *handle;
127 struct peer_list_element *head, *tail;
130 struct GNUNET_MESH_Handle
132 struct GNUNET_CORE_Handle *core;
133 struct GNUNET_TRANSPORT_Handle *transport;
134 struct GNUNET_MESH_MessageHandler *handlers;
135 struct GNUNET_PeerIdentity myself;
136 unsigned int connected_to_core;
137 struct peer_list connected_peers;
138 struct tunnel_list established_tunnels;
139 struct tunnel_list pending_tunnels;
140 struct tunnel_list pending_by_type_tunnels;
142 GNUNET_MESH_TunnelEndHandler *cleaner;
143 size_t hello_message_size;
144 uint16_t *hello_message;
148 send_end_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
150 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
153 struct GNUNET_MESH_Tunnel *tunnel = cls;
155 tunnel->connect_handler (tunnel->handler_cls, NULL, NULL);
159 send_self_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
161 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
164 struct GNUNET_MESH_Tunnel *tunnel = cls;
166 tunnel->connect_handler (tunnel->handler_cls, &tunnel->handle->myself, NULL);
167 GNUNET_SCHEDULER_add_now (send_end_connect, tunnel);
171 call_connect_handler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
173 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
176 struct GNUNET_MESH_Tunnel *tunnel = cls;
178 tunnel->connect_handler (tunnel->handler_cls, &tunnel->peer, NULL);
179 GNUNET_SCHEDULER_add_now (send_end_connect, tunnel);
183 core_startup (void *cls, struct GNUNET_CORE_Handle *core
184 __attribute__ ((unused)),
185 const struct GNUNET_PeerIdentity *my_identity,
186 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey
187 __attribute__ ((unused)))
189 struct GNUNET_MESH_Handle *handle = cls;
191 memcpy (&handle->myself, my_identity, sizeof (struct GNUNET_PeerIdentity));
192 handle->connected_to_core = GNUNET_YES;
196 send_hello_message (void *cls, size_t size, void *buf)
201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending hello\n");
203 struct peer_list_element *element = cls;
204 struct GNUNET_MESH_Handle *handle = element->handle;
206 element->hello = NULL;
207 struct GNUNET_MessageHeader *hdr = buf;
210 sizeof (struct GNUNET_MessageHeader) + handle->hello_message_size;
215 hdr->type = htons (GNUNET_MESSAGE_TYPE_MESH_HELLO);
216 hdr->size = htons (size);
218 memcpy (hdr + 1, handle->hello_message, handle->hello_message_size);
223 schedule_hello_message (void *cls,
224 const struct GNUNET_SCHEDULER_TaskContext *tctx)
226 struct peer_list_element *element = cls;
228 element->sched = GNUNET_SCHEDULER_NO_TASK;
230 if ((tctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
233 if (element->hello == NULL)
235 GNUNET_CORE_notify_transmit_ready (element->handle->core, GNUNET_NO, 42,
236 GNUNET_TIME_UNIT_SECONDS,
238 sizeof (struct GNUNET_MessageHeader)
240 element->handle->hello_message_size,
241 &send_hello_message, element);
244 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
245 schedule_hello_message, cls);
250 * Core calls this if we are connected to a new peer.
252 * The peer is added to the connected_peers-list.
256 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
257 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
259 struct GNUNET_MESH_Handle *handle = cls;
261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
262 "Core tells us we are connected to peer %s\n", GNUNET_i2s (peer));
264 /* put the new peer into the list of connected peers */
265 struct peer_list_element *element =
266 GNUNET_malloc (sizeof (struct peer_list_element));
267 memcpy (&element->peer, peer, sizeof (struct GNUNET_PeerIdentity));
268 element->handle = handle;
270 /* Send a hello to this peer */
271 element->sched = GNUNET_SCHEDULER_add_now (schedule_hello_message, element);
274 memcpy (&element->atsi, atsi,
275 sizeof (struct GNUNET_TRANSPORT_ATS_Information));
277 GNUNET_CONTAINER_DLL_insert_after (handle->connected_peers.head,
278 handle->connected_peers.tail,
279 handle->connected_peers.tail, element);
281 struct tunnel_list_element *tunnel = handle->pending_tunnels.head;
283 while (tunnel != NULL)
286 memcmp (&tunnel->tunnel.peer, peer,
287 sizeof (struct GNUNET_PeerIdentity)))
289 struct tunnel_list_element *next = tunnel->next;
291 GNUNET_CONTAINER_DLL_remove (handle->pending_tunnels.head,
292 handle->pending_tunnels.tail, tunnel);
293 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
294 handle->established_tunnels.tail,
295 handle->established_tunnels.tail,
297 tunnel->tunnel.connect_handler (tunnel->tunnel.handler_cls, peer, atsi);
298 GNUNET_SCHEDULER_add_now (send_end_connect, tunnel);
302 tunnel = tunnel->next;
307 * Core calls this if we disconnect a peer
309 * Remove this peer from the list of connected peers
310 * Close all tunnels this peer belongs to
313 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
315 struct GNUNET_MESH_Handle *handle = cls;
317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
318 "Core tells us we are no longer connected to peer %s\n",
321 struct peer_list_element *element = handle->connected_peers.head;
323 while (element != NULL)
325 if (0 == memcmp (&element->peer, peer, sizeof (struct GNUNET_PeerIdentity)))
327 element = element->next;
331 GNUNET_CONTAINER_DLL_remove (handle->connected_peers.head,
332 handle->connected_peers.tail, element);
333 while (element->type_head != NULL)
335 struct type_list_element *tail = element->type_tail;
337 GNUNET_CONTAINER_DLL_remove (element->type_head, element->type_tail,
341 if (element->hello != NULL)
342 GNUNET_CORE_notify_transmit_ready_cancel (element->hello);
343 GNUNET_SCHEDULER_cancel (element->sched);
344 GNUNET_free (element);
347 struct tunnel_list_element *telement = handle->established_tunnels.head;
349 while (telement != NULL)
352 memcmp (&telement->tunnel.peer, peer,
353 sizeof (struct GNUNET_PeerIdentity)))
355 /* disconnect tunnels */
356 /* outbound tunnels */
357 if (telement->tunnel.connect_handler != NULL &&
358 NULL != telement->tunnel.disconnect_handler)
359 telement->tunnel.disconnect_handler (telement->tunnel.handler_cls,
361 /* inbound tunnels */
362 else if (NULL != handle->cleaner)
363 handle->cleaner (handle->cls, &telement->tunnel, &telement->tunnel.ctx);
365 struct tunnel_list_element *next = telement->next;
367 GNUNET_CONTAINER_DLL_remove (handle->established_tunnels.head,
368 handle->established_tunnels.tail, telement);
369 GNUNET_free (telement);
374 telement = telement->next;
380 * Receive a message from core.
381 * This is a hello-message, containing the application-types the other peer can receive
384 receive_hello (void *cls, const struct GNUNET_PeerIdentity *other,
385 const struct GNUNET_MessageHeader *message,
386 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
388 struct GNUNET_MESH_Handle *handle = cls;
389 uint16_t *num = (uint16_t *) (message + 1);
390 GNUNET_MESH_ApplicationType *ports =
391 (GNUNET_MESH_ApplicationType *) (num + 1);
394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
395 "The peer %s tells us he supports %d application-types.\n",
396 GNUNET_i2s (other), ntohs (*num));
398 struct peer_list_element *element = handle->connected_peers.head;
400 while (element != NULL)
403 memcmp (&element->peer, other, sizeof (struct GNUNET_PeerIdentity)))
405 element = element->next;
408 GNUNET_assert (NULL != element);
410 for (i = 0; i < ntohs (*num); i++)
412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
413 "The peer %s newly supports the application-type %d\n",
414 GNUNET_i2s (other), ntohs (ports[i]));
415 if (GNUNET_APPLICATION_TYPE_END == ntohs (ports[i]))
417 struct type_list_element *new_type = GNUNET_malloc (sizeof *new_type);
419 new_type->type = (GNUNET_MESH_ApplicationType) ntohs (ports[i]);
420 GNUNET_CONTAINER_DLL_insert (element->type_head, element->type_tail,
424 struct type_list_element *type;
426 for (type = element->type_head; type != NULL; type = type->next)
428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
429 "The peer %s supports the application-type %d\n",
430 GNUNET_i2s (other), type->type);
433 struct tunnel_list_element *tunnel = handle->pending_by_type_tunnels.head;
435 while (tunnel != NULL)
437 struct tunnel_list_element *next = tunnel->next;
439 for (i = 0; i < ntohs (*num); i++)
441 if (ntohs (ports[i]) == tunnel->tunnel.application_type)
443 GNUNET_CONTAINER_DLL_remove (handle->pending_by_type_tunnels.head,
444 handle->pending_by_type_tunnels.tail,
446 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
447 handle->established_tunnels.tail,
448 handle->established_tunnels.tail,
450 memcpy (&tunnel->tunnel.peer, other,
451 sizeof (struct GNUNET_PeerIdentity));
452 tunnel->tunnel.connect_handler (tunnel->tunnel.handler_cls,
453 &tunnel->tunnel.peer, atsi);
454 GNUNET_SCHEDULER_add_now (send_end_connect, tunnel);
458 if (ntohs (ports[i]) == tunnel->tunnel.application_type)
461 tunnel = tunnel->next;
467 * Receive a message from core.
470 core_receive (void *cls, const struct GNUNET_PeerIdentity *other,
471 const struct GNUNET_MessageHeader *message,
472 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
474 struct GNUNET_MESH_Handle *handle = cls;
475 struct tunnel_message *tmessage = (struct tunnel_message *) message;
476 struct GNUNET_MessageHeader *rmessage =
477 (struct GNUNET_MessageHeader *) (tmessage + 1);
479 struct GNUNET_MESH_MessageHandler *handler;
481 for (handler = handle->handlers; handler->callback != NULL; handler++)
483 if ((ntohs (rmessage->type) == handler->type) &&
484 ((handler->expected_size == 0) ||
485 (handler->expected_size == ntohs (rmessage->size))))
491 /* handler->callback handles this message */
493 /* If no handler was found, drop the message but keep the channel open */
494 if (handler->callback == NULL)
496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
497 "Received message of type %d from peer %s; dropping it.\n",
498 ntohs (rmessage->type), GNUNET_i2s (other));
502 struct tunnel_list_element *tunnel = handle->established_tunnels.head;
504 while (tunnel != NULL)
506 if (tunnel->tunnel.id.id == tmessage->id.id &&
508 memcmp (&tmessage->id.initiator, &tunnel->tunnel.id.initiator,
509 sizeof (struct GNUNET_PeerIdentity))) &&
511 memcmp (&tmessage->id.target, &tunnel->tunnel.id.target,
512 sizeof (struct GNUNET_PeerIdentity))))
514 tunnel = tunnel->next;
517 /* if no tunnel was found: create a new inbound tunnel */
520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
521 "New inbound tunnel from peer %s; first message has type %d.\n",
522 GNUNET_i2s (other), ntohs (rmessage->type));
523 tunnel = GNUNET_malloc (sizeof (struct tunnel_list_element));
524 tunnel->tunnel.connect_handler = NULL;
525 tunnel->tunnel.disconnect_handler = NULL;
526 tunnel->tunnel.handler_cls = NULL;
527 tunnel->tunnel.ctx = NULL;
528 tunnel->tunnel.handle = handle;
529 memcpy (&tunnel->tunnel.peer, other, sizeof (struct GNUNET_PeerIdentity));
530 memcpy (&tunnel->tunnel.id, &tmessage->id, sizeof (struct tunnel_id));
532 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
533 handle->established_tunnels.tail,
534 handle->established_tunnels.tail,
538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
539 "Inbound message from peer %s; type %d.\n", GNUNET_i2s (other),
540 ntohs (rmessage->type));
542 return handler->callback (handle->cls, &tunnel->tunnel, &tunnel->tunnel.ctx,
543 other, rmessage, atsi);
546 struct GNUNET_MESH_Tunnel *
547 GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Handle *handle,
548 struct GNUNET_TIME_Relative timeout,
549 GNUNET_MESH_ApplicationType
551 GNUNET_MESH_TunnelConnectHandler
553 GNUNET_MESH_TunnelDisconnectHandler
554 disconnect_handler, void *handler_cls)
556 /* Look in the list of connected peers */
557 struct peer_list_element *element = handle->connected_peers.head;
559 while (element != NULL)
561 struct type_list_element *i;
563 for (i = element->type_head; i != NULL; i = i->next)
564 if (application_type == i->type)
565 return GNUNET_MESH_peer_request_connect_all (handle, timeout, 1,
570 element = element->next;
573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect by tupe %d.\n",
576 /* Put into pending list */
577 struct tunnel_list_element *tunnel =
578 GNUNET_malloc (sizeof (struct tunnel_list_element));
580 tunnel->tunnel.connect_handler = connect_handler;
581 tunnel->tunnel.disconnect_handler = disconnect_handler;
582 tunnel->tunnel.handler_cls = handler_cls;
583 tunnel->tunnel.ctx = NULL;
584 tunnel->tunnel.handle = handle;
585 memcpy (&tunnel->tunnel.id.initiator, &handle->myself,
586 sizeof (struct GNUNET_PeerIdentity));
587 tunnel->tunnel.id.id = current_id++;
588 tunnel->tunnel.application_type = application_type;
590 GNUNET_CONTAINER_DLL_insert_after (handle->pending_by_type_tunnels.head,
591 handle->pending_by_type_tunnels.tail,
592 handle->pending_by_type_tunnels.tail,
594 return &tunnel->tunnel;
599 struct GNUNET_MESH_Tunnel *
600 GNUNET_MESH_peer_request_connect_all (struct GNUNET_MESH_Handle *handle,
601 struct GNUNET_TIME_Relative timeout,
602 unsigned int num_peers,
603 const struct GNUNET_PeerIdentity *peers,
604 GNUNET_MESH_TunnelConnectHandler
606 GNUNET_MESH_TunnelDisconnectHandler
607 disconnect_handler, void *handler_cls)
612 struct tunnel_list_element *tunnel =
613 GNUNET_malloc (sizeof (struct tunnel_list_element));
615 tunnel->tunnel.connect_handler = connect_handler;
616 tunnel->tunnel.disconnect_handler = disconnect_handler;
617 tunnel->tunnel.handler_cls = handler_cls;
618 tunnel->tunnel.ctx = NULL;
619 tunnel->tunnel.handle = handle;
620 memcpy (&tunnel->tunnel.id.initiator, &handle->myself,
621 sizeof (struct GNUNET_PeerIdentity));
622 memcpy (&tunnel->tunnel.id.target, peers,
623 sizeof (struct GNUNET_PeerIdentity));
624 tunnel->tunnel.id.id = current_id++;
625 memcpy (&tunnel->tunnel.peer, peers, sizeof (struct GNUNET_PeerIdentity));
627 struct peer_list_element *element = handle->connected_peers.head;
629 while (element != NULL)
632 memcmp (&element->peer, peers, sizeof (struct GNUNET_PeerIdentity)))
634 element = element->next;
639 /* we are connected to this peer */
640 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
641 handle->established_tunnels.tail,
642 handle->established_tunnels.tail,
644 GNUNET_SCHEDULER_add_now (call_connect_handler, tunnel);
647 memcmp (peers, &handle->myself, sizeof (struct GNUNET_PeerIdentity)))
649 /* we are the peer */
650 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
651 handle->established_tunnels.tail,
652 handle->established_tunnels.tail,
654 GNUNET_SCHEDULER_add_now (send_self_connect, tunnel);
658 /* we are not connected to this peer */
659 GNUNET_CONTAINER_DLL_insert_after (handle->pending_tunnels.head,
660 handle->pending_tunnels.tail,
661 handle->pending_tunnels.tail, tunnel);
662 GNUNET_TRANSPORT_try_connect (handle->transport, peers);
665 return &tunnel->tunnel;
668 const struct GNUNET_PeerIdentity *
669 GNUNET_MESH_get_peer (const struct GNUNET_MESH_Tunnel *tunnel)
671 return &tunnel->peer;
675 core_notify (void *cls, size_t size, void *buf)
677 struct notify_cls *ncls = cls;
678 struct GNUNET_MESH_Tunnel *tunnel = ncls->tunnel;
681 return ncls->notify (ncls->notify_cls, 0, NULL);
683 tunnel->notify_handle = NULL;
684 struct tunnel_message *message = buf;
685 void *cbuf = (void *) &message[1];
687 GNUNET_assert (NULL != ncls->notify);
690 ncls->notify (ncls->notify_cls, size - sizeof (struct tunnel_message),
698 sent += sizeof (struct tunnel_message);
700 message->hdr.type = htons (GNUNET_MESSAGE_TYPE_MESH);
701 message->hdr.size = htons (sent);
702 memcpy (&message->id, &tunnel->id, sizeof (struct tunnel_id));
708 * Ask the mesh to call "notify" once it is ready to transmit the
709 * given number of bytes to the specified "target". If we are not yet
710 * connected to the specified peer, a call to this function will cause
711 * us to try to establish a connection.
713 * @param tunnel tunnel to use for transmission
714 * @param cork is corking allowed for this transmission?
715 * @param priority how important is the message?
716 * @param maxdelay how long can the message wait?
717 * @param target destination for the message, NULL for multicast to all tunnel targets
718 * @param notify_size how many bytes of buffer space does notify want?
719 * @param notify function to call when buffer space is available;
720 * will be called with NULL on timeout or if the overall queue
721 * for this peer is larger than queue_size and this is currently
722 * the message with the lowest priority
723 * @param notify_cls closure for notify
724 * @return non-NULL if the notify callback was queued,
725 * NULL if we can not even queue the request (insufficient
726 * memory); if NULL is returned, "notify" will NOT be called.
728 struct GNUNET_MESH_TransmitHandle *
729 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel, int cork,
731 struct GNUNET_TIME_Relative maxdelay,
732 const struct GNUNET_PeerIdentity *target
733 __attribute__ ((unused)), size_t notify_size,
734 GNUNET_CONNECTION_TransmitReadyNotify notify,
737 if (NULL != tunnel->notify_handle)
743 struct notify_cls *cls = GNUNET_malloc (sizeof (struct notify_cls));
745 cls->notify_cls = notify_cls;
746 GNUNET_assert (NULL != notify);
747 cls->notify = notify;
748 cls->tunnel = tunnel;
750 tunnel->notify_handle =
751 (struct GNUNET_MESH_TransmitHandle *)
752 GNUNET_CORE_notify_transmit_ready (tunnel->handle->core, cork, priority,
753 maxdelay, &tunnel->peer,
755 sizeof (struct tunnel_message),
756 &core_notify, (void *) cls);
758 return tunnel->notify_handle;
762 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle *th)
764 GNUNET_CORE_notify_transmit_ready_cancel ((struct GNUNET_CORE_TransmitHandle
769 GNUNET_MESH_tunnel_set_head (struct GNUNET_MESH_Tunnel *tunnel, void *head)
771 tunnel->app_head = head;
775 GNUNET_MESH_tunnel_set_tail (struct GNUNET_MESH_Tunnel *tunnel, void *tail)
777 tunnel->app_tail = tail;
781 GNUNET_MESH_tunnel_get_head (struct GNUNET_MESH_Tunnel *tunnel)
783 return tunnel->app_head;
787 GNUNET_MESH_tunnel_get_tail (struct GNUNET_MESH_Tunnel *tunnel)
789 return tunnel->app_head;
793 GNUNET_MESH_tunnel_set_data (struct GNUNET_MESH_Tunnel *tunnel, void *data)
795 tunnel->app_data = data;
799 GNUNET_MESH_tunnel_get_data (struct GNUNET_MESH_Tunnel *tunnel)
801 return tunnel->app_data;
806 build_hello_message (struct GNUNET_MESH_Handle *handle,
807 const GNUNET_MESH_ApplicationType *stypes)
810 const GNUNET_MESH_ApplicationType *t;
812 for (t = stypes; *t != GNUNET_APPLICATION_TYPE_END; t++, num++) ;
814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "I can handle %d app-types.\n", num);
816 handle->hello_message_size = sizeof (uint16_t) + /* For the number of types */
817 num * sizeof (GNUNET_MESH_ApplicationType); /* For the types */
819 uint16_t *nums = GNUNET_malloc (handle->hello_message_size);
820 GNUNET_MESH_ApplicationType *types =
821 (GNUNET_MESH_ApplicationType *) (nums + 1);
827 for (i = 0; i < num; i++)
829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "I can handle the app-type %d\n",
831 types[i] = htons (stypes[i]);
834 handle->hello_message = nums;
838 struct GNUNET_MESH_Handle *
839 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
840 GNUNET_MESH_TunnelEndHandler cleaner,
841 const struct GNUNET_MESH_MessageHandler *handlers,
842 const GNUNET_MESH_ApplicationType *stypes)
844 struct GNUNET_MESH_Handle *ret =
845 GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
847 ret->connected_to_core = GNUNET_NO;
848 ret->connected_peers.head = NULL;
849 ret->connected_peers.tail = NULL;
850 ret->cleaner = cleaner;
853 const struct GNUNET_MESH_MessageHandler *it;
854 unsigned int len = 1;
856 for (it = handlers; it->callback != NULL; it++)
862 GNUNET_malloc (len * sizeof (struct GNUNET_MESH_MessageHandler));
863 memset (ret->handlers, 0, len * sizeof (struct GNUNET_MESH_MessageHandler));
864 memcpy (ret->handlers, handlers,
865 len * sizeof (struct GNUNET_MESH_MessageHandler));
867 build_hello_message (ret, stypes);
869 static const struct GNUNET_CORE_MessageHandler core_handlers[] = {
870 {&core_receive, GNUNET_MESSAGE_TYPE_MESH, 0},
871 {&receive_hello, GNUNET_MESSAGE_TYPE_MESH_HELLO, 0},
876 GNUNET_CORE_connect (cfg, 42, ret, &core_startup, &core_connect,
877 &core_disconnect, NULL, NULL, GNUNET_NO, NULL,
878 GNUNET_NO, core_handlers);
880 GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL, NULL, NULL);
885 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
887 GNUNET_free (handle->handlers);
888 GNUNET_free (handle->hello_message);
889 GNUNET_CORE_disconnect (handle->core);
890 GNUNET_TRANSPORT_disconnect (handle->transport);
892 struct peer_list_element *element = handle->connected_peers.head;
894 while (element != NULL)
896 struct peer_list_element *next = element->next;
898 while (element->type_head != NULL)
900 struct type_list_element *tail = element->type_tail;
902 GNUNET_CONTAINER_DLL_remove (element->type_head, element->type_tail,
906 GNUNET_CORE_notify_transmit_ready_cancel (element->hello);
907 GNUNET_SCHEDULER_cancel (element->sched);
908 GNUNET_free (element);
912 struct tunnel_list_element *tunnel = handle->pending_tunnels.head;
914 while (tunnel != NULL)
916 struct tunnel_list_element *next = tunnel->next;
918 GNUNET_free (tunnel);
921 tunnel = handle->established_tunnels.head;;
922 while (tunnel != NULL)
924 struct tunnel_list_element *next = tunnel->next;
926 GNUNET_free (tunnel);
930 GNUNET_free (handle);
933 /* end of mesh_api.c */