Browse Source

Merge pull request #49 from afsheenb/add_srv_records

Added support for SRV records, plus documentation and tests
Ask Bjørn Hansen 11 years ago
parent
commit
b5f0b19db2
4 changed files with 48 additions and 0 deletions
  1. 12 0
      README.md
  2. 1 0
      dns/test.example.com.json
  3. 7 0
      serve_test.go
  4. 28 0
      zones.go

+ 12 - 0
README.md

@@ -242,6 +242,18 @@ An spf record is typically at the root of a zone, and a label can have an array
 
       "spf": [ { "spf": "v=spf1 ~all", "weight": 1 } , "spf": "v=spf1 10.0.0.1", "weight": 100]
 
+### SRV
+
+An SRV record has four components: the weight, priority, port and target. The keys for these are "srv_weight", "priority", "target" and "port". Note the difference between srv_weight (the weight key for the SRV qtype) and "weight".
+
+An example srv record definition for the _sip._tcp service:
+
+"_sip._tcp": { "srv": [ { "port": 5060, "srv_weight": 100, "priority": 10, "target": "sipserver.example.com."}] },
+
+Much like MX records, SRV records can have multiple targets, eg:
+
+"_http._tcp": { "srv": [ { "port": 80, "srv_weight": 10, "priority": 10, "target": "www.example.com."}, { "port": 8080, "srv_weight": 10, "priority": 20, "target": "www2.example.com."}] },
+
 ## License and Copyright
 
 This software is Copyright 2012-2013 Ask Bjørn Hansen. For licensing information

+ 1 - 0
dns/test.example.com.json

@@ -28,6 +28,7 @@
              ],
       "max_hosts": "1"
     },
+   "_sip._tcp": { "srv": [ { "port": 5060, "srv_weight": 100, "priority": 10, "target": "sipserver.example.com."}] },
     "bar": {
       "a": [ [ "192.168.1.2" ] ],
       "ttl": "601"

+ 7 - 0
serve_test.go

@@ -78,6 +78,13 @@ func (s *ServeSuite) TestServing(c *C) {
 	r = exchange(c, "test.example.com.", dns.TypeSPF)
 	c.Check(r.Answer[0].(*dns.SPF).Txt[0], Equals, "v=spf1 ~all")
 
+	//SRV
+	r = exchange(c, "_sip._tcp.test.example.com.", dns.TypeSRV)
+	c.Check(r.Answer[0].(*dns.SRV).Target, Equals, "sipserver.example.com.")
+	c.Check(r.Answer[0].(*dns.SRV).Port, Equals, uint16(5060))
+	c.Check(r.Answer[0].(*dns.SRV).Priority, Equals, uint16(10))
+	c.Check(r.Answer[0].(*dns.SRV).Weight, Equals, uint16(100))
+
 	// MX
 	r = exchange(c, "test.example.com.", dns.TypeMX)
 	c.Check(r.Answer[0].(*dns.MX).Mx, Equals, "mx.example.net.")

+ 28 - 0
zones.go

@@ -225,6 +225,7 @@ func setupZoneData(data map[string]interface{}, Zone *Zone) {
 		"ns":    dns.TypeNS,
 		"txt":   dns.TypeTXT,
 		"spf":   dns.TypeSPF,
+		"srv":   dns.TypeSRV,
 	}
 
 	for dk, dv_inter := range data {
@@ -338,6 +339,33 @@ func setupZoneData(data map[string]interface{}, Zone *Zone) {
 						Mx:         mx,
 						Preference: pref}
 
+				case dns.TypeSRV:
+					rec := records[rType][i].(map[string]interface{})
+					priority := uint16(0)
+					srv_weight := uint16(0)
+					port := uint16(0)
+					target := rec["target"].(string)
+
+					if !dns.IsFqdn(target) {
+						target = target + "." + Zone.Origin
+					}
+
+					if rec["srv_weight"] != nil {
+						srv_weight = uint16(valueToInt(rec["srv_weight"]))
+					}
+					if rec["port"] != nil {
+						port = uint16(valueToInt(rec["port"]))
+					}
+					if rec["priority"] != nil {
+						priority = uint16(valueToInt(rec["priority"]))
+					}
+					record.RR = &dns.SRV{
+						Hdr:      h,
+						Priority: priority,
+						Weight:   srv_weight,
+						Port:     port,
+						Target:   target}
+
 				case dns.TypeCNAME:
 					rec := records[rType][i]
 					var target string