+/**
+ * Handler for a message we received along some trail.
+ *
+ * @param cls closure
+ * @param trail_id trail identifier
+ * @param trail_path path the message took on the trail, if available
+ * @param trail_path_length number of entries on the @a trail_path
+ * @param message the message we got
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
+ */
+typedef int
+(*TrailHandlerCallback)(void *cls,
+ const struct GNUNET_HashCode *trail_id,
+ const struct GNUNET_PeerIdentity *trail_path,
+ unsigned int trail_path_length,
+ const struct GNUNET_MessageHeader *message);
+
+
+/**
+ * Definition of a handler for a message received along some trail.
+ */
+struct TrailHandler
+{
+ /**
+ * NULL for end-of-list.
+ */
+ TrailHandlerCallback callback;
+
+ /**
+ * Closure for @e callback.
+ */
+ void *cls;
+
+ /**
+ * Message type this handler addresses.
+ */
+ uint16_t message_type;
+
+ /**
+ * Use 0 for variable-size.
+ */
+ uint16_t message_size;
+};
+
+
+/**
+ * Check that a `struct TrailRouteMessage` is well-formed.
+ *
+ * @param cls closure
+ * @param trm the finger destroy message
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
+ */
+static int
+check_dht_p2p_trail_route (void *cls,
+ const struct TrailRouteMessage *trm)
+{
+ const struct GNUNET_PeerIdentity *path;
+ uint16_t path_length;
+ const struct GNUNET_MessageHeader *payload;
+ size_t msize;
+
+ msize = ntohs (trm->header.size);
+ path_length = ntohs (trm->path_length);
+ if (msize < sizeof (struct TrailRouteMessage) +
+ path_length * sizeof (struct GNUNET_PeerIdentity) +
+ sizeof (struct GNUNET_MessageHeader) )
+ {
+ GNUNET_break_op (0);
+ return GNUNET_SYSERR;
+ }
+ path = (const struct GNUNET_PeerIdentity *) &trm[1];
+ payload = (const struct GNUNET_MessageHeader *) &path[path_length];
+ if (msize != (ntohs (payload->size) +
+ sizeof (struct TrailRouteMessage) +
+ path_length * sizeof (struct GNUNET_PeerIdentity)))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_SYSERR;
+ }
+ /* FIXME: verify payload is OK!? */
+ return GNUNET_OK;
+}
+
+
+/**
+ * Handle a `struct TrailRouteMessage`.
+ *
+ * @param cls closure
+ * @param trm the finger destroy message
+ */
+static void
+handle_dht_p2p_trail_route (void *cls,
+ const struct TrailRouteMessage *trm)
+{
+ static const struct TrailHandler handlers[] = {
+ { &handle_dht_p2p_successor_find, NULL,
+ GNUNET_MESSAGE_TYPE_WDHT_SUCCESSOR_FIND,
+ sizeof (struct FindSuccessorMessage) },
+ { &handle_dht_p2p_peer_get, NULL,
+ GNUNET_MESSAGE_TYPE_WDHT_GET,
+ 0 },
+ { &handle_dht_p2p_peer_get_result, NULL,
+ GNUNET_MESSAGE_TYPE_WDHT_GET_RESULT,
+ 0 },
+ { &handle_dht_p2p_peer_put, NULL,
+ GNUNET_MESSAGE_TYPE_WDHT_PUT,
+ 0 },
+ { NULL, NULL, 0, 0 }
+ };
+ struct FriendInfo *sender = cls;
+ unsigned int i;
+ const struct GNUNET_PeerIdentity *path;
+ uint16_t path_length;
+ const struct GNUNET_MessageHeader *payload;
+ const struct TrailHandler *th;
+ struct Trail *trail;
+
+ path_length = ntohs (trm->path_length);
+ path = (const struct GNUNET_PeerIdentity *) &trm[1];
+ payload = (const struct GNUNET_MessageHeader *) &path[path_length];
+ /* Is this message for us? */
+ trail = GNUNET_CONTAINER_multihashmap_get (trail_map,
+ &trm->trail_id);
+ if ( (NULL != trail->pred) &&
+ (0 == memcmp (sender->id,
+ &trail->pred->id,
+ sizeof (struct GNUNET_PeerIdentity))) )
+ {
+ /* forward to 'successor' */
+ if (NULL != trail->succ)
+ {
+ forward_message_on_trail (trail->succ,
+ &trail->succ_id,
+ ntohs (trm->record_path),
+ sender->id,
+ path,
+ path_length,
+ payload);
+ return;
+ }
+ }
+ else
+ {
+ /* forward to 'predecessor' */
+ GNUNET_break_op ( (NULL != trail->succ) &&
+ (0 == memcmp (sender->id,
+ &trail->succ->id,
+ sizeof (struct GNUNET_PeerIdentity))) );
+ if (NULL != trail->pred)
+ {
+ forward_message_on_trail (trail->pred,
+ &trail->pred_id,
+ ntohs (trm->record_path),
+ sender->id,
+ path,
+ path_length,
+ payload);
+ return;
+ }
+ }
+
+ /* Message is for us, dispatch to handler */
+ th = NULL;
+ for (i=0; NULL != handlers[i].callback; i++)
+ {
+ th = &handlers[i];
+ if (ntohs (payload->type) == th->message_type)
+ {
+ if ( (0 == th->message_size) ||
+ (ntohs (payload->size) == th->message_size) )
+ th->callback (th->cls,
+ &trm->trail_id,
+ path,
+ path_length,
+ payload);
+ else
+ GNUNET_break_op (0);
+ break;
+ }
+ }
+ GNUNET_break_op (NULL != th);
+}
+
+