弗莱彻校验和给出不同的值
fletcher checksum giving different values
我在这里尝试使用 16 位 Fletcher 校验和。基本上,我的程序通过两个虚拟实体之间的 "sending" 和 "receiving" 数据包模拟物理层上的流量。我正在打印两边的数据包,它们确实匹配,但我在接收端计算出不同的校验和。
数据包结构:
#define MESSAGE_LENGTH 20
struct pkt {
int seqnum;
int acknum;
int checksum;
char payload[MESSAGE_LENGTH];
};
这是我用来计算每个数据包校验和的代码:
/*
* Computes the fletcher checksum of the input packet
*/
uint16_t calcChecksum(struct pkt *packet) {
/* the data for the checksum needs to be continuous, so here I am making
a temporary block of memory to hold everything except the packet checksum */
size_t sizeint = sizeof(int);
size_t size = sizeof(struct pkt) - sizeint;
uint8_t *temp = malloc(size);
memcpy(temp, packet, sizeint * 2); // copy the seqnum and acknum
memcpy(temp + (2*sizeint), &packet->payload, MESSAGE_LENGTH); // copy data
// calculate checksum
uint16_t checksum = fletcher16((uint8_t const *) &temp, size);
free(temp);
return checksum;
}
/*
* This is a checksum algorithm that I shamelessly copied off a wikipedia page.
*/
uint16_t fletcher16( uint8_t const *data, size_t bytes ) {
uint16_t sum1 = 0xff, sum2 = 0xff;
size_t tlen;
while (bytes) {
tlen = bytes >= 20 ? 20 : bytes;
bytes -= tlen;
do {
sum2 += sum1 += *data++;
} while (--tlen);
sum1 = (sum1 & 0xff) + (sum1 >> 8);
sum2 = (sum2 & 0xff) + (sum2 >> 8);
}
/* Second reduction step to reduce sums to 8 bits */
sum1 = (sum1 & 0xff) + (sum1 >> 8);
sum2 = (sum2 & 0xff) + (sum2 >> 8);
return sum2 << 8 | sum1;
}
我对校验和知之甚少,我从找到的页面上复制了该算法,所以如果有人能理解为什么两个其他方面相同的数据包的校验和不同,我将不胜感激。谢谢!
出现问题是因为您没有将 temp
数据的地址传递给校验和函数,而是传递变量 temp
在堆栈中存储的地址。
你应该改变
uint16_t checksum = fletcher16((uint8_t const *) &temp, size);
到
uint16_t checksum = fletcher16((uint8_t const *) temp, size);
^ no & operator
我在这里尝试使用 16 位 Fletcher 校验和。基本上,我的程序通过两个虚拟实体之间的 "sending" 和 "receiving" 数据包模拟物理层上的流量。我正在打印两边的数据包,它们确实匹配,但我在接收端计算出不同的校验和。
数据包结构:
#define MESSAGE_LENGTH 20
struct pkt {
int seqnum;
int acknum;
int checksum;
char payload[MESSAGE_LENGTH];
};
这是我用来计算每个数据包校验和的代码:
/*
* Computes the fletcher checksum of the input packet
*/
uint16_t calcChecksum(struct pkt *packet) {
/* the data for the checksum needs to be continuous, so here I am making
a temporary block of memory to hold everything except the packet checksum */
size_t sizeint = sizeof(int);
size_t size = sizeof(struct pkt) - sizeint;
uint8_t *temp = malloc(size);
memcpy(temp, packet, sizeint * 2); // copy the seqnum and acknum
memcpy(temp + (2*sizeint), &packet->payload, MESSAGE_LENGTH); // copy data
// calculate checksum
uint16_t checksum = fletcher16((uint8_t const *) &temp, size);
free(temp);
return checksum;
}
/*
* This is a checksum algorithm that I shamelessly copied off a wikipedia page.
*/
uint16_t fletcher16( uint8_t const *data, size_t bytes ) {
uint16_t sum1 = 0xff, sum2 = 0xff;
size_t tlen;
while (bytes) {
tlen = bytes >= 20 ? 20 : bytes;
bytes -= tlen;
do {
sum2 += sum1 += *data++;
} while (--tlen);
sum1 = (sum1 & 0xff) + (sum1 >> 8);
sum2 = (sum2 & 0xff) + (sum2 >> 8);
}
/* Second reduction step to reduce sums to 8 bits */
sum1 = (sum1 & 0xff) + (sum1 >> 8);
sum2 = (sum2 & 0xff) + (sum2 >> 8);
return sum2 << 8 | sum1;
}
我对校验和知之甚少,我从找到的页面上复制了该算法,所以如果有人能理解为什么两个其他方面相同的数据包的校验和不同,我将不胜感激。谢谢!
出现问题是因为您没有将 temp
数据的地址传递给校验和函数,而是传递变量 temp
在堆栈中存储的地址。
你应该改变
uint16_t checksum = fletcher16((uint8_t const *) &temp, size);
到
uint16_t checksum = fletcher16((uint8_t const *) temp, size);
^ no & operator