+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Own position: %u\n", own_pos);
+ if (own_pos == 0)
+ {
+ /* cannot be self, must be 'not found' */
+ /* create path: self not found in path through self */
+ GNUNET_break_op (0);
+ path_destroy (path);
+ /* FIXME error. destroy tunnel? leave for timeout? */
+ return 0;
+ }
+ path_add_to_peers (path, GNUNET_NO);
+ tunnel_add_path (t, path, own_pos);
+ if (own_pos == size - 1)
+ {
+ /* It is for us! Send ack. */
+ struct MeshTransmissionDescriptor *info;
+ unsigned int j;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: It's for us!\n");
+ peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
+ if (NULL == t->peers)
+ {
+ /* New tunnel! Notify clients! */
+ struct GNUNET_MESH_TunnelNotification cmsg;
+
+ cmsg.header.size = htons (sizeof (cmsg));
+ cmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
+ GNUNET_PEER_resolve (t->id.oid, &cmsg.peer);
+ cmsg.tunnel_id = htonl (t->local_tid);
+ GNUNET_SERVER_notification_context_broadcast (nc, &cmsg.header,
+ GNUNET_NO);
+
+ t->peers = GNUNET_CONTAINER_multihashmap_create (4);
+ }
+ GNUNET_break (GNUNET_OK ==
+ GNUNET_CONTAINER_multihashmap_put (t->peers,
+ &my_full_id.hashPubKey,
+ peer_info_get
+ (&my_full_id),
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
+ /* FIXME use send_message */
+ info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
+ info->origin = &t->id;
+ info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
+ GNUNET_assert (NULL != info->peer);
+ j = peer_info_transmit_slot (info->peer);
+ info->handler_n = j;
+ info->peer->types[j] = GNUNET_MESSAGE_TYPE_MESH_PATH_ACK;
+ info->peer->infos[j] = info;
+ info->peer->core_transmit[j] =
+ GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
+ GNUNET_TIME_UNIT_FOREVER_REL, peer,
+ sizeof (struct GNUNET_MESH_PathACK),
+ &send_core_path_ack, info);
+ }
+ else
+ {
+ struct MeshPeerPath *path2;
+
+ /* It's for somebody else! Retransmit. */
+ path2 = path_duplicate (path);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Retransmitting.\n");
+ peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
+ path2 = path_duplicate (path);
+ peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
+ send_create_path (dest_peer_info, path, t);
+ }
+ return GNUNET_OK;
+}
+
+
+/**
+ * Core handler for path destruction
+ *
+ * @param cls closure
+ * @param message message
+ * @param peer peer identity this notification is about
+ * @param atsi performance data
+ * @param atsi_count number of records in 'atsi'
+ *
+ * @return GNUNET_OK to keep the connection open,
+ * GNUNET_SYSERR to close it (signal serious error)
+ */
+static int
+handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
+ const struct GNUNET_MessageHeader *message,
+ const struct GNUNET_ATS_Information *atsi,
+ unsigned int atsi_count)
+{
+ struct GNUNET_MESH_ManipulatePath *msg;
+ struct GNUNET_PeerIdentity *pi;
+ struct MeshPeerPath *path;
+ struct MeshTunnel *t;
+ unsigned int own_pos;
+ unsigned int i;
+ size_t size;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "MESH: Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
+ size = ntohs (message->size);
+ if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_OK;
+ }
+
+ size -= sizeof (struct GNUNET_MESH_ManipulatePath);
+ if (size % sizeof (struct GNUNET_PeerIdentity))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_OK;
+ }
+ size /= sizeof (struct GNUNET_PeerIdentity);
+ if (size < 2)
+ {
+ GNUNET_break_op (0);
+ return GNUNET_OK;
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: path has %u hops.\n", size);
+
+ msg = (struct GNUNET_MESH_ManipulatePath *) message;
+ pi = (struct GNUNET_PeerIdentity *) &msg[1];
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "MESH: path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
+ msg->tid);
+ t = tunnel_get (pi, ntohl (msg->tid));
+ if (NULL == t)
+ {
+ /* TODO notify back: we don't know this tunnel */
+ GNUNET_break_op (0);
+ return GNUNET_OK;
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating path...\n");
+ path = path_new (size);
+ own_pos = 0;
+ for (i = 0; i < size; i++)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... adding %s\n",
+ GNUNET_i2s (&pi[i]));
+ path->peers[i] = GNUNET_PEER_intern (&pi[i]);
+ if (path->peers[i] == myid)
+ own_pos = i;
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Own position: %u\n", own_pos);
+ if (own_pos < path->length - 1)
+ send_message (message, &pi[own_pos + 1]);
+ tunnel_delete_peer (t, path->peers[path->length - 1]);
+ path_destroy (path);
+ return GNUNET_OK;
+}
+
+
+/**
+ * Core handler for notifications of broken paths
+ *
+ * @param cls closure
+ * @param message message
+ * @param peer peer identity this notification is about
+ * @param atsi performance data
+ * @param atsi_count number of records in 'atsi'
+ *
+ * @return GNUNET_OK to keep the connection open,
+ * GNUNET_SYSERR to close it (signal serious error)
+ */
+static int
+handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
+ const struct GNUNET_MessageHeader *message,
+ const struct GNUNET_ATS_Information *atsi,
+ unsigned int atsi_count)
+{
+ struct GNUNET_MESH_PathBroken *msg;
+ struct MeshTunnel *t;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "MESH: Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
+ msg = (struct GNUNET_MESH_PathBroken *) message;
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: regarding %s\n",
+ GNUNET_i2s (&msg->peer1));
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: regarding %s\n",
+ GNUNET_i2s (&msg->peer2));
+ t = tunnel_get (&msg->oid, ntohl (msg->tid));
+ if (NULL == t)
+ {
+ GNUNET_break_op (0);
+ return GNUNET_OK;
+ }
+ tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
+ GNUNET_PEER_search (&msg->peer2));
+ return GNUNET_OK;
+
+}
+
+
+/**
+ * Core handler for tunnel destruction
+ *
+ * @param cls closure
+ * @param message message
+ * @param peer peer identity this notification is about
+ * @param atsi performance data
+ * @param atsi_count number of records in 'atsi'
+ *
+ * @return GNUNET_OK to keep the connection open,
+ * GNUNET_SYSERR to close it (signal serious error)
+ */
+static int
+handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
+ const struct GNUNET_MessageHeader *message,
+ const struct GNUNET_ATS_Information *atsi,
+ unsigned int atsi_count)
+{
+ struct GNUNET_MESH_TunnelDestroy *msg;
+ struct MeshTunnel *t;
+