Check if packet is decrypted before using portnum when converting to JSON (#2857)

Co-authored-by: Ben Meadors <benmmeadors@gmail.com>
This commit is contained in:
GUVWAF 2023-10-03 13:39:35 +02:00 committed by GitHub
parent f301e236eb
commit 37c3d15978
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -516,34 +516,34 @@ std::string MQTT::meshPacketToJson(meshtastic_MeshPacket *mp)
JSONObject msgPayload; JSONObject msgPayload;
JSONObject jsonObj; JSONObject jsonObj;
switch (mp->decoded.portnum) { if (mp->which_payload_variant == meshtastic_MeshPacket_decoded_tag) {
case meshtastic_PortNum_TEXT_MESSAGE_APP: { switch (mp->decoded.portnum) {
msgType = "text"; case meshtastic_PortNum_TEXT_MESSAGE_APP: {
// convert bytes to string msgType = "text";
LOG_DEBUG("got text message of size %u\n", mp->decoded.payload.size); // convert bytes to string
char payloadStr[(mp->decoded.payload.size) + 1]; LOG_DEBUG("got text message of size %u\n", mp->decoded.payload.size);
memcpy(payloadStr, mp->decoded.payload.bytes, mp->decoded.payload.size); char payloadStr[(mp->decoded.payload.size) + 1];
payloadStr[mp->decoded.payload.size] = 0; // null terminated string memcpy(payloadStr, mp->decoded.payload.bytes, mp->decoded.payload.size);
// check if this is a JSON payload payloadStr[mp->decoded.payload.size] = 0; // null terminated string
JSONValue *json_value = JSON::Parse(payloadStr); // check if this is a JSON payload
if (json_value != NULL) { JSONValue *json_value = JSON::Parse(payloadStr);
LOG_INFO("text message payload is of type json\n"); if (json_value != NULL) {
// if it is, then we can just use the json object LOG_INFO("text message payload is of type json\n");
jsonObj["payload"] = json_value; // if it is, then we can just use the json object
} else { jsonObj["payload"] = json_value;
// if it isn't, then we need to create a json object } else {
// with the string as the value // if it isn't, then we need to create a json object
LOG_INFO("text message payload is of type plaintext\n"); // with the string as the value
msgPayload["text"] = new JSONValue(payloadStr); LOG_INFO("text message payload is of type plaintext\n");
jsonObj["payload"] = new JSONValue(msgPayload); msgPayload["text"] = new JSONValue(payloadStr);
jsonObj["payload"] = new JSONValue(msgPayload);
}
break;
} }
break; case meshtastic_PortNum_TELEMETRY_APP: {
} msgType = "telemetry";
case meshtastic_PortNum_TELEMETRY_APP: { meshtastic_Telemetry scratch;
msgType = "telemetry"; meshtastic_Telemetry *decoded = NULL;
meshtastic_Telemetry scratch;
meshtastic_Telemetry *decoded = NULL;
if (mp->which_payload_variant == meshtastic_MeshPacket_decoded_tag) {
memset(&scratch, 0, sizeof(scratch)); memset(&scratch, 0, sizeof(scratch));
if (pb_decode_from_bytes(mp->decoded.payload.bytes, mp->decoded.payload.size, &meshtastic_Telemetry_msg, &scratch)) { if (pb_decode_from_bytes(mp->decoded.payload.bytes, mp->decoded.payload.size, &meshtastic_Telemetry_msg, &scratch)) {
decoded = &scratch; decoded = &scratch;
@ -564,14 +564,12 @@ std::string MQTT::meshPacketToJson(meshtastic_MeshPacket *mp)
} else { } else {
LOG_ERROR("Error decoding protobuf for telemetry message!\n"); LOG_ERROR("Error decoding protobuf for telemetry message!\n");
} }
}; break;
break; }
} case meshtastic_PortNum_NODEINFO_APP: {
case meshtastic_PortNum_NODEINFO_APP: { msgType = "nodeinfo";
msgType = "nodeinfo"; meshtastic_User scratch;
meshtastic_User scratch; meshtastic_User *decoded = NULL;
meshtastic_User *decoded = NULL;
if (mp->which_payload_variant == meshtastic_MeshPacket_decoded_tag) {
memset(&scratch, 0, sizeof(scratch)); memset(&scratch, 0, sizeof(scratch));
if (pb_decode_from_bytes(mp->decoded.payload.bytes, mp->decoded.payload.size, &meshtastic_User_msg, &scratch)) { if (pb_decode_from_bytes(mp->decoded.payload.bytes, mp->decoded.payload.size, &meshtastic_User_msg, &scratch)) {
decoded = &scratch; decoded = &scratch;
@ -583,14 +581,12 @@ std::string MQTT::meshPacketToJson(meshtastic_MeshPacket *mp)
} else { } else {
LOG_ERROR("Error decoding protobuf for nodeinfo message!\n"); LOG_ERROR("Error decoding protobuf for nodeinfo message!\n");
} }
}; break;
break; }
} case meshtastic_PortNum_POSITION_APP: {
case meshtastic_PortNum_POSITION_APP: { msgType = "position";
msgType = "position"; meshtastic_Position scratch;
meshtastic_Position scratch; meshtastic_Position *decoded = NULL;
meshtastic_Position *decoded = NULL;
if (mp->which_payload_variant == meshtastic_MeshPacket_decoded_tag) {
memset(&scratch, 0, sizeof(scratch)); memset(&scratch, 0, sizeof(scratch));
if (pb_decode_from_bytes(mp->decoded.payload.bytes, mp->decoded.payload.size, &meshtastic_Position_msg, &scratch)) { if (pb_decode_from_bytes(mp->decoded.payload.bytes, mp->decoded.payload.size, &meshtastic_Position_msg, &scratch)) {
decoded = &scratch; decoded = &scratch;
@ -627,15 +623,12 @@ std::string MQTT::meshPacketToJson(meshtastic_MeshPacket *mp)
} else { } else {
LOG_ERROR("Error decoding protobuf for position message!\n"); LOG_ERROR("Error decoding protobuf for position message!\n");
} }
}; break;
break; }
} case meshtastic_PortNum_WAYPOINT_APP: {
msgType = "position";
case meshtastic_PortNum_WAYPOINT_APP: { meshtastic_Waypoint scratch;
msgType = "position"; meshtastic_Waypoint *decoded = NULL;
meshtastic_Waypoint scratch;
meshtastic_Waypoint *decoded = NULL;
if (mp->which_payload_variant == meshtastic_MeshPacket_decoded_tag) {
memset(&scratch, 0, sizeof(scratch)); memset(&scratch, 0, sizeof(scratch));
if (pb_decode_from_bytes(mp->decoded.payload.bytes, mp->decoded.payload.size, &meshtastic_Waypoint_msg, &scratch)) { if (pb_decode_from_bytes(mp->decoded.payload.bytes, mp->decoded.payload.size, &meshtastic_Waypoint_msg, &scratch)) {
decoded = &scratch; decoded = &scratch;
@ -650,14 +643,12 @@ std::string MQTT::meshPacketToJson(meshtastic_MeshPacket *mp)
} else { } else {
LOG_ERROR("Error decoding protobuf for position message!\n"); LOG_ERROR("Error decoding protobuf for position message!\n");
} }
}; break;
break; }
} case meshtastic_PortNum_NEIGHBORINFO_APP: {
case meshtastic_PortNum_NEIGHBORINFO_APP: { msgType = "neighborinfo";
msgType = "neighborinfo"; meshtastic_NeighborInfo scratch;
meshtastic_NeighborInfo scratch; meshtastic_NeighborInfo *decoded = NULL;
meshtastic_NeighborInfo *decoded = NULL;
if (mp->which_payload_variant == meshtastic_MeshPacket_decoded_tag) {
memset(&scratch, 0, sizeof(scratch)); memset(&scratch, 0, sizeof(scratch));
if (pb_decode_from_bytes(mp->decoded.payload.bytes, mp->decoded.payload.size, &meshtastic_NeighborInfo_msg, if (pb_decode_from_bytes(mp->decoded.payload.bytes, mp->decoded.payload.size, &meshtastic_NeighborInfo_msg,
&scratch)) { &scratch)) {
@ -678,12 +669,14 @@ std::string MQTT::meshPacketToJson(meshtastic_MeshPacket *mp)
} else { } else {
LOG_ERROR("Error decoding protobuf for neighborinfo message!\n"); LOG_ERROR("Error decoding protobuf for neighborinfo message!\n");
} }
}; break;
break; }
} // add more packet types here if needed
// add more packet types here if needed default:
default: break;
break; }
} else {
LOG_WARN("Couldn't convert encrypted payload of MeshPacket to JSON\n");
} }
jsonObj["id"] = new JSONValue((uint)mp->id); jsonObj["id"] = new JSONValue((uint)mp->id);