概述
可以参考一下我的代码:
//校验
USHORT checksum(USHORT* buff, INT size)
{
ULONG cksum = 0;
while(size>1)
{
cksum += *buff++;
size -= sizeof(USHORT);
}
// 是奇数
if(size)
{
cksum += *(UCHAR*)buff;
}
// 将位的chsum高位和低位相加,然后取反
cksum = (cksum >> 16) + (cksum & 0xffff);
cksum += (cksum >> 16);
return (USHORT)(~cksum);
}
//修改数包窗口大小,以及清除标志位
void ModifyPacketWndSize(PVOID pPacketContent,INT iPacketSize)
{
//定义数据包tcp/ip结构指针
int nLeavingLen;
PETHeader pEtherHdr=NULL;
PIPHeader pIpHdr=NULL;
PTCPHeader pTcpHdr=NULL;
PUDPHeader pUdpHdr=NULL;
CHAR *pData=NULL;
//基本判断
if (pPacketContent==NULL)
{
return FALSE;
}
//获得以太帧头,过滤协议,过滤ip协议,放行其他协议
nLeavingLen=iPacketSize;
if(nLeavingLen < sizeof(ETHeader))
{
return NDIS_STATUS_SUCCESS;
}
nLeavingLen -= sizeof(ETHeader);
pEtherHdr = (PETHeader)pPacketContent;
if(pEtherHdr->type != 0x8) // 如果不是IP协议,则不处理
return;
//获得ip头
pIpHdr = (PIPHeader)(pEtherHdr + 1);
// 验证剩余数据长度,防止发生内核非法访问
if(nLeavingLen < sizeof(IPHeader))
return;
nLeavingLen -= sizeof(IPHeader);
// 检查版本信息,我们仅处理IPv4
if(((pIpHdr->iphVerLen >> 4) & 0x0f) == 6)
{
return;
}
// 如果是TCP封包,修改窗口大小
if(pIpHdr->ipProtocol == 6)
{
//TCP伪数据包头指针
PSDHEADER *pPsdHeader=NULL;
//数据包校验缓存大小
INT iTcpCheckoutBufSize;
NDIS_PHYSICAL_ADDRESS phyaddr = {-1};
NDIS_STATUS Status;
//修改窗口大小
pTcpHdr = (PTCPHeader)(pIpHdr + 1);
pTcpHdr->windows=200;
KdPrint(("改变窗口大小!n"));
//pTcpHdr->flags=0;
//Tcp数据校验
iTcpCheckoutBufSize=sizeof(PSDHEADER)+nLeavingLen;
Status=NdisAllocateMemory(&pPsdHeader,iTcpCheckoutBufSize,0,phyaddr);
if (Status==NDIS_STATUS_SUCCESS)
{
NdisZeroMemory(pPsdHeader,iTcpCheckoutBufSize);
//填充伪TCP头
pPsdHeader->daddr=pIpHdr->ipSource;
pPsdHeader->saddr=pIpHdr->ipDestination;
pPsdHeader->mbz=0;
pPsdHeader->ptcl=0x06;
pPsdHeader->tcpl=htons(nLeavingLen);
计算TCP校验和
pTcpHdr->checksum=0;
NdisMoveMemory((PCHAR)pPsdHeader+sizeof(PSDHEADER),pTcpHdr,nLeavingLen);
pTcpHdr->checksum=checksum((PUSHORT)pPsdHeader,iTcpCheckoutBufSize);
NdisFreeMemory(pPsdHeader,iTcpCheckoutBufSize,0);
}
}
return;
}
最后
以上就是健忘啤酒为你收集整理的计算udp校验和例子_UDP 包校验和计算的全部内容,希望文章能够帮你解决计算udp校验和例子_UDP 包校验和计算所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复