Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Sources/Containerization/Interface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
6 changes: 4 additions & 2 deletions Sources/Containerization/LinuxContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions Sources/Containerization/NATInterface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions Sources/Containerization/NATNetworkInterface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
Expand All @@ -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?
}
Expand All @@ -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
) {
Expand All @@ -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(
Expand Down