关于构造ICMP数据包

InTheMiddle 2007-04-04 03:50:33
自己写了个程序发ICMP端口不可达的数据包,其中ICMP协议层包含了IP层字段和8个字节UDP字段,请问最后8个字节的UDP字段里的校验和是怎么算的?
是直接从某个包里拷过来的?
...全文
398 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
yq_m 2007-04-06
  • 打赏
  • 举报
回复
加伪IP头
InTheMiddle 2007-04-05
  • 打赏
  • 举报
回复
我想请教的是最后的校验和是只算那8位UDP的数据吗?还是加UDP的伪头部?
yq_m 2007-04-04
  • 打赏
  • 举报
回复
u_short in_cksum(u_short *addr, int len)
{
register int nleft = len;
register u_short *w = addr;
register u_short answer;
register int sum = 0;

/*
* Our algorithm is simple, using a 32 bit accumulator (sum),
* we add sequential 16 bit words to it, and at the end, fold
* back all the carry bits from the top 16 bits into the lower
* 16 bits.
*/
while( nleft > 1 ) {
sum += *w++;
nleft -= 2;
}

/* mop up an odd byte, if necessary */
if( nleft == 1 ) {
u_short u = 0;

*(u_char *)(&u) = *(u_char *)w ;
sum += u;
}

/*
* add back carry outs from top 16 bits to low 16 bits
*/
sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
sum += (sum >> 16); /* add carry */
answer = ~sum; /* truncate to 16 bits */
return (answer);
}

4,356

社区成员

发帖
与我相关
我的任务
社区描述
通信技术相关讨论
社区管理员
  • 网络通信
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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