Skip to content

Commit

Permalink
Add support for MX records
Browse files Browse the repository at this point in the history
(ticket #20)
  • Loading branch information
abh committed Nov 15, 2012
1 parent 0506509 commit c1cb58d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
22 changes: 20 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ func setupZoneData(data map[string]interface{}, Zone *Zone) {
"aaaa": dns.TypeAAAA,
"ns": dns.TypeNS,
"cname": dns.TypeCNAME,
"mx": dns.TypeMX,
"alias": dns.TypeMF,
}

Expand Down Expand Up @@ -237,10 +238,8 @@ func setupZoneData(data map[string]interface{}, Zone *Zone) {
if err != nil {
panic("Error converting weight to integer")
}
label.Weight[dnsType] += record.Weight
case float64:
record.Weight = int(rec[1].(float64))
label.Weight[dnsType] += record.Weight
}
switch dnsType {
case dns.TypeA:
Expand All @@ -257,6 +256,24 @@ func setupZoneData(data map[string]interface{}, Zone *Zone) {
panic("Bad AAAA record")
}

case dns.TypeMX:
rec := records[rType][i].(map[string]interface{})
pref := uint16(0)
mx := rec["mx"].(string)
if !strings.HasSuffix(mx, ".") {
mx = mx + "."
}
if rec["weight"] != nil {
record.Weight = valueToInt(rec["weight"])
}
if rec["preference"] != nil {
pref = uint16(valueToInt(rec["preference"]))
}
record.RR = &dns.RR_MX{
Hdr: h,
Mx: mx,
Pref: pref}

case dns.TypeCNAME:
rec := records[rType][i]
record.RR = &dns.RR_CNAME{Hdr: h, Target: dns.Fqdn(rec.(string))}
Expand Down Expand Up @@ -301,6 +318,7 @@ func setupZoneData(data map[string]interface{}, Zone *Zone) {
panic("record.RR is nil")
}

label.Weight[dnsType] += record.Weight
label.Records[dnsType][i] = *record
}
if label.Weight[dnsType] > 0 {
Expand Down
8 changes: 7 additions & 1 deletion dns/example.com.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
"ttl": 600,
"max_hosts": 2,
"data" : {
"": { "ns": { "ns1.example.net.": null, "ns2.example.net.": null } },
"": {
"ns": { "ns1.example.net.": null, "ns2.example.net.": null },
"mx": [ { "preference": 20, "mx": "mx2.example.net", "weight": 0 },
{ "preference": 10, "mx": "mx.example.net.", "weight": 1 }
]
},
"europe": { "mx": [ { "mx": "mx-eu.example.net" }]},
"foo": {
"a": [ [ "192.168.1.2", 10 ], [ "192.168.1.3", 10 ], [ "192.168.1.4", 10 ] ],
"aaaa": [ ["fd06:c1d3:e902::2", 10], ["fd06:c1d3:e902:202:a5ff:fecd:13a6:a", 10], ["fd06:c1d3:e902::4", 10] ]
Expand Down
12 changes: 12 additions & 0 deletions zone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,16 @@ func (s *ConfigSuite) TestZone(c *C) {
// Make sure that the empty "no.bar" zone gets skipped and "bar" is used
label := ex.findLabels("bar", "no", dns.TypeA)
c.Check(label.Records[dns.TypeA], HasLen, 1)
c.Check(label.Records[dns.TypeA][0].RR.(*dns.RR_A).A.String(), Equals, "192.168.1.2")

label = ex.findLabels("", "", dns.TypeMX)
Mxs := label.Records[dns.TypeMX]
c.Check(Mxs, HasLen, 2)
c.Check(Mxs[0].RR.(*dns.RR_MX).Mx, Equals, "mx.example.net.")
c.Check(Mxs[1].RR.(*dns.RR_MX).Mx, Equals, "mx2.example.net.")

Mxs = ex.findLabels("", "dk", dns.TypeMX).Records[dns.TypeMX]
c.Check(Mxs, HasLen, 1)
c.Check(Mxs[0].RR.(*dns.RR_MX).Mx, Equals, "mx-eu.example.net.")

}

0 comments on commit c1cb58d

Please sign in to comment.