restrict domain to 5 segments at max

This commit is contained in:
Muaz Ahmad 2024-05-10 20:33:00 +05:00
parent 0fcd68c38b
commit b147257993

View file

@ -7,19 +7,18 @@ const ArgError = error{
};
pub const IP_VER_ENUM = enum(u3) {
Both = 0,
IPv4 = 4,
IPv6 = 6,
};
pub const IPs = struct {
v4: [:0]const u8,
v6: [:0]const u8,
v4: []const u8,
v6: []const u8,
};
pub const Domain = struct {
name: [:0]const u8,
labels: u4,
labels: [5][]const u8,
};
pub fn check_perms() !void {
@ -38,7 +37,7 @@ pub fn get_input() !struct {
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;
const ip_ver = std.meta.intToEnum(IP_VER_ENUM, std.fmt.parseInt(u3, ip_ver_str, 10) catch return ArgError.InvalidAddressVer) catch return ArgError.InvalidAddressVer;
return .{ domain, ip_ver };
}
@ -46,15 +45,20 @@ pub fn get_input() !struct {
fn check_domain(domain_str: [:0]const u8) !Domain {
var domain = Domain{
.name = domain_str,
.labels = 0,
.labels = [_][]const u8{&[_]u8{}} ** 5,
};
var labels = std.mem.split(u8, domain_str, ".");
var last: []const u8 = "";
var i: usize = 0;
while (labels.next()) |segment| {
last = segment;
domain.labels += 1;
if (i >= 5) {
return ArgError.BadDomain;
}
if (domain.labels <= 1 or !std.mem.eql(u8, last, "local")) {
last = segment;
domain.labels[i] = segment;
i += 1;
}
if (domain.labels.len <= 1 or !std.mem.eql(u8, last, "local")) {
return ArgError.BadDomain;
}
return domain;