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

randombit / botan / 18906977410

29 Oct 2025 11:54AM UTC coverage: 90.674% (+0.006%) from 90.668%
18906977410

push

github

web-flow
Merge pull request #5127 from KaganCanSit/remove-todos-socket-utilities

Resolve TODO comments in socket utilities

100425 of 110754 relevant lines covered (90.67%)

12189943.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
    * 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
   std::array<char, 100> buf{};
62
   const auto res = strerror_s(buf.data(), buf.size() - 1, e);
63
   const std::string_view msg = (res == 0) ? buf.data() : "failed to map error with strerror_s()";
64
   return Botan::fmt("Error: {} - {}", e, msg);
65
}
66

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

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

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

79
#elif defined(BOTAN_TARGET_OS_HAS_POSIX1)
80

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

91
typedef int socket_type;
92
typedef size_t sendrecv_len_type;
93

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

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

102
inline void init_sockets() {}
13✔
103

104
inline void stop_sockets() {}
13✔
105

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

110
#endif
111

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

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