Thanks for the input, I tried dividing by sizeof(uint16_t) but it still doesn't work... Here's a little test program I used to test the function:
Code:
#include "saw.h"
uint16_t checksum(uint16_t *buf, int nwords)
{
uint32_t sum;
for (sum = 0; nwords > 0; nwords--)
sum += *buf++;
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
return ~sum;
}
int main(int argc, char *argv[]) {
sawhdr packet;
uint16_t check;
memset(&packet, sizeof(sawhdr), 0);
packet.type = SYNACK;
packet.checksum = checksum((uint16_t *)&packet, sizeof(sawhdr)/sizeof(uint16_t));
check = packet.checksum;
packet.checksum = 0;
if (checksum((uint16_t *)&packet, sizeof(sawhdr)/sizeof(uint16_t)) != check)
fprintf(stderr, "checksum bad!!\n");
else
printf("checksum ok\n");
}
And here's the stuff from saw.h that're relevant:
Code:
#define SYNACK 1
typedef struct {
uint8_t type; /* packet type (e.g. SYN, DATA, ACK, FIN) */
uint8_t seqnum; /* sequence number of the packet */
uint16_t checksum; /* header and payload checksum */
uint8_t data[DATALEN]; /* payload */
size_t numlen;
} __attribute__((packed)) sawhdr;
So when I ran the test program I get "bad checksum..."