Moved main to top, libs to subpackage

This commit is contained in:
Michael Bridgen committed 2014-10-04 14:51:46 +01:00
1 parent 1ff9f7e9b7
commit 3976e5ceaf
4 files changed

No files matched your search

+30
View File
@@ -0,0 +1,30 @@
package weavedns
import (
"fmt"
"log"
"net/http"
)
const (
UPDATE_PORT = 6785
)
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)
} else {
log.Printf("Invalid request: %s, %s, %s", name, addr, weave_ip)
}
})
address := fmt.Sprintf(":%d", UPDATE_PORT)
err := http.ListenAndServe(address, nil)
if err != nil {
log.Fatal("Unable to create http listener: ", err)
}
}
+134
View File
@@ -0,0 +1,134 @@
package weavedns
import (
"github.com/miekg/dns"
"log"
"net"
"sync"
)
// Portions of this code taken from github.com/armon/mdns
const (
ipv4mdns = "224.0.0.251" // link-local multicast address
mdnsPort = 5353 // mDNS assigned port
)
var (
ipv4Addr = &net.UDPAddr{
IP: net.ParseIP(ipv4mdns),
Port: mdnsPort,
}
)
type ResponseA struct {
Name string
Addr net.IP
}
type responseInfo struct {
ch chan<- *ResponseA
}
// Represents one query that we have sent for one name.
// If we, internally, get several requests for the same name while we have
// a query in flight, then we don't want to send more queries out.
type inflightQuery struct {
name string
Id uint16 // the DNS message ID
responseInfos []*responseInfo
// add timeout here ?
}
type MDNSClient struct {
sync.RWMutex
conn *net.UDPConn
addr *net.UDPAddr
inflight map[string]*inflightQuery
}
func NewMDNSClient() (*MDNSClient, error) {
conn, err := net.ListenUDP("udp4", &net.UDPAddr{IP: net.IPv4zero, Port: 0})
if err != nil {
log.Println(err)
return nil, err
}
retval := &MDNSClient{
conn: conn,
addr: ipv4Addr,
inflight: make(map[string]*inflightQuery)}
return retval, nil
}
func LinkLocalMulticastListener() (net.PacketConn, error) {
conn, err := net.ListenMulticastUDP("udp", nil, ipv4Addr)
return conn, err
}
func (c *MDNSClient) SendQuery(name string, querytype uint16, responseCh chan<- *ResponseA) error {
m := new(dns.Msg)
m.SetQuestion(name, querytype)
m.RecursionDesired = false
c.Lock()
query, found := c.inflight[name]
if !found {
query = &inflightQuery{name: name, Id: m.Id}
c.inflight[name] = query
}
info := &responseInfo{ch: responseCh}
query.responseInfos = append(query.responseInfos, info)
c.Unlock()
buf, err := m.Pack()
if err != nil {
return err
}
_, err = c.conn.WriteTo(buf, c.addr)
return err
}
func (c *MDNSClient) SendResponse(m *dns.Msg) error {
buf, err := m.Pack()
if err != nil {
return err
}
_, err = c.conn.WriteTo(buf, c.addr)
return err
}
func (c *MDNSClient) IsInflightQuery(m *dns.Msg) bool {
if len(m.Question) == 1 {
q := m.Question[0]
if q.Qtype == dns.TypeA {
c.Lock()
defer c.Unlock()
if query, found := c.inflight[q.Name]; found {
if query.Id == m.Id {
return true
}
}
}
}
return false
}
func (c *MDNSClient) HandleResponse(r *dns.Msg) {
for _, answer := range r.Answer {
switch rr := answer.(type) {
case *dns.A:
c.Lock()
if query, found := c.inflight[rr.Hdr.Name]; found {
for _, resp := range query.responseInfos {
resp.ch <- &ResponseA{Name: rr.Hdr.Name, Addr: rr.A}
}
// To be simple for now, assume this is the only response coming
delete(c.inflight, rr.Hdr.Name)
} else {
// We've received a response that didn't match a query
// Do we want to cache it?
}
c.Unlock()
}
}
}
+60
View File
@@ -0,0 +1,60 @@
package weavedns
import (
"github.com/miekg/dns"
"log"
"sync"
)
type Zone interface {
AddRecord(name string, ip string, weave_ip string) error
MatchLocal(name string) (string, error)
MatchGlobal(name string) (string, error)
}
type Record struct {
Name string
Ip string
WeaveIp string
}
type ZoneDb struct {
mx sync.RWMutex
recs []Record
}
type LookupError string
func (ops LookupError) Error() string {
return string(ops)
}
// Stop gap.
func (zone *ZoneDb) match(name string) (string, error) {
for _, r := range zone.recs {
log.Printf("%s == %s ?", r.Name, name)
if r.Name == name {
return r.WeaveIp, nil
}
}
return "", LookupError(name)
}
func (zone *ZoneDb) MatchLocal(name string) (string, error) {
zone.mx.Lock()
defer zone.mx.Unlock()
return zone.match(name)
}
func (zone *ZoneDb) MatchGlobal(name string) (string, 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})
return nil
}