9#include <botan/internal/socket_udp.h>
10#include <botan/internal/uri.h>
11#include <botan/exceptn.h>
12#include <botan/mem_ops.h>
15#if defined(BOTAN_HAS_BOOST_ASIO)
21 #define BOOST_ASIO_DISABLE_SERIAL_PORT
22 #include <boost/asio.hpp>
23 #include <boost/asio/system_timer.hpp>
24#elif defined(BOTAN_TARGET_OS_HAS_SOCKETS)
25 #include <sys/socket.h>
27 #include <netinet/in.h>
34#elif defined(BOTAN_TARGET_OS_HAS_WINSOCK2)
42#if defined(BOTAN_HAS_BOOST_ASIO)
43class Asio_SocketUDP
final :
public OS::SocketUDP
46 Asio_SocketUDP(
const std::string& hostname,
47 const std::string& service,
48 std::chrono::microseconds timeout) :
49 m_timeout(timeout), m_timer(m_io), m_udp(m_io)
51 m_timer.expires_from_now(m_timeout);
54 boost::asio::ip::udp::resolver resolver(m_io);
55 boost::asio::ip::udp::resolver::query query(hostname, service);
56 boost::asio::ip::udp::resolver::iterator dns_iter = resolver.resolve(query);
58 boost::system::error_code ec = boost::asio::error::would_block;
60 auto connect_cb = [&ec](
const boost::system::error_code& e,
61 boost::asio::ip::udp::resolver::iterator) { ec = e; };
63 boost::asio::async_connect(m_udp, dns_iter, connect_cb);
65 while(ec == boost::asio::error::would_block)
71 {
throw boost::system::system_error(ec); }
72 if(m_udp.is_open() ==
false)
73 {
throw System_Error(
"Connection to host " + hostname +
" failed"); }
76 void write(
const uint8_t buf[],
size_t len)
override
78 m_timer.expires_from_now(m_timeout);
80 boost::system::error_code ec = boost::asio::error::would_block;
82 m_udp.async_send(boost::asio::buffer(buf, len),
83 [&ec](boost::system::error_code e,
size_t) { ec = e; });
85 while(ec == boost::asio::error::would_block)
92 throw boost::system::system_error(ec);
96 size_t read(uint8_t buf[],
size_t len)
override
98 m_timer.expires_from_now(m_timeout);
100 boost::system::error_code ec = boost::asio::error::would_block;
103 m_udp.async_receive(boost::asio::buffer(buf, len),
104 [&](boost::system::error_code cb_ec,
size_t cb_got) { ec = cb_ec; got = cb_got; });
106 while(ec == boost::asio::error::would_block)
113 if(ec == boost::asio::error::eof)
115 throw boost::system::system_error(ec);
124 if(m_udp.is_open() && m_timer.expires_at() < std::chrono::system_clock::now())
126 boost::system::error_code err;
130 m_timer.async_wait(std::bind(&Asio_SocketUDP::check_timeout,
this));
133 const std::chrono::microseconds m_timeout;
134 boost::asio::io_service m_io;
135 boost::asio::system_timer m_timer;
136 boost::asio::ip::udp::socket m_udp;
138#elif defined(BOTAN_TARGET_OS_HAS_SOCKETS) || defined(BOTAN_TARGET_OS_HAS_WINSOCK2)
139class BSD_SocketUDP
final :
public OS::SocketUDP
142 BSD_SocketUDP(
const std::string& hostname,
143 const std::string& service,
144 std::chrono::microseconds timeout) : m_timeout(timeout)
148 m_socket = invalid_socket();
153 hints.ai_family = AF_UNSPEC;
154 hints.ai_socktype = SOCK_DGRAM;
156 int rc = ::getaddrinfo(hostname.c_str(), service.c_str(), &hints, &res);
160 throw System_Error(
"Name resolution failed for " + hostname, rc);
163 for(addrinfo* rp = res; (m_socket == invalid_socket()) && (rp !=
nullptr); rp = rp->ai_next)
165 if(rp->ai_family != AF_INET && rp->ai_family != AF_INET6)
168 m_socket = ::socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
170 if(m_socket == invalid_socket())
176 set_nonblocking(m_socket);
177 memcpy(&sa, res->ai_addr, res->ai_addrlen);
178 salen =
static_cast<socklen_t
>(res->ai_addrlen);
183 if(m_socket == invalid_socket())
185 throw System_Error(
"Connecting to " + hostname +
186 " for service " + service +
" failed", errno);
190 ~BSD_SocketUDP()
override
192 close_socket(m_socket);
193 m_socket = invalid_socket();
197 BSD_SocketUDP(
const BSD_SocketUDP& other) =
delete;
198 BSD_SocketUDP(BSD_SocketUDP&& other) =
delete;
199 BSD_SocketUDP& operator=(
const BSD_SocketUDP& other) =
delete;
200 BSD_SocketUDP& operator=(BSD_SocketUDP&& other) =
delete;
202 void write(
const uint8_t buf[],
size_t len)
override
206 FD_SET(m_socket, &write_set);
208 size_t sent_so_far = 0;
209 while(sent_so_far != len)
211 struct timeval timeout = make_timeout_tv();
212 int active = ::select(
static_cast<int>(m_socket + 1),
nullptr, &write_set,
nullptr, &timeout);
215 {
throw System_Error(
"Timeout during socket write"); }
217 const size_t left = len - sent_so_far;
219 static_cast<sendrecv_len_type
>(left), 0,
220 reinterpret_cast<sockaddr*
>(&sa), salen);
222 {
throw System_Error(
"Socket write failed", errno); }
224 { sent_so_far +=
static_cast<size_t>(sent); }
228 size_t read(uint8_t buf[],
size_t len)
override
232 FD_SET(m_socket, &read_set);
234 struct timeval timeout = make_timeout_tv();
235 int active = ::select(
static_cast<int>(m_socket + 1), &read_set,
nullptr,
nullptr, &timeout);
238 {
throw System_Error(
"Timeout during socket read"); }
240 socket_op_ret_type got = ::recvfrom(m_socket,
cast_uint8_ptr_to_char(buf),
static_cast<sendrecv_len_type
>(len), 0,
nullptr,
nullptr);
243 {
throw System_Error(
"Socket read failed", errno); }
245 return static_cast<size_t>(got);
249#if defined(BOTAN_TARGET_OS_HAS_WINSOCK2)
250 typedef SOCKET socket_type;
251 typedef int socket_op_ret_type;
252 typedef int sendrecv_len_type;
253 static socket_type invalid_socket() {
return INVALID_SOCKET; }
254 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()
259 return (::WSAGetLastError() == WSAEWOULDBLOCK);
262 static void set_nonblocking(socket_type s)
264 u_long nonblocking = 1;
265 ::ioctlsocket(s, FIONBIO, &nonblocking);
268 static void socket_init()
271 WORD wsa_version = MAKEWORD(2, 2);
273 if(::WSAStartup(wsa_version, &wsa_data) != 0)
275 throw System_Error(
"WSAStartup() failed", WSAGetLastError());
278 if(LOBYTE(wsa_data.wVersion) != 2 || HIBYTE(wsa_data.wVersion) != 2)
281 throw System_Error(
"Could not find a usable version of Winsock.dll");
285 static void socket_fini()
290 typedef int socket_type;
291 typedef ssize_t socket_op_ret_type;
292 typedef size_t sendrecv_len_type;
293 static socket_type invalid_socket() {
return -1; }
294 static void close_socket(socket_type s) { ::close(s); }
295 static std::string get_last_socket_error() { return ::strerror(errno); }
296 static bool nonblocking_connect_in_progress() {
return (errno == EINPROGRESS); }
297 static void set_nonblocking(socket_type s)
299 if(::fcntl(s, F_SETFL, O_NONBLOCK) < 0)
300 {
throw System_Error(
"Setting socket to non-blocking state failed", errno); }
303 static void socket_init() {}
304 static void socket_fini() {}
308 struct timeval make_timeout_tv() const
311 tv.tv_sec =
static_cast<decltype(timeval::tv_sec)
>(m_timeout.count() / 1000000);
312 tv.tv_usec =
static_cast<decltype(timeval::tv_usec)
>(m_timeout.count() % 1000000);;
316 const std::chrono::microseconds m_timeout;
317 socket_type m_socket;
322std::unique_ptr<OS::SocketUDP>
324 const std::string& service,
325 std::chrono::microseconds timeout)
327#if defined(BOTAN_HAS_BOOST_ASIO)
328 return std::make_unique<Asio_SocketUDP>(hostname, service, timeout);
329#elif defined(BOTAN_TARGET_OS_HAS_SOCKETS) || defined(BOTAN_TARGET_OS_HAS_WINSOCK2)
330 return std::make_unique<BSD_SocketUDP>(hostname, service, timeout);
335 return std::unique_ptr<OS::SocketUDP>();
339std::unique_ptr<OS::SocketUDP>
341 std::chrono::microseconds timeout)
#define BOTAN_UNUSED(...)
int(* final)(unsigned char *, CTX *)
std::string to_string(const BER_Object &obj)
std::unique_ptr< SocketUDP > BOTAN_TEST_API open_socket_udp(const std::string &hostname, const std::string &service, std::chrono::microseconds timeout)
const char * cast_uint8_ptr_to_char(const uint8_t *b)
constexpr void clear_mem(T *ptr, size_t n)
static URI fromAny(const std::string &uri)