• 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 #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_ = new (std::nothrow) DhcpClientCallbackProxy(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_ = new (std::nothrow) DhcpClientDeathRecipient();
137 #else
138         deathRecipient_ = new (std::nothrow) ClientDeathRecipient(*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     ErrCode ret = StartDhcpClient(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 
OnDealWifiDhcpCache(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)174 int DhcpClientStub::OnDealWifiDhcpCache(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
175 {
176     DHCP_LOGI("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
177     int32_t cmd = data.ReadInt32();
178     IpCacheInfo ipCacheInfo;
179     ipCacheInfo.ssid = data.ReadString();
180     ipCacheInfo.bssid = data.ReadString();
181     ErrCode ret = DealWifiDhcpCache(cmd, ipCacheInfo);
182     reply.WriteInt32(0);
183     reply.WriteInt32(ret);
184     return 0;
185 }
186 
OnStopClientSa(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)187 int DhcpClientStub::OnStopClientSa(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
188 {
189     DHCP_LOGI("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
190     ErrCode ret = StopClientSa();
191     reply.WriteInt32(0);
192     reply.WriteInt32(ret);
193     return 0;
194 }
195 }
196 }
197