private static int checkMaskLength(String field, int family, int val) { int max = Address.addressLength(family) * 8; if (val < 0 || val > max) throw new IllegalArgumentException("\"" + field + "\" " + val + " must be in the range " + "[0.." + max + "]"); return val; }
private static int checkMaskLength(String field, int family, int val) { int max = Address.addressLength(family) * 8; if (val < 0 || val > max) throw new IllegalArgumentException("\"" + field + "\" " + val + " must be in the range " + "[0.." + max + "]"); return val; }
private static int checkMaskLength(String field, int family, int val) { int max = Address.addressLength(family) * 8; if (val < 0 || val > max) throw new IllegalArgumentException("\"" + field + "\" " + val + " must be in the range " + "[0.." + max + "]"); return val; }
/** * Truncates an address to the specified number of bits. For example, * truncating the address 10.1.2.3 to 8 bits would yield 10.0.0.0. * @param address The source address * @param maskLength The number of bits to truncate the address to. */ public static InetAddress truncate(InetAddress address, int maskLength) { int family = familyOf(address); int maxMaskLength = addressLength(family) * 8; if (maskLength < 0 || maskLength > maxMaskLength) throw new IllegalArgumentException("invalid mask length"); if (maskLength == maxMaskLength) return address; byte [] bytes = address.getAddress(); for (int i = maskLength / 8 + 1; i < bytes.length; i++) bytes[i] = 0; int maskBits = maskLength % 8; int bitmask = 0; for (int i = 0; i < maskBits; i++) bitmask |= (1 << (7 - i)); bytes[maskLength / 8] &= bitmask; try { return InetAddress.getByAddress(bytes); } catch (UnknownHostException e) { throw new IllegalArgumentException("invalid address"); } }
/** * Truncates an address to the specified number of bits. For example, * truncating the address 10.1.2.3 to 8 bits would yield 10.0.0.0. * @param address The source address * @param maskLength The number of bits to truncate the address to. */ public static InetAddress truncate(InetAddress address, int maskLength) { int family = familyOf(address); int maxMaskLength = addressLength(family) * 8; if (maskLength < 0 || maskLength > maxMaskLength) throw new IllegalArgumentException("invalid mask length"); if (maskLength == maxMaskLength) return address; byte [] bytes = address.getAddress(); for (int i = maskLength / 8 + 1; i < bytes.length; i++) bytes[i] = 0; int maskBits = maskLength % 8; int bitmask = 0; for (int i = 0; i < maskBits; i++) bitmask |= (1 << (7 - i)); bytes[maskLength / 8] &= bitmask; try { return InetAddress.getByAddress(bytes); } catch (UnknownHostException e) { throw new IllegalArgumentException("invalid address"); } }
/** * Truncates an address to the specified number of bits. For example, * truncating the address 10.1.2.3 to 8 bits would yield 10.0.0.0. * @param address The source address * @param maskLength The number of bits to truncate the address to. */ public static InetAddress truncate(InetAddress address, int maskLength) { int family = familyOf(address); int maxMaskLength = addressLength(family) * 8; if (maskLength < 0 || maskLength > maxMaskLength) throw new IllegalArgumentException("invalid mask length"); if (maskLength == maxMaskLength) return address; byte [] bytes = address.getAddress(); for (int i = maskLength / 8 + 1; i < bytes.length; i++) bytes[i] = 0; int maskBits = maskLength % 8; int bitmask = 0; for (int i = 0; i < maskBits; i++) bitmask |= (1 << (7 - i)); bytes[maskLength / 8] &= bitmask; try { return InetAddress.getByAddress(bytes); } catch (UnknownHostException e) { throw new IllegalArgumentException("invalid address"); } }
throw new WireParseException("unknown address family"); sourceNetmask = in.readU8(); if (sourceNetmask > Address.addressLength(family) * 8) throw new WireParseException("invalid source netmask"); scopeNetmask = in.readU8(); if (scopeNetmask > Address.addressLength(family) * 8) throw new WireParseException("invalid scope netmask"); byte [] fulladdr = new byte[Address.addressLength(family)]; System.arraycopy(addr, 0, fulladdr, 0, addr.length);
throw new WireParseException("unknown address family"); sourceNetmask = in.readU8(); if (sourceNetmask > Address.addressLength(family) * 8) throw new WireParseException("invalid source netmask"); scopeNetmask = in.readU8(); if (scopeNetmask > Address.addressLength(family) * 8) throw new WireParseException("invalid scope netmask"); byte [] fulladdr = new byte[Address.addressLength(family)]; System.arraycopy(addr, 0, fulladdr, 0, addr.length);
throw new WireParseException("unknown address family"); sourceNetmask = in.readU8(); if (sourceNetmask > Address.addressLength(family) * 8) throw new WireParseException("invalid source netmask"); scopeNetmask = in.readU8(); if (scopeNetmask > Address.addressLength(family) * 8) throw new WireParseException("invalid scope netmask"); byte [] fulladdr = new byte[Address.addressLength(family)]; System.arraycopy(addr, 0, fulladdr, 0, addr.length);
void rrFromWire(DNSInput in) throws IOException { elements = new ArrayList(1); while (in.remaining() != 0) { int family = in.readU16(); int prefix = in.readU8(); int length = in.readU8(); boolean negative = (length & 0x80) != 0; length &= ~0x80; byte [] data = in.readByteArray(length); Element element; if (!validatePrefixLength(family, prefix)) { throw new WireParseException("invalid prefix length"); } if (family == Address.IPv4 || family == Address.IPv6) { data = parseAddress(data, Address.addressLength(family)); InetAddress addr = InetAddress.getByAddress(data); element = new Element(negative, addr, prefix); } else { element = new Element(family, negative, data, prefix); } elements.add(element); } }
void rrFromWire(DNSInput in) throws IOException { elements = new ArrayList(1); while (in.remaining() != 0) { int family = in.readU16(); int prefix = in.readU8(); int length = in.readU8(); boolean negative = (length & 0x80) != 0; length &= ~0x80; byte [] data = in.readByteArray(length); Element element; if (!validatePrefixLength(family, prefix)) { throw new WireParseException("invalid prefix length"); } if (family == Address.IPv4 || family == Address.IPv6) { data = parseAddress(data, Address.addressLength(family)); InetAddress addr = InetAddress.getByAddress(data); element = new Element(negative, addr, prefix); } else { element = new Element(family, negative, data, prefix); } elements.add(element); } }
void rrFromWire(DNSInput in) throws IOException { elements = new ArrayList(1); while (in.remaining() != 0) { int family = in.readU16(); int prefix = in.readU8(); int length = in.readU8(); boolean negative = (length & 0x80) != 0; length &= ~0x80; byte [] data = in.readByteArray(length); Element element; if (!validatePrefixLength(family, prefix)) { throw new WireParseException("invalid prefix length"); } if (family == Address.IPv4 || family == Address.IPv6) { data = parseAddress(data, Address.addressLength(family)); InetAddress addr = InetAddress.getByAddress(data); element = new Element(negative, addr, prefix); } else { element = new Element(family, negative, data, prefix); } elements.add(element); } }
void rrFromWire(DNSInput in) throws IOException { elements = new ArrayList(1); while (in.remaining() != 0) { int family = in.readU16(); int prefix = in.readU8(); int length = in.readU8(); boolean negative = (length & 0x80) != 0; length &= ~0x80; byte [] data = in.readByteArray(length); Element element; if (!validatePrefixLength(family, prefix)) { throw new WireParseException("invalid prefix length"); } if (family == Address.IPv4 || family == Address.IPv6) { data = parseAddress(data, Address.addressLength(family)); InetAddress addr = InetAddress.getByAddress(data); element = new Element(negative, addr, prefix); } else { element = new Element(family, negative, data, prefix); } elements.add(element); } }