Zig 0.17.0-dev (Split by item) This is an example of documentation generated by
ZigDoc , an alternative to Zig's built-in
Auto Doc feature. See also
examples in other modes/formats . The project being documented here (as the example) is the Zig standard library.
std › Io › net › HostName › DnsResponse Public Struct DnsResponse Doc Source Public Struct std.Io.net.HostName.DnsResponse
pub const DnsResponse = struct Fields bytes : []const u8 bytes_index : u32 answers_remaining : u16 Structs Values Functions File Io/net/HostName.zig: 248
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297 pub const DnsResponse = struct {
bytes : []const u8 ,
bytes_index : u32 ,
answers_remaining : u16 ,
pub const Answer = struct {
rr : DnsRecord ,
packet : []const u8 ,
data_off : u32 ,
data_len : u16 ,
};
pub const Error = error {InvalidDnsPacket };
pub fn init (r : []const u8 ) Error !DnsResponse {
if (r .len < 12 ) return error .InvalidDnsPacket ;
if ((r [3 ] & 15 ) != 0 ) return .{ .bytes = r , .bytes_index = 3 , .answers_remaining = 0 };
var i : u32 = 12 ;
var query_count = std .mem .readInt (u16 , r [4 ..6 ], .big );
while (query_count != 0 ) : (query_count -= 1 ) {
while (i < r .len and r [i ] -% 1 < 127 ) i += 1 ;
if (r .len - i < 6 ) return error .InvalidDnsPacket ;
i = i + 5 + @intFromBool (r [i ] != 0 );
}
return .{
.bytes = r ,
.bytes_index = i ,
.answers_remaining = std .mem .readInt (u16 , r [6 ..8 ], .big ),
};
}
pub fn next (dr : *DnsResponse ) Error !?Answer {
if (dr .answers_remaining == 0 ) return null ;
dr .answers_remaining -= 1 ;
const r = dr .bytes ;
var i = dr .bytes_index ;
while (i < r .len and r [i ] -% 1 < 127 ) i += 1 ;
if (r .len - i < 12 ) return error .InvalidDnsPacket ;
i = i + 1 + @intFromBool (r [i ] != 0 );
const len = std .mem .readInt (u16 , r [i + 8 ..][0 ..2 ], .big );
if (i + 10 + len > r .len ) return error .InvalidDnsPacket ;
defer dr .bytes_index = i + 10 + len ;
return .{
.rr = @fromBackingInt (@intCast (r [i + 1 ])),
.packet = r ,
.data_off = i + 10 ,
.data_len = len ,
};
}
}