@@ -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);