revamp error handling on tcp connection receive loop
- treat crypto decoding errors as errors instead of warnings - introduce helper for handling protocol messages - put conn.Shutdown in one place
This commit is contained in:
1 parent
6253be6ecf
commit
3baa3c09a4
1 file changed
+29
-30
+29
-30
@@ -534,28 +534,30 @@ func (conn *LocalConnection) receiveTCP(decoder *gob.Decoder) {
|
|||||||
var msg []byte
|
var msg []byte
|
||||||
conn.extendReadDeadline()
|
conn.extendReadDeadline()
|
||||||
if err = decoder.Decode(&msg); err != nil {
|
if err = decoder.Decode(&msg); err != nil {
|
||||||
conn.Shutdown(err)
|
break
|
||||||
return
|
|
||||||
}
|
}
|
||||||
msg, err = receiver.Decode(msg)
|
msg, err = receiver.Decode(msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
checkWarn(err)
|
break
|
||||||
// remote peer may be using wrong password. Or there could
|
|
||||||
// be some sort of injection attack going on. Just ignore
|
|
||||||
// the traffic rather than shutting down.
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
if len(msg) < 1 {
|
if len(msg) < 1 {
|
||||||
conn.log("ignoring blank msg")
|
conn.log("ignoring blank msg")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
payload := msg[1:]
|
if err = conn.handleProtocolMsg(ProtocolTag(msg[0]), msg[1:]); err != nil {
|
||||||
switch ProtocolTag(msg[0]) {
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
conn.Shutdown(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (conn *LocalConnection) handleProtocolMsg(tag ProtocolTag, payload []byte) error {
|
||||||
|
switch tag {
|
||||||
case ProtocolConnectionEstablished:
|
case ProtocolConnectionEstablished:
|
||||||
// We sent fast heartbeats to the remote peer, which has
|
// We sent fast heartbeats to the remote peer, which has now
|
||||||
// now received at least one of them and told us via this
|
// received at least one of them and told us via this message.
|
||||||
// message. We can now consider the connection as
|
// We can now consider the connection as established from our
|
||||||
// established from our end.
|
// end.
|
||||||
conn.SetEstablished()
|
conn.SetEstablished()
|
||||||
case ProtocolStartFragmentationTest:
|
case ProtocolStartFragmentationTest:
|
||||||
conn.Forward(false, &ForwardedFrame{
|
conn.Forward(false, &ForwardedFrame{
|
||||||
@@ -566,24 +568,22 @@ func (conn *LocalConnection) receiveTCP(decoder *gob.Decoder) {
|
|||||||
case ProtocolFragmentationReceived:
|
case ProtocolFragmentationReceived:
|
||||||
conn.setStackFrag(true)
|
conn.setStackFrag(true)
|
||||||
case ProtocolNonce:
|
case ProtocolNonce:
|
||||||
if usingPassword {
|
if conn.SessionKey == nil {
|
||||||
conn.Decryptor.ReceiveNonce(payload)
|
return fmt.Errorf("unexpected nonce on unencrypted connection")
|
||||||
} else {
|
|
||||||
conn.log("ignoring unexpected nonce on unencrypted connection")
|
|
||||||
}
|
}
|
||||||
|
conn.Decryptor.ReceiveNonce(payload)
|
||||||
case ProtocolFetchAll:
|
case ProtocolFetchAll:
|
||||||
// There are exactly two messages that relate to topology
|
// There are exactly two messages that relate to topology
|
||||||
// updates.
|
// updates.
|
||||||
//
|
//
|
||||||
// 1. FetchAll. This carries no payload. The receiver
|
// 1. FetchAll. This carries no payload. The receiver responds
|
||||||
// responds with the entire topology model as the receiver
|
// with the entire topology model as the receiver has it.
|
||||||
// has it.
|
|
||||||
//
|
//
|
||||||
// 2. Update. This carries a topology payload. The
|
// 2. Update. This carries a topology payload. The receiver
|
||||||
// receiver merges it with its own topology model. If the
|
// merges it with its own topology model. If the payload is a
|
||||||
// payload is a subset of the receiver's topology, no
|
// subset of the receiver's topology, no further action is
|
||||||
// further action is taken. Otherwise, the receiver sends
|
// taken. Otherwise, the receiver sends out to all its
|
||||||
// out to all its connections an "improved" update.
|
// connections an "improved" update.
|
||||||
conn.SendProtocolMsg(ProtocolMsg{ProtocolUpdate, conn.Router.Peers.EncodeAllPeers()})
|
conn.SendProtocolMsg(ProtocolMsg{ProtocolUpdate, conn.Router.Peers.EncodeAllPeers()})
|
||||||
case ProtocolUpdate:
|
case ProtocolUpdate:
|
||||||
newUpdate, err := conn.Router.Peers.ApplyUpdate(payload)
|
newUpdate, err := conn.Router.Peers.ApplyUpdate(payload)
|
||||||
@@ -591,11 +591,10 @@ func (conn *LocalConnection) receiveTCP(decoder *gob.Decoder) {
|
|||||||
// That update contained a peer we didn't know about;
|
// That update contained a peer we didn't know about;
|
||||||
// request full update
|
// request full update
|
||||||
conn.SendProtocolMsg(ProtocolMsg{ProtocolFetchAll, nil})
|
conn.SendProtocolMsg(ProtocolMsg{ProtocolFetchAll, nil})
|
||||||
continue
|
break
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
conn.Shutdown(err)
|
return err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
if len(newUpdate) != 0 {
|
if len(newUpdate) != 0 {
|
||||||
conn.Router.ConnectionMaker.Refresh()
|
conn.Router.ConnectionMaker.Refresh()
|
||||||
@@ -605,9 +604,9 @@ func (conn *LocalConnection) receiveTCP(decoder *gob.Decoder) {
|
|||||||
case ProtocolPMTUVerified:
|
case ProtocolPMTUVerified:
|
||||||
conn.verifyPMTU <- int(binary.BigEndian.Uint16(payload))
|
conn.verifyPMTU <- int(binary.BigEndian.Uint16(payload))
|
||||||
default:
|
default:
|
||||||
conn.log("received unknown msg:\n", msg)
|
conn.log("ignoring unknown protocol tag:", tag)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (conn *LocalConnection) extendReadDeadline() {
|
func (conn *LocalConnection) extendReadDeadline() {
|
||||||
|
|||||||
Reference in new issue
Block a user