大家好,欢迎来到IT知识分享网。
unsigned short checksum(unsigned short* buffer, int size)
{
unsigned int sum = 0;
for (sum = 0; size > 0;)
{
sum += *buffer++;
size -= 2;
}
sum = (sum >> 16) + (sum & 0xFFFF);
sum += (sum >> 16);
return (unsigned short)(~sum);
}
unsigned short TCPChecksum(IPV4_HDR* ipHeader, TCPHeader* tcpHeader, void* pData, int dataSize)
{
PSD_HEADER psdHeader = { 0 };
psdHeader.saddr = *(unsigned long*)ipHeader->ip_srcaddr;
psdHeader.daddr = *(unsigned long*)ipHeader->ip_destaddr;
psdHeader.mbz = 0;
psdHeader.ptcl = ipHeader->ip_protocol;
psdHeader.tcpl = htons(sizeof(TCPHeader) + dataSize);
int bufSize = sizeof(PSD_HEADER) + sizeof(TCPHeader) + dataSize;
if (0 != (bufSize % 2))
bufSize += 1;
char* pHeader = new char[bufSize];
memset(pHeader, 0, bufSize);
int offset = 0;
memcpy(pHeader + offset, &psdHeader, sizeof(PSD_HEADER));
offset += sizeof(PSD_HEADER);
memcpy(pHeader + offset, tcpHeader, sizeof(TCPHeader));
offset += sizeof(TCPHeader);
if ((NULL != pData) && (dataSize > 0))
memcpy(pHeader + offset, pData, dataSize);
((TCPHeader*)(pHeader + sizeof(PSD_HEADER)))->th_sum = 0;
((TCPHeader*)(pHeader + sizeof(PSD_HEADER)))->th_sum = checksum((unsigned short*)pHeader, bufSize);
tcpHeader->th_sum = ((TCPHeader*)(pHeader + sizeof(PSD_HEADER)))->th_sum;
delete[] pHeader;
pHeader = NULL;
return tcpHeader->th_sum;
}
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/21806.html