TechnitiumSoftware/DnsServer · error · InvalidDataException

DnsServer zone file format is invalid.

Error message

DnsServer zone file format is invalid.

What it means

Thrown by AuthZoneManager.LoadZoneFrom when the first 2 bytes of a zone file stream are not the ASCII magic 'DZ'. Technitium persists each authoritative zone in a custom binary format starting with 'DZ' then a 1-byte version; a wrong marker means the stream is not a Technitium zone file or is corrupt/truncated.

Source

Thrown at DnsServerCore/Dns/ZoneManagers/AuthZoneManager.cs:434

                        uint minExpiryTtl = GetMinExpiryTtlFor(records);
                        if (minExpiryTtl > 0u)
                            apexZone.StartRecordExpiryTimer(minExpiryTtl);
                    }
                    break;

                case AuthZoneType.SecondaryCatalog:
                    {
                        (apexZone as SecondaryZone).TriggerRefresh();
                        (apexZone as SecondaryCatalogZone).BuildMembersIndex();
                    }
                    break;
            }
        }

        public AuthZoneInfo LoadZoneFrom(Stream s, DateTime lastModified)
        {
            if (Encoding.ASCII.GetString(s.ReadExactly(2)) != "DZ")
                throw new InvalidDataException("DnsServer zone file format is invalid.");

            BinaryReader bR = new BinaryReader(s);

            switch (bR.ReadByte())
            {
                case 2:
                    {
                        DnsResourceRecord[] records = new DnsResourceRecord[bR.ReadInt32()];
                        if (records.Length == 0)
                            throw new InvalidDataException("Zone does not contain SOA record.");

                        DnsResourceRecord soaRecord = null;

                        for (int i = 0; i < records.Length; i++)
                        {
                            records[i] = new DnsResourceRecord(s);

                            if (records[i].Type == DnsResourceRecordType.SOA)

View on GitHub (pinned to d0484b6c1e)

Solutions

  1. Import RFC text zone files via the text/import code path, not LoadZoneFrom (which expects the binary 'DZ' format).
  2. Restore the zone file from a known-good backup or delete it and re-create the zone.
  3. Ensure zone files are written atomically (write to temp + rename) so partial files never exist.

Example fix

// before
using var fs = File.OpenRead(path);
zoneManager.LoadZoneFrom(fs, lastModified); // throws if not 'DZ'

// after
using var fs = File.OpenRead(path);
var magic = Encoding.ASCII.GetString(new BinaryReader(fs).ReadBytes(2));
fs.Position = 0;
if (magic != "DZ") throw new InvalidDataException("Not a Technitium binary zone file: " + path);
zoneManager.LoadZoneFrom(fs, lastModified);
Defensive patterns

Strategy: validation

Validate before calling

bool IsTechnitiumZoneFile(string path)
{
    using var fs = File.OpenRead(path);
    if (fs.Length < 3) return false;
    using var br = new BinaryReader(fs);
    return Encoding.ASCII.GetString(br.ReadBytes(2)) == "DZ";
}

Try / catch

try { zoneManager.LoadZoneFrom(fs, lastModified); }
catch (InvalidDataException ex) when (ex.Message.Contains("zone file format is invalid"))
{
    // not a binary zone — fall back to text import, or restore from backup
}

Prevention

When it happens

Trigger: Loading a zone from a stream (zone import, restore, or startup load) whose first two bytes are not 'DZ' — e.g. a plain RFC zone text file, a different binary format, or a zero-byte/corrupt file.

Common situations: Importing an RFC-format text zone file through a binary loader, a truncated zone file from a crash mid-write, restoring a backup that was incompletely copied, or pointing the loader at a non-zone file.

Related errors


AI-assisted analysis of TechnitiumSoftware/DnsServer@d0484b6c1e (2026-08-13). Data as JSON: /api/errors/95ff3b4609d5f86d. Report an issue: GitHub.