9#include <botan/internal/socket_udp.h>
11#include <botan/exceptn.h>
12#include <botan/mem_ops.h>
13#include <botan/internal/fmt.h>
14#include <botan/internal/uri.h>
17#if defined(BOTAN_HAS_BOOST_ASIO)
23 #define BOOST_ASIO_DISABLE_SERIAL_PORT
24 #include <boost/asio.hpp>
25 #include <boost/asio/system_timer.hpp>
26#elif defined(BOTAN_TARGET_OS_HAS_SOCKETS)
30 #include <netinet/in.h>
32 #include <sys/socket.h>
36#elif defined(BOTAN_TARGET_OS_HAS_WINSOCK2)
44#if defined(BOTAN_HAS_BOOST_ASIO)
45class Asio_SocketUDP
final :
public OS::SocketUDP {
47 Asio_SocketUDP(std::string_view hostname, std::string_view service, std::chrono::microseconds timeout) :
48 m_timeout(timeout), m_timer(m_io), m_udp(m_io) {
49 m_timer.expires_after(m_timeout);
52 boost::asio::ip::udp::resolver resolver(m_io);
53 boost::asio::ip::udp::resolver::results_type dns_iter =
54 resolver.resolve(std::string{hostname}, std::string{service});
56 boost::system::error_code ec = boost::asio::error::would_block;
58 auto connect_cb = [&ec](
const boost::system::error_code& e,
59 const boost::asio::ip::udp::resolver::results_type::iterator&) { ec = e; };
61 boost::asio::async_connect(m_udp, dns_iter.begin(), dns_iter.end(), connect_cb);
63 while(ec == boost::asio::error::would_block) {
68 throw boost::system::system_error(ec);
70 if(m_udp.is_open() ==
false) {
71 throw System_Error(
fmt(
"Connection to host {} failed", hostname));
75 void write(
const uint8_t buf[],
size_t len)
override {
76 m_timer.expires_after(m_timeout);
78 boost::system::error_code ec = boost::asio::error::would_block;
80 m_udp.async_send(boost::asio::buffer(buf, len), [&ec](boost::system::error_code e,
size_t) { ec = e; });
82 while(ec == boost::asio::error::would_block) {
87 throw boost::system::system_error(ec);
91 size_t read(uint8_t buf[],
size_t len)
override {
92 m_timer.expires_after(m_timeout);
94 boost::system::error_code ec = boost::asio::error::would_block;
97 m_udp.async_receive(boost::asio::buffer(buf, len), [&](boost::system::error_code cb_ec,
size_t cb_got) {
102 while(ec == boost::asio::error::would_block) {
107 if(ec == boost::asio::error::eof) {
110 throw boost::system::system_error(ec);
117 void check_timeout() {
118 if(m_udp.is_open() && m_timer.expiry() < std::chrono::system_clock::now()) {
119 boost::system::error_code err;
125 m_timer.async_wait(std::bind(&Asio_SocketUDP::check_timeout,
this));
128 const std::chrono::microseconds m_timeout;
129 boost::asio::io_context m_io;
130 boost::asio::system_timer m_timer;
131 boost::asio::ip::udp::socket m_udp;
133#elif defined(BOTAN_TARGET_OS_HAS_SOCKETS) || defined(BOTAN_TARGET_OS_HAS_WINSOCK2)
134class BSD_SocketUDP
final :
public OS::SocketUDP {
136 BSD_SocketUDP(std::string_view hostname, std::string_view service, std::chrono::microseconds timeout) :
140 m_socket = invalid_socket();
144 clear_mem(&hints, 1);
145 hints.ai_family = AF_UNSPEC;
146 hints.ai_socktype = SOCK_DGRAM;
148 const std::string hostname_str(hostname);
149 const std::string service_str(service);
151 int rc = ::getaddrinfo(hostname_str.c_str(), service_str.c_str(), &hints, &res);
154 throw System_Error(fmt(
"Name resolution failed for {}", hostname), rc);
157 for(addrinfo* rp = res; (m_socket == invalid_socket()) && (rp !=
nullptr); rp = rp->ai_next) {
158 if(rp->ai_family != AF_INET && rp->ai_family != AF_INET6) {
162 m_socket = ::socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
164 if(m_socket == invalid_socket()) [[unlikely]] {
169 set_nonblocking(m_socket);
170 memcpy(&sa, res->ai_addr, res->ai_addrlen);
171 salen =
static_cast<socklen_t
>(res->ai_addrlen);
176 if(m_socket == invalid_socket()) {
177 throw System_Error(fmt(
"Connecting to {} for service {} failed with errno {}", hostname, service, errno),
182 ~BSD_SocketUDP()
override {
183 close_socket(m_socket);
184 m_socket = invalid_socket();
188 BSD_SocketUDP(
const BSD_SocketUDP& other) =
delete;
189 BSD_SocketUDP(BSD_SocketUDP&& other) =
delete;
190 BSD_SocketUDP& operator=(
const BSD_SocketUDP& other) =
delete;
191 BSD_SocketUDP& operator=(BSD_SocketUDP&& other) =
delete;
193 void write(
const uint8_t buf[],
size_t len)
override {
196 FD_SET(m_socket, &write_set);
198 size_t sent_so_far = 0;
199 while(sent_so_far != len) {
200 struct timeval timeout = make_timeout_tv();
201 int active = ::select(
static_cast<int>(m_socket + 1),
nullptr, &write_set,
nullptr, &timeout);
204 throw System_Error(
"Timeout during socket write");
207 const size_t left = len - sent_so_far;
208 socket_op_ret_type sent = ::sendto(m_socket,
209 cast_uint8_ptr_to_char(buf + sent_so_far),
210 static_cast<sendrecv_len_type
>(left),
212 reinterpret_cast<sockaddr*
>(&sa),
215 throw System_Error(
"Socket write failed", errno);
217 sent_so_far +=
static_cast<size_t>(sent);
222 size_t read(uint8_t buf[],
size_t len)
override {
225 FD_SET(m_socket, &read_set);
227 struct timeval timeout = make_timeout_tv();
228 int active = ::select(
static_cast<int>(m_socket + 1), &read_set,
nullptr,
nullptr, &timeout);
231 throw System_Error(
"Timeout during socket read");
234 socket_op_ret_type got =
235 ::recvfrom(m_socket, cast_uint8_ptr_to_char(buf),
static_cast<sendrecv_len_type
>(len), 0,
nullptr,
nullptr);
238 throw System_Error(
"Socket read failed", errno);
241 return static_cast<size_t>(got);
245 #if defined(BOTAN_TARGET_OS_HAS_WINSOCK2)
246 typedef SOCKET socket_type;
247 typedef int socket_op_ret_type;
248 typedef int sendrecv_len_type;
250 static socket_type invalid_socket() {
return INVALID_SOCKET; }
252 static void close_socket(socket_type s) { ::closesocket(s); }
254 static std::string get_last_socket_error() {
return std::to_string(::WSAGetLastError()); }
256 static bool nonblocking_connect_in_progress() {
return (::WSAGetLastError() == WSAEWOULDBLOCK); }
258 static void set_nonblocking(socket_type s) {
259 u_long nonblocking = 1;
260 ::ioctlsocket(s, FIONBIO, &nonblocking);
263 static void socket_init() {
265 WORD wsa_version = MAKEWORD(2, 2);
267 if(::WSAStartup(wsa_version, &wsa_data) != 0) {
268 throw System_Error(
"WSAStartup() failed", WSAGetLastError());
271 if(LOBYTE(wsa_data.wVersion) != 2 || HIBYTE(wsa_data.wVersion) != 2) {
273 throw System_Error(
"Could not find a usable version of Winsock.dll");
277 static void socket_fini() { ::WSACleanup(); }
279 typedef int socket_type;
280 typedef ssize_t socket_op_ret_type;
281 typedef size_t sendrecv_len_type;
283 static socket_type invalid_socket() {
return -1; }
285 static void close_socket(socket_type s) { ::close(s); }
287 static std::string get_last_socket_error() { return ::strerror(errno); }
289 static bool nonblocking_connect_in_progress() {
return (errno == EINPROGRESS); }
291 static void set_nonblocking(socket_type s) {
292 if(::fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
293 throw System_Error(
"Setting socket to non-blocking state failed", errno);
297 static void socket_init() {}
299 static void socket_fini() {}
304 struct timeval make_timeout_tv() const {
306 tv.tv_sec =
static_cast<decltype(timeval::tv_sec)
>(m_timeout.count() / 1000000);
307 tv.tv_usec =
static_cast<decltype(timeval::tv_usec)
>(m_timeout.count() % 1000000);
311 const std::chrono::microseconds m_timeout;
312 socket_type m_socket;
318 std::string_view service,
319 std::chrono::microseconds timeout) {
320#if defined(BOTAN_HAS_BOOST_ASIO)
321 return std::make_unique<Asio_SocketUDP>(hostname, service, timeout);
322#elif defined(BOTAN_TARGET_OS_HAS_SOCKETS) || defined(BOTAN_TARGET_OS_HAS_WINSOCK2)
323 return std::make_unique<BSD_SocketUDP>(hostname, service, timeout);
328 return std::unique_ptr<OS::SocketUDP>();
332std::unique_ptr<OS::SocketUDP>
OS::open_socket_udp(std::string_view uri_string, std::chrono::microseconds timeout) {
334 if(uri.port() == 0) {
337 return open_socket_udp(uri.host(), std::to_string(uri.port()), timeout);
332std::unique_ptr<OS::SocketUDP>
OS::open_socket_udp(std::string_view uri_string, std::chrono::microseconds timeout) {
…}
static URI from_any(std::string_view uri)
int(* final)(unsigned char *, CTX *)
std::unique_ptr< SocketUDP > BOTAN_TEST_API open_socket_udp(std::string_view hostname, std::string_view service, std::chrono::microseconds timeout)
std::string fmt(std::string_view format, const T &... args)