try parse domain address as IP. fixes #894.

pull/931/head
Darien Raymond 2018-03-01 11:32:55 +01:00
parent bdab1af29a
commit a7d467992d
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
2 changed files with 35 additions and 1 deletions

View File

@ -55,6 +55,19 @@ func (p *AddressParser) readPort(b *buf.Buffer, reader io.Reader) (net.Port, err
return net.PortFromBytes(b.BytesFrom(-2)), nil
}
func maybeIPPrefix(b byte) bool {
return b == '[' || (b >= '0' && b <= '9')
}
func isValidDomain(d string) bool {
for _, c := range d {
if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '-' || c == '.') {
return false
}
}
return true
}
func (p *AddressParser) readAddress(b *buf.Buffer, reader io.Reader) (net.Address, error) {
if err := b.AppendSupplier(buf.ReadFullFrom(reader, 1)); err != nil {
return nil, err
@ -89,7 +102,17 @@ func (p *AddressParser) readAddress(b *buf.Buffer, reader io.Reader) (net.Addres
if err := b.AppendSupplier(buf.ReadFullFrom(reader, domainLength)); err != nil {
return nil, err
}
return net.DomainAddress(string(b.BytesFrom(-domainLength))), nil
domain := string(b.BytesFrom(-domainLength))
if maybeIPPrefix(domain[0]) {
addr := net.ParseAddress(domain)
if addr.Family().IsIPv4() || addrFamily.IsIPv6() {
return addr, nil
}
}
if !isValidDomain(domain) {
return nil, newError("invalid domain name: ", domain)
}
return net.DomainAddress(domain), nil
default:
panic("impossible case")
}

View File

@ -58,6 +58,17 @@ func TestAddressReading(t *testing.T) {
Input: []byte{3, 9, 118, 50, 114, 97, 121, 46, 99, 111, 109, 0},
Error: true,
},
{
Options: []AddressOption{AddressFamilyByte(0x03, net.AddressFamilyDomain)},
Input: []byte{3, 7, 56, 46, 56, 46, 56, 46, 56, 0, 80},
Address: net.ParseAddress("8.8.8.8"),
Port: net.Port(80),
},
{
Options: []AddressOption{AddressFamilyByte(0x03, net.AddressFamilyDomain)},
Input: []byte{3, 7, 10, 46, 56, 46, 56, 46, 56, 0, 80},
Error: true,
},
}
for _, tc := range data {