DNS.Zone.FileParser (DNS v0.4.1)

View Source

DNS Zone file parser and generator for standard BIND format zone files.

Supports parsing and generation of DNS record types including:

  • Standard records: SOA, NS, A, AAAA, CNAME, MX, TXT, SRV, PTR
  • DNSSEC records: DNSKEY, DS, RRSIG, NSEC, NSEC3, NSEC3PARAM
  • Security records: CAA, TLSA
  • Modern records: HTTPS, SVCB

Handles:

  • Comments (starting with ;)
  • Line continuations
  • Zone directives: $ORIGIN, $TTL, $INCLUDE, $GENERATE
  • Class specifications (IN, CH, HS, NONE, ANY)
  • TTL specifications and defaults
  • Zone validation and error reporting

Summary

Functions

Generate a zone file string from zone data.

Parse a zone file from a string with enhanced error handling and validation.

Parse a zone file from a file path with enhanced error handling.

Parse a zone file from a string with detailed error reporting.

Validate zone data for common issues.

Types

parse_error()

@type parse_error() :: %{line: integer(), message: String.t(), context: String.t()}

parse_warning()

@type parse_warning() :: %{line: integer(), message: String.t(), context: String.t()}

zone_data()

@type zone_data() :: %{
  origin: String.t() | nil,
  ttl: integer(),
  soa: map() | nil,
  records: [DNS.Zone.RRSet.t()],
  includes: [String.t()],
  directives: [map()],
  errors: [map()],
  warnings: [map()]
}

Functions

generate(zone)

@spec generate(zone_data()) :: String.t()

Generate a zone file string from zone data.

parse(content)

@spec parse(String.t()) :: {:ok, zone_data()} | {:error, String.t()}

Parse a zone file from a string with enhanced error handling and validation.

Examples

iex> zone_content = """
...> $ORIGIN example.com.
...> $TTL 3600
...> @       IN  SOA ns1.example.com. admin.example.com. (
...>                     2024010101  ; Serial
...>                     3600        ; Refresh
...>                     1800        ; Retry
...>                     604800      ; Expire
...>                     86400 )     ; Minimum TTL
...> @       IN  NS  ns1.example.com.
...> @       IN  NS  ns2.example.com.
...> ns1     IN  A   192.0.2.1
...> ns2     IN  A   192.0.2.2
...> www     IN  A   192.0.2.100
...> """
iex> {:ok, zone} = DNS.Zone.FileParser.parse(zone_content)
iex> length(zone.records)
5
iex> zone.origin
"example.com"

parse_file(file_path)

@spec parse_file(String.t()) :: {:ok, zone_data()} | {:error, String.t()}

Parse a zone file from a file path with enhanced error handling.

parse_with_context(content, context)

@spec parse_with_context(String.t(), map()) ::
  {:ok, zone_data()} | {:error, String.t()}

Parse a zone file from a string with detailed error reporting.

validate_zone(zone)

@spec validate_zone(zone_data()) :: {:ok, zone_data()} | {:error, String.t()}

Validate zone data for common issues.