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.address_ = GetSecondDnsLists();
264     linkInfo.dnsList_.push_back(dnsSecond);
265     return NETMANAGER_EXT_SUCCESS;
266 }
267 
SetNetLinkIPInfo(NetLinkInfo &linkInfo)268 int32_t WearableDistributedNetLinkInfo::SetNetLinkIPInfo(NetLinkInfo &linkInfo)
269 {
270     INetAddr netAddr;
271     netAddr.type_ = INetAddr::IPV4;
272     netAddr.family_ = AF_INET;
273     netAddr.address_ = GetIpv4DeRouteAddr();
274     netAddr.netMask_ = GetDefaultNetMask();
275     netAddr.prefixlen_ = CommonUtils::GetMaskLength(GetDefaultNetMask());
276 
277     linkInfo.netAddrList_.push_back(*netAddr);
278     return NETMANAGER_EXT_SUCCESS;
279 }
280 
SetNetLinkRouteInfo(NetLinkInfo &linkInfo)281 int32_t WearableDistributedNetLinkInfo::SetNetLinkRouteInfo(NetLinkInfo &linkInfo)
282 {
283     Route route;
284     route.iface_ = GetIfaceName();
285     route.destination_.type_ = INetAddr::IPV4;
286     route.destination_.family_ = AF_INET;
287     route.destination_.address_ = GetRouteEstinationAddr();
288     route.gateway_.address_ = GetNetIfaceAddress();
289     route.gateway_.family_ = AF_INET;
290 
291     linkInfo.routeList_.push_back(*route);
292     return NETMANAGER_EXT_SUCCESS;
293 }
294 
SetMtu(NetLinkInfo &linkInfo)295 void WearableDistributedNetLinkInfo::SetMtu(NetLinkInfo &linkInfo)
296 {
297     linkInfo.mtu_ = CONSTANTS::WEARABLE_DISTRIBUTED_NET_MTU;
298 }
299 
SetInterfaceDummyDown()300 int32_t SetInterfaceDummyDown()
301 {
302     if (NetsysController::GetInstance().SetInterfaceDown(INTERFACE_DUMMY.c_str()) != 0) {
303         NETMGR_EXT_LOG_E("Failed setting dummy0 interface down");
304         return NETMANAGER_EXT_ERR_INTERNAL;
305     }
306     return NETMANAGER_EXT_SUCCESS;
307 }
308 
309 // Add and pull up interface dummy0
SetInterfaceDummyUp()310 int32_t WearableDistributedNetLinkInfo::SetInterfaceDummyUp()
311 {
312     std::string addr = GetDummyAddress();
313     auto prefixLen = CommonUtils::GetMaskLength(GetIpv4AddrNetMask());
314     if (NetsysController::GetInstance().AddInterfaceAddress(INTERFACE_DUMMY.c_str(), addr, prefixLen) != 0) {
315         NETMGR_EXT_LOG_E("Failed setting dummy0 address");
316         return NETMANAGER_EXT_ERR_INTERNAL;
317     }
318     if (NetsysController::GetInstance().SetInterfaceUp(INTERFACE_DUMMY.c_str()) != 0) {
319         NETMGR_EXT_LOG_E("Failed setting dummy0 interface up");
320         return NETMANAGER_EXT_ERR_INTERNAL;
321     }
322     return NETMANAGER_EXT_SUCCESS;
323 }
324 
CreateNetLinkInfo(NetLinkInfo &linkInfo)325 int32_t CreateNetLinkInfo(NetLinkInfo &linkInfo)
326 {
327     WearableDistributedNetLinkInfo info;
328     if (!info.ReadSystemNetlinkinfoConfiguration()) {
329         NETMGR_EXT_LOG_E("ReadSystemNetlinkinfoConfiguration failed");
330         return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
331     }
332     info.SetNetLinkIPInfo(linkInfo);
333     info.SetInterFaceName(linkInfo);
334     info.SetDnsLists(linkInfo);
335     info.SetNetLinkRouteInfo(linkInfo);
336     info.SetMtu(linkInfo);
337     int32_t result = info.SetInterfaceDummyUp();
338     if (result != NETMANAGER_EXT_SUCCESS) {
339         NETMGR_EXT_LOG_E("CreateNetLinkInfo SetInterfaceDummyUp failed, result: [%{public}d]", result);
340     }
341     return result;
342 }
343 
344 } // namespace NetManagerStandard
345 } // namespace OHOS
346