]> Cypherpunks.ru repositories - govpn.git/blobdiff - transport.go
Ability to generate Constant Packet Rate traffic
[govpn.git] / transport.go
index a595130a6b1d2ffec7a3385033e6439bcc38d696..7379d680f64fd94e6b6f62d4c9c9930eabb77dce 100644 (file)
@@ -39,6 +39,8 @@ const (
        MaxBytesPerKey int64 = 1 << 32
        // Size of packet's size mark in bytes
        PktSizeSize = 2
+       // Heartbeat rate, relative to Timeout
+       TimeoutHeartbeat = 4
 )
 
 type UDPPkt struct {
@@ -47,29 +49,32 @@ type UDPPkt struct {
 }
 
 type Peer struct {
-       Addr          *net.UDPAddr
-       Id            PeerId
-       Key           *[KeySize]byte `json:"-"`
-       NonceOur      uint64         `json:"-"`
-       NonceRecv     uint64         `json:"-"`
-       NonceCipher   *xtea.Cipher   `json:"-"`
-       LastPing      time.Time
-       LastSent      time.Time
-       buf           []byte
-       tag           *[poly1305.TagSize]byte
-       keyAuth       *[KeySize]byte
-       nonceRecv     uint64
-       frame         []byte
-       nonce         []byte
-       pktSize       uint64
-       BytesIn       int64
-       BytesOut      int64
-       FramesIn      int
-       FramesOut     int
-       FramesUnauth  int
-       FramesDup     int
-       HeartbeatRecv int
-       HeartbeatSent int
+       Addr            *net.UDPAddr
+       Id              PeerId
+       Key             *[KeySize]byte `json:"-"`
+       NonceOur        uint64         `json:"-"`
+       NonceRecv       uint64         `json:"-"`
+       NonceCipher     *xtea.Cipher   `json:"-"`
+       LastPing        time.Time
+       LastSent        time.Time
+       willSentCycle   time.Time
+       buf             []byte
+       tag             *[poly1305.TagSize]byte
+       keyAuth         *[KeySize]byte
+       nonceRecv       uint64
+       frame           []byte
+       nonce           []byte
+       pktSize         uint64
+       BytesIn         int64
+       BytesOut        int64
+       BytesPayloadIn  int64
+       BytesPayloadOut int64
+       FramesIn        int
+       FramesOut       int
+       FramesUnauth    int
+       FramesDup       int
+       HeartbeatRecv   int
+       HeartbeatSent   int
 }
 
 func (p *Peer) String() string {
@@ -87,17 +92,16 @@ func (p *Peer) Zero() {
 }
 
 var (
-       Emptiness       = make([]byte, 1<<16)
+       Emptiness       = make([]byte, 1<<14)
        taps            = make(map[string]*TAP)
-       heartbeatPeriod *time.Duration
+       heartbeatPeriod time.Duration
 )
 
 func heartbeatPeriodGet() time.Duration {
-       if heartbeatPeriod == nil {
-               period := time.Second * time.Duration(Timeout/4)
-               heartbeatPeriod = &period
+       if heartbeatPeriod == time.Duration(0) {
+               heartbeatPeriod = Timeout / TimeoutHeartbeat
        }
-       return *heartbeatPeriod
+       return heartbeatPeriod
 }
 
 // Create TAP listening goroutine.
@@ -253,6 +257,8 @@ func (p *Peer) UDPProcess(udpPkt []byte, tap io.Writer, ready chan struct{}) boo
                return false
        }
        ready <- struct{}{}
+       p.FramesIn++
+       p.BytesIn += int64(size)
        p.LastPing = time.Now()
        p.NonceRecv = p.nonceRecv
        p.pktSize, _ = binary.Uvarint(p.buf[S20BS : S20BS+PktSizeSize])
@@ -261,8 +267,7 @@ func (p *Peer) UDPProcess(udpPkt []byte, tap io.Writer, ready chan struct{}) boo
                return true
        }
        p.frame = p.buf[S20BS+PktSizeSize : S20BS+PktSizeSize+p.pktSize]
-       p.BytesIn += int64(p.pktSize)
-       p.FramesIn++
+       p.BytesPayloadIn += int64(p.pktSize)
        tap.Write(p.frame)
        return true
 }
@@ -288,12 +293,12 @@ func (p *Peer) EthProcess(ethPkt []byte, conn WriteToer, ready chan struct{}) {
                copy(p.buf[S20BS+PktSizeSize:], ethPkt)
                ready <- struct{}{}
                binary.PutUvarint(p.buf[S20BS:S20BS+PktSizeSize], uint64(size))
-               p.BytesOut += int64(size)
+               p.BytesPayloadOut += int64(size)
        } else {
                p.HeartbeatSent++
        }
 
-       p.NonceOur = p.NonceOur + 2
+       p.NonceOur += 2
        copy(p.nonce, Emptiness)
        binary.PutUvarint(p.nonce, p.NonceOur)
        p.NonceCipher.Encrypt(p.nonce, p.nonce)
@@ -308,7 +313,16 @@ func (p *Peer) EthProcess(ethPkt []byte, conn WriteToer, ready chan struct{}) {
        }
        poly1305.Sum(p.tag, p.frame, p.keyAuth)
 
+       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)