blob: 7b53aec27e8f9dc70e7cb00bbb9cad8740b9dd5c [file] [log] [blame]
Jason Jeremy Imana21be272020-10-21 17:53:45 +09001// 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 Iman9df645a2020-11-09 04:37:09 +090012#include "brillo/brillo_export.h"
Jason Jeremy Imana21be272020-10-21 17:53:45 +090013
14namespace 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 Iman9df645a2020-11-09 04:37:09 +090022BRILLO_EXPORT bool DNSDomainFromDot(const base::StringPiece& dotted,
23 std::string* out);
Jason Jeremy Imana21be272020-10-21 17:53:45 +090024
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 Iman9df645a2020-11-09 04:37:09 +090037// not be BRILLO_EXPORT.
38BRILLO_EXPORT bool IsValidHostLabelCharacter(char c, bool is_first_char);
Jason Jeremy Imana21be272020-10-21 17:53:45 +090039
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 Iman9df645a2020-11-09 04:37:09 +090043BRILLO_EXPORT base::Optional<std::string> DnsDomainToString(
Jason Jeremy Imana21be272020-10-21 17:53:45 +090044 base::StringPiece dns_name);
45
46} // namespace patchpanel
47
48#endif // PATCHPANEL_DNS_DNS_UTIL_H_