Skip to content

Commit

Permalink
feat(head): use uniform head extract & crc64 -> md5
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed Aug 8, 2024
1 parent e018aee commit d5d7a94
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Get dependencies
run: go mod tidy

- name: Enable Debug Log
- name: Enable debug log
run: sed -i 's/false/true/g' config/global.go

- name: Build
Expand Down
34 changes: 34 additions & 0 deletions gold/head/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package head

import "encoding/binary"

type PacketFlags uint16

func (pf PacketFlags) IsValid() bool {
return pf&0x8000 == 0
}

func (pf PacketFlags) DontFrag() bool {
return pf&0x4000 == 0x4000
}

func (pf PacketFlags) NoFrag() bool {
return pf == 0x4000
}

func (pf PacketFlags) IsSingle() bool {
return pf == 0
}

func (pf PacketFlags) ZeroOffset() bool {
return pf&0x1fff == 0
}

func (pf PacketFlags) Offset() uint16 {
return uint16(pf << 3)
}

// Flags extract flags from raw data
func Flags(data []byte) PacketFlags {
return PacketFlags(binary.LittleEndian.Uint16(data[10:12]))
}
40 changes: 4 additions & 36 deletions gold/head/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/binary"
"encoding/hex"
"errors"
"hash/crc64"
"net"

blake2b "github.com/fumiama/blake2b-simd"
Expand All @@ -21,37 +20,6 @@ var (
ErrDataLenLT60 = errors.New("data len < 60")
)

type PacketFlags uint16

func (pf PacketFlags) IsValid() bool {
return pf&0x8000 == 0
}

func (pf PacketFlags) DontFrag() bool {
return pf&0x4000 == 0x4000
}

func (pf PacketFlags) NoFrag() bool {
return pf == 0x4000
}

func (pf PacketFlags) IsSingle() bool {
return pf == 0
}

func (pf PacketFlags) ZeroOffset() bool {
return pf&0x1fff == 0
}

func (pf PacketFlags) Offset() uint16 {
return uint16(pf << 3)
}

// Flags extract flags from raw data
func Flags(data []byte) PacketFlags {
return PacketFlags(binary.LittleEndian.Uint16(data[10:12]))
}

// Packet 是发送和接收的最小单位
type Packet struct {
// idxdatsz len(Data)
Expand All @@ -77,7 +45,7 @@ type Packet struct {
// 生成时 Hash 全 0
// https://github.com/fumiama/blake2b-simd
Hash [32]byte
// crc64 包头字段的 checksum 值,可以认为在一定时间内唯一
// crc64 包头字段的 checksum 值,可以认为在一定时间内唯一 (现已更改算法为 md5 但名字未变)
crc64 uint64
// data 承载的数据
data []byte
Expand Down Expand Up @@ -108,8 +76,8 @@ func (p *Packet) Unmarshal(data []byte) (complete bool, err error) {
err = ErrDataLenLT60
return
}
p.crc64 = binary.LittleEndian.Uint64(data[52:PacketHeadLen])
if crc64.Checksum(data[:52], crc64.MakeTable(crc64.ISO)) != p.crc64 {
p.crc64 = CRC64(data)
if CalcCRC64(data) != p.crc64 {
err = ErrBadCRCChecksum
return
}
Expand Down Expand Up @@ -189,7 +157,7 @@ func (p *Packet) Marshal(src net.IP, teatype uint8, additional uint16, datasz ui
w.Write(p.Src.To4())
w.Write(p.Dst.To4())
w.Write(p.Hash[:])
w.WriteUInt64(crc64.Checksum(w.Bytes(), crc64.MakeTable(crc64.ISO)))
w.WriteUInt64(CalcCRC64(w.Bytes()))
w.Write(p.Body())
})
}
Expand Down
22 changes: 22 additions & 0 deletions gold/head/raw.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package head

import (
"crypto/md5"
"encoding/binary"
)

// CRC64 extract packet header checksum
func CRC64(data []byte) uint64 {
return binary.LittleEndian.Uint64(data[52:PacketHeadLen])
}

// CalcCRC64 calculate packet header checksum
func CalcCRC64(data []byte) uint64 {
m := md5.Sum(data[:52])
return binary.LittleEndian.Uint64(m[:8])
}

// Hash extract 32 bytes blake2b hash from raw bytes
func Hash(data []byte) []byte {
return data[20:52]
}
4 changes: 2 additions & 2 deletions gold/link/recv.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (m *Me) wait(data []byte) *head.Packet {
}
return nil
}
crc := binary.LittleEndian.Uint64(data[52:head.PacketHeadLen])
crc := head.CRC64(data)
crclog := crc
crc ^= (uint64(seq) << 16)
if config.ShowDebugLog {
Expand All @@ -67,7 +67,7 @@ func (m *Me) wait(data []byte) *head.Packet {
}

crchash := crc64.New(crc64.MakeTable(crc64.ISO))
_, _ = crchash.Write(data[20:52])
_, _ = crchash.Write(head.Hash(data))
var buf [4]byte
binary.LittleEndian.PutUint32(buf[:], seq)
_, _ = crchash.Write(buf[:])
Expand Down

0 comments on commit d5d7a94

Please sign in to comment.