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_server_callback_proxy.h"
16 #include "dhcp_server_stub_lite.h"
17 #include "dhcp_logger.h"
18 #include "dhcp_manager_service_ipc_interface_code.h"
19 #include "dhcp_sdk_define.h"
20 #include "ipc_skeleton.h"
21 #include "rpc_errno.h"
22 
23 DEFINE_DHCPLOG_DHCP_LABEL("DhcpServerStubLite");
24 
25 namespace OHOS {
26 namespace DHCP {
DhcpServerStub()27 DhcpServerStub::DhcpServerStub()
28 {
29 }
30 
~DhcpServerStub()31 DhcpServerStub::~DhcpServerStub()
32 {}
33 
CheckInterfaceToken(uint32_t code,IpcIo * req)34 int DhcpServerStub::CheckInterfaceToken(uint32_t code, IpcIo *req)
35 {
36     size_t length;
37     uint16_t* interfaceRead = nullptr;
38     interfaceRead = ReadInterfaceToken(req, &length);
39     for (size_t i = 0; i < length; i++) {
40         if (i >= DECLARE_INTERFACE_DESCRIPTOR_L1_LENGTH ||interfaceRead[i] != DECLARE_INTERFACE_DESCRIPTOR_L1[i]) {
41             DHCP_LOGE("Scan stub token verification error: %{public}d", code);
42             return DHCP_E_FAILED;
43         }
44     }
45     return DHCP_E_SUCCESS;
46 }
47 
OnRemoteRequest(uint32_t code,IpcIo * req,IpcIo * reply)48 int DhcpServerStub::OnRemoteRequest(uint32_t code, IpcIo *req, IpcIo *reply)
49 {
50     DHCP_LOGD("DhcpServerStub::OnRemoteRequest,code:%{public}u", code);
51     if (req == nullptr || reply == nullptr) {
52         DHCP_LOGE("req:%{public}d, reply:%{public}d", req == nullptr, reply == nullptr);
53         return DHCP_E_FAILED;
54     }
55     if (CheckInterfaceToken(code, req) == DHCP_E_FAILED) {
56         return DHCP_E_FAILED;
57     }
58     int exception = DHCP_E_FAILED;
59     (void)ReadInt32(req, &exception);
60     if (exception) {
61         return DHCP_E_FAILED;
62     }
63 
64     int ret = -1;
65     switch (code) {
66         case static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_REG_CALL_BACK): {
67             ret = OnRegisterCallBack(code, req, reply);
68             break;
69         }
70         case static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_START_DHCP_SERVER): {
71             ret = OnStartDhcpServer(code, req, reply);
72             break;
73         }
74         case static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_STOP_DHCP_SERVER): {
75             ret = OnStopDhcpServer(code, req, reply);
76             break;
77         }
78         default: {
79             ret = -1;
80         }
81     }
82     return ret;
83 }
84 
OnRegisterCallBack(uint32_t code,IpcIo * req,IpcIo * reply)85 int DhcpServerStub::OnRegisterCallBack(uint32_t code, IpcIo *req, IpcIo *reply)
86 {
87     DHCP_LOGD("run %{public}s code %{public}u", __func__, code);
88     ErrCode ret = DHCP_E_FAILED;
89     size_t readLen;
90     SvcIdentity sid;
91     bool readSid = ReadRemoteObject(req, &sid);
92     if (!readSid) {
93         DHCP_LOGE("read SvcIdentity failed");
94         (void)WriteInt32(reply, 0);
95         (void)WriteInt32(reply, ret);
96         return ret;
97     }
98 
99     std::shared_ptr<IDhcpServerCallBack> callback_ = std::make_shared<DhcpServerCallbackProxy>(&sid);
100     DHCP_LOGD("create new DhcpServerCallbackProxy!");
101     std::string ifName = (char *)ReadString(req, &readLen);
102 
103     ret = RegisterDhcpServerCallBack(ifName, callback_);
104 
105     (void)WriteInt32(reply, 0);
106     (void)WriteInt32(reply, ret);
107     return 0;
108 }
109 
OnStartDhcpServer(uint32_t code,IpcIo * req,IpcIo * reply)110 int DhcpServerStub::OnStartDhcpServer(uint32_t code, IpcIo *req, IpcIo *reply)
111 {
112     DHCP_LOGI("OnStartDhcpServer\n");
113     size_t readLen;
114     int pid;
115     int tokenId;
116     (void)ReadInt32(req, &pid);
117     (void)ReadInt32(req, &tokenId);
118     std::string ifName = (char *)ReadString(req, &readLen);
119     DHCP_LOGD("%{public}s, get pid: %{public}d, tokenId: %{private}d", __func__, pid, tokenId);
120 
121     ErrCode ret = StartDhcpServer(ifName);
122     (void)WriteInt32(reply, 0);
123     (void)WriteInt32(reply, ret);
124 
125     return 0;
126 }
127 
OnStopDhcpServer(uint32_t code,IpcIo * req,IpcIo * reply)128 int DhcpServerStub::OnStopDhcpServer(uint32_t code, IpcIo *req, IpcIo *reply)
129 {
130     DHCP_LOGI("OnStopDhcpServer\n");
131     size_t readLen;
132     std::string ifName = (char *)ReadString(req, &readLen);
133     ErrCode ret = StopDhcpServer(ifName);
134     (void)WriteInt32(reply, 0);
135     (void)WriteInt32(reply, ret);
136 
137     return 0;
138 }
139 
OnSetDhcpName(uint32_t code,IpcIo * req,IpcIo * reply)140 int DhcpServerStub::OnSetDhcpName(uint32_t code, IpcIo *req, IpcIo *reply)
141 {
142     size_t readLen;
143     std::string tagName = (char *)ReadString(req, &readLen);
144     std::string ifName = (char *)ReadString(req, &readLen);
145     ErrCode ret = SetDhcpName(ifName, tagName);
146     (void)WriteInt32(reply, 0);
147     (void)WriteInt32(reply, ret);
148 
149     return 0;
150 }
OnSetDhcpRange(uint32_t code,IpcIo * req,IpcIo * reply)151 int DhcpServerStub::OnSetDhcpRange(uint32_t code, IpcIo *req, IpcIo *reply)
152 {
153     DHCP_LOGI("OnSetDhcpRange\n");
154     DhcpRange range;
155     size_t readLen;
156     (void)ReadInt32(req, &range.iptype);
157     (void)ReadInt32(req, &range.leaseHours);
158     range.strTagName = (char *)ReadString(req, &readLen);
159     range.strStartip = (char *)ReadString(req, &readLen);
160     range.strEndip = (char *)ReadString(req, &readLen);
161     range.strSubnet = (char *)ReadString(req, &readLen);
162     std::string ifname = (char *)ReadString(req, &readLen);
163 
164     ErrCode ret = SetDhcpRange(ifname, range);
165     (void)WriteInt32(reply, 0);
166     (void)WriteInt32(reply, ret);
167 
168     return 0;
169 }
OnRemoveAllDhcpRange(uint32_t code,IpcIo * req,IpcIo * reply)170 int DhcpServerStub::OnRemoveAllDhcpRange(uint32_t code, IpcIo *req, IpcIo *reply)
171 {
172     DHCP_LOGI("OnRemoveAllDhcpRange\n");
173     size_t readLen;
174     std::string tagName = (char *)ReadString(req, &readLen);
175     ErrCode ret = RemoveAllDhcpRange(tagName);
176     (void)WriteInt32(reply, 0);
177     (void)WriteInt32(reply, ret);
178 
179     return 0;
180 }
OnRemoveDhcpRange(uint32_t code,IpcIo * req,IpcIo * reply)181 int DhcpServerStub::OnRemoveDhcpRange(uint32_t code, IpcIo *req, IpcIo *reply)
182 {
183     DHCP_LOGI("OnRemoveDhcpRange\n");
184     DhcpRange range;
185     size_t readLen;
186     (void)ReadInt32(req, &range.iptype);
187     (void)ReadInt32(req, &range.leaseHours);
188     range.strTagName = (char *)ReadString(req, &readLen);
189     range.strStartip = (char *)ReadString(req, &readLen);
190     range.strEndip = (char *)ReadString(req, &readLen);
191     range.strSubnet = (char *)ReadString(req, &readLen);
192     std::string tagName = (char *)ReadString(req, &readLen);
193 
194     ErrCode ret = RemoveDhcpRange(tagName, range);
195     (void)WriteInt32(reply, 0);
196     (void)WriteInt32(reply, ret);
197 
198     return 0;
199 }
OnGetDhcpClientInfos(uint32_t code,IpcIo * req,IpcIo * reply)200 int DhcpServerStub::OnGetDhcpClientInfos(uint32_t code, IpcIo *req, IpcIo *reply)
201 {
202     DHCP_LOGI("OnGetDhcpClientInfos\n");
203     size_t readLen;
204     std::vector<std::string> leases;
205     std::string ifname = (char *)ReadString(req, &readLen);
206     ErrCode ret = GetDhcpClientInfos(ifname, leases);
207     (void)WriteInt32(reply, 0);
208     (void)WriteInt32(reply, ret);
209     if (ret != DHCP_E_SUCCESS) {
210         return ret;
211     }
212     int size = leases.size();
213     DHCP_LOGI("OnGetDhcpClientInfos, reply message size:%{public}d\n", size);
214     (void)WriteInt32(reply, size);
215     for (auto str : leases) {
216         (void)WriteString(reply, str.c_str());
217     }
218 
219     return 0;
220 }
221 
OnUpdateLeasesTime(uint32_t code,IpcIo * req,IpcIo * reply)222 int DhcpServerStub::OnUpdateLeasesTime(uint32_t code, IpcIo *req, IpcIo *reply)
223 {
224     DHCP_LOGI("OnUpdateLeasesTime\n");
225     size_t readLen;
226     std::string leaseTime = (char *)ReadString(req, &readLen);
227     ErrCode ret = UpdateLeasesTime(leaseTime);
228     (void)WriteInt32(reply, 0);
229     (void)WriteInt32(reply, ret);
230 
231     return 0;
232 }
233 
OnPutDhcpRange(uint32_t code,IpcIo * req,IpcIo * reply)234 int DhcpServerStub::OnPutDhcpRange(uint32_t code, IpcIo *req, IpcIo *reply)
235 {
236     DHCP_LOGI("OnPutDhcpRange\n");
237     DhcpRange range;
238     size_t readLen;
239     (void)ReadInt32(req, &range.iptype);
240     (void)ReadInt32(req, &range.leaseHours);
241     range.strTagName = (char *)ReadString(req, &readLen);
242     range.strStartip = (char *)ReadString(req, &readLen);
243     range.strEndip = (char *)ReadString(req, &readLen);
244     range.strSubnet = (char *)ReadString(req, &readLen);
245     std::string tagName = (char *)ReadString(req, &readLen);
246 
247     ErrCode ret = PutDhcpRange(tagName, range);
248     (void)WriteInt32(reply, 0);
249     (void)WriteInt32(reply, ret);
250 
251     return 0;
252 }
253 
254 }
255 }