Java InetAddress Create getNatType(final InetAddress localAdr, final InetAddress publicAdr)

Here you can find the source of getNatType(final InetAddress localAdr, final InetAddress publicAdr)

Description

get Nat Type

License

Apache License

Declaration

public static String getNatType(final InetAddress localAdr,
            final InetAddress publicAdr) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2013-2014 alladin-IT GmbH/*from  w  ww. jav a  2s  .c om*/
 * 
 * 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.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;

public class Main {
    public static String getNatType(final InetAddress localAdr,
            final InetAddress publicAdr) {
        try {
            final String ipVersion;
            if (publicAdr instanceof Inet4Address)
                ipVersion = "ipv4";
            else if (publicAdr instanceof Inet6Address)
                ipVersion = "ipv6";
            else
                ipVersion = "ipv?";

            if (localAdr.equals(publicAdr))
                return "no_nat_" + ipVersion;
            else {
                final String localType = isIPLocal(localAdr) ? "local"
                        : "public";
                final String publicType = isIPLocal(publicAdr) ? "local"
                        : "public";
                return String.format("nat_%s_to_%s_%s", localType,
                        publicType, ipVersion);
            }
        } catch (final IllegalArgumentException e) {
            return "illegal_ip";
        }
    }

    public static boolean isIPLocal(final InetAddress adr) {
        return adr.isLinkLocalAddress() || adr.isLoopbackAddress()
                || adr.isSiteLocalAddress();
    }
}

Related

  1. getLocalInetAddresses()
  2. getLocalInetAddresses(boolean includeLoopback)
  3. getMacAddress(InetAddress addr)
  4. getMacAddress(InetAddress address)
  5. getMieleUUIDLowerPart(int deviceType50523, InetAddress address)
  6. getNetwork(InetAddress start, InetAddress end)
  7. getNetworkCIDR(InetAddress addr, short prefixLength)
  8. getNetworkInterface(InetAddress localAddress)
  9. getNetworkInterfaceByIp(InetAddress inetAddress)