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,\
66 /******************************************************************************/
67 /************************ DATA STRUCTURES ****************************/
68 /******************************************************************************/
70 /** FWD declaration */
74 * Struct containing all info possibly needed to build a package when called
77 struct MeshDataDescriptor
79 /** ID of the tunnel this packet travels in */
80 struct MESH_TunnelID *origin;
82 /** Ultimate destination of the packet */
83 GNUNET_PEER_Id destination;
85 /** Number of identical messages sent to different hops (multicast) */
88 /** Size of the data */
91 /** Client that asked for the transmission, if any */
92 struct GNUNET_SERVER_Client *client;
94 /** Who was is message being sent to */
95 struct MeshPeerInfo *peer;
97 /** Which handler was used to request the transmission */
98 unsigned int handler_n;
100 /* Data at the end */
105 * Struct containing all information regarding a given peer
115 * Last time we heard from this peer
117 struct GNUNET_TIME_Absolute last_contact;
120 * Number of attempts to reconnect so far
122 int n_reconnect_attempts;
125 * Paths to reach the peer, ordered by ascending hop count
127 struct MeshPeerPath *path_head;
130 * Paths to reach the peer, ordered by ascending hop count
132 struct MeshPeerPath *path_tail;
135 * Handle to stop the DHT search for a path to this peer
137 struct GNUNET_DHT_GetHandle *dhtget;
140 * Handles to stop queued transmissions for this peer
142 struct GNUNET_CORE_TransmitHandle *core_transmit[CORE_QUEUE_SIZE];
145 * Pointer to info stuctures used as cls for queued transmissions
147 struct MeshDataDescriptor *infos[CORE_QUEUE_SIZE];
150 * Array of tunnels this peer participates in
151 * (most probably a small amount, therefore not a hashmap)
152 * When the path to the peer changes, notify these tunnels to let them
153 * re-adjust their path trees.
155 struct MeshTunnel **tunnels;
158 * Number of tunnels above
160 unsigned int ntunnels;
165 * Data scheduled to transmit (to local client or remote peer)
172 struct MeshQueue *next;
173 struct MeshQueue *prev;
176 * Target of the data (NULL if target is client)
178 struct MeshPeerInfo *peer;
181 * Client to send the data to (NULL if target is peer)
183 struct MeshClient *client;
186 * Size of the message to transmit
191 * How old is the data?
193 struct GNUNET_TIME_Absolute timestamp;
198 struct GNUNET_MessageHeader *data;
202 * Globally unique tunnel identification (owner + number)
203 * DO NOT USE OVER THE NETWORK
208 * Node that owns the tunnel
213 * Tunnel number to differentiate all the tunnels owned by the node oid
214 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
216 MESH_TunnelNumber tid;
220 struct MeshClient; /* FWD declaration */
223 * Struct containing all information regarding a tunnel
224 * For an intermediate node the improtant info used will be:
225 * - id Tunnel unique identification
226 * - paths[0] To know where to send it next
227 * - metainfo: ready, speeds, accounting
234 struct MESH_TunnelID id;
237 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
239 MESH_TunnelNumber local_tid;
242 * Last time the tunnel was used
244 struct GNUNET_TIME_Absolute timestamp;
247 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
249 struct GNUNET_CONTAINER_MultiHashMap *peers;
252 * Number of peers that are connected and potentially ready to receive data
254 unsigned int peers_ready;
257 * Number of peers that have been added to the tunnel
259 unsigned int peers_total;
262 * Client owner of the tunnel, if any
264 struct MeshClient *client;
267 * Messages ready to transmit
269 struct MeshQueue *queue_head;
270 struct MeshQueue *queue_tail;
275 struct MeshTunnelTree *tree;
278 * Task to keep the used paths alive
280 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
285 * Info needed to work with tunnel paths and peers
292 struct MeshTunnel *t;
297 struct MeshPeerInfo *peer;
302 struct MeshPeerPath *path;
307 * Struct containing information about a client of the service
314 struct MeshClient *next;
315 struct MeshClient *prev;
318 * Tunnels that belong to this client, indexed by local id
320 struct GNUNET_CONTAINER_MultiHashMap *tunnels;
323 * Handle to communicate with the client
325 struct GNUNET_SERVER_Client *handle;
328 * Applications that this client has claimed to provide
330 struct GNUNET_CONTAINER_MultiHashMap *apps;
333 * Messages that this client has declared interest in
335 struct GNUNET_CONTAINER_MultiHashMap *types;
338 * Used to search peers offering a service
340 struct GNUNET_DHT_GetHandle *dht_get_type;
344 * ID of the client, for debug messages
353 /******************************************************************************/
354 /************************ DEBUG FUNCTIONS ****************************/
355 /******************************************************************************/
359 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
360 * @param cls string to print
361 * @param tc task context
364 mesh_debug (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
368 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n", s);
377 /******************************************************************************/
378 /*********************** GLOBAL VARIABLES ****************************/
379 /******************************************************************************/
384 static struct MeshClient *clients;
385 static struct MeshClient *clients_tail;
388 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
390 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
393 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
395 static struct GNUNET_CONTAINER_MultiHashMap *peers;
398 * Handle to communicate with core
400 static struct GNUNET_CORE_Handle *core_handle;
405 static struct GNUNET_DHT_Handle *dht_handle;
410 static struct GNUNET_SERVER_Handle *server_handle;
413 * Notification context, to send messages to local clients
415 static struct GNUNET_SERVER_NotificationContext *nc;
418 * Local peer own ID (memory efficient handle)
420 static GNUNET_PEER_Id myid;
423 * Local peer own ID (full value)
425 static struct GNUNET_PeerIdentity my_full_id;
430 static struct GNUNET_CRYPTO_RsaPrivateKey* my_private_key;
435 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
438 * Tunnel ID for the next created tunnel (global tunnel number)
440 static MESH_TunnelNumber next_tid;
443 * All application types provided by this peer
445 static struct GNUNET_CONTAINER_MultiHashMap *applications;
448 * All message types clients of this peer are interested in
450 static struct GNUNET_CONTAINER_MultiHashMap *types;
453 * Task to periodically announce provided applications
455 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
458 * Task to periodically announce itself in the network
460 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
463 unsigned int next_client_id;
467 /******************************************************************************/
468 /************************ ITERATORS ****************************/
469 /******************************************************************************/
473 * Iterator over hash map peer entries collect all neighbors who to resend the
476 * @param cls closure (**GNUNET_PEER_Id to store hops to send packet)
477 * @param key current key code (peer id hash)
478 * @param value value in the hash map (peer_info)
479 * @return GNUNET_YES if we should continue to iterate, GNUNET_NO if not.
482 iterate_collect_neighbors (void *cls, const GNUNET_HashCode * key, void *value)
484 struct MeshPeerInfo *peer_info = value;
485 struct MeshPathInfo *neighbors = cls;
486 struct GNUNET_PeerIdentity *id;
487 GNUNET_PEER_Id peer_id;
490 if (peer_info->id == myid)
494 id = path_get_first_hop (neighbors->t->tree, peer_info->id);
495 peer_id = GNUNET_PEER_search(id);
496 for (i = 0; i < neighbors->path->length; i++)
498 if (neighbors->path->peers[i] == peer_id)
501 GNUNET_array_append (neighbors->path->peers, neighbors->path->length,
508 /******************************************************************************/
509 /************************ PERIODIC FUNCTIONS ****************************/
510 /******************************************************************************/
513 * Announce iterator over for each application provided by the peer
516 * @param key current key code
517 * @param value value in the hash map
518 * @return GNUNET_YES if we should continue to
523 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
525 /* FIXME are hashes in multihash map equal on all aquitectures? */
526 GNUNET_DHT_put (dht_handle, key, 10U, GNUNET_DHT_RO_RECORD_ROUTE,
527 GNUNET_BLOCK_TYPE_TEST, sizeof (struct GNUNET_PeerIdentity),
528 (const char *) &my_full_id,
530 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
531 &mesh_debug, "DHT_put for app completed");
533 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
535 APP_ANNOUNCE_TIME, NULL, NULL);
542 * Periodically announce what applications are provided by local clients
545 * @param tc task context
548 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
550 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
552 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Starting PUT for apps\n");
556 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
558 announce_applications_task =
559 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Finished PUT for apps\n");
567 * Periodically announce self id in the DHT
570 * @param tc task context
573 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
575 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
577 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
581 * - Set data expiration in function of X
584 GNUNET_DHT_put (dht_handle, /* DHT handle */
585 &my_full_id.hashPubKey, /* Key to use */
586 10U, /* Replication level */
587 GNUNET_DHT_RO_RECORD_ROUTE, /* DHT options */
588 GNUNET_BLOCK_TYPE_TEST, /* Block type */
589 0, /* Size of the data */
590 NULL, /* Data itself */
591 GNUNET_TIME_absolute_get_forever (), /* Data expiration */
592 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
594 &mesh_debug, "DHT_put for id completed");
596 NULL, /* Continuation */
597 NULL); /* Continuation closure */
600 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
604 /******************************************************************************/
605 /****************** GENERAL HELPER FUNCTIONS ************************/
606 /******************************************************************************/
609 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
610 * and insert it in the appropiate structures if the peer is not known yet.
612 * @param peer Identity of the peer
614 * @return Existing or newly created peer info
616 static struct MeshPeerInfo *
617 peer_info_get (const struct GNUNET_PeerIdentity *peer)
619 struct MeshPeerInfo *peer_info;
621 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
622 if (NULL == peer_info)
625 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
626 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
627 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
628 peer_info->id = GNUNET_PEER_intern (peer);
637 * Destroy the peer_info and free any allocated resources linked to it
638 * @param t tunnel the path belongs to
639 * @param pi the peer_info to destroy
640 * @return GNUNET_OK on success
643 peer_info_destroy (struct MeshPeerInfo *pi)
645 GNUNET_HashCode hash;
646 struct GNUNET_PeerIdentity id;
648 GNUNET_PEER_resolve (pi->id, &id);
649 GNUNET_PEER_change_rc (pi->id, -1);
650 GNUNET_CRYPTO_hash (&id, sizeof (struct GNUNET_PeerIdentity), &hash);
652 GNUNET_CONTAINER_multihashmap_remove (peers, &hash, pi);
653 GNUNET_SCHEDULER_cancel (pi->path_refresh_task);
661 * Notify a tunnel that a connection has broken that affects at least
664 * @param t Tunnel affected
665 * @param peer Peer that (at least) has been affected by the disconnection
666 * @param p1 Peer that got disconnected from p2
667 * @param p2 Peer that got disconnected from p1
670 tunnel_notify_connection_broken (struct MeshTunnel *t,
671 struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
676 * Remove all paths that rely on a direct connection between p1 and p2
677 * from the peer itself and notify all tunnels about it.
679 * @param pi PeerInfo of affected peer
680 * @param p1 GNUNET_PEER_Id of one peer.
681 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
685 path_remove_from_peer (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
688 struct MeshPeerPath *p;
689 struct MeshPeerPath *aux;
690 unsigned int destroyed;
698 for (i = 0; i < (p->length - 1); i++)
700 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
701 (p->peers[i] == p2 && p->peers[i + 1] == p1))
713 for (i = 0; i < peer->ntunnels; i++)
715 tunnel_notify_connection_broken (peer->tunnels[i], peer, p1, p2);
721 * Add the path to the peer and update the path used to reach it in case this
724 * @param peer_info Destination peer to add the path to.
725 * @param path New path to add. Last peer must be the peer in arg 1.
726 * Path will be either used of freed if already known.
728 * TODO: trim the part from origin to us? Add it as path to origin?
731 path_add_to_peer (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
733 struct MeshPeerPath *aux;
737 if (NULL == peer_info || NULL == path)
743 l = path_get_length (path);
745 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
747 l2 = path_get_length (aux);
750 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
751 peer_info->path_tail, aux, path);
755 if (l2 == l && memcmp(path->peers, aux->peers, l) == 0)
762 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
769 * Add the path to the origin peer and update the path used to reach it in case
770 * this is the shortest.
771 * The path is given in peer_info -> destination, therefore we turn the path
774 * @param peer_info Peer to add the path to, being the origin of the path.
775 * @param path New path to add after being inversed.
778 path_add_to_origin (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
781 path_add_to_peer (peer_info, path);
786 * Build a PeerPath from the paths returned from the DHT, reversing the paths
787 * to obtain a local peer -> destination path and interning the peer ids.
789 * @param get_path NULL-terminated array of pointers
790 * to the peers on reverse GET path (or NULL if not recorded)
791 * @param put_path NULL-terminated array of pointers
792 * to the peers on the PUT path (or NULL if not recorded)
794 * @return Newly allocated and created path
796 static struct MeshPeerPath *
797 path_build_from_dht (const struct GNUNET_PeerIdentity *const *get_path,
798 const struct GNUNET_PeerIdentity *const *put_path)
800 struct MeshPeerPath *p;
805 for (i = 0; get_path[i] != NULL; i++) ;
806 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: GET has %d hops.\n", i);
807 for (i--; i >= 0; i--)
809 id = GNUNET_PEER_intern (get_path[i]);
810 if (p->length > 0 && id == p->peers[p->length - 1])
812 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
813 GNUNET_PEER_change_rc(id, -1);
818 GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
819 p->peers[p->length] = id;
823 for (i = 0; put_path[i] != NULL; i++) ;
824 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: PUT has %d hops.\n", i);
825 for (i--; i >= 0; i--)
827 id = GNUNET_PEER_intern (put_path[i]);
828 if (p->length > 0 && id == p->peers[p->length - 1])
830 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
831 GNUNET_PEER_change_rc(id, -1);
836 GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
837 p->peers[p->length] = id;
842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
843 "MESH: (first of GET: %s)\n",
844 GNUNET_h2s_full(&get_path[0]->hashPubKey));
845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
846 "MESH: (first of PUT: %s)\n",
847 GNUNET_h2s_full(&put_path[0]->hashPubKey));
848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
849 "MESH: In total: %d hops\n",
857 * Send keepalive packets for a peer
865 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
867 struct MeshTunnel *t = cls;
869 // struct GNUNET_PeerIdentity id;
871 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
875 /* FIXME implement multicast keepalive. Just an empty multicast packet? */
876 // GNUNET_PEER_resolve (path_get_first_hop (path->t, path->peer)->id, &id);
877 // GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
878 // GNUNET_TIME_UNIT_FOREVER_REL, &id,
879 // sizeof (struct GNUNET_MESH_ManipulatePath)
881 // (path->path->length *
882 // sizeof (struct GNUNET_PeerIdentity)),
883 // &send_core_create_path,
885 t->path_refresh_task =
886 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
892 * Check if client has registered with the service and has not disconnected
894 * @param client the client to check
896 * @return non-NULL if client exists in the global DLL
898 static struct MeshClient *
899 client_get (struct GNUNET_SERVER_Client *client)
901 struct MeshClient *c;
906 if (c->handle == client)
915 * Checks if a given client has subscribed to certain message type
917 * @param message_type Type of message to check
918 * @param c Client to check
920 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
925 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
929 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
930 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
935 * Search for a tunnel among the tunnels for a client
937 * @param c the client whose tunnels to search in
938 * @param tid the local id of the tunnel
940 * @return tunnel handler, NULL if doesn't exist
942 static struct MeshTunnel *
943 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
945 GNUNET_HashCode hash;
947 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
948 return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
953 * Search for a tunnel by global ID using PEER_ID
955 * @param pi owner of the tunnel
956 * @param tid global tunnel number
958 * @return tunnel handler, NULL if doesn't exist
960 static struct MeshTunnel *
961 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
963 struct MESH_TunnelID id;
964 GNUNET_HashCode hash;
969 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
970 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
975 * Search for a tunnel by global ID using full PeerIdentities
977 * @param oid owner of the tunnel
978 * @param tid global tunnel number
980 * @return tunnel handler, NULL if doesn't exist
982 static struct MeshTunnel *
983 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
985 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
990 * Callback used to notify a client owner of a tunnel that a peer has
991 * disconnected, most likely because of a path change.
993 * @param n Node in the tree representing the disconnected peer
996 notify_peer_disconnected (const struct MeshTunnelTreeNode *n)
998 struct GNUNET_MESH_PeerControl msg;
1000 if (NULL == n->t->client || NULL == nc)
1003 msg.header.size = htons (sizeof (msg));
1004 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1005 msg.tunnel_id = htonl (n->t->local_tid);
1006 GNUNET_PEER_resolve (n->peer, &msg.peer);
1007 GNUNET_SERVER_notification_context_unicast (nc, n->t->client->handle,
1008 &msg.header, GNUNET_NO);
1013 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1014 * needed rescources.
1016 * @param t Tunnel we want to add a new peer to
1017 * @param peer PeerInfo of the peer being added
1021 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1023 struct MeshPeerPath *p;
1024 struct MeshPeerPath *best_p;
1025 unsigned int best_cost;
1028 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
1029 if (NULL == (p = peer->path_head))
1033 best_cost = UINT_MAX;
1036 if ((cost = path_get_cost (t->tree, p)) < best_cost)
1043 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected);
1044 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
1045 t->path_refresh_task =
1046 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
1051 * Notify a tunnel that a connection has broken that affects at least
1052 * some of its peers.
1054 * @param t Tunnel affected
1055 * @param peer Peer that (at least) has been affected by the disconnection
1056 * @param p1 Peer that got disconnected from p2
1057 * @param p2 Peer that got disconnected from p1
1062 tunnel_notify_connection_broken (struct MeshTunnel *t,
1063 struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1070 * Destroy the tunnel and free any allocated resources linked to it
1072 * @param t the tunnel to destroy
1074 * @return GNUNET_OK on success
1077 tunnel_destroy (struct MeshTunnel *t)
1079 struct MeshClient *c;
1080 struct MeshQueue *q;
1081 struct MeshQueue *qn;
1082 GNUNET_HashCode hash;
1085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DESTROYING TUNNEL at %p\n", t);
1091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
1094 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1095 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
1100 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1101 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
1105 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
1109 if (NULL != q->data)
1110 GNUNET_free (q->data);
1114 /* TODO cancel core transmit ready in case it was active */
1116 tree_destroy(t->tree);
1123 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
1124 * client when the client disconnects.
1126 * @param cls closure (client that is disconnecting)
1127 * @param key the hash of the local tunnel id (used to access the hashmap)
1128 * @param value the value stored at the key (tunnel to destroy)
1130 * @return GNUNET_OK on success
1133 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
1137 r = tunnel_destroy ((struct MeshTunnel *) value);
1142 /******************************************************************************/
1143 /**************** MESH NETWORK HANDLER HELPERS ***********************/
1144 /******************************************************************************/
1148 * Function called to notify a client about the socket
1149 * being ready to queue more data. "buf" will be
1150 * NULL and "size" zero if the socket was closed for
1151 * writing in the meantime.
1153 * @param cls closure
1154 * @param size number of bytes available in buf
1155 * @param buf where the callee should write the message
1156 * @return number of bytes written to buf
1159 send_core_create_path (void *cls, size_t size, void *buf)
1161 struct MeshPathInfo *info = cls;
1162 struct GNUNET_MESH_ManipulatePath *msg;
1163 struct GNUNET_PeerIdentity *peer_ptr;
1164 struct MeshPeerInfo *peer = info->peer;
1165 struct MeshTunnel *t = info->t;
1166 struct MeshPeerPath *p = info->path;
1171 sizeof (struct GNUNET_MESH_ManipulatePath) +
1172 p->length * sizeof (struct GNUNET_PeerIdentity);
1174 if (size < size_needed || NULL == buf)
1176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Retransmitting create path\n");
1177 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1178 GNUNET_TIME_UNIT_FOREVER_REL,
1179 path_get_first_hop (t->tree, peer->id),
1180 size_needed, &send_core_create_path,
1185 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
1186 msg->header.size = htons (size_needed);
1187 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
1188 msg->tid = ntohl (t->id.tid);
1190 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1191 for (i = 0; i < p->length; i++)
1193 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1205 * Function called to notify a client about the socket
1206 * being ready to queue more data. "buf" will be
1207 * NULL and "size" zero if the socket was closed for
1208 * writing in the meantime.
1210 * @param cls closure (MeshDataDescriptor with all info to build packet)
1211 * @param size number of bytes available in buf
1212 * @param buf where the callee should write the message
1213 * @return number of bytes written to buf
1216 send_core_data_to_origin (void *cls, size_t size, void *buf)
1218 struct MeshDataDescriptor *info = cls;
1219 struct GNUNET_MESH_ToOrigin *msg = buf;
1222 GNUNET_assert (NULL != info);
1223 total_size = sizeof (struct GNUNET_MESH_ToOrigin) + info->size;
1224 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
1226 if (total_size > size)
1228 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1229 "not enough buffer to send data to origin\n");
1232 msg->header.size = htons (total_size);
1233 msg->header.type = htons (GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN);
1234 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1235 msg->tid = htonl (info->origin->tid);
1236 if (0 != info->size)
1238 memcpy (&msg[1], &info[1], info->size);
1240 if (NULL != info->client)
1242 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1250 * Function called to notify a client about the socket
1251 * being ready to queue more data. "buf" will be
1252 * NULL and "size" zero if the socket was closed for
1253 * writing in the meantime.
1255 * @param cls closure (data itself)
1256 * @param size number of bytes available in buf
1257 * @param buf where the callee should write the message
1258 * @return number of bytes written to buf
1261 send_core_data_unicast (void *cls, size_t size, void *buf)
1263 struct MeshDataDescriptor *info = cls;
1264 struct GNUNET_MESH_Unicast *msg = buf;
1267 GNUNET_assert (NULL != info);
1268 total_size = sizeof (struct GNUNET_MESH_Unicast) + info->size;
1269 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
1271 if (total_size > size)
1273 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1274 "not enough buffer to send data to peer\n");
1277 msg->header.size = htons (total_size);
1278 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_UNICAST);
1279 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1280 GNUNET_PEER_resolve (info->destination, &msg->destination);
1281 msg->tid = htonl (info->origin->tid);
1282 if (0 != info->size)
1284 memcpy (&msg[1], &info[1], info->size);
1286 if (NULL != info->client)
1288 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1296 * Function called to notify a client about the socket
1297 * being ready to queue more data. "buf" will be
1298 * NULL and "size" zero if the socket was closed for
1299 * writing in the meantime.
1301 * @param cls closure (data itself)
1302 * @param size number of bytes available in buf
1303 * @param buf where the callee should write the message
1304 * @return number of bytes written to buf
1307 send_core_data_multicast (void *cls, size_t size, void *buf)
1309 struct MeshDataDescriptor *info = cls;
1310 struct GNUNET_MESH_Multicast *msg = buf;
1313 GNUNET_assert (NULL != info);
1314 total_size = info->size + sizeof (struct GNUNET_MESH_Multicast);
1315 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1319 info->peer->core_transmit[info->handler_n] = NULL;
1321 if (total_size > size)
1323 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1324 "not enough buffer to send data futher\n");
1327 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
1328 msg->header.size = htons (total_size);
1329 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1330 msg->tid = htonl (info->origin->tid);
1331 memcpy (&msg[1], &info[1], total_size);
1332 if (0 == --info->copies)
1334 if (NULL != info->client)
1336 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1345 * Function called to notify a client about the socket
1346 * being ready to queue more data. "buf" will be
1347 * NULL and "size" zero if the socket was closed for
1348 * writing in the meantime.
1350 * @param cls closure (MeshDataDescriptor)
1351 * @param size number of bytes available in buf
1352 * @param buf where the callee should write the message
1353 * @return number of bytes written to buf
1356 send_core_path_ack (void *cls, size_t size, void *buf)
1358 struct MeshDataDescriptor *info = cls;
1359 struct GNUNET_MESH_PathACK *msg = buf;
1361 GNUNET_assert (NULL != info);
1364 info->peer->core_transmit[info->handler_n] = NULL;
1366 if (sizeof (struct GNUNET_MESH_PathACK) > size)
1371 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
1372 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
1373 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1374 msg->tid = htonl (info->origin->tid);
1375 msg->peer_id = my_full_id;
1377 /* TODO add signature */
1379 return sizeof (struct GNUNET_MESH_PathACK);
1384 * Function called to notify a client about the socket
1385 * being ready to queue more data. "buf" will be
1386 * NULL and "size" zero if the socket was closed for
1387 * writing in the meantime.
1389 * @param cls closure (data itself)
1390 * @param size number of bytes available in buf
1391 * @param buf where the callee should write the message
1392 * @return number of bytes written to buf
1395 send_core_data_raw (void *cls, size_t size, void *buf)
1397 struct GNUNET_MessageHeader *msg = cls;
1400 GNUNET_assert (NULL != msg);
1401 total_size = ntohs (msg->size);
1403 if (total_size > size)
1408 memcpy (buf, msg, total_size);
1416 * Send another peer a notification to destroy a tunnel
1417 * @param cls The tunnel to destroy
1418 * @param size Size in the buffer
1419 * @param buf Memory where to put the data to transmit
1420 * @return Size of data put in buffer
1423 send_p2p_tunnel_destroy (void *cls, size_t size, void *buf)
1425 struct MeshTunnel *t = cls;
1426 struct MeshClient *c;
1427 struct GNUNET_MESH_TunnelMessage *msg;
1431 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1432 /*FIXME*/ msg->header.size =
1433 htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1434 msg->tunnel_id = htonl (t->id.tid);
1436 tunnel_destroy (c, t);
1437 return sizeof (struct GNUNET_MESH_TunnelMessage);
1443 * Send the message to all clients that have subscribed to its type
1445 * @param msg Pointer to the message itself
1446 * @return number of clients this message was sent to
1449 send_subscribed_clients (struct GNUNET_MessageHeader *msg)
1451 struct MeshClient *c;
1455 type = ntohs (msg->type);
1456 for (count = 0, c = clients; c != NULL; c = c->next)
1458 if (client_is_subscribed (type, c))
1461 GNUNET_SERVER_notification_context_unicast (nc, c->handle, msg,
1471 * Notify the client that owns the tunnel that a peer has connected to it
1473 * @param t Tunnel whose owner to notify
1474 * @param id Short id of the peer that has connected
1477 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1479 struct GNUNET_MESH_PeerControl pc;
1481 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1482 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1483 pc.tunnel_id = htonl (t->local_tid);
1484 GNUNET_PEER_resolve (id, &pc.peer);
1485 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1486 &pc.header, GNUNET_NO);
1490 /******************************************************************************/
1491 /******************** MESH NETWORK HANDLERS **************************/
1492 /******************************************************************************/
1496 * Core handler for path creation
1497 * struct GNUNET_CORE_MessageHandler
1499 * @param cls closure
1500 * @param message message
1501 * @param peer peer identity this notification is about
1502 * @param atsi performance data
1503 * @return GNUNET_OK to keep the connection open,
1504 * GNUNET_SYSERR to close it (signal serious error)
1508 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1509 const struct GNUNET_MessageHeader *message,
1510 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1512 unsigned int own_pos;
1515 MESH_TunnelNumber tid;
1516 struct GNUNET_MESH_ManipulatePath *msg;
1517 struct GNUNET_PeerIdentity *pi;
1518 struct GNUNET_PeerIdentity id;
1519 GNUNET_HashCode hash;
1520 struct MeshPeerPath *path;
1521 struct MeshPeerInfo *dest_peer_info;
1522 struct MeshPeerInfo *orig_peer_info;
1523 struct MeshTunnel *t;
1525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1526 "MESH: Received a MESH path create msg\n");
1527 size = ntohs (message->size);
1528 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
1530 GNUNET_break_op (0);
1534 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
1535 if (size % sizeof (struct GNUNET_PeerIdentity))
1537 GNUNET_break_op (0);
1540 size /= sizeof (struct GNUNET_PeerIdentity);
1543 GNUNET_break_op (0);
1546 msg = (struct GNUNET_MESH_ManipulatePath *) message;
1548 tid = ntohl (msg->tid);
1549 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1550 t = tunnel_get (pi, tid);
1554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
1555 t = GNUNET_malloc (sizeof (struct MeshTunnel));
1556 t->id.oid = GNUNET_PEER_intern (pi);
1558 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
1560 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1562 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
1563 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1571 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
1572 if (NULL == dest_peer_info)
1574 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1575 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
1576 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
1578 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1580 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
1581 if (NULL == orig_peer_info)
1583 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1584 orig_peer_info->id = GNUNET_PEER_intern (pi);
1585 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
1586 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1589 path = path_new (size);
1591 for (i = 0; i < size; i++)
1593 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
1594 if (path->peers[i] == myid)
1598 { /* cannot be self, must be 'not found' */
1599 /* create path: self not found in path through self */
1600 GNUNET_break_op (0);
1601 path_destroy (path);
1602 /* FIXME error. destroy tunnel? leave for timeout? */
1605 if (own_pos == size - 1)
1607 /* It is for us! Send ack. */
1608 struct MeshDataDescriptor *info;
1611 path_add_to_origin (orig_peer_info, path); /* inverts path! */
1612 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1613 info->origin = &t->id;
1614 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
1615 GNUNET_assert (info->peer);
1616 for (j = 0; info->peer->core_transmit[j]; j++)
1624 info->handler_n = j;
1625 info->peer->core_transmit[j] =
1626 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1627 GNUNET_TIME_UNIT_FOREVER_REL, peer,
1628 sizeof (struct GNUNET_MessageHeader),
1629 &send_core_path_ack, info);
1633 /* It's for somebody else! Retransmit. */
1634 struct MeshPathInfo *path_info;
1636 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1638 path_info->path = path;
1639 path_info->peer = dest_peer_info;
1641 path_add_to_peer (dest_peer_info, path);
1642 GNUNET_PEER_resolve (path->peers[own_pos + 1], &id);
1643 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1644 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1645 sizeof (struct GNUNET_MessageHeader),
1646 &send_core_create_path, path_info);
1653 * Core handler for mesh network traffic going from the origin to a peer
1655 * @param cls closure
1656 * @param peer peer identity this notification is about
1657 * @param message message
1658 * @param atsi performance data
1659 * @return GNUNET_OK to keep the connection open,
1660 * GNUNET_SYSERR to close it (signal serious error)
1663 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1664 const struct GNUNET_MessageHeader *message,
1665 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1667 struct GNUNET_MESH_Unicast *msg;
1668 struct MeshTunnel *t;
1669 struct MeshPeerInfo *pi;
1672 size = ntohs (message->size);
1674 sizeof (struct GNUNET_MESH_Unicast) +
1675 sizeof (struct GNUNET_MessageHeader))
1680 msg = (struct GNUNET_MESH_Unicast *) message;
1681 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1684 /* TODO notify back: we don't know this tunnel */
1687 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
1688 &msg->destination.hashPubKey);
1691 /* TODO maybe feedback, log to statistics */
1696 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
1699 msg = GNUNET_malloc (size);
1700 memcpy (msg, message, size);
1701 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1702 GNUNET_TIME_UNIT_FOREVER_REL,
1703 path_get_first_hop (t->tree, pi->id),
1705 &send_core_data_raw, msg);
1711 * Core handler for mesh network traffic going from the origin to all peers
1713 * @param cls closure
1714 * @param message message
1715 * @param peer peer identity this notification is about
1716 * @param atsi performance data
1717 * @return GNUNET_OK to keep the connection open,
1718 * GNUNET_SYSERR to close it (signal serious error)
1721 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1722 const struct GNUNET_MessageHeader *message,
1723 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1725 struct GNUNET_MESH_Multicast *msg;
1726 struct GNUNET_PeerIdentity id;
1727 struct MeshDataDescriptor *info;
1728 struct MeshPathInfo neighbors;
1729 struct MeshTunnel *t;
1735 size = ntohs (message->size);
1737 sizeof (struct GNUNET_MESH_Multicast) +
1738 sizeof (struct GNUNET_MessageHeader))
1740 GNUNET_break_op (0);
1743 msg = (struct GNUNET_MESH_Multicast *) message;
1744 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1748 /* TODO notify that we dont know that tunnel */
1752 /* Transmit to locally interested clients */
1753 if (GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
1755 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
1758 /* Retransmit to other peers.
1759 * Using path here as just a collection of peers, not a path per se.
1762 neighbors.path = path_new (0);
1763 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &iterate_collect_neighbors,
1765 if (0 == neighbors.path->length)
1767 GNUNET_free (neighbors.path);
1770 size -= sizeof (struct GNUNET_MESH_Multicast);
1771 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor) + size);
1772 info->origin = &t->id;
1773 info->copies = neighbors.path->length;
1774 for (i = 0; i < info->copies; i++)
1776 GNUNET_PEER_resolve (neighbors.path->peers[i], &id);
1777 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
1778 GNUNET_assert (NULL != info->peer);
1779 for (j = 0; 0 != info->peer->core_transmit[j]; j++)
1781 if (j == (CORE_QUEUE_SIZE - 1))
1787 info->handler_n = j;
1788 info->peer->infos[j] = info;
1789 info->peer->core_transmit[j] =
1790 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1791 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1792 ntohs (msg->header.size),
1793 &send_core_data_multicast, info);
1795 GNUNET_free (neighbors.path->peers);
1796 GNUNET_free (neighbors.path);
1802 * Core handler for mesh network traffic
1804 * @param cls closure
1805 * @param message message
1806 * @param peer peer identity this notification is about
1807 * @param atsi performance data
1809 * @return GNUNET_OK to keep the connection open,
1810 * GNUNET_SYSERR to close it (signal serious error)
1813 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
1814 const struct GNUNET_MessageHeader *message,
1815 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1817 struct GNUNET_MESH_ToOrigin *msg;
1818 struct GNUNET_PeerIdentity id;
1819 struct MeshPeerInfo *peer_info;
1820 struct MeshTunnel *t;
1823 size = ntohs (message->size);
1824 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
1825 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
1827 GNUNET_break_op (0);
1830 msg = (struct GNUNET_MESH_ToOrigin *) message;
1831 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1835 /* TODO notify that we dont know this tunnel (whom)? */
1839 if (t->id.oid == myid)
1841 if (NULL == t->client)
1843 /* got data packet for ownerless tunnel */
1844 GNUNET_break_op (0);
1847 /* TODO signature verification */
1848 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle, message,
1852 peer_info = peer_info_get (&msg->oid);
1853 if (NULL == peer_info)
1855 /* unknown origin of tunnel */
1859 GNUNET_PEER_resolve (t->tree->me->parent->peer, &id);
1860 msg = GNUNET_malloc (size);
1861 memcpy (msg, message, size);
1862 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1863 GNUNET_TIME_UNIT_FOREVER_REL, &id, size,
1864 &send_core_data_raw, msg);
1871 * Core handler for path ACKs
1873 * @param cls closure
1874 * @param message message
1875 * @param peer peer identity this notification is about
1876 * @param atsi performance data
1878 * @return GNUNET_OK to keep the connection open,
1879 * GNUNET_SYSERR to close it (signal serious error)
1881 * FIXME path change state
1884 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1885 const struct GNUNET_MessageHeader *message,
1886 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1888 struct GNUNET_MESH_PathACK *msg;
1889 struct MeshTunnel *t;
1890 struct MeshPeerInfo *peer_info;
1892 msg = (struct GNUNET_MESH_PathACK *) message;
1893 t = tunnel_get (&msg->oid, msg->tid);
1896 /* TODO notify that we don't know the tunnel */
1900 /* Message for us? */
1901 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
1904 if (NULL == t->client)
1909 peer_info = peer_info_get (&msg->peer_id);
1910 if (NULL == peer_info)
1912 GNUNET_break_op (0);
1915 /* FIXME change state of peer */
1916 send_client_peer_connected(t, peer_info->id);
1920 peer_info = peer_info_get (&msg->oid);
1921 if (NULL == peer_info)
1923 /* If we know the tunnel, we should DEFINITELY know the peer */
1927 msg = GNUNET_malloc (sizeof (struct GNUNET_MESH_PathACK));
1928 memcpy (msg, message, sizeof (struct GNUNET_MESH_PathACK));
1929 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1930 GNUNET_TIME_UNIT_FOREVER_REL,
1931 path_get_first_hop (t->tree,
1933 sizeof (struct GNUNET_MESH_PathACK),
1934 &send_core_data_raw, msg);
1940 * Functions to handle messages from core
1942 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1943 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
1944 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
1945 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
1946 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
1947 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1948 sizeof (struct GNUNET_MESH_PathACK)},
1954 /******************************************************************************/
1955 /**************** MESH LOCAL HANDLER HELPERS ***********************/
1956 /******************************************************************************/
1959 * deregister_app: iterator for removing each application registered by a client
1961 * @param cls closure
1962 * @param key the hash of the application id (used to access the hashmap)
1963 * @param value the value stored at the key (client)
1965 * @return GNUNET_OK on success
1968 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
1970 GNUNET_CONTAINER_multihashmap_remove (applications, key, value);
1976 * notify_client_connection_failure: notify a client that the connection to the
1977 * requested remote peer is not possible (for instance, no route found)
1978 * Function called when the socket is ready to queue more data. "buf" will be
1979 * NULL and "size" zero if the socket was closed for writing in the meantime.
1981 * @param cls closure
1982 * @param size number of bytes available in buf
1983 * @param buf where the callee should write the message
1984 * @return number of bytes written to buf
1987 notify_client_connection_failure (void *cls, size_t size, void *buf)
1990 struct MeshPeerInfo *peer_info;
1991 struct GNUNET_MESH_PeerControl *msg;
1992 struct GNUNET_PeerIdentity id;
1994 if (0 == size && NULL == buf)
1996 // TODO retry? cancel?
2000 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
2001 peer_info = (struct MeshPeerInfo *) cls;
2002 msg = (struct GNUNET_MESH_PeerControl *) buf;
2003 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2004 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
2005 // msg->tunnel_id = htonl(peer_info->t->tid);
2006 GNUNET_PEER_resolve (peer_info->id, &id);
2007 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
2015 * Function to process paths received for a new peer addition. The recorded
2016 * paths form the initial tunnel, which can be optimized later.
2017 * Called on each result obtained for the DHT search.
2019 * @param cls closure
2020 * @param exp when will this value expire
2021 * @param key key of the result
2022 * @param get_path NULL-terminated array of pointers
2023 * to the peers on reverse GET path (or NULL if not recorded)
2024 * @param put_path NULL-terminated array of pointers
2025 * to the peers on the PUT path (or NULL if not recorded)
2026 * @param type type of the result
2027 * @param size number of bytes in data
2028 * @param data pointer to the result data
2033 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2034 const GNUNET_HashCode * key,
2035 const struct GNUNET_PeerIdentity *const *get_path,
2036 const struct GNUNET_PeerIdentity *const *put_path,
2037 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
2039 struct MeshPathInfo *path_info = cls;
2040 struct MeshPeerPath *p;
2041 struct GNUNET_PeerIdentity pi;
2044 if (NULL == get_path || NULL == put_path)
2046 if (NULL == path_info->peer->path_head)
2048 // Find ourselves some alternate initial path to the destination: retry
2049 GNUNET_DHT_get_stop (path_info->peer->dhtget);
2050 GNUNET_PEER_resolve (path_info->peer->id, &pi);
2051 path_info->peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2052 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
2053 GNUNET_BLOCK_TYPE_TEST, /* type */
2054 &pi.hashPubKey, /*key to search */
2055 4, /* replication level */
2056 GNUNET_DHT_RO_RECORD_ROUTE, NULL, /* bloom filter */
2059 0, /* xquery bits */
2061 (void *) path_info);
2066 p = path_build_from_dht (get_path, put_path);
2067 path_add_to_peer (path_info->peer, p);
2068 for (i = 0; i < path_info->peer->ntunnels; i++)
2070 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
2072 GNUNET_free (path_info);
2079 * Function to process paths received for a new peer addition. The recorded
2080 * paths form the initial tunnel, which can be optimized later.
2081 * Called on each result obtained for the DHT search.
2083 * @param cls closure
2084 * @param exp when will this value expire
2085 * @param key key of the result
2086 * @param get_path NULL-terminated array of pointers
2087 * to the peers on reverse GET path (or NULL if not recorded)
2088 * @param put_path NULL-terminated array of pointers
2089 * to the peers on the PUT path (or NULL if not recorded)
2090 * @param type type of the result
2091 * @param size number of bytes in data
2092 * @param data pointer to the result data
2095 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2096 const GNUNET_HashCode * key,
2097 const struct GNUNET_PeerIdentity *const *get_path,
2098 const struct GNUNET_PeerIdentity *const *put_path,
2099 enum GNUNET_BLOCK_Type type, size_t size,
2102 const struct GNUNET_PeerIdentity *pi = data;
2103 struct GNUNET_PeerIdentity id;
2104 struct MeshTunnel *t = cls;
2105 struct MeshPeerInfo *peer_info;
2106 struct MeshPathInfo *path_info;
2107 struct MeshPeerPath *p;
2110 if (size != sizeof (struct GNUNET_PeerIdentity))
2112 GNUNET_break_op (0);
2115 GNUNET_assert (NULL != t->client);
2116 GNUNET_DHT_get_stop (t->client->dht_get_type);
2117 t->client->dht_get_type = NULL;
2118 peer_info = peer_info_get (pi);
2119 GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey, peer_info,
2120 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2122 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path_head &&
2123 NULL == peer_info->dhtget)
2125 /* we don't have a route to the peer, let's try a direct lookup */
2126 peer_info->dhtget = GNUNET_DHT_get_start (dht_handle,
2128 GNUNET_TIME_UNIT_FOREVER_REL,
2130 GNUNET_BLOCK_TYPE_TEST,
2133 /* key to look up */
2135 /* replication level */
2136 GNUNET_DHT_RO_RECORD_ROUTE,
2137 /* option to dht: record route */
2138 NULL, /* bloom filter */
2141 0, /* xquery bits */
2144 peer_info); /* closure */
2147 p = path_build_from_dht (get_path, put_path);
2148 path_add_to_peer (peer_info, p);
2149 tunnel_add_peer(t, peer_info);
2150 p = tree_get_path_to_peer(t->tree, peer_info->id);
2152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2153 "MESH: new route for tunnel 0x%x found, has %u hops\n",
2154 t->local_tid, p->length);
2155 for (i = 0; i < p->length; i++)
2157 GNUNET_PEER_resolve (p->peers[0], &id);
2158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:\t%d\t%s\n", i,
2159 GNUNET_h2s_full (&id.hashPubKey));
2165 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
2167 path_info->peer = peer_info;
2168 path_info->path = p;
2169 GNUNET_PEER_resolve (p->peers[1], &id);
2170 GNUNET_CORE_notify_transmit_ready (core_handle,
2176 GNUNET_TIME_UNIT_FOREVER_REL,
2180 sizeof (struct GNUNET_MESH_ManipulatePath)
2183 sizeof (struct GNUNET_PeerIdentity)),
2185 &send_core_create_path,
2187 path_info); /* cls */
2191 send_client_peer_connected(t, myid);
2194 /******************************************************************************/
2195 /********************* MESH LOCAL HANDLES **************************/
2196 /******************************************************************************/
2200 * Handler for client disconnection
2202 * @param cls closure
2203 * @param client identification of the client; NULL
2204 * for the last call when the server is destroyed
2207 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
2209 struct MeshClient *c;
2210 struct MeshClient *next;
2212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
2214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
2218 if (c->handle != client && NULL != client)
2220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
2224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
2225 if (NULL != c->tunnels)
2227 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
2228 &tunnel_destroy_iterator,
2230 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
2233 /* deregister clients applications */
2234 if (NULL != c->apps)
2236 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
2237 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
2239 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
2240 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
2242 GNUNET_SCHEDULER_cancel (announce_applications_task);
2243 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
2245 if (NULL != c->types)
2246 GNUNET_CONTAINER_multihashmap_destroy (c->types);
2247 if (NULL != c->dht_get_type)
2248 GNUNET_DHT_get_stop (c->dht_get_type);
2249 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
2255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
2261 * Handler for new clients
2263 * @param cls closure
2264 * @param client identification of the client
2265 * @param message the actual message, which includes messages the client wants
2268 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
2269 const struct GNUNET_MessageHeader *message)
2271 struct GNUNET_MESH_ClientConnect *cc_msg;
2272 struct MeshClient *c;
2273 GNUNET_MESH_ApplicationType *a;
2280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
2281 /* Check data sanity */
2282 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
2283 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
2284 ntypes = ntohs (cc_msg->types);
2285 napps = ntohs (cc_msg->applications);
2287 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
2290 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2294 /* Create new client structure */
2295 c = GNUNET_malloc (sizeof (struct MeshClient));
2297 c->id = next_client_id++;
2300 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
2303 GNUNET_MESH_ApplicationType at;
2306 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
2307 for (i = 0; i < napps; i++)
2310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
2311 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
2312 /* store in clients hashmap */
2313 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
2314 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2315 /* store in global hashmap, for announcements */
2316 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
2317 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2319 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
2320 announce_applications_task =
2321 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
2329 t = (uint16_t *) & a[napps];
2330 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
2331 for (i = 0; i < ntypes; i++)
2334 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
2336 /* store in clients hashmap */
2337 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
2338 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2339 /* store in global hashmap */
2340 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
2341 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2345 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
2347 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
2348 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
2349 GNUNET_SERVER_notification_context_add (nc, client);
2351 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
2359 * Handler for requests of new tunnels
2361 * @param cls closure
2362 * @param client identification of the client
2363 * @param message the actual message
2366 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
2367 const struct GNUNET_MessageHeader *message)
2369 struct GNUNET_MESH_TunnelMessage *t_msg;
2370 struct MeshTunnel *t;
2371 struct MeshClient *c;
2372 GNUNET_HashCode hash;
2374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
2376 /* Sanity check for client registration */
2377 if (NULL == (c = client_get (client)))
2380 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2387 /* Message sanity check */
2388 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2391 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2395 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2396 /* Sanity check for tunnel numbering */
2397 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
2400 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2403 /* Sanity check for duplicate tunnel IDs */
2404 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
2407 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2411 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATED TUNNEL at %p\n", t);
2413 while (NULL != tunnel_get_by_pi (myid, next_tid))
2414 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
2415 t->id.tid = next_tid++;
2417 t->local_tid = ntohl (t_msg->tunnel_id);
2419 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
2421 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2423 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
2424 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2427 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2431 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2433 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2434 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2437 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2440 t->tree = tree_new (t, myid);
2441 t->tree->refresh = REFRESH_PATH_TIME;
2442 t->tree->root->status = MESH_PEER_READY;
2443 t->tree->me = t->tree->root;
2445 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2451 * Handler for requests of deleting tunnels
2453 * @param cls closure
2454 * @param client identification of the client
2455 * @param message the actual message
2458 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
2459 const struct GNUNET_MessageHeader *message)
2461 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
2462 struct MeshClient *c;
2463 struct MeshTunnel *t;
2464 MESH_TunnelNumber tid;
2465 GNUNET_HashCode hash;
2467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel\n");
2469 /* Sanity check for client registration */
2470 if (NULL == (c = client_get (client)))
2473 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2476 /* Message sanity check */
2477 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2480 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2486 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2488 /* Retrieve tunnel */
2489 tid = ntohl (tunnel_msg->tunnel_id);
2491 /* Remove from local id hashmap */
2492 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2493 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
2494 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t);
2496 /* Remove from global id hashmap */
2497 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2498 GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t);
2500 // notify_tunnel_destroy(t); FIXME
2502 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2508 * Handler for connection requests to new peers
2510 * @param cls closure
2511 * @param client identification of the client
2512 * @param message the actual message (PeerControl)
2517 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
2518 const struct GNUNET_MessageHeader *message)
2520 struct GNUNET_MESH_PeerControl *peer_msg;
2521 struct MeshClient *c;
2522 struct MeshTunnel *t;
2523 MESH_TunnelNumber tid;
2524 struct MeshPeerInfo *peer_info;
2527 /* Sanity check for client registration */
2528 if (NULL == (c = client_get (client)))
2531 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2535 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2536 /* Sanity check for message size */
2537 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2540 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2544 /* Tunnel exists? */
2545 tid = ntohl (peer_msg->tunnel_id);
2546 t = tunnel_get_by_local_id (c, tid);
2550 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2554 /* Does client own tunnel? */
2555 if (t->client->handle != client)
2558 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2563 peer_info = peer_info_get (&peer_msg->peer);
2565 /* Start DHT search if needed FIXME: if not already connected */
2566 if (NULL == peer_info->dhtget)
2568 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 */
2569 GNUNET_DHT_RO_RECORD_ROUTE, NULL, /* bloom filter */
2572 0, /* xquery bits */
2574 (void *) peer_info);
2577 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2583 * Handler for disconnection requests of peers in a tunnel
2585 * @param cls closure
2586 * @param client identification of the client
2587 * @param message the actual message (PeerControl)
2590 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
2591 const struct GNUNET_MessageHeader *message)
2593 struct GNUNET_MESH_PeerControl *peer_msg;
2594 struct MeshClient *c;
2595 struct MeshTunnel *t;
2596 MESH_TunnelNumber tid;
2598 /* Sanity check for client registration */
2599 if (NULL == (c = client_get (client)))
2602 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2605 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2606 /* Sanity check for message size */
2607 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2610 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2614 /* Tunnel exists? */
2615 tid = ntohl (peer_msg->tunnel_id);
2616 t = tunnel_get_by_local_id (c, tid);
2620 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2624 /* Does client own tunnel? */
2625 if (t->client->handle != client)
2628 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2632 /* Ok, delete peer from tunnel */
2633 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
2634 &peer_msg->peer.hashPubKey);
2636 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2642 * Handler for connection requests to new peers by type
2644 * @param cls closure
2645 * @param client identification of the client
2646 * @param message the actual message (ConnectPeerByType)
2649 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
2650 const struct GNUNET_MessageHeader *message)
2652 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
2653 struct MeshClient *c;
2654 struct MeshTunnel *t;
2655 GNUNET_HashCode hash;
2656 GNUNET_MESH_ApplicationType type;
2657 MESH_TunnelNumber tid;
2659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
2660 /* Sanity check for client registration */
2661 if (NULL == (c = client_get (client)))
2664 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2668 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
2669 /* Sanity check for message size */
2670 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
2671 ntohs (connect_msg->header.size))
2674 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2678 /* Tunnel exists? */
2679 tid = ntohl (connect_msg->tunnel_id);
2680 t = tunnel_get_by_local_id (c, tid);
2684 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2688 /* Does client own tunnel? */
2689 if (t->client->handle != client)
2692 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2696 /* Do WE have the service? */
2697 type = ntohl (connect_msg->type);
2698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", type);
2699 GNUNET_CRYPTO_hash (&type, sizeof (GNUNET_MESH_ApplicationType), &hash);
2700 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
2703 /* Yes! Fast forward, add ourselves to the tunnel and send the
2704 * good news to the client
2706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
2707 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
2708 peer_info_get (&my_full_id),
2709 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
2712 send_client_peer_connected(t, myid);
2713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
2714 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2717 /* Ok, lets find a peer offering the service */
2718 if (c->dht_get_type)
2720 GNUNET_DHT_get_stop (c->dht_get_type);
2722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
2723 GNUNET_h2s_full (&hash));
2725 GNUNET_DHT_get_start (dht_handle, GNUNET_TIME_UNIT_FOREVER_REL,
2726 GNUNET_BLOCK_TYPE_TEST, &hash, 10U,
2727 GNUNET_DHT_RO_RECORD_ROUTE, NULL, 0, NULL, 0,
2728 &dht_get_type_handler, t);
2730 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2736 * Handler for client traffic directed to one peer
2738 * @param cls closure
2739 * @param client identification of the client
2740 * @param message the actual message
2743 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
2744 const struct GNUNET_MessageHeader *message)
2746 struct MeshClient *c;
2747 struct MeshTunnel *t;
2748 struct MeshPeerInfo *pi;
2749 struct GNUNET_MESH_Unicast *data_msg;
2750 struct MeshDataDescriptor *info;
2751 MESH_TunnelNumber tid;
2754 /* Sanity check for client registration */
2755 if (NULL == (c = client_get (client)))
2758 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2761 data_msg = (struct GNUNET_MESH_Unicast *) message;
2762 /* Sanity check for message size */
2763 if (sizeof (struct GNUNET_MESH_Unicast) +
2764 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
2767 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2771 /* Tunnel exists? */
2772 tid = ntohl (data_msg->tid);
2773 t = tunnel_get_by_local_id (c, tid);
2777 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2781 /* Is it a local tunnel? Then, does client own the tunnel? */
2782 if (t->client->handle != NULL && t->client->handle != client)
2785 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2789 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
2790 &data_msg->destination.hashPubKey);
2791 /* Is the selected peer in the tunnel? */
2795 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2800 struct GNUNET_MESH_Unicast copy;
2802 /* Work around const limitation */
2803 memcpy (©, data_msg, sizeof (struct GNUNET_MESH_Unicast));
2804 copy.oid = my_full_id;
2805 copy.tid = htonl (t->id.tid);
2806 handle_mesh_data_unicast (NULL, &my_full_id, ©.header, NULL);
2809 data_size = ntohs (message->size) - sizeof (struct GNUNET_MESH_Unicast);
2810 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor) + data_size);
2811 memcpy (&info[1], &data_msg[1], data_size);
2812 info->destination = pi->id;
2813 info->origin = &t->id;
2814 info->size = data_size;
2815 info->client = client;
2816 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2817 GNUNET_TIME_UNIT_FOREVER_REL,
2818 path_get_first_hop (t->tree, pi->id),
2819 /* FIXME re-check types */
2821 sizeof (struct GNUNET_MESH_Unicast),
2822 &send_core_data_unicast, info);
2827 * Handler for client traffic directed to all peers in a tunnel
2829 * @param cls closure
2830 * @param client identification of the client
2831 * @param message the actual message
2834 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
2835 const struct GNUNET_MessageHeader *message)
2837 struct MeshClient *c;
2838 struct MeshTunnel *t;
2839 struct GNUNET_MESH_Multicast *data_msg;
2840 MESH_TunnelNumber tid;
2842 /* Sanity check for client registration */
2843 if (NULL == (c = client_get (client)))
2846 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2849 data_msg = (struct GNUNET_MESH_Multicast *) message;
2850 /* Sanity check for message size */
2851 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (data_msg->header.size))
2854 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2858 /* Tunnel exists? */
2859 tid = ntohl (data_msg->tid);
2860 t = tunnel_get_by_local_id (c, tid);
2864 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2868 /* Does client own tunnel? */
2869 if (t->client->handle != client)
2872 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2878 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2883 * Functions to handle messages from clients
2885 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
2886 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
2887 {&handle_local_tunnel_create, NULL,
2888 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
2889 sizeof (struct GNUNET_MESH_TunnelMessage)},
2890 {&handle_local_tunnel_destroy, NULL,
2891 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
2892 sizeof (struct GNUNET_MESH_TunnelMessage)},
2893 {&handle_local_connect_add, NULL,
2894 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
2895 sizeof (struct GNUNET_MESH_PeerControl)},
2896 {&handle_local_connect_del, NULL,
2897 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
2898 sizeof (struct GNUNET_MESH_PeerControl)},
2899 {&handle_local_connect_by_type, NULL,
2900 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
2901 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
2902 {&handle_local_unicast, NULL,
2903 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
2904 {&handle_local_unicast, NULL,
2905 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
2906 {&handle_local_multicast, NULL,
2907 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
2913 * To be called on core init/fail.
2915 * @param cls service closure
2916 * @param server handle to the server for this service
2917 * @param identity the public identity of this peer
2918 * @param publicKey the public key of this peer
2921 core_init (void *cls, struct GNUNET_CORE_Handle *server,
2922 const struct GNUNET_PeerIdentity *identity,
2923 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
2925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
2926 core_handle = server;
2927 if (0 != memcmp(identity, &my_full_id, sizeof(my_full_id)) || NULL == server)
2929 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
2930 GNUNET_SCHEDULER_shutdown();
2936 * Method called whenever a given peer connects.
2938 * @param cls closure
2939 * @param peer peer identity this notification is about
2940 * @param atsi performance data for the connection
2943 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
2944 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2946 // GNUNET_PEER_Id pid;
2947 struct MeshPeerInfo *peer_info;
2948 struct MeshPeerPath *path;
2950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
2951 peer_info = peer_info_get (peer);
2952 if (myid == peer_info->id)
2954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
2957 path = path_new (2);
2958 path->peers[0] = myid;
2959 path->peers[1] = peer_info->id;
2960 path_add_to_peer (peer_info, path);
2965 * Method called whenever a peer disconnects.
2967 * @param cls closure
2968 * @param peer peer identity this notification is about
2971 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
2973 struct MeshPeerInfo *pi;
2976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
2977 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2983 for (i = 0; i < CORE_QUEUE_SIZE; i++)
2985 if (pi->core_transmit[i])
2987 GNUNET_CORE_notify_transmit_ready_cancel (pi->core_transmit[i]);
2988 /* TODO: notify that tranmission has failed */
2989 GNUNET_free (pi->infos[i]);
2992 path_remove_from_peer (pi, pi->id, myid);
2995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3001 /******************************************************************************/
3002 /************************ MAIN FUNCTIONS ****************************/
3003 /******************************************************************************/
3006 * Task run during shutdown.
3012 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3014 struct MeshClient *c;
3016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
3017 if (core_handle != NULL)
3019 GNUNET_CORE_disconnect (core_handle);
3022 if (dht_handle != NULL)
3024 for (c = clients; NULL != c; c = c->next)
3025 if (NULL != c->dht_get_type)
3026 GNUNET_DHT_get_stop (c->dht_get_type);
3027 GNUNET_DHT_disconnect (dht_handle);
3032 GNUNET_SERVER_notification_context_destroy (nc);
3035 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
3037 GNUNET_SCHEDULER_cancel (announce_id_task);
3038 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
3040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
3044 * Process mesh requests.
3046 * @param cls closure
3047 * @param server the initialized server
3048 * @param c configuration to use
3051 run (void *cls, struct GNUNET_SERVER_Handle *server,
3052 const struct GNUNET_CONFIGURATION_Handle *c)
3054 struct MeshPeerInfo *peer;
3055 struct MeshPeerPath *p;
3058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
3059 server_handle = server;
3060 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
3061 CORE_QUEUE_SIZE, /* queue size */
3062 NULL, /* Closure passed to MESH functions */
3063 &core_init, /* Call core_init once connected */
3064 &core_connect, /* Handle connects */
3065 &core_disconnect, /* remove peers on disconnects */
3066 NULL, /* Do we care about "status" updates? */
3067 NULL, /* Don't notify about all incoming messages */
3068 GNUNET_NO, /* For header only in notification */
3069 NULL, /* Don't notify about all outbound messages */
3070 GNUNET_NO, /* For header-only out notification */
3071 core_handlers); /* Register these handlers */
3072 if (core_handle == NULL)
3075 GNUNET_SCHEDULER_shutdown ();
3080 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
3083 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3085 ("Mesh service is lacking key configuration settings. Exiting.\n"));
3086 GNUNET_SCHEDULER_shutdown ();
3089 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3090 GNUNET_free (keyfile);
3091 if (my_private_key == NULL)
3093 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3094 _("Mesh service could not access hostkey. Exiting.\n"));
3095 GNUNET_SCHEDULER_shutdown ();
3098 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
3099 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
3100 &my_full_id.hashPubKey);
3101 myid = GNUNET_PEER_intern (&my_full_id);
3103 dht_handle = GNUNET_DHT_connect (c, 64);
3104 if (dht_handle == NULL)
3106 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error connecting to DHT.\
3107 Running without DHT has a severe\
3108 impact in MESH capabilities.\n\
3109 Plase check your configuretion and enable DHT.\n");
3115 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3116 peers = GNUNET_CONTAINER_multihashmap_create (32);
3117 applications = GNUNET_CONTAINER_multihashmap_create (32);
3118 types = GNUNET_CONTAINER_multihashmap_create (32);
3120 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
3121 nc = GNUNET_SERVER_notification_context_create (server_handle,
3123 GNUNET_SERVER_disconnect_notify (server_handle,
3124 &handle_local_client_disconnect,
3129 clients_tail = NULL;
3134 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3135 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
3137 /* Create a peer_info for the local peer */
3138 peer = peer_info_get(&my_full_id);
3141 path_add_to_peer(peer, p);
3143 /* Scheduled the task to clean up when shutdown is called */
3144 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
3147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
3151 * The main function for the mesh service.
3153 * @param argc number of arguments from the command line
3154 * @param argv command line arguments
3155 * @return 0 ok, 1 on error
3158 main (int argc, char *const *argv)
3163 fprintf (stderr, "main ()\n");
3165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
3168 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
3170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");
3172 fprintf (stderr, "main () END\n");