Do not modify the packet when AP gets decoded.

This commit is contained in:
antirez 2013-01-19 16:20:39 +01:00
parent c9bb48675c
commit 2fe188286e

View File

@ -698,15 +698,18 @@ int ICAOAddressWasRecentlySeen(uint32_t addr) {
* the address XOR checksum field in the message. This will recover the * the address XOR checksum field in the message. This will recover the
* address: if we found it in our cache, we can assume the message is ok. * address: if we found it in our cache, we can assume the message is ok.
* *
* On success the input buffer is modified to remove the xored checksum * This function expects mm->msgtype and mm->msgbits to be correctly
* from the packet, so that the last three bytes will contain the * populated by the caller.
* plain ICAO address. *
* On success the correct ICAO address is stored in the modesMessage
* structure in the aa3, aa2, and aa1 fiedls.
* *
* If the function successfully recovers a message with a correct checksum * If the function successfully recovers a message with a correct checksum
* it returns 1. Otherwise 0 is returned. */ * it returns 1. Otherwise 0 is returned. */
int bruteForceAP(unsigned char *msg, int msgbits) { int bruteForceAP(unsigned char *msg, struct modesMessage *mm) {
unsigned char aux[MODES_LONG_MSG_BYTES]; unsigned char aux[MODES_LONG_MSG_BYTES];
int msgtype = msg[0]>>3; int msgtype = mm->msgtype;
int msgbits = mm->msgbits;
if (msgtype == 0 || /* Short air surveillance */ if (msgtype == 0 || /* Short air surveillance */
msgtype == 4 || /* Surveillance, altitude reply */ msgtype == 4 || /* Surveillance, altitude reply */
@ -736,9 +739,9 @@ int bruteForceAP(unsigned char *msg, int msgbits) {
* the message valid. */ * the message valid. */
addr = aux[lastbyte] | (aux[lastbyte-1] << 8) | (aux[lastbyte-2] << 16); addr = aux[lastbyte] | (aux[lastbyte-1] << 8) | (aux[lastbyte-2] << 16);
if (ICAOAddressWasRecentlySeen(addr)) { if (ICAOAddressWasRecentlySeen(addr)) {
msg[lastbyte] = aux[lastbyte]; mm->aa1 = aux[lastbyte-2];
msg[lastbyte-1] = aux[lastbyte-1]; mm->aa2 = aux[lastbyte-1];
msg[lastbyte-2] = aux[lastbyte-2]; mm->aa3 = aux[lastbyte];
return 1; return 1;
} }
} }
@ -939,30 +942,26 @@ void decodeModesMessage(struct modesMessage *mm, unsigned char *msg) {
mm->identity = a*1000 + b*100 + c*10 + d; mm->identity = a*1000 + b*100 + c*10 + d;
} }
/* Check if we can check the checksum for the Downlink Formats where /* DF 11 & 17: try to populate our ICAO addresses whitelist.
* the checksum is xored with the aircraft ICAO address. We try to * DFs with an AP field (xored addr and crc), try to decode it. */
* brute force it using a list of recently seen aircraft addresses. */
if (mm->msgtype != 11 && mm->msgtype != 17) { if (mm->msgtype != 11 && mm->msgtype != 17) {
/* Return to the caller now if we can't resolve the API field and /* Check if we can check the checksum for the Downlink Formats where
* we need to check the checksum. */ * the checksum is xored with the aircraft ICAO address. We try to
if (bruteForceAP(msg,mm->msgbits)) { * brute force it using a list of recently seen aircraft addresses. */
/* We recovered the message! if (bruteForceAP(msg,mm)) {
* Populate the AA fields with the right information. */ /* We recovered the message, mark the checksum as valid. */
mm->aa3 = msg[mm->msgbits/8-1];
mm->aa2 = msg[mm->msgbits/8-2];
mm->aa1 = msg[mm->msgbits/8-3];
mm->crcok = 1; mm->crcok = 1;
} else { } else {
mm->crcok = 0; mm->crcok = 0;
} }
} } else {
/* If this is DF 11 or DF 17 and the checksum was ok,
/* If this is DF 11 or DF 17 and the checksum was ok, * we can add this address to the list of recently seen
* we can add this address to the list of recently seen * addresses. */
* addresses. */ if (mm->crcok && mm->errorbit == -1) {
if (mm->crcok && mm->errorbit == -1) { uint32_t addr = (mm->aa1 << 16) | (mm->aa2 << 8) | mm->aa3;
uint32_t addr = (mm->aa1 << 16) | (mm->aa2 << 8) | mm->aa3; addRecentlySeenICAOAddr(addr);
addRecentlySeenICAOAddr(addr); }
} }
/* Decode 13 bit altitude for DF0, DF4, DF16, DF20 */ /* Decode 13 bit altitude for DF0, DF4, DF16, DF20 */