]> Cypherpunks.ru repositories - govpn.git/blobdiff - transport.go
Ability to generate Constant Packet Rate traffic
[govpn.git] / transport.go
index 904d3f1f56f9936c3f1f4d90e38a08fac515a226..7379d680f64fd94e6b6f62d4c9c9930eabb77dce 100644 (file)
@@ -57,6 +57,7 @@ type Peer struct {
        NonceCipher     *xtea.Cipher   `json:"-"`
        LastPing        time.Time
        LastSent        time.Time
+       willSentCycle   time.Time
        buf             []byte
        tag             *[poly1305.TagSize]byte
        keyAuth         *[KeySize]byte
@@ -314,6 +315,14 @@ func (p *Peer) EthProcess(ethPkt []byte, conn WriteToer, ready chan struct{}) {
 
        p.BytesOut += int64(len(p.frame) + poly1305.TagSize)
        p.FramesOut++
+
+       if cprEnable {
+               p.willSentCycle = p.LastSent.Add(cprCycle)
+               if p.willSentCycle.After(now) {
+                       time.Sleep(p.willSentCycle.Sub(now))
+                       now = p.willSentCycle
+               }
+       }
        p.LastSent = now
        if _, err := conn.WriteTo(append(p.frame, p.tag[:]...), p.Addr); err != nil {
                log.Println("Error sending UDP", err)