Java Code Examples for org.xbill.DNS.Name#fromString()

The following examples show how to use org.xbill.DNS.Name#fromString() . 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: XBillDnsSrvResolverTest.java    From dns-java with Apache License 2.0 6 votes vote down vote up
private Message messageWithNodes(String query, String[] names) throws TextParseException {
  Name queryName = Name.fromString(query);
  Record question = Record.newRecord(queryName, Type.SRV, DClass.IN);
  Message queryMessage = Message.newQuery(question);
  Message result = new Message();
  result.setHeader(queryMessage.getHeader());
  result.addRecord(question, Section.QUESTION);

  for (String name1 : names) {
    result.addRecord(
        new SRVRecord(queryName, DClass.IN, 1, 1, 1, 8080, Name.fromString(name1)),
        Section.ANSWER);
  }

  return result;
}
 
Example 3
Source File: Output.java    From helios with Apache License 2.0 6 votes vote down vote up
public static String shortHostname(final String host) {
  final Name root = Name.fromConstantString(".");
  final Name hostname;
  try {
    hostname = Name.fromString(host, root);
  } catch (TextParseException e) {
    throw new IllegalArgumentException("Invalid hostname '" + host + "'");
  }

  final ResolverConfig currentConfig = ResolverConfig.getCurrentConfig();
  if (currentConfig != null) {
    final Name[] searchPath = currentConfig.searchPath();
    if (searchPath != null) {
      for (final Name domain : searchPath) {
        if (hostname.subdomain(domain)) {
          return hostname.relativize(domain).toString();
        }
      }
    }
  }
  return hostname.toString();
}
 
Example 4
Source File: DnsUpdateWriter.java    From nomulus with Apache License 2.0 5 votes vote down vote up
private Name toAbsoluteName(String name) {
  try {
    return Name.fromString(name, Name.root);
  } catch (TextParseException e) {
    throw new RuntimeException(
        String.format("toAbsoluteName failed for name: %s in zone: %s", name, zoneName), e);
  }
}
 
Example 5
Source File: XBillDnsSrvResolverTest.java    From dns-java with Apache License 2.0 5 votes vote down vote up
private Message messageWithRCode(String query, int rcode) throws TextParseException {
  Name queryName = Name.fromString(query);
  Record question = Record.newRecord(queryName, Type.SRV, DClass.IN);
  Message queryMessage = Message.newQuery(question);
  Message result = new Message();
  result.setHeader(queryMessage.getHeader());
  result.addRecord(question, Section.QUESTION);

  result.getHeader().setRcode(rcode);

  return result;
}
 
Example 6
Source File: BaseResolverConfigProvider.java    From dnsjava with BSD 2-Clause "Simplified" License 5 votes vote down vote up
protected void addSearchPath(String searchPath) {
  if (searchPath == null || searchPath.isEmpty()) {
    return;
  }

  try {
    Name n = Name.fromString(searchPath, Name.root);
    if (!searchlist.contains(n)) {
      searchlist.add(n);
      log.debug("Added {} to search paths", n);
    }
  } catch (TextParseException e) {
    log.warn("Could not parse search path {} as a dns name, ignoring", searchPath);
  }
}
 
Example 7
Source File: jnamed.java    From dnsjava with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void addPrimaryZone(String zname, String zonefile) throws IOException {
  Name origin = null;
  if (zname != null) {
    origin = Name.fromString(zname, Name.root);
  }
  Zone newzone = new Zone(origin, zonefile);
  znames.put(newzone.getOrigin(), newzone);
}
 
Example 8
Source File: Helperfunctions.java    From open-rmbt with Apache License 2.0 5 votes vote down vote up
public static Name getReverseIPName(final InetAddress adr, final Name postfix)
{
    final byte[] addr = adr.getAddress();
    final StringBuilder sb = new StringBuilder();
    if (addr.length == 4)
        for (int i = addr.length - 1; i >= 0; i--)
        {
            sb.append(addr[i] & 0xFF);
            if (i > 0)
                sb.append(".");
        }
    else
    {
        final int[] nibbles = new int[2];
        for (int i = addr.length - 1; i >= 0; i--)
        {
            nibbles[0] = (addr[i] & 0xFF) >> 4;
            nibbles[1] = addr[i] & 0xFF & 0xF;
            for (int j = nibbles.length - 1; j >= 0; j--)
            {
                sb.append(Integer.toHexString(nibbles[j]));
                if (i > 0 || j > 0)
                    sb.append(".");
            }
        }
    }
    try
    {
        return Name.fromString(sb.toString(), postfix);
    }
    catch (final TextParseException e)
    {
        throw new IllegalStateException("name cannot be invalid");
    }
}
 
Example 9
Source File: DNSJavaServiceTest.java    From james-project with Apache License 2.0 4 votes vote down vote up
private static Zone loadZone(String zoneName) throws IOException {
    String zoneFilename = zoneName + "zone";
    URL zoneResource = Resources.getResource(DNSJavaServiceTest.class, zoneFilename);
    assertThat(zoneResource).withFailMessage("test resource for zone could not be loaded: " + zoneFilename).isNotNull();
    return new Zone(Name.fromString(zoneName), zoneResource.getFile());
}
 
Example 10
Source File: jnamed.java    From dnsjava with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void addSecondaryZone(String zone, String remote)
    throws IOException, ZoneTransferException {
  Name zname = Name.fromString(zone, Name.root);
  Zone newzone = new Zone(zname, DClass.IN, remote);
  znames.put(zname, newzone);
}
 
Example 11
Source File: jnamed.java    From dnsjava with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void addTSIG(String algstr, String namestr, String key) throws IOException {
  Name name = Name.fromString(namestr, Name.root);
  TSIGs.put(name, new TSIG(algstr, namestr, key));
}
 
Example 12
Source File: Domain.java    From mireka with Apache License 2.0 3 votes vote down vote up
/**
 * converts this value to a dnsjava absolute domain name, assuming that the
 * name represented by this object is absolute. Domains must be absolute in
 * SMTP, except perhaps in a submission server.
 * 
 * @throws RuntimeException
 *             if the name in this object is syntactically invalid as a
 *             domain name
 */
public Name toName() throws RuntimeException {
    String absoluteNameString = value.endsWith(".") ? value : value + ".";
    try {
        return Name.fromString(absoluteNameString);
    } catch (TextParseException e) {
        throw new RuntimeException(e);
    }
}