Skip to content

Commit

Permalink
feat(breakchange): resolve interface should use IP net to allow IP ra…
Browse files Browse the repository at this point in the history
…nges
  • Loading branch information
emiago committed Nov 21, 2024
1 parent b2cb072 commit 5fcba72
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
8 changes: 4 additions & 4 deletions sip/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func findAnyUnescaped(text string, targets string, delims ...delimiter) int {
// Using targetIP it will try to match interface with same subnet
// network can be "ip" "ip4" "ip6"
// by default it avoids loopack IP unless targetIP is loopback
func ResolveInterfacesIP(network string, targetIP net.IP) (net.IP, net.Interface, error) {
func ResolveInterfacesIP(network string, targetIP *net.IPNet) (net.IP, net.Interface, error) {
ifaces, err := net.Interfaces()
if err != nil {
return nil, net.Interface{}, err
Expand All @@ -268,7 +268,7 @@ func ResolveInterfacesIP(network string, targetIP net.IP) (net.IP, net.Interface
continue // interface down
}
if iface.Flags&net.FlagLoopback != 0 {
if targetIP != nil && !targetIP.IsLoopback() {
if targetIP != nil && !targetIP.IP.IsLoopback() {
continue // loopback interface
}
}
Expand All @@ -283,7 +283,7 @@ func ResolveInterfacesIP(network string, targetIP net.IP) (net.IP, net.Interface
return nil, net.Interface{}, errors.New("no interface found on system")
}

func resolveInterfaceIp(iface net.Interface, network string, targetIP net.IP) (net.IP, error) {
func resolveInterfaceIp(iface net.Interface, network string, targetIP *net.IPNet) (net.IP, error) {
addrs, err := iface.Addrs()
if err != nil {
return nil, err
Expand All @@ -297,7 +297,7 @@ func resolveInterfaceIp(iface net.Interface, network string, targetIP net.IP) (n
}
ip = ipNet.IP
if targetIP != nil {
if !ipNet.Contains(targetIP) {
if !targetIP.Contains(ip) {
continue
}
} else {
Expand Down
6 changes: 5 additions & 1 deletion sip/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ func TestResolveInterfaceIP(t *testing.T) {
assert.False(t, ip.IsLoopback())
assert.NotNil(t, ip.To16())

ip, iface, err = ResolveInterfacesIP("ip4", net.ParseIP("127.0.0.1"))
ipnet := net.IPNet{
IP: net.ParseIP("127.0.0.1"),
Mask: net.IPv4Mask(255, 255, 255, 0),
}
ip, iface, err = ResolveInterfacesIP("ip4", &ipnet)
require.NoError(t, err)
require.NotNil(t, ip)
}
Expand Down

0 comments on commit 5fcba72

Please sign in to comment.