2009-02-23 11 views

Odpowiedz

19

Jakarta Commons Net ma org.apache.commons.net.util.SubnetUtils, który wydaje się spełniać Twoje potrzeby. Wygląda jak można zrobić coś takiego:

SubnetInfo subnet = (new SubnetUtils("10.10.10.0", "255.255.255.128")).getInfo(); 
boolean test = subnet.isInRange("10.10.10.10"); 

uwaga, jak carson zwraca uwagę, że Jakarta Commons Net ma a bug uniemożliwiającego mu daje poprawną odpowiedź w niektórych przypadkach. Carson sugeruje użycie wersji SVN, aby uniknąć tego błędu.

+4

Należy zachować ostrożność, używając tego. Jest błąd, który uniemożliwi poprawne działanie. Możesz go wyciągnąć z SVN. http://mail-archives.apache.org/mod_mbox/commons-issues/200902.mbox/%[email protected]%3E – carson

+0

@carson: Dzięki za ostrzeżenie. Poprawiłem moją odpowiedź, aby zawrzeć te informacje. – Eddie

9

Możesz także spróbować

boolean inSubnet = (ip & netmask) == (subnet & netmask); 

lub krótszy

boolean inSubnet = (ip^subnet) & netmask == 0; 
+0

Czy int i longs są wartościami IP i netmask? – simgineer

+0

32-bitowe adresy, IPv4, są numerami wewnętrznymi. Podejrzewam, że IPv6 ma wartości 64-bitowe, ale ja sam nie używałem tego samego –

3

Zastosowanie sprężyny IpAddressMatcher. W przeciwieństwie do Apache Commons Net, obsługuje zarówno ipv4, jak i ipv6.

import org.springframework.security.web.util.matcher.IpAddressMatcher; 
... 

private void checkIpMatch() { 
    matches("192.168.2.1", "192.168.2.1"); // true 
    matches("192.168.2.1", "192.168.2.0/32"); // false 
    matches("192.168.2.5", "192.168.2.0/24"); // true 
    matches("92.168.2.1", "fe80:0:0:0:0:0:c0a8:1/120"); // false 
    matches("fe80:0:0:0:0:0:c0a8:11", "fe80:0:0:0:0:0:c0a8:1/120"); // true 
    matches("fe80:0:0:0:0:0:c0a8:11", "fe80:0:0:0:0:0:c0a8:1/128"); // false 
    matches("fe80:0:0:0:0:0:c0a8:11", "192.168.2.0/32"); // false 
} 

private boolean matches(String ip, String subnet) { 
    IpAddressMatcher ipAddressMatcher = new IpAddressMatcher(subnet); 
    return ipAddressMatcher.matches(ip); 
} 

Źródło: here

0

Aby sprawdzić adres IP w podsieci, użyłem metody isInRange w klasie SubnetUtils. Ale ta metoda ma błąd, że jeśli twoja podsieć to X, każdy adres IP niższy niż X, isInRange zwraca true. Na przykład, jeśli twoja podsieć to 10.10.30.0/24 i chcesz sprawdzić 10.10.20.5, ta metoda zwróci true. Aby poradzić sobie z tym błędem, użyłem poniższego kodu.

public static void main(String[] args){ 
    String list = "10.10.20.0/24"; 
    String IP1 = "10.10.20.5"; 
    String IP2 = "10.10.30.5"; 
    SubnetUtils subnet = new SubnetUtils(list); 
    SubnetUtils.SubnetInfo subnetInfo = subnet.getInfo(); 
    if(MyisInRange(subnetInfo , IP1) == true) 
     System.out.println("True"); 
    else 
     System.out.println("False"); 
    if(MyisInRange(subnetInfo , IP2) == true) 
     System.out.println("True"); 
    else 
     System.out.println("False"); 
} 

private boolean MyisInRange(SubnetUtils.SubnetInfo info, String Addr) 
{ 
    int address = info.asInteger(Addr); 
    int low = info.asInteger(info.getLowAddress()); 
    int high = info.asInteger(info.getHighAddress()); 
    return low <= address && address <= high; 
} 
Powiązane problemy