質問

私は助けを求めて検索しましたが、私の質問に対する答えを見つけることができませんでした。

状況:「/nn」サブネットマスク表記(iptables)を0.0.0.0シスコ表記に変換する必要があります。

nnは、最低のオクテットからより高いものまで、サブマスク内の「1」の数です。各オクテットは8ビット整数です。

可能な解決策:

32 "0"の配列を作成し、最後のnn桁を「1」で塗りつぶし、次に4オクテットでグループ化してint ... a /23マスクに変換する必要があります。

私の質問は、.NETでそれを行う方法です...私はバイナリ操作と変換を使用したことはありません。

このソリューションを手伝ってくれませんか?

更新 - スティーブンは正しく答えています!

これが.NETに移植されたコードです

        if (p.LastIndexOf("/") < 0 ) return p;
        int mask= Convert.ToInt32("0"+p.Substring(p.LastIndexOf("/")+1,2));

        int zeroBits = 32 - mask; // the number of zero bits
        uint result = uint.MaxValue; // all ones

        // Shift "cidr" and subtract one to create "cidr" one bits;
        //  then move them left the number of zero bits.
        result &= (uint)((((ulong)0x1 << mascara) - 1) << zeroBits);
        result = ~result;
        // Note that the result is in host order, so we'd have to convert
        //  like this before passing to an IPAddress constructor
        result = (uint)IPAddress.HostToNetworkOrder((int)result);
        string convertedMask = new IPAddress(result).ToString();
役に立ちましたか?

解決

私はいくつかの汎用アドレスマスキングルーチンを一緒に投げることを意味していました...

ここから転換するための迅速な方法があります cidr サブネットマスクの表記:

var cidr = 23; // e.g., "/23"
var zeroBits = 32 - cidr; // the number of zero bits
var result = uint.MaxValue; // all ones

// Shift "cidr" and subtract one to create "cidr" one bits;
//  then move them left the number of zero bits.
result &= (uint)((((ulong)0x1 << cidr) - 1) << zeroBits);

// Note that the result is in host order, so we'd have to convert
//  like this before passing to an IPAddress constructor
result = (uint)IPAddress.HostToNetworkOrder((int)result);

他のヒント

同じ? VB .NETのStephensとして

Function CIDRtoMask(ByVal CIDR As Integer) As String

    If CIDR < 2 OrElse CIDR > 30 Then
        Stop
    End If

    Debug.WriteLine(CIDR.ToString)

    'simulated ip address
    Dim ipAsNum As UInt32 = 3232300291 '192.168.253.3
    Debug.WriteLine(Convert.ToString(ipAsNum, 2).PadLeft(32, "0"c) & " IP as num") 'show binary


    'create mask
    Dim mask As UInt32 = UInt32.MaxValue << (32 - CIDR)
    Debug.WriteLine(Convert.ToString(mask, 2).PadLeft(32, "0"c) & " mask") 'show binary

    Dim CT As UInt32 = UInt32.MaxValue Xor mask 'the zero based count of hosts in network
    Dim NN As UInt32 = ipAsNum And mask 'network number
    Dim NB As UInt32 = NN Or CT 'network broadcast
    Debug.WriteLine(Convert.ToString(CT, 2).PadLeft(32, "0"c) & " CT") 'show binary
    Debug.WriteLine(Convert.ToString(NN, 2).PadLeft(32, "0"c) & " NN") 'show binary
    Debug.WriteLine(Convert.ToString(NB, 2).PadLeft(32, "0"c) & " NB") 'show binary

    'get bytes
    Dim tb() As Byte = BitConverter.GetBytes(mask)
    Array.Reverse(tb)

    'convert to string
    Dim stringMask As String = String.Format("{0}.{1}.{2}.{3}",
                                  tb(0), tb(1), tb(2), tb(3))

    Return stringMask
End Function

IPNetworkライブラリの使用をお勧めします https://github.com/lduchosal/ipnetwork。バージョン2の時点では、IPv4とIPv6もサポートしています。

IPv4

IPNetwork ipnetwork = IPNetwork.Parse("192.168.0.1/25");

Console.WriteLine("Network : {0}", ipnetwork.Network);
Console.WriteLine("Netmask : {0}", ipnetwork.Netmask);
Console.WriteLine("Cidr : {0}", ipnetwork.Cidr);

出力

Network : 192.168.0.0
Netmask : 255.255.255.128
Cidr : 25

楽しむ !

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top