coredns/plugin/bind/setup.go
Mohammad Yosefpor 61b5cdb352
plugin/bind: Bind by interface name (#4522)
* auto make -f Makefile.doc

Signed-off-by: Mohammad Yosefpor <myusefpur@gmail.com>

* Bind by interface name

Signed-off-by: Mohammad Yosefpor <myusefpur@gmail.com>

* README.md: Interface with multiple address

Signed-off-by: Mohammad Yosefpor <myusefpur@gmail.com>

* auto make -f Makefile.doc

Signed-off-by: Mohammad Yosefpor <myusefpur@gmail.com>

* auto make -f Makefile.doc

Signed-off-by: Mohammad Yosefpor <myusefpur@gmail.com>

* Elaborate more on the behaviour in README.md, revert man/*, fix tests

Signed-off-by: Mohammad Yosefpor <myusefpur@gmail.com>

* auto make -f Makefile.doc

Signed-off-by: Mohammad Yosefpor <myusefpur@gmail.com>

* --sign-off

Revert man/* to fix DCO check

Signed-off-by: Mohammad Yosefpor <myusefpur@gmail.com>

* auto make -f Makefile.doc

* Revert man/* to fix DCO check

Signed-off-by: Mohammad Yosefpor <myusefpur@gmail.com>

Co-authored-by: coredns-auto-go-mod-tidy[bot] <coredns-auto-go-mod-tidy[bot]@users.noreply.github.com>
2021-03-18 07:38:48 +01:00

55 lines
1.3 KiB
Go

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(s) of the interface: %s", arg))
}
for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok {
all = append(all, ipnet.IP.String())
}
}
}
}
if !isIface {
if net.ParseIP(arg) == nil {
return plugin.Error("bind", fmt.Errorf("not a valid IP address: %s", arg))
}
all = append(all, arg)
}
}
}
config.ListenHosts = all
return nil
}