2010-12-14 3 views
2

Wir schreiben eine Funktion, die es einem Administrator erlaubt, Bereiche von IP-Adressen zu blockieren/erlauben.Der einfachste Weg, IP-Adresse gegen einen Bereich von Werten zu prüfen mit C#

Ist das einfach genug mit C# zu tun?

Ich dachte darüber nach, jede Zahl [hier] [hier] [hier] [hier] anzuschauen und dann mit den Reihen zu mähen und nur zu sehen, ob jede Zahl zwischen den beiden lag.

Funktioniert das für Standard-IP-Adressen V4?

+0

Wie definieren Sie eine Reihe von IPs? – CodesInChaos

+0

+1 sehr interessante Frage! Interessanterweise ... erlauben Sie, dass Bereiche mit einem Standard wie der CIDR-Notation ausgedrückt werden? –

Antwort

3

Ich würde sie in ganze Zahlen konvertieren und dann die Ganzzahl vergleichen. Was jedoch richtig ist, hängt davon ab, wie Sie Bereiche definieren.

UInt32 Ip4ToInt(string ip) 
{ 
    UInt32[] parts=ip.Split('.').Select(s=>UInt32.Parse(s)).ToArray(); 
    if (parts.Length != 4) 
    throw new ArgumentException("InvalidIP"); 
    return (parts[0]<<24) | (parts[1]<<16) | (parts[2]<<8) | parts[3]; 
} 

Zum Beispiel sollte 1.1.1.991.1.1.1 - 1.1.2.2 Teil des Bereichs sein? Beim Vergleichen jeder Gruppe ist es nicht, wenn man die ganzen Zahlen vergleicht.

0
public static bool CheckIfIpValid(string allowedStartIp, string allowedEndIp, string ip) 
     { 
      // if both start and end ip's are null, every user with these credential can log in, no ip restriction needed. 

     if (string.IsNullOrEmpty(allowedStartIp) && string.IsNullOrEmpty(allowedEndIp)) 
      return true; 
     bool isStartNull = string.IsNullOrEmpty(allowedStartIp), 
      isEndNull = string.IsNullOrEmpty(allowedEndIp); 
     string[] startIpBlocks, endIpBlocks, userIp = ip.Split('.'); 
     if (!string.IsNullOrEmpty(allowedStartIp)) 
      startIpBlocks = allowedStartIp.Split('.'); 
     else 
      startIpBlocks = "0.0.0.0".Split('.'); 
     if (!string.IsNullOrEmpty(allowedEndIp)) 
      endIpBlocks = allowedEndIp.Split('.'); 
     else 
      endIpBlocks = "999.999.999.999".Split('.'); 

     for (int i = 0; i < userIp.Length; i++) 
     { 
      // if current block is smaller than allowed minimum, ip is not valid. 
      if (Convert.ToInt32(userIp[i]) < Convert.ToInt32(startIpBlocks[i])) 
       return false; 
      // if current block is greater than allowed maximum, ip is not valid. 
      else if (Convert.ToInt32(userIp[i]) > Convert.ToInt32(endIpBlocks[i])) 
       return false; 
      // if current block is greater than allowed minimum, ip is valid. 
      else if ((Convert.ToInt32(userIp[i]) > Convert.ToInt32(startIpBlocks[i])) && !isStartNull) 
       return true; 

     } 
     return true; 
    } 
1

Ich weiß, dass System.Net.IPAddress.Address veraltet ist, aber ich denke, dass dies der einfachste Weg ist:

bool result = false; 

System.Net.IPAddress iStart = System.Net.IPAddress.Parse(ipStart); 
System.Net.IPAddress iEnd = System.Net.IPAddress.Parse(ipEnd); 
System.Net.IPAddress iIp = System.Net.IPAddress.Parse(ip); 

if (iStart.Address <= iIp.Address && iEnd.Address >= iIp.Address) 
{ 
    result = true; 
} 

ps. ipStart, ipEnd, ip sind Strings. Beispiel: 127.0.0.1

Verwandte Themen