Changed to accept a CIDR address and use net.IP

* In general we'll have an IP address and a subnet mask for the weave
   network address; account for this in the zone DB and in the HTTP
   server.

 * Use the net.IP type rather than string, which gives us a bit more
   generality (in theory it'll deal with Ipv6 too) and validation (we
   attempt to parse the input)
This commit is contained in:
Michael Bridgen committed 2014-10-07 12:49:52 +01:00
1 parent e7ac6dafb3
commit e8e86dadc0
3 files changed
+27 -23

No files matched your search

+2 -2
View File
@@ -38,7 +38,7 @@ func handleMDNS(w dns.ResponseWriter, r *dns.Msg) {
q := r.Question[0]
ip, err := zone.MatchLocal(q.Name)
if err == nil {
m := makeDNSReply(r, q.Name, net.ParseIP(ip))
m := makeDNSReply(r, q.Name, ip)
mdnsClient.SendResponse(m)
} else if mdnsClient.IsInflightQuery(r) {
// ignore this - it's our own query received via multicast
@@ -53,7 +53,7 @@ func handleLocal(w dns.ResponseWriter, r *dns.Msg) {
q := r.Question[0]
ip, err := zone.MatchLocal(q.Name)
if err == nil {
m := makeDNSReply(r, q.Name, net.ParseIP(ip))
m := makeDNSReply(r, q.Name, ip)
w.WriteMsg(m)
} else {
log.Printf("Failed lookup for %s; sending mDNS query", q.Name)
+14 -5
View File
@@ -3,6 +3,7 @@ package weavedns
import (
"fmt"
"log"
"net"
"net/http"
)
@@ -14,12 +15,20 @@ func ListenHttp(db Zone) {
http.HandleFunc("/add", func(w http.ResponseWriter, r *http.Request) {
name := r.FormValue("name")
addr := r.FormValue("ip")
weave_ip := r.FormValue("weave_ip")
if name != "" && addr != "" && weave_ip != "" {
log.Printf("Adding %s, %s, %s", name, addr, weave_ip)
db.AddRecord(name, addr, weave_ip)
weave_cidr := r.FormValue("weave_cidr")
if name != "" && addr != "" && weave_cidr != "" {
ip := net.ParseIP(addr)
weave_ip, subnet, err := net.ParseCIDR(weave_cidr)
if err == nil && ip != nil {
log.Printf("Adding %s (%s) -> %s", name, addr, weave_cidr)
db.AddRecord(name, ip, weave_ip, subnet)
} else if err != nil {
log.Printf("Invalid CIDR in request: %s", weave_cidr)
} else {
log.Printf("Invalid IP in request: %s", addr)
}
} else {
log.Printf("Invalid request: %s, %s, %s", name, addr, weave_ip)
log.Printf("Invalid request: %s, %s, %s", name, addr, weave_cidr)
}
})
address := fmt.Sprintf(":%d", UPDATE_PORT)
+11 -16
View File
@@ -3,19 +3,20 @@ package weavedns
import (
"github.com/miekg/dns"
"log"
"net"
"sync"
)
type Zone interface {
AddRecord(name string, ip string, weave_ip string) error
MatchLocal(name string) (string, error)
MatchGlobal(name string) (string, error)
AddRecord(string, net.IP, net.IP, *net.IPNet) error
MatchLocal(string) (net.IP, error)
}
type Record struct {
Name string
Ip string
WeaveIp string
Ip net.IP
WeaveIp net.IP
Subnet *net.IPNet
}
type ZoneDb struct {
@@ -30,31 +31,25 @@ func (ops LookupError) Error() string {
}
// Stop gap.
func (zone *ZoneDb) match(name string) (string, error) {
func (zone *ZoneDb) match(name string) (net.IP, error) {
for _, r := range zone.recs {
log.Printf("%s == %s ?", r.Name, name)
if r.Name == name {
return r.WeaveIp, nil
}
}
return "", LookupError(name)
return nil, LookupError(name)
}
func (zone *ZoneDb) MatchLocal(name string) (string, error) {
func (zone *ZoneDb) MatchLocal(name string) (net.IP, error) {
zone.mx.Lock()
defer zone.mx.Unlock()
return zone.match(name)
}
func (zone *ZoneDb) MatchGlobal(name string) (string, error) {
func (zone *ZoneDb) AddRecord(name string, ip net.IP, weave_ip net.IP, weave_subnet *net.IPNet) error {
zone.mx.Lock()
defer zone.mx.Unlock()
return zone.match(name)
}
func (zone *ZoneDb) AddRecord(name string, ip string, weave_ip string) error {
zone.mx.Lock()
defer zone.mx.Unlock()
zone.recs = append(zone.recs, Record{dns.Fqdn(name), ip, weave_ip})
zone.recs = append(zone.recs, Record{dns.Fqdn(name), ip, weave_ip, weave_subnet})
return nil
}