Add tailscale checksum
This commit is contained in:
parent
4ebeb2fa86
commit
06b4d4ecd1
11 changed files with 2254 additions and 28 deletions
|
|
@ -30,34 +30,6 @@ func Put(b []byte, xsum uint16) {
|
|||
binary.BigEndian.PutUint16(b, xsum)
|
||||
}
|
||||
|
||||
// Checksum calculates the checksum (as defined in RFC 1071) of the bytes in the
|
||||
// given byte array. This function uses an optimized version of the checksum
|
||||
// algorithm.
|
||||
//
|
||||
// The initial checksum must have been computed on an even number of bytes.
|
||||
func Checksum(buf []byte, initial uint16) uint16 {
|
||||
s, _ := calculateChecksum(buf, false, initial)
|
||||
return s
|
||||
}
|
||||
|
||||
// Checksumer calculates checksum defined in RFC 1071.
|
||||
type Checksumer struct {
|
||||
sum uint16
|
||||
odd bool
|
||||
}
|
||||
|
||||
// Add adds b to checksum.
|
||||
func (c *Checksumer) Add(b []byte) {
|
||||
if len(b) > 0 {
|
||||
c.sum, c.odd = calculateChecksum(b, c.odd, c.sum)
|
||||
}
|
||||
}
|
||||
|
||||
// Checksum returns the latest checksum value.
|
||||
func (c *Checksumer) Checksum() uint16 {
|
||||
return c.sum
|
||||
}
|
||||
|
||||
// Combine combines the two uint16 to form their checksum. This is done
|
||||
// by adding them and the carry.
|
||||
//
|
||||
|
|
|
|||
13
internal/gtcpip/checksum/checksum_default.go
Normal file
13
internal/gtcpip/checksum/checksum_default.go
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
//go:build !amd64
|
||||
|
||||
package checksum
|
||||
|
||||
// Checksum calculates the checksum (as defined in RFC 1071) of the bytes in the
|
||||
// given byte array. This function uses an optimized version of the checksum
|
||||
// algorithm.
|
||||
//
|
||||
// The initial checksum must have been computed on an even number of bytes.
|
||||
func Checksum(buf []byte, initial uint16) uint16 {
|
||||
s, _ := calculateChecksum(buf, false, initial)
|
||||
return s
|
||||
}
|
||||
9
internal/gtcpip/checksum/checksum_ts.go
Normal file
9
internal/gtcpip/checksum/checksum_ts.go
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
//go:build amd64
|
||||
|
||||
package checksum
|
||||
|
||||
import "github.com/sagernet/sing-tun/internal/tschecksum"
|
||||
|
||||
func Checksum(buf []byte, initial uint16) uint16 {
|
||||
return tschecksum.Checksum(buf, initial)
|
||||
}
|
||||
|
|
@ -1,3 +1,5 @@
|
|||
//go:build !amd64
|
||||
|
||||
// Copyright 2023 The gVisor Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue