void setup() { ebox_init(); uart1.begin(115200); ret = sizeof(long); ret = sizeof(int); ret = sizeof(short int); ret = sizeof(short); w5500.begin(2,mac,ip,sub,gw,dns); attach_eth_to_socket(&w5500); w5500.getMAC (buf); uart1.printf("mac : %02x.%02x.%02x.%02x.%02x.%02x\r\n", buf[0],buf[1],buf[2],buf[3],buf[4],buf[5]); w5500.getIP (buf); uart1.printf("IP : %d.%d.%d.%d\r\n", buf[0],buf[1],buf[2],buf[3]); w5500.getSubnet(buf); uart1.printf("mask : %d.%d.%d.%d\r\n", buf[0],buf[1],buf[2],buf[3]); w5500.getGateway(buf); uart1.printf("GW : %d.%d.%d.%d\r\n", buf[0],buf[1],buf[2],buf[3]); uart1.printf("Network is ready.\r\n"); ddns.begin(SOCKET1,3000); ret = ddns.query(name); if(ret == DNS_RET_SUCCESS) /*发送DNS请求*/ uart1.printf("Get [%s]'s IP address [%d.%d.%d.%d] from %d.%d.%d.%d\r\n",name,ddns.domain_ip[0],ddns.domain_ip[1],ddns.domain_ip[2],ddns.domain_ip[3],dns[0],dns[1],dns[2],dns[3]); else if(ret == DNS_RET_FAIL) uart1.printf("获取超时\r\n"); else uart1.printf("未知错误.\r\n"); }
TEST_F(DNSTest, Answers) { DNS dns; dns.add_answer( DNS::Resource("www.example.com", "127.0.0.1", DNS::A, DNS::IN, 0x762) ); dns.add_answer( DNS::Resource("www.example2.com", "mail.example.com", DNS::MX, DNS::IN, 0x762) ); ASSERT_EQ(dns.answers_count(), 2); DNS::resources_type resources = dns.answers(); for(DNS::resources_type::const_iterator it = resources.begin(); it != resources.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->ttl(), 0x762U); EXPECT_EQ(it->data(), "127.0.0.1"); EXPECT_EQ(it->query_class(), DNS::IN); } else if(it->dname() == "www.example2.com") { EXPECT_EQ(it->type(), DNS::MX); EXPECT_EQ(it->ttl(), 0x762U); EXPECT_EQ(it->data(), "mail.example.com"); EXPECT_EQ(it->query_class(), DNS::IN); } } }
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()); }
DWORD WINAPI batch_handler(void* param) { DNS dns; queue<string>* questions; queue<string>* answers; parameter* p = (parameter*)param; CRITICAL_SECTION* cs = p->cs; questions = p->questions; answers = p->answers; string question; string answer; while(1) { EnterCriticalSection(cs); c++; if(c>100000) { LeaveCriticalSection(cs); return 0; } question = questions->front(); questions->pop(); LeaveCriticalSection(cs); /* EnterCriticalSection(&statistic_lock); stat.total_queries++; LeaveCriticalSection(&statistic_lock); unsigned long start = GetTickCount(); if((gethostbyname(question.c_str())) != NULL) { //printf("Get host by name with error = %d\n",WSAGetLastError()); EnterCriticalSection(&statistic_lock); stat.sucess++; LeaveCriticalSection(&statistic_lock); } unsigned long end = GetTickCount(); EnterCriticalSection(&statistic_lock); stat.delays.push_back(end - start); LeaveCriticalSection(&statistic_lock); */ unsigned long start = GetTickCount(); answer = dns.resolve(question); unsigned long end = GetTickCount(); EnterCriticalSection(&statistic_lock); stat.delays.push_back(end - start); LeaveCriticalSection(&statistic_lock); EnterCriticalSection(cs); if(answer != "") answers->push(answer); LeaveCriticalSection(cs); } return 0; }
TEST_F(DNSTest, AnswersWithSameName) { DNS dns; dns.add_answer("www.example.com", DNS::make_info(DNS::A, DNS::IN, 0x762), IPv4Address("127.0.0.1")); dns.add_answer("www.example.com", DNS::make_info(DNS::A, DNS::IN, 0x762), IPv4Address("127.0.0.2")); ASSERT_EQ(dns.answers_count(), 2); DNS::resources_type resources = dns.answers(); for(DNS::resources_type::const_iterator it = resources.begin(); it != resources.end(); ++it) { EXPECT_TRUE(it->data() == "127.0.0.1" || it->data() == "127.0.0.2"); EXPECT_EQ(it->dname(), "www.example.com"); EXPECT_EQ(it->type(), DNS::A); EXPECT_EQ(it->ttl(), 0x762U); EXPECT_EQ(it->query_class(), DNS::IN); } }
void CGUL::Network::HTTPRequest::Https(const String& url) { host = url; DNS dns; Vector< Network::IPAddress > lookup; dns.Lookup(url, &lookup); if (lookup.size() == 0) { return; } sock->ConnectSSL(IPAddress(lookup[0]), 443); }
TEST_F(DNSTest, AnswersV6) { DNS dns; dns.add_answer("www.example.com", DNS::make_info(DNS::AAAA, DNS::IN, 0x762), IPv6Address("f9a8:239::1:1")); dns.add_answer("www.example.com", DNS::make_info(DNS::AAAA, DNS::IN, 0x762), IPv6Address("f9a8:239::1:1")); ASSERT_EQ(dns.answers_count(), 2); DNS::resources_type resources = dns.answers(); for(DNS::resources_type::const_iterator it = resources.begin(); it != resources.end(); ++it) { EXPECT_EQ(it->dname(), "www.example.com"); EXPECT_EQ(it->type(), DNS::AAAA); EXPECT_EQ(it->ttl(), 0x762U); EXPECT_EQ(it->data(), "f9a8:239::1:1"); EXPECT_EQ(it->query_class(), DNS::IN); } }
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; }
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); } } }
TEST_F(DNSTest, Additional) { DNS dns; const char *domain = "carlos.example.com"; dns.add_additional( DNS::Resource("www.example.com", domain, DNS::CNAME, DNS::IN, 0x762) ); dns.add_additional( DNS::Resource("www.example.com", domain, DNS::CNAME, DNS::IN, 0x762) ); ASSERT_EQ(dns.additional_count(), 2); DNS::resources_type resources = dns.additional(); for(DNS::resources_type::const_iterator it = resources.begin(); it != resources.end(); ++it) { EXPECT_EQ("www.example.com", it->dname()); EXPECT_EQ(it->type(), DNS::CNAME); EXPECT_EQ(it->ttl(), 0x762U); EXPECT_EQ(it->data(), domain); EXPECT_EQ(it->query_class(), DNS::IN); } }
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, Truncated) { DNS dns; dns.truncated(1); EXPECT_EQ(dns.truncated(), 1); }
void DNSTest::test_equals(const DNS &dns1, const DNS &dns2) { EXPECT_EQ(dns1.id(), dns2.id()); EXPECT_EQ(dns1.type(), dns2.type()); EXPECT_EQ(dns1.opcode(), dns2.opcode()); EXPECT_EQ(dns1.authoritative_answer(), dns2.authoritative_answer()); EXPECT_EQ(dns1.truncated(), dns2.truncated()); EXPECT_EQ(dns1.recursion_desired(), dns2.recursion_desired()); EXPECT_EQ(dns1.recursion_available(), dns2.recursion_available()); EXPECT_EQ(dns1.z(), dns2.z()); EXPECT_EQ(dns1.authenticated_data(), dns2.authenticated_data()); EXPECT_EQ(dns1.checking_disabled(), dns2.checking_disabled()); EXPECT_EQ(dns1.rcode(), dns2.rcode()); EXPECT_EQ(dns1.questions_count(), dns2.questions_count()); EXPECT_EQ(dns1.answers_count(), dns2.answers_count()); EXPECT_EQ(dns1.authority_count(), dns2.authority_count()); EXPECT_EQ(dns1.additional_count(), dns2.additional_count()); EXPECT_EQ(dns1.pdu_type(), dns2.pdu_type()); EXPECT_EQ(dns1.header_size(), dns2.header_size()); EXPECT_EQ(bool(dns1.inner_pdu()), bool(dns2.inner_pdu())); }
int HTTPDNSCorrelator<ReaderType, WriterType, ResponseWriterType>::run() { IPv4Network network; DNS dns; SearchDNSResult dnsResult; SearchDNSResponseResult dnsResponseResult; bool moreToCorrelate(true); // prefetch the first DNS ErrorStatus errorStatus; if ((errorStatus = _reader->read(dns)) != E_SUCCESS) { if (errorStatus != E_EOF) { _error = true; _errorMsg.assign("Reader: error reading "); return 1; } // no more http data to read. we're done. moreToCorrelate = false; } std::deque <DNS> _q; TimeStamp windowSize(300, 0); // 5 minutes uint32_t tmpRawSourceIP, tmpRawDestinationIP; // for each SearchNeoflowResult for (std::set<SearchHTTPResult>::const_iterator i(_set->begin()); i != _set->end(); ++i) { // fill up the 5 minute window while (!_q.empty() && _q.front().responseTime() < i->time() - windowSize) { _q.pop_front(); } while (moreToCorrelate && dns.responseTime() <= i->time()) { if (dns.responseTime() >= i->time() - windowSize) { // in range. push it into the deque _q.push_back(dns); } if ((errorStatus = _reader->read(dns)) != E_SUCCESS) { if (errorStatus != E_EOF) { _error = true; _errorMsg.assign("Reader: error reading"); return 1; } // no more http data to read. we're done. moreToCorrelate = false; } } // if there are HTTPs to look at, make flow matcher based on i // FIXME this should make a matcher that matches DNS flows // where the client is the INTERNAL address of the flow // for now we just match either, as this will probably suffice /* IPv4FlowMatcher matcher, reverseMatcher; if (!_q.empty()) { network.rawSet(i->rawSourceIP(), 0xffffffff); matcher.addDestinationNetwork(network); network.rawSet(i->rawDestinationIP(), 0xffffffff); reverseMatcher.addDestinationNetwork(network); } */ if (i->request().time() != TimeStamp()) { tmpRawSourceIP = i->request().raw_source_ip(); tmpRawDestinationIP = i->request().raw_destination_ip(); } else { tmpRawSourceIP = i->response().raw_source_ip(); tmpRawDestinationIP = i->response().raw_destination_ip(); } // look for the correlated DNSs. bool foundMatch(false); DNS *serverDNS(NULL); for (std::deque <DNS>::const_reverse_iterator it(_q.rbegin()); it != _q.rend(); ++it) { if (tmpRawSourceIP != it->rawClientIP() && tmpRawDestinationIP != it->rawClientIP() && (_dnsServers == NULL || _dnsServers->find(it->rawClientIP()) == _dnsServers->end())) { continue; } const std::vector<DNS::DNSResponse*> &responses(it->responses()); foundMatch = false; for (std::vector<DNS::DNSResponse*>::const_iterator r(responses.begin()); r != responses.end(); ++r) { if ((*r)->type() == 1) { if ((tmpRawSourceIP == it->rawClientIP()) && (tmpRawDestinationIP == *reinterpret_cast<const uint32_t *>((*r)->resourceData().data()))) { foundMatch = true; break; } else if ((tmpRawDestinationIP == it->rawClientIP()) && (tmpRawSourceIP == *reinterpret_cast<const uint32_t *>((*r)->resourceData().data()))) { foundMatch = true; break; } else if (serverDNS == NULL && (tmpRawSourceIP == *reinterpret_cast<const uint32_t *>((*r)->resourceData().data()) || tmpRawDestinationIP == *reinterpret_cast<const uint32_t *>((*r)->resourceData().data()))) { serverDNS = new DNS(*it); } } } if (!foundMatch) { continue; } // create SearchDNSResult from DNS and write it. dnsResult.index(_dns_index); dnsResult.neoflow_index(i->neoflow_index()); dnsResult.queryTime(it->queryTime()); dnsResult.responseTime(it->responseTime()); dnsResult.raw_client_ip(it->rawClientIP()); dnsResult.raw_server_ip(it->rawServerIP()); dnsResult.queryName(it->queryName()); dnsResult.rawQueryType(it->rawQueryType()); if (!(_writer->write(dnsResult))) { _error = true; _errorMsg.assign("Writer: "); _errorMsg.append(_writer->error()); return 1; } // create a SearchDNSResponseResult for each result and write them for (std::vector<DNS::DNSResponse*>::const_iterator r(responses.begin()); r != responses.end(); ++r) { dnsResponseResult.dns_index(_dns_index); dnsResponseResult.name((*r)->name()); dnsResponseResult.rawType((*r)->rawType()); dnsResponseResult.resource_data((*r)->resourceData()); dnsResponseResult.rawTTL((*r)->rawTTL()); if (!(_responseWriter->write(dnsResponseResult))) { _error = true; _errorMsg.assign("Writer: "); _errorMsg.append(_responseWriter->error()); return 1; } } ++_dns_index; break; } if (!foundMatch && serverDNS != NULL) { // create SearchDNSResult from serverDNS and write it. dnsResult.index(_dns_index); dnsResult.neoflow_index(i->neoflow_index()); dnsResult.queryTime(serverDNS->queryTime()); dnsResult.responseTime(serverDNS->responseTime()); dnsResult.queryName(serverDNS->queryName()); dnsResult.rawQueryType(serverDNS->rawQueryType()); if (!(_writer->write(dnsResult))) { _error = true; _errorMsg.assign("Writer: "); _errorMsg.append(_writer->error()); return 1; } // create a SearchDNSResponseResult for each result and write them const std::vector<DNS::DNSResponse*> &responses(serverDNS->responses()); for (std::vector<DNS::DNSResponse*>::const_iterator r(responses.begin()); r != responses.end(); ++r) { dnsResponseResult.dns_index(_dns_index); dnsResponseResult.name((*r)->name()); dnsResponseResult.rawType((*r)->rawType()); dnsResponseResult.resource_data((*r)->resourceData()); dnsResponseResult.rawTTL((*r)->rawTTL()); if (!(_responseWriter->write(dnsResponseResult))) { _error = true; _errorMsg.assign("Writer: "); _errorMsg.append(_responseWriter->error()); return 1; } } ++_dns_index; delete serverDNS; serverDNS = NULL; } } return 0; }
TEST_F(DNSTest, Type) { DNS dns; dns.type(DNS::RESPONSE); EXPECT_EQ(dns.type(), DNS::RESPONSE); }
TEST_F(DNSTest, ID) { DNS dns; dns.id(0x7263); EXPECT_EQ(dns.id(), 0x7263); }
TEST_F(DNSTest, RCode) { DNS dns; dns.rcode(0xa); EXPECT_EQ(dns.rcode(), 0xa); }
TEST_F(DNSTest, Opcode) { DNS dns; dns.opcode(0xa); EXPECT_EQ(dns.opcode(), 0xa); }
TEST_F(DNSTest, AuthenticatedData) { DNS dns; dns.authenticated_data(1); EXPECT_EQ(dns.authenticated_data(), 1); }
TEST_F(DNSTest, CheckingDisabled) { DNS dns; dns.checking_disabled(1); EXPECT_EQ(dns.checking_disabled(), 1); }
TEST_F(DNSTest, Z) { DNS dns; dns.z(1); EXPECT_EQ(dns.z(), 1); }
TEST_F(DNSTest, RecursionAvailable) { DNS dns; dns.recursion_available(1); EXPECT_EQ(dns.recursion_available(), 1); }
TEST_F(DNSTest, RecursionDesired) { DNS dns; dns.recursion_desired(1); EXPECT_EQ(dns.recursion_desired(), 1); }
virtual void Tick(time_t) { dns->PruneCache(); }
TEST_F(DNSTest, AuthoritativeAnswer) { DNS dns; dns.authoritative_answer(1); EXPECT_EQ(dns.authoritative_answer(), 1); }