Java InetAddress Check isSiteLocalAddress(InetAddress i)

Here you can find the source of isSiteLocalAddress(InetAddress i)

Description

Check if address is in site-local range.

License

GNU General Public License

Declaration

public static boolean isSiteLocalAddress(InetAddress i) 

Method Source Code


//package com.java2s;
/* This code is part of Freenet. It is distributed under the GNU General
 * Public License, version 2 (or at your option any later version). See
 * http://www.gnu.org/ for further details of the GPL. */

import java.net.InetAddress;
import java.net.Inet6Address;

public class Main {
    /** Check if address is in site-local range.
     * [Oracle|Open]JDK up to 8 contains obsolete check for site-local ipv6
     * addresses, this repaces it with correct one.
     */// w w w.  java2s . co  m
    public static boolean isSiteLocalAddress(InetAddress i) {
        if (i instanceof Inet6Address) {
            byte[] addr = i.getAddress();
            assert (addr.length == 128 / 8);
            // XXX what about ipv6-mapped ipv4 site-local addresses?
            // (weird/insane/not-sure-if-possible-but)
            /*
            try {
               if(addr[0] == (byte)0x20 && addr[1] == (byte)0x02) {
              // 2002::/16, 6to4 tunnels
              return InetAddress.getByAddress(
              Arrays.copyOfRange(addr,2,6)).isSiteLocalAddress();
               }
               if(addr[ 0] == (byte)0 && addr[ 1] == (byte)0 &&
              addr[ 2] == (byte)0 && addr[ 3] == (byte)0 &&
              addr[ 4] == (byte)0 && addr[ 5] == (byte)0 &&
              addr[ 6] == (byte)0 && addr[ 7] == (byte)0 &&
              addr[ 8] == (byte)0 && addr[ 9] == (byte)0 &&
              addr[10] == (byte)0 && addr[11] == (byte)0) {
              // ::/96, ipv4-compatible ipv6 addresses
              // [DEPRECATED by 2002::/16, probably not worth checking]
              return InetAddress.getByAddress(
              Arrays.copyOfRange(addr,12,16)).isSiteLocalAddress();
               }
            } catch(UnknownHostException e) {
               return false; // impossible
            }
            */
            return ((addr[0] & (byte) 0xfe) == (byte) 0xfc
            /* unique local: fc00::/7 */) || (addr[0] == (byte) 0xfe && (addr[1] & (byte) 0xc0) == (byte) 0xc0
            /* DEPRECATED site local: 0xfec0::/10 */);
        }
        return i.isSiteLocalAddress();
    }
}

Related

  1. isReachable(InetAddress address, int port)
  2. isReachable(NetworkInterface iface, InetAddress address, int timeout)
  3. isReservedAddr(InetAddress inetAddr)
  4. isServerAlive(InetAddress host, int port)
  5. isServerAlive(InetAddress host, int port)
  6. isTenDot(InetAddress adr)
  7. isThisMyIpAddress(InetAddress addr)
  8. isThisMyIpAddress(InetAddress addr)
  9. isUDPPortFree(int port, InetAddress addr)