Skip to content

Commit

Permalink
[#3605] use server listener
Browse files Browse the repository at this point in the history
  • Loading branch information
Razvan Becheriu authored and andrei-pavel committed Oct 23, 2024
1 parent 9f7d0a9 commit f340ef1
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 71 deletions.
41 changes: 7 additions & 34 deletions fuzz/fuzz_http_endpoint_kea_dhcp4.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -97,8 +93,6 @@ struct PostHttpRequestBytes : PostHttpRequest {

using PostHttpRequestBytesPtr = boost::shared_ptr<PostHttpRequestBytes>;

ThreadPool<function<void()>> THREAD_POOL;

} // namespace

extern "C" {
Expand All @@ -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"
}
}
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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;
Expand Down
41 changes: 7 additions & 34 deletions fuzz/fuzz_http_endpoint_kea_dhcp6.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -97,8 +93,6 @@ struct PostHttpRequestBytes : PostHttpRequest {

using PostHttpRequestBytesPtr = boost::shared_ptr<PostHttpRequestBytes>;

ThreadPool<function<void()>> THREAD_POOL;

} // namespace

extern "C" {
Expand All @@ -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": {
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion fuzz/fuzz_unix_socket_kea_dhcp4.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -60,6 +59,7 @@ LLVMFuzzerInitialize() {
},
"lease-database": {
"name": ")" + KEA_DHCP4_CSV + R"(",
"persist": false,
"type": "memfile"
}
}
Expand Down
2 changes: 1 addition & 1 deletion fuzz/fuzz_unix_socket_kea_dhcp6.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -60,6 +59,7 @@ LLVMFuzzerInitialize() {
},
"lease-database": {
"name": ")" + KEA_DHCP6_CSV + R"(",
"persist": false,
"type": "memfile"
},
"server-id": {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/config/http_command_mgr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f340ef1

Please sign in to comment.