"bytes"
"encoding/binary"
"io"
+ "log"
"sync"
"sync/atomic"
"time"
NoiseEnable bool
CPR int
CPRCycle time.Duration `json:"-"`
- EncLess bool
+ Encless bool
+ MTU int
// Cryptography related
Key *[SSize]byte `json:"-"`
p.NonceCipher.Encrypt(buf, buf)
}
+func cprCycleCalculate(conf *PeerConf) time.Duration {
+ if conf.CPR == 0 {
+ return time.Duration(0)
+ }
+ rate := conf.CPR * 1 << 10
+ if conf.Encless {
+ rate /= EnclessEnlargeSize + conf.MTU
+ } else {
+ rate /= conf.MTU
+ }
+ return time.Second / time.Duration(rate)
+}
+
func newPeer(isClient bool, addr string, conn io.Writer, conf *PeerConf, key *[SSize]byte) *Peer {
now := time.Now()
timeout := conf.Timeout
- cprCycle := cprCycleCalculate(conf.CPR)
+ cprCycle := cprCycleCalculate(conf)
noiseEnable := conf.Noise
if conf.CPR > 0 {
noiseEnable = true
timeout = timeout / TimeoutHeartbeat
}
- bufSize := S20BS + MTU + NonceSize
- if conf.EncLess {
- bufSize += EncLessEnlargeSize
+ bufSize := S20BS + 2*conf.MTU
+ if conf.Encless {
+ bufSize += EnclessEnlargeSize
noiseEnable = true
}
peer := Peer{
NoiseEnable: noiseEnable,
CPR: conf.CPR,
CPRCycle: cprCycle,
- EncLess: conf.EncLess,
+ Encless: conf.Encless,
+ MTU: conf.MTU,
Key: key,
NonceCipher: newNonceCipher(key),
// that he is free to receive new packets. Encrypted and authenticated
// packets will be sent to remote Peer side immediately.
func (p *Peer) EthProcess(data []byte) {
+ if len(data) > p.MTU-1 { // 1 is for padding byte
+ log.Println("Padded data packet size", len(data)+1, "is bigger than MTU", p.MTU, p)
+ return
+ }
p.now = time.Now()
p.BusyT.Lock()
p.BytesPayloadOut += int64(len(data))
}
- if p.NoiseEnable && !p.EncLess {
- p.frameT = p.bufT[S20BS : S20BS+MTU-TagSize]
- } else if p.EncLess {
- p.frameT = p.bufT[S20BS : S20BS+MTU]
+ if p.NoiseEnable && !p.Encless {
+ p.frameT = p.bufT[S20BS : S20BS+p.MTU-TagSize]
+ } else if p.Encless {
+ p.frameT = p.bufT[S20BS : S20BS+p.MTU]
} else {
p.frameT = p.bufT[S20BS : S20BS+len(data)+1+NonceSize]
}
p.frameT[len(p.frameT)-NonceSize:],
)
var out []byte
- if p.EncLess {
+ if p.Encless {
var err error
- out, err = EncLessEncode(
+ out, err = EnclessEncode(
p.Key,
p.frameT[len(p.frameT)-NonceSize:],
p.frameT[:len(p.frameT)-NonceSize],
if len(data) < MinPktLength {
return false
}
+ if !p.Encless && len(data) > len(p.bufR)-S20BS {
+ return false
+ }
var out []byte
p.BusyR.Lock()
- if p.EncLess {
+ if p.Encless {
var err error
- out, err = EncLessDecode(
+ out, err = EnclessDecode(
p.Key,
data[len(data)-NonceSize:],
data[:len(data)-NonceSize],