diff options
author | 2021-03-18 10:08:48 +0330 | |
---|---|---|
committer | 2021-03-18 07:38:48 +0100 | |
commit | 61b5cdb3526649fcc3356c71342a057f472a9985 (patch) | |
tree | 89461089a171f88b73ca8da1af1a6c3af533146d /plugin/bind/setup.go | |
parent | 5457cdcd4b2a37cdb6fa2e876dfe4b378c2f1fed (diff) | |
download | coredns-61b5cdb3526649fcc3356c71342a057f472a9985.tar.gz coredns-61b5cdb3526649fcc3356c71342a057f472a9985.tar.zst coredns-61b5cdb3526649fcc3356c71342a057f472a9985.zip |
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>
Diffstat (limited to 'plugin/bind/setup.go')
-rw-r--r-- | plugin/bind/setup.go | 38 |
1 files changed, 31 insertions, 7 deletions
diff --git a/plugin/bind/setup.go b/plugin/bind/setup.go index 44c9b8a6f..afca06097 100644 --- a/plugin/bind/setup.go +++ b/plugin/bind/setup.go @@ -15,16 +15,40 @@ func setup(c *caddy.Controller) error { // addresses will be consolidated over all BIND directives available in that BlocServer all := []string{} for c.Next() { - addrs := c.RemainingArgs() - if len(addrs) == 0 { - return plugin.Error("bind", fmt.Errorf("at least one address is expected")) + args := c.RemainingArgs() + if len(args) == 0 { + return plugin.Error("bind", fmt.Errorf("at least one address or interface name is expected")) } - for _, addr := range addrs { - if net.ParseIP(addr) == nil { - return plugin.Error("bind", fmt.Errorf("not a valid IP address: %s", addr)) + + 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) } } - all = append(all, addrs...) } config.ListenHosts = all return nil |