org.xbill.DNS.ARecord Java Examples

The following examples show how to use org.xbill.DNS.ARecord. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DnsMessageTransportTest.java    From nomulus with Apache License 2.0 6 votes vote down vote up
@Test
public void testSentMessageTooLongThrowsException() throws Exception {
  Update oversize = new Update(Name.fromString("tld", Name.root));
  for (int i = 0; i < 2000; i++) {
    oversize.add(
        ARecord.newRecord(
            Name.fromString("test-extremely-long-name-" + i + ".tld", Name.root),
            Type.A,
            DClass.IN));
  }
  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  when(mockSocket.getOutputStream()).thenReturn(outputStream);
  IllegalArgumentException thrown =
      assertThrows(IllegalArgumentException.class, () -> resolver.send(oversize));
  assertThat(thrown).hasMessageThat().contains("message larger than maximum");
}
 
Example #2
Source File: DNSJavaService.java    From james-project with Apache License 2.0 6 votes vote down vote up
@Override
public InetAddress getByName(String host) throws UnknownHostException {
    TimeMetric timeMetric = metricFactory.timer("getByName");
    String name = allowIPLiteral(host);

    try {
        // Check if its local
        if (name.equalsIgnoreCase(localHostName) || name.equalsIgnoreCase(localCanonicalHostName) || name.equals(localAddress)) {
            return getLocalHost();
        }

        return org.xbill.DNS.Address.getByAddress(name);
    } catch (UnknownHostException e) {
        Record[] records = lookupNoException(name, Type.A, "A");

        if (records != null && records.length >= 1) {
            ARecord a = (ARecord) records[0];
            return InetAddress.getByAddress(name, a.getAddress().getAddress());
        } else {
            throw e;
        }
    } finally {
        timeMetric.stopAndPublish();
    }
}
 
Example #3
Source File: AddressLookupTest.java    From mireka with Apache License 2.0 6 votes vote down vote up
@Test
public void testQueryAddresses() throws SendException {
    new Expectations() {
        {
            lookup.run();
            result =
                    new Record[] {
                            new ARecord(HOST1_EXAMPLE_COM_NAME, 0, 0, IP1),
                            new ARecord(HOST1_EXAMPLE_COM_NAME, 0, 0, IP2)

                    };

        }
    };

    InetAddress[] addresses =
            addressLookup.queryAddresses(HOST1_EXAMPLE_COM_NAME);

    InetAddress[] expected = new InetAddress[] { IP1, IP2 };
    assertArrayEquals(expected, addresses);
}
 
Example #4
Source File: ForwardLookupHelper.java    From yeti with MIT License 6 votes vote down vote up
public static List<ForwardLookupResult> getARecord(String hostName, String domainName) throws TextParseException {
    List<ForwardLookupResult> entries = null;
    if (hostName != null && !hostName.isEmpty() && domainName != null && !domainName.isEmpty()) {
        Record[] recs = new Lookup(hostName, Type.A).run();
        if (recs != null) {
            if (recs.length > 0) {
                entries = new ArrayList<>();
                for (Record record : recs) {
                    ForwardLookupResult foundSubDomain = new ForwardLookupResult(domainName);
                    foundSubDomain.setHostName(hostName);
                    String ipAddress = ((ARecord) record).getAddress().getHostAddress();
                    foundSubDomain.setIpAddress(ipAddress);
                    foundSubDomain.setLookupType("A");
                    entries.add(foundSubDomain);
                }
            }
        }
    }
    return entries;
}
 
Example #5
Source File: DNS.java    From yeti with MIT License 6 votes vote down vote up
public static List<ARecordResult> getARecord(String hostName) throws TextParseException {
    List<ARecordResult> entries = null;

    Record[] recs = new Lookup(hostName, Type.A).run();
    if (recs != null) {
        if (recs.length > 0) {
            entries = new ArrayList<>();
            for (Record record : recs) {
                ARecordResult foundSubDomain = new ARecordResult(NetworkTools.getDomainFromHost(hostName));
                foundSubDomain.setHostName(hostName);
                String ipAddress = ((ARecord) record).getAddress().getHostAddress();
                foundSubDomain.setIpAddress(ipAddress);
                foundSubDomain.setLookupType("A");
                entries.add(foundSubDomain);
            }
        }
    }

    return entries;
}
 
Example #6
Source File: DataAPI.java    From yeti with MIT License 6 votes vote down vote up
public List<ARecordResult> getARecord(String hostName) throws TextParseException {
    List<ARecordResult> entries = null;

    Record[] recs = new Lookup(hostName, Type.A).run();
    if (recs != null) {
        if (recs.length > 0) {
            entries = new ArrayList<>();
            for (Record record : recs) {
                ARecordResult foundSubDomain = new ARecordResult(NetworkTools.getDomainFromHost(hostName));
                foundSubDomain.setHostName(hostName);
                String ipAddress = ((ARecord) record).getAddress().getHostAddress();
                foundSubDomain.setIpAddress(ipAddress);
                foundSubDomain.setLookupType("A");
                entries.add(foundSubDomain);
            }
        }
    }

    return entries;
}
 
Example #7
Source File: Utils.java    From ShadowsocksRR with Apache License 2.0 5 votes vote down vote up
public static String resolve(String host, int addrType) {
    try {
        Lookup lookup = new Lookup(host, addrType);
        SimpleResolver resolver = new SimpleResolver("114.114.114.114");
        resolver.setTimeout(5);
        lookup.setResolver(resolver);
        Record[] result = lookup.run();
        if (result == null || result.length == 0) {
            return null;
        }

        List<Record> records = new ArrayList<>(Arrays.asList(result));
        Collections.shuffle(records);
        for (Record r : records) {
            switch (addrType) {
                case Type.A:
                    return ((ARecord) r).getAddress().getHostAddress();
                case Type.AAAA:
                    return ((AAAARecord) r).getAddress().getHostAddress();
                default:
                    break;
            }
        }
    } catch (Exception e) {
        VayLog.e(TAG, "resolve", e);
        app.track(e);
    }
    return null;
}
 
Example #8
Source File: Utils.java    From Maying with Apache License 2.0 5 votes vote down vote up
public static String resolve(String host, int addrType) {
    try {
        Lookup lookup = new Lookup(host, addrType);
        SimpleResolver resolver = new SimpleResolver("114.114.114.114");
        resolver.setTimeout(5);
        lookup.setResolver(resolver);
        Record[] result = lookup.run();
        if (result == null || result.length == 0) {
            return null;
        }

        List<Record> records = new ArrayList<>(Arrays.asList(result));
        Collections.shuffle(records);
        for (Record r : records) {
            switch (addrType) {
                case Type.A:
                    return ((ARecord) r).getAddress().getHostAddress();
                case Type.AAAA:
                    return ((AAAARecord) r).getAddress().getHostAddress();
                default:
                    break;
            }
        }
    } catch (Exception e) {
        VayLog.e(TAG, "resolve", e);
        ShadowsocksApplication.app.track(e);
    }
    return null;
}
 
Example #9
Source File: DnsUpdateWriter.java    From nomulus with Apache License 2.0 5 votes vote down vote up
private RRset makeAddressSet(HostResource host) {
  RRset addressSet = new RRset();
  for (InetAddress address : host.getInetAddresses()) {
    if (address instanceof Inet4Address) {
      ARecord record =
          new ARecord(
              toAbsoluteName(host.getHostName()),
              DClass.IN,
              dnsDefaultATtl.getStandardSeconds(),
              address);
      addressSet.addRR(record);
    }
  }
  return addressSet;
}
 
Example #10
Source File: DNSJavaService.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<InetAddress> getAllByName(String host) throws UnknownHostException {
    TimeMetric timeMetric = metricFactory.timer("getAllByName");
    String name = allowIPLiteral(host);
    try {
        // Check if its local
        if (name.equalsIgnoreCase(localHostName) || name.equalsIgnoreCase(localCanonicalHostName) || name.equals(localAddress)) {
            return ImmutableList.of(getLocalHost());
        }

        InetAddress addr = org.xbill.DNS.Address.getByAddress(name);
        return ImmutableList.of(addr);
    } catch (UnknownHostException e) {
        Record[] records = lookupNoException(name, Type.A, "A");

        if (records != null && records.length >= 1) {
            InetAddress[] addrs = new InetAddress[records.length];
            for (int i = 0; i < records.length; i++) {
                ARecord a = (ARecord) records[i];
                addrs[i] = InetAddress.getByAddress(name, a.getAddress().getAddress());
            }
            return ImmutableList.copyOf(addrs);
        } else {
            throw e;
        }
    } finally {
        timeMetric.stopAndPublish();
    }
}
 
Example #11
Source File: AddressLookup.java    From mireka with Apache License 2.0 5 votes vote down vote up
private InetAddress[] convertAddressRecordsToAddresses(Record[] records) {
    InetAddress[] addresses = new InetAddress[records.length];
    for (int i = 0; i < records.length; i++) {
        Record record = records[i];
        if (record instanceof ARecord) {
            addresses[i] = ((ARecord) record).getAddress();
        } else if (record instanceof AAAARecord) {
            addresses[i] = ((AAAARecord) record).getAddress();
        } else {
            throw new RuntimeException();
        }
    }
    return addresses;
}
 
Example #12
Source File: ForwardLookupHelper.java    From yeti with MIT License 5 votes vote down vote up
public static List<ForwardLookupResult> attemptZoneTransfer(String domain, List<ForwardLookupResult> nameServers) throws TextParseException {
    List<ForwardLookupResult> result = new ArrayList<>();

    ZoneTransferIn xfr;
    Iterator i = nameServers.iterator();
    for (ForwardLookupResult nameServer : nameServers) {
        try {
            xfr = ZoneTransferIn.newAXFR(new Name(domain), nameServer.getIpAddress(), null);
            List records = xfr.run();
            for (Iterator it = records.iterator(); it.hasNext();) {
                Record r = (Record) it.next();
                if (r.getType() == 1) {
                    ForwardLookupResult rec = new ForwardLookupResult(domain);
                    String hostName = ((ARecord) r).getName().toString().toLowerCase();

                    if (hostName.endsWith(".")) {
                        hostName = hostName.substring(0, hostName.length() - 1);
                    }

                    rec.setHostName(hostName);
                    rec.setIpAddress(((ARecord) r).getAddress().getHostAddress());
                    rec.setLookupType("A");
                    result.add(rec);
                }
            }
        } catch (IOException ioex) {
            Logger.getLogger("ForwardLookupHelper.attemptZoneTransfer").log(Level.WARNING, null, ioex);
        } catch (ZoneTransferException zex) {
            Log.debug("ForwardLookupHelper.attemptZoneTransfer: Failed zonetransfer");
        }
    }
    return result;
}
 
Example #13
Source File: NetworkTools.java    From yeti with MIT License 5 votes vote down vote up
public static List<String> getIpFromHost(String hostname) throws TextParseException {
    List<String> result = new ArrayList<>();
    Record[] recs = new Lookup(hostname, Type.A).run();
    if (recs != null) {
        if (recs.length > 0) {
            for (Record rec : recs) {
                String ipAddress = ((ARecord) rec).getAddress().toString();
                result.add(ipAddress.replace("/", ""));
            }
        }
    }
    return result;
}