TechnitiumSoftware/DnsServer · error · InvalidDataException

DnsServer allowed zone file format is invalid.

Error message

DnsServer allowed zone file format is invalid.

What it means

Thrown by AllowedZoneManager.ReadConfigFrom when the first 2 bytes of the allowed-zones config stream are not the ASCII magic 'AZ'. The persisted allowed-zones file is a custom binary format that begins with a 2-byte format marker followed by a 1-byte version; a missing/wrong marker means the file is not a valid allowed-zones config or is corrupt.

Source

Thrown at DnsServerCore/Dns/ZoneManagers/AllowedZoneManager.cs:193

            _dnsServer.LogManager.Write("DNS Server allowed zone file was saved: " + allowedZoneFile);
        }

        public void SaveZoneFile()
        {
            lock (_saveLock)
            {
                if (_pendingSave)
                    return;

                _pendingSave = true;
                _saveTimer.Change(SAVE_TIMER_INITIAL_INTERVAL, Timeout.Infinite);
            }
        }

        private void ReadConfigFrom(Stream s)
        {
            if (Encoding.ASCII.GetString(s.ReadExactly(2)) != "AZ") //format
                throw new InvalidDataException("DnsServer allowed zone file format is invalid.");

            BinaryReader bR = new BinaryReader(s);

            byte version = bR.ReadByte();
            switch (version)
            {
                case 1:
                    int length = bR.ReadInt32();
                    int i = 0;

                    AuthZoneManager zoneManager = new AuthZoneManager(_dnsServer);

                    zoneManager.LoadSpecialPrimaryZones(delegate ()
                    {
                        if (i++ < length)
                            return s.ReadShortString();

                        return null;

View on GitHub (pinned to d0484b6c1e)

Solutions

  1. Restore the allowed-zones config from a backup or delete it so the manager recreates an empty one on next save.
  2. Verify the config directory is writable and on a journaled filesystem to avoid partial writes.
  3. Upgrade/reinstall from a matching Technitium version to regenerate a valid config.

Example fix

// before: corrupt config crashes startup
server.Start();

// after: detect and reset bad config
var path = Path.Combine(configDir, "allowedZones.bin");
using var fs = File.OpenRead(path);
if (Encoding.ASCII.GetString(new BinaryReader(fs).ReadBytes(2)) != "AZ") {
    File.Delete(path); // let manager recreate empty config
}
server.Start();
Defensive patterns

Strategy: try-catch

Validate before calling

bool IsValidAllowedZonesFile(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)) == "AZ";
}

Try / catch

try { allowedZoneManager.Load(...); }
catch (InvalidDataException ex) when (ex.Message.Contains("allowed zone file format is invalid"))
{
    File.Delete(configPath); // regenerate empty on next save
    allowedZoneManager.Load(...);
}

Prevention

When it happens

Trigger: Loading the allowed-zones config file (e.g. on DnsServer start or config transfer) when the file is empty, truncated, written by a different component, or corrupted so its first two bytes are not 'AZ'.

Common situations: An empty or zero-byte config file left by a crashed write, a file from an incompatible/forked build, manual editing of the binary config, or a partial write during a power loss.

Related errors


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