/src/botan/src/lib/utils/socket/socket.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * (C) 2015,2016,2017 Jack Lloyd |
3 | | * (C) 2016 Daniel Neus |
4 | | * |
5 | | * Botan is released under the Simplified BSD License (see license.txt) |
6 | | */ |
7 | | |
8 | | #include <botan/internal/socket.h> |
9 | | |
10 | | #include <botan/exceptn.h> |
11 | | #include <botan/mem_ops.h> |
12 | | #include <botan/internal/fmt.h> |
13 | | #include <botan/internal/target_info.h> |
14 | | #include <chrono> |
15 | | |
16 | | #if defined(BOTAN_HAS_BOOST_ASIO) |
17 | | /* |
18 | | * We don't need serial port support anyway, and asking for it causes |
19 | | * macro conflicts with termios.h when this file is included in the |
20 | | * amalgamation. |
21 | | */ |
22 | | #define BOOST_ASIO_DISABLE_SERIAL_PORT |
23 | | #include <boost/asio.hpp> |
24 | | #include <boost/asio/system_timer.hpp> |
25 | | |
26 | | #elif defined(BOTAN_TARGET_OS_HAS_SOCKETS) |
27 | | #include <errno.h> |
28 | | #include <fcntl.h> |
29 | | #include <netdb.h> |
30 | | #include <netinet/in.h> |
31 | | #include <string.h> |
32 | | #include <sys/socket.h> |
33 | | #include <sys/time.h> |
34 | | #include <unistd.h> |
35 | | |
36 | | #elif defined(BOTAN_TARGET_OS_HAS_WINSOCK2) |
37 | | #include <ws2tcpip.h> |
38 | | #endif |
39 | | |
40 | | namespace Botan { |
41 | | |
42 | | namespace { |
43 | | |
44 | | #if defined(BOTAN_HAS_BOOST_ASIO) |
45 | | |
46 | | class Asio_Socket final : public OS::Socket { |
47 | | public: |
48 | | Asio_Socket(std::string_view hostname, std::string_view service, std::chrono::milliseconds timeout) : |
49 | | m_timeout(timeout), m_timer(m_io), m_tcp(m_io) { |
50 | | m_timer.expires_after(m_timeout); |
51 | | check_timeout(); |
52 | | |
53 | | boost::asio::ip::tcp::resolver resolver(m_io); |
54 | | boost::asio::ip::tcp::resolver::results_type dns_iter = |
55 | | resolver.resolve(std::string{hostname}, std::string{service}); |
56 | | |
57 | | boost::system::error_code ec = boost::asio::error::would_block; |
58 | | |
59 | | auto connect_cb = [&ec](const boost::system::error_code& e, const auto&) { ec = e; }; |
60 | | |
61 | | boost::asio::async_connect(m_tcp, dns_iter.begin(), dns_iter.end(), connect_cb); |
62 | | |
63 | | while(ec == boost::asio::error::would_block) { |
64 | | m_io.run_one(); |
65 | | } |
66 | | |
67 | | if(ec) { |
68 | | throw boost::system::system_error(ec); |
69 | | } |
70 | | if(m_tcp.is_open() == false) { |
71 | | throw System_Error(fmt("Connection to host {} failed", hostname)); |
72 | | } |
73 | | } |
74 | | |
75 | | void write(const uint8_t buf[], size_t len) override { |
76 | | m_timer.expires_after(m_timeout); |
77 | | |
78 | | boost::system::error_code ec = boost::asio::error::would_block; |
79 | | |
80 | | m_tcp.async_send(boost::asio::buffer(buf, len), [&ec](boost::system::error_code e, size_t) { ec = e; }); |
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 | | } |
90 | | |
91 | | size_t read(uint8_t buf[], size_t len) override { |
92 | | m_timer.expires_after(m_timeout); |
93 | | |
94 | | boost::system::error_code ec = boost::asio::error::would_block; |
95 | | size_t got = 0; |
96 | | |
97 | | m_tcp.async_read_some(boost::asio::buffer(buf, len), [&](boost::system::error_code cb_ec, size_t cb_got) { |
98 | | ec = cb_ec; |
99 | | got = cb_got; |
100 | | }); |
101 | | |
102 | | while(ec == boost::asio::error::would_block) { |
103 | | m_io.run_one(); |
104 | | } |
105 | | |
106 | | if(ec) { |
107 | | if(ec == boost::asio::error::eof) { |
108 | | return 0; |
109 | | } |
110 | | throw boost::system::system_error(ec); // Some other error. |
111 | | } |
112 | | |
113 | | return got; |
114 | | } |
115 | | |
116 | | private: |
117 | | void check_timeout() { |
118 | | if(m_tcp.is_open() && m_timer.expiry() < std::chrono::system_clock::now()) { |
119 | | boost::system::error_code err; |
120 | | |
121 | | // NOLINTNEXTLINE(bugprone-unused-return-value,cert-err33-c) |
122 | | m_tcp.close(err); |
123 | | } |
124 | | |
125 | | m_timer.async_wait(std::bind(&Asio_Socket::check_timeout, this)); |
126 | | } |
127 | | |
128 | | const std::chrono::milliseconds m_timeout; |
129 | | boost::asio::io_context m_io; |
130 | | boost::asio::system_timer m_timer; |
131 | | boost::asio::ip::tcp::socket m_tcp; |
132 | | }; |
133 | | |
134 | | #elif defined(BOTAN_TARGET_OS_HAS_SOCKETS) || defined(BOTAN_TARGET_OS_HAS_WINSOCK2) |
135 | | |
136 | | class BSD_Socket final : public OS::Socket { |
137 | | private: |
138 | | #if defined(BOTAN_TARGET_OS_HAS_WINSOCK2) |
139 | | typedef SOCKET socket_type; |
140 | | typedef int socket_op_ret_type; |
141 | | typedef int socklen_type; |
142 | | typedef int sendrecv_len_type; |
143 | | |
144 | | static socket_type invalid_socket() { return INVALID_SOCKET; } |
145 | | |
146 | | static void close_socket(socket_type s) { ::closesocket(s); } |
147 | | |
148 | | static std::string get_last_socket_error() { return std::to_string(::WSAGetLastError()); } |
149 | | |
150 | | static bool nonblocking_connect_in_progress() { return (::WSAGetLastError() == WSAEWOULDBLOCK); } |
151 | | |
152 | | static void set_nonblocking(socket_type s) { |
153 | | u_long nonblocking = 1; |
154 | | ::ioctlsocket(s, FIONBIO, &nonblocking); |
155 | | } |
156 | | |
157 | | static void socket_init() { |
158 | | WSAData wsa_data; |
159 | | WORD wsa_version = MAKEWORD(2, 2); |
160 | | |
161 | | if(::WSAStartup(wsa_version, &wsa_data) != 0) { |
162 | | throw System_Error("WSAStartup() failed", WSAGetLastError()); |
163 | | } |
164 | | |
165 | | if(LOBYTE(wsa_data.wVersion) != 2 || HIBYTE(wsa_data.wVersion) != 2) { |
166 | | ::WSACleanup(); |
167 | | throw System_Error("Could not find a usable version of Winsock.dll"); |
168 | | } |
169 | | } |
170 | | |
171 | | static void socket_fini() { ::WSACleanup(); } |
172 | | #else |
173 | | typedef int socket_type; |
174 | | typedef ssize_t socket_op_ret_type; |
175 | | typedef socklen_t socklen_type; |
176 | | typedef size_t sendrecv_len_type; |
177 | | |
178 | 0 | static socket_type invalid_socket() { return -1; } |
179 | | |
180 | 0 | static void close_socket(socket_type s) { ::close(s); } |
181 | | |
182 | 0 | static std::string get_last_socket_error() { return ::strerror(errno); } |
183 | | |
184 | 0 | static bool nonblocking_connect_in_progress() { return (errno == EINPROGRESS); } |
185 | | |
186 | 0 | static void set_nonblocking(socket_type s) { |
187 | 0 | if(::fcntl(s, F_SETFL, O_NONBLOCK) < 0) { |
188 | 0 | throw System_Error("Setting socket to non-blocking state failed", errno); |
189 | 0 | } |
190 | 0 | } |
191 | | |
192 | 0 | static void socket_init() {} |
193 | | |
194 | 0 | static void socket_fini() {} |
195 | | #endif |
196 | | |
197 | | public: |
198 | | BSD_Socket(std::string_view hostname, std::string_view service, std::chrono::microseconds timeout) : |
199 | 0 | m_timeout(timeout) { |
200 | 0 | socket_init(); |
201 | |
|
202 | 0 | m_socket = invalid_socket(); |
203 | |
|
204 | 0 | addrinfo hints; |
205 | 0 | clear_mem(&hints, 1); |
206 | 0 | hints.ai_family = AF_UNSPEC; |
207 | 0 | hints.ai_socktype = SOCK_STREAM; |
208 | 0 | addrinfo* res; |
209 | |
|
210 | 0 | const std::string hostname_str(hostname); |
211 | 0 | const std::string service_str(service); |
212 | |
|
213 | 0 | int rc = ::getaddrinfo(hostname_str.c_str(), service_str.c_str(), &hints, &res); |
214 | |
|
215 | 0 | if(rc != 0) { |
216 | 0 | throw System_Error(fmt("Name resolution failed for {}", hostname), rc); |
217 | 0 | } |
218 | | |
219 | 0 | for(addrinfo* rp = res; (m_socket == invalid_socket()) && (rp != nullptr); rp = rp->ai_next) { |
220 | 0 | if(rp->ai_family != AF_INET && rp->ai_family != AF_INET6) { |
221 | 0 | continue; |
222 | 0 | } |
223 | | |
224 | 0 | m_socket = ::socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); |
225 | |
|
226 | 0 | if(m_socket == invalid_socket()) { |
227 | | // unsupported socket type? |
228 | 0 | continue; |
229 | 0 | } |
230 | | |
231 | 0 | set_nonblocking(m_socket); |
232 | |
|
233 | 0 | int err = ::connect(m_socket, rp->ai_addr, static_cast<socklen_type>(rp->ai_addrlen)); |
234 | |
|
235 | 0 | if(err == -1) { |
236 | 0 | int active = 0; |
237 | 0 | if(nonblocking_connect_in_progress()) { |
238 | 0 | struct timeval timeout_tv = make_timeout_tv(); |
239 | 0 | fd_set write_set; |
240 | 0 | FD_ZERO(&write_set); |
241 | | // Weirdly, Winsock uses a SOCKET type but wants FD_SET to get an int instead |
242 | 0 | FD_SET(static_cast<int>(m_socket), &write_set); |
243 | |
|
244 | 0 | active = ::select(static_cast<int>(m_socket + 1), nullptr, &write_set, nullptr, &timeout_tv); |
245 | |
|
246 | 0 | if(active) { |
247 | 0 | int socket_error = 0; |
248 | 0 | socklen_t len = sizeof(socket_error); |
249 | |
|
250 | 0 | if(::getsockopt(m_socket, SOL_SOCKET, SO_ERROR, reinterpret_cast<char*>(&socket_error), &len) < |
251 | 0 | 0) { |
252 | 0 | throw System_Error("Error calling getsockopt", errno); |
253 | 0 | } |
254 | | |
255 | 0 | if(socket_error != 0) { |
256 | 0 | active = 0; |
257 | 0 | } |
258 | 0 | } |
259 | 0 | } |
260 | | |
261 | 0 | if(active == 0) { |
262 | 0 | close_socket(m_socket); |
263 | 0 | m_socket = invalid_socket(); |
264 | 0 | continue; |
265 | 0 | } |
266 | 0 | } |
267 | 0 | } |
268 | | |
269 | 0 | ::freeaddrinfo(res); |
270 | |
|
271 | 0 | if(m_socket == invalid_socket()) { |
272 | 0 | throw System_Error(fmt("Connecting to {} for service {} failed with errno {}", hostname, service, errno), |
273 | 0 | errno); |
274 | 0 | } |
275 | 0 | } |
276 | | |
277 | 0 | ~BSD_Socket() override { |
278 | 0 | close_socket(m_socket); |
279 | 0 | m_socket = invalid_socket(); |
280 | 0 | socket_fini(); |
281 | 0 | } |
282 | | |
283 | | BSD_Socket(const BSD_Socket& other) = delete; |
284 | | BSD_Socket(BSD_Socket&& other) = delete; |
285 | | BSD_Socket& operator=(const BSD_Socket& other) = delete; |
286 | | BSD_Socket& operator=(BSD_Socket&& other) = delete; |
287 | | |
288 | 0 | void write(const uint8_t buf[], size_t len) override { |
289 | 0 | fd_set write_set; |
290 | 0 | FD_ZERO(&write_set); |
291 | 0 | FD_SET(m_socket, &write_set); |
292 | |
|
293 | 0 | size_t sent_so_far = 0; |
294 | 0 | while(sent_so_far != len) { |
295 | 0 | struct timeval timeout = make_timeout_tv(); |
296 | 0 | int active = ::select(static_cast<int>(m_socket + 1), nullptr, &write_set, nullptr, &timeout); |
297 | |
|
298 | 0 | if(active == 0) { |
299 | 0 | throw System_Error("Timeout during socket write"); |
300 | 0 | } |
301 | | |
302 | 0 | const size_t left = len - sent_so_far; |
303 | 0 | socket_op_ret_type sent = |
304 | 0 | ::send(m_socket, cast_uint8_ptr_to_char(&buf[sent_so_far]), static_cast<sendrecv_len_type>(left), 0); |
305 | 0 | if(sent < 0) { |
306 | 0 | throw System_Error("Socket write failed", errno); |
307 | 0 | } else { |
308 | 0 | sent_so_far += static_cast<size_t>(sent); |
309 | 0 | } |
310 | 0 | } |
311 | 0 | } |
312 | | |
313 | 0 | size_t read(uint8_t buf[], size_t len) override { |
314 | 0 | fd_set read_set; |
315 | 0 | FD_ZERO(&read_set); |
316 | 0 | FD_SET(m_socket, &read_set); |
317 | |
|
318 | 0 | struct timeval timeout = make_timeout_tv(); |
319 | 0 | int active = ::select(static_cast<int>(m_socket + 1), &read_set, nullptr, nullptr, &timeout); |
320 | |
|
321 | 0 | if(active == 0) { |
322 | 0 | throw System_Error("Timeout during socket read"); |
323 | 0 | } |
324 | | |
325 | 0 | socket_op_ret_type got = ::recv(m_socket, cast_uint8_ptr_to_char(buf), static_cast<sendrecv_len_type>(len), 0); |
326 | |
|
327 | 0 | if(got < 0) { |
328 | 0 | throw System_Error("Socket read failed", errno); |
329 | 0 | } |
330 | | |
331 | 0 | return static_cast<size_t>(got); |
332 | 0 | } |
333 | | |
334 | | private: |
335 | 0 | struct timeval make_timeout_tv() const { |
336 | 0 | struct timeval tv; |
337 | 0 | tv.tv_sec = static_cast<decltype(timeval::tv_sec)>(m_timeout.count() / 1000000); |
338 | 0 | tv.tv_usec = static_cast<decltype(timeval::tv_usec)>(m_timeout.count() % 1000000); |
339 | 0 | return tv; |
340 | 0 | } |
341 | | |
342 | | const std::chrono::microseconds m_timeout; |
343 | | socket_type m_socket; |
344 | | }; |
345 | | |
346 | | #endif |
347 | | |
348 | | } // namespace |
349 | | |
350 | | std::unique_ptr<OS::Socket> OS::open_socket(std::string_view hostname, |
351 | | std::string_view service, |
352 | 0 | std::chrono::milliseconds timeout) { |
353 | | #if defined(BOTAN_HAS_BOOST_ASIO) |
354 | | return std::make_unique<Asio_Socket>(hostname, service, timeout); |
355 | | |
356 | | #elif defined(BOTAN_TARGET_OS_HAS_SOCKETS) || defined(BOTAN_TARGET_OS_HAS_WINSOCK2) |
357 | | return std::make_unique<BSD_Socket>(hostname, service, timeout); |
358 | |
|
359 | | #else |
360 | | BOTAN_UNUSED(hostname, service, timeout); |
361 | | // No sockets for you |
362 | | return std::unique_ptr<Socket>(); |
363 | | #endif |
364 | 0 | } |
365 | | |
366 | | } // namespace Botan |