1 /*
2 * Copyright (C) 2021-2023 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_stub_lite.h"
16 #include "dhcp_manager_service_ipc_interface_code.h"
17 #include "ipc_skeleton.h"
18 #include "rpc_errno.h"
19 #include "dhcp_sdk_define.h"
20 #include "dhcp_logger.h"
21
22 DEFINE_DHCPLOG_DHCP_LABEL("DhcpClientCallBackStubLite");
23 namespace OHOS {
24 namespace DHCP {
DhcpClientCallBackStub()25 DhcpClientCallBackStub::DhcpClientCallBackStub() : callback_(nullptr), mRemoteDied(false)
26 {
27 DHCP_LOGI("Enter DhcpClientCallBackStub");
28 }
29
~DhcpClientCallBackStub()30 DhcpClientCallBackStub::~DhcpClientCallBackStub()
31 {
32 DHCP_LOGI("Enter ~DhcpClientCallBackStub");
33 }
34
OnRemoteInterfaceToken(uint32_t code,IpcIo * data)35 int DhcpClientCallBackStub::OnRemoteInterfaceToken(uint32_t code, IpcIo *data)
36 {
37 size_t length;
38 uint16_t* interfaceRead = nullptr;
39 interfaceRead = ReadInterfaceToken(data, &length);
40 for (size_t i = 0; i < length; i++) {
41 if (i >= DECLARE_INTERFACE_DESCRIPTOR_L1_LENGTH || interfaceRead[i] != DECLARE_INTERFACE_DESCRIPTOR_L1[i]) {
42 DHCP_LOGE("Sta stub token verification error: %{public}d", code);
43 return DHCP_OPT_FAILED;
44 }
45 }
46 return DHCP_OPT_SUCCESS;
47 }
48
OnRemoteRequest(uint32_t code,IpcIo * data)49 int DhcpClientCallBackStub::OnRemoteRequest(uint32_t code, IpcIo *data)
50 {
51 int ret = DHCP_OPT_FAILED;
52 DHCP_LOGI("OnRemoteRequest code:%{public}u!", code);
53 if (mRemoteDied || data == nullptr) {
54 DHCP_LOGE("Failed to %{public}s,mRemoteDied:%{public}d data:%{public}d!",
55 __func__, mRemoteDied, data == nullptr);
56 return ret;
57 }
58
59 if (OnRemoteInterfaceToken(code, data) == DHCP_OPT_FAILED) {
60 return DHCP_OPT_FAILED;
61 }
62 int exception = DHCP_OPT_FAILED;
63 (void)ReadInt32(data, &exception);
64 if (exception) {
65 DHCP_LOGE("DhcpClientCallBackStub::OnRemoteRequest, got exception: %{public}d!", exception);
66 return ret;
67 }
68 switch (code) {
69 case static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_CBK_CMD_IP_SUCCESS_CHANGE): {
70 ret = RemoteOnIpSuccessChanged(code, data);
71 break;
72 }
73 case static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_CBK_CMD_IP_FAIL_CHANGE): {
74 ret = RemoteOnIpFailChanged(code, data);
75 break;
76 }
77 default: {
78 ret = DHCP_OPT_FAILED;
79 break;
80 }
81 }
82 DHCP_LOGI("DhcpClientCallBackStub OnRemoteRequest, ret:%{public}d", ret);
83 return ret;
84 }
85
RegisterCallBack(const std::shared_ptr<IDhcpClientCallBack> & callBack)86 void DhcpClientCallBackStub::RegisterCallBack(const std::shared_ptr<IDhcpClientCallBack> &callBack)
87 {
88 if (callBack == nullptr) {
89 DHCP_LOGE("DhcpClientCallBackStub:callBack is nullptr!");
90 return;
91 }
92 callback_ = callBack;
93 }
94
IsRemoteDied() const95 bool DhcpClientCallBackStub::IsRemoteDied() const
96 {
97 return mRemoteDied;
98 }
99
SetRemoteDied(bool val)100 void DhcpClientCallBackStub::SetRemoteDied(bool val)
101 {
102 DHCP_LOGI("DhcpClientCallBackStub::SetRemoteDied, state:%{public}d!", val);
103 mRemoteDied = val;
104 }
105
OnIpSuccessChanged(int status,const std::string & ifname,DhcpResult & result)106 void DhcpClientCallBackStub::OnIpSuccessChanged(int status, const std::string& ifname, DhcpResult& result)
107 {
108 DHCP_LOGI("DhcpClientCallBackStub::OnIpSuccessChanged, status:%{public}d!", status);
109 if (callback_) {
110 callback_->OnIpSuccessChanged(status, ifname, result);
111 }
112 }
113
OnIpFailChanged(int status,const std::string & ifname,const std::string & reason)114 void DhcpClientCallBackStub::OnIpFailChanged(int status, const std::string& ifname, const std::string& reason)
115 {
116 DHCP_LOGI("DhcpClientCallBackStub::OnIpFailChanged, status:%{public}d!", status);
117 if (callback_) {
118 callback_->OnIpFailChanged(status, ifname, reason);
119 }
120 }
121
122
RemoteOnIpSuccessChanged(uint32_t code,IpcIo * data)123 int DhcpClientCallBackStub::RemoteOnIpSuccessChanged(uint32_t code, IpcIo *data)
124 {
125 DHCP_LOGI("run %{public}s code %{public}u", __func__, code);
126 size_t readLen;
127 int state = 0;
128 (void)ReadInt32(data, &state);
129 std::string ifname = (char *)ReadString(data, &readLen);
130
131 DhcpResult result;
132 (void)ReadInt32(data, &result.iptype);
133 (void)ReadBool(data, &result.isOptSuc);
134 (void)ReadUint32(data, &result.uLeaseTime);
135 (void)ReadUint32(data, &result.uAddTime);
136 (void)ReadUint32(data, &result.uGetTime);
137 result.strYourCli = (char *)ReadString(data, &readLen);
138 result.strServer = (char *)ReadString(data, &readLen);
139 result.strSubnet = (char *)ReadString(data, &readLen);
140 result.strDns1 = (char *)ReadString(data, &readLen);
141 result.strDns2 = (char *)ReadString(data, &readLen);
142 result.strRouter1 = (char *)ReadString(data, &readLen);
143 result.strRouter2 = (char *)ReadString(data, &readLen);
144 result.strVendor = (char *)ReadString(data, &readLen);
145 result.strLinkIpv6Addr = (char *)ReadString(data, &readLen);
146 result.strRandIpv6Addr = (char *)ReadString(data, &readLen);
147 result.strLocalAddr1 = (char *)ReadString(data, &readLen);
148 result.strLocalAddr2 = (char *)ReadString(data, &readLen);
149 OnIpSuccessChanged(state, ifname, result);
150 return 0;
151 }
152
RemoteOnIpFailChanged(uint32_t code,IpcIo * data)153 int DhcpClientCallBackStub::RemoteOnIpFailChanged(uint32_t code, IpcIo *data)
154 {
155 DHCP_LOGI("run %{public}s code %{public}u", __func__, code);
156 size_t readLen;
157 int state = 0;
158 (void)ReadInt32(data, &state);
159 std::string ifname = (char *)ReadString(data, &readLen);
160 std::string reason = (char *)ReadString(data, &readLen);
161 OnIpFailChanged(state, ifname, reason);
162 return 0;
163 }
164 } // namespace DHCP
165 } // namespace OHOS