/** * Determines the IP address of a host * @param name The hostname to look up * @return The first matching IP address * @exception UnknownHostException The hostname does not have any addresses */ public static InetAddress getByName(String name) throws UnknownHostException { try { return getByAddress(name); } catch (UnknownHostException e) { Record [] records = lookupHostName(name, false); return addrFromRecord(name, records[0]); } }
if (address == null) { address = Address.getByName(host);
/** * Determines if a string contains a valid IP address. * @param s The string * @return Whether the string contains a valid IP address */ public static boolean isDottedQuad(String s) { byte [] address = Address.toByteArray(s, IPv4); return (address != null); }
/** * Convert a string containing an IP address to an array of 4 or 16 bytes. * @param s The address, in text format. * @param family The address family. * @return The address */ public static byte [] toByteArray(String s, int family) { if (family == IPv4) return parseV4(s); else if (family == IPv6) return parseV6(s); else throw new IllegalArgumentException("unknown address family"); }
protected void initializeDefaults() throws UnknownHostException { if (m_ports.size() == 0) { m_ports.add(Integer.valueOf(53)); } if (m_addresses.size() == 0) { m_addresses.add(Address.getByAddress("0.0.0.0")); } }
/** * Creates an APL element corresponding to an IPv4 or IPv6 prefix. * @param negative Indicates if this prefix is a negation. * @param address The IPv4 or IPv6 address. * @param prefixLength The length of this prefix, in bits. * @throws IllegalArgumentException The prefix length is invalid. */ public Element(boolean negative, InetAddress address, int prefixLength) { this(Address.familyOf(address), negative, address, prefixLength); }
/** * 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"); } }
/** * Construct a Client Subnet option. Note that the number of significant bits * in the address must not be greater than the supplied source netmask. There * may also be issues related to Java's handling of mapped addresses * @param sourceNetmask The length of the netmask pertaining to the query. * In replies, it mirrors the same value as in the requests. * @param scopeNetmask The length of the netmask pertaining to the reply. * In requests, it MUST be set to 0. In responses, this may or may not match * the source netmask. * @param address The address of the client. */ public ClientSubnetOption(int sourceNetmask, int scopeNetmask, InetAddress address) { super(EDNSOption.Code.CLIENT_SUBNET); this.family = Address.familyOf(address); this.sourceNetmask = checkMaskLength("source netmask", this.family, sourceNetmask); this.scopeNetmask = checkMaskLength("scope netmask", this.family, scopeNetmask); this.address = Address.truncate(address, sourceNetmask); if (!address.equals(this.address)) throw new IllegalArgumentException("source netmask is not " + "valid for 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); InetAddress tmp = Address.truncate(address, sourceNetmask); if (!tmp.equals(address)) throw new WireParseException("invalid padding");
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; }
/** * Convert a string containing an IPv4 address to an array of 4 integers. * @param s The address, in text format. * @return The address */ public static int [] toArray(String s) { return toArray(s, IPv4); }
/** * Gets the next token from a tokenizer and converts it to an IP Address. * @param family The address family. * @return The next token in the stream, as an InetAddress * @throws TextParseException The input was invalid or not a valid address. * @throws IOException An I/O error occurred. * @see Address */ public InetAddress getAddress(int family) throws IOException { String next = _getIdentifier("an address"); try { return Address.getByAddress(next, family); } catch (UnknownHostException e) { throw exception(e.getMessage()); } }
/** * Creates an APL element corresponding to an IPv4 or IPv6 prefix. * @param negative Indicates if this prefix is a negation. * @param address The IPv4 or IPv6 address. * @param prefixLength The length of this prefix, in bits. * @throws IllegalArgumentException The prefix length is invalid. */ public Element(boolean negative, InetAddress address, int prefixLength) { this(Address.familyOf(address), negative, address, prefixLength); }
/** * Convert a string containing an IP address to an array of 4 or 16 bytes. * @param s The address, in text format. * @param family The address family. * @return The address */ public static byte [] toByteArray(String s, int family) { if (family == IPv4) return parseV4(s); else if (family == IPv6) return parseV6(s); else throw new IllegalArgumentException("unknown address family"); }
/** * 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"); } }
/** * Construct a Client Subnet option. Note that the number of significant bits in * the address must not be greater than the supplied source netmask. * XXX something about Java's mapped addresses * @param sourceNetmask The length of the netmask pertaining to the query. * In replies, it mirrors the same value as in the requests. * @param scopeNetmask The length of the netmask pertaining to the reply. * In requests, it MUST be set to 0. In responses, this may or may not match * the source netmask. * @param address The address of the client. */ public ClientSubnetOption(int sourceNetmask, int scopeNetmask, InetAddress address) { super(Code.CLIENT_SUBNET); this.family = Address.familyOf(address); this.sourceNetmask = checkMaskLength("source netmask", this.family, sourceNetmask); this.scopeNetmask = checkMaskLength("scope netmask", this.family, scopeNetmask); this.address = Address.truncate(address, sourceNetmask); if (!address.equals(this.address)) throw new IllegalArgumentException("source netmask is not " + "valid for 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); InetAddress tmp = Address.truncate(address, sourceNetmask); if (!tmp.equals(address)) throw new WireParseException("invalid padding");
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; }
/** * Convert a string containing an IPv4 address to an array of 4 integers. * @param s The address, in text format. * @return The address */ public static int [] toArray(String s) { return toArray(s, IPv4); }
/** * Determines the IP address of a host * @param name The hostname to look up * @return The first matching IP address * @exception UnknownHostException The hostname does not have any addresses */ public static InetAddress getByName(String name) throws UnknownHostException { try { return getByAddress(name); } catch (UnknownHostException e) { Record [] records = lookupHostName(name); return addrFromRecord(name, records[0]); } }