Botan 3.13.0
Crypto and TLS for C&
socket_udp.cpp
Go to the documentation of this file.
1/*
2* (C) 2015,2016,2017 Jack Lloyd
3* (C) 2016 Daniel Neus
4* (C) 2019 Nuno Goncalves <nunojpg@gmail.com>
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#include <botan/internal/socket_udp.h>
10
11#include <botan/exceptn.h>
12#include <botan/mem_ops.h>
13#include <botan/uri.h>
14#include <botan/internal/fmt.h>
15#include <botan/internal/stl_util.h>
16#include <botan/internal/target_info.h>
17#include <chrono>
18
19#if defined(BOTAN_HAS_BOOST_ASIO)
20 /*
21 * We don't need serial port support anyway, and asking for it
22 * causes macro conflicts with Darwin's termios.h when this
23 * file is included in the amalgamation. GH #350
24 */
25 #define BOOST_ASIO_DISABLE_SERIAL_PORT
26 #include <boost/asio.hpp>
27 #include <boost/asio/system_timer.hpp>
28#elif defined(BOTAN_TARGET_OS_HAS_SOCKETS)
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <netdb.h>
32 #include <netinet/in.h>
33 #include <string.h>
34 #include <sys/socket.h>
35 #include <sys/time.h>
36 #include <unistd.h>
37
38#elif defined(BOTAN_TARGET_OS_HAS_WINSOCK2)
39 #include <ws2tcpip.h>
40#endif
41
42namespace Botan {
43
44namespace {
45
46#if defined(BOTAN_HAS_BOOST_ASIO)
47class Asio_SocketUDP final : public OS::SocketUDP {
48 public:
49 Asio_SocketUDP(std::string_view hostname, std::string_view service, std::chrono::microseconds timeout) :
50 m_timeout(timeout), m_timer(m_io), m_udp(m_io) {
51 m_timer.expires_after(m_timeout);
52 check_timeout();
53
54 // Resolve asynchronously so the timer covers DNS as well as connect.
55 // check_timeout() only closes m_udp, which isn't open yet during the
56 // resolve, so cancel the resolver inline if the deadline passes.
57 boost::asio::ip::udp::resolver resolver(m_io);
58 boost::asio::ip::udp::resolver::results_type dns_iter;
59 boost::system::error_code resolve_ec = boost::asio::error::would_block;
60 resolver.async_resolve(
61 std::string{hostname}, std::string{service}, [&](const boost::system::error_code& e, auto results) {
62 resolve_ec = e;
63 dns_iter = std::move(results);
64 });
65 while(resolve_ec == boost::asio::error::would_block) {
66 if(m_timer.expiry() < decltype(m_timer)::clock_type::now()) {
67 resolver.cancel();
68 }
69 m_io.run_one();
70 }
71 if(resolve_ec) {
72 throw boost::system::system_error(resolve_ec);
73 }
74
75 boost::system::error_code ec = boost::asio::error::would_block;
76
77 auto connect_cb = [&ec](const boost::system::error_code& e,
78 const boost::asio::ip::udp::resolver::results_type::iterator&) { ec = e; };
79
80 boost::asio::async_connect(m_udp, dns_iter.begin(), dns_iter.end(), connect_cb);
81
82 while(ec == boost::asio::error::would_block) {
83 m_io.run_one();
84 }
85
86 if(ec) {
87 throw boost::system::system_error(ec);
88 }
89 if(!m_udp.is_open()) {
90 throw System_Error(fmt("Connection to host {} failed", hostname));
91 }
92 }
93
94 void write(const uint8_t buf[], size_t len) override {
95 m_timer.expires_after(m_timeout);
96
97 boost::system::error_code ec = boost::asio::error::would_block;
98
99 m_udp.async_send(boost::asio::buffer(buf, len), [&ec](boost::system::error_code e, size_t) { ec = e; });
100
101 while(ec == boost::asio::error::would_block) {
102 m_io.run_one();
103 }
104
105 if(ec) {
106 throw boost::system::system_error(ec);
107 }
108 }
109
110 size_t read(uint8_t buf[], size_t len) override {
111 m_timer.expires_after(m_timeout);
112
113 boost::system::error_code ec = boost::asio::error::would_block;
114 size_t got = 0;
115
116 m_udp.async_receive(boost::asio::buffer(buf, len), [&](boost::system::error_code cb_ec, size_t cb_got) {
117 ec = cb_ec;
118 got = cb_got;
119 });
120
121 while(ec == boost::asio::error::would_block) {
122 m_io.run_one();
123 }
124
125 if(ec) {
126 if(ec == boost::asio::error::eof) {
127 return 0;
128 }
129 throw boost::system::system_error(ec); // Some other error.
130 }
131
132 return got;
133 }
134
135 private:
136 void check_timeout() {
137 if(m_udp.is_open() && m_timer.expiry() < decltype(m_timer)::clock_type::now()) {
138 boost::system::error_code err;
139
140 // NOLINTNEXTLINE(bugprone-unused-return-value,cert-err33-c)
141 m_udp.close(err);
142 }
143
144 // NOLINTNEXTLINE(*-avoid-bind) FIXME - unclear why we can't use a lambda here
145 m_timer.async_wait(std::bind(&Asio_SocketUDP::check_timeout, this));
146 }
147
148 const std::chrono::microseconds m_timeout;
149 boost::asio::io_context m_io;
150 boost::asio::system_timer m_timer;
151 boost::asio::ip::udp::socket m_udp;
152};
153#elif defined(BOTAN_TARGET_OS_HAS_SOCKETS) || defined(BOTAN_TARGET_OS_HAS_WINSOCK2)
154class BSD_SocketUDP final : public OS::SocketUDP {
155 public:
156 BSD_SocketUDP(std::string_view hostname, std::string_view service, std::chrono::microseconds timeout) :
157 m_timeout(timeout), m_socket(invalid_socket()) {
158 socket_init();
159
160 // A constructor that throws does not run its destructor, so do
161 // cleanup explicitly on any failure between socket_init() above and
162 // the end of construction below.
163 try {
164 do_connect(hostname, service);
165 } catch(...) {
166 if(m_socket != invalid_socket()) {
167 close_socket(m_socket);
168 m_socket = invalid_socket();
169 }
170 socket_fini();
171 throw;
172 }
173 }
174
175 private:
176 void do_connect(std::string_view hostname, std::string_view service) {
177 const std::string hostname_str(hostname);
178 const std::string service_str(service);
179
180 addrinfo hints{};
181 hints.ai_family = AF_UNSPEC;
182 hints.ai_socktype = SOCK_DGRAM;
183
184 unique_addr_info_ptr res = nullptr;
185
186 const int rc = ::getaddrinfo(hostname_str.c_str(), service_str.c_str(), &hints, Botan::out_ptr(res));
187 if(rc != 0) {
188 throw System_Error(fmt("Name resolution failed for {}", hostname), rc);
189 }
190
191 for(const addrinfo* rp = res.get(); (m_socket == invalid_socket()) && (rp != nullptr); rp = rp->ai_next) {
192 if(rp->ai_family != AF_INET && rp->ai_family != AF_INET6) {
193 continue;
194 }
195
196 m_socket = ::socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
197
198 if(m_socket == invalid_socket()) [[unlikely]] {
199 // unsupported socket type?
200 continue;
201 }
202
203 #if !defined(BOTAN_TARGET_OS_HAS_WINSOCK2)
204 // Windows fd_set is an array of sockets and doesn't have this limitation
205 if(m_socket >= FD_SETSIZE) {
206 close_socket(m_socket);
207 m_socket = invalid_socket();
208 throw System_Error("Socket descriptor exceeds FD_SETSIZE; select() would be unsafe");
209 }
210 #endif
211
212 set_nonblocking(m_socket);
213
214 // Connect the UDP socket to the selected peer so the kernel drops
215 // datagrams arriving from anyone else. Without this a stray or
216 // spoofed datagram could be accepted as the response.
217 //
218 // ::connect on a UDP socket only records the peer address in the
219 // kernel; it does not produce or consume traffic. It is effectively
220 // non-blocking on every supported platform, so we don't wrap this
221 // in the select() pattern used by the TCP path.
222 if(::connect(m_socket, rp->ai_addr, static_cast<socklen_t>(rp->ai_addrlen)) != 0) {
223 close_socket(m_socket);
224 m_socket = invalid_socket();
225 continue;
226 }
227 }
228
229 if(m_socket == invalid_socket()) {
230 throw System_Error(
231 fmt("Connecting to {} for service {} failed with errno {}", hostname, service, last_socket_error()),
232 last_socket_error());
233 }
234 }
235
236 public:
237 ~BSD_SocketUDP() override {
238 close_socket(m_socket);
239 m_socket = invalid_socket();
240 socket_fini();
241 }
242
243 BSD_SocketUDP(const BSD_SocketUDP& other) = delete;
244 BSD_SocketUDP(BSD_SocketUDP&& other) = delete;
245 BSD_SocketUDP& operator=(const BSD_SocketUDP& other) = delete;
246 BSD_SocketUDP& operator=(BSD_SocketUDP&& other) = delete;
247
248 void write(const uint8_t buf[], size_t len) override {
249 size_t sent_so_far = 0;
250 while(sent_so_far != len) {
251 fd_set write_set;
252 FD_ZERO(&write_set);
253 FD_SET(m_socket, &write_set);
254
255 struct timeval timeout = make_timeout_tv();
256 const int active = ::select(static_cast<int>(m_socket + 1), nullptr, &write_set, nullptr, &timeout);
257
258 if(active < 0) {
259 if(last_error_is_retryable()) {
260 continue;
261 }
262 throw System_Error("Socket select failed", last_socket_error());
263 }
264
265 if(active == 0) {
266 throw System_Error("Timeout during socket write");
267 }
268
269 const size_t left = len - sent_so_far;
270 const socket_op_ret_type sent =
271 ::send(m_socket, cast_uint8_ptr_to_char(buf + sent_so_far), static_cast<sendrecv_len_type>(left), 0);
272 if(sent < 0) {
273 if(last_error_is_retryable()) {
274 continue;
275 }
276 throw System_Error("Socket write failed", last_socket_error());
277 } else {
278 sent_so_far += static_cast<size_t>(sent);
279 }
280 }
281 }
282
283 size_t read(uint8_t buf[], size_t len) override {
284 for(;;) {
285 fd_set read_set;
286 FD_ZERO(&read_set);
287 FD_SET(m_socket, &read_set);
288
289 struct timeval timeout = make_timeout_tv();
290 const int active = ::select(static_cast<int>(m_socket + 1), &read_set, nullptr, nullptr, &timeout);
291
292 if(active < 0) {
293 if(last_error_is_retryable()) {
294 continue;
295 }
296 throw System_Error("Socket select failed", last_socket_error());
297 }
298
299 if(active == 0) {
300 throw System_Error("Timeout during socket read");
301 }
302
303 // Socket is connected, so recv() filters out datagrams from any
304 // sender other than the configured peer.
305 const socket_op_ret_type got =
306 ::recv(m_socket, cast_uint8_ptr_to_char(buf), static_cast<sendrecv_len_type>(len), 0);
307
308 if(got < 0) {
309 if(last_error_is_retryable()) {
310 continue;
311 }
312 throw System_Error("Socket read failed", last_socket_error());
313 }
314
315 return static_cast<size_t>(got);
316 }
317 }
318
319 private:
320 #if defined(BOTAN_TARGET_OS_HAS_WINSOCK2)
321 typedef SOCKET socket_type;
322 typedef int socket_op_ret_type;
323 typedef int sendrecv_len_type;
324
325 static socket_type invalid_socket() { return INVALID_SOCKET; }
326
327 static void close_socket(socket_type s) { ::closesocket(s); }
328
329 static int last_socket_error() { return ::WSAGetLastError(); }
330
331 static std::string get_last_socket_error() { return std::to_string(::WSAGetLastError()); }
332
333 static bool nonblocking_connect_in_progress() { return (::WSAGetLastError() == WSAEWOULDBLOCK); }
334
335 static bool last_error_is_retryable() { return (::WSAGetLastError() == WSAEINTR); }
336
337 static void set_nonblocking(socket_type s) {
338 u_long nonblocking = 1;
339 ::ioctlsocket(s, FIONBIO, &nonblocking);
340 }
341
342 static void socket_init() {
343 WSAData wsa_data;
344 WORD wsa_version = MAKEWORD(2, 2);
345
346 if(::WSAStartup(wsa_version, &wsa_data) != 0) {
347 throw System_Error("WSAStartup() failed", WSAGetLastError());
348 }
349
350 if(LOBYTE(wsa_data.wVersion) != 2 || HIBYTE(wsa_data.wVersion) != 2) {
351 ::WSACleanup();
352 throw System_Error("Could not find a usable version of Winsock.dll");
353 }
354 }
355
356 static void socket_fini() { ::WSACleanup(); }
357 #else
358 typedef int socket_type;
359 typedef ssize_t socket_op_ret_type;
360 typedef size_t sendrecv_len_type;
361
362 static socket_type invalid_socket() { return -1; }
363
364 static void close_socket(socket_type s) { ::close(s); }
365
366 static int last_socket_error() { return errno; }
367
368 static std::string get_last_socket_error() { return ::strerror(errno); }
369
370 static bool nonblocking_connect_in_progress() { return (errno == EINPROGRESS); }
371
372 static bool last_error_is_retryable() { return (errno == EINTR); }
373
374 static void set_nonblocking(socket_type s) {
375 // NOLINTNEXTLINE(*-vararg)
376 if(::fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
377 throw System_Error("Setting socket to non-blocking state failed", errno);
378 }
379 }
380
381 static void socket_init() {}
382
383 static void socket_fini() {}
384 #endif
385 struct timeval make_timeout_tv() const {
386 struct timeval tv {};
387
388 tv.tv_sec = static_cast<decltype(timeval::tv_sec)>(m_timeout.count() / 1000000);
389 tv.tv_usec = static_cast<decltype(timeval::tv_usec)>(m_timeout.count() % 1000000);
390 return tv;
391 }
392
393 const std::chrono::microseconds m_timeout;
394 socket_type m_socket;
395
396 using unique_addr_info_ptr = std::unique_ptr<addrinfo, decltype([](addrinfo* p) {
397 if(p != nullptr) {
398 ::freeaddrinfo(p);
399 }
400 })>;
401};
402#endif
403} // namespace
404
405std::unique_ptr<OS::SocketUDP> OS::open_socket_udp(std::string_view hostname,
406 std::string_view service,
407 std::chrono::microseconds timeout) {
408#if defined(BOTAN_HAS_BOOST_ASIO)
409 return std::make_unique<Asio_SocketUDP>(hostname, service, timeout);
410#elif defined(BOTAN_TARGET_OS_HAS_SOCKETS) || defined(BOTAN_TARGET_OS_HAS_WINSOCK2)
411 return std::make_unique<BSD_SocketUDP>(hostname, service, timeout);
412#else
413 BOTAN_UNUSED(hostname);
414 BOTAN_UNUSED(service);
415 BOTAN_UNUSED(timeout);
416 return std::unique_ptr<OS::SocketUDP>();
417#endif
418}
419
420std::unique_ptr<OS::SocketUDP> OS::open_socket_udp(std::string_view uri_string, std::chrono::microseconds timeout) {
421 const auto authority = URI::Authority::from_string(uri_string);
422 if(!authority.has_value() || !authority->port().has_value()) {
423 throw Invalid_Argument("UDP port not specified");
424 }
425 return open_socket_udp(authority->host_to_string(), std::to_string(*authority->port()), timeout);
426}
427
428} // namespace Botan
#define BOTAN_UNUSED
Definition assert.h:144
static std::optional< Authority > from_string(std::string_view raw)
Definition uri.cpp:248
std::unique_ptr< SocketUDP > BOTAN_TEST_API open_socket_udp(std::string_view hostname, std::string_view service, std::chrono::microseconds timeout)
constexpr auto out_ptr(T &outptr) noexcept
Definition stl_util.h:148
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53