Use SourceNetmask to truncate IP addresses in EDNS-CLIENT-SUBNET packets

OpenDNS returns SERVFAIL to the client if the address in the EDNS packet
is too long. The spec says to truncate it to the shortest possible address
when the SourceNetmask is applied.
This commit is contained in:
Ask Bjørn Hansen 2013-05-07 16:53:43 -07:00
parent 6c9efcdf1f
commit 0bcd43e7cc
1 changed files with 10 additions and 1 deletions

11
edns.go
View File

@ -227,6 +227,11 @@ func (e *EDNS0_SUBNET) pack() ([]byte, error) {
}
ip[i] = a[i]
}
needLength := e.SourceNetmask / 8
if e.SourceNetmask%8 > 0 {
needLength++
}
ip = ip[:needLength]
b = append(b, ip...)
case 2:
if e.SourceNetmask > net.IPv6len*8 {
@ -240,7 +245,11 @@ func (e *EDNS0_SUBNET) pack() ([]byte, error) {
}
ip[i] = a[i]
}
// chop off ip a SourceNetmask/8: ip = ip[:e.SourceNetmask/8] ?
needLength := e.SourceNetmask / 8
if e.SourceNetmask%8 > 0 {
needLength++
}
ip = ip[:needLength]
b = append(b, ip...)
default:
return nil, errors.New("dns: bad address family")