Question : checksum error with libnet and winpcap

I am writing a program in VC using libnet and winpcap. This program captures the network packets and append a 4 byte extra info to each packet, i am able to add the 4 byte data and also incremented the packet size by 4. But the problem is that, at the destination i found my packets are having bad tcp chksum (i am using tcpdump on destination unix machine to check the packets)

// Code to append the extra info to packet
memcpy((void *)(pktdta + hdr->len), (void *)(strExInfo), 4);
lptr = (unsigned int *)&(hdr->caplen);
*lptr = hdr->caplen + 4 ;
lptr = (unsigned int *)&(hdr->len);
*lptr = hdr->len + 4;

// code to recalculate the checksum
if (libnet_do_checksum(lnetptr, (u_char *)pktdta + LIBNET_ETH_H, IPPROTO_TCP,
header->len) == -1)
    printf("\nTCP Checksum error");

please tell me is there something I am doing wrong in it or is there a better solution for it.
thanks in advance....
D. Kelly

Answer : checksum error with libnet and winpcap

Hi david,
you might want to do this -

// recalculate the checksum
ip_len = ntohs(ih->tlen);
ip_len -= (ih->ver_ihl & 0x0f) * 4;
csresult = libnet_do_checksum(libnet_ptr, (u_char *)(pkt_data + LIBNET_ETH_H), IPPROTO_TCP, ip_len);
if (csresult == -1)
      printf("\nIP Checksum error");

where ih is the ip_header.

use this function to write back data to the wire.
libnet_write_link(libnet_ptr, (unsigned char *)pkt_data, *lenp);

hope that solves your problem

Aditya Milan :)
http://www.sanchartantra.com/
http://www.milansoft.com/


Random Solutions  
 
programming4us programming4us