1
0
mirror of https://github.com/fumiama/WireGold.git synced 2026-06-21 02:40:24 +08:00

feat: impl. trans & ttl

This commit is contained in:
源文雨
2025-03-13 01:52:35 +09:00
parent 658916268a
commit 6fc45333d8
20 changed files with 335 additions and 271 deletions

View File

@@ -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) {