Skip to content

Commit ae69e9c

Browse files
committed
Make interface gateway configuration optional.
1 parent c7763c7 commit ae69e9c

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
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

22-
public init(address: String, gateway: String, macAddress: String? = nil) {
22+
public init(address: String, gateway: String?, macAddress: String? = nil) {
2323
self.address = address
2424
self.gateway = gateway
2525
self.macAddress = macAddress

Sources/Containerization/NATNetworkInterface.swift

Lines changed: 4 additions & 4 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
}
@@ -60,7 +60,7 @@ public final class NATNetworkInterface: Interface, Sendable {
6060
@available(macOS 26, *)
6161
public init(
6262
address: String,
63-
gateway: String,
63+
gateway: String?,
6464
reference: sending vmnet_network_ref,
6565
macAddress: String? = nil
6666
) {
@@ -75,7 +75,7 @@ public final class NATNetworkInterface: Interface, Sendable {
7575
@available(macOS, obsoleted: 26, message: "Use init(address:gateway:reference:macAddress:) instead")
7676
public init(
7777
address: String,
78-
gateway: String,
78+
gateway: String?,
7979
macAddress: String? = nil
8080
) {
8181
self.state = .init(

0 commit comments

Comments
 (0)