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 #include "dhcp_client_callback_proxy.h"
16 #include "dhcp_client_stub.h"
17 #include "dhcp_logger.h"
18 #include "dhcp_manager_service_ipc_interface_code.h"
19 #include "dhcp_client_state_machine.h"
20
21 DEFINE_DHCPLOG_DHCP_LABEL("DhcpClientStub");
22
23 namespace OHOS {
24 namespace DHCP {
DhcpClientStub()25 DhcpClientStub::DhcpClientStub()
26 {
27 InitHandleMap();
28 }
29
~DhcpClientStub()30 DhcpClientStub::~DhcpClientStub()
31 {
32 DHCP_LOGI("enter ~DhcpClientStub!");
33 #ifndef OHOS_ARCH_LITE
34 #ifndef DTFUZZ_TEST
35 RemoveDeviceCbDeathRecipient();
36 #endif
37 #endif
38 }
39
40 #ifndef OHOS_ARCH_LITE
RemoveDeviceCbDeathRecipient()41 void DhcpClientStub::RemoveDeviceCbDeathRecipient()
42 {
43 DHCP_LOGI("enter RemoveDeathRecipient!");
44 std::lock_guard<std::mutex> lock(mutex_);
45 for (auto iter = remoteDeathMap.begin(); iter != remoteDeathMap.end(); ++iter) {
46 iter->first->RemoveDeathRecipient(iter->second);
47 }
48 remoteDeathMap.clear();
49 }
50
RemoveDeviceCbDeathRecipient(const wptr<IRemoteObject> & remoteObject)51 void DhcpClientStub::RemoveDeviceCbDeathRecipient(const wptr<IRemoteObject> &remoteObject)
52 {
53 DHCP_LOGI("RemoveDeathRecipient, remoteObject: %{private}p!", &remoteObject);
54 std::lock_guard<std::mutex> lock(mutex_);
55 if (remoteObject == nullptr) {
56 DHCP_LOGI("remoteObject is nullptr");
57 return;
58 }
59 RemoteDeathMap::iterator iter = remoteDeathMap.find(remoteObject.promote());
60 if (iter == remoteDeathMap.end()) {
61 DHCP_LOGI("not find remoteObject to deal!");
62 } else {
63 remoteObject->RemoveDeathRecipient(iter->second);
64 remoteDeathMap.erase(iter);
65 DHCP_LOGI("remove death recipient success!");
66 }
67 }
68
OnRemoteDied(const wptr<IRemoteObject> & remoteObject)69 void DhcpClientStub::OnRemoteDied(const wptr<IRemoteObject> &remoteObject)
70 {
71 DHCP_LOGI("OnRemoteDied, Remote is died! remoteObject: %{private}p", &remoteObject);
72 RemoveDeviceCbDeathRecipient(remoteObject);
73 }
74 #endif
75
InitHandleMap()76 void DhcpClientStub::InitHandleMap()
77 {
78 handleFuncMap[static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_REG_CALL_BACK)] =
79 [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
80 return OnRegisterCallBack(code, data, reply, option);
81 };
82 handleFuncMap[static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_START_DHCP_CLIENT)] =
83 [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
84 return OnStartDhcpClient(code, data, reply, option);
85 };
86 handleFuncMap[static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_STOP_DHCP_CLIENT)] =
87 [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
88 return OnStopDhcpClient(code, data, reply, option);
89 };
90 handleFuncMap[static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_DEAL_DHCP_CACHE)] =
91 [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
92 return OnDealWifiDhcpCache(code, data, reply, option);
93 };
94 handleFuncMap[static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_STOP_SA)] =
95 [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
96 return OnStopClientSa(code, data, reply, option);
97 };
98 }
99
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)100 int DhcpClientStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
101 {
102 if (data.ReadInterfaceToken() != GetDescriptor()) {
103 DHCP_LOGE("dhcp client stub token verification error: %{public}d", code);
104 return DHCP_OPT_FAILED;
105 }
106 int exception = data.ReadInt32();
107 if (exception) {
108 DHCP_LOGI("exception is ture, return failed");
109 return DHCP_OPT_FAILED;
110 }
111 HandleFuncMap::iterator iter = handleFuncMap.find(code);
112 if (iter == handleFuncMap.end()) {
113 DHCP_LOGI("not find function to deal, code %{public}u", code);
114 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
115 } else {
116 return (iter->second)(code, data, reply, option);
117 }
118 }
119
OnRegisterCallBack(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)120 int DhcpClientStub::OnRegisterCallBack(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
121 {
122 DHCP_LOGI("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
123 sptr<IRemoteObject> remote = data.ReadRemoteObject();
124 if (remote == nullptr) {
125 DHCP_LOGE("Failed to ReadRemoteObject!");
126 return DHCP_E_INVALID_PARAM;
127 }
128 sptr<IDhcpClientCallBack> callback_ = iface_cast<IDhcpClientCallBack>(remote);
129 if (callback_ == nullptr) {
130 callback_ = sptr<DhcpClientCallbackProxy>::MakeSptr(remote);
131 DHCP_LOGI("create new DhcpClientCallbackProxy!");
132 }
133 std::string ifName = data.ReadString();
134 if (deathRecipient_ == nullptr) {
135 #ifdef OHOS_ARCH_LITE
136 deathRecipient_ =sptr<DhcpClientDeathRecipient>::MakeSptr();
137 #else
138 deathRecipient_ =sptr<ClientDeathRecipient>::MakeSptr(*this);
139 remoteDeathMap.insert(std::make_pair(remote, deathRecipient_));
140 #endif
141 }
142 ErrCode ret = RegisterDhcpClientCallBack(ifName, callback_);
143 reply.WriteInt32(0);
144 reply.WriteInt32(ret);
145 return 0;
146 }
147
OnStartDhcpClient(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)148 int DhcpClientStub::OnStartDhcpClient(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
149 {
150 DHCP_LOGI("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
151 RouterConfig config;
152 config.ifname = data.ReadString();
153 config.bssid = data.ReadString();
154 config.prohibitUseCacheIp = data.ReadBool();
155 config.bIpv6 = data.ReadBool();
156 config.bSpecificNetwork = data.ReadBool();
157 config.isStaticIpv4 = data.ReadBool();
158 config.bIpv4 = data.ReadBool();
159 ErrCode ret = StartDhcpClient(config);
160 reply.WriteInt32(0);
161 reply.WriteInt32(ret);
162 return 0;
163 }
164
OnStopDhcpClient(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)165 int DhcpClientStub::OnStopDhcpClient(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
166 {
167 DHCP_LOGI("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
168 std::string ifname = data.ReadString();
169 bool bIpv6 = data.ReadBool();
170 bool bIpv4 = data.ReadBool();
171 ErrCode ret = StopDhcpClient(ifname, bIpv6, bIpv4);
172 reply.WriteInt32(0);
173 reply.WriteInt32(ret);
174 return 0;
175 }
176
OnDealWifiDhcpCache(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)177 int DhcpClientStub::OnDealWifiDhcpCache(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
178 {
179 DHCP_LOGI("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
180 int32_t cmd = data.ReadInt32();
181 IpCacheInfo ipCacheInfo;
182 ipCacheInfo.ssid = data.ReadString();
183 ipCacheInfo.bssid = data.ReadString();
184 ErrCode ret = DealWifiDhcpCache(cmd, ipCacheInfo);
185 reply.WriteInt32(0);
186 reply.WriteInt32(ret);
187 return 0;
188 }
189
OnStopClientSa(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)190 int DhcpClientStub::OnStopClientSa(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
191 {
192 DHCP_LOGI("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
193 ErrCode ret = StopClientSa();
194 reply.WriteInt32(0);
195 reply.WriteInt32(ret);
196 return 0;
197 }
198 }
199 }
200