mirror of
https://github.com/fumiama/WireGold.git
synced 2026-06-11 04:06:14 +08:00
fix: mtu calculation
This commit is contained in:
@@ -170,10 +170,6 @@ func (m *Me) sendAllSameDst(packet []byte) (n int) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
i += int(totl)
|
i += int(totl)
|
||||||
if i > int(m.mtu) {
|
|
||||||
logrus.Debugln("[me] wrap exceed mtu, break")
|
|
||||||
break
|
|
||||||
}
|
|
||||||
ptr = rem[i:]
|
ptr = rem[i:]
|
||||||
logrus.Debugln("[me] wrap", totl, "bytes packet to send together")
|
logrus.Debugln("[me] wrap", totl, "bytes packet to send together")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ func (m *Me) AddPeer(cfg *PeerConfig) (l *Link) {
|
|||||||
if ok {
|
if ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if cfg.MTU == 0 || cfg.MTU == 65535 || cfg.MTU%8 != 0 || (m.mtu != 0 && cfg.MTU > m.mtu) {
|
if cfg.MTU == 0 || (m.mtu != 0 && cfg.MTU > m.mtu) {
|
||||||
panic("invalid mtu for peer " + cfg.PeerIP)
|
panic("invalid mtu for peer " + cfg.PeerIP)
|
||||||
}
|
}
|
||||||
l = &Link{
|
l = &Link{
|
||||||
|
|||||||
@@ -21,39 +21,44 @@ func (l *Link) WriteAndPut(p *head.Packet, istransfer bool) (n int, err error) {
|
|||||||
sndcnt := atomic.AddUintptr(&l.sendcount, 1)
|
sndcnt := atomic.AddUintptr(&l.sendcount, 1)
|
||||||
mtu := l.mtu
|
mtu := l.mtu
|
||||||
if l.mturandomrange > 0 {
|
if l.mturandomrange > 0 {
|
||||||
mtu -= uint16(rand.Intn(int(l.mturandomrange))) & 0xfff8
|
mtu -= uint16(rand.Intn(int(l.mturandomrange)))
|
||||||
}
|
|
||||||
logrus.Debugln("[send] mtu:", mtu, ", count:", sndcnt, ", additional data:", uint16(sndcnt))
|
|
||||||
if len(p.Data) <= int(mtu) {
|
|
||||||
if !istransfer {
|
|
||||||
l.encrypt(p, uint16(sndcnt), teatype)
|
|
||||||
}
|
|
||||||
defer p.Put()
|
|
||||||
return l.write(p, teatype, uint16(sndcnt), mtu, uint32(len(p.Data)), 0, istransfer, false)
|
|
||||||
}
|
}
|
||||||
|
logrus.Debugln("[send] mtu:", mtu, ", count:", sndcnt, ", additional data:", uint16(sndcnt)&0x0fff)
|
||||||
if !istransfer {
|
if !istransfer {
|
||||||
l.encrypt(p, uint16(sndcnt), teatype)
|
l.encrypt(p, uint16(sndcnt), teatype)
|
||||||
}
|
}
|
||||||
|
delta := (int(mtu) - 60) & 0x0000fff8
|
||||||
|
if delta <= 0 {
|
||||||
|
logrus.Warnln("[send] reset invalid data frag len", delta, "to 8")
|
||||||
|
delta = 8
|
||||||
|
}
|
||||||
|
if len(p.Data) <= delta {
|
||||||
|
defer p.Put()
|
||||||
|
return l.write(p, teatype, uint16(sndcnt), uint32(len(p.Data)), 0, istransfer, false)
|
||||||
|
}
|
||||||
|
if istransfer && p.Flags&0x4000 == 0x4000 && len(p.Data) > delta {
|
||||||
|
return 0, errors.New("drop dont fragmnet big trans packet")
|
||||||
|
}
|
||||||
data := p.Data
|
data := p.Data
|
||||||
ttl := p.TTL
|
ttl := p.TTL
|
||||||
totl := uint32(len(data))
|
totl := uint32(len(data))
|
||||||
i := 0
|
i := 0
|
||||||
packet := head.SelectPacket()
|
packet := head.SelectPacket()
|
||||||
*packet = *p
|
*packet = *p
|
||||||
for ; int(totl)-i > int(mtu); i += int(mtu) {
|
for ; int(totl)-i > delta; i += delta {
|
||||||
logrus.Debugln("[send] split frag [", i, "~", i+int(mtu), "], remain:", int(totl)-i-int(mtu))
|
logrus.Debugln("[send] split frag [", i, "~", i+delta, "], remain:", int(totl)-i-delta)
|
||||||
packet.Data = data[:int(mtu)]
|
packet.Data = data[:delta]
|
||||||
cnt, err := l.write(packet, teatype, uint16(sndcnt), mtu, totl, uint16(i>>3), istransfer, true)
|
cnt, err := l.write(packet, teatype, uint16(sndcnt), totl, uint16(i>>3), istransfer, true)
|
||||||
n += cnt
|
n += cnt
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return n, err
|
return n, err
|
||||||
}
|
}
|
||||||
data = data[int(mtu):]
|
data = data[delta:]
|
||||||
packet.TTL = ttl
|
packet.TTL = ttl
|
||||||
}
|
}
|
||||||
packet.Put()
|
packet.Put()
|
||||||
p.Data = data
|
p.Data = data
|
||||||
cnt, err := l.write(p, teatype, uint16(sndcnt), mtu, totl, uint16(i>>3), istransfer, false)
|
cnt, err := l.write(p, teatype, uint16(sndcnt), totl, uint16(i>>3), istransfer, false)
|
||||||
p.Put()
|
p.Put()
|
||||||
n += cnt
|
n += cnt
|
||||||
return n, err
|
return n, err
|
||||||
@@ -80,13 +85,10 @@ func (l *Link) encrypt(p *head.Packet, sndcnt uint16, teatype uint8) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// write 向 peer 发一个包
|
// write 向 peer 发一个包
|
||||||
func (l *Link) write(p *head.Packet, teatype uint8, additional, mtu uint16, datasz uint32, offset uint16, istransfer, hasmore bool) (n int, err error) {
|
func (l *Link) write(p *head.Packet, teatype uint8, additional uint16, datasz uint32, offset uint16, istransfer, hasmore bool) (n int, err error) {
|
||||||
var d []byte
|
var d []byte
|
||||||
var cl func()
|
var cl func()
|
||||||
if istransfer {
|
if istransfer {
|
||||||
if p.Flags&0x4000 == 0x4000 && len(p.Data) > int(mtu) {
|
|
||||||
return len(p.Data), errors.New("drop dont fragmnet big trans packet")
|
|
||||||
}
|
|
||||||
d, cl = p.Marshal(nil, teatype, additional, 0, 0, false, false)
|
d, cl = p.Marshal(nil, teatype, additional, 0, 0, false, false)
|
||||||
} else {
|
} else {
|
||||||
d, cl = p.Marshal(l.me.me, teatype, additional, datasz, offset, false, hasmore)
|
d, cl = p.Marshal(l.me.me, teatype, additional, datasz, offset, false, hasmore)
|
||||||
|
|||||||
Reference in New Issue
Block a user