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,\
65 #define GET_RESTART_TIME GNUNET_TIME_relative_multiply(\
66 GNUNET_TIME_UNIT_SECONDS,\
69 /******************************************************************************/
70 /************************ DATA STRUCTURES ****************************/
71 /******************************************************************************/
73 /** FWD declaration */
77 * Struct containing all info possibly needed to build a package when called
80 struct MeshDataDescriptor
82 /** ID of the tunnel this packet travels in */
83 struct MESH_TunnelID *origin;
88 /** Client that asked for the transmission, if any */
89 struct GNUNET_SERVER_Client *client;
91 /** Who was this message being sent to */
92 struct MeshPeerInfo *peer;
94 /** Ultimate destination of the packet */
95 GNUNET_PEER_Id destination;
97 /** Number of identical messages sent to different hops (multicast) */
100 /** Which handler was used to request the transmission */
101 unsigned int handler_n;
103 /** Size of the data */
110 * Struct containing all information regarding a given peer
120 * Last time we heard from this peer
122 struct GNUNET_TIME_Absolute last_contact;
125 * Number of attempts to reconnect so far
127 int n_reconnect_attempts;
130 * Paths to reach the peer, ordered by ascending hop count
132 struct MeshPeerPath *path_head;
135 * Paths to reach the peer, ordered by ascending hop count
137 struct MeshPeerPath *path_tail;
140 * Handle to stop the DHT search for a path to this peer
142 struct GNUNET_DHT_GetHandle *dhtget;
145 * Handles to stop queued transmissions for this peer
147 struct GNUNET_CORE_TransmitHandle *core_transmit[CORE_QUEUE_SIZE];
150 * Pointer to info stuctures used as cls for queued transmissions
152 void *infos[CORE_QUEUE_SIZE];
155 * Type of message being in each transmission
157 uint16_t types[CORE_QUEUE_SIZE];
160 * Array of tunnels this peer participates in
161 * (most probably a small amount, therefore not a hashmap)
162 * When the path to the peer changes, notify these tunnels to let them
163 * re-adjust their path trees.
165 struct MeshTunnel **tunnels;
168 * Number of tunnels this peers participates in
170 unsigned int ntunnels;
175 * Data scheduled to transmit (to local client or remote peer)
182 struct MeshQueue *next;
183 struct MeshQueue *prev;
186 * Target of the data (NULL if target is client)
188 struct MeshPeerInfo *peer;
191 * Client to send the data to (NULL if target is peer)
193 struct MeshClient *client;
196 * Size of the message to transmit
201 * How old is the data?
203 struct GNUNET_TIME_Absolute timestamp;
208 struct GNUNET_MessageHeader *data;
212 * Globally unique tunnel identification (owner + number)
213 * DO NOT USE OVER THE NETWORK
218 * Node that owns the tunnel
223 * Tunnel number to differentiate all the tunnels owned by the node oid
224 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
226 MESH_TunnelNumber tid;
230 struct MeshClient; /* FWD declaration */
233 * Struct containing all information regarding a tunnel
234 * For an intermediate node the improtant info used will be:
235 * - id Tunnel unique identification
236 * - paths[0] To know where to send it next
237 * - metainfo: ready, speeds, accounting
244 struct MESH_TunnelID id;
247 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
249 MESH_TunnelNumber local_tid;
252 * Last time the tunnel was used
254 struct GNUNET_TIME_Absolute timestamp;
257 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
259 struct GNUNET_CONTAINER_MultiHashMap *peers;
262 * Number of peers that are connected and potentially ready to receive data
264 unsigned int peers_ready;
267 * Number of peers that have been added to the tunnel
269 unsigned int peers_total;
272 * Client owner of the tunnel, if any
274 struct MeshClient *client;
277 * Messages ready to transmit
279 struct MeshQueue *queue_head;
280 struct MeshQueue *queue_tail;
285 struct MeshTunnelTree *tree;
288 * Application type we are looking for in this tunnel
290 GNUNET_MESH_ApplicationType type;
293 * Used to search peers offering a service
295 struct GNUNET_DHT_GetHandle *dht_get_type;
298 * Task to keep the used paths alive
300 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
305 * Info needed to work with tunnel paths and peers
312 struct MeshTunnel *t;
317 struct MeshPeerInfo *peer;
322 struct MeshPeerPath *path;
325 * Position in peer's transmit queue
332 * Struct containing information about a client of the service
339 struct MeshClient *next;
340 struct MeshClient *prev;
343 * Tunnels that belong to this client, indexed by local id
345 struct GNUNET_CONTAINER_MultiHashMap *tunnels;
348 * Handle to communicate with the client
350 struct GNUNET_SERVER_Client *handle;
353 * Applications that this client has claimed to provide
355 struct GNUNET_CONTAINER_MultiHashMap *apps;
358 * Messages that this client has declared interest in
360 struct GNUNET_CONTAINER_MultiHashMap *types;
364 * ID of the client, for debug messages
373 /******************************************************************************/
374 /************************ DEBUG FUNCTIONS ****************************/
375 /******************************************************************************/
379 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
380 * @param cls string to print
381 * @param tc task context
384 mesh_debug (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
388 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n", s);
396 /******************************************************************************/
397 /*********************** GLOBAL VARIABLES ****************************/
398 /******************************************************************************/
403 static struct MeshClient *clients;
404 static struct MeshClient *clients_tail;
407 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
409 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
412 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
414 static struct GNUNET_CONTAINER_MultiHashMap *peers;
417 * Handle to communicate with core
419 static struct GNUNET_CORE_Handle *core_handle;
424 static struct GNUNET_DHT_Handle *dht_handle;
429 static struct GNUNET_SERVER_Handle *server_handle;
432 * Notification context, to send messages to local clients
434 static struct GNUNET_SERVER_NotificationContext *nc;
437 * Local peer own ID (memory efficient handle)
439 static GNUNET_PEER_Id myid;
442 * Local peer own ID (full value)
444 static struct GNUNET_PeerIdentity my_full_id;
449 static struct GNUNET_CRYPTO_RsaPrivateKey* my_private_key;
454 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
457 * Tunnel ID for the next created tunnel (global tunnel number)
459 static MESH_TunnelNumber next_tid;
462 * Tunnel ID for the next incoming tunnel (local tunnel number)
464 static MESH_TunnelNumber next_local_tid;
467 * All application types provided by this peer
469 static struct GNUNET_CONTAINER_MultiHashMap *applications;
472 * All message types clients of this peer are interested in
474 static struct GNUNET_CONTAINER_MultiHashMap *types;
477 * Task to periodically announce provided applications
479 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
482 * Task to periodically announce itself in the network
484 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
487 unsigned int next_client_id;
491 /******************************************************************************/
492 /************************ ITERATORS ****************************/
493 /******************************************************************************/
495 /* FIXME move iterators here */
498 /******************************************************************************/
499 /************************ PERIODIC FUNCTIONS ****************************/
500 /******************************************************************************/
503 * Announce iterator over for each application provided by the peer
506 * @param key current key code
507 * @param value value in the hash map
508 * @return GNUNET_YES if we should continue to
513 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
515 /* FIXME are hashes in multihash map equal on all aquitectures? */
516 GNUNET_DHT_put (dht_handle,
519 GNUNET_DHT_RO_RECORD_ROUTE |
520 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
521 GNUNET_BLOCK_TYPE_TEST,
522 sizeof (struct GNUNET_PeerIdentity),
523 (const char *) &my_full_id,
525 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
526 &mesh_debug, "DHT_put for app completed");
528 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
530 APP_ANNOUNCE_TIME, NULL, NULL);
537 * Periodically announce what applications are provided by local clients
540 * @param tc task context
543 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
545 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
547 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Starting PUT for apps\n");
551 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
553 announce_applications_task =
554 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Finished PUT for apps\n");
562 * Periodically announce self id in the DHT
565 * @param tc task context
568 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
570 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
572 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
576 * - Set data expiration in function of X
579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DHT_put for ID %s started.\n",
580 GNUNET_h2s_full (&my_full_id.hashPubKey));
581 GNUNET_DHT_put (dht_handle, /* DHT handle */
582 &my_full_id.hashPubKey, /* Key to use */
583 10U, /* Replication level */
584 GNUNET_DHT_RO_RECORD_ROUTE |
585 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
586 GNUNET_BLOCK_TYPE_TEST, /* Block type */
587 sizeof(my_full_id), /* Size of the data */
588 (char *)&my_full_id, /* Data itself */
589 GNUNET_TIME_absolute_get_forever (), /* Data expiration */
590 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
592 &mesh_debug, "DHT_put for id completed");
594 NULL, /* Continuation */
595 NULL); /* Continuation closure */
598 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
603 * Function to process paths received for a new peer addition. The recorded
604 * paths form the initial tunnel, which can be optimized later.
605 * Called on each result obtained for the DHT search.
608 * @param exp when will this value expire
609 * @param key key of the result
610 * @param type type of the result
611 * @param size number of bytes in data
612 * @param data pointer to the result data
615 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
616 const GNUNET_HashCode * key,
617 const struct GNUNET_PeerIdentity *get_path,
618 unsigned int get_path_length,
619 const struct GNUNET_PeerIdentity *put_path,
620 unsigned int put_path_length,
621 enum GNUNET_BLOCK_Type type, size_t size, const void *data);
624 /******************************************************************************/
625 /****************** GENERAL HELPER FUNCTIONS ************************/
626 /******************************************************************************/
629 * Check if client has registered with the service and has not disconnected
631 * @param client the client to check
633 * @return non-NULL if client exists in the global DLL
635 static struct MeshClient *
636 client_get (struct GNUNET_SERVER_Client *client)
638 struct MeshClient *c;
643 if (c->handle == client)
652 * Checks if a given client has subscribed to certain message type
654 * @param message_type Type of message to check
655 * @param c Client to check
657 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
662 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
666 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
667 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
672 * Send the message to all clients that have subscribed to its type
674 * @param msg Pointer to the message itself
675 * @return number of clients this message was sent to
678 send_subscribed_clients (struct GNUNET_MessageHeader *msg)
680 struct MeshClient *c;
684 type = ntohs (msg->type);
685 for (count = 0, c = clients; c != NULL; c = c->next)
687 if (client_is_subscribed (type, c))
690 GNUNET_SERVER_notification_context_unicast (nc, c->handle, msg,
699 * Notify the client that owns the tunnel that a peer has connected to it
701 * @param t Tunnel whose owner to notify
702 * @param id Short id of the peer that has connected
705 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
707 struct GNUNET_MESH_PeerControl pc;
709 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
710 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
711 pc.tunnel_id = htonl (t->local_tid);
712 GNUNET_PEER_resolve (id, &pc.peer);
713 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
714 &pc.header, GNUNET_NO);
719 * Function called to notify a client about the socket
720 * being ready to queue more data. "buf" will be
721 * NULL and "size" zero if the socket was closed for
722 * writing in the meantime.
725 * @param size number of bytes available in buf
726 * @param buf where the callee should write the message
727 * @return number of bytes written to buf
730 send_core_create_path (void *cls, size_t size, void *buf);
733 * Cancel a core transmission that was already requested and free all resources
734 * associated to the request.
736 * @param peer PeeInfo of the peer whose transmission is cancelled.
737 * @param i Position of the transmission to be cancelled.
740 peer_info_cancel_transmission(struct MeshPeerInfo *peer, unsigned int i)
742 if (peer->core_transmit[i])
744 struct MeshDataDescriptor *dd;
745 struct MeshPathInfo *path_info;
746 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit[i]);
747 /* TODO: notify that tranmission has failed */
748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
749 "MESH: Cancelled data transmission at %u\n",
751 switch (peer->types[i])
753 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
754 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
755 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type payload\n");
758 if (0 == --(*dd->copies))
760 GNUNET_free (dd->copies);
761 GNUNET_free (dd->data);
764 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type create path\n");
766 path_info = peer->infos[i];
767 path_destroy(path_info->path);
770 GNUNET_free (peer->infos[i]);
779 peer_info_transmit_slot (struct MeshPeerInfo *peer)
783 for (i = 0; peer->core_transmit[i]; i++)
785 if (i == (CORE_QUEUE_SIZE - 1))
787 /* All positions are taken! Overwriting! */
789 peer_info_cancel_transmission(peer, 0);
798 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
799 * and insert it in the appropiate structures if the peer is not known yet.
801 * @param peer Full identity of the peer.
803 * @return Existing or newly created peer info.
805 static struct MeshPeerInfo *
806 peer_info_get (const struct GNUNET_PeerIdentity *peer)
808 struct MeshPeerInfo *peer_info;
810 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
811 if (NULL == peer_info)
814 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
815 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
816 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
817 peer_info->id = GNUNET_PEER_intern (peer);
825 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
826 * and insert it in the appropiate structures if the peer is not known yet.
828 * @param peer Short identity of the peer.
830 * @return Existing or newly created peer info.
832 static struct MeshPeerInfo *
833 peer_info_get_short (const GNUNET_PEER_Id peer)
835 struct GNUNET_PeerIdentity id;
837 GNUNET_PEER_resolve(peer, &id);
838 return peer_info_get(&id);
843 * Sends a CREATE PATH message for a path to a peer, properly registrating
844 * all used resources.
846 * @param peer PeerInfo of the final peer for whom this path is being created.
847 * @param p Path itself.
848 * @param t Tunnel for which the path is created.
851 send_create_path (struct MeshPeerInfo *peer,
852 struct MeshPeerPath *p,
853 struct MeshTunnel *t)
855 struct GNUNET_PeerIdentity id;
856 struct MeshPathInfo *path_info;
857 struct MeshPeerInfo *neighbor;
862 p = tree_get_path_to_peer(t->tree, peer->id);
869 for (i = 0; i < p->length; i++)
871 if (p->peers[i] == myid)
874 if (i >= p->length - 1)
880 GNUNET_PEER_resolve(p->peers[i + 1], &id);
882 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
884 path_info->peer = peer;
886 neighbor = peer_info_get(&id);
887 path_info->pos = peer_info_transmit_slot(neighbor);
888 neighbor->types[path_info->pos] = GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE;
889 neighbor->infos[path_info->pos] = path_info;
890 neighbor->core_transmit[path_info->pos] =
891 GNUNET_CORE_notify_transmit_ready (
892 core_handle, /* handle */
895 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
897 sizeof (struct GNUNET_MESH_ManipulatePath)
898 + (p->length * sizeof (struct GNUNET_PeerIdentity)), /*size */
899 &send_core_create_path, /* callback */
900 path_info); /* cls */
905 * Try to establish a new connection to this peer.
906 * Use the best path for the given tunnel.
907 * If the peer doesn't have any path to it yet, try to get one.
908 * If the peer already has some path, send a CREATE PATH towards it.
910 * @param peer PeerInfo of the peer.
911 * @param t Tunnel for which to create the path, if possible.
914 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
916 struct MeshPeerPath *p;
917 struct MeshPathInfo *path_info;
919 if (NULL != peer->path_head)
921 p = tree_get_path_to_peer(t->tree, peer->id);
924 send_create_path(peer, p, t);
929 send_client_peer_connected(t, myid);
932 else if (NULL == peer->dhtget)
934 struct GNUNET_PeerIdentity id;
936 GNUNET_PEER_resolve(peer->id, &id);
937 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
938 path_info->peer = peer;
941 GNUNET_DHT_get_start(dht_handle, /* handle */
942 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
943 GNUNET_BLOCK_TYPE_TEST, /* type */
944 &id.hashPubKey, /* key to search */
945 4, /* replication level */
946 GNUNET_DHT_RO_RECORD_ROUTE |
947 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
953 /* Otherwise, there is no path but the DHT get is already started. */
959 * Destroy the peer_info and free any allocated resources linked to it
960 * @param t tunnel the path belongs to
961 * @param pi the peer_info to destroy
962 * @return GNUNET_OK on success
965 peer_info_destroy (struct MeshPeerInfo *pi)
967 GNUNET_HashCode hash;
968 struct GNUNET_PeerIdentity id;
970 GNUNET_PEER_resolve (pi->id, &id);
971 GNUNET_PEER_change_rc (pi->id, -1);
972 GNUNET_CRYPTO_hash (&id, sizeof (struct GNUNET_PeerIdentity), &hash);
974 GNUNET_CONTAINER_multihashmap_remove (peers, &hash, pi);
975 GNUNET_SCHEDULER_cancel (pi->path_refresh_task);
983 * Notify a tunnel that a connection has broken that affects at least
986 * @param t Tunnel affected.
987 * @param peer Peer that (at least) has been affected by the disconnection.
988 * @param p1 Peer that got disconnected from p2.
989 * @param p2 Peer that got disconnected from p1.
991 * @return Short ID of the peer disconnected (either p1 or p2).
992 * 0 if the tunnel remained unaffected.
994 static GNUNET_PEER_Id
995 tunnel_notify_connection_broken (struct MeshTunnel *t,
996 struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1000 * Remove all paths that rely on a direct connection between p1 and p2
1001 * from the peer itself and notify all tunnels about it.
1003 * @param peer PeerInfo of affected peer.
1004 * @param p1 GNUNET_PEER_Id of one peer.
1005 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1008 * TODO: optimize (see below)
1011 path_remove_from_peer (struct MeshPeerInfo *peer,
1015 struct GNUNET_PeerIdentity id;
1016 struct MeshPeerPath *p;
1017 struct MeshPeerPath *aux;
1018 struct MeshPeerInfo *peer_d;
1020 unsigned int destroyed;
1026 p = peer->path_head;
1030 for (i = 0; i < (p->length - 1); i++)
1032 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1033 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1045 for (i = 0; i < peer->ntunnels; i++)
1047 d = tunnel_notify_connection_broken (peer->tunnels[i], peer, p1, p2);
1049 * Problem: one or more peers have been deleted from the tunnel tree.
1050 * We don't know who they are to try to add them again.
1051 * We need to try to find a new path for each of the disconnected peers.
1052 * Some of them might already have a path to reach them that does not
1053 * involve p1 and p2. Adding all anew might render in a better tree than
1054 * the trivial immediate fix.
1056 * Trivial immiediate fix: try to reconnect to the disconnected node. All
1057 * its children will be reachable trough him.
1059 peer_d = peer_info_get_short(d);
1062 for (p = peer_d->path_head; NULL != p; p = p->next)
1064 if ((cost = path_get_cost(peer->tunnels[i]->tree, p)) < best)
1072 /* No callback, as peer will be already disconnected */
1073 tree_add_path(peer->tunnels[i]->tree, aux, NULL);
1077 struct MeshPathInfo *path_info;
1079 if (NULL != peer_d->dhtget)
1081 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
1082 path_info->path = p;
1083 path_info->peer = peer_d;
1084 path_info->t = peer->tunnels[i];
1086 GNUNET_DHT_get_start(dht_handle, /* handle */
1087 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1088 GNUNET_BLOCK_TYPE_TEST, /* type */
1089 &id.hashPubKey, /*key to search */
1090 4, /* replication level */
1091 GNUNET_DHT_RO_RECORD_ROUTE |
1092 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1094 0, /* xquery bits */
1095 &dht_get_id_handler,
1096 (void *) path_info);
1103 * Add the path to the peer and update the path used to reach it in case this
1106 * @param peer_info Destination peer to add the path to.
1107 * @param path New path to add. Last peer must be the peer in arg 1.
1108 * Path will be either used of freed if already known.
1110 * TODO: trim the part from origin to us? Add it as path to origin?
1113 path_add_to_peer (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
1115 struct MeshPeerPath *aux;
1119 if (NULL == peer_info || NULL == path)
1125 l = path_get_length (path);
1132 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1134 l2 = path_get_length (aux);
1137 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1138 peer_info->path_tail, aux, path);
1142 if (l2 == l && memcmp(path->peers, aux->peers, l) == 0)
1149 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1156 * Add the path to the origin peer and update the path used to reach it in case
1157 * this is the shortest.
1158 * The path is given in peer_info -> destination, therefore we turn the path
1159 * upside down first.
1161 * @param peer_info Peer to add the path to, being the origin of the path.
1162 * @param path New path to add after being inversed.
1165 path_add_to_origin (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
1168 path_add_to_peer (peer_info, path);
1173 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1174 * to obtain a local peer -> destination path and interning the peer ids.
1176 * @return Newly allocated and created path
1178 static struct MeshPeerPath *
1179 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1180 unsigned int get_path_length,
1181 const struct GNUNET_PeerIdentity *put_path,
1182 unsigned int put_path_length)
1184 struct MeshPeerPath *p;
1190 GNUNET_PEER_change_rc(myid, 1);
1191 i = get_path_length;
1192 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: GET has %d hops.\n", i);
1193 for (i--; i >= 0; i--)
1195 id = GNUNET_PEER_intern (&get_path[i]);
1196 if (p->length > 0 && id == p->peers[p->length - 1])
1198 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1199 GNUNET_PEER_change_rc(id, -1);
1203 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1204 "MESH: Adding from GET: %s.\n",
1205 GNUNET_i2s(&get_path[i]));
1207 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1208 p->peers[p->length - 1] = id;
1211 i = put_path_length;
1212 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: PUT has %d hops.\n", i);
1213 for (i--; i >= 0; i--)
1215 id = GNUNET_PEER_intern (&put_path[i]);
1218 /* PUT path went through us, so discard the path up until now and start
1219 * from here to get a much shorter (and loop-free) path.
1224 if (p->length > 0 && id == p->peers[p->length - 1])
1226 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1227 GNUNET_PEER_change_rc(id, -1);
1231 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1232 "MESH: Adding from PUT: %s.\n",
1233 GNUNET_i2s(&put_path[i]));
1235 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1236 p->peers[p->length - 1] = id;
1240 if (get_path_length > 0)
1241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1242 "MESH: (first of GET: %s)\n",
1243 GNUNET_h2s_full(&get_path[0].hashPubKey));
1244 if (put_path_length > 0)
1245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1246 "MESH: (first of PUT: %s)\n",
1247 GNUNET_h2s_full(&put_path[0].hashPubKey));
1248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1249 "MESH: In total: %d hops\n",
1251 for (i = 0; i < p->length; i++)
1253 struct GNUNET_PeerIdentity peer_id;
1255 GNUNET_PEER_resolve(p->peers[i], &peer_id);
1256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1259 GNUNET_h2s_full(&peer_id.hashPubKey));
1267 * Send keepalive packets for a peer
1269 * @param cls Closure (tunnel for which to send the keepalive).
1270 * @param tc Notification context.
1272 * TODO: implement explicit multicast keepalive?
1275 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1279 * Search for a tunnel among the tunnels for a client
1281 * @param c the client whose tunnels to search in
1282 * @param tid the local id of the tunnel
1284 * @return tunnel handler, NULL if doesn't exist
1286 static struct MeshTunnel *
1287 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1289 GNUNET_HashCode hash;
1291 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1292 return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
1297 * Search for a tunnel by global ID using PEER_ID
1299 * @param pi owner of the tunnel
1300 * @param tid global tunnel number
1302 * @return tunnel handler, NULL if doesn't exist
1304 static struct MeshTunnel *
1305 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1307 struct MESH_TunnelID id;
1308 GNUNET_HashCode hash;
1313 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1314 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1319 * Search for a tunnel by global ID using full PeerIdentities
1321 * @param oid owner of the tunnel
1322 * @param tid global tunnel number
1324 * @return tunnel handler, NULL if doesn't exist
1326 static struct MeshTunnel *
1327 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1329 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1334 * Callback used to notify a client owner of a tunnel that a peer has
1335 * disconnected, most likely because of a path change.
1337 * @param n Node in the tree representing the disconnected peer
1340 notify_peer_disconnected (const struct MeshTunnelTreeNode *n)
1342 struct MeshPeerInfo *peer;
1344 if (NULL != n->t->client && NULL != nc)
1346 struct GNUNET_MESH_PeerControl msg;
1347 msg.header.size = htons (sizeof (msg));
1348 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1349 msg.tunnel_id = htonl (n->t->local_tid);
1350 GNUNET_PEER_resolve (n->peer, &msg.peer);
1351 GNUNET_SERVER_notification_context_unicast (nc, n->t->client->handle,
1352 &msg.header, GNUNET_NO);
1354 peer = peer_info_get_short(n->peer);
1355 peer_info_connect(peer, n->t);
1360 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1361 * needed rescources.
1362 * If peer already exists, reevaluate shortest path and change if different.
1364 * @param t Tunnel we want to add a new peer to
1365 * @param peer PeerInfo of the peer being added
1369 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1371 struct GNUNET_PeerIdentity id;
1372 struct MeshPeerPath *best_p;
1373 struct MeshPeerPath *p;
1374 unsigned int best_cost;
1377 GNUNET_PEER_resolve(peer->id, &id);
1379 GNUNET_CONTAINER_multihashmap_contains(t->peers, &id.hashPubKey))
1382 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
1383 GNUNET_CONTAINER_multihashmap_put(
1387 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1390 if (NULL != (p = peer->path_head))
1393 best_cost = path_get_cost(t->tree, p);
1396 if ((cost = path_get_cost (t->tree, p)) < best_cost)
1403 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected);
1404 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
1405 t->path_refresh_task =
1406 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
1410 /* Start a DHT get if necessary */
1411 peer_info_connect(peer, t);
1416 * Add a path to a tunnel which we don't own, just to remember the next hop.
1417 * If destination node was already in the tunnel, the first hop information
1418 * will be replaced with the new path.
1420 * @param t Tunnel we want to add a new peer to
1421 * @param p Path to add
1422 * @param own_pos Position of local node in path.
1426 tunnel_add_path (struct MeshTunnel *t,
1427 struct MeshPeerPath *p,
1428 unsigned int own_pos)
1430 GNUNET_assert (0 != own_pos);
1431 tree_add_path(t->tree, p, NULL);
1432 if (NULL == t->tree->me)
1433 t->tree->me = tree_find_peer(t->tree->root, p->peers[own_pos]);
1438 * Notify a tunnel that a connection has broken that affects at least
1439 * some of its peers.
1441 * @param t Tunnel affected.
1442 * @param peer Peer that (at least) has been affected by the disconnection.
1443 * @param p1 Peer that got disconnected from p2.
1444 * @param p2 Peer that got disconnected from p1.
1446 * @return Short ID of the peer disconnected (either p1 or p2).
1447 * 0 if the tunnel remained unaffected.
1449 static GNUNET_PEER_Id
1450 tunnel_notify_connection_broken (struct MeshTunnel *t,
1451 struct MeshPeerInfo *peer,
1455 return tree_notify_connection_broken (t->tree, p1, p2,
1456 ¬ify_peer_disconnected);
1461 * Destroy the tunnel and free any allocated resources linked to it
1463 * @param t the tunnel to destroy
1465 * @return GNUNET_OK on success
1468 tunnel_destroy (struct MeshTunnel *t)
1470 struct MeshClient *c;
1471 struct MeshQueue *q;
1472 struct MeshQueue *qn;
1473 GNUNET_HashCode hash;
1476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DESTROYING TUNNEL at %p\n", t);
1482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
1485 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1486 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
1491 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1492 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
1496 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
1500 if (NULL != q->data)
1501 GNUNET_free (q->data);
1505 /* TODO cancel core transmit ready in case it was active */
1507 tree_destroy(t->tree);
1508 if (NULL != t->dht_get_type)
1509 GNUNET_DHT_get_stop(t->dht_get_type);
1510 t->dht_get_type = NULL;
1517 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
1518 * client when the client disconnects.
1520 * @param cls closure (client that is disconnecting)
1521 * @param key the hash of the local tunnel id (used to access the hashmap)
1522 * @param value the value stored at the key (tunnel to destroy)
1524 * @return GNUNET_OK on success
1527 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
1529 struct MeshTunnel *t = value;
1532 if (NULL != t->dht_get_type)
1534 GNUNET_DHT_get_stop (t->dht_get_type);
1536 r = tunnel_destroy (t);
1541 /******************************************************************************/
1542 /**************** MESH NETWORK HANDLER HELPERS ***********************/
1543 /******************************************************************************/
1546 * Function called to notify a client about the socket
1547 * being ready to queue more data. "buf" will be
1548 * NULL and "size" zero if the socket was closed for
1549 * writing in the meantime.
1551 * @param cls closure
1552 * @param size number of bytes available in buf
1553 * @param buf where the callee should write the message
1554 * @return number of bytes written to buf
1557 send_core_create_path (void *cls, size_t size, void *buf)
1559 struct MeshPathInfo *info = cls;
1560 struct GNUNET_MESH_ManipulatePath *msg;
1561 struct GNUNET_PeerIdentity *peer_ptr;
1562 struct MeshPeerInfo *peer = info->peer;
1563 struct MeshTunnel *t = info->t;
1564 struct MeshPeerPath *p = info->path;
1569 sizeof (struct GNUNET_MESH_ManipulatePath) +
1570 p->length * sizeof (struct GNUNET_PeerIdentity);
1572 if (size < size_needed || NULL == buf)
1574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: create path retransmit!\n");
1575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: buf: %p\n", buf);
1576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: size: (%u/%u)\n",
1578 info->peer->core_transmit[info->pos] =
1579 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1580 GNUNET_TIME_UNIT_FOREVER_REL,
1581 path_get_first_hop (t->tree, peer->id),
1583 &send_core_create_path,
1588 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
1589 msg->header.size = htons (size_needed);
1590 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
1591 msg->tid = ntohl (t->id.tid);
1593 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1594 for (i = 0; i < p->length; i++)
1596 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1603 "MESH: CREATE PATH (%u bytes long) sent!\n",
1611 * Function called to notify a client about the socket
1612 * being ready to queue more data. "buf" will be
1613 * NULL and "size" zero if the socket was closed for
1614 * writing in the meantime.
1616 * @param cls closure (MeshDataDescriptor with all info to build packet)
1617 * @param size number of bytes available in buf
1618 * @param buf where the callee should write the message
1619 * @return number of bytes written to buf
1622 send_core_data_to_origin (void *cls, size_t size, void *buf)
1624 struct MeshDataDescriptor *info = cls;
1625 struct GNUNET_MESH_ToOrigin *msg = buf;
1628 GNUNET_assert (NULL != info);
1629 total_size = sizeof (struct GNUNET_MESH_ToOrigin) + info->size;
1630 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
1632 if (total_size > size)
1634 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1635 "not enough buffer to send data to origin\n");
1638 msg->header.size = htons (total_size);
1639 msg->header.type = htons (GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN);
1640 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1641 msg->tid = htonl (info->origin->tid);
1642 if (0 != info->size)
1644 memcpy (&msg[1], &info[1], info->size);
1646 if (NULL != info->client)
1648 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1657 * Function called to notify a client about the socket
1658 * being ready to queue more data. "buf" will be
1659 * NULL and "size" zero if the socket was closed for
1660 * writing in the meantime.
1662 * @param cls closure (data itself)
1663 * @param size number of bytes available in buf
1664 * @param buf where the callee should write the message
1666 * @return number of bytes written to buf
1669 send_core_data_multicast (void *cls, size_t size, void *buf)
1671 struct MeshDataDescriptor *info = cls;
1672 struct GNUNET_MESH_Multicast *msg = buf;
1675 GNUNET_assert (NULL != info);
1676 GNUNET_assert (NULL != info->peer);
1677 total_size = info->size + sizeof (struct GNUNET_MESH_Multicast);
1678 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1680 if (total_size > size)
1683 struct GNUNET_PeerIdentity id;
1685 GNUNET_PEER_resolve(info->peer->id, &id);
1686 info->peer->infos[info->handler_n] = info;
1687 info->peer->types[info->handler_n] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
1688 info->peer->core_transmit[info->handler_n] =
1689 GNUNET_CORE_notify_transmit_ready (core_handle,
1692 GNUNET_TIME_UNIT_FOREVER_REL,
1695 &send_core_data_multicast,
1699 info->peer->core_transmit[info->handler_n] = NULL;
1700 info->peer->infos[info->handler_n] = NULL;
1701 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
1702 msg->header.size = htons (total_size);
1703 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1704 msg->tid = htonl (info->origin->tid);
1705 memcpy (&msg[1], info->data, info->size);
1706 if (0 == --(*info->copies))
1708 if (NULL != info->client)
1710 /* FIXME One unresponsive neighbor (who doesn't "call" tmt_rdy) can lock
1711 * the client from sending anything else to the service.
1712 * - Call receive_done after certain timeout.
1713 * - Here cancel the timeout.
1715 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1717 GNUNET_free (info->data);
1718 GNUNET_free (info->copies);
1726 * Function called to notify a client about the socket
1727 * being ready to queue more data. "buf" will be
1728 * NULL and "size" zero if the socket was closed for
1729 * writing in the meantime.
1731 * @param cls closure (MeshDataDescriptor)
1732 * @param size number of bytes available in buf
1733 * @param buf where the callee should write the message
1734 * @return number of bytes written to buf
1737 send_core_path_ack (void *cls, size_t size, void *buf)
1739 struct MeshDataDescriptor *info = cls;
1740 struct GNUNET_MESH_PathACK *msg = buf;
1742 GNUNET_assert (NULL != info);
1745 info->peer->core_transmit[info->handler_n] = NULL;
1747 if (sizeof (struct GNUNET_MESH_PathACK) > size)
1752 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
1753 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
1754 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1755 msg->tid = htonl (info->origin->tid);
1756 msg->peer_id = my_full_id;
1758 /* TODO add signature */
1760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: PATH ACK sent!\n");
1761 return sizeof (struct GNUNET_MESH_PathACK);
1766 * Function called to notify a client about the socket
1767 * being ready to queue more data. "buf" will be
1768 * NULL and "size" zero if the socket was closed for
1769 * writing in the meantime.
1771 * @param cls closure (data itself)
1772 * @param size number of bytes available in buf
1773 * @param buf where the callee should write the message
1774 * @return number of bytes written to buf
1777 send_core_data_raw (void *cls, size_t size, void *buf)
1779 struct GNUNET_MessageHeader *msg = cls;
1782 GNUNET_assert (NULL != msg);
1783 total_size = ntohs (msg->size);
1785 if (total_size > size)
1790 memcpy (buf, msg, total_size);
1798 * Send another peer a notification to destroy a tunnel
1799 * @param cls The tunnel to destroy
1800 * @param size Size in the buffer
1801 * @param buf Memory where to put the data to transmit
1802 * @return Size of data put in buffer
1805 send_p2p_tunnel_destroy (void *cls, size_t size, void *buf)
1807 struct MeshTunnel *t = cls;
1808 struct MeshClient *c;
1809 struct GNUNET_MESH_TunnelMessage *msg;
1813 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1814 /*FIXME*/ msg->header.size =
1815 htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1816 msg->tunnel_id = htonl (t->id.tid);
1818 tunnel_destroy (c, t);
1819 return sizeof (struct GNUNET_MESH_TunnelMessage);
1824 /******************************************************************************/
1825 /******************** MESH NETWORK HANDLERS **************************/
1826 /******************************************************************************/
1830 * Core handler for path creation
1831 * struct GNUNET_CORE_MessageHandler
1833 * @param cls closure
1834 * @param message message
1835 * @param peer peer identity this notification is about
1836 * @param atsi performance data
1837 * @return GNUNET_OK to keep the connection open,
1838 * GNUNET_SYSERR to close it (signal serious error)
1842 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1843 const struct GNUNET_MessageHeader *message,
1844 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1846 unsigned int own_pos;
1849 MESH_TunnelNumber tid;
1850 struct GNUNET_MESH_ManipulatePath *msg;
1851 struct GNUNET_PeerIdentity *pi;
1852 GNUNET_HashCode hash;
1853 struct MeshPeerPath *path;
1854 struct MeshPeerInfo *dest_peer_info;
1855 struct MeshPeerInfo *orig_peer_info;
1856 struct MeshTunnel *t;
1858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1859 "MESH: Received a path create msg [%s]\n",
1860 GNUNET_i2s(&my_full_id));
1861 size = ntohs (message->size);
1862 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
1864 GNUNET_break_op (0);
1868 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
1869 if (size % sizeof (struct GNUNET_PeerIdentity))
1871 GNUNET_break_op (0);
1874 size /= sizeof (struct GNUNET_PeerIdentity);
1877 GNUNET_break_op (0);
1880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1881 "MESH: path has %u hops.\n",
1883 msg = (struct GNUNET_MESH_ManipulatePath *) message;
1885 tid = ntohl (msg->tid);
1886 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1887 t = tunnel_get (pi, tid);
1888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1889 "MESH: path is for tunnel %s [%X].\n",
1894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
1895 t = GNUNET_malloc (sizeof (struct MeshTunnel));
1896 t->id.oid = GNUNET_PEER_intern (pi);
1898 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
1899 t->local_tid = next_local_tid++;
1900 /* FIXME test if taken */
1901 next_local_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
1902 t->tree = tree_new(t, t->id.oid);
1904 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1906 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
1907 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1914 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
1915 if (NULL == dest_peer_info)
1917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1918 "MESH: Creating PeerInfo for destination.\n");
1919 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1920 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
1921 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
1923 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1925 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
1926 if (NULL == orig_peer_info)
1928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1929 "MESH: Creating PeerInfo for origin.\n");
1930 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1931 orig_peer_info->id = GNUNET_PEER_intern (pi);
1932 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
1933 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating path...\n");
1936 path = path_new (size);
1938 for (i = 0; i < size; i++)
1940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1941 "MESH: ... adding %s\n",
1942 GNUNET_i2s(&pi[i]));
1943 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
1944 if (path->peers[i] == myid)
1947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1948 "MESH: Own position: %u\n", own_pos);
1951 /* cannot be self, must be 'not found' */
1952 /* create path: self not found in path through self */
1953 GNUNET_break_op (0);
1954 path_destroy (path);
1955 /* FIXME error. destroy tunnel? leave for timeout? */
1958 tunnel_add_path (t, path, own_pos);
1959 t->tree->me = tree_find_peer(t->tree->root, myid);
1960 if (own_pos == size - 1)
1962 /* It is for us! Send ack. */
1963 struct GNUNET_MESH_TunnelNotification cmsg;
1964 struct MeshDataDescriptor *info;
1967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1968 "MESH: It's for us!\n");
1969 path_add_to_origin (orig_peer_info, path);
1970 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1971 info->origin = &t->id;
1972 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1973 GNUNET_assert (NULL != info->peer);
1974 for (j = 0; info->peer->core_transmit[j]; j++)
1976 if (j == (CORE_QUEUE_SIZE - 1))
1982 info->handler_n = j;
1983 info->peer->types[j] = GNUNET_MESSAGE_TYPE_MESH_PATH_ACK;
1984 info->peer->infos[j] = info;
1985 info->peer->core_transmit[j] =
1986 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1987 GNUNET_TIME_UNIT_FOREVER_REL, peer,
1988 sizeof (struct GNUNET_MESH_PathACK),
1989 &send_core_path_ack, info);
1990 cmsg.header.size = htons(sizeof(cmsg));
1991 cmsg.header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1992 GNUNET_PEER_resolve(t->id.oid, &cmsg.peer);
1993 cmsg.tunnel_id = htonl(t->local_tid);
1994 GNUNET_SERVER_notification_context_broadcast(nc, &cmsg.header, GNUNET_NO);
1998 struct MeshPeerPath *path2;
2000 /* It's for somebody else! Retransmit. */
2001 path2 = path_duplicate(path);
2002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2003 "MESH: Retransmitting.\n");
2004 path_add_to_peer(dest_peer_info, path);
2005 path = path_duplicate(path2);
2006 path_add_to_origin(orig_peer_info, path2);
2007 send_create_path(dest_peer_info, path, t);
2014 * Core handler for mesh network traffic going from the origin to a peer
2016 * @param cls closure
2017 * @param peer peer identity this notification is about
2018 * @param message message
2019 * @param atsi performance data
2020 * @return GNUNET_OK to keep the connection open,
2021 * GNUNET_SYSERR to close it (signal serious error)
2024 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2025 const struct GNUNET_MessageHeader *message,
2026 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2028 struct GNUNET_MESH_Unicast *msg;
2029 struct MeshTunnel *t;
2030 struct MeshPeerInfo *pi;
2033 size = ntohs (message->size);
2035 sizeof (struct GNUNET_MESH_Unicast) +
2036 sizeof (struct GNUNET_MessageHeader))
2041 msg = (struct GNUNET_MESH_Unicast *) message;
2042 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2045 /* TODO notify back: we don't know this tunnel */
2046 GNUNET_break_op (0);
2049 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
2050 &msg->destination.hashPubKey);
2053 /* TODO maybe feedback, log to statistics */
2054 GNUNET_break_op (0);
2059 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
2062 msg = GNUNET_malloc (size);
2063 memcpy (msg, message, size);
2064 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2065 GNUNET_TIME_UNIT_FOREVER_REL,
2066 path_get_first_hop (t->tree, pi->id),
2068 &send_core_data_raw, msg);
2074 * Core handler for mesh network traffic going from the origin to all peers
2076 * @param cls closure
2077 * @param message message
2078 * @param peer peer identity this notification is about
2079 * @param atsi performance data
2080 * @return GNUNET_OK to keep the connection open,
2081 * GNUNET_SYSERR to close it (signal serious error)
2083 * TODO: Check who we got this from, to validate route.
2086 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2087 const struct GNUNET_MessageHeader *message,
2088 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2090 struct GNUNET_MESH_Multicast *msg;
2091 struct GNUNET_PeerIdentity *id;
2092 struct MeshDataDescriptor *info;
2093 struct MeshTunnelTreeNode *n;
2094 struct MeshTunnel *t;
2095 unsigned int *copies;
2100 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_Multicast);
2101 if (size < sizeof (struct GNUNET_MessageHeader))
2103 GNUNET_break_op (0);
2106 msg = (struct GNUNET_MESH_Multicast *) message;
2107 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2111 /* TODO notify that we dont know that tunnel */
2112 GNUNET_break_op (0);
2116 /* Transmit to locally interested clients */
2117 if (GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
2119 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
2121 n = t->tree->me->children_head;
2124 copies = GNUNET_malloc (sizeof (unsigned int));
2125 for (*copies = 0; NULL != n; n = n->next)
2127 n = t->tree->me->children_head;
2128 data = GNUNET_malloc (size);
2129 memcpy (data, &msg[1], size);
2132 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
2133 info->origin = &t->id;
2136 info->copies = copies;
2137 info->client = t->client->handle;
2138 info->destination = n->peer;
2139 id = path_get_first_hop(t->tree, n->peer);
2140 info->peer = peer_info_get(id);
2141 GNUNET_assert (NULL != info->peer);
2142 for (i = 0; NULL != info->peer->core_transmit[i]; i++)
2144 if (i == (CORE_QUEUE_SIZE - 1))
2150 info->handler_n = i;
2151 info->peer->infos[i] = info;
2152 info->peer->types[i] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
2153 info->peer->core_transmit[i] =
2154 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2155 GNUNET_TIME_UNIT_FOREVER_REL, id,
2156 ntohs (msg->header.size),
2157 &send_core_data_multicast, info);
2165 * Core handler for mesh network traffic
2167 * @param cls closure
2168 * @param message message
2169 * @param peer peer identity this notification is about
2170 * @param atsi performance data
2172 * @return GNUNET_OK to keep the connection open,
2173 * GNUNET_SYSERR to close it (signal serious error)
2176 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
2177 const struct GNUNET_MessageHeader *message,
2178 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2180 struct GNUNET_MESH_ToOrigin *msg;
2181 struct GNUNET_PeerIdentity id;
2182 struct MeshPeerInfo *peer_info;
2183 struct MeshTunnel *t;
2186 size = ntohs (message->size);
2187 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
2188 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
2190 GNUNET_break_op (0);
2193 msg = (struct GNUNET_MESH_ToOrigin *) message;
2194 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2198 /* TODO notify that we dont know this tunnel (whom)? */
2202 if (t->id.oid == myid)
2204 if (NULL == t->client)
2206 /* got data packet for ownerless tunnel */
2207 GNUNET_break_op (0);
2210 /* TODO signature verification */
2211 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle, message,
2215 peer_info = peer_info_get (&msg->oid);
2216 if (NULL == peer_info)
2218 /* unknown origin of tunnel */
2222 GNUNET_PEER_resolve (t->tree->me->parent->peer, &id);
2223 msg = GNUNET_malloc (size);
2224 memcpy (msg, message, size);
2225 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2226 GNUNET_TIME_UNIT_FOREVER_REL, &id, size,
2227 &send_core_data_raw, msg);
2234 * Core handler for path ACKs
2236 * @param cls closure
2237 * @param message message
2238 * @param peer peer identity this notification is about
2239 * @param atsi performance data
2241 * @return GNUNET_OK to keep the connection open,
2242 * GNUNET_SYSERR to close it (signal serious error)
2245 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2246 const struct GNUNET_MessageHeader *message,
2247 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2249 struct GNUNET_MESH_PathACK *msg;
2250 struct GNUNET_PeerIdentity id;
2251 struct MeshTunnelTreeNode *n;
2252 struct MeshPeerInfo *peer_info;
2253 struct MeshTunnel *t;
2255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2256 "MESH: Received a path ACK msg [%s]\n",
2257 GNUNET_i2s(&my_full_id));
2258 msg = (struct GNUNET_MESH_PathACK *) message;
2259 t = tunnel_get (&msg->oid, msg->tid);
2262 /* TODO notify that we don't know the tunnel */
2266 /* Message for us? */
2267 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
2269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2270 "MESH: It's for us!\n");
2271 if (NULL == t->client)
2273 GNUNET_break_op (0);
2276 peer_info = peer_info_get (&msg->peer_id);
2277 if (NULL == peer_info)
2279 GNUNET_break_op (0);
2282 n = tree_find_peer(t->tree->root, peer_info->id);
2285 GNUNET_break_op (0);
2288 n->status = MESH_PEER_READY;
2289 send_client_peer_connected(t, peer_info->id);
2293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2294 "MESH: not for us, retransmitting...\n");
2295 GNUNET_PEER_resolve(t->tree->me->parent->peer, &id);
2296 peer_info = peer_info_get (&msg->oid);
2297 if (NULL == peer_info)
2299 /* If we know the tunnel, we should DEFINITELY know the peer */
2303 msg = GNUNET_malloc (sizeof (struct GNUNET_MESH_PathACK));
2304 memcpy (msg, message, sizeof (struct GNUNET_MESH_PathACK));
2305 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2306 GNUNET_TIME_UNIT_FOREVER_REL,
2308 sizeof (struct GNUNET_MESH_PathACK),
2309 &send_core_data_raw, msg);
2315 * Functions to handle messages from core
2317 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
2318 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
2319 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
2320 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
2321 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
2322 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2323 sizeof (struct GNUNET_MESH_PathACK)},
2329 /******************************************************************************/
2330 /**************** MESH LOCAL HANDLER HELPERS ***********************/
2331 /******************************************************************************/
2334 * deregister_app: iterator for removing each application registered by a client
2336 * @param cls closure
2337 * @param key the hash of the application id (used to access the hashmap)
2338 * @param value the value stored at the key (client)
2340 * @return GNUNET_OK on success
2343 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
2345 GNUNET_CONTAINER_multihashmap_remove (applications, key, value);
2351 * notify_client_connection_failure: notify a client that the connection to the
2352 * requested remote peer is not possible (for instance, no route found)
2353 * Function called when the socket is ready to queue more data. "buf" will be
2354 * NULL and "size" zero if the socket was closed for writing in the meantime.
2356 * @param cls closure
2357 * @param size number of bytes available in buf
2358 * @param buf where the callee should write the message
2359 * @return number of bytes written to buf
2362 notify_client_connection_failure (void *cls, size_t size, void *buf)
2365 struct MeshPeerInfo *peer_info;
2366 struct GNUNET_MESH_PeerControl *msg;
2367 struct GNUNET_PeerIdentity id;
2369 if (0 == size && NULL == buf)
2371 // TODO retry? cancel?
2375 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
2376 peer_info = (struct MeshPeerInfo *) cls;
2377 msg = (struct GNUNET_MESH_PeerControl *) buf;
2378 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2379 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
2380 // msg->tunnel_id = htonl(peer_info->t->tid);
2381 GNUNET_PEER_resolve (peer_info->id, &id);
2382 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
2390 * Send keepalive packets for a peer
2392 * @param cls Closure (tunnel for which to send the keepalive).
2393 * @param tc Notification context.
2395 * TODO: implement explicit multicast keepalive?
2398 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2400 struct MeshTunnel *t = cls;
2401 struct GNUNET_MessageHeader *payload;
2402 struct GNUNET_MESH_Multicast *msg;
2405 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
2406 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2412 "MESH: sending keepalive for tunnel %d\n",
2415 size = sizeof(struct GNUNET_MESH_Multicast) +
2416 sizeof(struct GNUNET_MessageHeader);
2417 msg = GNUNET_malloc (size);
2418 msg->header.size = htons (size);
2419 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
2420 msg->oid = my_full_id;
2421 msg->tid = htonl(t->id.tid);
2422 payload = (struct GNUNET_MessageHeader *) &msg[1];
2423 payload->size = htons (sizeof(struct GNUNET_MessageHeader));
2424 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2425 handle_mesh_data_multicast (NULL, &my_full_id, &msg->header, NULL);
2428 t->path_refresh_task =
2429 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
2435 * Function to process paths received for a new peer addition. The recorded
2436 * paths form the initial tunnel, which can be optimized later.
2437 * Called on each result obtained for the DHT search.
2439 * @param cls closure
2440 * @param exp when will this value expire
2441 * @param key key of the result
2442 * @param type type of the result
2443 * @param size number of bytes in data
2444 * @param data pointer to the result data
2446 * TODO: re-issue the request after certain time? cancel after X results?
2449 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2450 const GNUNET_HashCode * key,
2451 const struct GNUNET_PeerIdentity *get_path,
2452 unsigned int get_path_length,
2453 const struct GNUNET_PeerIdentity *put_path,
2454 unsigned int put_path_length,
2455 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
2457 struct MeshPathInfo *path_info = cls;
2458 struct MeshPeerPath *p;
2459 struct GNUNET_PeerIdentity pi;
2462 GNUNET_PEER_resolve (path_info->peer->id, &pi);
2463 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2464 "MESH: Got results from DHT for %s\n",
2465 GNUNET_h2s_full(&pi.hashPubKey));
2466 GNUNET_DHT_get_stop(path_info->peer->dhtget);
2467 path_info->peer->dhtget = NULL;
2469 p = path_build_from_dht (get_path, get_path_length,
2470 put_path, put_path_length);
2471 path_add_to_peer (path_info->peer, p);
2472 for (i = 0; i < path_info->peer->ntunnels; i++)
2474 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
2475 peer_info_connect(path_info->peer, path_info->t);
2477 GNUNET_free (path_info);
2484 * Function to process paths received for a new peer addition. The recorded
2485 * paths form the initial tunnel, which can be optimized later.
2486 * Called on each result obtained for the DHT search.
2488 * @param cls closure
2489 * @param exp when will this value expire
2490 * @param key key of the result
2491 * @param type type of the result
2492 * @param size number of bytes in data
2493 * @param data pointer to the result data
2496 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2497 const GNUNET_HashCode * key,
2498 const struct GNUNET_PeerIdentity *get_path,
2499 unsigned int get_path_length,
2500 const struct GNUNET_PeerIdentity *put_path,
2501 unsigned int put_path_length,
2502 enum GNUNET_BLOCK_Type type, size_t size,
2505 const struct GNUNET_PeerIdentity *pi = data;
2506 struct GNUNET_PeerIdentity id;
2507 struct MeshTunnel *t = cls;
2508 struct MeshPeerInfo *peer_info;
2509 struct MeshPathInfo *path_info;
2510 struct MeshPeerPath *p;
2513 if (size != sizeof (struct GNUNET_PeerIdentity))
2515 GNUNET_break_op (0);
2518 GNUNET_assert (NULL != t->client);
2519 GNUNET_DHT_get_stop (t->dht_get_type);
2520 t->dht_get_type = NULL;
2521 peer_info = peer_info_get (pi);
2522 GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey, peer_info,
2523 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2525 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path_head &&
2526 NULL == peer_info->dhtget)
2528 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2529 path_info->peer = peer_info;
2531 /* we don't have a route to the peer, let's try a direct lookup */
2533 GNUNET_DHT_get_start (dht_handle, /* handle */
2534 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
2535 GNUNET_BLOCK_TYPE_TEST, /* block type */
2536 &pi->hashPubKey, /* key to look up */
2537 10U, /* replication level */
2538 GNUNET_DHT_RO_RECORD_ROUTE |
2539 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2540 /* option to dht: record route */
2542 0, /* xquery bits */
2543 dht_get_id_handler, /* callback */
2544 path_info); /* closure */
2548 p = path_build_from_dht (get_path, get_path_length, put_path, put_path_length);
2549 path_add_to_peer (peer_info, p);
2550 tunnel_add_peer(t, peer_info);
2551 p = tree_get_path_to_peer(t->tree, peer_info->id);
2553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2554 "MESH: new route for tunnel 0x%x found, has %u hops\n",
2555 t->local_tid, p->length);
2556 for (i = 0; i < p->length; i++)
2558 GNUNET_PEER_resolve (p->peers[0], &id);
2559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:\t%d\t%s\n", i,
2560 GNUNET_h2s_full (&id.hashPubKey));
2566 send_create_path(peer_info, p, t);
2570 send_client_peer_connected(t, myid);
2574 /******************************************************************************/
2575 /********************* MESH LOCAL HANDLES **************************/
2576 /******************************************************************************/
2580 * Handler for client disconnection
2582 * @param cls closure
2583 * @param client identification of the client; NULL
2584 * for the last call when the server is destroyed
2587 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
2589 struct MeshClient *c;
2590 struct MeshClient *next;
2592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
2594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
2598 if (c->handle != client && NULL != client)
2600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
2604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
2605 if (NULL != c->tunnels)
2607 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
2608 &tunnel_destroy_iterator,
2610 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
2613 /* deregister clients applications */
2614 if (NULL != c->apps)
2616 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
2617 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
2619 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
2620 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
2622 GNUNET_SCHEDULER_cancel (announce_applications_task);
2623 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
2625 if (NULL != c->types)
2626 GNUNET_CONTAINER_multihashmap_destroy (c->types);
2627 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
2633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
2639 * Handler for new clients
2641 * @param cls closure
2642 * @param client identification of the client
2643 * @param message the actual message, which includes messages the client wants
2646 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
2647 const struct GNUNET_MessageHeader *message)
2649 struct GNUNET_MESH_ClientConnect *cc_msg;
2650 struct MeshClient *c;
2651 GNUNET_MESH_ApplicationType *a;
2658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
2659 /* Check data sanity */
2660 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
2661 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
2662 ntypes = ntohs (cc_msg->types);
2663 napps = ntohs (cc_msg->applications);
2665 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
2668 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2672 /* Create new client structure */
2673 c = GNUNET_malloc (sizeof (struct MeshClient));
2675 c->id = next_client_id++;
2678 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
2681 GNUNET_MESH_ApplicationType at;
2684 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
2685 for (i = 0; i < napps; i++)
2688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
2689 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
2690 /* store in clients hashmap */
2691 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
2692 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2693 /* store in global hashmap, for announcements */
2694 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
2695 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2697 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
2698 announce_applications_task =
2699 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
2707 t = (uint16_t *) & a[napps];
2708 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
2709 for (i = 0; i < ntypes; i++)
2712 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
2714 /* store in clients hashmap */
2715 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
2716 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2717 /* store in global hashmap */
2718 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
2719 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2723 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
2725 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
2726 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
2727 GNUNET_SERVER_notification_context_add (nc, client);
2729 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
2737 * Handler for requests of new tunnels
2739 * @param cls closure
2740 * @param client identification of the client
2741 * @param message the actual message
2744 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
2745 const struct GNUNET_MessageHeader *message)
2747 struct GNUNET_MESH_TunnelMessage *t_msg;
2748 struct MeshTunnel *t;
2749 struct MeshClient *c;
2750 GNUNET_HashCode hash;
2752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
2754 /* Sanity check for client registration */
2755 if (NULL == (c = client_get (client)))
2758 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2765 /* Message sanity check */
2766 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2769 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2773 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2774 /* Sanity check for tunnel numbering */
2775 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
2778 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2781 /* Sanity check for duplicate tunnel IDs */
2782 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
2785 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2789 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATED TUNNEL at %p\n", t);
2791 while (NULL != tunnel_get_by_pi (myid, next_tid))
2792 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
2793 t->id.tid = next_tid++;
2795 t->local_tid = ntohl (t_msg->tunnel_id);
2797 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
2799 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2801 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
2802 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2805 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2809 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2811 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2812 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2815 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2818 t->tree = tree_new (t, myid);
2819 t->tree->refresh = REFRESH_PATH_TIME;
2820 t->tree->root->status = MESH_PEER_READY;
2821 t->tree->me = t->tree->root;
2823 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2829 * Handler for requests of deleting tunnels
2831 * @param cls closure
2832 * @param client identification of the client
2833 * @param message the actual message
2836 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
2837 const struct GNUNET_MessageHeader *message)
2839 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
2840 struct MeshClient *c;
2841 struct MeshTunnel *t;
2842 MESH_TunnelNumber tid;
2843 GNUNET_HashCode hash;
2845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel\n");
2847 /* Sanity check for client registration */
2848 if (NULL == (c = client_get (client)))
2851 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2854 /* Message sanity check */
2855 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2858 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2864 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2866 /* Retrieve tunnel */
2867 tid = ntohl (tunnel_msg->tunnel_id);
2869 /* Remove from local id hashmap */
2870 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2871 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
2872 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t);
2874 /* Remove from global id hashmap */
2875 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2876 GNUNET_break (GNUNET_YES ==
2877 GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t));
2879 // notify_tunnel_destroy(t); FIXME
2881 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2887 * Handler for connection requests to new peers
2889 * @param cls closure
2890 * @param client identification of the client
2891 * @param message the actual message (PeerControl)
2894 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
2895 const struct GNUNET_MessageHeader *message)
2897 struct GNUNET_MESH_PeerControl *peer_msg;
2898 struct MeshPeerInfo *peer_info;
2899 struct MeshClient *c;
2900 struct MeshTunnel *t;
2901 MESH_TunnelNumber tid;
2903 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Got connection request\n");
2904 /* Sanity check for client registration */
2905 if (NULL == (c = client_get (client)))
2908 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2912 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2913 /* Sanity check for message size */
2914 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2917 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2921 /* Tunnel exists? */
2922 tid = ntohl (peer_msg->tunnel_id);
2923 t = tunnel_get_by_local_id (c, tid);
2927 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2931 /* Does client own tunnel? */
2932 if (t->client->handle != client)
2935 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2938 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: for %s\n",
2939 GNUNET_h2s_full(&peer_msg->peer.hashPubKey));
2940 peer_info = peer_info_get (&peer_msg->peer);
2942 tunnel_add_peer(t, peer_info);
2943 peer_info_connect(peer_info, t);
2945 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2951 * Handler for disconnection requests of peers in a tunnel
2953 * @param cls closure
2954 * @param client identification of the client
2955 * @param message the actual message (PeerControl)
2958 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
2959 const struct GNUNET_MessageHeader *message)
2961 struct GNUNET_MESH_PeerControl *peer_msg;
2962 struct MeshClient *c;
2963 struct MeshTunnel *t;
2964 MESH_TunnelNumber tid;
2966 /* Sanity check for client registration */
2967 if (NULL == (c = client_get (client)))
2970 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2973 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2974 /* Sanity check for message size */
2975 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2978 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2982 /* Tunnel exists? */
2983 tid = ntohl (peer_msg->tunnel_id);
2984 t = tunnel_get_by_local_id (c, tid);
2988 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2992 /* Does client own tunnel? */
2993 if (t->client->handle != client)
2996 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3000 /* Ok, delete peer from tunnel */
3001 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
3002 &peer_msg->peer.hashPubKey);
3004 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3010 * Handler for connection requests to new peers by type
3012 * @param cls closure
3013 * @param client identification of the client
3014 * @param message the actual message (ConnectPeerByType)
3017 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
3018 const struct GNUNET_MessageHeader *message)
3020 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
3021 struct MeshClient *c;
3022 struct MeshTunnel *t;
3023 GNUNET_HashCode hash;
3024 MESH_TunnelNumber tid;
3026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
3027 /* Sanity check for client registration */
3028 if (NULL == (c = client_get (client)))
3031 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3035 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
3036 /* Sanity check for message size */
3037 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
3038 ntohs (connect_msg->header.size))
3041 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3045 /* Tunnel exists? */
3046 tid = ntohl (connect_msg->tunnel_id);
3047 t = tunnel_get_by_local_id (c, tid);
3051 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3055 /* Does client own tunnel? */
3056 if (t->client->handle != client)
3059 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3063 /* Do WE have the service? */
3064 t->type = ntohl (connect_msg->type);
3065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", t->type);
3066 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
3067 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
3070 /* Yes! Fast forward, add ourselves to the tunnel and send the
3071 * good news to the client
3073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
3074 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
3075 peer_info_get (&my_full_id),
3076 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
3079 send_client_peer_connected(t, myid);
3080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
3081 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3084 /* Ok, lets find a peer offering the service */
3085 if (NULL != t->dht_get_type)
3087 GNUNET_DHT_get_stop (t->dht_get_type);
3089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
3090 GNUNET_h2s_full (&hash));
3092 GNUNET_DHT_get_start (dht_handle,
3093 GNUNET_TIME_UNIT_FOREVER_REL,
3094 GNUNET_BLOCK_TYPE_TEST,
3097 GNUNET_DHT_RO_RECORD_ROUTE |
3098 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
3100 &dht_get_type_handler, t);
3102 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3108 * Handler for client traffic directed to one peer
3110 * @param cls closure
3111 * @param client identification of the client
3112 * @param message the actual message
3115 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
3116 const struct GNUNET_MessageHeader *message)
3118 struct MeshClient *c;
3119 struct MeshTunnel *t;
3120 struct MeshPeerInfo *pi;
3121 struct GNUNET_MESH_Unicast *data_msg;
3122 MESH_TunnelNumber tid;
3125 /* Sanity check for client registration */
3126 if (NULL == (c = client_get (client)))
3129 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3132 data_msg = (struct GNUNET_MESH_Unicast *) message;
3133 /* Sanity check for message size */
3134 size = ntohs (message->size);
3135 if (sizeof (struct GNUNET_MESH_Unicast) +
3136 sizeof (struct GNUNET_MessageHeader) > size)
3139 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3143 /* Tunnel exists? */
3144 tid = ntohl (data_msg->tid);
3145 t = tunnel_get_by_local_id (c, tid);
3149 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3153 /* Is it a local tunnel? Then, does client own the tunnel? */
3154 if (t->client->handle != NULL && t->client->handle != client)
3157 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3161 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
3162 &data_msg->destination.hashPubKey);
3163 /* Is the selected peer in the tunnel? */
3167 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3171 /* Ok, everything is correct, send the message
3172 * (pretend we got it from a mesh peer)
3175 char buf[ntohs (message->size)];
3176 struct GNUNET_MESH_Unicast *copy;
3178 /* Work around const limitation */
3179 copy = (struct GNUNET_MESH_Unicast *) buf;
3180 memcpy (buf, data_msg, size);
3181 copy->oid = my_full_id;
3182 copy->tid = htonl (t->id.tid);
3183 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL);
3185 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3190 * Handler for client traffic directed to all peers in a tunnel
3192 * @param cls closure
3193 * @param client identification of the client
3194 * @param message the actual message
3197 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
3198 const struct GNUNET_MessageHeader *message)
3200 struct MeshClient *c;
3201 struct MeshTunnel *t;
3202 struct GNUNET_MESH_Multicast *data_msg;
3203 MESH_TunnelNumber tid;
3205 /* Sanity check for client registration */
3206 if (NULL == (c = client_get (client)))
3209 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3212 data_msg = (struct GNUNET_MESH_Multicast *) message;
3213 /* Sanity check for message size */
3214 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (data_msg->header.size))
3217 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3221 /* Tunnel exists? */
3222 tid = ntohl (data_msg->tid);
3223 t = tunnel_get_by_local_id (c, tid);
3227 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3231 /* Does client own tunnel? */
3232 if (t->client->handle != client)
3235 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3240 char buf[ntohs(message->size)];
3241 struct GNUNET_MESH_Multicast *copy;
3243 copy = (struct GNUNET_MESH_Multicast *)buf;
3244 memcpy(buf, message, ntohs(message->size));
3245 copy->oid = my_full_id;
3246 copy->tid = htonl(t->id.tid);
3247 handle_mesh_data_multicast(client, &my_full_id, ©->header, NULL);
3250 /* receive done gets called when last copy is sent */
3255 * Functions to handle messages from clients
3257 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
3258 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
3259 {&handle_local_tunnel_create, NULL,
3260 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
3261 sizeof (struct GNUNET_MESH_TunnelMessage)},
3262 {&handle_local_tunnel_destroy, NULL,
3263 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
3264 sizeof (struct GNUNET_MESH_TunnelMessage)},
3265 {&handle_local_connect_add, NULL,
3266 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
3267 sizeof (struct GNUNET_MESH_PeerControl)},
3268 {&handle_local_connect_del, NULL,
3269 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
3270 sizeof (struct GNUNET_MESH_PeerControl)},
3271 {&handle_local_connect_by_type, NULL,
3272 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
3273 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
3274 {&handle_local_unicast, NULL,
3275 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3276 {&handle_local_unicast, NULL,
3277 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3278 {&handle_local_multicast, NULL,
3279 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3285 * To be called on core init/fail.
3287 * @param cls service closure
3288 * @param server handle to the server for this service
3289 * @param identity the public identity of this peer
3292 core_init (void *cls, struct GNUNET_CORE_Handle *server,
3293 const struct GNUNET_PeerIdentity *identity)
3295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
3296 core_handle = server;
3297 if (0 != memcmp(identity, &my_full_id, sizeof(my_full_id)) || NULL == server)
3299 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
3300 GNUNET_SCHEDULER_shutdown();
3306 * Method called whenever a given peer connects.
3308 * @param cls closure
3309 * @param peer peer identity this notification is about
3310 * @param atsi performance data for the connection
3313 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
3314 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
3316 struct MeshPeerInfo *peer_info;
3317 struct MeshPeerPath *path;
3319 #if MESH_DEBUG_CONNECTION
3320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
3321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
3322 GNUNET_h2s(&my_full_id.hashPubKey));
3324 peer_info = peer_info_get (peer);
3325 if (myid == peer_info->id)
3327 #if MESH_DEBUG_CONNECTION
3328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3332 #if MESH_DEBUG_CONNECTION
3335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
3336 GNUNET_h2s(&peer->hashPubKey));
3339 path = path_new (2);
3340 path->peers[0] = myid;
3341 path->peers[1] = peer_info->id;
3342 GNUNET_PEER_change_rc(myid, 1);
3343 GNUNET_PEER_change_rc(peer_info->id, 1);
3344 path_add_to_peer (peer_info, path);
3349 * Method called whenever a peer disconnects.
3351 * @param cls closure
3352 * @param peer peer identity this notification is about
3355 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
3357 struct MeshPeerInfo *pi;
3360 #if MESH_DEBUG_CONNECTION
3361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
3363 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
3369 for (i = 0; i < CORE_QUEUE_SIZE; i++)
3371 peer_info_cancel_transmission(pi, i);
3373 path_remove_from_peer (pi, pi->id, myid);
3374 #if MESH_DEBUG_CONNECTION
3377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3384 /******************************************************************************/
3385 /************************ MAIN FUNCTIONS ****************************/
3386 /******************************************************************************/
3389 * Iterator over hash map entries.
3391 * @param cls closure
3392 * @param key current key code
3393 * @param value value in the hash map
3394 * @return GNUNET_YES if we should continue to
3399 shutdown_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
3401 struct MeshTunnel *t = value;
3407 * Task run during shutdown.
3413 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
3416 /* TODO: destroy tunnels? */
3417 if (core_handle != NULL)
3419 GNUNET_CORE_disconnect (core_handle);
3422 GNUNET_CONTAINER_multihashmap_iterate(tunnels, &shutdown_tunnel, NULL);
3423 if (dht_handle != NULL)
3425 GNUNET_DHT_disconnect (dht_handle);
3430 GNUNET_SERVER_notification_context_destroy (nc);
3433 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
3435 GNUNET_SCHEDULER_cancel (announce_id_task);
3436 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
3438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
3442 * Process mesh requests.
3444 * @param cls closure
3445 * @param server the initialized server
3446 * @param c configuration to use
3449 run (void *cls, struct GNUNET_SERVER_Handle *server,
3450 const struct GNUNET_CONFIGURATION_Handle *c)
3452 struct MeshPeerInfo *peer;
3453 struct MeshPeerPath *p;
3456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
3457 server_handle = server;
3458 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
3459 CORE_QUEUE_SIZE, /* queue size */
3460 NULL, /* Closure passed to MESH functions */
3461 &core_init, /* Call core_init once connected */
3462 &core_connect, /* Handle connects */
3463 &core_disconnect, /* remove peers on disconnects */
3464 NULL, /* Don't notify about all incoming messages */
3465 GNUNET_NO, /* For header only in notification */
3466 NULL, /* Don't notify about all outbound messages */
3467 GNUNET_NO, /* For header-only out notification */
3468 core_handlers); /* Register these handlers */
3469 if (core_handle == NULL)
3472 GNUNET_SCHEDULER_shutdown ();
3477 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
3480 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3482 ("Mesh service is lacking key configuration settings. Exiting.\n"));
3483 GNUNET_SCHEDULER_shutdown ();
3486 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3487 GNUNET_free (keyfile);
3488 if (my_private_key == NULL)
3490 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3491 _("Mesh service could not access hostkey. Exiting.\n"));
3492 GNUNET_SCHEDULER_shutdown ();
3495 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
3496 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
3497 &my_full_id.hashPubKey);
3498 myid = GNUNET_PEER_intern (&my_full_id);
3500 dht_handle = GNUNET_DHT_connect (c, 64);
3501 if (dht_handle == NULL)
3507 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3509 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3510 peers = GNUNET_CONTAINER_multihashmap_create (32);
3511 applications = GNUNET_CONTAINER_multihashmap_create (32);
3512 types = GNUNET_CONTAINER_multihashmap_create (32);
3514 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
3515 nc = GNUNET_SERVER_notification_context_create (server_handle,
3517 GNUNET_SERVER_disconnect_notify (server_handle,
3518 &handle_local_client_disconnect,
3523 clients_tail = NULL;
3528 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3529 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
3531 /* Create a peer_info for the local peer */
3532 peer = peer_info_get(&my_full_id);
3535 path_add_to_peer(peer, p);
3537 /* Scheduled the task to clean up when shutdown is called */
3538 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
3541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
3545 * The main function for the mesh service.
3547 * @param argc number of arguments from the command line
3548 * @param argv command line arguments
3549 * @return 0 ok, 1 on error
3552 main (int argc, char *const *argv)
3557 // fprintf (stderr, "main ()\n");
3559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
3562 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
3564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");
3566 // fprintf (stderr, "main () END\n");