ApplicationZone: updated code to make the zone work like a primary zone with SOA and NS records with auto increment SOA serial.

This commit is contained in:
Shreyas Zare
2021-03-06 16:32:00 +05:30
parent c0402c96d4
commit 8c0a42c659

View File

@@ -34,10 +34,15 @@ namespace DnsServerCore.Dns.Zones
_disabled = zoneInfo.Disabled;
}
public ApplicationZone(string name, string package, string classPath, string data)
public ApplicationZone(string name, string primaryNameServer, string appName, string classPath, string data)
: base(name)
{
DnsResourceRecord appRecord = new DnsResourceRecord(name, DnsResourceRecordType.APP, DnsClass.IN, 60, new DnsApplicationRecord(package, classPath, data));
DnsSOARecord soa = new DnsSOARecord(primaryNameServer, _name.Length == 0 ? "hostadmin" : "hostadmin." + _name, 1, 14400, 3600, 604800, 900);
_entries[DnsResourceRecordType.SOA] = new DnsResourceRecord[] { new DnsResourceRecord(_name, DnsResourceRecordType.SOA, DnsClass.IN, soa.Refresh, soa) };
_entries[DnsResourceRecordType.NS] = new DnsResourceRecord[] { new DnsResourceRecord(_name, DnsResourceRecordType.NS, DnsClass.IN, soa.Refresh, new DnsNSRecord(soa.PrimaryNameServer)) };
DnsResourceRecord appRecord = new DnsResourceRecord(name, DnsResourceRecordType.APP, DnsClass.IN, 60, new DnsApplicationRecord(appName, classPath, data));
_entries[DnsResourceRecordType.APP] = new DnsResourceRecord[] { appRecord };
}
@@ -46,6 +51,21 @@ namespace DnsServerCore.Dns.Zones
#region public
public void IncrementSoaSerial()
{
DnsResourceRecord record = _entries[DnsResourceRecordType.SOA][0];
DnsSOARecord soa = record.RDATA as DnsSOARecord;
uint serial = soa.Serial;
if (serial < uint.MaxValue)
serial++;
else
serial = 0;
DnsResourceRecord newRecord = new DnsResourceRecord(record.Name, record.Type, record.Class, record.TtlValue, new DnsSOARecord(soa.PrimaryNameServer, soa.ResponsiblePerson, serial, soa.Refresh, soa.Retry, soa.Expire, soa.Minimum)) { Tag = record.Tag };
_entries[DnsResourceRecordType.SOA] = new DnsResourceRecord[] { newRecord };
}
public override void SetRecords(DnsResourceRecordType type, IReadOnlyList<DnsResourceRecord> records)
{
switch (type)
@@ -53,32 +73,58 @@ namespace DnsServerCore.Dns.Zones
case DnsResourceRecordType.CNAME:
throw new InvalidOperationException("Cannot set CNAME record to zone root.");
case DnsResourceRecordType.NS:
throw new InvalidOperationException("Cannot set NS record to application zone root.");
case DnsResourceRecordType.SOA:
throw new InvalidOperationException("Cannot set SOA record to application zone root.");
if ((records.Count != 1) || !records[0].Name.Equals(_name, StringComparison.OrdinalIgnoreCase))
throw new InvalidOperationException("Invalid SOA record.");
default:
base.SetRecords(type, records);
//remove any resource record info
records[0].Tag = null;
break;
}
base.SetRecords(type, records);
IncrementSoaSerial();
}
public override void AddRecord(DnsResourceRecord record)
{
switch (record.Type)
if (record.Type == DnsResourceRecordType.APP)
throw new InvalidOperationException("Cannot add record: use SetRecords() for " + record.Type.ToString() + " record");
base.AddRecord(record);
IncrementSoaSerial();
}
public override bool DeleteRecords(DnsResourceRecordType type)
{
if (type == DnsResourceRecordType.SOA)
throw new InvalidOperationException("Cannot delete SOA record.");
if (base.DeleteRecords(type))
{
case DnsResourceRecordType.APP:
throw new InvalidOperationException("Cannot add record: use SetRecords() for " + record.Type.ToString() + " record");
IncrementSoaSerial();
case DnsResourceRecordType.NS:
throw new InvalidOperationException("Cannot add NS record at application zone root.");
default:
base.AddRecord(record);
break;
return true;
}
return false;
}
public override bool DeleteRecord(DnsResourceRecordType type, DnsResourceRecordData record)
{
if (type == DnsResourceRecordType.SOA)
throw new InvalidOperationException("Cannot delete SOA record.");
if (base.DeleteRecord(type, record))
{
IncrementSoaSerial();
return true;
}
return false;
}
#endregion