diff options
author | Jack Lloyd <[email protected]> | 2019-12-06 11:03:51 -0500 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2019-12-06 11:11:56 -0500 |
commit | 4537becd9f2e2084a7d5a072156a6754e8cbc861 (patch) | |
tree | 58e0405c83fe5dfb090471652c8c35e9814bba82 | |
parent | 22dd2f60084951bee277a145bbb3a0c91aca701b (diff) |
Fix warnings in CLI socket operations
Need to disable tls_client and tls_server here, at least temporarily.
tls_client clearly never worked, because select on stdin doesn't
work in Winsock.
tls_server seems like it would work but has warnings that don't make
any sense with certain versions of VC.
-rw-r--r-- | src/cli/socket_utils.h | 24 | ||||
-rw-r--r-- | src/cli/tls_client.cpp | 19 | ||||
-rw-r--r-- | src/cli/tls_server.cpp | 40 |
3 files changed, 54 insertions, 29 deletions
diff --git a/src/cli/socket_utils.h b/src/cli/socket_utils.h index 7856f5508..d52b5a0e7 100644 --- a/src/cli/socket_utils.h +++ b/src/cli/socket_utils.h @@ -16,7 +16,14 @@ #include <winsock2.h> #include <WS2tcpip.h> +typedef SOCKET socket_type; + +inline socket_type invalid_socket() { return INVALID_SOCKET; } + typedef size_t ssize_t; +typedef int sendrecv_len_type; + +inline void close_socket(socket_type s) { ::closesocket(s); } #define STDIN_FILENO _fileno(stdin) @@ -42,6 +49,12 @@ inline void stop_sockets() ::WSACleanup(); } +inline std::string err_to_string(int e) + { + // TODO use strerror_s here + return "Error code " + std::to_string(e); + } + inline int close(int fd) { return ::closesocket(fd); @@ -69,9 +82,20 @@ inline int send(int s, const uint8_t* buf, size_t len, int flags) #include <errno.h> #include <fcntl.h> +typedef int socket_type; +typedef size_t sendrecv_len_type; + +inline socket_type invalid_socket() { return -1; } +inline void close_socket(socket_type s) { ::close(s); } + inline void init_sockets() {} inline void stop_sockets() {} +inline std::string err_to_string(int e) + { + return std::strerror(e); + } + #endif #if !defined(MSG_NOSIGNAL) diff --git a/src/cli/tls_client.cpp b/src/cli/tls_client.cpp index 4df8b61cc..9541f8fbc 100644 --- a/src/cli/tls_client.cpp +++ b/src/cli/tls_client.cpp @@ -8,8 +8,7 @@ #include "cli.h" -#if defined(BOTAN_HAS_TLS) && defined(BOTAN_TARGET_OS_HAS_FILESYSTEM) && \ - (defined(BOTAN_TARGET_OS_HAS_SOCKETS) || defined(BOTAN_TARGET_OS_HAS_WINSOCK2)) +#if defined(BOTAN_HAS_TLS) && defined(BOTAN_TARGET_OS_HAS_FILESYSTEM) && defined(BOTAN_TARGET_OS_HAS_SOCKETS) #include <botan/tls_client.h> #include <botan/tls_policy.h> @@ -195,7 +194,7 @@ class TLS_Client final : public Command, public Botan::TLS::Callbacks struct timeval timeout = { 1, 0 }; - ::select(m_sockfd + 1, &readfds, nullptr, nullptr, &timeout); + ::select(static_cast<int>(m_sockfd + 1), &readfds, nullptr, nullptr, &timeout); if(FD_ISSET(m_sockfd, &readfds)) { @@ -210,7 +209,7 @@ class TLS_Client final : public Command, public Botan::TLS::Callbacks } else if(got == -1) { - output() << "Socket error: " << errno << " " << std::strerror(errno) << "\n"; + output() << "Socket error: " << errno << " " << err_to_string(errno) << "\n"; continue; } @@ -230,7 +229,7 @@ class TLS_Client final : public Command, public Botan::TLS::Callbacks } else if(got == -1) { - output() << "Stdin error: " << errno << " " << std::strerror(errno) << "\n"; + output() << "Stdin error: " << errno << " " << err_to_string(errno) << "\n"; continue; } @@ -265,7 +264,7 @@ class TLS_Client final : public Command, public Botan::TLS::Callbacks } private: - int connect_to_host(const std::string& host, uint16_t port, bool tcp) + socket_type connect_to_host(const std::string& host, uint16_t port, bool tcp) { addrinfo hints; Botan::clear_mem(&hints, 1); @@ -278,18 +277,18 @@ class TLS_Client final : public Command, public Botan::TLS::Callbacks throw CLI_Error("getaddrinfo failed for " + host); } - int fd = 0; + socket_type fd = 0; for(rp = res; rp != nullptr; rp = rp->ai_next) { fd = ::socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); - if(fd == -1) + if(fd == invalid_socket()) { continue; } - if(::connect(fd, rp->ai_addr, rp->ai_addrlen) != 0) + if(::connect(fd, rp->ai_addr, static_cast<socklen_t>(rp->ai_addrlen)) != 0) { ::close(fd); continue; @@ -427,7 +426,7 @@ class TLS_Client final : public Command, public Botan::TLS::Callbacks } } - int m_sockfd = -1; + socket_type m_sockfd = invalid_socket(); }; BOTAN_REGISTER_COMMAND("tls_client", TLS_Client); diff --git a/src/cli/tls_server.cpp b/src/cli/tls_server.cpp index 8b6a60ea4..c39061e64 100644 --- a/src/cli/tls_server.cpp +++ b/src/cli/tls_server.cpp @@ -10,7 +10,7 @@ #include "sandbox.h" #if defined(BOTAN_HAS_TLS) && defined(BOTAN_TARGET_OS_HAS_FILESYSTEM) && \ - (defined(BOTAN_TARGET_OS_HAS_SOCKETS) || defined(BOTAN_TARGET_OS_HAS_WINSOCK2)) + defined(BOTAN_TARGET_OS_HAS_SOCKETS) #if defined(SO_USER_COOKIE) #define SOCKET_ID 1 @@ -22,6 +22,7 @@ #include <botan/tls_policy.h> #include <botan/hex.h> #include <botan/internal/os_utils.h> +#include <botan/mem_ops.h> #include <list> #include <fstream> @@ -91,7 +92,7 @@ class TLS_Server final : public Command, public Botan::TLS::Callbacks return; } - int server_fd = make_server_socket(port); + socket_type server_fd = make_server_socket(port); size_t clients_served = 0; while(true) @@ -119,7 +120,8 @@ class TLS_Server final : public Command, public Botan::TLS::Callbacks peek_len = sizeof(dummy); #endif - if(::recvfrom(server_fd, static_cast<char*>(peek_buf), peek_len, MSG_PEEK, reinterpret_cast<struct sockaddr*>(&from), &from_len) != 0) + if(::recvfrom(server_fd, static_cast<char*>(peek_buf), static_cast<sendrecv_len_type>(peek_len), + MSG_PEEK, reinterpret_cast<struct sockaddr*>(&from), &from_len) != 0) { throw CLI_Error("Could not peek next packet"); } @@ -158,11 +160,11 @@ class TLS_Server final : public Command, public Botan::TLS::Callbacks try { uint8_t buf[4 * 1024] = { 0 }; - ssize_t got = ::read(m_socket, buf, sizeof(buf)); + ssize_t got = ::recv(m_socket, Botan::cast_uint8_ptr_to_char(buf), sizeof(buf), 0); if(got == -1) { - error_output() << "Error in socket read - " << std::strerror(errno) << std::endl; + error_output() << "Error in socket read - " << err_to_string(errno) << std::endl; break; } @@ -196,8 +198,8 @@ class TLS_Server final : public Command, public Botan::TLS::Callbacks error_output() << "Connection problem: " << e.what() << std::endl; if(m_is_tcp) { - ::close(m_socket); - m_socket = -1; + close_socket(m_socket); + m_socket = invalid_socket(); } } } @@ -209,20 +211,20 @@ class TLS_Server final : public Command, public Botan::TLS::Callbacks if(m_is_tcp) { - ::close(m_socket); - m_socket = -1; + close_socket(m_socket); + m_socket = invalid_socket(); } } - ::close(server_fd); + close_socket(server_fd); } private: - int make_server_socket(uint16_t port) + socket_type make_server_socket(uint16_t port) { const int type = m_is_tcp ? SOCK_STREAM : SOCK_DGRAM; - int fd = ::socket(PF_INET, type, 0); - if(fd == -1) + socket_type fd = ::socket(PF_INET, type, 0); + if(fd == invalid_socket()) { throw CLI_Error("Unable to acquire socket"); } @@ -237,7 +239,7 @@ class TLS_Server final : public Command, public Botan::TLS::Callbacks if(::bind(fd, reinterpret_cast<struct sockaddr*>(&socket_info), sizeof(struct sockaddr)) != 0) { - ::close(fd); + close_socket(fd); throw CLI_Error("server bind failed"); } @@ -245,7 +247,7 @@ class TLS_Server final : public Command, public Botan::TLS::Callbacks { if(::listen(fd, 100) != 0) { - ::close(fd); + close_socket(fd); throw CLI_Error("listen failed"); } } @@ -301,11 +303,11 @@ class TLS_Server final : public Command, public Botan::TLS::Callbacks { if(m_is_tcp) { - ssize_t sent = ::send(m_socket, buf, length, MSG_NOSIGNAL); + ssize_t sent = ::send(m_socket, buf, static_cast<sendrecv_len_type>(length), MSG_NOSIGNAL); if(sent == -1) { - error_output() << "Error writing to socket - " << std::strerror(errno) << std::endl; + error_output() << "Error writing to socket - " << err_to_string(errno) << std::endl; } else if(sent != static_cast<ssize_t>(length)) { @@ -316,7 +318,7 @@ class TLS_Server final : public Command, public Botan::TLS::Callbacks { while(length) { - ssize_t sent = ::send(m_socket, buf, length, MSG_NOSIGNAL); + ssize_t sent = ::send(m_socket, buf, static_cast<sendrecv_len_type>(length), MSG_NOSIGNAL); if(sent == -1) { @@ -347,7 +349,7 @@ class TLS_Server final : public Command, public Botan::TLS::Callbacks return "echo/0.1"; } - int m_socket = -1; + socket_type m_socket = invalid_socket(); bool m_is_tcp = false; uint32_t m_socket_id = 0; std::string m_line_buf; |