• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 #include <string>
16 #include "netmanager_base_common_utils.h"
17 #include "netsys_controller.h"
18 #include "net_manager_constants.h"
19 #include "wearable_distributed_net_link_info.h"
20 
21 namespace OHOS {
22 namespace NetManagerStandard {
23 namespace {
24 const std::string DNSLISTS_FIRST = "dnslistsfirst";
25 const std::string DNSLISTS_SECOND = "dnslistssecond";
26 const std::string IFACENAME = "ifacename";
27 const std::string DEFAULT_NET_MASK = "defaultnetmask";
28 const std::string NET_IFACE_ADDRESS = "netifaceaddress";
29 const std::string IPV4_DEFAULT_ROUTE_ADDR = "ipv4defaultrouteaddr";
30 const std::string DUMMY_ADDRESS = "dummyaddress";
31 const std::string IPV4_ADDR_NET_MASK = "ipv4addrnetmask";
32 const std::string ROUTE_DESTINATION_ADDR = "routedestinationaddr";
33 const std::string INTERFACE_DUMMY = "dummy0";
34 } // namespace
35 
GetIfaceName()36 std::string WearableDistributedNetLinkInfo::GetIfaceName()
37 {
38     return ifaceName_;
39 }
40 
GetPrimaryDnsLists()41 std::string WearableDistributedNetLinkInfo::GetPrimaryDnsLists()
42 {
43     return primaryDnsLists_;
44 }
45 
GetSecondDnsLists()46 std::string WearableDistributedNetLinkInfo::GetSecondDnsLists()
47 {
48     return secondDnsLists_;
49 }
50 
GetDefaultNetMask()51 std::string WearableDistributedNetLinkInfo::GetDefaultNetMask()
52 {
53     return defaultNetMask_;
54 }
55 
GetNetIfaceAddress()56 std::string WearableDistributedNetLinkInfo::GetNetIfaceAddress()
57 {
58     return netIfaceAddress_;
59 }
60 
GetIpv4DeRouteAddr()61 std::string WearableDistributedNetLinkInfo::GetIpv4DeRouteAddr()
62 {
63     return ipv4DeRouteAddr_;
64 }
65 
GetDummyAddress()66 std::string WearableDistributedNetLinkInfo::GetDummyAddress()
67 {
68     return dummyAddress_;
69 }
70 
GetIpv4AddrNetMask()71 std::string WearableDistributedNetLinkInfo::GetIpv4AddrNetMask()
72 {
73     return ipv4AddrNetMask_;
74 }
75 
GetRouteEstinationAddr()76 std::string WearableDistributedNetLinkInfo::GetRouteEstinationAddr()
77 {
78     return routeDestinationAddr_;
79 }
80 
ParseDnsLists(const cJSON & json)81 bool WearableDistributedNetLinkInfo::ParseDnsLists(const cJSON &json)
82 {
83     cJSON *dnsListsFirst = cJSON_GetObjectItemCaseSensitive(&json, DNSLISTS_FIRST.c_str());
84     cJSON *dnsListsSecond = cJSON_GetObjectItemCaseSensitive(&json, DNSLISTS_SECOND.c_str());
85     if (dnsListsFirst == nullptr || dnsListsSecond == nullptr) {
86         NETMGR_EXT_LOG_E("Failed to find dnslists information!");
87         return false;
88     }
89     primaryDnsLists_ = cJSON_GetStringValue(dnsListsFirst);
90     secondDnsLists_ = cJSON_GetStringValue(dnsListsSecond);
91     return true;
92 }
93 
ParseIfaceName(const cJSON & json)94 bool WearableDistributedNetLinkInfo::ParseIfaceName(const cJSON &json)
95 {
96     cJSON *ifaceNameItem = cJSON_GetObjectItemCaseSensitive(&json, IFACENAME.c_str());
97     if (ifaceNameItem == nullptr) {
98         NETMGR_EXT_LOG_E("Failed to find ifacename information!");
99         return false;
100     }
101     ifaceName_ = cJSON_GetStringValue(ifaceNameItem);
102     return true;
103 }
104 
ParseDefaultNetMask(const cJSON & json)105 bool WearableDistributedNetLinkInfo::ParseDefaultNetMask(const cJSON &json)
106 {
107     cJSON *defaultNetmaskItem = cJSON_GetObjectItemCaseSensitive(&json, DEFAULT_NET_MASK.c_str());
108     if (defaultNetmaskItem == nullptr) {
109         NETMGR_EXT_LOG_E("Failed to find defaultNetMask information!");
110         return false;
111     }
112     defaultNetMask_ = cJSON_GetStringValue(defaultNetmaskItem);
113     return true;
114 }
115 
ParseNetIfaceAddress(const cJSON & json)116 bool WearableDistributedNetLinkInfo::ParseNetIfaceAddress(const cJSON &json)
117 {
118     cJSON *networkInterfaceAddressItem = cJSON_GetObjectItemCaseSensitive(&json, NET_IFACE_ADDRESS.c_str());
119     if (networkInterfaceAddressItem == nullptr) {
120         NETMGR_EXT_LOG_E("Failed to find netifaceaddress information!");
121         return false;
122     }
123     netIfaceAddress_ = cJSON_GetStringValue(networkInterfaceAddressItem);
124     return true;
125 }
126 
ParseIpv4DeRouteAddr(const cJSON & json)127 bool WearableDistributedNetLinkInfo::ParseIpv4DeRouteAddr(const cJSON &json)
128 {
129     cJSON *ipv4DefaultRouteAddressItem = cJSON_GetObjectItemCaseSensitive(&json, IPV4_DEFAULT_ROUTE_ADDR.c_str());
130     if (ipv4DefaultRouteAddressItem == nullptr) {
131         NETMGR_EXT_LOG_E("Failed to find ipv4derouteaddr information!");
132         return false;
133     }
134     ipv4DeRouteAddr_ = cJSON_GetStringValue(ipv4DefaultRouteAddressItem);
135     return true;
136 }
137 
ParseDummyAddress(const cJSON & json)138 bool WearableDistributedNetLinkInfo::ParseDummyAddress(const cJSON &json)
139 {
140     cJSON *dummyAddressItem = cJSON_GetObjectItemCaseSensitive(&json, DUMMY_ADDRESS.c_str());
141     if (dummyAddressItem == nullptr) {
142         NETMGR_EXT_LOG_E("Failed to find dummyaddress information!");
143         return false;
144     }
145     dummyAddress_ = cJSON_GetStringValue(dummyAddressItem);
146     return true;
147 }
148 
ParseIpv4AddrNetMask(const cJSON & json)149 bool WearableDistributedNetLinkInfo::ParseIpv4AddrNetMask(const cJSON &json)
150 {
151     cJSON *ipv4AddressNetMaskItem = cJSON_GetObjectItemCaseSensitive(&json, IPV4_ADDR_NET_MASK.c_str());
152     if (ipv4AddressNetMaskItem == nullptr) {
153         NETMGR_EXT_LOG_E("Failed to find ipv4addrnetmask information!");
154         return false;
155     }
156     ipv4AddrNetMask_ = cJSON_GetStringValue(ipv4AddressNetMaskItem);
157     return true;
158 }
159 
ParseRouteDestinationAddr(const cJSON & json)160 bool WearableDistributedNetLinkInfo::ParseRouteDestinationAddr(const cJSON &json)
161 {
162     cJSON *routeDestinationAddrItem = cJSON_GetObjectItemCaseSensitive(&json, ROUTE_DESTINATION_ADDR.c_str());
163     if (routeDestinationAddrItem == nullptr) {
164         NETMGR_EXT_LOG_E("Failed to find routedestinationaddr information!");
165         return false;
166     }
167     routeDestinationAddr_ = cJSON_GetStringValue(routeDestinationAddrItem);
168     return true;
169 }
170 
ReadJsonFile()171 std::string WearableDistributedNetLinkInfo::ReadJsonFile()
172 {
173     std::ifstream infile;
174     std::string lineConfigInfo;
175     std::string allConfigInfo;
176     infile.open(configPath_);
177     if (!infile.is_open()) {
178         NETMGR_EXT_LOG_E("ReadJsonFile filePath failed");
179         return allConfigInfo;
180     }
181     while (getline(infile, lineConfigInfo)) {
182         allConfigInfo.append(lineConfigInfo);
183     }
184     infile.close();
185     return allConfigInfo;
186 }
187 
ReadNetlinkinfoInterfaces(const cJSON & json)188 bool WearableDistributedNetLinkInfo::ReadNetlinkinfoInterfaces(const cJSON &json)
189 {
190     if (!ParseDnsLists(json)) {
191         NETMGR_EXT_LOG_E("ParseDnsLists failed");
192         return false;
193     }
194     if (!ParseIfaceName(json)) {
195         NETMGR_EXT_LOG_E("ParseIfaceName failed");
196         return false;
197     }
198     if (!ParseDefaultNetMask(json)) {
199         NETMGR_EXT_LOG_E("ParseDefaultNetMask failed");
200         return false;
201     }
202     if (!ParseNetIfaceAddress(json)) {
203         NETMGR_EXT_LOG_E("ParseNetIfaceAddress failed");
204         return false;
205     }
206     if (!ParseIpv4DeRouteAddr(json)) {
207         NETMGR_EXT_LOG_E("ParseIpv4DeRouteAddr failed");
208         return false;
209     }
210     if (!ParseDummyAddress(json)) {
211         NETMGR_EXT_LOG_E("ParseDummyAddress failed");
212         return false;
213     }
214     if (!ParseIpv4AddrNetMask(json)) {
215         NETMGR_EXT_LOG_E("ParseIpv4AddrNetMask failed");
216         return false;
217     }
218     if (!ParseRouteDestinationAddr(json)) {
219         NETMGR_EXT_LOG_E("ParseRouteDestinationAddr failed");
220         return false;
221     }
222     return true;
223 }
224 
ReadSystemNetlinkinfoConfiguration()225 bool WearableDistributedNetLinkInfo::ReadSystemNetlinkinfoConfiguration()
226 {
227     const auto &jsonStr = ReadJsonFile();
228     if (jsonStr.length() == 0) {
229         NETMGR_EXT_LOG_E("ReadConfigData config file is return empty!");
230         return false;
231     }
232     cJSON *json = cJSON_Parse(jsonStr.c_str());
233     if (json == nullptr) {
234         NETMGR_EXT_LOG_E("json parse failed!");
235         return false;
236     }
237     bool result = ReadNetlinkinfoInterfaces(*json);
238     if (result == false) {
239         NETMGR_EXT_LOG_E("parse files failed!");
240         cJSON_Delete(json);
241         return false;
242     }
243     cJSON_Delete(json);
244     return true;
245 }
246 
SetInterFaceName(NetLinkInfo & linkInfo)247 void WearableDistributedNetLinkInfo::SetInterFaceName(NetLinkInfo &linkInfo)
248 {
249     if (!GetIfaceName().empty()) {
250         linkInfo.ifaceName_ = GetIfaceName();
251     }
252 }
253 
SetDnsLists(NetLinkInfo & linkInfo)254 int32_t WearableDistributedNetLinkInfo::SetDnsLists(NetLinkInfo &linkInfo)
255 {
256     INetAddr dnsFirst;
257     INetAddr dnsSecond;
258     dnsFirst.type_ = INetAddr::IPV4;
259     dnsFirst.family_ = AF_INET;
260     dnsFirst.address_ = GetPrimaryDnsLists();
261     linkInfo.dnsList_.push_back(dnsFirst);
262 
263     dnsSecond.type_ = INetAddr::IPV4;
264     dnsSecond.family_ = AF_INET;
265     dnsSecond.address_ = GetSecondDnsLists();
266     linkInfo.dnsList_.push_back(dnsSecond);
267     return NETMANAGER_EXT_SUCCESS;
268 }
269 
SetNetLinkIPInfo(NetLinkInfo & linkInfo)270 int32_t WearableDistributedNetLinkInfo::SetNetLinkIPInfo(NetLinkInfo &linkInfo)
271 {
272     INetAddr netAddr;
273     netAddr.type_ = INetAddr::IPV4;
274     netAddr.family_ = AF_INET;
275     netAddr.address_ = GetIpv4DeRouteAddr();
276     netAddr.netMask_ = GetDefaultNetMask();
277     netAddr.prefixlen_ = CommonUtils::GetMaskLength(GetDefaultNetMask());
278 
279     linkInfo.netAddrList_.push_back(netAddr);
280     return NETMANAGER_EXT_SUCCESS;
281 }
282 
SetNetLinkRouteInfo(NetLinkInfo & linkInfo)283 int32_t WearableDistributedNetLinkInfo::SetNetLinkRouteInfo(NetLinkInfo &linkInfo)
284 {
285     Route route;
286     route.iface_ = GetIfaceName();
287     route.destination_.type_ = INetAddr::IPV4;
288     route.destination_.family_ = AF_INET;
289     route.destination_.address_ = GetRouteEstinationAddr();
290     route.gateway_.address_ = GetNetIfaceAddress();
291     route.gateway_.family_ = AF_INET;
292 
293     linkInfo.routeList_.push_back(route);
294     return NETMANAGER_EXT_SUCCESS;
295 }
296 
SetMtu(NetLinkInfo & linkInfo)297 void WearableDistributedNetLinkInfo::SetMtu(NetLinkInfo &linkInfo)
298 {
299     linkInfo.mtu_ = CONSTANTS::WEARABLE_DISTRIBUTED_NET_MTU;
300 }
301 
SetInterfaceDummyDown()302 int32_t WearableDistributedNetLinkInfo::SetInterfaceDummyDown()
303 {
304     std::string addr = GetDummyAddress();
305     auto prefixLen = CommonUtils::GetMaskLength(GetIpv4AddrNetMask());
306     if (NetsysController::GetInstance().DelInterfaceAddress(INTERFACE_DUMMY.c_str(), addr, prefixLen) != 0) {
307         NETMGR_EXT_LOG_E("Failed delete dummy0 address");
308         return NETMANAGER_EXT_ERR_INTERNAL;
309     }
310     return NETMANAGER_EXT_SUCCESS;
311 }
312 
SetInterfaceDummyDown()313 int32_t SetInterfaceDummyDown()
314 {
315     WearableDistributedNetLinkInfo info;
316     if (!info.ReadSystemNetlinkinfoConfiguration()) {
317         NETMGR_EXT_LOG_E("ReadSystemNetlinkinfoConfiguration failed");
318         return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
319     }
320     int32_t result = info.SetInterfaceDummyDown();
321     if (result != NETMANAGER_EXT_SUCCESS) {
322         NETMGR_EXT_LOG_E("SetInterfaceDummyDown failed, result: [%{public}d]", result);
323     }
324     return result;
325 }
326 
327 // Add and pull up interface dummy0
SetInterfaceDummyUp()328 int32_t WearableDistributedNetLinkInfo::SetInterfaceDummyUp()
329 {
330     std::string addr = GetDummyAddress();
331     auto prefixLen = CommonUtils::GetMaskLength(GetIpv4AddrNetMask());
332     if (NetsysController::GetInstance().AddInterfaceAddress(INTERFACE_DUMMY.c_str(), addr, prefixLen) != 0) {
333         NETMGR_EXT_LOG_E("Failed setting dummy0 address");
334         return NETMANAGER_EXT_ERR_INTERNAL;
335     }
336     if (NetsysController::GetInstance().SetInterfaceUp(INTERFACE_DUMMY.c_str()) != 0) {
337         NETMGR_EXT_LOG_E("Failed setting dummy0 interface up");
338         return NETMANAGER_EXT_ERR_INTERNAL;
339     }
340     return NETMANAGER_EXT_SUCCESS;
341 }
342 
CreateNetLinkInfo(NetLinkInfo & linkInfo)343 int32_t CreateNetLinkInfo(NetLinkInfo &linkInfo)
344 {
345     WearableDistributedNetLinkInfo info;
346     if (!info.ReadSystemNetlinkinfoConfiguration()) {
347         NETMGR_EXT_LOG_E("ReadSystemNetlinkinfoConfiguration failed");
348         return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
349     }
350     info.SetNetLinkIPInfo(linkInfo);
351     info.SetInterFaceName(linkInfo);
352     info.SetDnsLists(linkInfo);
353     info.SetNetLinkRouteInfo(linkInfo);
354     info.SetMtu(linkInfo);
355     return NETMANAGER_EXT_SUCCESS;
356 }
357 
SetInterfaceDummyUp()358 int32_t SetInterfaceDummyUp()
359 {
360     WearableDistributedNetLinkInfo info;
361     if (!info.ReadSystemNetlinkinfoConfiguration()) {
362         NETMGR_EXT_LOG_E("ReadSystemNetlinkinfoConfiguration failed");
363         return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
364     }
365     int32_t result = info.SetInterfaceDummyUp();
366     if (result != NETMANAGER_EXT_SUCCESS) {
367         NETMGR_EXT_LOG_E("SetInterfaceDummyUp failed, result: [%{public}d]", result);
368     }
369     return result;
370 }
371 } // namespace NetManagerStandard
372 } // namespace OHOS
373