Search in sources :

Example 6 with TextParseException

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;
}
Also used : ARecord(org.xbill.DNS.ARecord) JSONObject(org.json.JSONObject) UnknownHostException(java.net.UnknownHostException) MXRecord(org.xbill.DNS.MXRecord) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Name(org.xbill.DNS.Name) TextParseException(org.xbill.DNS.TextParseException)

Example 7 with TextParseException

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;
}
Also used : ARecord(org.xbill.DNS.ARecord) JSONObject(org.json.JSONObject) UnknownHostException(java.net.UnknownHostException) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) NSRecord(org.xbill.DNS.NSRecord) Name(org.xbill.DNS.Name) TextParseException(org.xbill.DNS.TextParseException)

Example 8 with TextParseException

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;
    }
}
Also used : Lookup(org.xbill.DNS.Lookup) TextParseException(org.xbill.DNS.TextParseException)

Example 9 with TextParseException

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));
    }
}
Also used : ExtendedResolver(org.xbill.DNS.ExtendedResolver) SimpleResolver(org.xbill.DNS.SimpleResolver) ExtendedResolver(org.xbill.DNS.ExtendedResolver) Resolver(org.xbill.DNS.Resolver) UnknownHostException(java.net.UnknownHostException) SimpleResolver(org.xbill.DNS.SimpleResolver) Name(org.xbill.DNS.Name) TextParseException(org.xbill.DNS.TextParseException)

Example 10 with TextParseException

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;
}
Also used : ARecord(org.xbill.DNS.ARecord) AAAARecord(org.xbill.DNS.AAAARecord) Inet4Address(java.net.Inet4Address) AAAARecord(org.xbill.DNS.AAAARecord) UnknownHostException(java.net.UnknownHostException) ArrayList(java.util.ArrayList) Lookup(org.xbill.DNS.Lookup) ARecord(org.xbill.DNS.ARecord) AAAARecord(org.xbill.DNS.AAAARecord) Record(org.xbill.DNS.Record) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress) TextParseException(org.xbill.DNS.TextParseException)

Aggregations

TextParseException (org.xbill.DNS.TextParseException)12 ARecord (org.xbill.DNS.ARecord)7 UnknownHostException (java.net.UnknownHostException)6 ArrayList (java.util.ArrayList)5 Name (org.xbill.DNS.Name)5 Lookup (org.xbill.DNS.Lookup)4 InetAddress (java.net.InetAddress)3 JSONArray (org.json.JSONArray)3 JSONException (org.json.JSONException)3 JSONObject (org.json.JSONObject)3 DNSEntryForm (org.nhindirect.config.ui.form.DNSEntryForm)3 AAAARecord (org.xbill.DNS.AAAARecord)3 MXRecord (org.xbill.DNS.MXRecord)3 NSRecord (org.xbill.DNS.NSRecord)3 Record (org.xbill.DNS.Record)3 SOARecord (org.xbill.DNS.SOARecord)3 IOException (java.io.IOException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 X509Certificate (java.security.cert.X509Certificate)2 CertificateEncodingException (javax.security.cert.CertificateEncodingException)2