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