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 * - relay corking down to core
44 * - set ttl relative to tree depth
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,\
72 #define DEFAULT_TTL 64
74 #define MESH_DEBUG_DHT GNUNET_NO
78 /******************************************************************************/
79 /************************ DATA STRUCTURES ****************************/
80 /******************************************************************************/
82 /** FWD declaration */
87 * Struct representing a piece of data being sent to other peers
91 /** Tunnel it belongs to. */
94 /** In case of a multicast, task to allow a client to send more data if
95 * some neighbor is too slow. */
96 GNUNET_SCHEDULER_TaskIdentifier *task;
98 /** How many remaining neighbors we need to send this to. */
99 unsigned int *reference_counter;
101 /** Size of the data. */
110 * Struct containing all info possibly needed to build a package when called
113 struct MeshTransmissionDescriptor
115 /** ID of the tunnel this packet travels in */
116 struct MESH_TunnelID *origin;
118 /** Who was this message being sent to */
119 struct MeshPeerInfo *peer;
121 /** Ultimate destination of the packet */
122 GNUNET_PEER_Id destination;
124 /** Which handler was used to request the transmission */
125 unsigned int handler_n;
127 /** Data descriptor */
128 struct MeshData* mesh_data;
133 * Struct containing all information regarding a given peer
143 * Last time we heard from this peer
145 struct GNUNET_TIME_Absolute last_contact;
148 * Number of attempts to reconnect so far
150 int n_reconnect_attempts;
153 * Paths to reach the peer, ordered by ascending hop count
155 struct MeshPeerPath *path_head;
158 * Paths to reach the peer, ordered by ascending hop count
160 struct MeshPeerPath *path_tail;
163 * Handle to stop the DHT search for a path to this peer
165 struct GNUNET_DHT_GetHandle *dhtget;
168 * Closure given to the DHT GET
170 struct MeshPathInfo *dhtgetcls;
173 * Handles to stop queued transmissions for this peer
175 struct GNUNET_CORE_TransmitHandle *core_transmit[CORE_QUEUE_SIZE];
178 * Pointer to info stuctures used as cls for queued transmissions
180 void *infos[CORE_QUEUE_SIZE];
183 * Type of message being in each transmission
185 uint16_t types[CORE_QUEUE_SIZE];
188 * Array of tunnels this peer participates in
189 * (most probably a small amount, therefore not a hashmap)
190 * When the path to the peer changes, notify these tunnels to let them
191 * re-adjust their path trees.
193 struct MeshTunnel **tunnels;
196 * Number of tunnels this peers participates in
198 unsigned int ntunnels;
203 * Data scheduled to transmit (to local client or remote peer)
210 struct MeshQueue *next;
211 struct MeshQueue *prev;
214 * Target of the data (NULL if target is client)
216 struct MeshPeerInfo *peer;
219 * Client to send the data to (NULL if target is peer)
221 struct MeshClient *client;
224 * Size of the message to transmit
229 * How old is the data?
231 struct GNUNET_TIME_Absolute timestamp;
236 struct GNUNET_MessageHeader *data;
240 * Globally unique tunnel identification (owner + number)
241 * DO NOT USE OVER THE NETWORK
246 * Node that owns the tunnel
251 * Tunnel number to differentiate all the tunnels owned by the node oid
252 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
254 MESH_TunnelNumber tid;
258 struct MeshClient; /* FWD declaration */
261 * Struct containing all information regarding a tunnel
262 * For an intermediate node the improtant info used will be:
263 * - id Tunnel unique identification
264 * - paths[0] To know where to send it next
265 * - metainfo: ready, speeds, accounting
272 struct MESH_TunnelID id;
275 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
277 MESH_TunnelNumber local_tid;
280 * Local tunnel number for local destination clients
282 MESH_TunnelNumber local_tid_dest;
285 * ID of the last multicast packet seen/sent.
290 * Last time the tunnel was used
292 struct GNUNET_TIME_Absolute timestamp;
295 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
296 * containing peers added by id or by type, not intermediate peers.
298 struct GNUNET_CONTAINER_MultiHashMap *peers;
301 * Number of peers that are connected and potentially ready to receive data
303 unsigned int peers_ready;
306 * Number of peers that have been added to the tunnel
308 unsigned int peers_total;
311 * Client owner of the tunnel, if any
313 struct MeshClient *client;
316 * Messages ready to transmit
318 struct MeshQueue *queue_head;
319 struct MeshQueue *queue_tail;
324 struct MeshTunnelTree *tree;
327 * Application type we are looking for in this tunnel
329 GNUNET_MESH_ApplicationType type;
332 * Used to search peers offering a service
334 struct GNUNET_DHT_GetHandle *dht_get_type;
337 * Task to keep the used paths alive
339 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
342 * Task to destroy the tunnel after timeout
344 * FIXME: merge the two? a tunnel will have either
345 * a path refresh OR a timeout, never both!
347 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
352 * Info needed to work with tunnel paths and peers
359 struct MeshTunnel *t;
362 * Neighbouring peer to whom we send the packet to
364 struct MeshPeerInfo *peer;
369 struct MeshPeerPath *path;
372 * Position in peer's transmit queue
379 * Struct containing information about a client of the service
386 struct MeshClient *next;
387 struct MeshClient *prev;
390 * Tunnels that belong to this client, indexed by local id
392 struct GNUNET_CONTAINER_MultiHashMap *tunnels;
395 * Handle to communicate with the client
397 struct GNUNET_SERVER_Client *handle;
400 * Applications that this client has claimed to provide
402 struct GNUNET_CONTAINER_MultiHashMap *apps;
405 * Messages that this client has declared interest in
407 struct GNUNET_CONTAINER_MultiHashMap *types;
411 * ID of the client, for debug messages
420 /******************************************************************************/
421 /************************ DEBUG FUNCTIONS ****************************/
422 /******************************************************************************/
426 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
427 * @param cls string to print
428 * @param tc task context
431 mesh_debug (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
435 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n", s);
443 /******************************************************************************/
444 /*********************** GLOBAL VARIABLES ****************************/
445 /******************************************************************************/
450 static struct MeshClient *clients;
451 static struct MeshClient *clients_tail;
454 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
456 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
459 * Tunnels incoming, indexed by MESH_TunnelNumber
460 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
462 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
465 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
467 static struct GNUNET_CONTAINER_MultiHashMap *peers;
470 * Handle to communicate with core
472 static struct GNUNET_CORE_Handle *core_handle;
475 * Handle to communicate with transport
477 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
482 static struct GNUNET_DHT_Handle *dht_handle;
487 static struct GNUNET_SERVER_Handle *server_handle;
490 * Notification context, to send messages to local clients
492 static struct GNUNET_SERVER_NotificationContext *nc;
495 * Local peer own ID (memory efficient handle)
497 static GNUNET_PEER_Id myid;
500 * Local peer own ID (full value)
502 static struct GNUNET_PeerIdentity my_full_id;
507 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
512 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
515 * Tunnel ID for the next created tunnel (global tunnel number)
517 static MESH_TunnelNumber next_tid;
520 * Tunnel ID for the next incoming tunnel (local tunnel number)
522 static MESH_TunnelNumber next_local_tid;
525 * All application types provided by this peer
527 static struct GNUNET_CONTAINER_MultiHashMap *applications;
530 * All message types clients of this peer are interested in
532 static struct GNUNET_CONTAINER_MultiHashMap *types;
535 * Task to periodically announce provided applications
537 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
540 * Task to periodically announce itself in the network
542 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
545 unsigned int next_client_id;
549 /******************************************************************************/
550 /************************ ITERATORS ****************************/
551 /******************************************************************************/
553 /* FIXME move iterators here */
556 /******************************************************************************/
557 /************************ PERIODIC FUNCTIONS ****************************/
558 /******************************************************************************/
561 * Announce iterator over for each application provided by the peer
564 * @param key current key code
565 * @param value value in the hash map
566 * @return GNUNET_YES if we should continue to
571 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
573 /* FIXME are hashes in multihash map equal on all aquitectures? */
574 GNUNET_DHT_put (dht_handle, key, 10U,
575 GNUNET_DHT_RO_RECORD_ROUTE |
576 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, GNUNET_BLOCK_TYPE_TEST,
577 sizeof (struct GNUNET_PeerIdentity),
578 (const char *) &my_full_id,
580 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
581 &mesh_debug, "DHT_put for app completed");
583 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
585 APP_ANNOUNCE_TIME, NULL, NULL);
592 * Periodically announce what applications are provided by local clients
595 * @param tc task context
598 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
600 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
602 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Starting PUT for apps\n");
608 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
610 announce_applications_task =
611 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Finished PUT for apps\n");
621 * Periodically announce self id in the DHT
624 * @param tc task context
627 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
629 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
631 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
635 * - Set data expiration in function of X
639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DHT_put for ID %s started.\n",
640 GNUNET_i2s (&my_full_id));
642 GNUNET_DHT_put (dht_handle, /* DHT handle */
643 &my_full_id.hashPubKey, /* Key to use */
644 10U, /* Replication level */
645 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
646 GNUNET_BLOCK_TYPE_TEST, /* Block type */
647 sizeof (my_full_id), /* Size of the data */
648 (char *) &my_full_id, /* Data itself */
649 GNUNET_TIME_absolute_get_forever (), /* Data expiration */
650 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
652 &mesh_debug, "DHT_put for id completed");
654 NULL, /* Continuation */
655 NULL); /* Continuation closure */
658 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
663 * Function to process paths received for a new peer addition. The recorded
664 * paths form the initial tunnel, which can be optimized later.
665 * Called on each result obtained for the DHT search.
668 * @param exp when will this value expire
669 * @param key key of the result
670 * @param type type of the result
671 * @param size number of bytes in data
672 * @param data pointer to the result data
675 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
676 const GNUNET_HashCode * key,
677 const struct GNUNET_PeerIdentity *get_path,
678 unsigned int get_path_length,
679 const struct GNUNET_PeerIdentity *put_path,
680 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
681 size_t size, const void *data);
684 /******************************************************************************/
685 /****************** GENERAL HELPER FUNCTIONS ************************/
686 /******************************************************************************/
689 * Check if client has registered with the service and has not disconnected
691 * @param client the client to check
693 * @return non-NULL if client exists in the global DLL
695 static struct MeshClient *
696 client_get (struct GNUNET_SERVER_Client *client)
698 struct MeshClient *c;
703 if (c->handle == client)
712 * Checks if a given client has subscribed to certain message type
714 * @param message_type Type of message to check
715 * @param c Client to check
717 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
722 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
726 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
727 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
732 * Allow a client to send more data after transmitting a multicast message
733 * which some neighbor has not yet accepted altough a reasonable time has
736 * @param cls Closure (DataDescriptor containing the task identifier)
737 * @param tc Task Context
740 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
742 struct MeshData *mdata = cls;
744 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
747 GNUNET_assert (NULL != mdata->reference_counter);
748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
749 "MESH: CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
750 mdata->reference_counter);
752 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
753 GNUNET_SERVER_receive_done (mdata->t->client->handle, GNUNET_OK);
758 * Search for a tunnel by global ID using full PeerIdentities
760 * @param oid owner of the tunnel
761 * @param tid global tunnel number
763 * @return tunnel handler, NULL if doesn't exist
765 static struct MeshTunnel *
766 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
770 * Notify a tunnel that a connection has broken that affects at least
773 * @param t Tunnel affected.
774 * @param p1 Peer that got disconnected from p2.
775 * @param p2 Peer that got disconnected from p1.
777 * @return Short ID of the peer disconnected (either p1 or p2).
778 * 0 if the tunnel remained unaffected.
780 static GNUNET_PEER_Id
781 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
786 * Send the message to all clients that have subscribed to its type
788 * @param msg Pointer to the message itself
789 * @param payload Pointer to the payload of the message.
790 * @return number of clients this message was sent to
793 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
794 const struct GNUNET_MessageHeader *payload)
796 struct GNUNET_PeerIdentity *oid;
797 struct MeshClient *c;
798 struct MeshTunnel *t;
799 MESH_TunnelNumber *tid;
802 char cbuf[htons (msg->size)];
805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Sending to clients...\n");
806 type = ntohs (payload->type);
807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: message of type %u\n", type);
810 memcpy (cbuf, msg, sizeof (cbuf));
811 switch (htons (msg->type))
813 struct GNUNET_MESH_Unicast *uc;
814 struct GNUNET_MESH_Multicast *mc;
815 struct GNUNET_MESH_ToOrigin *to;
817 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
818 uc = (struct GNUNET_MESH_Unicast *) cbuf;
822 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
823 mc = (struct GNUNET_MESH_Multicast *) cbuf;
827 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
828 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
836 t = tunnel_get (oid, ntohl (*tid));
842 // FIXME proper client differentiation mechanism required
843 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
844 *tid = htonl (t->local_tid);
846 *tid = htonl (t->local_tid_dest != 0 ? t->local_tid_dest : t->local_tid);
847 for (count = 0, c = clients; c != NULL; c = c->next)
850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client %u\n", c->id);
852 if (client_is_subscribed (type, c))
855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: sending\n");
856 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
857 (struct GNUNET_MessageHeader
858 *) cbuf, GNUNET_YES);
866 * Notify the client that owns the tunnel that a peer has connected to it
867 * (the requested path to it has been confirmed).
869 * @param t Tunnel whose owner to notify
870 * @param id Short id of the peer that has connected
873 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
875 struct GNUNET_MESH_PeerControl pc;
877 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
878 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
879 pc.tunnel_id = htonl (t->local_tid);
880 GNUNET_PEER_resolve (id, &pc.peer);
881 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle, &pc.header,
887 * Notify all clients (not depending on registration status) that the incoming
888 * tunnel is no longer valid.
890 * @param t Tunnel that was destroyed.
893 send_clients_tunnel_destroy (struct MeshTunnel *t)
895 struct GNUNET_MESH_TunnelMessage msg;
897 msg.header.size = htons (sizeof (msg));
898 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
899 msg.tunnel_id = htonl (t->local_tid);
900 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
905 * Function called to notify a client about the socket
906 * being ready to queue more data. "buf" will be
907 * NULL and "size" zero if the socket was closed for
908 * writing in the meantime.
911 * @param size number of bytes available in buf
912 * @param buf where the callee should write the message
913 * @return number of bytes written to buf
916 send_core_create_path (void *cls, size_t size, void *buf);
920 * Function called to notify a client about the socket
921 * being ready to queue more data. "buf" will be
922 * NULL and "size" zero if the socket was closed for
923 * writing in the meantime.
925 * @param cls closure (data itself)
926 * @param size number of bytes available in buf
927 * @param buf where the callee should write the message
929 * @return number of bytes written to buf
932 send_core_data_multicast (void *cls, size_t size, void *buf);
936 * Decrements the reference counter and frees all resources if needed
938 * @param dd Data Descriptor used in a multicast message. Freed if needed.
941 data_descriptor_decrement_multicast (struct MeshData *mesh_data)
943 /* Make sure it's a multicast packet */
944 GNUNET_assert (NULL != mesh_data->reference_counter);
946 if (0 == --(*(mesh_data->reference_counter)))
948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Last copy!\n");
949 if (NULL != mesh_data->task)
951 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
953 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client...\n");
955 GNUNET_SERVER_receive_done (mesh_data->t->client->handle, GNUNET_OK);
957 GNUNET_free (mesh_data->task);
959 GNUNET_free (mesh_data->reference_counter);
960 GNUNET_free (mesh_data->data);
961 GNUNET_free (mesh_data);
967 * Cancel a core transmission that was already requested and free all resources
968 * associated to the request.
970 * @param peer PeeInfo of the peer whose transmission is cancelled.
971 * @param i Position of the transmission to be cancelled.
974 peer_info_cancel_transmission (struct MeshPeerInfo *peer, unsigned int i)
976 if (NULL != peer->core_transmit[i])
978 struct MeshTransmissionDescriptor *dd;
979 struct MeshPathInfo *path_info;
983 struct GNUNET_PeerIdentity id;
985 GNUNET_PEER_resolve (peer->id, &id);
986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
987 "MESH: Cancelling data transmission at %s [%u]\n",
988 GNUNET_i2s (&id), i);
989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: message type %u\n",
993 /* TODO: notify that tranmission has failed */
994 switch (peer->types[i])
996 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
997 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
998 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type payload\n");
1000 dd = peer->infos[i];
1001 data_descriptor_decrement_multicast (dd->mesh_data);
1003 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
1004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type create path\n");
1005 path_info = peer->infos[i];
1006 path_destroy (path_info->path);
1010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type unknown!\n");
1012 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit[i]);
1013 peer->core_transmit[i] = NULL;
1014 GNUNET_free (peer->infos[i]);
1020 * Get a unused CORE slot to transmit a message to a peer. If all the slots
1021 * are used, cancel one and return it's position.
1023 * @param peer PeerInfo of the neighbor we want to transmit to.
1025 * @return The index of an available slot to transmit to the neighbor.
1028 peer_info_transmit_slot (struct MeshPeerInfo *peer)
1032 for (i = 0; peer->core_transmit[i]; i++)
1034 if (i == (CORE_QUEUE_SIZE - 1))
1036 /* All positions are taken! Overwriting! */
1038 peer_info_cancel_transmission (peer, 0);
1047 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1048 * and insert it in the appropiate structures if the peer is not known yet.
1050 * @param peer Full identity of the peer.
1052 * @return Existing or newly created peer info.
1054 static struct MeshPeerInfo *
1055 peer_info_get (const struct GNUNET_PeerIdentity *peer)
1057 struct MeshPeerInfo *peer_info;
1059 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1060 if (NULL == peer_info)
1063 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1064 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1065 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1066 peer_info->id = GNUNET_PEER_intern (peer);
1074 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1075 * and insert it in the appropiate structures if the peer is not known yet.
1077 * @param peer Short identity of the peer.
1079 * @return Existing or newly created peer info.
1081 static struct MeshPeerInfo *
1082 peer_info_get_short (const GNUNET_PEER_Id peer)
1084 struct GNUNET_PeerIdentity id;
1086 GNUNET_PEER_resolve (peer, &id);
1087 return peer_info_get (&id);
1092 * Iterator to remove the tunnel from the list of tunnels a peer participates
1095 * @param cls Closure (tunnel info)
1096 * @param key GNUNET_PeerIdentity of the peer (unused)
1097 * @param value PeerInfo of the peer
1099 * @return always GNUNET_YES, to keep iterating
1102 peer_info_delete_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
1104 struct MeshTunnel *t = cls;
1105 struct MeshPeerInfo *peer = value;
1108 for (i = 0; i < peer->ntunnels; i++)
1111 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1114 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1115 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
1124 * Core callback to write a
1126 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1127 * @param size Number of bytes available in buf.
1128 * @param buf Where the to write the message.
1130 * @return number of bytes written to buf
1133 send_core_data_raw (void *cls, size_t size, void *buf)
1135 struct MeshTransmissionDescriptor *info = cls;
1136 struct GNUNET_MessageHeader *msg;
1139 GNUNET_assert (NULL != info);
1140 GNUNET_assert (NULL != info->mesh_data);
1141 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
1142 total_size = ntohs (msg->size);
1144 if (total_size > size)
1146 struct GNUNET_PeerIdentity id;
1148 GNUNET_PEER_resolve (info->peer->id, &id);
1149 info->peer->core_transmit[info->handler_n] =
1150 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1151 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1152 size, &send_core_data_raw, info);
1155 info->peer->core_transmit[info->handler_n] = NULL;
1156 memcpy (buf, msg, total_size);
1157 GNUNET_free (info->mesh_data);
1164 * Sends an already built message to a peer, properly registrating
1165 * all used resources.
1167 * @param message Message to send. Fucntion makes a copy of it.
1168 * @param peer Short ID of the neighbor whom to send the message.
1173 send_message (const struct GNUNET_MessageHeader *message,
1174 const struct GNUNET_PeerIdentity *peer)
1176 struct MeshTransmissionDescriptor *info;
1177 struct MeshPeerInfo *neighbor;
1178 struct MeshPeerPath *p;
1182 // GNUNET_TRANSPORT_try_connect();
1184 size = ntohs (message->size);
1185 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1186 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
1187 info->mesh_data->data = GNUNET_malloc (size);
1188 memcpy (info->mesh_data->data, message, size);
1189 info->mesh_data->data_len = size;
1190 neighbor = peer_info_get (peer);
1191 for (p = neighbor->path_head; NULL != p; p = p->next)
1201 GNUNET_free (info->mesh_data->data);
1202 GNUNET_free (info->mesh_data);
1206 i = peer_info_transmit_slot (neighbor);
1207 info->handler_n = i;
1208 info->peer = neighbor;
1209 neighbor->types[i] = GNUNET_MESSAGE_TYPE_MESH_UNICAST;
1210 neighbor->infos[i] = info;
1211 neighbor->core_transmit[i] =
1212 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1213 GNUNET_TIME_UNIT_FOREVER_REL, peer,
1214 size, &send_core_data_raw, info);
1220 * Sends a CREATE PATH message for a path to a peer, properly registrating
1221 * all used resources.
1223 * @param peer PeerInfo of the final peer for whom this path is being created.
1224 * @param p Path itself.
1225 * @param t Tunnel for which the path is created.
1228 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
1229 struct MeshTunnel *t)
1231 struct GNUNET_PeerIdentity id;
1232 struct MeshPathInfo *path_info;
1233 struct MeshPeerInfo *neighbor;
1238 p = tree_get_path_to_peer (t->tree, peer->id);
1245 for (i = 0; i < p->length; i++)
1247 if (p->peers[i] == myid)
1250 if (i >= p->length - 1)
1256 GNUNET_PEER_resolve (p->peers[i + 1], &id);
1258 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1259 path_info->path = p;
1261 neighbor = peer_info_get (&id);
1262 path_info->peer = neighbor;
1263 path_info->pos = peer_info_transmit_slot (neighbor);
1264 neighbor->types[path_info->pos] = GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE;
1265 neighbor->infos[path_info->pos] = path_info;
1266 neighbor->core_transmit[path_info->pos] = GNUNET_CORE_notify_transmit_ready (core_handle, /* handle */
1269 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1271 sizeof (struct GNUNET_MESH_ManipulatePath) + (p->length * sizeof (struct GNUNET_PeerIdentity)), /*size */
1272 &send_core_create_path, /* callback */
1273 path_info); /* cls */
1278 * Sends a DESTROY PATH message to free resources for a path in a tunnel
1280 * @param t Tunnel whose path to destroy.
1281 * @param destination Short ID of the peer to whom the path to destroy.
1284 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
1286 struct MeshPeerPath *p;
1289 p = tree_get_path_to_peer (t->tree, destination);
1295 size = sizeof (struct GNUNET_MESH_ManipulatePath);
1296 size += p->length * sizeof (struct GNUNET_PeerIdentity);
1298 struct GNUNET_MESH_ManipulatePath *msg;
1299 struct GNUNET_PeerIdentity *pi;
1303 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
1304 msg->header.size = htons (size);
1305 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
1306 msg->tid = htonl (t->id.tid);
1307 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1308 for (i = 0; i < p->length; i++)
1310 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
1312 send_message (&msg->header, tree_get_first_hop (t->tree, destination));
1319 * Try to establish a new connection to this peer.
1320 * Use the best path for the given tunnel.
1321 * If the peer doesn't have any path to it yet, try to get one.
1322 * If the peer already has some path, send a CREATE PATH towards it.
1324 * @param peer PeerInfo of the peer.
1325 * @param t Tunnel for which to create the path, if possible.
1328 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1330 struct MeshPeerPath *p;
1331 struct MeshPathInfo *path_info;
1333 if (NULL != peer->path_head)
1335 p = tree_get_path_to_peer (t->tree, peer->id);
1344 send_create_path (peer, p, t);
1349 send_client_peer_connected (t, myid);
1352 else if (NULL == peer->dhtget)
1354 struct GNUNET_PeerIdentity id;
1356 GNUNET_PEER_resolve (peer->id, &id);
1357 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1358 path_info->peer = peer;
1360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1361 "MESH: Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1362 peer->dhtgetcls = path_info;
1363 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1364 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1365 GNUNET_BLOCK_TYPE_TEST, /* type */
1366 &id.hashPubKey, /* key to search */
1367 4, /* replication level */
1368 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, /* xquery */
1369 0, /* xquery bits */
1370 &dht_get_id_handler, path_info);
1372 /* Otherwise, there is no path but the DHT get is already started. */
1377 * Task to delay the connection of a peer
1379 * @param cls Closure (path info with tunnel and peer to connect).
1380 * Will be free'd on exection.
1381 * @param tc TaskContext
1384 peer_info_connect_task (void *cls,
1385 const struct GNUNET_SCHEDULER_TaskContext *tc)
1387 struct MeshPathInfo *path_info = cls;
1389 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1394 peer_info_connect (path_info->peer, path_info->t);
1400 * Destroy the peer_info and free any allocated resources linked to it
1402 * @param pi The peer_info to destroy.
1404 * @return GNUNET_OK on success
1407 peer_info_destroy (struct MeshPeerInfo *pi)
1409 struct GNUNET_PeerIdentity id;
1410 struct MeshPeerPath *p;
1411 struct MeshPeerPath *nextp;
1414 GNUNET_PEER_resolve (pi->id, &id);
1415 GNUNET_PEER_change_rc (pi->id, -1);
1418 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1421 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1422 "MESH: removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1424 if (NULL != pi->dhtget)
1426 GNUNET_DHT_get_stop (pi->dhtget);
1427 GNUNET_free (pi->dhtgetcls);
1429 for (i = 0; i < CORE_QUEUE_SIZE; i++)
1431 peer_info_cancel_transmission (pi, i);
1437 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1447 * Remove all paths that rely on a direct connection between p1 and p2
1448 * from the peer itself and notify all tunnels about it.
1450 * @param peer PeerInfo of affected peer.
1451 * @param p1 GNUNET_PEER_Id of one peer.
1452 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1455 * TODO: optimize (see below)
1458 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1461 struct MeshPeerPath *p;
1462 struct MeshPeerPath *aux;
1463 struct MeshPeerInfo *peer_d;
1465 unsigned int destroyed;
1471 p = peer->path_head;
1475 for (i = 0; i < (p->length - 1); i++)
1477 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1478 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1480 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1491 for (i = 0; i < peer->ntunnels; i++)
1493 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1497 * Problem: one or more peers have been deleted from the tunnel tree.
1498 * We don't know who they are to try to add them again.
1499 * We need to try to find a new path for each of the disconnected peers.
1500 * Some of them might already have a path to reach them that does not
1501 * involve p1 and p2. Adding all anew might render in a better tree than
1502 * the trivial immediate fix.
1504 * Trivial immiediate fix: try to reconnect to the disconnected node. All
1505 * its children will be reachable trough him.
1507 peer_d = peer_info_get_short (d);
1510 for (p = peer_d->path_head; NULL != p; p = p->next)
1512 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
1520 /* No callback, as peer will be already disconnected and a connection
1521 * scheduled by tunnel_notify_connection_broken.
1523 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
1527 peer_info_connect (peer_d, peer->tunnels[i]);
1534 * Add the path to the peer and update the path used to reach it in case this
1537 * @param peer_info Destination peer to add the path to.
1538 * @param path New path to add. Last peer must be the peer in arg 1.
1539 * Path will be either used of freed if already known.
1540 * @param trusted Do we trust that this path is real?
1543 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1546 struct MeshPeerPath *aux;
1550 if ((NULL == peer_info) || (NULL == path))
1553 path_destroy (path);
1556 if (path->peers[path->length - 1] != peer_info->id)
1559 path_destroy (path);
1562 if (path->length <= 2 && GNUNET_NO == trusted)
1564 /* Only allow CORE to tell us about direct paths */
1565 path_destroy (path);
1568 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1569 for (l = 1; l < path->length; l++)
1571 if (path->peers[l] == myid)
1573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shortening path by %u\n", l);
1574 for (l2 = 0; l2 < path->length - l; l2++)
1576 path->peers[l2] = path->peers[l + l2];
1581 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1586 struct GNUNET_PeerIdentity id;
1588 GNUNET_PEER_resolve (peer_info->id, &id);
1589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: adding path [%u] to peer %s\n",
1590 path->length, GNUNET_i2s (&id));
1593 l = path_get_length (path);
1600 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1601 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1603 l2 = path_get_length (aux);
1606 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1607 peer_info->path_tail, aux, path);
1612 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1614 path_destroy (path);
1619 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1626 * Add the path to the origin peer and update the path used to reach it in case
1627 * this is the shortest.
1628 * The path is given in peer_info -> destination, therefore we turn the path
1629 * upside down first.
1631 * @param peer_info Peer to add the path to, being the origin of the path.
1632 * @param path New path to add after being inversed.
1633 * @param trusted Do we trust that this path is real?
1636 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1637 struct MeshPeerPath *path, int trusted)
1640 peer_info_add_path (peer_info, path, trusted);
1645 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1646 * to obtain a local peer -> destination path and interning the peer ids.
1648 * @return Newly allocated and created path
1650 static struct MeshPeerPath *
1651 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1652 unsigned int get_path_length,
1653 const struct GNUNET_PeerIdentity *put_path,
1654 unsigned int put_path_length)
1656 struct MeshPeerPath *p;
1662 GNUNET_PEER_change_rc (myid, 1);
1663 i = get_path_length;
1664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: GET has %d hops.\n", i);
1665 for (i--; i >= 0; i--)
1667 id = GNUNET_PEER_intern (&get_path[i]);
1668 if (p->length > 0 && id == p->peers[p->length - 1])
1670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1671 GNUNET_PEER_change_rc (id, -1);
1675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Adding from GET: %s.\n",
1676 GNUNET_i2s (&get_path[i]));
1678 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1679 p->peers[p->length - 1] = id;
1682 i = put_path_length;
1683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: PUT has %d hops.\n", i);
1684 for (i--; i >= 0; i--)
1686 id = GNUNET_PEER_intern (&put_path[i]);
1689 /* PUT path went through us, so discard the path up until now and start
1690 * from here to get a much shorter (and loop-free) path.
1695 if (p->length > 0 && id == p->peers[p->length - 1])
1697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1698 GNUNET_PEER_change_rc (id, -1);
1702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Adding from PUT: %s.\n",
1703 GNUNET_i2s (&put_path[i]));
1705 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1706 p->peers[p->length - 1] = id;
1710 if (get_path_length > 0)
1711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (first of GET: %s)\n",
1712 GNUNET_i2s (&get_path[0]));
1713 if (put_path_length > 0)
1714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (first of PUT: %s)\n",
1715 GNUNET_i2s (&put_path[0]));
1716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: In total: %d hops\n",
1718 for (i = 0; i < p->length; i++)
1720 struct GNUNET_PeerIdentity peer_id;
1722 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %u: %s\n", p->peers[i],
1724 GNUNET_i2s (&peer_id));
1732 * Adds a path to the peer_infos of all the peers in the path
1734 * @param p Path to process.
1735 * @param confirmed Whether we know if the path works or not. FIXME use
1738 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
1742 /* TODO: invert and add */
1743 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1744 for (i++; i < p->length; i++)
1746 struct MeshPeerInfo *aux;
1747 struct MeshPeerPath *copy;
1749 aux = peer_info_get_short (p->peers[i]);
1750 copy = path_duplicate (p);
1751 copy->length = i + 1;
1752 peer_info_add_path (aux, copy, GNUNET_NO);
1758 * Send keepalive packets for a peer
1760 * @param cls Closure (tunnel for which to send the keepalive).
1761 * @param tc Notification context.
1763 * TODO: implement explicit multicast keepalive?
1766 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1770 * Search for a tunnel among the incoming tunnels
1772 * @param tid the local id of the tunnel
1774 * @return tunnel handler, NULL if doesn't exist
1776 static struct MeshTunnel *
1777 tunnel_get_incoming (MESH_TunnelNumber tid)
1779 GNUNET_HashCode hash;
1781 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
1782 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1783 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
1788 * Search for a tunnel among the tunnels for a client
1790 * @param c the client whose tunnels to search in
1791 * @param tid the local id of the tunnel
1793 * @return tunnel handler, NULL if doesn't exist
1795 static struct MeshTunnel *
1796 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1798 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1800 return tunnel_get_incoming (tid);
1804 GNUNET_HashCode hash;
1806 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1807 return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
1813 * Search for a tunnel by global ID using PEER_ID
1815 * @param pi owner of the tunnel
1816 * @param tid global tunnel number
1818 * @return tunnel handler, NULL if doesn't exist
1820 static struct MeshTunnel *
1821 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1823 struct MESH_TunnelID id;
1824 GNUNET_HashCode hash;
1829 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1830 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1835 * Search for a tunnel by global ID using full PeerIdentities
1837 * @param oid owner of the tunnel
1838 * @param tid global tunnel number
1840 * @return tunnel handler, NULL if doesn't exist
1842 static struct MeshTunnel *
1843 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1845 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1850 * Callback used to notify a client owner of a tunnel that a peer has
1851 * disconnected, most likely because of a path change.
1853 * @param cls Closure (tunnel this notification is about).
1854 * @param peer_id Short ID of disconnected peer.
1857 notify_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
1859 struct MeshTunnel *t = cls;
1860 struct MeshPeerInfo *peer;
1861 struct MeshPathInfo *path_info;
1863 if (NULL != t->client && NULL != nc)
1865 struct GNUNET_MESH_PeerControl msg;
1867 msg.header.size = htons (sizeof (msg));
1868 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1869 msg.tunnel_id = htonl (t->local_tid);
1870 GNUNET_PEER_resolve (peer_id, &msg.peer);
1871 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1872 &msg.header, GNUNET_NO);
1874 peer = peer_info_get_short (peer_id);
1875 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1876 path_info->peer = peer;
1878 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
1883 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1884 * needed rescources.
1885 * If peer already exists, reevaluate shortest path and change if different.
1887 * @param t Tunnel we want to add a new peer to
1888 * @param peer PeerInfo of the peer being added
1892 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1894 struct GNUNET_PeerIdentity id;
1895 struct MeshPeerPath *best_p;
1896 struct MeshPeerPath *p;
1897 unsigned int best_cost;
1900 GNUNET_PEER_resolve (peer->id, &id);
1902 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
1905 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
1906 GNUNET_assert (GNUNET_OK ==
1907 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
1909 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1912 if (NULL != (p = peer->path_head))
1915 best_cost = tree_get_path_cost (t->tree, p);
1918 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
1925 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected, t);
1926 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
1927 t->path_refresh_task =
1928 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
1932 /* Start a DHT get */
1933 peer_info_connect (peer, t);
1938 * Add a path to a tunnel which we don't own, just to remember the next hop.
1939 * If destination node was already in the tunnel, the first hop information
1940 * will be replaced with the new path.
1942 * @param t Tunnel we want to add a new peer to
1943 * @param p Path to add
1944 * @param own_pos Position of local node in path.
1948 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
1949 unsigned int own_pos)
1951 struct GNUNET_PeerIdentity id;
1953 GNUNET_assert (0 != own_pos);
1954 tree_add_path (t->tree, p, NULL, NULL);
1955 if (own_pos < p->length - 1)
1957 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
1958 tree_update_first_hops (t->tree, myid, &id);
1964 * Notifies a tunnel that a connection has broken that affects at least
1965 * some of its peers. Sends a notification towards the root of the tree.
1966 * In case the peer is the owner of the tree, notifies the client that owns
1967 * the tunnel and tries to reconnect.
1969 * @param t Tunnel affected.
1970 * @param p1 Peer that got disconnected from p2.
1971 * @param p2 Peer that got disconnected from p1.
1973 * @return Short ID of the peer disconnected (either p1 or p2).
1974 * 0 if the tunnel remained unaffected.
1976 static GNUNET_PEER_Id
1977 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1983 tree_notify_connection_broken (t->tree, p1, p2, ¬ify_peer_disconnected,
1985 if (myid != p1 && myid != p2)
1991 if (tree_get_predecessor (t->tree) != 0)
1993 /* We are the peer still connected, notify owner of the disconnection. */
1994 struct GNUNET_MESH_PathBroken msg;
1995 struct GNUNET_PeerIdentity neighbor;
1997 msg.header.size = htons (sizeof (msg));
1998 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
1999 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2000 msg.tid = htonl (t->id.tid);
2001 msg.peer1 = my_full_id;
2002 GNUNET_PEER_resolve (pid, &msg.peer2);
2003 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2004 send_message (&msg.header, &neighbor);
2012 * Send a multicast packet to a neighbor.
2014 * @param cls Closure (Info about the multicast packet)
2015 * @param neighbor_id Short ID of the neighbor to send the packet to.
2018 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
2020 struct MeshData *mdata = cls;
2021 struct MeshTransmissionDescriptor *info;
2022 struct GNUNET_PeerIdentity neighbor;
2025 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2027 info->mesh_data = mdata;
2028 (*(mdata->reference_counter)) ++;
2029 info->destination = neighbor_id;
2030 GNUNET_PEER_resolve (neighbor_id, &neighbor);
2032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: sending to %s...\n",
2033 GNUNET_i2s (&neighbor));
2035 info->peer = peer_info_get (&neighbor);
2036 GNUNET_assert (NULL != info->peer);
2037 i = peer_info_transmit_slot (info->peer);
2038 info->handler_n = i;
2039 info->peer->infos[i] = info;
2040 info->peer->types[i] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
2041 info->peer->core_transmit[i] =
2042 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2043 GNUNET_TIME_UNIT_FOREVER_REL,
2044 &neighbor, info->mesh_data->data_len,
2045 &send_core_data_multicast, info);
2049 * Send a message in a tunnel in multicast, sending a copy to each child node
2050 * down the local one in the tunnel tree.
2052 * @param t Tunnel in which to send the data.
2053 * @param msg Message to be sent
2056 tunnel_send_multicast (struct MeshTunnel *t,
2057 const struct GNUNET_MessageHeader *msg)
2059 struct MeshData *mdata;
2062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2063 "MESH: sending a multicast packet...\n");
2065 mdata = GNUNET_malloc (sizeof (struct MeshData));
2066 mdata->data_len = ntohs (msg->size);
2067 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2069 mdata->data = GNUNET_malloc (mdata->data_len);
2070 memcpy (mdata->data, msg, mdata->data_len);
2071 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2073 struct GNUNET_MESH_Multicast *mcast;
2075 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
2076 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
2078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: data packet, ttl: %u\n",
2079 ntohl (mcast->ttl));
2083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: not a data packet, no ttl\n");
2086 if (NULL != t->client)
2088 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
2090 GNUNET_SCHEDULER_add_delayed (UNACKNOWLEDGED_WAIT, &client_allow_send,
2092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: timeout task %u\n",
2096 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
2097 if (*(mdata->reference_counter) == 0)
2099 GNUNET_free (mdata->data);
2100 GNUNET_free (mdata->reference_counter);
2101 if (NULL != mdata->task)
2103 GNUNET_SCHEDULER_cancel(*(mdata->task));
2104 GNUNET_free (mdata->task);
2105 GNUNET_SERVER_receive_done(t->client->handle, GNUNET_OK);
2107 // FIXME change order?
2108 GNUNET_free (mdata);
2111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2112 "MESH: sending a multicast packet done\n");
2119 * Send a message to all peers in this tunnel that the tunnel is no longer
2122 * @param t The tunnel whose peers to notify.
2125 tunnel_send_destroy (struct MeshTunnel *t)
2127 struct GNUNET_MESH_TunnelDestroy msg;
2129 msg.header.size = htons (sizeof (msg));
2130 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2131 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2132 msg.tid = htonl (t->id.tid);
2133 tunnel_send_multicast (t, &msg.header);
2139 * Destroy the tunnel and free any allocated resources linked to it
2141 * @param t the tunnel to destroy
2143 * @return GNUNET_OK on success
2146 tunnel_destroy (struct MeshTunnel *t)
2148 struct MeshClient *c;
2149 struct MeshQueue *q;
2150 struct MeshQueue *qn;
2151 GNUNET_HashCode hash;
2160 struct GNUNET_PeerIdentity id;
2162 GNUNET_PEER_resolve (t->id.oid, &id);
2163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel %s [%x]\n",
2164 GNUNET_i2s (&id), t->id.tid);
2166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2170 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2171 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2176 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2178 GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
2182 if (t->local_tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2184 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2185 GNUNET_break (GNUNET_YES ==
2186 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash,
2189 if (NULL != t->peers)
2191 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
2193 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
2198 if (NULL != q->data)
2199 GNUNET_free (q->data);
2203 /* TODO cancel core transmit ready in case it was active */
2205 tree_destroy (t->tree);
2206 if (NULL != t->dht_get_type)
2207 GNUNET_DHT_get_stop (t->dht_get_type);
2208 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2209 GNUNET_SCHEDULER_cancel (t->timeout_task);
2210 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
2211 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
2218 * Removes an explicit path from a tunnel, freeing all intermediate nodes
2219 * that are no longer needed, as well as nodes of no longer reachable peers.
2220 * The tunnel itself is also destoyed if results in a remote empty tunnel.
2222 * @param t Tunnel from which to remove the path.
2223 * @param peer Short id of the peer which should be removed.
2226 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
2228 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
2234 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
2235 * client when the client disconnects.
2237 * @param cls closure (client that is disconnecting)
2238 * @param key the hash of the local tunnel id (used to access the hashmap)
2239 * @param value the value stored at the key (tunnel to destroy)
2241 * @return GNUNET_OK on success
2244 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2246 struct MeshTunnel *t = value;
2249 r = tunnel_destroy (t);
2255 * Timeout function, destroys tunnel if called
2257 * @param cls Closure (tunnel to destroy).
2258 * @param tc TaskContext
2261 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2263 struct MeshTunnel *t = cls;
2265 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
2267 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2272 * Resets the tunnel timeout. Starts it if no timeout was running.
2274 * @param t Tunnel whose timeout to reset.
2277 tunnel_reset_timeout (struct MeshTunnel *t)
2279 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2280 GNUNET_SCHEDULER_cancel (t->timeout_task);
2282 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2283 (REFRESH_PATH_TIME, 4), &tunnel_timeout, t);
2287 /******************************************************************************/
2288 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2289 /******************************************************************************/
2292 * Function called to notify a client about the socket
2293 * being ready to queue more data. "buf" will be
2294 * NULL and "size" zero if the socket was closed for
2295 * writing in the meantime.
2297 * @param cls closure
2298 * @param size number of bytes available in buf
2299 * @param buf where the callee should write the message
2300 * @return number of bytes written to buf
2303 send_core_create_path (void *cls, size_t size, void *buf)
2305 struct MeshPathInfo *info = cls;
2306 struct GNUNET_MESH_ManipulatePath *msg;
2307 struct GNUNET_PeerIdentity *peer_ptr;
2308 struct MeshPeerInfo *peer = info->peer;
2309 struct MeshTunnel *t = info->t;
2310 struct MeshPeerPath *p = info->path;
2314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATE PATH sending...\n");
2316 sizeof (struct GNUNET_MESH_ManipulatePath) +
2317 p->length * sizeof (struct GNUNET_PeerIdentity);
2319 if (size < size_needed || NULL == buf)
2321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: create path retransmit!\n");
2322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: buf: %p\n", buf);
2323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: size: (%u/%u)\n", size,
2325 info->peer->core_transmit[info->pos] =
2326 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2327 GNUNET_TIME_UNIT_FOREVER_REL,
2328 tree_get_first_hop (t->tree,
2330 size_needed, &send_core_create_path,
2334 info->peer->core_transmit[info->pos] = NULL;
2337 struct GNUNET_PeerIdentity id;
2339 GNUNET_PEER_resolve (peer->id, &id);
2340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2341 "MESH: setting core_transmit %s [%u] to NULL\n",
2342 GNUNET_i2s (&id), info->pos);
2345 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
2346 msg->header.size = htons (size_needed);
2347 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2348 msg->tid = ntohl (t->id.tid);
2350 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2351 for (i = 0; i < p->length; i++)
2353 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2360 "MESH: CREATE PATH (%u bytes long) sent!\n", size_needed);
2366 * Function called to notify a client about the socket
2367 * being ready to queue more data. "buf" will be
2368 * NULL and "size" zero if the socket was closed for
2369 * writing in the meantime.
2371 * @param cls closure (data itself)
2372 * @param size number of bytes available in buf
2373 * @param buf where the callee should write the message
2375 * @return number of bytes written to buf
2378 send_core_data_multicast (void *cls, size_t size, void *buf)
2380 struct MeshTransmissionDescriptor *info = cls;
2383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Multicast callback.\n");
2384 GNUNET_assert (NULL != info);
2385 GNUNET_assert (NULL != info->peer);
2386 total_size = info->mesh_data->data_len;
2387 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
2389 if (total_size > size)
2392 struct GNUNET_PeerIdentity id;
2394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2395 "MESH: Multicast: retransmitting... (%u/%u)\n", size,
2397 GNUNET_PEER_resolve (info->peer->id, &id);
2398 info->peer->core_transmit[info->handler_n] =
2399 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2400 GNUNET_TIME_UNIT_FOREVER_REL, &id,
2402 &send_core_data_multicast, info);
2405 info->peer->core_transmit[info->handler_n] = NULL;
2406 info->peer->infos[info->handler_n] = NULL;
2407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: copying data...\n");
2408 memcpy (buf, info->mesh_data->data, total_size);
2411 struct GNUNET_MESH_Multicast *mc;
2412 struct GNUNET_MessageHeader *mh;
2415 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2417 mc = (struct GNUNET_MESH_Multicast *) mh;
2418 mh = (struct GNUNET_MessageHeader *) &mc[1];
2419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2420 "MESH: multicast, payload type %u\n", ntohs (mh->type));
2421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2422 "MESH: multicast, payload size %u\n", ntohs (mh->size));
2426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type %u\n",
2431 data_descriptor_decrement_multicast (info->mesh_data);
2432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: freeing info...\n");
2434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: return %u\n", total_size);
2440 * Function called to notify a client about the socket
2441 * being ready to queue more data. "buf" will be
2442 * NULL and "size" zero if the socket was closed for
2443 * writing in the meantime.
2445 * @param cls closure (MeshTransmissionDescriptor)
2446 * @param size number of bytes available in buf
2447 * @param buf where the callee should write the message
2448 * @return number of bytes written to buf
2451 send_core_path_ack (void *cls, size_t size, void *buf)
2453 struct MeshTransmissionDescriptor *info = cls;
2454 struct GNUNET_MESH_PathACK *msg = buf;
2456 GNUNET_assert (NULL != info);
2459 info->peer->core_transmit[info->handler_n] = NULL;
2461 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2466 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2467 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2468 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
2469 msg->tid = htonl (info->origin->tid);
2470 msg->peer_id = my_full_id;
2472 /* TODO add signature */
2474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: PATH ACK sent!\n");
2475 return sizeof (struct GNUNET_MESH_PathACK);
2479 /******************************************************************************/
2480 /******************** MESH NETWORK HANDLERS **************************/
2481 /******************************************************************************/
2485 * Core handler for path creation
2487 * @param cls closure
2488 * @param message message
2489 * @param peer peer identity this notification is about
2490 * @param atsi performance data
2491 * @param atsi_count number of records in 'atsi'
2493 * @return GNUNET_OK to keep the connection open,
2494 * GNUNET_SYSERR to close it (signal serious error)
2497 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
2498 const struct GNUNET_MessageHeader *message,
2499 const struct GNUNET_ATS_Information *atsi,
2500 unsigned int atsi_count)
2502 unsigned int own_pos;
2505 MESH_TunnelNumber tid;
2506 struct GNUNET_MESH_ManipulatePath *msg;
2507 struct GNUNET_PeerIdentity *pi;
2508 GNUNET_HashCode hash;
2509 struct MeshPeerPath *path;
2510 struct MeshPeerInfo *dest_peer_info;
2511 struct MeshPeerInfo *orig_peer_info;
2512 struct MeshTunnel *t;
2514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2515 "MESH: Received a path create msg [%s]\n",
2516 GNUNET_i2s (&my_full_id));
2517 size = ntohs (message->size);
2518 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2520 GNUNET_break_op (0);
2524 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2525 if (size % sizeof (struct GNUNET_PeerIdentity))
2527 GNUNET_break_op (0);
2530 size /= sizeof (struct GNUNET_PeerIdentity);
2533 GNUNET_break_op (0);
2536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: path has %u hops.\n", size);
2537 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2539 tid = ntohl (msg->tid);
2540 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2542 "MESH: path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
2543 t = tunnel_get (pi, tid);
2546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
2547 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2548 t->id.oid = GNUNET_PEER_intern (pi);
2550 while (NULL != tunnel_get_incoming (next_local_tid))
2551 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2552 t->local_tid = next_local_tid++;
2553 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2554 t->tree = tree_new (t->id.oid);
2556 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2558 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2559 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2565 tunnel_reset_timeout (t);
2566 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2568 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2569 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2577 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
2578 if (NULL == dest_peer_info)
2580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2581 "MESH: Creating PeerInfo for destination.\n");
2582 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2583 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
2584 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
2586 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2588 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
2589 if (NULL == orig_peer_info)
2591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2592 "MESH: Creating PeerInfo for origin.\n");
2593 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2594 orig_peer_info->id = GNUNET_PEER_intern (pi);
2595 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
2596 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating path...\n");
2599 path = path_new (size);
2601 for (i = 0; i < size; i++)
2603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... adding %s\n",
2604 GNUNET_i2s (&pi[i]));
2605 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2606 if (path->peers[i] == myid)
2609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Own position: %u\n", own_pos);
2612 /* cannot be self, must be 'not found' */
2613 /* create path: self not found in path through self */
2614 GNUNET_break_op (0);
2615 path_destroy (path);
2616 /* FIXME error. destroy tunnel? leave for timeout? */
2619 path_add_to_peers (path, GNUNET_NO);
2620 tunnel_add_path (t, path, own_pos);
2621 if (own_pos == size - 1)
2623 /* It is for us! Send ack. */
2624 struct MeshTransmissionDescriptor *info;
2627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: It's for us!\n");
2628 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
2629 if (NULL == t->peers)
2631 /* New tunnel! Notify clients! */
2632 struct GNUNET_MESH_TunnelNotification cmsg;
2634 cmsg.header.size = htons (sizeof (cmsg));
2635 cmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2636 GNUNET_PEER_resolve (t->id.oid, &cmsg.peer);
2637 cmsg.tunnel_id = htonl (t->local_tid);
2638 GNUNET_SERVER_notification_context_broadcast (nc, &cmsg.header,
2641 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
2643 GNUNET_break (GNUNET_OK ==
2644 GNUNET_CONTAINER_multihashmap_put (t->peers,
2645 &my_full_id.hashPubKey,
2648 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
2649 /* FIXME use send_message */
2650 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2651 info->origin = &t->id;
2652 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2653 GNUNET_assert (NULL != info->peer);
2654 j = peer_info_transmit_slot (info->peer);
2655 info->handler_n = j;
2656 info->peer->types[j] = GNUNET_MESSAGE_TYPE_MESH_PATH_ACK;
2657 info->peer->infos[j] = info;
2658 info->peer->core_transmit[j] =
2659 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
2660 GNUNET_TIME_UNIT_FOREVER_REL, peer,
2661 sizeof (struct GNUNET_MESH_PathACK),
2662 &send_core_path_ack, info);
2666 struct MeshPeerPath *path2;
2668 /* It's for somebody else! Retransmit. */
2669 path2 = path_duplicate (path);
2670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Retransmitting.\n");
2671 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
2672 path2 = path_duplicate (path);
2673 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
2674 send_create_path (dest_peer_info, path, t);
2681 * Core handler for path destruction
2683 * @param cls closure
2684 * @param message message
2685 * @param peer peer identity this notification is about
2686 * @param atsi performance data
2687 * @param atsi_count number of records in 'atsi'
2689 * @return GNUNET_OK to keep the connection open,
2690 * GNUNET_SYSERR to close it (signal serious error)
2693 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
2694 const struct GNUNET_MessageHeader *message,
2695 const struct GNUNET_ATS_Information *atsi,
2696 unsigned int atsi_count)
2698 struct GNUNET_MESH_ManipulatePath *msg;
2699 struct GNUNET_PeerIdentity *pi;
2700 struct MeshPeerPath *path;
2701 struct MeshTunnel *t;
2702 unsigned int own_pos;
2706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2707 "MESH: Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
2708 size = ntohs (message->size);
2709 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2711 GNUNET_break_op (0);
2715 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2716 if (size % sizeof (struct GNUNET_PeerIdentity))
2718 GNUNET_break_op (0);
2721 size /= sizeof (struct GNUNET_PeerIdentity);
2724 GNUNET_break_op (0);
2727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: path has %u hops.\n", size);
2729 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2730 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2732 "MESH: path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
2734 t = tunnel_get (pi, ntohl (msg->tid));
2737 /* TODO notify back: we don't know this tunnel */
2738 GNUNET_break_op (0);
2741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating path...\n");
2742 path = path_new (size);
2744 for (i = 0; i < size; i++)
2746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... adding %s\n",
2747 GNUNET_i2s (&pi[i]));
2748 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2749 if (path->peers[i] == myid)
2752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Own position: %u\n", own_pos);
2753 if (own_pos < path->length - 1)
2754 send_message (message, &pi[own_pos + 1]);
2755 tunnel_delete_peer (t, path->peers[path->length - 1]);
2756 path_destroy (path);
2762 * Core handler for notifications of broken paths
2764 * @param cls closure
2765 * @param message message
2766 * @param peer peer identity this notification is about
2767 * @param atsi performance data
2768 * @param atsi_count number of records in 'atsi'
2770 * @return GNUNET_OK to keep the connection open,
2771 * GNUNET_SYSERR to close it (signal serious error)
2774 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
2775 const struct GNUNET_MessageHeader *message,
2776 const struct GNUNET_ATS_Information *atsi,
2777 unsigned int atsi_count)
2779 struct GNUNET_MESH_PathBroken *msg;
2780 struct MeshTunnel *t;
2782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2783 "MESH: Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
2784 msg = (struct GNUNET_MESH_PathBroken *) message;
2785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: regarding %s\n",
2786 GNUNET_i2s (&msg->peer1));
2787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: regarding %s\n",
2788 GNUNET_i2s (&msg->peer2));
2789 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2792 GNUNET_break_op (0);
2795 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
2796 GNUNET_PEER_search (&msg->peer2));
2803 * Core handler for tunnel destruction
2805 * @param cls closure
2806 * @param message message
2807 * @param peer peer identity this notification is about
2808 * @param atsi performance data
2809 * @param atsi_count number of records in 'atsi'
2811 * @return GNUNET_OK to keep the connection open,
2812 * GNUNET_SYSERR to close it (signal serious error)
2815 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
2816 const struct GNUNET_MessageHeader *message,
2817 const struct GNUNET_ATS_Information *atsi,
2818 unsigned int atsi_count)
2820 struct GNUNET_MESH_TunnelDestroy *msg;
2821 struct MeshTunnel *t;
2823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2824 "MESH: Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
2825 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
2826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: for tunnel %s [%u]\n",
2827 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
2828 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2831 /* Probably already got the message from another path,
2832 * destroyed the tunnel and retransmitted to children.
2837 if (t->id.oid == myid)
2839 GNUNET_break_op (0);
2842 if (t->local_tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2844 /* Tunnel was incoming, notify clients */
2845 send_clients_tunnel_destroy (t);
2847 tunnel_send_destroy (t);
2854 * Core handler for mesh network traffic going from the origin to a peer
2856 * @param cls closure
2857 * @param peer peer identity this notification is about
2858 * @param message message
2859 * @param atsi performance data
2860 * @param atsi_count number of records in 'atsi'
2861 * @return GNUNET_OK to keep the connection open,
2862 * GNUNET_SYSERR to close it (signal serious error)
2865 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2866 const struct GNUNET_MessageHeader *message,
2867 const struct GNUNET_ATS_Information *atsi,
2868 unsigned int atsi_count)
2870 struct GNUNET_MESH_Unicast *msg;
2871 struct MeshTunnel *t;
2875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got a unicast packet from %s\n",
2877 size = ntohs (message->size);
2879 sizeof (struct GNUNET_MESH_Unicast) +
2880 sizeof (struct GNUNET_MessageHeader))
2885 msg = (struct GNUNET_MESH_Unicast *) message;
2886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: of type %u\n",
2887 ntohs (msg[1].header.type));
2888 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2891 /* TODO notify back: we don't know this tunnel */
2892 GNUNET_break_op (0);
2895 tunnel_reset_timeout (t);
2896 pid = GNUNET_PEER_search (&msg->destination);
2899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2900 "MESH: it's for us! sending to clients...\n");
2901 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
2904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2905 "MESH: not for us, retransmitting...\n");
2906 send_message (message, tree_get_first_hop (t->tree, pid));
2912 * Core handler for mesh network traffic going from the origin to all peers
2914 * @param cls closure
2915 * @param message message
2916 * @param peer peer identity this notification is about
2917 * @param atsi performance data
2918 * @param atsi_count number of records in 'atsi'
2919 * @return GNUNET_OK to keep the connection open,
2920 * GNUNET_SYSERR to close it (signal serious error)
2922 * TODO: Check who we got this from, to validate route.
2925 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2926 const struct GNUNET_MessageHeader *message,
2927 const struct GNUNET_ATS_Information *atsi,
2928 unsigned int atsi_count)
2930 struct GNUNET_MESH_Multicast *msg;
2931 struct MeshTunnel *t;
2934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got a multicast packet from %s\n",
2936 size = ntohs (message->size);
2937 if (sizeof (struct GNUNET_MESH_Multicast) +
2938 sizeof (struct GNUNET_MessageHeader) > size)
2940 GNUNET_break_op (0);
2943 msg = (struct GNUNET_MESH_Multicast *) message;
2944 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2948 /* TODO notify that we dont know that tunnel */
2949 GNUNET_break_op (0);
2952 if (t->mid == ntohl (msg->mid))
2954 /* FIXME: already seen this packet, log dropping */
2955 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2956 "MESH: Already seen mid %u, DROPPING!\n", t->mid);
2962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2963 "MESH: mid %u not seen yet, forwarding\n", ntohl (msg->mid));
2966 t->mid = ntohl (msg->mid);
2967 tunnel_reset_timeout (t);
2969 /* Transmit to locally interested clients */
2970 if (NULL != t->peers &&
2971 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
2973 send_subscribed_clients (message, &msg[1].header);
2976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ttl: %u\n", ntohl (msg->ttl));
2978 if (ntohl (msg->ttl) == 0)
2980 /* FIXME: ttl is 0, log dropping */
2981 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "MESH: TTL is 0, DROPPING!\n");
2984 tunnel_send_multicast (t, message);
2990 * Core handler for mesh network traffic toward the owner of a tunnel
2992 * @param cls closure
2993 * @param message message
2994 * @param peer peer identity this notification is about
2995 * @param atsi performance data
2996 * @param atsi_count number of records in 'atsi'
2998 * @return GNUNET_OK to keep the connection open,
2999 * GNUNET_SYSERR to close it (signal serious error)
3002 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
3003 const struct GNUNET_MessageHeader *message,
3004 const struct GNUNET_ATS_Information *atsi,
3005 unsigned int atsi_count)
3007 struct GNUNET_MESH_ToOrigin *msg;
3008 struct GNUNET_PeerIdentity id;
3009 struct MeshPeerInfo *peer_info;
3010 struct MeshTunnel *t;
3013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got a ToOrigin packet from %s\n",
3015 size = ntohs (message->size);
3016 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
3017 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
3019 GNUNET_break_op (0);
3022 msg = (struct GNUNET_MESH_ToOrigin *) message;
3023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: of type %u\n",
3024 ntohs (msg[1].header.type));
3025 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3029 /* TODO notify that we dont know this tunnel (whom)? */
3030 GNUNET_break_op (0);
3034 if (t->id.oid == myid)
3037 struct GNUNET_MESH_ToOrigin *copy;
3039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3040 "MESH: it's for us! sending to clients...\n");
3041 if (NULL == t->client)
3043 /* got data packet for ownerless tunnel */
3044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: no clients!\n");
3045 GNUNET_break_op (0);
3048 /* TODO signature verification */
3049 memcpy (cbuf, message, size);
3050 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
3051 copy->tid = htonl (t->local_tid);
3052 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
3053 ©->header, GNUNET_YES);
3056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3057 "MESH: not for us, retransmitting...\n");
3059 peer_info = peer_info_get (&msg->oid);
3060 if (NULL == peer_info)
3062 /* unknown origin of tunnel */
3066 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3067 send_message (message, &id);
3074 * Core handler for path ACKs
3076 * @param cls closure
3077 * @param message message
3078 * @param peer peer identity this notification is about
3079 * @param atsi performance data
3080 * @param atsi_count number of records in 'atsi'
3082 * @return GNUNET_OK to keep the connection open,
3083 * GNUNET_SYSERR to close it (signal serious error)
3086 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3087 const struct GNUNET_MessageHeader *message,
3088 const struct GNUNET_ATS_Information *atsi,
3089 unsigned int atsi_count)
3091 struct GNUNET_MESH_PathACK *msg;
3092 struct GNUNET_PeerIdentity id;
3093 struct MeshPeerInfo *peer_info;
3094 struct MeshPeerPath *p;
3095 struct MeshTunnel *t;
3097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Received a path ACK msg [%s]\n",
3098 GNUNET_i2s (&my_full_id));
3099 msg = (struct GNUNET_MESH_PathACK *) message;
3100 t = tunnel_get (&msg->oid, msg->tid);
3103 /* TODO notify that we don't know the tunnel */
3107 peer_info = peer_info_get (&msg->peer_id);
3109 /* Add paths to peers? */
3110 p = tree_get_path_to_peer (t->tree, peer_info->id);
3113 path_add_to_peers (p, GNUNET_YES);
3121 /* Message for us? */
3122 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: It's for us!\n");
3125 if (NULL == t->client)
3127 GNUNET_break_op (0);
3130 if (NULL != t->dht_get_type)
3132 GNUNET_DHT_get_stop (t->dht_get_type);
3133 t->dht_get_type = NULL;
3135 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
3137 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
3138 send_client_peer_connected (t, peer_info->id);
3143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3144 "MESH: not for us, retransmitting...\n");
3145 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3146 peer_info = peer_info_get (&msg->oid);
3147 if (NULL == peer_info)
3149 /* If we know the tunnel, we should DEFINITELY know the peer */
3153 send_message (message, &id);
3159 * Functions to handle messages from core
3161 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
3162 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
3163 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
3164 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
3165 sizeof (struct GNUNET_MESH_PathBroken)},
3166 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY, 0},
3167 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3168 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3169 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3170 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3171 sizeof (struct GNUNET_MESH_PathACK)},
3177 /******************************************************************************/
3178 /**************** MESH LOCAL HANDLER HELPERS ***********************/
3179 /******************************************************************************/
3182 * deregister_app: iterator for removing each application registered by a client
3184 * @param cls closure
3185 * @param key the hash of the application id (used to access the hashmap)
3186 * @param value the value stored at the key (client)
3188 * @return GNUNET_OK on success
3191 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
3193 GNUNET_break (GNUNET_YES ==
3194 GNUNET_CONTAINER_multihashmap_remove (applications, key,
3201 * notify_client_connection_failure: notify a client that the connection to the
3202 * requested remote peer is not possible (for instance, no route found)
3203 * Function called when the socket is ready to queue more data. "buf" will be
3204 * NULL and "size" zero if the socket was closed for writing in the meantime.
3206 * @param cls closure
3207 * @param size number of bytes available in buf
3208 * @param buf where the callee should write the message
3209 * @return number of bytes written to buf
3212 notify_client_connection_failure (void *cls, size_t size, void *buf)
3215 struct MeshPeerInfo *peer_info;
3216 struct GNUNET_MESH_PeerControl *msg;
3217 struct GNUNET_PeerIdentity id;
3219 if (0 == size && NULL == buf)
3221 // TODO retry? cancel?
3225 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
3226 peer_info = (struct MeshPeerInfo *) cls;
3227 msg = (struct GNUNET_MESH_PeerControl *) buf;
3228 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
3229 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
3230 // msg->tunnel_id = htonl(peer_info->t->tid);
3231 GNUNET_PEER_resolve (peer_info->id, &id);
3232 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
3240 * Send keepalive packets for a peer
3242 * @param cls Closure (tunnel for which to send the keepalive).
3243 * @param tc Notification context.
3245 * TODO: implement explicit multicast keepalive?
3248 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3250 struct MeshTunnel *t = cls;
3251 struct GNUNET_MessageHeader *payload;
3252 struct GNUNET_MESH_Multicast *msg;
3254 sizeof (struct GNUNET_MESH_Multicast) +
3255 sizeof (struct GNUNET_MessageHeader);
3258 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3262 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
3264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3265 "MESH: sending keepalive for tunnel %d\n", t->id.tid);
3267 msg = (struct GNUNET_MESH_Multicast *) cbuf;
3268 msg->header.size = htons (size);
3269 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
3270 msg->oid = my_full_id;
3271 msg->tid = htonl (t->id.tid);
3272 payload = (struct GNUNET_MessageHeader *) &msg[1];
3273 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
3274 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
3275 tunnel_send_multicast (t, &msg->header);
3277 t->path_refresh_task =
3278 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
3284 * Function to process paths received for a new peer addition. The recorded
3285 * paths form the initial tunnel, which can be optimized later.
3286 * Called on each result obtained for the DHT search.
3288 * @param cls closure
3289 * @param exp when will this value expire
3290 * @param key key of the result
3291 * @param get_path path of the get request
3292 * @param get_path_length lenght of get_path
3293 * @param put_path path of the put request
3294 * @param put_path_length length of the put_path
3295 * @param type type of the result
3296 * @param size number of bytes in data
3297 * @param data pointer to the result data
3299 * TODO: re-issue the request after certain time? cancel after X results?
3302 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3303 const GNUNET_HashCode * key,
3304 const struct GNUNET_PeerIdentity *get_path,
3305 unsigned int get_path_length,
3306 const struct GNUNET_PeerIdentity *put_path,
3307 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3308 size_t size, const void *data)
3310 struct MeshPathInfo *path_info = cls;
3311 struct MeshPeerPath *p;
3312 struct GNUNET_PeerIdentity pi;
3315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Got results from DHT!\n");
3316 GNUNET_PEER_resolve (path_info->peer->id, &pi);
3317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: for %s\n", GNUNET_i2s (&pi));
3319 p = path_build_from_dht (get_path, get_path_length, put_path,
3321 path_add_to_peers (p, GNUNET_NO);
3323 for (i = 0; i < path_info->peer->ntunnels; i++)
3325 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
3326 peer_info_connect (path_info->peer, path_info->t);
3334 * Function to process paths received for a new peer addition. The recorded
3335 * paths form the initial tunnel, which can be optimized later.
3336 * Called on each result obtained for the DHT search.
3338 * @param cls closure
3339 * @param exp when will this value expire
3340 * @param key key of the result
3341 * @param get_path path of the get request
3342 * @param get_path_length lenght of get_path
3343 * @param put_path path of the put request
3344 * @param put_path_length length of the put_path
3345 * @param type type of the result
3346 * @param size number of bytes in data
3347 * @param data pointer to the result data
3350 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3351 const GNUNET_HashCode * key,
3352 const struct GNUNET_PeerIdentity *get_path,
3353 unsigned int get_path_length,
3354 const struct GNUNET_PeerIdentity *put_path,
3355 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3356 size_t size, const void *data)
3358 const struct GNUNET_PeerIdentity *pi = data;
3359 struct MeshTunnel *t = cls;
3360 struct MeshPeerInfo *peer_info;
3361 struct MeshPeerPath *p;
3363 if (size != sizeof (struct GNUNET_PeerIdentity))
3365 GNUNET_break_op (0);
3368 GNUNET_assert (NULL != t->client);
3369 peer_info = peer_info_get (pi);
3370 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
3372 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
3374 p = path_build_from_dht (get_path, get_path_length, put_path,
3376 path_add_to_peers (p, GNUNET_NO);
3378 tunnel_add_peer (t, peer_info);
3379 peer_info_connect (peer_info, t);
3383 /******************************************************************************/
3384 /********************* MESH LOCAL HANDLES **************************/
3385 /******************************************************************************/
3389 * Handler for client disconnection
3391 * @param cls closure
3392 * @param client identification of the client; NULL
3393 * for the last call when the server is destroyed
3396 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
3398 struct MeshClient *c;
3399 struct MeshClient *next;
3401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
3404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
3410 if (c->handle != client && NULL != client)
3412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
3416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
3417 GNUNET_SERVER_client_drop (c->handle);
3418 if (NULL != c->tunnels)
3420 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
3421 &tunnel_destroy_iterator, c);
3422 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
3425 /* deregister clients applications */
3426 if (NULL != c->apps)
3428 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
3429 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
3431 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
3432 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
3434 GNUNET_SCHEDULER_cancel (announce_applications_task);
3435 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3437 if (NULL != c->types)
3438 GNUNET_CONTAINER_multihashmap_destroy (c->types);
3440 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
3441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CLIENT FREE at %p\n", c);
3446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
3452 * Handler for new clients
3454 * @param cls closure
3455 * @param client identification of the client
3456 * @param message the actual message, which includes messages the client wants
3459 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
3460 const struct GNUNET_MessageHeader *message)
3462 struct GNUNET_MESH_ClientConnect *cc_msg;
3463 struct MeshClient *c;
3464 GNUNET_MESH_ApplicationType *a;
3471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
3472 /* Check data sanity */
3473 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
3474 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
3475 ntypes = ntohs (cc_msg->types);
3476 napps = ntohs (cc_msg->applications);
3478 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
3481 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3485 /* Create new client structure */
3486 c = GNUNET_malloc (sizeof (struct MeshClient));
3488 c->id = next_client_id++;
3489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CLIENT NEW %u at %p\n", c->id,
3493 GNUNET_SERVER_client_keep (client);
3494 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
3497 GNUNET_MESH_ApplicationType at;
3500 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
3501 for (i = 0; i < napps; i++)
3504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
3505 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
3506 /* store in clients hashmap */
3507 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
3508 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3509 /* store in global hashmap, for announcements */
3510 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
3511 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3513 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
3514 announce_applications_task =
3515 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
3523 t = (uint16_t *) & a[napps];
3524 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
3525 for (i = 0; i < ntypes; i++)
3528 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
3530 /* store in clients hashmap */
3531 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
3532 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3533 /* store in global hashmap */
3534 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
3535 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3539 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
3541 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
3542 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3543 GNUNET_SERVER_notification_context_add (nc, client);
3545 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
3553 * Handler for requests of new tunnels
3555 * @param cls closure
3556 * @param client identification of the client
3557 * @param message the actual message
3560 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
3561 const struct GNUNET_MessageHeader *message)
3563 struct GNUNET_MESH_TunnelMessage *t_msg;
3564 struct MeshTunnel *t;
3565 struct MeshClient *c;
3566 GNUNET_HashCode hash;
3568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
3570 /* Sanity check for client registration */
3571 if (NULL == (c = client_get (client)))
3574 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3581 /* Message sanity check */
3582 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3585 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3589 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3590 /* Sanity check for tunnel numbering */
3591 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
3594 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3597 /* Sanity check for duplicate tunnel IDs */
3598 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
3601 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3605 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3606 while (NULL != tunnel_get_by_pi (myid, next_tid))
3607 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3608 t->id.tid = next_tid++;
3609 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3611 t->local_tid = ntohl (t_msg->tunnel_id);
3613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATED TUNNEL %s [%x] (%x)\n",
3614 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
3617 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
3619 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3621 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
3622 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3625 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3629 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3631 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3632 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3635 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3638 t->tree = tree_new (myid);
3640 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3646 * Handler for requests of deleting tunnels
3648 * @param cls closure
3649 * @param client identification of the client
3650 * @param message the actual message
3653 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
3654 const struct GNUNET_MessageHeader *message)
3656 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
3657 struct MeshClient *c;
3658 struct MeshTunnel *t;
3659 MESH_TunnelNumber tid;
3660 GNUNET_HashCode hash;
3662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3663 "MESH: Got a DESTROY TUNNEL from client!\n");
3665 /* Sanity check for client registration */
3666 if (NULL == (c = client_get (client)))
3669 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3672 /* Message sanity check */
3673 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3676 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3682 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3684 /* Retrieve tunnel */
3685 tid = ntohl (tunnel_msg->tunnel_id);
3687 /* Remove from local id hashmap */
3688 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3689 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
3690 GNUNET_assert (GNUNET_YES ==
3691 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t));
3694 tunnel_send_destroy (t);
3696 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3702 * Handler for connection requests to new peers
3704 * @param cls closure
3705 * @param client identification of the client
3706 * @param message the actual message (PeerControl)
3709 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
3710 const struct GNUNET_MessageHeader *message)
3712 struct GNUNET_MESH_PeerControl *peer_msg;
3713 struct MeshPeerInfo *peer_info;
3714 struct MeshClient *c;
3715 struct MeshTunnel *t;
3716 MESH_TunnelNumber tid;
3718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Got connection request\n");
3719 /* Sanity check for client registration */
3720 if (NULL == (c = client_get (client)))
3723 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3727 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3728 /* Sanity check for message size */
3729 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3732 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3736 /* Tunnel exists? */
3737 tid = ntohl (peer_msg->tunnel_id);
3738 t = tunnel_get_by_local_id (c, tid);
3742 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3746 /* Does client own tunnel? */
3747 if (t->client->handle != client)
3750 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: for %s\n",
3754 GNUNET_i2s (&peer_msg->peer));
3755 peer_info = peer_info_get (&peer_msg->peer);
3757 tunnel_add_peer (t, peer_info);
3758 peer_info_connect (peer_info, t);
3760 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3766 * Handler for disconnection requests of peers in a tunnel
3768 * @param cls closure
3769 * @param client identification of the client
3770 * @param message the actual message (PeerControl)
3773 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
3774 const struct GNUNET_MessageHeader *message)
3776 struct GNUNET_MESH_PeerControl *peer_msg;
3777 struct MeshPeerInfo *peer_info;
3778 struct MeshClient *c;
3779 struct MeshTunnel *t;
3780 MESH_TunnelNumber tid;
3782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Got a PEER DEL request\n");
3783 /* Sanity check for client registration */
3784 if (NULL == (c = client_get (client)))
3787 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3790 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3791 /* Sanity check for message size */
3792 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3795 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3799 /* Tunnel exists? */
3800 tid = ntohl (peer_msg->tunnel_id);
3801 t = tunnel_get_by_local_id (c, tid);
3805 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: on tunnel %X\n", t->id.tid);
3810 /* Does client own tunnel? */
3811 if (t->client->handle != client)
3814 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: for peer %s\n",
3819 GNUNET_i2s (&peer_msg->peer));
3820 /* Is the peer in the tunnel? */
3822 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
3823 if (NULL == peer_info)
3826 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3830 /* Ok, delete peer from tunnel */
3831 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
3832 &peer_msg->peer.hashPubKey);
3834 send_destroy_path (t, peer_info->id);
3835 tunnel_delete_peer (t, peer_info->id);
3836 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3842 * Handler for connection requests to new peers by type
3844 * @param cls closure
3845 * @param client identification of the client
3846 * @param message the actual message (ConnectPeerByType)
3849 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
3850 const struct GNUNET_MessageHeader *message)
3852 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
3853 struct MeshClient *c;
3854 struct MeshTunnel *t;
3855 GNUNET_HashCode hash;
3856 MESH_TunnelNumber tid;
3858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
3859 /* Sanity check for client registration */
3860 if (NULL == (c = client_get (client)))
3863 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3867 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
3868 /* Sanity check for message size */
3869 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
3870 ntohs (connect_msg->header.size))
3873 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3877 /* Tunnel exists? */
3878 tid = ntohl (connect_msg->tunnel_id);
3879 t = tunnel_get_by_local_id (c, tid);
3883 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3887 /* Does client own tunnel? */
3888 if (t->client->handle != client)
3891 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3895 /* Do WE have the service? */
3896 t->type = ntohl (connect_msg->type);
3897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", t->type);
3898 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
3899 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
3902 /* Yes! Fast forward, add ourselves to the tunnel and send the
3903 * good news to the client, and alert the destination client of
3904 * an incoming tunnel.
3906 struct GNUNET_MESH_TunnelNotification cmsg;
3907 struct MeshClient *c;
3909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
3910 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
3911 peer_info_get (&my_full_id),
3912 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
3915 send_client_peer_connected (t, myid);
3916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
3917 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3919 /* FIXME implement a proper handling of this case,
3920 a client differentiation mechanism */
3921 cmsg.header.size = htons (sizeof (cmsg));
3922 cmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
3923 cmsg.peer = my_full_id;
3924 t->local_tid_dest = next_local_tid++;
3925 cmsg.tunnel_id = htonl (t->local_tid_dest);
3926 c = (struct MeshClient *) GNUNET_CONTAINER_multihashmap_get(applications,
3928 GNUNET_SERVER_notification_context_unicast (nc, c->handle, &cmsg.header,
3934 /* Ok, lets find a peer offering the service */
3935 if (NULL != t->dht_get_type)
3937 GNUNET_DHT_get_stop (t->dht_get_type);
3939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
3940 GNUNET_h2s (&hash));
3942 GNUNET_DHT_get_start (dht_handle, GNUNET_TIME_UNIT_FOREVER_REL,
3943 GNUNET_BLOCK_TYPE_TEST, &hash, 10U,
3944 GNUNET_DHT_RO_RECORD_ROUTE |
3945 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, 0,
3946 &dht_get_type_handler, t);
3948 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3954 * Handler for client traffic directed to one peer
3956 * @param cls closure
3957 * @param client identification of the client
3958 * @param message the actual message
3961 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
3962 const struct GNUNET_MessageHeader *message)
3964 struct MeshClient *c;
3965 struct MeshTunnel *t;
3966 struct MeshPeerInfo *pi;
3967 struct GNUNET_MESH_Unicast *data_msg;
3968 MESH_TunnelNumber tid;
3971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3972 "MESH: Got a unicast request from a client!\n");
3974 /* Sanity check for client registration */
3975 if (NULL == (c = client_get (client)))
3978 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3981 data_msg = (struct GNUNET_MESH_Unicast *) message;
3982 /* Sanity check for message size */
3983 size = ntohs (message->size);
3984 if (sizeof (struct GNUNET_MESH_Unicast) +
3985 sizeof (struct GNUNET_MessageHeader) > size)
3988 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3992 /* Tunnel exists? */
3993 tid = ntohl (data_msg->tid);
3994 t = tunnel_get_by_local_id (c, tid);
3998 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4002 /* Is it a local tunnel? Then, does client own the tunnel? */
4003 if (NULL != t->client && NULL != t->client->handle &&
4004 t->client->handle != client)
4007 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4011 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
4012 &data_msg->destination.hashPubKey);
4013 /* Is the selected peer in the tunnel? */
4017 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4021 /* Ok, everything is correct, send the message
4022 * (pretend we got it from a mesh peer)
4025 char buf[ntohs (message->size)];
4026 struct GNUNET_MESH_Unicast *copy;
4028 /* Work around const limitation */
4029 copy = (struct GNUNET_MESH_Unicast *) buf;
4030 memcpy (buf, data_msg, size);
4031 copy->oid = my_full_id;
4032 copy->tid = htonl (t->id.tid);
4033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4034 "MESH: calling generic handler...\n");
4035 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
4037 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4043 * Handler for client traffic directed to the origin
4045 * @param cls closure
4046 * @param client identification of the client
4047 * @param message the actual message
4050 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
4051 const struct GNUNET_MessageHeader *message)
4053 struct GNUNET_MESH_ToOrigin *data_msg;
4054 struct GNUNET_PeerIdentity id;
4055 struct MeshClient *c;
4056 struct MeshTunnel *t;
4057 MESH_TunnelNumber tid;
4060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4061 "MESH: Got a ToOrigin request from a client!\n");
4063 /* Sanity check for client registration */
4064 if (NULL == (c = client_get (client)))
4067 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4070 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
4071 /* Sanity check for message size */
4072 size = ntohs (message->size);
4073 if (sizeof (struct GNUNET_MESH_ToOrigin) +
4074 sizeof (struct GNUNET_MessageHeader) > size)
4077 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4081 /* Tunnel exists? */
4082 tid = ntohl (data_msg->tid);
4083 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4086 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4089 t = tunnel_get_by_local_id (c, tid);
4093 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4097 /* It shouldn't be a local tunnel. */
4098 if (NULL != t->client)
4101 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4104 GNUNET_PEER_resolve (t->id.oid, &id);
4106 /* Ok, everything is correct, send the message
4107 * (pretend we got it from a mesh peer)
4110 char buf[ntohs (message->size)];
4111 struct GNUNET_MESH_ToOrigin *copy;
4113 /* Work around const limitation */
4114 copy = (struct GNUNET_MESH_ToOrigin *) buf;
4115 memcpy (buf, data_msg, size);
4117 copy->tid = htonl (t->id.tid);
4118 copy->sender = my_full_id;
4119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4120 "MESH: calling generic handler...\n");
4121 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
4123 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4129 * Handler for client traffic directed to all peers in a tunnel
4131 * @param cls closure
4132 * @param client identification of the client
4133 * @param message the actual message
4136 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
4137 const struct GNUNET_MessageHeader *message)
4139 struct MeshClient *c;
4140 struct MeshTunnel *t;
4141 struct GNUNET_MESH_Multicast *data_msg;
4142 MESH_TunnelNumber tid;
4144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4145 "MESH: Got a multicast request from a client!\n");
4147 /* Sanity check for client registration */
4148 if (NULL == (c = client_get (client)))
4151 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4154 data_msg = (struct GNUNET_MESH_Multicast *) message;
4155 /* Sanity check for message size */
4156 if (sizeof (struct GNUNET_MESH_Multicast) +
4157 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
4160 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4164 /* Tunnel exists? */
4165 tid = ntohl (data_msg->tid);
4166 t = tunnel_get_by_local_id (c, tid);
4170 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4174 /* Does client own tunnel? */
4175 if (t->client->handle != client)
4178 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4183 char buf[ntohs (message->size)];
4184 struct GNUNET_MESH_Multicast *copy;
4186 copy = (struct GNUNET_MESH_Multicast *) buf;
4187 memcpy (buf, message, ntohs (message->size));
4188 copy->oid = my_full_id;
4189 copy->tid = htonl (t->id.tid);
4190 copy->ttl = htonl (DEFAULT_TTL);
4191 copy->mid = htonl (t->mid + 1);
4192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4193 "MESH: calling generic handler...\n");
4194 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
4197 /* receive done gets called when last copy is sent to a neighbor */
4202 * Functions to handle messages from clients
4204 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
4205 {&handle_local_new_client, NULL,
4206 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
4207 {&handle_local_tunnel_create, NULL,
4208 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
4209 sizeof (struct GNUNET_MESH_TunnelMessage)},
4210 {&handle_local_tunnel_destroy, NULL,
4211 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
4212 sizeof (struct GNUNET_MESH_TunnelMessage)},
4213 {&handle_local_connect_add, NULL,
4214 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
4215 sizeof (struct GNUNET_MESH_PeerControl)},
4216 {&handle_local_connect_del, NULL,
4217 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
4218 sizeof (struct GNUNET_MESH_PeerControl)},
4219 {&handle_local_connect_by_type, NULL,
4220 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
4221 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
4222 {&handle_local_unicast, NULL,
4223 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4224 {&handle_local_to_origin, NULL,
4225 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4226 {&handle_local_multicast, NULL,
4227 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
4233 * To be called on core init/fail.
4235 * @param cls service closure
4236 * @param server handle to the server for this service
4237 * @param identity the public identity of this peer
4240 core_init (void *cls, struct GNUNET_CORE_Handle *server,
4241 const struct GNUNET_PeerIdentity *identity)
4243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
4244 core_handle = server;
4245 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
4248 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
4249 GNUNET_SCHEDULER_shutdown ();
4255 * Method called whenever a given peer connects.
4257 * @param cls closure
4258 * @param peer peer identity this notification is about
4259 * @param atsi performance data for the connection
4260 * @param atsi_count number of records in 'atsi'
4263 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
4264 const struct GNUNET_ATS_Information *atsi,
4265 unsigned int atsi_count)
4267 struct MeshPeerInfo *peer_info;
4268 struct MeshPeerPath *path;
4270 #if MESH_DEBUG_CONNECTION
4271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
4272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
4273 GNUNET_i2s (&my_full_id));
4275 peer_info = peer_info_get (peer);
4276 if (myid == peer_info->id)
4278 #if MESH_DEBUG_CONNECTION
4279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
4283 #if MESH_DEBUG_CONNECTION
4286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n", GNUNET_i2s (peer));
4289 path = path_new (2);
4290 path->peers[0] = myid;
4291 path->peers[1] = peer_info->id;
4292 GNUNET_PEER_change_rc (myid, 1);
4293 GNUNET_PEER_change_rc (peer_info->id, 1);
4294 peer_info_add_path (peer_info, path, GNUNET_YES);
4299 * Method called whenever a peer disconnects.
4301 * @param cls closure
4302 * @param peer peer identity this notification is about
4305 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4307 struct MeshPeerInfo *pi;
4310 #if MESH_DEBUG_CONNECTION
4311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
4313 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4319 for (i = 0; i < CORE_QUEUE_SIZE; i++)
4321 /* TODO: notify that the transmission failed */
4322 peer_info_cancel_transmission (pi, i);
4324 peer_info_remove_path (pi, pi->id, myid);
4325 #if MESH_DEBUG_CONNECTION
4328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
4335 /******************************************************************************/
4336 /************************ MAIN FUNCTIONS ****************************/
4337 /******************************************************************************/
4340 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
4342 * @param cls closure
4343 * @param key current key code
4344 * @param value value in the hash map
4345 * @return GNUNET_YES if we should continue to iterate,
4349 shutdown_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
4351 struct MeshTunnel *t = value;
4358 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
4360 * @param cls closure
4361 * @param key current key code
4362 * @param value value in the hash map
4363 * @return GNUNET_YES if we should continue to iterate,
4367 shutdown_peer (void *cls, const GNUNET_HashCode * key, void *value)
4369 struct MeshPeerInfo *p = value;
4371 peer_info_destroy (p);
4376 * Task run during shutdown.
4382 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
4386 if (core_handle != NULL)
4388 GNUNET_CORE_disconnect (core_handle);
4391 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
4392 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
4393 if (dht_handle != NULL)
4395 GNUNET_DHT_disconnect (dht_handle);
4400 GNUNET_SERVER_notification_context_destroy (nc);
4403 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
4405 GNUNET_SCHEDULER_cancel (announce_id_task);
4406 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
4408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
4412 * Process mesh requests.
4414 * @param cls closure
4415 * @param server the initialized server
4416 * @param c configuration to use
4419 run (void *cls, struct GNUNET_SERVER_Handle *server,
4420 const struct GNUNET_CONFIGURATION_Handle *c)
4422 struct MeshPeerInfo *peer;
4423 struct MeshPeerPath *p;
4426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
4427 server_handle = server;
4428 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4429 CORE_QUEUE_SIZE, /* queue size */
4430 NULL, /* Closure passed to MESH functions */
4431 &core_init, /* Call core_init once connected */
4432 &core_connect, /* Handle connects */
4433 &core_disconnect, /* remove peers on disconnects */
4434 NULL, /* Don't notify about all incoming messages */
4435 GNUNET_NO, /* For header only in notification */
4436 NULL, /* Don't notify about all outbound messages */
4437 GNUNET_NO, /* For header-only out notification */
4438 core_handlers); /* Register these handlers */
4440 if (core_handle == NULL)
4443 GNUNET_SCHEDULER_shutdown ();
4448 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
4451 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4453 ("Mesh service is lacking key configuration settings. Exiting.\n"));
4454 GNUNET_SCHEDULER_shutdown ();
4457 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
4458 GNUNET_free (keyfile);
4459 if (my_private_key == NULL)
4461 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4462 _("Mesh service could not access hostkey. Exiting.\n"));
4463 GNUNET_SCHEDULER_shutdown ();
4466 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
4467 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
4468 &my_full_id.hashPubKey);
4469 myid = GNUNET_PEER_intern (&my_full_id);
4471 // // transport_handle = GNUNET_TRANSPORT_connect(c,
4478 dht_handle = GNUNET_DHT_connect (c, 64);
4479 if (dht_handle == NULL)
4485 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4487 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4488 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4489 peers = GNUNET_CONTAINER_multihashmap_create (32);
4490 applications = GNUNET_CONTAINER_multihashmap_create (32);
4491 types = GNUNET_CONTAINER_multihashmap_create (32);
4493 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
4494 nc = GNUNET_SERVER_notification_context_create (server_handle,
4496 GNUNET_SERVER_disconnect_notify (server_handle,
4497 &handle_local_client_disconnect, NULL);
4501 clients_tail = NULL;
4506 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
4507 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
4509 /* Create a peer_info for the local peer */
4510 peer = peer_info_get (&my_full_id);
4513 GNUNET_PEER_change_rc (myid, 1);
4514 peer_info_add_path (peer, p, GNUNET_YES);
4516 /* Scheduled the task to clean up when shutdown is called */
4517 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
4520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
4524 * The main function for the mesh service.
4526 * @param argc number of arguments from the command line
4527 * @param argv command line arguments
4528 * @return 0 ok, 1 on error
4531 main (int argc, char *const *argv)
4535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
4538 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
4540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");