func (srv *Server) Shutdown() error {
	srv.lock.Lock()
	if !srv.started {
		srv.lock.Unlock()
		return &Error{err: "server not started"}
	}
	srv.started = false
	srv.lock.Unlock()
	net, addr := srv.Net, srv.Addr
	switch {
	case srv.Listener != nil:
		a := srv.Listener.Addr()
		net, addr = a.Network(), a.String()
	case srv.PacketConn != nil:
		a := srv.PacketConn.LocalAddr()
		net, addr = a.Network(), a.String()
	}
	fin := make(chan bool)
	switch net {
	case "tcp", "tcp4", "tcp6":
		go func() {
			srv.stopTCP <- true
			srv.wgTCP.Wait()
			fin <- true
		}()
	case "udp", "udp4", "udp6":
		go func() {
			srv.stopUDP <- true
			srv.wgUDP.Wait()
			fin <- true
		}()
	}
	c := &Client{Net: net}
	go c.Exchange(new(Msg), addr)  
	select {
	case <-time.After(srv.getReadTimeout()):
		return &Error{err: "server shutdown is pending"}
	case <-fin:
		return nil
	}
}
Shutdown gracefully shuts down a server. After a call to Shutdown, ListenAndServe and
ActivateAndServe will return. All in progress queries are completed before the server
is taken down. If the Shutdown is taking longer than the reading timeout and error
is returned.
package dns
import (
	"bytes"
	"io"
	"net"
	"sync"
	"time"
)
type Handler interface {
	ServeDNS(w ResponseWriter, r *Msg)
}
type ResponseWriter interface {
	LocalAddr() net.Addr
	RemoteAddr() net.Addr
	WriteMsg(*Msg) error
	Write([]byte) (int, error)
	Close() error
	TsigStatus() error
	TsigTimersOnly(bool)
	Hijack()
}
type response struct {
	hijacked       bool  
	tsigStatus     error
	tsigTimersOnly bool
	tsigRequestMAC string
	tsigSecret     map[string]string  
	udp            *net.UDPConn       
	tcp            *net.TCPConn       
	udpSession     *sessionUDP        
	remoteAddr     net.Addr           
}
type ServeMux struct {
	z map[string]Handler
	m *sync.RWMutex
}
func NewServeMux() *ServeMux { return &ServeMux{z: make(map[string]Handler), m: new(sync.RWMutex)} }
var DefaultServeMux = NewServeMux()
type HandlerFunc func(ResponseWriter, *Msg)
func (f HandlerFunc) ServeDNS(w ResponseWriter, r *Msg) {
	f(w, r)
}
func HandleFailed(w ResponseWriter, r *Msg) {
	m := new(Msg)
	m.SetRcode(r, RcodeServerFailure)
	w.WriteMsg(m)
}
func failedHandler() Handler { return HandlerFunc(HandleFailed) }
func ListenAndServe(addr string, network string, handler Handler) error {
	server := &Server{Addr: addr, Net: network, Handler: handler}
	return server.ListenAndServe()
}
func ActivateAndServe(l net.Listener, p net.PacketConn, handler Handler) error {
	server := &Server{Listener: l, PacketConn: p, Handler: handler}
	return server.ActivateAndServe()
}
func (mux *ServeMux) match(q string, t uint16) Handler {
	mux.m.RLock()
	defer mux.m.RUnlock()
	var handler Handler
	b := make([]byte, len(q))  
	off := 0
	end := false
	for {
		l := len(q[off:])
		for i := 0; i < l; i++ {
			b[i] = q[off+i]
			if b[i] >= 'A' && b[i] <= 'Z' {
				b[i] |= ('a' - 'A')
			}
		}
		if h, ok := mux.z[string(b[:l])]; ok {  
			if t != TypeDS {
				return h
			} else {
				handler = h
			}
		}
		off, end = NextLabel(q, off)
		if end {
			break
		}
	}
	if h, ok := mux.z["."]; ok {
		return h
	}
	return handler
}
func (mux *ServeMux) Handle(pattern string, handler Handler) {
	if pattern == "" {
		panic("dns: invalid pattern " + pattern)
	}
	mux.m.Lock()
	mux.z[Fqdn(pattern)] = handler
	mux.m.Unlock()
}
func (mux *ServeMux) HandleFunc(pattern string, handler func(ResponseWriter, *Msg)) {
	mux.Handle(pattern, HandlerFunc(handler))
}
func (mux *ServeMux) HandleRemove(pattern string) {
	if pattern == "" {
		panic("dns: invalid pattern " + pattern)
	}
	delete(mux.z, Fqdn(pattern))
}
func (mux *ServeMux) ServeDNS(w ResponseWriter, request *Msg) {
	var h Handler
	if len(request.Question) < 1 {  
		h = failedHandler()
	} else {
		if h = mux.match(request.Question[0].Name, request.Question[0].Qtype); h == nil {
			h = failedHandler()
		}
	}
	h.ServeDNS(w, request)
}
func Handle(pattern string, handler Handler) { DefaultServeMux.Handle(pattern, handler) }
func HandleRemove(pattern string) { DefaultServeMux.HandleRemove(pattern) }
func HandleFunc(pattern string, handler func(ResponseWriter, *Msg)) {
	DefaultServeMux.HandleFunc(pattern, handler)
}
type Server struct {
	Addr string
	Net string
	Listener net.Listener
	PacketConn net.PacketConn
	Handler Handler
	UDPSize int
	ReadTimeout time.Duration
	WriteTimeout time.Duration
	IdleTimeout func() time.Duration
	TsigSecret map[string]string
	Unsafe bool
	NotifyStartedFunc func()
	stopUDP chan bool
	stopTCP chan bool
	wgUDP   sync.WaitGroup
	wgTCP   sync.WaitGroup
	lock    sync.Mutex
	started bool
}
func (srv *Server) ListenAndServe() error {
	srv.lock.Lock()
	if srv.started {
		srv.lock.Unlock()
		return &Error{err: "server already started"}
	}
	srv.stopUDP, srv.stopTCP = make(chan bool), make(chan bool)
	srv.started = true
	srv.lock.Unlock()
	addr := srv.Addr
	if addr == "" {
		addr = ":domain"
	}
	if srv.UDPSize == 0 {
		srv.UDPSize = MinMsgSize
	}
	switch srv.Net {
	case "tcp", "tcp4", "tcp6":
		a, e := net.ResolveTCPAddr(srv.Net, addr)
		if e != nil {
			return e
		}
		l, e := net.ListenTCP(srv.Net, a)
		if e != nil {
			return e
		}
		return srv.serveTCP(l)
	case "udp", "udp4", "udp6":
		a, e := net.ResolveUDPAddr(srv.Net, addr)
		if e != nil {
			return e
		}
		l, e := net.ListenUDP(srv.Net, a)
		if e != nil {
			return e
		}
		if e := setUDPSocketOptions(l); e != nil {
			return e
		}
		return srv.serveUDP(l)
	}
	return &Error{err: "bad network"}
}
func (srv *Server) ActivateAndServe() error {
	srv.lock.Lock()
	if srv.started {
		srv.lock.Unlock()
		return &Error{err: "server already started"}
	}
	srv.stopUDP, srv.stopTCP = make(chan bool), make(chan bool)
	srv.started = true
	srv.lock.Unlock()
	if srv.PacketConn != nil {
		if srv.UDPSize == 0 {
			srv.UDPSize = MinMsgSize
		}
		if t, ok := srv.PacketConn.(*net.UDPConn); ok {
			if e := setUDPSocketOptions(t); e != nil {
				return e
			}
			return srv.serveUDP(t)
		}
	}
	if srv.Listener != nil {
		if t, ok := srv.Listener.(*net.TCPListener); ok {
			return srv.serveTCP(t)
		}
	}
	return &Error{err: "bad listeners"}
}