just use println

This commit is contained in:
Miek Gieben 2011-08-01 13:59:02 +02:00
parent 0565d0bddf
commit a460c45d2e
3 changed files with 27 additions and 40 deletions

View File

@ -33,29 +33,27 @@ func delay(m *dns.Msg) (buf []byte) {
ok1 bool ok1 bool
o *dns.Msg o *dns.Msg
) )
if previous, ok1 = checkDelay(); !ok1 { if previous, ok1 = checkDelay(); !ok1 {
println("Info: Dropping: too often") println("Dropping: too often")
time.Sleep(NSECDELAY) time.Sleep(NSECDELAY)
return return
} }
println("Info: Ok: let it through") println("Ok: let it through")
for _, c := range qr { for _, c := range qr {
o = c.Client.Exchange(m, c.Addr) o = c.Client.Exchange(m, c.Addr)
} }
buf, _ = o.Pack() buf, _ = o.Pack()
return return
} }
// Return the configration // Return the configration
func NewFunkenSturm() *FunkenSturm { func NewFunkenSturm() *FunkenSturm {
f := new(FunkenSturm) f := new(FunkenSturm)
f.Funk = make([]*Funk, 1)
// Not concurrent save
f.Setup = func() bool { previous = time.Nanoseconds(); return true } f.Setup = func() bool { previous = time.Nanoseconds(); return true }
f.Funk[0] = NewFunk(1) f.Funk = make([]*Funk, 1)
f.Funk[0].Matches[0].Op = AND f.Funk[0] = NewFunk()
f.Funk[0].Matches[0].Func = match f.Funk[0].Match = match
f.Funk[0].Action = delay f.Funk[0].Action = delay
return f return f
} }

View File

@ -91,8 +91,8 @@ func NewFunkenSturm() *FunkenSturm {
f := new(FunkenSturm) f := new(FunkenSturm)
f.Funk = make([]*Funk, 1) f.Funk = make([]*Funk, 1)
f.Setup = func() bool { cache = NewCache(); return true } f.Setup = func() bool { cache = NewCache(); return true }
f.Funk[0] = NewFunk(1) f.Funk[0] = NewFunk()
f.Funk[0].Matches[0].Func = func(m *dns.Msg) (*dns.Msg, bool) { return m, true } f.Funk[0].Match = func(m *dns.Msg) (*dns.Msg, bool) { return m, true }
f.Funk[0].Action = checkcache f.Funk[0].Action = checkcache
return f return f
} }

View File

@ -10,7 +10,6 @@ import (
"os" "os"
"log" "log"
"flag" "flag"
"fmt"
"dns" "dns"
"os/signal" "os/signal"
"strings" "strings"
@ -32,8 +31,8 @@ type FunkClient struct {
// the matches are successfull (return true) the action is // the matches are successfull (return true) the action is
// performed // performed
type Funk struct { type Funk struct {
Match func(*dns.Msg) (*dns.Msg, bool) Match func(*dns.Msg) (*dns.Msg, bool)
Action func(*dns.Msg) []byte Action func(*dns.Msg) []byte
} }
func NewFunk() *Funk { func NewFunk() *Funk {
@ -41,20 +40,11 @@ func NewFunk() *Funk {
return f return f
} }
// A complete config for Funkensturm. All matches in the Matches slice are // Hold the information.
// chained together: incoming dns.Msg -> Match[0] -> dns.Msg -> Match[1] -> dns.Msg -> ...
// The dns.Msg output of Match[n] is the input for Match[n+1].
//
// The final outcome (does a packet match or not?) is calculated as follows:
// true Match[0].Op Match[0].Func() Match[1].Op Match[1].Func() ...
// The result of this matching is given to the action function. That last
// function decides "what to do with the packet" is the match(es) return 'true'
// There is no NewFunkenSturm() because that is what needs to be done in the
// configuration file.
type FunkenSturm struct { type FunkenSturm struct {
Setup func() bool // Inital setup (for extra resolvers, or loading keys, or ...) Setup func() bool // Inital setup (for extra resolvers, or loading keys, or ...)
Default func(*dns.Msg) []byte // Default action is all fails Default func(*dns.Msg) []byte // Default action is all fails
Funk []*Funk // The configuration Funk []*Funk // The configuration
} }
func doFunkenSturm(pkt *dns.Msg) (ret []byte) { func doFunkenSturm(pkt *dns.Msg) (ret []byte) {
@ -66,12 +56,12 @@ func doFunkenSturm(pkt *dns.Msg) (ret []byte) {
// Loop through the Funks and decide what to do with // Loop through the Funks and decide what to do with
// the packet. // the packet.
for _, f := range f.Funk { for _, f := range f.Funk {
if m, ok := f.Match(pkt); ok { if m, ok := f.Match(pkt); ok {
ret = f.Action(m) ret = f.Action(m)
return return
} }
} }
ret = f.Default(pkt) ret = f.Default(pkt)
return return
} }
@ -83,7 +73,7 @@ func serve(w dns.ResponseWriter, req *dns.Msg) {
func listenAndServe(add, net string) { func listenAndServe(add, net string) {
if err := dns.ListenAndServe(add, net, nil); err != nil { if err := dns.ListenAndServe(add, net, nil); err != nil {
fmt.Printf("Failed to setup: " + net + " " + add + "\n") println("Failed to setup: ", net, " ", add)
} }
} }
@ -93,7 +83,6 @@ func main() {
cpuprofile := flag.String("cpuprofile", "", "write cpu profile to file") cpuprofile := flag.String("cpuprofile", "", "write cpu profile to file")
verbose = flag.Bool("verbose", false, "Print packet as it flows through") verbose = flag.Bool("verbose", false, "Print packet as it flows through")
flag.Usage = func() { flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s\n", os.Args[0])
flag.PrintDefaults() flag.PrintDefaults()
} }
flag.Parse() flag.Parse()
@ -117,7 +106,7 @@ func main() {
f = NewFunkenSturm() f = NewFunkenSturm()
ok := f.Setup() ok := f.Setup()
if !ok { if !ok {
fmt.Fprintf(os.Stderr, "Setup failed") println("Setup failed")
return return
} }
@ -129,7 +118,7 @@ forever:
for { for {
select { select {
case <-signal.Incoming: case <-signal.Incoming:
fmt.Printf("Signal received, stopping\n") println("Signal received, stopping")
break forever break forever
} }
} }