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 #define UNACKNOWLEDGED_WAIT GNUNET_TIME_relative_multiply(\
70 GNUNET_TIME_UNIT_SECONDS,\
73 /******************************************************************************/
74 /************************ DATA STRUCTURES ****************************/
75 /******************************************************************************/
77 /** FWD declaration */
81 * Struct containing all info possibly needed to build a package when called
84 struct MeshDataDescriptor
86 /** ID of the tunnel this packet travels in */
87 struct MESH_TunnelID *origin;
92 /** Client that asked for the transmission, if any */
93 struct GNUNET_SERVER_Client *client;
95 /** Who was this message being sent to */
96 struct MeshPeerInfo *peer;
98 /** Ultimate destination of the packet */
99 GNUNET_PEER_Id destination;
101 /** Number of identical messages sent to different hops (multicast) */
102 unsigned int *copies;
104 /** Which handler was used to request the transmission */
105 unsigned int handler_n;
107 /** Size of the data */
110 /** Used to allow a client send more traffic to the service after a
111 * previous packet was tried to be sent to a neighbor and couldn't */
112 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
117 * Struct containing all information regarding a given peer
127 * Last time we heard from this peer
129 struct GNUNET_TIME_Absolute last_contact;
132 * Number of attempts to reconnect so far
134 int n_reconnect_attempts;
137 * Paths to reach the peer, ordered by ascending hop count
139 struct MeshPeerPath *path_head;
142 * Paths to reach the peer, ordered by ascending hop count
144 struct MeshPeerPath *path_tail;
147 * Handle to stop the DHT search for a path to this peer
149 struct GNUNET_DHT_GetHandle *dhtget;
152 * Handles to stop queued transmissions for this peer
154 struct GNUNET_CORE_TransmitHandle *core_transmit[CORE_QUEUE_SIZE];
157 * Pointer to info stuctures used as cls for queued transmissions
159 void *infos[CORE_QUEUE_SIZE];
162 * Type of message being in each transmission
164 uint16_t types[CORE_QUEUE_SIZE];
167 * Array of tunnels this peer participates in
168 * (most probably a small amount, therefore not a hashmap)
169 * When the path to the peer changes, notify these tunnels to let them
170 * re-adjust their path trees.
172 struct MeshTunnel **tunnels;
175 * Number of tunnels this peers participates in
177 unsigned int ntunnels;
182 * Data scheduled to transmit (to local client or remote peer)
189 struct MeshQueue *next;
190 struct MeshQueue *prev;
193 * Target of the data (NULL if target is client)
195 struct MeshPeerInfo *peer;
198 * Client to send the data to (NULL if target is peer)
200 struct MeshClient *client;
203 * Size of the message to transmit
208 * How old is the data?
210 struct GNUNET_TIME_Absolute timestamp;
215 struct GNUNET_MessageHeader *data;
219 * Globally unique tunnel identification (owner + number)
220 * DO NOT USE OVER THE NETWORK
225 * Node that owns the tunnel
230 * Tunnel number to differentiate all the tunnels owned by the node oid
231 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
233 MESH_TunnelNumber tid;
237 struct MeshClient; /* FWD declaration */
240 * Struct containing all information regarding a tunnel
241 * For an intermediate node the improtant info used will be:
242 * - id Tunnel unique identification
243 * - paths[0] To know where to send it next
244 * - metainfo: ready, speeds, accounting
251 struct MESH_TunnelID id;
254 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
256 MESH_TunnelNumber local_tid;
259 * Last time the tunnel was used
261 struct GNUNET_TIME_Absolute timestamp;
264 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
265 * containing peers added by id or by type, not intermediate peers.
267 struct GNUNET_CONTAINER_MultiHashMap *peers;
270 * Number of peers that are connected and potentially ready to receive data
272 unsigned int peers_ready;
275 * Number of peers that have been added to the tunnel
277 unsigned int peers_total;
280 * Client owner of the tunnel, if any
282 struct MeshClient *client;
285 * Messages ready to transmit
287 struct MeshQueue *queue_head;
288 struct MeshQueue *queue_tail;
293 struct MeshTunnelTree *tree;
296 * Application type we are looking for in this tunnel
298 GNUNET_MESH_ApplicationType type;
301 * Used to search peers offering a service
303 struct GNUNET_DHT_GetHandle *dht_get_type;
306 * Task to keep the used paths alive
308 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
313 * Info needed to work with tunnel paths and peers
320 struct MeshTunnel *t;
325 struct MeshPeerInfo *peer;
330 struct MeshPeerPath *path;
333 * Position in peer's transmit queue
340 * Struct containing information about a client of the service
347 struct MeshClient *next;
348 struct MeshClient *prev;
351 * Tunnels that belong to this client, indexed by local id
353 struct GNUNET_CONTAINER_MultiHashMap *tunnels;
356 * Handle to communicate with the client
358 struct GNUNET_SERVER_Client *handle;
361 * Applications that this client has claimed to provide
363 struct GNUNET_CONTAINER_MultiHashMap *apps;
366 * Messages that this client has declared interest in
368 struct GNUNET_CONTAINER_MultiHashMap *types;
372 * ID of the client, for debug messages
381 /******************************************************************************/
382 /************************ DEBUG FUNCTIONS ****************************/
383 /******************************************************************************/
387 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
388 * @param cls string to print
389 * @param tc task context
392 mesh_debug (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
396 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n", s);
404 /******************************************************************************/
405 /*********************** GLOBAL VARIABLES ****************************/
406 /******************************************************************************/
411 static struct MeshClient *clients;
412 static struct MeshClient *clients_tail;
415 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
417 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
420 * Tunnels incoming, indexed by MESH_TunnelNumber
421 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
423 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
426 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
428 static struct GNUNET_CONTAINER_MultiHashMap *peers;
431 * Handle to communicate with core
433 static struct GNUNET_CORE_Handle *core_handle;
438 static struct GNUNET_DHT_Handle *dht_handle;
443 static struct GNUNET_SERVER_Handle *server_handle;
446 * Notification context, to send messages to local clients
448 static struct GNUNET_SERVER_NotificationContext *nc;
451 * Local peer own ID (memory efficient handle)
453 static GNUNET_PEER_Id myid;
456 * Local peer own ID (full value)
458 static struct GNUNET_PeerIdentity my_full_id;
463 static struct GNUNET_CRYPTO_RsaPrivateKey* my_private_key;
468 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
471 * Tunnel ID for the next created tunnel (global tunnel number)
473 static MESH_TunnelNumber next_tid;
476 * Tunnel ID for the next incoming tunnel (local tunnel number)
478 static MESH_TunnelNumber next_local_tid;
481 * All application types provided by this peer
483 static struct GNUNET_CONTAINER_MultiHashMap *applications;
486 * All message types clients of this peer are interested in
488 static struct GNUNET_CONTAINER_MultiHashMap *types;
491 * Task to periodically announce provided applications
493 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
496 * Task to periodically announce itself in the network
498 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
501 unsigned int next_client_id;
505 /******************************************************************************/
506 /************************ ITERATORS ****************************/
507 /******************************************************************************/
509 /* FIXME move iterators here */
512 /******************************************************************************/
513 /************************ PERIODIC FUNCTIONS ****************************/
514 /******************************************************************************/
517 * Announce iterator over for each application provided by the peer
520 * @param key current key code
521 * @param value value in the hash map
522 * @return GNUNET_YES if we should continue to
527 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
529 /* FIXME are hashes in multihash map equal on all aquitectures? */
530 GNUNET_DHT_put (dht_handle,
533 GNUNET_DHT_RO_RECORD_ROUTE |
534 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
535 GNUNET_BLOCK_TYPE_TEST,
536 sizeof (struct GNUNET_PeerIdentity),
537 (const char *) &my_full_id,
539 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
540 &mesh_debug, "DHT_put for app completed");
542 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
544 APP_ANNOUNCE_TIME, NULL, NULL);
551 * Periodically announce what applications are provided by local clients
554 * @param tc task context
557 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
559 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
561 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Starting PUT for apps\n");
565 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
567 announce_applications_task =
568 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Finished PUT for apps\n");
576 * Periodically announce self id in the DHT
579 * @param tc task context
582 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
584 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
586 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
590 * - Set data expiration in function of X
593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DHT_put for ID %s started.\n",
594 GNUNET_h2s_full (&my_full_id.hashPubKey));
595 GNUNET_DHT_put (dht_handle, /* DHT handle */
596 &my_full_id.hashPubKey, /* Key to use */
597 10U, /* Replication level */
598 GNUNET_DHT_RO_RECORD_ROUTE |
599 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
600 GNUNET_BLOCK_TYPE_TEST, /* Block type */
601 sizeof(my_full_id), /* Size of the data */
602 (char *)&my_full_id, /* Data itself */
603 GNUNET_TIME_absolute_get_forever (), /* Data expiration */
604 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
606 &mesh_debug, "DHT_put for id completed");
608 NULL, /* Continuation */
609 NULL); /* Continuation closure */
612 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
617 * Function to process paths received for a new peer addition. The recorded
618 * paths form the initial tunnel, which can be optimized later.
619 * Called on each result obtained for the DHT search.
622 * @param exp when will this value expire
623 * @param key key of the result
624 * @param type type of the result
625 * @param size number of bytes in data
626 * @param data pointer to the result data
629 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
630 const GNUNET_HashCode * key,
631 const struct GNUNET_PeerIdentity *get_path,
632 unsigned int get_path_length,
633 const struct GNUNET_PeerIdentity *put_path,
634 unsigned int put_path_length,
635 enum GNUNET_BLOCK_Type type, size_t size, const void *data);
638 /******************************************************************************/
639 /****************** GENERAL HELPER FUNCTIONS ************************/
640 /******************************************************************************/
643 * Check if client has registered with the service and has not disconnected
645 * @param client the client to check
647 * @return non-NULL if client exists in the global DLL
649 static struct MeshClient *
650 client_get (struct GNUNET_SERVER_Client *client)
652 struct MeshClient *c;
657 if (c->handle == client)
666 * Checks if a given client has subscribed to certain message type
668 * @param message_type Type of message to check
669 * @param c Client to check
671 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
676 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
680 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
681 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
685 client_allow_send(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
687 struct MeshDataDescriptor *info = cls;
689 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
691 info->timeout_task = GNUNET_SCHEDULER_NO_TASK;
692 GNUNET_SERVER_receive_done(info->client, GNUNET_OK);
697 * Search for a tunnel by global ID using full PeerIdentities
699 * @param oid owner of the tunnel
700 * @param tid global tunnel number
702 * @return tunnel handler, NULL if doesn't exist
704 static struct MeshTunnel *
705 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
709 * Send the message to all clients that have subscribed to its type
711 * @param msg Pointer to the message itself
712 * @return number of clients this message was sent to
715 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
716 const struct GNUNET_MessageHeader *payload)
718 struct GNUNET_PeerIdentity *oid;
719 struct MeshClient *c;
720 struct MeshTunnel *t;
721 MESH_TunnelNumber *tid;
724 char cbuf[htons(msg->size)];
726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Sending to clients...\n");
727 type = ntohs (payload->type);
728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: message of type %u\n", type);
730 memcpy (cbuf, msg, sizeof(cbuf));
731 switch (htons(msg->type))
733 struct GNUNET_MESH_Unicast *uc;
734 struct GNUNET_MESH_Multicast *mc;
735 struct GNUNET_MESH_ToOrigin *to;
737 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
738 uc = (struct GNUNET_MESH_Unicast *) cbuf;
742 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
743 mc = (struct GNUNET_MESH_Multicast *) cbuf;
747 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
748 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
756 t = tunnel_get (oid, ntohl(*tid));
762 *tid = htonl (t->local_tid);
763 for (count = 0, c = clients; c != NULL; c = c->next)
765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client %u\n", c->id);
766 if (client_is_subscribed (type, c))
769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: sending\n");
770 GNUNET_SERVER_notification_context_unicast (
773 (struct GNUNET_MessageHeader *) cbuf,
782 * Notify the client that owns the tunnel that a peer has connected to it
784 * @param t Tunnel whose owner to notify
785 * @param id Short id of the peer that has connected
788 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
790 struct GNUNET_MESH_PeerControl pc;
792 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
793 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
794 pc.tunnel_id = htonl (t->local_tid);
795 GNUNET_PEER_resolve (id, &pc.peer);
796 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
797 &pc.header, GNUNET_NO);
802 * Function called to notify a client about the socket
803 * being ready to queue more data. "buf" will be
804 * NULL and "size" zero if the socket was closed for
805 * writing in the meantime.
808 * @param size number of bytes available in buf
809 * @param buf where the callee should write the message
810 * @return number of bytes written to buf
813 send_core_create_path (void *cls, size_t size, void *buf);
816 * Cancel a core transmission that was already requested and free all resources
817 * associated to the request.
819 * @param peer PeeInfo of the peer whose transmission is cancelled.
820 * @param i Position of the transmission to be cancelled.
823 peer_info_cancel_transmission(struct MeshPeerInfo *peer, unsigned int i)
825 if (NULL != peer->core_transmit[i])
827 struct MeshDataDescriptor *dd;
828 struct MeshPathInfo *path_info;
830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
831 "MESH: Cancelling data transmission at %u\n",
833 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit[i]);
834 peer->core_transmit[i] = NULL;
835 /* TODO: notify that tranmission has failed */
836 switch (peer->types[i])
838 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
839 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
840 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type payload\n");
843 if (0 == --(*dd->copies))
845 GNUNET_free (dd->copies);
846 GNUNET_free (dd->data);
849 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type create path\n");
851 path_info = peer->infos[i];
852 path_destroy(path_info->path);
855 GNUNET_free (peer->infos[i]);
864 peer_info_transmit_slot (struct MeshPeerInfo *peer)
868 for (i = 0; peer->core_transmit[i]; i++)
870 if (i == (CORE_QUEUE_SIZE - 1))
872 /* All positions are taken! Overwriting! */
874 peer_info_cancel_transmission(peer, 0);
883 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
884 * and insert it in the appropiate structures if the peer is not known yet.
886 * @param peer Full identity of the peer.
888 * @return Existing or newly created peer info.
890 static struct MeshPeerInfo *
891 peer_info_get (const struct GNUNET_PeerIdentity *peer)
893 struct MeshPeerInfo *peer_info;
895 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
896 if (NULL == peer_info)
899 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
900 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
901 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
902 peer_info->id = GNUNET_PEER_intern (peer);
910 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
911 * and insert it in the appropiate structures if the peer is not known yet.
913 * @param peer Short identity of the peer.
915 * @return Existing or newly created peer info.
917 static struct MeshPeerInfo *
918 peer_info_get_short (const GNUNET_PEER_Id peer)
920 struct GNUNET_PeerIdentity id;
922 GNUNET_PEER_resolve(peer, &id);
923 return peer_info_get(&id);
928 * Iterator to remove the tunnel from the list of tunnels a peer participates
931 * @param cls Closure (tunnel info)
932 * @param key GNUNET_PeerIdentity of the peer (unused)
933 * @param value PeerInfo of the peer
935 * @return always GNUNET_YES, to keep iterating
938 peer_info_delete_tunnel (void* cls, const GNUNET_HashCode* key, void* value)
940 struct MeshTunnel *t = cls;
941 struct MeshPeerInfo *peer = value;
944 for (i = 0; i < peer->ntunnels; i++)
946 if (0 == memcmp(&peer->tunnels[i]->id,
948 sizeof(struct MESH_TunnelID)))
951 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
952 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
961 * Sends a CREATE PATH message for a path to a peer, properly registrating
962 * all used resources.
964 * @param peer PeerInfo of the final peer for whom this path is being created.
965 * @param p Path itself.
966 * @param t Tunnel for which the path is created.
969 send_create_path (struct MeshPeerInfo *peer,
970 struct MeshPeerPath *p,
971 struct MeshTunnel *t)
973 struct GNUNET_PeerIdentity id;
974 struct MeshPathInfo *path_info;
975 struct MeshPeerInfo *neighbor;
980 p = tree_get_path_to_peer(t->tree, peer->id);
987 for (i = 0; i < p->length; i++)
989 if (p->peers[i] == myid)
992 if (i >= p->length - 1)
998 GNUNET_PEER_resolve(p->peers[i + 1], &id);
1000 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1001 path_info->path = p;
1002 path_info->peer = peer;
1004 neighbor = peer_info_get(&id);
1005 path_info->pos = peer_info_transmit_slot(neighbor);
1006 neighbor->types[path_info->pos] = GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE;
1007 neighbor->infos[path_info->pos] = path_info;
1008 neighbor->core_transmit[path_info->pos] =
1009 GNUNET_CORE_notify_transmit_ready (
1010 core_handle, /* handle */
1013 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1015 sizeof (struct GNUNET_MESH_ManipulatePath)
1016 + (p->length * sizeof (struct GNUNET_PeerIdentity)), /*size */
1017 &send_core_create_path, /* callback */
1018 path_info); /* cls */
1023 * Try to establish a new connection to this peer.
1024 * Use the best path for the given tunnel.
1025 * If the peer doesn't have any path to it yet, try to get one.
1026 * If the peer already has some path, send a CREATE PATH towards it.
1028 * @param peer PeerInfo of the peer.
1029 * @param t Tunnel for which to create the path, if possible.
1032 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1034 struct MeshPeerPath *p;
1035 struct MeshPathInfo *path_info;
1037 if (NULL != peer->path_head)
1039 p = tree_get_path_to_peer(t->tree, peer->id);
1042 send_create_path(peer, p, t);
1047 send_client_peer_connected(t, myid);
1050 else if (NULL == peer->dhtget)
1052 struct GNUNET_PeerIdentity id;
1054 GNUNET_PEER_resolve(peer->id, &id);
1055 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
1056 path_info->peer = peer;
1059 GNUNET_DHT_get_start(dht_handle, /* handle */
1060 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1061 GNUNET_BLOCK_TYPE_TEST, /* type */
1062 &id.hashPubKey, /* key to search */
1063 4, /* replication level */
1064 GNUNET_DHT_RO_RECORD_ROUTE |
1065 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1067 0, /* xquery bits */
1068 &dht_get_id_handler,
1071 /* Otherwise, there is no path but the DHT get is already started. */
1077 * Destroy the peer_info and free any allocated resources linked to it
1078 * @param t tunnel the path belongs to
1079 * @param pi the peer_info to destroy
1080 * @return GNUNET_OK on success
1083 peer_info_destroy (struct MeshPeerInfo *pi)
1085 GNUNET_HashCode hash;
1086 struct GNUNET_PeerIdentity id;
1088 GNUNET_PEER_resolve (pi->id, &id);
1089 GNUNET_PEER_change_rc (pi->id, -1);
1090 GNUNET_CRYPTO_hash (&id, sizeof (struct GNUNET_PeerIdentity), &hash);
1092 GNUNET_CONTAINER_multihashmap_remove (peers, &hash, pi);
1093 GNUNET_SCHEDULER_cancel (pi->path_refresh_task);
1101 * Notify a tunnel that a connection has broken that affects at least
1102 * some of its peers.
1104 * @param t Tunnel affected.
1105 * @param peer Peer that (at least) has been affected by the disconnection.
1106 * @param p1 Peer that got disconnected from p2.
1107 * @param p2 Peer that got disconnected from p1.
1109 * @return Short ID of the peer disconnected (either p1 or p2).
1110 * 0 if the tunnel remained unaffected.
1112 static GNUNET_PEER_Id
1113 tunnel_notify_connection_broken (struct MeshTunnel *t,
1114 struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1118 * Remove all paths that rely on a direct connection between p1 and p2
1119 * from the peer itself and notify all tunnels about it.
1121 * @param peer PeerInfo of affected peer.
1122 * @param p1 GNUNET_PEER_Id of one peer.
1123 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1126 * TODO: optimize (see below)
1129 path_remove_from_peer (struct MeshPeerInfo *peer,
1133 struct GNUNET_PeerIdentity id;
1134 struct MeshPeerPath *p;
1135 struct MeshPeerPath *aux;
1136 struct MeshPeerInfo *peer_d;
1138 unsigned int destroyed;
1144 p = peer->path_head;
1148 for (i = 0; i < (p->length - 1); i++)
1150 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1151 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1163 for (i = 0; i < peer->ntunnels; i++)
1165 d = tunnel_notify_connection_broken (peer->tunnels[i], peer, p1, p2);
1167 * Problem: one or more peers have been deleted from the tunnel tree.
1168 * We don't know who they are to try to add them again.
1169 * We need to try to find a new path for each of the disconnected peers.
1170 * Some of them might already have a path to reach them that does not
1171 * involve p1 and p2. Adding all anew might render in a better tree than
1172 * the trivial immediate fix.
1174 * Trivial immiediate fix: try to reconnect to the disconnected node. All
1175 * its children will be reachable trough him.
1177 peer_d = peer_info_get_short(d);
1180 for (p = peer_d->path_head; NULL != p; p = p->next)
1182 if ((cost = path_get_cost(peer->tunnels[i]->tree, p)) < best)
1190 /* No callback, as peer will be already disconnected */
1191 tree_add_path(peer->tunnels[i]->tree, aux, NULL);
1195 struct MeshPathInfo *path_info;
1197 if (NULL != peer_d->dhtget)
1199 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
1200 path_info->path = p;
1201 path_info->peer = peer_d;
1202 path_info->t = peer->tunnels[i];
1204 GNUNET_DHT_get_start(dht_handle, /* handle */
1205 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1206 GNUNET_BLOCK_TYPE_TEST, /* type */
1207 &id.hashPubKey, /*key to search */
1208 4, /* replication level */
1209 GNUNET_DHT_RO_RECORD_ROUTE |
1210 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1212 0, /* xquery bits */
1213 &dht_get_id_handler,
1214 (void *) path_info);
1221 * Add the path to the peer and update the path used to reach it in case this
1224 * @param peer_info Destination peer to add the path to.
1225 * @param path New path to add. Last peer must be the peer in arg 1.
1226 * Path will be either used of freed if already known.
1228 * TODO: trim the part from origin to us? Add it as path to origin?
1231 path_add_to_peer (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
1233 struct MeshPeerPath *aux;
1237 if (NULL == peer_info || NULL == path)
1243 l = path_get_length (path);
1250 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1252 l2 = path_get_length (aux);
1255 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1256 peer_info->path_tail, aux, path);
1260 if (l2 == l && memcmp(path->peers, aux->peers, l) == 0)
1267 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1274 * Add the path to the origin peer and update the path used to reach it in case
1275 * this is the shortest.
1276 * The path is given in peer_info -> destination, therefore we turn the path
1277 * upside down first.
1279 * @param peer_info Peer to add the path to, being the origin of the path.
1280 * @param path New path to add after being inversed.
1283 path_add_to_origin (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
1286 path_add_to_peer (peer_info, path);
1291 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1292 * to obtain a local peer -> destination path and interning the peer ids.
1294 * @return Newly allocated and created path
1296 static struct MeshPeerPath *
1297 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1298 unsigned int get_path_length,
1299 const struct GNUNET_PeerIdentity *put_path,
1300 unsigned int put_path_length)
1302 struct MeshPeerPath *p;
1308 GNUNET_PEER_change_rc(myid, 1);
1309 i = get_path_length;
1310 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: GET has %d hops.\n", i);
1311 for (i--; i >= 0; i--)
1313 id = GNUNET_PEER_intern (&get_path[i]);
1314 if (p->length > 0 && id == p->peers[p->length - 1])
1316 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1317 GNUNET_PEER_change_rc(id, -1);
1321 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1322 "MESH: Adding from GET: %s.\n",
1323 GNUNET_i2s(&get_path[i]));
1325 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1326 p->peers[p->length - 1] = id;
1329 i = put_path_length;
1330 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: PUT has %d hops.\n", i);
1331 for (i--; i >= 0; i--)
1333 id = GNUNET_PEER_intern (&put_path[i]);
1336 /* PUT path went through us, so discard the path up until now and start
1337 * from here to get a much shorter (and loop-free) path.
1342 if (p->length > 0 && id == p->peers[p->length - 1])
1344 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1345 GNUNET_PEER_change_rc(id, -1);
1349 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1350 "MESH: Adding from PUT: %s.\n",
1351 GNUNET_i2s(&put_path[i]));
1353 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1354 p->peers[p->length - 1] = id;
1358 if (get_path_length > 0)
1359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1360 "MESH: (first of GET: %s)\n",
1361 GNUNET_h2s_full(&get_path[0].hashPubKey));
1362 if (put_path_length > 0)
1363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1364 "MESH: (first of PUT: %s)\n",
1365 GNUNET_h2s_full(&put_path[0].hashPubKey));
1366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1367 "MESH: In total: %d hops\n",
1369 for (i = 0; i < p->length; i++)
1371 struct GNUNET_PeerIdentity peer_id;
1373 GNUNET_PEER_resolve(p->peers[i], &peer_id);
1374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1377 GNUNET_h2s_full(&peer_id.hashPubKey));
1385 * Send keepalive packets for a peer
1387 * @param cls Closure (tunnel for which to send the keepalive).
1388 * @param tc Notification context.
1390 * TODO: implement explicit multicast keepalive?
1393 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1397 * Search for a tunnel among the tunnels for a client
1399 * @param c the client whose tunnels to search in
1400 * @param tid the local id of the tunnel
1402 * @return tunnel handler, NULL if doesn't exist
1404 static struct MeshTunnel *
1405 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1407 GNUNET_HashCode hash;
1409 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1410 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1412 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
1414 return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
1419 * Search for a tunnel by global ID using PEER_ID
1421 * @param pi owner of the tunnel
1422 * @param tid global tunnel number
1424 * @return tunnel handler, NULL if doesn't exist
1426 static struct MeshTunnel *
1427 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1429 struct MESH_TunnelID id;
1430 GNUNET_HashCode hash;
1435 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1436 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1441 * Search for a tunnel by global ID using full PeerIdentities
1443 * @param oid owner of the tunnel
1444 * @param tid global tunnel number
1446 * @return tunnel handler, NULL if doesn't exist
1448 static struct MeshTunnel *
1449 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1451 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1456 * Callback used to notify a client owner of a tunnel that a peer has
1457 * disconnected, most likely because of a path change.
1459 * @param n Node in the tree representing the disconnected peer
1462 notify_peer_disconnected (const struct MeshTunnelTreeNode *n)
1464 struct MeshPeerInfo *peer;
1466 if (NULL != n->t->client && NULL != nc)
1468 struct GNUNET_MESH_PeerControl msg;
1469 msg.header.size = htons (sizeof (msg));
1470 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1471 msg.tunnel_id = htonl (n->t->local_tid);
1472 GNUNET_PEER_resolve (n->peer, &msg.peer);
1473 GNUNET_SERVER_notification_context_unicast (nc, n->t->client->handle,
1474 &msg.header, GNUNET_NO);
1476 peer = peer_info_get_short(n->peer);
1477 peer_info_connect(peer, n->t);
1482 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1483 * needed rescources.
1484 * If peer already exists, reevaluate shortest path and change if different.
1486 * @param t Tunnel we want to add a new peer to
1487 * @param peer PeerInfo of the peer being added
1491 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1493 struct GNUNET_PeerIdentity id;
1494 struct MeshPeerPath *best_p;
1495 struct MeshPeerPath *p;
1496 unsigned int best_cost;
1499 GNUNET_PEER_resolve(peer->id, &id);
1501 GNUNET_CONTAINER_multihashmap_contains(t->peers, &id.hashPubKey))
1504 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
1505 GNUNET_CONTAINER_multihashmap_put(
1509 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1512 if (NULL != (p = peer->path_head))
1515 best_cost = path_get_cost(t->tree, p);
1518 if ((cost = path_get_cost (t->tree, p)) < best_cost)
1525 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected);
1526 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
1527 t->path_refresh_task =
1528 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
1532 /* Start a DHT get if necessary */
1533 peer_info_connect(peer, t);
1538 * Add a path to a tunnel which we don't own, just to remember the next hop.
1539 * If destination node was already in the tunnel, the first hop information
1540 * will be replaced with the new path.
1542 * @param t Tunnel we want to add a new peer to
1543 * @param p Path to add
1544 * @param own_pos Position of local node in path.
1548 tunnel_add_path (struct MeshTunnel *t,
1549 struct MeshPeerPath *p,
1550 unsigned int own_pos)
1552 struct GNUNET_PeerIdentity id;
1554 GNUNET_assert (0 != own_pos);
1555 tree_add_path(t->tree, p, NULL);
1556 if (NULL == t->tree->me)
1557 t->tree->me = tree_find_peer(t->tree->root, p->peers[own_pos]);
1558 if (own_pos < p->length - 1)
1560 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
1561 tree_update_first_hops(t->tree, t->tree->me, &id);
1567 * Notify a tunnel that a connection has broken that affects at least
1568 * some of its peers.
1570 * @param t Tunnel affected.
1571 * @param peer Peer that (at least) has been affected by the disconnection.
1572 * @param p1 Peer that got disconnected from p2.
1573 * @param p2 Peer that got disconnected from p1.
1575 * @return Short ID of the peer disconnected (either p1 or p2).
1576 * 0 if the tunnel remained unaffected.
1578 static GNUNET_PEER_Id
1579 tunnel_notify_connection_broken (struct MeshTunnel *t,
1580 struct MeshPeerInfo *peer,
1584 return tree_notify_connection_broken (t->tree, p1, p2,
1585 ¬ify_peer_disconnected);
1590 * Destroy the tunnel and free any allocated resources linked to it
1592 * @param t the tunnel to destroy
1594 * @return GNUNET_OK on success
1597 tunnel_destroy (struct MeshTunnel *t)
1599 struct MeshClient *c;
1600 struct MeshQueue *q;
1601 struct MeshQueue *qn;
1602 GNUNET_HashCode hash;
1611 struct GNUNET_PeerIdentity id;
1613 GNUNET_PEER_resolve(t->id.oid, &id);
1614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1615 "MESH: destroying tunnel %s [%x]\n",
1619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
1623 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1624 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
1629 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1631 GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
1635 if (t->local_tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1637 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1638 GNUNET_break (GNUNET_YES ==
1639 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t));
1642 if (NULL != t->peers)
1644 GNUNET_CONTAINER_multihashmap_iterate(t->peers,
1645 &peer_info_delete_tunnel, t);
1646 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
1651 if (NULL != q->data)
1652 GNUNET_free (q->data);
1656 /* TODO cancel core transmit ready in case it was active */
1658 tree_destroy(t->tree);
1659 if (NULL != t->dht_get_type)
1660 GNUNET_DHT_get_stop(t->dht_get_type);
1667 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
1668 * client when the client disconnects.
1670 * @param cls closure (client that is disconnecting)
1671 * @param key the hash of the local tunnel id (used to access the hashmap)
1672 * @param value the value stored at the key (tunnel to destroy)
1674 * @return GNUNET_OK on success
1677 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
1679 struct MeshTunnel *t = value;
1682 if (NULL != t->dht_get_type)
1684 GNUNET_DHT_get_stop (t->dht_get_type);
1686 r = tunnel_destroy (t);
1691 /******************************************************************************/
1692 /**************** MESH NETWORK HANDLER HELPERS ***********************/
1693 /******************************************************************************/
1696 * Function called to notify a client about the socket
1697 * being ready to queue more data. "buf" will be
1698 * NULL and "size" zero if the socket was closed for
1699 * writing in the meantime.
1701 * @param cls closure
1702 * @param size number of bytes available in buf
1703 * @param buf where the callee should write the message
1704 * @return number of bytes written to buf
1707 send_core_create_path (void *cls, size_t size, void *buf)
1709 struct MeshPathInfo *info = cls;
1710 struct GNUNET_MESH_ManipulatePath *msg;
1711 struct GNUNET_PeerIdentity *peer_ptr;
1712 struct MeshPeerInfo *peer = info->peer;
1713 struct MeshTunnel *t = info->t;
1714 struct MeshPeerPath *p = info->path;
1719 sizeof (struct GNUNET_MESH_ManipulatePath) +
1720 p->length * sizeof (struct GNUNET_PeerIdentity);
1722 if (size < size_needed || NULL == buf)
1724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: create path retransmit!\n");
1725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: buf: %p\n", buf);
1726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: size: (%u/%u)\n",
1728 info->peer->core_transmit[info->pos] =
1729 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1730 GNUNET_TIME_UNIT_FOREVER_REL,
1731 path_get_first_hop (t->tree, peer->id),
1733 &send_core_create_path,
1737 info->peer->core_transmit[info->pos] = NULL;
1739 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
1740 msg->header.size = htons (size_needed);
1741 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
1742 msg->tid = ntohl (t->id.tid);
1744 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1745 for (i = 0; i < p->length; i++)
1747 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1754 "MESH: CREATE PATH (%u bytes long) sent!\n",
1762 * Function called to notify a client about the socket
1763 * being ready to queue more data. "buf" will be
1764 * NULL and "size" zero if the socket was closed for
1765 * writing in the meantime.
1767 * @param cls closure (MeshDataDescriptor with all info to build packet)
1768 * @param size number of bytes available in buf
1769 * @param buf where the callee should write the message
1770 * @return number of bytes written to buf
1773 send_core_data_to_origin (void *cls, size_t size, void *buf)
1775 struct MeshDataDescriptor *info = cls;
1776 struct GNUNET_MESH_ToOrigin *msg = buf;
1779 GNUNET_assert (NULL != info);
1780 total_size = sizeof (struct GNUNET_MESH_ToOrigin) + info->size;
1781 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
1783 if (total_size > size)
1785 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1786 "not enough buffer to send data to origin\n");
1789 msg->header.size = htons (total_size);
1790 msg->header.type = htons (GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN);
1791 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1792 msg->tid = htonl (info->origin->tid);
1793 if (0 != info->size)
1795 memcpy (&msg[1], &info[1], info->size);
1797 if (NULL != info->client)
1799 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1808 * Function called to notify a client about the socket
1809 * being ready to queue more data. "buf" will be
1810 * NULL and "size" zero if the socket was closed for
1811 * writing in the meantime.
1813 * @param cls closure (data itself)
1814 * @param size number of bytes available in buf
1815 * @param buf where the callee should write the message
1817 * @return number of bytes written to buf
1820 send_core_data_multicast (void *cls, size_t size, void *buf)
1822 struct MeshDataDescriptor *info = cls;
1823 struct GNUNET_MESH_Multicast *msg = buf;
1826 GNUNET_assert (NULL != info);
1827 GNUNET_assert (NULL != info->peer);
1828 total_size = info->size + sizeof (struct GNUNET_MESH_Multicast);
1829 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1831 if (total_size > size)
1834 struct GNUNET_PeerIdentity id;
1836 GNUNET_PEER_resolve(info->peer->id, &id);
1837 info->peer->infos[info->handler_n] = info;
1838 info->peer->types[info->handler_n] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
1839 info->peer->core_transmit[info->handler_n] =
1840 GNUNET_CORE_notify_transmit_ready (core_handle,
1843 GNUNET_TIME_UNIT_FOREVER_REL,
1846 &send_core_data_multicast,
1850 info->peer->core_transmit[info->handler_n] = NULL;
1851 info->peer->infos[info->handler_n] = NULL;
1852 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
1853 msg->header.size = htons (total_size);
1854 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1855 msg->tid = htonl (info->origin->tid);
1856 memcpy (&msg[1], info->data, info->size);
1857 if (0 == --(*info->copies))
1859 if (NULL != info->client)
1861 /* FIXME One unresponsive neighbor (who doesn't "call" tmt_rdy) can lock
1862 * the client from sending anything else to the service.
1863 * - Call receive_done after certain timeout.
1864 * - Here cancel the timeout.
1866 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1868 GNUNET_free (info->data);
1869 GNUNET_free (info->copies);
1877 * Function called to notify a client about the socket
1878 * being ready to queue more data. "buf" will be
1879 * NULL and "size" zero if the socket was closed for
1880 * writing in the meantime.
1882 * @param cls closure (MeshDataDescriptor)
1883 * @param size number of bytes available in buf
1884 * @param buf where the callee should write the message
1885 * @return number of bytes written to buf
1888 send_core_path_ack (void *cls, size_t size, void *buf)
1890 struct MeshDataDescriptor *info = cls;
1891 struct GNUNET_MESH_PathACK *msg = buf;
1893 GNUNET_assert (NULL != info);
1896 info->peer->core_transmit[info->handler_n] = NULL;
1898 if (sizeof (struct GNUNET_MESH_PathACK) > size)
1903 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
1904 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
1905 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1906 msg->tid = htonl (info->origin->tid);
1907 msg->peer_id = my_full_id;
1909 /* TODO add signature */
1911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: PATH ACK sent!\n");
1912 return sizeof (struct GNUNET_MESH_PathACK);
1917 * Function called to notify a client about the socket
1918 * being ready to queue more data. "buf" will be
1919 * NULL and "size" zero if the socket was closed for
1920 * writing in the meantime.
1922 * @param cls closure (data itself)
1923 * @param size number of bytes available in buf
1924 * @param buf where the callee should write the message
1925 * @return number of bytes written to buf
1928 send_core_data_raw (void *cls, size_t size, void *buf)
1930 struct GNUNET_MessageHeader *msg = cls;
1933 GNUNET_assert (NULL != msg);
1934 total_size = ntohs (msg->size);
1936 if (total_size > size)
1941 memcpy (buf, msg, total_size);
1949 * Send another peer a notification to destroy a tunnel
1950 * @param cls The tunnel to destroy
1951 * @param size Size in the buffer
1952 * @param buf Memory where to put the data to transmit
1953 * @return Size of data put in buffer
1956 send_p2p_tunnel_destroy (void *cls, size_t size, void *buf)
1958 struct MeshTunnel *t = cls;
1959 struct MeshClient *c;
1960 struct GNUNET_MESH_TunnelMessage *msg;
1964 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1965 /*FIXME*/ msg->header.size =
1966 htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1967 msg->tunnel_id = htonl (t->id.tid);
1969 tunnel_destroy (c, t);
1970 return sizeof (struct GNUNET_MESH_TunnelMessage);
1975 /******************************************************************************/
1976 /******************** MESH NETWORK HANDLERS **************************/
1977 /******************************************************************************/
1981 * Core handler for path creation
1982 * struct GNUNET_CORE_MessageHandler
1984 * @param cls closure
1985 * @param message message
1986 * @param peer peer identity this notification is about
1987 * @param atsi performance data
1988 * @return GNUNET_OK to keep the connection open,
1989 * GNUNET_SYSERR to close it (signal serious error)
1993 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1994 const struct GNUNET_MessageHeader *message,
1995 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1997 unsigned int own_pos;
2000 MESH_TunnelNumber tid;
2001 struct GNUNET_MESH_ManipulatePath *msg;
2002 struct GNUNET_PeerIdentity *pi;
2003 GNUNET_HashCode hash;
2004 struct MeshPeerPath *path;
2005 struct MeshPeerInfo *dest_peer_info;
2006 struct MeshPeerInfo *orig_peer_info;
2007 struct MeshTunnel *t;
2009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2010 "MESH: Received a path create msg [%s]\n",
2011 GNUNET_i2s(&my_full_id));
2012 size = ntohs (message->size);
2013 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2015 GNUNET_break_op (0);
2019 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2020 if (size % sizeof (struct GNUNET_PeerIdentity))
2022 GNUNET_break_op (0);
2025 size /= sizeof (struct GNUNET_PeerIdentity);
2028 GNUNET_break_op (0);
2031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2032 "MESH: path has %u hops.\n",
2034 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2036 tid = ntohl (msg->tid);
2037 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2038 t = tunnel_get (pi, tid);
2039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2040 "MESH: path is for tunnel %s [%X].\n",
2045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
2046 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2047 t->id.oid = GNUNET_PEER_intern (pi);
2049 t->local_tid = next_local_tid++;
2050 /* FIXME test if taken */
2051 next_local_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2052 t->tree = tree_new(t, t->id.oid);
2054 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2056 GNUNET_CONTAINER_multihashmap_put (
2060 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2066 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2068 GNUNET_CONTAINER_multihashmap_put (
2072 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2080 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
2081 if (NULL == dest_peer_info)
2083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2084 "MESH: Creating PeerInfo for destination.\n");
2085 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2086 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
2087 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
2089 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2091 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
2092 if (NULL == orig_peer_info)
2094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2095 "MESH: Creating PeerInfo for origin.\n");
2096 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2097 orig_peer_info->id = GNUNET_PEER_intern (pi);
2098 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
2099 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating path...\n");
2102 path = path_new (size);
2104 for (i = 0; i < size; i++)
2106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2107 "MESH: ... adding %s\n",
2108 GNUNET_i2s(&pi[i]));
2109 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2110 if (path->peers[i] == myid)
2113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2114 "MESH: Own position: %u\n", own_pos);
2117 /* cannot be self, must be 'not found' */
2118 /* create path: self not found in path through self */
2119 GNUNET_break_op (0);
2120 path_destroy (path);
2121 /* FIXME error. destroy tunnel? leave for timeout? */
2124 tunnel_add_path (t, path, own_pos);
2125 t->tree->me = tree_find_peer(t->tree->root, myid);
2126 if (own_pos == size - 1)
2128 /* It is for us! Send ack. */
2129 struct GNUNET_MESH_TunnelNotification cmsg;
2130 struct MeshDataDescriptor *info;
2133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2134 "MESH: It's for us!\n");
2135 path_add_to_origin (orig_peer_info, path);
2136 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
2137 info->origin = &t->id;
2138 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2139 GNUNET_assert (NULL != info->peer);
2140 j = peer_info_transmit_slot(info->peer);
2141 info->handler_n = j;
2142 info->peer->types[j] = GNUNET_MESSAGE_TYPE_MESH_PATH_ACK;
2143 info->peer->infos[j] = info;
2144 info->peer->core_transmit[j] =
2145 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
2146 GNUNET_TIME_UNIT_FOREVER_REL, peer,
2147 sizeof (struct GNUNET_MESH_PathACK),
2148 &send_core_path_ack, info);
2149 cmsg.header.size = htons(sizeof(cmsg));
2150 cmsg.header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2151 GNUNET_PEER_resolve(t->id.oid, &cmsg.peer);
2152 cmsg.tunnel_id = htonl(t->local_tid);
2153 GNUNET_SERVER_notification_context_broadcast(nc, &cmsg.header, GNUNET_NO);
2157 struct MeshPeerPath *path2;
2159 /* It's for somebody else! Retransmit. */
2160 path2 = path_duplicate(path);
2161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2162 "MESH: Retransmitting.\n");
2163 path_add_to_peer(dest_peer_info, path);
2164 path = path_duplicate(path2);
2165 path_add_to_origin(orig_peer_info, path2);
2166 send_create_path(dest_peer_info, path, t);
2173 * Core handler for mesh network traffic going from the origin to a peer
2175 * @param cls closure
2176 * @param peer peer identity this notification is about
2177 * @param message message
2178 * @param atsi performance data
2179 * @return GNUNET_OK to keep the connection open,
2180 * GNUNET_SYSERR to close it (signal serious error)
2183 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2184 const struct GNUNET_MessageHeader *message,
2185 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2187 struct GNUNET_MESH_Unicast *msg;
2188 struct MeshTunnel *t;
2192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2193 "MESH: got a unicast packet from %s\n",
2195 size = ntohs (message->size);
2197 sizeof (struct GNUNET_MESH_Unicast) +
2198 sizeof (struct GNUNET_MessageHeader))
2203 msg = (struct GNUNET_MESH_Unicast *) message;
2204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2205 "MESH: of type %u\n",
2206 ntohs (msg[1].header.type));
2207 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2210 /* TODO notify back: we don't know this tunnel */
2211 GNUNET_break_op (0);
2214 pid = GNUNET_PEER_search(&msg->destination);
2217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2218 "MESH: it's for us! sending to clients...\n");
2219 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
2222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2223 "MESH: not for us, retransmitting...\n");
2224 msg = GNUNET_malloc (size);
2225 memcpy (msg, message, size);
2226 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2227 GNUNET_TIME_UNIT_FOREVER_REL,
2228 path_get_first_hop (t->tree, pid),
2230 &send_core_data_raw, msg);
2236 * Core handler for mesh network traffic going from the origin to all peers
2238 * @param cls closure
2239 * @param message message
2240 * @param peer peer identity this notification is about
2241 * @param atsi performance data
2242 * @return GNUNET_OK to keep the connection open,
2243 * GNUNET_SYSERR to close it (signal serious error)
2245 * TODO: Check who we got this from, to validate route.
2248 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2249 const struct GNUNET_MessageHeader *message,
2250 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2252 struct GNUNET_MESH_Multicast *msg;
2253 struct GNUNET_PeerIdentity *id;
2254 struct MeshDataDescriptor *info;
2255 struct MeshTunnelTreeNode *n;
2256 struct MeshTunnel *t;
2257 unsigned int *copies;
2262 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_Multicast);
2263 if (size < sizeof (struct GNUNET_MessageHeader))
2265 GNUNET_break_op (0);
2268 msg = (struct GNUNET_MESH_Multicast *) message;
2269 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2273 /* TODO notify that we dont know that tunnel */
2274 GNUNET_break_op (0);
2278 /* Transmit to locally interested clients */
2279 if (GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
2281 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
2283 n = t->tree->me->children_head;
2286 copies = GNUNET_malloc (sizeof (unsigned int));
2287 for (*copies = 0; NULL != n; n = n->next)
2289 n = t->tree->me->children_head;
2290 data = GNUNET_malloc (size);
2291 memcpy (data, &msg[1], size);
2294 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
2295 info->origin = &t->id;
2298 info->copies = copies;
2299 info->client = t->client->handle;
2300 info->timeout_task = GNUNET_SCHEDULER_add_delayed(UNACKNOWLEDGED_WAIT,
2303 info->destination = n->peer;
2304 id = path_get_first_hop(t->tree, n->peer);
2305 info->peer = peer_info_get(id);
2306 GNUNET_assert (NULL != info->peer);
2307 for (i = 0; NULL != info->peer->core_transmit[i]; i++)
2309 if (i == (CORE_QUEUE_SIZE - 1))
2316 info->handler_n = i;
2317 info->peer->infos[i] = info;
2318 info->peer->types[i] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
2319 info->peer->core_transmit[i] =
2320 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2321 GNUNET_TIME_UNIT_FOREVER_REL, id,
2322 ntohs (msg->header.size),
2323 &send_core_data_multicast, info);
2331 * Core handler for mesh network traffic toward the owner of a tunnel
2333 * @param cls closure
2334 * @param message message
2335 * @param peer peer identity this notification is about
2336 * @param atsi performance data
2338 * @return GNUNET_OK to keep the connection open,
2339 * GNUNET_SYSERR to close it (signal serious error)
2342 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
2343 const struct GNUNET_MessageHeader *message,
2344 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2346 struct GNUNET_MESH_ToOrigin *msg;
2347 struct GNUNET_PeerIdentity id;
2348 struct MeshPeerInfo *peer_info;
2349 struct MeshTunnel *t;
2352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2353 "MESH: got a ToOrigin packet from %s\n",
2355 size = ntohs (message->size);
2356 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
2357 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
2359 GNUNET_break_op (0);
2362 msg = (struct GNUNET_MESH_ToOrigin *) message;
2363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2364 "MESH: of type %u\n",
2365 ntohs (msg[1].header.type));
2366 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2370 /* TODO notify that we dont know this tunnel (whom)? */
2371 GNUNET_break_op (0);
2375 if (t->id.oid == myid)
2378 struct GNUNET_MESH_ToOrigin *copy;
2380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2381 "MESH: it's for us! sending to clients...\n");
2382 if (NULL == t->client)
2384 /* got data packet for ownerless tunnel */
2385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2386 "MESH: no clients!\n");
2387 GNUNET_break_op (0);
2390 /* TODO signature verification */
2391 memcpy (cbuf, message, size);
2392 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
2393 copy->tid = htonl (t->local_tid);
2394 GNUNET_SERVER_notification_context_unicast (nc,
2400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2401 "MESH: not for us, retransmitting...\n");
2403 peer_info = peer_info_get (&msg->oid);
2404 if (NULL == peer_info)
2406 /* unknown origin of tunnel */
2410 GNUNET_PEER_resolve (t->tree->me->parent->peer, &id);
2411 msg = GNUNET_malloc (size);
2412 memcpy (msg, message, size);
2413 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2414 GNUNET_TIME_UNIT_FOREVER_REL, &id, size,
2415 &send_core_data_raw, msg);
2422 * Core handler for path ACKs
2424 * @param cls closure
2425 * @param message message
2426 * @param peer peer identity this notification is about
2427 * @param atsi performance data
2429 * @return GNUNET_OK to keep the connection open,
2430 * GNUNET_SYSERR to close it (signal serious error)
2433 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2434 const struct GNUNET_MessageHeader *message,
2435 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2437 struct GNUNET_MESH_PathACK *msg;
2438 struct GNUNET_PeerIdentity id;
2439 struct MeshTunnelTreeNode *n;
2440 struct MeshPeerInfo *peer_info;
2441 struct MeshTunnel *t;
2443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2444 "MESH: Received a path ACK msg [%s]\n",
2445 GNUNET_i2s(&my_full_id));
2446 msg = (struct GNUNET_MESH_PathACK *) message;
2447 t = tunnel_get (&msg->oid, msg->tid);
2450 /* TODO notify that we don't know the tunnel */
2454 /* Message for us? */
2455 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
2457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2458 "MESH: It's for us!\n");
2459 if (NULL == t->client)
2461 GNUNET_break_op (0);
2464 peer_info = peer_info_get (&msg->peer_id);
2465 if (NULL == peer_info)
2467 GNUNET_break_op (0);
2470 n = tree_find_peer(t->tree->root, peer_info->id);
2473 GNUNET_break_op (0);
2476 n->status = MESH_PEER_READY;
2477 send_client_peer_connected(t, peer_info->id);
2481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2482 "MESH: not for us, retransmitting...\n");
2483 GNUNET_PEER_resolve(t->tree->me->parent->peer, &id);
2484 peer_info = peer_info_get (&msg->oid);
2485 if (NULL == peer_info)
2487 /* If we know the tunnel, we should DEFINITELY know the peer */
2491 msg = GNUNET_malloc (sizeof (struct GNUNET_MESH_PathACK));
2492 memcpy (msg, message, sizeof (struct GNUNET_MESH_PathACK));
2493 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2494 GNUNET_TIME_UNIT_FOREVER_REL,
2496 sizeof (struct GNUNET_MESH_PathACK),
2497 &send_core_data_raw, msg);
2503 * Functions to handle messages from core
2505 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
2506 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
2507 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
2508 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
2509 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
2510 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2511 sizeof (struct GNUNET_MESH_PathACK)},
2517 /******************************************************************************/
2518 /**************** MESH LOCAL HANDLER HELPERS ***********************/
2519 /******************************************************************************/
2522 * deregister_app: iterator for removing each application registered by a client
2524 * @param cls closure
2525 * @param key the hash of the application id (used to access the hashmap)
2526 * @param value the value stored at the key (client)
2528 * @return GNUNET_OK on success
2531 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
2533 GNUNET_break (GNUNET_YES ==
2534 GNUNET_CONTAINER_multihashmap_remove (applications, key, value));
2540 * notify_client_connection_failure: notify a client that the connection to the
2541 * requested remote peer is not possible (for instance, no route found)
2542 * Function called when the socket is ready to queue more data. "buf" will be
2543 * NULL and "size" zero if the socket was closed for writing in the meantime.
2545 * @param cls closure
2546 * @param size number of bytes available in buf
2547 * @param buf where the callee should write the message
2548 * @return number of bytes written to buf
2551 notify_client_connection_failure (void *cls, size_t size, void *buf)
2554 struct MeshPeerInfo *peer_info;
2555 struct GNUNET_MESH_PeerControl *msg;
2556 struct GNUNET_PeerIdentity id;
2558 if (0 == size && NULL == buf)
2560 // TODO retry? cancel?
2564 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
2565 peer_info = (struct MeshPeerInfo *) cls;
2566 msg = (struct GNUNET_MESH_PeerControl *) buf;
2567 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2568 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
2569 // msg->tunnel_id = htonl(peer_info->t->tid);
2570 GNUNET_PEER_resolve (peer_info->id, &id);
2571 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
2579 * Send keepalive packets for a peer
2581 * @param cls Closure (tunnel for which to send the keepalive).
2582 * @param tc Notification context.
2584 * TODO: implement explicit multicast keepalive?
2587 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2589 struct MeshTunnel *t = cls;
2590 struct GNUNET_MessageHeader *payload;
2591 struct GNUNET_MESH_Multicast *msg;
2594 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
2595 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2601 "MESH: sending keepalive for tunnel %d\n",
2604 size = sizeof(struct GNUNET_MESH_Multicast) +
2605 sizeof(struct GNUNET_MessageHeader);
2606 msg = GNUNET_malloc (size);
2607 msg->header.size = htons (size);
2608 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
2609 msg->oid = my_full_id;
2610 msg->tid = htonl(t->id.tid);
2611 payload = (struct GNUNET_MessageHeader *) &msg[1];
2612 payload->size = htons (sizeof(struct GNUNET_MessageHeader));
2613 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2614 handle_mesh_data_multicast (NULL, &my_full_id, &msg->header, NULL);
2617 t->path_refresh_task =
2618 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
2624 * Function to process paths received for a new peer addition. The recorded
2625 * paths form the initial tunnel, which can be optimized later.
2626 * Called on each result obtained for the DHT search.
2628 * @param cls closure
2629 * @param exp when will this value expire
2630 * @param key key of the result
2631 * @param type type of the result
2632 * @param size number of bytes in data
2633 * @param data pointer to the result data
2635 * TODO: re-issue the request after certain time? cancel after X results?
2638 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2639 const GNUNET_HashCode * key,
2640 const struct GNUNET_PeerIdentity *get_path,
2641 unsigned int get_path_length,
2642 const struct GNUNET_PeerIdentity *put_path,
2643 unsigned int put_path_length,
2644 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
2646 struct MeshPathInfo *path_info = cls;
2647 struct MeshPeerPath *p;
2648 struct GNUNET_PeerIdentity pi;
2651 GNUNET_PEER_resolve (path_info->peer->id, &pi);
2652 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2653 "MESH: Got results from DHT for %s\n",
2654 GNUNET_h2s_full(&pi.hashPubKey));
2655 GNUNET_DHT_get_stop(path_info->peer->dhtget);
2656 path_info->peer->dhtget = NULL;
2658 p = path_build_from_dht (get_path, get_path_length,
2659 put_path, put_path_length);
2660 path_add_to_peer (path_info->peer, p);
2661 for (i = 0; i < path_info->peer->ntunnels; i++)
2663 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
2664 peer_info_connect(path_info->peer, path_info->t);
2666 GNUNET_free (path_info);
2673 * Function to process paths received for a new peer addition. The recorded
2674 * paths form the initial tunnel, which can be optimized later.
2675 * Called on each result obtained for the DHT search.
2677 * @param cls closure
2678 * @param exp when will this value expire
2679 * @param key key of the result
2680 * @param type type of the result
2681 * @param size number of bytes in data
2682 * @param data pointer to the result data
2685 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2686 const GNUNET_HashCode * key,
2687 const struct GNUNET_PeerIdentity *get_path,
2688 unsigned int get_path_length,
2689 const struct GNUNET_PeerIdentity *put_path,
2690 unsigned int put_path_length,
2691 enum GNUNET_BLOCK_Type type, size_t size,
2694 const struct GNUNET_PeerIdentity *pi = data;
2695 struct GNUNET_PeerIdentity id;
2696 struct MeshTunnel *t = cls;
2697 struct MeshPeerInfo *peer_info;
2698 struct MeshPathInfo *path_info;
2699 struct MeshPeerPath *p;
2702 if (size != sizeof (struct GNUNET_PeerIdentity))
2704 GNUNET_break_op (0);
2707 GNUNET_assert (NULL != t->client);
2708 GNUNET_DHT_get_stop (t->dht_get_type);
2709 t->dht_get_type = NULL;
2710 peer_info = peer_info_get (pi);
2711 GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey, peer_info,
2712 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2714 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path_head &&
2715 NULL == peer_info->dhtget)
2717 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2718 path_info->peer = peer_info;
2720 /* we don't have a route to the peer, let's try a direct lookup */
2722 GNUNET_DHT_get_start (dht_handle, /* handle */
2723 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
2724 GNUNET_BLOCK_TYPE_TEST, /* block type */
2725 &pi->hashPubKey, /* key to look up */
2726 10U, /* replication level */
2727 GNUNET_DHT_RO_RECORD_ROUTE |
2728 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2729 /* option to dht: record route */
2731 0, /* xquery bits */
2732 dht_get_id_handler, /* callback */
2733 path_info); /* closure */
2737 p = path_build_from_dht (get_path, get_path_length, put_path, put_path_length);
2738 path_add_to_peer (peer_info, p);
2739 tunnel_add_peer(t, peer_info);
2740 p = tree_get_path_to_peer(t->tree, peer_info->id);
2742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2743 "MESH: new route for tunnel 0x%x found, has %u hops\n",
2744 t->local_tid, p->length);
2745 for (i = 0; i < p->length; i++)
2747 GNUNET_PEER_resolve (p->peers[0], &id);
2748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:\t%d\t%s\n", i,
2749 GNUNET_h2s_full (&id.hashPubKey));
2755 send_create_path(peer_info, p, t);
2759 send_client_peer_connected(t, myid);
2763 /******************************************************************************/
2764 /********************* MESH LOCAL HANDLES **************************/
2765 /******************************************************************************/
2769 * Handler for client disconnection
2771 * @param cls closure
2772 * @param client identification of the client; NULL
2773 * for the last call when the server is destroyed
2776 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
2778 struct MeshClient *c;
2779 struct MeshClient *next;
2781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
2783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
2787 if (c->handle != client && NULL != client)
2789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
2793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
2794 if (NULL != c->tunnels)
2796 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
2797 &tunnel_destroy_iterator,
2799 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
2802 /* deregister clients applications */
2803 if (NULL != c->apps)
2805 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
2806 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
2808 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
2809 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
2811 GNUNET_SCHEDULER_cancel (announce_applications_task);
2812 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
2814 if (NULL != c->types)
2815 GNUNET_CONTAINER_multihashmap_destroy (c->types);
2816 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
2822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
2828 * Handler for new clients
2830 * @param cls closure
2831 * @param client identification of the client
2832 * @param message the actual message, which includes messages the client wants
2835 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
2836 const struct GNUNET_MessageHeader *message)
2838 struct GNUNET_MESH_ClientConnect *cc_msg;
2839 struct MeshClient *c;
2840 GNUNET_MESH_ApplicationType *a;
2847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
2848 /* Check data sanity */
2849 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
2850 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
2851 ntypes = ntohs (cc_msg->types);
2852 napps = ntohs (cc_msg->applications);
2854 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
2857 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2861 /* Create new client structure */
2862 c = GNUNET_malloc (sizeof (struct MeshClient));
2864 c->id = next_client_id++;
2867 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
2870 GNUNET_MESH_ApplicationType at;
2873 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
2874 for (i = 0; i < napps; i++)
2877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
2878 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
2879 /* store in clients hashmap */
2880 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
2881 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2882 /* store in global hashmap, for announcements */
2883 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
2884 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2886 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
2887 announce_applications_task =
2888 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
2896 t = (uint16_t *) & a[napps];
2897 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
2898 for (i = 0; i < ntypes; i++)
2901 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
2903 /* store in clients hashmap */
2904 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
2905 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2906 /* store in global hashmap */
2907 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
2908 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2912 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
2914 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
2915 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
2916 GNUNET_SERVER_notification_context_add (nc, client);
2918 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
2926 * Handler for requests of new tunnels
2928 * @param cls closure
2929 * @param client identification of the client
2930 * @param message the actual message
2933 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
2934 const struct GNUNET_MessageHeader *message)
2936 struct GNUNET_MESH_TunnelMessage *t_msg;
2937 struct MeshTunnel *t;
2938 struct MeshClient *c;
2939 GNUNET_HashCode hash;
2941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
2943 /* Sanity check for client registration */
2944 if (NULL == (c = client_get (client)))
2947 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2954 /* Message sanity check */
2955 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2958 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2962 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2963 /* Sanity check for tunnel numbering */
2964 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
2967 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2970 /* Sanity check for duplicate tunnel IDs */
2971 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
2974 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2978 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATED TUNNEL at %p\n", t);
2980 while (NULL != tunnel_get_by_pi (myid, next_tid))
2981 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
2982 t->id.tid = next_tid++;
2984 t->local_tid = ntohl (t_msg->tunnel_id);
2986 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
2988 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2990 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
2991 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2994 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2998 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3000 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3001 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3004 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3007 t->tree = tree_new (t, myid);
3008 t->tree->refresh = REFRESH_PATH_TIME;
3009 t->tree->root->status = MESH_PEER_READY;
3010 t->tree->me = t->tree->root;
3012 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3018 * Handler for requests of deleting tunnels
3020 * @param cls closure
3021 * @param client identification of the client
3022 * @param message the actual message
3025 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
3026 const struct GNUNET_MessageHeader *message)
3028 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
3029 struct MeshClient *c;
3030 struct MeshTunnel *t;
3031 MESH_TunnelNumber tid;
3032 GNUNET_HashCode hash;
3034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel\n");
3036 /* Sanity check for client registration */
3037 if (NULL == (c = client_get (client)))
3040 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3043 /* Message sanity check */
3044 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3047 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3053 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3055 /* Retrieve tunnel */
3056 tid = ntohl (tunnel_msg->tunnel_id);
3058 /* Remove from local id hashmap */
3059 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3060 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
3061 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t);
3063 // notify_tunnel_destroy(t); FIXME
3065 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3071 * Handler for connection requests to new peers
3073 * @param cls closure
3074 * @param client identification of the client
3075 * @param message the actual message (PeerControl)
3078 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
3079 const struct GNUNET_MessageHeader *message)
3081 struct GNUNET_MESH_PeerControl *peer_msg;
3082 struct MeshPeerInfo *peer_info;
3083 struct MeshClient *c;
3084 struct MeshTunnel *t;
3085 MESH_TunnelNumber tid;
3087 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Got connection request\n");
3088 /* Sanity check for client registration */
3089 if (NULL == (c = client_get (client)))
3092 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3096 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3097 /* Sanity check for message size */
3098 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3101 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3105 /* Tunnel exists? */
3106 tid = ntohl (peer_msg->tunnel_id);
3107 t = tunnel_get_by_local_id (c, tid);
3111 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3115 /* Does client own tunnel? */
3116 if (t->client->handle != client)
3119 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3122 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: for %s\n",
3123 GNUNET_h2s_full(&peer_msg->peer.hashPubKey));
3124 peer_info = peer_info_get (&peer_msg->peer);
3126 tunnel_add_peer(t, peer_info);
3127 peer_info_connect(peer_info, t);
3129 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3135 * Handler for disconnection requests of peers in a tunnel
3137 * @param cls closure
3138 * @param client identification of the client
3139 * @param message the actual message (PeerControl)
3142 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
3143 const struct GNUNET_MessageHeader *message)
3145 struct GNUNET_MESH_PeerControl *peer_msg;
3146 struct MeshClient *c;
3147 struct MeshTunnel *t;
3148 MESH_TunnelNumber tid;
3150 /* Sanity check for client registration */
3151 if (NULL == (c = client_get (client)))
3154 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3157 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3158 /* Sanity check for message size */
3159 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3162 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3166 /* Tunnel exists? */
3167 tid = ntohl (peer_msg->tunnel_id);
3168 t = tunnel_get_by_local_id (c, tid);
3172 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3176 /* Does client own tunnel? */
3177 if (t->client->handle != client)
3180 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3184 /* Ok, delete peer from tunnel */
3185 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
3186 &peer_msg->peer.hashPubKey);
3188 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3194 * Handler for connection requests to new peers by type
3196 * @param cls closure
3197 * @param client identification of the client
3198 * @param message the actual message (ConnectPeerByType)
3201 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
3202 const struct GNUNET_MessageHeader *message)
3204 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
3205 struct MeshClient *c;
3206 struct MeshTunnel *t;
3207 GNUNET_HashCode hash;
3208 MESH_TunnelNumber tid;
3210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
3211 /* Sanity check for client registration */
3212 if (NULL == (c = client_get (client)))
3215 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3219 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
3220 /* Sanity check for message size */
3221 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
3222 ntohs (connect_msg->header.size))
3225 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3229 /* Tunnel exists? */
3230 tid = ntohl (connect_msg->tunnel_id);
3231 t = tunnel_get_by_local_id (c, tid);
3235 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3239 /* Does client own tunnel? */
3240 if (t->client->handle != client)
3243 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3247 /* Do WE have the service? */
3248 t->type = ntohl (connect_msg->type);
3249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", t->type);
3250 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
3251 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
3254 /* Yes! Fast forward, add ourselves to the tunnel and send the
3255 * good news to the client
3257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
3258 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
3259 peer_info_get (&my_full_id),
3260 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
3263 send_client_peer_connected(t, myid);
3264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
3265 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3268 /* Ok, lets find a peer offering the service */
3269 if (NULL != t->dht_get_type)
3271 GNUNET_DHT_get_stop (t->dht_get_type);
3273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
3274 GNUNET_h2s_full (&hash));
3276 GNUNET_DHT_get_start (dht_handle,
3277 GNUNET_TIME_UNIT_FOREVER_REL,
3278 GNUNET_BLOCK_TYPE_TEST,
3281 GNUNET_DHT_RO_RECORD_ROUTE |
3282 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
3284 &dht_get_type_handler, t);
3286 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3292 * Handler for client traffic directed to one peer
3294 * @param cls closure
3295 * @param client identification of the client
3296 * @param message the actual message
3299 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
3300 const struct GNUNET_MessageHeader *message)
3302 struct MeshClient *c;
3303 struct MeshTunnel *t;
3304 struct MeshPeerInfo *pi;
3305 struct GNUNET_MESH_Unicast *data_msg;
3306 MESH_TunnelNumber tid;
3309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3310 "MESH: Got a unicast request from a client!\n");
3312 /* Sanity check for client registration */
3313 if (NULL == (c = client_get (client)))
3316 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3319 data_msg = (struct GNUNET_MESH_Unicast *) message;
3320 /* Sanity check for message size */
3321 size = ntohs (message->size);
3322 if (sizeof (struct GNUNET_MESH_Unicast) +
3323 sizeof (struct GNUNET_MessageHeader) > size)
3326 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3330 /* Tunnel exists? */
3331 tid = ntohl (data_msg->tid);
3332 t = tunnel_get_by_local_id (c, tid);
3336 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3340 /* Is it a local tunnel? Then, does client own the tunnel? */
3341 if (NULL != t->client &&
3342 NULL != t->client->handle &&
3343 t->client->handle != client)
3346 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3350 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
3351 &data_msg->destination.hashPubKey);
3352 /* Is the selected peer in the tunnel? */
3356 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3360 /* Ok, everything is correct, send the message
3361 * (pretend we got it from a mesh peer)
3364 char buf[ntohs (message->size)];
3365 struct GNUNET_MESH_Unicast *copy;
3367 /* Work around const limitation */
3368 copy = (struct GNUNET_MESH_Unicast *) buf;
3369 memcpy (buf, data_msg, size);
3370 copy->oid = my_full_id;
3371 copy->tid = htonl (t->id.tid);
3372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3373 "MESH: calling generic handler...\n");
3374 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL);
3376 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3382 * Handler for client traffic directed to the origin
3384 * @param cls closure
3385 * @param client identification of the client
3386 * @param message the actual message
3389 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
3390 const struct GNUNET_MessageHeader *message)
3392 struct GNUNET_MESH_ToOrigin *data_msg;
3393 struct GNUNET_PeerIdentity id;
3394 struct MeshClient *c;
3395 struct MeshTunnel *t;
3396 MESH_TunnelNumber tid;
3399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3400 "MESH: Got a ToOrigin request from a client!\n");
3402 /* Sanity check for client registration */
3403 if (NULL == (c = client_get (client)))
3406 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3409 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
3410 /* Sanity check for message size */
3411 size = ntohs (message->size);
3412 if (sizeof (struct GNUNET_MESH_ToOrigin) +
3413 sizeof (struct GNUNET_MessageHeader) > size)
3416 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3420 /* Tunnel exists? */
3421 tid = ntohl (data_msg->tid);
3422 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3425 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3428 t = tunnel_get_by_local_id (c, tid);
3432 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3436 /* It shouldn't be a local tunnel. */
3437 if (NULL != t->client)
3440 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3443 GNUNET_PEER_resolve(t->id.oid, &id);
3445 /* Ok, everything is correct, send the message
3446 * (pretend we got it from a mesh peer)
3449 char buf[ntohs (message->size)];
3450 struct GNUNET_MESH_ToOrigin *copy;
3452 /* Work around const limitation */
3453 copy = (struct GNUNET_MESH_ToOrigin *) buf;
3454 memcpy (buf, data_msg, size);
3456 copy->tid = htonl (t->id.tid);
3457 copy->sender = my_full_id;
3458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3459 "MESH: calling generic handler...\n");
3460 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL);
3462 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3468 * Handler for client traffic directed to all peers in a tunnel
3470 * @param cls closure
3471 * @param client identification of the client
3472 * @param message the actual message
3475 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
3476 const struct GNUNET_MessageHeader *message)
3478 struct MeshClient *c;
3479 struct MeshTunnel *t;
3480 struct GNUNET_MESH_Multicast *data_msg;
3481 MESH_TunnelNumber tid;
3483 /* Sanity check for client registration */
3484 if (NULL == (c = client_get (client)))
3487 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3490 data_msg = (struct GNUNET_MESH_Multicast *) message;
3491 /* Sanity check for message size */
3492 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (data_msg->header.size))
3495 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3499 /* Tunnel exists? */
3500 tid = ntohl (data_msg->tid);
3501 t = tunnel_get_by_local_id (c, tid);
3505 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3509 /* Does client own tunnel? */
3510 if (t->client->handle != client)
3513 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3518 char buf[ntohs(message->size)];
3519 struct GNUNET_MESH_Multicast *copy;
3521 copy = (struct GNUNET_MESH_Multicast *)buf;
3522 memcpy(buf, message, ntohs(message->size));
3523 copy->oid = my_full_id;
3524 copy->tid = htonl(t->id.tid);
3525 handle_mesh_data_multicast(client, &my_full_id, ©->header, NULL);
3528 /* receive done gets called when last copy is sent */
3533 * Functions to handle messages from clients
3535 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
3536 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
3537 {&handle_local_tunnel_create, NULL,
3538 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
3539 sizeof (struct GNUNET_MESH_TunnelMessage)},
3540 {&handle_local_tunnel_destroy, NULL,
3541 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
3542 sizeof (struct GNUNET_MESH_TunnelMessage)},
3543 {&handle_local_connect_add, NULL,
3544 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
3545 sizeof (struct GNUNET_MESH_PeerControl)},
3546 {&handle_local_connect_del, NULL,
3547 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
3548 sizeof (struct GNUNET_MESH_PeerControl)},
3549 {&handle_local_connect_by_type, NULL,
3550 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
3551 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
3552 {&handle_local_unicast, NULL,
3553 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3554 {&handle_local_to_origin, NULL,
3555 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3556 {&handle_local_multicast, NULL,
3557 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3563 * To be called on core init/fail.
3565 * @param cls service closure
3566 * @param server handle to the server for this service
3567 * @param identity the public identity of this peer
3570 core_init (void *cls, struct GNUNET_CORE_Handle *server,
3571 const struct GNUNET_PeerIdentity *identity)
3573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
3574 core_handle = server;
3575 if (0 != memcmp(identity, &my_full_id, sizeof(my_full_id)) || NULL == server)
3577 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
3578 GNUNET_SCHEDULER_shutdown();
3584 * Method called whenever a given peer connects.
3586 * @param cls closure
3587 * @param peer peer identity this notification is about
3588 * @param atsi performance data for the connection
3591 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
3592 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
3594 struct MeshPeerInfo *peer_info;
3595 struct MeshPeerPath *path;
3597 #if MESH_DEBUG_CONNECTION
3598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
3599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
3600 GNUNET_h2s(&my_full_id.hashPubKey));
3602 peer_info = peer_info_get (peer);
3603 if (myid == peer_info->id)
3605 #if MESH_DEBUG_CONNECTION
3606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3610 #if MESH_DEBUG_CONNECTION
3613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
3614 GNUNET_h2s(&peer->hashPubKey));
3617 path = path_new (2);
3618 path->peers[0] = myid;
3619 path->peers[1] = peer_info->id;
3620 GNUNET_PEER_change_rc(myid, 1);
3621 GNUNET_PEER_change_rc(peer_info->id, 1);
3622 path_add_to_peer (peer_info, path);
3627 * Method called whenever a peer disconnects.
3629 * @param cls closure
3630 * @param peer peer identity this notification is about
3633 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
3635 struct MeshPeerInfo *pi;
3638 #if MESH_DEBUG_CONNECTION
3639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
3641 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
3647 for (i = 0; i < CORE_QUEUE_SIZE; i++)
3649 peer_info_cancel_transmission(pi, i);
3651 path_remove_from_peer (pi, pi->id, myid);
3652 #if MESH_DEBUG_CONNECTION
3655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3662 /******************************************************************************/
3663 /************************ MAIN FUNCTIONS ****************************/
3664 /******************************************************************************/
3667 * Iterator over hash map entries.
3669 * @param cls closure
3670 * @param key current key code
3671 * @param value value in the hash map
3672 * @return GNUNET_YES if we should continue to
3677 shutdown_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
3679 struct MeshTunnel *t = value;
3685 * Task run during shutdown.
3691 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
3694 /* TODO: destroy tunnels? */
3695 if (core_handle != NULL)
3697 GNUNET_CORE_disconnect (core_handle);
3700 GNUNET_CONTAINER_multihashmap_iterate(tunnels, &shutdown_tunnel, NULL);
3701 if (dht_handle != NULL)
3703 GNUNET_DHT_disconnect (dht_handle);
3708 GNUNET_SERVER_notification_context_destroy (nc);
3711 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
3713 GNUNET_SCHEDULER_cancel (announce_id_task);
3714 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
3716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
3720 * Process mesh requests.
3722 * @param cls closure
3723 * @param server the initialized server
3724 * @param c configuration to use
3727 run (void *cls, struct GNUNET_SERVER_Handle *server,
3728 const struct GNUNET_CONFIGURATION_Handle *c)
3730 struct MeshPeerInfo *peer;
3731 struct MeshPeerPath *p;
3734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
3735 server_handle = server;
3736 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
3737 CORE_QUEUE_SIZE, /* queue size */
3738 NULL, /* Closure passed to MESH functions */
3739 &core_init, /* Call core_init once connected */
3740 &core_connect, /* Handle connects */
3741 &core_disconnect, /* remove peers on disconnects */
3742 NULL, /* Don't notify about all incoming messages */
3743 GNUNET_NO, /* For header only in notification */
3744 NULL, /* Don't notify about all outbound messages */
3745 GNUNET_NO, /* For header-only out notification */
3746 core_handlers); /* Register these handlers */
3747 if (core_handle == NULL)
3750 GNUNET_SCHEDULER_shutdown ();
3755 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
3758 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3760 ("Mesh service is lacking key configuration settings. Exiting.\n"));
3761 GNUNET_SCHEDULER_shutdown ();
3764 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3765 GNUNET_free (keyfile);
3766 if (my_private_key == NULL)
3768 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3769 _("Mesh service could not access hostkey. Exiting.\n"));
3770 GNUNET_SCHEDULER_shutdown ();
3773 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
3774 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
3775 &my_full_id.hashPubKey);
3776 myid = GNUNET_PEER_intern (&my_full_id);
3778 dht_handle = GNUNET_DHT_connect (c, 64);
3779 if (dht_handle == NULL)
3785 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3787 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3788 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3789 peers = GNUNET_CONTAINER_multihashmap_create (32);
3790 applications = GNUNET_CONTAINER_multihashmap_create (32);
3791 types = GNUNET_CONTAINER_multihashmap_create (32);
3793 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
3794 nc = GNUNET_SERVER_notification_context_create (server_handle,
3796 GNUNET_SERVER_disconnect_notify (server_handle,
3797 &handle_local_client_disconnect,
3802 clients_tail = NULL;
3807 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3808 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
3810 /* Create a peer_info for the local peer */
3811 peer = peer_info_get(&my_full_id);
3814 path_add_to_peer(peer, p);
3816 /* Scheduled the task to clean up when shutdown is called */
3817 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
3820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
3824 * The main function for the mesh service.
3826 * @param argc number of arguments from the command line
3827 * @param argv command line arguments
3828 * @return 0 ok, 1 on error
3831 main (int argc, char *const *argv)
3836 // fprintf (stderr, "main ()\n");
3838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
3841 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
3843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");
3845 // fprintf (stderr, "main () END\n");