Java Code Examples for org.xbill.DNS.Lookup#SUCCESSFUL

The following examples show how to use org.xbill.DNS.Lookup#SUCCESSFUL . 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: XBillDnsSrvResolver.java    From dns-java with Apache License 2.0 7 votes vote down vote up
@Override
public List<LookupResult> resolve(final String fqdn) {
  Lookup lookup = lookupFactory.forName(fqdn);
  Record[] queryResult = lookup.run();

  switch (lookup.getResult()) {
    case Lookup.SUCCESSFUL:
      return toLookupResults(queryResult);
    case Lookup.HOST_NOT_FOUND:
      // fallthrough
    case Lookup.TYPE_NOT_FOUND:
      LOG.warn("No results returned for query '{}'; result from XBill: {} - {}",
          fqdn, lookup.getResult(), lookup.getErrorString());
      return ImmutableList.of();
    default:
      throw new DnsException(
          String.format("Lookup of '%s' failed with code: %d - %s ",
              fqdn, lookup.getResult(), lookup.getErrorString()));
  }
}
 
Example 2
Source File: lookup.java    From dnsjava with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public static void printAnswer(String name, Lookup lookup) {
  System.out.print(name + ":");
  int result = lookup.getResult();
  if (result != Lookup.SUCCESSFUL) {
    System.out.print(" " + lookup.getErrorString());
  }
  System.out.println();
  Name[] aliases = lookup.getAliases();
  if (aliases.length > 0) {
    System.out.print("# aliases: ");
    for (int i = 0; i < aliases.length; i++) {
      System.out.print(aliases[i]);
      if (i < aliases.length - 1) {
        System.out.print(" ");
      }
    }
    System.out.println();
  }
  if (lookup.getResult() == Lookup.SUCCESSFUL) {
    Record[] answers = lookup.getAnswers();
    for (Record answer : answers) {
      System.out.println(answer);
    }
  }
}
 
Example 3
Source File: Helperfunctions.java    From open-rmbt with Apache License 2.0 6 votes vote down vote up
public static String reverseDNSLookup(final InetAddress adr)
{
    try
    {
        final Name name = ReverseMap.fromAddress(adr);
        
        final Lookup lookup = new Lookup(name, Type.PTR);
        lookup.setResolver(new SimpleResolver());
        lookup.setCache(null);
        final Record[] records = lookup.run();
        if (lookup.getResult() == Lookup.SUCCESSFUL)
            for (final Record record : records)
                if (record instanceof PTRRecord)
                {
                    final PTRRecord ptr = (PTRRecord) record;
                    return ptr.getTarget().toString();
                }
    }
    catch (final Exception e)
    {
    }
    return null;
}
 
Example 4
Source File: AddressLookup.java    From mireka with Apache License 2.0 5 votes vote down vote up
private Record[] queryAddressRecords(Name name) throws SendException {
    Lookup lookup = new Lookup(name);
    Record[] records = lookup.run();
    switch (lookup.getResult()) {
    case Lookup.SUCCESSFUL:
        return records;
    case Lookup.TYPE_NOT_FOUND:
        throw new SendException("Host " + name + " has no address record ("
                + lookup.getErrorString() + ")",
                EnhancedStatus.PERMANENT_UNABLE_TO_ROUTE);
    case Lookup.HOST_NOT_FOUND:
        throw new SendException("Host " + name + " is not found ("
                + lookup.getErrorString() + ")",
                EnhancedStatus.PERMANENT_UNABLE_TO_ROUTE);
    case Lookup.TRY_AGAIN:
        throw new SendException(
                "DNS network failure while looking up address of " + name
                        + ": " + lookup.getErrorString(),
                EnhancedStatus.TRANSIENT_DIRECTORY_SERVER_FAILURE);
    case Lookup.UNRECOVERABLE:
        throw new SendException(
                "Unrecoverable DNS error while looking up address of "
                        + name + ": " + lookup.getErrorString(),
                EnhancedStatus.PERMANENT_UNABLE_TO_ROUTE);
    default:
        throw new SendException(
                "Unknown DNS status while looking up address of " + name
                        + ": " + lookup.getResult() + ". "
                        + lookup.getErrorString(),
                EnhancedStatus.PERMANENT_INTERNAL_ERROR);
    }
}
 
Example 5
Source File: Helperfunctions.java    From open-rmbt with Apache License 2.0 5 votes vote down vote up
public static Long getASN(final InetAddress adr)
{
    try
    {
        final Name postfix;
        if (adr instanceof Inet6Address)
            postfix = Name.fromConstantString("origin6.asn.cymru.com");
        else
            postfix = Name.fromConstantString("origin.asn.cymru.com");
        
        final Name name = getReverseIPName(adr, postfix);
        System.out.println("lookup: " + name);
        
        final Lookup lookup = new Lookup(name, Type.TXT);
        lookup.setResolver(new SimpleResolver());
        lookup.setCache(null);
        final Record[] records = lookup.run();
        if (lookup.getResult() == Lookup.SUCCESSFUL)
            for (final Record record : records)
                if (record instanceof TXTRecord)
                {
                    final TXTRecord txt = (TXTRecord) record;
                    @SuppressWarnings("unchecked")
                    final List<String> strings = txt.getStrings();
                    if (strings != null && !strings.isEmpty())
                    {
                        final String result = strings.get(0);
                        final String[] parts = result.split(" ?\\| ?");
                        if (parts != null && parts.length >= 1)
                            return new Long(parts[0]);
                    }
                }
    }
    catch (final Exception e)
    {
    }
    return null;
}
 
Example 6
Source File: Helperfunctions.java    From open-rmbt with Apache License 2.0 5 votes vote down vote up
public static String getASName(final long asn)
{
    try
    {
        final Name postfix = Name.fromConstantString("asn.cymru.com.");
        final Name name = new Name(String.format("AS%d", asn), postfix);
        System.out.println("lookup: " + name);
        
        final Lookup lookup = new Lookup(name, Type.TXT);
        lookup.setResolver(new SimpleResolver());
        lookup.setCache(null);
        final Record[] records = lookup.run();
        if (lookup.getResult() == Lookup.SUCCESSFUL)
            for (final Record record : records)
                if (record instanceof TXTRecord)
                {
                    final TXTRecord txt = (TXTRecord) record;
                    @SuppressWarnings("unchecked")
                    final List<String> strings = txt.getStrings();
                    if (strings != null && !strings.isEmpty())
                    {
                        System.out.println(strings);
                        
                        final String result = strings.get(0);
                        final String[] parts = result.split(" ?\\| ?");
                        if (parts != null && parts.length >= 1)
                            return parts[4];
                    }
                }
    }
    catch (final Exception e)
    {
    }
    return null;
}
 
Example 7
Source File: Helperfunctions.java    From open-rmbt with Apache License 2.0 5 votes vote down vote up
public static String getAScountry(final long asn)
{
    try
    {
        final Name postfix = Name.fromConstantString("asn.cymru.com.");
        final Name name = new Name(String.format("AS%d", asn), postfix);
        System.out.println("lookup: " + name);
        
        final Lookup lookup = new Lookup(name, Type.TXT);
        lookup.setResolver(new SimpleResolver());
        lookup.setCache(null);
        final Record[] records = lookup.run();
        if (lookup.getResult() == Lookup.SUCCESSFUL)
            for (final Record record : records)
                if (record instanceof TXTRecord)
                {
                    final TXTRecord txt = (TXTRecord) record;
                    @SuppressWarnings("unchecked")
                    final List<String> strings = txt.getStrings();
                    if (strings != null && !strings.isEmpty())
                    {
                        final String result = strings.get(0);
                        final String[] parts = result.split(" ?\\| ?");
                        if (parts != null && parts.length >= 1)
                            return parts[1];
                    }
                }
    }
    catch (final Exception e)
    {
    }
    return null;
}