/*
GoVPN -- simple secure free software virtual private network daemon
-Copyright (C) 2014-2016 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2014-2017 Sergey Matveev <stargrave@stargrave.org>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
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
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,
}
}
-// 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(),
timeout = timeout / timeoutHeartbeat
}
- bufSize := chacha20InternalBlockSize + 2*conf.MTU
+ bufSize := CC20IBS + 2*conf.MTU
if conf.Encless {
bufSize += EnclessEnlargeSize
noiseEnable = true
const paddingSize = 1
lenData := len(data)
if lenData > p.MTU-paddingSize {
- logger.WithFields(p.LogFields()).WithFields(p.ConfigurationLogFields()).WithFields(
+ logger.WithFields(p.LogFields()).WithFields(
+ p.ConfigurationLogFields(),
+ ).WithFields(
logrus.Fields{
"func": logFuncPrefix + "Peer.EthProcess",
"padding": paddingSize,
// Zero size is a heartbeat packet
SliceZero(p.bufT)
if lenData == 0 {
- p.bufT[chacha20InternalBlockSize+0] = padByte
+ 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
+ copy(p.bufT[CC20IBS:], data)
+ p.bufT[CC20IBS+lenData] = padByte
p.BytesPayloadOut += uint64(lenData)
}
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+lenData+1+NonceSize]
}
copy(p.frameT[len(p.frameT)-NonceSize:], (<-p.noncesT)[:])
var out []byte
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,
)
"data": lenData,
}
if lenData < MinPktLength {
- logger.WithFields(p.LogFields()).WithFields(fields).WithField("minimum_packet_Length", MinPktLength).Debug("Ignore packet smaller than allowed minimum")
+ 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 && lenData > len(p.bufR)-CC20IBS {
return false
}
var out []byte
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+lenData-tagSize-NonceSize],
+ p.bufR[:CC20IBS+lenData-tagSize-NonceSize],
p.nonceR,
p.key,
)
p.BusyR.Unlock()
return false
}
- out = p.bufR[chacha20InternalBlockSize : chacha20InternalBlockSize+lenData-tagSize-NonceSize]
+ out = p.bufR[CC20IBS : CC20IBS+lenData-tagSize-NonceSize]
}
if reorderable {
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()
}
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)