2011-04-12 19:44:56 +00:00
|
|
|
package dns
|
|
|
|
|
|
|
|
// A concurrent client implementation.
|
|
|
|
// Client sends query to a channel which
|
|
|
|
// will then handle the query. Returned replys
|
|
|
|
// are return on another channel. Ready for handling --- same
|
|
|
|
// setup for server - a HANDLER function that gets run
|
|
|
|
// when the query returns.
|
|
|
|
|
|
|
|
// This completely mirrors server.go impl.
|
|
|
|
import (
|
2011-04-12 20:21:09 +00:00
|
|
|
"os"
|
2011-04-14 20:11:41 +00:00
|
|
|
// "net"
|
2011-04-12 19:44:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type QueryHandler interface {
|
2011-04-12 20:21:09 +00:00
|
|
|
QueryDNS(w RequestWriter, q *Msg)
|
2011-04-12 19:44:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A RequestWriter interface is used by an DNS query handler to
|
|
|
|
// construct an DNS request.
|
|
|
|
type RequestWriter interface {
|
2011-04-14 20:11:41 +00:00
|
|
|
Write(*Msg)
|
2011-04-12 20:21:09 +00:00
|
|
|
}
|
|
|
|
|
2011-04-14 20:11:41 +00:00
|
|
|
// hijacked connections...?
|
2011-04-12 20:39:38 +00:00
|
|
|
type reply struct {
|
2011-04-14 20:11:41 +00:00
|
|
|
Client *Client
|
|
|
|
req *Msg
|
2011-04-12 20:21:09 +00:00
|
|
|
}
|
2011-04-12 19:44:56 +00:00
|
|
|
|
|
|
|
// QueryMux is an DNS request multiplexer. It matches the
|
|
|
|
// zone name of each incoming request against a list of
|
|
|
|
// registered patterns add calls the handler for the pattern
|
|
|
|
// that most closely matches the zone name.
|
|
|
|
type QueryMux struct {
|
2011-04-12 20:21:09 +00:00
|
|
|
m map[string]QueryHandler
|
2011-04-12 19:44:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewQueryMux allocates and returns a new QueryMux.
|
|
|
|
func NewQueryMux() *QueryMux { return &QueryMux{make(map[string]QueryHandler)} }
|
|
|
|
|
|
|
|
// DefaultQueryMux is the default QueryMux used by Query.
|
|
|
|
var DefaultQueryMux = NewQueryMux()
|
|
|
|
|
2011-04-13 18:41:16 +00:00
|
|
|
func newQueryChan() chan *Msg { return make(chan *Msg) }
|
|
|
|
|
|
|
|
// Default channel to use for the resolver
|
2011-04-14 20:11:41 +00:00
|
|
|
var DefaultReplyChan = newQueryChan()
|
2011-04-13 18:41:16 +00:00
|
|
|
var DefaultQueryChan = newQueryChan()
|
|
|
|
|
2011-04-12 19:44:56 +00:00
|
|
|
// The HandlerQueryFunc type is an adapter to allow the use of
|
|
|
|
// ordinary functions as DNS query handlers. If f is a function
|
|
|
|
// with the appropriate signature, HandlerQueryFunc(f) is a
|
2011-04-14 20:11:41 +00:00
|
|
|
// QueryHandler object that calls f.
|
2011-04-12 19:44:56 +00:00
|
|
|
type HandlerQueryFunc func(RequestWriter, *Msg)
|
|
|
|
|
|
|
|
// QueryDNS calls f(w, reg)
|
|
|
|
func (f HandlerQueryFunc) QueryDNS(w RequestWriter, r *Msg) {
|
2011-04-14 20:11:41 +00:00
|
|
|
go f(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func HandleQueryFunc(pattern string, handler func(RequestWriter, *Msg)) {
|
|
|
|
DefaultQueryMux.HandleQueryFunc(pattern, handler)
|
2011-04-12 19:44:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Helper handlers
|
|
|
|
// Todo
|
|
|
|
|
|
|
|
// reusing zoneMatch from server.go
|
|
|
|
func (mux *QueryMux) match(zone string) QueryHandler {
|
2011-04-12 20:21:09 +00:00
|
|
|
var h QueryHandler
|
|
|
|
var n = 0
|
|
|
|
for k, v := range mux.m {
|
|
|
|
if !zoneMatch(k, zone) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if h == nil || len(k) > n {
|
|
|
|
n = len(k)
|
|
|
|
h = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return h
|
2011-04-12 19:44:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mux *QueryMux) Handle(pattern string, handler QueryHandler) {
|
2011-04-12 20:21:09 +00:00
|
|
|
if pattern == "" {
|
|
|
|
panic("dns: invalid pattern " + pattern)
|
|
|
|
}
|
|
|
|
if pattern[len(pattern)-1] != '.' { // no ending .
|
|
|
|
mux.m[pattern+"."] = handler
|
|
|
|
} else {
|
|
|
|
mux.m[pattern] = handler
|
|
|
|
}
|
2011-04-12 19:44:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mux *QueryMux) HandleQueryFunc(pattern string, handler func(RequestWriter, *Msg)) {
|
2011-04-12 20:21:09 +00:00
|
|
|
mux.Handle(pattern, HandlerQueryFunc(handler))
|
2011-04-12 19:44:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mux *QueryMux) QueryDNS(w RequestWriter, request *Msg) {
|
2011-04-12 20:21:09 +00:00
|
|
|
h := mux.match(request.Question[0].Name)
|
|
|
|
if h == nil {
|
|
|
|
// h = RefusedHandler()
|
|
|
|
// something else
|
|
|
|
}
|
|
|
|
h.QueryDNS(w, request)
|
2011-04-12 19:44:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Client struct {
|
2011-04-12 20:21:09 +00:00
|
|
|
Network string // if "tcp" a TCP query will be initiated, otherwise an UDP one
|
|
|
|
Attempts int // number of attempts
|
2011-04-12 20:39:38 +00:00
|
|
|
Retry bool // retry with TCP
|
2011-04-14 20:11:41 +00:00
|
|
|
ChannelQuery chan *Msg // read DNS request from this channel
|
|
|
|
ChannelReply chan *Msg // read DNS request from this channel
|
2011-04-12 20:21:09 +00:00
|
|
|
Handler QueryHandler // handler to invoke, dns.DefaultQueryMux if nil
|
|
|
|
ReadTimeout int64 // the net.Conn.SetReadTimeout value for new connections
|
|
|
|
WriteTimeout int64 // the net.Conn.SetWriteTimeout value for new connections
|
2011-04-12 19:44:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Query accepts incoming DNS request,
|
|
|
|
// Write to in
|
|
|
|
// creating a new service thread for each. The service threads
|
|
|
|
// read requests and then call handler to reply to them.
|
|
|
|
// Handler is typically nil, in which case the DefaultServeMux is used.
|
2011-04-14 20:11:41 +00:00
|
|
|
func Query(c chan *Msg, handler QueryHandler) os.Error {
|
|
|
|
client := &Client{ChannelQuery: c, Handler: handler}
|
|
|
|
return client.Query()
|
2011-04-12 19:44:56 +00:00
|
|
|
}
|
|
|
|
|
2011-04-14 20:11:41 +00:00
|
|
|
func (c *Client) Query() os.Error {
|
|
|
|
handler := c.Handler
|
2011-04-12 20:21:09 +00:00
|
|
|
if handler == nil {
|
|
|
|
handler = DefaultQueryMux
|
|
|
|
}
|
2011-04-14 20:11:41 +00:00
|
|
|
forever:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case in := <-c.ChannelQuery:
|
|
|
|
w := new(reply)
|
|
|
|
w.Client = c
|
|
|
|
w.req = in
|
|
|
|
handler.QueryDNS(w, w.req)
|
|
|
|
}
|
|
|
|
}
|
2011-04-12 20:21:09 +00:00
|
|
|
return nil
|
2011-04-12 19:44:56 +00:00
|
|
|
}
|
|
|
|
|
2011-04-14 20:11:41 +00:00
|
|
|
func (c *Client) ListenAndQuery() os.Error {
|
|
|
|
if c.ChannelQuery == nil {
|
|
|
|
c.ChannelQuery = DefaultQueryChan
|
|
|
|
}
|
|
|
|
if c.ChannelReply == nil {
|
|
|
|
c.ChannelReply = DefaultReplyChan
|
|
|
|
}
|
|
|
|
return c.Query()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Do(m *Msg, addr string) {
|
|
|
|
// addr !!!
|
|
|
|
if c.ChannelQuery == nil {
|
|
|
|
DefaultQueryChan <- m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ListenAndQuery(c chan *Msg, handler QueryHandler) {
|
|
|
|
client := &Client{ChannelQuery: c, Handler: handler}
|
|
|
|
go client.ListenAndQuery()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *reply) Write(m *Msg) {
|
|
|
|
// Write to the channel
|
|
|
|
w.Client.ChannelReply <- m
|
2011-04-12 19:44:56 +00:00
|
|
|
}
|