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;
119 struct peer_list_element *head, *tail;
122 struct GNUNET_MESH_Handle
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;
133 GNUNET_MESH_TunnelEndHandler *cleaner;
134 size_t hello_message_size;
135 uint16_t *hello_message;
139 send_end_connect(void* cls,
140 const struct GNUNET_SCHEDULER_TaskContext* tc)
142 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
145 struct GNUNET_MESH_Tunnel* tunnel = cls;
147 tunnel->connect_handler(tunnel->handler_cls, NULL, NULL);
151 send_self_connect(void* cls,
152 const struct GNUNET_SCHEDULER_TaskContext* tc)
154 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
157 struct GNUNET_MESH_Tunnel* tunnel = cls;
159 tunnel->connect_handler(tunnel->handler_cls, &tunnel->handle->myself, NULL);
160 GNUNET_SCHEDULER_add_now(send_end_connect, tunnel);
164 call_connect_handler (void *cls,
165 const struct GNUNET_SCHEDULER_TaskContext *tc)
167 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
170 struct GNUNET_MESH_Tunnel *tunnel = cls;
172 tunnel->connect_handler (tunnel->handler_cls, &tunnel->peer,
174 GNUNET_SCHEDULER_add_now (send_end_connect, tunnel);
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)))
183 struct GNUNET_MESH_Handle *handle = cls;
184 memcpy (&handle->myself, my_identity, sizeof (struct GNUNET_PeerIdentity));
185 handle->connected_to_core = GNUNET_YES;
189 send_hello_message (void *cls, size_t size, void *buf)
191 if (cls == NULL) return 0;
193 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending hello\n");
195 struct GNUNET_MESH_Handle *handle = cls;
196 struct GNUNET_MessageHeader *hdr = buf;
198 size_t sent = sizeof(struct GNUNET_MessageHeader) + handle->hello_message_size;
200 if (sent > size) return 0;
202 hdr->type = htons(GNUNET_MESSAGE_TYPE_MESH_HELLO);
203 hdr->size = htons(size);
205 memcpy(hdr+1, handle->hello_message, handle->hello_message_size);
211 * Core calls this if we are connected to a new peer.
213 * The peer is added to the connected_peers-list.
217 core_connect (void *cls,
218 const struct GNUNET_PeerIdentity *peer,
219 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
221 struct GNUNET_MESH_Handle *handle = cls;
223 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Core tells us we are connected to peer %s\n", GNUNET_i2s(peer));
225 /* Send a hello to this peer */
226 GNUNET_CORE_notify_transmit_ready(handle->core,
229 GNUNET_TIME_UNIT_SECONDS,
231 sizeof(struct GNUNET_MessageHeader) + handle->hello_message_size,
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));
241 memcpy (&element->atsi, atsi,
242 sizeof (struct GNUNET_TRANSPORT_ATS_Information));
244 GNUNET_CONTAINER_DLL_insert_after (handle->connected_peers.head,
245 handle->connected_peers.tail,
246 handle->connected_peers.tail, element);
248 struct tunnel_list_element *tunnel = handle->pending_tunnels.head;
249 while (tunnel != NULL)
252 memcmp (&tunnel->tunnel.peer, peer,
253 sizeof (struct GNUNET_PeerIdentity)))
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,
262 tunnel->tunnel.connect_handler (tunnel->tunnel.handler_cls,
264 GNUNET_SCHEDULER_add_now(send_end_connect, tunnel);
268 tunnel = tunnel->next;
273 * Core calls this if we disconnect a peer
275 * Remove this peer from the list of connected peers
276 * Close all tunnels this peer belongs to
279 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
281 struct GNUNET_MESH_Handle *handle = cls;
283 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Core tells us we are no longer connected to peer %s\n", GNUNET_i2s(peer));
285 struct peer_list_element *element = handle->connected_peers.head;
286 while (element != NULL)
289 memcmp (&element->peer, peer, sizeof (struct GNUNET_PeerIdentity)))
291 element = element->next;
295 GNUNET_CONTAINER_DLL_remove (handle->connected_peers.head,
296 handle->connected_peers.tail, element);
297 while (element->type_head != NULL)
299 struct type_list_element* tail = element->type_tail;
300 GNUNET_CONTAINER_DLL_remove(element->type_head, element->type_tail, tail);
303 GNUNET_free (element);
306 struct tunnel_list_element *telement = handle->established_tunnels.head;
307 while (telement != NULL)
310 memcmp (&telement->tunnel.peer, peer,
311 sizeof (struct GNUNET_PeerIdentity)))
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,
318 /* inbound tunnels */
319 else if (NULL != handle->cleaner)
320 handle->cleaner (handle->cls, &telement->tunnel,
321 &telement->tunnel.ctx);
323 struct tunnel_list_element *next = telement->next;
324 GNUNET_CONTAINER_DLL_remove (handle->established_tunnels.head,
325 handle->established_tunnels.tail,
327 GNUNET_free (telement);
332 telement = telement->next;
338 * Receive a message from core.
339 * This is a hello-message, containing the application-types the other peer can receive
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)
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);
352 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "The peer %s tells us he supports %d application-types.\n", GNUNET_i2s(other), ntohs(*num));
354 struct peer_list_element *element = handle->connected_peers.head;
355 while (element != NULL)
358 memcmp (&element->peer, other, sizeof (struct GNUNET_PeerIdentity)))
360 element = element->next;
363 GNUNET_assert(NULL != element);
365 for (i = 0; i < ntohs(*num); i++)
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);
374 struct type_list_element *type;
375 for (type = element->type_head; type != NULL; type = type->next)
377 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "The peer %s supports the application-type %d\n", GNUNET_i2s(other), type->type);
380 struct tunnel_list_element *tunnel = handle->pending_by_type_tunnels.head;
381 while (tunnel != NULL)
383 struct tunnel_list_element *next = tunnel->next;
384 for (i = 0; i < ntohs(*num); i++)
386 if (ntohs (ports[i]) == tunnel->tunnel.application_type)
388 GNUNET_CONTAINER_DLL_remove (handle->pending_by_type_tunnels.head,
389 handle->pending_by_type_tunnels.tail,
391 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.
393 handle->established_tunnels.
395 handle->established_tunnels.
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);
404 if (ntohs (ports[i]) == tunnel->tunnel.application_type)
407 tunnel = tunnel->next;
413 * Receive a message from core.
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)
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);
426 struct GNUNET_MESH_MessageHandler *handler;
428 for (handler = handle->handlers; handler->callback != NULL; handler++)
430 if ( (ntohs (rmessage->type) == handler->type)
431 && ( (handler->expected_size == 0)
432 || (handler->expected_size == ntohs (rmessage->size))) )
438 /* handler->callback handles this message */
440 /* If no handler was found, drop the message but keep the channel open */
441 if (handler->callback == NULL)
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));
448 struct tunnel_list_element *tunnel = handle->established_tunnels.head;
450 while (tunnel != NULL)
452 if (tunnel->tunnel.id.id == tmessage->id.id &&
454 memcmp (&tmessage->id.initiator, &tunnel->tunnel.id.initiator,
455 sizeof (struct GNUNET_PeerIdentity)))
457 memcmp (&tmessage->id.target, &tunnel->tunnel.id.target,
458 sizeof (struct GNUNET_PeerIdentity))))
460 tunnel = tunnel->next;
463 /* if no tunnel was found: create a new inbound tunnel */
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));
478 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
479 handle->established_tunnels.tail,
480 handle->established_tunnels.tail,
484 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Inbound message from peer %s; type %d.\n",
485 GNUNET_i2s(other), ntohs(rmessage->type));
487 return handler->callback (handle->cls, &tunnel->tunnel,
488 &tunnel->tunnel.ctx, other, rmessage, atsi);
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
497 GNUNET_MESH_TunnelDisconnectHandler
501 /* Look in the list of connected peers */
502 struct peer_list_element *element = handle->connected_peers.head;
503 while (element != NULL)
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,
513 element = element->next;
516 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Trying to connect by tupe %d.\n", application_type);
518 /* Put into pending list */
519 struct tunnel_list_element *tunnel =
520 GNUNET_malloc (sizeof (struct tunnel_list_element));
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;
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,
536 return &tunnel->tunnel;
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
548 GNUNET_MESH_TunnelDisconnectHandler
549 disconnect_handler, void *handler_cls)
554 struct tunnel_list_element *tunnel =
555 GNUNET_malloc (sizeof (struct tunnel_list_element));
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));
569 struct peer_list_element *element = handle->connected_peers.head;
570 while (element != NULL)
573 memcmp (&element->peer, peers, sizeof (struct GNUNET_PeerIdentity)))
575 element = element->next;
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,
585 GNUNET_SCHEDULER_add_now(call_connect_handler, tunnel);
588 memcmp (peers, &handle->myself,
589 sizeof (struct GNUNET_PeerIdentity)))
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,
596 GNUNET_SCHEDULER_add_now(send_self_connect, tunnel);
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,
605 (void) GNUNET_CORE_peer_request_connect (handle->core,
610 return &tunnel->tunnel;
613 const struct GNUNET_PeerIdentity*
614 GNUNET_MESH_get_peer(const struct GNUNET_MESH_Tunnel* tunnel)
616 return &tunnel->peer;
620 core_notify(void* cls, size_t size, void* buf)
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);
629 size_t sent = ncls->notify(ncls->notify_cls, size - sizeof(struct tunnel_message), cbuf);
633 if (0 == sent) return 0;
635 sent += sizeof(struct tunnel_message);
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));
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.
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.
665 struct GNUNET_MESH_TransmitHandle *
666 GNUNET_MESH_notify_transmit_ready (struct
674 const struct GNUNET_PeerIdentity *target
675 __attribute__ ((unused)),
677 GNUNET_CONNECTION_TransmitReadyNotify
678 notify, void *notify_cls)
680 if (NULL != tunnel->notify_handle)
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;
692 tunnel->notify_handle = (struct GNUNET_MESH_TransmitHandle *)
693 GNUNET_CORE_notify_transmit_ready (tunnel->handle->core, cork, priority,
694 maxdelay, &tunnel->peer,
696 sizeof (struct tunnel_message),
697 &core_notify, (void *) cls);
699 return tunnel->notify_handle;
703 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle
706 GNUNET_CORE_notify_transmit_ready_cancel ((struct GNUNET_CORE_TransmitHandle
711 GNUNET_MESH_tunnel_set_head (struct GNUNET_MESH_Tunnel *tunnel, void *head)
713 tunnel->app_head = head;
717 GNUNET_MESH_tunnel_set_tail (struct GNUNET_MESH_Tunnel *tunnel, void *tail)
719 tunnel->app_tail = tail;
723 GNUNET_MESH_tunnel_get_head (struct GNUNET_MESH_Tunnel *tunnel)
725 return tunnel->app_head;
729 GNUNET_MESH_tunnel_get_tail (struct GNUNET_MESH_Tunnel *tunnel)
731 return tunnel->app_head;
735 GNUNET_MESH_tunnel_set_data (struct GNUNET_MESH_Tunnel *tunnel, void *data)
737 tunnel->app_data = data;
741 GNUNET_MESH_tunnel_get_data (struct GNUNET_MESH_Tunnel *tunnel)
743 return tunnel->app_data;
747 void build_hello_message(struct GNUNET_MESH_Handle* handle,
748 const GNUNET_MESH_ApplicationType *stypes)
751 const GNUNET_MESH_ApplicationType *t;
753 for (t = stypes; *t != GNUNET_APPLICATION_TYPE_END; t++, num++);
755 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "I can handle %d app-types.\n", num);
757 handle->hello_message_size = sizeof(uint16_t) + /* For the number of types */
758 num * sizeof(GNUNET_MESH_ApplicationType); /* For the types */
760 uint16_t *nums = GNUNET_malloc(handle->hello_message_size);
761 GNUNET_MESH_ApplicationType *types = (GNUNET_MESH_ApplicationType*)(nums + 1);
766 for (i = 0; i < num; i++)
768 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "I can handle the app-type %d\n", stypes[i]);
769 types[i] = htons(stypes[i]);
772 handle->hello_message = nums;
776 struct GNUNET_MESH_Handle *
777 GNUNET_MESH_connect (const struct
778 GNUNET_CONFIGURATION_Handle
780 GNUNET_MESH_TunnelEndHandler
782 const struct GNUNET_MESH_MessageHandler *handlers,
783 const GNUNET_MESH_ApplicationType *stypes)
785 struct GNUNET_MESH_Handle *ret =
786 GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
788 ret->connected_to_core = GNUNET_NO;
789 ret->connected_peers.head = NULL;
790 ret->connected_peers.tail = NULL;
791 ret->cleaner = cleaner;
794 const struct GNUNET_MESH_MessageHandler *it;
795 unsigned int len = 1;
796 for (it = handlers; it->callback != NULL; it++)
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));
807 build_hello_message(ret, stypes);
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},
815 ret->core = GNUNET_CORE_connect (cfg,
823 GNUNET_NO, NULL, GNUNET_NO, core_handlers);
828 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
830 GNUNET_free (handle->handlers);
831 GNUNET_free (handle->hello_message);
832 GNUNET_CORE_disconnect (handle->core);
834 struct peer_list_element *element = handle->connected_peers.head;
835 while (element != NULL)
837 struct peer_list_element *next = element->next;
838 while (element->type_head != NULL)
840 struct type_list_element* tail = element->type_tail;
841 GNUNET_CONTAINER_DLL_remove(element->type_head, element->type_tail, tail);
844 GNUNET_free (element);
848 struct tunnel_list_element *tunnel = handle->pending_tunnels.head;
849 while (tunnel != NULL)
851 struct tunnel_list_element *next = tunnel->next;
852 GNUNET_free (tunnel);
855 tunnel = handle->established_tunnels.head;;
856 while (tunnel != NULL)
858 struct tunnel_list_element *next = tunnel->next;
859 GNUNET_free (tunnel);
863 GNUNET_free (handle);
866 /* end of mesh_api.c */