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 /* The context of the receive-function. */
80 struct tunnel_list_element
82 struct GNUNET_MESH_Tunnel tunnel;
83 struct tunnel_list_element *next, *prev;
88 struct tunnel_list_element *head, *tail;
91 struct peer_list_element
93 struct GNUNET_PeerIdentity peer;
95 /* how many Message-Types can this peer receive */
96 unsigned int num_types;
98 /* array of message-types */
99 GNUNET_MESH_ApplicationType *types;
101 struct GNUNET_TRANSPORT_ATS_Information atsi;
102 struct peer_list_element *next, *prev;
107 struct peer_list_element *head, *tail;
110 struct GNUNET_MESH_Handle
112 struct GNUNET_CORE_Handle *core;
113 struct GNUNET_MESH_MessageHandler *handlers;
114 struct GNUNET_PeerIdentity myself;
115 unsigned int connected_to_core;
116 struct peer_list connected_peers;
117 struct tunnel_list established_tunnels;
118 struct tunnel_list pending_tunnels;
119 struct tunnel_list pending_by_type_tunnels;
121 GNUNET_MESH_TunnelEndHandler *cleaner;
122 size_t hello_message_size;
123 uint16_t *hello_message;
127 send_end_connect(void* cls,
128 const struct GNUNET_SCHEDULER_TaskContext* tc)
130 struct GNUNET_MESH_Tunnel* tunnel = cls;
132 tunnel->connect_handler(tunnel->handler_cls, NULL, NULL);
136 send_self_connect(void* cls,
137 const struct GNUNET_SCHEDULER_TaskContext* tc)
139 struct GNUNET_MESH_Tunnel* tunnel = cls;
141 tunnel->connect_handler(tunnel->handler_cls, &tunnel->handle->myself, NULL);
142 GNUNET_SCHEDULER_add_now(send_end_connect, tunnel);
146 call_connect_handler (void *cls,
147 const struct GNUNET_SCHEDULER_TaskContext *tc)
149 struct GNUNET_MESH_Tunnel *tunnel = cls;
151 tunnel->connect_handler (tunnel->handler_cls, &tunnel->peer,
153 GNUNET_SCHEDULER_add_now (send_end_connect, tunnel);
157 core_startup (void *cls,
158 struct GNUNET_CORE_Handle *core,
159 const struct GNUNET_PeerIdentity *my_identity,
160 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
162 struct GNUNET_MESH_Handle *handle = cls;
163 memcpy (&handle->myself, my_identity, sizeof (struct GNUNET_PeerIdentity));
164 handle->connected_to_core = GNUNET_YES;
168 send_hello_message (void *cls, size_t size, void *buf)
170 if (cls == NULL) return 0;
171 struct GNUNET_MESH_Handle *handle = cls;
172 struct GNUNET_MessageHeader *hdr = buf;
174 size_t sent = sizeof(struct GNUNET_MessageHeader) + handle->hello_message_size;
176 hdr->type = htons(GNUNET_MESSAGE_TYPE_MESH_HELLO);
177 hdr->size = htons(size);
179 memcpy(hdr+1, handle->hello_message, handle->hello_message_size);
185 * Core calls this if we are connected to a new peer.
187 * The peer is added to the connected_peers-list.
191 core_connect (void *cls,
192 const struct GNUNET_PeerIdentity *peer,
193 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
195 struct GNUNET_MESH_Handle *handle = cls;
197 /* Send a hello to this peer */
198 GNUNET_CORE_notify_transmit_ready(handle->core,
201 GNUNET_TIME_UNIT_SECONDS,
203 sizeof(struct GNUNET_MessageHeader) + handle->hello_message_size,
207 /* put the new peer into the list of connected peers */
208 struct peer_list_element *element =
209 GNUNET_malloc (sizeof (struct peer_list_element));
210 memcpy (&element->peer, peer, sizeof (struct GNUNET_PeerIdentity));
213 memcpy (&element->atsi, atsi,
214 sizeof (struct GNUNET_TRANSPORT_ATS_Information));
216 GNUNET_CONTAINER_DLL_insert_after (handle->connected_peers.head,
217 handle->connected_peers.tail,
218 handle->connected_peers.tail, element);
220 struct tunnel_list_element *tunnel = handle->pending_tunnels.head;
221 while (tunnel != NULL)
224 memcmp (&tunnel->tunnel.peer, peer,
225 sizeof (struct GNUNET_PeerIdentity)))
227 struct tunnel_list_element *next = tunnel->next;
228 GNUNET_CONTAINER_DLL_remove (handle->pending_tunnels.head,
229 handle->pending_tunnels.tail, tunnel);
230 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
231 handle->established_tunnels.tail,
232 handle->established_tunnels.tail,
234 tunnel->tunnel.connect_handler (tunnel->tunnel.handler_cls,
236 GNUNET_SCHEDULER_add_now(send_end_connect, tunnel);
240 tunnel = tunnel->next;
245 * Core calls this if we disconnect a peer
247 * Remove this peer from the list of connected peers
248 * Close all tunnels this peer belongs to
251 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
253 struct GNUNET_MESH_Handle *handle = cls;
255 struct peer_list_element *element = handle->connected_peers.head;
256 while (element != NULL)
259 memcmp (&element->peer, peer, sizeof (struct GNUNET_PeerIdentity)))
261 element = element->next;
265 GNUNET_CONTAINER_DLL_remove (handle->connected_peers.head,
266 handle->connected_peers.tail, element);
267 GNUNET_free_non_null(element->types);
268 GNUNET_free (element);
271 struct tunnel_list_element *telement = handle->established_tunnels.head;
272 while (telement != NULL)
275 memcmp (&telement->tunnel.peer, peer,
276 sizeof (struct GNUNET_PeerIdentity)))
278 /* disconnect tunnels */
279 /* outbound tunnels */
280 if (telement->tunnel.connect_handler != NULL && NULL != telement->tunnel.disconnect_handler)
281 telement->tunnel.disconnect_handler (telement->tunnel.handler_cls,
283 /* inbound tunnels */
284 else if (NULL != handle->cleaner)
285 handle->cleaner (handle->cls, &telement->tunnel,
286 &telement->tunnel.ctx);
288 struct tunnel_list_element *next = telement->next;
289 GNUNET_CONTAINER_DLL_remove (handle->established_tunnels.head,
290 handle->established_tunnels.tail,
292 GNUNET_free (telement);
297 telement = telement->next;
303 * Receive a message from core.
304 * This is a hello-message, containing the application-types the other peer can receive
307 receive_hello (void *cls,
308 const struct GNUNET_PeerIdentity *other,
309 const struct GNUNET_MessageHeader *message,
310 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
312 struct GNUNET_MESH_Handle *handle = cls;
313 uint16_t *num = (uint16_t *) (message + 1);
314 uint16_t *ports = num + 1;
317 struct peer_list_element *element = handle->connected_peers.head;
318 while (element != NULL)
321 memcmp (&element->peer, other, sizeof (struct GNUNET_PeerIdentity)))
323 element = element->next;
326 /* TODO: add, not replace! */
327 element->num_types = *num;
328 element->types = GNUNET_malloc (*num * sizeof (GNUNET_MESH_ApplicationType));
330 for (i = 0; i < *num; i++)
331 element->types[i] = (GNUNET_MESH_ApplicationType)ntohs (ports[i]);
333 struct tunnel_list_element *tunnel = handle->pending_by_type_tunnels.head;
334 while (tunnel != NULL)
336 struct tunnel_list_element *next = tunnel->next;
337 for (i = 0; i < *num; i++)
339 if (ntohs (ports[i]) == tunnel->tunnel.application_type)
341 GNUNET_CONTAINER_DLL_remove (handle->pending_tunnels.head,
342 handle->pending_tunnels.tail,
344 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.
346 handle->established_tunnels.
348 handle->established_tunnels.
350 tunnel->tunnel.connect_handler (tunnel->tunnel.handler_cls,
351 &tunnel->tunnel.peer, atsi);
352 GNUNET_SCHEDULER_add_now (send_end_connect, tunnel);
356 if (ntohs (ports[i]) == tunnel->tunnel.application_type)
359 tunnel = tunnel->next;
365 * Receive a message from core.
368 core_receive (void *cls,
369 const struct GNUNET_PeerIdentity *other,
370 const struct GNUNET_MessageHeader *message,
371 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
373 struct GNUNET_MESH_Handle *handle = cls;
374 struct tunnel_message *tmessage = (struct tunnel_message *) message;
375 struct GNUNET_MessageHeader *rmessage =
376 (struct GNUNET_MessageHeader *) (tmessage + 1);
378 struct GNUNET_MESH_MessageHandler *handler;
380 for (handler = handle->handlers; handler->callback != NULL; handler++)
382 if ( (ntohs (rmessage->type) == handler->type)
383 && ( (handler->expected_size == 0)
384 || (handler->expected_size == ntohs (rmessage->size))) )
390 /* handler->callback handles this message */
392 /* If no handler was found, drop the message but keep the channel open */
393 if (handler->callback == NULL)
396 struct tunnel_list_element *tunnel = handle->established_tunnels.head;
398 while (tunnel != NULL)
400 if (tunnel->tunnel.id.id == tmessage->id.id &&
402 memcmp (&tmessage->id.initiator, &tunnel->tunnel.id.initiator,
403 sizeof (struct GNUNET_PeerIdentity)))
405 memcmp (&tmessage->id.target, &tunnel->tunnel.id.target,
406 sizeof (struct GNUNET_PeerIdentity))))
408 tunnel = tunnel->next;
411 /* if no tunnel was found: create a new inbound tunnel */
414 tunnel = GNUNET_malloc (sizeof (struct tunnel_list_element));
415 tunnel->tunnel.connect_handler = NULL;
416 tunnel->tunnel.disconnect_handler = NULL;
417 tunnel->tunnel.handler_cls = NULL;
418 tunnel->tunnel.ctx = NULL;
419 tunnel->tunnel.handle = handle;
420 memcpy (&tunnel->tunnel.peer, other,
421 sizeof (struct GNUNET_PeerIdentity));
422 memcpy (&tunnel->tunnel.id, &tmessage->id, sizeof (struct tunnel_id));
424 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
425 handle->established_tunnels.tail,
426 handle->established_tunnels.tail,
430 return handler->callback (handle->cls, &tunnel->tunnel,
431 &tunnel->tunnel.ctx, other, rmessage, atsi);
434 struct GNUNET_MESH_Tunnel *
435 GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Handle *handle,
436 struct GNUNET_TIME_Relative timeout,
437 GNUNET_MESH_ApplicationType application_type,
438 GNUNET_MESH_TunnelConnectHandler
440 GNUNET_MESH_TunnelDisconnectHandler
444 /* Look in the list of connected peers */
445 struct peer_list_element *element = handle->connected_peers.head;
446 while (element != NULL)
449 for (i = 0; i < element->num_types; i++)
450 if (application_type == element->types[i])
451 return GNUNET_MESH_peer_request_connect_all (handle, timeout, 1,
456 element = element->next;
459 /* Put into pending list */
460 struct tunnel_list_element *tunnel =
461 GNUNET_malloc (sizeof (struct tunnel_list_element));
463 tunnel->tunnel.connect_handler = connect_handler;
464 tunnel->tunnel.disconnect_handler = disconnect_handler;
465 tunnel->tunnel.handler_cls = handler_cls;
466 tunnel->tunnel.ctx = NULL;
467 tunnel->tunnel.handle = handle;
468 memcpy (&tunnel->tunnel.id.initiator, &handle->myself,
469 sizeof (struct GNUNET_PeerIdentity));
470 tunnel->tunnel.id.id = current_id++;
471 tunnel->tunnel.application_type = application_type;
473 GNUNET_CONTAINER_DLL_insert_after (handle->pending_by_type_tunnels.head,
474 handle->pending_by_type_tunnels.tail,
475 handle->pending_by_type_tunnels.tail,
477 return &tunnel->tunnel;
482 struct GNUNET_MESH_Tunnel *
483 GNUNET_MESH_peer_request_connect_all (struct GNUNET_MESH_Handle *handle,
484 struct GNUNET_TIME_Relative timeout,
485 unsigned int num_peers,
486 const struct GNUNET_PeerIdentity *peers,
487 GNUNET_MESH_TunnelConnectHandler
489 GNUNET_MESH_TunnelDisconnectHandler
490 disconnect_handler, void *handler_cls)
495 struct tunnel_list_element *tunnel =
496 GNUNET_malloc (sizeof (struct tunnel_list_element));
498 tunnel->tunnel.connect_handler = connect_handler;
499 tunnel->tunnel.disconnect_handler = disconnect_handler;
500 tunnel->tunnel.handler_cls = handler_cls;
501 tunnel->tunnel.ctx = NULL;
502 tunnel->tunnel.handle = handle;
503 memcpy (&tunnel->tunnel.id.initiator, &handle->myself,
504 sizeof (struct GNUNET_PeerIdentity));
505 memcpy (&tunnel->tunnel.id.target, peers,
506 sizeof (struct GNUNET_PeerIdentity));
507 tunnel->tunnel.id.id = current_id++;
508 memcpy (&tunnel->tunnel.peer, peers, sizeof(struct GNUNET_PeerIdentity));
510 struct peer_list_element *element = handle->connected_peers.head;
511 while (element != NULL)
514 memcmp (&element->peer, peers, sizeof (struct GNUNET_PeerIdentity)))
516 element = element->next;
521 /* we are connected to this peer */
522 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
523 handle->established_tunnels.tail,
524 handle->established_tunnels.tail,
526 GNUNET_SCHEDULER_add_now(call_connect_handler, tunnel);
529 memcmp (peers, &handle->myself,
530 sizeof (struct GNUNET_PeerIdentity)))
532 /* we are the peer */
533 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
534 handle->established_tunnels.tail,
535 handle->established_tunnels.tail,
537 GNUNET_SCHEDULER_add_now(send_self_connect, tunnel);
541 /* we are not connected to this peer */
542 GNUNET_CONTAINER_DLL_insert_after (handle->pending_tunnels.head,
543 handle->pending_tunnels.tail,
544 handle->pending_tunnels.tail,
546 (void) GNUNET_CORE_peer_request_connect (handle->core,
551 return &tunnel->tunnel;
554 const struct GNUNET_PeerIdentity*
555 GNUNET_MESH_get_peer(const struct GNUNET_MESH_Tunnel* tunnel)
557 return &tunnel->peer;
561 core_notify(void* cls, size_t size, void* buf)
563 struct notify_cls *ncls = cls;
564 struct GNUNET_MESH_Tunnel *tunnel = ncls->tunnel;
565 struct tunnel_message* message = buf;
566 void* cbuf = (void*) &message[1];
567 GNUNET_assert(NULL != ncls->notify);
569 size_t sent = ncls->notify(ncls->notify_cls, size - sizeof(struct tunnel_message), cbuf);
573 if (0 == sent) return 0;
575 sent += sizeof(struct tunnel_message);
577 message->hdr.type = htons(GNUNET_MESSAGE_TYPE_MESH);
578 message->hdr.size = htons(sent);
579 memcpy(&message->id, &tunnel->id, sizeof(struct tunnel_id));
585 * Ask the mesh to call "notify" once it is ready to transmit the
586 * given number of bytes to the specified "target". If we are not yet
587 * connected to the specified peer, a call to this function will cause
588 * us to try to establish a connection.
590 * @param tunnel tunnel to use for transmission
591 * @param cork is corking allowed for this transmission?
592 * @param priority how important is the message?
593 * @param maxdelay how long can the message wait?
594 * @param target destination for the message, NULL for multicast to all tunnel targets
595 * @param notify_size how many bytes of buffer space does notify want?
596 * @param notify function to call when buffer space is available;
597 * will be called with NULL on timeout or if the overall queue
598 * for this peer is larger than queue_size and this is currently
599 * the message with the lowest priority
600 * @param notify_cls closure for notify
601 * @return non-NULL if the notify callback was queued,
602 * NULL if we can not even queue the request (insufficient
603 * memory); if NULL is returned, "notify" will NOT be called.
605 struct GNUNET_MESH_TransmitHandle *
606 GNUNET_MESH_notify_transmit_ready (struct
614 const struct GNUNET_PeerIdentity *target,
617 GNUNET_CONNECTION_TransmitReadyNotify
618 notify, void *notify_cls)
620 struct notify_cls *cls = GNUNET_malloc(sizeof(struct notify_cls));
621 cls->notify_cls = notify_cls;
622 GNUNET_assert(NULL != notify);
623 cls->notify = notify;
624 cls->tunnel = tunnel;
625 GNUNET_CORE_notify_transmit_ready(tunnel->handle->core,
630 notify_size + sizeof(struct tunnel_message),
634 /* aborting is not implemented yet */
635 return (struct GNUNET_MESH_TransmitHandle*) 1;
638 void build_hello_message(struct GNUNET_MESH_Handle* handle,
639 const GNUNET_MESH_ApplicationType *stypes)
642 const GNUNET_MESH_ApplicationType *t;
644 for (t = stypes; *t != GNUNET_APPLICATION_TYPE_END; t++, num++);
646 handle->hello_message_size = sizeof(uint16_t) + /* For the number of types */
647 num * sizeof(GNUNET_MESH_ApplicationType); /* For the types */
649 uint16_t *nums = GNUNET_malloc(handle->hello_message_size);
650 GNUNET_MESH_ApplicationType *types = (GNUNET_MESH_ApplicationType*)(nums + 1);
653 memcpy(types, stypes, num*sizeof(GNUNET_MESH_ApplicationType));
655 handle->hello_message = nums;
659 struct GNUNET_MESH_Handle *
660 GNUNET_MESH_connect (const struct
661 GNUNET_CONFIGURATION_Handle
663 GNUNET_MESH_TunnelEndHandler
665 const struct GNUNET_MESH_MessageHandler *handlers,
666 const GNUNET_MESH_ApplicationType *stypes)
668 struct GNUNET_MESH_Handle *ret =
669 GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
671 ret->connected_to_core = GNUNET_NO;
672 ret->connected_peers.head = NULL;
673 ret->connected_peers.tail = NULL;
674 ret->cleaner = cleaner;
677 const struct GNUNET_MESH_MessageHandler *it;
678 unsigned int len = 1;
679 for (it = handlers; it->callback != NULL; it++)
685 GNUNET_malloc (len * sizeof (struct GNUNET_MESH_MessageHandler));
686 memset(ret->handlers, 0, len * sizeof(struct GNUNET_MESH_MessageHandler));
687 memcpy (ret->handlers, handlers,
688 len * sizeof (struct GNUNET_MESH_MessageHandler));
690 build_hello_message(ret, stypes);
692 const static struct GNUNET_CORE_MessageHandler core_handlers[] = {
693 {&core_receive, GNUNET_MESSAGE_TYPE_MESH, 0},
694 {&receive_hello, GNUNET_MESSAGE_TYPE_MESH_HELLO, 0},
698 ret->core = GNUNET_CORE_connect (cfg,
706 GNUNET_NO, NULL, GNUNET_NO, core_handlers);
711 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
713 GNUNET_free (handle->handlers);
714 GNUNET_free (handle->hello_message);
715 GNUNET_CORE_disconnect (handle->core);
717 struct peer_list_element *element = handle->connected_peers.head;
718 while (element != NULL)
720 struct peer_list_element *next = element->next;
721 GNUNET_free_non_null(element->types);
722 GNUNET_free (element);
726 struct tunnel_list_element *tunnel = handle->pending_tunnels.head;
727 while (tunnel != NULL)
729 struct tunnel_list_element *next = tunnel->next;
730 GNUNET_free (tunnel);
733 tunnel = handle->established_tunnels.head;;
734 while (tunnel != NULL)
736 struct tunnel_list_element *next = tunnel->next;
737 GNUNET_free (tunnel);
741 GNUNET_free (handle);
744 /* end of mesh_api.c */