2021-07-04 20:31:23 -06:00
|
|
|
// SPDX-License-Identifier: CC0-1.0
|
2015-02-16 23:53:50 -07:00
|
|
|
|
|
|
|
// The central server implementation.
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <cerrno>
|
2017-11-01 15:56:17 -06:00
|
|
|
#include <chrono>
|
2015-02-16 23:53:50 -07:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <netinet/in.h>
|
2023-01-12 09:45:49 -07:00
|
|
|
#include <sys/select.h>
|
2015-02-16 23:53:50 -07:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
2023-01-12 09:45:49 -07:00
|
|
|
#include <tuple>
|
2015-02-16 23:53:50 -07:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2018-03-07 23:10:17 -07:00
|
|
|
|
2023-12-18 08:48:42 -07:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2018-03-07 23:10:17 -07:00
|
|
|
#ifdef HAVE_LIBSYSTEMD
|
|
|
|
#include <systemd/sd-daemon.h>
|
|
|
|
#endif
|
|
|
|
|
2018-05-21 07:48:17 -06:00
|
|
|
#include "Common/Random.h"
|
2015-02-16 23:53:50 -07:00
|
|
|
#include "Common/TraversalProto.h"
|
|
|
|
|
2017-02-25 21:41:12 -07:00
|
|
|
#define DEBUG 0
|
2015-02-16 23:53:50 -07:00
|
|
|
#define NUMBER_OF_TRIES 5
|
2018-03-07 23:10:17 -07:00
|
|
|
#define PORT 6262
|
2023-01-12 09:45:49 -07:00
|
|
|
#define PORT_ALT 6226
|
2015-02-16 23:53:50 -07:00
|
|
|
|
|
|
|
static u64 currentTime;
|
|
|
|
|
|
|
|
struct OutgoingPacketInfo
|
|
|
|
{
|
2023-04-24 06:29:12 -06:00
|
|
|
Common::TraversalPacket packet;
|
|
|
|
Common::TraversalRequestId misc;
|
2023-01-12 09:45:49 -07:00
|
|
|
bool fromAlt;
|
2015-02-16 23:53:50 -07:00
|
|
|
sockaddr_in6 dest;
|
|
|
|
int tries;
|
|
|
|
u64 sendTime;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct EvictEntry
|
|
|
|
{
|
|
|
|
u64 updateTime;
|
|
|
|
T value;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename V>
|
|
|
|
struct EvictFindResult
|
|
|
|
{
|
|
|
|
bool found;
|
|
|
|
V* value;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename K, typename V>
|
|
|
|
EvictFindResult<V> EvictFind(std::unordered_map<K, EvictEntry<V>>& map, const K& key,
|
|
|
|
bool refresh = false)
|
|
|
|
{
|
|
|
|
retry:
|
|
|
|
const u64 expiryTime = 30 * 1000000; // 30s
|
|
|
|
EvictFindResult<V> result;
|
|
|
|
if (map.bucket_count())
|
|
|
|
{
|
|
|
|
auto bucket = map.bucket(key);
|
|
|
|
auto it = map.begin(bucket);
|
|
|
|
for (; it != map.end(bucket); ++it)
|
|
|
|
{
|
|
|
|
if (currentTime - it->second.updateTime > expiryTime)
|
|
|
|
{
|
|
|
|
map.erase(it->first);
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
if (it->first == key)
|
|
|
|
{
|
|
|
|
if (refresh)
|
|
|
|
it->second.updateTime = currentTime;
|
|
|
|
result.found = true;
|
|
|
|
result.value = &it->second.value;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#if DEBUG
|
2023-12-18 08:55:31 -07:00
|
|
|
fmt::print("failed to find key '");
|
2015-02-16 23:53:50 -07:00
|
|
|
for (size_t i = 0; i < sizeof(key); i++)
|
|
|
|
{
|
2023-12-18 08:55:31 -07:00
|
|
|
fmt::print("{:02x}", ((u8*)&key)[i]);
|
2015-02-16 23:53:50 -07:00
|
|
|
}
|
2023-12-18 08:55:31 -07:00
|
|
|
fmt::print("'\n");
|
2015-02-16 23:53:50 -07:00
|
|
|
#endif
|
|
|
|
result.found = false;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename K, typename V>
|
|
|
|
V* EvictSet(std::unordered_map<K, EvictEntry<V>>& map, const K& key)
|
|
|
|
{
|
|
|
|
// can't use a local_iterator to emplace...
|
|
|
|
auto& result = map[key];
|
|
|
|
result.updateTime = currentTime;
|
|
|
|
return &result.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace std
|
|
|
|
{
|
|
|
|
template <>
|
2023-04-24 06:29:12 -06:00
|
|
|
struct hash<Common::TraversalHostId>
|
2015-02-16 23:53:50 -07:00
|
|
|
{
|
2023-04-24 06:29:12 -06:00
|
|
|
size_t operator()(const Common::TraversalHostId& id) const noexcept
|
2015-02-16 23:53:50 -07:00
|
|
|
{
|
|
|
|
auto p = (u32*)id.data();
|
|
|
|
return p[0] ^ ((p[1] << 13) | (p[1] >> 19));
|
|
|
|
}
|
|
|
|
};
|
2019-05-05 17:48:12 -06:00
|
|
|
} // namespace std
|
2015-02-16 23:53:50 -07:00
|
|
|
|
2023-04-24 06:29:12 -06:00
|
|
|
using ConnectedClients =
|
|
|
|
std::unordered_map<Common::TraversalHostId, EvictEntry<Common::TraversalInetAddress>>;
|
|
|
|
using OutgoingPackets = std::unordered_map<Common::TraversalRequestId, OutgoingPacketInfo>;
|
|
|
|
|
2015-02-16 23:53:50 -07:00
|
|
|
static int sock;
|
2023-01-12 09:45:49 -07:00
|
|
|
static int sockAlt;
|
2023-04-24 06:29:12 -06:00
|
|
|
static OutgoingPackets outgoingPackets;
|
|
|
|
static ConnectedClients connectedClients;
|
2015-02-16 23:53:50 -07:00
|
|
|
|
2023-04-24 06:29:12 -06:00
|
|
|
static Common::TraversalInetAddress MakeInetAddress(const sockaddr_in6& addr)
|
2015-02-16 23:53:50 -07:00
|
|
|
{
|
|
|
|
if (addr.sin6_family != AF_INET6)
|
|
|
|
{
|
2023-12-18 08:55:31 -07:00
|
|
|
fmt::print(stderr, "bad sockaddr_in6\n");
|
2015-02-16 23:53:50 -07:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
u32* words = (u32*)addr.sin6_addr.s6_addr;
|
2023-04-24 06:29:12 -06:00
|
|
|
Common::TraversalInetAddress result = {};
|
2015-02-16 23:53:50 -07:00
|
|
|
if (words[0] == 0 && words[1] == 0 && words[2] == 0xffff0000)
|
|
|
|
{
|
|
|
|
result.isIPV6 = false;
|
|
|
|
result.address[0] = words[3];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result.isIPV6 = true;
|
|
|
|
memcpy(result.address, words, sizeof(result.address));
|
|
|
|
}
|
|
|
|
result.port = addr.sin6_port;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-04-24 06:29:12 -06:00
|
|
|
static sockaddr_in6 MakeSinAddr(const Common::TraversalInetAddress& addr)
|
2015-02-16 23:53:50 -07:00
|
|
|
{
|
|
|
|
sockaddr_in6 result;
|
|
|
|
#ifdef SIN6_LEN
|
|
|
|
result.sin6_len = sizeof(result);
|
|
|
|
#endif
|
|
|
|
result.sin6_family = AF_INET6;
|
|
|
|
result.sin6_port = addr.port;
|
|
|
|
result.sin6_flowinfo = 0;
|
|
|
|
if (addr.isIPV6)
|
|
|
|
{
|
|
|
|
memcpy(&result.sin6_addr, addr.address, 16);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
u32* words = (u32*)result.sin6_addr.s6_addr;
|
|
|
|
words[0] = 0;
|
|
|
|
words[1] = 0;
|
|
|
|
words[2] = 0xffff0000;
|
|
|
|
words[3] = addr.address[0];
|
|
|
|
}
|
|
|
|
result.sin6_scope_id = 0;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-04-24 06:29:12 -06:00
|
|
|
static void GetRandomHostId(Common::TraversalHostId* hostId)
|
2015-02-16 23:53:50 -07:00
|
|
|
{
|
2023-12-18 08:48:42 -07:00
|
|
|
char buf[9]{};
|
2018-06-14 08:26:11 -06:00
|
|
|
const u32 num = Common::Random::GenerateValue<u32>();
|
2023-12-18 08:48:42 -07:00
|
|
|
fmt::format_to_n(buf, sizeof(buf) - 1, "{:08x}", num);
|
2015-02-16 23:53:50 -07:00
|
|
|
memcpy(hostId->data(), buf, 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char* SenderName(sockaddr_in6* addr)
|
|
|
|
{
|
2023-12-18 08:48:42 -07:00
|
|
|
static char buf[INET6_ADDRSTRLEN + 10]{};
|
2015-02-16 23:53:50 -07:00
|
|
|
inet_ntop(PF_INET6, &addr->sin6_addr, buf, sizeof(buf));
|
2023-12-18 08:48:42 -07:00
|
|
|
fmt::format_to(buf + strlen(buf), ":{}", ntohs(addr->sin6_port));
|
2015-02-16 23:53:50 -07:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2023-01-12 09:45:49 -07:00
|
|
|
static void TrySend(const void* buffer, size_t size, sockaddr_in6* addr, bool fromAlt)
|
2015-02-16 23:53:50 -07:00
|
|
|
{
|
|
|
|
#if DEBUG
|
2023-04-24 06:29:12 -06:00
|
|
|
const auto* packet = static_cast<const Common::TraversalPacket*>(buffer);
|
2023-12-18 08:55:31 -07:00
|
|
|
fmt::print("{}-> {} {} {}\n", fromAlt ? "alt " : "", static_cast<int>(packet->type),
|
|
|
|
static_cast<long long>(packet->requestId), SenderName(addr));
|
2015-02-16 23:53:50 -07:00
|
|
|
#endif
|
2023-01-12 09:45:49 -07:00
|
|
|
if ((size_t)sendto(fromAlt ? sockAlt : sock, buffer, size, 0, (sockaddr*)addr, sizeof(*addr)) !=
|
|
|
|
size)
|
2015-02-16 23:53:50 -07:00
|
|
|
{
|
|
|
|
perror("sendto");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-12 09:45:49 -07:00
|
|
|
static Common::TraversalPacket* AllocPacket(const sockaddr_in6& dest, bool fromAlt,
|
2023-04-24 06:29:12 -06:00
|
|
|
Common::TraversalRequestId misc = 0)
|
2015-02-16 23:53:50 -07:00
|
|
|
{
|
2023-04-24 06:29:12 -06:00
|
|
|
Common::TraversalRequestId requestId{};
|
2018-05-21 07:48:17 -06:00
|
|
|
Common::Random::Generate(&requestId, sizeof(requestId));
|
2015-02-16 23:53:50 -07:00
|
|
|
OutgoingPacketInfo* info = &outgoingPackets[requestId];
|
2023-01-12 09:45:49 -07:00
|
|
|
info->fromAlt = fromAlt;
|
2015-02-16 23:53:50 -07:00
|
|
|
info->dest = dest;
|
|
|
|
info->misc = misc;
|
|
|
|
info->tries = 0;
|
|
|
|
info->sendTime = currentTime;
|
2023-04-24 06:29:12 -06:00
|
|
|
Common::TraversalPacket* result = &info->packet;
|
2015-02-16 23:53:50 -07:00
|
|
|
memset(result, 0, sizeof(*result));
|
|
|
|
result->requestId = requestId;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void SendPacket(OutgoingPacketInfo* info)
|
|
|
|
{
|
|
|
|
info->tries++;
|
|
|
|
info->sendTime = currentTime;
|
2023-01-12 09:45:49 -07:00
|
|
|
TrySend(&info->packet, sizeof(info->packet), &info->dest, info->fromAlt);
|
2015-02-16 23:53:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ResendPackets()
|
|
|
|
{
|
2023-01-12 09:45:49 -07:00
|
|
|
std::vector<std::tuple<Common::TraversalInetAddress, bool, Common::TraversalRequestId>>
|
|
|
|
todoFailures;
|
2015-02-16 23:53:50 -07:00
|
|
|
todoFailures.clear();
|
|
|
|
for (auto it = outgoingPackets.begin(); it != outgoingPackets.end();)
|
|
|
|
{
|
|
|
|
OutgoingPacketInfo* info = &it->second;
|
|
|
|
if (currentTime - info->sendTime >= (u64)(300000 * info->tries))
|
|
|
|
{
|
|
|
|
if (info->tries >= NUMBER_OF_TRIES)
|
|
|
|
{
|
2023-04-24 06:29:12 -06:00
|
|
|
if (info->packet.type == Common::TraversalPacketType::PleaseSendPacket)
|
2015-02-16 23:53:50 -07:00
|
|
|
{
|
2023-01-12 09:45:49 -07:00
|
|
|
todoFailures.push_back(
|
|
|
|
std::make_tuple(info->packet.pleaseSendPacket.address, info->fromAlt, info->misc));
|
2015-02-16 23:53:50 -07:00
|
|
|
}
|
|
|
|
it = outgoingPackets.erase(it);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SendPacket(info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-02-16 23:53:50 -07:00
|
|
|
for (const auto& p : todoFailures)
|
|
|
|
{
|
2023-01-12 09:45:49 -07:00
|
|
|
Common::TraversalPacket* fail = AllocPacket(MakeSinAddr(std::get<0>(p)), std::get<1>(p));
|
2023-04-24 06:29:12 -06:00
|
|
|
fail->type = Common::TraversalPacketType::ConnectFailed;
|
2023-01-12 09:45:49 -07:00
|
|
|
fail->connectFailed.requestId = std::get<2>(p);
|
2023-04-24 06:29:12 -06:00
|
|
|
fail->connectFailed.reason = Common::TraversalConnectFailedReason::ClientDidntRespond;
|
2015-02-16 23:53:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-12 09:45:49 -07:00
|
|
|
static void HandlePacket(Common::TraversalPacket* packet, sockaddr_in6* addr, bool toAlt)
|
2015-02-16 23:53:50 -07:00
|
|
|
{
|
|
|
|
#if DEBUG
|
2023-12-18 08:55:31 -07:00
|
|
|
fmt::print("<- {} {} {}\n", static_cast<int>(packet->type),
|
|
|
|
static_cast<long long>(packet->requestId), SenderName(addr));
|
2015-02-16 23:53:50 -07:00
|
|
|
#endif
|
|
|
|
bool packetOk = true;
|
|
|
|
switch (packet->type)
|
|
|
|
{
|
2023-04-24 06:29:12 -06:00
|
|
|
case Common::TraversalPacketType::Ack:
|
2015-02-16 23:53:50 -07:00
|
|
|
{
|
|
|
|
auto it = outgoingPackets.find(packet->requestId);
|
|
|
|
if (it == outgoingPackets.end())
|
|
|
|
break;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-02-16 23:53:50 -07:00
|
|
|
OutgoingPacketInfo* info = &it->second;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2023-04-24 06:29:12 -06:00
|
|
|
if (info->packet.type == Common::TraversalPacketType::PleaseSendPacket)
|
2015-02-16 23:53:50 -07:00
|
|
|
{
|
2023-01-12 09:45:49 -07:00
|
|
|
auto* ready = AllocPacket(MakeSinAddr(info->packet.pleaseSendPacket.address), toAlt);
|
2015-02-16 23:53:50 -07:00
|
|
|
if (packet->ack.ok)
|
|
|
|
{
|
2023-04-24 06:29:12 -06:00
|
|
|
ready->type = Common::TraversalPacketType::ConnectReady;
|
2015-02-16 23:53:50 -07:00
|
|
|
ready->connectReady.requestId = info->misc;
|
|
|
|
ready->connectReady.address = MakeInetAddress(info->dest);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-24 06:29:12 -06:00
|
|
|
ready->type = Common::TraversalPacketType::ConnectFailed;
|
2015-02-16 23:53:50 -07:00
|
|
|
ready->connectFailed.requestId = info->misc;
|
2023-04-24 06:29:12 -06:00
|
|
|
ready->connectFailed.reason = Common::TraversalConnectFailedReason::ClientFailure;
|
2015-02-16 23:53:50 -07:00
|
|
|
}
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-02-16 23:53:50 -07:00
|
|
|
outgoingPackets.erase(it);
|
|
|
|
break;
|
|
|
|
}
|
2023-04-24 06:29:12 -06:00
|
|
|
case Common::TraversalPacketType::Ping:
|
2015-02-16 23:53:50 -07:00
|
|
|
{
|
|
|
|
auto r = EvictFind(connectedClients, packet->ping.hostId, true);
|
|
|
|
packetOk = r.found;
|
|
|
|
break;
|
|
|
|
}
|
2023-04-24 06:29:12 -06:00
|
|
|
case Common::TraversalPacketType::HelloFromClient:
|
2015-02-16 23:53:50 -07:00
|
|
|
{
|
2023-04-24 06:29:12 -06:00
|
|
|
u8 ok = packet->helloFromClient.protoVersion <= Common::TraversalProtoVersion;
|
2023-01-12 09:45:49 -07:00
|
|
|
Common::TraversalPacket* reply = AllocPacket(*addr, toAlt);
|
2023-04-24 06:29:12 -06:00
|
|
|
reply->type = Common::TraversalPacketType::HelloFromServer;
|
2015-02-16 23:53:50 -07:00
|
|
|
reply->helloFromServer.ok = ok;
|
|
|
|
if (ok)
|
|
|
|
{
|
2023-04-24 06:29:12 -06:00
|
|
|
Common::TraversalHostId hostId{};
|
|
|
|
Common::TraversalInetAddress* iaddr{};
|
2015-02-16 23:53:50 -07:00
|
|
|
// not that there is any significant change of
|
|
|
|
// duplication, but...
|
|
|
|
while (true)
|
|
|
|
{
|
2023-07-13 05:33:36 -06:00
|
|
|
GetRandomHostId(&hostId);
|
2015-02-16 23:53:50 -07:00
|
|
|
auto r = EvictFind(connectedClients, hostId);
|
|
|
|
if (!r.found)
|
|
|
|
{
|
|
|
|
iaddr = EvictSet(connectedClients, hostId);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-02-16 23:53:50 -07:00
|
|
|
*iaddr = MakeInetAddress(*addr);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-02-16 23:53:50 -07:00
|
|
|
reply->helloFromServer.yourAddress = *iaddr;
|
|
|
|
reply->helloFromServer.yourHostId = hostId;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2023-04-24 06:29:12 -06:00
|
|
|
case Common::TraversalPacketType::ConnectPlease:
|
2015-02-16 23:53:50 -07:00
|
|
|
{
|
2023-04-24 06:29:12 -06:00
|
|
|
Common::TraversalHostId& hostId = packet->connectPlease.hostId;
|
2015-02-16 23:53:50 -07:00
|
|
|
auto r = EvictFind(connectedClients, hostId);
|
|
|
|
if (!r.found)
|
|
|
|
{
|
2023-01-12 09:45:49 -07:00
|
|
|
Common::TraversalPacket* reply = AllocPacket(*addr, toAlt);
|
2023-04-24 06:29:12 -06:00
|
|
|
reply->type = Common::TraversalPacketType::ConnectFailed;
|
2015-02-16 23:53:50 -07:00
|
|
|
reply->connectFailed.requestId = packet->requestId;
|
2023-04-24 06:29:12 -06:00
|
|
|
reply->connectFailed.reason = Common::TraversalConnectFailedReason::NoSuchClient;
|
2015-02-16 23:53:50 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-01-12 09:45:49 -07:00
|
|
|
Common::TraversalPacket* please =
|
|
|
|
AllocPacket(MakeSinAddr(*r.value), toAlt, packet->requestId);
|
2023-04-24 06:29:12 -06:00
|
|
|
please->type = Common::TraversalPacketType::PleaseSendPacket;
|
2015-02-16 23:53:50 -07:00
|
|
|
please->pleaseSendPacket.address = MakeInetAddress(*addr);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2023-01-12 09:45:49 -07:00
|
|
|
case Common::TraversalPacketType::TestPlease:
|
|
|
|
{
|
|
|
|
Common::TraversalHostId& hostId = packet->testPlease.hostId;
|
|
|
|
auto r = EvictFind(connectedClients, hostId);
|
|
|
|
if (r.found)
|
|
|
|
{
|
|
|
|
Common::TraversalPacket ack = {};
|
|
|
|
ack.type = Common::TraversalPacketType::Ack;
|
|
|
|
ack.requestId = packet->requestId;
|
|
|
|
ack.ack.ok = true;
|
|
|
|
sockaddr_in6 mainAddr = MakeSinAddr(*r.value);
|
|
|
|
TrySend(&ack, sizeof(ack), &mainAddr, toAlt);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2015-02-16 23:53:50 -07:00
|
|
|
default:
|
2023-12-18 08:55:31 -07:00
|
|
|
fmt::print(stderr, "received unknown packet type {} from {}\n", static_cast<int>(packet->type),
|
|
|
|
SenderName(addr));
|
2021-01-19 12:11:39 -07:00
|
|
|
break;
|
2015-02-16 23:53:50 -07:00
|
|
|
}
|
2023-04-24 06:29:12 -06:00
|
|
|
if (packet->type != Common::TraversalPacketType::Ack)
|
2015-02-16 23:53:50 -07:00
|
|
|
{
|
2023-04-24 06:29:12 -06:00
|
|
|
Common::TraversalPacket ack = {};
|
|
|
|
ack.type = Common::TraversalPacketType::Ack;
|
2015-02-16 23:53:50 -07:00
|
|
|
ack.requestId = packet->requestId;
|
|
|
|
ack.ack.ok = packetOk;
|
2023-01-12 09:45:49 -07:00
|
|
|
TrySend(&ack, sizeof(ack), addr,
|
|
|
|
packet->type != Common::TraversalPacketType::TestPlease ? toAlt : !toAlt);
|
2015-02-16 23:53:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
sock = socket(PF_INET6, SOCK_DGRAM, 0);
|
|
|
|
if (sock == -1)
|
|
|
|
{
|
|
|
|
perror("socket");
|
|
|
|
return 1;
|
|
|
|
}
|
2023-01-12 09:45:49 -07:00
|
|
|
sockAlt = socket(PF_INET6, SOCK_DGRAM, 0);
|
|
|
|
if (sockAlt == -1)
|
|
|
|
{
|
|
|
|
perror("socket alt");
|
|
|
|
return 1;
|
|
|
|
}
|
2015-02-16 23:53:50 -07:00
|
|
|
int no = 0;
|
|
|
|
rv = setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &no, sizeof(no));
|
|
|
|
if (rv < 0)
|
|
|
|
{
|
|
|
|
perror("setsockopt IPV6_V6ONLY");
|
|
|
|
return 1;
|
|
|
|
}
|
2023-01-12 09:45:49 -07:00
|
|
|
rv = setsockopt(sockAlt, IPPROTO_IPV6, IPV6_V6ONLY, &no, sizeof(no));
|
|
|
|
if (rv < 0)
|
|
|
|
{
|
|
|
|
perror("setsockopt IPV6_V6ONLY alt");
|
|
|
|
return 1;
|
|
|
|
}
|
2015-02-16 23:53:50 -07:00
|
|
|
in6_addr any = IN6ADDR_ANY_INIT;
|
|
|
|
sockaddr_in6 addr;
|
|
|
|
#ifdef SIN6_LEN
|
|
|
|
addr.sin6_len = sizeof(addr);
|
|
|
|
#endif
|
|
|
|
addr.sin6_family = AF_INET6;
|
2018-03-07 23:10:17 -07:00
|
|
|
addr.sin6_port = htons(PORT);
|
2015-02-16 23:53:50 -07:00
|
|
|
addr.sin6_flowinfo = 0;
|
|
|
|
addr.sin6_addr = any;
|
|
|
|
addr.sin6_scope_id = 0;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-02-16 23:53:50 -07:00
|
|
|
rv = bind(sock, (sockaddr*)&addr, sizeof(addr));
|
|
|
|
if (rv < 0)
|
|
|
|
{
|
|
|
|
perror("bind");
|
|
|
|
return 1;
|
|
|
|
}
|
2023-01-12 09:45:49 -07:00
|
|
|
addr.sin6_port = htons(PORT_ALT);
|
|
|
|
rv = bind(sockAlt, (sockaddr*)&addr, sizeof(addr));
|
|
|
|
if (rv < 0)
|
|
|
|
{
|
|
|
|
perror("bind alt");
|
|
|
|
return 1;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-02-16 23:53:50 -07:00
|
|
|
timeval tv;
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 300000;
|
|
|
|
rv = setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
|
|
|
|
if (rv < 0)
|
|
|
|
{
|
|
|
|
perror("setsockopt SO_RCVTIMEO");
|
|
|
|
return 1;
|
|
|
|
}
|
2023-01-12 09:45:49 -07:00
|
|
|
rv = setsockopt(sockAlt, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
|
|
|
|
if (rv < 0)
|
|
|
|
{
|
|
|
|
perror("setsockopt SO_RCVTIMEO alt");
|
|
|
|
return 1;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2018-03-07 23:10:17 -07:00
|
|
|
#ifdef HAVE_LIBSYSTEMD
|
2023-01-12 09:45:49 -07:00
|
|
|
sd_notifyf(0, "READY=1\nSTATUS=Listening on port %d (alt port: %d)", PORT, PORT_ALT);
|
2018-03-07 23:10:17 -07:00
|
|
|
#endif
|
|
|
|
|
2015-02-16 23:53:50 -07:00
|
|
|
while (true)
|
|
|
|
{
|
2023-01-12 09:45:49 -07:00
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 300000;
|
|
|
|
fd_set readSet;
|
|
|
|
FD_ZERO(&readSet);
|
|
|
|
FD_SET(sock, &readSet);
|
|
|
|
FD_SET(sockAlt, &readSet);
|
|
|
|
rv = select(std::max(sock, sockAlt) + 1, &readSet, nullptr, nullptr, &tv);
|
|
|
|
if (rv < 0)
|
|
|
|
{
|
|
|
|
if (errno != EINTR && errno != EAGAIN)
|
|
|
|
{
|
|
|
|
perror("recvfrom");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int recvsock;
|
|
|
|
if (FD_ISSET(sock, &readSet))
|
|
|
|
{
|
|
|
|
recvsock = sock;
|
|
|
|
}
|
|
|
|
else if (FD_ISSET(sockAlt, &readSet))
|
|
|
|
{
|
|
|
|
recvsock = sockAlt;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ResendPackets();
|
|
|
|
continue;
|
|
|
|
}
|
2015-02-16 23:53:50 -07:00
|
|
|
sockaddr_in6 raddr;
|
|
|
|
socklen_t addrLen = sizeof(raddr);
|
2023-04-24 06:29:12 -06:00
|
|
|
Common::TraversalPacket packet{};
|
2015-02-16 23:53:50 -07:00
|
|
|
// note: switch to recvmmsg (yes, mmsg) if this becomes
|
|
|
|
// expensive
|
2023-01-12 09:45:49 -07:00
|
|
|
rv = recvfrom(recvsock, &packet, sizeof(packet), 0, (sockaddr*)&raddr, &addrLen);
|
2017-11-01 15:56:17 -06:00
|
|
|
currentTime = std::chrono::duration_cast<std::chrono::microseconds>(
|
|
|
|
std::chrono::system_clock::now().time_since_epoch())
|
|
|
|
.count();
|
2015-02-16 23:53:50 -07:00
|
|
|
if (rv < 0)
|
|
|
|
{
|
|
|
|
if (errno != EINTR && errno != EAGAIN)
|
|
|
|
{
|
|
|
|
perror("recvfrom");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ((size_t)rv < sizeof(packet))
|
|
|
|
{
|
2023-12-18 08:55:31 -07:00
|
|
|
fmt::print(stderr, "received short packet from {}\n", SenderName(&raddr));
|
2015-02-16 23:53:50 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-01-12 09:45:49 -07:00
|
|
|
HandlePacket(&packet, &raddr, recvsock == sockAlt);
|
2015-02-16 23:53:50 -07:00
|
|
|
}
|
|
|
|
ResendPackets();
|
2018-03-07 23:10:17 -07:00
|
|
|
#ifdef HAVE_LIBSYSTEMD
|
|
|
|
sd_notify(0, "WATCHDOG=1");
|
|
|
|
#endif
|
2015-02-16 23:53:50 -07:00
|
|
|
}
|
|
|
|
}
|