Skip to content

Commit

Permalink
refactor: remove is_loopback from MacAddress
Browse files Browse the repository at this point in the history
related: #9
  • Loading branch information
weskoerber committed Sep 12, 2024
1 parent 668ffc6 commit 6469581
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
3 changes: 0 additions & 3 deletions lib/MacAddress.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/// Determines whether MAC address belongs to a loopback device
is_loopback: bool,

/// Individual bytes of the MAC address.
data: [6]u8,

Expand Down
31 changes: 21 additions & 10 deletions lib/linux.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/// The caller is owns the returned memory.
pub fn getAll(allocator: mem.Allocator) ![]MacAddress {
var addrs = std.ArrayList(MacAddress).init(allocator);
var iter = try IfIterator.initAlloc(allocator);
var iter = try IfIterator.initAlloc(allocator, .{});
defer iter.deinit();

while (try iter.next()) |addr| {
Expand All @@ -33,14 +33,10 @@ pub fn getAll(allocator: mem.Allocator) ![]MacAddress {
/// The returned memory is stack allocated and returned by value; the caller
/// need not free anything.
pub fn getFirstNoLoopback(allocator: mem.Allocator) !MacAddress {
var iter = try IfIterator.initAlloc(allocator);
var iter = try IfIterator.initAlloc(allocator, .{ .skip_loopback = true });
defer iter.deinit();

while (try iter.next()) |addr| {
if (addr.is_loopback) {
continue;
}

return addr;
}

Expand All @@ -63,8 +59,13 @@ const IfIterator = struct {
buffer: []ifreq,
index: usize,
sock_fd: i32,
iterator_options: IteratorOptions,

pub const IteratorOptions = struct {
skip_loopback: bool = false,
};

pub fn initAlloc(allocator: mem.Allocator) !IfIterator {
pub fn initAlloc(allocator: mem.Allocator, iterator_options: IteratorOptions) !IfIterator {
const sock = linux.socket(linux.AF.INET, linux.SOCK.DGRAM, linux.IPPROTO.IP);
const sock_fd = if (posix.errno(sock) == .SUCCESS)
@as(linux.fd_t, @intCast(sock))
Expand All @@ -87,6 +88,7 @@ const IfIterator = struct {
.buffer = elems,
.index = 0,
.sock_fd = sock_fd,
.iterator_options = iterator_options,
};
}

Expand All @@ -101,13 +103,22 @@ const IfIterator = struct {
}

const elem = &self.buffer[self.index];
var addr = mem.zeroes(MacAddress);

std.debug.print("if: {s}\n", .{elem.ifrn.name});

ioctlReq(self.sock_fd, SIOCGIFFLAGS, elem) catch return MacAddressError.OsError;
addr.is_loopback = elem.ifru.flags & IFF_LOOPBACK != 0;
const is_loopback = elem.ifru.flags & IFF_LOOPBACK != 0;

if (self.iterator_options.skip_loopback and is_loopback) {
self.index += 1;
return self.next();
}

ioctlReq(self.sock_fd, SIOCGIFHWADDR, elem) catch return MacAddressError.OsError;
addr.data = elem.ifru.hwaddr.data[0..6].*;
const hwaddr = elem.ifru.hwaddr.data[0..6].*;

var addr = mem.zeroes(MacAddress);
addr.data = hwaddr;

self.index += 1;

Expand Down
3 changes: 1 addition & 2 deletions lib/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,5 @@ test "get_all" {
test "get_first_no_loopback" {
const addr = try getFirstNoLoopback(testing.allocator);

try testing.expectEqual(false, addr.is_loopback);
try testing.expect(!std.mem.eql(u8, &addr.data, &.{ 0, 0, 0, 0, 0, 0 }));
try testing.expectEqualSlices(u8, &addr.data, &.{ 0, 0, 0, 0, 0, 0 });
}

0 comments on commit 6469581

Please sign in to comment.