]> Cypherpunks.ru repositories - govpn.git/blobdiff - src/cypherpunks.ru/govpn/peer.go
Split long lines
[govpn.git] / src / cypherpunks.ru / govpn / peer.go
index 54bef040ea1cece2d9f65a4a5acf24a0b6171fb1..e914cd69f5216903a718a5aaa2ea92bcab1dcd5e 100644 (file)
@@ -36,10 +36,10 @@ import (
 
 const (
        // NonceSize is nonce size
-       NonceSize                 = 8
-       nonceBucketSize           = 256
-       tagSize                   = poly1305.TagSize
-       chacha20InternalBlockSize = 64
+       NonceSize       = 8
+       nonceBucketSize = 256
+       tagSize         = poly1305.TagSize
+       CC20IBS         = 64
        // MaxBytesPerKey is maximal amount of bytes transferred with single key (4 GiB)
        MaxBytesPerKey uint64 = 1 << 32
        // Heartbeat rate, relative to Timeout
@@ -139,7 +139,8 @@ type Peer struct {
        noncesT  chan *[NonceSize]byte
 }
 
-// LogFields return a logrus compatible Fields to identity a single peer in logs
+// LogFields returns a logrus compatible Fields to identity a single
+// peer in logs
 func (p *Peer) LogFields() logrus.Fields {
        return logrus.Fields{
                logPrefixPeer + "addr":        p.Addr,
@@ -149,8 +150,9 @@ func (p *Peer) LogFields() logrus.Fields {
        }
 }
 
-// ConfigurationLogFields return a logrus compatible Fields with the settings of
-// a single peer. Complement LogFields() for extra debugging details.
+// ConfigurationLogFields returns a logrus compatible Fields with the
+// settings of a single peer. Complement LogFields() for extra debugging
+// details.
 func (p *Peer) ConfigurationLogFields() logrus.Fields {
        return logrus.Fields{
                logPrefixPeer + "timeout":  p.Timeout.String(),
@@ -205,7 +207,7 @@ func newPeer(isClient bool, addr string, conn io.Writer, conf *PeerConf, key *[S
                timeout = timeout / timeoutHeartbeat
        }
 
-       bufSize := chacha20InternalBlockSize + 2*conf.MTU
+       bufSize := CC20IBS + 2*conf.MTU
        if conf.Encless {
                bufSize += EnclessEnlargeSize
                noiseEnable = true
@@ -291,14 +293,18 @@ func newPeer(isClient bool, addr string, conn io.Writer, conf *PeerConf, key *[S
 // packets will be sent to remote Peer side immediately.
 func (p *Peer) EthProcess(data []byte) error {
        const paddingSize = 1
-       lenData := len(data)
-       if lenData > p.MTU-paddingSize {
-               logger.WithFields(p.LogFields()).WithFields(p.ConfigurationLogFields()).WithFields(
+       if len(data) > p.MTU-paddingSize {
+               logger.WithFields(
+                       p.LogFields(),
+               ).WithFields(
+                       p.ConfigurationLogFields(),
+               ).WithFields(
                        logrus.Fields{
                                "func":        logFuncPrefix + "Peer.EthProcess",
                                "padding":     paddingSize,
-                               "packet_size": lenData,
-                       }).Warning("Ignore padded data packet larger than MTU")
+                               "packet_size": len(data),
+                       },
+               ).Warning("Ignore padded data packet larger than MTU")
                return nil
        }
        p.BusyT.Lock()
@@ -306,23 +312,23 @@ func (p *Peer) EthProcess(data []byte) error {
 
        // Zero size is a heartbeat packet
        SliceZero(p.bufT)
-       if lenData == 0 {
-               p.bufT[chacha20InternalBlockSize+0] = padByte
+       if len(data) == 0 {
+               p.bufT[CC20IBS+0] = padByte
                p.HeartbeatSent++
        } else {
                // Copy payload to our internal buffer and we are ready to
                // accept the next one
-               copy(p.bufT[chacha20InternalBlockSize:], data)
-               p.bufT[chacha20InternalBlockSize+lenData] = padByte
-               p.BytesPayloadOut += uint64(lenData)
+               copy(p.bufT[CC20IBS:], data)
+               p.bufT[CC20IBS+len(data)] = padByte
+               p.BytesPayloadOut += uint64(len(data))
        }
 
        if p.NoiseEnable && !p.Encless {
-               p.frameT = p.bufT[chacha20InternalBlockSize : chacha20InternalBlockSize+p.MTU-tagSize]
+               p.frameT = p.bufT[CC20IBS : CC20IBS+p.MTU-tagSize]
        } else if p.Encless {
-               p.frameT = p.bufT[chacha20InternalBlockSize : chacha20InternalBlockSize+p.MTU]
+               p.frameT = p.bufT[CC20IBS : CC20IBS+p.MTU]
        } else {
-               p.frameT = p.bufT[chacha20InternalBlockSize : chacha20InternalBlockSize+lenData+1+NonceSize]
+               p.frameT = p.bufT[CC20IBS : CC20IBS+len(data)+1+NonceSize]
        }
        copy(p.frameT[len(p.frameT)-NonceSize:], (<-p.noncesT)[:])
        var out []byte
@@ -336,8 +342,8 @@ func (p *Peer) EthProcess(data []byte) error {
                out = append(out, p.frameT[len(p.frameT)-NonceSize:]...)
        } else {
                chacha20.XORKeyStream(
-                       p.bufT[:chacha20InternalBlockSize+len(p.frameT)-NonceSize],
-                       p.bufT[:chacha20InternalBlockSize+len(p.frameT)-NonceSize],
+                       p.bufT[:CC20IBS+len(p.frameT)-NonceSize],
+                       p.bufT[:CC20IBS+len(p.frameT)-NonceSize],
                        p.nonceT,
                        p.key,
                )
@@ -353,27 +359,35 @@ func (p *Peer) EthProcess(data []byte) error {
 
 // PktProcess processes data of a single packet
 func (p *Peer) PktProcess(data []byte, tap io.Writer, reorderable bool) bool {
-       lenData := len(data)
        fields := logrus.Fields{
                "func":        logFuncPrefix + "Peer.PktProcess",
                "reorderable": reorderable,
-               "data":        lenData,
+               "data":        len(data),
        }
-       if lenData < MinPktLength {
-               logger.WithFields(p.LogFields()).WithFields(fields).WithField("minimum_packet_Length", MinPktLength).Debug("Ignore packet smaller than allowed minimum")
+       if len(data) < MinPktLength {
+               logger.WithFields(
+                       p.LogFields(),
+               ).WithFields(
+                       fields,
+               ).WithField(
+                       "minimum_packet_Length",
+                       MinPktLength,
+               ).Debug("Ignore packet smaller than allowed minimum")
                return false
        }
-       if !p.Encless && lenData > len(p.bufR)-chacha20InternalBlockSize {
+       if !p.Encless && len(data) > len(p.bufR)-CC20IBS {
                return false
        }
        var out []byte
        p.BusyR.Lock() // TODO use defer to unlock?
-       copy(p.nonceR[8:], data[lenData-NonceSize:])
+       copy(p.nonceR[8:], data[len(data)-NonceSize:])
        if p.Encless {
                var err error
-               out, err = EnclessDecode(p.key, p.nonceR, data[:lenData-NonceSize])
+               out, err = EnclessDecode(p.key, p.nonceR, data[:len(data)-NonceSize])
                if err != nil {
-                       logger.WithFields(p.LogFields()).WithError(err).Debug("Failed to decode encless")
+                       logger.WithFields(
+                               p.LogFields(),
+                       ).WithError(err).Debug("Failed to decode encless")
                        p.FramesUnauth++
                        p.BusyR.Unlock()
                        return false
@@ -382,10 +396,10 @@ func (p *Peer) PktProcess(data []byte, tap io.Writer, reorderable bool) bool {
                for i := 0; i < SSize; i++ {
                        p.bufR[i] = 0
                }
-               copy(p.bufR[chacha20InternalBlockSize:], data[tagSize:])
+               copy(p.bufR[CC20IBS:], data[tagSize:])
                chacha20.XORKeyStream(
-                       p.bufR[:chacha20InternalBlockSize+lenData-tagSize-NonceSize],
-                       p.bufR[:chacha20InternalBlockSize+lenData-tagSize-NonceSize],
+                       p.bufR[:CC20IBS+len(data)-tagSize-NonceSize],
+                       p.bufR[:CC20IBS+len(data)-tagSize-NonceSize],
                        p.nonceR,
                        p.key,
                )
@@ -396,11 +410,11 @@ func (p *Peer) PktProcess(data []byte, tap io.Writer, reorderable bool) bool {
                        p.BusyR.Unlock()
                        return false
                }
-               out = p.bufR[chacha20InternalBlockSize : chacha20InternalBlockSize+lenData-tagSize-NonceSize]
+               out = p.bufR[CC20IBS : CC20IBS+len(data)-tagSize-NonceSize]
        }
 
        if reorderable {
-               copy(p.nonceRecv[:], data[lenData-NonceSize:])
+               copy(p.nonceRecv[:], data[len(data)-NonceSize:])
                _, foundL := p.nonceBucketL[p.nonceRecv]
                _, foundM := p.nonceBucketM[p.nonceRecv]
                _, foundH := p.nonceBucketH[p.nonceRecv]
@@ -432,7 +446,7 @@ func (p *Peer) PktProcess(data []byte, tap io.Writer, reorderable bool) bool {
                        }
                }
        } else {
-               if subtle.ConstantTimeCompare(data[lenData-NonceSize:], p.NonceExpect) != 1 {
+               if subtle.ConstantTimeCompare(data[len(data)-NonceSize:], p.NonceExpect) != 1 {
                        p.FramesDup++
                        p.BusyR.Unlock()
                        return false
@@ -441,7 +455,7 @@ func (p *Peer) PktProcess(data []byte, tap io.Writer, reorderable bool) bool {
        }
 
        p.FramesIn++
-       atomic.AddUint64(&p.BytesIn, uint64(lenData))
+       atomic.AddUint64(&p.BytesIn, uint64(len(data)))
        p.LastPing = time.Now()
        p.pktSizeR = bytes.LastIndexByte(out, padByte)
        if p.pktSizeR == -1 {
@@ -488,13 +502,23 @@ func PeerTapProcessor(peer *Peer, tap *TAP, terminator chan struct{}) {
                                now = time.Now()
                                if lastSent.Add(peer.Timeout).Before(now) {
                                        if err = peer.EthProcess(nil); err != nil {
-                                               logger.WithFields(fields).WithFields(peer.LogFields()).WithError(err).Warn("Can't process nil ethernet packet")
+                                               logger.WithFields(
+                                                       fields,
+                                               ).WithFields(
+                                                       peer.LogFields(),
+                                               ).WithError(err).Warn(
+                                                       "Can't process nil ethernet packet",
+                                               )
                                        }
                                        lastSent = now
                                }
                        case data = <-tap.Sink:
                                if err = peer.EthProcess(data); err != nil {
-                                       logger.WithFields(fields).WithFields(peer.LogFields()).WithError(err).Warn("Can't process ethernet packet")
+                                       logger.WithFields(
+                                               fields,
+                                       ).WithFields(
+                                               peer.LogFields(),
+                                       ).WithError(err).Warn("Can't process ethernet packet")
                                }
                                lastSent = time.Now()
                        }
@@ -508,13 +532,21 @@ func PeerTapProcessor(peer *Peer, tap *TAP, terminator chan struct{}) {
                                break CPRProcessor
                        case data = <-tap.Sink:
                                if err = peer.EthProcess(data); err != nil {
-                                       logger.WithFields(fields).WithFields(peer.LogFields()).WithError(err).Warn("Can't process ethernet packet")
+                                       logger.WithFields(
+                                               fields,
+                                       ).WithFields(
+                                               peer.LogFields(),
+                                       ).WithError(err).Warn("Can't process ethernet packet")
                                }
                        default:
                        }
                        if data == nil {
                                if err = peer.EthProcess(nil); err != nil {
-                                       logger.WithFields(fields).WithFields(peer.LogFields()).WithError(err).Warn("Can't process nil ethernet packet")
+                                       logger.WithFields(
+                                               fields,
+                                       ).WithFields(
+                                               peer.LogFields(),
+                                       ).WithError(err).Warn("Can't process nil ethernet packet")
                                }
                        }
                        time.Sleep(peer.CPRCycle)