forked from luncliff/coroutine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc2_socket.cpp
More file actions
118 lines (97 loc) · 3.04 KB
/
c2_socket.cpp
File metadata and controls
118 lines (97 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//
// Author : github.com/luncliff (luncliff@gmail.com)
// License : CC BY 4.0
//
#include <catch2/catch.hpp>
#include <gsl/gsl>
#include "./socket_test.h"
#if defined(_MSC_VER)
#include <Ws2tcpip.h>
#elif defined(__unix__) || defined(__linux__) || defined(__APPLE__)
#include <netinet/tcp.h>
#endif
using namespace std;
int64_t socket_create(const addrinfo& hint) {
int64_t sd = socket(hint.ai_family, hint.ai_socktype, hint.ai_protocol);
if (sd == -1)
FAIL(strerror(errno));
return sd;
}
auto socket_create(const addrinfo& hint, size_t count)
-> coro::enumerable<int64_t> {
while (count--) {
auto sd = socket_create(hint);
co_yield sd;
}
}
void socket_close(int64_t sd) {
#if defined(_MSC_VER)
shutdown(sd, SD_BOTH);
closesocket(sd);
#elif defined(__unix__) || defined(__linux__) || defined(__APPLE__)
shutdown(sd, SHUT_RDWR);
close(sd);
#endif
}
void socket_bind(int64_t sd, endpoint_t& ep) {
socklen_t addrlen = sizeof(sockaddr_in);
if (ep.storage.ss_family == AF_INET)
addrlen = sizeof(sockaddr_in);
if (ep.storage.ss_family == AF_INET6)
addrlen = sizeof(sockaddr_in6);
// bind socket and address
if (::bind(sd, &ep.addr, addrlen))
FAIL(strerror(errno));
}
void socket_listen(int64_t sd) {
if (::listen(sd, 7) != 0)
FAIL(strerror(errno));
}
void socket_set_option_nonblock(int64_t sd) {
#if defined(_MSC_VER)
u_long mode = TRUE;
REQUIRE(ioctlsocket(sd, FIONBIO, &mode) == NO_ERROR);
#elif defined(__unix__) || defined(__linux__) || defined(__APPLE__)
// make non-block/async
REQUIRE(fcntl(sd, F_SETFL, O_NONBLOCK) != -1);
#endif
}
void socket_set_option_reuse_address(int64_t sd) {
// reuse address for multiple test execution
int opt = true;
opt = setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char*)&opt, sizeof(opt));
if (opt != 0)
FAIL(strerror(errno));
}
void socket_set_option_nodelay(int64_t sd) {
// reuse address for multiple test execution
int opt = true;
opt = setsockopt(sd, IPPROTO_TCP, TCP_NODELAY, (char*)&opt, sizeof(opt));
if (opt != 0)
FAIL(strerror(errno));
}
#if defined(_MSC_VER)
WSADATA wsa_data{};
auto net_api_release = gsl::finally([]() noexcept {
// check and release
if (wsa_data.wVersion != 0)
::WSACleanup();
wsa_data.wVersion = 0;
});
void load_network_api() noexcept(false) {
using namespace std;
if (wsa_data.wVersion) // already initialized
return;
// init version 2.2
if (::WSAStartup(MAKEWORD(2, 2), &wsa_data)) {
auto errc = WSAGetLastError();
auto em = system_category().message(errc);
fputs(em.c_str(), stderr);
// throw system_error{errc, system_category(), "WSAStartup"};
}
}
#elif defined(__unix__) || defined(__linux__) || defined(__APPLE__)
void load_network_api() noexcept(false) {
// do nothing for posix system. network operation already available
}
#endif