From 98b509e5fda47da662b9d58b5abc5e386384e3c2 Mon Sep 17 00:00:00 2001 From: John Logan Date: Thu, 26 Jun 2025 19:37:38 -0700 Subject: [PATCH] Make interface gateway configuration optional. --- Sources/Containerization/Interface.swift | 8 +++++++- Sources/Containerization/LinuxContainer.swift | 6 ++++-- Sources/Containerization/NATInterface.swift | 4 ++-- Sources/Containerization/NATNetworkInterface.swift | 8 ++++---- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/Sources/Containerization/Interface.swift b/Sources/Containerization/Interface.swift index 77c2867e..ebe56a43 100644 --- a/Sources/Containerization/Interface.swift +++ b/Sources/Containerization/Interface.swift @@ -16,7 +16,13 @@ /// A network interface. public protocol Interface: Sendable { + /// The interface IPv4 address and subnet prefix length, as a CIDR address. + /// Example: `192.168.64.3/24` var address: String { get } - var gateway: String { get } + + /// The IP address for the default route, or nil for no default route. + var gateway: String? { get } + + /// The interface MAC address, or nil to auto-configure the address. var macAddress: String? { get } } diff --git a/Sources/Containerization/LinuxContainer.swift b/Sources/Containerization/LinuxContainer.swift index dea41f7d..141c3057 100644 --- a/Sources/Containerization/LinuxContainer.swift +++ b/Sources/Containerization/LinuxContainer.swift @@ -500,12 +500,14 @@ extension LinuxContainer { // For every interface asked for: // 1. Add the address requested // 2. Online the adapter - // 3. Add the gateway address + // 3. If a gateway IP address is present, add the default route. for (index, i) in self.interfaces.enumerated() { let name = "eth\(index)" try await agent.addressAdd(name: name, address: i.address) try await agent.up(name: name) - try await agent.routeAddDefault(name: name, gateway: i.gateway) + if let gateway = i.gateway { + try await agent.routeAddDefault(name: name, gateway: gateway) + } } if let dns = self.dns { try await agent.configureDNS(config: dns, location: rootfs.destination) diff --git a/Sources/Containerization/NATInterface.swift b/Sources/Containerization/NATInterface.swift index fbcbd838..cba24401 100644 --- a/Sources/Containerization/NATInterface.swift +++ b/Sources/Containerization/NATInterface.swift @@ -16,10 +16,10 @@ public struct NATInterface: Interface { public var address: String - public var gateway: String + public var gateway: String? public var macAddress: String? - public init(address: String, gateway: String, macAddress: String? = nil) { + public init(address: String, gateway: String?, macAddress: String? = nil) { self.address = address self.gateway = gateway self.macAddress = macAddress diff --git a/Sources/Containerization/NATNetworkInterface.swift b/Sources/Containerization/NATNetworkInterface.swift index ed5e833c..b90c0932 100644 --- a/Sources/Containerization/NATNetworkInterface.swift +++ b/Sources/Containerization/NATNetworkInterface.swift @@ -32,7 +32,7 @@ public final class NATNetworkInterface: Interface, Sendable { } - public var gateway: String { + public var gateway: String? { get { state.gateway } set { state.gateway = newValue } } @@ -49,7 +49,7 @@ public final class NATNetworkInterface: Interface, Sendable { private struct State { fileprivate var address: String - fileprivate var gateway: String + fileprivate var gateway: String? fileprivate var reference: vmnet_network_ref! fileprivate var macAddress: String? } @@ -60,7 +60,7 @@ public final class NATNetworkInterface: Interface, Sendable { @available(macOS 26, *) public init( address: String, - gateway: String, + gateway: String?, reference: sending vmnet_network_ref, macAddress: String? = nil ) { @@ -75,7 +75,7 @@ public final class NATNetworkInterface: Interface, Sendable { @available(macOS, obsoleted: 26, message: "Use init(address:gateway:reference:macAddress:) instead") public init( address: String, - gateway: String, + gateway: String?, macAddress: String? = nil ) { self.state = .init(