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