Search in sources :

Example 1 with TextParseException

use of org.xbill.DNS.TextParseException in project Smack by igniterealtime.

the class DNSJavaResolver method lookupSRVRecords0.

@Override
protected List<SRVRecord> lookupSRVRecords0(String name, List<HostAddress> failedAddresses, DnssecMode dnssecMode) {
    List<SRVRecord> res = new ArrayList<SRVRecord>();
    Lookup lookup;
    try {
        lookup = new Lookup(name, Type.SRV);
    } catch (TextParseException e) {
        throw new IllegalStateException(e);
    }
    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;
}
Also used : ArrayList(java.util.ArrayList) Lookup(org.xbill.DNS.Lookup) SRVRecord(org.jivesoftware.smack.util.dns.SRVRecord) Record(org.xbill.DNS.Record) SRVRecord(org.jivesoftware.smack.util.dns.SRVRecord) InetAddress(java.net.InetAddress) TextParseException(org.xbill.DNS.TextParseException)

Example 2 with TextParseException

use of org.xbill.DNS.TextParseException in project GNS by MobilityFirst.

the class NameResolution method getARecordsFromAField.

/**
   * retrieve all A records from A field of a JSON object
   * 
   * @return
   */
private static JSONArray getARecordsFromAField(JSONObject fieldResponseJson, String nameToResolve) {
    JSONArray aList = new JSONArray();
    /**
       * Format of A record in GNS:
       * {
       * 	"A":
       * 		{
       * 			"record": String[ip1, ip2, ...],
       * 			"ttl": int
       * 		}
       * }
       */
    if (fieldResponseJson.has("A")) {
        JSONArray records = null;
        int ttl = 60;
        try {
            JSONObject recordObj = fieldResponseJson.getJSONObject("A");
            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 ip addresses
        for (int i = 0; i < records.length(); i++) {
            try {
                String ip = records.getString(i);
                ARecord gnsARecord = new ARecord(new Name(nameToResolve), DClass.IN, ttl, InetAddress.getByName(ip));
                aList.put(gnsARecord);
            } catch (JSONException | TextParseException | UnknownHostException e) {
                // do nothing, just trash this record
                e.printStackTrace();
            }
        }
    } else {
        // there is no A record in the original GNS record, so return null
        return null;
    }
    return aList;
}
Also used : ARecord(org.xbill.DNS.ARecord) JSONObject(org.json.JSONObject) UnknownHostException(java.net.UnknownHostException) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Name(org.xbill.DNS.Name) TextParseException(org.xbill.DNS.TextParseException)

Example 3 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 4 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 5 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)

Aggregations

TextParseException (org.xbill.DNS.TextParseException)21 Lookup (org.xbill.DNS.Lookup)12 ArrayList (java.util.ArrayList)10 Record (org.xbill.DNS.Record)10 ARecord (org.xbill.DNS.ARecord)9 UnknownHostException (java.net.UnknownHostException)7 SRVRecord (org.xbill.DNS.SRVRecord)7 Name (org.xbill.DNS.Name)5 InetAddress (java.net.InetAddress)4 IOException (java.io.IOException)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 SOARecord (org.xbill.DNS.SOARecord)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 X509Certificate (java.security.cert.X509Certificate)2