cifs volume resolves hostname correctly
<!--
Please make sure you've read and understood our contributing guidelines;
https://github.com/moby/moby/blob/master/CONTRIBUTING.md

** Make sure all your commits include a signature generated with `git commit -s` **

For additional information on our contributing process, read our contributing
guide https://docs.docker.com/opensource/code/

If this is a bug fix, make sure your description includes "fixes #xxxx", or
"closes #xxxx"

Please provide the following information:
-->

- fixes: https://github.com/moby/moby/issues/46180

**- What I did**
Fixed this issue https://github.com/moby/moby/issues/46180. There is a old PR which claims to fix this #39250, but is hasn't.

The problem is that, the hostname in NFS and CIFS mounts are at different places.

NFS uses the `addr` value in the `MountOpts`.
CIFS uses the hostname part of the `MountDevice` URL.

**- How I did it**

Splitted the switch case statement of #39250 for NFS and CIFS. Used the `net/url` `Parse` function to extract the hostname. And handled the rest in the same way NFS did.

**- How to verify it**
Create a docker volume using a hostname instead of an IP address.
Start a container using that volume.

**- Description for the changelog**
<!--
Write a short (one line) summary that describes the changes in this
pull request for inclusion in the changelog:
-->
CIFS volumes resolving FQDN correctly.

I am not a go hacker, so maybe it's not "go idiomatic".

volume/local/local_unix.go
//go:build linux || freebsd

// Package local provides the default implementation for volumes. It
// is used to mount data volume containers and directories local to
// the host server.
package local // import "github.com/docker/docker/volume/local"

import (
	"fmt"
	"net"
	"os"
	"strings"
	"syscall"
	"time"

	"github.com/docker/docker/errdefs"
	"github.com/docker/docker/quota"
	units "github.com/docker/go-units"
	"github.com/moby/sys/mount"
	"github.com/moby/sys/mountinfo"
	"github.com/pkg/errors"
)

var (
	validOpts = map[string]struct{}{
		"type":   {}, // specify the filesystem type for mount, e.g. nfs
		"o":      {}, // generic mount options
		"device": {}, // device to mount from
		"size":   {}, // quota size limit
	}
	mandatoryOpts = map[string][]string{
		"device": {"type"},
		"type":   {"device"},
		"o":      {"device", "type"},
	}
)

type optsConfig struct {
	MountType   string
	MountOpts   string
	MountDevice string
	Quota       quota.Quota
}

func (o *optsConfig) String() string {
	return fmt.Sprintf("type='%s' device='%s' o='%s' size='%d'", o.MountType, o.MountDevice, o.MountOpts, o.Quota.Size)
}

func (r *Root) validateOpts(opts map[string]string) error {
	if len(opts) == 0 {
		return nil
	}
	for opt := range opts {
		if _, ok := validOpts[opt]; !ok {
			return errdefs.InvalidParameter(errors.Errorf("invalid option: %q", opt))
		}
	}
	if val, ok := opts["size"]; ok {
		size, err := units.RAMInBytes(val)
		if err != nil {
			return errdefs.InvalidParameter(err)
		}
		if size > 0 && r.quotaCtl == nil {
			return errdefs.InvalidParameter(errors.New("quota size requested but no quota support"))
		}
	}
	for opt, reqopts := range mandatoryOpts {
		if _, ok := opts[opt]; ok {
			for _, reqopt := range reqopts {
				if _, ok := opts[reqopt]; !ok {
					return errdefs.InvalidParameter(errors.Errorf("missing required option: %q", reqopt))
				}
			}
		}
	}
	return nil
}

func (v *localVolume) setOpts(opts map[string]string) error {
	if len(opts) == 0 {
		return nil
	}
	v.opts = &optsConfig{
		MountType:   opts["type"],
		MountOpts:   opts["o"],
		MountDevice: opts["device"],
	}
	if val, ok := opts["size"]; ok {
		size, err := units.RAMInBytes(val)
		if err != nil {
			return errdefs.InvalidParameter(err)
		}
		if size > 0 && v.quotaCtl == nil {
			return errdefs.InvalidParameter(errors.New("quota size requested but no quota support"))
		}
		v.opts.Quota.Size = uint64(size)
	}
	return v.saveOpts()
}

func (v *localVolume) needsMount() bool {
	if v.opts == nil {
		return false
	}
	if v.opts.MountDevice != "" || v.opts.MountType != "" {
		return true
	}
	return false
}

func (v *localVolume) mount() error {
	if v.opts.MountDevice == "" {
		return fmt.Errorf("missing device in volume options")
	}
	mountOpts := v.opts.MountOpts
	switch v.opts.MountType {
	case "nfs", "cifs":
		if addrValue := getAddress(v.opts.MountOpts); addrValue != "" && net.ParseIP(addrValue).To4() == nil {
			ipAddr, err := net.ResolveIPAddr("ip", addrValue)
			if err != nil {
				return errors.Wrapf(err, "error resolving passed in network volume address")
			}
			mountOpts = strings.Replace(mountOpts, "addr="+addrValue, "addr="+ipAddr.String(), 1)
		}
	}
	if err := mount.Mount(v.opts.MountDevice, v.path, v.opts.MountType, mountOpts); err != nil {
		if password := getPassword(v.opts.MountOpts); password != "" {
			err = errors.New(strings.Replace(err.Error(), "password="+password, "password=********", 1))
		}
		return errors.Wrap(err, "failed to mount local volume")
	}
	return nil
}

func (v *localVolume) postMount() error {
	if v.opts == nil {
		return nil
	}
	if v.opts.Quota.Size > 0 {
		if v.quotaCtl != nil {
			return v.quotaCtl.SetQuota(v.path, v.opts.Quota)
		} else {
			return errors.New("size quota requested for volume but no quota support")
		}
	}
	return nil
}

func (v *localVolume) unmount() error {
	if v.needsMount() {
		if err := mount.Unmount(v.path); err != nil {
			if mounted, mErr := mountinfo.Mounted(v.path); mounted || mErr != nil {
				return errdefs.System(err)
			}
		}
		v.active.mounted = false
	}
	return nil
}

// restoreIfMounted restores the mounted status if the _data directory is already mounted.
func (v *localVolume) restoreIfMounted() error {
	if v.needsMount() {
		// Check if the _data directory is already mounted.
		mounted, err := mountinfo.Mounted(v.path)
		if err != nil {
			return fmt.Errorf("failed to determine if volume _data path is already mounted: %w", err)
		}

		if mounted {
			// Mark volume as mounted, but don't increment active count. If
			// any container needs this, the refcount will be incremented
			// by the live-restore (if enabled).
			// In other case, refcount will be zero but the volume will
			// already be considered as mounted when Mount is called, and
			// only the refcount will be incremented.
			v.active.mounted = true
		}
	}

	return nil
}

func (v *localVolume) CreatedAt() (time.Time, error) {
	fileInfo, err := os.Stat(v.rootPath)
	if err != nil {
		return time.Time{}, err
	}
	sec, nsec := fileInfo.Sys().(*syscall.Stat_t).Ctim.Unix()
	return time.Unix(sec, nsec), nil
}

//go:build linux || freebsd

// Package local provides the default implementation for volumes. It
// is used to mount data volume containers and directories local to
// the host server.
package local // import "github.com/docker/docker/volume/local"

import (
	"fmt"
	"net"
	"net/url"
	"os"
	"strings"
	"syscall"
	"time"

	"github.com/docker/docker/errdefs"
	"github.com/docker/docker/quota"
	units "github.com/docker/go-units"
	"github.com/moby/sys/mount"
	"github.com/moby/sys/mountinfo"
	"github.com/pkg/errors"
)

var (
	validOpts = map[string]struct{}{
		"type":   {}, // specify the filesystem type for mount, e.g. nfs
		"o":      {}, // generic mount options
		"device": {}, // device to mount from
		"size":   {}, // quota size limit
	}
	mandatoryOpts = map[string][]string{
		"device": {"type"},
		"type":   {"device"},
		"o":      {"device", "type"},
	}
)

type optsConfig struct {
	MountType   string
	MountOpts   string
	MountDevice string
	Quota       quota.Quota
}

func (o *optsConfig) String() string {
	return fmt.Sprintf("type='%s' device='%s' o='%s' size='%d'", o.MountType, o.MountDevice, o.MountOpts, o.Quota.Size)
}

func (r *Root) validateOpts(opts map[string]string) error {
	if len(opts) == 0 {
		return nil
	}
	for opt := range opts {
		if _, ok := validOpts[opt]; !ok {
			return errdefs.InvalidParameter(errors.Errorf("invalid option: %q", opt))
		}
	}
	if val, ok := opts["size"]; ok {
		size, err := units.RAMInBytes(val)
		if err != nil {
			return errdefs.InvalidParameter(err)
		}
		if size > 0 && r.quotaCtl == nil {
			return errdefs.InvalidParameter(errors.New("quota size requested but no quota support"))
		}
	}
	for opt, reqopts := range mandatoryOpts {
		if _, ok := opts[opt]; ok {
			for _, reqopt := range reqopts {
				if _, ok := opts[reqopt]; !ok {
					return errdefs.InvalidParameter(errors.Errorf("missing required option: %q", reqopt))
				}
			}
		}
	}
	return nil
}

func (v *localVolume) setOpts(opts map[string]string) error {
	if len(opts) == 0 {
		return nil
	}
	v.opts = &optsConfig{
		MountType:   opts["type"],
		MountOpts:   opts["o"],
		MountDevice: opts["device"],
	}
	if val, ok := opts["size"]; ok {
		size, err := units.RAMInBytes(val)
		if err != nil {
			return errdefs.InvalidParameter(err)
		}
		if size > 0 && v.quotaCtl == nil {
			return errdefs.InvalidParameter(errors.New("quota size requested but no quota support"))
		}
		v.opts.Quota.Size = uint64(size)
	}
	return v.saveOpts()
}

func (v *localVolume) needsMount() bool {
	if v.opts == nil {
		return false
	}
	if v.opts.MountDevice != "" || v.opts.MountType != "" {
		return true
	}
	return false
}

func (v *localVolume) mount() error {
	if v.opts.MountDevice == "" {
		return fmt.Errorf("missing device in volume options")
	}

	mountOpts := v.opts.MountOpts
	mountDevice := v.opts.MountDevice

	switch v.opts.MountType {
	case "nfs":
		if addrValue := getAddress(v.opts.MountOpts); addrValue != "" && net.ParseIP(addrValue).To4() == nil {
			ipAddr, err := net.ResolveIPAddr("ip", addrValue)
			if err != nil {
				return errors.Wrapf(err, "error resolving passed in network volume address")
			}
			mountOpts = strings.Replace(mountOpts, "addr="+addrValue, "addr="+ipAddr.String(), 1)
		}
	case "cifs":
		deviceURL, err := url.Parse(v.opts.MountDevice)
		if err != nil {
			return errors.Wrapf(err, "error parsing mount device url")
		}
		if deviceURL.Host != "" && net.ParseIP(deviceURL.Hostname()) == nil {
			ipAddr, err := net.ResolveIPAddr("ip", deviceURL.Hostname())
			if err != nil {
				return errors.Wrapf(err, "error resolving passed in network volume address")
			}
			if deviceURL.Port() != "" {
				deviceURL.Host = net.JoinHostPort(ipAddr.String(), deviceURL.Port())
			} else {
				deviceURL.Host = ipAddr.String()
			}
			mountDevice = deviceURL.String()
		}
	}
	if err := mount.Mount(mountDevice, v.path, v.opts.MountType, mountOpts); err != nil {
		if password := getPassword(v.opts.MountOpts); password != "" {
			err = errors.New(strings.Replace(err.Error(), "password="+password, "password=********", 1))
		}
		return errors.Wrap(err, "failed to mount local volume")
	}
	return nil
}

func (v *localVolume) postMount() error {
	if v.opts == nil {
		return nil
	}
	if v.opts.Quota.Size > 0 {
		if v.quotaCtl != nil {
			return v.quotaCtl.SetQuota(v.path, v.opts.Quota)
		} else {
			return errors.New("size quota requested for volume but no quota support")
		}
	}
	return nil
}

func (v *localVolume) unmount() error {
	if v.needsMount() {
		if err := mount.Unmount(v.path); err != nil {
			if mounted, mErr := mountinfo.Mounted(v.path); mounted || mErr != nil {
				return errdefs.System(err)
			}
		}
		v.active.mounted = false
	}
	return nil
}

// restoreIfMounted restores the mounted status if the _data directory is already mounted.
func (v *localVolume) restoreIfMounted() error {
	if v.needsMount() {
		// Check if the _data directory is already mounted.
		mounted, err := mountinfo.Mounted(v.path)
		if err != nil {
			return fmt.Errorf("failed to determine if volume _data path is already mounted: %w", err)
		}

		if mounted {
			// Mark volume as mounted, but don't increment active count. If
			// any container needs this, the refcount will be incremented
			// by the live-restore (if enabled).
			// In other case, refcount will be zero but the volume will
			// already be considered as mounted when Mount is called, and
			// only the refcount will be incremented.
			v.active.mounted = true
		}
	}

	return nil
}

func (v *localVolume) CreatedAt() (time.Time, error) {
	fileInfo, err := os.Stat(v.rootPath)
	if err != nil {
		return time.Time{}, err
	}
	sec, nsec := fileInfo.Sys().(*syscall.Stat_t).Ctim.Unix()
	return time.Unix(sec, nsec), nil
}

Looks like in the cifs case we should be able to resolve to an IPv6 address just fine. I just checked the `mount.cifs` userspace helper source and it seems to handle IPv6 addresses: https://github.com/Distrotech/cifs-utils/blob/0b0b81a533a8990d9a19e80b317a320c1d8a09d3/resolve_host.c#L37

The probable reason why the original code has the IPv4 restriction was to match the `mount.nfs` behavior which seems to only resolve IPv4 addresses (explicit `AF_INET` lookup): https://git.linux-nfs.org/?p=steved/nfs-utils.git;a=blob;f=utils/mount/network.c;h=01ead49f0008ecfaee3fba4671cc9192fa8f9b99;hb=HEAD#l268 (thanks @akerouanton  for finding the source!).

