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     RemoteDeathMap::iterator iter = remoteDeathMap.find(remoteObject.promote());
56     if (iter == remoteDeathMap.end()) {
57         DHCP_LOGI("not find remoteObject to deal!");
58     } else {
59         remoteObject->RemoveDeathRecipient(iter->second);
60         remoteDeathMap.erase(iter);
61         DHCP_LOGI("remove death recipient success!");
62     }
63 }
64 
OnRemoteDied(const wptr<IRemoteObject> & remoteObject)65 void DhcpClientStub::OnRemoteDied(const wptr<IRemoteObject> &remoteObject)
66 {
67     DHCP_LOGI("OnRemoteDied, Remote is died! remoteObject: %{private}p", &remoteObject);
68     RemoveDeviceCbDeathRecipient(remoteObject);
69 }
70 #endif
71 
InitHandleMap()72 void DhcpClientStub::InitHandleMap()
73 {
74     handleFuncMap[static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_REG_CALL_BACK)] =
75         [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
76             return OnRegisterCallBack(code, data, reply, option);
77         };
78     handleFuncMap[static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_START_DHCP_CLIENT)] =
79         [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
80             return OnStartDhcpClient(code, data, reply, option);
81         };
82     handleFuncMap[static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_STOP_DHCP_CLIENT)] =
83         [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
84             return OnStopDhcpClient(code, data, reply, option);
85         };
86     handleFuncMap[static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_SET_CONFIG)] =
87         [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
88             return OnSetConfiguration(code, data, reply, option);
89         };
90 }
91 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)92 int DhcpClientStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
93 {
94     if (data.ReadInterfaceToken() != GetDescriptor()) {
95         DHCP_LOGE("dhcp client stub token verification error: %{public}d", code);
96         return DHCP_OPT_FAILED;
97     }
98     int exception = data.ReadInt32();
99     if (exception) {
100         DHCP_LOGI("exception is ture, return failed");
101         return DHCP_OPT_FAILED;
102     }
103     HandleFuncMap::iterator iter = handleFuncMap.find(code);
104     if (iter == handleFuncMap.end()) {
105         DHCP_LOGI("not find function to deal, code %{public}u", code);
106         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
107     } else {
108         return (iter->second)(code, data, reply, option);
109     }
110 }
111 
OnRegisterCallBack(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)112 int DhcpClientStub::OnRegisterCallBack(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
113 {
114     DHCP_LOGI("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
115     sptr<IRemoteObject> remote = data.ReadRemoteObject();
116     if (remote == nullptr) {
117         DHCP_LOGE("Failed to ReadRemoteObject!");
118         return DHCP_E_INVALID_PARAM;
119     }
120     sptr<IDhcpClientCallBack> callback_ = iface_cast<IDhcpClientCallBack>(remote);
121     if (callback_ == nullptr) {
122         callback_ = new (std::nothrow) DhcpClientCallbackProxy(remote);
123         DHCP_LOGI("create new DhcpClientCallbackProxy!");
124     }
125     std::string ifName = data.ReadString();
126     if (deathRecipient_ == nullptr) {
127 #ifdef OHOS_ARCH_LITE
128         deathRecipient_ = new (std::nothrow) DhcpClientDeathRecipient();
129 #else
130         deathRecipient_ = new (std::nothrow) ClientDeathRecipient(*this);
131         remoteDeathMap.insert(std::make_pair(remote, deathRecipient_));
132 #endif
133     }
134     ErrCode ret = RegisterDhcpClientCallBack(ifName, callback_);
135     reply.WriteInt32(0);
136     reply.WriteInt32(ret);
137     return 0;
138 }
139 
OnStartDhcpClient(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)140 int DhcpClientStub::OnStartDhcpClient(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
141 {
142     DHCP_LOGI("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
143     std::string ifname = data.ReadString();
144     bool bIpv6 = data.ReadBool();
145     ErrCode ret = StartDhcpClient(ifname, bIpv6);
146     reply.WriteInt32(0);
147     reply.WriteInt32(ret);
148     return 0;
149 }
150 
OnSetConfiguration(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)151 int DhcpClientStub::OnSetConfiguration(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
152 {
153     DHCP_LOGI("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
154     std::string ifname = data.ReadString();
155     RouterConfig config;
156     config.bssid = data.ReadString();
157     ErrCode ret = SetConfiguration(ifname, config);
158     reply.WriteInt32(0);
159     reply.WriteInt32(ret);
160     return 0;
161 }
162 
OnStopDhcpClient(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)163 int DhcpClientStub::OnStopDhcpClient(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
164 {
165     DHCP_LOGI("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
166     std::string ifname = data.ReadString();
167     bool bIpv6 = data.ReadBool();
168     ErrCode ret = StopDhcpClient(ifname, bIpv6);
169     reply.WriteInt32(0);
170     reply.WriteInt32(ret);
171     return 0;
172 }
173 }
174 }
175