Revert commits unrelated to PackDomainName

This reverts commit 2eeda8aabc,
                    8e6e188a87
                and 7bef528091.
This commit is contained in:
Tom Thorogood 2018-11-27 17:54:24 +10:30
parent f522504216
commit 30d0133e57
No known key found for this signature in database
GPG Key ID: 86C63CDA416C6D2F
4 changed files with 22 additions and 50 deletions

18
edns.go
View File

@ -273,16 +273,22 @@ func (e *EDNS0_SUBNET) unpack(b []byte) error {
if e.SourceNetmask > net.IPv4len*8 || e.SourceScope > net.IPv4len*8 {
return errors.New("dns: bad netmask")
}
addr := make(net.IP, net.IPv4len)
copy(addr, b[4:])
e.Address = addr.To16()
addr := make([]byte, net.IPv4len)
for i := 0; i < net.IPv4len && 4+i < len(b); i++ {
addr[i] = b[4+i]
}
e.Address = net.IPv4(addr[0], addr[1], addr[2], addr[3])
case 2:
if e.SourceNetmask > net.IPv6len*8 || e.SourceScope > net.IPv6len*8 {
return errors.New("dns: bad netmask")
}
addr := make(net.IP, net.IPv6len)
copy(addr, b[4:])
e.Address = addr
addr := make([]byte, net.IPv6len)
for i := 0; i < net.IPv6len && 4+i < len(b); i++ {
addr[i] = b[4+i]
}
e.Address = net.IP{addr[0], addr[1], addr[2], addr[3], addr[4],
addr[5], addr[6], addr[7], addr[8], addr[9], addr[10],
addr[11], addr[12], addr[13], addr[14], addr[15]}
default:
return errors.New("dns: bad address family")
}

View File

@ -1,9 +1,6 @@
package dns
import (
"net"
"testing"
)
import "testing"
func TestOPTTtl(t *testing.T) {
e := &OPT{}
@ -66,8 +63,8 @@ func TestOPTTtl(t *testing.T) {
e.SetExtendedRcode(42)
// ExtendedRcode has the last 4 bits set to 0.
if e.ExtendedRcode() != 42&0xFFFFFFF0 {
t.Errorf("set 42, expected %d, got %d", 42&0xFFFFFFF0, e.ExtendedRcode())
if e.ExtendedRcode() != 42 & 0xFFFFFFF0 {
t.Errorf("set 42, expected %d, got %d", 42 & 0xFFFFFFF0, e.ExtendedRcode())
}
// This will reset the 8 upper bits of the extended rcode
@ -76,36 +73,3 @@ func TestOPTTtl(t *testing.T) {
t.Errorf("Setting a non-extended rcode is expected to set extended rcode to 0, got: %d", e.ExtendedRcode())
}
}
func TestEDNS0_SUBNETUnpack(t *testing.T) {
for _, ip := range []net.IP{
net.IPv4(0xde, 0xad, 0xbe, 0xef),
net.ParseIP("192.0.2.1"),
net.ParseIP("2001:db8::68"),
} {
var s1 EDNS0_SUBNET
s1.Address = ip
if ip.To4() == nil {
s1.Family = 2
s1.SourceNetmask = net.IPv6len * 8
} else {
s1.Family = 1
s1.SourceNetmask = net.IPv4len * 8
}
b, err := s1.pack()
if err != nil {
t.Fatalf("failed to pack: %v", err)
}
var s2 EDNS0_SUBNET
if err := s2.unpack(b); err != nil {
t.Fatalf("failed to unpack: %v", err)
}
if !ip.Equal(s2.Address) {
t.Errorf("address different after unpacking; expected %s, got %s", ip, s2.Address)
}
}
}

8
msg.go
View File

@ -1019,9 +1019,11 @@ func compressionLenHelper(c map[string]int, s string, currentLen int) int {
return 0
}
initLen := currentLen
pref := ""
prev := s
for off, end := 0, false; !end; off, end = NextLabel(s, off) {
pref := s[off:]
lbs := Split(s)
for j := 0; j < len(lbs); j++ {
pref = s[lbs[j]:]
currentLen += len(prev) - len(pref)
prev = pref
if _, ok := c[pref]; !ok {
@ -1031,7 +1033,7 @@ func compressionLenHelper(c map[string]int, s string, currentLen int) int {
}
} else {
added := currentLen - initLen
if off > 0 {
if j > 0 {
// We added a new PTR
added += 2
}

View File

@ -223,8 +223,8 @@ func unpackUint48(msg []byte, off int) (i uint64, off1 int, err error) {
return 0, len(msg), &Error{err: "overflow unpacking uint64 as uint48"}
}
// Used in TSIG where the last 48 bits are occupied, so for now, assume a uint48 (6 bytes)
i = uint64(msg[off])<<40 | uint64(msg[off+1])<<32 | uint64(msg[off+2])<<24 | uint64(msg[off+3])<<16 |
uint64(msg[off+4])<<8 | uint64(msg[off+5])
i = uint64(uint64(msg[off])<<40 | uint64(msg[off+1])<<32 | uint64(msg[off+2])<<24 | uint64(msg[off+3])<<16 |
uint64(msg[off+4])<<8 | uint64(msg[off+5]))
off += 6
return i, off, nil
}