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 "napi_utils.h"
17 #include "netmanager_ext_log.h"
18 
19 #include "constant.h"
20 #include "mdns_base_context.h"
21 #include "mdns_common.h"
22 
23 namespace OHOS::NetManagerStandard {
MDnsBaseContext(napi_env env,EventManager * manager)24 MDnsBaseContext::MDnsBaseContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {}
25 
ParseAddressObj(napi_env env,napi_value obj)26 void MDnsBaseContext::ParseAddressObj(napi_env env, napi_value obj)
27 {
28     if (NapiUtils::GetValueType(env, obj) == napi_object) {
29         if (!NapiUtils::HasNamedProperty(env, obj, SERVICEINFO_ADDRESS)) {
30             serviceInfo_.addr = "";
31         } else {
32             serviceInfo_.addr = NapiUtils::GetStringPropertyUtf8(env, obj, SERVICEINFO_ADDRESS);
33         }
34 
35         if (!NapiUtils::HasNamedProperty(env, obj, SERVICEINFO_FAMILY)) {
36             serviceInfo_.family = MDnsServiceInfo::UNKNOWN;
37             return;
38         }
39         int32_t family = NapiUtils::GetInt32Property(env, obj, SERVICEINFO_FAMILY);
40         if (family == 0) {
41             serviceInfo_.family = MDnsServiceInfo::IPV6;
42         } else if (family == 1) {
43             serviceInfo_.family = MDnsServiceInfo::IPV4;
44         }
45     } else {
46         NETMANAGER_EXT_LOGE("host is not napi_object");
47     }
48 }
49 
GetAttributeObj(napi_env env,napi_value obj,uint32_t & len)50 bool MDnsBaseContext::GetAttributeObj(napi_env env, napi_value obj, uint32_t &len)
51 {
52     if (!NapiUtils::IsArray(env, obj)) {
53         return false;
54     }
55     len = NapiUtils::GetArrayLength(env, obj);
56     return true;
57 }
58 
ParseAttributeObj(napi_env env,napi_value obj,TxtRecord & attrMap)59 void MDnsBaseContext::ParseAttributeObj(napi_env env, napi_value obj, TxtRecord &attrMap)
60 {
61     uint32_t arrLen = 0;
62     if (!GetAttributeObj(env, obj, arrLen)) {
63         NETMANAGER_EXT_LOGE("GetAttributeObj failed");
64         return;
65     }
66 
67     for (size_t i = 0; i < arrLen; i++) {
68         napi_value svrAttr = NapiUtils::GetArrayElement(env, obj, i);
69         std::string key;
70         if (NapiUtils::GetValueType(env, svrAttr) != napi_object) {
71             NETMANAGER_EXT_LOGE("svrAttr is not napi_object");
72             continue;
73         }
74         if (NapiUtils::HasNamedProperty(env, svrAttr, SERVICEINFO_ATTR_KEY)) {
75             key = NapiUtils::GetStringPropertyUtf8(env, svrAttr, SERVICEINFO_ATTR_KEY);
76         }
77         if (!NapiUtils::HasNamedProperty(env, svrAttr, SERVICEINFO_ATTR_VALUE)) {
78             NETMANAGER_EXT_LOGE("SERVICEINFO_ATTR_VALUE is not exist");
79             continue;
80         }
81         napi_value valueObj = NapiUtils::GetNamedProperty(env, svrAttr, SERVICEINFO_ATTR_VALUE);
82         uint32_t valArrLen = 0;
83         if (!GetAttributeObj(env, valueObj, valArrLen)) {
84             NETMANAGER_EXT_LOGE("GetAttributeObj failed");
85             continue;
86         }
87         std::vector<uint8_t> typeArray;
88         for (size_t j = 0; j < valArrLen; j++) {
89             napi_value valAttr = NapiUtils::GetArrayElement(env, valueObj, j);
90             if (NapiUtils::GetValueType(env, valAttr) != napi_number) {
91                 NETMANAGER_EXT_LOGE("valAttr is not napi_number");
92                 continue;
93             }
94             typeArray.push_back(static_cast<uint8_t>(NapiUtils::GetInt32FromValue(env, valAttr)));
95         }
96         if (typeArray.size() > 0) {
97             attrMap[key] = typeArray;
98         }
99     }
100 }
101 
GetContextIdString(napi_env env,napi_value obj)102 std::string MDnsBaseContext::GetContextIdString(napi_env env, napi_value obj)
103 {
104     if (NapiUtils::HasNamedProperty(env, obj, CONTEXT_ATTR_APPINFO)) {
105         napi_value info = NapiUtils::GetNamedProperty(env, obj, CONTEXT_ATTR_APPINFO);
106         if (NapiUtils::HasNamedProperty(env, info, APPINFO_ATTR_NAME)) {
107             return NapiUtils::GetStringPropertyUtf8(env, info, APPINFO_ATTR_NAME);
108         }
109     }
110     return std::string();
111 }
112 
ParseServiceInfo(napi_env env,napi_value value)113 void MDnsBaseContext::ParseServiceInfo(napi_env env, napi_value value)
114 {
115     if (NapiUtils::HasNamedProperty(env, value, SERVICEINFO_TYPE)) {
116         serviceInfo_.type = NapiUtils::GetStringPropertyUtf8(env, value, SERVICEINFO_TYPE);
117     }
118     if (NapiUtils::HasNamedProperty(env, value, SERVICEINFO_NAME)) {
119         serviceInfo_.name = NapiUtils::GetStringPropertyUtf8(env, value, SERVICEINFO_NAME);
120     }
121     if (NapiUtils::HasNamedProperty(env, value, SERVICEINFO_PORT)) {
122         serviceInfo_.port = NapiUtils::GetInt32Property(env, value, SERVICEINFO_PORT);
123     }
124     if (NapiUtils::HasNamedProperty(env, value, SERVICEINFO_HOST)) {
125         napi_value hostObj = NapiUtils::GetNamedProperty(env, value, SERVICEINFO_HOST);
126         ParseAddressObj(env, hostObj);
127     }
128     if (NapiUtils::HasNamedProperty(env, value, SERVICEINFO_ATTR)) {
129         napi_value attrObj = NapiUtils::GetNamedProperty(env, value, SERVICEINFO_ATTR);
130         TxtRecord attrMap;
131         ParseAttributeObj(env, attrObj, attrMap);
132         serviceInfo_.SetAttrMap(attrMap);
133     }
134 }
135 
GetServiceInfo()136 MDnsServiceInfo &MDnsBaseContext::GetServiceInfo()
137 {
138     return serviceInfo_;
139 }
140 
SetServiceInfo(const MDnsServiceInfo & info)141 void MDnsBaseContext::SetServiceInfo(const MDnsServiceInfo &info)
142 {
143     serviceInfo_ = info;
144 }
145 } // namespace OHOS::NetManagerStandard
146