1 /*
2 * Copyright (c) 2021, The OpenThread Authors.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Neither the name of the copyright holder nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "utils/dns_utils.hpp"
30
31 #include <assert.h>
32
33 #include "common/code_utils.hpp"
34
35 namespace otbr {
36
37 namespace DnsUtils {
38
UnescapeInstanceName(const std::string & aName)39 std::string UnescapeInstanceName(const std::string &aName)
40 {
41 std::string newName;
42 auto nameLen = aName.length();
43
44 newName.reserve(nameLen);
45
46 for (unsigned int i = 0; i < nameLen; i++)
47 {
48 char c = aName[i];
49
50 if (c == '\\')
51 {
52 if (i + 3 < nameLen && isdigit(aName[i + 1]) && isdigit(aName[i + 2]) && isdigit(aName[i + 3]))
53 {
54 uint8_t b = (aName[i + 1] - '0') * 100 + (aName[i + 2] - '0') * 10 + (aName[i + 3] - '0');
55
56 newName.push_back(b);
57 i += 3;
58 continue;
59 }
60
61 if (i + 1 < nameLen)
62 {
63 newName.push_back(aName[i + 1]);
64 i += 1;
65 continue;
66 }
67 }
68
69 // append all not escaped characters
70 newName.push_back(c);
71 }
72
73 return newName;
74 }
75
CheckHostnameSanity(const std::string & aHostName)76 void CheckHostnameSanity(const std::string &aHostName)
77 {
78 OTBR_UNUSED_VARIABLE(aHostName);
79
80 assert(aHostName.length() > 0);
81 assert(aHostName.back() == '.');
82 }
83
CheckServiceNameSanity(const std::string & aServiceName)84 void CheckServiceNameSanity(const std::string &aServiceName)
85 {
86 size_t dotpos;
87
88 OTBR_UNUSED_VARIABLE(aServiceName);
89 OTBR_UNUSED_VARIABLE(dotpos);
90
91 assert(aServiceName.length() > 0);
92 assert(aServiceName[aServiceName.length() - 1] != '.');
93 dotpos = aServiceName.find_first_of('.');
94 assert(dotpos != std::string::npos);
95 assert(dotpos == aServiceName.find_last_of('.'));
96 }
97
98 } // namespace DnsUtils
99
100 } // namespace otbr
101