Skip to content

Commit ff9f26e

Browse files
The127claude
andcommitted
feat: a run on a host without an IPv6 resolver gets no IPv6 nameserver
Co-Authored-By: Claude <noreply@anthropic.com> Signed-off-by: karo <karolin.kostial@gmail.com>
1 parent 08a56ac commit ff9f26e

2 files changed

Lines changed: 36 additions & 13 deletions

File tree

‎internal/builder/network.go‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,37 @@ import (
88
// mac is the builder's card's, 52:54:00 as QEMU's own and then mis.
99
const mac = "52:54:00:6d:69:73"
1010

11+
// Resolving is which address families the host resolves names in. QEMU
12+
// forwards a query only to a resolver of the query's own family.
13+
type Resolving struct {
14+
IPv4 bool
15+
IPv6 bool
16+
}
17+
1118
// Network is the builder VM's card on QEMU's user network and the network a
1219
// run gets through it, from one set of numbers so the two agree.
13-
func Network() (qemu.Card, protocol.Network) {
20+
func Network(resolving Resolving) (qemu.Card, protocol.Network) {
1421
ipv4 := qemu.Family{Prefix: "10.0.2.0/24", Gateway: "10.0.2.2", Nameserver: "10.0.2.3"}
1522
ipv6 := qemu.Family{Prefix: "fd6d:6973:6f00::/64", Gateway: "fd6d:6973:6f00::2", Nameserver: "fd6d:6973:6f00::3"}
1623

1724
card := qemu.Card{MAC: mac, IPv4: ipv4, IPv6: ipv6}
1825
network := protocol.Network{
1926
Card: mac,
20-
IPv4: run(ipv4, "10.0.2.15/24"),
21-
IPv6: run(ipv6, "fd6d:6973:6f00::15/64"),
27+
IPv4: run(ipv4, "10.0.2.15/24", true),
28+
IPv6: run(ipv6, "fd6d:6973:6f00::15/64", resolving.IPv6),
2229
}
2330

2431
return card, network
2532
}
2633

2734
// run is a run's side of one family of the card's network, with the address
28-
// given.
29-
func run(family qemu.Family, address string) protocol.Family {
30-
return protocol.Family{Address: address, Gateway: family.Gateway, Nameserver: family.Nameserver}
35+
// given. It gets the card's nameserver only where the host resolves, one
36+
// that forwards nowhere would cost every lookup a timeout.
37+
func run(family qemu.Family, address string, resolves bool) protocol.Family {
38+
run := protocol.Family{Address: address, Gateway: family.Gateway}
39+
if resolves {
40+
run.Nameserver = family.Nameserver
41+
}
42+
43+
return run
3144
}

‎internal/builder/network_test.go‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
func TestTheRunsLookForTheMACOfTheBuildersCard(t *testing.T) {
1414
// act
15-
card, network := builder.Network()
15+
card, network := builder.Network(builder.Resolving{IPv4: true, IPv6: true})
1616

1717
// assert
1818
require.NotEmpty(t, card.MAC)
@@ -21,7 +21,7 @@ func TestTheRunsLookForTheMACOfTheBuildersCard(t *testing.T) {
2121

2222
func TestARunGetsAnIPv4AddressOfItsOwnOnTheCardsNetwork(t *testing.T) {
2323
// act
24-
card, network := builder.Network()
24+
card, network := builder.Network(builder.Resolving{IPv4: true, IPv6: true})
2525

2626
// assert
2727
prefix, err := netip.ParsePrefix(card.IPv4.Prefix)
@@ -37,7 +37,7 @@ func TestARunGetsAnIPv4AddressOfItsOwnOnTheCardsNetwork(t *testing.T) {
3737

3838
func TestARunGoesOutThroughTheIPv4GatewayOfTheCardsNetwork(t *testing.T) {
3939
// act
40-
card, network := builder.Network()
40+
card, network := builder.Network(builder.Resolving{IPv4: true, IPv6: true})
4141

4242
// assert
4343
prefix, err := netip.ParsePrefix(card.IPv4.Prefix)
@@ -50,7 +50,7 @@ func TestARunGoesOutThroughTheIPv4GatewayOfTheCardsNetwork(t *testing.T) {
5050

5151
func TestARunResolvesThroughTheIPv4NameserverOfTheCardsNetwork(t *testing.T) {
5252
// act
53-
card, network := builder.Network()
53+
card, network := builder.Network(builder.Resolving{IPv4: true, IPv6: true})
5454

5555
// assert
5656
prefix, err := netip.ParsePrefix(card.IPv4.Prefix)
@@ -63,7 +63,7 @@ func TestARunResolvesThroughTheIPv4NameserverOfTheCardsNetwork(t *testing.T) {
6363

6464
func TestARunGetsAnIPv6AddressOfItsOwnOnTheCardsNetwork(t *testing.T) {
6565
// act
66-
card, network := builder.Network()
66+
card, network := builder.Network(builder.Resolving{IPv4: true, IPv6: true})
6767

6868
// assert
6969
prefix, err := netip.ParsePrefix(card.IPv6.Prefix)
@@ -79,7 +79,7 @@ func TestARunGetsAnIPv6AddressOfItsOwnOnTheCardsNetwork(t *testing.T) {
7979

8080
func TestARunGoesOutThroughTheIPv6GatewayOfTheCardsNetwork(t *testing.T) {
8181
// act
82-
card, network := builder.Network()
82+
card, network := builder.Network(builder.Resolving{IPv4: true, IPv6: true})
8383

8484
// assert
8585
prefix, err := netip.ParsePrefix(card.IPv6.Prefix)
@@ -92,7 +92,7 @@ func TestARunGoesOutThroughTheIPv6GatewayOfTheCardsNetwork(t *testing.T) {
9292

9393
func TestARunResolvesThroughTheIPv6NameserverOfTheCardsNetwork(t *testing.T) {
9494
// act
95-
card, network := builder.Network()
95+
card, network := builder.Network(builder.Resolving{IPv4: true, IPv6: true})
9696

9797
// assert
9898
prefix, err := netip.ParsePrefix(card.IPv6.Prefix)
@@ -102,3 +102,13 @@ func TestARunResolvesThroughTheIPv6NameserverOfTheCardsNetwork(t *testing.T) {
102102
assert.True(t, prefix.Contains(nameserver))
103103
assert.Equal(t, card.IPv6.Nameserver, network.IPv6.Nameserver)
104104
}
105+
106+
func TestARunOnAHostThatResolvesOnlyOverIPv4GetsNoIPv6Nameserver(t *testing.T) {
107+
// act
108+
card, network := builder.Network(builder.Resolving{IPv4: true})
109+
110+
// assert
111+
assert.NotEmpty(t, card.IPv6.Nameserver)
112+
assert.Empty(t, network.IPv6.Nameserver)
113+
assert.Equal(t, card.IPv4.Nameserver, network.IPv4.Nameserver)
114+
}

0 commit comments

Comments
 (0)