Java InetAddress Create getAddressPriority(InetAddress addr)

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

Description

get Address Priority

License

Apache License

Declaration

private static int getAddressPriority(InetAddress addr) 

Method Source Code

//package com.java2s;
/*//from w  w w . j av a 2  s  .  c  o m
   Copyright 2013 Philipp Leitner
    
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
    
   http://www.apache.org/licenses/LICENSE-2.0
    
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

import java.net.InetAddress;

public class Main {
    private static int getAddressPriority(InetAddress addr) {
        //if it's some bullshit -- 0.
        if (addr.isLoopbackAddress() || addr.isAnyLocalAddress() || addr.isMulticastAddress())
            return 0;

        //if it's local due to RFC1918 -- 1
        byte[] octets = addr.getAddress();
        if ((octets[0] == 10) || (octets[0] == (byte) 172 && octets[1] >= 16 && octets[1] <= 32)
                || (octets[0] == (byte) 192 && octets[1] == (byte) 168))
            return 1;

        //no, this should be some nice address.
        return 2;
    }
}

Related

  1. getActiveInterfaceInetAddresses()
  2. getAddressInfoFindAll(Function convertValue, Predicate doBreak)
  3. getAddrInfo(InetAddress pAddr)
  4. getAllInetAddress(final String interfaceName)
  5. getAllIPv4InetAddresses()
  6. getAllLocalIPv4InetAddresses()