• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "static_configuration.h"
17 
18 #include "inet_addr.h"
19 #include "netmgr_ext_log_wrapper.h"
20 #include "parcel.h"
21 #include "refbase.h"
22 
23 namespace OHOS {
24 namespace NetManagerStandard {
25 namespace {
26 constexpr uint32_t MAX_SIZE = 50;
27 }
28 
Marshalling(Parcel & parcel) const29 bool StaticConfiguration::Marshalling(Parcel &parcel) const
30 {
31     if (!ipAddr_.Marshalling(parcel)) {
32         NETMGR_EXT_LOG_E("write ipAddr_ to parcel failed");
33         return false;
34     }
35     if (!route_.Marshalling(parcel)) {
36         NETMGR_EXT_LOG_E("write route_ to parcel failed");
37         return false;
38     }
39     if (!gateway_.Marshalling(parcel)) {
40         NETMGR_EXT_LOG_E("write gateway_ to parcel failed");
41         return false;
42     }
43     if (!netMask_.Marshalling(parcel)) {
44         NETMGR_EXT_LOG_E("write netMask_ to parcel failed");
45         return false;
46     }
47     if (!parcel.WriteUint32(dnsServers_.size())) {
48         NETMGR_EXT_LOG_E("write dnsServers_ size to parcel failed");
49         return false;
50     }
51     for (auto dnsServer : dnsServers_) {
52         if (!dnsServer.Marshalling(parcel)) {
53             NETMGR_EXT_LOG_E("write dnsServers_ to parcel failed");
54             return false;
55         }
56     }
57     if (!parcel.WriteString(domain_)) {
58         NETMGR_EXT_LOG_E("write domain_ to parcel failed");
59         return false;
60     }
61     return true;
62 }
63 
Unmarshalling(Parcel & parcel)64 sptr<StaticConfiguration> StaticConfiguration::Unmarshalling(Parcel &parcel)
65 {
66     sptr<StaticConfiguration> ptr = new (std::nothrow) StaticConfiguration();
67     if (ptr == nullptr) {
68         NETMGR_EXT_LOG_E("ptr new failed");
69         return nullptr;
70     }
71     sptr<INetAddr> ipAddr = INetAddr::Unmarshalling(parcel);
72     if (ipAddr == nullptr) {
73         NETMGR_EXT_LOG_E("ipAddr is null");
74         return nullptr;
75     }
76     ptr->ipAddr_ = *ipAddr;
77     sptr<INetAddr> route = INetAddr::Unmarshalling(parcel);
78     if (route == nullptr) {
79         NETMGR_EXT_LOG_E("route is null");
80         return nullptr;
81     }
82     ptr->route_ = *route;
83     sptr<INetAddr> gateway = INetAddr::Unmarshalling(parcel);
84     if (gateway == nullptr) {
85         NETMGR_EXT_LOG_E("gateway is null");
86         return nullptr;
87     }
88     ptr->gateway_ = *gateway;
89     sptr<INetAddr> netMask = INetAddr::Unmarshalling(parcel);
90     if (netMask == nullptr) {
91         NETMGR_EXT_LOG_E("netMask is null");
92         return nullptr;
93     }
94     ptr->netMask_ = *netMask;
95     uint32_t size = 0;
96     if (!parcel.ReadUint32(size)) {
97         return nullptr;
98     }
99     if (size > MAX_SIZE) {
100         NETMGR_EXT_LOG_E("size=[%{public}d] is too large", size);
101         return nullptr;
102     }
103     for (uint32_t i = 0; i < size; i++) {
104         sptr<INetAddr> netAddr = INetAddr::Unmarshalling(parcel);
105         if (netAddr == nullptr) {
106             NETMGR_EXT_LOG_E("netAddr is null");
107             return nullptr;
108         }
109         ptr->dnsServers_.push_back(*netAddr);
110     }
111     if (!parcel.ReadString(ptr->domain_)) {
112         return nullptr;
113     }
114     return ptr;
115 }
116 } // namespace NetManagerStandard
117 } // namespace OHOS