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 #include "mdns_service_info.h"
17 #include "netmgr_ext_log_wrapper.h"
18
19 namespace OHOS {
20 namespace NetManagerStandard {
21
22 namespace {
23 constexpr uint8_t MDNS_TXT_KEY_LENGTH_MASK = 0xff;
24 constexpr size_t MDNS_TXT_KEY_LENGTH_LIMIT = 9;
25 constexpr uint16_t MDNS_TXT_KEY_RANGE_LOWER = 0x20;
26 constexpr uint16_t MDNS_TXT_KEY_RANGE_UPPER = 0x7e;
27 constexpr char MDNS_TXT_KV_EQ = '=';
28 } // namespace
29
Marshalling(Parcel & parcel) const30 bool MDnsServiceInfo::Marshalling(Parcel &parcel) const
31 {
32 return parcel.WriteString(name) && parcel.WriteString(type) && parcel.WriteInt32(family) &&
33 parcel.WriteString(addr) && parcel.WriteInt32(port) && parcel.WriteUInt8Vector(txtRecord);
34 }
35
Marshalling(Parcel & data,const sptr<MDnsServiceInfo> & obj)36 bool MDnsServiceInfo::Marshalling(Parcel &data, const sptr<MDnsServiceInfo> &obj)
37 {
38 return (obj != nullptr) && obj->Marshalling(data);
39 }
40
Unmarshalling(Parcel & parcel)41 sptr<MDnsServiceInfo> MDnsServiceInfo::Unmarshalling(Parcel &parcel)
42 {
43 sptr<MDnsServiceInfo> ptr = new (std::nothrow) MDnsServiceInfo;
44 if (ptr == nullptr) {
45 return nullptr;
46 }
47 bool allOK = parcel.ReadString(ptr->name) && parcel.ReadString(ptr->type) && parcel.ReadInt32(ptr->family) &&
48 parcel.ReadString(ptr->addr) && parcel.ReadInt32(ptr->port) && parcel.ReadUInt8Vector(&ptr->txtRecord);
49 return allOK ? ptr : nullptr;
50 }
51
IsKeyValueVaild(const::std::string & key,const std::vector<uint8_t> & value)52 bool MDnsServiceInfo::IsKeyValueVaild(const ::std::string &key, const std::vector<uint8_t> &value)
53 {
54 if (key.length() == 0) {
55 return false;
56 }
57 if (key.length() > MDNS_TXT_KEY_LENGTH_LIMIT) {
58 NETMGR_EXT_LOG_W("Key lengths > 9 are discouraged: %{public}s", key.c_str());
59 }
60 for (size_t i = 0; i < key.size(); ++i) {
61 char character = key[i];
62 if (character < MDNS_TXT_KEY_RANGE_LOWER || character > MDNS_TXT_KEY_RANGE_UPPER) {
63 return false;
64 }
65 if (character == MDNS_TXT_KV_EQ) {
66 return false;
67 }
68 }
69 return key.length() + value.size() < MDNS_TXT_KEY_LENGTH_MASK;
70 }
71
GetAttrMap()72 TxtRecord MDnsServiceInfo::GetAttrMap()
73 {
74 TxtRecord map;
75 size_t pos = 0;
76 while (pos < txtRecord.size()) {
77 size_t recordLen = txtRecord[pos] & MDNS_TXT_KEY_LENGTH_MASK;
78 pos += 1;
79 if (recordLen == 0) {
80 NETMGR_EXT_LOG_W("Zero sized txt record detected");
81 } else if (pos + recordLen > txtRecord.size()) {
82 NETMGR_EXT_LOG_W("Bad record size");
83 recordLen = txtRecord.size() - pos;
84 }
85 std::string key;
86 std::vector<uint8_t> value;
87 auto start = txtRecord.begin() + static_cast<int>(pos);
88 auto cur = std::find(start, start + static_cast<int>(recordLen), MDNS_TXT_KV_EQ);
89 key = std::string(start, cur);
90 ++cur;
91 if (cur < start + static_cast<int>(recordLen)) {
92 value = std::vector<uint8_t>(cur, start + static_cast<int>(recordLen));
93 }
94 if (map.count(key) != 0) {
95 continue;
96 }
97 if (!IsKeyValueVaild(key, value)) {
98 NETMGR_EXT_LOG_W("Key -> value is not valid");
99 continue;
100 }
101 map[key] = value;
102 pos += recordLen;
103 }
104 return map;
105 }
106
SetAttrMap(const TxtRecord & map)107 void MDnsServiceInfo::SetAttrMap(const TxtRecord &map)
108 {
109 txtRecord.clear();
110 for (const auto &[key, value] : map) {
111 if (!IsKeyValueVaild(key, value)) {
112 NETMGR_EXT_LOG_W("Key -> value is not valid");
113 continue;
114 }
115 txtRecord.emplace_back((key.size() + value.size() + 1) & MDNS_TXT_KEY_LENGTH_MASK);
116 txtRecord.insert(txtRecord.end(), key.begin(), key.end());
117 txtRecord.emplace_back(MDNS_TXT_KV_EQ);
118 txtRecord.insert(txtRecord.end(), value.begin(), value.end());
119 }
120 }
121
122 } // namespace NetManagerStandard
123 } // namespace OHOS