Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve this in java?

Tags:

java

My program gets as input parameter a String containing a list of IP Addresses. Each IP address is separated by a line break. It can look like this:

10.1.1.1
2.2.2.2
11.1.1.1

it can look like this

10.1.1.1-20
1.1.1.1

but it can looki like this

172.16.12.1-20 /24
10.1.1.1

I want to check every IP address and return two Lists validAddresses, invalidAddresses.

I've already wrote a program that deals with the first the simplest type of input, i.e. no IP address ranges and no network masks.

private String[] extractIPAddress(String address){
    String[] temp;
    temp = address.split("\\s+");
    return temp;
}

Then I do

addressList = extractIPAddress(String.valueOf(value));

for (int i=0; i < addressList.length; i++) {
    if (InetAddresses.isInetAddress(addressList[i]) == true) {
        validAddress = validAddress.concat(addressList[i] + '\n');
    } else {
        invalidAddress = invalidAddress.concat(addressList[i] + '\n');
    }
}

Now I'm pondering how to deal with the most complex type of input, esp.

  • when the line has a range attached to it 1.1.1.1-10, how to remove the -10 part in order check the main IP address; how to check whether range part -10 would make a valid IP address i.e. 1.1.1.10 and then how to put everything together, so I can return it as a line of the validAddress String, looking the same way as at the beginning, i.e. 1.1.1.1-10

  • same question applies to the network mask /24

What elements would this kind of program have? Could you outline it for me?

I thought I would do the following, but I'm not sure if that's the right way and how to implement some parts:

  • if I find a - then cut off the part starting at the position of - until end of line or "/" (how to do that?)
  • save that part into the ipRange variable
  • if I find / then cut off that part starting at the position of / until the end of the line
  • save that part into netMask variable
  • copy the content into the tmp_ipRange = ipRange
  • remove the - in the tem_ipRange variable
  • replace the last octet of the main IP address with tmp_ipRange (how to do that?)
  • add the new IP address to the array created by the String.split() (impossible, because you can't just add something to an array in java? what alternative do I have? so I can't use split here?)
  • loop through the addressList (see above code) and check if the IP address is a valid IP address
  • after the validation add ipRange to the main ipAddress if ipRange is not null (how do I find the main ipAddress the ipRange belongs to?)
  • after the validation add netMaske to the main ipAddress (and range) if mainAddress is not null (how do I find the main ipAddress the netMask belongs to?)
like image 375
Thomas Avatar asked May 27 '26 13:05

Thomas


1 Answers

The parsing part could be done by a regular expression. Something like:

final Pattern p = Pattern.compile(
    "(\\d+\\.\\d+\\.\\d+\\.\\d+)(?:-(\\d+))?(?:/(\\d+))?" );
for(String line : new String [] { "172.16.12.1-20/24",
                                  "172.16.12.1-20", 
                                  "172.16.12.1/24", 
                                  "172.16.12.1" })
{
    Matcher m = p.matcher(line);
    if (m.matches()) {
        String address = m.group(1);
        String rangePart = m.group(2); // is null if there is no range part
        String netmask = m.group(3); // is null if there is no netmask
        System.out.println(address + " - " + rangePart + " - " + netmask);
    }
}

Edit: If you need to deal with spaces, you can augment the regular expression by adding \\s*, for example:

"\\s*(\\d+\\.\\d+\\.\\d+\\.\\d+)(?:\\s*-\\s*(\\d+))?(?:\\s*/\\s*(\\d+))?\\s*"

This way, you won't need to bother with spaces.

You could also create a similar regular expression for IPv6 addresses. It will be longer, of course, but the principle is the same.

like image 189
Petr Avatar answered May 31 '26 22:05

Petr