use of org.xbill.DNS.Name in project camel by apache.
the class DnsWikipediaProducer method process.
@Override
public void process(Exchange exchange) throws Exception {
SimpleResolver resolver = new SimpleResolver();
int type = Type.TXT;
Name name = Name.fromString(String.valueOf(exchange.getIn().getHeader(DnsConstants.TERM)) + ".wp.dg.cx", Name.root);
Record rec = Record.newRecord(name, type, DClass.IN);
Message query = Message.newQuery(rec);
Message response = resolver.send(query);
Record[] records = response.getSectionArray(Section.ANSWER);
if (records.length > 0) {
exchange.getIn().setBody(records[0].rdataToString());
} else {
exchange.getIn().setBody(null);
}
}
use of org.xbill.DNS.Name in project camel by apache.
the class DnsRecordConverter method toRecord.
/**
* @param ip, like "192.168.1.1"
* @return the complete DNS record for that IP.
*/
@Converter
public static Record toRecord(String ip) throws IOException {
Resolver res = new ExtendedResolver();
Name name = ReverseMap.fromAddress(ip);
int type = Type.PTR;
int dclass = DClass.IN;
Record rec = Record.newRecord(name, type, dclass);
Message query = Message.newQuery(rec);
Message response = res.send(query);
Record[] answers = response.getSectionArray(Section.ANSWER);
if (answers.length == 0) {
return null;
} else {
return answers[0];
}
}
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 opennms by OpenNMS.
the class JUnitDNSServerExecutionListener method beforeTestMethod.
/** {@inheritDoc} */
@Override
public void beforeTestMethod(final TestContext testContext) throws Exception {
super.beforeTestMethod(testContext);
final JUnitDNSServer config = findTestAnnotation(JUnitDNSServer.class, testContext);
if (config == null) {
return;
}
LOG.info("initializing DNS on port {}", config.port());
m_server = new DNSServer();
m_server.addPort(config.port());
for (final DNSZone dnsZone : config.zones()) {
String name = dnsZone.name();
if (!name.endsWith(".")) {
name = name + ".";
}
final Name zoneName = Name.fromString(name, Name.root);
LOG.debug("zoneName = {}", zoneName);
final Zone zone = new Zone(zoneName, new Record[] { new SOARecord(zoneName, DClass.IN, DEFAULT_TTL, zoneName, Name.fromString("admin." + name), 1, DEFAULT_TTL, DEFAULT_TTL, DEFAULT_TTL, DEFAULT_TTL), new NSRecord(zoneName, DClass.IN, DEFAULT_TTL, Name.fromString("resolver1.opendns.com.")), new NSRecord(zoneName, DClass.IN, DEFAULT_TTL, Name.fromString("resolver2.opendns.com.")), new ARecord(zoneName, DClass.IN, DEFAULT_TTL, InetAddressUtils.addr(dnsZone.v4address())), new AAAARecord(zoneName, DClass.IN, DEFAULT_TTL, InetAddressUtils.addr(dnsZone.v6address())) });
LOG.debug("zone = {}", zone);
for (final DNSEntry entry : dnsZone.entries()) {
LOG.debug("adding entry: {}", entry);
String hostname = entry.hostname();
final Name recordName = Name.fromString(hostname, zoneName);
LOG.debug("name = {}", recordName);
if (entry.ipv6()) {
zone.addRecord(new AAAARecord(recordName, DClass.IN, DEFAULT_TTL, InetAddressUtils.addr(entry.address())));
} else {
zone.addRecord(new ARecord(recordName, DClass.IN, DEFAULT_TTL, InetAddressUtils.addr(entry.address())));
}
}
m_server.addZone(zone);
}
LOG.debug("starting DNS server");
m_server.start();
}
use of org.xbill.DNS.Name in project opennms by OpenNMS.
the class DNSServer method addAdditional2.
private void addAdditional2(final Message response, final int section, final int flags) {
final Record[] records = response.getSectionArray(section);
for (int i = 0; i < records.length; i++) {
final Record r = records[i];
final Name glueName = r.getAdditionalName();
if (glueName != null)
addGlue(response, glueName, flags);
}
}
Aggregations