|
From: Periya.Data <per...@gm...> - 2011-09-05 20:42:40
|
Hi,
I have a DNS packet and I want to retrieve the entries in the query
section, answer section, name server resource records and additional records
sections. I see several approaches in the DNS Message library. I have not
yet attempted to run this code. Currently building the templates carefully.
Here is what I am doing and would like to know if there are better/elegant
approaches:
// dns_msg.getQuestion() returns the first record in the Q section.
Record[] dns_query_record_array =
dns_msg.getSectionArray(Section.QUESTION);
// dns_query_record_array.length must be equal to qdcount
if (dns_query_record_array.length == 0) {
// do something.
} else {
// do something...maybe print to verify
int i = 0;
while(i < dns_query_record_array.length) {
//
Record dns_query_rec = dns_query_record_array[i];
System.out.println("Resource Record: " +
dns_query_rec.rdataToString());
if (dns_query_rec != null){
Name qname = dns_query_rec.getName();
String qname_s = qname.toString();
int qtype = dns_query_rec.getType();
int qclass = dns_query_rec.getDClass();
}
i++;
}
}
// Will the above get me all the entries in the question section? Or
should I do something like below..?????
/*
Record queryRecord = dns_msg.getQuestion();
for (int q=0; q < qdcount; q++){
if (queryRecord != null){
qname = queryRecord.getName();
qname_s = qname.toString();
qtype = queryRecord.getType();
qclass = queryRecord.getDClass();
} else {
qname_s = "none";
}
}
*/
// DNS Answers
// Note: there will be ANCOUNT number of resource records. You have
to loop to
// get all the entries of the answer section.
char rdlength;
char rdata;
Record[] dns_answer_record_array =
dns_msg.getSectionArray(Section.ANSWER);
if (dns_answer_record_array.length == 0) {
// do something.
} else {
// do something...maybe print to verify
int i = 0;
while(i < dns_answer_record_array.length) {
Record dns_answer_rec = dns_answer_record_array[i];
System.out.println("Resource Record: " +
dns_answer_rec.rdataToString());
int type = dns_answer_rec.getType();
Name name = dns_answer_rec.getName();
int rdata_class = dns_answer_rec.getDClass();
long ttl = dns_answer_rec.getTTL();
i++;
}
}
// Name server resource records
// Note: There will be NSCOUNT number of name server rr in the
authority
// records section.
Record[] dns_nameserver_rr_array =
dns_msg.getSectionArray(Section.AUTHORITY);
if (dns_nameserver_rr_array.length == 0) {
// do something.
} else {
// do something...maybe print to verify
int i = 0;
while(i < dns_nameserver_rr_array.length) {
Record dns_nameserver_rr = dns_nameserver_rr_array[i];
System.out.println("Authority Record: " +
dns_nameserver_rr.rdataToString());
i++;
}
}
// Additional records section
// Note: There will be ARCOUNT number of resource records in the
additional
// records section.
Record[] dns_additional_record_array =
dns_msg.getSectionArray(Section.ADDITIONAL);
if (dns_additional_record_array.length == 0) {
// do something.
} else {
// do something...maybe print to verify
int i = 0;
while(i < dns_additional_record_array.length) {
Record dns_additional_rec = dns_additional_record_array[i];
System.out.println("Additional Record: " +
dns_additional_rec.rdataToString());
i++;
}
}
I have seen the RRSet and it has a nice iterator also. But, I think, a
simple record array is enough for me.
If there is a nice example that shows how to parse a DNS packet completely,
it would be useful.
Suggestions on improvements are greatly appreciated.
Thanks,
PD
|