-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnat_punch_socket_pool.cpp
More file actions
166 lines (155 loc) · 5.27 KB
/
Copy pathnat_punch_socket_pool.cpp
File metadata and controls
166 lines (155 loc) · 5.27 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "nat_punch_socket_pool.h"
#include <cerrno>
#ifndef _WIN32
#include <poll.h>
#endif
namespace {
socket_t OpenBoundIpv4UdpSocket(int receiveTimeoutMs, int* socketError) {
socket_t sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sock == kInvalidSocket) {
if (socketError != nullptr) *socketError = GetSocketError();
return sock;
}
sockaddr_in local{};
local.sin_family = AF_INET;
local.sin_addr.s_addr = htonl(INADDR_ANY);
local.sin_port = 0;
if (bind(sock, reinterpret_cast<const sockaddr*>(&local),
static_cast<socket_len_t>(sizeof(local))) != 0) {
if (socketError != nullptr) *socketError = GetSocketError();
CloseSocket(sock);
return kInvalidSocket;
}
SetSocketRecvTimeoutMs(sock, receiveTimeoutMs);
return sock;
}
UdpEndpoint FromSockaddr(const sockaddr_storage& address, socket_len_t len) {
UdpEndpoint endpoint{};
endpoint.addr = address;
endpoint.addr_len = len;
endpoint.family = address.ss_family;
return endpoint;
}
bool ReceiveDatagram(socket_t sock, std::vector<uint8_t>* buffer,
UdpEndpoint* source, int* received,
std::string* error) {
sockaddr_storage sourceAddress{};
socket_len_t sourceLen = static_cast<socket_len_t>(sizeof(sourceAddress));
*received = recvfrom(sock, reinterpret_cast<char*>(buffer->data()),
static_cast<int>(buffer->size()), 0,
reinterpret_cast<sockaddr*>(&sourceAddress), &sourceLen);
if (*received >= 0) {
*source = FromSockaddr(sourceAddress, sourceLen);
return true;
}
const int socketError = GetSocketError();
if (IsRecvTimeout(socketError)
|| IsUdpDestinationUnreachable(socketError)) {
return true;
}
*error = "UDP receive failed during adaptive NAT traversal. err="
+ std::to_string(socketError);
return false;
}
} // namespace
NatPunchSocketPool::NatPunchSocketPool(
socket_t primary, int receiveTimeoutMs,
NatPunchSocketOpener opener)
: receiveTimeoutMs_(receiveTimeoutMs),
opener_(opener == nullptr ? OpenBoundIpv4UdpSocket : opener) {
if (primary != kInvalidSocket) sockets_.push_back(primary);
}
NatPunchSocketPool::~NatPunchSocketPool() {
CloseAll();
}
bool NatPunchSocketPool::GrowTo(size_t desiredCount, int* socketError) {
if (socketError != nullptr) *socketError = 0;
while (sockets_.size() < desiredCount) {
socket_t auxiliary = opener_(receiveTimeoutMs_, socketError);
if (auxiliary == kInvalidSocket) return false;
sockets_.push_back(auxiliary);
}
return true;
}
bool NatPunchSocketPool::Receive(
int timeoutMs, std::vector<uint8_t>* buffer,
socket_t* receivingSocket, UdpEndpoint* source,
int* received, std::string* error) const {
*receivingSocket = kInvalidSocket;
*received = -1;
if (sockets_.empty()) {
*error = "No NAT punch sockets are available";
return false;
}
#ifdef _WIN32
std::vector<WSAPOLLFD> descriptors;
descriptors.reserve(sockets_.size());
for (const socket_t sock : sockets_) {
WSAPOLLFD descriptor{};
descriptor.fd = sock;
descriptor.events = POLLRDNORM;
descriptors.push_back(descriptor);
}
const int ready = WSAPoll(descriptors.data(),
static_cast<ULONG>(descriptors.size()), timeoutMs);
if (ready == SOCKET_ERROR) {
const int socketError = GetSocketError();
if (socketError == WSAEINTR) return true;
*error = "UDP poll failed during adaptive NAT traversal. err="
+ std::to_string(socketError);
return false;
}
if (ready == 0) return true;
for (size_t i = 0; i < descriptors.size(); ++i) {
if ((descriptors[i].revents
& (POLLRDNORM | POLLERR | POLLHUP)) == 0) {
continue;
}
*receivingSocket = sockets_[i];
return ReceiveDatagram(
sockets_[i], buffer, source, received, error);
}
#else
std::vector<pollfd> descriptors;
descriptors.reserve(sockets_.size());
for (const socket_t sock : sockets_) {
pollfd descriptor{};
descriptor.fd = sock;
descriptor.events = POLLIN;
descriptors.push_back(descriptor);
}
const int ready = poll(descriptors.data(), descriptors.size(), timeoutMs);
if (ready < 0) {
if (errno == EINTR) return true;
*error = "UDP poll failed during adaptive NAT traversal. err="
+ std::to_string(errno);
return false;
}
if (ready == 0) return true;
for (size_t i = 0; i < descriptors.size(); ++i) {
if ((descriptors[i].revents & (POLLIN | POLLERR | POLLHUP)) == 0) {
continue;
}
*receivingSocket = sockets_[i];
return ReceiveDatagram(
sockets_[i], buffer, source, received, error);
}
#endif
return true;
}
socket_t NatPunchSocketPool::ReleaseWinner(socket_t winner) {
bool found = false;
for (socket_t& sock : sockets_) {
if (sock == winner) {
found = true;
} else {
CloseSocket(sock);
}
}
sockets_.clear();
return found ? winner : kInvalidSocket;
}
void NatPunchSocketPool::CloseAll() {
for (socket_t& sock : sockets_) CloseSocket(sock);
sockets_.clear();
}