Pārlūkot izejas kodu

Support NS records

Ask Bjørn Hansen 13 gadi atpakaļ
vecāks
revīzija
7ef789bb11
2 mainītis faili ar 40 papildinājumiem un 29 dzēšanām
  1. 38 28
      geodns.go
  2. 2 1
      serve.go

+ 38 - 28
geodns.go

@@ -104,6 +104,7 @@ func setupZoneData(data map[string]interface{}, Zone *Zone, Options *Options) {
 	var recordTypes = map[string]uint16{
 		"a":    dns.TypeA,
 		"aaaa": dns.TypeAAAA,
+		"ns":   dns.TypeNS,
 	}
 
 	for dk, dv := range data {
@@ -112,15 +113,17 @@ func setupZoneData(data map[string]interface{}, Zone *Zone, Options *Options) {
 
 		Zone.Labels[dk] = new(Label)
 		label := Zone.Labels[dk]
-		//make([]Server, len(Records))
+
+		// BUG(ask) Read 'ttl' value in label data
 
 		for rType, dnsType := range recordTypes {
+
 			fmt.Println(rType, dnsType)
 
 			var rdata = dv.(map[string]interface{})[rType]
 
 			if rdata == nil {
-				fmt.Printf("No %s records for label %s", rType, dk)
+				fmt.Printf("No %s records for label %s\n", rType, dk)
 				continue
 			}
 
@@ -139,47 +142,54 @@ func setupZoneData(data map[string]interface{}, Zone *Zone, Options *Options) {
 			label.Records[dnsType] = make([]Record, len(Records[rType]))
 
 			for i := 0; i < len(Records[rType]); i++ {
-				foo := Records[rType][i].([]interface{})
-				//fmt.Printf("FOO TYPE %T %s\n", foo, foo)
-				record := new(Record)
-				ip := foo[0].(string)
 
-				var err error
-				record.Weight, err = strconv.Atoi(foo[1].(string))
-				if err != nil {
-					panic("Error converting weight to integer")
-				}
+				fmt.Printf("RT %T %#v\n", Records[rType][i], Records[rType][i])
+
+				record := new(Record)
 
 				var h dns.RR_Header
 				fmt.Println("TTL OPTIONS", Options.Ttl)
 				h.Ttl = uint32(Options.Ttl)
 				h.Class = dns.ClassINET
-
 				h.Rrtype = dnsType
 
-				fmt.Println("H", h)
-
 				switch dnsType {
-				case dns.TypeA:
-					rr := new(dns.RR_A)
-					rr.Hdr = h
-					rr.A = net.ParseIP(ip)
-					if rr.A == nil {
-						panic("Bad A record")
+				case dns.TypeA, dns.TypeAAAA:
+					rec := Records[rType][i].([]interface{})
+					ip := rec[0].(string)
+					var err error
+					record.Weight, err = strconv.Atoi(rec[1].(string))
+					if err != nil {
+						panic("Error converting weight to integer")
 					}
-					record.RR = rr
-				case dns.TypeAAAA:
-					rr := new(dns.RR_AAAA)
-					rr.Hdr = h
-					rr.AAAA = net.ParseIP(ip)
-					if rr.AAAA == nil {
-						panic("Bad AAAA record")
+					switch dnsType {
+					case dns.TypeA:
+						rr := &dns.RR_A{Hdr: h}
+						rr.A = net.ParseIP(ip)
+						if rr.A == nil {
+							panic("Bad A record")
+						}
+						record.RR = rr
+					case dns.TypeAAAA:
+						rr := &dns.RR_AAAA{Hdr: h}
+						rr.AAAA = net.ParseIP(ip)
+						if rr.AAAA == nil {
+							panic("Bad AAAA record")
+						}
+						record.RR = rr
 					}
+				case dns.TypeNS:
+					ns := Records[rType][i].(string)
+					if h.Ttl < 43000 {
+						h.Ttl = 43200
+					}
+					rr := &dns.RR_NS{Hdr: h}
+					rr.Ns = ns
 					record.RR = rr
+
 				default:
 					fmt.Println("type:", rType)
 					panic("Don't know how to handle this type")
-
 				}
 
 				if record.RR == nil {

+ 2 - 1
serve.go

@@ -24,7 +24,8 @@ func serve(w dns.ResponseWriter, req *dns.Msg, z *Zone, opt *Options) {
 	m.MsgHdr.Authoritative = true
 
 	// TODO: Function to find appropriate label with records
-	if region, ok := z.Labels["2"]; ok {
+	if region, ok := z.Labels[""]; ok {
+		fmt.Println("REG", region)
 		if region_rr := region.Records[req.Question[0].Qtype]; region_rr != nil {
 			//fmt.Printf("REGION_RR %T %v\n", region_rr, region_rr)
 			max := len(region_rr)