Search in sources :

Example 1 with ARecord

use of org.xbill.DNS.ARecord in project nhin-d by DirectProject.

the class DNSRecordCommands_removeRecords_Test method testRemoveOnlyARecords_AssertOnlyARecordRemoved.

public void testRemoveOnlyARecords_AssertOnlyARecordRemoved() throws Exception {
    new TestPlan() {

        private List<Record> recordsToAdd;

        private List<Record> expectedRemainingRecords;

        @Override
        protected List<Record> getRecordsToAdd() throws Exception {
            recordsToAdd = new ArrayList<Record>();
            recordsToAdd.add(new ARecord(Name.fromString("example.domain.com."), DClass.IN, 3600, InetAddress.getByName("127.0.0.1")));
            recordsToAdd.add(new ARecord(Name.fromString("example.domain.com."), DClass.IN, 3600, InetAddress.getByName("127.0.0.2")));
            recordsToAdd.add(new MXRecord(Name.fromString("example.domain.com."), DClass.IN, 3600, 1, Name.fromString("mail1.example.domain.com.")));
            recordsToAdd.add(new MXRecord(Name.fromString("example2.domain.com."), DClass.IN, 3600, 1, Name.fromString("mail2.exampl2.domain.com.")));
            recordsToAdd.add(new SOARecord(Name.fromString("example.domain.com."), DClass.IN, 3600, Name.fromString("ns1.example.domain.com."), Name.fromString("gm2552.example.domain.com."), 1, 3600, 60, 60, 3600));
            return recordsToAdd;
        }

        @Override
        protected List<DnsRecord> getRecordsToRemove(List<DnsRecord> addedRecords) throws Exception {
            List<DnsRecord> retVal = new ArrayList<DnsRecord>();
            expectedRemainingRecords = new ArrayList<Record>();
            for (DnsRecord addedRecord : addedRecords) {
                if (addedRecord.getType() == Type.A)
                    retVal.add(addedRecord);
                else
                    expectedRemainingRecords.add(toRecord(addedRecord));
            }
            return retVal;
        }

        @Override
        protected void doAssertions(List<Record> remainingRecords) throws Exception {
            assertNotNull(remainingRecords);
            assertEquals(3, remainingRecords.size());
            for (Record expectedRemaining : expectedRemainingRecords) {
                int index = remainingRecords.indexOf(expectedRemaining);
                assertTrue(index >= 0);
                Record checkRecord = remainingRecords.get(index);
                assertTrue(checkRecord.getType() != Type.A);
                assertEquals(checkRecord, expectedRemaining);
            }
        }
    }.perform();
}
Also used : BaseTestPlan(org.nhindirect.dns.util.BaseTestPlan) ArrayList(java.util.ArrayList) SOARecord(org.xbill.DNS.SOARecord) ARecord(org.xbill.DNS.ARecord) MXRecord(org.xbill.DNS.MXRecord) DnsRecord(org.nhind.config.DnsRecord) SOARecord(org.xbill.DNS.SOARecord) ARecord(org.xbill.DNS.ARecord) MXRecord(org.xbill.DNS.MXRecord) Record(org.xbill.DNS.Record) ArrayList(java.util.ArrayList) List(java.util.List) SOARecord(org.xbill.DNS.SOARecord) DnsRecord(org.nhind.config.DnsRecord)

Example 2 with ARecord

use of org.xbill.DNS.ARecord 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 ARecord

use of org.xbill.DNS.ARecord 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 ARecord

use of org.xbill.DNS.ARecord 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 ARecord

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

the class NameResolution method queryAndResponseToString.

/**
   * Builds a pretty string showing the query and response.
   * Used for debugging purposes.
   *
   * @param query
   * @param response
   * @return a string
   */
public static String queryAndResponseToString(Message query, Message response) {
    StringBuilder result = new StringBuilder();
    result.append(query.getQuestion().getName());
    result.append(", type: ");
    result.append(Type.string(query.getQuestion().getType()));
    result.append(" -> ");
    if (response.getHeader().getRcode() == Rcode.NOERROR) {
        Record[] records = response.getSectionArray(Section.ANSWER);
        if (records.length > 0) {
            Record record = records[0];
            if (record instanceof ARecord) {
                ARecord aRecord = (ARecord) record;
                result.append(aRecord.getAddress().getHostAddress());
            } else {
                result.append("<NOT AN A RECORD>");
            }
        } else {
            result.append("<NO ANSWER SECTIONS>");
        }
    } else {
        result.append(Rcode.string(response.getHeader().getRcode()));
    }
    return result.toString();
}
Also used : ARecord(org.xbill.DNS.ARecord) CNAMERecord(org.xbill.DNS.CNAMERecord) ARecord(org.xbill.DNS.ARecord) Record(org.xbill.DNS.Record) NSRecord(org.xbill.DNS.NSRecord) MXRecord(org.xbill.DNS.MXRecord)

Aggregations

ARecord (org.xbill.DNS.ARecord)14 TextParseException (org.xbill.DNS.TextParseException)8 MXRecord (org.xbill.DNS.MXRecord)7 Record (org.xbill.DNS.Record)7 UnknownHostException (java.net.UnknownHostException)6 ArrayList (java.util.ArrayList)5 AAAARecord (org.xbill.DNS.AAAARecord)5 NSRecord (org.xbill.DNS.NSRecord)5 SOARecord (org.xbill.DNS.SOARecord)5 CNAMERecord (org.xbill.DNS.CNAMERecord)4 Lookup (org.xbill.DNS.Lookup)4 Name (org.xbill.DNS.Name)4 SRVRecord (org.xbill.DNS.SRVRecord)4 InetAddress (java.net.InetAddress)3 List (java.util.List)3 JSONArray (org.json.JSONArray)3 JSONException (org.json.JSONException)3 JSONObject (org.json.JSONObject)3 IOException (java.io.IOException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2