|
| 1 | +//go:build linux || freebsd |
| 2 | + |
| 3 | +package netavark |
| 4 | + |
| 5 | +import ( |
| 6 | + "encoding/json" |
| 7 | + "errors" |
| 8 | + "io/fs" |
| 9 | + "os" |
| 10 | + "sort" |
| 11 | + |
| 12 | + "go.podman.io/common/libnetwork/types" |
| 13 | +) |
| 14 | + |
| 15 | +// pestoAddrState tracks which container IPs are currently used for pesto |
| 16 | +// port forwarding. Stored as a per-container JSON file in the rootless |
| 17 | +// netns directory so connect/disconnect operations can detect whether |
| 18 | +// ports are already published and remap when necessary. |
| 19 | +type pestoAddrState struct { |
| 20 | + ActiveIPv4 string `json:"active_ipv4,omitempty"` |
| 21 | + ActiveIPv4Network string `json:"active_ipv4_network,omitempty"` |
| 22 | + ActiveIPv6 string `json:"active_ipv6,omitempty"` |
| 23 | + ActiveIPv6Network string `json:"active_ipv6_network,omitempty"` |
| 24 | + NetworkIPs map[string]pestoNetworkIPs `json:"network_ips"` |
| 25 | +} |
| 26 | + |
| 27 | +// pestoNetworkIPs holds the first IPv4 and IPv6 address assigned to a |
| 28 | +// container on a specific network. |
| 29 | +type pestoNetworkIPs struct { |
| 30 | + IPv4 string `json:"ipv4,omitempty"` |
| 31 | + IPv6 string `json:"ipv6,omitempty"` |
| 32 | +} |
| 33 | + |
| 34 | +func readPestoAddrState(path string) (*pestoAddrState, error) { |
| 35 | + data, err := os.ReadFile(path) |
| 36 | + if err != nil { |
| 37 | + if errors.Is(err, fs.ErrNotExist) { |
| 38 | + return nil, nil |
| 39 | + } |
| 40 | + return nil, err |
| 41 | + } |
| 42 | + var state pestoAddrState |
| 43 | + if err := json.Unmarshal(data, &state); err != nil { |
| 44 | + return nil, err |
| 45 | + } |
| 46 | + return &state, nil |
| 47 | +} |
| 48 | + |
| 49 | +func writePestoAddrState(path string, state *pestoAddrState) error { |
| 50 | + data, err := json.Marshal(state) |
| 51 | + if err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + tmp := path + ".tmp" |
| 55 | + if err := os.WriteFile(tmp, data, 0o600); err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + return os.Rename(tmp, path) |
| 59 | +} |
| 60 | + |
| 61 | +func removePestoAddrState(path string) { |
| 62 | + _ = os.Remove(path) |
| 63 | +} |
| 64 | + |
| 65 | +// containerIPsPerNetwork extracts the first IPv4 and IPv6 from each network |
| 66 | +// in the StatusBlock result. The map is keyed by network name. |
| 67 | +func containerIPsPerNetwork(result map[string]types.StatusBlock) map[string]pestoNetworkIPs { |
| 68 | + out := make(map[string]pestoNetworkIPs, len(result)) |
| 69 | + for netName, status := range result { |
| 70 | + var ips pestoNetworkIPs |
| 71 | + for _, netInt := range status.Interfaces { |
| 72 | + for _, netAddr := range netInt.Subnets { |
| 73 | + if netAddr.IPNet.IP.To4() != nil { |
| 74 | + if ips.IPv4 == "" { |
| 75 | + ips.IPv4 = netAddr.IPNet.IP.String() |
| 76 | + } |
| 77 | + } else if ips.IPv6 == "" { |
| 78 | + ips.IPv6 = netAddr.IPNet.IP.String() |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + out[netName] = ips |
| 83 | + } |
| 84 | + return out |
| 85 | +} |
| 86 | + |
| 87 | +// sortedNetworkNames returns the keys of a pestoNetworkIPs map in sorted |
| 88 | +// order so that IP selection is deterministic across runs. |
| 89 | +func sortedNetworkNames(m map[string]pestoNetworkIPs) []string { |
| 90 | + names := make([]string, 0, len(m)) |
| 91 | + for name := range m { |
| 92 | + names = append(names, name) |
| 93 | + } |
| 94 | + sort.Strings(names) |
| 95 | + return names |
| 96 | +} |
| 97 | + |
| 98 | +// firstIPsWithNetwork picks the first IPv4 and IPv6 from the network IPs |
| 99 | +// map (sorted by network name), returning the IP and the owning network. |
| 100 | +func firstIPsWithNetwork(netIPs map[string]pestoNetworkIPs) (ipv4, ipv4Net, ipv6, ipv6Net string) { |
| 101 | + for _, name := range sortedNetworkNames(netIPs) { |
| 102 | + ips := netIPs[name] |
| 103 | + if ipv4 == "" && ips.IPv4 != "" { |
| 104 | + ipv4 = ips.IPv4 |
| 105 | + ipv4Net = name |
| 106 | + } |
| 107 | + if ipv6 == "" && ips.IPv6 != "" { |
| 108 | + ipv6 = ips.IPv6 |
| 109 | + ipv6Net = name |
| 110 | + } |
| 111 | + if ipv4 != "" && ipv6 != "" { |
| 112 | + break |
| 113 | + } |
| 114 | + } |
| 115 | + return |
| 116 | +} |
| 117 | + |
| 118 | +// firstIPv4FromState picks the first available IPv4 from the remaining |
| 119 | +// NetworkIPs in the state (sorted by network name for determinism). |
| 120 | +func firstIPv4FromState(state *pestoAddrState) (ip, network string) { |
| 121 | + for _, name := range sortedNetworkNames(state.NetworkIPs) { |
| 122 | + if v4 := state.NetworkIPs[name].IPv4; v4 != "" { |
| 123 | + return v4, name |
| 124 | + } |
| 125 | + } |
| 126 | + return "", "" |
| 127 | +} |
| 128 | + |
| 129 | +// firstIPv6FromState picks the first available IPv6 from the remaining |
| 130 | +// NetworkIPs in the state (sorted by network name for determinism). |
| 131 | +func firstIPv6FromState(state *pestoAddrState) (ip, network string) { |
| 132 | + for _, name := range sortedNetworkNames(state.NetworkIPs) { |
| 133 | + if v6 := state.NetworkIPs[name].IPv6; v6 != "" { |
| 134 | + return v6, name |
| 135 | + } |
| 136 | + } |
| 137 | + return "", "" |
| 138 | +} |
0 commit comments