Sample code for 30+ languages & platforms
Zig

DNS Query SOA Records

See more DNS Examples

Shows how to perform a DNS query to retrieve SOA records.

Note: This example requires Chilkat v9.5.0.96 or later.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    const dns = try chilkat.Dns.init();
    defer dns.deinit();

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.setEmitCompact(false);

    dns.query("SOA", "x.com", json) catch {
        std.debug.print("{s}\n", .{try dns.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("{s}\n", .{try json.emit(alloc)});

    // Sample response.
    // Parsing code below..

    // {
    //   "answer": {
    //     "soa": [
    //       {
    //         "name": "x.com",
    //         "ttl": 1800,
    //         "mname": "alexia.ns.cloudflare.com",
    //         "rname": "dns.cloudflare.com",
    //         "serial": -1971124188,
    //         "refresh": 10000,
    //         "retry": 2400,
    //         "expire": 604800,
    //         "minttl": 1800
    //       }
    //     ]
    //   }
    // }

    // Use this online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    var name: [:0]const u8 = "";
    var ttl: i32 = 0;
    var mname: [:0]const u8 = "";
    var rname: [:0]const u8 = "";
    var serial: i32 = 0;
    var refresh: i32 = 0;
    var retry: i32 = 0;
    var expire: i32 = 0;
    var minttl: i32 = 0;

    var i: i32 = 0;
    const count_i = json.sizeOfArray("answer.soa");
    while (i < count_i) {
        json.setI(i);
        name = try json.stringOf(alloc, "answer.soa[i].name");
        ttl = json.intOf("answer.soa[i].ttl");
        mname = try json.stringOf(alloc, "answer.soa[i].mname");
        rname = try json.stringOf(alloc, "answer.soa[i].rname");
        serial = json.intOf("answer.soa[i].serial");
        refresh = json.intOf("answer.soa[i].refresh");
        retry = json.intOf("answer.soa[i].retry");
        expire = json.intOf("answer.soa[i].expire");
        minttl = json.intOf("answer.soa[i].minttl");
        i = i + 1;
    }
}