/*------------------------------------------------------------------------
* udpcksum - compute a UDP pseudo-header checksum
*------------------------------------------------------------------------
*/
unsigned short
udpcksum(struct ep *pep, int len)
{
struct ip *pip = (struct ip *)pep->ep_data;
struct udp *pudp = (struct udp *)pip->ip_data;
unsigned short *sptr;
unsigned long ucksum;
int i;
ucksum = 0;
sptr = (unsigned short *) &pip->ip_src;
/* 2*IP_ALEN octets = IP_ALEN shorts... */
/* they are in net order. */
for (i=0; i<IP_ALEN; ++i)
ucksum += *sptr++;
sptr = (unsigned short *)pudp;
ucksum += hs2net(IPT_UDP + len);
if (len % 2) {
((char *)pudp)[len] = 0; /* pad */
len += 1; /* for the following division */
}
len >>= 1; /* convert to length in shorts */
for (i=0; i<len; ++i)
ucksum += *sptr++;
ucksum = (ucksum >> 16) + (ucksum & 0xffff);
ucksum += (ucksum >> 16);
return (short)(~ucksum & 0xffff);
}
udpcksum
最新推荐文章于 2024-11-06 15:48:48 发布
本文详细介绍了如何使用C语言实现UDP伪头部校验和的计算。通过解析IP和UDP数据包结构,逐步演示了计算过程,包括源地址、目的地址、协议类型及数据长度等字段的处理。
2565

被折叠的 条评论
为什么被折叠?



