2019-06-06 07:02:48 +00:00
|
|
|
#include "UDPC_Defines.hpp"
|
2019-06-06 07:42:07 +00:00
|
|
|
#include "UDPConnection.h"
|
2019-06-06 07:02:48 +00:00
|
|
|
|
2019-07-25 11:51:08 +00:00
|
|
|
#include <cassert>
|
2019-07-21 05:29:31 +00:00
|
|
|
#include <chrono>
|
2019-07-25 11:51:08 +00:00
|
|
|
#include <cstring>
|
2019-07-07 05:44:37 +00:00
|
|
|
#include <optional>
|
2019-07-21 05:29:31 +00:00
|
|
|
#include <vector>
|
2019-08-27 08:00:29 +00:00
|
|
|
#include <functional>
|
2019-08-29 03:56:09 +00:00
|
|
|
#include <type_traits>
|
2019-09-07 07:36:11 +00:00
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
|
|
|
#include <ios>
|
2019-07-07 05:44:37 +00:00
|
|
|
|
2019-08-22 11:16:07 +00:00
|
|
|
UDPC::SentPktInfo::SentPktInfo() :
|
|
|
|
id(0),
|
|
|
|
sentTime(std::chrono::steady_clock::now())
|
|
|
|
{}
|
|
|
|
|
2019-09-03 03:06:46 +00:00
|
|
|
std::size_t UDPC::ConnectionIdHasher::operator()(const UDPC_ConnectionId& key) const {
|
2019-09-07 07:36:11 +00:00
|
|
|
std::string value((const char*)key.addr.s6_addr, 16);
|
|
|
|
value.push_back((char)((key.port >> 8) & 0xFF));
|
|
|
|
value.push_back((char)(key.port & 0xFF));
|
|
|
|
return std::hash<std::string>()(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t UDPC::IPV6_Hasher::operator()(const struct in6_addr& addr) const {
|
|
|
|
return std::hash<std::string>()(std::string((const char*)addr.s6_addr, 16));
|
2019-08-27 08:00:29 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 03:06:46 +00:00
|
|
|
bool operator ==(const UDPC_ConnectionId& a, const UDPC_ConnectionId& b) {
|
|
|
|
return a.addr == b.addr && a.port == b.port;
|
2019-08-27 08:00:29 +00:00
|
|
|
}
|
|
|
|
|
2019-09-07 07:36:11 +00:00
|
|
|
bool operator ==(const struct in6_addr& a, const struct in6_addr& b) {
|
|
|
|
for(unsigned int i = 0; i < 16; ++i) {
|
|
|
|
if(a.s6_addr[i] != b.s6_addr[i]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-08-18 10:14:14 +00:00
|
|
|
UDPC::ConnectionData::ConnectionData() :
|
|
|
|
flags(),
|
2019-08-29 02:07:24 +00:00
|
|
|
id(0),
|
|
|
|
lseq(0),
|
|
|
|
rseq(0),
|
|
|
|
ack(0xFFFFFFFF),
|
2019-08-27 07:31:24 +00:00
|
|
|
timer(std::chrono::steady_clock::duration::zero()),
|
2019-08-29 02:07:24 +00:00
|
|
|
toggleT(UDPC::THIRTY_SECONDS),
|
2019-08-27 07:31:24 +00:00
|
|
|
toggleTimer(std::chrono::steady_clock::duration::zero()),
|
|
|
|
toggledTimer(std::chrono::steady_clock::duration::zero()),
|
2019-08-18 10:14:14 +00:00
|
|
|
sentPkts(),
|
|
|
|
sendPkts(UDPC_QUEUED_PKTS_MAX_SIZE),
|
|
|
|
priorityPkts(UDPC_QUEUED_PKTS_MAX_SIZE),
|
2019-08-27 04:06:15 +00:00
|
|
|
receivedPkts(UDPC_RECEIVED_PKTS_MAX_SIZE),
|
2019-08-18 10:14:14 +00:00
|
|
|
received(std::chrono::steady_clock::now()),
|
2019-08-22 07:08:05 +00:00
|
|
|
sent(std::chrono::steady_clock::now()),
|
2019-08-27 07:31:24 +00:00
|
|
|
rtt(std::chrono::steady_clock::duration::zero())
|
2019-08-18 10:14:14 +00:00
|
|
|
{
|
2019-08-27 04:06:15 +00:00
|
|
|
flags.set(0);
|
2019-08-18 10:14:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UDPC::ConnectionData::ConnectionData(bool isServer, Context *ctx) :
|
|
|
|
UDPC::ConnectionData::ConnectionData()
|
|
|
|
{
|
2019-08-29 02:07:24 +00:00
|
|
|
flags.set(3);
|
2019-08-18 10:14:14 +00:00
|
|
|
if(isServer) {
|
|
|
|
id = UDPC::generateConnectionID(*ctx);
|
|
|
|
flags.set(4);
|
2019-08-29 02:07:24 +00:00
|
|
|
} else {
|
|
|
|
lseq = 1;
|
2019-08-18 10:14:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-22 11:16:07 +00:00
|
|
|
void UDPC::ConnectionData::cleanupSentPkts() {
|
|
|
|
uint32_t id;
|
|
|
|
while(sentPkts.size() > UDPC_SENT_PKTS_MAX_SIZE) {
|
2019-08-28 06:54:18 +00:00
|
|
|
id = *((uint32_t*)(sentPkts.front().data + 8));
|
|
|
|
auto iter = sentInfoMap.find(id);
|
|
|
|
assert(iter != sentInfoMap.end()
|
|
|
|
&& "Sent packet must have correspoding entry in sentInfoMap");
|
|
|
|
sentInfoMap.erase(iter);
|
2019-08-22 11:16:07 +00:00
|
|
|
sentPkts.pop_front();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-29 02:20:36 +00:00
|
|
|
UDPC::Context::Context(bool isThreaded) :
|
|
|
|
_contextIdentifier(UDPC_CONTEXT_IDENTIFIER),
|
|
|
|
flags(),
|
|
|
|
isAcceptNewConnections(true),
|
|
|
|
protocolID(UDPC_DEFAULT_PROTOCOL_ID),
|
2019-06-06 07:02:48 +00:00
|
|
|
#ifndef NDEBUG
|
2019-08-29 02:20:36 +00:00
|
|
|
loggingType(INFO),
|
2019-06-06 07:02:48 +00:00
|
|
|
#else
|
2019-08-29 02:20:36 +00:00
|
|
|
loggingType(WARNING),
|
2019-06-06 07:02:48 +00:00
|
|
|
#endif
|
2019-09-03 07:19:47 +00:00
|
|
|
atostrBufIndex(0),
|
2019-08-29 02:20:36 +00:00
|
|
|
rng_engine()
|
2019-06-06 07:02:48 +00:00
|
|
|
{
|
2019-07-21 04:45:42 +00:00
|
|
|
if(isThreaded) {
|
2019-06-06 07:02:48 +00:00
|
|
|
flags.set(0);
|
|
|
|
} else {
|
|
|
|
flags.reset(0);
|
|
|
|
}
|
2019-07-25 11:51:08 +00:00
|
|
|
|
2019-08-18 10:14:14 +00:00
|
|
|
rng_engine.seed(std::chrono::system_clock::now().time_since_epoch().count());
|
2019-06-06 07:02:48 +00:00
|
|
|
}
|
|
|
|
|
2019-08-30 03:03:26 +00:00
|
|
|
UDPC::Context *UDPC::verifyContext(UDPC_HContext ctx) {
|
2019-07-21 04:45:42 +00:00
|
|
|
if(ctx == nullptr) {
|
|
|
|
return nullptr;
|
2019-06-06 07:02:48 +00:00
|
|
|
}
|
2019-06-06 07:42:07 +00:00
|
|
|
UDPC::Context *c = (UDPC::Context *)ctx;
|
2019-07-21 04:45:42 +00:00
|
|
|
if(c->_contextIdentifier == UDPC_CONTEXT_IDENTIFIER) {
|
|
|
|
return c;
|
2019-06-06 07:02:48 +00:00
|
|
|
} else {
|
2019-07-21 04:45:42 +00:00
|
|
|
return nullptr;
|
2019-06-06 07:02:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-07 05:44:37 +00:00
|
|
|
bool UDPC::isBigEndian() {
|
2019-07-07 05:48:58 +00:00
|
|
|
static std::optional<bool> isBigEndian = std::nullopt;
|
2019-07-07 05:44:37 +00:00
|
|
|
if(isBigEndian) {
|
|
|
|
return *isBigEndian;
|
|
|
|
}
|
|
|
|
union {
|
|
|
|
uint32_t i;
|
|
|
|
char c[4];
|
2019-07-21 04:45:42 +00:00
|
|
|
} bint = {0x01020304};
|
2019-07-07 05:44:37 +00:00
|
|
|
|
|
|
|
isBigEndian = (bint.c[0] == 1);
|
|
|
|
return *isBigEndian;
|
|
|
|
}
|
|
|
|
|
2019-07-25 11:51:08 +00:00
|
|
|
void UDPC::preparePacket(char *data, uint32_t protocolID, uint32_t conID,
|
|
|
|
uint32_t rseq, uint32_t ack, uint32_t *seqID,
|
|
|
|
int flags) {
|
|
|
|
uint32_t temp;
|
|
|
|
|
|
|
|
temp = htonl(protocolID);
|
|
|
|
std::memcpy(data, &temp, 4);
|
|
|
|
temp = htonl(conID | ((flags & 0x1) != 0 ? UDPC_ID_CONNECT : 0) |
|
|
|
|
((flags & 0x2) != 0 ? UDPC_ID_PING : 0) |
|
|
|
|
((flags & 0x4) != 0 ? UDPC_ID_NO_REC_CHK : 0) |
|
|
|
|
((flags & 0x8) != 0 ? UDPC_ID_RESENDING : 0));
|
|
|
|
std::memcpy(data + 4, &temp, 4);
|
|
|
|
|
|
|
|
if(seqID) {
|
|
|
|
temp = htonl(*seqID);
|
|
|
|
++(*seqID);
|
|
|
|
} else {
|
|
|
|
temp = 0;
|
|
|
|
}
|
|
|
|
std::memcpy(data + 8, &temp, 4);
|
|
|
|
|
|
|
|
temp = htonl(rseq);
|
|
|
|
std::memcpy(data + 12, &temp, 4);
|
|
|
|
temp = htonl(ack);
|
|
|
|
std::memcpy(data + 16, &temp, 4);
|
|
|
|
}
|
|
|
|
|
2019-08-18 10:14:14 +00:00
|
|
|
uint32_t UDPC::generateConnectionID(Context &ctx) {
|
|
|
|
auto dist = std::uniform_int_distribution<uint32_t>(0, 0xFFFFFFFF);
|
|
|
|
uint32_t id = dist(ctx.rng_engine);
|
|
|
|
while(ctx.idMap.find(id) != ctx.idMap.end()) {
|
|
|
|
id = dist(ctx.rng_engine);
|
|
|
|
}
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2019-08-29 03:56:09 +00:00
|
|
|
float UDPC::durationToFSec(const std::chrono::steady_clock::duration& duration) {
|
|
|
|
return (float)duration.count()
|
|
|
|
* (float)std::decay_t<decltype(duration)>::period::num
|
|
|
|
/ (float)std::decay_t<decltype(duration)>::period::den;
|
|
|
|
}
|
|
|
|
|
|
|
|
float UDPC::timePointsToFSec(
|
2019-08-22 11:16:07 +00:00
|
|
|
const std::chrono::steady_clock::time_point& older,
|
|
|
|
const std::chrono::steady_clock::time_point& newer) {
|
|
|
|
const auto dt = newer - older;
|
|
|
|
return (float)dt.count()
|
|
|
|
* (float)decltype(dt)::period::num / (float)decltype(dt)::period::den;
|
|
|
|
}
|
|
|
|
|
2019-09-07 07:36:11 +00:00
|
|
|
UDPC_PacketInfo UDPC::get_empty_pinfo() {
|
|
|
|
return UDPC_PacketInfo {
|
|
|
|
{0}, // data (array)
|
|
|
|
0, // flags
|
|
|
|
0, // dataSize
|
|
|
|
{ // sender
|
|
|
|
{0}, // ipv6 addr
|
|
|
|
0 // port
|
|
|
|
},
|
|
|
|
{ // receiver
|
|
|
|
{0}, // ipv6 addr
|
|
|
|
0 // port
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
UDPC_ConnectionId UDPC_create_id(struct in6_addr addr, uint16_t port) {
|
2019-09-03 03:06:46 +00:00
|
|
|
return UDPC_ConnectionId{addr, port};
|
|
|
|
}
|
|
|
|
|
2019-09-07 07:36:11 +00:00
|
|
|
UDPC_ConnectionId UDPC_create_id_anyaddr(uint16_t port) {
|
|
|
|
return UDPC_ConnectionId{in6addr_any, port};
|
|
|
|
}
|
|
|
|
|
|
|
|
UDPC_HContext UDPC_init(UDPC_ConnectionId listenId, int isClient) {
|
2019-06-06 07:02:48 +00:00
|
|
|
UDPC::Context *ctx = new UDPC::Context(false);
|
2019-08-27 05:06:27 +00:00
|
|
|
ctx->flags.set(1, isClient);
|
2019-06-06 07:02:48 +00:00
|
|
|
|
2019-07-21 05:29:31 +00:00
|
|
|
// create socket
|
2019-09-07 07:36:11 +00:00
|
|
|
ctx->socketHandle = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
|
2019-07-21 05:29:31 +00:00
|
|
|
if(ctx->socketHandle <= 0) {
|
|
|
|
// TODO maybe different way of handling init fail
|
|
|
|
delete ctx;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-09-07 07:36:11 +00:00
|
|
|
// allow ipv4 connections on ipv6 socket
|
|
|
|
{
|
|
|
|
int no = 0;
|
|
|
|
setsockopt(ctx->socketHandle, IPPROTO_IPV6, IPV6_V6ONLY, &no, sizeof(no));
|
|
|
|
}
|
|
|
|
|
2019-07-21 05:29:31 +00:00
|
|
|
// bind socket
|
2019-09-07 07:36:11 +00:00
|
|
|
ctx->socketInfo.sin6_family = AF_INET6;
|
|
|
|
ctx->socketInfo.sin6_addr = listenId.addr;
|
|
|
|
ctx->socketInfo.sin6_port = htons(listenId.port);
|
2019-07-21 05:29:31 +00:00
|
|
|
if(bind(ctx->socketHandle, (const struct sockaddr *)&ctx->socketInfo,
|
2019-09-07 07:36:11 +00:00
|
|
|
sizeof(struct sockaddr_in6)) < 0) {
|
2019-07-21 05:29:31 +00:00
|
|
|
// TODO maybe different way of handling init fail
|
|
|
|
CleanupSocket(ctx->socketHandle);
|
|
|
|
delete ctx;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-07-25 11:51:08 +00:00
|
|
|
// TODO verify this is necessary to get the listen port
|
2019-09-07 07:36:11 +00:00
|
|
|
if(ctx->socketInfo.sin6_port == 0) {
|
|
|
|
struct sockaddr_in6 getInfo;
|
|
|
|
socklen_t size = sizeof(struct sockaddr_in6);
|
2019-07-25 11:51:08 +00:00
|
|
|
if(getsockname(ctx->socketHandle, (struct sockaddr *)&getInfo, &size) == 0) {
|
2019-09-07 07:36:11 +00:00
|
|
|
ctx->socketInfo.sin6_port = getInfo.sin6_port;
|
2019-07-25 11:51:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-21 05:29:31 +00:00
|
|
|
// set non-blocking on socket
|
|
|
|
#if UDPC_PLATFORM == UDPC_PLATFORM_MAC || UDPC_PLATFORM == UDPC_PLATFORM_LINUX
|
|
|
|
int nonblocking = 1;
|
|
|
|
if(fcntl(ctx->socketHandle, F_SETFL, O_NONBLOCK, nonblocking) == -1) {
|
|
|
|
#elif UDPC_PLATFORM == UDPC_PLATFORM_WINDOWS
|
|
|
|
DWORD nonblocking = 1;
|
|
|
|
if(ioctlsocket(ctx->socketHandle, FIONBIO, &nonblocking) != 0) {
|
|
|
|
#else
|
|
|
|
{
|
|
|
|
#endif
|
|
|
|
// TODO maybe different way of handling init fail
|
|
|
|
CleanupSocket(ctx->socketHandle);
|
|
|
|
delete ctx;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-08-30 03:03:26 +00:00
|
|
|
return (UDPC_HContext) ctx;
|
2019-06-06 07:02:48 +00:00
|
|
|
}
|
|
|
|
|
2019-09-07 07:36:11 +00:00
|
|
|
UDPC_HContext UDPC_init_threaded_update(UDPC_ConnectionId listenId,
|
2019-06-06 07:42:07 +00:00
|
|
|
int isClient) {
|
2019-09-07 07:36:11 +00:00
|
|
|
UDPC::Context *ctx = (UDPC::Context *)UDPC_init(listenId, isClient);
|
2019-07-21 05:29:31 +00:00
|
|
|
if(!ctx) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
ctx->flags.set(0);
|
2019-06-06 07:02:48 +00:00
|
|
|
|
2019-08-30 03:03:26 +00:00
|
|
|
return (UDPC_HContext) ctx;
|
2019-06-06 07:02:48 +00:00
|
|
|
}
|
|
|
|
|
2019-08-30 03:03:26 +00:00
|
|
|
void UDPC_destroy(UDPC_HContext ctx) {
|
2019-07-21 04:45:42 +00:00
|
|
|
UDPC::Context *UDPC_ctx = UDPC::verifyContext(ctx);
|
|
|
|
if(UDPC_ctx) {
|
|
|
|
delete UDPC_ctx;
|
2019-06-06 07:02:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-30 03:03:26 +00:00
|
|
|
void UDPC_update(UDPC_HContext ctx) {
|
2019-07-21 04:45:42 +00:00
|
|
|
UDPC::Context *c = UDPC::verifyContext(ctx);
|
2019-07-21 05:29:31 +00:00
|
|
|
if(!c || c->flags.test(0)) {
|
|
|
|
// invalid or is threaded, update should not be called
|
2019-06-06 07:02:48 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-07-21 05:29:31 +00:00
|
|
|
|
|
|
|
const auto now = std::chrono::steady_clock::now();
|
2019-08-22 11:16:07 +00:00
|
|
|
c->lastUpdated = now;
|
2019-07-21 05:29:31 +00:00
|
|
|
|
2019-08-27 07:31:24 +00:00
|
|
|
std::chrono::steady_clock::duration temp_dt_fs;
|
2019-07-25 11:51:08 +00:00
|
|
|
{
|
|
|
|
// check timed out, check good/bad mode with rtt, remove timed out
|
2019-09-03 03:06:46 +00:00
|
|
|
std::vector<UDPC_ConnectionId> removed;
|
2019-07-25 11:51:08 +00:00
|
|
|
for(auto iter = c->conMap.begin(); iter != c->conMap.end(); ++iter) {
|
2019-08-27 07:31:24 +00:00
|
|
|
temp_dt_fs = now - iter->second.received;
|
|
|
|
if(temp_dt_fs >= UDPC::CONNECTION_TIMEOUT) {
|
2019-07-25 11:51:08 +00:00
|
|
|
removed.push_back(iter->first);
|
2019-08-29 03:56:09 +00:00
|
|
|
c->log(
|
|
|
|
UDPC_LoggingType::VERBOSE,
|
|
|
|
"Timed out connection with ",
|
2019-09-07 07:36:11 +00:00
|
|
|
UDPC_atostr(ctx, iter->first),
|
2019-08-29 03:56:09 +00:00
|
|
|
":",
|
|
|
|
iter->second.port);
|
2019-07-25 11:51:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check good/bad mode
|
|
|
|
iter->second.toggleTimer += temp_dt_fs;
|
|
|
|
iter->second.toggledTimer += temp_dt_fs;
|
|
|
|
if(iter->second.flags.test(1) && !iter->second.flags.test(2)) {
|
|
|
|
// good mode, bad rtt
|
2019-08-29 03:56:09 +00:00
|
|
|
c->log(
|
|
|
|
UDPC_LoggingType::INFO,
|
|
|
|
"Switching to bad mode in connection with ",
|
2019-09-07 07:36:11 +00:00
|
|
|
UDPC_atostr(ctx, iter->first),
|
2019-08-29 03:56:09 +00:00
|
|
|
":",
|
|
|
|
iter->second.port);
|
2019-07-25 11:51:08 +00:00
|
|
|
iter->second.flags.reset(1);
|
2019-08-27 07:31:24 +00:00
|
|
|
if(iter->second.toggledTimer <= UDPC::TEN_SECONDS) {
|
|
|
|
iter->second.toggleT *= 2;
|
2019-07-25 11:51:08 +00:00
|
|
|
}
|
2019-08-27 07:31:24 +00:00
|
|
|
iter->second.toggledTimer = std::chrono::steady_clock::duration::zero();
|
2019-07-25 11:51:08 +00:00
|
|
|
} else if(iter->second.flags.test(1)) {
|
|
|
|
// good mode, good rtt
|
2019-08-27 07:31:24 +00:00
|
|
|
if(iter->second.toggleTimer >= UDPC::TEN_SECONDS) {
|
|
|
|
iter->second.toggleTimer = std::chrono::steady_clock::duration::zero();
|
|
|
|
iter->second.toggleT /= 2;
|
|
|
|
if(iter->second.toggleT < UDPC::ONE_SECOND) {
|
|
|
|
iter->second.toggleT = UDPC::ONE_SECOND;
|
2019-07-25 11:51:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if(!iter->second.flags.test(1) &&
|
|
|
|
iter->second.flags.test(2)) {
|
|
|
|
// bad mode, good rtt
|
|
|
|
if(iter->second.toggledTimer >= iter->second.toggleT) {
|
2019-08-27 07:31:24 +00:00
|
|
|
iter->second.toggleTimer = std::chrono::steady_clock::duration::zero();
|
|
|
|
iter->second.toggledTimer = std::chrono::steady_clock::duration::zero();
|
2019-08-29 03:56:09 +00:00
|
|
|
c->log(
|
|
|
|
UDPC_LoggingType::INFO,
|
|
|
|
"Switching to good mode in connection with ",
|
2019-09-07 07:36:11 +00:00
|
|
|
UDPC_atostr(ctx, iter->first),
|
2019-08-29 03:56:09 +00:00
|
|
|
":",
|
|
|
|
iter->second.port);
|
2019-07-25 11:51:08 +00:00
|
|
|
iter->second.flags.set(1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// bad mode, bad rtt
|
2019-08-27 07:31:24 +00:00
|
|
|
iter->second.toggledTimer = std::chrono::steady_clock::duration::zero();
|
2019-07-25 11:51:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
iter->second.timer += temp_dt_fs;
|
|
|
|
if(iter->second.timer >= (iter->second.flags.test(1)
|
2019-08-27 07:31:24 +00:00
|
|
|
? UDPC::GOOD_MODE_SEND_RATE
|
|
|
|
: UDPC::BAD_MODE_SEND_RATE)) {
|
2019-08-27 08:53:18 +00:00
|
|
|
iter->second.timer -= (iter->second.flags.test(1)
|
|
|
|
? UDPC::GOOD_MODE_SEND_RATE : UDPC::BAD_MODE_SEND_RATE);
|
2019-07-25 11:51:08 +00:00
|
|
|
iter->second.flags.set(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(auto iter = removed.begin(); iter != removed.end(); ++iter) {
|
2019-09-03 03:06:46 +00:00
|
|
|
auto addrConIter = c->addrConMap.find(iter->addr);
|
2019-08-28 07:38:14 +00:00
|
|
|
assert(addrConIter != c->addrConMap.end()
|
|
|
|
&& "addrConMap must have an entry for a current connection");
|
|
|
|
auto addrConSetIter = addrConIter->second.find(*iter);
|
|
|
|
assert(addrConSetIter != addrConIter->second.end()
|
|
|
|
&& "nested set in addrConMap must have an entry for a current connection");
|
|
|
|
addrConIter->second.erase(addrConSetIter);
|
|
|
|
if(addrConIter->second.empty()) {
|
|
|
|
c->addrConMap.erase(addrConIter);
|
|
|
|
}
|
|
|
|
|
2019-07-25 11:51:08 +00:00
|
|
|
auto cIter = c->conMap.find(*iter);
|
2019-08-28 07:38:14 +00:00
|
|
|
assert(cIter != c->conMap.end()
|
|
|
|
&& "conMap must have the entry set to be removed");
|
|
|
|
|
2019-07-25 11:51:08 +00:00
|
|
|
if(cIter->second.flags.test(4)) {
|
|
|
|
c->idMap.erase(cIter->second.id);
|
|
|
|
}
|
2019-08-28 07:38:14 +00:00
|
|
|
|
2019-07-25 11:51:08 +00:00
|
|
|
c->conMap.erase(cIter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update send (only if triggerSend flag is set)
|
2019-07-21 05:29:31 +00:00
|
|
|
for(auto iter = c->conMap.begin(); iter != c->conMap.end(); ++iter) {
|
2019-07-25 11:51:08 +00:00
|
|
|
if(!iter->second.flags.test(0)) {
|
|
|
|
continue;
|
2019-07-21 05:29:31 +00:00
|
|
|
}
|
2019-07-25 11:51:08 +00:00
|
|
|
iter->second.flags.reset(0);
|
|
|
|
|
|
|
|
if(iter->second.flags.test(3)) {
|
|
|
|
if(c->flags.test(1)) {
|
|
|
|
// is initiating connection to server
|
|
|
|
auto initDT = now - iter->second.sent;
|
|
|
|
if(initDT < UDPC::INIT_PKT_INTERVAL_DT) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
iter->second.sent = now;
|
|
|
|
|
|
|
|
std::unique_ptr<char[]> buf = std::make_unique<char[]>(20);
|
|
|
|
UDPC::preparePacket(
|
|
|
|
buf.get(),
|
|
|
|
c->protocolID,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0xFFFFFFFF,
|
|
|
|
nullptr,
|
|
|
|
0x1);
|
|
|
|
|
2019-09-07 07:36:11 +00:00
|
|
|
struct sockaddr_in6 destinationInfo;
|
|
|
|
destinationInfo.sin6_family = AF_INET6;
|
|
|
|
std::memcpy(destinationInfo.sin6_addr.s6_addr, iter->first.addr.s6_addr, 16);
|
|
|
|
destinationInfo.sin6_port = htons(iter->second.port);
|
2019-07-25 11:51:08 +00:00
|
|
|
long int sentBytes = sendto(
|
|
|
|
c->socketHandle,
|
|
|
|
buf.get(),
|
|
|
|
20,
|
|
|
|
0,
|
|
|
|
(struct sockaddr*) &destinationInfo,
|
2019-09-07 07:36:11 +00:00
|
|
|
sizeof(struct sockaddr_in6));
|
2019-07-25 11:51:08 +00:00
|
|
|
if(sentBytes != 20) {
|
2019-08-29 03:56:09 +00:00
|
|
|
c->log(
|
|
|
|
UDPC_LoggingType::ERROR,
|
|
|
|
"Failed to send packet to initiate connection to ",
|
2019-09-07 07:36:11 +00:00
|
|
|
UDPC_atostr(ctx, iter->first),
|
2019-08-29 03:56:09 +00:00
|
|
|
":",
|
|
|
|
iter->second.port);
|
2019-08-28 06:54:18 +00:00
|
|
|
continue;
|
2019-07-25 11:51:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// is server, initiate connection to client
|
|
|
|
iter->second.flags.reset(3);
|
|
|
|
iter->second.sent = now;
|
|
|
|
|
|
|
|
std::unique_ptr<char[]> buf = std::make_unique<char[]>(20);
|
|
|
|
UDPC::preparePacket(
|
|
|
|
buf.get(),
|
|
|
|
c->protocolID,
|
|
|
|
iter->second.id,
|
|
|
|
iter->second.rseq,
|
|
|
|
iter->second.ack,
|
|
|
|
&iter->second.lseq,
|
|
|
|
0x1);
|
|
|
|
|
2019-09-07 07:36:11 +00:00
|
|
|
struct sockaddr_in6 destinationInfo;
|
|
|
|
destinationInfo.sin6_family = AF_INET6;
|
|
|
|
std::memcpy(destinationInfo.sin6_addr.s6_addr, iter->first.addr.s6_addr, 16);
|
|
|
|
destinationInfo.sin6_port = htons(iter->second.port);
|
2019-07-25 11:51:08 +00:00
|
|
|
long int sentBytes = sendto(
|
|
|
|
c->socketHandle,
|
|
|
|
buf.get(),
|
|
|
|
20,
|
|
|
|
0,
|
|
|
|
(struct sockaddr*) &destinationInfo,
|
2019-09-07 07:36:11 +00:00
|
|
|
sizeof(struct sockaddr_in6));
|
2019-07-25 11:51:08 +00:00
|
|
|
if(sentBytes != 20) {
|
2019-08-29 03:56:09 +00:00
|
|
|
c->log(
|
|
|
|
UDPC_LoggingType::ERROR,
|
|
|
|
"Failed to send packet to initiate connection to ",
|
2019-09-07 07:36:11 +00:00
|
|
|
UDPC_atostr(ctx, iter->first),
|
2019-08-29 03:56:09 +00:00
|
|
|
":",
|
|
|
|
iter->second.port);
|
2019-08-28 06:54:18 +00:00
|
|
|
continue;
|
2019-07-25 11:51:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not initiating connection, send as normal on current connection
|
|
|
|
if(iter->second.sendPkts.empty() && iter->second.priorityPkts.empty()) {
|
|
|
|
// nothing in queues, send heartbeat packet
|
|
|
|
auto sentDT = now - iter->second.sent;
|
|
|
|
if(sentDT < UDPC::HEARTBEAT_PKT_INTERVAL_DT) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<char[]> buf = std::make_unique<char[]>(20);
|
|
|
|
UDPC::preparePacket(
|
|
|
|
buf.get(),
|
|
|
|
c->protocolID,
|
|
|
|
iter->second.id,
|
|
|
|
iter->second.rseq,
|
|
|
|
iter->second.ack,
|
|
|
|
&iter->second.lseq,
|
|
|
|
0);
|
|
|
|
|
2019-09-07 07:36:11 +00:00
|
|
|
struct sockaddr_in6 destinationInfo;
|
|
|
|
destinationInfo.sin6_family = AF_INET6;
|
|
|
|
std::memcpy(destinationInfo.sin6_addr.s6_addr, iter->first.addr.s6_addr, 16);
|
|
|
|
destinationInfo.sin6_port = htons(iter->second.port);
|
2019-07-25 11:51:08 +00:00
|
|
|
long int sentBytes = sendto(
|
|
|
|
c->socketHandle,
|
|
|
|
buf.get(),
|
|
|
|
20,
|
|
|
|
0,
|
|
|
|
(struct sockaddr*) &destinationInfo,
|
2019-09-07 07:36:11 +00:00
|
|
|
sizeof(struct sockaddr_in6));
|
2019-07-25 11:51:08 +00:00
|
|
|
if(sentBytes != 20) {
|
2019-08-29 03:56:09 +00:00
|
|
|
c->log(
|
|
|
|
UDPC_LoggingType::ERROR,
|
|
|
|
"Failed to send heartbeat packet to ",
|
2019-09-07 07:36:11 +00:00
|
|
|
UDPC_atostr(ctx, iter->first),
|
2019-08-29 03:56:09 +00:00
|
|
|
":",
|
|
|
|
iter->second.port);
|
2019-08-28 06:54:18 +00:00
|
|
|
continue;
|
2019-07-25 11:51:08 +00:00
|
|
|
}
|
|
|
|
|
2019-09-07 07:36:11 +00:00
|
|
|
UDPC_PacketInfo pInfo = UDPC::get_empty_pinfo();
|
|
|
|
pInfo.sender.addr = in6addr_loopback;
|
2019-09-03 03:06:46 +00:00
|
|
|
pInfo.receiver.addr = iter->first.addr;
|
2019-09-07 07:36:11 +00:00
|
|
|
pInfo.sender.port = c->socketInfo.sin6_port;
|
2019-09-03 03:06:46 +00:00
|
|
|
pInfo.receiver.port = iter->second.port;
|
2019-08-28 06:54:18 +00:00
|
|
|
*((uint32_t*)(pInfo.data + 8)) = iter->second.lseq - 1;
|
2019-07-21 05:29:31 +00:00
|
|
|
|
2019-07-25 11:51:08 +00:00
|
|
|
iter->second.sentPkts.push_back(std::move(pInfo));
|
2019-08-22 11:16:07 +00:00
|
|
|
iter->second.cleanupSentPkts();
|
|
|
|
|
|
|
|
// store other pkt info
|
|
|
|
UDPC::SentPktInfo::Ptr sentPktInfo = std::make_shared<UDPC::SentPktInfo>();
|
|
|
|
sentPktInfo->id = iter->second.lseq - 1;
|
|
|
|
iter->second.sentInfoMap.insert(std::make_pair(sentPktInfo->id, sentPktInfo));
|
2019-07-25 11:51:08 +00:00
|
|
|
} else {
|
|
|
|
// sendPkts or priorityPkts not empty
|
2019-09-07 07:36:11 +00:00
|
|
|
UDPC_PacketInfo pInfo = UDPC::get_empty_pinfo();
|
2019-07-25 11:51:08 +00:00
|
|
|
bool isResending = false;
|
|
|
|
if(!iter->second.priorityPkts.empty()) {
|
|
|
|
// TODO verify getting struct copy is valid
|
2019-09-03 06:15:09 +00:00
|
|
|
pInfo = std::move(iter->second.priorityPkts.top().value());
|
2019-07-25 11:51:08 +00:00
|
|
|
iter->second.priorityPkts.pop();
|
|
|
|
isResending = true;
|
|
|
|
} else {
|
2019-09-03 06:15:09 +00:00
|
|
|
pInfo = std::move(iter->second.sendPkts.top().value());
|
2019-07-25 11:51:08 +00:00
|
|
|
iter->second.sendPkts.pop();
|
|
|
|
}
|
|
|
|
std::unique_ptr<char[]> buf = std::make_unique<char[]>(20 + pInfo.dataSize);
|
2019-08-18 09:34:45 +00:00
|
|
|
UDPC::preparePacket(
|
|
|
|
buf.get(),
|
|
|
|
c->protocolID,
|
|
|
|
iter->second.id,
|
|
|
|
iter->second.rseq,
|
|
|
|
iter->second.ack,
|
|
|
|
&iter->second.lseq,
|
2019-08-27 05:06:27 +00:00
|
|
|
(pInfo.flags & 0x4) | (isResending ? 0x8 : 0));
|
2019-08-18 09:34:45 +00:00
|
|
|
std::memcpy(buf.get() + 20, pInfo.data, pInfo.dataSize);
|
|
|
|
|
2019-09-07 07:36:11 +00:00
|
|
|
struct sockaddr_in6 destinationInfo;
|
|
|
|
destinationInfo.sin6_family = AF_INET6;
|
|
|
|
std::memcpy(destinationInfo.sin6_addr.s6_addr, iter->first.addr.s6_addr, 16);
|
|
|
|
destinationInfo.sin6_port = htons(iter->second.port);
|
2019-08-18 09:34:45 +00:00
|
|
|
long int sentBytes = sendto(
|
|
|
|
c->socketHandle,
|
|
|
|
buf.get(),
|
|
|
|
pInfo.dataSize + 20,
|
|
|
|
0,
|
|
|
|
(struct sockaddr*) &destinationInfo,
|
2019-09-07 07:36:11 +00:00
|
|
|
sizeof(struct sockaddr_in6));
|
2019-08-18 09:34:45 +00:00
|
|
|
if(sentBytes != 20 + pInfo.dataSize) {
|
2019-08-29 03:56:09 +00:00
|
|
|
c->log(
|
|
|
|
UDPC_LoggingType::ERROR,
|
|
|
|
"Failed to send packet to ",
|
2019-09-07 07:36:11 +00:00
|
|
|
UDPC_atostr(ctx, iter->first),
|
2019-08-29 03:56:09 +00:00
|
|
|
":",
|
|
|
|
iter->second.port);
|
2019-08-28 06:54:18 +00:00
|
|
|
continue;
|
2019-08-18 09:34:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if((pInfo.flags & 0x4) == 0) {
|
|
|
|
// is check-received, store data in case packet gets lost
|
2019-09-07 07:36:11 +00:00
|
|
|
UDPC_PacketInfo sentPInfo = UDPC::get_empty_pinfo();
|
2019-08-18 09:34:45 +00:00
|
|
|
std::memcpy(sentPInfo.data, buf.get(), 20 + pInfo.dataSize);
|
|
|
|
sentPInfo.flags = 0;
|
|
|
|
sentPInfo.dataSize = 20 + pInfo.dataSize;
|
2019-09-07 07:36:11 +00:00
|
|
|
sentPInfo.sender.addr = in6addr_loopback;
|
2019-09-03 03:06:46 +00:00
|
|
|
sentPInfo.receiver.addr = iter->first.addr;
|
2019-09-07 07:36:11 +00:00
|
|
|
sentPInfo.sender.port = c->socketInfo.sin6_port;
|
2019-09-03 03:06:46 +00:00
|
|
|
sentPInfo.receiver.port = iter->second.port;
|
2019-08-18 09:34:45 +00:00
|
|
|
|
|
|
|
iter->second.sentPkts.push_back(std::move(pInfo));
|
2019-08-22 11:16:07 +00:00
|
|
|
iter->second.cleanupSentPkts();
|
2019-08-18 09:34:45 +00:00
|
|
|
} else {
|
2019-08-22 11:16:07 +00:00
|
|
|
// is not check-received, only id stored in data array
|
2019-09-07 07:36:11 +00:00
|
|
|
UDPC_PacketInfo sentPInfo = UDPC::get_empty_pinfo();
|
2019-08-18 09:34:45 +00:00
|
|
|
sentPInfo.flags = 0x4;
|
|
|
|
sentPInfo.dataSize = 0;
|
2019-09-07 07:36:11 +00:00
|
|
|
sentPInfo.sender.addr = in6addr_loopback;
|
2019-09-03 03:06:46 +00:00
|
|
|
sentPInfo.receiver.addr = iter->first.addr;
|
2019-09-07 07:36:11 +00:00
|
|
|
sentPInfo.sender.port = c->socketInfo.sin6_port;
|
2019-09-03 03:06:46 +00:00
|
|
|
sentPInfo.receiver.port = iter->second.port;
|
2019-08-28 06:54:18 +00:00
|
|
|
*((uint32_t*)(sentPInfo.data + 8)) = iter->second.lseq - 1;
|
2019-08-18 09:34:45 +00:00
|
|
|
|
|
|
|
iter->second.sentPkts.push_back(std::move(pInfo));
|
2019-08-22 11:16:07 +00:00
|
|
|
iter->second.cleanupSentPkts();
|
2019-08-18 09:34:45 +00:00
|
|
|
}
|
2019-08-22 11:16:07 +00:00
|
|
|
|
|
|
|
// store other pkt info
|
|
|
|
UDPC::SentPktInfo::Ptr sentPktInfo = std::make_shared<UDPC::SentPktInfo>();
|
|
|
|
sentPktInfo->id = iter->second.lseq - 1;
|
|
|
|
iter->second.sentInfoMap.insert(std::make_pair(sentPktInfo->id, sentPktInfo));
|
2019-07-21 05:29:31 +00:00
|
|
|
}
|
2019-06-06 07:02:48 +00:00
|
|
|
}
|
|
|
|
|
2019-08-18 10:14:14 +00:00
|
|
|
// receive packet
|
|
|
|
#if UDPC_PLATFORM == UDPC_PLATFORM_WINDOWS
|
|
|
|
typedef int socklen_t;
|
|
|
|
#endif
|
2019-09-07 07:36:11 +00:00
|
|
|
struct sockaddr_in6 receivedData;
|
2019-08-18 10:14:14 +00:00
|
|
|
socklen_t receivedDataSize = sizeof(receivedData);
|
|
|
|
int bytes = recvfrom(
|
|
|
|
c->socketHandle,
|
|
|
|
c->recvBuf,
|
|
|
|
UDPC_PACKET_MAX_SIZE,
|
|
|
|
0,
|
|
|
|
(struct sockaddr*) &receivedData,
|
|
|
|
&receivedDataSize);
|
|
|
|
|
|
|
|
if(bytes == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
|
|
|
|
// no packet was received
|
|
|
|
return;
|
|
|
|
} else if(bytes < 20) {
|
|
|
|
// packet size is too small, invalid packet
|
2019-08-29 03:56:09 +00:00
|
|
|
c->log(
|
|
|
|
UDPC_LoggingType::INFO,
|
|
|
|
"Received packet is smaller than header, ignoring packet from ",
|
2019-09-07 07:36:11 +00:00
|
|
|
UDPC_atostr(ctx, UDPC_ConnectionId{receivedData.sin6_addr, 0}),
|
2019-08-29 03:56:09 +00:00
|
|
|
":",
|
2019-09-07 07:36:11 +00:00
|
|
|
receivedData.sin6_port);
|
2019-08-18 10:14:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t temp = ntohl(*((uint32_t*)c->recvBuf));
|
|
|
|
if(temp != c->protocolID) {
|
|
|
|
// Invalid protocol id in packet
|
2019-08-29 03:56:09 +00:00
|
|
|
c->log(
|
|
|
|
UDPC_LoggingType::INFO,
|
|
|
|
"Received packet has invalid protocol id, ignoring packet from ",
|
2019-09-07 07:36:11 +00:00
|
|
|
UDPC_atostr(ctx, UDPC_ConnectionId{receivedData.sin6_addr, 0}),
|
2019-08-29 03:56:09 +00:00
|
|
|
":",
|
2019-09-07 07:36:11 +00:00
|
|
|
receivedData.sin6_port);
|
2019-08-18 10:14:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t conID = ntohl(*((uint32_t*)(c->recvBuf + 4)));
|
|
|
|
uint32_t seqID = ntohl(*((uint32_t*)(c->recvBuf + 8)));
|
|
|
|
uint32_t rseq = ntohl(*((uint32_t*)(c->recvBuf + 12)));
|
|
|
|
uint32_t ack = htonl(*((uint32_t*)(c->recvBuf + 16)));
|
|
|
|
|
|
|
|
bool isConnect = conID & UDPC_ID_CONNECT;
|
|
|
|
bool isPing = conID & UDPC_ID_PING;
|
|
|
|
bool isNotRecChecked = conID & UDPC_ID_NO_REC_CHK;
|
|
|
|
bool isResending = conID & UDPC_ID_RESENDING;
|
|
|
|
conID &= 0x0FFFFFFF;
|
|
|
|
|
2019-09-07 07:36:11 +00:00
|
|
|
UDPC_ConnectionId identifier{receivedData.sin6_addr, ntohs(receivedData.sin6_port)};
|
2019-08-27 08:00:29 +00:00
|
|
|
|
2019-08-18 10:14:14 +00:00
|
|
|
if(isConnect && c->flags.test(2)) {
|
|
|
|
// is connect packet and is accepting new connections
|
|
|
|
if(!c->flags.test(1)
|
2019-08-27 08:00:29 +00:00
|
|
|
&& c->conMap.find(identifier) == c->conMap.end()) {
|
2019-08-18 10:14:14 +00:00
|
|
|
// is receiving as server, connection did not already exist
|
2019-08-29 03:56:09 +00:00
|
|
|
c->log(
|
|
|
|
UDPC_LoggingType::VERBOSE,
|
|
|
|
"Establishing connection with client ",
|
2019-09-07 07:36:11 +00:00
|
|
|
UDPC_atostr(ctx, UDPC_ConnectionId{receivedData.sin6_addr, 0}),
|
2019-08-29 03:56:09 +00:00
|
|
|
":",
|
2019-09-07 07:36:11 +00:00
|
|
|
receivedData.sin6_port);
|
2019-08-18 10:14:14 +00:00
|
|
|
UDPC::ConnectionData newConnection(true, c);
|
2019-09-07 07:36:11 +00:00
|
|
|
newConnection.addr = receivedData.sin6_addr;
|
|
|
|
newConnection.port = ntohs(receivedData.sin6_port);
|
2019-08-22 07:08:05 +00:00
|
|
|
|
2019-08-27 08:00:29 +00:00
|
|
|
c->idMap.insert(std::make_pair(newConnection.id, identifier));
|
|
|
|
c->conMap.insert(std::make_pair(identifier, std::move(newConnection)));
|
2019-09-03 03:06:46 +00:00
|
|
|
auto addrConIter = c->addrConMap.find(identifier.addr);
|
2019-08-28 07:38:14 +00:00
|
|
|
if(addrConIter == c->addrConMap.end()) {
|
|
|
|
auto insertResult = c->addrConMap.insert(
|
|
|
|
std::make_pair(
|
2019-09-03 03:06:46 +00:00
|
|
|
identifier.addr,
|
|
|
|
std::unordered_set<UDPC_ConnectionId, UDPC::ConnectionIdHasher>{}
|
2019-08-28 07:38:14 +00:00
|
|
|
));
|
|
|
|
assert(insertResult.second
|
|
|
|
&& "Must successfully insert into addrConMap");
|
|
|
|
addrConIter = insertResult.first;
|
|
|
|
}
|
|
|
|
addrConIter->second.insert(identifier);
|
2019-08-22 07:08:05 +00:00
|
|
|
// TODO trigger event server established connection with client
|
|
|
|
} else if (c->flags.test(1)) {
|
|
|
|
// is client
|
2019-08-27 08:00:29 +00:00
|
|
|
auto iter = c->conMap.find(identifier);
|
2019-08-22 07:08:05 +00:00
|
|
|
if(iter == c->conMap.end() || !iter->second.flags.test(3)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
iter->second.flags.reset(3);
|
|
|
|
iter->second.id = conID;
|
|
|
|
iter->second.flags.set(4);
|
2019-08-29 03:56:09 +00:00
|
|
|
c->log(
|
|
|
|
UDPC_LoggingType::VERBOSE,
|
|
|
|
"Established connection with server ",
|
2019-09-07 07:36:11 +00:00
|
|
|
UDPC_atostr(ctx, UDPC_ConnectionId{receivedData.sin6_addr, 0}),
|
2019-08-29 03:56:09 +00:00
|
|
|
":",
|
2019-09-07 07:36:11 +00:00
|
|
|
receivedData.sin6_port);
|
2019-08-22 07:08:05 +00:00
|
|
|
// TODO trigger event client established connection with server
|
2019-08-18 10:14:14 +00:00
|
|
|
}
|
2019-08-22 07:08:05 +00:00
|
|
|
return;
|
2019-08-18 10:14:14 +00:00
|
|
|
}
|
2019-08-18 09:34:45 +00:00
|
|
|
|
2019-08-27 08:00:29 +00:00
|
|
|
auto iter = c->conMap.find(identifier);
|
2019-08-22 07:08:05 +00:00
|
|
|
if(iter == c->conMap.end() || iter->second.flags.test(3)
|
|
|
|
|| !iter->second.flags.test(4) || iter->second.id != conID) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if(isPing) {
|
|
|
|
iter->second.flags.set(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// packet is valid
|
2019-08-29 03:56:09 +00:00
|
|
|
c->log(
|
|
|
|
UDPC_LoggingType::INFO,
|
|
|
|
"Received valid packet from ",
|
2019-09-07 07:36:11 +00:00
|
|
|
UDPC_atostr(ctx, UDPC_ConnectionId{receivedData.sin6_addr, 0}),
|
2019-08-29 03:56:09 +00:00
|
|
|
":",
|
2019-09-07 07:36:11 +00:00
|
|
|
receivedData.sin6_port);
|
2019-08-22 07:08:05 +00:00
|
|
|
|
2019-08-22 11:16:07 +00:00
|
|
|
// update rtt
|
|
|
|
for(auto sentIter = iter->second.sentPkts.rbegin(); sentIter != iter->second.sentPkts.rend(); ++sentIter) {
|
2019-08-27 03:42:43 +00:00
|
|
|
uint32_t id = ntohl(*((uint32_t*)(sentIter->data + 8)));
|
2019-08-22 11:16:07 +00:00
|
|
|
if(id == rseq) {
|
|
|
|
auto sentInfoIter = iter->second.sentInfoMap.find(id);
|
|
|
|
assert(sentInfoIter != iter->second.sentInfoMap.end()
|
|
|
|
&& "sentInfoMap should have known stored id");
|
2019-08-27 07:31:24 +00:00
|
|
|
auto diff = now - sentInfoIter->second->sentTime;
|
2019-08-22 11:16:07 +00:00
|
|
|
if(diff > iter->second.rtt) {
|
2019-08-27 07:31:24 +00:00
|
|
|
iter->second.rtt += (diff - iter->second.rtt) / 10;
|
2019-08-22 11:16:07 +00:00
|
|
|
} else {
|
2019-08-27 07:31:24 +00:00
|
|
|
iter->second.rtt -= (iter->second.rtt - diff) / 10;
|
2019-08-22 11:16:07 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 07:31:24 +00:00
|
|
|
iter->second.flags.set(2, iter->second.rtt <= UDPC::GOOD_RTT_LIMIT);
|
2019-08-22 11:16:07 +00:00
|
|
|
|
2019-08-29 03:56:09 +00:00
|
|
|
c->log(
|
|
|
|
UDPC_LoggingType::INFO,
|
|
|
|
"RTT: ",
|
|
|
|
UDPC::durationToFSec(iter->second.rtt));
|
2019-08-22 11:16:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
iter->second.received = now;
|
2019-08-27 03:42:43 +00:00
|
|
|
|
|
|
|
// check pkt timeout
|
|
|
|
--rseq;
|
|
|
|
for(; ack != 0; ack = ack << 1) {
|
|
|
|
if((ack & 0x80000000) != 0) {
|
|
|
|
--rseq;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// pkt not received yet, find it in sent to check if it timed out
|
|
|
|
for(auto sentIter = iter->second.sentPkts.rbegin(); sentIter != iter->second.sentPkts.rend(); ++sentIter) {
|
|
|
|
uint32_t sentID = ntohl(*((uint32_t*)(sentIter->data + 8)));
|
|
|
|
if(sentID == rseq) {
|
|
|
|
if((sentIter->flags & 0x4) != 0 || (sentIter->flags & 0x8) != 0) {
|
|
|
|
// already resent or not rec-checked pkt
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
auto sentInfoIter = iter->second.sentInfoMap.find(sentID);
|
|
|
|
assert(sentInfoIter != iter->second.sentInfoMap.end()
|
|
|
|
&& "Every entry in sentPkts must have a corresponding entry in sentInfoMap");
|
2019-08-27 07:31:24 +00:00
|
|
|
auto duration = now - sentInfoIter->second->sentTime;
|
|
|
|
if(duration > UDPC::PACKET_TIMEOUT_TIME) {
|
2019-08-27 03:42:43 +00:00
|
|
|
if(sentIter->dataSize <= 20) {
|
2019-08-29 03:56:09 +00:00
|
|
|
c->log(
|
|
|
|
UDPC_LoggingType::INFO,
|
|
|
|
"Timed out packet has no payload (probably "
|
|
|
|
"heartbeat packet), ignoring it");
|
2019-08-27 03:42:43 +00:00
|
|
|
sentIter->flags |= 0x8;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-09-07 07:36:11 +00:00
|
|
|
UDPC_PacketInfo resendingData = UDPC::get_empty_pinfo();
|
2019-08-27 03:42:43 +00:00
|
|
|
resendingData.dataSize = sentIter->dataSize - 20;
|
|
|
|
std::memcpy(resendingData.data, sentIter->data + 20, resendingData.dataSize);
|
|
|
|
resendingData.flags = 0;
|
|
|
|
iter->second.priorityPkts.push(resendingData);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
--rseq;
|
|
|
|
}
|
2019-08-22 11:16:07 +00:00
|
|
|
|
2019-08-27 04:03:38 +00:00
|
|
|
// calculate sequence and ack
|
|
|
|
bool isOutOfOrder = false;
|
|
|
|
uint32_t diff = 0;
|
|
|
|
if(seqID > iter->second.rseq) {
|
|
|
|
diff = seqID - iter->second.rseq;
|
|
|
|
if(diff <= 0x7FFFFFFF) {
|
|
|
|
// sequence is more recent
|
|
|
|
iter->second.rseq = seqID;
|
|
|
|
iter->second.ack = (iter->second.ack >> diff) | 0x80000000;
|
|
|
|
} else {
|
|
|
|
// sequence is older, recalc diff
|
|
|
|
diff = 0xFFFFFFFF - seqID + 1 + iter->second.rseq;
|
|
|
|
if((iter->second.ack & (0x80000000 >> (diff - 1))) != 0) {
|
|
|
|
// already received packet
|
2019-08-29 03:56:09 +00:00
|
|
|
c->log(
|
|
|
|
UDPC_LoggingType::INFO,
|
|
|
|
"Received packet is already marked as received, ignoring it");
|
2019-08-27 04:03:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
iter->second.ack |= 0x80000000 >> (diff - 1);
|
|
|
|
isOutOfOrder = true;
|
|
|
|
}
|
|
|
|
} else if(seqID < iter->second.rseq) {
|
|
|
|
diff = iter->second.rseq - seqID;
|
|
|
|
if(diff <= 0x7FFFFFFF) {
|
|
|
|
// sequence is older
|
|
|
|
if((iter->second.ack & (0x80000000 >> (diff - 1))) != 0) {
|
|
|
|
// already received packet
|
2019-08-29 03:56:09 +00:00
|
|
|
c->log(
|
|
|
|
UDPC_LoggingType::INFO,
|
|
|
|
"Received packet is already marked as received, ignoring it");
|
2019-08-27 04:03:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
iter->second.ack |= 0x80000000 >> (diff - 1);
|
|
|
|
isOutOfOrder = true;
|
|
|
|
} else {
|
|
|
|
// sequence is more recent, recalc diff
|
|
|
|
diff = 0xFFFFFFFF - iter->second.rseq + 1 + seqID;
|
|
|
|
iter->second.rseq = seqID;
|
|
|
|
iter->second.ack = (iter->second.ack >> diff) | 0x80000000;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// already received packet
|
2019-08-29 03:56:09 +00:00
|
|
|
c->log(
|
|
|
|
UDPC_LoggingType::INFO,
|
|
|
|
"Received packet is already marked as received, ignoring it");
|
2019-08-27 04:03:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-29 03:56:09 +00:00
|
|
|
if(isOutOfOrder) {
|
|
|
|
c->log(
|
|
|
|
UDPC_LoggingType::VERBOSE,
|
|
|
|
"Received packet is out of order");
|
|
|
|
}
|
2019-08-27 04:03:38 +00:00
|
|
|
|
|
|
|
if(bytes > 20) {
|
2019-09-07 07:36:11 +00:00
|
|
|
UDPC_PacketInfo recPktInfo = UDPC::get_empty_pinfo();
|
2019-08-27 04:03:38 +00:00
|
|
|
std::memcpy(recPktInfo.data, c->recvBuf, bytes);
|
|
|
|
recPktInfo.dataSize = bytes;
|
|
|
|
recPktInfo.flags =
|
|
|
|
(isConnect ? 0x1 : 0)
|
|
|
|
| (isPing ? 0x2 : 0)
|
|
|
|
| (isNotRecChecked ? 0x4 : 0)
|
|
|
|
| (isResending ? 0x8 : 0);
|
2019-09-07 07:36:11 +00:00
|
|
|
recPktInfo.sender.addr = receivedData.sin6_addr;
|
|
|
|
recPktInfo.receiver.addr = in6addr_loopback;
|
|
|
|
recPktInfo.sender.port = receivedData.sin6_port;
|
|
|
|
recPktInfo.receiver.port = c->socketInfo.sin6_port;
|
2019-08-27 04:03:38 +00:00
|
|
|
|
|
|
|
if(iter->second.receivedPkts.size() == iter->second.receivedPkts.capacity()) {
|
2019-08-29 03:56:09 +00:00
|
|
|
c->log(
|
|
|
|
UDPC_LoggingType::WARNING,
|
|
|
|
"receivedPkts is full, removing oldest entry to make room");
|
2019-08-27 04:03:38 +00:00
|
|
|
iter->second.receivedPkts.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
iter->second.receivedPkts.push(recPktInfo);
|
|
|
|
} else if(bytes == 20) {
|
2019-08-29 03:56:09 +00:00
|
|
|
c->log(
|
|
|
|
UDPC_LoggingType::VERBOSE,
|
|
|
|
"Received packet has no payload");
|
2019-08-27 04:03:38 +00:00
|
|
|
}
|
2019-06-06 07:02:48 +00:00
|
|
|
}
|
|
|
|
|
2019-09-07 07:36:11 +00:00
|
|
|
void UDPC_client_initiate_connection(UDPC_HContext ctx, UDPC_ConnectionId connectionId) {
|
2019-08-29 02:07:24 +00:00
|
|
|
UDPC::Context *c = UDPC::verifyContext(ctx);
|
|
|
|
if(!c || !c->flags.test(1)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
UDPC::ConnectionData newCon(false, c);
|
|
|
|
|
|
|
|
// TODO make thread safe by using mutex
|
2019-09-07 07:36:11 +00:00
|
|
|
c->conMap.insert(std::make_pair(connectionId, std::move(newCon)));
|
|
|
|
auto addrConIter = c->addrConMap.find(connectionId.addr);
|
2019-08-29 02:07:24 +00:00
|
|
|
if(addrConIter == c->addrConMap.end()) {
|
|
|
|
auto insertResult = c->addrConMap.insert(std::make_pair(
|
2019-09-07 07:36:11 +00:00
|
|
|
connectionId.addr,
|
2019-09-03 03:06:46 +00:00
|
|
|
std::unordered_set<UDPC_ConnectionId, UDPC::ConnectionIdHasher>{}
|
2019-08-29 02:07:24 +00:00
|
|
|
));
|
|
|
|
assert(insertResult.second);
|
|
|
|
addrConIter = insertResult.first;
|
|
|
|
}
|
2019-09-07 07:36:11 +00:00
|
|
|
addrConIter->second.insert(connectionId);
|
2019-08-29 02:07:24 +00:00
|
|
|
}
|
|
|
|
|
2019-09-07 07:36:11 +00:00
|
|
|
int UDPC_get_queue_send_available(UDPC_HContext ctx, UDPC_ConnectionId connectionId) {
|
2019-07-21 04:45:42 +00:00
|
|
|
UDPC::Context *c = UDPC::verifyContext(ctx);
|
|
|
|
if(!c) {
|
2019-06-06 07:02:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2019-08-27 05:06:27 +00:00
|
|
|
|
2019-09-07 07:36:11 +00:00
|
|
|
auto iter = c->conMap.find(connectionId);
|
2019-08-27 05:06:27 +00:00
|
|
|
if(iter != c->conMap.end()) {
|
|
|
|
return iter->second.sendPkts.capacity() - iter->second.sendPkts.size();
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2019-06-06 07:02:48 +00:00
|
|
|
}
|
|
|
|
|
2019-09-07 07:36:11 +00:00
|
|
|
void UDPC_queue_send(UDPC_HContext ctx, UDPC_ConnectionId destinationId,
|
2019-06-06 07:42:07 +00:00
|
|
|
uint32_t isChecked, void *data, uint32_t size) {
|
2019-08-27 05:06:27 +00:00
|
|
|
if(size == 0 || !data) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-21 04:45:42 +00:00
|
|
|
UDPC::Context *c = UDPC::verifyContext(ctx);
|
|
|
|
if(!c) {
|
2019-06-06 07:02:48 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-08-27 05:06:27 +00:00
|
|
|
|
2019-09-07 07:36:11 +00:00
|
|
|
auto iter = c->conMap.find(destinationId);
|
2019-08-27 05:06:27 +00:00
|
|
|
if(iter == c->conMap.end()) {
|
2019-08-29 03:56:09 +00:00
|
|
|
c->log(
|
|
|
|
UDPC_LoggingType::ERROR,
|
|
|
|
"Failed to add packet to queue, no established connection "
|
|
|
|
"with recipient");
|
2019-08-27 05:06:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-07 07:36:11 +00:00
|
|
|
UDPC_PacketInfo sendInfo = UDPC::get_empty_pinfo();
|
2019-08-27 05:06:27 +00:00
|
|
|
std::memcpy(sendInfo.data, data, size);
|
|
|
|
sendInfo.dataSize = size;
|
2019-09-07 07:36:11 +00:00
|
|
|
sendInfo.sender.addr = in6addr_loopback;
|
|
|
|
sendInfo.sender.port = c->socketInfo.sin6_port;
|
|
|
|
sendInfo.receiver.addr = destinationId.addr;
|
2019-09-03 03:06:46 +00:00
|
|
|
sendInfo.receiver.port = iter->second.port;
|
2019-08-27 05:06:27 +00:00
|
|
|
sendInfo.flags = (isChecked ? 0x0 : 0x4);
|
|
|
|
|
|
|
|
iter->second.sendPkts.push(sendInfo);
|
2019-06-06 07:02:48 +00:00
|
|
|
}
|
|
|
|
|
2019-08-30 03:03:26 +00:00
|
|
|
int UDPC_set_accept_new_connections(UDPC_HContext ctx, int isAccepting) {
|
2019-07-21 04:45:42 +00:00
|
|
|
UDPC::Context *c = UDPC::verifyContext(ctx);
|
|
|
|
if(!c) {
|
2019-06-06 07:02:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return c->isAcceptNewConnections.exchange(isAccepting == 0 ? false : true);
|
|
|
|
}
|
|
|
|
|
2019-09-07 07:36:11 +00:00
|
|
|
int UDPC_drop_connection(UDPC_HContext ctx, UDPC_ConnectionId connectionId, bool dropAllWithAddr) {
|
2019-07-21 04:45:42 +00:00
|
|
|
UDPC::Context *c = UDPC::verifyContext(ctx);
|
|
|
|
if(!c) {
|
2019-06-06 07:02:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2019-08-27 05:06:27 +00:00
|
|
|
|
2019-09-07 07:36:11 +00:00
|
|
|
if(dropAllWithAddr) {
|
|
|
|
auto addrConIter = c->addrConMap.find(connectionId.addr);
|
2019-08-28 07:38:14 +00:00
|
|
|
if(addrConIter != c->addrConMap.end()) {
|
2019-09-07 07:36:11 +00:00
|
|
|
for(auto identIter = addrConIter->second.begin();
|
|
|
|
identIter != addrConIter->second.end();
|
|
|
|
++identIter) {
|
|
|
|
auto conIter = c->conMap.find(*identIter);
|
|
|
|
assert(conIter != c->conMap.end());
|
|
|
|
if(conIter->second.flags.test(4)) {
|
|
|
|
c->idMap.erase(conIter->second.id);
|
|
|
|
}
|
|
|
|
c->conMap.erase(conIter);
|
2019-08-28 07:38:14 +00:00
|
|
|
}
|
2019-09-07 07:36:11 +00:00
|
|
|
c->addrConMap.erase(addrConIter);
|
|
|
|
return 1;
|
2019-08-28 07:38:14 +00:00
|
|
|
}
|
2019-09-07 07:36:11 +00:00
|
|
|
} else {
|
|
|
|
auto iter = c->conMap.find(connectionId);
|
|
|
|
if(iter != c->conMap.end()) {
|
|
|
|
if(iter->second.flags.test(4)) {
|
|
|
|
c->idMap.erase(iter->second.id);
|
2019-08-28 07:38:14 +00:00
|
|
|
}
|
2019-09-07 07:36:11 +00:00
|
|
|
auto addrConIter = c->addrConMap.find(connectionId.addr);
|
|
|
|
if(addrConIter != c->addrConMap.end()) {
|
|
|
|
addrConIter->second.erase(connectionId);
|
|
|
|
if(addrConIter->second.empty()) {
|
|
|
|
c->addrConMap.erase(addrConIter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c->conMap.erase(iter);
|
|
|
|
return 1;
|
2019-08-28 07:38:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-08-30 03:03:26 +00:00
|
|
|
uint32_t UDPC_set_protocol_id(UDPC_HContext ctx, uint32_t id) {
|
2019-07-21 04:45:42 +00:00
|
|
|
UDPC::Context *c = UDPC::verifyContext(ctx);
|
|
|
|
if(!c) {
|
2019-06-06 07:02:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return c->protocolID.exchange(id);
|
|
|
|
}
|
|
|
|
|
2019-08-30 03:03:26 +00:00
|
|
|
UDPC_LoggingType set_logging_type(UDPC_HContext ctx, UDPC_LoggingType loggingType) {
|
2019-07-21 04:45:42 +00:00
|
|
|
UDPC::Context *c = UDPC::verifyContext(ctx);
|
|
|
|
if(!c) {
|
2019-08-30 05:57:24 +00:00
|
|
|
return UDPC_LoggingType::SILENT;
|
2019-06-06 07:02:48 +00:00
|
|
|
}
|
|
|
|
return static_cast<UDPC_LoggingType>(c->loggingType.exchange(loggingType));
|
|
|
|
}
|
|
|
|
|
2019-08-30 03:03:26 +00:00
|
|
|
UDPC_PacketInfo UDPC_get_received(UDPC_HContext ctx) {
|
2019-07-21 04:45:42 +00:00
|
|
|
UDPC::Context *c = UDPC::verifyContext(ctx);
|
|
|
|
if(!c) {
|
2019-09-07 07:36:11 +00:00
|
|
|
return UDPC::get_empty_pinfo();
|
2019-06-06 07:02:48 +00:00
|
|
|
}
|
|
|
|
// TODO impl
|
2019-09-07 07:36:11 +00:00
|
|
|
return UDPC::get_empty_pinfo();
|
2019-06-06 07:02:48 +00:00
|
|
|
}
|
|
|
|
|
2019-09-07 07:36:11 +00:00
|
|
|
const char *UDPC_atostr(UDPC_HContext ctx, UDPC_ConnectionId connectionId) {
|
2019-07-21 04:45:42 +00:00
|
|
|
UDPC::Context *c = UDPC::verifyContext(ctx);
|
|
|
|
if(!c) {
|
2019-06-06 07:02:48 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2019-09-03 07:19:47 +00:00
|
|
|
const uint32_t headIndex =
|
|
|
|
c->atostrBufIndex.fetch_add(UDPC_ATOSTR_BUFSIZE) % UDPC_ATOSTR_SIZE;
|
|
|
|
uint32_t index = headIndex;
|
2019-09-07 07:36:11 +00:00
|
|
|
bool usedDouble = false;
|
|
|
|
|
|
|
|
for(unsigned int i = 0; i < 16; ++i) {
|
|
|
|
if(i != 0 && i % 2 == 0) {
|
|
|
|
if(headIndex - index > 1 && c->atostrBuf[index - 1] == ':') {
|
|
|
|
if(usedDouble) {
|
|
|
|
if(c->atostrBuf[index - 2] != ':') {
|
|
|
|
c->atostrBuf[index++] = '0';
|
|
|
|
c->atostrBuf[index++] = ':';
|
|
|
|
} else {
|
|
|
|
// continue use of double :, do nothing here
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
usedDouble = true;
|
|
|
|
c->atostrBuf[index++] = ':';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c->atostrBuf[index++] = ':';
|
|
|
|
}
|
2019-06-06 07:02:48 +00:00
|
|
|
}
|
|
|
|
|
2019-09-07 07:36:11 +00:00
|
|
|
if(connectionId.addr.s6_addr[i] == 0) {
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
std::stringstream sstream;
|
|
|
|
sstream << std::hex << (unsigned int) connectionId.addr.s6_addr[i];
|
|
|
|
std::string out(sstream.str());
|
|
|
|
if(out.size() == 1) {
|
|
|
|
if(out[0] != '0') {
|
|
|
|
std::memcpy(c->atostrBuf + index, out.c_str(), 1);
|
|
|
|
++index;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
std::memcpy(c->atostrBuf + index, out.c_str(), 2);
|
|
|
|
index += 2;
|
|
|
|
}
|
2019-06-06 07:02:48 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-07 07:36:11 +00:00
|
|
|
if(c->atostrBuf[index - 1] == ':'
|
|
|
|
&& (headIndex - index <= 2 || c->atostrBuf[index - 2] != ':')) {
|
|
|
|
c->atostrBuf[index++] = '0';
|
|
|
|
}
|
|
|
|
|
2019-06-06 07:02:48 +00:00
|
|
|
c->atostrBuf[index] = 0;
|
|
|
|
|
2019-09-03 07:19:47 +00:00
|
|
|
return c->atostrBuf + headIndex;
|
2019-06-06 07:02:48 +00:00
|
|
|
}
|
|
|
|
|
2019-09-07 07:36:11 +00:00
|
|
|
struct in6_addr UDPC_strtoa(const char *addrStr) {
|
|
|
|
struct in6_addr result{{0}};
|
|
|
|
unsigned int index = 0;
|
|
|
|
unsigned int strIndex = 0;
|
|
|
|
int doubleColonIndex = -1;
|
|
|
|
unsigned char bytes[2] = {0, 0};
|
|
|
|
unsigned char bytesState = 0;
|
|
|
|
bool prevColon = false;
|
|
|
|
|
|
|
|
const auto checkInc = [&result, &index, &bytes] () -> bool {
|
|
|
|
if(index < 15) {
|
|
|
|
result.s6_addr[index++] = bytes[0];
|
|
|
|
result.s6_addr[index++] = bytes[1];
|
|
|
|
bytes[0] = 0;
|
|
|
|
bytes[1] = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
while(addrStr[strIndex] != '\0') {
|
|
|
|
if(addrStr[strIndex] >= '0' && addrStr[strIndex] <= '9') {
|
|
|
|
switch(bytesState) {
|
|
|
|
case 0:
|
|
|
|
bytes[0] = (addrStr[strIndex] - '0');
|
|
|
|
bytesState = 1;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
bytes[0] = (bytes[0] << 4) | (addrStr[strIndex] - '0');
|
|
|
|
bytesState = 2;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
bytes[1] = (addrStr[strIndex] - '0');
|
|
|
|
bytesState = 3;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
bytes[1] = (bytes[1] << 4) | (addrStr[strIndex] - '0');
|
|
|
|
bytesState = 0;
|
|
|
|
if(checkInc()) {
|
|
|
|
return in6addr_loopback;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return in6addr_loopback;
|
|
|
|
}
|
|
|
|
prevColon = false;
|
|
|
|
} else if(addrStr[strIndex] >= 'a' && addrStr[strIndex] <= 'f') {
|
|
|
|
switch(bytesState) {
|
|
|
|
case 0:
|
|
|
|
bytes[0] = (addrStr[strIndex] - 'a' + 10);
|
|
|
|
bytesState = 1;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
bytes[0] = (bytes[0] << 4) | (addrStr[strIndex] - 'a' + 10);
|
|
|
|
bytesState = 2;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
bytes[1] = (addrStr[strIndex] - 'a' + 10);
|
|
|
|
bytesState = 3;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
bytes[1] = (bytes[1] << 4) | (addrStr[strIndex] - 'a' + 10);
|
|
|
|
bytesState = 0;
|
|
|
|
if(checkInc()) {
|
|
|
|
return in6addr_loopback;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return in6addr_loopback;
|
|
|
|
}
|
|
|
|
prevColon = false;
|
|
|
|
} else if(addrStr[strIndex] >= 'A' && addrStr[strIndex] <= 'F') {
|
|
|
|
switch(bytesState) {
|
|
|
|
case 0:
|
|
|
|
bytes[0] = (addrStr[strIndex] - 'A' + 10);
|
|
|
|
bytesState = 1;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
bytes[0] = (bytes[0] << 4) | (addrStr[strIndex] - 'A' + 10);
|
|
|
|
bytesState = 2;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
bytes[1] = (addrStr[strIndex] - 'A' + 10);
|
|
|
|
bytesState = 3;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
bytes[1] = (bytes[1] << 4) | (addrStr[strIndex] - 'A' + 10);
|
|
|
|
bytesState = 0;
|
|
|
|
if(checkInc()) {
|
|
|
|
return in6addr_loopback;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return in6addr_loopback;
|
|
|
|
}
|
|
|
|
prevColon = false;
|
|
|
|
} else if(addrStr[strIndex] == ':') {
|
|
|
|
switch(bytesState) {
|
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
bytes[1] = bytes[0];
|
|
|
|
bytes[0] = 0;
|
|
|
|
if(checkInc()) {
|
|
|
|
return in6addr_loopback;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
bytes[1] |= (bytes[0] & 0xF) << 4;
|
|
|
|
bytes[0] = bytes[0] >> 4;
|
|
|
|
if(checkInc()) {
|
|
|
|
return in6addr_loopback;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return in6addr_loopback;
|
|
|
|
}
|
|
|
|
bytesState = 0;
|
|
|
|
if(prevColon) {
|
|
|
|
if(doubleColonIndex >= 0) {
|
|
|
|
return in6addr_loopback;
|
|
|
|
} else {
|
|
|
|
doubleColonIndex = index;
|
|
|
|
}
|
2019-07-07 05:44:37 +00:00
|
|
|
} else {
|
2019-09-07 07:36:11 +00:00
|
|
|
prevColon = true;
|
2019-07-07 05:44:37 +00:00
|
|
|
}
|
2019-06-06 07:42:07 +00:00
|
|
|
} else {
|
2019-09-07 07:36:11 +00:00
|
|
|
return in6addr_loopback;
|
|
|
|
}
|
|
|
|
|
|
|
|
++strIndex;
|
|
|
|
}
|
|
|
|
switch(bytesState) {
|
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
bytes[1] = bytes[0];
|
|
|
|
bytes[0] = 0;
|
|
|
|
if(checkInc()) {
|
|
|
|
return in6addr_loopback;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
bytes[1] |= (bytes[0] & 0xF) << 4;
|
|
|
|
bytes[0] = bytes[0] >> 4;
|
|
|
|
if(checkInc()) {
|
|
|
|
return in6addr_loopback;
|
2019-06-06 07:02:48 +00:00
|
|
|
}
|
2019-09-07 07:36:11 +00:00
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return in6addr_loopback;
|
2019-06-06 07:02:48 +00:00
|
|
|
}
|
|
|
|
|
2019-09-07 07:36:11 +00:00
|
|
|
if(doubleColonIndex >= 0) {
|
|
|
|
strIndex = 16 - index;
|
|
|
|
if(strIndex < 2) {
|
|
|
|
return in6addr_loopback;
|
|
|
|
}
|
|
|
|
for(unsigned int i = 16; i-- > (unsigned int)doubleColonIndex + strIndex; ) {
|
|
|
|
result.s6_addr[i] = result.s6_addr[i - strIndex];
|
|
|
|
result.s6_addr[i - strIndex] = 0;
|
2019-07-07 05:44:37 +00:00
|
|
|
}
|
2019-06-06 07:02:48 +00:00
|
|
|
}
|
2019-09-07 07:36:11 +00:00
|
|
|
|
|
|
|
return result;
|
2019-06-06 07:02:48 +00:00
|
|
|
}
|