more happy with the interface

This commit is contained in:
Miek Gieben 2011-01-12 16:33:56 +01:00
parent a2ef425b84
commit 1ee22eb579
2 changed files with 43 additions and 51 deletions

View File

@ -21,7 +21,7 @@ func (r *RR_DNSKEY) GenerateRSA(bits int) (*rsa.PrivateKey, os.Error) {
case AlgRSASHA1: fallthrough case AlgRSASHA1: fallthrough
case AlgRSASHA256: case AlgRSASHA256:
if bits < 512 || bits > 4096 { if bits < 512 || bits > 4096 {
return nil, &Error{Error: "Size not in range [512..4096]"} //return nil, &Error{Error: "Size not in range [512..4096]"}
} }
case AlgRSASHA512: case AlgRSASHA512:
if bits < 1024 || bits > 4096 { if bits < 1024 || bits > 4096 {

View File

@ -12,11 +12,13 @@ import (
"os" "os"
"net" "net"
"dns" "dns"
"fmt" "fmt"
) )
// Some helper function for sending tcp/udp queries, like those
// in resolver.go, but then exported?
type Server struct { type Server struct {
Addresses []string // interfaces to use Address string // interface to use, for multiple interfaces, use multiple servers
Port string // what port to use Port string // what port to use
Timeout int // seconds before giving up on packet Timeout int // seconds before giving up on packet
Tcp bool // use TCP Tcp bool // use TCP
@ -25,31 +27,20 @@ type Server struct {
// Every nameserver must implement the Handler interface. // Every nameserver must implement the Handler interface.
type Responder interface { type Responder interface {
// Receives the raw message content // Receives the raw message content
ResponderUDP(c *net.UDPConn, raddr net.Addr, in []byte) ResponderUDP(c *net.UDPConn, raddr net.Addr, in []byte)
// Receives the raw message content // Receives the raw message content
ResponderTCP(c *net.TCPConn, raddr net.Addr, in []byte) ResponderTCP(c *net.TCPConn, raddr net.Addr, in []byte)
}
// When communicating with a resolver, we use this structure
// to send packets to it, for sending Error must be nil.
// A resolver responds with a reply packet and a possible error.
// Sending a nil message instructs to resolver to stop.
type DnsMsg struct {
Dns *dns.Msg
Error os.Error
} }
// This is a NAMESERVER // This is a NAMESERVER
// Communicate withit via a channel // Stop it by sending it true over the channel
// Interface UDPhandler - has function that gets called
// Interface TCPhandler - has function that gets called
// NewResponder returns a channel, for communication (start/stop) // NewResponder returns a channel, for communication (start/stop)
// caN we use the channel for other stuff?? // caN we use the channel for other stuff??
func (res *Server) NewResponder(h Responder) (ch chan DnsMsg) { func (res *Server) NewResponder(h Responder, ch chan bool) os.Error {
var port string var port string
if len(res.Addresses) == 0 { if len(res.Address) == 0 {
// We cannot start responding with an addresss // We cannot start responding without an addresss
return nil return nil
} }
if res.Port == "" { if res.Port == "" {
@ -57,49 +48,50 @@ func (res *Server) NewResponder(h Responder) (ch chan DnsMsg) {
} else { } else {
port = res.Port port = res.Port
} }
// TODO(mg) handle multiple addresses
switch res.Tcp { switch res.Tcp {
case true: case true:
/* Todo tcp conn. */
case false: case false:
udpaddr, _ := net.ResolveUDPAddr(res.Addresses[0] + ":" + port) udpaddr, _ := net.ResolveUDPAddr(res.Address + ":" + port)
c, _ := net.ListenUDP("udp", udpaddr) c, _ := net.ListenUDP("udp", udpaddr)
m := make([]byte, 4096) foreverudp:
n, raddr, err := c.ReadFrom(m) for {
if err != nil { select {
//continue case <-ch:
} c.Close()
m = m[:n] break foreverudp
// If I don't pick off the remote addr, but do it in the Go routine default:
// I've created a race condition?? TODO(mg) m := make([]byte, 4096) // Can we take this out of this loop TODO(mg)
h.ResponderUDP(c, raddr, m) n, raddr, err := c.ReadFrom(m)
c.Close() if err != nil {
//continue
}
m = m[:n]
go h.ResponderUDP(c, raddr, m)
}
}
} }
return nil return nil
} }
// The raw packet // The raw packet
func handlerUDP(c *net.UDPConn, raddr net.Addr, in []byte) { func handlerUDP(c *net.UDPConn, raddr net.Addr, i []byte) {
// don't care what you've read, just blap a default, but put in the in := new(dns.Msg)
// correct Id in.Unpack(i)
fmt.Printf("handlerUDP called!") fmt.Printf("%v\n", in)
inmsg := new(dns.Msg)
inmsg.Unpack(in)
fmt.Printf("%v\n", inmsg)
m := new(dns.Msg) m := new(dns.Msg)
m.MsgHdr.Id = inmsg.MsgHdr.Id m.MsgHdr.Id = in.MsgHdr.Id // Copy the Id over
m.MsgHdr.Authoritative = true m.MsgHdr.Authoritative = true
m.MsgHdr.Response = true m.MsgHdr.Response = true
m.MsgHdr.Rcode = dns.RcodeSuccess m.MsgHdr.Rcode = dns.RcodeSuccess
m.Question = make([]dns.Question, 1) m.Question = make([]dns.Question, 1)
m.Question[0] = dns.Question{"miek.nl.", dns.TypeTXT, dns.ClassINET} m.Question[0] = dns.Question{"miek.nl.", dns.TypeTXT, dns.ClassINET}
m.Answer = make([]dns.RR, 1) m.Answer = make([]dns.RR, 1)
a := new(dns.RR_TXT) a := new(dns.RR_TXT)
a.Hdr = dns.RR_Header{Name: "miek.nl.", Rrtype: dns.TypeTXT, Class: dns.ClassINET, Ttl: 3600} a.Hdr = dns.RR_Header{Name: "miek.nl.", Rrtype: dns.TypeTXT, Class: dns.ClassINET, Ttl: 3600}
a.Txt = "dit dan" a.Txt = "dit dan"
m.Answer[0] = a m.Answer[0] = a
out, _ := m.Pack() out, _ := m.Pack()
c.WriteTo(out, raddr) c.WriteTo(out, raddr)
} }