• 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 
Marshalling(Parcel & parcel) const39 bool INetAddr::Marshalling(Parcel &parcel) const
40 {
41     if (!parcel.WriteUint8(type_)) {
42         return false;
43     }
44     if (!parcel.WriteUint8(family_)) {
45         return false;
46     }
47     if (!parcel.WriteUint8(prefixlen_)) {
48         return false;
49     }
50     if (!parcel.WriteString(address_)) {
51         return false;
52     }
53     if (!parcel.WriteString(netMask_)) {
54         return false;
55     }
56     if (!parcel.WriteString(hostName_)) {
57         return false;
58     }
59     if (!parcel.WriteUint8(port_)) {
60         return false;
61     }
62     return true;
63 }
64 
Unmarshalling(Parcel & parcel)65 sptr<INetAddr> INetAddr::Unmarshalling(Parcel &parcel)
66 {
67     sptr<INetAddr> ptr = new (std::nothrow) INetAddr();
68     if (ptr == nullptr) {
69         NETMGR_LOG_E("create INetAddr failed");
70         return nullptr;
71     }
72     if (!parcel.ReadUint8(ptr->type_)) {
73         return nullptr;
74     }
75     if (!parcel.ReadUint8(ptr->family_)) {
76         return nullptr;
77     }
78     if (!parcel.ReadUint8(ptr->prefixlen_)) {
79         return nullptr;
80     }
81     if (!parcel.ReadString(ptr->address_)) {
82         return nullptr;
83     }
84     if (!parcel.ReadString(ptr->netMask_)) {
85         return nullptr;
86     }
87     if (!parcel.ReadString(ptr->hostName_)) {
88         return nullptr;
89     }
90     if (!parcel.ReadUint8(ptr->port_)) {
91         return nullptr;
92     }
93     return ptr;
94 }
95 
Marshalling(Parcel & parcel,const sptr<INetAddr> & object)96 bool INetAddr::Marshalling(Parcel &parcel, const sptr<INetAddr> &object)
97 {
98     if (object == nullptr) {
99         NETMGR_LOG_E("INetAddr object ptr is nullptr");
100         return false;
101     }
102     if (!parcel.WriteUint8(object->type_)) {
103         return false;
104     }
105 
106     if (!parcel.WriteUint8(object->family_)) {
107         return false;
108     }
109 
110     if (!parcel.WriteUint8(object->prefixlen_)) {
111         return false;
112     }
113 
114     if (!parcel.WriteString(object->address_)) {
115         return false;
116     }
117 
118     if (!parcel.WriteString(object->netMask_)) {
119         return false;
120     }
121 
122     if (!parcel.WriteString(object->hostName_)) {
123         return false;
124     }
125 
126     if (!parcel.WriteUint8(object->port_)) {
127         return false;
128     }
129     return true;
130 }
131 
ToString(const std::string & tab) const132 std::string INetAddr::ToString(const std::string &tab) const
133 {
134     std::string str;  // print the member values of the INetAddr class
135     str.append(tab);
136     str.append("[INetAddr]");
137 
138     str.append(tab);
139     str.append("type = ");
140     str.append(std::to_string(type_));
141 
142     str.append(tab);
143     str.append("family = ");
144     str.append(std::to_string(family_));
145 
146     str.append(tab);
147     str.append("prefixLength = ");
148     str.append(std::to_string(prefixlen_));
149 
150     str.append(tab);
151     str.append("address = ");
152     str.append(CommonUtils::ToAnonymousIp(address_));
153 
154     str.append(tab);
155     str.append("netMask = ");
156     str.append(netMask_);
157 
158     str.append(tab);
159     str.append("hostName = ");
160     str.append(hostName_);
161 
162     str.append(tab);
163     str.append("port = ");
164     str.append(std::to_string(port_));
165     return str;
166 }
167 } // namespace NetManagerStandard
168 } // namespace OHOS
169