Skip to content

Commit b42dd24

Browse files
committed
feat: add async DNS resolver I/O object with Windows IOCP support
- Add resolver class with resolve() awaitable returning io_result<resolver_results> - Add resolver_results and resolver_entry for holding DNS resolution results - Add resolve_flags enum for controlling resolution behavior - Implement win_iocp_resolver_service using GetAddrInfoExW for async DNS - Require Windows 8+ SDK (_WIN32_WINNT >= 0x0602) for GetAddrInfoExW - Add nslookup example demonstrating resolver usage
1 parent 3aba311 commit b42dd24

9 files changed

Lines changed: 1226 additions & 0 deletions

File tree

example/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
add_subdirectory(client)
1111
add_subdirectory(echo-server)
12+
add_subdirectory(nslookup)
1213

1314
if(WolfSSL_FOUND)
1415
add_subdirectory(https-client)

example/nslookup/CMakeLists.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#
2+
# Copyright (c) 2025 Vinnie Falco (vinnie dot falco at gmail dot com)
3+
#
4+
# Distributed under the Boost Software License, Version 1.0. (See accompanying
5+
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
#
7+
# Official repository: https://github.com/cppalliance/corosio
8+
#
9+
10+
file(GLOB_RECURSE PFILES CONFIGURE_DEPENDS *.cpp *.hpp
11+
CMakeLists.txt
12+
Jamfile)
13+
14+
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} PREFIX "" FILES ${PFILES})
15+
16+
add_executable(corosio_example_nslookup ${PFILES})
17+
18+
set_property(TARGET corosio_example_nslookup
19+
PROPERTY FOLDER "examples")
20+
21+
target_link_libraries(corosio_example_nslookup
22+
Boost::corosio)

example/nslookup/nslookup.cpp

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//
2+
// Copyright (c) 2025 Vinnie Falco (vinnie dot falco at gmail dot com)
3+
//
4+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
//
7+
// Official repository: https://github.com/cppalliance/corosio
8+
//
9+
10+
#include <boost/corosio.hpp>
11+
#include <boost/capy/task.hpp>
12+
#include <boost/capy/ex/async_run.hpp>
13+
14+
#include <cstdlib>
15+
#include <iostream>
16+
#include <string_view>
17+
18+
namespace corosio = boost::corosio;
19+
namespace capy = boost::capy;
20+
21+
// Coroutine that performs the DNS lookup
22+
capy::task<void>
23+
do_lookup(
24+
corosio::io_context& ioc,
25+
std::string_view host,
26+
std::string_view service)
27+
{
28+
corosio::resolver r(ioc);
29+
30+
auto [ec, results] = co_await r.resolve(host, service);
31+
if (ec)
32+
{
33+
std::cerr << "Resolve failed: " << ec.message() << "\n";
34+
co_return;
35+
}
36+
37+
std::cout << "Results for " << host;
38+
if (!service.empty())
39+
std::cout << ":" << service;
40+
std::cout << "\n";
41+
42+
for (auto const& entry : results)
43+
{
44+
auto ep = entry.get_endpoint();
45+
if (ep.is_v4())
46+
{
47+
std::cout << " IPv4: " << ep.v4_address().to_string()
48+
<< ":" << ep.port() << "\n";
49+
}
50+
else
51+
{
52+
std::cout << " IPv6: " << ep.v6_address().to_string()
53+
<< ":" << ep.port() << "\n";
54+
}
55+
}
56+
57+
std::cout << "\nTotal: " << results.size() << " addresses\n";
58+
}
59+
60+
int
61+
main(int argc, char* argv[])
62+
{
63+
if (argc < 2 || argc > 3)
64+
{
65+
std::cerr <<
66+
"Usage: nslookup <hostname> [service]\n"
67+
"Examples:\n"
68+
" nslookup www.google.com\n"
69+
" nslookup www.google.com https\n"
70+
" nslookup localhost 8080\n";
71+
return EXIT_FAILURE;
72+
}
73+
74+
std::string_view host = argv[1];
75+
std::string_view service = (argc == 3) ? argv[2] : "";
76+
77+
corosio::io_context ioc;
78+
capy::async_run(ioc.get_executor())(
79+
do_lookup(ioc, host, service));
80+
ioc.run();
81+
82+
return EXIT_SUCCESS;
83+
}

include/boost/corosio.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,7 @@
1717
#include <boost/corosio/consuming_buffers.hpp>
1818
#include <boost/corosio/read.hpp>
1919
#include <boost/corosio/write.hpp>
20+
#include <boost/corosio/resolver.hpp>
21+
#include <boost/corosio/resolver_results.hpp>
2022

2123
#endif

0 commit comments

Comments
 (0)