Here you can find the source of isPrivateIp(InetAddress addr)
Parameter | Description |
---|---|
addr | target address |
public static boolean isPrivateIp(InetAddress addr)
//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; } }