aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/utils/os_utils.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/utils/os_utils.cpp')
-rw-r--r--src/lib/utils/os_utils.cpp386
1 files changed, 193 insertions, 193 deletions
diff --git a/src/lib/utils/os_utils.cpp b/src/lib/utils/os_utils.cpp
index 261fb5f1f..275cfac3b 100644
--- a/src/lib/utils/os_utils.cpp
+++ b/src/lib/utils/os_utils.cpp
@@ -47,232 +47,241 @@
namespace Botan {
-std::unique_ptr<OS::Socket>
-OS::open_socket(const std::string& hostname,
- const std::string& service)
- {
+namespace {
+
#if defined(BOTAN_HAS_BOOST_ASIO)
- class Asio_Socket : public OS::Socket
- {
- public:
- Asio_Socket(const std::string& hostname, const std::string& service) :
- m_tcp(m_io)
- {
- boost::asio::ip::tcp::resolver resolver(m_io);
- boost::asio::ip::tcp::resolver::query query(hostname, service);
- boost::asio::connect(m_tcp, resolver.resolve(query));
- }
- void write(const uint8_t buf[], size_t len)
- {
- boost::asio::write(m_tcp, boost::asio::buffer(buf, len));
- }
+class Asio_Socket : public OS::Socket
+ {
+ public:
+ Asio_Socket(const std::string& hostname, const std::string& service) :
+ m_tcp(m_io)
+ {
+ boost::asio::ip::tcp::resolver resolver(m_io);
+ boost::asio::ip::tcp::resolver::query query(hostname, service);
+ boost::asio::connect(m_tcp, resolver.resolve(query));
+ }
- size_t read(uint8_t buf[], size_t len)
- {
- boost::system::error_code error;
- size_t got = m_tcp.read_some(boost::asio::buffer(buf, len), error);
+ void write(const uint8_t buf[], size_t len) override
+ {
+ boost::asio::write(m_tcp, boost::asio::buffer(buf, len));
+ }
- if(error)
- {
- if(error == boost::asio::error::eof)
- return 0;
- throw boost::system::system_error(error); // Some other error.
- }
+ size_t read(uint8_t buf[], size_t len) override
+ {
+ boost::system::error_code error;
+ size_t got = m_tcp.read_some(boost::asio::buffer(buf, len), error);
- return got;
+ if(error)
+ {
+ if(error == boost::asio::error::eof)
+ return 0;
+ throw boost::system::system_error(error); // Some other error.
}
- private:
- boost::asio::io_service m_io;
- boost::asio::ip::tcp::socket m_tcp;
- };
+ return got;
+ }
- return std::unique_ptr<OS::Socket>(new Asio_Socket(hostname, service));
+ private:
+ boost::asio::io_service m_io;
+ boost::asio::ip::tcp::socket m_tcp;
+ };
#elif defined(BOTAN_TARGET_OS_TYPE_IS_WINDOWS)
- class Winsock_Socket : public OS::Socket
- {
- public:
- Winsock_Socket(const std::string& hostname, const std::string& service)
- {
- WSAData wsa_data;
- WORD wsa_version = MAKEWORD(2, 2);
-
- if (::WSAStartup(wsa_version, &wsa_data) != 0)
- {
- throw Exception("WSAStartup() failed: " + std::to_string(WSAGetLastError()));
- }
-
- if (LOBYTE(wsa_data.wVersion) != 2 || HIBYTE(wsa_data.wVersion) != 2)
- {
- ::WSACleanup();
- throw Exception("Could not find a usable version of Winsock.dll");
- }
+class Winsock_Socket : public OS::Socket
+ {
+ public:
+ Winsock_Socket(const std::string& hostname, const std::string& service)
+ {
+ WSAData wsa_data;
+ WORD wsa_version = MAKEWORD(2, 2);
- addrinfo hints;
- ::memset(&hints, 0, sizeof(addrinfo));
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- addrinfo* res;
+ if (::WSAStartup(wsa_version, &wsa_data) != 0)
+ {
+ throw Exception("WSAStartup() failed: " + std::to_string(WSAGetLastError()));
+ }
- if(::getaddrinfo(hostname.c_str(), service.c_str(), &hints, &res) != 0)
- {
- throw Exception("Name resolution failed for " + hostname);
- }
+ if (LOBYTE(wsa_data.wVersion) != 2 || HIBYTE(wsa_data.wVersion) != 2)
+ {
+ ::WSACleanup();
+ throw Exception("Could not find a usable version of Winsock.dll");
+ }
- for(addrinfo* rp = res; (m_socket == INVALID_SOCKET) && (rp != nullptr); rp = rp->ai_next)
- {
- m_socket = ::socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
+ addrinfo hints;
+ ::memset(&hints, 0, sizeof(addrinfo));
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_socktype = SOCK_STREAM;
+ addrinfo* res;
- // unsupported socket type?
- if(m_socket == INVALID_SOCKET)
- continue;
-
- if(::connect(m_socket, rp->ai_addr, rp->ai_addrlen) != 0)
- {
- ::closesocket(m_socket);
- m_socket = INVALID_SOCKET;
- continue;
- }
- }
+ if(::getaddrinfo(hostname.c_str(), service.c_str(), &hints, &res) != 0)
+ {
+ throw Exception("Name resolution failed for " + hostname);
+ }
- ::freeaddrinfo(res);
+ for(addrinfo* rp = res; (m_socket == INVALID_SOCKET) && (rp != nullptr); rp = rp->ai_next)
+ {
+ m_socket = ::socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
+ // unsupported socket type?
if(m_socket == INVALID_SOCKET)
+ continue;
+
+ if(::connect(m_socket, rp->ai_addr, rp->ai_addrlen) != 0)
{
- throw Exception("Connecting to " + hostname +
- " for service " + service + " failed");
+ ::closesocket(m_socket);
+ m_socket = INVALID_SOCKET;
+ continue;
}
}
- ~Winsock_Socket()
- {
- ::closesocket(m_socket);
- m_socket = INVALID_SOCKET;
- ::WSACleanup();
- }
+ ::freeaddrinfo(res);
- void write(const uint8_t buf[], size_t len)
+ if(m_socket == INVALID_SOCKET)
{
- size_t sent_so_far = 0;
- while(sent_so_far != len)
- {
- const size_t left = len - sent_so_far;
- int sent = ::send(m_socket,
- reinterpret_cast<const char*>(buf + sent_so_far),
- static_cast<int>(left),
- 0);
-
- if(sent == SOCKET_ERROR)
- throw Exception("Socket write failed with error " +
- std::to_string(::WSAGetLastError()));
- else
- sent_so_far += static_cast<size_t>(sent);
- }
+ throw Exception("Connecting to " + hostname +
+ " for service " + service + " failed");
}
+ }
- size_t read(uint8_t buf[], size_t len)
- {
- int got = ::recv(m_socket,
- reinterpret_cast<char*>(buf),
- static_cast<int>(len), 0);
+ ~Winsock_Socket()
+ {
+ ::closesocket(m_socket);
+ m_socket = INVALID_SOCKET;
+ ::WSACleanup();
+ }
- if(got == SOCKET_ERROR)
- throw Exception("Socket read failed with error " +
+ void write(const uint8_t buf[], size_t len) override
+ {
+ size_t sent_so_far = 0;
+ while(sent_so_far != len)
+ {
+ const size_t left = len - sent_so_far;
+ int sent = ::send(m_socket,
+ reinterpret_cast<const char*>(buf + sent_so_far),
+ static_cast<int>(left),
+ 0);
+
+ if(sent == SOCKET_ERROR)
+ throw Exception("Socket write failed with error " +
std::to_string(::WSAGetLastError()));
- return static_cast<size_t>(got);
+ else
+ sent_so_far += static_cast<size_t>(sent);
}
+ }
- private:
- SOCKET m_socket = INVALID_SOCKET;
- };
+ size_t read(uint8_t buf[], size_t len) override
+ {
+ int got = ::recv(m_socket,
+ reinterpret_cast<char*>(buf),
+ static_cast<int>(len), 0);
+
+ if(got == SOCKET_ERROR)
+ throw Exception("Socket read failed with error " +
+ std::to_string(::WSAGetLastError()));
+ return static_cast<size_t>(got);
+ }
- return std::unique_ptr<OS::Socket>(new Winsock_Socket(hostname, service));
+ private:
+ SOCKET m_socket = INVALID_SOCKET;
+ };
#elif defined(BOTAN_TARGET_OS_TYPE_IS_UNIX)
+class BSD_Socket : public OS::Socket
+ {
+ public:
+ BSD_Socket(const std::string& hostname, const std::string& service)
+ {
+ addrinfo hints;
+ ::memset(&hints, 0, sizeof(addrinfo));
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_socktype = SOCK_STREAM;
+ addrinfo* res;
- class BSD_Socket : public OS::Socket
- {
- public:
- BSD_Socket(const std::string& hostname, const std::string& service)
+ if(::getaddrinfo(hostname.c_str(), service.c_str(), &hints, &res) != 0)
{
- addrinfo hints;
- ::memset(&hints, 0, sizeof(addrinfo));
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- addrinfo* res;
+ throw Exception("Name resolution failed for " + hostname);
+ }
- if(::getaddrinfo(hostname.c_str(), service.c_str(), &hints, &res) != 0)
- {
- throw Exception("Name resolution failed for " + hostname);
- }
+ m_fd = -1;
- m_fd = -1;
+ for(addrinfo* rp = res; (m_fd < 0) && (rp != nullptr); rp = rp->ai_next)
+ {
+ m_fd = ::socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
- for(addrinfo* rp = res; (m_fd < 0) && (rp != nullptr); rp = rp->ai_next)
+ if(m_fd < 0)
{
- m_fd = ::socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
-
- if(m_fd < 0)
- {
- // unsupported socket type?
- continue;
- }
-
- if(::connect(m_fd, rp->ai_addr, rp->ai_addrlen) != 0)
- {
- ::close(m_fd);
- m_fd = -1;
- continue;
- }
+ // unsupported socket type?
+ continue;
}
- ::freeaddrinfo(res);
-
- if(m_fd < 0)
+ if(::connect(m_fd, rp->ai_addr, rp->ai_addrlen) != 0)
{
- throw Exception("Connecting to " + hostname +
- " for service " + service + " failed");
+ ::close(m_fd);
+ m_fd = -1;
+ continue;
}
}
- ~BSD_Socket()
- {
- ::close(m_fd);
- m_fd = -1;
- }
+ ::freeaddrinfo(res);
- void write(const uint8_t buf[], size_t len)
+ if(m_fd < 0)
{
- size_t sent_so_far = 0;
- while(sent_so_far != len)
- {
- const size_t left = len - sent_so_far;
- ssize_t sent = ::write(m_fd, &buf[sent_so_far], left);
- if(sent < 0)
- throw Exception("Socket write failed with error '" +
- std::string(::strerror(errno)) + "'");
- else
- sent_so_far += static_cast<size_t>(sent);
- }
+ throw Exception("Connecting to " + hostname +
+ " for service " + service + " failed");
}
+ }
- size_t read(uint8_t buf[], size_t len)
- {
- ssize_t got = ::read(m_fd, buf, len);
+ ~BSD_Socket()
+ {
+ ::close(m_fd);
+ m_fd = -1;
+ }
- if(got < 0)
- throw Exception("Socket read failed with error '" +
+ void write(const uint8_t buf[], size_t len) override
+ {
+ size_t sent_so_far = 0;
+ while(sent_so_far != len)
+ {
+ const size_t left = len - sent_so_far;
+ ssize_t sent = ::write(m_fd, &buf[sent_so_far], left);
+ if(sent < 0)
+ throw Exception("Socket write failed with error '" +
std::string(::strerror(errno)) + "'");
- return static_cast<size_t>(got);
+ else
+ sent_so_far += static_cast<size_t>(sent);
}
+ }
+
+ size_t read(uint8_t buf[], size_t len) override
+ {
+ ssize_t got = ::read(m_fd, buf, len);
+
+ if(got < 0)
+ throw Exception("Socket read failed with error '" +
+ std::string(::strerror(errno)) + "'");
+ return static_cast<size_t>(got);
+ }
+
+ private:
+ int m_fd;
+ };
+
+#endif
+
+}
- private:
- int m_fd;
- };
+std::unique_ptr<OS::Socket>
+OS::open_socket(const std::string& hostname,
+ const std::string& service)
+ {
+#if defined(BOTAN_HAS_BOOST_ASIO)
+ return std::unique_ptr<OS::Socket>(new Asio_Socket(hostname, service));
+
+#elif defined(BOTAN_TARGET_OS_TYPE_IS_WINDOWS)
+ return std::unique_ptr<OS::Socket>(new Winsock_Socket(hostname, service));
+#elif defined(BOTAN_TARGET_OS_TYPE_IS_UNIX)
return std::unique_ptr<OS::Socket>(new BSD_Socket(hostname, service));
#else
@@ -323,21 +332,23 @@ uint32_t OS::get_process_id()
uint64_t OS::get_processor_timestamp()
{
+ uint64_t rtc = 0;
+
#if defined(BOTAN_TARGET_OS_HAS_QUERY_PERF_COUNTER)
LARGE_INTEGER tv;
::QueryPerformanceCounter(&tv);
- return tv.QuadPart;
+ rtc = tv.QuadPart;
#elif defined(BOTAN_USE_GCC_INLINE_ASM)
#if defined(BOTAN_TARGET_CPU_IS_X86_FAMILY)
- if(CPUID::has_rdtsc() == false)
- return 0;
-
- uint32_t rtc_low = 0, rtc_high = 0;
- asm volatile("rdtsc" : "=d" (rtc_high), "=a" (rtc_low));
- return (static_cast<uint64_t>(rtc_high) << 32) | rtc_low;
+ if(CPUID::has_rdtsc())
+ {
+ uint32_t rtc_low = 0, rtc_high = 0;
+ asm volatile("rdtsc" : "=d" (rtc_high), "=a" (rtc_low));
+ rtc = (static_cast<uint64_t>(rtc_high) << 32) | rtc_low;
+ }
#elif defined(BOTAN_TARGET_ARCH_IS_PPC64)
uint32_t rtc_low = 0, rtc_high = 0;
@@ -349,43 +360,32 @@ uint64_t OS::get_processor_timestamp()
*/
if(rtc_high > 0 || rtc_low > 0)
{
- return (static_cast<uint64_t>(rtc_high) << 32) | rtc_low;
+ rtc = (static_cast<uint64_t>(rtc_high) << 32) | rtc_low;
}
#elif defined(BOTAN_TARGET_ARCH_IS_ALPHA)
- uint64_t rtc = 0;
asm volatile("rpcc %0" : "=r" (rtc));
- return rtc;
// OpenBSD does not trap access to the %tick register
#elif defined(BOTAN_TARGET_ARCH_IS_SPARC64) && !defined(BOTAN_TARGET_OS_IS_OPENBSD)
- uint64_t rtc = 0;
asm volatile("rd %%tick, %0" : "=r" (rtc));
- return rtc;
#elif defined(BOTAN_TARGET_ARCH_IS_IA64)
- uint64_t rtc = 0;
asm volatile("mov %0=ar.itc" : "=r" (rtc));
- return rtc;
#elif defined(BOTAN_TARGET_ARCH_IS_S390X)
- uint64_t rtc = 0;
asm volatile("stck 0(%0)" : : "a" (&rtc) : "memory", "cc");
- return rtc;
#elif defined(BOTAN_TARGET_ARCH_IS_HPPA)
- uint64_t rtc = 0;
asm volatile("mfctl 16,%0" : "=r" (rtc)); // 64-bit only?
- return rtc;
#else
//#warning "OS::get_processor_timestamp not implemented"
- return 0;
#endif
-#else
- return 0;
#endif
+
+ return rtc;
}
uint64_t OS::get_high_resolution_clock()