AdVpnThread implements Runnable, DnsPacketProxy.EventLoop { void newDNSServer(VpnService.Builder builder, String format, byte[] ipv6Template, InetAddress addr) throws UnknownHostException { if (addr instanceof Inet6Address && ipv6Template == null) { Log.i(TAG, "newDNSServer: Ignoring DNS server " + addr); } else if (addr instanceof Inet4Address && format == null) { Log.i(TAG, "newDNSServer: Ignoring DNS server " + addr); } else if (addr instanceof Inet4Address) { upstreamDnsServers.add(addr); String alias = String.format(format, upstreamDnsServers.size() + 1); Log.i(TAG, "configure: Adding DNS Server " + addr + " as " + alias); builder.addDnsServer(alias); builder.addRoute(alias, 32); vpnWatchDog.setTarget(InetAddress.getByName(alias)); } else if (addr instanceof Inet6Address) { upstreamDnsServers.add(addr); ipv6Template[ipv6Template.length - 1] = (byte) (upstreamDnsServers.size() + 1); InetAddress i6addr = Inet6Address.getByAddress(ipv6Template); Log.i(TAG, "configure: Adding DNS Server " + addr + " as " + i6addr); builder.addDnsServer(i6addr); vpnWatchDog.setTarget(i6addr); } } AdVpnThread(VpnService vpnService, Notify notify); void startThread(); void stopThread(); @Override synchronized void run(); void forwardPacket(DatagramPacket outPacket, IpPacket parsedPacket); void queueDeviceWrite(IpPacket ipOutPacket);  }
@Test public void testNewDNSServer() throws Exception { String format = "192.168.0.%d"; byte[] ipv6Template = new byte[]{32, 1, 13, (byte) (184 & 0xFF), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; InetAddress i6addr = Inet6Address.getByName("::1"); InetAddress i4addr = Inet4Address.getByName("127.0.0.1"); thread.newDNSServer(builder, format, ipv6Template, i4addr); assertTrue(thread.upstreamDnsServers.contains(i4addr)); assertTrue(serversAdded.contains(InetAddress.getByName("192.168.0.2"))); thread.newDNSServer(builder, format, ipv6Template, i6addr); assertTrue(thread.upstreamDnsServers.contains(i6addr)); assertEquals(3, ipv6Template[ipv6Template.length - 1]); assertTrue(serversAdded.contains(InetAddress.getByAddress(ipv6Template))); }
@Test public void testNewDNSServer_ipv6disabled() throws Exception { byte[] ipv6Template = new byte[]{32, 1, 13, (byte) (184 & 0xFF), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; InetAddress i6addr = Inet6Address.getByName("::1"); thread.newDNSServer(builder, "192.168.0.%d", null, i6addr); assertTrue(serversAdded.isEmpty()); assertTrue(thread.upstreamDnsServers.isEmpty()); InetAddress i4addr = Inet4Address.getByName("127.0.0.1"); thread.newDNSServer(builder, "192.168.0.%d", null, i4addr); assertTrue(serversAdded.contains(InetAddress.getByName("192.168.0.2"))); assertTrue(thread.upstreamDnsServers.contains(i4addr)); }
@Test public void testNewDNSServer_ipv4disabled() throws Exception { String format = "192.168.0.%d"; byte[] ipv6Template = new byte[]{32, 1, 13, (byte) (184 & 0xFF), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; InetAddress i6addr = Inet6Address.getByName("::1"); InetAddress i4addr = Inet4Address.getByName("127.0.0.1"); thread.newDNSServer(builder, null, ipv6Template, i4addr); assertTrue(thread.upstreamDnsServers.isEmpty()); assertTrue(serversAdded.isEmpty()); thread.newDNSServer(builder, format, ipv6Template, i6addr); assertTrue(thread.upstreamDnsServers.contains(i6addr)); assertEquals(2, ipv6Template[ipv6Template.length - 1]); assertTrue(serversAdded.contains(InetAddress.getByAddress(ipv6Template))); }