Skip to content

Commit

Permalink
Feat: support IPv4 address in IPv6 format
Browse files Browse the repository at this point in the history
This change will convert 4in6 IPv6 addresses to IPv4 addresses
  • Loading branch information
Loyalsoldier committed Oct 14, 2023
1 parent a1bd197 commit db04afe
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ func (e *Entry) processPrefix(src any) (*netip.Prefix, IPType, error) {
_, network, err := net.ParseCIDR(src)
switch err {
case nil:
prefix, ok := netipx.FromStdIPNet(network)
if !ok {
prefix, err2 := netip.ParsePrefix(network.String())
if err2 != nil {
return nil, "", ErrInvalidIPNet
}
ip := prefix.Addr()
Expand All @@ -190,6 +190,16 @@ func (e *Entry) processPrefix(src any) (*netip.Prefix, IPType, error) {
case ip.Is4():
prefix := netip.PrefixFrom(ip, 32)
return &prefix, IPv4, nil
case ip.Is4In6():
_, network, err2 := net.ParseCIDR(src + "/128")
if err2 != nil {
return nil, "", ErrInvalidIPNet
}
prefix, err3 := netip.ParsePrefix(network.String())
if err3 != nil {
return nil, "", ErrInvalidIPNet
}
return &prefix, IPv4, nil
case ip.Is6():
prefix := netip.PrefixFrom(ip, 128)
return &prefix, IPv6, nil
Expand Down

0 comments on commit db04afe

Please sign in to comment.