dns/ex/funkensturm/config_delay.go

60 lines
1.2 KiB
Go
Raw Normal View History

package main
// This proxy delays pkt that have the RD bit set.
2011-01-24 02:21:56 +11:00
import (
"dns"
2011-01-23 03:17:09 +11:00
"time"
)
2011-01-24 02:21:56 +11:00
const NSECDELAY = 1 * 1e9 // 1 second, meaning 1 qps (smaller means higher qps)
var previous int64 // previous tick
2011-01-23 03:17:09 +11:00
2011-01-24 02:21:56 +11:00
// returns false if we hit the limit set by NSECDELAY
func checkDelay() (ti int64, limitok bool) {
2011-01-23 03:17:09 +11:00
current := time.Nanoseconds()
tdiff := (current - previous)
if tdiff < NSECDELAY {
2011-01-23 03:17:09 +11:00
// too often
return previous, false
}
return current, true
}
2011-01-24 02:21:56 +11:00
// the only matching we do is on the RD bit
2011-08-01 21:15:15 +10:00
func match(m *dns.Msg) (*dns.Msg, bool) {
// only delay pkts with RD bit
return m, m.MsgHdr.RecursionDesired == true
}
2011-08-01 21:15:15 +10:00
func delay(m *dns.Msg) (buf []byte) {
var (
ok1 bool
o *dns.Msg
)
2011-08-01 21:59:02 +10:00
if previous, ok1 = checkDelay(); !ok1 {
println("Dropping: too often")
time.Sleep(NSECDELAY)
return
}
println("Ok: let it through")
for _, c := range qr {
2011-08-08 21:10:35 +10:00
o, _ = c.Client.Exchange(m, c.Addr)
2011-08-01 21:59:02 +10:00
}
buf, _ = o.Pack()
return
}
// Return the configration
2011-08-01 21:15:15 +10:00
func NewFunkenSturm() *FunkenSturm {
f := new(FunkenSturm)
2011-01-23 03:17:09 +11:00
f.Setup = func() bool { previous = time.Nanoseconds(); return true }
2011-08-01 21:59:02 +10:00
f.Funk = make([]*Funk, 1)
2011-08-01 22:13:13 +10:00
f.Funk[0] = new(Funk)
2011-08-01 21:59:02 +10:00
f.Funk[0].Match = match
2011-08-01 21:15:15 +10:00
f.Funk[0].Action = delay
return f
}