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.h"
17 #include "dhcp_logger.h"
18 #include "dhcp_manager_service_ipc_interface_code.h"
19 #include "dhcp_server_death_recipient.h"
20 
21 DEFINE_DHCPLOG_DHCP_LABEL("DhcpServerStub");
22 
23 namespace OHOS {
24 namespace DHCP {
DhcpServerStub()25 DhcpServerStub::DhcpServerStub() : mSingleCallback(false), callback_(nullptr)
26 {
27     InitHandleMap();
28 }
29 
~DhcpServerStub()30 DhcpServerStub::~DhcpServerStub()
31 {}
32 
InitHandleMap()33 void DhcpServerStub::InitHandleMap()
34 {
35     handleFuncMap[static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_REG_CALL_BACK)] =
36         [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
37             return OnRegisterCallBack(code, data, reply, option);
38         };
39     handleFuncMap[static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_START_DHCP_SERVER)] =
40         [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
41             return OnStartDhcpServer(code, data, reply, option);
42         };
43     handleFuncMap[static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_STOP_DHCP_SERVER)] =
44         [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
45             return OnStopDhcpServer(code, data, reply, option);
46         };
47     handleFuncMap[static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_SET_DHCP_RANGE)] =
48         [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
49             return OnSetDhcpRange(code, data, reply, option);
50         };
51     handleFuncMap[static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_SET_DHCP_NAME)] =
52         [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
53             return OnSetDhcpName(code, data, reply, option);
54         };
55     handleFuncMap[static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_PUT_DHCP_RANGE)] =
56         [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
57             return OnPutDhcpRange(code, data, reply, option);
58         };
59     handleFuncMap[static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_REMOVE_ALL_DHCP_RANGE)] =
60         [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
61             return OnRemoveAllDhcpRange(code, data, reply, option);
62         };
63     handleFuncMap[static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_REMOVE_DHCP_RANGE)] =
64         [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
65             return OnRemoveDhcpRange(code, data, reply, option);
66         };
67     handleFuncMap[static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_UPDATE_RENEW_TIME)] =
68         [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
69             return OnUpdateLeasesTime(code, data, reply, option);
70         };
71     handleFuncMap[static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_GET_DHCP_CLIENT_INFO)] =
72         [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
73             return OnGetDhcpClientInfos(code, data, reply, option);
74         };
75 }
76 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)77 int DhcpServerStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
78 {
79     DHCP_LOGI("DhcpServerStub::OnRemoteRequest,code:%{public}u", code);
80     if (data.ReadInterfaceToken() != GetDescriptor()) {
81         DHCP_LOGE("dhcp server stub token verification error: %{public}d", code);
82         return DHCP_OPT_FAILED;
83     }
84     HandleFuncMap::iterator iter = handleFuncMap.find(code);
85     if (iter == handleFuncMap.end()) {
86         DHCP_LOGI("not find function to deal, code %{public}u", code);
87         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
88     } else {
89         int exception = data.ReadInt32();
90         DHCP_LOGI("server rec, exception:%{public}d", exception);
91         if (exception) {
92             return DHCP_OPT_FAILED;
93         }
94         return (iter->second)(code, data, reply, option);
95     }
96 }
97 
IsSingleCallback() const98 bool DhcpServerStub::IsSingleCallback() const
99 {
100     return mSingleCallback;
101 }
102 
SetSingleCallback(const bool isSingleCallback)103 void DhcpServerStub::SetSingleCallback(const bool isSingleCallback)
104 {
105     mSingleCallback = true;
106 }
107 
OnRegisterCallBack(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)108 int DhcpServerStub::OnRegisterCallBack(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
109 {
110     DHCP_LOGI("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
111     sptr<IRemoteObject> remote = data.ReadRemoteObject();
112     if (remote == nullptr) {
113         DHCP_LOGE("Failed to ReadRemoteObject!");
114         return DHCP_OPT_FAILED;
115     }
116     callback_ = iface_cast<IDhcpServerCallBack>(remote);
117     if (callback_ == nullptr) {
118         callback_ = new (std::nothrow) DhcpServerCallbackProxy(remote);
119         DHCP_LOGI("create new DhcpServerCallbackProxy!");
120     }
121     DHCP_LOGI("send OnStartDhcpClient data start");
122     int pid = data.ReadInt32();
123     int tokenId = data.ReadInt32();
124     std::string ifName = data.ReadString();
125     DHCP_LOGD("%{public}s, get pid: %{public}d, tokenId: %{private}d", __func__, pid, tokenId);
126 
127     ErrCode ret = RegisterDhcpServerCallBack(ifName, callback_);
128     reply.WriteInt32(0);
129     reply.WriteInt32(ret);
130     return 0;
131 }
132 
OnStartDhcpServer(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)133 int DhcpServerStub::OnStartDhcpServer(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
134 {
135     DHCP_LOGI("OnStartDhcpServer\n");
136     int pid = data.ReadInt32();
137     int tokenId = data.ReadInt32();
138     std::string ifName = data.ReadString();
139     DHCP_LOGD("%{public}s, get pid: %{public}d, tokenId: %{private}d", __func__, pid, tokenId);
140 
141     ErrCode ret = StartDhcpServer(ifName);
142     reply.WriteInt32(0);
143     reply.WriteInt32(ret);
144 
145     return 0;
146 }
147 
OnStopDhcpServer(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)148 int DhcpServerStub::OnStopDhcpServer(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
149 {
150     DHCP_LOGI("OnStopDhcpServer\n");
151     std::string ifName = data.ReadString();
152     ErrCode ret = StopDhcpServer(ifName);
153     reply.WriteInt32(0);
154     reply.WriteInt32(ret);
155 
156     return 0;
157 }
158 
OnSetDhcpName(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)159 int DhcpServerStub::OnSetDhcpName(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
160 {
161     std::string tagName = data.ReadString();
162     std::string ifname = data.ReadString();
163     DHCP_LOGI("OnSetDhcpName ifname = %{public}s\n", ifname.c_str());
164     ErrCode ret = SetDhcpName(ifname, tagName);
165     reply.WriteInt32(0);
166     reply.WriteInt32(ret);
167 
168     return 0;
169 }
OnSetDhcpRange(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)170 int DhcpServerStub::OnSetDhcpRange(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
171 {
172     DHCP_LOGI("OnSetDhcpRange\n");
173     DhcpRange range;
174     range.iptype = data.ReadInt32();
175     range.leaseHours = data.ReadInt32();
176     range.strTagName = data.ReadString();
177     range.strStartip = data.ReadString();
178     range.strEndip = data.ReadString();
179     range.strSubnet = data.ReadString();
180     std::string ifname = data.ReadString();
181     ErrCode ret = SetDhcpRange(ifname, range);
182     reply.WriteInt32(0);
183     reply.WriteInt32(ret);
184 
185     return 0;
186 }
OnRemoveAllDhcpRange(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)187 int DhcpServerStub::OnRemoveAllDhcpRange(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
188 {
189     DHCP_LOGI("OnRemoveAllDhcpRange\n");
190     std::string tagName = data.ReadString();
191     ErrCode ret = RemoveAllDhcpRange(tagName);
192     reply.WriteInt32(0);
193     reply.WriteInt32(ret);
194 
195     return 0;
196 }
OnRemoveDhcpRange(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)197 int DhcpServerStub::OnRemoveDhcpRange(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
198 {
199     DHCP_LOGI("OnRemoveDhcpRange\n");
200     DhcpRange range;
201     range.iptype = data.ReadInt32();
202     range.leaseHours = data.ReadInt32();
203     range.strTagName = data.ReadString();
204     range.strStartip = data.ReadString();
205     range.strEndip = data.ReadString();
206     range.strSubnet = data.ReadString();
207     std::string tagName = data.ReadString();
208     ErrCode ret = RemoveDhcpRange(tagName, range);
209     reply.WriteInt32(0);
210     reply.WriteInt32(ret);
211 
212     return 0;
213 }
OnGetDhcpClientInfos(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)214 int DhcpServerStub::OnGetDhcpClientInfos(uint32_t code, MessageParcel &data, MessageParcel &reply,
215     MessageOption &option)
216 {
217     DHCP_LOGI("OnGetDhcpClientInfos\n");
218     std::vector<std::string> leases;
219     std::string ifname = data.ReadString();
220     ErrCode ret = GetDhcpClientInfos(ifname, leases);
221     reply.WriteInt32(0);
222     reply.WriteInt32(ret);
223     if (ret != DHCP_E_SUCCESS) {
224         return ret;
225     }
226     int size = leases.size();
227     DHCP_LOGI("OnGetDhcpClientInfos, reply message size:%{public}d\n", size);
228     reply.WriteInt32(size);
229     for (auto str : leases) {
230         reply.WriteString(str);
231     }
232 
233     return 0;
234 }
235 
OnUpdateLeasesTime(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)236 int DhcpServerStub::OnUpdateLeasesTime(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
237 {
238     DHCP_LOGI("OnUpdateLeasesTime\n");
239     std::string leaseTime = data.ReadString();
240     ErrCode ret = UpdateLeasesTime(leaseTime);
241     reply.WriteInt32(0);
242     reply.WriteInt32(ret);
243 
244     return 0;
245 }
246 
OnPutDhcpRange(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)247 int DhcpServerStub::OnPutDhcpRange(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
248 {
249     DHCP_LOGI("OnPutDhcpRange\n");
250     DhcpRange range;
251     range.iptype = data.ReadInt32();
252     range.leaseHours = data.ReadInt32();
253     range.strTagName = data.ReadString();
254     range.strStartip = data.ReadString();
255     range.strEndip = data.ReadString();
256     range.strSubnet = data.ReadString();
257     std::string tagName = data.ReadString();
258     ErrCode ret = PutDhcpRange(tagName, range);
259     reply.WriteInt32(0);
260     reply.WriteInt32(ret);
261 
262     return 0;
263 }
264 }
265 }