1 /*
2 * Copyright (C) 2024 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
16 #include "dhcpserver_fuzzer.h"
17 #include "dhcp_define.h"
18 #include "dhcp_server.h"
19 #include "dhcp_event.h"
20
21 namespace OHOS {
22 namespace DHCP {
23 std::shared_ptr<DhcpServer> dhcpServer = DhcpServer::GetInstance(DHCP_SERVER_ABILITY_ID);
24 static OHOS::sptr<DhcpServerCallBack> dhcpServerCallBack =
25 OHOS::sptr<DhcpServerCallBack>(new (std::nothrow)DhcpServerCallBack());
DhcpServerFuzzerTest(const uint8_t * data,size_t size)26 bool DhcpServerFuzzerTest(const uint8_t* data, size_t size)
27 {
28 if (dhcpServer == nullptr) {
29 return false;
30 }
31 std::string ifname = std::string(reinterpret_cast<const char*>(data), size);
32 DhcpRange range;
33 int call = 2;
34 range.strTagName = std::string(reinterpret_cast<const char*>(data), size);
35 range.strStartip = std::string(reinterpret_cast<const char*>(data), size);
36 range.strEndip = std::string(reinterpret_cast<const char*>(data), size);
37 range.strSubnet = std::string(reinterpret_cast<const char*>(data), size);
38 range.iptype = static_cast<int>(data[0]) % call;
39 range.leaseHours = static_cast<int>(data[0]);
40 std::vector<std::string> dhcpClientInfo;
41 dhcpClientInfo.push_back(std::string(reinterpret_cast<const char*>(data), size));
42
43 dhcpServer->StartDhcpServer(ifname);
44 dhcpServer->StopDhcpServer(ifname);
45 dhcpServer->PutDhcpRange(ifname, range);
46 dhcpServer->RemoveDhcpRange(ifname, range);
47 dhcpServer->RemoveAllDhcpRange(ifname);
48 dhcpServer->SetDhcpRange(ifname, range);
49 dhcpServer->SetDhcpName(ifname, ifname);
50 dhcpServer->GetDhcpClientInfos(ifname, dhcpClientInfo);
51 dhcpServer->UpdateLeasesTime(ifname);
52 dhcpServer->RegisterDhcpServerCallBack(ifname, dhcpServerCallBack);
53
54 return true;
55 }
56 } // namespace DHCP
57 } // namespace OHOS
58
59 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)60 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
61 {
62 OHOS::DHCP::DhcpServerFuzzerTest(data, size);
63 return 0;
64 }
65