Skip to content

Commit 4b60801

Browse files
committed
fix(nat): hello packet dead loop
1 parent 6049522 commit 4b60801

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

config/global.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
package config
22

3-
const ShowDebugLog = false
3+
const ShowDebugLog = true

gold/head/hello.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package head
2+
3+
type Hello uint8
4+
5+
const (
6+
HelloPing Hello = iota
7+
HelloPong
8+
)

gold/link/listen.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,18 @@ func (m *Me) dispatch(packet *head.Packet, addr p2p.EndPoint, index int, finish
226226
}
227227
switch packet.Proto {
228228
case head.ProtoHello:
229-
n, err := p.WriteAndPut(head.NewPacket(head.ProtoHello, m.SrcPort(), p.peerip, m.DstPort(), nil), false)
230-
if err == nil {
231-
if config.ShowDebugLog {
232-
logrus.Debugln("[listen] @", index, "send", n, "bytes hello ack packet")
229+
switch {
230+
case len(packet.Body()) == 0:
231+
logrus.Warnln("[listen] @", index, "recv old hello packet, do nothing")
232+
case packet.Body()[0] == byte(head.HelloPing):
233+
n, err := p.WriteAndPut(head.NewPacket(head.ProtoHello, m.SrcPort(), p.peerip, m.DstPort(), []byte{byte(head.HelloPong)}), false)
234+
if err == nil {
235+
logrus.Infoln("[listen] @", index, "recv hello, send", n, "bytes hello ack packet")
236+
} else {
237+
logrus.Errorln("[listen] @", index, "send hello ack packet error:", err)
233238
}
234-
} else {
235-
logrus.Errorln("[listen] @", index, "send hello ack packet error:", err)
239+
default:
240+
logrus.Infoln("[listen] @", index, "recv hello ack packet, do nothing")
236241
}
237242
packet.Put()
238243
case head.ProtoNotify:

gold/link/nat.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (l *Link) keepAlive(dur int64) {
3535
logrus.Infoln("[nat] re-connect me succeeded")
3636
}
3737
}
38-
n, err := l.WriteAndPut(head.NewPacket(head.ProtoHello, l.me.srcport, l.peerip, l.me.dstport, nil), false)
38+
n, err := l.WriteAndPut(head.NewPacket(head.ProtoHello, l.me.srcport, l.peerip, l.me.dstport, []byte{byte(head.HelloPing)}), false)
3939
if err == nil {
4040
logrus.Infoln("[nat] send", n, "bytes keep alive packet")
4141
} else {

0 commit comments

Comments
 (0)