Some cleanup

This commit is contained in:
Miek Gieben 2010-12-25 13:09:27 +01:00
parent 70552b49ca
commit d6efd37302
5 changed files with 31 additions and 14 deletions

2
TODO
View File

@ -1,6 +1,8 @@
EDNS -- finish EDNS0 support
DNSSEC - validation and the remaining records, DS (encoding), NSEC and NSEC3
unknown RRs - if I ever get around to make it
convience functions
parsing from strings
"miek.nl IN A 192.168.1.2" -> <correct Go type>

View File

@ -9,6 +9,7 @@ const (
_DO = 1 << 15 // dnssec ok
)
// An ENDS0 option rdata element.
type Option struct {
Code uint16
Data string "hex"

30
msg.go
View File

@ -174,9 +174,6 @@ func packStructValue(val *reflect.StructValue, msg []byte, off int) (off1 int, o
BadType:
fmt.Fprintf(os.Stderr, "net: dns: unknown packing type %v\n", f.Type)
return len(msg), false
case *reflect.BoolValue:
// Used internally for Edns, not present in the DNS
continue
case *reflect.SliceValue:
switch f.Tag {
default:
@ -306,9 +303,6 @@ func unpackStructValue(val *reflect.StructValue, msg []byte, off int) (off1 int,
BadType:
fmt.Fprintf(os.Stderr, "net: dns: unknown packing type %v", f.Type)
return len(msg), false
case *reflect.BoolValue:
// Used internally for Edns, not present in the DNS
continue
case *reflect.SliceValue:
switch f.Tag {
default:
@ -331,7 +325,18 @@ func unpackStructValue(val *reflect.StructValue, msg []byte, off int) (off1 int,
fv.Set(reflect.NewValue(b).(*reflect.SliceValue))
off += net.IPv6len
case "OPT": // edns
// do it here
// check rdlength, if more, then more options
// TODO(mg) checking
// for now: allow only 1. TODO(mg)
/*
opt := make([]Option, 1)
opt[0].Code, off = unpackUint16(msg, off)
length := uint16(msg[off])<<8 | uint16(msg[off+1])
off += 2
opt[0].Data = string(msg[off:off+int(length)])
off += int(length)
//opt
*/
}
case *reflect.StructValue:
off, ok = unpackStructValue(fv, msg, off)
@ -347,12 +352,12 @@ func unpackStructValue(val *reflect.StructValue, msg []byte, off int) (off1 int,
fv.Set(uint64(i))
off++
case reflect.Uint16:
var i uint16
if off+2 > len(msg) {
return len(msg), false
}
i := uint16(msg[off])<<8 | uint16(msg[off+1])
i, off = unpackUint16(msg, off)
fv.Set(uint64(i))
off += 2
case reflect.Uint32:
if off+4 > len(msg) {
return len(msg), false
@ -425,6 +430,13 @@ func unpackStructValue(val *reflect.StructValue, msg []byte, off int) (off1 int,
return off, true
}
// Helper function for unpacking
func unpackUint16(msg []byte, off int) (v uint16, off1 int) {
v = uint16(msg[off])<<8 | uint16(msg[off+1])
off1 = off+2
return
}
func unpackStruct(any interface{}, msg []byte, off int) (off1 int, ok bool) {
off, ok = unpackStructValue(structValue(any), msg, off)
return off, ok

View File

@ -15,7 +15,7 @@ func TestResolverEdns(t *testing.T) {
m := new(Msg)
m.MsgHdr.Recursion_desired = true //only set this bit
m.Question = make([]Question, 1)
m.Ns = make([]RR, 1)
m.Extra = make([]RR, 1)
// Add EDNS rr
edns := new(RR_OPT)
@ -23,11 +23,11 @@ func TestResolverEdns(t *testing.T) {
edns.Hdr.Rrtype = TypeOPT
// You can handle an OTP RR as any other, but there
// are some convience functions
ends.UDPSize(4096, true)
edns.DoBit(true, true)
// edns.UDPSize(4096, true)
// edns.DoBit(true, true)
// edns.Nsid("mieks-server", true)
// edns.Hdr.Class = ClassINET
// edns.Hdr.Ttl = 3600
edns.Hdr.Class = ClassINET
edns.Hdr.Ttl = 3600
// no options for now
// edns.Option = make([]Option, 1)
// edns.Option[0].Code = OptionCodeNSID

View File

@ -1,5 +1,7 @@
package dns
// use scanner or ebnf
// Convert a string to an resource record. The string must fir on one line.
// miek.nl. 3600 IN A 192.168.1.1 // ok
// miek.nl. IN A 192.168.1.1 // ok, ttl may be omitted