]> Cypherpunks.ru repositories - govpn.git/blobdiff - src/govpn/peer.go
Move cprCycleCalculate to peer.go, as it is the only user of it
[govpn.git] / src / govpn / peer.go
index f3bb0b43befea0c5081fc9dfb994be8415b8a532..f4e04b0126aaa464cb3dd015940373b21220ed7e 100644 (file)
@@ -22,6 +22,7 @@ import (
        "bytes"
        "encoding/binary"
        "io"
+       "log"
        "sync"
        "sync/atomic"
        "time"
@@ -72,6 +73,7 @@ type Peer struct {
        CPR         int
        CPRCycle    time.Duration `json:"-"`
        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,7 +165,7 @@ func newPeer(isClient bool, addr string, conn io.Writer, conf *PeerConf, key *[S
                timeout = timeout / TimeoutHeartbeat
        }
 
-       bufSize := S20BS + MTU + NonceSize
+       bufSize := S20BS + 2*conf.MTU
        if conf.EncLess {
                bufSize += EncLessEnlargeSize
                noiseEnable = true
@@ -170,6 +179,7 @@ func newPeer(isClient bool, addr string, conn io.Writer, conf *PeerConf, key *[S
                CPR:         conf.CPR,
                CPRCycle:    cprCycle,
                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()
 
@@ -225,9 +239,9 @@ func (p *Peer) EthProcess(data []byte) {
        }
 
        if p.NoiseEnable && !p.EncLess {
-               p.frameT = p.bufT[S20BS : S20BS+MTU-TagSize]
+               p.frameT = p.bufT[S20BS : S20BS+p.MTU-TagSize]
        } else if p.EncLess {
-               p.frameT = p.bufT[S20BS : S20BS+MTU]
+               p.frameT = p.bufT[S20BS : S20BS+p.MTU]
        } else {
                p.frameT = p.bufT[S20BS : S20BS+len(data)+1+NonceSize]
        }