• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "route.h"
17 
18 #include "net_mgr_log_wrapper.h"
19 
20 namespace OHOS {
21 namespace NetManagerStandard {
operator ==(const Route & obj) const22 bool Route::operator==(const Route &obj) const
23 {
24     bool out = true;
25     out = out && (iface_ == obj.iface_);
26     out = out && (destination_ == obj.destination_);
27     out = out && (gateway_ == obj.gateway_);
28     out = out && (isExcludedRoute_ == obj.isExcludedRoute_);
29     return out;
30 }
31 
Marshalling(Parcel & parcel) const32 bool Route::Marshalling(Parcel &parcel) const
33 {
34     if (!parcel.WriteString(iface_)) {
35         return false;
36     }
37     if (!destination_.Marshalling(parcel)) {
38         NETMGR_LOG_E("write destination_ to parcel failed");
39         return false;
40     }
41     if (!gateway_.Marshalling(parcel)) {
42         NETMGR_LOG_E("write gateway_ to parcel failed");
43         return false;
44     }
45     if (!parcel.WriteInt32(rtnType_)) {
46         return false;
47     }
48     if (!parcel.WriteInt32(mtu_)) {
49         return false;
50     }
51     if (!parcel.WriteBool(isHost_)) {
52         return false;
53     }
54     if (!parcel.WriteBool(hasGateway_)) {
55         return false;
56     }
57     if (!parcel.WriteBool(isDefaultRoute_)) {
58         return false;
59     }
60     if (!parcel.WriteBool(isExcludedRoute_)) {
61         return false;
62     }
63     return true;
64 }
65 
Unmarshalling(Parcel & parcel)66 sptr<Route> Route::Unmarshalling(Parcel &parcel)
67 {
68     sptr<Route> ptr = new (std::nothrow) Route();
69     if (ptr == nullptr) {
70         NETMGR_LOG_E("make_unique<Route>() failed");
71         return nullptr;
72     }
73     if (!parcel.ReadString(ptr->iface_)) {
74         return nullptr;
75     }
76     sptr<INetAddr> destination = INetAddr::Unmarshalling(parcel);
77     if (destination == nullptr) {
78         NETMGR_LOG_E("read destination from parcel failed");
79         return nullptr;
80     }
81     ptr->destination_ = *destination;
82     sptr<INetAddr> gateway = INetAddr::Unmarshalling(parcel);
83     if (gateway == nullptr) {
84         NETMGR_LOG_E("read gateway from parcel failed");
85         return nullptr;
86     }
87     ptr->gateway_ = *gateway;
88     if (!parcel.ReadInt32(ptr->rtnType_)) {
89         return nullptr;
90     }
91     if (!parcel.ReadInt32(ptr->mtu_)) {
92         return nullptr;
93     }
94     if (!parcel.ReadBool(ptr->isHost_)) {
95         return nullptr;
96     }
97     if (!parcel.ReadBool(ptr->hasGateway_)) {
98         return nullptr;
99     }
100     if (!parcel.ReadBool(ptr->isDefaultRoute_)) {
101         return nullptr;
102     }
103     if (!parcel.ReadBool(ptr->isExcludedRoute_)) {
104         return nullptr;
105     }
106     return ptr;
107 }
108 
Marshalling(Parcel & parcel,const sptr<Route> & object)109 bool Route::Marshalling(Parcel &parcel, const sptr<Route> &object)
110 {
111     if (object == nullptr) {
112         NETMGR_LOG_E("Route object ptr is nullptr");
113         return false;
114     }
115     if (!parcel.WriteString(object->iface_)) {
116         return false;
117     }
118     if (!object->destination_.Marshalling(parcel)) {
119         NETMGR_LOG_E("write object->destination_ to parcel failed");
120         return false;
121     }
122     if (!object->gateway_.Marshalling(parcel)) {
123         NETMGR_LOG_E("write object->gateway_ to parcel failed");
124         return false;
125     }
126     if (!parcel.WriteInt32(object->rtnType_)) {
127         return false;
128     }
129     if (!parcel.WriteInt32(object->mtu_)) {
130         return false;
131     }
132     if (!parcel.WriteBool(object->isHost_)) {
133         return false;
134     }
135     if (!parcel.WriteBool(object->hasGateway_)) {
136         return false;
137     }
138     if (!parcel.WriteBool(object->isDefaultRoute_)) {
139         return false;
140     }
141     if (!parcel.WriteBool(object->isExcludedRoute_)) {
142         return false;
143     }
144     return true;
145 }
146 
ToString(const std::string & tab) const147 std::string Route::ToString(const std::string &tab) const
148 {
149     std::string str;
150     str.append(tab);
151     str.append("[Route]");
152 
153     str.append(tab);
154     str.append("iface_ = ");
155     str.append(iface_);
156 
157     str.append(tab);
158     str.append("destination_ = ");
159     str.append(destination_.ToString(tab));
160 
161     str.append("\n");
162     str.append(tab);
163     str.append("gateway_ = ");
164     str.append(gateway_.ToString(tab));
165 
166     str.append("\n");
167     str.append(tab);
168     str.append("rtnType_ = ");
169     str.append(std::to_string(rtnType_));
170 
171     str.append("\n");
172     str.append(tab);
173     str.append("mtu_ = ");
174     str.append(std::to_string(mtu_));
175 
176     str.append("\n");
177     str.append(tab);
178     str.append("isHost_ = ");
179     str.append(isHost_ ? "true" : "false");
180 
181     str.append("\n");
182     str.append(tab);
183     str.append("hasGateway_ = ");
184     str.append(hasGateway_ ? "true" : "false");
185 
186     str.append("\n");
187     str.append(tab);
188     str.append("isDefaultRoute = ");
189     str.append(isDefaultRoute_ ? "true" : "false");
190 
191     return str;
192 }
193 } // namespace NetManagerStandard
194 } // namespace OHOS
195