关于checksum校检和的计算.

endicking 2004-05-09 02:13:39
通常ip,或者,tcp,udp的校检和要先统计整个ip头,或者整个tcp,udp数据包的数据.
我有个想法,我截获了数据包,并改动了源地址,目标地址
但是我不想再重新统计整个数据包来计算checksum了,我想在原来的checksum基础上
根据我改的源地址和目标地址的改变偏移来计算checksum,不知道这可不可以

但是我发现checksum的函数经过了移位,会有数据丢失,有那位大侠有高招能够很好的处理这些问题呢?

标准checksum计算函数
USHORT CNetwork::CheckSum(USHORT *buffer, int size)
{
unsigned long cksum=0;
while(size >1)
{
cksum+=*buffer++;
size -=sizeof(USHORT);
}
if(size )
{
cksum += *(UCHAR*)buffer;
}

cksum = (cksum >> 16) + (cksum & 0xffff);
cksum += (cksum >>16);
return (USHORT)(~cksum);
}
...全文
360 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
agent 2004-05-10
  • 打赏
  • 举报
回复
改变部分的数据
endicking 2004-05-10
  • 打赏
  • 举报
回复
谢谢兄弟,结分
endicking 2004-05-09
  • 打赏
  • 举报
回复
没有人帮帮我吗?
endicking 2004-05-09
  • 打赏
  • 举报
回复
谢谢楼上的仗义相助,
但是有个别参数的意义没明

optr,nptr是指向整个ip包的指针,还是指向改变数据的部分?
agent 2004-05-09
  • 打赏
  • 举报
回复
涉及的协议包括:IP、UDP、TCP、ICMP。
关键是计算出修改前后的校验和的差值,代码如下:

void checksumadjust(unsigned char *chksum, unsigned char *optr,
int olen, unsigned char *nptr, int nlen)
/* assuming: unsigned char is 8 bits, long is 32 bits.
- chksum points to the chksum in the packet
- optr points to the old data in the packet
- nptr points to the new data in the packet
*/
{
long x, old, new;
x=chksum[0]*256+chksum[1];
x=~x & 0xFFFF;
while (olen)
{
old=optr[0]*256+optr[1]; optr+=2;
x-=old & 0xffff;
if (x<=0) { x--; x&=0xffff; }
olen-=2;
}
while (nlen)
{
new=nptr[0]*256+nptr[1]; nptr+=2;
x+=new & 0xffff;
if (x & 0x10000) { x++; x&=0xffff; }
nlen-=2;
}
x=~x & 0xFFFF;
chksum[0]=x/256; chksum[1]=x & 0xff;
}
endicking 2004-05-09
  • 打赏
  • 举报
回复
关键怎么做呢?
能否提示一点?
echoe 2004-05-09
  • 打赏
  • 举报
回复
可以这个做,而且tcp/ip协议也是这么推荐的。
如果你改变了一个bit,在checksum字段相应改变

18,363

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧