summaryrefslogtreecommitdiff
path: root/src/Client.zig
blob: 3425bbad1ccf82d01dc80ac06ec7e1f88b31cc6a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const base64_enc = std.base64.standard.Encoder;
const base64_dec = std.base64.standard.Decoder;

const Client = @This();

const max_message_size = 2048;

pub const max_payload_len = RawSocket.max_payload_len;

socket: RawSocket,

pub fn init() !Client {
    const socket: RawSocket = try .init();
    return .{
        .socket = socket,
    };
}

pub fn deinit(self: *Client) void {
    self.socket.deinit();
    self.* = undefined;
}

pub fn sendRelay(self: *Client, io: Io, payload: []const u8, dest: [4]u8) !void {
    const rand = blk: {
        const io_source: std.Random.IoSource = .{ .io = io };
        break :blk io_source.interface();
    };

    var headers: EthIpUdp = .{
        .src_mac = self.socket.mac,
        .ip = .{
            .id = rand.int(u16),
            .src_addr = 0, //rand.int(u32),
            .dst_addr = @bitCast([_]u8{ 255, 255, 255, 255 }),
            .len = undefined,
        },
        .udp = .{
            .src_port = rand.intRangeAtMost(u16, 1025, std.math.maxInt(u16)),
            .dst_port = 8888,
            .len = undefined,
        },
    };

    const relay: SaprusMessage = .{
        .relay = .{
            .dest = .fromBytes(&dest),
            .payload = payload,
        },
    };

    var relay_buf: [max_message_size - (@bitSizeOf(EthIpUdp) / 8)]u8 = undefined;
    const relay_bytes = relay.toBytes(&relay_buf);
    headers.setPayloadLen(relay_bytes.len);

    const full_msg = blk: {
        var msg_buf: [max_message_size]u8 = undefined;
        var msg_w: Writer = .fixed(&msg_buf);
        msg_w.writeAll(&headers.toBytes()) catch unreachable;
        msg_w.writeAll(relay_bytes) catch unreachable;
        break :blk msg_w.buffered();
    };

    try self.socket.send(full_msg);
}

pub fn connect(self: Client, io: Io, payload: []const u8) !SaprusConnection {
    const rand = blk: {
        const io_source: std.Random.IoSource = .{ .io = io };
        break :blk io_source.interface();
    };

    var headers: EthIpUdp = .{
        .src_mac = self.socket.mac,
        .ip = .{
            .id = rand.int(u16),
            .src_addr = 0, //rand.int(u32),
            .dst_addr = @bitCast([_]u8{ 255, 255, 255, 255 }),
            .len = undefined,
        },
        .udp = .{
            .src_port = rand.intRangeAtMost(u16, 1025, std.math.maxInt(u16)),
            .dst_port = 8888,
            .len = undefined,
        },
    };

    // udp dest port should not be 8888 after first
    const udp_dest_port = rand.intRangeAtMost(u16, 9000, std.math.maxInt(u16));
    var connection: SaprusMessage = .{
        .connection = .{
            .src = rand.intRangeAtMost(u16, 1025, std.math.maxInt(u16)),
            .dest = rand.intRangeAtMost(u16, 1025, std.math.maxInt(u16)),
            .seq = undefined,
            .id = undefined,
            .payload = payload,
        },
    };

    log.debug("Setting bpf filter to port {}", .{connection.connection.src});
    try self.socket.attachSaprusPortFilter(connection.connection.src);
    log.debug("bpf set", .{});

    var connection_buf: [2048]u8 = undefined;
    var connection_bytes = connection.toBytes(&connection_buf);
    headers.setPayloadLen(connection_bytes.len);

    log.debug("Building full message", .{});
    var full_msg = blk: {
        var msg_buf: [2048]u8 = undefined;
        var msg_w: Writer = .fixed(&msg_buf);
        msg_w.writeAll(&headers.toBytes()) catch unreachable;
        msg_w.writeAll(connection_bytes) catch unreachable;
        break :blk msg_w.buffered();
    };
    log.debug("Built full message. Sending message", .{});

    try self.socket.send(full_msg);
    var res_buf: [4096]u8 = undefined;

    // Ignore response from sentinel, just accept that we got one.
    log.debug("Awaiting handshake response", .{});
    _ = try self.socket.receive(&res_buf);
    try io.sleep(.fromMilliseconds(40), .real);

    headers.udp.dst_port = udp_dest_port;
    headers.ip.id = rand.int(u16);
    headers.setPayloadLen(connection_bytes.len);

    log.debug("Building final handshake message", .{});
    full_msg = blk: {
        var msg_buf: [2048]u8 = undefined;
        var msg_w: Writer = .fixed(&msg_buf);
        msg_w.writeAll(&headers.toBytes()) catch unreachable;
        msg_w.writeAll(connection_bytes) catch unreachable;
        break :blk msg_w.buffered();
    };
    try self.socket.send(full_msg);

    return .init(self.socket, headers, connection);
}

const RawSocket = @import("./RawSocket.zig");

const SaprusMessage = @import("message.zig").Message;
const SaprusConnection = @import("Connection.zig");
const EthIpUdp = @import("./EthIpUdp.zig").EthIpUdp;

const std = @import("std");
const Io = std.Io;
const Writer = std.Io.Writer;
const log = std.log;