mirror of
https://github.com/fumiama/WireGold.git
synced 2026-06-18 08:50:25 +08:00
feat: impl. trans & ttl
This commit is contained in:
@@ -17,14 +17,18 @@ import (
|
||||
// PreCRC64 calculate crc64 checksum without idxdatsz.
|
||||
func (p *Packet) PreCRC64() (crc uint64) {
|
||||
w := bin.SelectWriter()
|
||||
// 固定 TTL 为 0 计算
|
||||
if bin.IsLittleEndian {
|
||||
ttl := p.TTL
|
||||
p.TTL = 0
|
||||
w.Write((*[PacketHeadNoCRCLen]byte)(
|
||||
(unsafe.Pointer)(p),
|
||||
)[:])
|
||||
p.TTL = ttl
|
||||
} else {
|
||||
w.WriteUInt32(p.idxdatsz)
|
||||
w.WriteUInt32(uint32(p.randn))
|
||||
w.WriteUInt16((uint16(p.TTL) << 8) | uint16(p.Proto))
|
||||
w.WriteUInt16(uint16(p.Proto)) // TTL is set to 0
|
||||
w.WriteUInt16(p.SrcPort)
|
||||
w.WriteUInt16(p.DstPort)
|
||||
w.WriteUInt16(p.Offset)
|
||||
@@ -46,11 +50,16 @@ func (p *Packet) PreCRC64() (crc uint64) {
|
||||
// WriteHeaderTo write header bytes to buf
|
||||
// with crc64 checksum.
|
||||
func (p *Packet) WriteHeaderTo(buf *bytes.Buffer) {
|
||||
// 固定 TTL 为 0 计算
|
||||
if bin.IsLittleEndian {
|
||||
buf.Write((*[PacketHeadNoCRCLen]byte)(
|
||||
(unsafe.Pointer)(p),
|
||||
)[:])
|
||||
p.md5h8rem = int64(algo.MD5Hash8(buf.Bytes()))
|
||||
pbuf.NewBytes(buf.Len()).V(func(b []byte) {
|
||||
copy(b, buf.Bytes())
|
||||
ClearTTL(b)
|
||||
p.md5h8rem = int64(algo.MD5Hash8(b))
|
||||
})
|
||||
_ = binary.Write(buf, binary.LittleEndian, p.md5h8rem)
|
||||
return
|
||||
}
|
||||
@@ -63,8 +72,12 @@ func (p *Packet) WriteHeaderTo(buf *bytes.Buffer) {
|
||||
w.WriteUInt16(p.Offset)
|
||||
w.Write(p.src[:])
|
||||
w.Write(p.dst[:])
|
||||
w.P(func(b *pbuf.Buffer) {
|
||||
p.md5h8rem = int64(algo.MD5Hash8(b.Bytes()))
|
||||
w.P(func(buf *pbuf.Buffer) {
|
||||
pbuf.NewBytes(buf.Len()).V(func(b []byte) {
|
||||
copy(b, buf.Bytes())
|
||||
ClearTTL(b)
|
||||
p.md5h8rem = int64(algo.MD5Hash8(b))
|
||||
})
|
||||
})
|
||||
w.WriteUInt64(uint64(p.md5h8rem))
|
||||
w.P(func(b *pbuf.Buffer) {
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
const (
|
||||
hasmorebit FlagsProto = 0x20 << iota
|
||||
nofragbit
|
||||
topbit //TODO: 改为 trans 标记
|
||||
topbit
|
||||
)
|
||||
|
||||
const (
|
||||
|
||||
@@ -27,13 +27,13 @@ func TestBuilderNative(t *testing.T) {
|
||||
Split(16384, false)[0]).Trans()
|
||||
s := hex.EncodeToString(dat)
|
||||
if s[:8] != "12004593" {
|
||||
panic("1")
|
||||
panic(s[:8])
|
||||
}
|
||||
if s[16:48] != "03ff05000a0000000102030406070809" {
|
||||
panic("2")
|
||||
panic(s[16:48])
|
||||
}
|
||||
if s[80:] != "30313233343536373839" {
|
||||
panic("3")
|
||||
panic(s[80:])
|
||||
}
|
||||
p, err := ParsePacketHeader(dat)
|
||||
if err != nil {
|
||||
@@ -88,13 +88,13 @@ func TestBuilderBE(t *testing.T) {
|
||||
Split(16384, false)[0]).Trans()
|
||||
s := hex.EncodeToString(dat)
|
||||
if s[:8] != "12004593" {
|
||||
panic("1")
|
||||
panic(s[:8])
|
||||
}
|
||||
if s[16:48] != "03ff05000a0000000102030406070809" {
|
||||
panic("2")
|
||||
panic(s[16:48])
|
||||
}
|
||||
if s[80:] != "30313233343536373839" {
|
||||
panic("3")
|
||||
panic(s[80:])
|
||||
}
|
||||
p, err := ParsePacketHeader(dat)
|
||||
if err != nil {
|
||||
|
||||
20
gold/head/raw.go
Normal file
20
gold/head/raw.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package head
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
const (
|
||||
ttloffset = unsafe.Offsetof(Packet{}.TTL)
|
||||
)
|
||||
|
||||
// ClearTTL for hash use
|
||||
func ClearTTL(data []byte) {
|
||||
data[ttloffset] = 0
|
||||
}
|
||||
|
||||
// DecTTL on transferring
|
||||
func DecTTL(data []byte) (drop bool) {
|
||||
data[ttloffset]--
|
||||
return data[ttloffset] == 0
|
||||
}
|
||||
@@ -54,7 +54,12 @@ func ParsePacketHeader(data []byte) (pbytes PacketBytes, err error) {
|
||||
}
|
||||
return
|
||||
}
|
||||
crc := algo.MD5Hash8(data[:PacketHeadNoCRCLen])
|
||||
var crc uint64
|
||||
pbuf.NewBytes(int(PacketHeadNoCRCLen)).V(func(b []byte) {
|
||||
copy(b, data[:PacketHeadNoCRCLen])
|
||||
ClearTTL(b)
|
||||
crc = algo.MD5Hash8(b)
|
||||
})
|
||||
if crc != uint64(pb.DAT.md5h8rem) {
|
||||
err = ErrBadCRCChecksum
|
||||
if config.ShowDebugLog {
|
||||
|
||||
Reference in New Issue
Block a user