Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1225 from weaveworks/21-dns-listen-addr
Browse files Browse the repository at this point in the history
Allow users to set dns listen address
  • Loading branch information
awh committed Jul 29, 2015
2 parents 7735814 + db6ee99 commit 2d450ac
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
24 changes: 12 additions & 12 deletions nameserver/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,31 @@ const (
udpBuffSize = uint16(4096)
minUDPSize = 512

DefaultPort = 53
DefaultListenAddress = "0.0.0.0:53"
DefaultTTL = 1
DefaultClientTimeout = 5 * time.Second
)

type DNSServer struct {
ns *Nameserver
domain string
ttl uint32
port int
ns *Nameserver
domain string
ttl uint32
address string

servers []*dns.Server
upstream *dns.ClientConfig
tcpClient *dns.Client
udpClient *dns.Client
}

func NewDNSServer(ns *Nameserver, domain string, port int, ttl uint32,
func NewDNSServer(ns *Nameserver, domain string, address string, ttl uint32,
clientTimeout time.Duration) (*DNSServer, error) {

s := &DNSServer{
ns: ns,
domain: dns.Fqdn(domain),
ttl: ttl,
port: port,
address: address,
tcpClient: &dns.Client{Net: "tcp", ReadTimeout: clientTimeout},
udpClient: &dns.Client{Net: "udp", ReadTimeout: clientTimeout, UDPSize: udpBuffSize},
}
Expand All @@ -54,26 +54,26 @@ func NewDNSServer(ns *Nameserver, domain string, port int, ttl uint32,
return nil, err
}

err = s.listen(port)
err = s.listen(address)
return s, err
}

func (d *DNSServer) String() string {
var buf bytes.Buffer
fmt.Fprintf(&buf, "WeaveDNS (%s)\n", d.ns.ourName)
fmt.Fprintf(&buf, " listening on port %d, for domain %s\n", d.port, d.domain)
fmt.Fprintf(&buf, " listening on %s, for domain %s\n", d.address, d.domain)
fmt.Fprintf(&buf, " response ttl %d\n", d.ttl)
return buf.String()
}

func (d *DNSServer) listen(port int) error {
udpListener, err := net.ListenPacket("udp", fmt.Sprintf(":%d", port))
func (d *DNSServer) listen(address string) error {
udpListener, err := net.ListenPacket("udp", address)
if err != nil {
return err
}
udpServer := &dns.Server{PacketConn: udpListener, Handler: d.createMux(d.udpClient, minUDPSize)}

tcpListener, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
tcpListener, err := net.Listen("tcp", address)
if err != nil {
udpServer.Shutdown()
return err
Expand Down
2 changes: 1 addition & 1 deletion nameserver/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestTruncation(t *testing.T) {
peername, err := router.PeerNameFromString("00:00:00:02:00:00")
require.Nil(t, err)
nameserver := New(peername, nil, nil, "")
dnsserver, err := NewDNSServer(nameserver, "weave.local.", 0, 30, 5*time.Second)
dnsserver, err := NewDNSServer(nameserver, "weave.local.", "0.0.0.0:0", 30, 5*time.Second)
require.Nil(t, err)
udpPort := dnsserver.servers[0].PacketConn.LocalAddr().(*net.UDPAddr).Port
tcpPort := dnsserver.servers[1].Listener.Addr().(*net.TCPAddr).Port
Expand Down
6 changes: 3 additions & 3 deletions prog/weaver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func main() {
peers []string
noDNS bool
dnsDomain string
dnsPort int
dnsListenAddress string
dnsTTL int
dnsClientTimeout time.Duration
)
Expand All @@ -83,7 +83,7 @@ func main() {
mflag.StringVar(&apiPath, []string{"#api", "-api"}, "unix:///var/run/docker.sock", "Path to Docker API socket")
mflag.BoolVar(&noDNS, []string{"-no-dns"}, false, "disable DNS server")
mflag.StringVar(&dnsDomain, []string{"-dns-domain"}, nameserver.DefaultDomain, "local domain to server requests for")
mflag.IntVar(&dnsPort, []string{"-dns-port"}, nameserver.DefaultPort, "port to listen on for DNS requests")
mflag.StringVar(&dnsListenAddress, []string{"-dns-listen-address"}, nameserver.DefaultListenAddress, "address to listen on for DNS requests")
mflag.IntVar(&dnsTTL, []string{"-dns-ttl"}, nameserver.DefaultTTL, "TTL for DNS request from our domain")
mflag.DurationVar(&dnsClientTimeout, []string{"-dns-fallback-timeout"}, nameserver.DefaultClientTimeout, "timeout for fallback DNS requests")

Expand Down Expand Up @@ -183,7 +183,7 @@ func main() {
}
ns.Start()
defer ns.Stop()
dnsserver, err = nameserver.NewDNSServer(ns, dnsDomain, dnsPort, uint32(dnsTTL), dnsClientTimeout)
dnsserver, err = nameserver.NewDNSServer(ns, dnsDomain, dnsListenAddress, uint32(dnsTTL), dnsClientTimeout)
if err != nil {
Log.Fatal("Unable to start dns server: ", err)
}
Expand Down

0 comments on commit 2d450ac

Please sign in to comment.