mirror of
https://git.robbyzambito.me/zaprus
synced 2026-05-06 06:10:38 +00:00
refactor: use packed struct for IpAddr
This is more intuitive than using u32 directly, and follows the same pattern as the new MAC addr handling
This commit is contained in:
@@ -46,11 +46,11 @@ pub fn sendRelay(self: *Client, io: Io, payload: []const u8, dest: [4]u8) !void
|
|||||||
const rand = io_source.interface();
|
const rand = io_source.interface();
|
||||||
|
|
||||||
var headers: EthIpUdp = .{
|
var headers: EthIpUdp = .{
|
||||||
.src_mac = .fromSlice(self.socket.mac),
|
.src_mac = .fromBytes(self.socket.mac),
|
||||||
.ip = .{
|
.ip = .{
|
||||||
.id = rand.int(u16),
|
.id = rand.int(u16),
|
||||||
.src_addr = 0, //rand.int(u32),
|
.src_addr = .fromBytes(.{ 0, 0, 0, 0 }), //rand.int(u32),
|
||||||
.dst_addr = @bitCast([_]u8{ 255, 255, 255, 255 }),
|
.dst_addr = .fromBytes(.{ 255, 255, 255, 255 }),
|
||||||
.len = undefined,
|
.len = undefined,
|
||||||
},
|
},
|
||||||
.udp = .{
|
.udp = .{
|
||||||
@@ -86,11 +86,11 @@ pub fn connect(self: Client, io: Io, payload: []const u8) (error{ BpfAttachFaile
|
|||||||
const rand = io_source.interface();
|
const rand = io_source.interface();
|
||||||
|
|
||||||
var headers: EthIpUdp = .{
|
var headers: EthIpUdp = .{
|
||||||
.src_mac = .fromSlice(self.socket.mac),
|
.src_mac = .fromBytes(self.socket.mac),
|
||||||
.ip = .{
|
.ip = .{
|
||||||
.id = rand.int(u16),
|
.id = rand.int(u16),
|
||||||
.src_addr = 0, //rand.int(u32),
|
.src_addr = .fromBytes(.{ 0, 0, 0, 0 }), //rand.int(u32),
|
||||||
.dst_addr = @bitCast([_]u8{ 255, 255, 255, 255 }),
|
.dst_addr = .fromBytes(.{ 255, 255, 255, 255 }),
|
||||||
.len = undefined,
|
.len = undefined,
|
||||||
},
|
},
|
||||||
.udp = .{
|
.udp = .{
|
||||||
|
|||||||
@@ -14,6 +14,28 @@
|
|||||||
// You should have received a copy of the GNU General Public License along with
|
// You should have received a copy of the GNU General Public License along with
|
||||||
// Zaprus. If not, see <https://www.gnu.org/licenses/>.
|
// Zaprus. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
pub const IpAddr = packed struct {
|
||||||
|
int: I,
|
||||||
|
|
||||||
|
const V = @Vector(4, u8);
|
||||||
|
const I = u32;
|
||||||
|
|
||||||
|
pub fn fromBytes(s: V) IpAddr {
|
||||||
|
return .{ .int = @bitCast(s) };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const MacAddr = packed struct {
|
||||||
|
int: I,
|
||||||
|
|
||||||
|
const V = @Vector(6, u8);
|
||||||
|
const I = @Int(.unsigned, @bitSizeOf(V));
|
||||||
|
|
||||||
|
pub fn fromBytes(s: V) MacAddr {
|
||||||
|
return .{ .int = @bitCast(s) };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
pub const EthIpUdp = packed struct(u336) { // 42 bytes * 8 bits = 336
|
pub const EthIpUdp = packed struct(u336) { // 42 bytes * 8 bits = 336
|
||||||
// --- UDP (Last in memory, defined first for LSB->MSB) ---
|
// --- UDP (Last in memory, defined first for LSB->MSB) ---
|
||||||
udp: packed struct {
|
udp: packed struct {
|
||||||
@@ -25,8 +47,8 @@ pub const EthIpUdp = packed struct(u336) { // 42 bytes * 8 bits = 336
|
|||||||
|
|
||||||
// --- IP ---
|
// --- IP ---
|
||||||
ip: packed struct {
|
ip: packed struct {
|
||||||
dst_addr: u32,
|
dst_addr: IpAddr,
|
||||||
src_addr: u32,
|
src_addr: IpAddr,
|
||||||
header_checksum: u16 = 0,
|
header_checksum: u16 = 0,
|
||||||
protocol: u8 = 17, // udp
|
protocol: u8 = 17, // udp
|
||||||
ttl: u8 = 0x40,
|
ttl: u8 = 0x40,
|
||||||
@@ -54,18 +76,7 @@ pub const EthIpUdp = packed struct(u336) { // 42 bytes * 8 bits = 336
|
|||||||
// --- Ethernet ---
|
// --- Ethernet ---
|
||||||
eth_type: u16 = std.os.linux.ETH.P.IP,
|
eth_type: u16 = std.os.linux.ETH.P.IP,
|
||||||
src_mac: MacAddr,
|
src_mac: MacAddr,
|
||||||
dst_mac: MacAddr = .fromSlice(@splat(0xff)),
|
dst_mac: MacAddr = .fromBytes(@splat(0xff)),
|
||||||
|
|
||||||
pub const MacAddr = packed struct {
|
|
||||||
int: I,
|
|
||||||
|
|
||||||
pub const V = @Vector(6, u8);
|
|
||||||
pub const I = @Int(.unsigned, @bitSizeOf(V));
|
|
||||||
|
|
||||||
pub fn fromSlice(s: V) MacAddr {
|
|
||||||
return .{ .int = @bitCast(s) };
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
pub fn toBytes(self: @This()) [336 / 8]u8 {
|
pub fn toBytes(self: @This()) [336 / 8]u8 {
|
||||||
var res: [336 / 8]u8 = undefined;
|
var res: [336 / 8]u8 = undefined;
|
||||||
|
|||||||
Reference in New Issue
Block a user