Skip to content

Commit

Permalink
libnetwork/util: add GetNetworkIPs()
Browse files Browse the repository at this point in the history
Add helper function to get the first ipv4 and ipv6 from the network
status. Since this is stored as a map, calling this function multiple
times is not deterministic. The caller (Podman) should store this result
to make sure it will keep using the same ips.

Signed-off-by: Paul Holzinger <[email protected]>
  • Loading branch information
Luap99 committed Apr 11, 2022
1 parent 6517fbd commit 954f444
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions libnetwork/util/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package util

import (
"net"

"github.com/containers/common/libnetwork/types"
)

// IsIPv6 returns true if netIP is IPv6.
Expand Down Expand Up @@ -54,3 +56,27 @@ func NormalizeIP(ip *net.IP) {
*ip = ipv4
}
}

// GetNetworkIPs return one ipv4 and ipv6 address of this network status.
// The first return value is the ipv4 and the second one the ipv6 address.
// If there is no ipv4/6 address it will return an empty string instead.
func GetNetworkIPs(netStatus map[string]types.StatusBlock) (ipv4, ipv6 string) {
for _, status := range netStatus {
for _, netInt := range status.Interfaces {
for _, netAddress := range netInt.Subnets {
if IsIPv4(netAddress.IPNet.IP) {
if ipv4 == "" {
ipv4 = netAddress.IPNet.IP.String()
}
} else if ipv6 == "" {
ipv6 = netAddress.IPNet.IP.String()
}

if ipv4 != "" && ipv6 != "" {
return ipv4, ipv6
}
}
}
}
return ipv4, ipv6
}

0 comments on commit 954f444

Please sign in to comment.