use of org.xbill.DNS.Name in project opennms by OpenNMS.
the class DNSServer method addPrimaryZone.
public void addPrimaryZone(final String zname, final String zonefile) throws IOException {
Name origin = null;
if (zname != null)
origin = Name.fromString(zname, Name.root);
final Zone newzone = new Zone(origin, zonefile);
m_znames.put(newzone.getOrigin(), newzone);
}
use of org.xbill.DNS.Name in project opennms by OpenNMS.
the class DNSServer method addAnswer.
byte addAnswer(final Message response, final Name name, int type, int dclass, int iterations, int flags) {
SetResponse sr;
byte rcode = Rcode.NOERROR;
if (iterations > 6)
return Rcode.NOERROR;
if (type == Type.SIG || type == Type.RRSIG) {
type = Type.ANY;
flags |= FLAG_SIGONLY;
}
final Zone zone = findBestZone(name);
if (zone != null)
sr = zone.findRecords(name, type);
else {
sr = getCache(dclass).lookupRecords(name, type, Credibility.NORMAL);
}
if (sr.isUnknown()) {
addCacheNS(response, getCache(dclass), name);
}
if (sr.isNXDOMAIN()) {
response.getHeader().setRcode(Rcode.NXDOMAIN);
if (zone != null) {
addSOA(response, zone);
if (iterations == 0)
response.getHeader().setFlag(Flags.AA);
}
rcode = Rcode.NXDOMAIN;
} else if (sr.isNXRRSET()) {
if (zone != null) {
addSOA(response, zone);
if (iterations == 0)
response.getHeader().setFlag(Flags.AA);
}
} else if (sr.isDelegation()) {
final RRset nsRecords = sr.getNS();
addRRset(nsRecords.getName(), response, nsRecords, Section.AUTHORITY, flags);
} else if (sr.isCNAME()) {
final CNAMERecord cname = sr.getCNAME();
addRRset(name, response, new RRset(cname), Section.ANSWER, flags);
if (zone != null && iterations == 0)
response.getHeader().setFlag(Flags.AA);
rcode = addAnswer(response, cname.getTarget(), type, dclass, iterations + 1, flags);
} else if (sr.isDNAME()) {
final DNAMERecord dname = sr.getDNAME();
RRset rrset = new RRset(dname);
addRRset(name, response, rrset, Section.ANSWER, flags);
final Name newname;
try {
newname = name.fromDNAME(dname);
} catch (final NameTooLongException e) {
return Rcode.YXDOMAIN;
}
rrset = new RRset(new CNAMERecord(name, dclass, 0, newname));
addRRset(name, response, rrset, Section.ANSWER, flags);
if (zone != null && iterations == 0)
response.getHeader().setFlag(Flags.AA);
rcode = addAnswer(response, newname, type, dclass, iterations + 1, flags);
} else if (sr.isSuccessful()) {
final RRset[] rrsets = sr.answers();
for (int i = 0; i < rrsets.length; i++) addRRset(name, response, rrsets[i], Section.ANSWER, flags);
if (zone != null) {
addNS(response, zone, flags);
if (iterations == 0)
response.getHeader().setFlag(Flags.AA);
} else
addCacheNS(response, getCache(dclass), name);
}
return rcode;
}
use of org.xbill.DNS.Name in project opennms by OpenNMS.
the class DnsRequisitionProvider method getRequisitionFor.
@Override
public Requisition getRequisitionFor(DnsRequisitionRequest request) {
ZoneTransferIn xfer = null;
List<Record> records = null;
LOG.debug("connecting to host {}:{}", request.getHost(), request.getPort());
try {
try {
xfer = ZoneTransferIn.newIXFR(new Name(request.getZone()), request.getSerial(), request.getFallback(), request.getHost(), request.getPort(), null);
records = getRecords(xfer);
} catch (ZoneTransferException e) {
// Fallback to AXFR
String message = "IXFR not supported trying AXFR: " + e;
LOG.warn(message, e);
xfer = ZoneTransferIn.newAXFR(new Name(request.getZone()), request.getHost(), null);
records = getRecords(xfer);
}
} catch (IOException | ZoneTransferException e) {
throw new RuntimeException(e);
}
if (records.size() > 0) {
// for now, set the foreign source to the specified dns zone
final Requisition r = new Requisition(request.getForeignSource());
for (Record rec : records) {
if (matchingRecord(request, rec)) {
r.insertNode(createRequisitionNode(request, rec));
}
}
return r;
}
return null;
}
use of org.xbill.DNS.Name in project GNS by MobilityFirst.
the class NameResolution method lookupGnsServer.
/**
* Lookup the query in the GNS server.
* @param addr
* @param query
* @param handler
* @return A message with either a good response or an error.
*/
public static Message lookupGnsServer(InetAddress addr, Message query, ClientRequestHandlerInterface handler) {
// 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 int fieldName = query.getQuestion().getType();
final Name requestedName = query.getQuestion().getName();
final byte[] rawName = requestedName.toWire();
final String domainName = querytoStringForGNS(rawName);
// The domain name must be an absolute name, i.e., ended with a dot
assert (domainName.endsWith(".")) : "The domain name " + domainName + "to resolve is not an absolute name!";
/**
* The query type or domain name can't be null, otherwise return an error message
*/
if (Type.string(fieldName) == null || domainName == null) {
return errorMessage(query, Rcode.NXDOMAIN);
}
NameResolution.getLogger().log(Level.FINE, "Trying GNS lookup for domain {0}, type {1}", new Object[] { domainName, Type.string(fieldName) });
/**
* Create a response message, build the header first.
* The response is constructed later after GNS query.
*/
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);
/**
* Request DNS fields of an alias and prepare a DNS response message
*/
ArrayList<String> fields = new ArrayList<>(Arrays.asList("A", "NS", "CNAME", "SOA", "PTR", "MX"));
/**
* <p>
* RFC 1034: the additional section "carries RRs(Resource Records) which may be helpful in
* using the RRs in the other section"
* RFC 2181: data you put in the additional section can never be promoted into real answers.
*
* <p>When a DNS client needs to look up a name used in a program, it queries DNS servers to resolve the name.
* Each query message the client sends contains three pieces of information, specifying a question for the server to answer:
* 1. A specified DNS domain name, stated as a fully qualified domain name (FQDN).
* 2. A specified query type, which can either specify a resource record (RR) by type or a specialized type of query operation.
* 3. A specified class for the DNS domain name. For DNS servers running the Windows operating system, this should always be specified as the Internet (IN) class.
*
* <p>The information is retrieved from GNS based on the queried domain.
* <p>The response is constructed based on the query type,
* 1. A: return A records in ANSWER section, NS records in AUTHORITY section, A records of name servers in ADDITIONAL section
* 2. NS: return NS records in ANSWER section, A records of name servers in ADDITIONAL section
* 3. MX: return MX records in ANSWER section, NS records in AUTHORITY section, A record of name servers in ADDITIONAL section
* 4. CNAME: return CNAME records in in ANSWER section, NS records in AUTHORITY section, A record of name servers in ADDITIONAL section
*
* Records in ADDITIONAL section is not required, we do a best-effort resolution for the names in ADDITIONAL section.
*/
long resolveStart = System.currentTimeMillis();
JSONObject fieldResponseJson = lookupGuidField(addr.getHostAddress().toString(), query.getHeader().getID(), domainName, null, fields, handler);
if (fieldResponseJson == null) {
NameResolution.getLogger().log(Level.FINE, "GNS lookup for domain {0} failed.", domainName);
return errorMessage(query, Rcode.NXDOMAIN);
}
NameResolution.getLogger().log(Level.FINE, "fieldResponse all fields (NS, MX, CNAME, A): {0}", fieldResponseJson.toString());
switch(fieldName) {
case Type.NS:
{
JSONObject obj = getNSRecordsFromNSField(fieldResponseJson, domainName);
if (obj != null) {
try {
JSONArray nsList = obj.getJSONArray("NS");
JSONArray aList = obj.getJSONArray("A");
for (int i = 0; i < nsList.length(); i++) {
response.addRecord((Record) nsList.get(i), Section.ANSWER);
}
for (int i = 0; i < aList.length(); i++) {
response.addRecord((Record) aList.get(i), Section.ADDITIONAL);
}
} catch (JSONException e) {
// do nothing, this happens only because some record is corrupted
}
} else {
// I don't have the requested A record, you must ask a wrong guy
return errorMessage(query, Rcode.NXDOMAIN);
}
}
break;
case Type.A:
{
// Get A records from retrieved GNS record
JSONArray aList = getARecordsFromAField(fieldResponseJson, domainName);
if (aList != null) {
for (int i = 0; i < aList.length(); i++) {
try {
response.addRecord((Record) aList.get(i), Section.ANSWER);
} catch (JSONException e) {
// trash the record
}
}
} else {
// I don't have the requested A record, you must ask a wrong guy
return errorMessage(query, Rcode.NXDOMAIN);
}
//Get NS record if we can
JSONObject obj = getNSRecordsFromNSField(fieldResponseJson, domainName);
if (obj != null) {
try {
JSONArray nsList = obj.getJSONArray("NS");
JSONArray aNSList = obj.getJSONArray("A");
for (int i = 0; i < nsList.length(); i++) {
response.addRecord((Record) nsList.get(i), Section.AUTHORITY);
}
for (int i = 0; i < aNSList.length(); i++) {
response.addRecord((Record) aNSList.get(i), Section.ADDITIONAL);
}
} catch (JSONException e) {
// do nothing, this happens only because some record is corrupted
}
}
}
break;
case Type.MX:
{
JSONObject obj = getMXRecordsFromMXField(fieldResponseJson, domainName);
NameResolution.getLogger().log(Level.FINE, "MX record for domain {0} is {1}", new Object[] { domainName, obj });
if (obj != null) {
try {
JSONArray mxList = obj.getJSONArray("MX");
JSONArray aList = obj.getJSONArray("A");
for (int i = 0; i < mxList.length(); i++) {
response.addRecord((Record) mxList.get(i), Section.ANSWER);
}
for (int i = 0; i < aList.length(); i++) {
response.addRecord((Record) aList.get(i), Section.ADDITIONAL);
}
} catch (JSONException e) {
// do nothing, this happens only because some record is corrupted
}
} else {
// I don't have the requested MX record, you must ask a wrong guy
return errorMessage(query, Rcode.NXDOMAIN);
}
//Get NS record if we can
obj = getNSRecordsFromNSField(fieldResponseJson, domainName);
if (obj != null) {
try {
JSONArray nsList = obj.getJSONArray("NS");
JSONArray aNSList = obj.getJSONArray("A");
for (int i = 0; i < nsList.length(); i++) {
response.addRecord((Record) nsList.get(i), Section.AUTHORITY);
}
for (int i = 0; i < aNSList.length(); i++) {
response.addRecord((Record) aNSList.get(i), Section.ADDITIONAL);
}
} catch (JSONException e) {
// do nothing, this happens only because some record is corrupted
}
}
}
break;
case Type.CNAME:
{
if (fieldResponseJson.has("CNAME")) {
// get CNAME alias, no need to resolve it to an IP address
try {
String cname = fieldResponseJson.getString("CNAME");
// The cname must be an absolute name, i.e., ended with a dot
if (!cname.endsWith(".")) {
cname = cname + ".";
}
CNAMERecord cnameRecord = new CNAMERecord(new Name(domainName), DClass.IN, 60, new Name(cname));
response.addRecord(cnameRecord, Section.ANSWER);
} catch (JSONException | TextParseException e) {
}
} else {
// I don't have the requested CNAME record, you must ask a wrong guy
return errorMessage(query, Rcode.NXDOMAIN);
}
}
break;
default:
// we haven't implemented yet
return errorMessage(query, Rcode.NOTIMPL);
}
DelayProfiler.updateDelay("ResolveName", resolveStart);
NameResolution.getLogger().log(Level.FINER, "Outgoing response from GNS: {0}", response.toString());
return response;
}
use of org.xbill.DNS.Name in project nhin-d by DirectProject.
the class LDAPPublicCertUtil_createLDAPUrl_Test method testCreateLDAPUrl_singleSRVRecord.
public void testCreateLDAPUrl_singleSRVRecord() throws Exception {
LdapPublicCertUtilImpl impl = new LdapPublicCertUtilImpl();
SRVRecord rec = new SRVRecord(new Name("test.com."), DClass.IN, 3600, 0, 1, 339, new Name("ldap.test.com."));
String url = impl.createLDAPUrl(new Record[] { rec });
String[] urls = url.split(" ");
assertEquals(1, urls.length);
assertTrue(urls[0].startsWith("ldap://ldap.test.com"));
}
Aggregations