refactor: don't return encoder from handshake()
We did this for symmetry/completeness, but the encoder is actually "consumed" by handshake() in the creation of the tcp senders, hence it is in fact dangerous to return it.
This commit is contained in:
1 parent
d6fedc1e6e
commit
ab943d938a
2 files changed
+14
-14
No files matched your search
@@ -279,7 +279,7 @@ func (conn *LocalConnection) run(actionChan <-chan ConnectionAction, finished ch
|
|||||||
|
|
||||||
conn.TCPConn.SetLinger(0)
|
conn.TCPConn.SetLinger(0)
|
||||||
|
|
||||||
_, dec, err := conn.handshake(acceptNewPeer)
|
dec, err := conn.handshake(acceptNewPeer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-13
@@ -45,7 +45,7 @@ func (fv *FieldValidator) Err() error {
|
|||||||
return fv.err
|
return fv.err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (conn *LocalConnection) handshake(acceptNewPeer bool) (*gob.Encoder, *gob.Decoder, error) {
|
func (conn *LocalConnection) handshake(acceptNewPeer bool) (*gob.Decoder, error) {
|
||||||
// We do not need to worry about locking in here as at this point
|
// We do not need to worry about locking in here as at this point
|
||||||
// the connection is not reachable by any go-routine other than
|
// the connection is not reachable by any go-routine other than
|
||||||
// ourself. Only when we add this connection to the conn.local
|
// ourself. Only when we add this connection to the conn.local
|
||||||
@@ -54,7 +54,7 @@ func (conn *LocalConnection) handshake(acceptNewPeer bool) (*gob.Encoder, *gob.D
|
|||||||
conn.TCPConn.SetDeadline(time.Now().Add(HeaderTimeout))
|
conn.TCPConn.SetDeadline(time.Now().Add(HeaderTimeout))
|
||||||
version, err := exchangeProtocolHeader(conn.TCPConn, conn.TCPConn)
|
version, err := exchangeProtocolHeader(conn.TCPConn, conn.TCPConn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
conn.version = version
|
conn.version = version
|
||||||
conn.TCPConn.SetDeadline(time.Time{})
|
conn.TCPConn.SetDeadline(time.Time{})
|
||||||
@@ -66,7 +66,7 @@ func (conn *LocalConnection) handshake(acceptNewPeer bool) (*gob.Encoder, *gob.D
|
|||||||
dec := gob.NewDecoder(conn.TCPConn)
|
dec := gob.NewDecoder(conn.TCPConn)
|
||||||
fv, private, err := conn.exchangeHandshake(localConnID, usingPassword, enc, dec)
|
fv, private, err := conn.exchangeHandshake(localConnID, usingPassword, enc, dec)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
nameStr, _ := fv.Value("Name")
|
nameStr, _ := fv.Value("Name")
|
||||||
@@ -74,39 +74,39 @@ func (conn *LocalConnection) handshake(acceptNewPeer bool) (*gob.Encoder, *gob.D
|
|||||||
uidStr, _ := fv.Value("UID")
|
uidStr, _ := fv.Value("UID")
|
||||||
remoteConnIDStr, _ := fv.Value("ConnID")
|
remoteConnIDStr, _ := fv.Value("ConnID")
|
||||||
if err := fv.Err(); err != nil {
|
if err := fv.Err(); err != nil {
|
||||||
return nil, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
name, err := PeerNameFromString(nameStr)
|
name, err := PeerNameFromString(nameStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if !acceptNewPeer {
|
if !acceptNewPeer {
|
||||||
if _, found := conn.Router.Peers.Fetch(name); !found {
|
if _, found := conn.Router.Peers.Fetch(name); !found {
|
||||||
return nil, nil, fmt.Errorf("Found unknown remote name: %s at %s", name, conn.remoteTCPAddr)
|
return nil, fmt.Errorf("Found unknown remote name: %s at %s", name, conn.remoteTCPAddr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if existingConn, found := conn.Router.Ourself.ConnectionTo(name); found && existingConn.Established() {
|
if existingConn, found := conn.Router.Ourself.ConnectionTo(name); found && existingConn.Established() {
|
||||||
return nil, nil, fmt.Errorf("Already have connection to %s at %s", existingConn.Remote(), existingConn.RemoteTCPAddr())
|
return nil, fmt.Errorf("Already have connection to %s at %s", existingConn.Remote(), existingConn.RemoteTCPAddr())
|
||||||
}
|
}
|
||||||
uid, err := ParsePeerUID(uidStr)
|
uid, err := ParsePeerUID(uidStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
remoteConnID, err := strconv.ParseUint(remoteConnIDStr, 10, 64)
|
remoteConnID, err := strconv.ParseUint(remoteConnIDStr, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
conn.uid = localConnID ^ remoteConnID
|
conn.uid = localConnID ^ remoteConnID
|
||||||
|
|
||||||
remotePublicStr, rpErr := fv.Value("PublicKey")
|
remotePublicStr, rpErr := fv.Value("PublicKey")
|
||||||
if usingPassword {
|
if usingPassword {
|
||||||
if rpErr != nil {
|
if rpErr != nil {
|
||||||
return nil, nil, rpErr
|
return nil, rpErr
|
||||||
}
|
}
|
||||||
remotePublicSlice, rpErr := hex.DecodeString(remotePublicStr)
|
remotePublicSlice, rpErr := hex.DecodeString(remotePublicStr)
|
||||||
if rpErr != nil {
|
if rpErr != nil {
|
||||||
return nil, nil, rpErr
|
return nil, rpErr
|
||||||
}
|
}
|
||||||
remotePublic := [32]byte{}
|
remotePublic := [32]byte{}
|
||||||
for idx, elem := range remotePublicSlice {
|
for idx, elem := range remotePublicSlice {
|
||||||
@@ -117,13 +117,13 @@ func (conn *LocalConnection) handshake(acceptNewPeer bool) (*gob.Encoder, *gob.D
|
|||||||
conn.Decryptor = NewNaClDecryptor(conn.SessionKey, conn.outbound)
|
conn.Decryptor = NewNaClDecryptor(conn.SessionKey, conn.outbound)
|
||||||
} else {
|
} else {
|
||||||
if rpErr == nil {
|
if rpErr == nil {
|
||||||
return nil, nil, fmt.Errorf("Remote network is encrypted. Password required.")
|
return nil, fmt.Errorf("Remote network is encrypted. Password required.")
|
||||||
}
|
}
|
||||||
conn.tcpSender = NewSimpleTCPSender(enc)
|
conn.tcpSender = NewSimpleTCPSender(enc)
|
||||||
conn.Decryptor = NewNonDecryptor()
|
conn.Decryptor = NewNonDecryptor()
|
||||||
}
|
}
|
||||||
|
|
||||||
return enc, dec, conn.setRemote(NewPeer(name, nickNameStr, uid, 0))
|
return dec, conn.setRemote(NewPeer(name, nickNameStr, uid, 0))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (conn *LocalConnection) exchangeHandshake(localConnID uint64, usingPassword bool, enc *gob.Encoder, dec *gob.Decoder) (*FieldValidator, *[32]byte, error) {
|
func (conn *LocalConnection) exchangeHandshake(localConnID uint64, usingPassword bool, enc *gob.Encoder, dec *gob.Decoder) (*FieldValidator, *[32]byte, error) {
|
||||||
|
|||||||
Reference in new issue
Block a user