mirror of
https://github.com/fumiama/WireGold.git
synced 2026-06-04 23:40:26 +08:00
feat(head): add more method to packet
This commit is contained in:
@@ -63,8 +63,8 @@ func NewPacketPartial(
|
||||
proto uint8, srcPort uint16,
|
||||
dst net.IP, dstPort uint16,
|
||||
data pbuf.Bytes,
|
||||
) (p *orbyte.Item[Packet]) {
|
||||
p = selectPacket()
|
||||
) *orbyte.Item[Packet] {
|
||||
p := selectPacket()
|
||||
pp := p.Pointer()
|
||||
pp.Proto = proto
|
||||
pp.TTL = 16
|
||||
@@ -73,7 +73,7 @@ func NewPacketPartial(
|
||||
pp.Dst = dst
|
||||
pp.data = data
|
||||
pp.b = data.Len()
|
||||
return
|
||||
return p
|
||||
}
|
||||
|
||||
func ParsePacket(p Packet) *orbyte.Item[Packet] {
|
||||
@@ -131,7 +131,7 @@ func ParsePacketHeader(data []byte) (p *orbyte.Item[Packet], err error) {
|
||||
func (p *Packet) ParseData(data []byte) (complete bool) {
|
||||
sz := p.Len()
|
||||
if sz+PacketHeadLen == len(data) {
|
||||
p.data = pbuf.ParseBytes(data[PacketHeadLen:]...)
|
||||
p.data = pbuf.ParseBytes(data[PacketHeadLen:]...).Copy()
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -199,14 +199,14 @@ func (p *Packet) MarshalWith(
|
||||
w.Write(p.Hash[:])
|
||||
p.crc64 = CalcCRC64(w.UnsafeBytes())
|
||||
w.WriteUInt64(p.crc64)
|
||||
w.Write(p.Body())
|
||||
w.Write(p.UnsafeBody())
|
||||
})
|
||||
}
|
||||
|
||||
// FillHash 生成 p.Data 的 Hash
|
||||
func (p *Packet) FillHash() {
|
||||
h := blake2b.New256()
|
||||
_, err := h.Write(p.Body())
|
||||
_, err := h.Write(p.UnsafeBody())
|
||||
if err != nil {
|
||||
logrus.Errorln("[packet] err when fill hash:", err)
|
||||
return
|
||||
@@ -220,7 +220,7 @@ func (p *Packet) FillHash() {
|
||||
// IsVaildHash 验证 packet 合法性
|
||||
func (p *Packet) IsVaildHash() bool {
|
||||
h := blake2b.New256()
|
||||
_, err := h.Write(p.Body())
|
||||
_, err := h.Write(p.UnsafeBody())
|
||||
if err != nil {
|
||||
logrus.Errorln("[packet] err when check hash:", err)
|
||||
return false
|
||||
@@ -228,7 +228,7 @@ func (p *Packet) IsVaildHash() bool {
|
||||
var sum [32]byte
|
||||
_ = h.Sum(sum[:0])
|
||||
if config.ShowDebugLog {
|
||||
logrus.Debugln("[packet] sum data len:", len(p.Body()))
|
||||
logrus.Debugln("[packet] sum data len:", len(p.UnsafeBody()))
|
||||
logrus.Debugln("[packet] sum calulated:", hex.EncodeToString(sum[:]))
|
||||
logrus.Debugln("[packet] sum in packet:", hex.EncodeToString(p.Hash[:]))
|
||||
}
|
||||
@@ -254,8 +254,15 @@ func (p *Packet) CRC64() uint64 {
|
||||
return p.crc64
|
||||
}
|
||||
|
||||
// Body returns data
|
||||
func (p *Packet) Body() []byte {
|
||||
// TransBody returns item.Trans().Slice()
|
||||
func (p *Packet) TransBody() pbuf.Bytes {
|
||||
d := p.data.Trans().Slice(p.a, p.b)
|
||||
p.data = pbuf.Bytes{}
|
||||
return d
|
||||
}
|
||||
|
||||
// UnsafeBody returns data
|
||||
func (p *Packet) UnsafeBody() []byte {
|
||||
return p.data.Bytes()[p.a:p.b]
|
||||
}
|
||||
|
||||
|
||||
@@ -47,7 +47,7 @@ func TestMarshalUnmarshal(t *testing.T) {
|
||||
t.Fatal("index", i)
|
||||
}
|
||||
if !p.Pointer().IsVaildHash() {
|
||||
t.Fatal("index", i, "expect body", hex.EncodeToString(data.SliceTo(i).Bytes()), "got", hex.EncodeToString(p.Pointer().Body()))
|
||||
t.Fatal("index", i, "expect body", hex.EncodeToString(data.SliceTo(i).Bytes()), "got", hex.EncodeToString(p.Pointer().UnsafeBody()))
|
||||
}
|
||||
if p.Pointer().Proto != proto {
|
||||
t.Fatal("index", i)
|
||||
|
||||
@@ -126,7 +126,7 @@ func (m *Me) waitordispatch(index int, addr p2p.EndPoint, buf pbuf.Bytes, hasntf
|
||||
return
|
||||
}
|
||||
if config.ShowDebugLog {
|
||||
logrus.Debugln("[listen] index", index, "dispatch", len(packet.Pointer().Body()), "bytes packet")
|
||||
logrus.Debugln("[listen] index", index, "dispatch", len(packet.Pointer().UnsafeBody()), "bytes packet")
|
||||
}
|
||||
if index >= 0 {
|
||||
defer hasntfinished[index].Unlock()
|
||||
@@ -176,24 +176,27 @@ func (m *Me) dispatch(packet *orbyte.Item[head.Packet], addr p2p.EndPoint, index
|
||||
}
|
||||
addt := pp.AdditionalData()
|
||||
var err error
|
||||
data, err := p.decode(pp.CipherIndex(), addt, pp.Body())
|
||||
data, err := p.decode(pp.CipherIndex(), addt, pp.UnsafeBody())
|
||||
if err != nil {
|
||||
if config.ShowDebugLog {
|
||||
logrus.Debugln("[listen] @", index, "drop invalid packet key idx:", pp.CipherIndex(), "addt:", addt, "err:", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
pp.SetBody(data.Trans().Bytes())
|
||||
if p.usezstd {
|
||||
dat, err := decodezstd(pp.Body())
|
||||
dat, err := decodezstd(data.Trans().Bytes())
|
||||
if err != nil {
|
||||
if config.ShowDebugLog {
|
||||
logrus.Debugln("[listen] @", index, "drop invalid zstd packet:", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
pp.SetBody(dat.Trans().Bytes())
|
||||
if config.ShowDebugLog {
|
||||
logrus.Debugln("[listen] @", index, "zstd decoded len:", dat.Len())
|
||||
}
|
||||
data = dat
|
||||
}
|
||||
pp.SetBody(data.Trans().Bytes())
|
||||
if !pp.IsVaildHash() {
|
||||
if config.ShowDebugLog {
|
||||
logrus.Debugln("[listen] @", index, "drop invalid hash packet")
|
||||
@@ -203,9 +206,9 @@ func (m *Me) dispatch(packet *orbyte.Item[head.Packet], addr p2p.EndPoint, index
|
||||
switch pp.Proto {
|
||||
case head.ProtoHello:
|
||||
switch {
|
||||
case len(pp.Body()) == 0:
|
||||
case len(pp.UnsafeBody()) == 0:
|
||||
logrus.Warnln("[listen] @", index, "recv old hello packet, do nothing")
|
||||
case pp.Body()[0] == byte(head.HelloPing):
|
||||
case pp.UnsafeBody()[0] == byte(head.HelloPing):
|
||||
n, err := p.WritePacket(head.NewPacketPartial(
|
||||
head.ProtoHello, m.SrcPort(), p.peerip, m.DstPort(), pbuf.ParseBytes(byte(head.HelloPong))), false)
|
||||
if err == nil {
|
||||
@@ -218,10 +221,10 @@ func (m *Me) dispatch(packet *orbyte.Item[head.Packet], addr p2p.EndPoint, index
|
||||
}
|
||||
case head.ProtoNotify:
|
||||
logrus.Infoln("[listen] @", index, "recv notify from", pp.Src)
|
||||
p.onNotify(pp.Body())
|
||||
p.onNotify(pp.UnsafeBody())
|
||||
case head.ProtoQuery:
|
||||
logrus.Infoln("[listen] @", index, "recv query from", pp.Src)
|
||||
p.onQuery(pp.Body())
|
||||
p.onQuery(pp.UnsafeBody())
|
||||
case head.ProtoData:
|
||||
if p.pipe != nil {
|
||||
p.pipe <- packet.Copy()
|
||||
@@ -229,7 +232,7 @@ func (m *Me) dispatch(packet *orbyte.Item[head.Packet], addr p2p.EndPoint, index
|
||||
logrus.Debugln("[listen] @", index, "deliver to pipe of", p.peerip)
|
||||
}
|
||||
} else {
|
||||
_, err := m.nic.Write(pp.Body())
|
||||
_, err := m.nic.Write(pp.UnsafeBody())
|
||||
if err != nil {
|
||||
logrus.Errorln("[listen] @", index, "deliver", pp.BodyLen(), "bytes data to nic err:", err)
|
||||
} else if config.ShowDebugLog {
|
||||
|
||||
@@ -96,7 +96,7 @@ func (l *Link) encrypt(p *head.Packet, sndcnt uint16, teatype uint8) {
|
||||
if config.ShowDebugLog {
|
||||
logrus.Debugln("[send] data len before encrypt:", p.BodyLen())
|
||||
}
|
||||
data := p.Body()
|
||||
data := p.TransBody().Bytes()
|
||||
if l.usezstd {
|
||||
data = encodezstd(data).Trans().Bytes()
|
||||
if config.ShowDebugLog {
|
||||
|
||||
Reference in New Issue
Block a user