1518678f8Sopenharmony_ci/* 2518678f8Sopenharmony_ci * Copyright (C) 2024 Huawei Device Co., Ltd. 3518678f8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4518678f8Sopenharmony_ci * you may not use this file except in compliance with the License. 5518678f8Sopenharmony_ci * You may obtain a copy of the License at 6518678f8Sopenharmony_ci * 7518678f8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8518678f8Sopenharmony_ci * 9518678f8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10518678f8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11518678f8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12518678f8Sopenharmony_ci * See the License for the specific language governing permissions and 13518678f8Sopenharmony_ci * limitations under the License. 14518678f8Sopenharmony_ci */ 15518678f8Sopenharmony_ci 16518678f8Sopenharmony_ci#include "dhcpserver_fuzzer.h" 17518678f8Sopenharmony_ci#include "dhcp_define.h" 18518678f8Sopenharmony_ci#include "dhcp_server.h" 19518678f8Sopenharmony_ci#include "dhcp_event.h" 20518678f8Sopenharmony_ci 21518678f8Sopenharmony_cinamespace OHOS { 22518678f8Sopenharmony_cinamespace DHCP { 23518678f8Sopenharmony_ci std::shared_ptr<DhcpServer> dhcpServer = DhcpServer::GetInstance(DHCP_SERVER_ABILITY_ID); 24518678f8Sopenharmony_ci static OHOS::sptr<DhcpServerCallBack> dhcpServerCallBack = 25518678f8Sopenharmony_ci OHOS::sptr<DhcpServerCallBack>(new (std::nothrow)DhcpServerCallBack()); 26518678f8Sopenharmony_ci bool DhcpServerFuzzerTest(const uint8_t* data, size_t size) 27518678f8Sopenharmony_ci { 28518678f8Sopenharmony_ci if (dhcpServer == nullptr) { 29518678f8Sopenharmony_ci return false; 30518678f8Sopenharmony_ci } 31518678f8Sopenharmony_ci std::string ifname = std::string(reinterpret_cast<const char*>(data), size); 32518678f8Sopenharmony_ci DhcpRange range; 33518678f8Sopenharmony_ci int call = 2; 34518678f8Sopenharmony_ci range.strTagName = std::string(reinterpret_cast<const char*>(data), size); 35518678f8Sopenharmony_ci range.strStartip = std::string(reinterpret_cast<const char*>(data), size); 36518678f8Sopenharmony_ci range.strEndip = std::string(reinterpret_cast<const char*>(data), size); 37518678f8Sopenharmony_ci range.strSubnet = std::string(reinterpret_cast<const char*>(data), size); 38518678f8Sopenharmony_ci range.iptype = static_cast<int>(data[0]) % call; 39518678f8Sopenharmony_ci range.leaseHours = static_cast<int>(data[0]); 40518678f8Sopenharmony_ci std::vector<std::string> dhcpClientInfo; 41518678f8Sopenharmony_ci dhcpClientInfo.push_back(std::string(reinterpret_cast<const char*>(data), size)); 42518678f8Sopenharmony_ci 43518678f8Sopenharmony_ci dhcpServer->StartDhcpServer(ifname); 44518678f8Sopenharmony_ci dhcpServer->StopDhcpServer(ifname); 45518678f8Sopenharmony_ci dhcpServer->PutDhcpRange(ifname, range); 46518678f8Sopenharmony_ci dhcpServer->RemoveDhcpRange(ifname, range); 47518678f8Sopenharmony_ci dhcpServer->RemoveAllDhcpRange(ifname); 48518678f8Sopenharmony_ci dhcpServer->SetDhcpRange(ifname, range); 49518678f8Sopenharmony_ci dhcpServer->SetDhcpName(ifname, ifname); 50518678f8Sopenharmony_ci dhcpServer->GetDhcpClientInfos(ifname, dhcpClientInfo); 51518678f8Sopenharmony_ci dhcpServer->UpdateLeasesTime(ifname); 52518678f8Sopenharmony_ci dhcpServer->RegisterDhcpServerCallBack(ifname, dhcpServerCallBack); 53518678f8Sopenharmony_ci 54518678f8Sopenharmony_ci return true; 55518678f8Sopenharmony_ci } 56518678f8Sopenharmony_ci} // namespace DHCP 57518678f8Sopenharmony_ci} // namespace OHOS 58518678f8Sopenharmony_ci 59518678f8Sopenharmony_ci/* Fuzzer entry point */ 60518678f8Sopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) 61518678f8Sopenharmony_ci{ 62518678f8Sopenharmony_ci OHOS::DHCP::DhcpServerFuzzerTest(data, size); 63518678f8Sopenharmony_ci return 0; 64518678f8Sopenharmony_ci} 65