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_from_now(m_timeout);
52 boost::asio::ip::udp::resolver resolver(m_io);
53 boost::asio::ip::udp::resolver::query query(std::string{hostname}, std::string{service});
54 boost::asio::ip::udp::resolver::iterator dns_iter = resolver.resolve(query);
56 boost::system::error_code ec = boost::asio::error::would_block;
58 auto connect_cb = [&ec](
const boost::system::error_code& e,
const boost::asio::ip::udp::resolver::iterator&) {
62 boost::asio::async_connect(m_udp, dns_iter, connect_cb);
64 while(ec == boost::asio::error::would_block) {
69 throw boost::system::system_error(ec);
71 if(m_udp.is_open() ==
false) {
72 throw System_Error(
fmt(
"Connection to host {} failed", hostname));
76 void write(
const uint8_t buf[],
size_t len)
override {
77 m_timer.expires_from_now(m_timeout);
79 boost::system::error_code ec = boost::asio::error::would_block;
81 m_udp.async_send(boost::asio::buffer(buf, len), [&ec](boost::system::error_code e,
size_t) { ec = e; });
83 while(ec == boost::asio::error::would_block) {
88 throw boost::system::system_error(ec);
92 size_t read(uint8_t buf[],
size_t len)
override {
93 m_timer.expires_from_now(m_timeout);
95 boost::system::error_code ec = boost::asio::error::would_block;
98 m_udp.async_receive(boost::asio::buffer(buf, len), [&](boost::system::error_code cb_ec,
size_t cb_got) {
103 while(ec == boost::asio::error::would_block) {
108 if(ec == boost::asio::error::eof) {
111 throw boost::system::system_error(ec);
118 void check_timeout() {
119 if(m_udp.is_open() && m_timer.expires_at() < std::chrono::system_clock::now()) {
120 boost::system::error_code err;
126 m_timer.async_wait(std::bind(&Asio_SocketUDP::check_timeout,
this));
129 const std::chrono::microseconds m_timeout;
130 boost::asio::io_service m_io;
131 boost::asio::system_timer m_timer;
132 boost::asio::ip::udp::socket m_udp;
134#elif defined(BOTAN_TARGET_OS_HAS_SOCKETS) || defined(BOTAN_TARGET_OS_HAS_WINSOCK2)
135class BSD_SocketUDP
final :
public OS::SocketUDP {
137 BSD_SocketUDP(std::string_view hostname, std::string_view service, std::chrono::microseconds timeout) :
141 m_socket = invalid_socket();
145 clear_mem(&hints, 1);
146 hints.ai_family = AF_UNSPEC;
147 hints.ai_socktype = SOCK_DGRAM;
149 const std::string hostname_str(hostname);
150 const std::string service_str(service);
152 int rc = ::getaddrinfo(hostname_str.c_str(), service_str.c_str(), &hints, &res);
155 throw System_Error(fmt(
"Name resolution failed for {}", hostname), rc);
158 for(addrinfo* rp = res; (m_socket == invalid_socket()) && (rp !=
nullptr); rp = rp->ai_next) {
159 if(rp->ai_family != AF_INET && rp->ai_family != AF_INET6) {
163 m_socket = ::socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
165 if(m_socket == invalid_socket()) [[unlikely]] {
170 set_nonblocking(m_socket);
171 memcpy(&sa, res->ai_addr, res->ai_addrlen);
172 salen =
static_cast<socklen_t
>(res->ai_addrlen);
177 if(m_socket == invalid_socket()) {
178 throw System_Error(fmt(
"Connecting to {} for service {} failed with errno {}", hostname, service, errno),
183 ~BSD_SocketUDP()
override {
184 close_socket(m_socket);
185 m_socket = invalid_socket();
189 BSD_SocketUDP(
const BSD_SocketUDP& other) =
delete;
190 BSD_SocketUDP(BSD_SocketUDP&& other) =
delete;
191 BSD_SocketUDP& operator=(
const BSD_SocketUDP& other) =
delete;
192 BSD_SocketUDP& operator=(BSD_SocketUDP&& other) =
delete;
194 void write(
const uint8_t buf[],
size_t len)
override {
197 FD_SET(m_socket, &write_set);
199 size_t sent_so_far = 0;
200 while(sent_so_far != len) {
201 struct timeval timeout = make_timeout_tv();
202 int active = ::select(
static_cast<int>(m_socket + 1),
nullptr, &write_set,
nullptr, &timeout);
205 throw System_Error(
"Timeout during socket write");
208 const size_t left = len - sent_so_far;
209 socket_op_ret_type sent = ::sendto(m_socket,
210 cast_uint8_ptr_to_char(buf + sent_so_far),
211 static_cast<sendrecv_len_type
>(left),
213 reinterpret_cast<sockaddr*
>(&sa),
216 throw System_Error(
"Socket write failed", errno);
218 sent_so_far +=
static_cast<size_t>(sent);
223 size_t read(uint8_t buf[],
size_t len)
override {
226 FD_SET(m_socket, &read_set);
228 struct timeval timeout = make_timeout_tv();
229 int active = ::select(
static_cast<int>(m_socket + 1), &read_set,
nullptr,
nullptr, &timeout);
232 throw System_Error(
"Timeout during socket read");
235 socket_op_ret_type got =
236 ::recvfrom(m_socket, cast_uint8_ptr_to_char(buf),
static_cast<sendrecv_len_type
>(len), 0,
nullptr,
nullptr);
239 throw System_Error(
"Socket read failed", errno);
242 return static_cast<size_t>(got);
246 #if defined(BOTAN_TARGET_OS_HAS_WINSOCK2)
247 typedef SOCKET socket_type;
248 typedef int socket_op_ret_type;
249 typedef int sendrecv_len_type;
251 static socket_type invalid_socket() {
return INVALID_SOCKET; }
253 static void close_socket(socket_type s) { ::closesocket(s); }
255 static std::string get_last_socket_error() {
return std::to_string(::WSAGetLastError()); }
257 static bool nonblocking_connect_in_progress() {
return (::WSAGetLastError() == WSAEWOULDBLOCK); }
259 static void set_nonblocking(socket_type s) {
260 u_long nonblocking = 1;
261 ::ioctlsocket(s, FIONBIO, &nonblocking);
264 static void socket_init() {
266 WORD wsa_version = MAKEWORD(2, 2);
268 if(::WSAStartup(wsa_version, &wsa_data) != 0) {
269 throw System_Error(
"WSAStartup() failed", WSAGetLastError());
272 if(LOBYTE(wsa_data.wVersion) != 2 || HIBYTE(wsa_data.wVersion) != 2) {
274 throw System_Error(
"Could not find a usable version of Winsock.dll");
278 static void socket_fini() { ::WSACleanup(); }
280 typedef int socket_type;
281 typedef ssize_t socket_op_ret_type;
282 typedef size_t sendrecv_len_type;
284 static socket_type invalid_socket() {
return -1; }
286 static void close_socket(socket_type s) { ::close(s); }
288 static std::string get_last_socket_error() { return ::strerror(errno); }
290 static bool nonblocking_connect_in_progress() {
return (errno == EINPROGRESS); }
292 static void set_nonblocking(socket_type s) {
293 if(::fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
294 throw System_Error(
"Setting socket to non-blocking state failed", errno);
298 static void socket_init() {}
300 static void socket_fini() {}
305 struct timeval make_timeout_tv() const {
307 tv.tv_sec =
static_cast<decltype(timeval::tv_sec)
>(m_timeout.count() / 1000000);
308 tv.tv_usec =
static_cast<decltype(timeval::tv_usec)
>(m_timeout.count() % 1000000);
312 const std::chrono::microseconds m_timeout;
313 socket_type m_socket;
319 std::string_view service,
320 std::chrono::microseconds timeout) {
321#if defined(BOTAN_HAS_BOOST_ASIO)
322 return std::make_unique<Asio_SocketUDP>(hostname, service, timeout);
323#elif defined(BOTAN_TARGET_OS_HAS_SOCKETS) || defined(BOTAN_TARGET_OS_HAS_WINSOCK2)
324 return std::make_unique<BSD_SocketUDP>(hostname, service, timeout);
329 return std::unique_ptr<OS::SocketUDP>();
333std::unique_ptr<OS::SocketUDP>
OS::open_socket_udp(std::string_view uri_string, std::chrono::microseconds timeout) {
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)
static URI fromAny(std::string_view uri)