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_container_lib.h>
32 #include <gnunet_applications.h>
36 uint32_t id GNUNET_PACKED;
37 struct GNUNET_PeerIdentity initiator;
38 struct GNUNET_PeerIdentity target;
41 static uint32_t current_id = 0;
45 struct GNUNET_MessageHeader hdr;
47 /* followed by another GNUNET_MessageHeader */
53 GNUNET_CONNECTION_TransmitReadyNotify notify;
54 struct GNUNET_MESH_Tunnel *tunnel;
57 struct GNUNET_MESH_Tunnel
59 /* The other peer this tunnel leads to; just unicast for the moment! */
60 struct GNUNET_PeerIdentity peer;
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;
69 struct GNUNET_MESH_Handle *handle;
71 /* The application-type requested for this tunnel. Is only needed for pending
74 uint16_t application_type;
76 struct GNUNET_MESH_TransmitHandle *notify_handle;
78 /* The context of the receive-function. */
81 /* A list, usable by application-code (for queues) */
85 /* A pointer, usable by application-code */
89 struct tunnel_list_element
91 struct GNUNET_MESH_Tunnel tunnel;
92 struct tunnel_list_element *next, *prev;
97 struct tunnel_list_element *head, *tail;
100 struct type_list_element
102 GNUNET_MESH_ApplicationType type;
103 struct type_list_element *next, *prev;
106 struct peer_list_element
108 struct GNUNET_PeerIdentity peer;
110 /* list of application-types */
111 struct type_list_element *type_head, *type_tail;
113 struct GNUNET_TRANSPORT_ATS_Information atsi;
114 struct peer_list_element *next, *prev;
116 /* The handle that sends the hellos to this peer */
117 struct GNUNET_CORE_TransmitHandle *hello;
119 GNUNET_SCHEDULER_TaskIdentifier sched;
121 struct GNUNET_MESH_Handle *handle;
126 struct peer_list_element *head, *tail;
129 struct GNUNET_MESH_Handle
131 struct GNUNET_CORE_Handle *core;
132 struct GNUNET_MESH_MessageHandler *handlers;
133 struct GNUNET_PeerIdentity myself;
134 unsigned int connected_to_core;
135 struct peer_list connected_peers;
136 struct tunnel_list established_tunnels;
137 struct tunnel_list pending_tunnels;
138 struct tunnel_list pending_by_type_tunnels;
140 GNUNET_MESH_TunnelEndHandler *cleaner;
141 size_t hello_message_size;
142 uint16_t *hello_message;
146 send_end_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
148 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
151 struct GNUNET_MESH_Tunnel *tunnel = cls;
153 tunnel->connect_handler (tunnel->handler_cls, NULL, NULL);
157 send_self_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
159 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
162 struct GNUNET_MESH_Tunnel *tunnel = cls;
164 tunnel->connect_handler (tunnel->handler_cls, &tunnel->handle->myself, NULL);
165 GNUNET_SCHEDULER_add_now (send_end_connect, tunnel);
169 call_connect_handler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
171 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
174 struct GNUNET_MESH_Tunnel *tunnel = cls;
176 tunnel->connect_handler (tunnel->handler_cls, &tunnel->peer, NULL);
177 GNUNET_SCHEDULER_add_now (send_end_connect, tunnel);
181 core_startup (void *cls, struct GNUNET_CORE_Handle *core
182 __attribute__ ((unused)),
183 const struct GNUNET_PeerIdentity *my_identity,
184 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey
185 __attribute__ ((unused)))
187 struct GNUNET_MESH_Handle *handle = cls;
189 memcpy (&handle->myself, my_identity, sizeof (struct GNUNET_PeerIdentity));
190 handle->connected_to_core = GNUNET_YES;
194 send_hello_message (void *cls, size_t size, void *buf)
199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending hello\n");
201 struct peer_list_element *element = cls;
202 struct GNUNET_MESH_Handle *handle = element->handle;
204 element->hello = NULL;
205 struct GNUNET_MessageHeader *hdr = buf;
208 sizeof (struct GNUNET_MessageHeader) + handle->hello_message_size;
213 hdr->type = htons (GNUNET_MESSAGE_TYPE_MESH_HELLO);
214 hdr->size = htons (size);
216 memcpy (hdr + 1, handle->hello_message, handle->hello_message_size);
221 schedule_hello_message (void *cls,
222 const struct GNUNET_SCHEDULER_TaskContext *tctx)
224 struct peer_list_element *element = cls;
226 element->sched = GNUNET_SCHEDULER_NO_TASK;
228 if ((tctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
231 if (element->hello == NULL)
233 GNUNET_CORE_notify_transmit_ready (element->handle->core, GNUNET_NO, 42,
234 GNUNET_TIME_UNIT_SECONDS,
236 sizeof (struct GNUNET_MessageHeader)
238 element->handle->hello_message_size,
239 &send_hello_message, element);
242 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
243 schedule_hello_message, cls);
248 * Core calls this if we are connected to a new peer.
250 * The peer is added to the connected_peers-list.
254 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
255 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
257 struct GNUNET_MESH_Handle *handle = cls;
259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
260 "Core tells us we are connected to peer %s\n", GNUNET_i2s (peer));
262 /* put the new peer into the list of connected peers */
263 struct peer_list_element *element =
264 GNUNET_malloc (sizeof (struct peer_list_element));
265 memcpy (&element->peer, peer, sizeof (struct GNUNET_PeerIdentity));
266 element->handle = handle;
268 /* Send a hello to this peer */
269 element->sched = GNUNET_SCHEDULER_add_now (schedule_hello_message, element);
272 memcpy (&element->atsi, atsi,
273 sizeof (struct GNUNET_TRANSPORT_ATS_Information));
275 GNUNET_CONTAINER_DLL_insert_after (handle->connected_peers.head,
276 handle->connected_peers.tail,
277 handle->connected_peers.tail, element);
279 struct tunnel_list_element *tunnel = handle->pending_tunnels.head;
281 while (tunnel != NULL)
284 memcmp (&tunnel->tunnel.peer, peer,
285 sizeof (struct GNUNET_PeerIdentity)))
287 struct tunnel_list_element *next = tunnel->next;
289 GNUNET_CONTAINER_DLL_remove (handle->pending_tunnels.head,
290 handle->pending_tunnels.tail, tunnel);
291 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
292 handle->established_tunnels.tail,
293 handle->established_tunnels.tail,
295 tunnel->tunnel.connect_handler (tunnel->tunnel.handler_cls, peer, atsi);
296 GNUNET_SCHEDULER_add_now (send_end_connect, tunnel);
300 tunnel = tunnel->next;
305 * Core calls this if we disconnect a peer
307 * Remove this peer from the list of connected peers
308 * Close all tunnels this peer belongs to
311 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
313 struct GNUNET_MESH_Handle *handle = cls;
315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
316 "Core tells us we are no longer connected to peer %s\n",
319 struct peer_list_element *element = handle->connected_peers.head;
321 while (element != NULL)
323 if (0 == memcmp (&element->peer, peer, sizeof (struct GNUNET_PeerIdentity)))
325 element = element->next;
329 GNUNET_CONTAINER_DLL_remove (handle->connected_peers.head,
330 handle->connected_peers.tail, element);
331 while (element->type_head != NULL)
333 struct type_list_element *tail = element->type_tail;
335 GNUNET_CONTAINER_DLL_remove (element->type_head, element->type_tail,
339 if (element->hello != NULL)
340 GNUNET_CORE_notify_transmit_ready_cancel (element->hello);
341 GNUNET_SCHEDULER_cancel (element->sched);
342 GNUNET_free (element);
345 struct tunnel_list_element *telement = handle->established_tunnels.head;
347 while (telement != NULL)
350 memcmp (&telement->tunnel.peer, peer,
351 sizeof (struct GNUNET_PeerIdentity)))
353 /* disconnect tunnels */
354 /* outbound tunnels */
355 if (telement->tunnel.connect_handler != NULL &&
356 NULL != telement->tunnel.disconnect_handler)
357 telement->tunnel.disconnect_handler (telement->tunnel.handler_cls,
359 /* inbound tunnels */
360 else if (NULL != handle->cleaner)
361 handle->cleaner (handle->cls, &telement->tunnel, &telement->tunnel.ctx);
363 struct tunnel_list_element *next = telement->next;
365 GNUNET_CONTAINER_DLL_remove (handle->established_tunnels.head,
366 handle->established_tunnels.tail, telement);
367 GNUNET_free (telement);
372 telement = telement->next;
378 * Receive a message from core.
379 * This is a hello-message, containing the application-types the other peer can receive
382 receive_hello (void *cls, const struct GNUNET_PeerIdentity *other,
383 const struct GNUNET_MessageHeader *message,
384 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
386 struct GNUNET_MESH_Handle *handle = cls;
387 uint16_t *num = (uint16_t *) (message + 1);
388 GNUNET_MESH_ApplicationType *ports =
389 (GNUNET_MESH_ApplicationType *) (num + 1);
392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
393 "The peer %s tells us he supports %d application-types.\n",
394 GNUNET_i2s (other), ntohs (*num));
396 struct peer_list_element *element = handle->connected_peers.head;
398 while (element != NULL)
401 memcmp (&element->peer, other, sizeof (struct GNUNET_PeerIdentity)))
403 element = element->next;
406 GNUNET_assert (NULL != element);
408 for (i = 0; i < ntohs (*num); i++)
410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
411 "The peer %s newly supports the application-type %d\n",
412 GNUNET_i2s (other), ntohs (ports[i]));
413 if (GNUNET_APPLICATION_TYPE_END == ntohs (ports[i]))
415 struct type_list_element *new_type = GNUNET_malloc (sizeof *new_type);
417 new_type->type = (GNUNET_MESH_ApplicationType) ntohs (ports[i]);
418 GNUNET_CONTAINER_DLL_insert (element->type_head, element->type_tail,
422 struct type_list_element *type;
424 for (type = element->type_head; type != NULL; type = type->next)
426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
427 "The peer %s supports the application-type %d\n",
428 GNUNET_i2s (other), type->type);
431 struct tunnel_list_element *tunnel = handle->pending_by_type_tunnels.head;
433 while (tunnel != NULL)
435 struct tunnel_list_element *next = tunnel->next;
437 for (i = 0; i < ntohs (*num); i++)
439 if (ntohs (ports[i]) == tunnel->tunnel.application_type)
441 GNUNET_CONTAINER_DLL_remove (handle->pending_by_type_tunnels.head,
442 handle->pending_by_type_tunnels.tail,
444 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
445 handle->established_tunnels.tail,
446 handle->established_tunnels.tail,
448 memcpy (&tunnel->tunnel.peer, other,
449 sizeof (struct GNUNET_PeerIdentity));
450 tunnel->tunnel.connect_handler (tunnel->tunnel.handler_cls,
451 &tunnel->tunnel.peer, atsi);
452 GNUNET_SCHEDULER_add_now (send_end_connect, tunnel);
456 if (ntohs (ports[i]) == tunnel->tunnel.application_type)
459 tunnel = tunnel->next;
465 * Receive a message from core.
468 core_receive (void *cls, const struct GNUNET_PeerIdentity *other,
469 const struct GNUNET_MessageHeader *message,
470 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
472 struct GNUNET_MESH_Handle *handle = cls;
473 struct tunnel_message *tmessage = (struct tunnel_message *) message;
474 struct GNUNET_MessageHeader *rmessage =
475 (struct GNUNET_MessageHeader *) (tmessage + 1);
477 struct GNUNET_MESH_MessageHandler *handler;
479 for (handler = handle->handlers; handler->callback != NULL; handler++)
481 if ((ntohs (rmessage->type) == handler->type) &&
482 ((handler->expected_size == 0) ||
483 (handler->expected_size == ntohs (rmessage->size))))
489 /* handler->callback handles this message */
491 /* If no handler was found, drop the message but keep the channel open */
492 if (handler->callback == NULL)
494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
495 "Received message of type %d from peer %s; dropping it.\n",
496 ntohs (rmessage->type), GNUNET_i2s (other));
500 struct tunnel_list_element *tunnel = handle->established_tunnels.head;
502 while (tunnel != NULL)
504 if (tunnel->tunnel.id.id == tmessage->id.id &&
506 memcmp (&tmessage->id.initiator, &tunnel->tunnel.id.initiator,
507 sizeof (struct GNUNET_PeerIdentity))) &&
509 memcmp (&tmessage->id.target, &tunnel->tunnel.id.target,
510 sizeof (struct GNUNET_PeerIdentity))))
512 tunnel = tunnel->next;
515 /* if no tunnel was found: create a new inbound tunnel */
518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
519 "New inbound tunnel from peer %s; first message has type %d.\n",
520 GNUNET_i2s (other), ntohs (rmessage->type));
521 tunnel = GNUNET_malloc (sizeof (struct tunnel_list_element));
522 tunnel->tunnel.connect_handler = NULL;
523 tunnel->tunnel.disconnect_handler = NULL;
524 tunnel->tunnel.handler_cls = NULL;
525 tunnel->tunnel.ctx = NULL;
526 tunnel->tunnel.handle = handle;
527 memcpy (&tunnel->tunnel.peer, other, sizeof (struct GNUNET_PeerIdentity));
528 memcpy (&tunnel->tunnel.id, &tmessage->id, sizeof (struct tunnel_id));
530 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
531 handle->established_tunnels.tail,
532 handle->established_tunnels.tail,
536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
537 "Inbound message from peer %s; type %d.\n", GNUNET_i2s (other),
538 ntohs (rmessage->type));
540 return handler->callback (handle->cls, &tunnel->tunnel, &tunnel->tunnel.ctx,
541 other, rmessage, atsi);
544 struct GNUNET_MESH_Tunnel *
545 GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Handle *handle,
546 struct GNUNET_TIME_Relative timeout,
547 GNUNET_MESH_ApplicationType
549 GNUNET_MESH_TunnelConnectHandler
551 GNUNET_MESH_TunnelDisconnectHandler
552 disconnect_handler, void *handler_cls)
554 /* Look in the list of connected peers */
555 struct peer_list_element *element = handle->connected_peers.head;
557 while (element != NULL)
559 struct type_list_element *i;
561 for (i = element->type_head; i != NULL; i = i->next)
562 if (application_type == i->type)
563 return GNUNET_MESH_peer_request_connect_all (handle, timeout, 1,
568 element = element->next;
571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect by tupe %d.\n",
574 /* Put into pending list */
575 struct tunnel_list_element *tunnel =
576 GNUNET_malloc (sizeof (struct tunnel_list_element));
578 tunnel->tunnel.connect_handler = connect_handler;
579 tunnel->tunnel.disconnect_handler = disconnect_handler;
580 tunnel->tunnel.handler_cls = handler_cls;
581 tunnel->tunnel.ctx = NULL;
582 tunnel->tunnel.handle = handle;
583 memcpy (&tunnel->tunnel.id.initiator, &handle->myself,
584 sizeof (struct GNUNET_PeerIdentity));
585 tunnel->tunnel.id.id = current_id++;
586 tunnel->tunnel.application_type = application_type;
588 GNUNET_CONTAINER_DLL_insert_after (handle->pending_by_type_tunnels.head,
589 handle->pending_by_type_tunnels.tail,
590 handle->pending_by_type_tunnels.tail,
592 return &tunnel->tunnel;
597 struct GNUNET_MESH_Tunnel *
598 GNUNET_MESH_peer_request_connect_all (struct GNUNET_MESH_Handle *handle,
599 struct GNUNET_TIME_Relative timeout,
600 unsigned int num_peers,
601 const struct GNUNET_PeerIdentity *peers,
602 GNUNET_MESH_TunnelConnectHandler
604 GNUNET_MESH_TunnelDisconnectHandler
605 disconnect_handler, void *handler_cls)
610 struct tunnel_list_element *tunnel =
611 GNUNET_malloc (sizeof (struct tunnel_list_element));
613 tunnel->tunnel.connect_handler = connect_handler;
614 tunnel->tunnel.disconnect_handler = disconnect_handler;
615 tunnel->tunnel.handler_cls = handler_cls;
616 tunnel->tunnel.ctx = NULL;
617 tunnel->tunnel.handle = handle;
618 memcpy (&tunnel->tunnel.id.initiator, &handle->myself,
619 sizeof (struct GNUNET_PeerIdentity));
620 memcpy (&tunnel->tunnel.id.target, peers,
621 sizeof (struct GNUNET_PeerIdentity));
622 tunnel->tunnel.id.id = current_id++;
623 memcpy (&tunnel->tunnel.peer, peers, sizeof (struct GNUNET_PeerIdentity));
625 struct peer_list_element *element = handle->connected_peers.head;
627 while (element != NULL)
630 memcmp (&element->peer, peers, sizeof (struct GNUNET_PeerIdentity)))
632 element = element->next;
637 /* we are connected to this peer */
638 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
639 handle->established_tunnels.tail,
640 handle->established_tunnels.tail,
642 GNUNET_SCHEDULER_add_now (call_connect_handler, tunnel);
645 memcmp (peers, &handle->myself, sizeof (struct GNUNET_PeerIdentity)))
647 /* we are the peer */
648 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
649 handle->established_tunnels.tail,
650 handle->established_tunnels.tail,
652 GNUNET_SCHEDULER_add_now (send_self_connect, tunnel);
656 /* we are not connected to this peer */
657 GNUNET_CONTAINER_DLL_insert_after (handle->pending_tunnels.head,
658 handle->pending_tunnels.tail,
659 handle->pending_tunnels.tail, tunnel);
660 (void) GNUNET_CORE_peer_request_connect (handle->core, peers, NULL, NULL);
663 return &tunnel->tunnel;
666 const struct GNUNET_PeerIdentity *
667 GNUNET_MESH_get_peer (const struct GNUNET_MESH_Tunnel *tunnel)
669 return &tunnel->peer;
673 core_notify (void *cls, size_t size, void *buf)
675 struct notify_cls *ncls = cls;
676 struct GNUNET_MESH_Tunnel *tunnel = ncls->tunnel;
678 tunnel->notify_handle = NULL;
679 struct tunnel_message *message = buf;
680 void *cbuf = (void *) &message[1];
682 GNUNET_assert (NULL != ncls->notify);
685 ncls->notify (ncls->notify_cls, size - sizeof (struct tunnel_message),
693 sent += sizeof (struct tunnel_message);
695 message->hdr.type = htons (GNUNET_MESSAGE_TYPE_MESH);
696 message->hdr.size = htons (sent);
697 memcpy (&message->id, &tunnel->id, sizeof (struct tunnel_id));
703 * Ask the mesh to call "notify" once it is ready to transmit the
704 * given number of bytes to the specified "target". If we are not yet
705 * connected to the specified peer, a call to this function will cause
706 * us to try to establish a connection.
708 * @param tunnel tunnel to use for transmission
709 * @param cork is corking allowed for this transmission?
710 * @param priority how important is the message?
711 * @param maxdelay how long can the message wait?
712 * @param target destination for the message, NULL for multicast to all tunnel targets
713 * @param notify_size how many bytes of buffer space does notify want?
714 * @param notify function to call when buffer space is available;
715 * will be called with NULL on timeout or if the overall queue
716 * for this peer is larger than queue_size and this is currently
717 * the message with the lowest priority
718 * @param notify_cls closure for notify
719 * @return non-NULL if the notify callback was queued,
720 * NULL if we can not even queue the request (insufficient
721 * memory); if NULL is returned, "notify" will NOT be called.
723 struct GNUNET_MESH_TransmitHandle *
724 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel, int cork,
726 struct GNUNET_TIME_Relative maxdelay,
727 const struct GNUNET_PeerIdentity *target
728 __attribute__ ((unused)), size_t notify_size,
729 GNUNET_CONNECTION_TransmitReadyNotify notify,
732 if (NULL != tunnel->notify_handle)
738 struct notify_cls *cls = GNUNET_malloc (sizeof (struct notify_cls));
740 cls->notify_cls = notify_cls;
741 GNUNET_assert (NULL != notify);
742 cls->notify = notify;
743 cls->tunnel = tunnel;
745 tunnel->notify_handle =
746 (struct GNUNET_MESH_TransmitHandle *)
747 GNUNET_CORE_notify_transmit_ready (tunnel->handle->core, cork, priority,
748 maxdelay, &tunnel->peer,
750 sizeof (struct tunnel_message),
751 &core_notify, (void *) cls);
753 return tunnel->notify_handle;
757 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle *th)
759 GNUNET_CORE_notify_transmit_ready_cancel ((struct GNUNET_CORE_TransmitHandle
764 GNUNET_MESH_tunnel_set_head (struct GNUNET_MESH_Tunnel *tunnel, void *head)
766 tunnel->app_head = head;
770 GNUNET_MESH_tunnel_set_tail (struct GNUNET_MESH_Tunnel *tunnel, void *tail)
772 tunnel->app_tail = tail;
776 GNUNET_MESH_tunnel_get_head (struct GNUNET_MESH_Tunnel *tunnel)
778 return tunnel->app_head;
782 GNUNET_MESH_tunnel_get_tail (struct GNUNET_MESH_Tunnel *tunnel)
784 return tunnel->app_head;
788 GNUNET_MESH_tunnel_set_data (struct GNUNET_MESH_Tunnel *tunnel, void *data)
790 tunnel->app_data = data;
794 GNUNET_MESH_tunnel_get_data (struct GNUNET_MESH_Tunnel *tunnel)
796 return tunnel->app_data;
801 build_hello_message (struct GNUNET_MESH_Handle *handle,
802 const GNUNET_MESH_ApplicationType *stypes)
805 const GNUNET_MESH_ApplicationType *t;
807 for (t = stypes; *t != GNUNET_APPLICATION_TYPE_END; t++, num++) ;
809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "I can handle %d app-types.\n", num);
811 handle->hello_message_size = sizeof (uint16_t) + /* For the number of types */
812 num * sizeof (GNUNET_MESH_ApplicationType); /* For the types */
814 uint16_t *nums = GNUNET_malloc (handle->hello_message_size);
815 GNUNET_MESH_ApplicationType *types =
816 (GNUNET_MESH_ApplicationType *) (nums + 1);
822 for (i = 0; i < num; i++)
824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "I can handle the app-type %d\n",
826 types[i] = htons (stypes[i]);
829 handle->hello_message = nums;
833 struct GNUNET_MESH_Handle *
834 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
835 GNUNET_MESH_TunnelEndHandler cleaner,
836 const struct GNUNET_MESH_MessageHandler *handlers,
837 const GNUNET_MESH_ApplicationType *stypes)
839 struct GNUNET_MESH_Handle *ret =
840 GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
842 ret->connected_to_core = GNUNET_NO;
843 ret->connected_peers.head = NULL;
844 ret->connected_peers.tail = NULL;
845 ret->cleaner = cleaner;
848 const struct GNUNET_MESH_MessageHandler *it;
849 unsigned int len = 1;
851 for (it = handlers; it->callback != NULL; it++)
857 GNUNET_malloc (len * sizeof (struct GNUNET_MESH_MessageHandler));
858 memset (ret->handlers, 0, len * sizeof (struct GNUNET_MESH_MessageHandler));
859 memcpy (ret->handlers, handlers,
860 len * sizeof (struct GNUNET_MESH_MessageHandler));
862 build_hello_message (ret, stypes);
864 static const struct GNUNET_CORE_MessageHandler core_handlers[] = {
865 {&core_receive, GNUNET_MESSAGE_TYPE_MESH, 0},
866 {&receive_hello, GNUNET_MESSAGE_TYPE_MESH_HELLO, 0},
871 GNUNET_CORE_connect (cfg, 42, ret, &core_startup, &core_connect,
872 &core_disconnect, NULL, NULL, GNUNET_NO, NULL,
873 GNUNET_NO, core_handlers);
878 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
880 GNUNET_free (handle->handlers);
881 GNUNET_free (handle->hello_message);
882 GNUNET_CORE_disconnect (handle->core);
884 struct peer_list_element *element = handle->connected_peers.head;
886 while (element != NULL)
888 struct peer_list_element *next = element->next;
890 while (element->type_head != NULL)
892 struct type_list_element *tail = element->type_tail;
894 GNUNET_CONTAINER_DLL_remove (element->type_head, element->type_tail,
898 GNUNET_CORE_notify_transmit_ready_cancel (element->hello);
899 GNUNET_SCHEDULER_cancel (element->sched);
900 GNUNET_free (element);
904 struct tunnel_list_element *tunnel = handle->pending_tunnels.head;
906 while (tunnel != NULL)
908 struct tunnel_list_element *next = tunnel->next;
910 GNUNET_free (tunnel);
913 tunnel = handle->established_tunnels.head;;
914 while (tunnel != NULL)
916 struct tunnel_list_element *next = tunnel->next;
918 GNUNET_free (tunnel);
922 GNUNET_free (handle);
925 /* end of mesh_api.c */