Skip to content

Commit 17b9885

Browse files
torarnvcurdbecker
andcommitted
vminitd: Fall back to /proc/net/pnp for unset nameservers or domain
When configureDns is called with an empty nameservers list or no domain, read /proc/net/pnp (written by the kernel IP_PNP DHCP client) and use any nameserver and domain lines found there. The two are filled in independently, so an explicit nameserver does not prevent the domain from being read from pnp. This provides automatic DNS configuration for bridge-mode containers without a new RPC or proto change. Co-authored-by: Curd Becker <me@curd-becker.de>
1 parent 6bd7d11 commit 17b9885

1 file changed

Lines changed: 21 additions & 3 deletions

File tree

vminitd/Sources/VminitdCore/Server+GRPC.swift

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,13 +1427,12 @@ extension Initd: Com_Apple_Containerization_Sandbox_V3_SandboxContext.SimpleServ
14271427
request: Com_Apple_Containerization_Sandbox_V3_ConfigureDnsRequest,
14281428
context: GRPCCore.ServerContext
14291429
) async throws -> Com_Apple_Containerization_Sandbox_V3_ConfigureDnsResponse {
1430-
let domain = request.hasDomain ? request.domain : nil
14311430
log.debug(
14321431
"configureDns",
14331432
metadata: [
14341433
"location": "\(request.location)",
14351434
"nameservers": "\(request.nameservers)",
1436-
"domain": "\(domain ?? "")",
1435+
"domain": "\(request.hasDomain ? request.domain : "")",
14371436
"searchDomains": "\(request.searchDomains)",
14381437
"options": "\(request.options)",
14391438
])
@@ -1442,8 +1441,27 @@ extension Initd: Com_Apple_Containerization_Sandbox_V3_SandboxContext.SimpleServ
14421441
let etc = URL(fileURLWithPath: request.location).appendingPathComponent("etc")
14431442
try FileManager.default.createDirectory(atPath: etc.path, withIntermediateDirectories: true)
14441443
let resolvConf = etc.appendingPathComponent("resolv.conf")
1444+
var nameservers = request.nameservers
1445+
var domain = request.hasDomain ? request.domain : nil
1446+
if nameservers.isEmpty || domain == nil,
1447+
let pnp = try? String(contentsOfFile: "/proc/net/pnp", encoding: .utf8)
1448+
{
1449+
let lines = pnp.split(separator: "\n")
1450+
if nameservers.isEmpty {
1451+
nameservers =
1452+
lines
1453+
.filter { $0.hasPrefix("nameserver") }
1454+
.compactMap { $0.split(separator: " ").dropFirst().first.map(String.init) }
1455+
}
1456+
if domain == nil {
1457+
domain =
1458+
lines
1459+
.first { $0.hasPrefix("domain") }
1460+
.flatMap { $0.split(separator: " ").dropFirst().first.map(String.init) }
1461+
}
1462+
}
14451463
let config = DNS(
1446-
nameservers: request.nameservers,
1464+
nameservers: nameservers,
14471465
domain: domain,
14481466
searchDomains: request.searchDomains,
14491467
options: request.options

0 commit comments

Comments
 (0)