• Stars
    star
    32
  • Rank 801,539 (Top 16 %)
  • Language
    Zig
  • License
    MIT License
  • Created over 5 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

naive dns client library in zig

zigdig

naive dns client library in zig

what does it do

  • serialization and deserialization of dns packets as per rfc1035
  • supports a subset of rdata (A and AAAA are there, so, for most cases, this will be enough)
  • has helpers for reading /etc/resolv.conf (not that much, really)

what does it not do

  • no edns0
  • support all resolv.conf options
  • can deserialize pointer labels, but does not serialize into pointers
  • follow CNAME records, this provides only the basic serialization/deserializtion

how do

  • zig 0.11.0: https://ziglang.org
  • have a /etc/resolv.conf
  • tested on linux, should work on bsd i think
git clone ...
cd zigdig

zig build test
zig build install --prefix ~/.local/

and then

zigdig google.com a

or, for the host(1) equivalent

zigdig-tiny google.com

using the library

getAddressList-style api

const dns = @import("dns");

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer {
        _ = gpa.deinit();
    }
    var allocator = gpa.alloator();

    var addresses = try dns.helpers.getAddressList("ziglang.org", allocator);
    defer addresses.deinit();

    for (addresses.addrs) |address| {
        std.debug.print("we live in a society {}\n", .{address});
    }
}

full api

const dns = @import("dns");

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer {
        _ = gpa.deinit();
    }
    var allocator = gpa.alloator();

    var name_buffer: [128][]const u8 = undefined;
    const name = try dns.Name.fromString("ziglang.org", &name_buffer);

    var questions = [_]dns.Question{
        .{
            .name = name,
            .typ = .A,
            .class = .IN,
        },
    };

    var packet = dns.Packet{
        .header = .{
            .id = dns.helpers.randomHeaderId(),
            .is_response = false,
            .wanted_recursion = true,
            .question_length = 1,
        },
        .questions = &questions,
        .answers = &[_]dns.Resource{},
        .nameservers = &[_]dns.Resource{},
        .additionals = &[_]dns.Resource{},
    };

    // use helper function to connect to a resolver in the systems'
    // resolv.conf

    const conn = try dns.helpers.connectToSystemResolver();
    defer conn.close();

    try conn.sendPacket(packet);

    // you can also do this to support any Writer
    // const written_bytes = try packet.writeTo(some_fun_writer_goes_here);

    const reply = try conn.receivePacket(allocator, 4096);
    defer reply.deinit();

    // you can also do this to support any Reader
    // const packet = try dns.Packet.readFrom(some_fun_reader, allocator);
    // defer packet.deinit();

    const reply_packet = reply.packet;
    logger.info("reply: {}", .{reply_packet});

    try std.testing.expectEqual(packet.header.id, reply_packet.header.id);
    try std.testing.expect(reply_packet.header.is_response);

    // ASSERTS that there's one A resource in the answer!!! you should verify
    // reply_packet.header.opcode to see if there's any errors

    const resource = reply_packet.answers[0];
    var resource_data = try dns.ResourceData.fromOpaque(
        reply_packet,
        resource.typ,
        resource.opaque_rdata,
        allocator
    );
    defer resource_data.deinit(allocator);

    // you now have an std.net.Address to use to your hearts content
    const ziglang_address = resource_data.A;
}

it is recommended to look at zigdig's source on src/main.zig to understand how things tick using the library, but it boils down to three things:

  • packet generation and serialization
  • sending/receiving (via a small shim on top of std.os.socket)
  • packet deserialization

More Repositories

1

awtfdb

the Anime Woman's Tagged File Data Base.
Zig
32
star
2

obsidian2web

my obsidian publish knockoff that generates (largely static) websites
Zig
26
star
3

storcord

a badly-designed document store on top of discord (meme, not production ready, NOT PRODUCTION READY)
Python
24
star
4

ziget

simple wget in zig without libc
Zig
17
star
5

ytsearch

vrchat youtube search world backend
Elixir
16
star
6

jose

moved to https://gitlab.com/luna/jose
Python
13
star
7

pp

--dry-run should be evangelized more. this is the dd(1) edition of it
Shell
12
star
8

maid

(WIP) self-contained todo list tui, inspired by void-rs
Zig
12
star
9

alligator

securely share files between computers using the tor network
Python
7
star
10

tsusu

proof of concept process manager written in zig (was rust)
Zig
6
star
11

esp32-heart-rate-vrchat

take data out of a heart sensor and ship it to vrchat using osc using an esp32 (m5stickc-plus used)
C++
4
star
12

callisto

extract json structure from discord json
Python
4
star
13

drip

among us elixir server experimentation
Elixir
3
star
14

expiring_hash

expiring hash for crystal
Crystal
3
star
15

dril-instruct

can large language models create good shitposts?
Python
3
star
16

hellish-screams

Scream protocol for PulseAudio
Rust
3
star
17

obsidian-maid

the wip todo saga that never finished, now in obsidian (and finished!!!)
TypeScript
2
star
18

hyperuniversality

apple's universal binaries BUT MUCH WORSE (PRACTICAL SHITPOST) (NOT PRODUCTION READY)
Python
2
star
19

gtk-zig-test

testing if gtk and zig can build together (answer = almost)
Zig
2
star
20

zeb

experimentation on a zig web framework
Zig
2
star
21

rana

(DISCONTINUED) reverse engineered wakatime server
Python
2
star
22

atsuko

zig http experiment
Zig
2
star
23

fog

fucking log
Elixir
1
star
24

tupleinator

update your std.debug.warn addiction with ease
Python
1
star
25

finfox

scripts for personal finance
Python
1
star
26

jose-elixir

José but its Elixir
Elixir
1
star
27

linguex

[wip] large language model (high-level) agent framework in elixir
Elixir
1
star
28

wcpl

The simplest-HTML language I thought of
Python
1
star
29

expiring-hash-map.zig

need a cache or you're limited by memory in your long-lived zig code, well fear no more!
Zig
1
star
30

discord-rpc-test

discord-rpc test thing [archived, exists for historical purposes only]
C
1
star
31

cord

experimental discord library
Python
1
star
32

catcord

barebones discord library, python
Python
1
star
33

minishare

python -m http.server but this time its in zig because i can
Zig
1
star
34

vrr

[prototype] vr (screen) repeater
Lua
1
star
35

lun-4

this really be a bruh moment
1
star
36

shitircd

shitpost capabilities induced ircd
Zig
1
star
37

asot

"a series of tubes". private ngrok/pagekite solution but trying to do it (at least a working linux version) in less than 690 lines of code
Python
1
star
38

zig-kak

kakoune plugin for zig support (wip)
1
star
39

synthnav

[prototype quality] story writing with large language models
Python
1
star
40

lovr-rtsp

streaming rtsp to a lovr Image object, courtesy of ffmpeg
Zig
1
star
41

nyaf

[to be continued?] nya firewall (an ufw frontend to netbsd's npf)
Zig
1
star
42

carnation

remotely control mouse-tracker-for-cubism
Lua
1
star