This commit is contained in:
2026-01-18 00:03:06 -05:00
parent a33a96dafd
commit 8a53c7366a

View File

@@ -97,26 +97,45 @@ pub fn main(init: std.process.Init) !void {
const linux_socket = std.os.linux.socket(AF.PACKET, SOCK.RAW, 0);
const errno = std.os.linux.errno(linux_socket);
if (errno != .SUCCESS) {
std.debug.log("Failed to open socket: {t}\n", .{errno});
std.debug.print("Failed to open socket: {t}\n", .{errno});
return error.Error; // TODO: better error
}
break :blk linux_socket;
};
const socket_fd = blk: {
const socket_fd = std.os.linux.bind(linux_socket, @bitCast(std.os.linux.sockaddr.ll{
.protocol = ,
const socket_fd = std.os.linux.bind(@intCast(linux_socket), @bitCast(std.os.linux.sockaddr.ll{
// https://codeberg.org/jeffective/gatorcat/src/commit/1da40c85c2d063368e2e5c130e654cb32b6bff0e/src/module/nic.zig#L137
.protocol = std.mem.nativeToBig(u16, @as(u16, std.os.linux.ETH.P.ALL)),
.ifindex = 1,
.hatype = ,
.pkttype = ,
.halen = ,
.hatype = 0,
.pkttype = 0,
.halen = 0,
.addr = @splat(0),
}), @sizeOf(std.os.linux.sockaddr.ll));
};
const ip: std.Io.net.IpAddress = .{ .ip4 = .unspecified(0) };
const socket = try ip.bind(init.io, .{ .mode = .raw, .protocol = .raw });
defer socket.close(init.io);
try socket.send(init.io, &.{ .ip4 = try .parse("255.255.255.255", 8888) }, "foo");
const errno = std.os.linux.errno(socket_fd);
if (errno != .SUCCESS) {
std.debug.print("Failed to create link layer socket: {t}\n", .{errno});
return error.Error; // TODO: better error
}
break :blk socket_fd;
};
const EthIpUdp = struct {
// eth
dst_mac: [6]u8 = @splat(0xff),
src_mac: [6]u8,
eth_type: u16 = std.os.linux.ETH.P.IP,
// ip
ip_version: u4 = 4,
};
// const ip: std.Io.net.IpAddress = .{ .ip4 = .unspecified(0) };
// const socket = try ip.bind(init.io, .{ .mode = .rdm, .protocol = .raw });
// defer socket.close(init.io);
// try socket.send(init.io, &.{ .ip4 = .{ .bytes = @splat(255), .port = 8888 } }, "foo");
// var sock_buffer: [1500]u8 = undefined;
// var raw_socket_writer: RawSocketWriter = try .init("enp7s0", &sock_buffer); // /proc/net/dev
@@ -170,6 +189,16 @@ const SaprusClient = zaprus.Client;
const SaprusMessage = zaprus.Message;
const RawSocketWriter = zaprus.RawSocketWriter;
// Import C headers for network constants and structs
const c = @cImport({
@cInclude("sys/socket.h");
@cInclude("linux/if_packet.h");
@cInclude("net/ethernet.h");
@cInclude("sys/ioctl.h");
@cInclude("net/if.h");
@cInclude("arpa/inet.h");
});
const AF = std.os.linux.AF;
const SOCK = std.os.linux.SOCK;
// const NetWriter = zaprus.NetWriter;