[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | c36f064 | 2009-09-09 01:10:50 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef NET_BASE_DNS_UTIL_H_ |
| 6 | #define NET_BASE_DNS_UTIL_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
[email protected] | c36f064 | 2009-09-09 01:10:50 | [diff] [blame] | 8 | |
| 9 | #include <string> |
| 10 | |
[email protected] | b3ccac8 | 2010-08-04 15:54:40 | [diff] [blame] | 11 | #include "base/basictypes.h" |
[email protected] | d10740b | 2011-05-24 19:26:31 | [diff] [blame] | 12 | #include "base/string_piece.h" |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame] | 13 | #include "net/base/net_api.h" |
[email protected] | b3ccac8 | 2010-08-04 15:54:40 | [diff] [blame] | 14 | |
[email protected] | c36f064 | 2009-09-09 01:10:50 | [diff] [blame] | 15 | namespace net { |
| 16 | |
| 17 | // DNSDomainFromDot - convert a domain string to DNS format. From DJB's |
| 18 | // public domain DNS library. |
| 19 | // |
| 20 | // dotted: a string in dotted form: "www.google.com" |
| 21 | // out: a result in DNS form: "\x03www\x06google\x03com\x00" |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame] | 22 | NET_TEST bool DNSDomainFromDot(const std::string& dotted, std::string* out); |
[email protected] | c36f064 | 2009-09-09 01:10:50 | [diff] [blame] | 23 | |
[email protected] | f060be3 | 2011-02-17 17:20:28 | [diff] [blame] | 24 | // DNSDomainToString coverts a domain in DNS format to a dotted string. |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame] | 25 | NET_TEST std::string DNSDomainToString(const std::string& domain); |
[email protected] | f060be3 | 2011-02-17 17:20:28 | [diff] [blame] | 26 | |
[email protected] | c36f064 | 2009-09-09 01:10:50 | [diff] [blame] | 27 | // Returns true iff the given character is in the set of valid DNS label |
| 28 | // characters as given in RFC 3490, 4.1, 3(a) |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame] | 29 | NET_TEST bool IsSTD3ASCIIValidCharacter(char c); |
[email protected] | c36f064 | 2009-09-09 01:10:50 | [diff] [blame] | 30 | |
[email protected] | 13c340e1 | 2010-03-08 19:29:41 | [diff] [blame] | 31 | // Returns the hostname by trimming the ending dot, if one exists. |
[email protected] | d01c6bd | 2011-05-31 23:20:59 | [diff] [blame] | 32 | NET_API std::string TrimEndingDot(const std::string& host); |
[email protected] | 13c340e1 | 2010-03-08 19:29:41 | [diff] [blame] | 33 | |
[email protected] | b581064 | 2011-06-03 21:26:49 | [diff] [blame] | 34 | // DNS class types. |
| 35 | static const uint16 kClassIN = 1; |
| 36 | |
[email protected] | b3ccac8 | 2010-08-04 15:54:40 | [diff] [blame] | 37 | // DNS resource record types. See |
| 38 | // http://www.iana.org/assignments/dns-parameters |
[email protected] | b1d8c25 | 2011-01-13 20:27:50 | [diff] [blame] | 39 | // WARNING: if you're adding any new values here you may need to add them to |
| 40 | // dnsrr_resolver.cc:DnsRRIsParsedByWindows. |
[email protected] | b581064 | 2011-06-03 21:26:49 | [diff] [blame] | 41 | static const uint16 kDNS_A = 1; |
[email protected] | b40f05a | 2010-09-09 20:13:23 | [diff] [blame] | 42 | static const uint16 kDNS_CNAME = 5; |
[email protected] | b3ccac8 | 2010-08-04 15:54:40 | [diff] [blame] | 43 | static const uint16 kDNS_TXT = 16; |
[email protected] | b581064 | 2011-06-03 21:26:49 | [diff] [blame] | 44 | static const uint16 kDNS_AAAA = 28; |
[email protected] | b3ccac8 | 2010-08-04 15:54:40 | [diff] [blame] | 45 | static const uint16 kDNS_CERT = 37; |
[email protected] | b2471359 | 2010-08-11 19:50:02 | [diff] [blame] | 46 | static const uint16 kDNS_DS = 43; |
| 47 | static const uint16 kDNS_RRSIG = 46; |
| 48 | static const uint16 kDNS_DNSKEY = 48; |
[email protected] | b3ccac8 | 2010-08-04 15:54:40 | [diff] [blame] | 49 | static const uint16 kDNS_ANY = 0xff; |
[email protected] | bad796d | 2011-06-02 21:06:54 | [diff] [blame] | 50 | static const uint16 kDNS_CAA = 257; |
[email protected] | 9cf1e9da7 | 2010-09-30 16:13:15 | [diff] [blame] | 51 | static const uint16 kDNS_TESTING = 0xfffe; // in private use area. |
[email protected] | b3ccac8 | 2010-08-04 15:54:40 | [diff] [blame] | 52 | |
[email protected] | b2471359 | 2010-08-11 19:50:02 | [diff] [blame] | 53 | // http://www.iana.org/assignments/dns-sec-alg-numbers/dns-sec-alg-numbers.xhtml |
| 54 | static const uint8 kDNSSEC_RSA_SHA1 = 5; |
| 55 | static const uint8 kDNSSEC_RSA_SHA1_NSEC3 = 7; |
| 56 | static const uint8 kDNSSEC_RSA_SHA256 = 8; |
| 57 | |
| 58 | // RFC 4509 |
| 59 | static const uint8 kDNSSEC_SHA1 = 1; |
| 60 | static const uint8 kDNSSEC_SHA256 = 2; |
| 61 | |
[email protected] | d10740b | 2011-05-24 19:26:31 | [diff] [blame] | 62 | // A Buffer is used for walking over a DNS response packet. |
| 63 | class DnsResponseBuffer { |
| 64 | public: |
| 65 | DnsResponseBuffer(const uint8* p, unsigned len) |
| 66 | : p_(p), |
| 67 | packet_(p), |
| 68 | len_(len), |
| 69 | packet_len_(len) { |
| 70 | } |
| 71 | |
| 72 | bool U8(uint8* v); |
| 73 | bool U16(uint16* v); |
| 74 | bool U32(uint32* v); |
| 75 | bool Skip(unsigned n); |
| 76 | |
| 77 | bool Block(base::StringPiece* out, unsigned len); |
| 78 | |
| 79 | // DNSName parses a (possibly compressed) DNS name from the packet. If |name| |
| 80 | // is not NULL, then the name is written into it. See RFC 1035 section 4.1.4. |
| 81 | bool DNSName(std::string* name); |
| 82 | |
| 83 | private: |
| 84 | const uint8* p_; |
| 85 | const uint8* const packet_; |
| 86 | unsigned len_; |
| 87 | const unsigned packet_len_; |
| 88 | |
| 89 | DISALLOW_COPY_AND_ASSIGN(DnsResponseBuffer); |
| 90 | }; |
| 91 | |
| 92 | |
[email protected] | c36f064 | 2009-09-09 01:10:50 | [diff] [blame] | 93 | } // namespace net |
| 94 | |
| 95 | #endif // NET_BASE_DNS_UTIL_H_ |