Merge branch 'format'

This commit is contained in:
Miek Gieben 2015-01-13 16:40:23 +00:00
commit 2c80cd7c5c
4 changed files with 147 additions and 16 deletions

6
dns.go
View File

@ -93,9 +93,7 @@
// spaces, semicolons and the at symbol are escaped.
package dns
import (
"strconv"
)
import "strconv"
const (
year68 = 1 << 31 // For RFC1982 (Serial Arithmetic) calculations in 32 bits.
@ -124,7 +122,7 @@ type RR interface {
String() string
// copy returns a copy of the RR
copy() RR
// len returns the length (in octects) of the uncompressed RR in wire format.
// len returns the length (in octets) of the uncompressed RR in wire format.
len() int
}

96
format.go Normal file
View File

@ -0,0 +1,96 @@
package dns
import (
"net"
"reflect"
"strconv"
)
// NumField returns the number of rdata fields r has.
func NumField(r RR) int {
return reflect.ValueOf(r).Elem().NumField() - 1 // Remove RR_Header
}
// Field returns the rdata field i as a string. Fields are indexed starting from 1.
// RR types that holds slice data, for instance the NSEC type bitmap will return a single
// string where the types are concatenated using a space.
// Accessing non existing fields will cause a panic.
func Field(r RR, i int) string {
if i == 0 {
return ""
}
d := reflect.ValueOf(r).Elem().Field(i)
switch k := d.Kind(); k {
case reflect.String:
return d.String()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return strconv.FormatInt(d.Int(), 10)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return strconv.FormatUint(d.Uint(), 10)
case reflect.Slice:
switch reflect.ValueOf(r).Elem().Type().Field(i).Tag {
case `dns:"a"`:
// TODO(miek): Hmm store this as 16 bytes
if d.Len() < net.IPv6len {
return net.IPv4(byte(d.Index(0).Uint()),
byte(d.Index(1).Uint()),
byte(d.Index(2).Uint()),
byte(d.Index(3).Uint())).String()
}
return net.IPv4(byte(d.Index(12).Uint()),
byte(d.Index(13).Uint()),
byte(d.Index(14).Uint()),
byte(d.Index(15).Uint())).String()
case `dns:"aaaa"`:
return net.IP{
byte(d.Index(0).Uint()),
byte(d.Index(1).Uint()),
byte(d.Index(2).Uint()),
byte(d.Index(3).Uint()),
byte(d.Index(4).Uint()),
byte(d.Index(5).Uint()),
byte(d.Index(6).Uint()),
byte(d.Index(7).Uint()),
byte(d.Index(8).Uint()),
byte(d.Index(9).Uint()),
byte(d.Index(10).Uint()),
byte(d.Index(11).Uint()),
byte(d.Index(12).Uint()),
byte(d.Index(13).Uint()),
byte(d.Index(14).Uint()),
byte(d.Index(15).Uint()),
}.String()
case `dns:"nsec"`:
if d.Len() == 0 {
return ""
}
s := Type(d.Index(0).Uint()).String()
for i := 1; i < d.Len(); i++ {
s += " " + Type(d.Index(i).Uint()).String()
}
return s
case `dns:"wks"`:
if d.Len() == 0 {
return ""
}
s := strconv.Itoa(int(d.Index(0).Uint()))
for i := 0; i < d.Len(); i++ {
s += " " + strconv.Itoa(int(d.Index(i).Uint()))
}
return s
default:
// if it does not have a tag its a string slice
fallthrough
case `dns:"txt"`:
if d.Len() == 0 {
return ""
}
s := d.Index(0).String()
for i := 1; i < d.Len(); i++ {
s += " " + d.Index(i).String()
}
return s
}
}
return ""
}

View File

@ -1286,54 +1286,90 @@ func TestNewRRSpecial(t *testing.T) {
rr, err = NewRR("; comment")
expect = ""
if err != nil {
t.Errorf("unexpect err: %s", err)
t.Errorf("unexpected err: %s", err)
}
if rr != nil {
t.Errorf("unexpect result: [%s] != [%s]", rr, expect)
t.Errorf("unexpected result: [%s] != [%s]", rr, expect)
}
rr, err = NewRR("")
expect = ""
if err != nil {
t.Errorf("unexpect err: %s", err)
t.Errorf("unexpected err: %s", err)
}
if rr != nil {
t.Errorf("unexpect result: [%s] != [%s]", rr, expect)
t.Errorf("unexpected result: [%s] != [%s]", rr, expect)
}
rr, err = NewRR("$ORIGIN foo.")
expect = ""
if err != nil {
t.Errorf("unexpect err: %s", err)
t.Errorf("unexpected err: %s", err)
}
if rr != nil {
t.Errorf("unexpect result: [%s] != [%s]", rr, expect)
t.Errorf("unexpected result: [%s] != [%s]", rr, expect)
}
rr, err = NewRR(" ")
expect = ""
if err != nil {
t.Errorf("unexpect err: %s", err)
t.Errorf("unexpected err: %s", err)
}
if rr != nil {
t.Errorf("unexpect result: [%s] != [%s]", rr, expect)
t.Errorf("unexpected result: [%s] != [%s]", rr, expect)
}
rr, err = NewRR("\n")
expect = ""
if err != nil {
t.Errorf("unexpect err: %s", err)
t.Errorf("unexpected err: %s", err)
}
if rr != nil {
t.Errorf("unexpect result: [%s] != [%s]", rr, expect)
t.Errorf("unexpected result: [%s] != [%s]", rr, expect)
}
rr, err = NewRR("foo. A 1.1.1.1\nbar. A 2.2.2.2")
expect = "foo.\t3600\tIN\tA\t1.1.1.1"
if err != nil {
t.Errorf("unexpect err: %s", err)
t.Errorf("unexpected err: %s", err)
}
if rr == nil || rr.String() != expect {
t.Errorf("unexpect result: [%s] != [%s]", rr, expect)
t.Errorf("unexpected result: [%s] != [%s]", rr, expect)
}
}
func TestPrintfVerbsRdata(t *testing.T) {
x, _ := NewRR("www.miek.nl. IN MX 20 mx.miek.nl.")
if Field(x, 1) != "20" {
t.Errorf("should be 20")
}
if Field(x, 2) != "mx.miek.nl." {
t.Errorf("should be mx.miek.nl.")
}
x, _ = NewRR("www.miek.nl. IN A 127.0.0.1")
if Field(x, 1) != "127.0.0.1" {
t.Errorf("should be 127.0.0.1")
}
x, _ = NewRR("www.miek.nl. IN AAAA ::1")
if Field(x, 1) != "::1" {
t.Errorf("should be ::1")
}
x, _ = NewRR("www.miek.nl. IN NSEC a.miek.nl. A NS SOA MX AAAA")
if Field(x, 1) != "a.miek.nl." {
t.Errorf("should be a.miek.nl.")
}
if Field(x, 2) != "A NS SOA MX AAAA" {
t.Errorf("should be A NS SOA MX AAAA")
}
x, _ = NewRR("www.miek.nl. IN TXT \"first\" \"second\"")
if Field(x, 1) != "first second" {
t.Errorf("should be first second")
}
if Field(x, 0) != "" {
t.Errorf("should be empty")
}
}

View File

@ -1394,6 +1394,7 @@ func (rr *WKS) String() (s string) {
if rr.Address != nil {
s += rr.Address.String()
}
// TODO(miek): missing protocol here, see /etc/protocols
for i := 0; i < len(rr.BitMap); i++ {
// should lookup the port
s += " " + strconv.Itoa(int(rr.BitMap[i]))