aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSimon Warta <[email protected]>2017-10-03 11:16:02 +0200
committerSimon Warta <[email protected]>2017-10-03 11:16:02 +0200
commit3b50c4b50e25330860de69cf5ef86444e1ef66f7 (patch)
treec0ceae783083daa82d9e90308eab4d1281fc6a85 /src
parent9efe16ed1d416390584621276dcb8e3782d17c71 (diff)
Move socket implementation into module http_util
This removes the requirement of linking socket libraries for applications that do not use http_util
Diffstat (limited to 'src')
-rw-r--r--src/lib/utils/http_util/http_util.cpp1
-rw-r--r--src/lib/utils/http_util/info.txt16
-rw-r--r--src/lib/utils/http_util/socket.cpp287
-rw-r--r--src/lib/utils/http_util/socket.h62
-rw-r--r--src/lib/utils/info.txt8
-rw-r--r--src/lib/utils/os_utils.cpp267
-rw-r--r--src/lib/utils/os_utils.h33
7 files changed, 366 insertions, 308 deletions
diff --git a/src/lib/utils/http_util/http_util.cpp b/src/lib/utils/http_util/http_util.cpp
index 035176c17..8dcd8d55a 100644
--- a/src/lib/utils/http_util/http_util.cpp
+++ b/src/lib/utils/http_util/http_util.cpp
@@ -10,6 +10,7 @@
#include <botan/parsing.h>
#include <botan/hex.h>
#include <botan/internal/os_utils.h>
+#include <botan/internal/socket.h>
#include <botan/internal/stl_util.h>
#include <sstream>
diff --git a/src/lib/utils/http_util/info.txt b/src/lib/utils/http_util/info.txt
index fe9fc3ea7..a6cbd902f 100644
--- a/src/lib/utils/http_util/info.txt
+++ b/src/lib/utils/http_util/info.txt
@@ -1,3 +1,17 @@
<defines>
-HTTP_UTIL -> 20131128
+HTTP_UTIL -> 20171003
</defines>
+
+<header:public>
+http_util.h
+</header:public>
+
+<header:internal>
+socket.h
+</header:internal>
+
+<libs>
+linux -> rt
+mingw -> ws2_32
+windows -> ws2_32.lib
+</libs>
diff --git a/src/lib/utils/http_util/socket.cpp b/src/lib/utils/http_util/socket.cpp
new file mode 100644
index 000000000..5f8cbcb05
--- /dev/null
+++ b/src/lib/utils/http_util/socket.cpp
@@ -0,0 +1,287 @@
+/*
+* OS and machine specific utility functions
+* (C) 2015,2016,2017 Jack Lloyd
+* (C) 2016 Daniel Neus
+*
+* Botan is released under the Simplified BSD License (see license.txt)
+*/
+
+#include <botan/internal/socket.h>
+#include <botan/exceptn.h>
+#include <chrono>
+
+#if defined(BOTAN_HAS_BOOST_ASIO)
+ /*
+ * We don't need serial port support anyway, and asking for it
+ * causes macro conflicts with Darwin's termios.h when this
+ * file is included in the amalgamation. GH #350
+ */
+ #define BOOST_ASIO_DISABLE_SERIAL_PORT
+ #include <boost/asio.hpp>
+#endif
+
+#if defined(BOTAN_TARGET_OS_TYPE_IS_UNIX)
+
+#if !defined(BOTAN_HAS_BOOST_ASIO)
+ #include <sys/socket.h>
+ #include <netinet/in.h>
+ #include <netdb.h>
+ #include <string.h>
+ #include <unistd.h>
+#endif
+
+#elif defined(BOTAN_TARGET_OS_TYPE_IS_WINDOWS)
+ #define NOMINMAX 1
+#if !defined(BOTAN_HAS_BOOST_ASIO)
+ #include <winsock2.h>
+ #include <ws2tcpip.h>
+#endif
+ #include <windows.h>
+#endif
+
+namespace Botan {
+
+namespace {
+
+#if defined(BOTAN_HAS_BOOST_ASIO)
+
+class Asio_Socket final : 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) override
+ {
+ boost::asio::write(m_tcp, boost::asio::buffer(buf, len));
+ }
+
+ 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);
+
+ if(error)
+ {
+ if(error == boost::asio::error::eof)
+ return 0;
+ throw boost::system::system_error(error); // Some other error.
+ }
+
+ return got;
+ }
+
+ 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 final : 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");
+ }
+
+ addrinfo hints;
+ ::memset(&hints, 0, sizeof(addrinfo));
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_socktype = SOCK_STREAM;
+ addrinfo* res;
+
+ if(::getaddrinfo(hostname.c_str(), service.c_str(), &hints, &res) != 0)
+ {
+ throw Exception("Name resolution failed for " + hostname);
+ }
+
+ 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)
+ {
+ ::closesocket(m_socket);
+ m_socket = INVALID_SOCKET;
+ continue;
+ }
+ }
+
+ ::freeaddrinfo(res);
+
+ if(m_socket == INVALID_SOCKET)
+ {
+ throw Exception("Connecting to " + hostname +
+ " for service " + service + " failed");
+ }
+ }
+
+ ~Winsock_Socket()
+ {
+ ::closesocket(m_socket);
+ m_socket = INVALID_SOCKET;
+ ::WSACleanup();
+ }
+
+ 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,
+ cast_uint8_ptr_to_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);
+ }
+ }
+
+ size_t read(uint8_t buf[], size_t len) override
+ {
+ int got = ::recv(m_socket,
+ cast_uint8_ptr_to_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);
+ }
+
+ private:
+ SOCKET m_socket = INVALID_SOCKET;
+ };
+
+#elif defined(BOTAN_TARGET_OS_TYPE_IS_UNIX)
+class BSD_Socket final : 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;
+
+ if(::getaddrinfo(hostname.c_str(), service.c_str(), &hints, &res) != 0)
+ {
+ throw Exception("Name resolution failed for " + hostname);
+ }
+
+ 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);
+
+ 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;
+ }
+ }
+
+ ::freeaddrinfo(res);
+
+ if(m_fd < 0)
+ {
+ throw Exception("Connecting to " + hostname +
+ " for service " + service + " failed");
+ }
+ }
+
+ ~BSD_Socket()
+ {
+ ::close(m_fd);
+ m_fd = -1;
+ }
+
+ 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)) + "'");
+ 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
+
+}
+
+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
+ // No sockets for you
+ return std::unique_ptr<Socket>();
+#endif
+ }
+
+}
diff --git a/src/lib/utils/http_util/socket.h b/src/lib/utils/http_util/socket.h
new file mode 100644
index 000000000..78c29f147
--- /dev/null
+++ b/src/lib/utils/http_util/socket.h
@@ -0,0 +1,62 @@
+/*
+* OS specific utility functions
+* (C) 2015,2016,2017 Jack Lloyd
+*
+* Botan is released under the Simplified BSD License (see license.txt)
+*/
+
+#ifndef BOTAN_SOCKET_H_
+#define BOTAN_SOCKET_H_
+
+#include <botan/types.h>
+#include <functional>
+
+namespace Botan {
+
+namespace OS {
+
+/*
+* This header is internal (not installed) and these functions are not
+* intended to be called by applications. However they are given public
+* visibility (using BOTAN_TEST_API macro) for the tests. This also probably
+* allows them to be overridden by the application on ELF systems, but
+* this hasn't been tested.
+*/
+
+
+/**
+* A wrapper around a simple blocking TCP socket
+*/
+class BOTAN_TEST_API Socket
+ {
+ public:
+ /**
+ * The socket will be closed upon destruction
+ */
+ virtual ~Socket() {};
+
+ /**
+ * Write to the socket. Blocks until all bytes sent.
+ * Throws on error.
+ */
+ virtual void write(const uint8_t buf[], size_t len) = 0;
+
+ /**
+ * Reads up to len bytes, returns bytes written to buf.
+ * Returns 0 on EOF. Throws on error.
+ */
+ virtual size_t read(uint8_t buf[], size_t len) = 0;
+ };
+
+/**
+* Open up a socket. Will throw on error. Returns null if sockets are
+* not available on this platform.
+*/
+std::unique_ptr<Socket>
+BOTAN_TEST_API open_socket(const std::string& hostname,
+ const std::string& service);
+
+} // OS
+} // Botan
+
+#endif
diff --git a/src/lib/utils/info.txt b/src/lib/utils/info.txt
index 5a0ce469c..60a10b058 100644
--- a/src/lib/utils/info.txt
+++ b/src/lib/utils/info.txt
@@ -1,5 +1,5 @@
<defines>
-UTIL_FUNCTIONS -> 20161127
+UTIL_FUNCTIONS -> 20171003
</defines>
load_on always
@@ -38,12 +38,6 @@ semaphore.h
stl_util.h
</header:internal>
-<libs>
-linux -> rt
-mingw -> ws2_32
-windows -> ws2_32.lib
-</libs>
-
<requires>
cpuid
</requires>
diff --git a/src/lib/utils/os_utils.cpp b/src/lib/utils/os_utils.cpp
index d516e7600..d56c54232 100644
--- a/src/lib/utils/os_utils.cpp
+++ b/src/lib/utils/os_utils.cpp
@@ -12,16 +12,6 @@
#include <botan/mem_ops.h>
#include <chrono>
-#if defined(BOTAN_HAS_BOOST_ASIO)
- /*
- * We don't need serial port support anyway, and asking for it
- * causes macro conflicts with Darwin's termios.h when this
- * file is included in the amalgamation. GH #350
- */
- #define BOOST_ASIO_DISABLE_SERIAL_PORT
- #include <boost/asio.hpp>
-#endif
-
#if defined(BOTAN_TARGET_OS_HAS_EXPLICIT_BZERO)
#include <string.h>
#endif
@@ -34,267 +24,10 @@
#include <setjmp.h>
#include <unistd.h>
#include <errno.h>
-
-#if !defined(BOTAN_HAS_BOOST_ASIO)
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netdb.h>
-#endif
-
-#elif defined(BOTAN_TARGET_OS_TYPE_IS_WINDOWS)
- #define NOMINMAX 1
-#if !defined(BOTAN_HAS_BOOST_ASIO)
- #include <winsock2.h>
- #include <ws2tcpip.h>
-#endif
- #include <windows.h>
#endif
namespace Botan {
-namespace {
-
-#if defined(BOTAN_HAS_BOOST_ASIO)
-
-class Asio_Socket final : 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) override
- {
- boost::asio::write(m_tcp, boost::asio::buffer(buf, len));
- }
-
- 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);
-
- if(error)
- {
- if(error == boost::asio::error::eof)
- return 0;
- throw boost::system::system_error(error); // Some other error.
- }
-
- return got;
- }
-
- 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 final : 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");
- }
-
- addrinfo hints;
- ::memset(&hints, 0, sizeof(addrinfo));
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- addrinfo* res;
-
- if(::getaddrinfo(hostname.c_str(), service.c_str(), &hints, &res) != 0)
- {
- throw Exception("Name resolution failed for " + hostname);
- }
-
- 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)
- {
- ::closesocket(m_socket);
- m_socket = INVALID_SOCKET;
- continue;
- }
- }
-
- ::freeaddrinfo(res);
-
- if(m_socket == INVALID_SOCKET)
- {
- throw Exception("Connecting to " + hostname +
- " for service " + service + " failed");
- }
- }
-
- ~Winsock_Socket()
- {
- ::closesocket(m_socket);
- m_socket = INVALID_SOCKET;
- ::WSACleanup();
- }
-
- 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,
- cast_uint8_ptr_to_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);
- }
- }
-
- size_t read(uint8_t buf[], size_t len) override
- {
- int got = ::recv(m_socket,
- cast_uint8_ptr_to_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);
- }
-
- private:
- SOCKET m_socket = INVALID_SOCKET;
- };
-
-#elif defined(BOTAN_TARGET_OS_TYPE_IS_UNIX)
-class BSD_Socket final : 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;
-
- if(::getaddrinfo(hostname.c_str(), service.c_str(), &hints, &res) != 0)
- {
- throw Exception("Name resolution failed for " + hostname);
- }
-
- 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);
-
- 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;
- }
- }
-
- ::freeaddrinfo(res);
-
- if(m_fd < 0)
- {
- throw Exception("Connecting to " + hostname +
- " for service " + service + " failed");
- }
- }
-
- ~BSD_Socket()
- {
- ::close(m_fd);
- m_fd = -1;
- }
-
- 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)) + "'");
- 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
-
-}
-
-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
- // No sockets for you
- return std::unique_ptr<Socket>();
-#endif
- }
-
// Not defined in OS namespace for historical reasons
void secure_scrub_memory(void* ptr, size_t n)
{
diff --git a/src/lib/utils/os_utils.h b/src/lib/utils/os_utils.h
index 405afb29c..feccdbe73 100644
--- a/src/lib/utils/os_utils.h
+++ b/src/lib/utils/os_utils.h
@@ -23,39 +23,6 @@ namespace OS {
* this hasn't been tested.
*/
-
-/**
-* A wrapper around a simple blocking TCP socket
-*/
-class BOTAN_TEST_API Socket
- {
- public:
- /**
- * The socket will be closed upon destruction
- */
- virtual ~Socket() {};
-
- /**
- * Write to the socket. Blocks until all bytes sent.
- * Throws on error.
- */
- virtual void write(const uint8_t buf[], size_t len) = 0;
-
- /**
- * Reads up to len bytes, returns bytes written to buf.
- * Returns 0 on EOF. Throws on error.
- */
- virtual size_t read(uint8_t buf[], size_t len) = 0;
- };
-
-/**
-* Open up a socket. Will throw on error. Returns null if sockets are
-* not available on this platform.
-*/
-std::unique_ptr<Socket>
-BOTAN_TEST_API open_socket(const std::string& hostname,
- const std::string& service);
-
/**
* @return process ID assigned by the operating system.
* On Unix and Windows systems, this always returns a result