问题描述
关键代码..typedefstructip_hdr{/*unsignedcharversion:4,ihl:4;*/unsignedcharh_ver;unsignedchartos;unsignedshorttot_len;unsignedshortid;unsignedshortzero:1,frag:1,more_frag:1,frag_off:13;unsignedcharttl;unsignedcharprotocol;unsignedshortcheckSum;unsignedlongsaddr;unsignedlongdaddr;/*Theoptionsstarthere.*/}IP_HEADER;unsignedshortcheckSum(unsignedchar*buff,intwlen){unsignedlongsum=0;if(wlen){unsignedlongbogus;__asm__("clcn""1:t""lodslnt""adcl%3,%0nt""decl%2nt""jne1bnt""adcl$0,%0nt""movl%0,%3nt""shrl$16,%3nt""addw%w3,%w0nt""adcw$0,%w0":"=r"(sum),"=S"(buff),"=r"(wlen),"=a"(bogus):"0"(sum),"1"(buff),"2"(wlen));}return(~sum)&0xffff;}intIPGen(ip_hdr*hdr,unsignedlongsrcIP,unsignedlongdstIP){/*hdr->version=4;hdr->ihl=5;//==?*/hdr->h_ver=(4<<4|sizeof(ip_hdr)/sizeof(unsignedint));hdr->tos=0;hdr->tot_len=sizeof(ip_hdr)+sizeof(tcp_hdr);hdr->id=0;hdr->zero=0;hdr->frag=0;hdr->more_frag=0;hdr->frag_off=0;hdr->ttl=64;hdr->protocol=IP_TCP;hdr->saddr=srcIP,hdr->daddr=dstIP;hdr->saddr=htonl(hdr->saddr);hdr->daddr=htonl(hdr->daddr);hdr->tot_len=htons(hdr->tot_len);hdr->checkSum=0;hdr->checkSum=checkSum((char*)hdr,sizeof(ip_hdr));return0;}
校验和的计算代码是从linux源码copy来的..应该不会错。但是!wireshark抓包永远是校验和错误的..whywhywhy....
解决方案
解决方案二:
这个不是x86构架的吧,大小端是否正确?调试一下就知道哪儿错了