srctree

Gregory Mullen parent 6c8286a9 0a55712f
mv upstream to peer

src/client.zig added: 16, removed: 16, total 0
@@ -16,7 +16,7 @@ pub fn main() !void {
}
}
 
const upstream = try DNS.Upstream.connect(nameserver, 53);
const upstream = try DNS.Peer.connect(nameserver, 53);
 
const msg = try DNS.Message.query(a, &[1][]const u8{domain orelse "gr.ht."});
var request: [1024]u8 = undefined;
 
src/dns.zig added: 16, removed: 16, total 0
@@ -8,13 +8,13 @@ pub const Domains = struct {
),
};
 
pub const Upstream = struct {
pub const Peer = struct {
addr: std.net.Address,
sock: std.posix.socket_t,
 
/// TODO ipv6
pub fn init(addr_ip: [4]u8, port: u16) !Upstream {
const up: Upstream = .{
pub fn init(addr_ip: [4]u8, port: u16) !Peer {
const up: Peer = .{
.addr = .{ .in = .{ .sa = .{
.port = nativeToBig(u16, port),
.addr = bytesToValue(u32, &addr_ip),
@@ -24,30 +24,30 @@ pub const Upstream = struct {
return up;
}
 
pub fn connect(addr_ip: [4]u8, port: u16) !Upstream {
const up: Upstream = try .init(addr_ip, port);
pub fn connect(addr_ip: [4]u8, port: u16) !Peer {
const up: Peer = try .init(addr_ip, port);
try std.posix.connect(up.sock, &up.addr.any, up.addr.getOsSockLen());
return up;
}
 
pub fn listen(addr_ip: [4]u8, port: u16) !Upstream {
const up: Upstream = try .init(addr_ip, port);
pub fn listen(addr_ip: [4]u8, port: u16) !Peer {
const up: Peer = try .init(addr_ip, port);
try std.posix.bind(up.sock, &up.addr.any, up.addr.getOsSockLen());
return up;
}
 
pub fn send(upstrm: Upstream, data: []const u8) !void {
pub fn send(upstrm: Peer, data: []const u8) !void {
const cnt = try std.posix.send(upstrm.sock, data, 0);
if (cnt != data.len) return error.TxFailed;
}
 
pub fn recv(upstrm: Upstream, buffer: []u8) !usize {
pub fn recv(upstrm: Peer, buffer: []u8) !usize {
if (buffer.len < 512) return error.BufferTooSmall;
const icnt = try std.posix.recv(upstrm.sock, buffer, 0);
return icnt;
}
 
pub fn recvFrom(upstrm: Upstream, buffer: []u8, addr: *std.net.Address) !usize {
pub fn recvFrom(upstrm: Peer, buffer: []u8, addr: *std.net.Address) !usize {
if (buffer.len < 512) return error.BufferTooSmall;
var src_len: u32 = undefined;
const cnt = try std.posix.recvfrom(upstrm.sock, buffer, 0, &addr.any, &src_len);
 
src/server.zig added: 16, removed: 16, total 0
@@ -5,7 +5,7 @@ pub fn main() !void {
_ = arg;
}
 
const upstream: DNS.Upstream = try .listen(.{ 127, 0, 0, 1 }, 53);
const downstream: DNS.Peer = try .listen(.{ 127, 0, 0, 1 }, 53);
 
// nobody on my machine
if (std.os.linux.getuid() == 0) {
@@ -22,7 +22,7 @@ pub fn main() !void {
 
var addr: std.net.Address = .{ .any = undefined };
var buffer: [1024]u8 = undefined;
const icnt = try upstream.recvFrom(&buffer, &addr);
const icnt = try downstream.recvFrom(&buffer, &addr);
std.debug.print("received {}\n", .{icnt});
std.debug.print("data {any}\n", .{buffer[0..icnt]});