简体   繁体   中英

chek if the elements of the list is between two ip addresses in java

I want to write a function that receives two variables of type String representing two IP addresses and a third variable representing a group of IP addresses. The function returns all addresses between IP1 and IP2 from the given set of addresses, taking into account the order.

this is the function signature:

public static String[] get_available_IPs(String ip1, String ip2, String[] addresses)

I tried to split each ip to compare them in the end but I didn't get any result

This is the code that I wrote while i'm trying to solve this problem. I don't think that it will help you to solve the problem but I just want you to take a look of what I did.

String[] ip_numbers1 = ip1.split("\\.");
    String[] ip_numbers2 = ip2.split("\\.");
    String[][] ip_numbers3 = new String[4][addresses.length];
    
    boolean[] flags = new boolean[4];
    
    for (int i = 0; i < addresses.length; i++) {
        for (int j = 0; j < 4; j++) {
            String[] new1 = addresses[j].split("\\.");
            for (int k = 0; k < 4; k++) {
                ip_numbers3[j][k] = new1[k];
            }
        }
    }
    
    for (int i = 0; i < ip_numbers3.length; i++) {
        if (Integer.parseInt(ip_numbers1[0]) <= Integer.parseInt(ip_numbers3[i][0])
                && Integer.parseInt(ip_numbers2[0]) >= Integer.parseInt(ip_numbers3[i][0])) {
            flags[i] = true;
        }
    }

sample inputs and outputs:

input:
ip1 = '192.168.1.1'
ip2 = '220.150.1.0'
addresses = ['193.168.10.20','221.155.1.5','194.200.1.5','192.168.1.2']

output:
['194.200.1.5','193.168.10.20','192.168.1.2']

input:
ip1 = '191.168.1.1'
ip2 = '222.155.1.5'
addresses = ['193.168.10.20','221.155.1.5','194.200.1.5','192.168.1.1']

output:
['221.155.1.5','194.200.1.5','193.168.10.20','192.168.1.1']

Strings in java are Comparable . This means you can order them with ip1.compareTo(ip) . The best part is that it works even with the . in the ips.

Here is a code sample that tells if an ip is in range of two others (not inclusive).

public static Boolean check(String ip1, String ip2, String test){
    return ip1.compareTo(test) < 0 && ip2.compareTo(test) > 0;
}

You can test it here: https://www.jdoodle.com/iembed/v0/vRZ

Since IP address are made of 4 "bytes" (numbers between 0 and 255), an IP can be encoded into an unsigned integer, and integer are definitely easier to use for operation involving comparison.

There is no true unsigned integer in Java, but you can use a long. Below are examples of methods to transform an IP to its long representation (and the inverse operation):

private static long ipToLong(String ipAsString) {
    return Arrays.stream(ipAsString.split("\\."))
                 .map(Long::parseLong)
                 .reduce(0L, (built, b) -> (built << 8) | b);
}

private static String longToIp(long ipAsLong) {
    return IntStream.of(24,16,8,0)
            .mapToLong(n -> ((ipAsLong)>>n)&0xff)
            .mapToObj(String::valueOf)
            .collect(Collectors.joining("."));
}

With this, IP1 will be before IP2 if the long representation of IP1 is lower than the one of IP2. For instance to filter IP address:

public static String[] select(String ipStart, String ipEnd, String[] addresses) {
    final var start = ipToLong(ipStart);
    final var end = ipToLong(ipEnd);

    final Predicate<String> isInRange = ip -> {final var value = ipToLong(ip);return value>=start && value<=end;};

    return Arrays.stream(addresses).filter(isInRange).toArray(String[]::new);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM