srctree

Gregory Mullen parent 026a2374 e1695088
mv peer to network.zig

src/client.zig added: 67, removed: 64, total 3
@@ -15,7 +15,7 @@ pub fn main() !void {
}
}
 
const upstream = try DNS.Peer.connect(nameserver, 53);
const upstream = try network.Peer.connect(nameserver, 53);
 
var request: [1024]u8 = undefined;
const msg = try DNS.Message.query(&[1][]const u8{domain orelse "gr.ht."}, &request);
@@ -40,6 +40,7 @@ test main {
}
 
const DNS = @import("dns.zig");
const network = @import("network.zig");
 
const std = @import("std");
const log = std.log;
 
src/dns.zig added: 67, removed: 64, total 3
@@ -1,58 +1,3 @@
pub const Peer = struct {
addr: std.net.Address,
sock: std.posix.socket_t,
 
/// TODO ipv6
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),
} } },
.sock = try std.posix.socket(std.posix.AF.INET, std.posix.SOCK.DGRAM, 0),
};
return up;
}
 
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) !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: Peer, data: []const u8) !void {
const cnt = try std.posix.send(upstrm.sock, data, 0);
if (cnt != data.len) return error.TxFailed;
}
 
pub fn sendTo(upstrm: Peer, addr: std.net.Address, data: []const u8) !void {
const cnt = try std.posix.sendto(upstrm.sock, data, 0, &addr.any, addr.getOsSockLen());
if (cnt != data.len) return error.TxFailed;
}
 
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: Peer, buffer: []u8, addr: *std.net.Address) !usize {
if (buffer.len < 512) return error.BufferTooSmall;
var src_len: u32 = addr.getOsSockLen();
const cnt = try std.posix.recvfrom(upstrm.sock, buffer, 0, &addr.any, &src_len);
if (cnt >= 512) {
@panic("packet too large");
}
return cnt;
}
};
 
pub const Label = struct {
len: u6,
name: []const u8,
@@ -432,5 +377,3 @@ const std = @import("std");
const log = std.log;
const Allocator = std.mem.Allocator;
const indexOfScalar = std.mem.indexOfScalar;
const nativeToBig = std.mem.nativeToBig;
const bytesToValue = std.mem.bytesToValue;
 
filename was Deleted added: 67, removed: 64, total 3
@@ -0,0 +1,58 @@
pub const Peer = struct {
addr: std.net.Address,
sock: std.posix.socket_t,
 
/// TODO ipv6
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),
} } },
.sock = try std.posix.socket(std.posix.AF.INET, std.posix.SOCK.DGRAM, 0),
};
return up;
}
 
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) !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: Peer, data: []const u8) !void {
const cnt = try std.posix.send(upstrm.sock, data, 0);
if (cnt != data.len) return error.TxFailed;
}
 
pub fn sendTo(upstrm: Peer, addr: std.net.Address, data: []const u8) !void {
const cnt = try std.posix.sendto(upstrm.sock, data, 0, &addr.any, addr.getOsSockLen());
if (cnt != data.len) return error.TxFailed;
}
 
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: Peer, buffer: []u8, addr: *std.net.Address) !usize {
if (buffer.len < 512) return error.BufferTooSmall;
var src_len: u32 = addr.getOsSockLen();
const cnt = try std.posix.recvfrom(upstrm.sock, buffer, 0, &addr.any, &src_len);
if (cnt >= 512) {
@panic("packet too large");
}
return cnt;
}
};
 
const std = @import("std");
const nativeToBig = std.mem.nativeToBig;
const bytesToValue = std.mem.bytesToValue;
 
src/server.zig added: 67, removed: 64, total 3
@@ -25,7 +25,7 @@ pub fn main() !void {
}
}
 
const downstream: DNS.Peer = try .listen(.{ 0, 0, 0, 0 }, 53);
const downstream: network.Peer = try .listen(.{ 0, 0, 0, 0 }, 53);
 
// nobody on my machine
if (std.os.linux.getuid() == 0) {
@@ -57,7 +57,7 @@ pub fn main() !void {
try tld.zones.put(a, domain.zone, .{ .behavior = .{ .nxdomain = 300 } });
}
 
var upconns: [4]DNS.Peer = undefined;
var upconns: [4]network.Peer = undefined;
for (&upconns, upstreams) |*dst, ip| {
dst.* = try .connect(ip, 53);
}
@@ -311,6 +311,7 @@ test main {
}
 
const DNS = @import("dns.zig");
const network = @import("network.zig");
 
const std = @import("std");
const log = std.log;