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

randombit / botan / 5696981105

28 Jul 2023 12:55PM UTC coverage: 91.69% (+0.005%) from 91.685%
5696981105

push

github

randombit
Merge GH #3649 Remove some macros from loadstor.h

78267 of 85360 relevant lines covered (91.69%)

12322548.38 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 <cstring>
14

15
#if defined(BOTAN_TARGET_OS_HAS_WINSOCK2)
16

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

20
typedef SOCKET socket_type;
21

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

26
typedef size_t ssize_t;
27
typedef int sendrecv_len_type;
28

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

33
   #define STDIN_FILENO _fileno(stdin)
34

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

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

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

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

53
inline std::string err_to_string(int e) {
54
   // TODO use strerror_s here
55
   return "Error code " + std::to_string(e);
56
}
57

58
inline int close(int fd) {
59
   return ::closesocket(fd);
60
}
61

62
inline int read(int s, void* buf, size_t len) {
63
   return ::recv(s, reinterpret_cast<char*>(buf), static_cast<int>(len), 0);
64
}
65

66
inline int send(int s, const uint8_t* buf, size_t len, int flags) {
67
   return ::send(s, reinterpret_cast<const char*>(buf), static_cast<int>(len), flags);
68
}
69

70
#elif defined(BOTAN_TARGET_OS_HAS_POSIX1)
71

72
   #include <arpa/inet.h>
73
   #include <errno.h>
74
   #include <fcntl.h>
75
   #include <netdb.h>
76
   #include <netinet/in.h>
77
   #include <sys/socket.h>
78
   #include <sys/time.h>
79
   #include <sys/types.h>
80
   #include <unistd.h>
81

82
typedef int socket_type;
83
typedef size_t sendrecv_len_type;
84

85
inline socket_type invalid_socket() {
7✔
86
   return -1;
7✔
87
}
88

89
inline void close_socket(socket_type s) {
5✔
90
   ::close(s);
5✔
91
}
5✔
92

93
inline void init_sockets() {}
7✔
94

95
inline void stop_sockets() {}
7✔
96

97
inline std::string err_to_string(int e) {
×
98
   return std::strerror(e);
×
99
}
100

101
#endif
102

103
#if !defined(MSG_NOSIGNAL)
104
   #define MSG_NOSIGNAL 0
105
#endif
106

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

© 2025 Coveralls, Inc