Code Monkey home page Code Monkey logo

dnszone's Introduction

About

DnsZone is a tool for parsing and building dns zone file.

Available at nuget.org

Parsing zone file

Parse dns zone from existing string:

const string str = @"
$ORIGIN example.com.     ; designates the start of this zone file in the namespace
$TTL 1h                  ; default expiration time of all resource records without their own TTL value
example.com.  IN  SOA   ns.example.com. username.example.com. ( 2007120710 1d 2h 4w 1h )
example.com.  IN  NS    ns                    ; ns.example.com is a nameserver for example.com
example.com.  IN  NS    ns.somewhere.example. ; ns.somewhere.example is a backup nameserver for example.com
example.com.  IN  MX    10 mail.example.com.  ; mail.example.com is the mailserver for example.com
@             IN  MX    20 mail2.example.com. ; equivalent to above line, ""@"" represents zone origin
@             IN  MX    50 mail3              ; equivalent to above line, but using a relative host name
example.com.  IN  A     192.0.2.1             ; IPv4 address for example.com
              IN  AAAA  2001:db8:10::1        ; IPv6 address for example.com
ns            IN  A     192.0.2.2             ; IPv4 address for ns.example.com
              IN  AAAA  2001:db8:10::2        ; IPv6 address for ns.example.com
www           IN  CNAME example.com.          ; www.example.com is an alias for example.com
wwwtest       IN  CNAME www                   ; wwwtest.example.com is another alias for www.example.com
mail          IN  A     192.0.2.3             ; IPv4 address for mail.example.com
mail2         IN  A     192.0.2.4             ; IPv4 address for mail2.example.com
mail3         IN  A     192.0.2.5             ; IPv4 address for mail3.example.com";
var zone = DnsZoneFile.Parse(content);

Alternatively your can load zone from external uri:

var zone = await DnsZoneFile.LoadAsync(uri)

Generating zone file

You can use regular ToString() method to produce zone file from DnsZoneFile object

var zone = new DnsZoneFile();
zone.Records.Add(new AResourceRecord {
    Name = "www.example.com",
    Class = "IN",
    Ttl = TimeSpan.FromMinutes(15),
    Address = IPAddress.Parse("127.0.0.1")
});
zone.Records.Add(new AResourceRecord {
    Name = "ftp.example.com",
    Class = "IN",
    Ttl = TimeSpan.FromMinutes(15),
    Address = IPAddress.Parse("127.0.0.1")
});
Console.Write(zone.ToString());

This will generate following content:

;A records
www.example.com.	IN	15m	A	127.0.0.1	
ftp.example.com.	IN	15m	A	127.0.0.1	

You can give a hint to the formatter which domain should be used as an origin to make output cleaner

Console.Write(zone.ToString("example.com"));

This will generate following content

$ORIGIN example.com.
;A records
www	IN	15m	A	127.0.0.1	
ftp	IN	15m	A	127.0.0.1	

References

RFC 1035 - Domain names - implementation and specification

RFC 1034 - Domain names - concepts and facilities

RFC 2782 - A DNS RR for specifying the location of services (DNS SRV)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.