【问题标题】:How do I found out if a Subnet mask is valid with C#如何确定子网掩码是否对 C# 有效
【发布时间】:2022-01-02 10:20:33
【问题描述】:

我需要了解如何确定提供的子网掩码示例 (255.255.192.0) 是否是有效的子网掩码,如果有效则返回 true,否则返回 false,我已经在检查该值是否超过 255。错误的子网将是 (255.64.0.0)

它在二进制中很有意义 (11111111.01000000.00000000.00000000) 子网不能停止拥有 1,然后再次开始拥有它们。我目前的想法涉及使用 bitshift,但我不确定如何去做。

我没有使用任何库,并且不允许用于此项目

我使用的代码类似于

    Console.WriteLine("Enter a subnet mask");
    input = Console.ReadLine(); //Enters example of 255.64.0.0 which is invalid

提前致谢,如有需要请提问

【问题讨论】:

  • 如果我自己写的话。将值转换为 int(按机器字节顺序)。 XOR 将111000 翻转为000111 加1,所以你现在应该有001000。如果地址有效,您最多应该设置 1 位。现在将最后两个值加在一起。如果答案为零,则地址是有效掩码。
  • 不是真的,你链接的代码显示了如何找到一个子网并检查它是否是正确的长度,我需要检查即使是错误的长度,它是否仍然有效
  • @JeremyLakeman 惊人的答案,我明白你的意思,但如果你明白我的意思,我不太擅长将文字转化为代码,如果你有时间,你能不能写一个例子

标签: c#


【解决方案1】:

你可以试试这个方法:

using System;
using System.Runtime.InteropServices;

namespace Example
{
    public class Program
    {   
        [StructLayout(LayoutKind.Explicit)]
        public struct byte_array
        {
            [FieldOffset(0)]
            public byte byte0;
            [FieldOffset(1)]
            public byte byte1;
            [FieldOffset(2)]
            public byte byte2;
            [FieldOffset(3)]
            public byte byte3;

            [FieldOffset(0)]
            public UInt32 Addr;
        }
        
        public static void Main(string[] args)
        {
            byte_array b_array = new byte_array();
            int i;
            
            b_array.byte3 = 255;
            b_array.byte2 = 64;
            b_array.byte1 = 0;
            b_array.byte0 = 0;
            
            Console.WriteLine(String.Format("{0:X4}", b_array.Addr));
            
            for(i = 31; i >= 0; i--)
                if(((1 << i) & b_array.Addr) == 0)
                    break;
            for(; i >= 0; i--)
                if(((1 << i) & b_array.Addr) != 0)
                {
                    Console.WriteLine("Bad Mask!");
                    break;
                }
        }
    }
}

【讨论】:

    【解决方案2】:

    我寻找了一种库方法,但找不到。下面是我只为 IPv4 地址编写它的方式;

    public static bool IsValidMask(string mask)
    {
        // 1) convert the address to an int32
        if (!IPAddress.TryParse(mask, out var addr))
            return false;
        var byteVal = addr.GetAddressBytes();
        if (byteVal.Length != 4)
            return false;
        var intVal = BitConverter.ToInt32(byteVal);
        intVal = IPAddress.NetworkToHostOrder(intVal);
    
        // A valid mask should start with ones, and end with zeros 0b111...111000...000
    
        // 2) XOR to flip all the bits (0b000...000111...111)
        var uintVal = (uint)intVal ^ uint.MaxValue;
    
        // 3) Add 1, causing all those 1's to become 0's. (0b000...001000...000)
        // An invalid address will have leading 1's that are untouched by this step. (0b101...001000...000)
        var addOne = uintVal + 1;
    
        // 4) AND the two values together, to detect if any leading 1's remain
        var ret = addOne & uintVal;
    
        return ret == 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-04-22
      • 2012-04-14
      • 2013-04-09
      • 2020-12-14
      • 1970-01-01
      • 1970-01-01
      • 2015-07-26
      • 2018-04-03
      • 1970-01-01
      相关资源
      最近更新 更多