public static void main(final String[] args) throws UnknownHostException { long t = System.nanoTime(); System.out.println(Address.getByName("www.google.fr").getHostAddress() + " " + (System.nanoTime() - t)); t = System.nanoTime(); System.out.println(InetAddress.getByName("www.google.fr").getHostAddress() + " " + (System.nanoTime() - t)); }
@Override public InetAddress[] resolve(String hostname) throws UnknownHostException { if ("localhost".equals(hostname)) return Frontier.LOOPBACK; // DnsJava does not understand dotted-notation IP addresses with additional zeroes (e.g., 127.0.0.01). if (RuntimeConfiguration.DOTTED_ADDRESS.matcher(hostname).matches()) return InetAddress.getAllByName(hostname); // This avoid expensive trials with domain suffixes (but must not be applied to dotted-notation IP addresses). hostname = hostname.endsWith(".") ? hostname : hostname + "."; return Address.getAllByName(hostname); }
/** * @return null if the host is not listed */ private InetAddress queryARecord() { try { return Address.getByName(queryDomain); } catch (UnknownHostException e) { return null; } }
public static ByteBuffer handle_dns_packet(Packet packet) { if (DOMAINS_IP_MAPS == null) { Log.d(TAG, "DOMAINS_IP_MAPS IS NULL HOST FILE ERROR"); return null; } try { ByteBuffer packet_buffer=packet.backingBuffer; packet_buffer.mark(); byte[] tmp_bytes = new byte[packet_buffer.remaining()]; packet_buffer.get(tmp_bytes); packet_buffer.reset(); Message message = new Message(tmp_bytes); Name query_domain = message.getQuestion().getName(); String query_string = query_domain.toString(); Log.d(TAG, "query: " + query_domain); if (!DOMAINS_IP_MAPS.containsKey(query_string)) { query_string="."+query_string; int j=0; while (true){ int i=query_string.indexOf(".",j); if (i==-1){ return null; } String str=query_string.substring(i); if("".equals(str)){ return null; } if(DOMAINS_IP_MAPS.containsKey(str)){ query_string=str; break; } j=i+1; } } InetAddress address = Address.getByAddress(DOMAINS_IP_MAPS.get(query_string)); ARecord a_record = new ARecord(query_domain, 1, 86400, address); message.addRecord(a_record, 1); message.getHeader().setFlag(Flags.QR); packet_buffer.limit(packet_buffer.capacity()); packet_buffer.put(message.toWire()); packet_buffer.limit(packet_buffer.position()); packet_buffer.reset(); packet.swapSourceAndDestination(); packet.updateUDPBuffer(packet_buffer, packet_buffer.remaining()); packet_buffer.position(packet_buffer.limit()); Log.d(TAG, "hit: " + query_domain.toString() + " " + address.getHostName()); return packet_buffer; } catch (Exception e) { Log.d(TAG, "dns hook error", e); return null; } }
public InetAddress getIp(final String name) throws UnknownHostException { return Address.getByName(name); }
@Converter public static String toString(Address address) { return address.toString(); }
@Converter public static InetAddress toInetAddress(String domain) throws UnknownHostException { return Address.getByName(domain); }
public InetAddress getIpAddress(String name) throws UnknownHostException { InetAddress addr = Address.getByName(name); return addr; }
@Override public List<InetAddress> lookup(String hostname) throws UnknownHostException { // I'm initializing the DNS resolvers here to take advantage of this method being called in a background-thread managed by OkHttp init(); return Collections.singletonList(Address.getByName(hostname)); }