MaxBytesPerKey int64 = 1 << 32
// Size of packet's size mark in bytes
PktSizeSize = 2
+ // Heartbeat rate, relative to Timeout
+ TimeoutHeartbeat = 4
)
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 {
}
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.
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])
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
}
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)
salsa20.XORKeyStream(p.buf, p.buf, p.nonce, p.Key)
copy(p.buf[S20BS-NonceSize:S20BS], p.nonce)
copy(p.keyAuth[:], p.buf[:KeySize])
- p.frame = p.buf[S20BS-NonceSize : S20BS+PktSizeSize+size]
+ if NoiseEnable {
+ p.frame = p.buf[S20BS-NonceSize : S20BS+MTU-NonceSize-poly1305.TagSize]
+ } else {
+ p.frame = p.buf[S20BS-NonceSize : S20BS+PktSizeSize+size]
+ }
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)