> 计算IP和TCP、UDP数据包头的校验和,首先补充下ipv4头部的结构图:

> ◆当发送IP包时,需要计算IP报头的校验和:

1、把校验和字段置为0;
2、对IP头部中的每16bit进行二进制求和;
3、如果和的高16bit不为0,则将和的高16bit和低16bit反复相加,直到和的高16bit为0,从而获得一个16bit的值;
4、将该16bit的值取反,存入校验和字段。

> ◆当接收IP包时,需要对报头进行确认,检查IP头是否有误,算法同上2、3步,然后判断取反的结果是否为0,是则正确,否则有错。

RFC里面的C源代码

//计算校验和
USHORT checksum(USHORT *buffer,int size)
{
    unsigned long cksum = 0;
    while(size>1)
    {
        cksum += *buffer++;
        size -= sizeof(USHORT);
    }
    if (size)
    {
        cksum += *(UCHAR *)buffer;
    }
    //将32位数转换成16
    while (cksum>>16)
        cksum=(cksum >> 16) + (cksum & 0xffff);
    return (USHORT) (~cksum);
}

VB.Net的

    ''' <summary>
    ''' 计算校验和
    ''' </summary>
    ''' <param name="arrHeader">要计算校验和的头部字节数组</param>
    ''' <param name="HeaderSize">头部长度,单位:字节</param>
    Private Function CalcCheckSum(ByVal arrHeader() As Byte, ByVal HeaderSize As Integer) As UInt16
        '若包头长度不是 2 字节的倍数
        '则用 0 比特填充到 2 字节的倍数
        Dim CkSum As UInt32, arrTmp() As UInt16
        ReDim arrTmp(IIf(HeaderSize Mod 2, (HeaderSize - 1) / 2, HeaderSize / 2 - 1))
        Dim GCH As GCHandle = GCHandle.Alloc(arrTmp, GCHandleType.Pinned)
        Dim tPtr As IntPtr = GCH.AddrOfPinnedObject
        Marshal.Copy(arrHeader, 0, tPtr, HeaderSize)
        For i As Integer = 0 To arrTmp.GetUpperBound(0)
            CkSum += arrTmp(i)
        Next
        While (CkSum >> 16)
            CkSum = (CkSum >> 16) + (CkSum And &HFFFF)
        End While
        Return (Not CkSum) And &HFFFF
    End Function

VB6的

Private Function CalcCheckSum(srcIPP() As Byte, ByVal IPSize As Long) As Integer
Dim CkSum As Long, i As Long, IPTmp() As Integer
'若包头长度不是 16 比特的倍数
'则用 0 比特填充到 16 比特的倍数
ReDim IPTmp(IIf(IPSize Mod 2, (IPSize - 1) / 2, IPSize / 2 - 1))
CopyMemory IPTmp(0), srcIPP(0), IPSize
For i = 0 To UBound(IPTmp)
    CkSum = CkSum + IPTmp(i) + (IPTmp(i) And &H8000&) * 2
Next
While (CkSum And &HFFFF0000)
    CkSum = (CkSum And &HFFFF0000) / &H10000 + CkSum And &HFFFF&
Wend
CkSum = (Not CkSum) And &HFFFF&
CalcCheckSum = CkSum - (CkSum And &H8000&) * 2
End Function