firmware/src/modules/TraceRouteModule.cpp

89 lines
2.9 KiB
C++
Raw Normal View History

2022-12-05 19:38:06 +00:00
#include "TraceRouteModule.h"
#include "FloodingRouter.h"
2023-01-21 13:34:29 +00:00
#include "MeshService.h"
2022-12-05 19:38:06 +00:00
2023-01-21 13:34:29 +00:00
TraceRouteModule *traceRouteModule;
2022-12-05 19:38:06 +00:00
bool TraceRouteModule::handleReceivedProtobuf(const meshtastic_MeshPacket &mp, meshtastic_RouteDiscovery *r)
2023-01-21 13:34:29 +00:00
{
2022-12-05 19:38:06 +00:00
// Only handle a response
if (mp.decoded.request_id) {
printRoute(r, mp.to, mp.from);
}
2023-01-21 13:34:29 +00:00
2022-12-05 19:38:06 +00:00
return false; // let it be handled by RoutingModule
}
void TraceRouteModule::updateRoute(meshtastic_MeshPacket *p)
2023-01-21 13:34:29 +00:00
{
2022-12-05 19:38:06 +00:00
auto &incoming = p->decoded;
// Only append an ID for the request (one way)
2023-01-21 13:34:29 +00:00
if (!incoming.request_id) {
meshtastic_RouteDiscovery scratch;
meshtastic_RouteDiscovery *updated = NULL;
2022-12-05 19:38:06 +00:00
memset(&scratch, 0, sizeof(scratch));
pb_decode_from_bytes(incoming.payload.bytes, incoming.payload.size, &meshtastic_RouteDiscovery_msg, &scratch);
2022-12-05 19:38:06 +00:00
updated = &scratch;
appendMyID(updated);
printRoute(updated, p->from, NODENUM_BROADCAST);
2023-01-21 13:34:29 +00:00
2022-12-05 19:38:06 +00:00
// Set updated route to the payload of the to be flooded packet
2023-01-21 17:39:58 +00:00
p->decoded.payload.size = pb_encode_to_bytes(p->decoded.payload.bytes, sizeof(p->decoded.payload.bytes),
&meshtastic_RouteDiscovery_msg, updated);
2022-12-05 19:38:06 +00:00
}
}
void TraceRouteModule::appendMyID(meshtastic_RouteDiscovery *updated)
2022-12-05 19:38:06 +00:00
{
// Length of route array can normally not be exceeded due to the max. hop_limit of 7
2023-01-21 13:34:29 +00:00
if (updated->route_count < sizeof(updated->route) / sizeof(updated->route[0])) {
2022-12-05 19:38:06 +00:00
updated->route[updated->route_count] = myNodeInfo.my_node_num;
2023-01-21 13:34:29 +00:00
updated->route_count += 1;
2022-12-05 19:38:06 +00:00
} else {
2022-12-30 02:41:37 +00:00
LOG_WARN("Route exceeded maximum hop limit, are you bridging networks?\n");
2022-12-05 19:38:06 +00:00
}
}
void TraceRouteModule::printRoute(meshtastic_RouteDiscovery *r, uint32_t origin, uint32_t dest)
2022-12-05 19:38:06 +00:00
{
#ifdef DEBUG_PORT
LOG_INFO("Route traced:\n");
LOG_INFO("0x%x --> ", origin);
2023-01-21 13:34:29 +00:00
for (uint8_t i = 0; i < r->route_count; i++) {
LOG_INFO("0x%x --> ", r->route[i]);
2022-12-05 19:38:06 +00:00
}
2023-01-21 13:34:29 +00:00
if (dest != NODENUM_BROADCAST)
LOG_INFO("0x%x\n", dest);
else
LOG_INFO("...\n");
#endif
2022-12-05 19:38:06 +00:00
}
meshtastic_MeshPacket *TraceRouteModule::allocReply()
2022-12-05 19:38:06 +00:00
{
assert(currentRequest);
// Copy the payload of the current request
auto req = *currentRequest;
auto &p = req.decoded;
meshtastic_RouteDiscovery scratch;
meshtastic_RouteDiscovery *updated = NULL;
2022-12-05 19:38:06 +00:00
memset(&scratch, 0, sizeof(scratch));
pb_decode_from_bytes(p.payload.bytes, p.payload.size, &meshtastic_RouteDiscovery_msg, &scratch);
2022-12-05 19:38:06 +00:00
updated = &scratch;
printRoute(updated, req.from, req.to);
// Create a MeshPacket with this payload and set it as the reply
meshtastic_MeshPacket *reply = allocDataProtobuf(*updated);
2022-12-05 19:38:06 +00:00
return reply;
}
2023-01-21 17:39:58 +00:00
TraceRouteModule::TraceRouteModule()
: ProtobufModule("traceroute", meshtastic_PortNum_TRACEROUTE_APP, &meshtastic_RouteDiscovery_msg)
2023-01-21 13:34:29 +00:00
{
ourPortNum = meshtastic_PortNum_TRACEROUTE_APP;
2022-12-05 19:38:06 +00:00
}