srctree

Gregory Mullen parent 86614bb7 ff52dbb1
convert debug to log

src/client.zig added: 32, removed: 29, total 3
@@ -1,6 +1,6 @@
pub fn main() !void {
const a = std.heap.page_allocator;
std.debug.print("started\n", .{});
log.err("started", .{});
 
var domain: ?[]const u8 = null;
var nameserver: [4]u8 = @splat(0);
@@ -22,19 +22,19 @@ pub fn main() !void {
var request: [1024]u8 = undefined;
const msgsize = try msg.write(&request);
 
std.debug.print("msg {}\n", .{msgsize});
std.debug.print("data {any}\n", .{request[0..msgsize]});
std.debug.print("data {s}\n", .{request[0..msgsize]});
log.err("msg {}", .{msgsize});
log.err("data {any}", .{request[0..msgsize]});
log.err("data {s}", .{request[0..msgsize]});
 
try upstream.send(request[0..msgsize]);
 
var buffer: [1024]u8 = undefined;
const icnt = try upstream.recv(&buffer);
std.debug.print("received {}\n", .{icnt});
std.debug.print("data {any}\n", .{buffer[0..icnt]});
std.debug.print("data {s}\n", .{buffer[0..icnt]});
log.err("received {}", .{icnt});
log.err("data {any}", .{buffer[0..icnt]});
log.err("data {s}", .{buffer[0..icnt]});
 
std.debug.print("done\n", .{});
log.err("done", .{});
}
 
const DNS = @import("dns.zig");
 
src/dns.zig added: 32, removed: 29, total 3
@@ -209,7 +209,7 @@ pub const Message = struct {
pub fn fromBytes(a: Allocator, bytes: []const u8) !Message {
if (bytes.len < 12) return error.MessageTooSmall;
const header: Header = .fromBytes(bytes[0..12].*);
std.debug.print("{}\n", .{header});
log.warn("{}", .{header});
 
var idx: usize = 12;
 
@@ -217,22 +217,22 @@ pub const Message = struct {
for (questions) |*q| {
const name = try Label.getName(a, bytes, &idx);
defer a.free(name);
std.debug.print("{s}\n", .{name});
log.warn("label name {s}", .{name});
q.* = .{
.name = name,
.qtype = @enumFromInt(@byteSwap(@as(u16, @bitCast(bytes[idx..][0..2].*)))),
.class = @enumFromInt(@byteSwap(@as(u16, @bitCast(bytes[idx..][2..4].*)))),
};
std.debug.print("{any}\n", .{q.*});
log.warn("{any}", .{q.*});
idx += 4;
}
 
const resources = try a.alloc(Resource, header.ancount);
for (resources) |*r| {
std.debug.print("{} {}\n", .{ idx, bytes[idx] });
log.warn("{} {}", .{ idx, bytes[idx] });
const name = try Label.getName(a, bytes, &idx);
defer a.free(name);
std.debug.print("{s}\n", .{name});
log.warn("{s}", .{name});
const rdlen: u16 = @byteSwap(@as(u16, @bitCast(bytes[idx..][8..10].*)));
r.* = .{
.name = name,
@@ -242,7 +242,7 @@ pub const Message = struct {
.rdlength = rdlen,
.rdata = bytes[idx..][10..][0..rdlen],
};
std.debug.print("{any}\n", .{r.*});
log.warn("{any}", .{r.*});
if (r.*.rtype != .a) @panic("not implemented");
}
 
@@ -284,6 +284,10 @@ pub const Message = struct {
_ = ip;
}
 
pub fn answerDrop(domain: []const u8) !Message {
_ = domain;
}
 
pub fn write(m: Message, buffer: []u8) !usize {
var fbs = std.io.fixedBufferStream(buffer);
var writer = fbs.writer();
 
src/server.zig added: 32, removed: 29, total 3
@@ -12,12 +12,12 @@ pub fn main() !void {
 
// nobody on my machine
if (std.os.linux.getuid() == 0) {
std.debug.print("dropping root\n", .{});
log.err("dropping root", .{});
_ = try std.posix.setgid(99);
_ = try std.posix.setuid(99);
}
 
std.debug.print("started\n", .{});
log.err("started", .{});
 
var cache: DNS.Cache = .{
.tld = .{},
@@ -43,31 +43,30 @@ pub fn main() !void {
while (true) {
var addr: std.net.Address = .{ .in = .{ .sa = .{ .port = 0, .addr = 0 } } };
var buffer: [1024]u8 = undefined;
std.debug.print("sock {}\n", .{downstream.sock});
const icnt = try downstream.recvFrom(&buffer, &addr);
std.debug.print("received {}\n", .{icnt});
//std.debug.print("data {any}\n", .{buffer[0..icnt]});
std.debug.print("received from {any}\n", .{addr.in});
log.err("received {}", .{icnt});
//log.err("data {any}", .{buffer[0..icnt]});
log.err("received from {any}", .{addr.in});
 
const msg = try DNS.Message.fromBytes(a, buffer[0..icnt]);
//std.debug.print("data {any}\n", .{msg});
//log.err("data {any}", .{msg});
// defer once in loop
if (msg.questions) |q| a.free(q);
if (msg.answers) |an| a.free(an);
 
std.debug.print("bounce\n", .{});
log.err("bounce", .{});
up_idx +%= 1;
try upconns[up_idx].send(buffer[0..icnt]);
var relay_buf: [1024]u8 = undefined;
const b_cnt = try upconns[up_idx].recv(&relay_buf);
std.debug.print("bounce received {}\n", .{b_cnt});
std.debug.print("bounce data {any}\n", .{buffer[0..b_cnt]});
log.err("bounce received {}", .{b_cnt});
log.err("bounce data {any}", .{relay_buf[0..b_cnt]});
 
try downstream.sendTo(addr, relay_buf[0..b_cnt]);
std.debug.print("responded\n", .{});
log.err("responded", .{});
}
 
std.debug.print("done\n", .{});
log.err("done", .{});
}
 
const upstreams: [4][4]u8 = .{