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)
879 GNUNET_PEER_resolve(p->peers[i + 1], &id);
881 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
883 path_info->peer = peer;
885 neighbor = peer_info_get(&id);
886 path_info->pos = peer_info_transmit_slot(neighbor);
887 neighbor->types[path_info->pos] = GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE;
888 neighbor->infos[path_info->pos] = path_info;
889 neighbor->core_transmit[path_info->pos] =
890 GNUNET_CORE_notify_transmit_ready (
891 core_handle, /* handle */
894 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
896 sizeof (struct GNUNET_MESH_ManipulatePath)
897 + (p->length * sizeof (struct GNUNET_PeerIdentity)), /*size */
898 &send_core_create_path, /* callback */
899 path_info); /* cls */
904 * Try to establish a new connection to this peer.
905 * Use the best path for the given tunnel.
906 * If the peer doesn't have any path to it yet, try to get one.
907 * If the peer already has some path, send a CREATE PATH towards it.
909 * @param peer PeerInfo of the peer.
910 * @param t Tunnel for which to create the path, if possible.
913 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
915 struct MeshPeerPath *p;
916 struct MeshPathInfo *path_info;
918 if (NULL != peer->path_head)
920 p = tree_get_path_to_peer(t->tree, peer->id);
923 send_create_path(peer, p, t);
927 send_client_peer_connected(t, myid);
930 else if (NULL == peer->dhtget)
932 struct GNUNET_PeerIdentity id;
934 GNUNET_PEER_resolve(peer->id, &id);
935 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
936 path_info->peer = peer;
939 GNUNET_DHT_get_start(dht_handle, /* handle */
940 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
941 GNUNET_BLOCK_TYPE_TEST, /* type */
942 &id.hashPubKey, /* key to search */
943 4, /* replication level */
944 GNUNET_DHT_RO_RECORD_ROUTE |
945 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
951 /* Otherwise, there is no path but the DHT get is already started. */
957 * Destroy the peer_info and free any allocated resources linked to it
958 * @param t tunnel the path belongs to
959 * @param pi the peer_info to destroy
960 * @return GNUNET_OK on success
963 peer_info_destroy (struct MeshPeerInfo *pi)
965 GNUNET_HashCode hash;
966 struct GNUNET_PeerIdentity id;
968 GNUNET_PEER_resolve (pi->id, &id);
969 GNUNET_PEER_change_rc (pi->id, -1);
970 GNUNET_CRYPTO_hash (&id, sizeof (struct GNUNET_PeerIdentity), &hash);
972 GNUNET_CONTAINER_multihashmap_remove (peers, &hash, pi);
973 GNUNET_SCHEDULER_cancel (pi->path_refresh_task);
981 * Notify a tunnel that a connection has broken that affects at least
984 * @param t Tunnel affected.
985 * @param peer Peer that (at least) has been affected by the disconnection.
986 * @param p1 Peer that got disconnected from p2.
987 * @param p2 Peer that got disconnected from p1.
989 * @return Short ID of the peer disconnected (either p1 or p2).
990 * 0 if the tunnel remained unaffected.
992 static GNUNET_PEER_Id
993 tunnel_notify_connection_broken (struct MeshTunnel *t,
994 struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
998 * Remove all paths that rely on a direct connection between p1 and p2
999 * from the peer itself and notify all tunnels about it.
1001 * @param peer PeerInfo of affected peer.
1002 * @param p1 GNUNET_PEER_Id of one peer.
1003 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1006 * TODO: optimize (see below)
1009 path_remove_from_peer (struct MeshPeerInfo *peer,
1013 struct GNUNET_PeerIdentity id;
1014 struct MeshPeerPath *p;
1015 struct MeshPeerPath *aux;
1016 struct MeshPeerInfo *peer_d;
1018 unsigned int destroyed;
1024 p = peer->path_head;
1028 for (i = 0; i < (p->length - 1); i++)
1030 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1031 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1043 for (i = 0; i < peer->ntunnels; i++)
1045 d = tunnel_notify_connection_broken (peer->tunnels[i], peer, p1, p2);
1047 * Problem: one or more peers have been deleted from the tunnel tree.
1048 * We don't know who they are to try to add them again.
1049 * We need to try to find a new path for each of the disconnected peers.
1050 * Some of them might already have a path to reach them that does not
1051 * involve p1 and p2. Adding all anew might render in a better tree than
1052 * the trivial immediate fix.
1054 * Trivial immiediate fix: try to reconnect to the disconnected node. All
1055 * its children will be reachable trough him.
1057 peer_d = peer_info_get_short(d);
1060 for (p = peer_d->path_head; NULL != p; p = p->next)
1062 if ((cost = path_get_cost(peer->tunnels[i]->tree, p)) < best)
1070 /* No callback, as peer will be already disconnected */
1071 tree_add_path(peer->tunnels[i]->tree, aux, NULL);
1075 struct MeshPathInfo *path_info;
1077 if (NULL != peer_d->dhtget)
1079 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
1080 path_info->path = p;
1081 path_info->peer = peer_d;
1082 path_info->t = peer->tunnels[i];
1084 GNUNET_DHT_get_start(dht_handle, /* handle */
1085 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1086 GNUNET_BLOCK_TYPE_TEST, /* type */
1087 &id.hashPubKey, /*key to search */
1088 4, /* replication level */
1089 GNUNET_DHT_RO_RECORD_ROUTE |
1090 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1092 0, /* xquery bits */
1093 &dht_get_id_handler,
1094 (void *) path_info);
1101 * Add the path to the peer and update the path used to reach it in case this
1104 * @param peer_info Destination peer to add the path to.
1105 * @param path New path to add. Last peer must be the peer in arg 1.
1106 * Path will be either used of freed if already known.
1108 * TODO: trim the part from origin to us? Add it as path to origin?
1111 path_add_to_peer (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
1113 struct MeshPeerPath *aux;
1117 if (NULL == peer_info || NULL == path)
1123 l = path_get_length (path);
1130 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1132 l2 = path_get_length (aux);
1135 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1136 peer_info->path_tail, aux, path);
1140 if (l2 == l && memcmp(path->peers, aux->peers, l) == 0)
1147 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1154 * Add the path to the origin peer and update the path used to reach it in case
1155 * this is the shortest.
1156 * The path is given in peer_info -> destination, therefore we turn the path
1157 * upside down first.
1159 * @param peer_info Peer to add the path to, being the origin of the path.
1160 * @param path New path to add after being inversed.
1163 path_add_to_origin (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
1166 path_add_to_peer (peer_info, path);
1171 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1172 * to obtain a local peer -> destination path and interning the peer ids.
1174 * @return Newly allocated and created path
1176 static struct MeshPeerPath *
1177 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1178 unsigned int get_path_length,
1179 const struct GNUNET_PeerIdentity *put_path,
1180 unsigned int put_path_length)
1182 struct MeshPeerPath *p;
1188 GNUNET_PEER_change_rc(myid, 1);
1189 i = get_path_length;
1190 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: GET has %d hops.\n", i);
1191 for (i--; i >= 0; i--)
1193 id = GNUNET_PEER_intern (&get_path[i]);
1194 if (p->length > 0 && id == p->peers[p->length - 1])
1196 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1197 GNUNET_PEER_change_rc(id, -1);
1201 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1202 "MESH: Adding from GET: %s.\n",
1203 GNUNET_i2s(&get_path[i]));
1205 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1206 p->peers[p->length - 1] = id;
1209 i = put_path_length;
1210 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: PUT has %d hops.\n", i);
1211 for (i--; i >= 0; i--)
1213 id = GNUNET_PEER_intern (&put_path[i]);
1216 /* PUT path went through us, so discard the path up until now and start
1217 * from here to get a much shorter (and loop-free) path.
1222 if (p->length > 0 && id == p->peers[p->length - 1])
1224 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1225 GNUNET_PEER_change_rc(id, -1);
1229 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1230 "MESH: Adding from PUT: %s.\n",
1231 GNUNET_i2s(&put_path[i]));
1233 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1234 p->peers[p->length - 1] = id;
1238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1239 "MESH: (first of GET: %s)\n",
1240 GNUNET_h2s_full(&get_path[0].hashPubKey));
1241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1242 "MESH: (first of PUT: %s)\n",
1243 GNUNET_h2s_full(&put_path[0].hashPubKey));
1244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1245 "MESH: In total: %d hops\n",
1247 for (i = 0; i < p->length; i++)
1249 struct GNUNET_PeerIdentity peer_id;
1251 GNUNET_PEER_resolve(p->peers[i], &peer_id);
1252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1255 GNUNET_h2s_full(&peer_id.hashPubKey));
1263 * Send keepalive packets for a peer
1265 * @param cls Closure (tunnel for which to send the keepalive).
1266 * @param tc Notification context.
1268 * TODO: implement explicit multicast keepalive?
1271 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1275 * Search for a tunnel among the tunnels for a client
1277 * @param c the client whose tunnels to search in
1278 * @param tid the local id of the tunnel
1280 * @return tunnel handler, NULL if doesn't exist
1282 static struct MeshTunnel *
1283 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1285 GNUNET_HashCode hash;
1287 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1288 return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
1293 * Search for a tunnel by global ID using PEER_ID
1295 * @param pi owner of the tunnel
1296 * @param tid global tunnel number
1298 * @return tunnel handler, NULL if doesn't exist
1300 static struct MeshTunnel *
1301 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1303 struct MESH_TunnelID id;
1304 GNUNET_HashCode hash;
1309 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1310 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1315 * Search for a tunnel by global ID using full PeerIdentities
1317 * @param oid owner of the tunnel
1318 * @param tid global tunnel number
1320 * @return tunnel handler, NULL if doesn't exist
1322 static struct MeshTunnel *
1323 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1325 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1330 * Callback used to notify a client owner of a tunnel that a peer has
1331 * disconnected, most likely because of a path change.
1333 * @param n Node in the tree representing the disconnected peer
1336 notify_peer_disconnected (const struct MeshTunnelTreeNode *n)
1338 struct MeshPeerInfo *peer;
1340 if (NULL != n->t->client && NULL != nc)
1342 struct GNUNET_MESH_PeerControl msg;
1343 msg.header.size = htons (sizeof (msg));
1344 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1345 msg.tunnel_id = htonl (n->t->local_tid);
1346 GNUNET_PEER_resolve (n->peer, &msg.peer);
1347 GNUNET_SERVER_notification_context_unicast (nc, n->t->client->handle,
1348 &msg.header, GNUNET_NO);
1350 peer = peer_info_get_short(n->peer);
1351 peer_info_connect(peer, n->t);
1356 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1357 * needed rescources.
1358 * If peer already exists, reevaluate shortest path and change if different.
1360 * @param t Tunnel we want to add a new peer to
1361 * @param peer PeerInfo of the peer being added
1365 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1367 struct GNUNET_PeerIdentity id;
1368 struct MeshPeerPath *best_p;
1369 struct MeshPeerPath *p;
1370 unsigned int best_cost;
1373 GNUNET_PEER_resolve(peer->id, &id);
1375 GNUNET_CONTAINER_multihashmap_contains(t->peers, &id.hashPubKey))
1378 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
1379 GNUNET_CONTAINER_multihashmap_put(
1383 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1386 if (NULL != (p = peer->path_head))
1389 best_cost = path_get_cost(t->tree, p);
1392 if ((cost = path_get_cost (t->tree, p)) < best_cost)
1399 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected);
1400 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
1401 t->path_refresh_task =
1402 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
1406 /* Start a DHT get if necessary */
1407 peer_info_connect(peer, t);
1412 * Add a path to a tunnel, without evaluating costs.
1414 * @param t Tunnel we want to add a new peer to
1415 * @param p Path to add
1419 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p)
1425 * Notify a tunnel that a connection has broken that affects at least
1426 * some of its peers.
1428 * @param t Tunnel affected.
1429 * @param peer Peer that (at least) has been affected by the disconnection.
1430 * @param p1 Peer that got disconnected from p2.
1431 * @param p2 Peer that got disconnected from p1.
1433 * @return Short ID of the peer disconnected (either p1 or p2).
1434 * 0 if the tunnel remained unaffected.
1436 static GNUNET_PEER_Id
1437 tunnel_notify_connection_broken (struct MeshTunnel *t,
1438 struct MeshPeerInfo *peer,
1442 return tree_notify_connection_broken (t->tree, p1, p2,
1443 ¬ify_peer_disconnected);
1448 * Destroy the tunnel and free any allocated resources linked to it
1450 * @param t the tunnel to destroy
1452 * @return GNUNET_OK on success
1455 tunnel_destroy (struct MeshTunnel *t)
1457 struct MeshClient *c;
1458 struct MeshQueue *q;
1459 struct MeshQueue *qn;
1460 GNUNET_HashCode hash;
1463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DESTROYING TUNNEL at %p\n", t);
1469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
1472 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1473 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
1478 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1479 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
1483 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
1487 if (NULL != q->data)
1488 GNUNET_free (q->data);
1492 /* TODO cancel core transmit ready in case it was active */
1494 tree_destroy(t->tree);
1495 if (NULL != t->dht_get_type)
1496 GNUNET_DHT_get_stop(t->dht_get_type);
1497 t->dht_get_type = NULL;
1504 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
1505 * client when the client disconnects.
1507 * @param cls closure (client that is disconnecting)
1508 * @param key the hash of the local tunnel id (used to access the hashmap)
1509 * @param value the value stored at the key (tunnel to destroy)
1511 * @return GNUNET_OK on success
1514 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
1516 struct MeshTunnel *t = value;
1519 if (NULL != t->dht_get_type)
1521 GNUNET_DHT_get_stop (t->dht_get_type);
1523 r = tunnel_destroy (t);
1528 /******************************************************************************/
1529 /**************** MESH NETWORK HANDLER HELPERS ***********************/
1530 /******************************************************************************/
1533 * Function called to notify a client about the socket
1534 * being ready to queue more data. "buf" will be
1535 * NULL and "size" zero if the socket was closed for
1536 * writing in the meantime.
1538 * @param cls closure
1539 * @param size number of bytes available in buf
1540 * @param buf where the callee should write the message
1541 * @return number of bytes written to buf
1544 send_core_create_path (void *cls, size_t size, void *buf)
1546 struct MeshPathInfo *info = cls;
1547 struct GNUNET_MESH_ManipulatePath *msg;
1548 struct GNUNET_PeerIdentity *peer_ptr;
1549 struct MeshPeerInfo *peer = info->peer;
1550 struct MeshTunnel *t = info->t;
1551 struct MeshPeerPath *p = info->path;
1556 sizeof (struct GNUNET_MESH_ManipulatePath) +
1557 p->length * sizeof (struct GNUNET_PeerIdentity);
1559 if (size < size_needed || NULL == buf)
1561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: create path retransmit!\n");
1562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: buf: %p\n", buf);
1563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: size: (%u/%u)\n",
1565 info->peer->core_transmit[info->pos] =
1566 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1567 GNUNET_TIME_UNIT_FOREVER_REL,
1568 path_get_first_hop (t->tree, peer->id),
1570 &send_core_create_path,
1575 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
1576 msg->header.size = htons (size_needed);
1577 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
1578 msg->tid = ntohl (t->id.tid);
1580 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1581 for (i = 0; i < p->length; i++)
1583 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1590 "MESH: CREATE PATH (%u bytes long) sent!\n",
1598 * Function called to notify a client about the socket
1599 * being ready to queue more data. "buf" will be
1600 * NULL and "size" zero if the socket was closed for
1601 * writing in the meantime.
1603 * @param cls closure (MeshDataDescriptor with all info to build packet)
1604 * @param size number of bytes available in buf
1605 * @param buf where the callee should write the message
1606 * @return number of bytes written to buf
1609 send_core_data_to_origin (void *cls, size_t size, void *buf)
1611 struct MeshDataDescriptor *info = cls;
1612 struct GNUNET_MESH_ToOrigin *msg = buf;
1615 GNUNET_assert (NULL != info);
1616 total_size = sizeof (struct GNUNET_MESH_ToOrigin) + info->size;
1617 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
1619 if (total_size > size)
1621 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1622 "not enough buffer to send data to origin\n");
1625 msg->header.size = htons (total_size);
1626 msg->header.type = htons (GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN);
1627 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1628 msg->tid = htonl (info->origin->tid);
1629 if (0 != info->size)
1631 memcpy (&msg[1], &info[1], info->size);
1633 if (NULL != info->client)
1635 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1644 * Function called to notify a client about the socket
1645 * being ready to queue more data. "buf" will be
1646 * NULL and "size" zero if the socket was closed for
1647 * writing in the meantime.
1649 * @param cls closure (data itself)
1650 * @param size number of bytes available in buf
1651 * @param buf where the callee should write the message
1653 * @return number of bytes written to buf
1656 send_core_data_multicast (void *cls, size_t size, void *buf)
1658 struct MeshDataDescriptor *info = cls;
1659 struct GNUNET_MESH_Multicast *msg = buf;
1662 GNUNET_assert (NULL != info);
1663 GNUNET_assert (NULL != info->peer);
1664 total_size = info->size + sizeof (struct GNUNET_MESH_Multicast);
1665 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1667 if (total_size > size)
1670 struct GNUNET_PeerIdentity id;
1672 GNUNET_PEER_resolve(info->peer->id, &id);
1673 info->peer->infos[info->handler_n] = info;
1674 info->peer->types[info->handler_n] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
1675 info->peer->core_transmit[info->handler_n] =
1676 GNUNET_CORE_notify_transmit_ready (core_handle,
1679 GNUNET_TIME_UNIT_FOREVER_REL,
1682 &send_core_data_multicast,
1686 info->peer->core_transmit[info->handler_n] = NULL;
1687 info->peer->infos[info->handler_n] = NULL;
1688 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
1689 msg->header.size = htons (total_size);
1690 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1691 msg->tid = htonl (info->origin->tid);
1692 memcpy (&msg[1], info->data, info->size);
1693 if (0 == --(*info->copies))
1695 if (NULL != info->client)
1697 /* FIXME One unresponsive neighbor (who doesn't "call" tmt_rdy) can lock
1698 * the client from sending anything else to the service.
1699 * - Call receive_done after certain timeout.
1700 * - Here cancel the timeout.
1702 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1704 GNUNET_free (info->data);
1705 GNUNET_free (info->copies);
1713 * Function called to notify a client about the socket
1714 * being ready to queue more data. "buf" will be
1715 * NULL and "size" zero if the socket was closed for
1716 * writing in the meantime.
1718 * @param cls closure (MeshDataDescriptor)
1719 * @param size number of bytes available in buf
1720 * @param buf where the callee should write the message
1721 * @return number of bytes written to buf
1724 send_core_path_ack (void *cls, size_t size, void *buf)
1726 struct MeshDataDescriptor *info = cls;
1727 struct GNUNET_MESH_PathACK *msg = buf;
1729 GNUNET_assert (NULL != info);
1732 info->peer->core_transmit[info->handler_n] = NULL;
1734 if (sizeof (struct GNUNET_MESH_PathACK) > size)
1739 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
1740 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
1741 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1742 msg->tid = htonl (info->origin->tid);
1743 msg->peer_id = my_full_id;
1745 /* TODO add signature */
1747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: PATH ACK sent!\n");
1748 return sizeof (struct GNUNET_MESH_PathACK);
1753 * Function called to notify a client about the socket
1754 * being ready to queue more data. "buf" will be
1755 * NULL and "size" zero if the socket was closed for
1756 * writing in the meantime.
1758 * @param cls closure (data itself)
1759 * @param size number of bytes available in buf
1760 * @param buf where the callee should write the message
1761 * @return number of bytes written to buf
1764 send_core_data_raw (void *cls, size_t size, void *buf)
1766 struct GNUNET_MessageHeader *msg = cls;
1769 GNUNET_assert (NULL != msg);
1770 total_size = ntohs (msg->size);
1772 if (total_size > size)
1777 memcpy (buf, msg, total_size);
1785 * Send another peer a notification to destroy a tunnel
1786 * @param cls The tunnel to destroy
1787 * @param size Size in the buffer
1788 * @param buf Memory where to put the data to transmit
1789 * @return Size of data put in buffer
1792 send_p2p_tunnel_destroy (void *cls, size_t size, void *buf)
1794 struct MeshTunnel *t = cls;
1795 struct MeshClient *c;
1796 struct GNUNET_MESH_TunnelMessage *msg;
1800 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1801 /*FIXME*/ msg->header.size =
1802 htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1803 msg->tunnel_id = htonl (t->id.tid);
1805 tunnel_destroy (c, t);
1806 return sizeof (struct GNUNET_MESH_TunnelMessage);
1811 /******************************************************************************/
1812 /******************** MESH NETWORK HANDLERS **************************/
1813 /******************************************************************************/
1817 * Core handler for path creation
1818 * struct GNUNET_CORE_MessageHandler
1820 * @param cls closure
1821 * @param message message
1822 * @param peer peer identity this notification is about
1823 * @param atsi performance data
1824 * @return GNUNET_OK to keep the connection open,
1825 * GNUNET_SYSERR to close it (signal serious error)
1829 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1830 const struct GNUNET_MessageHeader *message,
1831 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1833 unsigned int own_pos;
1836 MESH_TunnelNumber tid;
1837 struct GNUNET_MESH_ManipulatePath *msg;
1838 struct GNUNET_PeerIdentity *pi;
1839 GNUNET_HashCode hash;
1840 struct MeshPeerPath *path;
1841 struct MeshPeerInfo *dest_peer_info;
1842 struct MeshPeerInfo *orig_peer_info;
1843 struct MeshTunnel *t;
1845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1846 "MESH: Received a path create msg [%s]\n",
1847 GNUNET_i2s(&my_full_id));
1848 size = ntohs (message->size);
1849 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
1851 GNUNET_break_op (0);
1855 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
1856 if (size % sizeof (struct GNUNET_PeerIdentity))
1858 GNUNET_break_op (0);
1861 size /= sizeof (struct GNUNET_PeerIdentity);
1864 GNUNET_break_op (0);
1867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1868 "MESH: path has %u hops.\n",
1870 msg = (struct GNUNET_MESH_ManipulatePath *) message;
1872 tid = ntohl (msg->tid);
1873 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1874 t = tunnel_get (pi, tid);
1875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1876 "MESH: path is for tunnel %s [%X].\n",
1881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
1882 t = GNUNET_malloc (sizeof (struct MeshTunnel));
1883 t->id.oid = GNUNET_PEER_intern (pi);
1885 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
1886 t->local_tid = next_local_tid++;
1887 /* FIXME test if taken */
1888 next_local_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
1889 t->tree = tree_new(t, t->id.oid);
1891 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1893 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
1894 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1901 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
1902 if (NULL == dest_peer_info)
1904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1905 "MESH: Creating PeerInfo for destination.\n");
1906 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1907 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
1908 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
1910 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1912 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
1913 if (NULL == orig_peer_info)
1915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1916 "MESH: Creating PeerInfo for origin.\n");
1917 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1918 orig_peer_info->id = GNUNET_PEER_intern (pi);
1919 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
1920 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating path...\n");
1923 path = path_new (size);
1925 for (i = 0; i < size; i++)
1927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1928 "MESH: ... adding %s\n",
1929 GNUNET_i2s(&pi[i]));
1930 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
1931 if (path->peers[i] == myid)
1934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1935 "MESH: Own position: %u\n", own_pos);
1938 /* cannot be self, must be 'not found' */
1939 /* create path: self not found in path through self */
1940 GNUNET_break_op (0);
1941 path_destroy (path);
1942 /* FIXME error. destroy tunnel? leave for timeout? */
1945 if (own_pos == size - 1)
1947 /* It is for us! Send ack. */
1948 struct GNUNET_MESH_TunnelNotification cmsg;
1949 struct MeshDataDescriptor *info;
1952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1953 "MESH: It's for us!\n");
1954 path_add_to_origin (orig_peer_info, path); /* inverts path! */
1955 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1956 info->origin = &t->id;
1957 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1958 GNUNET_assert (NULL != info->peer);
1959 for (j = 0; info->peer->core_transmit[j]; j++)
1961 if (j == (CORE_QUEUE_SIZE - 1))
1967 info->handler_n = j;
1968 info->peer->core_transmit[j] =
1969 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1970 GNUNET_TIME_UNIT_FOREVER_REL, peer,
1971 sizeof (struct GNUNET_MESH_PathACK),
1972 &send_core_path_ack, info);
1973 cmsg.header.size = htons(sizeof(cmsg));
1974 cmsg.header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1975 GNUNET_PEER_resolve(t->id.oid, &cmsg.peer);
1976 cmsg.tunnel_id = htonl(t->local_tid);
1977 GNUNET_SERVER_notification_context_broadcast(nc, &cmsg.header, GNUNET_NO);
1981 struct MeshPeerPath *path2;
1983 path2 = path_duplicate(path);
1984 /* It's for somebody else! Retransmit. */
1985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1986 "MESH: Retransmitting.\n");
1987 path_add_to_peer(dest_peer_info, path);
1988 tunnel_add_path (t, path);
1989 path = path_duplicate(path2);
1990 path_add_to_origin(orig_peer_info, path2);
1991 send_create_path(dest_peer_info, path, t);
1998 * Core handler for mesh network traffic going from the origin to a peer
2000 * @param cls closure
2001 * @param peer peer identity this notification is about
2002 * @param message message
2003 * @param atsi performance data
2004 * @return GNUNET_OK to keep the connection open,
2005 * GNUNET_SYSERR to close it (signal serious error)
2008 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2009 const struct GNUNET_MessageHeader *message,
2010 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2012 struct GNUNET_MESH_Unicast *msg;
2013 struct MeshTunnel *t;
2014 struct MeshPeerInfo *pi;
2017 size = ntohs (message->size);
2019 sizeof (struct GNUNET_MESH_Unicast) +
2020 sizeof (struct GNUNET_MessageHeader))
2025 msg = (struct GNUNET_MESH_Unicast *) message;
2026 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2029 /* TODO notify back: we don't know this tunnel */
2030 GNUNET_break_op (0);
2033 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
2034 &msg->destination.hashPubKey);
2037 /* TODO maybe feedback, log to statistics */
2038 GNUNET_break_op (0);
2043 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
2046 msg = GNUNET_malloc (size);
2047 memcpy (msg, message, size);
2048 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2049 GNUNET_TIME_UNIT_FOREVER_REL,
2050 path_get_first_hop (t->tree, pi->id),
2052 &send_core_data_raw, msg);
2058 * Core handler for mesh network traffic going from the origin to all peers
2060 * @param cls closure
2061 * @param message message
2062 * @param peer peer identity this notification is about
2063 * @param atsi performance data
2064 * @return GNUNET_OK to keep the connection open,
2065 * GNUNET_SYSERR to close it (signal serious error)
2067 * TODO: Check who we got this from, to validate route.
2070 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2071 const struct GNUNET_MessageHeader *message,
2072 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2074 struct GNUNET_MESH_Multicast *msg;
2075 struct GNUNET_PeerIdentity *id;
2076 struct MeshDataDescriptor *info;
2077 struct MeshTunnelTreeNode *n;
2078 struct MeshTunnel *t;
2079 unsigned int *copies;
2084 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_Multicast);
2085 if (size < sizeof (struct GNUNET_MessageHeader))
2087 GNUNET_break_op (0);
2090 msg = (struct GNUNET_MESH_Multicast *) message;
2091 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2095 /* TODO notify that we dont know that tunnel */
2096 GNUNET_break_op (0);
2100 /* Transmit to locally interested clients */
2101 if (GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
2103 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
2105 n = t->tree->me->children_head;
2108 copies = GNUNET_malloc (sizeof (unsigned int));
2109 for (*copies = 0; NULL != n; n = n->next)
2111 n = t->tree->me->children_head;
2112 data = GNUNET_malloc (size);
2113 memcpy (data, &msg[1], size);
2116 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
2117 info->origin = &t->id;
2120 info->copies = copies;
2121 info->client = t->client->handle;
2122 info->destination = n->peer;
2123 id = path_get_first_hop(t->tree, n->peer);
2124 info->peer = peer_info_get(id);
2125 GNUNET_assert (NULL != info->peer);
2126 for (i = 0; NULL != info->peer->core_transmit[i]; i++)
2128 if (i == (CORE_QUEUE_SIZE - 1))
2134 info->handler_n = i;
2135 info->peer->infos[i] = info;
2136 info->peer->types[i] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
2137 info->peer->core_transmit[i] =
2138 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2139 GNUNET_TIME_UNIT_FOREVER_REL, id,
2140 ntohs (msg->header.size),
2141 &send_core_data_multicast, info);
2149 * Core handler for mesh network traffic
2151 * @param cls closure
2152 * @param message message
2153 * @param peer peer identity this notification is about
2154 * @param atsi performance data
2156 * @return GNUNET_OK to keep the connection open,
2157 * GNUNET_SYSERR to close it (signal serious error)
2160 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
2161 const struct GNUNET_MessageHeader *message,
2162 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2164 struct GNUNET_MESH_ToOrigin *msg;
2165 struct GNUNET_PeerIdentity id;
2166 struct MeshPeerInfo *peer_info;
2167 struct MeshTunnel *t;
2170 size = ntohs (message->size);
2171 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
2172 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
2174 GNUNET_break_op (0);
2177 msg = (struct GNUNET_MESH_ToOrigin *) message;
2178 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2182 /* TODO notify that we dont know this tunnel (whom)? */
2186 if (t->id.oid == myid)
2188 if (NULL == t->client)
2190 /* got data packet for ownerless tunnel */
2191 GNUNET_break_op (0);
2194 /* TODO signature verification */
2195 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle, message,
2199 peer_info = peer_info_get (&msg->oid);
2200 if (NULL == peer_info)
2202 /* unknown origin of tunnel */
2206 GNUNET_PEER_resolve (t->tree->me->parent->peer, &id);
2207 msg = GNUNET_malloc (size);
2208 memcpy (msg, message, size);
2209 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2210 GNUNET_TIME_UNIT_FOREVER_REL, &id, size,
2211 &send_core_data_raw, msg);
2218 * Core handler for path ACKs
2220 * @param cls closure
2221 * @param message message
2222 * @param peer peer identity this notification is about
2223 * @param atsi performance data
2225 * @return GNUNET_OK to keep the connection open,
2226 * GNUNET_SYSERR to close it (signal serious error)
2229 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2230 const struct GNUNET_MessageHeader *message,
2231 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2233 struct GNUNET_MESH_PathACK *msg;
2234 struct MeshTunnelTreeNode *n;
2235 struct MeshPeerInfo *peer_info;
2236 struct MeshTunnel *t;
2238 msg = (struct GNUNET_MESH_PathACK *) message;
2239 t = tunnel_get (&msg->oid, msg->tid);
2242 /* TODO notify that we don't know the tunnel */
2246 /* Message for us? */
2247 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
2249 if (NULL == t->client)
2251 GNUNET_break_op (0);
2254 peer_info = peer_info_get (&msg->peer_id);
2255 if (NULL == peer_info)
2257 GNUNET_break_op (0);
2260 n = tree_find_peer(t->tree->root, peer_info->id);
2263 GNUNET_break_op (0);
2266 n->status = MESH_PEER_READY;
2267 send_client_peer_connected(t, peer_info->id);
2271 peer_info = peer_info_get (&msg->oid);
2272 if (NULL == peer_info)
2274 /* If we know the tunnel, we should DEFINITELY know the peer */
2278 msg = GNUNET_malloc (sizeof (struct GNUNET_MESH_PathACK));
2279 memcpy (msg, message, sizeof (struct GNUNET_MESH_PathACK));
2280 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2281 GNUNET_TIME_UNIT_FOREVER_REL,
2282 path_get_first_hop (t->tree,
2284 sizeof (struct GNUNET_MESH_PathACK),
2285 &send_core_data_raw, msg);
2291 * Functions to handle messages from core
2293 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
2294 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
2295 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
2296 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
2297 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
2298 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2299 sizeof (struct GNUNET_MESH_PathACK)},
2305 /******************************************************************************/
2306 /**************** MESH LOCAL HANDLER HELPERS ***********************/
2307 /******************************************************************************/
2310 * deregister_app: iterator for removing each application registered by a client
2312 * @param cls closure
2313 * @param key the hash of the application id (used to access the hashmap)
2314 * @param value the value stored at the key (client)
2316 * @return GNUNET_OK on success
2319 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
2321 GNUNET_CONTAINER_multihashmap_remove (applications, key, value);
2327 * notify_client_connection_failure: notify a client that the connection to the
2328 * requested remote peer is not possible (for instance, no route found)
2329 * Function called when the socket is ready to queue more data. "buf" will be
2330 * NULL and "size" zero if the socket was closed for writing in the meantime.
2332 * @param cls closure
2333 * @param size number of bytes available in buf
2334 * @param buf where the callee should write the message
2335 * @return number of bytes written to buf
2338 notify_client_connection_failure (void *cls, size_t size, void *buf)
2341 struct MeshPeerInfo *peer_info;
2342 struct GNUNET_MESH_PeerControl *msg;
2343 struct GNUNET_PeerIdentity id;
2345 if (0 == size && NULL == buf)
2347 // TODO retry? cancel?
2351 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
2352 peer_info = (struct MeshPeerInfo *) cls;
2353 msg = (struct GNUNET_MESH_PeerControl *) buf;
2354 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2355 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
2356 // msg->tunnel_id = htonl(peer_info->t->tid);
2357 GNUNET_PEER_resolve (peer_info->id, &id);
2358 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
2366 * Send keepalive packets for a peer
2368 * @param cls Closure (tunnel for which to send the keepalive).
2369 * @param tc Notification context.
2371 * TODO: implement explicit multicast keepalive?
2374 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2376 struct MeshTunnel *t = cls;
2377 struct GNUNET_MessageHeader *payload;
2378 struct GNUNET_MESH_Multicast *msg;
2381 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
2382 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2388 "MESH: sending keepalive for tunnel %d\n",
2391 size = sizeof(struct GNUNET_MESH_Multicast) +
2392 sizeof(struct GNUNET_MessageHeader);
2393 msg = GNUNET_malloc (size);
2394 msg->header.size = htons (size);
2395 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
2396 msg->oid = my_full_id;
2397 msg->tid = htonl(t->id.tid);
2398 payload = (struct GNUNET_MessageHeader *) &msg[1];
2399 payload->size = htons (sizeof(struct GNUNET_MessageHeader));
2400 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2401 handle_mesh_data_multicast (NULL, &my_full_id, &msg->header, NULL);
2404 t->path_refresh_task =
2405 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
2411 * Function to process paths received for a new peer addition. The recorded
2412 * paths form the initial tunnel, which can be optimized later.
2413 * Called on each result obtained for the DHT search.
2415 * @param cls closure
2416 * @param exp when will this value expire
2417 * @param key key of the result
2418 * @param type type of the result
2419 * @param size number of bytes in data
2420 * @param data pointer to the result data
2422 * TODO: re-issue the request after certain time? cancel after X results?
2425 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2426 const GNUNET_HashCode * key,
2427 const struct GNUNET_PeerIdentity *get_path,
2428 unsigned int get_path_length,
2429 const struct GNUNET_PeerIdentity *put_path,
2430 unsigned int put_path_length,
2431 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
2433 struct MeshPathInfo *path_info = cls;
2434 struct MeshPeerPath *p;
2435 struct GNUNET_PeerIdentity pi;
2438 GNUNET_PEER_resolve (path_info->peer->id, &pi);
2439 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2440 "MESH: Got results from DHT for %s\n",
2441 GNUNET_h2s_full(&pi.hashPubKey));
2442 GNUNET_DHT_get_stop(path_info->peer->dhtget);
2443 path_info->peer->dhtget = NULL;
2445 p = path_build_from_dht (get_path, get_path_length,
2446 put_path, put_path_length);
2447 path_add_to_peer (path_info->peer, p);
2448 for (i = 0; i < path_info->peer->ntunnels; i++)
2450 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
2451 peer_info_connect(path_info->peer, path_info->t);
2453 GNUNET_free (path_info);
2460 * Function to process paths received for a new peer addition. The recorded
2461 * paths form the initial tunnel, which can be optimized later.
2462 * Called on each result obtained for the DHT search.
2464 * @param cls closure
2465 * @param exp when will this value expire
2466 * @param key key of the result
2467 * @param type type of the result
2468 * @param size number of bytes in data
2469 * @param data pointer to the result data
2472 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2473 const GNUNET_HashCode * key,
2474 const struct GNUNET_PeerIdentity *get_path,
2475 unsigned int get_path_length,
2476 const struct GNUNET_PeerIdentity *put_path,
2477 unsigned int put_path_length,
2478 enum GNUNET_BLOCK_Type type, size_t size,
2481 const struct GNUNET_PeerIdentity *pi = data;
2482 struct GNUNET_PeerIdentity id;
2483 struct MeshTunnel *t = cls;
2484 struct MeshPeerInfo *peer_info;
2485 struct MeshPathInfo *path_info;
2486 struct MeshPeerPath *p;
2489 if (size != sizeof (struct GNUNET_PeerIdentity))
2491 GNUNET_break_op (0);
2494 GNUNET_assert (NULL != t->client);
2495 GNUNET_DHT_get_stop (t->dht_get_type);
2496 t->dht_get_type = NULL;
2497 peer_info = peer_info_get (pi);
2498 GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey, peer_info,
2499 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2501 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path_head &&
2502 NULL == peer_info->dhtget)
2504 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2505 path_info->peer = peer_info;
2507 /* we don't have a route to the peer, let's try a direct lookup */
2509 GNUNET_DHT_get_start (dht_handle, /* handle */
2510 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
2511 GNUNET_BLOCK_TYPE_TEST, /* block type */
2512 &pi->hashPubKey, /* key to look up */
2513 10U, /* replication level */
2514 GNUNET_DHT_RO_RECORD_ROUTE |
2515 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2516 /* option to dht: record route */
2518 0, /* xquery bits */
2519 dht_get_id_handler, /* callback */
2520 path_info); /* closure */
2524 p = path_build_from_dht (get_path, get_path_length, put_path, put_path_length);
2525 path_add_to_peer (peer_info, p);
2526 tunnel_add_peer(t, peer_info);
2527 p = tree_get_path_to_peer(t->tree, peer_info->id);
2529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2530 "MESH: new route for tunnel 0x%x found, has %u hops\n",
2531 t->local_tid, p->length);
2532 for (i = 0; i < p->length; i++)
2534 GNUNET_PEER_resolve (p->peers[0], &id);
2535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:\t%d\t%s\n", i,
2536 GNUNET_h2s_full (&id.hashPubKey));
2542 send_create_path(peer_info, p, t);
2546 send_client_peer_connected(t, myid);
2550 /******************************************************************************/
2551 /********************* MESH LOCAL HANDLES **************************/
2552 /******************************************************************************/
2556 * Handler for client disconnection
2558 * @param cls closure
2559 * @param client identification of the client; NULL
2560 * for the last call when the server is destroyed
2563 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
2565 struct MeshClient *c;
2566 struct MeshClient *next;
2568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
2570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
2574 if (c->handle != client && NULL != client)
2576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
2580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
2581 if (NULL != c->tunnels)
2583 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
2584 &tunnel_destroy_iterator,
2586 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
2589 /* deregister clients applications */
2590 if (NULL != c->apps)
2592 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
2593 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
2595 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
2596 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
2598 GNUNET_SCHEDULER_cancel (announce_applications_task);
2599 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
2601 if (NULL != c->types)
2602 GNUNET_CONTAINER_multihashmap_destroy (c->types);
2603 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
2609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
2615 * Handler for new clients
2617 * @param cls closure
2618 * @param client identification of the client
2619 * @param message the actual message, which includes messages the client wants
2622 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
2623 const struct GNUNET_MessageHeader *message)
2625 struct GNUNET_MESH_ClientConnect *cc_msg;
2626 struct MeshClient *c;
2627 GNUNET_MESH_ApplicationType *a;
2634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
2635 /* Check data sanity */
2636 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
2637 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
2638 ntypes = ntohs (cc_msg->types);
2639 napps = ntohs (cc_msg->applications);
2641 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
2644 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2648 /* Create new client structure */
2649 c = GNUNET_malloc (sizeof (struct MeshClient));
2651 c->id = next_client_id++;
2654 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
2657 GNUNET_MESH_ApplicationType at;
2660 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
2661 for (i = 0; i < napps; i++)
2664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
2665 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
2666 /* store in clients hashmap */
2667 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
2668 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2669 /* store in global hashmap, for announcements */
2670 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
2671 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2673 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
2674 announce_applications_task =
2675 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
2683 t = (uint16_t *) & a[napps];
2684 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
2685 for (i = 0; i < ntypes; i++)
2688 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
2690 /* store in clients hashmap */
2691 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
2692 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2693 /* store in global hashmap */
2694 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
2695 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2699 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
2701 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
2702 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
2703 GNUNET_SERVER_notification_context_add (nc, client);
2705 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
2713 * Handler for requests of new tunnels
2715 * @param cls closure
2716 * @param client identification of the client
2717 * @param message the actual message
2720 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
2721 const struct GNUNET_MessageHeader *message)
2723 struct GNUNET_MESH_TunnelMessage *t_msg;
2724 struct MeshTunnel *t;
2725 struct MeshClient *c;
2726 GNUNET_HashCode hash;
2728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
2730 /* Sanity check for client registration */
2731 if (NULL == (c = client_get (client)))
2734 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2741 /* Message sanity check */
2742 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2745 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2749 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2750 /* Sanity check for tunnel numbering */
2751 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
2754 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2757 /* Sanity check for duplicate tunnel IDs */
2758 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
2761 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2765 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATED TUNNEL at %p\n", t);
2767 while (NULL != tunnel_get_by_pi (myid, next_tid))
2768 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
2769 t->id.tid = next_tid++;
2771 t->local_tid = ntohl (t_msg->tunnel_id);
2773 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
2775 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2777 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
2778 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2781 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2785 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2787 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2788 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2791 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2794 t->tree = tree_new (t, myid);
2795 t->tree->refresh = REFRESH_PATH_TIME;
2796 t->tree->root->status = MESH_PEER_READY;
2797 t->tree->me = t->tree->root;
2799 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2805 * Handler for requests of deleting tunnels
2807 * @param cls closure
2808 * @param client identification of the client
2809 * @param message the actual message
2812 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
2813 const struct GNUNET_MessageHeader *message)
2815 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
2816 struct MeshClient *c;
2817 struct MeshTunnel *t;
2818 MESH_TunnelNumber tid;
2819 GNUNET_HashCode hash;
2821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel\n");
2823 /* Sanity check for client registration */
2824 if (NULL == (c = client_get (client)))
2827 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2830 /* Message sanity check */
2831 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2834 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2840 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2842 /* Retrieve tunnel */
2843 tid = ntohl (tunnel_msg->tunnel_id);
2845 /* Remove from local id hashmap */
2846 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2847 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
2848 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t);
2850 /* Remove from global id hashmap */
2851 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2852 GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t);
2854 // notify_tunnel_destroy(t); FIXME
2856 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2862 * Handler for connection requests to new peers
2864 * @param cls closure
2865 * @param client identification of the client
2866 * @param message the actual message (PeerControl)
2869 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
2870 const struct GNUNET_MessageHeader *message)
2872 struct GNUNET_MESH_PeerControl *peer_msg;
2873 struct MeshPeerInfo *peer_info;
2874 struct MeshClient *c;
2875 struct MeshTunnel *t;
2876 MESH_TunnelNumber tid;
2878 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Got connection request\n");
2879 /* Sanity check for client registration */
2880 if (NULL == (c = client_get (client)))
2883 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2887 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2888 /* Sanity check for message size */
2889 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2892 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2896 /* Tunnel exists? */
2897 tid = ntohl (peer_msg->tunnel_id);
2898 t = tunnel_get_by_local_id (c, tid);
2902 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2906 /* Does client own tunnel? */
2907 if (t->client->handle != client)
2910 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2913 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: for %s\n",
2914 GNUNET_h2s_full(&peer_msg->peer.hashPubKey));
2915 peer_info = peer_info_get (&peer_msg->peer);
2917 tunnel_add_peer(t, peer_info);
2918 peer_info_connect(peer_info, t);
2920 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2926 * Handler for disconnection requests of peers in a tunnel
2928 * @param cls closure
2929 * @param client identification of the client
2930 * @param message the actual message (PeerControl)
2933 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
2934 const struct GNUNET_MessageHeader *message)
2936 struct GNUNET_MESH_PeerControl *peer_msg;
2937 struct MeshClient *c;
2938 struct MeshTunnel *t;
2939 MESH_TunnelNumber tid;
2941 /* Sanity check for client registration */
2942 if (NULL == (c = client_get (client)))
2945 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2948 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2949 /* Sanity check for message size */
2950 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2953 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2957 /* Tunnel exists? */
2958 tid = ntohl (peer_msg->tunnel_id);
2959 t = tunnel_get_by_local_id (c, tid);
2963 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2967 /* Does client own tunnel? */
2968 if (t->client->handle != client)
2971 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2975 /* Ok, delete peer from tunnel */
2976 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
2977 &peer_msg->peer.hashPubKey);
2979 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2985 * Handler for connection requests to new peers by type
2987 * @param cls closure
2988 * @param client identification of the client
2989 * @param message the actual message (ConnectPeerByType)
2992 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
2993 const struct GNUNET_MessageHeader *message)
2995 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
2996 struct MeshClient *c;
2997 struct MeshTunnel *t;
2998 GNUNET_HashCode hash;
2999 MESH_TunnelNumber tid;
3001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
3002 /* Sanity check for client registration */
3003 if (NULL == (c = client_get (client)))
3006 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3010 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
3011 /* Sanity check for message size */
3012 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
3013 ntohs (connect_msg->header.size))
3016 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3020 /* Tunnel exists? */
3021 tid = ntohl (connect_msg->tunnel_id);
3022 t = tunnel_get_by_local_id (c, tid);
3026 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3030 /* Does client own tunnel? */
3031 if (t->client->handle != client)
3034 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3038 /* Do WE have the service? */
3039 t->type = ntohl (connect_msg->type);
3040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", t->type);
3041 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
3042 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
3045 /* Yes! Fast forward, add ourselves to the tunnel and send the
3046 * good news to the client
3048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
3049 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
3050 peer_info_get (&my_full_id),
3051 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
3054 send_client_peer_connected(t, myid);
3055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
3056 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3059 /* Ok, lets find a peer offering the service */
3060 if (NULL != t->dht_get_type)
3062 GNUNET_DHT_get_stop (t->dht_get_type);
3064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
3065 GNUNET_h2s_full (&hash));
3067 GNUNET_DHT_get_start (dht_handle,
3068 GNUNET_TIME_UNIT_FOREVER_REL,
3069 GNUNET_BLOCK_TYPE_TEST,
3072 GNUNET_DHT_RO_RECORD_ROUTE |
3073 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
3075 &dht_get_type_handler, t);
3077 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3083 * Handler for client traffic directed to one peer
3085 * @param cls closure
3086 * @param client identification of the client
3087 * @param message the actual message
3090 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
3091 const struct GNUNET_MessageHeader *message)
3093 struct MeshClient *c;
3094 struct MeshTunnel *t;
3095 struct MeshPeerInfo *pi;
3096 struct GNUNET_MESH_Unicast *data_msg;
3097 MESH_TunnelNumber tid;
3100 /* Sanity check for client registration */
3101 if (NULL == (c = client_get (client)))
3104 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3107 data_msg = (struct GNUNET_MESH_Unicast *) message;
3108 /* Sanity check for message size */
3109 size = ntohs (message->size);
3110 if (sizeof (struct GNUNET_MESH_Unicast) +
3111 sizeof (struct GNUNET_MessageHeader) > size)
3114 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3118 /* Tunnel exists? */
3119 tid = ntohl (data_msg->tid);
3120 t = tunnel_get_by_local_id (c, tid);
3124 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3128 /* Is it a local tunnel? Then, does client own the tunnel? */
3129 if (t->client->handle != NULL && t->client->handle != client)
3132 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3136 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
3137 &data_msg->destination.hashPubKey);
3138 /* Is the selected peer in the tunnel? */
3142 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3146 /* Ok, everything is correct, send the message
3147 * (pretend we got it from a mesh peer)
3150 char buf[ntohs (message->size)];
3151 struct GNUNET_MESH_Unicast *copy;
3153 /* Work around const limitation */
3154 copy = (struct GNUNET_MESH_Unicast *) buf;
3155 memcpy (buf, data_msg, size);
3156 copy->oid = my_full_id;
3157 copy->tid = htonl (t->id.tid);
3158 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL);
3160 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3165 * Handler for client traffic directed to all peers in a tunnel
3167 * @param cls closure
3168 * @param client identification of the client
3169 * @param message the actual message
3172 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
3173 const struct GNUNET_MessageHeader *message)
3175 struct MeshClient *c;
3176 struct MeshTunnel *t;
3177 struct GNUNET_MESH_Multicast *data_msg;
3178 MESH_TunnelNumber tid;
3180 /* Sanity check for client registration */
3181 if (NULL == (c = client_get (client)))
3184 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3187 data_msg = (struct GNUNET_MESH_Multicast *) message;
3188 /* Sanity check for message size */
3189 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (data_msg->header.size))
3192 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3196 /* Tunnel exists? */
3197 tid = ntohl (data_msg->tid);
3198 t = tunnel_get_by_local_id (c, tid);
3202 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3206 /* Does client own tunnel? */
3207 if (t->client->handle != client)
3210 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3215 char buf[ntohs(message->size)];
3216 struct GNUNET_MESH_Multicast *copy;
3218 copy = (struct GNUNET_MESH_Multicast *)buf;
3219 memcpy(buf, message, ntohs(message->size));
3220 copy->oid = my_full_id;
3221 copy->tid = htonl(t->id.tid);
3222 handle_mesh_data_multicast(client, &my_full_id, ©->header, NULL);
3225 /* receive done gets called when last copy is sent */
3230 * Functions to handle messages from clients
3232 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
3233 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
3234 {&handle_local_tunnel_create, NULL,
3235 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
3236 sizeof (struct GNUNET_MESH_TunnelMessage)},
3237 {&handle_local_tunnel_destroy, NULL,
3238 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
3239 sizeof (struct GNUNET_MESH_TunnelMessage)},
3240 {&handle_local_connect_add, NULL,
3241 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
3242 sizeof (struct GNUNET_MESH_PeerControl)},
3243 {&handle_local_connect_del, NULL,
3244 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
3245 sizeof (struct GNUNET_MESH_PeerControl)},
3246 {&handle_local_connect_by_type, NULL,
3247 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
3248 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
3249 {&handle_local_unicast, NULL,
3250 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3251 {&handle_local_unicast, NULL,
3252 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3253 {&handle_local_multicast, NULL,
3254 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3260 * To be called on core init/fail.
3262 * @param cls service closure
3263 * @param server handle to the server for this service
3264 * @param identity the public identity of this peer
3267 core_init (void *cls, struct GNUNET_CORE_Handle *server,
3268 const struct GNUNET_PeerIdentity *identity)
3270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
3271 core_handle = server;
3272 if (0 != memcmp(identity, &my_full_id, sizeof(my_full_id)) || NULL == server)
3274 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
3275 GNUNET_SCHEDULER_shutdown();
3281 * Method called whenever a given peer connects.
3283 * @param cls closure
3284 * @param peer peer identity this notification is about
3285 * @param atsi performance data for the connection
3288 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
3289 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
3291 struct MeshPeerInfo *peer_info;
3292 struct MeshPeerPath *path;
3294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
3295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
3296 GNUNET_h2s(&my_full_id.hashPubKey));
3297 peer_info = peer_info_get (peer);
3298 if (myid == peer_info->id)
3300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
3306 GNUNET_h2s(&peer->hashPubKey));
3308 path = path_new (2);
3309 path->peers[0] = myid;
3310 path->peers[1] = peer_info->id;
3311 GNUNET_PEER_change_rc(myid, 1);
3312 GNUNET_PEER_change_rc(peer_info->id, 1);
3313 path_add_to_peer (peer_info, path);
3318 * Method called whenever a peer disconnects.
3320 * @param cls closure
3321 * @param peer peer identity this notification is about
3324 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
3326 struct MeshPeerInfo *pi;
3329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
3330 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
3336 for (i = 0; i < CORE_QUEUE_SIZE; i++)
3338 peer_info_cancel_transmission(pi, i);
3340 path_remove_from_peer (pi, pi->id, myid);
3343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3349 /******************************************************************************/
3350 /************************ MAIN FUNCTIONS ****************************/
3351 /******************************************************************************/
3354 * Iterator over hash map entries.
3356 * @param cls closure
3357 * @param key current key code
3358 * @param value value in the hash map
3359 * @return GNUNET_YES if we should continue to
3364 shutdown_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
3366 struct MeshTunnel *t = value;
3372 * Task run during shutdown.
3378 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
3381 /* TODO: destroy tunnels? */
3382 if (core_handle != NULL)
3384 GNUNET_CORE_disconnect (core_handle);
3387 GNUNET_CONTAINER_multihashmap_iterate(tunnels, &shutdown_tunnel, NULL);
3388 if (dht_handle != NULL)
3390 GNUNET_DHT_disconnect (dht_handle);
3395 GNUNET_SERVER_notification_context_destroy (nc);
3398 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
3400 GNUNET_SCHEDULER_cancel (announce_id_task);
3401 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
3403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
3407 * Process mesh requests.
3409 * @param cls closure
3410 * @param server the initialized server
3411 * @param c configuration to use
3414 run (void *cls, struct GNUNET_SERVER_Handle *server,
3415 const struct GNUNET_CONFIGURATION_Handle *c)
3417 struct MeshPeerInfo *peer;
3418 struct MeshPeerPath *p;
3421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
3422 server_handle = server;
3423 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
3424 CORE_QUEUE_SIZE, /* queue size */
3425 NULL, /* Closure passed to MESH functions */
3426 &core_init, /* Call core_init once connected */
3427 &core_connect, /* Handle connects */
3428 &core_disconnect, /* remove peers on disconnects */
3429 NULL, /* Don't notify about all incoming messages */
3430 GNUNET_NO, /* For header only in notification */
3431 NULL, /* Don't notify about all outbound messages */
3432 GNUNET_NO, /* For header-only out notification */
3433 core_handlers); /* Register these handlers */
3434 if (core_handle == NULL)
3437 GNUNET_SCHEDULER_shutdown ();
3442 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
3445 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3447 ("Mesh service is lacking key configuration settings. Exiting.\n"));
3448 GNUNET_SCHEDULER_shutdown ();
3451 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3452 GNUNET_free (keyfile);
3453 if (my_private_key == NULL)
3455 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3456 _("Mesh service could not access hostkey. Exiting.\n"));
3457 GNUNET_SCHEDULER_shutdown ();
3460 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
3461 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
3462 &my_full_id.hashPubKey);
3463 myid = GNUNET_PEER_intern (&my_full_id);
3465 dht_handle = GNUNET_DHT_connect (c, 64);
3466 if (dht_handle == NULL)
3472 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3474 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3475 peers = GNUNET_CONTAINER_multihashmap_create (32);
3476 applications = GNUNET_CONTAINER_multihashmap_create (32);
3477 types = GNUNET_CONTAINER_multihashmap_create (32);
3479 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
3480 nc = GNUNET_SERVER_notification_context_create (server_handle,
3482 GNUNET_SERVER_disconnect_notify (server_handle,
3483 &handle_local_client_disconnect,
3488 clients_tail = NULL;
3493 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3494 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
3496 /* Create a peer_info for the local peer */
3497 peer = peer_info_get(&my_full_id);
3500 path_add_to_peer(peer, p);
3502 /* Scheduled the task to clean up when shutdown is called */
3503 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
3506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
3510 * The main function for the mesh service.
3512 * @param argc number of arguments from the command line
3513 * @param argv command line arguments
3514 * @return 0 ok, 1 on error
3517 main (int argc, char *const *argv)
3522 // fprintf (stderr, "main ()\n");
3524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
3527 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
3529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");
3531 // fprintf (stderr, "main () END\n");