From b9b2891641da2dba340de2551c3d27c08902393f Mon Sep 17 00:00:00 2001 From: Aki Tuomi Date: Mon, 13 May 2013 16:22:58 +0300 Subject: [PATCH] Added xfrIP6 implementation --- pdns/dnsparser.cc | 1 - pdns/dnsparser.hh | 4 ++++ pdns/dnswriter.hh | 4 ++++ pdns/rcpgenerator.cc | 40 ++++++++++++++++++++++++++++++++++++++++ pdns/rcpgenerator.hh | 2 ++ 5 files changed, 50 insertions(+), 1 deletion(-) diff --git a/pdns/dnsparser.cc b/pdns/dnsparser.cc index 7889aa959..095454ac7 100644 --- a/pdns/dnsparser.cc +++ b/pdns/dnsparser.cc @@ -377,7 +377,6 @@ uint8_t PacketReader::get8BitInt() return d_content.at(d_pos++); } - string PacketReader::getLabel(unsigned int recurs) { string ret; diff --git a/pdns/dnsparser.hh b/pdns/dnsparser.hh index 0d6b24477..414c73e98 100644 --- a/pdns/dnsparser.hh +++ b/pdns/dnsparser.hh @@ -86,6 +86,10 @@ public: val=htonl(val); } + void xfrIP6(std::string &val) { + xfrBlob(val, 16); + } + void xfrTime(uint32_t& val) { xfr32BitInt(val); diff --git a/pdns/dnswriter.hh b/pdns/dnswriter.hh index b0219775d..69bd88e53 100644 --- a/pdns/dnswriter.hh +++ b/pdns/dnswriter.hh @@ -77,6 +77,10 @@ public: { xfr32BitInt(htonl(val)); } + void xfrIP6(const std::string& val) + { + xfrBlob(val,16); + } void xfrTime(const uint32_t& val) { xfr32BitInt(val); diff --git a/pdns/rcpgenerator.cc b/pdns/rcpgenerator.cc index b4946550d..269967110 100644 --- a/pdns/rcpgenerator.cc +++ b/pdns/rcpgenerator.cc @@ -124,6 +124,31 @@ void RecordTextReader::xfrIP(uint32_t &val) } +void RecordTextReader::xfrIP6(std::string &val) +{ + char addrbuf[40]; + struct in6_addr tmpbuf; + + skipSpaces(); + if(!isxdigit(d_string.at(d_pos))) + throw RecordTextException("while parsing IPv6 address, expected xdigits at position "+lexical_cast(d_pos)+" in '"+d_string+"'"); + + size_t len; + // lookup end of value + for(len=0; len < sizeof(addrbuf) && d_pos+len < d_string.length() && (isxdigit(d_string.at(d_pos+len)) || d_string.at(d_pos+len) == ':');len++); + + // end of value is here, try parse as IPv6 + d_string.copy(addrbuf, len, d_pos); + + if (inet_pton(AF_INET6, addrbuf, &tmpbuf) != 1) { + throw RecordTextException("while parsing IPv6 address: '" + std::string(addrbuf) + "' is invalid"); + } + + val = std::string((char*)tmpbuf.s6_addr, 16); + + d_pos += len; +} + bool RecordTextReader::eof() { return d_pos==d_end; @@ -400,6 +425,21 @@ void RecordTextWriter::xfrIP(const uint32_t& val) d_string.append(tmp, pos); } +void RecordTextWriter::xfrIP6(const std::string& val) +{ + char tmpbuf[16]; + char addrbuf[40]; + + if(!d_string.empty()) + d_string.append(1,' '); + + val.copy(tmpbuf,16); + + if (inet_ntop(AF_INET6, tmpbuf, addrbuf, sizeof addrbuf) == NULL) + throw RecordTextException("Unable to convert to ipv6 address"); + + d_string += std::string(addrbuf); +} void RecordTextWriter::xfrTime(const uint32_t& val) { diff --git a/pdns/rcpgenerator.hh b/pdns/rcpgenerator.hh index 174b70a67..02e211ab7 100644 --- a/pdns/rcpgenerator.hh +++ b/pdns/rcpgenerator.hh @@ -49,6 +49,7 @@ public: void xfrType(uint16_t& val); void xfrIP(uint32_t& val); + void xfrIP6(std::string& val); void xfrTime(uint32_t& val); void xfrLabel(string& val, bool compress=false); @@ -76,6 +77,7 @@ public: void xfr16BitInt(const uint16_t& val); void xfr8BitInt(const uint8_t& val); void xfrIP(const uint32_t& val); + void xfrIP6(const std::string& val); void xfrTime(const uint32_t& val); void xfrBase32HexBlob(const string& val); -- 2.50.0