use of org.xbill.DNS.Name 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;
}
use of org.xbill.DNS.Name in project GNS by MobilityFirst.
the class NameResolution method lookupDnsCache.
/**
* Look up the local dns server cache.
* Returns a {@link Message}.
*
* @param query
* @param dnsCache
* @return a Message
*/
public static Message lookupDnsCache(Message query, Cache dnsCache) {
// check for queries we can't handle
int type = query.getQuestion().getType();
// Was the query legitimate or implemented?
if (!Type.isRR(type) && type != Type.ANY) {
return errorMessage(query, Rcode.NOTIMP);
}
// extract the domain (guid) and field from the query
final Name requestedName = query.getQuestion().getName();
final byte[] rawName = requestedName.toWire();
final String lookupName = querytoStringForGNS(rawName);
NameResolution.getLogger().log(Level.FINER, "Looking up name in cache: {0}", lookupName);
SetResponse lookupresult = dnsCache.lookupRecords(requestedName, Type.ANY, Credibility.NORMAL);
if (lookupresult.isSuccessful()) {
Message response = new Message(query.getHeader().getID());
response.getHeader().setFlag(Flags.QR);
if (query.getHeader().getFlag(Flags.RD)) {
response.getHeader().setFlag(Flags.RA);
}
response.addRecord(query.getQuestion(), Section.QUESTION);
response.getHeader().setFlag(Flags.AA);
ArrayList<Name> cnameNames = new ArrayList<>();
// Write the response
for (RRset rrset : lookupresult.answers()) {
NameResolution.getLogger().log(Level.FINE, "{0}\n", rrset.toString());
Iterator<?> rrItr = rrset.rrs();
while (rrItr.hasNext()) {
Record curRecord = (Record) rrItr.next();
response.addRecord(curRecord, Section.ANSWER);
if (curRecord.getType() == Type.CNAME) {
cnameNames.add(((CNAMERecord) curRecord).getAlias());
}
}
}
if (cnameNames.isEmpty()) {
return response;
}
// For all CNAMES in the response, add their A records
for (Name cname : cnameNames) {
NameResolution.getLogger().log(Level.FINE, "Looking up CNAME in cache: {0}", cname.toString());
SetResponse lookUpResult = dnsCache.lookupRecords(cname, Type.ANY, Credibility.NORMAL);
if (lookUpResult.isSuccessful()) {
for (RRset rrset : lookUpResult.answers()) {
NameResolution.getLogger().log(Level.FINE, "{0}\n", rrset.toString());
Iterator<?> rrItr = rrset.rrs();
while (rrItr.hasNext()) {
Record curRecord = (Record) rrItr.next();
response.addRecord(curRecord, Section.ANSWER);
}
}
}
}
return response;
} else {
return errorMessage(query, Rcode.NOTIMP);
}
}
use of org.xbill.DNS.Name 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.Name 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.Name in project opennms by OpenNMS.
the class DNSServer method addTSIG.
public void addTSIG(final String algstr, final String namestr, final String key) throws IOException {
final Name name = Name.fromString(namestr, Name.root);
m_TSIGs.put(name, new TSIG(algstr, namestr, key));
}
Aggregations