• 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_stub_lite.h"
16 #include "dhcp_client_callback_proxy.h"
17 #include "dhcp_manager_service_ipc_interface_code.h"
18 #include "dhcp_sdk_define.h"
19 #ifndef  OHOS_EUPDATER
20 #include "ipc_skeleton.h"
21 #include "rpc_errno.h"
22 #endif
23 #include "dhcp_client_state_machine.h"
24 #include "dhcp_logger.h"
25 #include "dhcp_errcode.h"
26 
27 #ifndef OHOS_EUPDATER
28 DEFINE_DHCPLOG_DHCP_LABEL("DhcpClientStub");
29 #endif
30 namespace OHOS {
31 namespace DHCP {
DhcpClientStub()32 DhcpClientStub::DhcpClientStub()
33 {
34 #ifndef OHOS_EUPDATER
35     InitHandleMap();
36 #endif
37 }
38 
~DhcpClientStub()39 DhcpClientStub::~DhcpClientStub()
40 {}
41 
42 #ifndef OHOS_EUPDATER
InitHandleMap()43 void DhcpClientStub::InitHandleMap()
44 {
45     handleFuncMap[static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_REG_CALL_BACK)] = &DhcpClientStub::OnRegisterCallBack;
46     handleFuncMap[static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_START_DHCP_CLIENT)] = &DhcpClientStub::OnStartDhcpClient;
47     handleFuncMap[static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_STOP_DHCP_CLIENT)] = &DhcpClientStub::OnStopDhcpClient;
48     handleFuncMap[static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_DEAL_DHCP_CACHE)] =
49         &DhcpClientStub::OnDealWifiDhcpCache;
50 }
51 
OnRemoteRequest(uint32_t code,IpcIo * req,IpcIo * reply)52 int DhcpClientStub::OnRemoteRequest(uint32_t code, IpcIo *req, IpcIo *reply)
53 {
54     DHCP_LOGI("run: %{public}s code: %{public}u L1", __func__, code);
55     if (req == nullptr || reply == nullptr) {
56         DHCP_LOGD("req:%{public}d, reply:%{public}d", req == nullptr, reply == nullptr);
57         return DHCP_OPT_FAILED;
58     }
59 
60     DHCP_LOGI("run ReadInterfaceToken L1 code %{public}u", code);
61     size_t length;
62     uint16_t* interfaceRead = nullptr;
63     interfaceRead = ReadInterfaceToken(req, &length);
64     for (size_t i = 0; i < length; i++) {
65         if (i >= DECLARE_INTERFACE_DESCRIPTOR_L1_LENGTH || interfaceRead[i] != DECLARE_INTERFACE_DESCRIPTOR_L1[i]) {
66             DHCP_LOGE("Sta stub token verification error: %{public}d", code);
67             return DHCP_OPT_FAILED;
68         }
69     }
70 
71     int exception = 0;
72     (void)ReadInt32(req, &exception);
73     if (exception) {
74         (void)WriteInt32(reply, 0);
75         (void)WriteInt32(reply, DHCP_OPT_NOT_SUPPORTED);
76         return DHCP_OPT_FAILED;
77     }
78 
79     HandleFuncMap::iterator iter = handleFuncMap.find(code);
80     if (iter == handleFuncMap.end()) {
81         DHCP_LOGE("not find function to deal, code %{public}u", code);
82         (void)WriteInt32(reply, 0);
83         (void)WriteInt32(reply, DHCP_OPT_NOT_SUPPORTED);
84     } else {
85         (this->*(iter->second))(code, req, reply);
86     }
87     return DHCP_OPT_SUCCESS;
88 }
89 
OnRegisterCallBack(uint32_t code,IpcIo * req,IpcIo * reply)90 int DhcpClientStub::OnRegisterCallBack(uint32_t code, IpcIo *req, IpcIo *reply)
91 {
92     DHCP_LOGI("run %{public}s code %{public}u", __func__, code);
93     ErrCode ret = DHCP_E_FAILED;
94     SvcIdentity sid;
95     bool readSid = ReadRemoteObject(req, &sid);
96     if (!readSid) {
97         DHCP_LOGE("read SvcIdentity failed");
98         (void)WriteInt32(reply, 0);
99         (void)WriteInt32(reply, ret);
100         return DHCP_OPT_FAILED;
101     }
102 
103     std::shared_ptr<IDhcpClientCallBack> callback_ = std::make_shared<DhcpClientCallbackProxy>(&sid);
104     DHCP_LOGI("create new DhcpClientCallbackProxy!");
105 
106     size_t readLen;
107     std::string ifname = (char *)ReadString(req, &readLen);
108     DHCP_LOGI("ifname:%{public}s", ifname.c_str());
109 
110     ret = RegisterDhcpClientCallBack(ifname, callback_);
111     (void)WriteInt32(reply, 0);
112     (void)WriteInt32(reply, ret);
113     return DHCP_OPT_SUCCESS;
114 }
115 
OnStartDhcpClient(uint32_t code,IpcIo * req,IpcIo * reply)116 int DhcpClientStub::OnStartDhcpClient(uint32_t code, IpcIo *req, IpcIo *reply)
117 {
118     DHCP_LOGI("run %{public}s code %{public}u", __func__, code);
119     ErrCode ret = DHCP_E_FAILED;
120     SvcIdentity sid;
121     bool readSid = ReadRemoteObject(req, &sid);
122     if (!readSid) {
123         DHCP_LOGE("read SvcIdentity failed");
124         (void)WriteInt32(reply, 0);
125         (void)WriteInt32(reply, ret);
126         return DHCP_OPT_FAILED;
127     }
128 
129     size_t readLen;
130     RouterConfig config;
131     bool bIpv6;
132     std::string ifname = (char *)ReadString(req, &readLen);
133     std::string bssid = (char *)ReadString(req, &readLen);
134     (void)ReadBool(req, &config.prohibitUseCacheIp);
135     (void)ReadBool(req, &config.bIpv6);
136     (void)ReadBool(req, &config.bSpecificNetwork);
137     DHCP_LOGI("ifname:%{public}s prohibitUseCacheIp:%{public}d, bIpv6:%{public}d, bSpecificNetwork:%{public}d",
138         ifname.c_str(), config.prohibitUseCacheIp, config.bIpv6, config.bSpecificNetwork);
139 
140     ret = StartDhcpClient(config);
141     (void)WriteInt32(reply, 0);
142     (void)WriteInt32(reply, ret);
143     return DHCP_OPT_SUCCESS;
144 }
145 
OnStopDhcpClient(uint32_t code,IpcIo * req,IpcIo * reply)146 int DhcpClientStub::OnStopDhcpClient(uint32_t code, IpcIo *req, IpcIo *reply)
147 {
148     DHCP_LOGI("run %{public}s code %{public}u", __func__, code);
149     ErrCode ret = DHCP_E_FAILED;
150     SvcIdentity sid;
151     bool readSid = ReadRemoteObject(req, &sid);
152     if (!readSid) {
153         DHCP_LOGE("read SvcIdentity failed");
154         (void)WriteInt32(reply, 0);
155         (void)WriteInt32(reply, ret);
156         return DHCP_OPT_FAILED;
157     }
158 
159     size_t readLen;
160     bool bIpv6;
161     std::string ifname = (char *)ReadString(req, &readLen);
162     (void)ReadBool(req, &bIpv6);
163     DHCP_LOGI("ifname:%{public}s bIpv6:%{public}d", ifname.c_str(), bIpv6);
164 
165     ret = StopDhcpClient(ifname, bIpv6);
166     (void)WriteInt32(reply, 0);
167     (void)WriteInt32(reply, ret);
168     return 0;
169 }
170 
OnDealWifiDhcpCache(uint32_t code,IpcIo * req,IpcIo * reply)171 int DhcpClientStub::OnDealWifiDhcpCache(uint32_t code, IpcIo *req, IpcIo *reply)
172 {
173     DHCP_LOGI("run %{public}s code %{public}u", __func__, code);
174     ErrCode ret = DHCP_E_FAILED;
175     SvcIdentity sid;
176     bool readSid = ReadRemoteObject(req, &sid);
177     if (!readSid) {
178         DHCP_LOGE("read SvcIdentity failed");
179         (void)WriteInt32(reply, 0);
180         (void)WriteInt32(reply, ret);
181         return DHCP_OPT_FAILED;
182     }
183     int32_t cmd = 0;
184     (void)ReadInt32(req, &cmd);
185     size_t readLen;
186     IpCacheInfo ipCacheInfo;
187     ipCacheInfo.ssid = (char *)ReadString(req, &readLen);
188     ipCacheInfo.bssid = (char *)ReadString(req, &readLen);
189     ret = DealWifiDhcpCache(cmd, ipCacheInfo);
190     (void)WriteInt32(reply, 0);
191     (void)WriteInt32(reply, ret);
192     return 0;
193 }
194 #endif
195 }
196 }
197