Skip to content

Commit

Permalink
refactor(evpn): use ParseCIDR to reduce code
Browse files Browse the repository at this point in the history
Fixes #256

Signed-off-by: Boris Glimcher <[email protected]>
  • Loading branch information
glimchb committed Aug 11, 2023
1 parent 1d9df71 commit 6cbf85d
Showing 1 changed file with 12 additions and 36 deletions.
48 changes: 12 additions & 36 deletions network/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ package network
import (
"fmt"
"net"
"strconv"
"strings"

pc "github.com/opiproject/opi-api/network/opinetcommon/v1alpha1/gen/go"
)
Expand All @@ -21,51 +19,29 @@ func ip4ToInt(ip net.IP) uint32 {

// Function to parse IP address and prefix from a string of the form "IP/PREFIX"
func parseIPAndPrefix(ipPrefixStr string) (*pc.IPPrefix, error) {
parts := strings.Split(ipPrefixStr, "/")
if len(parts) != 2 {
return nil, fmt.Errorf("invalid IP address with prefix: %s", ipPrefixStr)
}

ipAddress := parts[0]
prefixLength64, err := strconv.ParseInt(parts[1], 10, 32)

ip, ipnet, err := net.ParseCIDR(ipPrefixStr)

Check warning on line 22 in network/utils.go

View check run for this annotation

Codecov / codecov/patch

network/utils.go#L22

Added line #L22 was not covered by tests
if err != nil {
return nil, fmt.Errorf("failed to parse prefix length: %s", err)
return nil, err

Check warning on line 24 in network/utils.go

View check run for this annotation

Codecov / codecov/patch

network/utils.go#L24

Added line #L24 was not covered by tests
}

prefixLength := int32(prefixLength64)
prefixLength, _ := ipnet.Mask.Size()
addr := &pc.IPAddress{}

Check warning on line 28 in network/utils.go

View check run for this annotation

Codecov / codecov/patch

network/utils.go#L27-L28

Added lines #L27 - L28 were not covered by tests

ip := net.ParseIP(ipAddress)
if ip == nil {
return nil, fmt.Errorf("invalid IP address: %s", ipAddress)
}

addressFamily := int32(0)
var ipv4Addr uint32
var ipv6Addr []byte
var addr *pc.IPAddress
if ip.To4() != nil {
addressFamily = 1 // IPv4
ipv4Addr = ip4ToInt(ip.To4())
addr = &pc.IPAddress{
Af: pc.IpAf(addressFamily),
V4OrV6: &pc.IPAddress_V4Addr{
V4Addr: ipv4Addr,
},
addr.Af = pc.IpAf_IP_AF_INET
addr.V4OrV6 = &pc.IPAddress_V4Addr{
V4Addr: ip4ToInt(ip.To4()),

Check warning on line 33 in network/utils.go

View check run for this annotation

Codecov / codecov/patch

network/utils.go#L31-L33

Added lines #L31 - L33 were not covered by tests
}
} else {
addressFamily = 2 // IPv6
ipv6Addr = ip.To16()
addr = &pc.IPAddress{
Af: pc.IpAf(addressFamily),
V4OrV6: &pc.IPAddress_V6Addr{
V6Addr: ipv6Addr,
},
addr.Af = pc.IpAf_IP_AF_INET6
addr.V4OrV6 = &pc.IPAddress_V6Addr{
V6Addr: ip.To16(),

Check warning on line 38 in network/utils.go

View check run for this annotation

Codecov / codecov/patch

network/utils.go#L36-L38

Added lines #L36 - L38 were not covered by tests
}
}

return &pc.IPPrefix{
Addr: addr,
Len: prefixLength,
Len: int32(prefixLength),

Check warning on line 44 in network/utils.go

View check run for this annotation

Codecov / codecov/patch

network/utils.go#L44

Added line #L44 was not covered by tests
}, nil
}

Expand Down

0 comments on commit 6cbf85d

Please sign in to comment.