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