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