summaryrefslogtreecommitdiff
path: root/src/main.zig
blob: fd77d301b84d09b2dcda21f61a963c5171e26738 (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
const is_debug = builtin.mode == .Debug;
const base64 = std.base64.Base64Encoder.init(std.base64.standard_alphabet_chars, '=');

const SaprusPacketType = enum(u16) {
    relay = 0x003C,
    file_transfer = 0x8888,
    connection = 0x00E9,
};

const SaprusConnectionOptions = packed struct(u8) {
    opt1: bool = false,
    opt2: bool = false,
    opt3: bool = false,
    opt4: bool = false,
    opt5: bool = false,
    opt6: bool = false,
    opt7: bool = false,
    opt8: bool = false,
};

const SaprusMessage = union(SaprusPacketType) {
    const Relay = struct {
        header: packed struct {
            dest: @Vector(4, u8),
        },
        payload: []const u8,
    };
    const Connection = struct {
        header: packed struct {
            src_port: u16,
            dest_port: u16,
            seq_num: u32 = 0,
            msg_id: u32 = 0,
            reserved: u8 = 0,
            options: SaprusConnectionOptions = .{},
        },
        payload: []const u8,
    };
    relay: Relay,
    file_transfer: void, // unimplemented
    connection: Connection,

    fn toBytes(self: SaprusMessage, allocator: Allocator) ![]u8 {
        var buf = std.ArrayList(u8).init(allocator);
        const w = buf.writer();
        try w.writeInt(u16, @intFromEnum(self), .big);

        switch (self) {
            .relay => |r| {
                try w.writeStructEndian(r.header, .big);
                try w.writeInt(u16, @intCast(r.payload.len), .big);
                try base64.encodeWriter(w, r.payload);
            },
            .file_transfer => unreachable,
            .connection => |c| {
                try w.writeStructEndian(c.header, .big);
                try w.writeInt(u16, @intCast(c.payload.len), .big);
                try base64.encodeWriter(w, c.payload);
            },
        }

        return buf.toOwnedSlice();
    }
};

pub fn main() !void {
    const DBA = std.heap.DebugAllocator(.{});
    var dba: ?DBA = if (comptime is_debug) DBA.init else null;
    defer if (dba) |*d| {
        _ = d.deinit();
    };

    var allocator = if (dba) |*d| d.allocator() else std.heap.smp_allocator;

    const msg = SaprusMessage{
        .relay = .{
            .header = .{ .dest = .{ 255, 255, 255, 255 } },
            .payload = "Hello darkness my old friend",
        },
    };

    const msg_bytes = try msg.toBytes(allocator);
    defer allocator.free(msg_bytes);

    try network.init();
    defer network.deinit();

    var sock = try network.Socket.create(.ipv4, .udp);
    defer sock.close();

    try sock.setBroadcast(true);

    // Bind to 0.0.0.0:0
    const bind_addr = network.EndPoint{
        .address = network.Address{ .ipv4 = network.Address.IPv4.any },
        .port = 0,
    };

    const dest_addr = network.EndPoint{
        .address = network.Address{ .ipv4 = network.Address.IPv4.broadcast },
        .port = 8888,
    };

    try sock.bind(bind_addr);

    _ = try sock.sendTo(dest_addr, msg_bytes);
}

const builtin = @import("builtin");
const std = @import("std");
const Allocator = std.mem.Allocator;

const network = @import("network");