Skip to content

Commit a582c71

Browse files
committed
Make interface gateway configuration optional.
1 parent e274a79 commit a582c71

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

Sources/Containerization/Interface.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@
1616

1717
/// A network interface.
1818
public protocol Interface: Sendable {
19+
/// The interface IPv4 address and subnet prefix length, as a CIDR address.
20+
/// Example: `192.168.64.3/24`
1921
var address: String { get }
20-
var gateway: String { get }
22+
23+
/// The IP address for the default route, or nil for no default route.
24+
var gateway: String? { get }
25+
26+
/// The interface MAC address, or nil to auto-configure the address.
2127
var macAddress: String? { get }
2228
}

Sources/Containerization/LinuxContainer.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,12 +500,14 @@ extension LinuxContainer {
500500
// For every interface asked for:
501501
// 1. Add the address requested
502502
// 2. Online the adapter
503-
// 3. Add the gateway address
503+
// 3. If a gateway IP address is present, add the default route.
504504
for (index, i) in self.interfaces.enumerated() {
505505
let name = "eth\(index)"
506506
try await agent.addressAdd(name: name, address: i.address)
507507
try await agent.up(name: name)
508-
try await agent.routeAddDefault(name: name, gateway: i.gateway)
508+
if let gateway = i.gateway {
509+
try await agent.routeAddDefault(name: name, gateway: gateway)
510+
}
509511
}
510512
if let dns = self.dns {
511513
try await agent.configureDNS(config: dns, location: rootfs.destination)

Sources/Containerization/NATInterface.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
public struct NATInterface: Interface {
1818
public var address: String
19-
public var gateway: String
19+
public var gateway: String?
2020
public var macAddress: String?
2121

2222
public init(address: String, gateway: String, macAddress: String? = nil) {

Sources/Containerization/NATNetworkInterface.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public final class NATNetworkInterface: Interface, Sendable {
3232

3333
}
3434

35-
public var gateway: String {
35+
public var gateway: String? {
3636
get { state.gateway }
3737
set { state.gateway = newValue }
3838
}
@@ -49,7 +49,7 @@ public final class NATNetworkInterface: Interface, Sendable {
4949

5050
private struct State {
5151
fileprivate var address: String
52-
fileprivate var gateway: String
52+
fileprivate var gateway: String?
5353
fileprivate var reference: vmnet_network_ref!
5454
fileprivate var macAddress: String?
5555
}

0 commit comments

Comments
 (0)