如何存储IP地址列表在C#列表中,使其可搜索子网? [英] How to store IP address list in C# List to make it searchable for subnets too?

查看:144
本文介绍了如何存储IP地址列表在C#列表中,使其可搜索子网?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该如何正确地存储IP地址列表,其地址是子网以使其可搜索?

how should I correctly store IP address list with addresses which are subnets to make it searchable?

有两个示例:


  1. 我有IP地址1.2.3.4,在我的C#列表中有1.2.3.4条目,所以这里没有问题。

  1. I have IP address 1.2.3.4 and in my C# List there is 1.2.3.4 entry so here we have no problems.

我有IP地址3.4.5.6和在我的C#列表我有子网3.4.0.0/24。这是我的问题。

I have IP address 3.4.5.6 and in my C# List I have subnet 3.4.0.0/24. Here is my problem.

如何在List中存储IP子网以覆盖第二个示例?

How to store IP subnet in List to cover second example?

感谢

推荐答案

在本答案的最后,代表一个IPV4地址。

At the end of this answer you will find a complete implementation of a structure to represent a IPV4 address.

这里是一个非常简单的例子: -

Here is really simple example of usage:-

List<IPV4Address> list = new List<IPV4Address>();
list.Add(IPV4Address.FromString("3.4.0.0", 24));
var x = IPV4Address.FromString("3.4.0.6");
foreach (var addr in list.Where(a => a.Contains(x)))
  Console.WriteLine(addr);

由于找到3.4.0.6,所以在控制台中显示值3.4.0.0/255.255.255.0在3.4.0.0/24子网中。假设 list 充满了各种子网, x 可以包含任何地址,则: -

The value "3.4.0.0/255.255.255.0" is displayed inthe console since 3.4.0.6 is found in the 3.4.0.0/24 subnet. Assuming list is full of various subnets and x could contain any address then this:-

var result = list.Where(a => a.Contains(x))
    .OrderByDescending(a => a.Mask)
    .FirstOrDefault();

将选择包含 x

public struct IPV4Address
{
  private UInt32 _Value;
  private UInt32 _Mask;

  public UInt32 Value
  {
    get { return _Value; }
    private set { _Value = value; }
  }

  public UInt32 Mask
  {
    get { return _Mask; }
    private set { _Mask = value; }
  }

  public static IPV4Address FromString(string address)
  {
    return FromString(address, 32);
  }

  public static IPV4Address FromString(string address, int maskLength)
  {
    string[] parts = address.Split('.');
    UInt32 value = ((UInt32.Parse(parts[0]) << 24) +
      ((UInt32.Parse(parts[1])) << 16) +
      ((UInt32.Parse(parts[2])) << 8) +
      UInt32.Parse(parts[3]));

    return new IPV4Address(value, maskLength);
  }

  public IPV4Address(UInt32 value)
  {
    _Value = value;
    _Mask = int.MaxValue;
  }

  public IPV4Address(UInt32 value, int maskLength)
  {
    if (maskLength < 0 || maskLength > 32)
      throw new ArgumentOutOfRangeException("maskLength", "Must be 0 to 32");

    _Value = value;
    if (maskLength == 32)
      _Mask = UInt32.MaxValue;
    else
      _Mask = ~(UInt32)((1 << (32 - maskLength))-1);

    if ((_Value & _Mask) != _Value)
      throw new ArgumentException("Address value must be contained in mask");
  }

  public bool Contains(IPV4Address address)
  {
    if ((Mask & address.Mask) == Mask)
    {
      return (address.Value & Mask) == Value;
    }
    return false;
  }

  public override string ToString()
  {
    string result = String.Format("{0}.{1}.{2}.{3}", (_Value >> 24), 
      (_Value >> 16) & 0xFF, 
      (_Value >> 8) & 0xFF, 
      _Value & 0xFF);

    if (_Mask != UInt32.MaxValue)
      result += "/" + String.Format("{0}.{1}.{2}.{3}", (_Mask >> 24),
      (_Mask >> 16) & 0xFF,
      (_Mask >> 8) & 0xFF,
      _Mask & 0xFF);

    return result;
  }
}

这篇关于如何存储IP地址列表在C#列表中,使其可搜索子网?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆