validate ipv6 zone

This commit is contained in:
Steven Allen 2018-09-14 18:51:03 -07:00
parent 1f9df061aa
commit 6f084e635d
1 changed files with 10 additions and 1 deletions

View File

@ -1,6 +1,7 @@
package multiaddr
import (
"bytes"
"encoding/base32"
"encoding/binary"
"fmt"
@ -47,7 +48,7 @@ func (t twrp) ValidateBytes(b []byte) error {
var TranscoderIP4 = NewTranscoderFromFunctions(ip4StB, ipBtS, nil)
var TranscoderIP6 = NewTranscoderFromFunctions(ip6StB, ipBtS, nil)
var TranscoderIP6Zone = NewTranscoderFromFunctions(ip6zoneStB, ip6zoneBtS, nil)
var TranscoderIP6Zone = NewTranscoderFromFunctions(ip6zoneStB, ip6zoneBtS, ip6zoneVal)
func ip4StB(s string) ([]byte, error) {
i := net.ParseIP(s).To4()
@ -71,6 +72,14 @@ func ip6zoneBtS(b []byte) (string, error) {
return string(b), nil
}
func ip6zoneVal(b []byte) error {
// Not supported as this would break multiaddrs.
if bytes.IndexByte(b, '/') >= 0 {
return fmt.Errorf("IPv6 zone ID contains '/': %s", string(b))
}
return nil
}
func ip6StB(s string) ([]byte, error) {
i := net.ParseIP(s).To16()
if i == nil {