• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 18853482764

27 Oct 2025 07:29PM UTC coverage: 90.67% (+0.001%) from 90.669%
18853482764

Pull #5127

github

web-flow
Merge edc61f342 into 46f6fc3b2
Pull Request #5127: Resolve TODO comments in socket utilities

100389 of 110719 relevant lines covered (90.67%)

12219025.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

77.78
/src/cli/socket_utils.h
1
/*
2
* (C) 2014,2017 Jack Lloyd
3
*     2017 René Korthaus, Rohde & Schwarz Cybersecurity
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#ifndef BOTAN_CLI_SOCKET_UTILS_H_
9
#define BOTAN_CLI_SOCKET_UTILS_H_
10

11
#include "cli_exceptions.h"
12
#include <botan/types.h>
13
#include <botan/internal/target_info.h>
14
#include <cstring>
15

16
#if defined(BOTAN_TARGET_OS_HAS_WINSOCK2)
17

18
   #include <WS2tcpip.h>
19
   #include <winsock2.h>
20

21
typedef SOCKET socket_type;
22

23
inline socket_type invalid_socket() {
24
   return INVALID_SOCKET;
25
}
26

27
typedef size_t ssize_t;
28
typedef int sendrecv_len_type;
29

30
inline void close_socket(socket_type s) {
31
   ::closesocket(s);
32
}
33

34
   #define STDIN_FILENO _fileno(stdin)
35

36
inline void init_sockets() {
37
   WSAData wsa_data;
38
   WORD wsa_version = MAKEWORD(2, 2);
39

40
   if(::WSAStartup(wsa_version, &wsa_data) != 0) {
41
      throw Botan_CLI::CLI_Error("WSAStartup() failed: " + std::to_string(WSAGetLastError()));
42
   }
43

44
   if(LOBYTE(wsa_data.wVersion) != 2 || HIBYTE(wsa_data.wVersion) != 2) {
45
      ::WSACleanup();
46
      throw Botan_CLI::CLI_Error("Could not find a usable version of Winsock.dll");
47
   }
48
}
49

50
inline void stop_sockets() {
51
   ::WSACleanup();
52
}
53

54
inline std::string err_to_string(int e) {
55
   /*
56
    * Buffer size per MS documentation for strerror_s. "Your string message can be, at most, 94 characters long."
57
    * https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strerror-s-strerror-s-wcserror-s-wcserror-s
58
    */
59
   std::array<char, 94> buf{};
60
   if(strerror_s(buf.data(), buf.size(), e) == 0) {
61
      return std::string(buf.data());
62
   }
63
   return "Error code " + std::to_string(e);
64
}
65

66
inline int close(int fd) {
67
   return ::closesocket(fd);
68
}
69

70
inline int read(int s, void* buf, size_t len) {
71
   return ::recv(s, reinterpret_cast<char*>(buf), static_cast<int>(len), 0);
72
}
73

74
inline int send(int s, const uint8_t* buf, size_t len, int flags) {
75
   return ::send(s, reinterpret_cast<const char*>(buf), static_cast<int>(len), flags);
76
}
77

78
#elif defined(BOTAN_TARGET_OS_HAS_POSIX1)
79

80
   #include <arpa/inet.h>
81
   #include <errno.h>
82
   #include <fcntl.h>
83
   #include <netdb.h>
84
   #include <netinet/in.h>
85
   #include <sys/socket.h>
86
   #include <sys/time.h>
87
   #include <sys/types.h>
88
   #include <unistd.h>
89

90
typedef int socket_type;
91
typedef size_t sendrecv_len_type;
92

93
inline socket_type invalid_socket() {
13✔
94
   return -1;
13✔
95
}
96

97
inline void close_socket(socket_type s) {
12✔
98
   ::close(s);
12✔
99
}
12✔
100

101
inline void init_sockets() {}
13✔
102

103
inline void stop_sockets() {}
13✔
104

105
inline std::string err_to_string(int e) {
×
106
   return std::strerror(e);
×
107
}
108

109
#endif
110

111
#if !defined(MSG_NOSIGNAL)
112
   #define MSG_NOSIGNAL 0
113
#endif
114

115
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc