Skip to content

Commit

Permalink
Merge pull request #247 from multiformats/marco/validate-ipcidr
Browse files Browse the repository at this point in the history
nit: validate ipcidr
  • Loading branch information
MarcoPolo authored Jul 17, 2024
2 parents bbdd1a5 + 32ec788 commit f63b0ed
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions transcoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ func (t twrp) ValidateBytes(b []byte) error {
var TranscoderIP4 = NewTranscoderFromFunctions(ip4StB, ip4BtS, nil)
var TranscoderIP6 = NewTranscoderFromFunctions(ip6StB, ip6BtS, nil)
var TranscoderIP6Zone = NewTranscoderFromFunctions(ip6zoneStB, ip6zoneBtS, ip6zoneVal)
var TranscoderIPCIDR = NewTranscoderFromFunctions(ipcidrStB, ipcidrBtS, nil)
var TranscoderIPCIDR = NewTranscoderFromFunctions(ipcidrStB, ipcidrBtS, ipcidrValidate)

func ipcidrBtS(b []byte) (string, error) {
if len(b) != 1 {
return "", fmt.Errorf("invalid length (should be == 1)")
if err := ipcidrValidate(b); err != nil {
return "", err
}
return strconv.Itoa(int(b[0])), nil
}
Expand All @@ -74,6 +74,13 @@ func ipcidrStB(s string) ([]byte, error) {
return []byte{byte(uint8(ipMask))}, nil
}

func ipcidrValidate(b []byte) error {
if len(b) != 1 {
return fmt.Errorf("invalid length (should be == 1)")
}
return nil
}

func ip4StB(s string) ([]byte, error) {
i := net.ParseIP(s).To4()
if i == nil {
Expand Down

0 comments on commit f63b0ed

Please sign in to comment.