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