Browse code

libnetwork/types: define IPFamily type for IP-family consts

Define a type to help discovery, and update the signatures of
`ResolveName`, `Network.ResolveName`, and `Sandbox.ResolveName`
accordingly.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2025/08/02 03:54:45
Showing 5 changed files
... ...
@@ -1930,7 +1930,7 @@ func (n *Network) hasLoadBalancerEndpoint() bool {
1930 1930
 // Returns (addresses, true) if req is found, but len(addresses) may be 0 if
1931 1931
 // there are no addresses of ipType. If the name is not found, the bool return
1932 1932
 // will be false.
1933
-func (n *Network) ResolveName(ctx context.Context, req string, ipType int) ([]net.IP, bool) {
1933
+func (n *Network) ResolveName(ctx context.Context, req string, ipType types.IPFamily) ([]net.IP, bool) {
1934 1934
 	c := n.getController()
1935 1935
 	networkID := n.ID()
1936 1936
 
... ...
@@ -33,7 +33,7 @@ type DNSBackend interface {
33 33
 	// the networks the sandbox is connected to. The second return value will be
34 34
 	// true if the name exists in docker domain, even if there are no addresses of
35 35
 	// the required type. Such queries shouldn't be forwarded to external nameservers.
36
-	ResolveName(ctx context.Context, name string, ipType int) ([]net.IP, bool)
36
+	ResolveName(ctx context.Context, name string, ipType types.IPFamily) ([]net.IP, bool)
37 37
 	// ResolveIP returns the service name for the passed in IP. IP is in reverse dotted
38 38
 	// notation; the format used for DNS PTR records
39 39
 	ResolveIP(ctx context.Context, name string) string
... ...
@@ -310,7 +310,7 @@ func (r *Resolver) handleMXQuery(ctx context.Context, query *dns.Msg) (*dns.Msg,
310 310
 	return resp, nil
311 311
 }
312 312
 
313
-func (r *Resolver) handleIPQuery(ctx context.Context, query *dns.Msg, ipType int) (*dns.Msg, error) {
313
+func (r *Resolver) handleIPQuery(ctx context.Context, query *dns.Msg, ipType types.IPFamily) (*dns.Msg, error) {
314 314
 	name := query.Question[0].Name
315 315
 	addr, ok := r.backend.ResolveName(ctx, name, ipType)
316 316
 	if !ok {
... ...
@@ -11,6 +11,7 @@ import (
11 11
 
12 12
 	"github.com/containerd/log"
13 13
 	"github.com/miekg/dns"
14
+	"github.com/moby/moby/v2/daemon/libnetwork/types"
14 15
 	"github.com/moby/moby/v2/internal/testutils/netnsutils"
15 16
 	"github.com/sirupsen/logrus"
16 17
 	"gotest.tools/v3/assert"
... ...
@@ -246,7 +247,7 @@ func (w tlogWriter) Write(p []byte) (int, error) {
246 246
 
247 247
 type noopDNSBackend struct{ DNSBackend }
248 248
 
249
-func (noopDNSBackend) ResolveName(_ context.Context, name string, ipType int) ([]net.IP, bool) {
249
+func (noopDNSBackend) ResolveName(_ context.Context, name string, ipType types.IPFamily) ([]net.IP, bool) {
250 250
 	return nil, false
251 251
 }
252 252
 
... ...
@@ -395,7 +395,7 @@ func (sb *Sandbox) ResolveService(ctx context.Context, name string) ([]*net.SRV,
395 395
 	return nil, nil
396 396
 }
397 397
 
398
-func (sb *Sandbox) ResolveName(ctx context.Context, name string, ipType int) ([]net.IP, bool) {
398
+func (sb *Sandbox) ResolveName(ctx context.Context, name string, ipType types.IPFamily) ([]net.IP, bool) {
399 399
 	// Embedded server owns the docker network domain. Resolution should work
400 400
 	// for both container_name and container_name.network_name
401 401
 	// We allow '.' in service name and network name. For a name a.b.c.d the
... ...
@@ -452,12 +452,12 @@ func (sb *Sandbox) ResolveName(ctx context.Context, name string, ipType int) ([]
452 452
 	return nil, false
453 453
 }
454 454
 
455
-func (sb *Sandbox) resolveName(ctx context.Context, nameOrAlias string, networkName string, epList []*Endpoint, lookupAlias bool, ipType int) ([]net.IP, bool) {
455
+func (sb *Sandbox) resolveName(ctx context.Context, nameOrAlias string, networkName string, epList []*Endpoint, lookupAlias bool, ipType types.IPFamily) ([]net.IP, bool) {
456 456
 	ctx, span := otel.Tracer("").Start(ctx, "Sandbox.resolveName", trace.WithAttributes(
457 457
 		attribute.String("libnet.resolver.name-or-alias", nameOrAlias),
458 458
 		attribute.String("libnet.network.name", networkName),
459 459
 		attribute.Bool("libnet.resolver.alias-lookup", lookupAlias),
460
-		attribute.Int("libnet.resolver.ip-family", ipType)))
460
+		attribute.Int("libnet.resolver.ip-family", int(ipType))))
461 461
 	defer span.End()
462 462
 
463 463
 	for _, ep := range epList {
... ...
@@ -12,11 +12,13 @@ import (
12 12
 	"github.com/moby/moby/v2/errdefs"
13 13
 )
14 14
 
15
-// constants for the IP address type.
15
+// IPFamily specify IP address type(s).
16
+type IPFamily int
17
+
16 18
 const (
17
-	IP = iota // IPv4 and IPv6
18
-	IPv4
19
-	IPv6
19
+	IP   IPFamily = iota // Either IPv4 or IPv6.
20
+	IPv4                 // Internet Protocol version 4 (IPv4).
21
+	IPv6                 // Internet Protocol version 6 (IPv6).
20 22
 )
21 23
 
22 24
 // EncryptionKey is the libnetwork representation of the key distributed by the lead