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 * Tunnels incoming, indexed by MESH_TunnelNumber
413 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
415 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
418 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
420 static struct GNUNET_CONTAINER_MultiHashMap *peers;
423 * Handle to communicate with core
425 static struct GNUNET_CORE_Handle *core_handle;
430 static struct GNUNET_DHT_Handle *dht_handle;
435 static struct GNUNET_SERVER_Handle *server_handle;
438 * Notification context, to send messages to local clients
440 static struct GNUNET_SERVER_NotificationContext *nc;
443 * Local peer own ID (memory efficient handle)
445 static GNUNET_PEER_Id myid;
448 * Local peer own ID (full value)
450 static struct GNUNET_PeerIdentity my_full_id;
455 static struct GNUNET_CRYPTO_RsaPrivateKey* my_private_key;
460 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
463 * Tunnel ID for the next created tunnel (global tunnel number)
465 static MESH_TunnelNumber next_tid;
468 * Tunnel ID for the next incoming tunnel (local tunnel number)
470 static MESH_TunnelNumber next_local_tid;
473 * All application types provided by this peer
475 static struct GNUNET_CONTAINER_MultiHashMap *applications;
478 * All message types clients of this peer are interested in
480 static struct GNUNET_CONTAINER_MultiHashMap *types;
483 * Task to periodically announce provided applications
485 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
488 * Task to periodically announce itself in the network
490 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
493 unsigned int next_client_id;
497 /******************************************************************************/
498 /************************ ITERATORS ****************************/
499 /******************************************************************************/
501 /* FIXME move iterators here */
504 /******************************************************************************/
505 /************************ PERIODIC FUNCTIONS ****************************/
506 /******************************************************************************/
509 * Announce iterator over for each application provided by the peer
512 * @param key current key code
513 * @param value value in the hash map
514 * @return GNUNET_YES if we should continue to
519 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
521 /* FIXME are hashes in multihash map equal on all aquitectures? */
522 GNUNET_DHT_put (dht_handle,
525 GNUNET_DHT_RO_RECORD_ROUTE |
526 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
527 GNUNET_BLOCK_TYPE_TEST,
528 sizeof (struct GNUNET_PeerIdentity),
529 (const char *) &my_full_id,
531 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
532 &mesh_debug, "DHT_put for app completed");
534 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
536 APP_ANNOUNCE_TIME, NULL, NULL);
543 * Periodically announce what applications are provided by local clients
546 * @param tc task context
549 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
551 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
553 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Starting PUT for apps\n");
557 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
559 announce_applications_task =
560 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Finished PUT for apps\n");
568 * Periodically announce self id in the DHT
571 * @param tc task context
574 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
576 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
578 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
582 * - Set data expiration in function of X
585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DHT_put for ID %s started.\n",
586 GNUNET_h2s_full (&my_full_id.hashPubKey));
587 GNUNET_DHT_put (dht_handle, /* DHT handle */
588 &my_full_id.hashPubKey, /* Key to use */
589 10U, /* Replication level */
590 GNUNET_DHT_RO_RECORD_ROUTE |
591 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
592 GNUNET_BLOCK_TYPE_TEST, /* Block type */
593 sizeof(my_full_id), /* Size of the data */
594 (char *)&my_full_id, /* Data itself */
595 GNUNET_TIME_absolute_get_forever (), /* Data expiration */
596 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
598 &mesh_debug, "DHT_put for id completed");
600 NULL, /* Continuation */
601 NULL); /* Continuation closure */
604 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
609 * Function to process paths received for a new peer addition. The recorded
610 * paths form the initial tunnel, which can be optimized later.
611 * Called on each result obtained for the DHT search.
614 * @param exp when will this value expire
615 * @param key key of the result
616 * @param type type of the result
617 * @param size number of bytes in data
618 * @param data pointer to the result data
621 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
622 const GNUNET_HashCode * key,
623 const struct GNUNET_PeerIdentity *get_path,
624 unsigned int get_path_length,
625 const struct GNUNET_PeerIdentity *put_path,
626 unsigned int put_path_length,
627 enum GNUNET_BLOCK_Type type, size_t size, const void *data);
630 /******************************************************************************/
631 /****************** GENERAL HELPER FUNCTIONS ************************/
632 /******************************************************************************/
635 * Check if client has registered with the service and has not disconnected
637 * @param client the client to check
639 * @return non-NULL if client exists in the global DLL
641 static struct MeshClient *
642 client_get (struct GNUNET_SERVER_Client *client)
644 struct MeshClient *c;
649 if (c->handle == client)
658 * Checks if a given client has subscribed to certain message type
660 * @param message_type Type of message to check
661 * @param c Client to check
663 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
668 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
672 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
673 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
678 * Search for a tunnel by global ID using full PeerIdentities
680 * @param oid owner of the tunnel
681 * @param tid global tunnel number
683 * @return tunnel handler, NULL if doesn't exist
685 static struct MeshTunnel *
686 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
690 * Send the message to all clients that have subscribed to its type
692 * @param msg Pointer to the message itself
693 * @return number of clients this message was sent to
696 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
697 const struct GNUNET_MessageHeader *payload)
699 struct GNUNET_PeerIdentity *oid;
700 struct MeshClient *c;
701 struct MeshTunnel *t;
702 MESH_TunnelNumber *tid;
705 char cbuf[htons(msg->size)];
707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Sending to clients...\n");
708 type = ntohs (payload->type);
709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: message of type %u\n", type);
711 memcpy (cbuf, msg, sizeof(cbuf));
712 switch (htons(msg->type))
714 struct GNUNET_MESH_Unicast *uc;
715 struct GNUNET_MESH_Multicast *mc;
716 struct GNUNET_MESH_ToOrigin *to;
718 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
719 uc = (struct GNUNET_MESH_Unicast *) cbuf;
723 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
724 mc = (struct GNUNET_MESH_Multicast *) cbuf;
728 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
729 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
737 t = tunnel_get (oid, ntohl(*tid));
743 *tid = htonl (t->local_tid);
744 for (count = 0, c = clients; c != NULL; c = c->next)
746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client %u\n", c->id);
747 if (client_is_subscribed (type, c))
750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: sending\n");
751 GNUNET_SERVER_notification_context_unicast (
754 (struct GNUNET_MessageHeader *) cbuf,
763 * Notify the client that owns the tunnel that a peer has connected to it
765 * @param t Tunnel whose owner to notify
766 * @param id Short id of the peer that has connected
769 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
771 struct GNUNET_MESH_PeerControl pc;
773 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
774 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
775 pc.tunnel_id = htonl (t->local_tid);
776 GNUNET_PEER_resolve (id, &pc.peer);
777 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
778 &pc.header, GNUNET_NO);
783 * Function called to notify a client about the socket
784 * being ready to queue more data. "buf" will be
785 * NULL and "size" zero if the socket was closed for
786 * writing in the meantime.
789 * @param size number of bytes available in buf
790 * @param buf where the callee should write the message
791 * @return number of bytes written to buf
794 send_core_create_path (void *cls, size_t size, void *buf);
797 * Cancel a core transmission that was already requested and free all resources
798 * associated to the request.
800 * @param peer PeeInfo of the peer whose transmission is cancelled.
801 * @param i Position of the transmission to be cancelled.
804 peer_info_cancel_transmission(struct MeshPeerInfo *peer, unsigned int i)
806 if (NULL != peer->core_transmit[i])
808 struct MeshDataDescriptor *dd;
809 struct MeshPathInfo *path_info;
811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
812 "MESH: Cancelling data transmission at %u\n",
814 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit[i]);
815 peer->core_transmit[i] = NULL;
816 /* TODO: notify that tranmission has failed */
817 switch (peer->types[i])
819 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
820 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
821 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type payload\n");
824 if (0 == --(*dd->copies))
826 GNUNET_free (dd->copies);
827 GNUNET_free (dd->data);
830 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type create path\n");
832 path_info = peer->infos[i];
833 path_destroy(path_info->path);
836 GNUNET_free (peer->infos[i]);
845 peer_info_transmit_slot (struct MeshPeerInfo *peer)
849 for (i = 0; peer->core_transmit[i]; i++)
851 if (i == (CORE_QUEUE_SIZE - 1))
853 /* All positions are taken! Overwriting! */
855 peer_info_cancel_transmission(peer, 0);
864 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
865 * and insert it in the appropiate structures if the peer is not known yet.
867 * @param peer Full identity of the peer.
869 * @return Existing or newly created peer info.
871 static struct MeshPeerInfo *
872 peer_info_get (const struct GNUNET_PeerIdentity *peer)
874 struct MeshPeerInfo *peer_info;
876 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
877 if (NULL == peer_info)
880 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
881 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
882 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
883 peer_info->id = GNUNET_PEER_intern (peer);
891 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
892 * and insert it in the appropiate structures if the peer is not known yet.
894 * @param peer Short identity of the peer.
896 * @return Existing or newly created peer info.
898 static struct MeshPeerInfo *
899 peer_info_get_short (const GNUNET_PEER_Id peer)
901 struct GNUNET_PeerIdentity id;
903 GNUNET_PEER_resolve(peer, &id);
904 return peer_info_get(&id);
909 * Iterator to remove the tunnel from the list of tunnels a peer participates
912 * @param cls Closure (tunnel info)
913 * @param key GNUNET_PeerIdentity of the peer (unused)
914 * @param value PeerInfo of the peer
916 * @return always GNUNET_YES, to keep iterating
919 peer_info_delete_tunnel (void* cls, const GNUNET_HashCode* key, void* value)
921 struct MeshTunnel *t = cls;
922 struct MeshPeerInfo *peer = value;
925 for (i = 0; i < peer->ntunnels; i++)
927 if (0 == memcmp(&peer->tunnels[i]->id,
929 sizeof(struct MESH_TunnelID)))
932 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
933 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
942 * Sends a CREATE PATH message for a path to a peer, properly registrating
943 * all used resources.
945 * @param peer PeerInfo of the final peer for whom this path is being created.
946 * @param p Path itself.
947 * @param t Tunnel for which the path is created.
950 send_create_path (struct MeshPeerInfo *peer,
951 struct MeshPeerPath *p,
952 struct MeshTunnel *t)
954 struct GNUNET_PeerIdentity id;
955 struct MeshPathInfo *path_info;
956 struct MeshPeerInfo *neighbor;
961 p = tree_get_path_to_peer(t->tree, peer->id);
968 for (i = 0; i < p->length; i++)
970 if (p->peers[i] == myid)
973 if (i >= p->length - 1)
979 GNUNET_PEER_resolve(p->peers[i + 1], &id);
981 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
983 path_info->peer = peer;
985 neighbor = peer_info_get(&id);
986 path_info->pos = peer_info_transmit_slot(neighbor);
987 neighbor->types[path_info->pos] = GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE;
988 neighbor->infos[path_info->pos] = path_info;
989 neighbor->core_transmit[path_info->pos] =
990 GNUNET_CORE_notify_transmit_ready (
991 core_handle, /* handle */
994 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
996 sizeof (struct GNUNET_MESH_ManipulatePath)
997 + (p->length * sizeof (struct GNUNET_PeerIdentity)), /*size */
998 &send_core_create_path, /* callback */
999 path_info); /* cls */
1004 * Try to establish a new connection to this peer.
1005 * Use the best path for the given tunnel.
1006 * If the peer doesn't have any path to it yet, try to get one.
1007 * If the peer already has some path, send a CREATE PATH towards it.
1009 * @param peer PeerInfo of the peer.
1010 * @param t Tunnel for which to create the path, if possible.
1013 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1015 struct MeshPeerPath *p;
1016 struct MeshPathInfo *path_info;
1018 if (NULL != peer->path_head)
1020 p = tree_get_path_to_peer(t->tree, peer->id);
1023 send_create_path(peer, p, t);
1028 send_client_peer_connected(t, myid);
1031 else if (NULL == peer->dhtget)
1033 struct GNUNET_PeerIdentity id;
1035 GNUNET_PEER_resolve(peer->id, &id);
1036 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
1037 path_info->peer = peer;
1040 GNUNET_DHT_get_start(dht_handle, /* handle */
1041 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1042 GNUNET_BLOCK_TYPE_TEST, /* type */
1043 &id.hashPubKey, /* key to search */
1044 4, /* replication level */
1045 GNUNET_DHT_RO_RECORD_ROUTE |
1046 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1048 0, /* xquery bits */
1049 &dht_get_id_handler,
1052 /* Otherwise, there is no path but the DHT get is already started. */
1058 * Destroy the peer_info and free any allocated resources linked to it
1059 * @param t tunnel the path belongs to
1060 * @param pi the peer_info to destroy
1061 * @return GNUNET_OK on success
1064 peer_info_destroy (struct MeshPeerInfo *pi)
1066 GNUNET_HashCode hash;
1067 struct GNUNET_PeerIdentity id;
1069 GNUNET_PEER_resolve (pi->id, &id);
1070 GNUNET_PEER_change_rc (pi->id, -1);
1071 GNUNET_CRYPTO_hash (&id, sizeof (struct GNUNET_PeerIdentity), &hash);
1073 GNUNET_CONTAINER_multihashmap_remove (peers, &hash, pi);
1074 GNUNET_SCHEDULER_cancel (pi->path_refresh_task);
1082 * Notify a tunnel that a connection has broken that affects at least
1083 * some of its peers.
1085 * @param t Tunnel affected.
1086 * @param peer Peer that (at least) has been affected by the disconnection.
1087 * @param p1 Peer that got disconnected from p2.
1088 * @param p2 Peer that got disconnected from p1.
1090 * @return Short ID of the peer disconnected (either p1 or p2).
1091 * 0 if the tunnel remained unaffected.
1093 static GNUNET_PEER_Id
1094 tunnel_notify_connection_broken (struct MeshTunnel *t,
1095 struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1099 * Remove all paths that rely on a direct connection between p1 and p2
1100 * from the peer itself and notify all tunnels about it.
1102 * @param peer PeerInfo of affected peer.
1103 * @param p1 GNUNET_PEER_Id of one peer.
1104 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1107 * TODO: optimize (see below)
1110 path_remove_from_peer (struct MeshPeerInfo *peer,
1114 struct GNUNET_PeerIdentity id;
1115 struct MeshPeerPath *p;
1116 struct MeshPeerPath *aux;
1117 struct MeshPeerInfo *peer_d;
1119 unsigned int destroyed;
1125 p = peer->path_head;
1129 for (i = 0; i < (p->length - 1); i++)
1131 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1132 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1144 for (i = 0; i < peer->ntunnels; i++)
1146 d = tunnel_notify_connection_broken (peer->tunnels[i], peer, p1, p2);
1148 * Problem: one or more peers have been deleted from the tunnel tree.
1149 * We don't know who they are to try to add them again.
1150 * We need to try to find a new path for each of the disconnected peers.
1151 * Some of them might already have a path to reach them that does not
1152 * involve p1 and p2. Adding all anew might render in a better tree than
1153 * the trivial immediate fix.
1155 * Trivial immiediate fix: try to reconnect to the disconnected node. All
1156 * its children will be reachable trough him.
1158 peer_d = peer_info_get_short(d);
1161 for (p = peer_d->path_head; NULL != p; p = p->next)
1163 if ((cost = path_get_cost(peer->tunnels[i]->tree, p)) < best)
1171 /* No callback, as peer will be already disconnected */
1172 tree_add_path(peer->tunnels[i]->tree, aux, NULL);
1176 struct MeshPathInfo *path_info;
1178 if (NULL != peer_d->dhtget)
1180 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
1181 path_info->path = p;
1182 path_info->peer = peer_d;
1183 path_info->t = peer->tunnels[i];
1185 GNUNET_DHT_get_start(dht_handle, /* handle */
1186 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1187 GNUNET_BLOCK_TYPE_TEST, /* type */
1188 &id.hashPubKey, /*key to search */
1189 4, /* replication level */
1190 GNUNET_DHT_RO_RECORD_ROUTE |
1191 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1193 0, /* xquery bits */
1194 &dht_get_id_handler,
1195 (void *) path_info);
1202 * Add the path to the peer and update the path used to reach it in case this
1205 * @param peer_info Destination peer to add the path to.
1206 * @param path New path to add. Last peer must be the peer in arg 1.
1207 * Path will be either used of freed if already known.
1209 * TODO: trim the part from origin to us? Add it as path to origin?
1212 path_add_to_peer (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
1214 struct MeshPeerPath *aux;
1218 if (NULL == peer_info || NULL == path)
1224 l = path_get_length (path);
1231 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1233 l2 = path_get_length (aux);
1236 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1237 peer_info->path_tail, aux, path);
1241 if (l2 == l && memcmp(path->peers, aux->peers, l) == 0)
1248 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1255 * Add the path to the origin peer and update the path used to reach it in case
1256 * this is the shortest.
1257 * The path is given in peer_info -> destination, therefore we turn the path
1258 * upside down first.
1260 * @param peer_info Peer to add the path to, being the origin of the path.
1261 * @param path New path to add after being inversed.
1264 path_add_to_origin (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
1267 path_add_to_peer (peer_info, path);
1272 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1273 * to obtain a local peer -> destination path and interning the peer ids.
1275 * @return Newly allocated and created path
1277 static struct MeshPeerPath *
1278 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1279 unsigned int get_path_length,
1280 const struct GNUNET_PeerIdentity *put_path,
1281 unsigned int put_path_length)
1283 struct MeshPeerPath *p;
1289 GNUNET_PEER_change_rc(myid, 1);
1290 i = get_path_length;
1291 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: GET has %d hops.\n", i);
1292 for (i--; i >= 0; i--)
1294 id = GNUNET_PEER_intern (&get_path[i]);
1295 if (p->length > 0 && id == p->peers[p->length - 1])
1297 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1298 GNUNET_PEER_change_rc(id, -1);
1302 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1303 "MESH: Adding from GET: %s.\n",
1304 GNUNET_i2s(&get_path[i]));
1306 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1307 p->peers[p->length - 1] = id;
1310 i = put_path_length;
1311 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: PUT has %d hops.\n", i);
1312 for (i--; i >= 0; i--)
1314 id = GNUNET_PEER_intern (&put_path[i]);
1317 /* PUT path went through us, so discard the path up until now and start
1318 * from here to get a much shorter (and loop-free) path.
1323 if (p->length > 0 && id == p->peers[p->length - 1])
1325 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1326 GNUNET_PEER_change_rc(id, -1);
1330 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1331 "MESH: Adding from PUT: %s.\n",
1332 GNUNET_i2s(&put_path[i]));
1334 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1335 p->peers[p->length - 1] = id;
1339 if (get_path_length > 0)
1340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1341 "MESH: (first of GET: %s)\n",
1342 GNUNET_h2s_full(&get_path[0].hashPubKey));
1343 if (put_path_length > 0)
1344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1345 "MESH: (first of PUT: %s)\n",
1346 GNUNET_h2s_full(&put_path[0].hashPubKey));
1347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1348 "MESH: In total: %d hops\n",
1350 for (i = 0; i < p->length; i++)
1352 struct GNUNET_PeerIdentity peer_id;
1354 GNUNET_PEER_resolve(p->peers[i], &peer_id);
1355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1358 GNUNET_h2s_full(&peer_id.hashPubKey));
1366 * Send keepalive packets for a peer
1368 * @param cls Closure (tunnel for which to send the keepalive).
1369 * @param tc Notification context.
1371 * TODO: implement explicit multicast keepalive?
1374 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1378 * Search for a tunnel among the tunnels for a client
1380 * @param c the client whose tunnels to search in
1381 * @param tid the local id of the tunnel
1383 * @return tunnel handler, NULL if doesn't exist
1385 static struct MeshTunnel *
1386 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1388 GNUNET_HashCode hash;
1390 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1391 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1393 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
1395 return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
1400 * Search for a tunnel by global ID using PEER_ID
1402 * @param pi owner of the tunnel
1403 * @param tid global tunnel number
1405 * @return tunnel handler, NULL if doesn't exist
1407 static struct MeshTunnel *
1408 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1410 struct MESH_TunnelID id;
1411 GNUNET_HashCode hash;
1416 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1417 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1422 * Search for a tunnel by global ID using full PeerIdentities
1424 * @param oid owner of the tunnel
1425 * @param tid global tunnel number
1427 * @return tunnel handler, NULL if doesn't exist
1429 static struct MeshTunnel *
1430 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1432 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1437 * Callback used to notify a client owner of a tunnel that a peer has
1438 * disconnected, most likely because of a path change.
1440 * @param n Node in the tree representing the disconnected peer
1443 notify_peer_disconnected (const struct MeshTunnelTreeNode *n)
1445 struct MeshPeerInfo *peer;
1447 if (NULL != n->t->client && NULL != nc)
1449 struct GNUNET_MESH_PeerControl msg;
1450 msg.header.size = htons (sizeof (msg));
1451 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1452 msg.tunnel_id = htonl (n->t->local_tid);
1453 GNUNET_PEER_resolve (n->peer, &msg.peer);
1454 GNUNET_SERVER_notification_context_unicast (nc, n->t->client->handle,
1455 &msg.header, GNUNET_NO);
1457 peer = peer_info_get_short(n->peer);
1458 peer_info_connect(peer, n->t);
1463 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1464 * needed rescources.
1465 * If peer already exists, reevaluate shortest path and change if different.
1467 * @param t Tunnel we want to add a new peer to
1468 * @param peer PeerInfo of the peer being added
1472 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1474 struct GNUNET_PeerIdentity id;
1475 struct MeshPeerPath *best_p;
1476 struct MeshPeerPath *p;
1477 unsigned int best_cost;
1480 GNUNET_PEER_resolve(peer->id, &id);
1482 GNUNET_CONTAINER_multihashmap_contains(t->peers, &id.hashPubKey))
1485 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
1486 GNUNET_CONTAINER_multihashmap_put(
1490 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1493 if (NULL != (p = peer->path_head))
1496 best_cost = path_get_cost(t->tree, p);
1499 if ((cost = path_get_cost (t->tree, p)) < best_cost)
1506 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected);
1507 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
1508 t->path_refresh_task =
1509 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
1513 /* Start a DHT get if necessary */
1514 peer_info_connect(peer, t);
1519 * Add a path to a tunnel which we don't own, just to remember the next hop.
1520 * If destination node was already in the tunnel, the first hop information
1521 * will be replaced with the new path.
1523 * @param t Tunnel we want to add a new peer to
1524 * @param p Path to add
1525 * @param own_pos Position of local node in path.
1529 tunnel_add_path (struct MeshTunnel *t,
1530 struct MeshPeerPath *p,
1531 unsigned int own_pos)
1533 struct GNUNET_PeerIdentity id;
1535 GNUNET_assert (0 != own_pos);
1536 tree_add_path(t->tree, p, NULL);
1537 if (NULL == t->tree->me)
1538 t->tree->me = tree_find_peer(t->tree->root, p->peers[own_pos]);
1539 if (own_pos < p->length - 1)
1541 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
1542 tree_update_first_hops(t->tree, t->tree->me, &id);
1548 * Notify a tunnel that a connection has broken that affects at least
1549 * some of its peers.
1551 * @param t Tunnel affected.
1552 * @param peer Peer that (at least) has been affected by the disconnection.
1553 * @param p1 Peer that got disconnected from p2.
1554 * @param p2 Peer that got disconnected from p1.
1556 * @return Short ID of the peer disconnected (either p1 or p2).
1557 * 0 if the tunnel remained unaffected.
1559 static GNUNET_PEER_Id
1560 tunnel_notify_connection_broken (struct MeshTunnel *t,
1561 struct MeshPeerInfo *peer,
1565 return tree_notify_connection_broken (t->tree, p1, p2,
1566 ¬ify_peer_disconnected);
1571 * Destroy the tunnel and free any allocated resources linked to it
1573 * @param t the tunnel to destroy
1575 * @return GNUNET_OK on success
1578 tunnel_destroy (struct MeshTunnel *t)
1580 struct MeshClient *c;
1581 struct MeshQueue *q;
1582 struct MeshQueue *qn;
1583 GNUNET_HashCode hash;
1592 struct GNUNET_PeerIdentity id;
1594 GNUNET_PEER_resolve(t->id.oid, &id);
1595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1596 "MESH: destroying tunnel %s [%x]\n",
1600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
1604 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1605 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
1610 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1611 if (NULL != c && GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
1615 if (t->local_tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1617 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1618 GNUNET_break (GNUNET_YES ==
1619 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t));
1622 GNUNET_CONTAINER_multihashmap_iterate(t->peers, &peer_info_delete_tunnel, t);
1623 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
1627 if (NULL != q->data)
1628 GNUNET_free (q->data);
1632 /* TODO cancel core transmit ready in case it was active */
1634 tree_destroy(t->tree);
1635 if (NULL != t->dht_get_type)
1636 GNUNET_DHT_get_stop(t->dht_get_type);
1643 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
1644 * client when the client disconnects.
1646 * @param cls closure (client that is disconnecting)
1647 * @param key the hash of the local tunnel id (used to access the hashmap)
1648 * @param value the value stored at the key (tunnel to destroy)
1650 * @return GNUNET_OK on success
1653 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
1655 struct MeshTunnel *t = value;
1658 if (NULL != t->dht_get_type)
1660 GNUNET_DHT_get_stop (t->dht_get_type);
1662 r = tunnel_destroy (t);
1667 /******************************************************************************/
1668 /**************** MESH NETWORK HANDLER HELPERS ***********************/
1669 /******************************************************************************/
1672 * Function called to notify a client about the socket
1673 * being ready to queue more data. "buf" will be
1674 * NULL and "size" zero if the socket was closed for
1675 * writing in the meantime.
1677 * @param cls closure
1678 * @param size number of bytes available in buf
1679 * @param buf where the callee should write the message
1680 * @return number of bytes written to buf
1683 send_core_create_path (void *cls, size_t size, void *buf)
1685 struct MeshPathInfo *info = cls;
1686 struct GNUNET_MESH_ManipulatePath *msg;
1687 struct GNUNET_PeerIdentity *peer_ptr;
1688 struct MeshPeerInfo *peer = info->peer;
1689 struct MeshTunnel *t = info->t;
1690 struct MeshPeerPath *p = info->path;
1695 sizeof (struct GNUNET_MESH_ManipulatePath) +
1696 p->length * sizeof (struct GNUNET_PeerIdentity);
1698 if (size < size_needed || NULL == buf)
1700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: create path retransmit!\n");
1701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: buf: %p\n", buf);
1702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: size: (%u/%u)\n",
1704 info->peer->core_transmit[info->pos] =
1705 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1706 GNUNET_TIME_UNIT_FOREVER_REL,
1707 path_get_first_hop (t->tree, peer->id),
1709 &send_core_create_path,
1713 info->peer->core_transmit[info->pos] = NULL;
1715 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
1716 msg->header.size = htons (size_needed);
1717 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
1718 msg->tid = ntohl (t->id.tid);
1720 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1721 for (i = 0; i < p->length; i++)
1723 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1730 "MESH: CREATE PATH (%u bytes long) sent!\n",
1738 * Function called to notify a client about the socket
1739 * being ready to queue more data. "buf" will be
1740 * NULL and "size" zero if the socket was closed for
1741 * writing in the meantime.
1743 * @param cls closure (MeshDataDescriptor with all info to build packet)
1744 * @param size number of bytes available in buf
1745 * @param buf where the callee should write the message
1746 * @return number of bytes written to buf
1749 send_core_data_to_origin (void *cls, size_t size, void *buf)
1751 struct MeshDataDescriptor *info = cls;
1752 struct GNUNET_MESH_ToOrigin *msg = buf;
1755 GNUNET_assert (NULL != info);
1756 total_size = sizeof (struct GNUNET_MESH_ToOrigin) + info->size;
1757 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
1759 if (total_size > size)
1761 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1762 "not enough buffer to send data to origin\n");
1765 msg->header.size = htons (total_size);
1766 msg->header.type = htons (GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN);
1767 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1768 msg->tid = htonl (info->origin->tid);
1769 if (0 != info->size)
1771 memcpy (&msg[1], &info[1], info->size);
1773 if (NULL != info->client)
1775 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1784 * Function called to notify a client about the socket
1785 * being ready to queue more data. "buf" will be
1786 * NULL and "size" zero if the socket was closed for
1787 * writing in the meantime.
1789 * @param cls closure (data itself)
1790 * @param size number of bytes available in buf
1791 * @param buf where the callee should write the message
1793 * @return number of bytes written to buf
1796 send_core_data_multicast (void *cls, size_t size, void *buf)
1798 struct MeshDataDescriptor *info = cls;
1799 struct GNUNET_MESH_Multicast *msg = buf;
1802 GNUNET_assert (NULL != info);
1803 GNUNET_assert (NULL != info->peer);
1804 total_size = info->size + sizeof (struct GNUNET_MESH_Multicast);
1805 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1807 if (total_size > size)
1810 struct GNUNET_PeerIdentity id;
1812 GNUNET_PEER_resolve(info->peer->id, &id);
1813 info->peer->infos[info->handler_n] = info;
1814 info->peer->types[info->handler_n] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
1815 info->peer->core_transmit[info->handler_n] =
1816 GNUNET_CORE_notify_transmit_ready (core_handle,
1819 GNUNET_TIME_UNIT_FOREVER_REL,
1822 &send_core_data_multicast,
1826 info->peer->core_transmit[info->handler_n] = NULL;
1827 info->peer->infos[info->handler_n] = NULL;
1828 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
1829 msg->header.size = htons (total_size);
1830 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1831 msg->tid = htonl (info->origin->tid);
1832 memcpy (&msg[1], info->data, info->size);
1833 if (0 == --(*info->copies))
1835 if (NULL != info->client)
1837 /* FIXME One unresponsive neighbor (who doesn't "call" tmt_rdy) can lock
1838 * the client from sending anything else to the service.
1839 * - Call receive_done after certain timeout.
1840 * - Here cancel the timeout.
1842 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1844 GNUNET_free (info->data);
1845 GNUNET_free (info->copies);
1853 * Function called to notify a client about the socket
1854 * being ready to queue more data. "buf" will be
1855 * NULL and "size" zero if the socket was closed for
1856 * writing in the meantime.
1858 * @param cls closure (MeshDataDescriptor)
1859 * @param size number of bytes available in buf
1860 * @param buf where the callee should write the message
1861 * @return number of bytes written to buf
1864 send_core_path_ack (void *cls, size_t size, void *buf)
1866 struct MeshDataDescriptor *info = cls;
1867 struct GNUNET_MESH_PathACK *msg = buf;
1869 GNUNET_assert (NULL != info);
1872 info->peer->core_transmit[info->handler_n] = NULL;
1874 if (sizeof (struct GNUNET_MESH_PathACK) > size)
1879 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
1880 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
1881 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1882 msg->tid = htonl (info->origin->tid);
1883 msg->peer_id = my_full_id;
1885 /* TODO add signature */
1887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: PATH ACK sent!\n");
1888 return sizeof (struct GNUNET_MESH_PathACK);
1893 * Function called to notify a client about the socket
1894 * being ready to queue more data. "buf" will be
1895 * NULL and "size" zero if the socket was closed for
1896 * writing in the meantime.
1898 * @param cls closure (data itself)
1899 * @param size number of bytes available in buf
1900 * @param buf where the callee should write the message
1901 * @return number of bytes written to buf
1904 send_core_data_raw (void *cls, size_t size, void *buf)
1906 struct GNUNET_MessageHeader *msg = cls;
1909 GNUNET_assert (NULL != msg);
1910 total_size = ntohs (msg->size);
1912 if (total_size > size)
1917 memcpy (buf, msg, total_size);
1925 * Send another peer a notification to destroy a tunnel
1926 * @param cls The tunnel to destroy
1927 * @param size Size in the buffer
1928 * @param buf Memory where to put the data to transmit
1929 * @return Size of data put in buffer
1932 send_p2p_tunnel_destroy (void *cls, size_t size, void *buf)
1934 struct MeshTunnel *t = cls;
1935 struct MeshClient *c;
1936 struct GNUNET_MESH_TunnelMessage *msg;
1940 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1941 /*FIXME*/ msg->header.size =
1942 htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1943 msg->tunnel_id = htonl (t->id.tid);
1945 tunnel_destroy (c, t);
1946 return sizeof (struct GNUNET_MESH_TunnelMessage);
1951 /******************************************************************************/
1952 /******************** MESH NETWORK HANDLERS **************************/
1953 /******************************************************************************/
1957 * Core handler for path creation
1958 * struct GNUNET_CORE_MessageHandler
1960 * @param cls closure
1961 * @param message message
1962 * @param peer peer identity this notification is about
1963 * @param atsi performance data
1964 * @return GNUNET_OK to keep the connection open,
1965 * GNUNET_SYSERR to close it (signal serious error)
1969 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1970 const struct GNUNET_MessageHeader *message,
1971 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1973 unsigned int own_pos;
1976 MESH_TunnelNumber tid;
1977 struct GNUNET_MESH_ManipulatePath *msg;
1978 struct GNUNET_PeerIdentity *pi;
1979 GNUNET_HashCode hash;
1980 struct MeshPeerPath *path;
1981 struct MeshPeerInfo *dest_peer_info;
1982 struct MeshPeerInfo *orig_peer_info;
1983 struct MeshTunnel *t;
1985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1986 "MESH: Received a path create msg [%s]\n",
1987 GNUNET_i2s(&my_full_id));
1988 size = ntohs (message->size);
1989 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
1991 GNUNET_break_op (0);
1995 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
1996 if (size % sizeof (struct GNUNET_PeerIdentity))
1998 GNUNET_break_op (0);
2001 size /= sizeof (struct GNUNET_PeerIdentity);
2004 GNUNET_break_op (0);
2007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2008 "MESH: path has %u hops.\n",
2010 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2012 tid = ntohl (msg->tid);
2013 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2014 t = tunnel_get (pi, tid);
2015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2016 "MESH: path is for tunnel %s [%X].\n",
2021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
2022 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2023 t->id.oid = GNUNET_PEER_intern (pi);
2025 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
2026 t->local_tid = next_local_tid++;
2027 /* FIXME test if taken */
2028 next_local_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2029 t->tree = tree_new(t, t->id.oid);
2031 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2033 GNUNET_CONTAINER_multihashmap_put (
2037 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2043 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2045 GNUNET_CONTAINER_multihashmap_put (
2049 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2057 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
2058 if (NULL == dest_peer_info)
2060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2061 "MESH: Creating PeerInfo for destination.\n");
2062 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2063 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
2064 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
2066 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2068 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
2069 if (NULL == orig_peer_info)
2071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2072 "MESH: Creating PeerInfo for origin.\n");
2073 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2074 orig_peer_info->id = GNUNET_PEER_intern (pi);
2075 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
2076 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating path...\n");
2079 path = path_new (size);
2081 for (i = 0; i < size; i++)
2083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2084 "MESH: ... adding %s\n",
2085 GNUNET_i2s(&pi[i]));
2086 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2087 if (path->peers[i] == myid)
2090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2091 "MESH: Own position: %u\n", own_pos);
2094 /* cannot be self, must be 'not found' */
2095 /* create path: self not found in path through self */
2096 GNUNET_break_op (0);
2097 path_destroy (path);
2098 /* FIXME error. destroy tunnel? leave for timeout? */
2101 tunnel_add_path (t, path, own_pos);
2102 t->tree->me = tree_find_peer(t->tree->root, myid);
2103 if (own_pos == size - 1)
2105 /* It is for us! Send ack. */
2106 struct GNUNET_MESH_TunnelNotification cmsg;
2107 struct MeshDataDescriptor *info;
2110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2111 "MESH: It's for us!\n");
2112 path_add_to_origin (orig_peer_info, path);
2113 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
2114 info->origin = &t->id;
2115 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2116 GNUNET_assert (NULL != info->peer);
2117 j = peer_info_transmit_slot(info->peer);
2118 info->handler_n = j;
2119 info->peer->types[j] = GNUNET_MESSAGE_TYPE_MESH_PATH_ACK;
2120 info->peer->infos[j] = info;
2121 info->peer->core_transmit[j] =
2122 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
2123 GNUNET_TIME_UNIT_FOREVER_REL, peer,
2124 sizeof (struct GNUNET_MESH_PathACK),
2125 &send_core_path_ack, info);
2126 cmsg.header.size = htons(sizeof(cmsg));
2127 cmsg.header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2128 GNUNET_PEER_resolve(t->id.oid, &cmsg.peer);
2129 cmsg.tunnel_id = htonl(t->local_tid);
2130 GNUNET_SERVER_notification_context_broadcast(nc, &cmsg.header, GNUNET_NO);
2134 struct MeshPeerPath *path2;
2136 /* It's for somebody else! Retransmit. */
2137 path2 = path_duplicate(path);
2138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2139 "MESH: Retransmitting.\n");
2140 path_add_to_peer(dest_peer_info, path);
2141 path = path_duplicate(path2);
2142 path_add_to_origin(orig_peer_info, path2);
2143 send_create_path(dest_peer_info, path, t);
2150 * Core handler for mesh network traffic going from the origin to a peer
2152 * @param cls closure
2153 * @param peer peer identity this notification is about
2154 * @param message message
2155 * @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_unicast (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_Unicast *msg;
2165 struct MeshTunnel *t;
2169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2170 "MESH: got a unicast packet from %s\n",
2172 size = ntohs (message->size);
2174 sizeof (struct GNUNET_MESH_Unicast) +
2175 sizeof (struct GNUNET_MessageHeader))
2180 msg = (struct GNUNET_MESH_Unicast *) message;
2181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2182 "MESH: of type %u\n",
2183 ntohs (msg[1].header.type));
2184 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2187 /* TODO notify back: we don't know this tunnel */
2188 GNUNET_break_op (0);
2191 pid = GNUNET_PEER_search(&msg->destination);
2194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2195 "MESH: it's for us! sending to clients...\n");
2196 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
2199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2200 "MESH: not for us, retransmitting...\n");
2201 msg = GNUNET_malloc (size);
2202 memcpy (msg, message, size);
2203 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2204 GNUNET_TIME_UNIT_FOREVER_REL,
2205 path_get_first_hop (t->tree, pid),
2207 &send_core_data_raw, msg);
2213 * Core handler for mesh network traffic going from the origin to all peers
2215 * @param cls closure
2216 * @param message message
2217 * @param peer peer identity this notification is about
2218 * @param atsi performance data
2219 * @return GNUNET_OK to keep the connection open,
2220 * GNUNET_SYSERR to close it (signal serious error)
2222 * TODO: Check who we got this from, to validate route.
2225 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2226 const struct GNUNET_MessageHeader *message,
2227 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2229 struct GNUNET_MESH_Multicast *msg;
2230 struct GNUNET_PeerIdentity *id;
2231 struct MeshDataDescriptor *info;
2232 struct MeshTunnelTreeNode *n;
2233 struct MeshTunnel *t;
2234 unsigned int *copies;
2239 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_Multicast);
2240 if (size < sizeof (struct GNUNET_MessageHeader))
2242 GNUNET_break_op (0);
2245 msg = (struct GNUNET_MESH_Multicast *) message;
2246 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2250 /* TODO notify that we dont know that tunnel */
2251 GNUNET_break_op (0);
2255 /* Transmit to locally interested clients */
2256 if (GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
2258 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
2260 n = t->tree->me->children_head;
2263 copies = GNUNET_malloc (sizeof (unsigned int));
2264 for (*copies = 0; NULL != n; n = n->next)
2266 n = t->tree->me->children_head;
2267 data = GNUNET_malloc (size);
2268 memcpy (data, &msg[1], size);
2271 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
2272 info->origin = &t->id;
2275 info->copies = copies;
2276 info->client = t->client->handle;
2277 info->destination = n->peer;
2278 id = path_get_first_hop(t->tree, n->peer);
2279 info->peer = peer_info_get(id);
2280 GNUNET_assert (NULL != info->peer);
2281 for (i = 0; NULL != info->peer->core_transmit[i]; i++)
2283 if (i == (CORE_QUEUE_SIZE - 1))
2290 info->handler_n = i;
2291 info->peer->infos[i] = info;
2292 info->peer->types[i] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
2293 info->peer->core_transmit[i] =
2294 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2295 GNUNET_TIME_UNIT_FOREVER_REL, id,
2296 ntohs (msg->header.size),
2297 &send_core_data_multicast, info);
2305 * Core handler for mesh network traffic toward the owner of a tunnel
2307 * @param cls closure
2308 * @param message message
2309 * @param peer peer identity this notification is about
2310 * @param atsi performance data
2312 * @return GNUNET_OK to keep the connection open,
2313 * GNUNET_SYSERR to close it (signal serious error)
2316 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
2317 const struct GNUNET_MessageHeader *message,
2318 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2320 struct GNUNET_MESH_ToOrigin *msg;
2321 struct GNUNET_PeerIdentity id;
2322 struct MeshPeerInfo *peer_info;
2323 struct MeshTunnel *t;
2326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2327 "MESH: got a ToOrigin packet from %s\n",
2329 size = ntohs (message->size);
2330 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
2331 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
2333 GNUNET_break_op (0);
2336 msg = (struct GNUNET_MESH_ToOrigin *) message;
2337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2338 "MESH: of type %u\n",
2339 ntohs (msg[1].header.type));
2340 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2344 /* TODO notify that we dont know this tunnel (whom)? */
2345 GNUNET_break_op (0);
2349 if (t->id.oid == myid)
2352 struct GNUNET_MESH_ToOrigin *copy;
2354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2355 "MESH: it's for us! sending to clients...\n");
2356 if (NULL == t->client)
2358 /* got data packet for ownerless tunnel */
2359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2360 "MESH: no clients!\n");
2361 GNUNET_break_op (0);
2364 /* TODO signature verification */
2365 memcpy (cbuf, message, size);
2366 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
2367 copy->tid = htonl (t->local_tid);
2368 GNUNET_SERVER_notification_context_unicast (nc,
2374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2375 "MESH: not for us, retransmitting...\n");
2377 peer_info = peer_info_get (&msg->oid);
2378 if (NULL == peer_info)
2380 /* unknown origin of tunnel */
2384 GNUNET_PEER_resolve (t->tree->me->parent->peer, &id);
2385 msg = GNUNET_malloc (size);
2386 memcpy (msg, message, size);
2387 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2388 GNUNET_TIME_UNIT_FOREVER_REL, &id, size,
2389 &send_core_data_raw, msg);
2396 * Core handler for path ACKs
2398 * @param cls closure
2399 * @param message message
2400 * @param peer peer identity this notification is about
2401 * @param atsi performance data
2403 * @return GNUNET_OK to keep the connection open,
2404 * GNUNET_SYSERR to close it (signal serious error)
2407 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2408 const struct GNUNET_MessageHeader *message,
2409 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2411 struct GNUNET_MESH_PathACK *msg;
2412 struct GNUNET_PeerIdentity id;
2413 struct MeshTunnelTreeNode *n;
2414 struct MeshPeerInfo *peer_info;
2415 struct MeshTunnel *t;
2417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2418 "MESH: Received a path ACK msg [%s]\n",
2419 GNUNET_i2s(&my_full_id));
2420 msg = (struct GNUNET_MESH_PathACK *) message;
2421 t = tunnel_get (&msg->oid, msg->tid);
2424 /* TODO notify that we don't know the tunnel */
2428 /* Message for us? */
2429 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
2431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2432 "MESH: It's for us!\n");
2433 if (NULL == t->client)
2435 GNUNET_break_op (0);
2438 peer_info = peer_info_get (&msg->peer_id);
2439 if (NULL == peer_info)
2441 GNUNET_break_op (0);
2444 n = tree_find_peer(t->tree->root, peer_info->id);
2447 GNUNET_break_op (0);
2450 n->status = MESH_PEER_READY;
2451 send_client_peer_connected(t, peer_info->id);
2455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2456 "MESH: not for us, retransmitting...\n");
2457 GNUNET_PEER_resolve(t->tree->me->parent->peer, &id);
2458 peer_info = peer_info_get (&msg->oid);
2459 if (NULL == peer_info)
2461 /* If we know the tunnel, we should DEFINITELY know the peer */
2465 msg = GNUNET_malloc (sizeof (struct GNUNET_MESH_PathACK));
2466 memcpy (msg, message, sizeof (struct GNUNET_MESH_PathACK));
2467 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2468 GNUNET_TIME_UNIT_FOREVER_REL,
2470 sizeof (struct GNUNET_MESH_PathACK),
2471 &send_core_data_raw, msg);
2477 * Functions to handle messages from core
2479 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
2480 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
2481 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
2482 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
2483 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
2484 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2485 sizeof (struct GNUNET_MESH_PathACK)},
2491 /******************************************************************************/
2492 /**************** MESH LOCAL HANDLER HELPERS ***********************/
2493 /******************************************************************************/
2496 * deregister_app: iterator for removing each application registered by a client
2498 * @param cls closure
2499 * @param key the hash of the application id (used to access the hashmap)
2500 * @param value the value stored at the key (client)
2502 * @return GNUNET_OK on success
2505 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
2507 GNUNET_break (GNUNET_YES ==
2508 GNUNET_CONTAINER_multihashmap_remove (applications, key, value));
2514 * notify_client_connection_failure: notify a client that the connection to the
2515 * requested remote peer is not possible (for instance, no route found)
2516 * Function called when the socket is ready to queue more data. "buf" will be
2517 * NULL and "size" zero if the socket was closed for writing in the meantime.
2519 * @param cls closure
2520 * @param size number of bytes available in buf
2521 * @param buf where the callee should write the message
2522 * @return number of bytes written to buf
2525 notify_client_connection_failure (void *cls, size_t size, void *buf)
2528 struct MeshPeerInfo *peer_info;
2529 struct GNUNET_MESH_PeerControl *msg;
2530 struct GNUNET_PeerIdentity id;
2532 if (0 == size && NULL == buf)
2534 // TODO retry? cancel?
2538 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
2539 peer_info = (struct MeshPeerInfo *) cls;
2540 msg = (struct GNUNET_MESH_PeerControl *) buf;
2541 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2542 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
2543 // msg->tunnel_id = htonl(peer_info->t->tid);
2544 GNUNET_PEER_resolve (peer_info->id, &id);
2545 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
2553 * Send keepalive packets for a peer
2555 * @param cls Closure (tunnel for which to send the keepalive).
2556 * @param tc Notification context.
2558 * TODO: implement explicit multicast keepalive?
2561 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2563 struct MeshTunnel *t = cls;
2564 struct GNUNET_MessageHeader *payload;
2565 struct GNUNET_MESH_Multicast *msg;
2568 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
2569 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2575 "MESH: sending keepalive for tunnel %d\n",
2578 size = sizeof(struct GNUNET_MESH_Multicast) +
2579 sizeof(struct GNUNET_MessageHeader);
2580 msg = GNUNET_malloc (size);
2581 msg->header.size = htons (size);
2582 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
2583 msg->oid = my_full_id;
2584 msg->tid = htonl(t->id.tid);
2585 payload = (struct GNUNET_MessageHeader *) &msg[1];
2586 payload->size = htons (sizeof(struct GNUNET_MessageHeader));
2587 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2588 handle_mesh_data_multicast (NULL, &my_full_id, &msg->header, NULL);
2591 t->path_refresh_task =
2592 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
2598 * Function to process paths received for a new peer addition. The recorded
2599 * paths form the initial tunnel, which can be optimized later.
2600 * Called on each result obtained for the DHT search.
2602 * @param cls closure
2603 * @param exp when will this value expire
2604 * @param key key of the result
2605 * @param type type of the result
2606 * @param size number of bytes in data
2607 * @param data pointer to the result data
2609 * TODO: re-issue the request after certain time? cancel after X results?
2612 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2613 const GNUNET_HashCode * key,
2614 const struct GNUNET_PeerIdentity *get_path,
2615 unsigned int get_path_length,
2616 const struct GNUNET_PeerIdentity *put_path,
2617 unsigned int put_path_length,
2618 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
2620 struct MeshPathInfo *path_info = cls;
2621 struct MeshPeerPath *p;
2622 struct GNUNET_PeerIdentity pi;
2625 GNUNET_PEER_resolve (path_info->peer->id, &pi);
2626 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2627 "MESH: Got results from DHT for %s\n",
2628 GNUNET_h2s_full(&pi.hashPubKey));
2629 GNUNET_DHT_get_stop(path_info->peer->dhtget);
2630 path_info->peer->dhtget = NULL;
2632 p = path_build_from_dht (get_path, get_path_length,
2633 put_path, put_path_length);
2634 path_add_to_peer (path_info->peer, p);
2635 for (i = 0; i < path_info->peer->ntunnels; i++)
2637 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
2638 peer_info_connect(path_info->peer, path_info->t);
2640 GNUNET_free (path_info);
2647 * Function to process paths received for a new peer addition. The recorded
2648 * paths form the initial tunnel, which can be optimized later.
2649 * Called on each result obtained for the DHT search.
2651 * @param cls closure
2652 * @param exp when will this value expire
2653 * @param key key of the result
2654 * @param type type of the result
2655 * @param size number of bytes in data
2656 * @param data pointer to the result data
2659 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2660 const GNUNET_HashCode * key,
2661 const struct GNUNET_PeerIdentity *get_path,
2662 unsigned int get_path_length,
2663 const struct GNUNET_PeerIdentity *put_path,
2664 unsigned int put_path_length,
2665 enum GNUNET_BLOCK_Type type, size_t size,
2668 const struct GNUNET_PeerIdentity *pi = data;
2669 struct GNUNET_PeerIdentity id;
2670 struct MeshTunnel *t = cls;
2671 struct MeshPeerInfo *peer_info;
2672 struct MeshPathInfo *path_info;
2673 struct MeshPeerPath *p;
2676 if (size != sizeof (struct GNUNET_PeerIdentity))
2678 GNUNET_break_op (0);
2681 GNUNET_assert (NULL != t->client);
2682 GNUNET_DHT_get_stop (t->dht_get_type);
2683 t->dht_get_type = NULL;
2684 peer_info = peer_info_get (pi);
2685 GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey, peer_info,
2686 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2688 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path_head &&
2689 NULL == peer_info->dhtget)
2691 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2692 path_info->peer = peer_info;
2694 /* we don't have a route to the peer, let's try a direct lookup */
2696 GNUNET_DHT_get_start (dht_handle, /* handle */
2697 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
2698 GNUNET_BLOCK_TYPE_TEST, /* block type */
2699 &pi->hashPubKey, /* key to look up */
2700 10U, /* replication level */
2701 GNUNET_DHT_RO_RECORD_ROUTE |
2702 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2703 /* option to dht: record route */
2705 0, /* xquery bits */
2706 dht_get_id_handler, /* callback */
2707 path_info); /* closure */
2711 p = path_build_from_dht (get_path, get_path_length, put_path, put_path_length);
2712 path_add_to_peer (peer_info, p);
2713 tunnel_add_peer(t, peer_info);
2714 p = tree_get_path_to_peer(t->tree, peer_info->id);
2716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2717 "MESH: new route for tunnel 0x%x found, has %u hops\n",
2718 t->local_tid, p->length);
2719 for (i = 0; i < p->length; i++)
2721 GNUNET_PEER_resolve (p->peers[0], &id);
2722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:\t%d\t%s\n", i,
2723 GNUNET_h2s_full (&id.hashPubKey));
2729 send_create_path(peer_info, p, t);
2733 send_client_peer_connected(t, myid);
2737 /******************************************************************************/
2738 /********************* MESH LOCAL HANDLES **************************/
2739 /******************************************************************************/
2743 * Handler for client disconnection
2745 * @param cls closure
2746 * @param client identification of the client; NULL
2747 * for the last call when the server is destroyed
2750 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
2752 struct MeshClient *c;
2753 struct MeshClient *next;
2755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
2757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
2761 if (c->handle != client && NULL != client)
2763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
2767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
2768 if (NULL != c->tunnels)
2770 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
2771 &tunnel_destroy_iterator,
2773 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
2776 /* deregister clients applications */
2777 if (NULL != c->apps)
2779 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
2780 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
2782 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
2783 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
2785 GNUNET_SCHEDULER_cancel (announce_applications_task);
2786 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
2788 if (NULL != c->types)
2789 GNUNET_CONTAINER_multihashmap_destroy (c->types);
2790 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
2796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
2802 * Handler for new clients
2804 * @param cls closure
2805 * @param client identification of the client
2806 * @param message the actual message, which includes messages the client wants
2809 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
2810 const struct GNUNET_MessageHeader *message)
2812 struct GNUNET_MESH_ClientConnect *cc_msg;
2813 struct MeshClient *c;
2814 GNUNET_MESH_ApplicationType *a;
2821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
2822 /* Check data sanity */
2823 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
2824 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
2825 ntypes = ntohs (cc_msg->types);
2826 napps = ntohs (cc_msg->applications);
2828 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
2831 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2835 /* Create new client structure */
2836 c = GNUNET_malloc (sizeof (struct MeshClient));
2838 c->id = next_client_id++;
2841 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
2844 GNUNET_MESH_ApplicationType at;
2847 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
2848 for (i = 0; i < napps; i++)
2851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
2852 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
2853 /* store in clients hashmap */
2854 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
2855 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2856 /* store in global hashmap, for announcements */
2857 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
2858 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2860 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
2861 announce_applications_task =
2862 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
2870 t = (uint16_t *) & a[napps];
2871 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
2872 for (i = 0; i < ntypes; i++)
2875 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
2877 /* store in clients hashmap */
2878 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
2879 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2880 /* store in global hashmap */
2881 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
2882 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2886 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
2888 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
2889 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
2890 GNUNET_SERVER_notification_context_add (nc, client);
2892 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
2900 * Handler for requests of new tunnels
2902 * @param cls closure
2903 * @param client identification of the client
2904 * @param message the actual message
2907 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
2908 const struct GNUNET_MessageHeader *message)
2910 struct GNUNET_MESH_TunnelMessage *t_msg;
2911 struct MeshTunnel *t;
2912 struct MeshClient *c;
2913 GNUNET_HashCode hash;
2915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
2917 /* Sanity check for client registration */
2918 if (NULL == (c = client_get (client)))
2921 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2928 /* Message sanity check */
2929 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2932 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2936 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2937 /* Sanity check for tunnel numbering */
2938 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
2941 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2944 /* Sanity check for duplicate tunnel IDs */
2945 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
2948 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2952 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATED TUNNEL at %p\n", t);
2954 while (NULL != tunnel_get_by_pi (myid, next_tid))
2955 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
2956 t->id.tid = next_tid++;
2958 t->local_tid = ntohl (t_msg->tunnel_id);
2960 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
2962 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2964 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
2965 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2968 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2972 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2974 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2975 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2978 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2981 t->tree = tree_new (t, myid);
2982 t->tree->refresh = REFRESH_PATH_TIME;
2983 t->tree->root->status = MESH_PEER_READY;
2984 t->tree->me = t->tree->root;
2986 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2992 * Handler for requests of deleting tunnels
2994 * @param cls closure
2995 * @param client identification of the client
2996 * @param message the actual message
2999 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
3000 const struct GNUNET_MessageHeader *message)
3002 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
3003 struct MeshClient *c;
3004 struct MeshTunnel *t;
3005 MESH_TunnelNumber tid;
3006 GNUNET_HashCode hash;
3008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel\n");
3010 /* Sanity check for client registration */
3011 if (NULL == (c = client_get (client)))
3014 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3017 /* Message sanity check */
3018 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3021 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3027 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3029 /* Retrieve tunnel */
3030 tid = ntohl (tunnel_msg->tunnel_id);
3032 /* Remove from local id hashmap */
3033 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3034 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
3035 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t);
3037 // notify_tunnel_destroy(t); FIXME
3039 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3045 * Handler for connection requests to new peers
3047 * @param cls closure
3048 * @param client identification of the client
3049 * @param message the actual message (PeerControl)
3052 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
3053 const struct GNUNET_MessageHeader *message)
3055 struct GNUNET_MESH_PeerControl *peer_msg;
3056 struct MeshPeerInfo *peer_info;
3057 struct MeshClient *c;
3058 struct MeshTunnel *t;
3059 MESH_TunnelNumber tid;
3061 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Got connection request\n");
3062 /* Sanity check for client registration */
3063 if (NULL == (c = client_get (client)))
3066 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3070 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3071 /* Sanity check for message size */
3072 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3075 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3079 /* Tunnel exists? */
3080 tid = ntohl (peer_msg->tunnel_id);
3081 t = tunnel_get_by_local_id (c, tid);
3085 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3089 /* Does client own tunnel? */
3090 if (t->client->handle != client)
3093 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3096 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: for %s\n",
3097 GNUNET_h2s_full(&peer_msg->peer.hashPubKey));
3098 peer_info = peer_info_get (&peer_msg->peer);
3100 tunnel_add_peer(t, peer_info);
3101 peer_info_connect(peer_info, t);
3103 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3109 * Handler for disconnection requests of peers in a tunnel
3111 * @param cls closure
3112 * @param client identification of the client
3113 * @param message the actual message (PeerControl)
3116 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
3117 const struct GNUNET_MessageHeader *message)
3119 struct GNUNET_MESH_PeerControl *peer_msg;
3120 struct MeshClient *c;
3121 struct MeshTunnel *t;
3122 MESH_TunnelNumber tid;
3124 /* Sanity check for client registration */
3125 if (NULL == (c = client_get (client)))
3128 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3131 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3132 /* Sanity check for message size */
3133 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3136 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3140 /* Tunnel exists? */
3141 tid = ntohl (peer_msg->tunnel_id);
3142 t = tunnel_get_by_local_id (c, tid);
3146 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3150 /* Does client own tunnel? */
3151 if (t->client->handle != client)
3154 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3158 /* Ok, delete peer from tunnel */
3159 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
3160 &peer_msg->peer.hashPubKey);
3162 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3168 * Handler for connection requests to new peers by type
3170 * @param cls closure
3171 * @param client identification of the client
3172 * @param message the actual message (ConnectPeerByType)
3175 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
3176 const struct GNUNET_MessageHeader *message)
3178 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
3179 struct MeshClient *c;
3180 struct MeshTunnel *t;
3181 GNUNET_HashCode hash;
3182 MESH_TunnelNumber tid;
3184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
3185 /* Sanity check for client registration */
3186 if (NULL == (c = client_get (client)))
3189 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3193 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
3194 /* Sanity check for message size */
3195 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
3196 ntohs (connect_msg->header.size))
3199 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3203 /* Tunnel exists? */
3204 tid = ntohl (connect_msg->tunnel_id);
3205 t = tunnel_get_by_local_id (c, tid);
3209 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3213 /* Does client own tunnel? */
3214 if (t->client->handle != client)
3217 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3221 /* Do WE have the service? */
3222 t->type = ntohl (connect_msg->type);
3223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", t->type);
3224 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
3225 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
3228 /* Yes! Fast forward, add ourselves to the tunnel and send the
3229 * good news to the client
3231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
3232 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
3233 peer_info_get (&my_full_id),
3234 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
3237 send_client_peer_connected(t, myid);
3238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
3239 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3242 /* Ok, lets find a peer offering the service */
3243 if (NULL != t->dht_get_type)
3245 GNUNET_DHT_get_stop (t->dht_get_type);
3247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
3248 GNUNET_h2s_full (&hash));
3250 GNUNET_DHT_get_start (dht_handle,
3251 GNUNET_TIME_UNIT_FOREVER_REL,
3252 GNUNET_BLOCK_TYPE_TEST,
3255 GNUNET_DHT_RO_RECORD_ROUTE |
3256 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
3258 &dht_get_type_handler, t);
3260 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3266 * Handler for client traffic directed to one peer
3268 * @param cls closure
3269 * @param client identification of the client
3270 * @param message the actual message
3273 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
3274 const struct GNUNET_MessageHeader *message)
3276 struct MeshClient *c;
3277 struct MeshTunnel *t;
3278 struct MeshPeerInfo *pi;
3279 struct GNUNET_MESH_Unicast *data_msg;
3280 MESH_TunnelNumber tid;
3283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3284 "MESH: Got a unicast request from a client!\n");
3286 /* Sanity check for client registration */
3287 if (NULL == (c = client_get (client)))
3290 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3293 data_msg = (struct GNUNET_MESH_Unicast *) message;
3294 /* Sanity check for message size */
3295 size = ntohs (message->size);
3296 if (sizeof (struct GNUNET_MESH_Unicast) +
3297 sizeof (struct GNUNET_MessageHeader) > size)
3300 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3304 /* Tunnel exists? */
3305 tid = ntohl (data_msg->tid);
3306 t = tunnel_get_by_local_id (c, tid);
3310 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3314 /* Is it a local tunnel? Then, does client own the tunnel? */
3315 if (NULL != t->client &&
3316 NULL != t->client->handle &&
3317 t->client->handle != client)
3320 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3324 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
3325 &data_msg->destination.hashPubKey);
3326 /* Is the selected peer in the tunnel? */
3330 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3334 /* Ok, everything is correct, send the message
3335 * (pretend we got it from a mesh peer)
3338 char buf[ntohs (message->size)];
3339 struct GNUNET_MESH_Unicast *copy;
3341 /* Work around const limitation */
3342 copy = (struct GNUNET_MESH_Unicast *) buf;
3343 memcpy (buf, data_msg, size);
3344 copy->oid = my_full_id;
3345 copy->tid = htonl (t->id.tid);
3346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3347 "MESH: calling generic handler...\n");
3348 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL);
3350 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3356 * Handler for client traffic directed to the origin
3358 * @param cls closure
3359 * @param client identification of the client
3360 * @param message the actual message
3363 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
3364 const struct GNUNET_MessageHeader *message)
3366 struct GNUNET_MESH_ToOrigin *data_msg;
3367 struct GNUNET_PeerIdentity id;
3368 struct MeshClient *c;
3369 struct MeshTunnel *t;
3370 MESH_TunnelNumber tid;
3373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3374 "MESH: Got a ToOrigin request from a client!\n");
3376 /* Sanity check for client registration */
3377 if (NULL == (c = client_get (client)))
3380 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3383 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
3384 /* Sanity check for message size */
3385 size = ntohs (message->size);
3386 if (sizeof (struct GNUNET_MESH_ToOrigin) +
3387 sizeof (struct GNUNET_MessageHeader) > size)
3390 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3394 /* Tunnel exists? */
3395 tid = ntohl (data_msg->tid);
3396 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3399 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3402 t = tunnel_get_by_local_id (c, tid);
3406 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3410 /* It shouldn't be a local tunnel. */
3411 if (NULL != t->client)
3414 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3417 GNUNET_PEER_resolve(t->id.oid, &id);
3419 /* Ok, everything is correct, send the message
3420 * (pretend we got it from a mesh peer)
3423 char buf[ntohs (message->size)];
3424 struct GNUNET_MESH_ToOrigin *copy;
3426 /* Work around const limitation */
3427 copy = (struct GNUNET_MESH_ToOrigin *) buf;
3428 memcpy (buf, data_msg, size);
3430 copy->tid = htonl (t->id.tid);
3431 copy->sender = my_full_id;
3432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3433 "MESH: calling generic handler...\n");
3434 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL);
3436 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3442 * Handler for client traffic directed to all peers in a tunnel
3444 * @param cls closure
3445 * @param client identification of the client
3446 * @param message the actual message
3449 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
3450 const struct GNUNET_MessageHeader *message)
3452 struct MeshClient *c;
3453 struct MeshTunnel *t;
3454 struct GNUNET_MESH_Multicast *data_msg;
3455 MESH_TunnelNumber tid;
3457 /* Sanity check for client registration */
3458 if (NULL == (c = client_get (client)))
3461 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3464 data_msg = (struct GNUNET_MESH_Multicast *) message;
3465 /* Sanity check for message size */
3466 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (data_msg->header.size))
3469 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3473 /* Tunnel exists? */
3474 tid = ntohl (data_msg->tid);
3475 t = tunnel_get_by_local_id (c, tid);
3479 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3483 /* Does client own tunnel? */
3484 if (t->client->handle != client)
3487 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3492 char buf[ntohs(message->size)];
3493 struct GNUNET_MESH_Multicast *copy;
3495 copy = (struct GNUNET_MESH_Multicast *)buf;
3496 memcpy(buf, message, ntohs(message->size));
3497 copy->oid = my_full_id;
3498 copy->tid = htonl(t->id.tid);
3499 handle_mesh_data_multicast(client, &my_full_id, ©->header, NULL);
3502 /* receive done gets called when last copy is sent */
3507 * Functions to handle messages from clients
3509 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
3510 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
3511 {&handle_local_tunnel_create, NULL,
3512 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
3513 sizeof (struct GNUNET_MESH_TunnelMessage)},
3514 {&handle_local_tunnel_destroy, NULL,
3515 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
3516 sizeof (struct GNUNET_MESH_TunnelMessage)},
3517 {&handle_local_connect_add, NULL,
3518 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
3519 sizeof (struct GNUNET_MESH_PeerControl)},
3520 {&handle_local_connect_del, NULL,
3521 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
3522 sizeof (struct GNUNET_MESH_PeerControl)},
3523 {&handle_local_connect_by_type, NULL,
3524 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
3525 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
3526 {&handle_local_unicast, NULL,
3527 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3528 {&handle_local_to_origin, NULL,
3529 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3530 {&handle_local_multicast, NULL,
3531 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3537 * To be called on core init/fail.
3539 * @param cls service closure
3540 * @param server handle to the server for this service
3541 * @param identity the public identity of this peer
3544 core_init (void *cls, struct GNUNET_CORE_Handle *server,
3545 const struct GNUNET_PeerIdentity *identity)
3547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
3548 core_handle = server;
3549 if (0 != memcmp(identity, &my_full_id, sizeof(my_full_id)) || NULL == server)
3551 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
3552 GNUNET_SCHEDULER_shutdown();
3558 * Method called whenever a given peer connects.
3560 * @param cls closure
3561 * @param peer peer identity this notification is about
3562 * @param atsi performance data for the connection
3565 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
3566 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
3568 struct MeshPeerInfo *peer_info;
3569 struct MeshPeerPath *path;
3571 #if MESH_DEBUG_CONNECTION
3572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
3573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
3574 GNUNET_h2s(&my_full_id.hashPubKey));
3576 peer_info = peer_info_get (peer);
3577 if (myid == peer_info->id)
3579 #if MESH_DEBUG_CONNECTION
3580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3584 #if MESH_DEBUG_CONNECTION
3587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
3588 GNUNET_h2s(&peer->hashPubKey));
3591 path = path_new (2);
3592 path->peers[0] = myid;
3593 path->peers[1] = peer_info->id;
3594 GNUNET_PEER_change_rc(myid, 1);
3595 GNUNET_PEER_change_rc(peer_info->id, 1);
3596 path_add_to_peer (peer_info, path);
3601 * Method called whenever a peer disconnects.
3603 * @param cls closure
3604 * @param peer peer identity this notification is about
3607 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
3609 struct MeshPeerInfo *pi;
3612 #if MESH_DEBUG_CONNECTION
3613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
3615 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
3621 for (i = 0; i < CORE_QUEUE_SIZE; i++)
3623 peer_info_cancel_transmission(pi, i);
3625 path_remove_from_peer (pi, pi->id, myid);
3626 #if MESH_DEBUG_CONNECTION
3629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3636 /******************************************************************************/
3637 /************************ MAIN FUNCTIONS ****************************/
3638 /******************************************************************************/
3641 * Iterator over hash map entries.
3643 * @param cls closure
3644 * @param key current key code
3645 * @param value value in the hash map
3646 * @return GNUNET_YES if we should continue to
3651 shutdown_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
3653 struct MeshTunnel *t = value;
3659 * Task run during shutdown.
3665 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
3668 /* TODO: destroy tunnels? */
3669 if (core_handle != NULL)
3671 GNUNET_CORE_disconnect (core_handle);
3674 GNUNET_CONTAINER_multihashmap_iterate(tunnels, &shutdown_tunnel, NULL);
3675 if (dht_handle != NULL)
3677 GNUNET_DHT_disconnect (dht_handle);
3682 GNUNET_SERVER_notification_context_destroy (nc);
3685 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
3687 GNUNET_SCHEDULER_cancel (announce_id_task);
3688 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
3690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
3694 * Process mesh requests.
3696 * @param cls closure
3697 * @param server the initialized server
3698 * @param c configuration to use
3701 run (void *cls, struct GNUNET_SERVER_Handle *server,
3702 const struct GNUNET_CONFIGURATION_Handle *c)
3704 struct MeshPeerInfo *peer;
3705 struct MeshPeerPath *p;
3708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
3709 server_handle = server;
3710 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
3711 CORE_QUEUE_SIZE, /* queue size */
3712 NULL, /* Closure passed to MESH functions */
3713 &core_init, /* Call core_init once connected */
3714 &core_connect, /* Handle connects */
3715 &core_disconnect, /* remove peers on disconnects */
3716 NULL, /* Don't notify about all incoming messages */
3717 GNUNET_NO, /* For header only in notification */
3718 NULL, /* Don't notify about all outbound messages */
3719 GNUNET_NO, /* For header-only out notification */
3720 core_handlers); /* Register these handlers */
3721 if (core_handle == NULL)
3724 GNUNET_SCHEDULER_shutdown ();
3729 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
3732 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3734 ("Mesh service is lacking key configuration settings. Exiting.\n"));
3735 GNUNET_SCHEDULER_shutdown ();
3738 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3739 GNUNET_free (keyfile);
3740 if (my_private_key == NULL)
3742 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3743 _("Mesh service could not access hostkey. Exiting.\n"));
3744 GNUNET_SCHEDULER_shutdown ();
3747 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
3748 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
3749 &my_full_id.hashPubKey);
3750 myid = GNUNET_PEER_intern (&my_full_id);
3752 dht_handle = GNUNET_DHT_connect (c, 64);
3753 if (dht_handle == NULL)
3759 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3761 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3762 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3763 peers = GNUNET_CONTAINER_multihashmap_create (32);
3764 applications = GNUNET_CONTAINER_multihashmap_create (32);
3765 types = GNUNET_CONTAINER_multihashmap_create (32);
3767 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
3768 nc = GNUNET_SERVER_notification_context_create (server_handle,
3770 GNUNET_SERVER_disconnect_notify (server_handle,
3771 &handle_local_client_disconnect,
3776 clients_tail = NULL;
3781 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3782 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
3784 /* Create a peer_info for the local peer */
3785 peer = peer_info_get(&my_full_id);
3788 path_add_to_peer(peer, p);
3790 /* Scheduled the task to clean up when shutdown is called */
3791 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
3794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
3798 * The main function for the mesh service.
3800 * @param argc number of arguments from the command line
3801 * @param argv command line arguments
3802 * @return 0 ok, 1 on error
3805 main (int argc, char *const *argv)
3810 // fprintf (stderr, "main ()\n");
3812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
3815 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
3817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");
3819 // fprintf (stderr, "main () END\n");