use of org.xbill.DNS.TextParseException in project fabric8 by jboss-fuse.
the class KubernetesHelper method lookupServiceInDns.
/**
* Looks up the service in DNS.
* If this is a headless service, this call returns the endpoint IPs from DNS.
* If this is a non-headless service, this call returns the service IP only.
* <p/>
* See https://github.com/GoogleCloudPlatform/kubernetes/blob/master/docs/services.md#headless-services
*/
public static Set<String> lookupServiceInDns(String serviceName) throws IllegalArgumentException, UnknownHostException {
try {
Lookup l = new Lookup(serviceName);
Record[] records = l.run();
if (l.getResult() == Lookup.SUCCESSFUL) {
Set<String> endpointAddresses = new HashSet<>(records.length);
for (int i = 0; i < records.length; i++) {
ARecord aRecord = (ARecord) records[i];
endpointAddresses.add(aRecord.getAddress().getHostAddress());
}
return endpointAddresses;
} else {
LOG.warn("Lookup {} result: {}", serviceName, l.getErrorString());
}
} catch (TextParseException e) {
LOG.error("Unparseable service name: {}", serviceName, e);
} catch (ClassCastException e) {
LOG.error("Invalid response from DNS server - should have been A records", e);
}
return Collections.EMPTY_SET;
}
use of org.xbill.DNS.TextParseException in project nhin-d by DirectProject.
the class ServiceTest method testSOA.
// @Test
public void testSOA() {
DNSEntryForm SoadnsForm = new DNSEntryForm();
SoadnsForm.setName("savvy");
SoadnsForm.setTtl(84555L);
SoadnsForm.setAdmin("ns.savvy.com");
SoadnsForm.setDomain("ns2.savvy.com");
SoadnsForm.setSerial(4L);
SoadnsForm.setRefresh(6L);
SoadnsForm.setRetry(8L);
SoadnsForm.setExpire(66L);
SoadnsForm.setMinimum(22L);
Collection<DNSRecord> records = new ArrayList<DNSRecord>();
records.add(DNSRecordUtils.createSOARecord(SoadnsForm.getName(), SoadnsForm.getTtl(), SoadnsForm.getDomain(), SoadnsForm.getAdmin(), (int) SoadnsForm.getSerial(), SoadnsForm.getRefresh(), SoadnsForm.getRetry(), SoadnsForm.getExpire(), SoadnsForm.getMinimum()));
try {
configSvc.addDNS(records);
Collection<DNSRecord> arecords = configSvc.getDNSByType(DNSType.SOA.getValue());
for (Iterator<DNSRecord> iter = arecords.iterator(); iter.hasNext(); ) {
DNSRecord arec = iter.next();
SOARecord newrec = (SOARecord) Record.newRecord(Name.fromString(arec.getName()), arec.getType(), arec.getDclass(), arec.getTtl(), arec.getData());
System.out.println("A admin: " + newrec.getAdmin());
System.out.println("A name: " + newrec.getName());
}
} catch (ConfigurationServiceException e) {
e.printStackTrace();
} catch (TextParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
use of org.xbill.DNS.TextParseException in project opennms by OpenNMS.
the class DnsUtils method resolveHostname.
/**
* This function is used inside XSLT documents, do a string search before refactoring.
*/
public static InetAddress resolveHostname(final String hostname, final boolean preferInet6Address, final boolean throwException) throws UnknownHostException {
InetAddress retval = null;
// return valid A and AAAA records for "localhost".
if ("localhost".equals(hostname)) {
return preferInet6Address ? InetAddress.getByName("::1") : InetAddress.getByName("127.0.0.1");
}
try {
// 2011-05-22 - Matt is seeing some platform-specific inconsistencies when using
// InetAddress.getAllByName(). It seems to miss some addresses occasionally on Mac.
// We need to use dnsjava here instead since it should be 100% reliable.
//
// InetAddress[] addresses = InetAddress.getAllByName(hostname);
//
List<InetAddress> v4Addresses = new ArrayList<>();
try {
Record[] aRecs = new Lookup(hostname, Type.A).run();
if (aRecs != null) {
for (Record aRec : aRecs) {
if (aRec instanceof ARecord) {
InetAddress addr = ((ARecord) aRec).getAddress();
if (addr instanceof Inet4Address) {
v4Addresses.add(addr);
} else {
// Should never happen
throw new UnknownHostException("Non-IPv4 address found via A record DNS lookup of host: " + hostname + ": " + addr.toString());
}
}
}
} else {
// throw new UnknownHostException("No IPv4 addresses found via A record DNS lookup of host: " + hostname);
}
} catch (final TextParseException e) {
final UnknownHostException ex = new UnknownHostException("Could not perform A record lookup for host: " + hostname);
ex.initCause(e);
throw ex;
}
final List<InetAddress> v6Addresses = new ArrayList<>();
try {
final Record[] quadARecs = new Lookup(hostname, Type.AAAA).run();
if (quadARecs != null) {
for (final Record quadARec : quadARecs) {
final InetAddress addr = ((AAAARecord) quadARec).getAddress();
if (addr instanceof Inet6Address) {
v6Addresses.add(addr);
} else {
// Should never happen
throw new UnknownHostException("Non-IPv6 address found via AAAA record DNS lookup of host: " + hostname + ": " + addr.toString());
}
}
} else {
// throw new UnknownHostException("No IPv6 addresses found via AAAA record DNS lookup of host: " + hostname);
}
} catch (final TextParseException e) {
final UnknownHostException ex = new UnknownHostException("Could not perform AAAA record lookup for host: " + hostname);
ex.initCause(e);
throw ex;
}
final List<InetAddress> addresses = new ArrayList<>();
if (preferInet6Address) {
addresses.addAll(v6Addresses);
addresses.addAll(v4Addresses);
} else {
addresses.addAll(v4Addresses);
addresses.addAll(v6Addresses);
}
for (final InetAddress address : addresses) {
retval = address;
if (!preferInet6Address && retval instanceof Inet4Address)
break;
if (preferInet6Address && retval instanceof Inet6Address)
break;
}
if (preferInet6Address && !(retval instanceof Inet6Address)) {
throw new UnknownHostException("No IPv6 address could be found for the hostname: " + hostname);
}
} catch (final UnknownHostException e) {
if (throwException) {
throw e;
} else {
// e.printStackTrace();
return null;
}
}
return retval;
}
use of org.xbill.DNS.TextParseException in project xabber-android by redsolution.
the class ExtDNSJavaResolver method lookupSRVRecords0.
@Override
protected List<SRVRecord> lookupSRVRecords0(String name, List<HostAddress> failedAddresses, ConnectionConfiguration.DnssecMode dnssecMode) {
List<SRVRecord> res = new ArrayList<SRVRecord>();
org.xbill.DNS.ResolverConfig.refresh();
ExtLookup lookup;
String[] servers = getDNSServersListForOreo();
try {
lookup = new ExtLookup(name, Type.SRV);
if (servers != null && servers.length > 0)
lookup.setResolver(new ExtendedResolver(servers));
else
lookup.setResolver(new ExtendedResolver());
} catch (TextParseException e) {
throw new IllegalStateException(e);
} catch (UnknownHostException e) {
throw new RuntimeException("Failed to initialize resolver");
}
Record[] recs = lookup.run();
if (recs == null)
return res;
for (Record record : recs) {
org.xbill.DNS.SRVRecord srvRecord = (org.xbill.DNS.SRVRecord) record;
if (srvRecord != null && srvRecord.getTarget() != null) {
String host = srvRecord.getTarget().toString();
int port = srvRecord.getPort();
int priority = srvRecord.getPriority();
int weight = srvRecord.getWeight();
List<InetAddress> hostAddresses = lookupHostAddress0(host, failedAddresses, dnssecMode);
if (hostAddresses == null) {
continue;
}
SRVRecord r = new SRVRecord(host, port, priority, weight, hostAddresses);
res.add(r);
}
}
return res;
}
use of org.xbill.DNS.TextParseException in project jmeter by apache.
the class DNSCacheManager method customRequestLookup.
private InetAddress[] customRequestLookup(String host) throws UnknownHostException {
InetAddress[] addresses = null;
try {
Lookup lookup = new Lookup(host, Type.A);
lookup.setCache(lookupCache);
if (timeoutMs > 0) {
resolver.setTimeout(timeoutMs / 1000, timeoutMs % 1000);
}
lookup.setResolver(resolver);
Record[] records = lookup.run();
if (records == null || records.length == 0) {
throw new UnknownHostException("Failed to resolve host name: " + host);
}
addresses = new InetAddress[records.length];
for (int i = 0; i < records.length; i++) {
addresses[i] = ((ARecord) records[i]).getAddress();
}
} catch (TextParseException tpe) {
// NOSONAR Exception handled
log.debug("Failed to create Lookup object for host:{}, error message:{}", host, tpe.toString());
}
return addresses;
}
Aggregations