From f340ef1b0587439d3fd86440ae8e21a276ca7f89 Mon Sep 17 00:00:00 2001 From: Razvan Becheriu Date: Tue, 22 Oct 2024 17:39:57 +0300 Subject: [PATCH] [#3605] use server listener --- fuzz/fuzz_http_endpoint_kea_dhcp4.cc | 41 +++++----------------------- fuzz/fuzz_http_endpoint_kea_dhcp6.cc | 41 +++++----------------------- fuzz/fuzz_unix_socket_kea_dhcp4.cc | 2 +- fuzz/fuzz_unix_socket_kea_dhcp6.cc | 2 +- src/lib/config/http_command_mgr.cc | 2 +- 5 files changed, 17 insertions(+), 71 deletions(-) diff --git a/fuzz/fuzz_http_endpoint_kea_dhcp4.cc b/fuzz/fuzz_http_endpoint_kea_dhcp4.cc index 5fae753fd8..d17107e392 100644 --- a/fuzz/fuzz_http_endpoint_kea_dhcp4.cc +++ b/fuzz/fuzz_http_endpoint_kea_dhcp4.cc @@ -45,14 +45,10 @@ static pid_t const PID(getpid()); static int const PORT(getpid() % 1000 + 2000); static string const PID_STR(to_string(PID)); static string const PORT_STR(to_string(PORT)); +static string const ADDRESS("0.0.0.0"); static string const KEA_DHCP4_CONF(KEA_FUZZ_DIR + "/kea-dhcp4-" + PID_STR + ".conf"); static string const KEA_DHCP4_CSV(KEA_FUZZ_DIR + "/kea-dhcp4-" + PID_STR + ".csv"); -void timeoutHandler() { - cerr << "Timeout occurred while fuzzing!" << endl; - abort(); -} - /// @brief Represents HTTP POST request with JSON body. /// /// In addition to the requirements specified by the @ref PostHttpRequest @@ -97,8 +93,6 @@ struct PostHttpRequestBytes : PostHttpRequest { using PostHttpRequestBytesPtr = boost::shared_ptr; -ThreadPool> THREAD_POOL; - } // namespace extern "C" { @@ -113,13 +107,14 @@ LLVMFuzzerInitialize() { "Dhcp4": { "control-sockets": [ { - "socket-address": "0.0.0.0", + "socket-address": ")" + ADDRESS + R"(", "socket-port": )" + PORT_STR + R"(, "socket-type": "http" } ], "lease-database": { "name": ")" + KEA_DHCP4_CSV + R"(", + "persist": false, "type": "memfile" } } @@ -148,22 +143,9 @@ LLVMFuzzerTestOneInput(uint8_t const* data, size_t size) { ControlledDhcpv4Srv server; server.init(KEA_DHCP4_CONF); - string const address("127.0.0.1"); - int const port(18000); - int const timeout(100000); - - CmdHttpListener listener(IOAddress(address), port); - MultiThreadingMgr::instance().setMode(true); - - // Start the server. - listener.start(); - - // Create a client and specify the URL on which the server can be reached. - IOServicePtr io_service(new IOService()); - IntervalTimer run_io_service_timer(io_service); - HttpClient client(io_service, false); + HttpClient client(ControlledDhcpv4Srv::getInstance()->getIOService(), false); stringstream ss; - ss << "http://" << address << ":" << port; + ss << "http://" << ADDRESS << ":" << PORT_STR; Url url(ss.str()); // Initiate request to the server. @@ -182,14 +164,7 @@ LLVMFuzzerTestOneInput(uint8_t const* data, size_t size) { string const&) { }); - // Actually trigger the requests. The requests should be handlded by the - // server one after another. While the first request is being processed - // the server should queue another one. - io_service->getInternalIOService().reset(); - run_io_service_timer.setup(&timeoutHandler, timeout, IntervalTimer::ONE_SHOT); - io_service->runOne(); - io_service->getInternalIOService().reset(); - io_service->poll(); + ControlledDhcpv4Srv::getInstance()->getIOService()->poll(); // Make sure that the received responses are different. We check that by // comparing value of the sequence parameters. @@ -200,10 +175,8 @@ LLVMFuzzerTestOneInput(uint8_t const* data, size_t size) { cout << "no response" << endl; } } - listener.stop(); client.stop(); - run_io_service_timer.cancel(); - io_service->poll(); + ControlledDhcpv4Srv::getInstance()->getIOService()->poll(); MultiThreadingMgr::instance().setMode(false); return 0; diff --git a/fuzz/fuzz_http_endpoint_kea_dhcp6.cc b/fuzz/fuzz_http_endpoint_kea_dhcp6.cc index be2c78dbb1..08f4f7e796 100644 --- a/fuzz/fuzz_http_endpoint_kea_dhcp6.cc +++ b/fuzz/fuzz_http_endpoint_kea_dhcp6.cc @@ -45,14 +45,10 @@ static pid_t const PID(getpid()); static int const PORT(getpid() % 1000 + 2000); static string const PID_STR(to_string(PID)); static string const PORT_STR(to_string(PORT)); +static string const ADDRESS("::"); static string const KEA_DHCP6_CONF(KEA_FUZZ_DIR + "/kea-dhcp6-" + PID_STR + ".conf"); static string const KEA_DHCP6_CSV(KEA_FUZZ_DIR + "/kea-dhcp6-" + PID_STR + ".csv"); -void timeoutHandler() { - cerr << "Timeout occurred while fuzzing!" << endl; - abort(); -} - /// @brief Represents HTTP POST request with JSON body. /// /// In addition to the requirements specified by the @ref PostHttpRequest @@ -97,8 +93,6 @@ struct PostHttpRequestBytes : PostHttpRequest { using PostHttpRequestBytesPtr = boost::shared_ptr; -ThreadPool> THREAD_POOL; - } // namespace extern "C" { @@ -113,13 +107,14 @@ LLVMFuzzerInitialize() { "Dhcp6": { "control-sockets": [ { - "socket-address": "0.0.0.0", + "socket-address": ")" + ADDRESS + R"(", "socket-port": )" + PORT_STR + R"(, "socket-type": "http" } ], "lease-database": { "name": ")" + KEA_DHCP6_CSV + R"(", + "persist": false, "type": "memfile" }, "server-id": { @@ -154,22 +149,9 @@ LLVMFuzzerTestOneInput(uint8_t const* data, size_t size) { ControlledDhcpv6Srv server; server.init(KEA_DHCP6_CONF); - string const address("127.0.0.1"); - int const port(18000); - int const timeout(100000); - - CmdHttpListener listener(IOAddress(address), port); - MultiThreadingMgr::instance().setMode(true); - - // Start the server. - listener.start(); - - // Create a client and specify the URL on which the server can be reached. - IOServicePtr io_service(new IOService()); - IntervalTimer run_io_service_timer(io_service); - HttpClient client(io_service, false); + HttpClient client(ControlledDhcpv6Srv::getInstance()->getIOService(), false); stringstream ss; - ss << "http://" << address << ":" << port; + ss << "http://[" << ADDRESS << "]:" << PORT_STR; Url url(ss.str()); // Initiate request to the server. @@ -188,14 +170,7 @@ LLVMFuzzerTestOneInput(uint8_t const* data, size_t size) { string const&) { }); - // Actually trigger the requests. The requests should be handlded by the - // server one after another. While the first request is being processed - // the server should queue another one. - io_service->getInternalIOService().reset(); - run_io_service_timer.setup(&timeoutHandler, timeout, IntervalTimer::ONE_SHOT); - io_service->runOne(); - io_service->getInternalIOService().reset(); - io_service->poll(); + ControlledDhcpv6Srv::getInstance()->getIOService()->poll(); // Make sure that the received responses are different. We check that by // comparing value of the sequence parameters. @@ -206,10 +181,8 @@ LLVMFuzzerTestOneInput(uint8_t const* data, size_t size) { cout << "no response" << endl; } } - listener.stop(); client.stop(); - run_io_service_timer.cancel(); - io_service->poll(); + ControlledDhcpv6Srv::getInstance()->getIOService()->poll(); MultiThreadingMgr::instance().setMode(false); return 0; diff --git a/fuzz/fuzz_unix_socket_kea_dhcp4.cc b/fuzz/fuzz_unix_socket_kea_dhcp4.cc index c87124d5bc..2f0f0d5404 100644 --- a/fuzz/fuzz_unix_socket_kea_dhcp4.cc +++ b/fuzz/fuzz_unix_socket_kea_dhcp4.cc @@ -37,7 +37,6 @@ static string const KEA_DHCP4_CSV(KEA_FUZZ_DIR + "/kea-dhcp4-" + PID_STR + ".csv static string const SOCKET(KEA_FUZZ_DIR + "/kea-dhcp4-ctrl-" + PID_STR + ".sock"); static UnixControlClient CLIENT; -static IOServicePtr IO_SERVICE(new IOService()); } // namespace @@ -60,6 +59,7 @@ LLVMFuzzerInitialize() { }, "lease-database": { "name": ")" + KEA_DHCP4_CSV + R"(", + "persist": false, "type": "memfile" } } diff --git a/fuzz/fuzz_unix_socket_kea_dhcp6.cc b/fuzz/fuzz_unix_socket_kea_dhcp6.cc index 021dcf48db..837aa27ba9 100644 --- a/fuzz/fuzz_unix_socket_kea_dhcp6.cc +++ b/fuzz/fuzz_unix_socket_kea_dhcp6.cc @@ -37,7 +37,6 @@ static string const KEA_DHCP6_CSV(KEA_FUZZ_DIR + "/kea-dhcp6-" + PID_STR + ".csv static string const SOCKET(KEA_FUZZ_DIR + "/kea-dhcp6-ctrl-" + PID_STR + ".sock"); static UnixControlClient CLIENT; -static IOServicePtr IO_SERVICE(new IOService()); } // namespace @@ -60,6 +59,7 @@ LLVMFuzzerInitialize() { }, "lease-database": { "name": ")" + KEA_DHCP6_CSV + R"(", + "persist": false, "type": "memfile" }, "server-id": { diff --git a/src/lib/config/http_command_mgr.cc b/src/lib/config/http_command_mgr.cc index 2f396ae83e..e13cadebe8 100644 --- a/src/lib/config/http_command_mgr.cc +++ b/src/lib/config/http_command_mgr.cc @@ -165,7 +165,7 @@ HttpCommandMgrImpl::close(bool remove) { ostringstream ep; if (current_config_) { use_https = !current_config_->getCertFile().empty(); - ep << " bound to address " << current_config_->getSocketAddress() + ep << "bound to address " << current_config_->getSocketAddress() << " port " << current_config_->getSocketPort(); } LOG_INFO(command_logger, HTTP_COMMAND_MGR_SERVICE_STOPPING)