• 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 "inet_addr.h"
17 
18 #include "parcel.h"
19 #include "refbase.h"
20 
21 #include "net_mgr_log_wrapper.h"
22 #include "netmanager_base_common_utils.h"
23 
24 namespace OHOS {
25 namespace NetManagerStandard {
operator ==(const INetAddr & obj) const26 bool INetAddr::operator==(const INetAddr &obj) const
27 {
28     bool out = true;
29     out = out && (type_ == obj.type_);
30     out = out && (family_ == obj.family_);
31     out = out && (prefixlen_ == obj.prefixlen_);
32     out = out && (address_ == obj.address_);
33     out = out && (netMask_ == obj.netMask_);
34     out = out && (hostName_ == obj.hostName_);
35     out = out && (port_ == obj.port_);
36     return out;
37 }
38 
operator <(const INetAddr & obj) const39 bool INetAddr::operator<(const INetAddr &obj) const
40 {
41     if (type_ < obj.type_) return true;
42     if (type_ > obj.type_) return false;
43 
44     if (family_ < obj.family_) return true;
45     if (family_ > obj.family_) return false;
46 
47     if (prefixlen_ < obj.prefixlen_) return true;
48     if (prefixlen_ > obj.prefixlen_) return false;
49 
50     if (address_ < obj.address_) return true;
51     if (address_ > obj.address_) return false;
52 
53     if (netMask_ < obj.netMask_) return true;
54     if (netMask_ > obj.netMask_) return false;
55 
56     if (hostName_ < obj.hostName_) return true;
57     if (hostName_ > obj.hostName_) return false;
58 
59     if (port_ < obj.port_) return true;
60     if (port_ > obj.port_) return false;
61 
62     return false;
63 }
64 
Marshalling(Parcel & parcel) const65 bool INetAddr::Marshalling(Parcel &parcel) const
66 {
67     if (!parcel.WriteUint8(type_)) {
68         return false;
69     }
70     if (!parcel.WriteUint8(family_)) {
71         return false;
72     }
73     if (!parcel.WriteUint8(prefixlen_)) {
74         return false;
75     }
76     if (!parcel.WriteString(address_)) {
77         return false;
78     }
79     if (!parcel.WriteString(netMask_)) {
80         return false;
81     }
82     if (!parcel.WriteString(hostName_)) {
83         return false;
84     }
85     if (!parcel.WriteUint8(port_)) {
86         return false;
87     }
88     return true;
89 }
90 
Unmarshalling(Parcel & parcel)91 sptr<INetAddr> INetAddr::Unmarshalling(Parcel &parcel)
92 {
93     sptr<INetAddr> ptr = new (std::nothrow) INetAddr();
94     if (ptr == nullptr) {
95         NETMGR_LOG_E("create INetAddr failed");
96         return nullptr;
97     }
98     if (!parcel.ReadUint8(ptr->type_)) {
99         return nullptr;
100     }
101     if (!parcel.ReadUint8(ptr->family_)) {
102         return nullptr;
103     }
104     if (!parcel.ReadUint8(ptr->prefixlen_)) {
105         return nullptr;
106     }
107     if (!parcel.ReadString(ptr->address_)) {
108         return nullptr;
109     }
110     if (!parcel.ReadString(ptr->netMask_)) {
111         return nullptr;
112     }
113     if (!parcel.ReadString(ptr->hostName_)) {
114         return nullptr;
115     }
116     if (!parcel.ReadUint8(ptr->port_)) {
117         return nullptr;
118     }
119     return ptr;
120 }
121 
Marshalling(Parcel & parcel,const sptr<INetAddr> & object)122 bool INetAddr::Marshalling(Parcel &parcel, const sptr<INetAddr> &object)
123 {
124     if (object == nullptr) {
125         NETMGR_LOG_E("INetAddr object ptr is nullptr");
126         return false;
127     }
128     if (!parcel.WriteUint8(object->type_)) {
129         return false;
130     }
131 
132     if (!parcel.WriteUint8(object->family_)) {
133         return false;
134     }
135 
136     if (!parcel.WriteUint8(object->prefixlen_)) {
137         return false;
138     }
139 
140     if (!parcel.WriteString(object->address_)) {
141         return false;
142     }
143 
144     if (!parcel.WriteString(object->netMask_)) {
145         return false;
146     }
147 
148     if (!parcel.WriteString(object->hostName_)) {
149         return false;
150     }
151 
152     if (!parcel.WriteUint8(object->port_)) {
153         return false;
154     }
155     return true;
156 }
157 
ToString(const std::string & tab) const158 std::string INetAddr::ToString(const std::string &tab) const
159 {
160     std::string str;  // print the member values of the INetAddr class
161     str.append(tab);
162     str.append("[INetAddr]");
163 
164     str.append(tab);
165     str.append("type = ");
166     str.append(std::to_string(type_));
167 
168     str.append(tab);
169     str.append("family = ");
170     str.append(std::to_string(family_));
171 
172     str.append(tab);
173     str.append("prefixLength = ");
174     str.append(std::to_string(prefixlen_));
175 
176     str.append(tab);
177     str.append("address = ");
178     str.append(CommonUtils::ToAnonymousIp(address_));
179 
180     str.append(tab);
181     str.append("netMask = ");
182     str.append(netMask_);
183 
184     str.append(tab);
185     str.append("hostName = ");
186     str.append(hostName_);
187 
188     str.append(tab);
189     str.append("port = ");
190     str.append(std::to_string(port_));
191     return str;
192 }
193 } // namespace NetManagerStandard
194 } // namespace OHOS
195