github.com/apernet/sing-tun@v0.2.6-0.20240323130332-b9f6511036ad/internal/clashtcpip/tcpip.go (about) 1 package clashtcpip 2 3 var zeroChecksum = [2]byte{0x00, 0x00} 4 5 var SumFnc = SumCompat 6 7 func Sum(b []byte) uint32 { 8 return SumFnc(b) 9 } 10 11 // Checksum for Internet Protocol family headers 12 func Checksum(sum uint32, b []byte) (answer [2]byte) { 13 sum += Sum(b) 14 sum = (sum >> 16) + (sum & 0xffff) 15 sum += sum >> 16 16 sum = ^sum 17 answer[0] = byte(sum >> 8) 18 answer[1] = byte(sum) 19 return 20 } 21 22 func SetIPv4(packet []byte) { 23 packet[0] = (packet[0] & 0x0f) | (4 << 4) 24 }