Java InetAddress Check isPrivateIp(InetAddress addr)

Here you can find the source of isPrivateIp(InetAddress addr)

Description

See if given address is private or not

License

Open Source License

Parameter

Parameter Description
addr target address

Return

true : private network

Declaration

public static boolean isPrivateIp(InetAddress addr) 

Method Source Code


//package com.java2s;
/*/*  ww w.  j a  va 2  s  .co m*/
 License:
    
 blueprint-sdk is licensed under the terms of Eclipse Public License(EPL) v1.0
 (http://www.eclipse.org/legal/epl-v10.html)
    
    
 Distribution:
    
 Repository - https://github.com/lempel/blueprint-sdk.git
 Blog - http://lempel.egloos.com
 */

import java.net.*;

public class Main {
    /**
     * See if given address is private or not
     *
     * @param addr target address
     * @return true : private network
     */
    public static boolean isPrivateIp(InetAddress addr) {
        boolean result = false;

        if (addr instanceof Inet4Address) {
            result = isPrivateIp((Inet4Address) addr);
        } else if (addr instanceof Inet6Address) {
            result = isPrivateIp((Inet6Address) addr);
        }

        return result;
    }

    /**
     * See if given address is private or not
     *
     * @param addr IPv4 address
     * @return true : private network
     */
    public static boolean isPrivateIp(Inet4Address addr) {
        boolean result = false;

        byte[] address = addr.getAddress();

        if (address[0] == 10) {
            // 10.*.*.*
            result = true;
        } else if ((address[0] & 0x000000ff) == 0xa9 && (address[1] & 0x000000ff) == 0xfe) {
            // 169.254.*.*
            result = true;
        } else if ((address[0] & 0x000000ff) == 0xac
                && ((address[1] & 0x000000ff) >= 16 && (address[1] & 0x000000ff) <= 31)) {
            // 172.16.*.* ~ 172.31.*.*
            result = true;
        } else if ((address[0] & 0x000000ff) == 0xc0 && (address[1] & 0x000000ff) == 0xa8) {
            // 192.168.*.*
            result = true;
        }

        return result;
    }

    /**
     * See if given address is private or not
     *
     * @param addr IPv6 address
     * @return true : private network
     */
    public static boolean isPrivateIp(Inet6Address addr) {
        boolean result = false;

        byte[] address = addr.getAddress();

        if ((address[0] & 0x000000ff) == 0xfc && (address[1] & 0x000000ff) == 0x00) {
            result = true;
        } else if ((address[0] & 0x000000ff) == 0xfe && (address[1] & 0x000000ff) == 0x80) {
            result = true;
        }

        return result;
    }
}

Related

  1. isPortInUse(InetAddress address, int port, int count)
  2. isPortOpen(InetAddress address, int port)
  3. isPrivate(InetAddress addr)
  4. isPrivate(InetAddress adr)
  5. isPrivateIp(final InetAddress address)
  6. isPrivateIP(InetAddress ip)
  7. isPrivateNetworkAddress(final InetAddress inetAddress)
  8. isPrivateSubnet(InetAddress address)
  9. isPublic(InetAddress inetAddress)