]> Cypherpunks.ru repositories - govpn.git/blobdiff - src/govpn/cmd/govpn-server/tcp.go
Move nonce expectation calculation to common function
[govpn.git] / src / govpn / cmd / govpn-server / tcp.go
index 6dd3a010437e3ca2632eea60f519475bd2c0e3bd..437b5005aa8ca11b085419e484bf218108941c3a 100644 (file)
@@ -20,7 +20,6 @@ package main
 
 import (
        "bytes"
-       "encoding/binary"
        "log"
        "net"
        "time"
@@ -72,12 +71,12 @@ func handleTCP(conn net.Conn) {
                        break
                }
                prev += n
-               peerId := govpn.IDsCache.Find(buf[:prev])
+               peerId := idsCache.Find(buf[:prev])
                if peerId == nil {
                        continue
                }
                if hs == nil {
-                       conf = peerId.Conf()
+                       conf = confs[*peerId]
                        if conf == nil {
                                log.Println("Can not get peer configuration:", peerId.String())
                                break
@@ -95,28 +94,25 @@ func handleTCP(conn net.Conn) {
                addrPrev, exists := peersById[*peer.Id]
                peersByIdLock.RUnlock()
                if exists {
-                       peersLock.RLock()
+                       peersLock.Lock()
+                       peers[addrPrev].terminator <- struct{}{}
                        tap = peers[addrPrev].tap
                        ps = &PeerState{
                                peer:       peer,
                                tap:        tap,
-                               terminator: peers[addrPrev].terminator,
+                               terminator: make(chan struct{}),
                        }
-                       peersLock.RUnlock()
-                       ps.terminator <- struct{}{}
-                       peersLock.Lock()
+                       go peerReady(*ps)
                        peersByIdLock.Lock()
                        kpLock.Lock()
                        delete(peers, addrPrev)
                        delete(knownPeers, addrPrev)
-                       delete(peersById, *peer.Id)
                        peers[addr] = ps
                        knownPeers[addr] = &peer
                        peersById[*peer.Id] = addr
                        peersLock.Unlock()
                        peersByIdLock.Unlock()
                        kpLock.Unlock()
-                       go peerReady(*ps)
                        log.Println("Rehandshake processed:", peer.Id.String())
                } else {
                        ifaceName, err := callUp(peer.Id)
@@ -155,8 +151,7 @@ func handleTCP(conn net.Conn) {
        }
 
        nonceExpectation := make([]byte, govpn.NonceSize)
-       binary.BigEndian.PutUint64(nonceExpectation, peer.NonceExpect)
-       peer.NonceCipher.Encrypt(nonceExpectation, nonceExpectation)
+       peer.NonceExpectation(nonceExpectation)
        prev = 0
        var i int
        for {
@@ -185,8 +180,7 @@ func handleTCP(conn net.Conn) {
                        )
                        break
                }
-               binary.BigEndian.PutUint64(nonceExpectation, peer.NonceExpect)
-               peer.NonceCipher.Encrypt(nonceExpectation, nonceExpectation)
+               peer.NonceExpectation(nonceExpectation)
                copy(buf, buf[i+govpn.NonceSize:prev])
                prev = prev - i - govpn.NonceSize
                goto CheckMore