1 /*
2 * Copyright (c) 2021-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 "static_configuration.h"
17
18 #include "inet_addr.h"
19 #include "netmanager_base_common_utils.h"
20 #include "netmgr_ext_log_wrapper.h"
21
22 namespace OHOS {
23 namespace NetManagerStandard {
24 namespace {
25 constexpr uint32_t MAX_DNS_SIZE = 10;
26 constexpr uint32_t MAX_ADDR_SIZE = 2;
27
28 constexpr const char *SEPARATOR = ",";
29 } // namespace
30
Marshalling(Parcel & parcel) const31 bool StaticConfiguration::Marshalling(Parcel &parcel) const
32 {
33 return MarshallingNetAddressList(ipAddrList_, MAX_ADDR_SIZE, parcel) &&
34 MarshallingNetAddressList(routeList_, MAX_ADDR_SIZE, parcel) &&
35 MarshallingNetAddressList(gatewayList_, MAX_ADDR_SIZE, parcel) &&
36 MarshallingNetAddressList(netMaskList_, MAX_ADDR_SIZE, parcel) &&
37 MarshallingNetAddressList(dnsServers_, MAX_DNS_SIZE, parcel) && parcel.WriteString(domain_);
38 }
39
MarshallingNetAddressList(const std::vector<INetAddr> & netAddrList,uint32_t maxSize,Parcel & parcel) const40 bool StaticConfiguration::MarshallingNetAddressList(const std::vector<INetAddr> &netAddrList, uint32_t maxSize,
41 Parcel &parcel) const
42 {
43 uint32_t size = static_cast<uint32_t>(std::min(maxSize, static_cast<uint32_t>(netAddrList.size())));
44 if (!parcel.WriteUint32(size)) {
45 NETMGR_EXT_LOG_E("write netAddrList size to parcel failed");
46 return false;
47 }
48
49 for (uint32_t index = 0; index < size; ++index) {
50 auto netAddr = netAddrList[index];
51 if (!netAddr.Marshalling(parcel)) {
52 NETMGR_EXT_LOG_E("write INetAddr to parcel failed");
53 return false;
54 }
55 }
56 return true;
57 }
58
Unmarshalling(Parcel & parcel)59 sptr<StaticConfiguration> StaticConfiguration::Unmarshalling(Parcel &parcel)
60 {
61 sptr<StaticConfiguration> ptr = new (std::nothrow) StaticConfiguration();
62 if (ptr == nullptr) {
63 NETMGR_EXT_LOG_E("ptr new failed");
64 return nullptr;
65 }
66
67 bool ret = UnmarshallingNetAddressList(parcel, ptr->ipAddrList_, MAX_ADDR_SIZE) &&
68 UnmarshallingNetAddressList(parcel, ptr->routeList_, MAX_ADDR_SIZE) &&
69 UnmarshallingNetAddressList(parcel, ptr->gatewayList_, MAX_ADDR_SIZE) &&
70 UnmarshallingNetAddressList(parcel, ptr->netMaskList_, MAX_ADDR_SIZE) &&
71 UnmarshallingNetAddressList(parcel, ptr->dnsServers_, MAX_DNS_SIZE) && parcel.ReadString(ptr->domain_);
72 return ret ? ptr : nullptr;
73 }
74
UnmarshallingNetAddressList(Parcel & parcel,std::vector<INetAddr> & netAddrList,uint32_t maxSize)75 bool StaticConfiguration::UnmarshallingNetAddressList(Parcel &parcel, std::vector<INetAddr> &netAddrList,
76 uint32_t maxSize)
77 {
78 std::vector<INetAddr>().swap(netAddrList);
79
80 uint32_t size = 0;
81 if (!parcel.ReadUint32(size)) {
82 NETMGR_EXT_LOG_E("Read INetAddr list size failed");
83 return false;
84 }
85 size = (size > maxSize) ? maxSize : size;
86 for (uint32_t i = 0; i < size; i++) {
87 auto netAddr = INetAddr::Unmarshalling(parcel);
88 if (netAddr == nullptr) {
89 return false;
90 }
91 netAddrList.push_back(*netAddr);
92 }
93 return true;
94 }
95
ExtractNetAddrBySeparator(const std::string & input,std::vector<INetAddr> & netAddrList)96 void StaticConfiguration::ExtractNetAddrBySeparator(const std::string &input, std::vector<INetAddr> &netAddrList)
97 {
98 std::vector<INetAddr>().swap(netAddrList);
99 for (const auto &netAddr : CommonUtils::Split(input, SEPARATOR)) {
100 INetAddr addr;
101 addr.address_ = netAddr;
102 netAddrList.push_back(addr);
103 }
104 }
105 } // namespace NetManagerStandard
106 } // namespace OHOS