Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if a String is IPv4 / IPv6 or domain name? (Java)

How can I check if a string matches a IPv4, IPv6 adress or a domain name like www.google.com?

I found this for IPv4: Validating IPv4 string in Java And the same kind of system works for IPv6 but how can I check domain names?

EDIT: To clarify: I want to check if a string is a valid server address. Valid just means that the FORMAT is correct. The connection will be testet at a different point in the program.

like image 827
F_Schmidt Avatar asked Sep 19 '25 20:09

F_Schmidt


2 Answers

While it is true there are a lot of corner cases, this is straightforward with The IPAddress Java library. I advise against using regular expressions, there are too many variations. Disclaimer: I am the project manager of the IPAddress library.

Here is sample code:

check("1.2.3.4");
check("1.2.a.4");
check("::1");
check("[::1]");
check("1.2.?.4");  

static void check(String hostStr) {
   HostName host = new HostName(hostStr);
   try {
       host.validate();
       if(host.isAddress()) {
           IPAddress addr = host.asAddress();
           System.out.println(addr.getIPVersion() + " address: " + addr);
       } else {
           System.out.println("host name: " + host);
       }
   } catch(HostNameException e) {
       System.out.println(e.getMessage());
   }
}

Output:

IPv4 address: 1.2.3.4
host name: 1.2.a.4
IPv6 address: ::1
IPv6 address: ::1
1.2.?.4 Host error: invalid character at index 4
like image 81
Sean F Avatar answered Sep 21 '25 12:09

Sean F


Apache has domain name validation:

 public void testValidDomains() {
        assertTrue("apache.org should validate", validator.isValid("apache.org"));
        assertTrue("www.google.com should validate", validator.isValid("www.google.com"));

        assertTrue("test-domain.com should validate", validator.isValid("test-domain.com"));
        assertTrue("test---domain.com should validate", validator.isValid("test---domain.com"));
        assertTrue("test-d-o-m-ain.com should validate", validator.isValid("test-d-o-m-ain.com"));
        assertTrue("two-letter domain label should validate", validator.isValid("as.uk"));

        assertTrue("case-insensitive ApAchE.Org should validate", validator.isValid("ApAchE.Org"));

        assertTrue("single-character domain label should validate", validator.isValid("z.com"));

        assertTrue("i.have.an-example.domain.name should validate", validator.isValid("i.have.an-example.domain.name"));
    }

    public void testInvalidDomains() {
        assertFalse("bare TLD .org shouldn't validate", validator.isValid(".org"));
        assertFalse("domain name with spaces shouldn't validate", validator.isValid(" apache.org "));
        assertFalse("domain name containing spaces shouldn't validate", validator.isValid("apa che.org"));
        assertFalse("domain name starting with dash shouldn't validate", validator.isValid("-testdomain.name"));
        assertFalse("domain name ending with dash shouldn't validate", validator.isValid("testdomain-.name"));
        assertFalse("domain name starting with multiple dashes shouldn't validate", validator.isValid("---c.com"));
        assertFalse("domain name ending with multiple dashes shouldn't validate", validator.isValid("c--.com"));
        assertFalse("domain name with invalid TLD shouldn't validate", validator.isValid("apache.rog"));

        assertFalse("URL shouldn't validate", validator.isValid("http://www.apache.org"));
        assertFalse("Empty string shouldn't validate as domain name", validator.isValid(" "));
        assertFalse("Null shouldn't validate as domain name", validator.isValid(null));
    }

Hope that helps.

like image 35
hd1 Avatar answered Sep 21 '25 12:09

hd1