diff options
| author | Robby Zambito <contact@robbyzambito.me> | 2026-01-18 00:03:06 -0500 |
|---|---|---|
| committer | Robby Zambito <contact@robbyzambito.me> | 2026-01-18 17:20:44 -0500 |
| commit | 8a53c7366aca7530bdb367ea46293a6109000d41 (patch) | |
| tree | a51f57c02b0188d1e18bcc83bff97ed23d92df20 /src/main.zig | |
| parent | a33a96dafd4c483f08ad3876d1a90cd18ab12c43 (diff) | |
Diffstat (limited to 'src/main.zig')
| -rw-r--r-- | src/main.zig | 51 |
1 files changed, 40 insertions, 11 deletions
diff --git a/src/main.zig b/src/main.zig index ad4d76b..b9d9e88 100644 --- a/src/main.zig +++ b/src/main.zig @@ -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)); + }), @sizeOf(std.os.linux.sockaddr.ll)); + + 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 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 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; |
