diff --git a/tunnel.cpp b/tunnel.cpp index e938942..a003202 100644 --- a/tunnel.cpp +++ b/tunnel.cpp @@ -21,6 +21,7 @@ Tunnel::Tunnel(): udpKeepAliveDuration(udpSilenceDuration/32), timeQwant(1200000000), pollDuration(5000000), + floatConnections(), udpSockId(-1), tcpSockId(-1), nextTermSendTime() // will be set by initTime() called by initSpeed() @@ -109,7 +110,7 @@ void Tunnel::close() bool Tunnel::recvUdpPacket() { - ConnId id; + ConnId id, floatId; unsigned char raw[CRYPT_PACKET_SIZE] = {}; int rawSize = Socket::udpRecvAsync(udpSockId, id.address, raw, sizeof(raw)); if (rawSize <= 0) @@ -137,9 +138,11 @@ bool Tunnel::recvUdpPacket() { return true; } - id.id = packet.connId; - ConnMap::iterator i = connections.find(id); - if (i == connections.end() && cmd == CMD_DATA && remoteTcpAddress.port && !closedConnections.count(id)) { + floatId.id = id.id = packet.connId; + ConnId &searchId = floatConnections ? floatId : id; + + ConnMap::iterator i = connections.find(searchId); + if (i == connections.end() && cmd == CMD_DATA && remoteTcpAddress.port && !closedConnections.count(searchId)) { logf("Open incoming connection from UDP: "); id.address.print(); logf(", id: %u\n", id.id); @@ -147,7 +150,7 @@ bool Tunnel::recvUdpPacket() { if (sockId < 0) { logf("cannot init TCP connection, cancelled\n"); } else { - i = connections.emplace(id, Connection::Args(*this, id, sockId)).first; + i = connections.emplace(searchId, Connection::Args(*this, id, sockId)).first; } } @@ -258,13 +261,15 @@ bool Tunnel::iteration() { Address address; int sockId = Socket::tcpAccept(tcpSockId, address); if (sockId >= 0) { - ConnId id; + ConnId id, floatId; id.address = remoteUdpAddress; - id.id = crypt.random(); + floatId.id = id.id = crypt.random(); logf("Open incoming connection from TCP: "); address.print(); logf(", id: %u\n", id.id); - Connection &conn = connections.emplace(id, Connection::Args(*this, id, sockId)).first->second; + Connection &conn = connections.emplace( + floatConnections ? floatId : id, + Connection::Args(*this, id, sockId) ).first->second; conn.tcpRecvQueue.readFromTcp(); } } @@ -325,7 +330,7 @@ bool Tunnel::iteration() { Socket::close(conn.tcpSockId); conn.tcpSockId = -1; logf("Close connection, id: %u\n", conn.id.id); - closedConnections[conn.id] = time + 2*udpSilenceDuration; + closedConnections[j->first] = time + 2*udpSilenceDuration; connections.erase(j); continue; } @@ -335,14 +340,14 @@ bool Tunnel::iteration() { conn.udpActive = false; termQueue.insert(conn.id); logf("Close connection, id: %u\n", conn.id.id); - closedConnections[conn.id] = time + 2*udpSilenceDuration; + closedConnections[j->first] = time + 2*udpSilenceDuration; connections.erase(j); continue; } if (!conn.udpActive && conn.tcpSockId < 0) { logf("Close connection, id: %u\n", conn.id.id); - closedConnections[conn.id] = time + 2*udpSilenceDuration; + closedConnections[j->first] = time + 2*udpSilenceDuration; connections.erase(j); continue; } diff --git a/tunnel.h b/tunnel.h index bf84906..8ef8c3a 100644 --- a/tunnel.h +++ b/tunnel.h @@ -27,6 +27,7 @@ public: Time udpKeepAliveDuration; Time timeQwant; Time pollDuration; + bool floatConnections; ConnMap connections; ClosedConnMap closedConnections;