small updates
This commit is contained in:
parent
98f1f80088
commit
aa7d08bc8a
21
Makefile
21
Makefile
|
@ -7,18 +7,19 @@ include $(GOROOT)/src/Make.inc
|
|||
|
||||
TARG=dns
|
||||
GOFILES=\
|
||||
dns.go\
|
||||
msg.go\
|
||||
types.go\
|
||||
edns.go\
|
||||
tsig.go\
|
||||
dnssec.go\
|
||||
keygen.go\
|
||||
string.go\
|
||||
resolver.go\
|
||||
config.go\
|
||||
server.go \
|
||||
dns.go\
|
||||
dnssec.go\
|
||||
edns.go\
|
||||
keygen.go\
|
||||
msg.go\
|
||||
notify.go\
|
||||
nsec3.go \
|
||||
resolver.go\
|
||||
server.go \
|
||||
string.go\
|
||||
tsig.go\
|
||||
types.go\
|
||||
# y.go\
|
||||
|
||||
include $(GOROOT)/src/Make.pkg
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
package dns
|
||||
|
||||
// Create a notify request packet.
|
||||
func (dns *Msg) SetNotifyRequest(z string, class uint16) {
|
||||
dns.MsgHdr.Opcode = OpcodeNotify
|
||||
dns.MsgHdr.Authoritative = true
|
||||
dns.MsgHdr.Id = Id()
|
||||
dns.Question = make([]Question, 1)
|
||||
dns.Question[0] = Question{z, TypeSOA, class}
|
||||
}
|
||||
|
||||
// Create a notify reply packet.
|
||||
func (dns *Msg) SetNotifyReply(z string, class, id uint16) {
|
||||
dns.MsgHdr.Opcode = OpcodeNotify
|
||||
dns.MsgHdr.Authoritative = true
|
||||
dns.MsgHdr.Response = true
|
||||
dns.MsgHdr.Id = id
|
||||
dns.Question = make([]Question, 1)
|
||||
dns.Question[0] = Question{z, TypeSOA, class}
|
||||
}
|
|
@ -5,6 +5,7 @@
|
|||
// DNS resolver client: see RFC 1035.
|
||||
|
||||
package dns
|
||||
// TODO: refacter this
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
@ -31,6 +32,10 @@ type Resolver struct {
|
|||
Rrb int // Last used server (for round robin)
|
||||
}
|
||||
|
||||
func (res *Resolver) QueryTSIG(q *Msg, secret *string) (d *Msg, err os.Error) {
|
||||
return nil,nil
|
||||
}
|
||||
|
||||
// Basic usage pattern for setting up a resolver:
|
||||
//
|
||||
// res := new(Resolver)
|
||||
|
|
85
server.go
85
server.go
|
@ -11,58 +11,65 @@ import (
|
|||
"net"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
ServeUDP func(*net.UDPConn, net.Addr, *Msg) os.Error
|
||||
ServeTCP func(*net.TCPConn, net.Addr, *Msg) os.Error
|
||||
/* notify stuff here? */
|
||||
/* tsig here */
|
||||
}
|
||||
|
||||
func ServeUDP(l *net.UDPConn, f func(*net.UDPConn, net.Addr, *Msg)) os.Error {
|
||||
for {
|
||||
m := make([]byte, DefaultMsgSize)
|
||||
n, radd, e := l.ReadFromUDP(m)
|
||||
if e != nil {
|
||||
continue
|
||||
}
|
||||
m = m[:n]
|
||||
msg := new(Msg)
|
||||
if ! msg.Unpack(m) {
|
||||
continue
|
||||
}
|
||||
go f(l, radd, msg)
|
||||
m := make([]byte, DefaultMsgSize)
|
||||
n, radd, e := l.ReadFromUDP(m)
|
||||
if e != nil {
|
||||
continue
|
||||
}
|
||||
m = m[:n]
|
||||
msg := new(Msg)
|
||||
if !msg.Unpack(m) {
|
||||
continue
|
||||
}
|
||||
go f(l, radd, msg)
|
||||
}
|
||||
panic("not reached")
|
||||
}
|
||||
|
||||
func ServeTCP(l *net.TCPListener, f func(*net.TCPConn, net.Addr, *Msg)) os.Error {
|
||||
b := make([]byte, 2)
|
||||
b := make([]byte, 2)
|
||||
for {
|
||||
c, e := l.AcceptTCP()
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
c, e := l.AcceptTCP()
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
n, e := c.Read(b)
|
||||
if e != nil {
|
||||
continue
|
||||
continue
|
||||
}
|
||||
|
||||
length := uint16(b[0])<<8 | uint16(b[1])
|
||||
if length == 0 {
|
||||
return &Error{Error: "received nil msg length"}
|
||||
return &Error{Error: "received nil msg length"}
|
||||
}
|
||||
m := make([]byte, length)
|
||||
m := make([]byte, length)
|
||||
|
||||
n, e = c.Read(m)
|
||||
n, e = c.Read(m)
|
||||
if e != nil {
|
||||
continue
|
||||
continue
|
||||
}
|
||||
i := n
|
||||
if i < int(length) {
|
||||
n, e = c.Read(m[i:])
|
||||
if e != nil {
|
||||
continue
|
||||
n, e = c.Read(m[i:])
|
||||
if e != nil {
|
||||
continue
|
||||
}
|
||||
i += n
|
||||
}
|
||||
msg := new(Msg)
|
||||
if ! msg.Unpack(m) {
|
||||
continue
|
||||
}
|
||||
go f(c, c.RemoteAddr(), msg)
|
||||
msg := new(Msg)
|
||||
if !msg.Unpack(m) {
|
||||
continue
|
||||
}
|
||||
go f(c, c.RemoteAddr(), msg)
|
||||
}
|
||||
panic("not reached")
|
||||
}
|
||||
|
@ -76,7 +83,7 @@ func ListenAndServeTCP(addr string, f func(*net.TCPConn, net.Addr, *Msg)) os.Err
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = ServeTCP(l, f)
|
||||
err = ServeTCP(l, f)
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -89,7 +96,7 @@ func ListenAndServeUDP(addr string, f func(*net.UDPConn, net.Addr, *Msg)) os.Err
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = ServeUDP(l, f)
|
||||
err = ServeUDP(l, f)
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -108,14 +115,14 @@ func SendTCP(m []byte, c *net.TCPConn, a net.Addr) os.Error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
i := n
|
||||
for i < len(m) {
|
||||
n, err = c.Write(m)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
i += n
|
||||
}
|
||||
i := n
|
||||
for i < len(m) {
|
||||
n, err = c.Write(m)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
i += n
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue