Skip to content

Commit e9495e9

Browse files
committed
cache ip: yurots forget IP of broken connections
1 parent f7d41e0 commit e9495e9

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

ots/source/protocol.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,28 @@ Protocol::~Protocol()
5050
player = NULL;
5151
game = NULL;
5252
}
53-
53+
#ifdef HHB_STATUS_MAX_4_PER_IP
54+
unsigned long Protocol::getIP() const
55+
{
56+
uint32_t ret = 0;
57+
sockaddr_in sain;
58+
socklen_t salen = sizeof(sockaddr_in);
59+
if (getpeername(s, (sockaddr*)&sain, &salen) == 0)
60+
{
61+
#if defined WIN32 || defined __WINDOWS__
62+
ret = sain.sin_addr.S_un.S_addr;
63+
#else
64+
ret = sain.sin_addr.s_addr;
65+
#endif
66+
}
67+
if(ret == 0){
68+
return this->cached_ip;
69+
}else {
70+
this->cached_ip = ret;
71+
return ret;
72+
}
73+
}
74+
#else
5475
unsigned long Protocol::getIP() const
5576
{
5677
sockaddr_in sain;
@@ -66,7 +87,7 @@ unsigned long Protocol::getIP() const
6687

6788
return 0;
6889
}
69-
90+
#endif
7091
void Protocol::setPlayer(Player* p)
7192
{
7293
player = p;

ots/source/protocol.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ class Protocol
3939
virtual ~Protocol();
4040

4141
void setPlayer(Player* p);
42+
#ifdef HHB_STATUS_MAX_4_PER_IP
43+
uint32_t cached_ip=0;
44+
#endif
45+
4246
unsigned long getIP() const;
4347

4448
virtual bool CanSee(int x, int y, int z) const = 0;

ots/source/status.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,18 @@ std::string Status::getStatusString(){
115115
#ifdef HHB_STATUS_MAX_4_PER_IP
116116
{
117117
size_t otservlist_legal_count = 0;
118+
#if __cplusplus >= 201703
119+
// this only works for >= c++17
118120
for (const auto& [ip, player_count] : this->ip_counts) {
119121
(void)ip;
120122
otservlist_legal_count += ( player_count > 4 ? 4 : player_count);
121123
}
124+
#else
125+
// this works on >=c++11
126+
for (const auto& n : this->ip_counts) {
127+
otservlist_legal_count += ( n.second > 4 ? 4 : n.second);
128+
}
129+
#endif
122130
ss << otservlist_legal_count;
123131
}
124132
#else

0 commit comments

Comments
 (0)