use of org.xbill.DNS.TextParseException in project GNS by MobilityFirst.
the class NameResolution method getMXRecordsFromMXField.
/**
* retrieve MX record from MX field of a JSON object
* the key "MX" contains a list of all MX records
* the key "A" contains a list of all A records, which must be put into ADDITIONAL section
*
* @return
*/
private static JSONObject getMXRecordsFromMXField(JSONObject fieldResponseJson, String nameToResolve) {
JSONObject obj = new JSONObject();
JSONArray mxList = new JSONArray();
JSONArray aList = new JSONArray();
if (fieldResponseJson.has("MX")) {
JSONArray records = null;
int ttl = 3600;
try {
JSONObject mxname = fieldResponseJson.getJSONObject("MX");
records = mxname.getJSONArray(ManagedDNSServiceProxy.RECORD_FIELD);
ttl = mxname.getInt(ManagedDNSServiceProxy.TTL_FIELD);
} catch (JSONException e) {
// something is wrong with the JSON object, return null
e.printStackTrace();
return null;
}
NameResolution.getLogger().log(Level.FINE, "Get MX records list: {0}", records.toString());
// The records may contain multiple NS records
for (int i = 0; i < records.length(); i++) {
try {
JSONArray record = records.getJSONArray(i);
String pString = record.getString(0);
int priority = Integer.parseInt(pString);
String host = record.getString(1);
// the host must be an absolute name, i.e., ended with a dot
if (!host.endsWith(".")) {
host = host + ".";
}
MXRecord mxRecord = new MXRecord(new Name(nameToResolve), DClass.IN, ttl, priority, new Name(host));
mxList.put(mxRecord);
if (record.length() == 3) {
String address = record.getString(2);
ARecord mxARecord = new ARecord(new Name(host), DClass.IN, ttl, InetAddress.getByName(address));
aList.put(mxARecord);
} else {
// no IP address in the record for the mail server
}
} catch (JSONException | TextParseException | UnknownHostException e) {
// trash this record
e.printStackTrace();
}
}
}
try {
obj.put("MX", mxList);
obj.put("A", aList);
} catch (JSONException e) {
// return a null if JSON operation fails
return null;
}
return obj;
}
use of org.xbill.DNS.TextParseException in project GNS by MobilityFirst.
the class NameResolution method getNSRecordsFromNSField.
/**
* retrieve all NS records and the corresponding A records from NS field of a JSON object.
* the key "NS" contains a list of all NS records
* the key "A" contains a list of all A records, which must be put into ADDITIONAL section
*
*/
private static JSONObject getNSRecordsFromNSField(JSONObject fieldResponseJson, String nameToResolve) {
JSONObject obj = new JSONObject();
JSONArray aList = new JSONArray();
JSONArray nsList = new JSONArray();
/**
* Format of NS record in GNS:
* {
* "NS":
* {
* "record":[(ns1, addr1), (ns2, addr2), ...],
* "ttl":int
* }
* }
*
*/
if (fieldResponseJson.has("NS")) {
JSONArray records = null;
int ttl = 3600;
try {
JSONObject recordObj = fieldResponseJson.getJSONObject("NS");
records = recordObj.getJSONArray(ManagedDNSServiceProxy.RECORD_FIELD);
ttl = recordObj.getInt(ManagedDNSServiceProxy.TTL_FIELD);
} catch (JSONException e) {
// something is wrong with the JSON object, return null
e.printStackTrace();
return null;
}
// The records may contain multiple NS records
for (int i = 0; i < records.length(); i++) {
try {
JSONArray record = records.getJSONArray(i);
String ns = record.getString(0);
// It must be an absolute name, i.e., the string must be ended with a dot, e.g., example.com.
if (!ns.endsWith(".")) {
ns = ns + ".";
}
NSRecord nsRecord = new NSRecord(new Name(nameToResolve), DClass.IN, ttl, new Name(ns));
nsList.put(nsRecord);
// address can be null as the domain name might use other service as its name server
if (record.length() == 2) {
String address = record.getString(1);
ARecord nsARecord = new ARecord(new Name(ns), DClass.IN, ttl, InetAddress.getByName(address));
aList.put(nsARecord);
} else {
// no IP address in the record for the name server
}
} catch (JSONException | TextParseException | UnknownHostException e) {
// do nothing and trash this record
e.printStackTrace();
}
}
} else {
// No NS record, return null
}
try {
obj.put("NS", nsList);
obj.put("A", aList);
} catch (JSONException e) {
// return a null if JSON operation fails
return null;
}
return obj;
}
use of org.xbill.DNS.TextParseException in project ORCID-Source by ORCID.
the class OrcidDomainValidator method isValid.
public boolean isValid(String hostLocation) {
try {
Lookup lookup = new Lookup(hostLocation);
lookup.run();
int result = lookup.getResult();
if (Lookup.SUCCESSFUL == result) {
return true;
}
if (Lookup.HOST_NOT_FOUND == result || Lookup.TYPE_NOT_FOUND == result) {
return false;
}
LOGGER.warn("DNS is not OK, so validating in offline mode");
return standardDomainValidator.isValid(hostLocation);
} catch (TextParseException e) {
return false;
}
}
use of org.xbill.DNS.TextParseException in project opennms by OpenNMS.
the class DNSResolutionMonitor method poll.
@Override
public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) {
// Get the name to query for
final Name name;
try {
name = new Name(svc.getNodeLabel());
} catch (final TextParseException e) {
return PollStatus.unavailable("Invalid record name '" + svc.getNodeLabel() + "': " + e.getMessage());
}
// Determine if records for IPv4 and/or IPv6 re required
final String resolutionType = ParameterMap.getKeyedString(parameters, PARM_RESOLUTION_TYPE, PARM_RESOLUTION_TYPE_DEFAULT);
final boolean ipv4Required = PARM_RESOLUTION_TYPE_V4.equalsIgnoreCase(resolutionType) || PARM_RESOLUTION_TYPE_BOTH.equals(resolutionType);
final boolean ipv6Required = PARM_RESOLUTION_TYPE_V6.equalsIgnoreCase(resolutionType) || PARM_RESOLUTION_TYPE_BOTH.equals(resolutionType);
// Build a resolver object used for lookups
final String nameserver = ParameterMap.getKeyedString(parameters, PARM_NAMESERVER, null);
final Resolver resolver;
try {
if (nameserver == null) {
// Use system-defined resolvers
resolver = new ExtendedResolver();
} else {
resolver = new SimpleResolver(nameserver);
}
} catch (final UnknownHostException e) {
return PollStatus.unavailable("Unable to resolve nameserver '" + nameserver + "': " + e.getMessage());
}
// Start resolving the records
final long start = System.currentTimeMillis();
// Resolve the name
final boolean ipv4Found = resolve(name, resolver, Type.A);
final boolean ipv6Found = resolve(name, resolver, Type.AAAA);
// Resolving succeeded - checking results
final long end = System.currentTimeMillis();
// Check if result is valid
if (!ipv4Found && !ipv6Found) {
return PollStatus.unavailable("Unable to resolve host '" + name + "'");
} else if (ipv4Required && !ipv4Found) {
return PollStatus.unavailable("'" + name + "' could be resolved to an IPv6 address (AAAA record) but not an IPv4 address (A record)");
} else if (ipv6Required && !ipv6Found) {
return PollStatus.unavailable("'" + name + "' could be resolved to an IPv4 address (A record) but not an IPv6 address (AAAA record)");
} else {
return PollStatus.available((double) (end - start));
}
}
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<InetAddress>();
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<InetAddress>();
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<InetAddress>();
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;
}
Aggregations