]> Cypherpunks.ru repositories - govpn.git/blobdiff - src/govpn/peer.go
EncLess -> Encless for convenience
[govpn.git] / src / govpn / peer.go
index f3bb0b43befea0c5081fc9dfb994be8415b8a532..2907662c4e168b7f96861e97f200cb5958c1df58 100644 (file)
@@ -22,6 +22,7 @@ import (
        "bytes"
        "encoding/binary"
        "io"
+       "log"
        "sync"
        "sync/atomic"
        "time"
@@ -71,7 +72,8 @@ type Peer struct {
        NoiseEnable bool
        CPR         int
        CPRCycle    time.Duration `json:"-"`
-       EncLess     bool
+       Encless     bool
+       MTU         int
 
        // Cryptography related
        Key          *[SSize]byte `json:"-"`
@@ -143,6 +145,13 @@ func (p *Peer) NonceExpectation(buf []byte) {
        p.NonceCipher.Encrypt(buf, buf)
 }
 
+func cprCycleCalculate(rate int) time.Duration {
+       if rate == 0 {
+               return time.Duration(0)
+       }
+       return time.Second / time.Duration(rate*(1<<10)/MTUMax)
+}
+
 func newPeer(isClient bool, addr string, conn io.Writer, conf *PeerConf, key *[SSize]byte) *Peer {
        now := time.Now()
        timeout := conf.Timeout
@@ -156,9 +165,9 @@ func newPeer(isClient bool, addr string, conn io.Writer, conf *PeerConf, key *[S
                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{
@@ -169,7 +178,8 @@ func newPeer(isClient bool, addr string, conn io.Writer, conf *PeerConf, key *[S
                NoiseEnable: noiseEnable,
                CPR:         conf.CPR,
                CPRCycle:    cprCycle,
-               EncLess:     conf.EncLess,
+               Encless:     conf.Encless,
+               MTU:         conf.MTU,
 
                Key:          key,
                NonceCipher:  newNonceCipher(key),
@@ -203,6 +213,10 @@ func newPeer(isClient bool, addr string, conn io.Writer, conf *PeerConf, key *[S
 // 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()
 
@@ -224,10 +238,10 @@ func (p *Peer) EthProcess(data []byte) {
                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]
        }
@@ -238,9 +252,9 @@ func (p *Peer) EthProcess(data []byte) {
                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],
@@ -282,9 +296,9 @@ func (p *Peer) PktProcess(data []byte, tap io.Writer, reorderable bool) bool {
        }
        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],