Java tutorial
//package com.java2s; /* * PHEX - The pure-java Gnutella-servent. * Copyright (C) 2001 - 2008 Phex Development Group * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * --- SVN Information --- * $Id$ */ public class Main { /** * Parses a netmask. This can either be in ip format (xxx.xxx.xxx.xxx) * or an int when using CIDR notation * @param netmask * @return */ public static byte parseNetmaskToCidr(String netmask) { if (netmask.indexOf('.') == -1) { // CIDR notation try { byte val = Byte.parseByte(netmask); if (val >= 0) { return val; } throw new IllegalArgumentException("Invalid netmask: " + netmask); } catch (NumberFormatException exp) { throw new IllegalArgumentException("Invalid netmask: " + netmask); } } else {// simple ip notation return calculateCidr(parseDottedIpToInt(netmask)); } } /** * Calculates the cidr from an netmask IP. * @param ip the IP to calculate the cidr for. * @return the cidr value of the IP. */ public static byte calculateCidr(int ip) { byte b = 0; b += cidrByteToNBits((byte) ((ip >>> 24) & 0xFF)); b += cidrByteToNBits((byte) ((ip >>> 16) & 0xFF)); b += cidrByteToNBits((byte) ((ip >>> 8) & 0xFF)); b += cidrByteToNBits((byte) (ip & 0xFF)); return b; } /** * Calculates the cidr from an netmask IP. * @param ip the IP to calculate the cidr for. * @return the cidr value of the IP. */ public static byte calculateCidr(byte[] ip) { int intIp = byteIpToIntIp(ip); return calculateCidr(intIp); } /** * Parses an ip in the format xxx.xxx.xxx.xxx into an int value. * @param hostIp * @return */ public static int parseDottedIpToInt(String hostIp) { /* The string (probably) represents a numerical IP address. * Parse it into an int, don't do uneeded reverese lookup, * leave hostName null, don't cache. If it isn't an IP address, * (i.e., not "%d.%d.%d.%d") or if any element > 0xFF, * it is a hostname and we return null. * This seems to be 100% compliant to the RFC1123 spec. */ String ipToParse; int portSeparatorIdx = hostIp.indexOf(':'); if (portSeparatorIdx != -1) {// cut of port. ipToParse = hostIp.substring(0, portSeparatorIdx); } else { ipToParse = hostIp; } char[] data = ipToParse.toCharArray(); int IP = 0x00; int hitDots = 0; for (int i = 0; i < data.length; i++) { char c = data[i]; if (c < 48 || c > 57) { // !digit throw new IllegalArgumentException("IP contains character: " + ipToParse + " - org: " + hostIp); } int b = 0x00; while (c != '.') { // '0' '9' if (c < 48 || c > 57) { // !digit throw new IllegalArgumentException("IP contains character: " + ipToParse); } b = b * 10 + c - '0'; if (++i >= data.length) { break; } c = data[i]; } if (b > 0xFF) { /* bogus - bigger than a byte */ throw new IllegalArgumentException("Bogus ip value: " + ipToParse); } IP = (IP << 8) + b; hitDots++; } if (hitDots != 4 || ipToParse.endsWith(".")) { throw new IllegalArgumentException("Bogus ip: " + ipToParse); } return IP; } private static byte cidrByteToNBits(byte val) { switch (val) { case (byte) 0xFF: return 8; case (byte) 0xFE: return 7; case (byte) 0xFC: return 6; case (byte) 0xF8: return 5; case (byte) 0xF0: return 4; case (byte) 0xE0: return 3; case (byte) 0xC0: return 2; case (byte) 0x80: return 1; case (byte) 0x00: return 0; } throw new IllegalArgumentException("Invalid byte value"); } public static int byteIpToIntIp(byte[] ip) { int v1 = ip[3] & 0xFF; int v2 = (ip[2] << 8) & 0xFF00; int v3 = (ip[1] << 16) & 0xFF0000; int v4 = (ip[0] << 24); int ipValue = ((v4 | v3 | v2 | v1)) & 0xFFFFFFFF; return ipValue; } }