jp.co.ntts.vhut.util.Ipv4ConversionUtil.java Source code

Java tutorial

Introduction

Here is the source code for jp.co.ntts.vhut.util.Ipv4ConversionUtil.java

Source

/*
 * Copyright 2011 NTT Software Corporation.
 * All Rights Reserved.
 */
package jp.co.ntts.vhut.util;

import java.math.BigInteger;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

/**
 * <p>IP??.
 *
 * @version 1.0.0
 * @author NTT Software Corporation.
 *
 * <!--
 * $Date: 2011-11-28 19:50:40 +0900 (, 28 11 2011) $
 * $Revision: 949 $
 * $Author: NTT Software Corporation. $
 * -->
 */
public class Ipv4ConversionUtil {

    private Ipv4ConversionUtil() {

    }

    /**
     * @private
     */
    private static byte[] FULL_BYTES;

    /**
     * @return 255.255.255.255
     */
    public static byte[] getFullBytes() {
        if (FULL_BYTES == null) {
            FULL_BYTES = addrTobyte("FFFFFFFF");
        }
        return FULL_BYTES;
    }

    /**
     * ?.
     * ????????
     * ????????
     * @param ipaddr ????"xxxxxxxx" HEX(??)?
     * @param segaddr ?"xxxxxxxx" HEX(??)?
     * @param maskaddr ?"xxxxxxxx" HEX(??)?
     * @return true=OK,false=NG
     */
    public static boolean isScorp(String ipaddr, String segaddr, String maskaddr) {
        if (ipaddr == null) {
            return false;
        }
        byte[] bipaddr = addrTobyte(ipaddr);
        byte[] bmaskaddr = addrTobyte(maskaddr);
        byte[] bsegaddr = addrTobyte(segaddr);
        byte[] maskedBipaddr = byteAND(bipaddr, bmaskaddr);
        byte[] maskedBsegaddr = byteAND(bsegaddr, bmaskaddr);
        return !convertToBoolean(byteXOR(maskedBipaddr, maskedBsegaddr));
    }

    /**
     * byte[]?????.
     * @param ipaddr IP
     * @param maskaddr ?
     * @return 
     */
    public static byte[] getHostAddressAsByte(String ipaddr, String maskaddr) {
        byte[] bipaddr = addrTobyte(ipaddr);
        byte[] bmaskaddr = addrTobyte(maskaddr);
        return getHostAddressAsByte(bipaddr, bmaskaddr);
    }

    /**
     * byte[]?????.
     * @param bipaddr IP
     * @param bmaskaddr ?
     * @return 
     */
    public static byte[] getHostAddressAsByte(byte[] bipaddr, byte[] bmaskaddr) {
        byte[] reversedBmaskaddr = byteXOR(bmaskaddr, getFullBytes());
        return byteAND(bipaddr, reversedBmaskaddr);
    }

    /**
     * ??????.
     * @param ipaddr IP
     * @param maskaddr ?
     * @return ?? (?=0)
     */
    public static int getHostAddressOrder(String ipaddr, String maskaddr) {
        byte[] bipaddr = addrTobyte(ipaddr);
        byte[] bmaskaddr = addrTobyte(maskaddr);
        return getHostAddressOrder(bipaddr, bmaskaddr);
    }

    /**
     * ??????.
     * @param bipaddr IP
     * @param bmaskaddr ?
     * @return ?? (?=0)
     */
    public static int getHostAddressOrder(byte[] bipaddr, byte[] bmaskaddr) {
        byte[] bhostaddr = getHostAddressAsByte(bipaddr, bmaskaddr);
        return convertToInt(bhostaddr);
    }

    /**
     * ?????????.
     * ??????
     * @param maskaddr ?
     * @return ?????
     */
    public static int getHostAddressCount(String maskaddr) {
        byte[] bmaskaddr = addrTobyte(maskaddr);
        return getHostAddressCount(bmaskaddr);
    }

    /**
     * ??IP???.
     * @param startIpaddr (HEX/)
     * @param endIpaddr (HEX/)
     * @return IP?
     */
    public static int getHostAddressCount(String startIpaddr, String endIpaddr) {
        BigInteger startBI = new BigInteger(addrTobyte(startIpaddr));
        BigInteger endBI = new BigInteger(addrTobyte(endIpaddr));
        return (int) Math.min(endBI.subtract(startBI).longValue() + 1, Integer.MAX_VALUE);
    }

    /**
     * ?????????.
     * ??????
     * @param bmaskaddr ?
     * @return ?????
     */
    public static int getHostAddressCount(byte[] bmaskaddr) {
        byte[] reversedBmaskaddr = byteXOR(bmaskaddr, getFullBytes());
        return convertToInt(reversedBmaskaddr) + 1;
    }

    /**
     * ????IPbyte[] ?????.
     * @param bsegaddr 
     * @param bmaskaddr 
     * @param order ?(0=1, 1=2, -1=??1)
     * @return IP
     */
    public static byte[] getIpAddressWithOrderAsByte(byte[] bsegaddr, byte[] bmaskaddr, int order) {
        int length = getHostAddressCount(bmaskaddr);
        byte[] maskedBsegaddr = byteAND(bsegaddr, bmaskaddr);
        byte[] bhostaddr = convertOrderToByte(order % length);
        return byteOR(maskedBsegaddr, bhostaddr);
    }

    /**
     * ????IPbyte[] ?????.
     * @param segaddr 
     * @param maskaddr 
     * @param order ?
     * @return IP
     */
    public static byte[] getIpAddressWithOrderAsByte(String segaddr, String maskaddr, int order) {
        byte[] bsegaddr = addrTobyte(segaddr);
        byte[] bmaskaddr = addrTobyte(maskaddr);
        return getIpAddressWithOrderAsByte(bsegaddr, bmaskaddr, order);
    }

    /**
     * ?"xxxxxxxx" HEX(??)?  byte[].
     * @param s 
     * @return byte[]
     */
    public static byte[] addrTobyte(String s) {
        byte[] b = new byte[4];
        String[] addrs = s.split("\\.");
        if (addrs.length == 1) {
            for (int i = 0; i < 4; i++) {
                b[i] = (byte) Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16);
            }
        } else if (addrs.length == 4) {
            for (int i = 0; i < 4; i++) {
                b[i] = (byte) Integer.parseInt(addrs[i]);
            }
        }
        return b;
    }

    /**
     * byte[]  "xxxxxxxx" HEX(??)?.
     * @param b 
     * @return s "xxxxxxxx" HEX(??)?
     */
    public static String byteToAddr(byte[] b) {
        String s = "";
        for (int i = 0; i < 4; i++) {
            s += StringUtils.leftPad(Integer.toHexString(convertToInt(b[i])).toUpperCase(), 2, "0");
        }
        return s;
    }

    /**
     * @param length 
     * @return ?
     */
    public static byte[] getMask(int length) {
        byte[] b = new BigInteger(1, getFullBytes()).shiftRight(length).xor(new BigInteger(1, getFullBytes()))
                .toByteArray();
        byte[] br = new byte[4];
        for (int i = 0; i < br.length; i++) {
            br[i] = b[i + 1];
        }
        return br;
    }

    /**
     * ???AND.
     * @param b1 ??
     * @param b2 ??
     * @return ??
     */
    static byte[] byteAND(byte[] b1, byte[] b2) {
        byte[] r = new byte[b1.length];
        for (int i = 0; i < r.length; i++) {
            r[i] = (byte) (b1[i] & b2[i]);
        }
        return r;
    }

    /**
     * ???OR.
     * @param b1 ??
     * @param b2 ??
     * @return ??
     */
    static byte[] byteOR(byte[] b1, byte[] b2) {
        byte[] r = new byte[b1.length];
        for (int i = 0; i < r.length; i++) {
            r[i] = (byte) (b1[i] | b2[i]);
        }
        return r;
    }

    /**
     * ???XOR.
     * @param b1 ??
     * @param b2 ??
     * @return ??
     */
    static byte[] byteXOR(byte[] b1, byte[] b2) {
        byte[] r = new byte[b1.length];
        for (int i = 0; i < r.length; i++) {
            r[i] = (byte) (b1[i] ^ b2[i]);
        }
        return r;
    }

    /**
     * ???XOR.
     * @param b ??
     * @return ??
     */
    static boolean convertToBoolean(byte[] b) {
        for (int i = 0; i < b.length; i++) {
            if (b[i] > 0) {
                return true;
            }
        }
        return false;
    }

    /**
     * ?(byte[])?????.
     * @param order ?
     * @return (byte[])
     */
    static byte[] convertOrderToByte(int order) {
        byte[] baddr = new byte[4];
        baddr[0] = (byte) ((order >> 24) & 255);
        baddr[1] = (byte) ((order >> 16) & 255);
        baddr[2] = (byte) ((order >> 8) & 255);
        baddr[3] = (byte) (order & 255);
        return baddr;
    }

    /**
     * (byte[])??????.
     * @param baddr 
     * @return ?
     */
    static int convertToInt(byte[] baddr) {
        BigInteger order = new BigInteger(baddr);
        return order.intValue();
    }

    /**
     * byteint?????.
     * @param b 
     * @return ?
     */
    static int convertToInt(byte b) {
        Integer intValue = new Byte(b).intValue();
        if (intValue < 0) {
            intValue += 256;
        }
        return intValue;
    }

    /**
     * HEX?????.
     * @param s HEX?IP
     * @return ?IP
     */
    public static String convertHexToDot(String s) {
        Object[] addrs = new Object[4];
        for (int i = 0; i < 4; i++) {
            addrs[i] = Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16);
        }
        return StringUtils.join(addrs, ".");
    }

    /**
     * HEX?????.
     * @param ipaddr ()
     * @return (HEX)
     */
    public static String convertDotToHex(String ipaddr) {
        return byteToAddr(addrTobyte(ipaddr));
    }

    /**
     * IP(HEX)????.
     * ????int????????????.
     * @param startIpaddr (HEX/)
     * @param endIpaddr (HEX/)
     * @return IP(HEX)??
     */
    public static Set<String> getIpAddressSetBetween(String startIpaddr, String endIpaddr) {
        BigInteger startBI = new BigInteger(addrTobyte(startIpaddr));
        BigInteger endBI = new BigInteger(addrTobyte(endIpaddr));
        int length = (int) Math.min(endBI.subtract(startBI).longValue(), Integer.MAX_VALUE);

        Set<String> resultSet = new HashSet<String>();

        BigInteger currentBI = startBI;

        for (int i = 0; i <= length; i++) {
            resultSet.add(byteToAddr(currentBI.toByteArray()));
            currentBI = currentBI.add(BigInteger.ONE);
        }

        return resultSet;
    }

    /**
     * ??IP??NW??????.
     * @param bAddr IP?
     * @param bMask (?)
     * @return ??
     */
    public static byte[] getNetworkAddressAsByte(byte[] bAddr, byte[] bMask) {
        return byteAND(bAddr, bMask);
    }

    /**
     * ??IP??NW??????.
     * @param bAddr IP?
     * @param bMask (?)
     * @return ??
     */
    public static byte[] getNextNetworkAddressAsByte(byte[] bAddr, byte[] bMask) {
        BigInteger current = new BigInteger(getNetworkAddressAsByte(bAddr, bMask));
        BigInteger reversedMask = new BigInteger(byteXOR(bMask, getFullBytes()));
        return current.add(reversedMask).add(BigInteger.ONE).toByteArray();
    }

}

/**
 * =====================================================================
 * 
 *    Copyright 2011 NTT Sofware Corporation
 * 
 *    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.
 * 
 * =====================================================================
 */