6 #include <boost/algorithm/string/join.hpp>
7 #include <boost/assign/list_of.hpp>
8 #include <boost/regex.hpp>
11 using namespace boost::assign;
47 (3,
"Non-existent domain")
48 (4,
"Not implemented")
50 (6,
"Name exists when it should not")
51 (7,
"RR Set Exists when it should not")
52 (8,
"RR Set that should exist does not")
54 (10,
"Name not contained in zone")
55 (16,
"Bad OPT Version")
56 (17,
"Key not recognized")
57 (18,
"Signature out of time window")
59 (20,
"Duplicate key name")
60 (21,
"Algorithm not supported")
61 (22,
"Bad truncation")
62 (4095,
"Invalid response code");
77 return plen < rhs.
plen;
82 }
else if (prefix.is_v6()) {
88 return plen < rhs.
plen;
91 return !rhs.
prefix.is_v4();
100 if (prefix.is_v4() && addr.is_v4()) {
102 }
else if (prefix.is_v6() && addr.is_v6()) {
143 return DnsResponseCode(4095);
148 uint16_t plen, uint16_t offset, uint16_t &length) {
149 std::size_t size = addr.size();
150 std::size_t cur_pos = 0;
151 std::size_t prev_pos = 0;
152 while (cur_pos < size && cur_pos != std::string::npos) {
153 if (offset && !plen) {
154 ptr = WriteShort(ptr, offset);
159 cur_pos = addr.find(
'.', prev_pos);
160 if (cur_pos == std::string::npos)
161 len = size - prev_pos;
163 len = cur_pos - prev_pos;
166 memcpy(ptr + 1, addr.substr(prev_pos, len).data(), len);
168 plen = (plen > len) ? plen - len - 1 : 0;
170 prev_pos = cur_pos + 1;
173 ptr = WriteByte(ptr, 0);
180 uint16_t
type, uint16_t cl,
182 ptr = AddName(ptr, name, 0, 0, length);
183 ptr = WriteShort(ptr,
type);
184 ptr = WriteShort(ptr, cl);
192 boost::system::error_code ec;
195 ptr = WriteShort(ptr, 4);
196 ptr = WriteWord(ptr, boost::asio::ip::address_v4::from_string(
197 item.
data, ec).to_ulong());
200 ptr = WriteShort(ptr, 16);
201 boost::asio::ip::address_v6 addr =
202 boost::asio::ip::address_v6::from_string(item.
data, ec);
206 memcpy(ptr, addr.to_bytes().data(), 16);
216 ptr = WriteShort(ptr, data_len);
223 ptr = WriteWord(ptr, item.
soa.
retry);
225 ptr = WriteWord(ptr, item.
soa.
ttl);
232 ptr = WriteShort(ptr, data_len);
238 ptr = WriteShort(ptr, data_len);
240 ptr = WriteShort(ptr, item.
priority);
246 ptr = WriteShort(ptr, data_len);
249 ptr = WriteShort(ptr, item.
srv.
port);
260 uint16_t size = item.
data.size();
261 ptr = WriteShort(ptr, size);
263 memcpy(ptr, item.
data.c_str(), size);
275 ptr = WriteShort(ptr, item.
type);
276 ptr = WriteShort(ptr, item.
eclass);
277 ptr = WriteWord(ptr, item.
ttl);
279 ptr = AddData(ptr, item, length);
285 uint16_t
type, uint16_t cl,
286 uint32_t ttl,
const std::string &data,
288 ptr = AddQuestionSection(ptr, name,
type, cl, length);
289 ptr = WriteWord(ptr, ttl);
290 ptr = WriteShort(ptr, data.size() + 1);
293 ptr = WriteByte(ptr, data.size());
294 memcpy(ptr, data.data(), data.size());
296 length += 4 + 2 + 1 + data.size();
302 uint16_t cl, uint32_t ttl, uint16_t &length) {
303 ptr = AddQuestionSection(ptr, item.
name, item.
type, cl, length);
304 ptr = WriteWord(ptr, ttl);
306 ptr = AddData(ptr, item, length);
312 std::string &name, uint16_t &plen, uint16_t &offset) {
317 uint8_t *ptr = dns + (dnslen - *remlen);
318 std::size_t len = *ptr;
320 if ((len & 0xC0) == 0xC0) {
321 plen = name.size() ? name.size() - 1 : 0;
322 if (ReadShort(dns, dnslen, remlen, offset) ==
false)
324 int offset_remlen = dnslen - (offset & ~0xC000);
326 return ReadName(dns, dnslen, &offset_remlen, name, dummy, dummy);
328 *remlen -= (len + 1);
332 name.append((
char *)ptr+1, len);
347 if (ReadShort(dns, dnslen, remlen, length) ==
false)
350 boost::system::error_code ec;
353 if (ReadWord(dns, dnslen, remlen, ip) ==
false)
355 boost::asio::ip::address_v4 addr(ip);
356 item.
data = addr.to_string(ec);
362 uint8_t *ptr = dns + (dnslen - *remlen);
363 boost::asio::ip::address_v6::bytes_type ip;
364 memcpy(&ip[0], ptr, 16);
365 boost::asio::ip::address_v6 addr(ip);
366 item.
data = addr.to_string(ec);
373 if (ReadName(dns, dnslen, remlen, item.
soa.
mailbox,
376 if (ReadWord(dns, dnslen, remlen, item.
soa.
serial) ==
false)
378 if (ReadWord(dns, dnslen, remlen, item.
soa.
refresh) ==
false)
380 if (ReadWord(dns, dnslen, remlen, item.
soa.
retry) ==
false)
382 if (ReadWord(dns, dnslen, remlen, item.
soa.
expiry) ==
false)
384 return ReadWord(dns, dnslen, remlen, item.
soa.
ttl);
390 if (ReadShort(dns, dnslen, remlen, item.
priority) ==
false)
394 if (ReadShort(dns, dnslen, remlen, item.
srv.
priority) ==
false)
396 if (ReadShort(dns, dnslen, remlen, item.
srv.
weight) ==
false)
398 if (ReadShort(dns, dnslen, remlen, item.
srv.
port) ==
false)
400 return ReadName(dns, dnslen, remlen, item.
srv.
hostname,
409 if (*remlen < length) {
412 uint8_t *ptr = dns + (dnslen - *remlen);
413 item.
data.assign((
const char *)ptr, length);
419 "Unsupported data type in DNS response : " << item.
type);
429 if (ReadShort(dns, dnslen, remlen, item.
type) ==
false)
432 return ReadShort(dns, dnslen, remlen, item.
eclass);
437 if (ReadQuestionEntry(dns, dnslen, remlen, item) ==
false)
440 if (ReadWord(dns, dnslen, remlen, item.
ttl) ==
false)
443 return ReadData(dns, dnslen, remlen, item);
450 if (dnslen <=
sizeof(
dnshdr)) {
452 "Invalid DNS Query with header missing - dropping it");
457 xid = ntohs(hdr->
xid);
460 int remlen = dnslen -
sizeof(
dnshdr);
461 for (
unsigned int i = 0; i < ques_rrcount; ++i) {
463 item.
offset = (dnslen - remlen) | 0xC000;
464 if (ReadQuestionEntry(dns, dnslen, &remlen, item) ==
false) {
466 "section - xid : " << xid <<
" - dropping it");
469 items.push_back(item);
472 *parsed_length = dnslen - remlen;
480 if (dnslen <
sizeof(
dnshdr)) {
482 "Invalid DNS Response with header missing - dropping it");
487 xid = ntohs(hdr->
xid);
495 int remlen = dnslen -
sizeof(
dnshdr);
499 for (
unsigned int i = 0; i < ques_rrcount; ++i) {
501 if (ReadQuestionEntry(dns, dnslen, &remlen, item) ==
false) {
502 errmsg =
"Parse error in question section";
505 ques.push_back(item);
509 for (
unsigned int i = 0; i < ans_rrcount; ++i) {
511 if (ReadAnswerEntry(dns, dnslen, &remlen, item) ==
false) {
512 errmsg =
"Parse error in answer section";
519 for (
unsigned int i = 0; i < auth_rrcount; ++i) {
521 if (ReadAnswerEntry(dns, dnslen, &remlen, item) ==
false) {
522 errmsg =
"Parse error in authority section";
525 auth.push_back(item);
529 for (
unsigned int i = 0; i < add_rrcount; ++i) {
531 if (ReadAnswerEntry(dns, dnslen, &remlen, item) ==
false) {
532 errmsg =
"Parse error in additional section";
542 "Invalid DNS response : " << errmsg <<
543 " xid : " << xid <<
" - dropping it");
549 if (dnslen <=
sizeof(
dnshdr)) {
550 DNS_BIND_TRACE(DnsBindError,
"Invalid DNS Update with header missing "
559 uint16_t xid = ntohs(hdr->
xid);
561 if (zone_count != 1) {
563 "Invalid zone count in Update request : " << zone_count);
567 if (prereq_count != 0) {
570 "which is not supported; dropping the request");
574 int remlen = dnslen -
sizeof(
dnshdr);
578 uint16_t zone_type, zone_class, plen, offset;
579 if (ReadName(dns, dnslen, &remlen, data.
zone, plen, offset) ==
false ||
580 ReadShort(dns, dnslen, &remlen, zone_type) ==
false ||
581 ReadShort(dns, dnslen, &remlen, zone_class) ==
false) {
582 errmsg =
"Parse error in reading zone";
587 for (
unsigned int i = 0; i < update_count; ++i) {
589 if (ReadName(dns, dnslen, &remlen, item.
name, plen, offset) ==
false ||
590 ReadShort(dns, dnslen, &remlen, item.
type) ==
false ||
591 ReadShort(dns, dnslen, &remlen, item.
eclass) ==
false ||
592 ReadWord(dns, dnslen, &remlen, item.
ttl) ==
false ||
593 ReadData(dns, dnslen, &remlen, item) ==
false) {
594 errmsg =
"Parse error";
597 data.
items.push_back(item);
604 "Invalid DNS Update : " << errmsg <<
605 " xid : " << xid <<
" - dropping it");
610 DnsOpcode op,
bool rd,
bool ra, uint8_t ret,
611 uint16_t ques_count) {
612 dns->
xid = htons(xid);
630 const std::string &domain,
634 1, 0, 0, items.size());
637 uint16_t len =
sizeof(
dnshdr);
638 uint8_t *ques = (uint8_t *) (dns + 1);
639 for (DnsItems::const_iterator it = items.begin(); it != items.end(); ++it) {
640 ques = AddQuestionSection(ques, (*it).name, (*it).type,
644 if (!domain.empty()) {
646 std::string view =
"view=" + domain;
655 const std::string &domain,
656 const std::string &zone,
666 uint16_t len =
sizeof(
dnshdr);
667 uint8_t *ptr = (uint8_t *) (dns + 1);
674 for (DnsItems::const_iterator it = items.begin();
675 it != items.end(); ++it) {
676 ptr = AddUpdate(ptr, *it, (*it).eclass, (*it).ttl, len);
681 for (DnsItems::const_iterator it = items.begin();
682 it != items.end(); ++it) {
692 std::string view =
"view=" + domain;
701 static boost::regex in_addr_arpa(
"(?:[0-9]+\\.){1,4}in-addr\\.arpa\\.?",
702 boost::regex::perl | boost::regex::icase);
707 static boost::regex ip6_arpa(
"(?:[0-9a-f]\\.){1,32}ip6\\.arpa\\.?",
708 boost::regex::perl | boost::regex::icase);
717 boost::system::error_code ec;
718 addr = boost::asio::ip::address::from_string(name, ec);
724 assert((bit >> 3) < 16);
725 uint8_t buf = addr[bit >> 3];
726 return ((bit & 0x7) ? (buf & 0xF) : (buf >> 4));
734 std::vector<std::string> items;
737 for (
size_t i = 0; i < (plen >> 2); i++) {
738 buf = GetNibble(addr, i << 2);
739 std::stringstream str;
740 str << std::hex << static_cast<unsigned int>(buf);
741 items.push_back(str.str());
744 std::reverse(items.begin(), items.end());
745 items.push_back(
"ip6.arpa");
746 return boost::algorithm::join(items,
".");
752 uint32_t addr = mask.to_ulong();
753 std::string zone_name =
"in-addr.arpa.";
755 std::stringstream str;
757 zone_name = str.str() +
"." + zone_name;
763 zones.push_back(zone_name);
766 for (
int j = 0; j <= (0xFF >> plen); j++) {
770 uint32_t last = (addr >> 24) + j;
771 std::stringstream str;
773 std::string rev_zone_name = str.str() +
"." + zone_name;
774 zones.push_back(rev_zone_name);
780 Ip6Address::bytes_type addr = mask.to_bytes();
781 std::string zone_name;
784 if (!plen || plen > 128)
789 uint32_t bits_rem = plen & 0x3;
793 for (
int j = 0; j <= (0xF >> bits_rem); j++) {
794 std::stringstream str;
795 str << std::hex << static_cast<unsigned int>(buf + j)
797 zones.push_back(str.str());
800 zones.push_back(zone_name);
807 GetReverseZoneList(mask.to_v4(), plen, zones);
808 }
else if (mask.is_v6()) {
809 GetReverseZoneList(mask.to_v6(), plen, zones);
815 uint32_t addr = ip.to_ulong();
816 zone =
"in-addr.arpa";
818 std::stringstream str;
820 zone = str.str() +
"." + zone;
828 std::stringstream str;
830 zone = str.str() +
"." + zone;
835 Ip6Address::bytes_type addr = ip.to_bytes();
838 if (!plen || plen > 128)
843 uint32_t bits_rem = plen & 0x3;
847 std::stringstream str;
848 str << std::hex << static_cast<unsigned int>(buf) <<
"." << zone;
856 GetReverseZone(addr.to_v4(), plen, zone);
857 }
else if (addr.is_v6()) {
858 GetReverseZone(addr.to_v6(), plen, zone);
868 std::stringstream str(ptr_name);
869 if (str.peek() ==
'.')
873 if (str.peek() ==
'i')
877 addr |= (
num << (8 * count));
887 Ip6Address::bytes_type addr;
888 BOOST_ASSERT(addr.size() == 16);
890 typedef boost::tokenizer<boost::char_separator<char> >
tokenizer;
891 typedef boost::array<uint8_t, 32> nibbles;
896 tokenizer tok(ptr_name, boost::char_separator<char>(
"."));
899 tokenizer::const_iterator tok_it = tok.begin();
900 nibbles::iterator nib_it = nib.begin();
901 for (; tok_it != tok.end() && nib_it != nib.end(); ++tok_it, ++nib_it) {
902 std::stringstream str(*tok_it);
906 if (!(str >> std::hex >> buf))
909 *nib_it =
static_cast<uint8_t
>(buf);
912 std::fill(addr.begin(), addr.end(), 0);
914 std::reverse_iterator<nibbles::const_iterator> r_nib_it(nib_it);
915 Ip6Address::bytes_type::iterator addr_it = addr.begin();
918 bool odd_nib =
false;
920 for (; r_nib_it != nib.rend(); ++r_nib_it) {
922 *addr_it |= (*r_nib_it & 0xF);
925 *addr_it |= (*r_nib_it << 4);
936 std::string name = boost::to_lower_copy(ptr_name);
939 pos = name.find(
".in-addr.arpa");
940 if (pos != std::string::npos) {
948 pos = name.find(
".ip6.arpa");
949 if (pos != std::string::npos) {
961 std::stringstream str;
962 for (
int i = 0; i < 4; i++) {
963 str << ((ip.to_ulong() >> (i * 8)) & 0xFF) <<
".";
965 str <<
"in-addr.arpa";
970 std::stringstream str;
972 bool odd_nibble =
true;
974 Ip6Address::bytes_type addr = ip6.to_bytes();
975 Ip6Address::bytes_type::reverse_iterator it = addr.rbegin();
976 while (it != addr.rend()) {
984 str << std::hex << buf <<
'.';
985 odd_nibble = !odd_nibble;
993 const std::string &match) {
994 if (name.find(match, 0) == std::string::npos)
995 return name +
"." + domain;
1001 for (
unsigned int i = 0; i < name.size(); ++i) {
1002 uint8_t c = name[i];
1003 if (!((c >=
'a' && c <=
'z') || (c >=
'A' && c <=
'Z') ||
1004 (c >=
'0' && c <=
'9') || (c ==
'-') || (c ==
'.')))
1011 for (
unsigned int i = 0; i < name.size(); ++i) {
1012 uint8_t c = name[i];
1013 if (!((c >=
'a' && c <=
'z') || (c >=
'A' && c <=
'Z') ||
1014 (c >=
'0' && c <=
'9') || (c ==
'-') || (c ==
'.')))
1020 uint16_t &name_plen, uint16_t &name_offset) {
1023 std::size_t pos = 0;
1024 while (pos < name.size()) {
1025 std::string str = name.substr(pos);
1026 if(IsPresent(str, name_offset)) {
1027 name_plen = pos ? pos - 1 : 0;
1031 pos = name.find(
'.', pos + 1);
1032 if (pos == std::string::npos)
1037 names_.push_back(
Name(name, curr_msg_offset));
1041 for (
unsigned int i = 0; i < names_.size(); i++) {
1042 if (names_[i].name == name) {
1043 name_offset = names_[i].offset;
boost::asio::ip::address_v6 Ip6Address
boost::asio::ip::address IpAddress
boost::asio::ip::address_v4 Ip4Address
bool IsIp6SubnetMember(const Ip6Address &ip, const Ip6Address &subnet, uint8_t plen)
bool IsIp4SubnetMember(const Ip4Address &ip, const Ip4Address &prefix_ip, uint16_t plen)
SandeshTraceBufferPtr DnsBindTraceBuf(SandeshTraceBufferCreate("DnsBind", 2000))
DnsTypeNumMap g_dns_class_num_map
DnsTypeMap g_dns_type_map
DnsTypeNumMap g_dns_type_num_map
DnsResponseMap g_dns_response_map
std::map< uint16_t, std::string >::const_iterator DnsResponseIter
std::list< DnsItem > DnsItems
#define DNS_BIND_TRACE(obj, arg)
std::map< std::string, uint16_t > DnsTypeMap
std::map< uint16_t, std::string > DnsResponseMap
std::map< uint16_t, std::string > DnsTypeNumMap
std::vector< std::string > ZoneList
std::map< std::string, uint16_t >::const_iterator DnsTypeIter
std::map< uint16_t, std::string >::const_iterator DnsTypeNumIter
static bool HasSpecialChars(const std::string &name)
static const std::string & DnsResponseCode(uint16_t code)
static bool ReadName(uint8_t *dns, uint16_t dnslen, int *remlen, std::string &name, uint16_t &plen, uint16_t &offset)
static uint8_t * AddAnswerSection(uint8_t *ptr, const DnsItem &item, uint16_t &length)
static int BuildDnsQuery(uint8_t *buf, uint16_t xid, const std::string &domain, const DnsItems &items)
static bool ParseDnsUpdate(uint8_t *dns, uint16_t dnslen, DnsUpdateData &data)
static std::string GetPtrNameFromAddr(const Ip4Address &ip)
static void GetReverseZone(const Ip4Address &addr, uint32_t plen, std::string &zone)
static void RemoveSpecialChars(std::string &name)
static uint8_t GetNibble(const Ip6Address::bytes_type &addr, size_t bit)
static bool IsReverseZone(const std::string &name)
static uint16_t DnsType(const std::string &tp)
static uint8_t * AddQuestionSection(uint8_t *ptr, const std::string &name, uint16_t type, uint16_t cl, uint16_t &length)
static bool GetAddrFromPtrName(std::string &ptr_name, IpAddress &mask)
static void GetReverseZoneList(const IpAddress &mask, uint32_t plen, ZoneList &zones)
static void BuildDnsHeader(dnshdr *dns, uint16_t xid, DnsReq req, DnsOpcode op, bool rd, bool ra, uint8_t ret, uint16_t ques_count)
static uint16_t DnsClass(const std::string &cl)
static uint8_t * AddName(uint8_t *ptr, const std::string &addr, uint16_t plen, uint16_t offset, uint16_t &length)
static std::string BuildIp6ArpaSuffix(const Ip6Address::bytes_type &addr, uint32_t plen)
static bool IsReverseZoneV4(const std::string &name)
static bool IsIP(const std::string &name, IpAddress &addr)
static bool ReadQuestionEntry(uint8_t *dns, uint16_t dnslen, int *remlen, DnsItem &item)
static bool ReadAnswerEntry(uint8_t *dns, uint16_t dnslen, int *remlen, DnsItem &item)
static uint8_t * AddUpdate(uint8_t *ptr, const DnsItem &item, uint16_t cl, uint32_t ttl, uint16_t &length)
static bool ParseDnsResponse(uint8_t *dns, uint16_t dnslen, uint16_t &xid, dns_flags &flags, DnsItems &ques, DnsItems &ans, DnsItems &auth, DnsItems &add)
static bool IsReverseZoneV6(const std::string &name)
static int BuildDnsUpdate(uint8_t *buf, Operation op, uint16_t xid, const std::string &domain, const std::string &zone, const DnsItems &items)
static bool ReadData(uint8_t *dns, uint16_t dnslen, int *remlen, DnsItem &item)
static uint8_t * AddData(uint8_t *ptr, const DnsItem &item, uint16_t &length)
static std::string GetFQDN(const std::string &name, const std::string &domain, const std::string &match)
static uint8_t * AddAdditionalSection(uint8_t *ptr, const std::string name, uint16_t type, uint16_t cl, uint32_t ttl, const std::string &data, uint16_t &length)
static bool ParseDnsQuery(uint8_t *dns, uint16_t dnslen, uint16_t *parsed_length, DnsItems &items)
bool IsPresent(std::string &name, uint16_t &name_offset)
void AddName(std::string &name, uint16_t curr_msg_offset, uint16_t &name_plen, uint16_t &name_offset)
static bool regex_match(const std::string &input, const regex ®ex)
boost::shared_ptr< TraceBuffer< SandeshTrace > > SandeshTraceBufferPtr
SandeshTraceBufferPtr SandeshTraceBufferCreate(const std::string &buf_name, size_t buf_size, bool trace_enable=true)
static const std::string integerToString(const NumberType &num)
std::string ToString() const
bool operator<(const Subnet &rhs) const
bool Contains(const IpAddress &addr) const
void GetReverseZones(ZoneList &zones) const
boost::tokenizer< boost::char_separator< char > > tokenizer