Skip to content

Commit a92b9ab

Browse files
committed
Remove expired mapped address using last activity
1 parent 5cacd48 commit a92b9ab

File tree

4 files changed

+68
-46
lines changed

4 files changed

+68
-46
lines changed

lib/enet/protocol.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
#include "enet/time.h"
1010
#include "enet/enet.h"
1111

12-
#ifdef ENABLE_IPV6
13-
extern void removeMappedAddress(const ENetAddress* ea);
14-
#endif
15-
1612
static size_t commandSizes [ENET_PROTOCOL_COMMAND_COUNT] =
1713
{
1814
0,
@@ -88,10 +84,6 @@ enet_protocol_dispatch_incoming_commands (ENetHost * host, ENetEvent * event)
8884
event -> peer = peer;
8985
event -> data = peer -> eventData;
9086

91-
#ifdef ENABLE_IPV6
92-
removeMappedAddress(&peer->address);
93-
#endif
94-
9587
enet_peer_reset (peer);
9688

9789
return 1;
@@ -156,10 +148,6 @@ enet_protocol_notify_disconnect (ENetHost * host, ENetPeer * peer, ENetEvent * e
156148
event -> peer = peer;
157149
event -> data = 0;
158150

159-
#ifdef ENABLE_IPV6
160-
removeMappedAddress(&peer->address);
161-
#endif
162-
163151
enet_peer_reset (peer);
164152
}
165153
else

src/network/stk_host.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,7 @@ void STKHost::mainLoop()
881881
uint64_t last_ping_time = StkTime::getMonoTimeMs();
882882
uint64_t last_update_speed_time = StkTime::getMonoTimeMs();
883883
uint64_t last_ping_time_update_for_client = StkTime::getMonoTimeMs();
884+
uint64_t last_disconnect_time_update = StkTime::getMonoTimeMs();
884885
std::map<std::string, uint64_t> ctp;
885886
while (m_exit_timeout.load() > StkTime::getMonoTimeMs())
886887
{
@@ -920,6 +921,15 @@ void STKHost::mainLoop()
920921

921922
if (is_server)
922923
{
924+
if (isIPV6() &&
925+
last_disconnect_time_update < StkTime::getMonoTimeMs())
926+
{
927+
// Check per 20 second, don't need to check client because it
928+
// only has 1 peer
929+
last_disconnect_time_update = StkTime::getMonoTimeMs() + 20000;
930+
removeDisconnectedMappedAddress();
931+
}
932+
923933
std::unique_lock<std::mutex> peer_lock(m_peers_mutex);
924934
const float timeout = ServerConfig::m_validation_timeout;
925935
bool need_ping = false;

src/network/unix_ipv6.cpp

Lines changed: 57 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -152,22 +152,39 @@ std::string getIPV6ReadableFromMappedAddress(const ENetAddress* ea)
152152
return "";
153153
} // getIPV6ReadableFromMappedAddress
154154

155+
// ----------------------------------------------------------------------------
156+
void removeDisconnectedMappedAddress()
157+
{
158+
} // removeDisconnectedMappedAddress
159+
155160
#else
156161

157162
#include "network/unix_ipv6.hpp"
158163
#include "network/transport_address.hpp"
159164
#include "utils/string_utils.hpp"
160165
#include "utils/log.hpp"
166+
#include "utils/time.hpp"
161167
#include "utils/types.hpp"
162168

163169
#include <algorithm>
164-
#include <utility>
165170
#include <vector>
166171

167172
// ============================================================================
168173
uint32_t g_mapped_ipv6_used;
169174
int g_ipv6;
170-
std::vector<std::pair<ENetAddress, struct sockaddr_in6> > g_mapped_ips;
175+
struct MappedAddress
176+
{
177+
ENetAddress m_addr;
178+
struct sockaddr_in6 m_in6;
179+
uint64_t m_last_activity;
180+
MappedAddress(const ENetAddress& addr, const struct sockaddr_in6& in6)
181+
{
182+
m_addr = addr;
183+
m_in6 = in6;
184+
m_last_activity = StkTime::getMonoTimeMs();
185+
}
186+
};
187+
std::vector<MappedAddress> g_mapped_ips;
171188
// ============================================================================
172189
int isIPV6()
173190
{
@@ -189,39 +206,18 @@ void unixInitialize()
189206
g_mapped_ips.clear();
190207
} // unixInitialize
191208

192-
// ----------------------------------------------------------------------------
193-
/* Called when a peer is disconnected and we remove its reference to the ipv6.
194-
*/
195-
void removeMappedAddress(const ENetAddress* ea)
196-
{
197-
auto it = std::find_if(g_mapped_ips.begin(), g_mapped_ips.end(),
198-
[ea](const std::pair<ENetAddress, struct sockaddr_in6>& addr)
199-
{
200-
return ea->host == addr.first.host && ea->port == addr.first.port;
201-
});
202-
if (it != g_mapped_ips.end())
203-
{
204-
TransportAddress addr(it->first);
205-
Log::debug("IPV6", "Removing %s, ipv4 address %s.",
206-
getIPV6ReadableFromIn6(&it->second).c_str(),
207-
addr.toString().c_str());
208-
g_mapped_ips.erase(it);
209-
Log::debug("IPV6", "Mapped address size now: %d.",
210-
g_mapped_ips.size());
211-
}
212-
} // removeMappedAddress
213-
214209
// ----------------------------------------------------------------------------
215210
std::string getIPV6ReadableFromMappedAddress(const ENetAddress* ea)
216211
{
217212
std::string result;
218213
auto it = std::find_if(g_mapped_ips.begin(), g_mapped_ips.end(),
219-
[ea](const std::pair<ENetAddress, struct sockaddr_in6>& addr)
214+
[ea](const MappedAddress& addr)
220215
{
221-
return ea->host == addr.first.host && ea->port == addr.first.port;
216+
return ea->host == addr.m_addr.host &&
217+
ea->port == addr.m_addr.port;
222218
});
223219
if (it != g_mapped_ips.end())
224-
result = getIPV6ReadableFromIn6(&it->second);
220+
result = getIPV6ReadableFromIn6(&it->m_in6);
225221
return result;
226222
} // getIPV6ReadableFromMappedAddress
227223

@@ -243,12 +239,16 @@ void addMappedAddress(const ENetAddress* ea, const struct sockaddr_in6* in6)
243239
void getIPV6FromMappedAddress(const ENetAddress* ea, struct sockaddr_in6* in6)
244240
{
245241
auto it = std::find_if(g_mapped_ips.begin(), g_mapped_ips.end(),
246-
[ea](const std::pair<ENetAddress, struct sockaddr_in6>& addr)
242+
[ea](const MappedAddress& addr)
247243
{
248-
return ea->host == addr.first.host && ea->port == addr.first.port;
244+
return ea->host == addr.m_addr.host &&
245+
ea->port == addr.m_addr.port;
249246
});
250247
if (it != g_mapped_ips.end())
251-
memcpy(in6, &it->second, sizeof(struct sockaddr_in6));
248+
{
249+
it->m_last_activity = StkTime::getMonoTimeMs();
250+
memcpy(in6, &it->m_in6, sizeof(struct sockaddr_in6));
251+
}
252252
else
253253
memset(in6, 0, sizeof(struct sockaddr_in6));
254254
} // getIPV6FromMappedAddress
@@ -260,13 +260,14 @@ void getIPV6FromMappedAddress(const ENetAddress* ea, struct sockaddr_in6* in6)
260260
void getMappedFromIPV6(const struct sockaddr_in6* in6, ENetAddress* ea)
261261
{
262262
auto it = std::find_if(g_mapped_ips.begin(), g_mapped_ips.end(),
263-
[in6](const std::pair<ENetAddress, struct sockaddr_in6>& addr)
263+
[in6](const MappedAddress& addr)
264264
{
265-
return sameIPV6(in6, &addr.second);
265+
return sameIPV6(in6, &addr.m_in6);
266266
});
267267
if (it != g_mapped_ips.end())
268268
{
269-
*ea = it->first;
269+
it->m_last_activity = StkTime::getMonoTimeMs();
270+
*ea = it->m_addr;
270271
return;
271272
}
272273

@@ -296,4 +297,27 @@ void getMappedFromIPV6(const struct sockaddr_in6* in6, ENetAddress* ea)
296297
}
297298
} // getMappedFromIPV6
298299

300+
// ----------------------------------------------------------------------------
301+
/* Called when a peer is expired (no activity for 20 seconds) and we remove its
302+
* reference to the ipv6.
303+
*/
304+
void removeDisconnectedMappedAddress()
305+
{
306+
for (auto it = g_mapped_ips.begin(); it != g_mapped_ips.end();)
307+
{
308+
if (it->m_last_activity + 20000 < StkTime::getMonoTimeMs())
309+
{
310+
TransportAddress addr(it->m_addr);
311+
Log::debug("IPV6", "Removing expired %s, ipv4 address %s.",
312+
getIPV6ReadableFromIn6(&it->m_in6).c_str(),
313+
addr.toString().c_str());
314+
it = g_mapped_ips.erase(it);
315+
Log::debug("IPV6", "Mapped address size now: %d.",
316+
g_mapped_ips.size());
317+
}
318+
else
319+
it++;
320+
}
321+
} // removeDisconnectedMappedAddress
322+
299323
#endif

src/network/unix_ipv6.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ extern "C" {
2424
int isIPV6();
2525
void setIPV6(int val);
2626
void unixInitialize();
27-
void removeMappedAddress(const ENetAddress* ea);
2827
void getIPV6FromMappedAddress(const ENetAddress* ea, struct sockaddr_in6* in6);
2928
void getMappedFromIPV6(const struct sockaddr_in6* in6, ENetAddress* ea);
3029
void addMappedAddress(const ENetAddress* ea, const struct sockaddr_in6* in6);
@@ -37,3 +36,4 @@ std::string getIPV6ReadableFromMappedAddress(const ENetAddress* ea);
3736
std::string getIPV6ReadableFromIn6(const struct sockaddr_in6* in);
3837
bool sameIPV6(const struct sockaddr_in6* in_1,
3938
const struct sockaddr_in6* in_2);
39+
void removeDisconnectedMappedAddress();

0 commit comments

Comments
 (0)