it compiles + more tweaks

This commit is contained in:
Miek Gieben 2011-04-03 11:49:23 +02:00
parent c7dbb1edc2
commit 064bfe4f2e
1 changed files with 247 additions and 194 deletions

View File

@ -7,7 +7,7 @@
package dns
import (
// "io"
"io"
"os"
"net"
)
@ -35,12 +35,14 @@ type ResponseWriter interface {
type conn struct {
remoteAddr net.Addr // address of remote side (sans port)
port int // port of the remote side
port int // port of the remote side, needed TODO(mg)
handler Handler // request handler
request []byte // bytes read
_UDP *net.UDPConn // i/o connection if UDP was used
_TCP *net.TCPConn // i/o connection if TCP was used
hijacked bool // connection has been hijacked by hander TODO(mg)
tsig map[string]string // tsig secrets
acl map[string]bool // ip acl list
}
type response struct {
@ -182,7 +184,7 @@ func (mux *ServeMux) HandleFunc(pattern string, handler func(ResponseWriter, *Ms
func (mux *ServeMux) ServeDNS(w ResponseWriter, request *Msg) {
h := mux.match(request.Question[0].Name)
if h == nil {
// h = NotFoundHandler()
// h = NotFoundHandler()
}
h.ServeDNS(w, request)
}
@ -260,7 +262,7 @@ func (srv *Server) ServeTCP(l *net.TCPListener) os.Error {
if handler == nil {
handler = DefaultServeMux
}
forever:
forever:
for {
rw, e := l.AcceptTCP()
if e != nil {
@ -364,17 +366,68 @@ func (c *conn) close() {
// Serve a new connection.
func (c *conn) serve() {
// c.ReadRequest
// c.Handler.ServeDNS(w, w.req) // this does the writing
// Request has been read in ServeUDP or ServeTCP
w := new(response)
w.conn = c
w.xfr = false
req := new(Msg)
if !req.Unpack(c.request) {
return
}
c.handler.ServeDNS(w, w.req) // this does the writing back to the client
if c.hijacked {
return
}
c.close()
}
func (c *conn) readRequest() (w *response, err os.Error) {
w = new(response)
return w, nil
func (w *response) Write(data []byte) (n int, err os.Error) {
switch {
case w.conn._UDP != nil:
n, err = w.conn._UDP.WriteTo(data, w.conn.remoteAddr)
if err != nil {
return 0, err
}
case w.conn._TCP != nil:
// TODO(mg) len(data) > 64K
l := make([]byte, 2)
l[0], l[1] = packUint16(uint16(len(data)))
n, err = w.conn._TCP.Write(data)
if err != nil {
return n, err
}
if n != 2 {
return n, io.ErrShortWrite
}
n, err = w.conn._TCP.Write(data)
if err != nil {
return n, err
}
i := n
if i < len(data) {
j, err := w.conn._TCP.Write(data[i:len(data)])
if err != nil {
return i, err
}
i += j
}
n = i
}
return n, nil
}
// Acl implements the ResponseWriter.Acl
func (w *response) Acl() map[string]bool {
return w.conn.acl
}
// Tsig implements the ResponseWriter.Tsig
func (w *response) Tsig() map[string]string {
return w.conn.tsig
}
// RemoteAddr implements the ResponseWriter.RemoteAddr method
func (w *response) RemoteAddr() string { return w.conn.remoteAddr.String() }