2 This file is part of GNUnet.
3 (C) 2001 - 2011 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/gnunet-service-mesh.c
23 * @brief GNUnet MESH service
24 * @author Bartlomiej Polot
30 * - PERIODIC FUNCTIONS
31 * - MESH NETWORK HANDLER HELPERS
32 * - MESH NETWORK HANDLES
33 * - MESH LOCAL HANDLER HELPERS
34 * - MESH LOCAL HANDLES
35 * - MAIN FUNCTIONS (main & run)
38 * - error reporting (CREATE/CHANGE/ADD/DEL?) -- new message!
39 * - partial disconnect reporting -- same as error reporting?
40 * - add vs create? change vs. keep-alive? same msg or different ones? -- thinking...
41 * - speed requirement specification (change?) in mesh API -- API call
43 * - add connection confirmation message
44 * - handle trnsmt_rdy return values
49 #include "mesh_protocol.h"
50 #include "gnunet_dht_service.h"
51 #include "mesh_tunnel_tree.h"
53 /* TODO: move into configuration file */
54 #define REFRESH_PATH_TIME GNUNET_TIME_relative_multiply(\
55 GNUNET_TIME_UNIT_SECONDS,\
57 #define APP_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
58 GNUNET_TIME_UNIT_SECONDS,\
61 #define ID_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
62 GNUNET_TIME_UNIT_SECONDS,\
67 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
68 * @param cls string to print
69 * @param tc task context
72 mesh_debug (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
76 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
80 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n", s);
85 /******************************************************************************/
86 /*********************** GLOBAL VARIABLES ****************************/
87 /******************************************************************************/
92 static struct MeshClient *clients;
93 static struct MeshClient *clients_tail;
96 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
98 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
101 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
103 static struct GNUNET_CONTAINER_MultiHashMap *peers;
106 * Handle to communicate with core
108 static struct GNUNET_CORE_Handle *core_handle;
113 static struct GNUNET_DHT_Handle *dht_handle;
118 static struct GNUNET_SERVER_Handle *server_handle;
121 * Notification context, to send messages to local clients
123 static struct GNUNET_SERVER_NotificationContext *nc;
126 * Local peer own ID (memory efficient handle)
128 static GNUNET_PEER_Id myid;
131 * Local peer own ID (full value)
133 static struct GNUNET_PeerIdentity my_full_id;
138 static struct GNUNET_CRYPTO_RsaPrivateKey* my_private_key;
143 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
146 * Tunnel ID for the next created tunnel (global tunnel number)
148 static MESH_TunnelNumber next_tid;
151 * All application types provided by this peer
153 static struct GNUNET_CONTAINER_MultiHashMap *applications;
156 * All message types clients of this peer are interested in
158 static struct GNUNET_CONTAINER_MultiHashMap *types;
161 * Task to periodically announce provided applications
163 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
166 * Task to periodically announce itself in the network
168 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
171 unsigned int next_client_id;
175 /******************************************************************************/
176 /************************ ITERATORS ****************************/
177 /******************************************************************************/
181 * Iterator over hash map peer entries collect all neighbors who to resend the
184 * @param cls closure (**GNUNET_PEER_Id to store hops to send packet)
185 * @param key current key code (peer id hash)
186 * @param value value in the hash map (peer_info)
187 * @return GNUNET_YES if we should continue to iterate, GNUNET_NO if not.
190 iterate_collect_neighbors (void *cls, const GNUNET_HashCode * key, void *value)
192 struct MeshPeerInfo *peer_info = value;
193 struct MeshPathInfo *neighbors = cls;
194 struct GNUNET_PeerIdentity *id;
195 GNUNET_PEER_Id peer_id;
198 if (peer_info->id == myid)
202 id = path_get_first_hop (neighbors->t, peer_info);
203 peer_id = GNUNET_PEER_search(id);
204 for (i = 0; i < neighbors->path->length; i++)
206 if (neighbors->path->peers[i] == peer_id)
209 GNUNET_array_append (neighbors->path->peers, neighbors->path->length,
217 * Iterator over hash map peer entries and frees all data in it.
218 * Used prior to destroying a hashmap. Makes you miss anonymous functions in C.
221 * @param key current key code (will no longer contain valid data!!)
222 * @param value value in the hash map (treated as void *)
223 * @return GNUNET_YES if we should continue to iterate, GNUNET_NO if not.
226 iterate_free (void *cls, const GNUNET_HashCode * key, void *value)
233 /******************************************************************************/
234 /************************ PERIODIC FUNCTIONS ****************************/
235 /******************************************************************************/
238 * Announce iterator over for each application provided by the peer
241 * @param key current key code
242 * @param value value in the hash map
243 * @return GNUNET_YES if we should continue to
248 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
250 /* FIXME are hashes in multihash map equal on all aquitectures? */
251 GNUNET_DHT_put (dht_handle, key, 10U, GNUNET_DHT_RO_RECORD_ROUTE,
252 GNUNET_BLOCK_TYPE_TEST, sizeof (struct GNUNET_PeerIdentity),
253 (const char *) &my_full_id,
255 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
256 &mesh_debug, "DHT_put for app completed");
258 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
260 APP_ANNOUNCE_TIME, NULL, NULL);
267 * Periodically announce what applications are provided by local clients
270 * @param tc task context
273 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
275 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
277 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Starting PUT for apps\n");
281 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
283 announce_applications_task =
284 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Finished PUT for apps\n");
292 * Periodically announce self id in the DHT
295 * @param tc task context
298 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
300 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
302 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
306 * - Set data expiration in function of X
309 GNUNET_DHT_put (dht_handle, /* DHT handle */
310 &my_full_id.hashPubKey, /* Key to use */
311 10U, /* Replication level */
312 GNUNET_DHT_RO_RECORD_ROUTE, /* DHT options */
313 GNUNET_BLOCK_TYPE_TEST, /* Block type */
314 0, /* Size of the data */
315 NULL, /* Data itself */
316 GNUNET_TIME_absolute_get_forever (), /* Data expiration */
317 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
319 &mesh_debug, "DHT_put for id completed");
321 NULL, /* Continuation */
322 NULL); /* Continuation closure */
325 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
329 /******************************************************************************/
330 /****************** GENERAL HELPER FUNCTIONS ************************/
331 /******************************************************************************/
334 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
335 * and insert it in the appropiate structures if the peer is not known yet.
337 * @param peer Identity of the peer
339 * @return Existing or newly created peer info
341 static struct MeshPeerInfo *
342 peer_info_get (const struct GNUNET_PeerIdentity *peer)
344 struct MeshPeerInfo *peer_info;
346 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
347 if (NULL == peer_info)
350 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
351 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
352 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
353 peer_info->id = GNUNET_PEER_intern (peer);
362 * Destroy the peer_info and free any allocated resources linked to it
363 * @param t tunnel the path belongs to
364 * @param pi the peer_info to destroy
365 * @return GNUNET_OK on success
368 peer_info_destroy (struct MeshPeerInfo *pi)
370 GNUNET_HashCode hash;
371 struct GNUNET_PeerIdentity id;
373 GNUNET_PEER_resolve (pi->id, &id);
374 GNUNET_PEER_change_rc (pi->id, -1);
375 GNUNET_CRYPTO_hash (&id, sizeof (struct GNUNET_PeerIdentity), &hash);
377 GNUNET_CONTAINER_multihashmap_remove (peers, &hash, pi);
378 GNUNET_SCHEDULER_cancel (pi->path_refresh_task);
386 * Notify a tunnel that a connection has broken that affects at least
389 * @param t Tunnel affected
390 * @param peer Peer that (at least) has been affected by the disconnection
391 * @param p1 Peer that got disconnected from p2
392 * @param p2 Peer that got disconnected from p1
395 tunnel_notify_connection_broken (struct MeshTunnel *t,
396 struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
401 * Remove all paths that rely on a direct connection between p1 and p2
402 * from the peer itself and notify all tunnels about it.
404 * @param pi PeerInfo of affected peer
405 * @param p1 GNUNET_PEER_Id of one peer.
406 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
410 path_remove_from_peer (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
413 struct MeshPeerPath *p;
414 struct MeshPeerPath *aux;
415 unsigned int destroyed;
423 for (i = 0; i < (p->length - 1); i++)
425 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
426 (p->peers[i] == p2 && p->peers[i + 1] == p1))
438 for (i = 0; i < peer->ntunnels; i++)
440 tunnel_notify_connection_broken (peer->tunnels[i], peer, p1, p2);
446 * Add the path to the origin peer and update the path used to reach it in case
447 * this is the shortest.
448 * The path is given in peer_info -> destination, therefore we turn the path
451 * @param peer_info Peer to add the path to, being the origin of the path.
452 * @param path New path to add after being inversed.
455 path_add_to_origin (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
458 path_add_to_peer (peer_info, path);
463 * Build a PeerPath from the paths returned from the DHT, reversing the paths
464 * to obtain a local peer -> destination path and interning the peer ids.
466 * @param get_path NULL-terminated array of pointers
467 * to the peers on reverse GET path (or NULL if not recorded)
468 * @param put_path NULL-terminated array of pointers
469 * to the peers on the PUT path (or NULL if not recorded)
471 * @return Newly allocated and created path
473 static struct MeshPeerPath *
474 path_build_from_dht (const struct GNUNET_PeerIdentity *const *get_path,
475 const struct GNUNET_PeerIdentity *const *put_path)
477 struct MeshPeerPath *p;
481 p = GNUNET_malloc (sizeof (struct MeshPeerPath));
482 for (i = 0; get_path[i] != NULL; i++) ;
483 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: GET has %d hops.\n", i);
484 for (i--; i >= 0; i--)
486 id = GNUNET_PEER_intern (get_path[i]);
487 if (p->length > 0 && id == p->peers[p->length - 1])
489 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
490 GNUNET_PEER_change_rc(id, -1);
495 GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
496 p->peers[p->length] = id;
500 for (i = 0; put_path[i] != NULL; i++) ;
501 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: PUT has %d hops.\n", i);
502 for (i--; i >= 0; i--)
504 id = GNUNET_PEER_intern (put_path[i]);
505 if (p->length > 0 && id == p->peers[p->length - 1])
507 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
508 GNUNET_PEER_change_rc(id, -1);
513 GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
514 p->peers[p->length] = id;
519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
520 "MESH: (first of GET: %s)\n",
521 GNUNET_h2s_full(&get_path[0]->hashPubKey));
522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
523 "MESH: (first of PUT: %s)\n",
524 GNUNET_h2s_full(&put_path[0]->hashPubKey));
525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
526 "MESH: In total: %d hops\n",
534 * Check if client has registered with the service and has not disconnected
536 * @param client the client to check
538 * @return non-NULL if client exists in the global DLL
540 static struct MeshClient *
541 client_get (struct GNUNET_SERVER_Client *client)
543 struct MeshClient *c;
548 if (c->handle == client)
557 * Checks if a given client has subscribed to certain message type
559 * @param message_type Type of message to check
560 * @param c Client to check
562 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
567 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
571 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
572 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
577 * Search for a tunnel among the tunnels for a client
579 * @param c the client whose tunnels to search in
580 * @param tid the local id of the tunnel
582 * @return tunnel handler, NULL if doesn't exist
584 static struct MeshTunnel *
585 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
587 GNUNET_HashCode hash;
589 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
590 return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
595 * Search for a tunnel by global ID using PEER_ID
597 * @param pi owner of the tunnel
598 * @param tid global tunnel number
600 * @return tunnel handler, NULL if doesn't exist
602 static struct MeshTunnel *
603 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
605 struct MESH_TunnelID id;
606 GNUNET_HashCode hash;
611 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
612 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
617 * Search for a tunnel by global ID using full PeerIdentities
619 * @param oid owner of the tunnel
620 * @param tid global tunnel number
622 * @return tunnel handler, NULL if doesn't exist
624 static struct MeshTunnel *
625 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
627 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
632 * Notify a tunnel that a connection has broken that affects at least
635 * @param t Tunnel affected
636 * @param peer Peer that (at least) has been affected by the disconnection
637 * @param p1 Peer that got disconnected from p2
638 * @param p2 Peer that got disconnected from p1
643 tunnel_notify_connection_broken (struct MeshTunnel *t,
644 struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
651 * Recursively destory the path tree of a tunnel.
652 * Note: it does not liberate memory for itself, parent must do it!
654 * @param n The node to destroy, along with children.
656 * @return GNUNET_OK on success
659 tunnel_destroy_tree_node (struct MeshTunnelPathNode *n)
663 for (i = 0; i < n->nchildren; i++)
665 tunnel_destroy_tree_node(&n->children[i]);
667 if (NULL != n->children)
668 GNUNET_free (n->children);
673 * Destroy the tunnel and free any allocated resources linked to it
675 * @param t the tunnel to destroy
677 * @return GNUNET_OK on success
680 tunnel_destroy (struct MeshTunnel *t)
682 struct MeshClient *c;
684 struct MeshQueue *qn;
685 GNUNET_HashCode hash;
688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DESTROYING TUNNEL at %p\n", t);
694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
697 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
698 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
703 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
704 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
708 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
713 GNUNET_free (q->data);
717 /* TODO cancel core transmit ready in case it was active */
720 GNUNET_CONTAINER_multihashmap_iterate(t->tree->first_hops, &iterate_free, t);
721 GNUNET_CONTAINER_multihashmap_destroy(t->tree->first_hops);
722 tunnel_destroy_tree_node(t->tree->root);
723 GNUNET_free(t->tree->root);
724 GNUNET_free (t->tree);
731 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
732 * client when the client disconnects.
734 * @param cls closure (client that is disconnecting)
735 * @param key the hash of the local tunnel id (used to access the hashmap)
736 * @param value the value stored at the key (tunnel to destroy)
738 * @return GNUNET_OK on success
741 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
745 r = tunnel_destroy ((struct MeshTunnel *) value);
750 /******************************************************************************/
751 /**************** MESH NETWORK HANDLER HELPERS ***********************/
752 /******************************************************************************/
756 * Function called to notify a client about the socket
757 * being ready to queue more data. "buf" will be
758 * NULL and "size" zero if the socket was closed for
759 * writing in the meantime.
762 * @param size number of bytes available in buf
763 * @param buf where the callee should write the message
764 * @return number of bytes written to buf
767 send_core_create_path (void *cls, size_t size, void *buf)
769 struct MeshPathInfo *info = cls;
770 struct GNUNET_MESH_ManipulatePath *msg;
771 struct GNUNET_PeerIdentity *peer_ptr;
772 struct MeshPeerInfo *peer = info->peer;
773 struct MeshTunnel *t = info->t;
774 struct MeshPeerPath *p = info->path;
779 sizeof (struct GNUNET_MESH_ManipulatePath) +
780 p->length * sizeof (struct GNUNET_PeerIdentity);
782 if (size < size_needed || NULL == buf)
784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Retransmitting create path\n");
785 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
786 GNUNET_TIME_UNIT_FOREVER_REL,
787 path_get_first_hop (t, peer),
788 size_needed, &send_core_create_path,
793 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
794 msg->header.size = htons (size_needed);
795 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
796 msg->tid = ntohl (t->id.tid);
798 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
799 for (i = 0; i < p->length; i++)
801 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
813 * Function called to notify a client about the socket
814 * being ready to queue more data. "buf" will be
815 * NULL and "size" zero if the socket was closed for
816 * writing in the meantime.
818 * @param cls closure (MeshDataDescriptor with all info to build packet)
819 * @param size number of bytes available in buf
820 * @param buf where the callee should write the message
821 * @return number of bytes written to buf
824 send_core_data_to_origin (void *cls, size_t size, void *buf)
826 struct MeshDataDescriptor *info = cls;
827 struct GNUNET_MESH_ToOrigin *msg = buf;
830 GNUNET_assert (NULL != info);
831 total_size = sizeof (struct GNUNET_MESH_ToOrigin) + info->size;
832 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
834 if (total_size > size)
836 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
837 "not enough buffer to send data to origin\n");
840 msg->header.size = htons (total_size);
841 msg->header.type = htons (GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN);
842 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
843 msg->tid = htonl (info->origin->tid);
846 memcpy (&msg[1], &info[1], info->size);
848 if (NULL != info->client)
850 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
858 * Function called to notify a client about the socket
859 * being ready to queue more data. "buf" will be
860 * NULL and "size" zero if the socket was closed for
861 * writing in the meantime.
863 * @param cls closure (data itself)
864 * @param size number of bytes available in buf
865 * @param buf where the callee should write the message
866 * @return number of bytes written to buf
869 send_core_data_unicast (void *cls, size_t size, void *buf)
871 struct MeshDataDescriptor *info = cls;
872 struct GNUNET_MESH_Unicast *msg = buf;
875 GNUNET_assert (NULL != info);
876 total_size = sizeof (struct GNUNET_MESH_Unicast) + info->size;
877 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
879 if (total_size > size)
881 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
882 "not enough buffer to send data to peer\n");
885 msg->header.size = htons (total_size);
886 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_UNICAST);
887 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
888 GNUNET_PEER_resolve (info->destination, &msg->destination);
889 msg->tid = htonl (info->origin->tid);
892 memcpy (&msg[1], &info[1], info->size);
894 if (NULL != info->client)
896 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
904 * Function called to notify a client about the socket
905 * being ready to queue more data. "buf" will be
906 * NULL and "size" zero if the socket was closed for
907 * writing in the meantime.
909 * @param cls closure (data itself)
910 * @param size number of bytes available in buf
911 * @param buf where the callee should write the message
912 * @return number of bytes written to buf
915 send_core_data_multicast (void *cls, size_t size, void *buf)
917 struct MeshDataDescriptor *info = cls;
918 struct GNUNET_MESH_Multicast *msg = buf;
921 GNUNET_assert (NULL != info);
922 total_size = info->size + sizeof (struct GNUNET_MESH_Multicast);
923 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
927 info->peer->core_transmit[info->handler_n] = NULL;
929 if (total_size > size)
931 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
932 "not enough buffer to send data futher\n");
935 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
936 msg->header.size = htons (total_size);
937 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
938 msg->tid = htonl (info->origin->tid);
939 memcpy (&msg[1], &info[1], total_size);
940 if (0 == --info->copies)
942 if (NULL != info->client)
944 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
953 * Function called to notify a client about the socket
954 * being ready to queue more data. "buf" will be
955 * NULL and "size" zero if the socket was closed for
956 * writing in the meantime.
958 * @param cls closure (MeshDataDescriptor)
959 * @param size number of bytes available in buf
960 * @param buf where the callee should write the message
961 * @return number of bytes written to buf
964 send_core_path_ack (void *cls, size_t size, void *buf)
966 struct MeshDataDescriptor *info = cls;
967 struct GNUNET_MESH_PathACK *msg = buf;
969 GNUNET_assert (NULL != info);
972 info->peer->core_transmit[info->handler_n] = NULL;
974 if (sizeof (struct GNUNET_MESH_PathACK) > size)
979 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
980 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
981 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
982 msg->tid = htonl (info->origin->tid);
983 msg->peer_id = my_full_id;
985 /* TODO add signature */
987 return sizeof (struct GNUNET_MESH_PathACK);
992 * Function called to notify a client about the socket
993 * being ready to queue more data. "buf" will be
994 * NULL and "size" zero if the socket was closed for
995 * writing in the meantime.
997 * @param cls closure (data itself)
998 * @param size number of bytes available in buf
999 * @param buf where the callee should write the message
1000 * @return number of bytes written to buf
1003 send_core_data_raw (void *cls, size_t size, void *buf)
1005 struct GNUNET_MessageHeader *msg = cls;
1008 GNUNET_assert (NULL != msg);
1009 total_size = ntohs (msg->size);
1011 if (total_size > size)
1016 memcpy (buf, msg, total_size);
1024 * Send another peer a notification to destroy a tunnel
1025 * @param cls The tunnel to destroy
1026 * @param size Size in the buffer
1027 * @param buf Memory where to put the data to transmit
1028 * @return Size of data put in buffer
1031 send_p2p_tunnel_destroy (void *cls, size_t size, void *buf)
1033 struct MeshTunnel *t = cls;
1034 struct MeshClient *c;
1035 struct GNUNET_MESH_TunnelMessage *msg;
1039 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1040 /*FIXME*/ msg->header.size =
1041 htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1042 msg->tunnel_id = htonl (t->id.tid);
1044 tunnel_destroy (c, t);
1045 return sizeof (struct GNUNET_MESH_TunnelMessage);
1051 * Send the message to all clients that have subscribed to its type
1053 * @param msg Pointer to the message itself
1054 * @return number of clients this message was sent to
1057 send_subscribed_clients (struct GNUNET_MessageHeader *msg)
1059 struct MeshClient *c;
1063 type = ntohs (msg->type);
1064 for (count = 0, c = clients; c != NULL; c = c->next)
1066 if (client_is_subscribed (type, c))
1069 GNUNET_SERVER_notification_context_unicast (nc, c->handle, msg,
1079 * Notify the client that owns the tunnel that a peer has connected to it
1081 * @param t Tunnel whose owner to notify
1082 * @param id Short id of the peer that has connected
1085 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1087 struct GNUNET_MESH_PeerControl pc;
1089 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1090 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1091 pc.tunnel_id = htonl (t->local_tid);
1092 GNUNET_PEER_resolve (id, &pc.peer);
1093 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1094 &pc.header, GNUNET_NO);
1098 /******************************************************************************/
1099 /******************** MESH NETWORK HANDLERS **************************/
1100 /******************************************************************************/
1104 * Core handler for path creation
1105 * struct GNUNET_CORE_MessageHandler
1107 * @param cls closure
1108 * @param message message
1109 * @param peer peer identity this notification is about
1110 * @param atsi performance data
1111 * @return GNUNET_OK to keep the connection open,
1112 * GNUNET_SYSERR to close it (signal serious error)
1116 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1117 const struct GNUNET_MessageHeader *message,
1118 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1120 unsigned int own_pos;
1123 MESH_TunnelNumber tid;
1124 struct GNUNET_MESH_ManipulatePath *msg;
1125 struct GNUNET_PeerIdentity *pi;
1126 struct GNUNET_PeerIdentity id;
1127 GNUNET_HashCode hash;
1128 struct MeshPeerPath *path;
1129 struct MeshPeerInfo *dest_peer_info;
1130 struct MeshPeerInfo *orig_peer_info;
1131 struct MeshTunnel *t;
1133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1134 "MESH: Received a MESH path create msg\n");
1135 size = ntohs (message->size);
1136 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
1138 GNUNET_break_op (0);
1142 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
1143 if (size % sizeof (struct GNUNET_PeerIdentity))
1145 GNUNET_break_op (0);
1148 size /= sizeof (struct GNUNET_PeerIdentity);
1151 GNUNET_break_op (0);
1154 msg = (struct GNUNET_MESH_ManipulatePath *) message;
1156 tid = ntohl (msg->tid);
1157 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1158 t = tunnel_get (pi, tid);
1162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
1163 t = GNUNET_malloc (sizeof (struct MeshTunnel));
1164 t->id.oid = GNUNET_PEER_intern (pi);
1166 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
1168 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1170 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
1171 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1179 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
1180 if (NULL == dest_peer_info)
1182 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1183 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
1184 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
1186 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1188 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
1189 if (NULL == orig_peer_info)
1191 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1192 orig_peer_info->id = GNUNET_PEER_intern (pi);
1193 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
1194 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1197 path = GNUNET_malloc (sizeof (struct MeshPeerPath));
1198 path->length = size;
1199 path->peers = GNUNET_malloc (size * sizeof (GNUNET_PEER_Id));
1201 for (i = 0; i < size; i++)
1203 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
1204 if (path->peers[i] == myid)
1208 { /* cannot be self, must be 'not found' */
1209 /* create path: self not found in path through self */
1210 GNUNET_break_op (0);
1211 path_destroy (path);
1212 /* FIXME error. destroy tunnel? leave for timeout? */
1215 if (own_pos == size - 1)
1217 /* It is for us! Send ack. */
1218 struct MeshDataDescriptor *info;
1221 path_add_to_origin (orig_peer_info, path); /* inverts path! */
1222 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1223 info->origin = &t->id;
1224 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
1225 GNUNET_assert (info->peer);
1226 for (j = 0; info->peer->core_transmit[j]; j++)
1234 info->handler_n = j;
1235 info->peer->core_transmit[j] =
1236 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1237 GNUNET_TIME_UNIT_FOREVER_REL, peer,
1238 sizeof (struct GNUNET_MessageHeader),
1239 &send_core_path_ack, info);
1243 /* It's for somebody else! Retransmit. */
1244 struct MeshPathInfo *path_info;
1246 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1248 path_info->path = path;
1249 path_info->peer = dest_peer_info;
1251 path_add_to_peer (dest_peer_info, path);
1252 GNUNET_PEER_resolve (path->peers[own_pos + 1], &id);
1253 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1254 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1255 sizeof (struct GNUNET_MessageHeader),
1256 &send_core_create_path, path_info);
1263 * Core handler for mesh network traffic going from the origin to a peer
1265 * @param cls closure
1266 * @param peer peer identity this notification is about
1267 * @param message message
1268 * @param atsi performance data
1269 * @return GNUNET_OK to keep the connection open,
1270 * GNUNET_SYSERR to close it (signal serious error)
1273 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1274 const struct GNUNET_MessageHeader *message,
1275 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1277 struct GNUNET_MESH_Unicast *msg;
1278 struct MeshTunnel *t;
1279 struct MeshPeerInfo *pi;
1282 size = ntohs (message->size);
1284 sizeof (struct GNUNET_MESH_Unicast) +
1285 sizeof (struct GNUNET_MessageHeader))
1290 msg = (struct GNUNET_MESH_Unicast *) message;
1291 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1294 /* TODO notify back: we don't know this tunnel */
1297 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
1298 &msg->destination.hashPubKey);
1301 /* TODO maybe feedback, log to statistics */
1306 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
1309 msg = GNUNET_malloc (size);
1310 memcpy (msg, message, size);
1311 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1312 GNUNET_TIME_UNIT_FOREVER_REL,
1313 path_get_first_hop (t, pi), size,
1314 &send_core_data_raw, msg);
1320 * Core handler for mesh network traffic going from the origin to all peers
1322 * @param cls closure
1323 * @param message message
1324 * @param peer peer identity this notification is about
1325 * @param atsi performance data
1326 * @return GNUNET_OK to keep the connection open,
1327 * GNUNET_SYSERR to close it (signal serious error)
1330 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1331 const struct GNUNET_MessageHeader *message,
1332 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1334 struct GNUNET_MESH_Multicast *msg;
1335 struct GNUNET_PeerIdentity id;
1336 struct MeshDataDescriptor *info;
1337 struct MeshPathInfo neighbors;
1338 struct MeshTunnel *t;
1344 size = ntohs (message->size);
1346 sizeof (struct GNUNET_MESH_Multicast) +
1347 sizeof (struct GNUNET_MessageHeader))
1349 GNUNET_break_op (0);
1352 msg = (struct GNUNET_MESH_Multicast *) message;
1353 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1357 /* TODO notify that we dont know that tunnel */
1361 /* Transmit to locally interested clients */
1362 if (GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
1364 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
1367 /* Retransmit to other peers.
1368 * Using path here as just a collection of peers, not a path per se.
1371 neighbors.path = GNUNET_malloc (sizeof (struct MeshPeerPath));
1372 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &iterate_collect_neighbors,
1374 if (0 == neighbors.path->length)
1376 GNUNET_free (neighbors.path);
1379 size -= sizeof (struct GNUNET_MESH_Multicast);
1380 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor) + size);
1381 info->origin = &t->id;
1382 info->copies = neighbors.path->length;
1383 for (i = 0; i < info->copies; i++)
1385 GNUNET_PEER_resolve (neighbors.path->peers[i], &id);
1386 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
1387 GNUNET_assert (NULL != info->peer);
1388 for (j = 0; 0 != info->peer->core_transmit[j]; j++)
1390 if (j == (CORE_QUEUE_SIZE - 1))
1396 info->handler_n = j;
1397 info->peer->infos[j] = info;
1398 info->peer->core_transmit[j] =
1399 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1400 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1401 ntohs (msg->header.size),
1402 &send_core_data_multicast, info);
1404 GNUNET_free (neighbors.path->peers);
1405 GNUNET_free (neighbors.path);
1411 * Core handler for mesh network traffic
1413 * @param cls closure
1414 * @param message message
1415 * @param peer peer identity this notification is about
1416 * @param atsi performance data
1418 * @return GNUNET_OK to keep the connection open,
1419 * GNUNET_SYSERR to close it (signal serious error)
1422 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
1423 const struct GNUNET_MessageHeader *message,
1424 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1426 struct GNUNET_MESH_ToOrigin *msg;
1427 struct GNUNET_PeerIdentity id;
1428 struct MeshPeerInfo *peer_info;
1429 struct MeshTunnel *t;
1432 size = ntohs (message->size);
1433 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
1434 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
1436 GNUNET_break_op (0);
1439 msg = (struct GNUNET_MESH_ToOrigin *) message;
1440 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1444 /* TODO notify that we dont know this tunnel (whom)? */
1448 if (t->id.oid == myid)
1450 if (NULL == t->client)
1452 /* got data packet for ownerless tunnel */
1453 GNUNET_break_op (0);
1456 /* TODO signature verification */
1457 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle, message,
1461 peer_info = peer_info_get (&msg->oid);
1462 if (NULL == peer_info)
1464 /* unknown origin of tunnel */
1468 GNUNET_PEER_resolve (t->tree->me->parent->peer, &id);
1469 msg = GNUNET_malloc (size);
1470 memcpy (msg, message, size);
1471 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1472 GNUNET_TIME_UNIT_FOREVER_REL, &id, size,
1473 &send_core_data_raw, msg);
1480 * Core handler for path ACKs
1482 * @param cls closure
1483 * @param message message
1484 * @param peer peer identity this notification is about
1485 * @param atsi performance data
1487 * @return GNUNET_OK to keep the connection open,
1488 * GNUNET_SYSERR to close it (signal serious error)
1490 * FIXME path change state
1493 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1494 const struct GNUNET_MessageHeader *message,
1495 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1497 struct GNUNET_MESH_PathACK *msg;
1498 struct MeshTunnel *t;
1499 struct MeshPeerInfo *peer_info;
1501 msg = (struct GNUNET_MESH_PathACK *) message;
1502 t = tunnel_get (&msg->oid, msg->tid);
1505 /* TODO notify that we don't know the tunnel */
1509 /* Message for us? */
1510 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
1513 if (NULL == t->client)
1518 peer_info = peer_info_get (&msg->peer_id);
1519 if (NULL == peer_info)
1521 GNUNET_break_op (0);
1524 /* FIXME change state of peer */
1525 send_client_peer_connected(t, peer_info->id);
1529 peer_info = peer_info_get (&msg->oid);
1530 if (NULL == peer_info)
1532 /* If we know the tunnel, we should DEFINITELY know the peer */
1536 msg = GNUNET_malloc (sizeof (struct GNUNET_MESH_PathACK));
1537 memcpy (msg, message, sizeof (struct GNUNET_MESH_PathACK));
1538 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1539 GNUNET_TIME_UNIT_FOREVER_REL,
1540 path_get_first_hop (t, peer_info),
1541 sizeof (struct GNUNET_MESH_PathACK),
1542 &send_core_data_raw, msg);
1548 * Functions to handle messages from core
1550 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1551 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
1552 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
1553 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
1554 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
1555 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1556 sizeof (struct GNUNET_MESH_PathACK)},
1562 /******************************************************************************/
1563 /**************** MESH LOCAL HANDLER HELPERS ***********************/
1564 /******************************************************************************/
1567 * deregister_app: iterator for removing each application registered by a client
1569 * @param cls closure
1570 * @param key the hash of the application id (used to access the hashmap)
1571 * @param value the value stored at the key (client)
1573 * @return GNUNET_OK on success
1576 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
1578 GNUNET_CONTAINER_multihashmap_remove (applications, key, value);
1584 * notify_client_connection_failure: notify a client that the connection to the
1585 * requested remote peer is not possible (for instance, no route found)
1586 * Function called when the socket is ready to queue more data. "buf" will be
1587 * NULL and "size" zero if the socket was closed for writing in the meantime.
1589 * @param cls closure
1590 * @param size number of bytes available in buf
1591 * @param buf where the callee should write the message
1592 * @return number of bytes written to buf
1595 notify_client_connection_failure (void *cls, size_t size, void *buf)
1598 struct MeshPeerInfo *peer_info;
1599 struct GNUNET_MESH_PeerControl *msg;
1600 struct GNUNET_PeerIdentity id;
1602 if (0 == size && NULL == buf)
1604 // TODO retry? cancel?
1608 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
1609 peer_info = (struct MeshPeerInfo *) cls;
1610 msg = (struct GNUNET_MESH_PeerControl *) buf;
1611 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1612 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
1613 // msg->tunnel_id = htonl(peer_info->t->tid);
1614 GNUNET_PEER_resolve (peer_info->id, &id);
1615 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
1623 * Function to process paths received for a new peer addition. The recorded
1624 * paths form the initial tunnel, which can be optimized later.
1625 * Called on each result obtained for the DHT search.
1627 * @param cls closure
1628 * @param exp when will this value expire
1629 * @param key key of the result
1630 * @param get_path NULL-terminated array of pointers
1631 * to the peers on reverse GET path (or NULL if not recorded)
1632 * @param put_path NULL-terminated array of pointers
1633 * to the peers on the PUT path (or NULL if not recorded)
1634 * @param type type of the result
1635 * @param size number of bytes in data
1636 * @param data pointer to the result data
1641 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1642 const GNUNET_HashCode * key,
1643 const struct GNUNET_PeerIdentity *const *get_path,
1644 const struct GNUNET_PeerIdentity *const *put_path,
1645 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
1647 struct MeshPathInfo *path_info = cls;
1648 struct MeshPeerPath *p;
1649 struct GNUNET_PeerIdentity pi;
1652 if (NULL == get_path || NULL == put_path)
1654 if (NULL == path_info->peer->path_head)
1656 // Find ourselves some alternate initial path to the destination: retry
1657 GNUNET_DHT_get_stop (path_info->peer->dhtget);
1658 GNUNET_PEER_resolve (path_info->peer->id, &pi);
1659 path_info->peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1660 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1661 GNUNET_BLOCK_TYPE_TEST, /* type */
1662 &pi.hashPubKey, /*key to search */
1663 4, /* replication level */
1664 GNUNET_DHT_RO_RECORD_ROUTE, NULL, /* bloom filter */
1667 0, /* xquery bits */
1669 (void *) path_info);
1674 p = path_build_from_dht (get_path, put_path);
1675 path_add_to_peer (path_info->peer, p);
1676 for (i = 0; i < path_info->peer->ntunnels; i++)
1678 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
1680 GNUNET_free (path_info);
1687 * Function to process paths received for a new peer addition. The recorded
1688 * paths form the initial tunnel, which can be optimized later.
1689 * Called on each result obtained for the DHT search.
1691 * @param cls closure
1692 * @param exp when will this value expire
1693 * @param key key of the result
1694 * @param get_path NULL-terminated array of pointers
1695 * to the peers on reverse GET path (or NULL if not recorded)
1696 * @param put_path NULL-terminated array of pointers
1697 * to the peers on the PUT path (or NULL if not recorded)
1698 * @param type type of the result
1699 * @param size number of bytes in data
1700 * @param data pointer to the result data
1703 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1704 const GNUNET_HashCode * key,
1705 const struct GNUNET_PeerIdentity *const *get_path,
1706 const struct GNUNET_PeerIdentity *const *put_path,
1707 enum GNUNET_BLOCK_Type type, size_t size,
1710 const struct GNUNET_PeerIdentity *pi = data;
1711 struct GNUNET_PeerIdentity id;
1712 struct MeshTunnel *t = cls;
1713 struct MeshPeerInfo *peer_info;
1714 struct MeshPathInfo *path_info;
1715 struct MeshPeerPath *p;
1718 if (size != sizeof (struct GNUNET_PeerIdentity))
1720 GNUNET_break_op (0);
1723 GNUNET_assert (NULL != t->client);
1724 GNUNET_DHT_get_stop (t->client->dht_get_type);
1725 t->client->dht_get_type = NULL;
1726 peer_info = peer_info_get (pi);
1727 GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey, peer_info,
1728 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1730 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path_head &&
1731 NULL == peer_info->dhtget)
1733 /* we don't have a route to the peer, let's try a direct lookup */
1734 peer_info->dhtget = GNUNET_DHT_get_start (dht_handle,
1736 GNUNET_TIME_UNIT_FOREVER_REL,
1738 GNUNET_BLOCK_TYPE_TEST,
1741 /* key to look up */
1743 /* replication level */
1744 GNUNET_DHT_RO_RECORD_ROUTE,
1745 /* option to dht: record route */
1746 NULL, /* bloom filter */
1749 0, /* xquery bits */
1752 peer_info); /* closure */
1755 p = path_build_from_dht (get_path, put_path);
1756 path_add_to_peer (peer_info, p);
1757 tunnel_add_peer(t, peer_info);
1758 p = tunnel_get_path_to_peer(t, peer_info);
1760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1761 "MESH: new route for tunnel 0x%x found, has %u hops\n",
1762 t->local_tid, p->length);
1763 for (i = 0; i < p->length; i++)
1765 GNUNET_PEER_resolve (p->peers[0], &id);
1766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:\t%d\t%s\n", i,
1767 GNUNET_h2s_full (&id.hashPubKey));
1773 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
1775 path_info->peer = peer_info;
1776 path_info->path = p;
1777 GNUNET_PEER_resolve (p->peers[1], &id);
1778 GNUNET_CORE_notify_transmit_ready (core_handle,
1784 GNUNET_TIME_UNIT_FOREVER_REL,
1788 sizeof (struct GNUNET_MESH_ManipulatePath)
1791 sizeof (struct GNUNET_PeerIdentity)),
1793 &send_core_create_path,
1795 path_info); /* cls */
1799 send_client_peer_connected(t, myid);
1802 /******************************************************************************/
1803 /********************* MESH LOCAL HANDLES **************************/
1804 /******************************************************************************/
1808 * Handler for client disconnection
1810 * @param cls closure
1811 * @param client identification of the client; NULL
1812 * for the last call when the server is destroyed
1815 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
1817 struct MeshClient *c;
1818 struct MeshClient *next;
1820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
1822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
1826 if (c->handle != client && NULL != client)
1828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
1832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
1833 if (NULL != c->tunnels)
1835 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
1836 &tunnel_destroy_iterator,
1838 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
1841 /* deregister clients applications */
1842 if (NULL != c->apps)
1844 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
1845 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
1847 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
1848 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
1850 GNUNET_SCHEDULER_cancel (announce_applications_task);
1851 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1853 if (NULL != c->types)
1854 GNUNET_CONTAINER_multihashmap_destroy (c->types);
1855 if (NULL != c->dht_get_type)
1856 GNUNET_DHT_get_stop (c->dht_get_type);
1857 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
1863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
1869 * Handler for new clients
1871 * @param cls closure
1872 * @param client identification of the client
1873 * @param message the actual message, which includes messages the client wants
1876 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
1877 const struct GNUNET_MessageHeader *message)
1879 struct GNUNET_MESH_ClientConnect *cc_msg;
1880 struct MeshClient *c;
1881 GNUNET_MESH_ApplicationType *a;
1888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
1889 /* Check data sanity */
1890 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
1891 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
1892 ntypes = ntohs (cc_msg->types);
1893 napps = ntohs (cc_msg->applications);
1895 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
1898 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1902 /* Create new client structure */
1903 c = GNUNET_malloc (sizeof (struct MeshClient));
1905 c->id = next_client_id++;
1908 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
1911 GNUNET_MESH_ApplicationType at;
1914 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
1915 for (i = 0; i < napps; i++)
1918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
1919 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
1920 /* store in clients hashmap */
1921 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
1922 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1923 /* store in global hashmap, for announcements */
1924 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
1925 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1927 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
1928 announce_applications_task =
1929 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
1937 t = (uint16_t *) & a[napps];
1938 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
1939 for (i = 0; i < ntypes; i++)
1942 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
1944 /* store in clients hashmap */
1945 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
1946 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1947 /* store in global hashmap */
1948 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
1949 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1953 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
1955 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
1956 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
1957 GNUNET_SERVER_notification_context_add (nc, client);
1959 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
1967 * Handler for requests of new tunnels
1969 * @param cls closure
1970 * @param client identification of the client
1971 * @param message the actual message
1974 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
1975 const struct GNUNET_MessageHeader *message)
1977 struct GNUNET_MESH_TunnelMessage *t_msg;
1978 struct MeshTunnel *t;
1979 struct MeshClient *c;
1980 GNUNET_HashCode hash;
1982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
1984 /* Sanity check for client registration */
1985 if (NULL == (c = client_get (client)))
1988 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
1995 /* Message sanity check */
1996 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
1999 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2003 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2004 /* Sanity check for tunnel numbering */
2005 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
2008 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2011 /* Sanity check for duplicate tunnel IDs */
2012 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
2015 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2019 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATED TUNNEL at %p\n", t);
2021 while (NULL != tunnel_get_by_pi (myid, next_tid))
2022 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
2023 t->id.tid = next_tid++;
2025 t->local_tid = ntohl (t_msg->tunnel_id);
2027 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
2029 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2031 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
2032 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2035 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2039 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2041 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2042 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2045 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2048 t->tree = GNUNET_malloc (sizeof(struct MeshTunnelPath));
2049 t->tree->first_hops = GNUNET_CONTAINER_multihashmap_create(32);
2051 t->tree->refresh = REFRESH_PATH_TIME;
2052 t->tree->root = GNUNET_malloc(sizeof(struct MeshTunnelPathNode));
2053 t->tree->root->status = MESH_PEER_READY;
2054 t->tree->root->t = t;
2055 t->tree->root->peer = myid;
2056 t->tree->me = t->tree->root;
2058 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2064 * Handler for requests of deleting tunnels
2066 * @param cls closure
2067 * @param client identification of the client
2068 * @param message the actual message
2071 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
2072 const struct GNUNET_MessageHeader *message)
2074 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
2075 struct MeshClient *c;
2076 struct MeshTunnel *t;
2077 MESH_TunnelNumber tid;
2078 GNUNET_HashCode hash;
2080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel\n");
2082 /* Sanity check for client registration */
2083 if (NULL == (c = client_get (client)))
2086 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2089 /* Message sanity check */
2090 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2093 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2099 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2101 /* Retrieve tunnel */
2102 tid = ntohl (tunnel_msg->tunnel_id);
2104 /* Remove from local id hashmap */
2105 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2106 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
2107 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t);
2109 /* Remove from global id hashmap */
2110 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2111 GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t);
2113 // notify_tunnel_destroy(t); FIXME
2115 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2121 * Handler for connection requests to new peers
2123 * @param cls closure
2124 * @param client identification of the client
2125 * @param message the actual message (PeerControl)
2130 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
2131 const struct GNUNET_MessageHeader *message)
2133 struct GNUNET_MESH_PeerControl *peer_msg;
2134 struct MeshClient *c;
2135 struct MeshTunnel *t;
2136 MESH_TunnelNumber tid;
2137 struct MeshPeerInfo *peer_info;
2140 /* Sanity check for client registration */
2141 if (NULL == (c = client_get (client)))
2144 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2148 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2149 /* Sanity check for message size */
2150 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2153 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2157 /* Tunnel exists? */
2158 tid = ntohl (peer_msg->tunnel_id);
2159 t = tunnel_get_by_local_id (c, tid);
2163 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2167 /* Does client own tunnel? */
2168 if (t->client->handle != client)
2171 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2176 peer_info = peer_info_get (&peer_msg->peer);
2178 /* Start DHT search if needed FIXME: if not already connected */
2179 if (NULL == peer_info->dhtget)
2181 peer_info->dhtget = GNUNET_DHT_get_start (dht_handle, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_BLOCK_TYPE_TEST, &peer_msg->peer.hashPubKey, 4, /* replication level */
2182 GNUNET_DHT_RO_RECORD_ROUTE, NULL, /* bloom filter */
2185 0, /* xquery bits */
2187 (void *) peer_info);
2190 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2196 * Handler for disconnection requests of peers in a tunnel
2198 * @param cls closure
2199 * @param client identification of the client
2200 * @param message the actual message (PeerControl)
2203 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
2204 const struct GNUNET_MessageHeader *message)
2206 struct GNUNET_MESH_PeerControl *peer_msg;
2207 struct MeshClient *c;
2208 struct MeshTunnel *t;
2209 MESH_TunnelNumber tid;
2211 /* Sanity check for client registration */
2212 if (NULL == (c = client_get (client)))
2215 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2218 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2219 /* Sanity check for message size */
2220 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2223 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2227 /* Tunnel exists? */
2228 tid = ntohl (peer_msg->tunnel_id);
2229 t = tunnel_get_by_local_id (c, tid);
2233 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2237 /* Does client own tunnel? */
2238 if (t->client->handle != client)
2241 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2245 /* Ok, delete peer from tunnel */
2246 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
2247 &peer_msg->peer.hashPubKey);
2249 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2255 * Handler for connection requests to new peers by type
2257 * @param cls closure
2258 * @param client identification of the client
2259 * @param message the actual message (ConnectPeerByType)
2262 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
2263 const struct GNUNET_MessageHeader *message)
2265 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
2266 struct MeshClient *c;
2267 struct MeshTunnel *t;
2268 GNUNET_HashCode hash;
2269 GNUNET_MESH_ApplicationType type;
2270 MESH_TunnelNumber tid;
2272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
2273 /* Sanity check for client registration */
2274 if (NULL == (c = client_get (client)))
2277 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2281 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
2282 /* Sanity check for message size */
2283 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
2284 ntohs (connect_msg->header.size))
2287 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2291 /* Tunnel exists? */
2292 tid = ntohl (connect_msg->tunnel_id);
2293 t = tunnel_get_by_local_id (c, tid);
2297 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2301 /* Does client own tunnel? */
2302 if (t->client->handle != client)
2305 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2309 /* Do WE have the service? */
2310 type = ntohl (connect_msg->type);
2311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", type);
2312 GNUNET_CRYPTO_hash (&type, sizeof (GNUNET_MESH_ApplicationType), &hash);
2313 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
2316 /* Yes! Fast forward, add ourselves to the tunnel and send the
2317 * good news to the client
2319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
2320 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
2321 peer_info_get (&my_full_id),
2322 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
2325 send_client_peer_connected(t, myid);
2326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
2327 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2330 /* Ok, lets find a peer offering the service */
2331 if (c->dht_get_type)
2333 GNUNET_DHT_get_stop (c->dht_get_type);
2335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
2336 GNUNET_h2s_full (&hash));
2338 GNUNET_DHT_get_start (dht_handle, GNUNET_TIME_UNIT_FOREVER_REL,
2339 GNUNET_BLOCK_TYPE_TEST, &hash, 10U,
2340 GNUNET_DHT_RO_RECORD_ROUTE, NULL, 0, NULL, 0,
2341 &dht_get_type_handler, t);
2343 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2349 * Handler for client traffic directed to one peer
2351 * @param cls closure
2352 * @param client identification of the client
2353 * @param message the actual message
2356 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
2357 const struct GNUNET_MessageHeader *message)
2359 struct MeshClient *c;
2360 struct MeshTunnel *t;
2361 struct MeshPeerInfo *pi;
2362 struct GNUNET_MESH_Unicast *data_msg;
2363 struct MeshDataDescriptor *info;
2364 MESH_TunnelNumber tid;
2367 /* Sanity check for client registration */
2368 if (NULL == (c = client_get (client)))
2371 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2374 data_msg = (struct GNUNET_MESH_Unicast *) message;
2375 /* Sanity check for message size */
2376 if (sizeof (struct GNUNET_MESH_Unicast) +
2377 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
2380 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2384 /* Tunnel exists? */
2385 tid = ntohl (data_msg->tid);
2386 t = tunnel_get_by_local_id (c, tid);
2390 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2394 /* Is it a local tunnel? Then, does client own the tunnel? */
2395 if (t->client->handle != NULL && t->client->handle != client)
2398 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2402 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
2403 &data_msg->destination.hashPubKey);
2404 /* Is the selected peer in the tunnel? */
2408 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2413 struct GNUNET_MESH_Unicast copy;
2415 /* Work around const limitation */
2416 memcpy (©, data_msg, sizeof (struct GNUNET_MESH_Unicast));
2417 copy.oid = my_full_id;
2418 copy.tid = htonl (t->id.tid);
2419 handle_mesh_data_unicast (NULL, &my_full_id, ©.header, NULL);
2422 data_size = ntohs (message->size) - sizeof (struct GNUNET_MESH_Unicast);
2423 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor) + data_size);
2424 memcpy (&info[1], &data_msg[1], data_size);
2425 info->destination = pi->id;
2426 info->origin = &t->id;
2427 info->size = data_size;
2428 info->client = client;
2429 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2430 GNUNET_TIME_UNIT_FOREVER_REL,
2431 path_get_first_hop (t, pi),
2432 /* FIXME re-check types */
2434 sizeof (struct GNUNET_MESH_Unicast),
2435 &send_core_data_unicast, info);
2440 * Handler for client traffic directed to all peers in a tunnel
2442 * @param cls closure
2443 * @param client identification of the client
2444 * @param message the actual message
2447 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
2448 const struct GNUNET_MessageHeader *message)
2450 struct MeshClient *c;
2451 struct MeshTunnel *t;
2452 struct GNUNET_MESH_Multicast *data_msg;
2453 MESH_TunnelNumber tid;
2455 /* Sanity check for client registration */
2456 if (NULL == (c = client_get (client)))
2459 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2462 data_msg = (struct GNUNET_MESH_Multicast *) message;
2463 /* Sanity check for message size */
2464 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (data_msg->header.size))
2467 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2471 /* Tunnel exists? */
2472 tid = ntohl (data_msg->tid);
2473 t = tunnel_get_by_local_id (c, tid);
2477 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2481 /* Does client own tunnel? */
2482 if (t->client->handle != client)
2485 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2491 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2496 * Functions to handle messages from clients
2498 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
2499 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
2500 {&handle_local_tunnel_create, NULL,
2501 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
2502 sizeof (struct GNUNET_MESH_TunnelMessage)},
2503 {&handle_local_tunnel_destroy, NULL,
2504 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
2505 sizeof (struct GNUNET_MESH_TunnelMessage)},
2506 {&handle_local_connect_add, NULL,
2507 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
2508 sizeof (struct GNUNET_MESH_PeerControl)},
2509 {&handle_local_connect_del, NULL,
2510 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
2511 sizeof (struct GNUNET_MESH_PeerControl)},
2512 {&handle_local_connect_by_type, NULL,
2513 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
2514 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
2515 {&handle_local_unicast, NULL,
2516 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
2517 {&handle_local_unicast, NULL,
2518 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
2519 {&handle_local_multicast, NULL,
2520 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
2526 * To be called on core init/fail.
2528 * @param cls service closure
2529 * @param server handle to the server for this service
2530 * @param identity the public identity of this peer
2531 * @param publicKey the public key of this peer
2534 core_init (void *cls, struct GNUNET_CORE_Handle *server,
2535 const struct GNUNET_PeerIdentity *identity,
2536 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
2538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
2539 core_handle = server;
2540 if (0 != memcmp(identity, &my_full_id, sizeof(my_full_id)) || NULL == server)
2542 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
2543 GNUNET_SCHEDULER_shutdown();
2549 * Method called whenever a given peer connects.
2551 * @param cls closure
2552 * @param peer peer identity this notification is about
2553 * @param atsi performance data for the connection
2556 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
2557 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2559 // GNUNET_PEER_Id pid;
2560 struct MeshPeerInfo *peer_info;
2561 struct MeshPeerPath *path;
2563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
2564 peer_info = peer_info_get (peer);
2565 if (myid == peer_info->id)
2567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
2570 path = GNUNET_malloc (sizeof (struct MeshPeerPath));
2572 path->peers = GNUNET_malloc (sizeof (GNUNET_PEER_Id) * 2);
2573 path->peers[0] = myid;
2574 path->peers[1] = peer_info->id;
2575 path_add_to_peer (peer_info, path);
2580 * Method called whenever a peer disconnects.
2582 * @param cls closure
2583 * @param peer peer identity this notification is about
2586 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
2588 struct MeshPeerInfo *pi;
2591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
2592 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2598 for (i = 0; i < CORE_QUEUE_SIZE; i++)
2600 if (pi->core_transmit[i])
2602 GNUNET_CORE_notify_transmit_ready_cancel (pi->core_transmit[i]);
2603 /* TODO: notify that tranmission has failed */
2604 GNUNET_free (pi->infos[i]);
2607 path_remove_from_peer (pi, pi->id, myid);
2610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
2616 /******************************************************************************/
2617 /************************ MAIN FUNCTIONS ****************************/
2618 /******************************************************************************/
2621 * Task run during shutdown.
2627 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2629 struct MeshClient *c;
2631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
2632 if (core_handle != NULL)
2634 GNUNET_CORE_disconnect (core_handle);
2637 if (dht_handle != NULL)
2639 for (c = clients; NULL != c; c = c->next)
2640 if (NULL != c->dht_get_type)
2641 GNUNET_DHT_get_stop (c->dht_get_type);
2642 GNUNET_DHT_disconnect (dht_handle);
2647 GNUNET_SERVER_notification_context_destroy (nc);
2650 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
2652 GNUNET_SCHEDULER_cancel (announce_id_task);
2653 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
2655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
2659 * Process mesh requests.
2661 * @param cls closure
2662 * @param server the initialized server
2663 * @param c configuration to use
2666 run (void *cls, struct GNUNET_SERVER_Handle *server,
2667 const struct GNUNET_CONFIGURATION_Handle *c)
2669 struct MeshPeerInfo *peer;
2670 struct MeshPeerPath *p;
2673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
2674 server_handle = server;
2675 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
2676 CORE_QUEUE_SIZE, /* queue size */
2677 NULL, /* Closure passed to MESH functions */
2678 &core_init, /* Call core_init once connected */
2679 &core_connect, /* Handle connects */
2680 &core_disconnect, /* remove peers on disconnects */
2681 NULL, /* Do we care about "status" updates? */
2682 NULL, /* Don't notify about all incoming messages */
2683 GNUNET_NO, /* For header only in notification */
2684 NULL, /* Don't notify about all outbound messages */
2685 GNUNET_NO, /* For header-only out notification */
2686 core_handlers); /* Register these handlers */
2687 if (core_handle == NULL)
2690 GNUNET_SCHEDULER_shutdown ();
2695 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
2698 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2700 ("Mesh service is lacking key configuration settings. Exiting.\n"));
2701 GNUNET_SCHEDULER_shutdown ();
2704 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2705 GNUNET_free (keyfile);
2706 if (my_private_key == NULL)
2708 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2709 _("Mesh service could not access hostkey. Exiting.\n"));
2710 GNUNET_SCHEDULER_shutdown ();
2713 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
2714 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
2715 &my_full_id.hashPubKey);
2716 myid = GNUNET_PEER_intern (&my_full_id);
2718 dht_handle = GNUNET_DHT_connect (c, 64);
2719 if (dht_handle == NULL)
2721 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error connecting to DHT.\
2722 Running without DHT has a severe\
2723 impact in MESH capabilities.\n\
2724 Plase check your configuretion and enable DHT.\n");
2730 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
2731 peers = GNUNET_CONTAINER_multihashmap_create (32);
2732 applications = GNUNET_CONTAINER_multihashmap_create (32);
2733 types = GNUNET_CONTAINER_multihashmap_create (32);
2735 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
2736 nc = GNUNET_SERVER_notification_context_create (server_handle,
2738 GNUNET_SERVER_disconnect_notify (server_handle,
2739 &handle_local_client_disconnect,
2744 clients_tail = NULL;
2749 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
2750 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
2752 /* Create a peer_info for the local peer */
2753 peer = peer_info_get(&my_full_id);
2754 p = GNUNET_malloc (sizeof (struct MeshPeerPath));
2755 p->peers = GNUNET_malloc (sizeof (GNUNET_PEER_Id));
2758 path_add_to_peer(peer, p);
2760 /* Scheduled the task to clean up when shutdown is called */
2761 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
2764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
2768 * The main function for the mesh service.
2770 * @param argc number of arguments from the command line
2771 * @param argv command line arguments
2772 * @return 0 ok, 1 on error
2775 main (int argc, char *const *argv)
2780 fprintf (stderr, "main ()\n");
2782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
2785 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
2787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");
2789 fprintf (stderr, "main () END\n");