Add param to specify ip address version

This commit is contained in:
Muaz Ahmad 2024-05-10 15:05:57 +05:00
parent b9f42d2ed2
commit 0fcd68c38b
3 changed files with 25 additions and 9 deletions

View file

@ -1,6 +1,6 @@
const std = @import("std"); const std = @import("std");
const util = @import("util.zig"); const util = @import("util.zig");
pub fn update_hosts(ips: util.IPs) !void { pub fn update_hosts(ips: util.IPs, ip_ver: util.IP_VER_ENUM) !void {
std.debug.print("{any}\n", .{ips}); std.debug.print("{any} {any}\n", .{ ips, ip_ver });
} }

View file

@ -5,7 +5,7 @@ const hosts = @import("hosts.zig");
pub fn main() !void { pub fn main() !void {
try util.check_perms(); try util.check_perms();
const domain = try util.get_input(); const domain, const ip_ver = try util.get_input();
const ips = try mdns.get_mdns(domain); const ips = try mdns.get_mdns(domain);
try hosts.update_hosts(ips); try hosts.update_hosts(ips, ip_ver);
} }

View file

@ -3,6 +3,13 @@ const std = @import("std");
const ArgError = error{ const ArgError = error{
NotEnoughArgs, NotEnoughArgs,
BadDomain, BadDomain,
InvalidAddressVer,
};
pub const IP_VER_ENUM = enum(u3) {
Both = 0,
IPv4 = 4,
IPv6 = 6,
}; };
pub const IPs = struct { pub const IPs = struct {
@ -12,7 +19,7 @@ pub const IPs = struct {
pub const Domain = struct { pub const Domain = struct {
name: [:0]const u8, name: [:0]const u8,
labels: u8, labels: u4,
}; };
pub fn check_perms() !void { pub fn check_perms() !void {
@ -20,11 +27,20 @@ pub fn check_perms() !void {
f.close(); f.close();
} }
pub fn get_input() !Domain { pub fn get_input() !struct {
Domain,
IP_VER_ENUM,
} {
var args = std.process.args(); var args = std.process.args();
_ = args.next(); _ = args.next(); // consume calling binary arg
const domain = args.next() orelse return ArgError.NotEnoughArgs;
return check_domain(domain); const domain_str = args.next() orelse return ArgError.NotEnoughArgs;
const domain = try check_domain(domain_str);
const ip_ver_str = args.next() orelse return ArgError.NotEnoughArgs;
const ip_ver = std.meta.intToEnum(IP_VER_ENUM, try std.fmt.parseInt(u3, ip_ver_str, 10)) catch return ArgError.InvalidAddressVer;
return .{ domain, ip_ver };
} }
fn check_domain(domain_str: [:0]const u8) !Domain { fn check_domain(domain_str: [:0]const u8) !Domain {