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 "vpn_config.h"
17 #include "netmgr_ext_log_wrapper.h"
18
19 namespace OHOS {
20 namespace NetManagerStandard {
21 namespace {
22 constexpr uint32_t MAX_SIZE = 64;
23 }
Marshalling(Parcel & parcel) const24 bool VpnConfig::Marshalling(Parcel &parcel) const
25 {
26 bool allOK = MarshallingAddrRoute(parcel) && parcel.WriteInt32(mtu_) && parcel.WriteBool(isAcceptIPv4_) &&
27 parcel.WriteBool(isAcceptIPv6_) && parcel.WriteBool(isLegacy_) && parcel.WriteBool(isMetered_) &&
28 parcel.WriteBool(isBlocking_) && MarshallingVectorString(parcel, dnsAddresses_) &&
29 MarshallingVectorString(parcel, searchDomains_) &&
30 MarshallingVectorString(parcel, acceptedApplications_) &&
31 MarshallingVectorString(parcel, refusedApplications_);
32 return allOK;
33 }
34
MarshallingAddrRoute(Parcel & parcel) const35 bool VpnConfig::MarshallingAddrRoute(Parcel &parcel) const
36 {
37 int32_t addrSize = static_cast<int32_t>(addresses_.size());
38 if (!parcel.WriteInt32(addrSize)) {
39 return false;
40 }
41 for (auto addr : addresses_) {
42 if (!addr.Marshalling(parcel)) {
43 return false;
44 }
45 }
46
47 int32_t routeSize = static_cast<int32_t>(routes_.size());
48 if (!parcel.WriteInt32(routeSize)) {
49 return false;
50 }
51
52 for (auto route : routes_) {
53 if (!route.Marshalling(parcel)) {
54 return false;
55 }
56 }
57 return true;
58 }
59
MarshallingVectorString(Parcel & parcel,const std::vector<std::string> & vec) const60 bool VpnConfig::MarshallingVectorString(Parcel &parcel, const std::vector<std::string> &vec) const
61 {
62 int32_t size = static_cast<int32_t>(vec.size());
63 if (!parcel.WriteInt32(size)) {
64 return false;
65 }
66 for (auto &elem : vec) {
67 if (!parcel.WriteString(elem)) {
68 return false;
69 }
70 }
71 return true;
72 }
73
Unmarshalling(Parcel & parcel)74 sptr<VpnConfig> VpnConfig::Unmarshalling(Parcel &parcel)
75 {
76 sptr<VpnConfig> ptr = new (std::nothrow) VpnConfig();
77 if (ptr == nullptr) {
78 NETMGR_EXT_LOG_E("ptr is null");
79 return nullptr;
80 }
81
82 bool allOK = UnmarshallingAddrRoute(parcel, ptr) && parcel.ReadInt32(ptr->mtu_) &&
83 parcel.ReadBool(ptr->isAcceptIPv4_) && parcel.ReadBool(ptr->isAcceptIPv6_) &&
84 parcel.ReadBool(ptr->isLegacy_) && parcel.ReadBool(ptr->isMetered_) &&
85 parcel.ReadBool(ptr->isBlocking_) && UnmarshallingVectorString(parcel, ptr->dnsAddresses_) &&
86 UnmarshallingVectorString(parcel, ptr->searchDomains_) &&
87 UnmarshallingVectorString(parcel, ptr->acceptedApplications_) &&
88 UnmarshallingVectorString(parcel, ptr->refusedApplications_);
89 return allOK ? ptr : nullptr;
90 }
91
UnmarshallingAddrRoute(Parcel & parcel,sptr<VpnConfig> & config)92 bool VpnConfig::UnmarshallingAddrRoute(Parcel &parcel, sptr<VpnConfig> &config)
93 {
94 int32_t addrSize = 0;
95 if (!parcel.ReadInt32(addrSize)) {
96 return false;
97 }
98 if (static_cast<uint32_t>(addrSize) > MAX_SIZE) {
99 NETMGR_EXT_LOG_E("addrSize=[%{public}d] is too large", addrSize);
100 return false;
101 }
102 for (int32_t idx = 0; idx < addrSize; idx++) {
103 sptr<INetAddr> address = INetAddr::Unmarshalling(parcel);
104 if (address == nullptr) {
105 NETMGR_EXT_LOG_E("address is null");
106 return false;
107 }
108 config->addresses_.push_back(*address);
109 }
110
111 int32_t routeSize = 0;
112 if (!parcel.ReadInt32(routeSize)) {
113 return false;
114 }
115 if (static_cast<uint32_t>(routeSize) > MAX_SIZE) {
116 NETMGR_EXT_LOG_E("routeSize=[%{public}d] is too large", routeSize);
117 return false;
118 }
119 for (int32_t idx = 0; idx < routeSize; idx++) {
120 sptr<Route> route = Route::Unmarshalling(parcel);
121 if (route == nullptr) {
122 NETMGR_EXT_LOG_E("route is null");
123 return false;
124 }
125 config->routes_.push_back(*route);
126 }
127 return true;
128 }
129
UnmarshallingVectorString(Parcel & parcel,std::vector<std::string> & vec)130 bool VpnConfig::UnmarshallingVectorString(Parcel &parcel, std::vector<std::string> &vec)
131 {
132 int32_t size = 0;
133 if (!parcel.ReadInt32(size)) {
134 return false;
135 }
136 if (static_cast<uint32_t>(size) > MAX_SIZE) {
137 NETMGR_EXT_LOG_E("size = [%{public}d] is too large", size);
138 return false;
139 }
140 for (int32_t idx = 0; idx < size; idx++) {
141 std::string elem;
142 if (!parcel.ReadString(elem)) {
143 return false;
144 }
145 vec.push_back(elem);
146 }
147 return true;
148 }
149
150 } // namespace NetManagerStandard
151 } // namespace OHOS
152