So I got an assignment about programming a whole TCP/IP stack (including ARP and ICMP). So far I'm stuck at ICMP. It seems that every packet I send, according to Wireshark, is malformed (the IP header isn't, only ICMP).
I can't use the existing structures so I had to create some myself.
Here is my icmp_header_t structure:
typedef struct {
uint8_t type;
uint8_t code;
uint16_t checksum;
} icmp_header_t;
I think this may have to do with my checksum calculation. Here is the checksum calculation:
uint16_t csum (uint16_t *buffer, int length){
unsigned long sum;
// initialize sum to zero and loop until length (in words) is 0
for (sum=0; length>1; length-=2) // sizeof() returns number of bytes, we're interested in number of words
sum += *buffer++; // add 1 word of buffer to sum and proceed to the next
// we may have an extra byte
if (length==1)
sum += (unsigned char)*buffer;
sum = (sum >> 16) + (sum & 0xFFFF); // add high 16 to low 16
sum += (sum >> 16); // add carry
return (uint16_t) ~sum;
}
And this is how I build my header:
construct_icmp_header(icmp_head, ICMP_ECHO, 0, 0);
And how I calculate the checksum: icmp_head->checksum = csum((uint16_t *) icmp_head, sizeof(icmp_header_t));
This is the hexdump of one of my icmp packets:
And one that is correct:
Would anyone have an idea about this?
Edit: So I solved it. It was a field missing, quench (now replaced by id and seq).