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 = UnmarshallingVpnConfig(parcel, ptr);
83     return allOK ? ptr : nullptr;
84 }
85 
UnmarshallingVpnConfig(Parcel & parcel,sptr<VpnConfig> ptr)86 bool VpnConfig::UnmarshallingVpnConfig(Parcel &parcel, sptr<VpnConfig> ptr)
87 {
88     if (ptr == nullptr) {
89         NETMGR_EXT_LOG_E("VpnConfig ptr is null");
90         return false;
91     }
92     bool allOK = UnmarshallingAddrRoute(parcel, ptr) && parcel.ReadInt32(ptr->mtu_) &&
93                  parcel.ReadBool(ptr->isAcceptIPv4_) && parcel.ReadBool(ptr->isAcceptIPv6_) &&
94                  parcel.ReadBool(ptr->isLegacy_) && parcel.ReadBool(ptr->isMetered_) &&
95                  parcel.ReadBool(ptr->isBlocking_) && UnmarshallingVectorString(parcel, ptr->dnsAddresses_) &&
96                  UnmarshallingVectorString(parcel, ptr->searchDomains_) &&
97                  UnmarshallingVectorString(parcel, ptr->acceptedApplications_) &&
98                  UnmarshallingVectorString(parcel, ptr->refusedApplications_);
99     return allOK;
100 }
101 
UnmarshallingAddrRoute(Parcel & parcel,sptr<VpnConfig> & config)102 bool VpnConfig::UnmarshallingAddrRoute(Parcel &parcel, sptr<VpnConfig> &config)
103 {
104     int32_t addrSize = 0;
105     if (!parcel.ReadInt32(addrSize)) {
106         return false;
107     }
108     if (static_cast<uint32_t>(addrSize) > MAX_SIZE) {
109         NETMGR_EXT_LOG_E("addrSize=[%{public}d] is too large", addrSize);
110         return false;
111     }
112     for (int32_t idx = 0; idx < addrSize; idx++) {
113         sptr<INetAddr> address = INetAddr::Unmarshalling(parcel);
114         if (address == nullptr) {
115             NETMGR_EXT_LOG_E("address is null");
116             return false;
117         }
118         config->addresses_.push_back(*address);
119     }
120 
121     int32_t routeSize = 0;
122     if (!parcel.ReadInt32(routeSize)) {
123         return false;
124     }
125     if (static_cast<uint32_t>(routeSize) > MAX_SIZE) {
126         NETMGR_EXT_LOG_E("routeSize=[%{public}d] is too large", routeSize);
127         return false;
128     }
129     for (int32_t idx = 0; idx < routeSize; idx++) {
130         sptr<Route> route = Route::Unmarshalling(parcel);
131         if (route == nullptr) {
132             NETMGR_EXT_LOG_E("route is null");
133             return false;
134         }
135         config->routes_.push_back(*route);
136     }
137     return true;
138 }
139 
UnmarshallingVectorString(Parcel & parcel,std::vector<std::string> & vec)140 bool VpnConfig::UnmarshallingVectorString(Parcel &parcel, std::vector<std::string> &vec)
141 {
142     int32_t size = 0;
143     if (!parcel.ReadInt32(size)) {
144         return false;
145     }
146     if (static_cast<uint32_t>(size) > MAX_SIZE) {
147         NETMGR_EXT_LOG_E("size = [%{public}d] is too large", size);
148         return false;
149     }
150     for (int32_t idx = 0; idx < size; idx++) {
151         std::string elem;
152         if (!parcel.ReadString(elem)) {
153             return false;
154         }
155         vec.push_back(elem);
156     }
157     return true;
158 }
159 
160 } // namespace NetManagerStandard
161 } // namespace OHOS
162