![]() |
| | #1 |
| Registered User Join Date: Aug 2004
Posts: 7
| weird checksum function I'm taking a networking class and my current project is to implement a "reliable data transfer protocol". So we're given a checksum function to both create a checksum for our packets and to check if the packets are error free. My problem is everytime I tried to check, I get a result that says packet has errors. Here's the function: Code: 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;
}
So roughly: on sender side: Code: Packet p; memset(&p sizeof(Packet), 0); . //set contents; p.checksum = checksum((uint16_t *)&p, sizeof(Packet)/2); Code: uint16_t check = p.checksum;
p.checksum = 0;
if (checksum((uint16_t *)&p, sizeof(Packet)/2) != check)
//corrupted
If anyone can help/comment I'll greatly appreciate it, Thanks! |
| sagitt13 is offline | |
| | #2 |
| Just Lurking Join Date: Oct 2002
Posts: 4,990
| Sorry, didn't look to closely. I'd take a closer look if you had a single, simple bit of code that demonstrated the problem. Code: p.checksum = checksum((uint16_t *)&p, sizeof(Packet)/2); Many checksums in which the checksum itself is included add to zero.
__________________ 7. It is easier to write an incorrect program than understand a correct one. 40. There are two ways to write error-free programs; only the third one works.* |
| Dave_Sinkula is offline | |
| | #3 |
| CSharpener Join Date: Oct 2006
Posts: 5,242
| Have you checked your structure? is it filled exactly as you sent it?
__________________ If I have eight hours for cutting wood, I spend six sharpening my axe. |
| vart is offline | |
| | #4 | |
| CSharpener Join Date: Oct 2006
Posts: 5,242
| Quote:
so actually it should be Code: sizeof(Packet)/sizeof(uint16_t) But I'm not sure what about aligning and bytes order
__________________ If I have eight hours for cutting wood, I spend six sharpening my axe. | |
| vart is offline | |
| | #5 |
| Registered User Join Date: Aug 2004
Posts: 7
| 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");
}
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;
|
| sagitt13 is offline | |
| | #6 |
| CSharpener Join Date: Oct 2006
Posts: 5,242
| Code: void *memset( void *dest, int c, size_t count ); Code: memset(&packet, sizeof(sawhdr), 0); Code: memset(&packet,0,sizeof(sawhdr));
__________________ If I have eight hours for cutting wood, I spend six sharpening my axe. Last edited by vart; 10-31-2006 at 01:02 AM. |
| vart is offline | |
| | #7 |
| Registered User Join Date: Jun 2004
Posts: 201
| Your problem is here: Code: memset(&packet, sizeof(sawhdr), 0); change to Code: memset(&packet, 0, sizeof(sawhdr)); |
| Laserve is offline | |
| | #8 |
| Registered User Join Date: Aug 2004
Posts: 7
| Oh DUH.... I feel stupid now... It works now! Thanks...haha.. |
| sagitt13 is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Screwy Linker Error - VC2005 | Tonto | C++ Programming | 5 | 06-19-2007 02:39 PM |
| Bisection Method function value at root incorrect | mr_glass | C Programming | 3 | 11-10-2005 09:10 AM |
| c++ linking problem for x11 | kron | Linux Programming | 1 | 11-19-2004 10:18 AM |
| I need help with passing pointers in function calls | vien_mti | C Programming | 3 | 04-24-2002 10:00 AM |