Jason Jeremy Iman | a21be27 | 2020-10-21 17:53:45 +0900 | [diff] [blame^] | 1 | // Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 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 PATCHPANEL_DNS_DNS_UTIL_H_ |
| 6 | #define PATCHPANEL_DNS_DNS_UTIL_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | |
| 10 | #include "base/optional.h" |
| 11 | #include "base/strings/string_piece.h" |
| 12 | |
| 13 | #include "patchpanel/dns/net_export.h" |
| 14 | |
| 15 | namespace patchpanel { |
| 16 | |
| 17 | // DNSDomainFromDot - convert a domain string to DNS format. From DJB's |
| 18 | // public domain DNS library. |dotted| may include only characters a-z, A-Z, |
| 19 | // 0-9, -, and _. |
| 20 | // |
| 21 | // dotted: a string in dotted form: "www.google.com" |
| 22 | // out: a result in DNS form: "\x03www\x06google\x03com\x00" |
| 23 | NET_EXPORT bool DNSDomainFromDot(const base::StringPiece& dotted, |
| 24 | std::string* out); |
| 25 | |
| 26 | // Returns true if the character is valid in a DNS hostname label, whether in |
| 27 | // the first position or later in the label. |
| 28 | // |
| 29 | // This function asserts a looser form of the restrictions in RFC 7719 (section |
| 30 | // 2; https://tools.ietf.org/html/rfc7719#section-2): hostnames can include |
| 31 | // characters a-z, A-Z, 0-9, -, and _, and any of those characters (except -) |
| 32 | // are legal in the first position. The looser rules are necessary to support |
| 33 | // service records (initial _), and non-compliant but attested hostnames that |
| 34 | // include _. These looser rules also allow Punycode and hence IDN. |
| 35 | // |
| 36 | // TODO(palmer): In the future, when we can remove support for invalid names, |
| 37 | // this can be a private implementation detail of |DNSDomainFromDot|, and need |
| 38 | // not be NET_EXPORT_PRIVATE. |
| 39 | NET_EXPORT_PRIVATE bool IsValidHostLabelCharacter(char c, bool is_first_char); |
| 40 | |
| 41 | // Converts a domain in DNS format to a dotted string. Excludes the dot at the |
| 42 | // end. Assumes the standard terminating zero-length label at the end if not |
| 43 | // included in the input. Returns nullopt on malformed input. |
| 44 | NET_EXPORT base::Optional<std::string> DnsDomainToString( |
| 45 | base::StringPiece dns_name); |
| 46 | |
| 47 | } // namespace patchpanel |
| 48 | |
| 49 | #endif // PATCHPANEL_DNS_DNS_UTIL_H_ |