2010-06-30 39 views
2

我搜索了SO尋求幫助,但無法找到我的問題的答案。將子網掩碼「/」符號轉換爲Cisco 0.0.0.0標準

情況:我需要將「/ NN」子網掩碼錶示法(認爲IPTABLES)轉換爲0.0.0.0思科表示法。

NN是子掩碼中「1」的數目,從最低八位位組到最高位。每個八位字節是8位整數。

可能的解決方案:

請32的「0」的一個陣列,並用「1」,則在4個八位字節組填充最後NN數字和轉換爲int ... A/23掩模應該像0.0 .1.255。

我的問題是如何在.NET中做到這一點......我從來沒有使用二進制操作和轉換。

你們可以幫我解決這個問題嗎?

更新 - Stephen已經正確回答!

這裏是代碼移植到.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(); 

回答

5

我一直想拋出一些通用的地址屏蔽例程一起...

這裏有一個快速和骯髒的方式來從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); 
+0

感謝您的回答:D 我會嘗試併發布反饋,但在此之前,我有一個問題。結果將是「255.255」方式還是像cisco一樣的「0.0.1.255」? 此致! – jaderanderson 2010-06-30 19:44:21

+0

這將轉換爲標準的「255.255.128.0」種子網掩碼。如果你需要一個「通配符」掩碼,那麼你可以在調用'HostToNetworkOrder'之前取得結果的位數('〜'),這會給你一個結果如「0.0.1.255」。 – 2010-06-30 19:58:21

+0

謝謝你,會用答案更新代碼!有用! – jaderanderson 2010-07-01 02:51:06

1

相同? as Stephens in VB .Net

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 
0

我會推薦使用IPNetwork Library 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 

玩得開心!