Skip to content

Commit

Permalink
add "API docs" to readme
Browse files Browse the repository at this point in the history
  • Loading branch information
lun-4 committed Dec 27, 2022
1 parent f5d0b72 commit 51815fa
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,74 @@ zigdig google.com a

## using the library

```zig
const dns = @import("dns");
pub fn main() !void {
var name_buffer: [128][]const u8 = undefined;
const name = try dns.Name.fromString("ziglang.org", &name_buffer);
var packet = dns.Packet{
.header = .{
.id = dns.helpers.randomHeaderId(),
.is_response = false,
.wanted_recursion = true,
.question_length = 1,
},
.questions = &[_]dns.Question{
.{
.name = name,
.typ = .A,
.class = .IN,
},
},
.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
const ziglang_address = resource_data.A;
}
```

**TODO docs**

it is recommended to look at zigdig's source on `src/main.zig` to understand
Expand Down

0 comments on commit 51815fa

Please sign in to comment.