Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AK: Introduce IPAddressCidr datatype #25006

Open
wants to merge 16 commits into
base: master
Choose a base branch
from

Conversation

famfo
Copy link
Contributor

@famfo famfo commented Sep 9, 2024

This commit introduces a CIDR datatype which will become handy especially for IPv6 to not have to deal with netmasks.

@github-actions github-actions bot added the 👀 pr-needs-review PR needs review from a maintainer or community member label Sep 9, 2024
Copy link
Collaborator

@kleinesfilmroellchen kleinesfilmroellchen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(note to other reviewers: this is part of SIP6TF, so I was consulted on quite a bit of this code and may be biased)

Very nice work, and extensive tests, I like it. I'm not happy with the name though, so some bikeshedding may be required.

AK/IpAddressCidr.h Outdated Show resolved Hide resolved
AK/IpAddressCidr.h Outdated Show resolved Hide resolved
AK/IpAddressCidr.h Outdated Show resolved Hide resolved
AK/IpAddressCidr.h Outdated Show resolved Hide resolved
AK/IpAddressCidr.h Outdated Show resolved Hide resolved
AK/IpAddressCidr.h Outdated Show resolved Hide resolved
AK/IpAddressCidr.h Outdated Show resolved Hide resolved
AK/IpAddressCidr.h Outdated Show resolved Hide resolved
AK/IpAddressCidr.h Outdated Show resolved Hide resolved
AK/IpAddressCidr.h Outdated Show resolved Hide resolved
Copy link
Contributor

@Hendiadyoin1 Hendiadyoin1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mainly Nits on my end, that would likely need support from other places in the code base to resolve

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer CIDR

AK/IpAddressCidr.h Outdated Show resolved Hide resolved
Comment on lines 80 to 89
auto address_string = TRY(m_address.to_string());

#ifdef KERNEL
TRY(builder.try_append(address_string->view()));
#else
TRY(builder.try_append(address_string));
#endif

TRY(builder.try_append('/'));
TRY(builder.try_appendff("{}", m_length));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally this would just be a

#ifdef KERNEL
        return KSting::formatted("{}/{}", m_address, m_length());
#else
        return Sting::try_formatted("{}/{}", m_address, m_length());
#endif

but we're likely missing a custom formatter, are we?
Note that fallibility in the Userland case is not enforced (anymore)

Copy link
Contributor Author

@famfo famfo Sep 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just doing

#ifdef KERNEL
    ErrorOr<NonnullOwnPtr<Kernel::KString>> to_string() const
    {
        return KString::formatted("{}/{}", m_address, m_length);
    }
#else
    ErrorOr<String> to_string() const
    {
        return String::formatted("{}/{}", m_address, m_length);
    }
#endif

works is this case both for IPv4 and IPv6 in hosts and serenity tests. I'll adopt this for formatting.

AK/IpAddressCidr.h Outdated Show resolved Hide resolved
AK/IpAddressCidr.h Show resolved Hide resolved
AK/IpAddressCidr.h Outdated Show resolved Hide resolved
AK/IpAddressCidr.h Outdated Show resolved Hide resolved
AK/IpAddressCidr.h Outdated Show resolved Hide resolved
Comment on lines +271 to +285
template<>
struct Formatter<IPv6AddressCidr> : Formatter<StringView> {
ErrorOr<void> format(FormatBuilder& builder, IPv6AddressCidr value)
{
return Formatter<StringView>::format(builder, TRY(value.to_string())->view());
}
};
#else
template<>
struct Formatter<IPv6AddressCidr> : Formatter<StringView> {
ErrorOr<void> format(FormatBuilder& builder, IPv6AddressCidr value)
{
return Formatter<StringView>::format(builder, TRY(value.to_string()));
}
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the OG commit

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I meant with this, is that this could mirror the actual implementation of the CIDR to_string method,
maybe even replace it.

With some template shenanigans (Ie DerivedFrom<IpAddressCIDR>) all formatters could use the same code

Comment on lines +177 to +191
template<>
struct Formatter<IPv4AddressCidr> : Formatter<StringView> {
ErrorOr<void> format(FormatBuilder& builder, IPv4AddressCidr value)
{
return Formatter<StringView>::format(builder, TRY(value.to_string())->view());
}
};
#else
template<>
struct Formatter<IPv4AddressCidr> : Formatter<StringView> {
ErrorOr<void> format(FormatBuilder& builder, IPv4AddressCidr value)
{
return Formatter<StringView>::format(builder, TRY(value.to_string()));
}
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See OG commit

@BuggieBot
Copy link
Member

Hello!

One or more of the commit messages in this PR do not match the SerenityOS code submission policy, please check the lint_commits CI job for more details on which commits were flagged and why.
Please do not close this PR and open another, instead modify your commit message(s) with git commit --amend and force push those changes to update this PR.

@famfo
Copy link
Contributor Author

famfo commented Sep 10, 2024

I will squash this again later when the code itself is fine

@famfo famfo marked this pull request as draft September 10, 2024 23:25
@github-actions github-actions bot removed the 👀 pr-needs-review PR needs review from a maintainer or community member label Sep 10, 2024
AK/IpAddressCidr.h Show resolved Hide resolved
AK/IpAddressCidr.h Outdated Show resolved Hide resolved
Tests/AK/TestIPv4AddressCidr.cpp Outdated Show resolved Hide resolved
Tests/AK/TestIPv4AddressCidr.cpp Outdated Show resolved Hide resolved
Tests/AK/TestIPv4AddressCidr.cpp Outdated Show resolved Hide resolved
Tests/AK/TestIPv4AddressCidr.cpp Show resolved Hide resolved
Tests/AK/TestIPv6Address.cpp Outdated Show resolved Hide resolved
Tests/AK/TestIPv6AddressCidr.cpp Outdated Show resolved Hide resolved
@famfo famfo marked this pull request as ready for review September 28, 2024 22:20
@github-actions github-actions bot added the 👀 pr-needs-review PR needs review from a maintainer or community member label Sep 28, 2024
Copy link
Collaborator

@kleinesfilmroellchen kleinesfilmroellchen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only Leon's comment about string formatting needs to be resolved, I think we want to switch the implementation place and move the implementation into the Formatter, while to_string only calls String::formatted.

Copy link

stale bot commented Nov 5, 2024

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions!

@stale stale bot added the stale label Nov 5, 2024
@famfo
Copy link
Contributor Author

famfo commented Nov 7, 2024

I will pick this up again eventually, currently a bit short on time unfortunately.

@stale stale bot removed the stale label Nov 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
👀 pr-needs-review PR needs review from a maintainer or community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants