-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
[API Proposal]: Move IPNetwork to System.Net and add a string constructor #86584
Comments
Tagging subscribers to this area: @dotnet/ncl Issue DetailsBackground and motivationIPNetwork currently sits in the Microsoft.AspNetCore.HttpOverrides namespace. However, this class is of great use for applications that don't necessarily run on ASP.NET. For example, This console tool built on golang is able to de-duplicate IPs and merge CIDRs: https://github.com/zhanhb/cidr-merger. Additionally, the IPNetwork class could either implement a string constructor or implement public static class IPHelper
{
public static IPNetwork ParseCidr(string cidr)
{
var splitCidr = cidr.Split('/', 2);
var ip = IPAddress.Parse(splitCidr[0]);
var maskLength = (splitCidr.Length, ip.AddressFamily) switch
{
// if less than 2, the original string is a simple IP address. We must consider it as the full mask.
(< 2, AddressFamily.InterNetwork) => 32,
(< 2, AddressFamily.InterNetworkV6) => 128,
_ => int.Parse(splitCidr[1])
};
return new IPNetwork(ip, maskLength);
}
} API Proposalnamespace System.Net;
public class IPNetwork
{
public IPNetwork(string cidr) { /* implementation */ }
// same class as Microsoft.AspNetCore.HttpOverrides.IPNetwork here
} API Usagenamespace System.Net;
var cidr = IPNetwork.Parse("192.168.0.0/16")
Console.WriteLine(cidr.Contains("192.168.0.1")); Alternative Designsnamespace System.Net;
public class IPNetwork : IParsable<IPNetwork>
{
// Implement IParsable instead of a string constructor...
} RisksCurrent users of the IPNetwork class will have to change their imports. I don't know much about the process of whether that is a breaking change or not, but that's something to consider when applying this change. Alternatively, we can keep the two classes, but since IPNetwork is such a recent addition, I feel like going the breaking change route may be the fastest way to move forward.
|
We are adding an |
And it's already implemented and in preview releases: |
Oh wow! Didn't realize it was already in the preview version. We're currently using .NET 7 in my project. The functionality of allowing an IP address to be a CIDR only containing itself is custom enough to not make it worth to implement in the base Can I close this issue then? |
Background and motivation
IPNetwork currently sits in the Microsoft.AspNetCore.HttpOverrides namespace. However, this class is of great use for applications that don't necessarily run on ASP.NET.
For example, This console tool built on golang is able to de-duplicate IPs and merge CIDRs: https://github.com/zhanhb/cidr-merger.
Currently, if you'd want to implement this in dotnet, you'd either need to import aspnet into your console application, or copy-paste the IPNetwork class into your code.
Additionally, the IPNetwork class could either implement a string constructor or implement
IParse<IPNetwork>
, in order to reduce manual parsing from the user's side. Here is anIPHelper
class which implements a simplified parser and helps us visualize a small nuance:API Proposal
API Usage
Alternative Designs
Risks
Current users of the IPNetwork class will have to change their imports. I don't know much about the process of whether that is a breaking change or not, but that's something to consider when applying this change.
Alternatively, we can keep the two classes, but since IPNetwork is such a recent addition, I feel like going the breaking change route may be the fastest way to move forward.
The text was updated successfully, but these errors were encountered: