works better

This commit is contained in:
Miek Gieben 2012-08-08 14:09:47 +02:00
parent 5ad0afa614
commit b30ea1d44f
3 changed files with 7 additions and 13 deletions

View File

@ -12,7 +12,7 @@ import (
// Cache elements, we using to key (toRadixKey) to distinguish between dns and dnssec
type Packet struct {
ttl time.Time // insertion time
d []byte // raw packet
d []byte // raw packet, except the first two bytes
}
func toRadixKey(d *dns.Msg) string {
@ -31,14 +31,6 @@ type Cache struct {
*radix.Radix
}
// Make an as-is copy, except for the first two bytes, as these hold
// the DNS id.
func quickCopy(p []byte) []byte {
q := make([]byte, 2)
q = append(q, p[2:]...)
return q
}
func NewCache() *Cache {
return &Cache{Radix: radix.New()}
}
@ -67,7 +59,7 @@ func (c *Cache) Find(d *dns.Msg) []byte {
}
return nil
}
return quickCopy(p.Value.(*Packet).d)
return p.Value.(*Packet).d
}
func (c *Cache) Insert(d *dns.Msg) {
@ -75,7 +67,7 @@ func (c *Cache) Insert(d *dns.Msg) {
log.Printf("fsk-shield: inserting " + toRadixKey(d))
}
buf, _ := d.Pack() // Should always work
c.Radix.Insert(toRadixKey(d), &Packet{d: buf, ttl: time.Now().UTC()})
c.Radix.Insert(toRadixKey(d), &Packet{d: buf[2:], ttl: time.Now().UTC()})
}
func (c *Cache) Remove(d *dns.Msg) {

View File

@ -36,7 +36,9 @@ func serve(w dns.ResponseWriter, r *dns.Msg, c *Cache) {
return
}
if p := c.Find(r); p != nil {
dns.RawSetId(p, r.MsgHdr.Id)
b := []byte{0, 0}
dns.RawSetId(b, r.MsgHdr.Id)
p = append(b, p...)
w.WriteBuf(p)
return
}

View File

@ -5,7 +5,7 @@ import (
"flag"
"log"
"os"
"os/Signal"
"os/signal"
"strings"
)