1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef MDNS_COMMON_H
17 #define MDNS_COMMON_H
18 
19 #include <cstddef>
20 #include <map>
21 #include <string>
22 #include <vector>
23 
24 #include "net_manager_constants.h"
25 
26 namespace OHOS {
27 namespace NetManagerStandard {
28 
29 using MDnsPayload = std::vector<uint8_t>;
30 using TxtRecord = std::map<std::string, std::vector<uint8_t>>;
31 using TxtRecordEncoded = std::vector<uint8_t>;
32 
33 static constexpr int32_t LOAD_SA_TIMEOUT = 5;
34 static constexpr int32_t SYNC_TIMEOUT = 5;
35 static constexpr size_t MDNS_MAX_DOMAIN_LABEL = 63;
36 static constexpr size_t MDNS_MAX_DOMAIN = 254;
37 static constexpr const char *MDNS_TOP_DOMAIN_DEFAULT = ".local";
38 static constexpr const char *MDNS_DOMAIN_SPLITER_STR = ".";
39 static constexpr const char *MDNS_HOSTPORT_SPLITER_STR = ":";
40 static constexpr char MDNS_DOMAIN_SPLITER = '.';
41 
42 bool EndsWith(const std::string_view &str, const std::string_view &pat);
43 bool StartsWith(const std::string_view &str, const std::string_view &pat);
44 std::vector<std::string_view> Split(const std::string_view &s, char seperator);
45 
46 bool IsNameValid(const std::string &name);
47 bool IsTypeValid(const std::string &type);
48 bool IsPortValid(int port);
49 bool IsInstanceValid(const std::string &instance);
50 
51 // Size limits (https://www.rfc-editor.org/rfc/rfc1035#section-2.3.4)
52 // Read https://devblogs.microsoft.com/oldnewthing/20120412-00/?p=7873
53 bool IsDomainValid(const std::string &domain);
54 void ExtractNameAndType(const std::string &instance, std::string &name, std::string& type);
55 
56 } // namespace NetManagerStandard
57 } // namespace OHOS
58 #endif // MDNS_COMMON_H
59