aboutsummaryrefslogtreecommitdiff
path: root/plugin/bind/setup.go
blob: d75d6c0ff1f258040b51f119c498b8e7fdae560b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package bind

import (
	"fmt"
	"net"

	"github.com/coredns/caddy"
	"github.com/coredns/coredns/core/dnsserver"
	"github.com/coredns/coredns/plugin"
)

func setup(c *caddy.Controller) error {
	config := dnsserver.GetConfig(c)

	// addresses will be consolidated over all BIND directives available in that BlocServer
	all := []string{}
	for c.Next() {
		args := c.RemainingArgs()
		if len(args) == 0 {
			return plugin.Error("bind", fmt.Errorf("at least one address or interface name is expected"))
		}

		ifaces, err := net.Interfaces()
		if err != nil {
			return plugin.Error("bind", fmt.Errorf("failed to get interfaces list"))
		}

		var isIface bool
		for _, arg := range args {
			isIface = false
			for _, iface := range ifaces {
				if arg == iface.Name {
					isIface = true
					addrs, err := iface.Addrs()
					if err != nil {
						return plugin.Error("bind", fmt.Errorf("failed to get the IP addresses of the interface: %q", arg))
					}
					for _, addr := range addrs {
						if ipnet, ok := addr.(*net.IPNet); ok {
							if ipnet.IP.To4() != nil || (!ipnet.IP.IsLinkLocalMulticast() && !ipnet.IP.IsLinkLocalUnicast()) {
								all = append(all, ipnet.IP.String())
							}
						}
					}
				}
			}
			if !isIface {
				if net.ParseIP(arg) == nil {
					return plugin.Error("bind", fmt.Errorf("not a valid IP address or interface name: %q", arg))
				}
				all = append(all, arg)
			}
		}
	}
	config.ListenHosts = all
	return nil
}