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

randombit / botan / 18884250355

28 Oct 2025 05:50PM UTC coverage: 90.668% (-0.003%) from 90.671%
18884250355

Pull #5127

github

web-flow
Merge 818b147ce into 4e91ea3e4
Pull Request #5127: Resolve TODO comments in socket utilities

100387 of 110719 relevant lines covered (90.67%)

12310261.37 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
    * MS documentation specifies 94 character max for user messages.
57
    * strerror_s truncates to buffer size - 1 and guarantees null termination.
58
    * Using 100 bytes yo ensure sufficient space with safety margin.
59
    * https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strerror-s-strerror-s-wcserror-s-wcserror-s
60
    */
61
   const auto msg = []() {
62
      std::array<char, 100> buf{};
63
      if(strerror_s(buf.data(), buf.size() - 1, e) == 0) {
64
         return std::string(buf.data());
65
      } else {
66
         return "failed to map error with strerror_s()"
67
      }
68
   }();
69
   return "Error code " + std::to_string(e) + " - " + msg;
70
}
71

72
inline int close(int fd) {
73
   return ::closesocket(fd);
74
}
75

76
inline int read(int s, void* buf, size_t len) {
77
   return ::recv(s, reinterpret_cast<char*>(buf), static_cast<int>(len), 0);
78
}
79

80
inline int send(int s, const uint8_t* buf, size_t len, int flags) {
81
   return ::send(s, reinterpret_cast<const char*>(buf), static_cast<int>(len), flags);
82
}
83

84
#elif defined(BOTAN_TARGET_OS_HAS_POSIX1)
85

86
   #include <arpa/inet.h>
87
   #include <errno.h>
88
   #include <fcntl.h>
89
   #include <netdb.h>
90
   #include <netinet/in.h>
91
   #include <sys/socket.h>
92
   #include <sys/time.h>
93
   #include <sys/types.h>
94
   #include <unistd.h>
95

96
typedef int socket_type;
97
typedef size_t sendrecv_len_type;
98

99
inline socket_type invalid_socket() {
13✔
100
   return -1;
13✔
101
}
102

103
inline void close_socket(socket_type s) {
12✔
104
   ::close(s);
12✔
105
}
12✔
106

107
inline void init_sockets() {}
13✔
108

109
inline void stop_sockets() {}
13✔
110

111
inline std::string err_to_string(int e) {
×
112
   return std::strerror(e);
×
113
}
114

115
#endif
116

117
#if !defined(MSG_NOSIGNAL)
118
   #define MSG_NOSIGNAL 0
119
#endif
120

121
#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