TEST_F(DNSTest, NoRecords) { DNS dns; EXPECT_TRUE(dns.queries().empty()); EXPECT_TRUE(dns.answers().empty()); EXPECT_TRUE(dns.authority().empty()); EXPECT_TRUE(dns.additional().empty()); }
bool callback(const PDU &pdu) { // The packet probably looks like this: // // EthernetII / IP / UDP / RawPDU // // So we retrieve the RawPDU layer, and construct a // DNS PDU using its contents. DNS dns = pdu.rfind_pdu<RawPDU>().to<DNS>(); // Retrieve the queries and print the domain name: for(const auto &query : dns.queries()) std::cout << query.dname() << std::endl; return true; }
bool callback(const PDU &pdu) { // The packet probably looks like this: // // EthernetII / IP / UDP / RawPDU // // So we retrieve each layer, and construct a // DNS PDU from the RawPDU layer contents. EthernetII eth = pdu.rfind_pdu<EthernetII>(); IP ip = eth.rfind_pdu<IP>(); UDP udp = ip.rfind_pdu<UDP>(); DNS dns = udp.rfind_pdu<RawPDU>().to<DNS>(); // Is it a DNS query? if(dns.type() == DNS::QUERY) { // Let's see if there's any query for an "A" record. for(const auto &query : dns.queries()) { if(query.type() == DNS::A) { // Here's one! Let's add an answer. dns.add_answer( DNS::Resource( query.dname(), "127.0.0.1", DNS::A, query.query_class(), // 777 is just a random TTL 777 ) ); } } // Have we added some answers? if(dns.answers_count() > 0) { // It's a response now dns.type(DNS::RESPONSE); // Recursion is available(just in case) dns.recursion_available(1); // Build our packet auto pkt = EthernetII(eth.src_addr(), eth.dst_addr()) / IP(ip.src_addr(), ip.dst_addr()) / UDP(udp.sport(), udp.dport()) / dns; // Send it! sender.send(pkt); } } return true; }
TEST_F(DNSTest, Question) { DNS dns; dns.add_query(DNS::Query("www.example.com", DNS::A, DNS::IN)); dns.add_query(DNS::Query("www.example2.com", DNS::MX, DNS::IN)); ASSERT_EQ(dns.questions_count(), 2); DNS::queries_type queries(dns.queries()); for(DNS::queries_type::const_iterator it = queries.begin(); it != queries.end(); ++it) { EXPECT_TRUE(it->dname() == "www.example.com" || it->dname() == "www.example2.com"); if(it->dname() == "www.example.com") { EXPECT_EQ(it->type(), DNS::A); EXPECT_EQ(it->query_class(), DNS::IN); } else if(it->dname() == "www.example2.com") { EXPECT_EQ(it->type(), DNS::MX); EXPECT_EQ(it->query_class(), DNS::IN); } } }