mirror of
https://git.robbyzambito.me/zaprus
synced 2026-02-04 00:14:52 +00:00
This commit is contained in:
53
src/main.zig
53
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 linux_socket = std.os.linux.socket(AF.PACKET, SOCK.RAW, 0);
|
||||||
const errno = std.os.linux.errno(linux_socket);
|
const errno = std.os.linux.errno(linux_socket);
|
||||||
if (errno != .SUCCESS) {
|
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
|
return error.Error; // TODO: better error
|
||||||
}
|
}
|
||||||
break :blk linux_socket;
|
break :blk linux_socket;
|
||||||
};
|
};
|
||||||
const socket_fd = blk: {
|
const socket_fd = blk: {
|
||||||
const socket_fd = std.os.linux.bind(linux_socket, @bitCast(std.os.linux.sockaddr.ll{
|
const socket_fd = std.os.linux.bind(@intCast(linux_socket), @bitCast(std.os.linux.sockaddr.ll{
|
||||||
.protocol = ,
|
// 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,
|
.ifindex = 1,
|
||||||
.hatype = ,
|
.hatype = 0,
|
||||||
.pkttype = ,
|
.pkttype = 0,
|
||||||
.halen = ,
|
.halen = 0,
|
||||||
.addr = @splat(0),
|
.addr = @splat(0),
|
||||||
}), @sizeOf(std.os.linux.sockaddr.ll));
|
}), @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 sock_buffer: [1500]u8 = undefined;
|
||||||
// var raw_socket_writer: RawSocketWriter = try .init("enp7s0", &sock_buffer); // /proc/net/dev
|
// 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 SaprusMessage = zaprus.Message;
|
||||||
const RawSocketWriter = zaprus.RawSocketWriter;
|
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 AF = std.os.linux.AF;
|
||||||
const SOCK = std.os.linux.SOCK;
|
const SOCK = std.os.linux.SOCK;
|
||||||
// const NetWriter = zaprus.NetWriter;
|
// const NetWriter = zaprus.NetWriter;
|
||||||
|
|||||||
Reference in New Issue
Block a user