mirror of
https://git.robbyzambito.me/zaprus/
synced 2026-02-04 03:34:48 +00:00
Start adding connection message
This commit is contained in:
57
src/main.zig
57
src/main.zig
@@ -90,10 +90,10 @@ pub fn main(init: std.process.Init) !void {
|
|||||||
std.debug.print("dest: {s}\n", .{flags.dest orelse "<null>"});
|
std.debug.print("dest: {s}\n", .{flags.dest orelse "<null>"});
|
||||||
std.debug.print("connect: {s}\n", .{flags.connect orelse "<null>"});
|
std.debug.print("connect: {s}\n", .{flags.connect orelse "<null>"});
|
||||||
|
|
||||||
// const rand = blk: {
|
const rand = blk: {
|
||||||
// const io_source: std.Random.IoSource = .{ .io = init.io };
|
const io_source: std.Random.IoSource = .{ .io = init.io };
|
||||||
// break :blk io_source.interface();
|
break :blk io_source.interface();
|
||||||
// };
|
};
|
||||||
|
|
||||||
// const net_interface: std.Io.net.Interface = .{ .index = 1 };
|
// const net_interface: std.Io.net.Interface = .{ .index = 1 };
|
||||||
// std.debug.print("Interface: {s}\n", .{(try net_interface.name(init.io)).toSlice()});
|
// std.debug.print("Interface: {s}\n", .{(try net_interface.name(init.io)).toSlice()});
|
||||||
@@ -153,21 +153,22 @@ pub fn main(init: std.process.Init) !void {
|
|||||||
};
|
};
|
||||||
|
|
||||||
var headers: EthIpUdp = .{
|
var headers: EthIpUdp = .{
|
||||||
.src_mac = @splat(0x0e),
|
.src_mac = undefined, // TODO: REAL MAC
|
||||||
.ip = .{
|
.ip = .{
|
||||||
.id = 0,
|
.id = 0,
|
||||||
.src_addr = 0,
|
.src_addr = rand.int(u32),
|
||||||
.dst_addr = @bitCast([_]u8{ 255, 255, 255, 255 }),
|
.dst_addr = @bitCast([_]u8{ 255, 255, 255, 255 }),
|
||||||
.len = undefined,
|
.len = undefined,
|
||||||
},
|
},
|
||||||
.udp = .{
|
.udp = .{
|
||||||
.src_port = undefined, // TODO: change this?
|
.src_port = rand.intRangeAtMost(u16, 1025, std.math.maxInt(u16)),
|
||||||
.dst_port = 8888,
|
.dst_port = 8888,
|
||||||
.len = undefined,
|
.len = undefined,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
std.debug.print("headers: {any}\n", .{&headers.toBytes()});
|
std.debug.print("headers: {any}\n", .{&headers.toBytes()});
|
||||||
|
|
||||||
|
if (flags.relay != null) {
|
||||||
const relay: SaprusMessage = .{
|
const relay: SaprusMessage = .{
|
||||||
.relay = .{
|
.relay = .{
|
||||||
.dest = .fromBytes(&parseDest(flags.dest)),
|
.dest = .fromBytes(&parseDest(flags.dest)),
|
||||||
@@ -193,6 +194,48 @@ pub fn main(init: std.process.Init) !void {
|
|||||||
var socket: RawSocket = try .init("enp7s0");
|
var socket: RawSocket = try .init("enp7s0");
|
||||||
defer socket.deinit();
|
defer socket.deinit();
|
||||||
try socket.send(full_msg);
|
try socket.send(full_msg);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (flags.connect != null) {
|
||||||
|
const dest = rand.intRangeAtMost(u16, 1025, std.math.maxInt(u16));
|
||||||
|
const src = rand.intRangeAtMost(u16, 1025, std.math.maxInt(u16));
|
||||||
|
// udp dest port should not be 8888 after first
|
||||||
|
const udp_dest_port = rand.intRangeAtMost(u16, 1025, std.math.maxInt(u16));
|
||||||
|
const connection: SaprusMessage = .{
|
||||||
|
.connection = .{
|
||||||
|
.src = src,
|
||||||
|
.dest = dest,
|
||||||
|
.seq = undefined,
|
||||||
|
.id = undefined,
|
||||||
|
.payload = flags.connect.?,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
_ = udp_dest_port;
|
||||||
|
|
||||||
|
var connection_buf: [2048]u8 = undefined;
|
||||||
|
const connection_bytes = connection.toBytes(&connection_buf);
|
||||||
|
std.debug.print("connection: {any}\n", .{connection_bytes});
|
||||||
|
headers.setPayloadLen(connection_bytes.len);
|
||||||
|
|
||||||
|
const 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();
|
||||||
|
};
|
||||||
|
|
||||||
|
std.debug.print("full message = {any}\n", .{full_msg});
|
||||||
|
|
||||||
|
var socket: RawSocket = try .init("enp7s0");
|
||||||
|
defer socket.deinit();
|
||||||
|
try socket.send(full_msg);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
unreachable;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parseDest(in: ?[]const u8) [4]u8 {
|
fn parseDest(in: ?[]const u8) [4]u8 {
|
||||||
|
|||||||
@@ -123,7 +123,7 @@ const Relay = struct {
|
|||||||
pub fn toBytes(self: Relay, buf: []u8) []u8 {
|
pub fn toBytes(self: Relay, buf: []u8) []u8 {
|
||||||
var out: Writer = .fixed(buf);
|
var out: Writer = .fixed(buf);
|
||||||
out.writeInt(u16, @intFromEnum(PacketType.relay), .big) catch unreachable;
|
out.writeInt(u16, @intFromEnum(PacketType.relay), .big) catch unreachable;
|
||||||
out.writeInt(u16, @intCast(self.payload.len), .big) catch unreachable; // Length field, but unread. Will switch to checksum
|
out.writeInt(u16, @intCast(self.payload.len + 4), .big) catch unreachable; // Length field, but unread. Will switch to checksum
|
||||||
out.writeAll(&self.dest.bytes) catch unreachable;
|
out.writeAll(&self.dest.bytes) catch unreachable;
|
||||||
out.writeAll(self.payload) catch unreachable;
|
out.writeAll(self.payload) catch unreachable;
|
||||||
return out.buffered();
|
return out.buffered();
|
||||||
@@ -177,7 +177,7 @@ const Connection = struct {
|
|||||||
pub fn toBytes(self: Connection, buf: []u8) []u8 {
|
pub fn toBytes(self: Connection, buf: []u8) []u8 {
|
||||||
var out: Writer = .fixed(buf);
|
var out: Writer = .fixed(buf);
|
||||||
out.writeInt(u16, @intFromEnum(PacketType.connection), .big) catch unreachable;
|
out.writeInt(u16, @intFromEnum(PacketType.connection), .big) catch unreachable;
|
||||||
out.writeInt(u16, undefined, .big) catch unreachable; // Saprus length field, unread.
|
out.writeInt(u16, @intCast(self.payload.len + 14), .big) catch unreachable; // Saprus length field, unread.
|
||||||
out.writeInt(u16, self.src, .big) catch unreachable;
|
out.writeInt(u16, self.src, .big) catch unreachable;
|
||||||
out.writeInt(u16, self.dest, .big) catch unreachable;
|
out.writeInt(u16, self.dest, .big) catch unreachable;
|
||||||
out.writeInt(u32, self.seq, .big) catch unreachable;
|
out.writeInt(u32, self.seq, .big) catch unreachable;
|
||||||
|
|||||||
Reference in New Issue
Block a user